18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * sorttable.c: Sort the kernel's table
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Added ORC unwind tables sort support and other updates:
68c2ecf20Sopenharmony_ci * Copyright (C) 1999-2019 Alibaba Group Holding Limited. by:
78c2ecf20Sopenharmony_ci * Shile Zhang <shile.zhang@linux.alibaba.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright 2011 - 2012 Cavium, Inc.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Based on code taken from recortmcount.c which is:
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>.  All rights reserved.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Restructured to fit Linux format, as well as other updates:
168c2ecf20Sopenharmony_ci * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * Strategy: alter the vmlinux file in-place.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <sys/types.h>
248c2ecf20Sopenharmony_ci#include <sys/mman.h>
258c2ecf20Sopenharmony_ci#include <sys/stat.h>
268c2ecf20Sopenharmony_ci#include <getopt.h>
278c2ecf20Sopenharmony_ci#include <elf.h>
288c2ecf20Sopenharmony_ci#include <fcntl.h>
298c2ecf20Sopenharmony_ci#include <stdio.h>
308c2ecf20Sopenharmony_ci#include <stdlib.h>
318c2ecf20Sopenharmony_ci#include <string.h>
328c2ecf20Sopenharmony_ci#include <unistd.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include <tools/be_byteshift.h>
358c2ecf20Sopenharmony_ci#include <tools/le_byteshift.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#ifndef EM_ARCOMPACT
388c2ecf20Sopenharmony_ci#define EM_ARCOMPACT	93
398c2ecf20Sopenharmony_ci#endif
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#ifndef EM_XTENSA
428c2ecf20Sopenharmony_ci#define EM_XTENSA	94
438c2ecf20Sopenharmony_ci#endif
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#ifndef EM_AARCH64
468c2ecf20Sopenharmony_ci#define EM_AARCH64	183
478c2ecf20Sopenharmony_ci#endif
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#ifndef EM_MICROBLAZE
508c2ecf20Sopenharmony_ci#define EM_MICROBLAZE	189
518c2ecf20Sopenharmony_ci#endif
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#ifndef EM_ARCV2
548c2ecf20Sopenharmony_ci#define EM_ARCV2	195
558c2ecf20Sopenharmony_ci#endif
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#ifndef EM_LOONGARCH
588c2ecf20Sopenharmony_ci#define EM_LOONGARCH	258
598c2ecf20Sopenharmony_ci#endif
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic uint32_t (*r)(const uint32_t *);
628c2ecf20Sopenharmony_cistatic uint16_t (*r2)(const uint16_t *);
638c2ecf20Sopenharmony_cistatic uint64_t (*r8)(const uint64_t *);
648c2ecf20Sopenharmony_cistatic void (*w)(uint32_t, uint32_t *);
658c2ecf20Sopenharmony_cistatic void (*w2)(uint16_t, uint16_t *);
668c2ecf20Sopenharmony_cistatic void (*w8)(uint64_t, uint64_t *);
678c2ecf20Sopenharmony_citypedef void (*table_sort_t)(char *, int);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/*
708c2ecf20Sopenharmony_ci * Get the whole file as a programming convenience in order to avoid
718c2ecf20Sopenharmony_ci * malloc+lseek+read+free of many pieces.  If successful, then mmap
728c2ecf20Sopenharmony_ci * avoids copying unused pieces; else just read the whole file.
738c2ecf20Sopenharmony_ci * Open for both read and write.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_cistatic void *mmap_file(char const *fname, size_t *size)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	int fd;
788c2ecf20Sopenharmony_ci	struct stat sb;
798c2ecf20Sopenharmony_ci	void *addr = NULL;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	fd = open(fname, O_RDWR);
828c2ecf20Sopenharmony_ci	if (fd < 0) {
838c2ecf20Sopenharmony_ci		perror(fname);
848c2ecf20Sopenharmony_ci		return NULL;
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci	if (fstat(fd, &sb) < 0) {
878c2ecf20Sopenharmony_ci		perror(fname);
888c2ecf20Sopenharmony_ci		goto out;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci	if (!S_ISREG(sb.st_mode)) {
918c2ecf20Sopenharmony_ci		fprintf(stderr, "not a regular file: %s\n", fname);
928c2ecf20Sopenharmony_ci		goto out;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
968c2ecf20Sopenharmony_ci	if (addr == MAP_FAILED) {
978c2ecf20Sopenharmony_ci		fprintf(stderr, "Could not mmap file: %s\n", fname);
988c2ecf20Sopenharmony_ci		goto out;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	*size = sb.st_size;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciout:
1048c2ecf20Sopenharmony_ci	close(fd);
1058c2ecf20Sopenharmony_ci	return addr;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic uint32_t rbe(const uint32_t *x)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	return get_unaligned_be32(x);
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic uint16_t r2be(const uint16_t *x)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	return get_unaligned_be16(x);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic uint64_t r8be(const uint64_t *x)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	return get_unaligned_be64(x);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic uint32_t rle(const uint32_t *x)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	return get_unaligned_le32(x);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic uint16_t r2le(const uint16_t *x)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	return get_unaligned_le16(x);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic uint64_t r8le(const uint64_t *x)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	return get_unaligned_le64(x);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic void wbe(uint32_t val, uint32_t *x)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	put_unaligned_be32(val, x);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic void w2be(uint16_t val, uint16_t *x)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	put_unaligned_be16(val, x);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic void w8be(uint64_t val, uint64_t *x)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	put_unaligned_be64(val, x);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic void wle(uint32_t val, uint32_t *x)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	put_unaligned_le32(val, x);
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic void w2le(uint16_t val, uint16_t *x)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	put_unaligned_le16(val, x);
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic void w8le(uint64_t val, uint64_t *x)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	put_unaligned_le64(val, x);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/*
1698c2ecf20Sopenharmony_ci * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
1708c2ecf20Sopenharmony_ci * the way to -256..-1, to avoid conflicting with real section
1718c2ecf20Sopenharmony_ci * indices.
1728c2ecf20Sopenharmony_ci */
1738c2ecf20Sopenharmony_ci#define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic inline int is_shndx_special(unsigned int i)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
1818c2ecf20Sopenharmony_cistatic inline unsigned int get_secindex(unsigned int shndx,
1828c2ecf20Sopenharmony_ci					unsigned int sym_offs,
1838c2ecf20Sopenharmony_ci					const Elf32_Word *symtab_shndx_start)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	if (is_shndx_special(shndx))
1868c2ecf20Sopenharmony_ci		return SPECIAL(shndx);
1878c2ecf20Sopenharmony_ci	if (shndx != SHN_XINDEX)
1888c2ecf20Sopenharmony_ci		return shndx;
1898c2ecf20Sopenharmony_ci	return r(&symtab_shndx_start[sym_offs]);
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/* 32 bit and 64 bit are very similar */
1938c2ecf20Sopenharmony_ci#include "sorttable.h"
1948c2ecf20Sopenharmony_ci#define SORTTABLE_64
1958c2ecf20Sopenharmony_ci#include "sorttable.h"
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic int compare_relative_table(const void *a, const void *b)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	int32_t av = (int32_t)r(a);
2008c2ecf20Sopenharmony_ci	int32_t bv = (int32_t)r(b);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	if (av < bv)
2038c2ecf20Sopenharmony_ci		return -1;
2048c2ecf20Sopenharmony_ci	if (av > bv)
2058c2ecf20Sopenharmony_ci		return 1;
2068c2ecf20Sopenharmony_ci	return 0;
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic void sort_relative_table(char *extab_image, int image_size)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	int i = 0;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/*
2148c2ecf20Sopenharmony_ci	 * Do the same thing the runtime sort does, first normalize to
2158c2ecf20Sopenharmony_ci	 * being relative to the start of the section.
2168c2ecf20Sopenharmony_ci	 */
2178c2ecf20Sopenharmony_ci	while (i < image_size) {
2188c2ecf20Sopenharmony_ci		uint32_t *loc = (uint32_t *)(extab_image + i);
2198c2ecf20Sopenharmony_ci		w(r(loc) + i, loc);
2208c2ecf20Sopenharmony_ci		i += 4;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	qsort(extab_image, image_size / 8, 8, compare_relative_table);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* Now denormalize. */
2268c2ecf20Sopenharmony_ci	i = 0;
2278c2ecf20Sopenharmony_ci	while (i < image_size) {
2288c2ecf20Sopenharmony_ci		uint32_t *loc = (uint32_t *)(extab_image + i);
2298c2ecf20Sopenharmony_ci		w(r(loc) - i, loc);
2308c2ecf20Sopenharmony_ci		i += 4;
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic void sort_relative_table_with_data(char *extab_image, int image_size)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	int i = 0;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	while (i < image_size) {
2398c2ecf20Sopenharmony_ci		uint32_t *loc = (uint32_t *)(extab_image + i);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		w(r(loc) + i, loc);
2428c2ecf20Sopenharmony_ci		w(r(loc + 1) + i + 4, loc + 1);
2438c2ecf20Sopenharmony_ci		/* Don't touch the fixup type or data */
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci		i += sizeof(uint32_t) * 3;
2468c2ecf20Sopenharmony_ci	}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	qsort(extab_image, image_size / 12, 12, compare_relative_table);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	i = 0;
2518c2ecf20Sopenharmony_ci	while (i < image_size) {
2528c2ecf20Sopenharmony_ci		uint32_t *loc = (uint32_t *)(extab_image + i);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		w(r(loc) - i, loc);
2558c2ecf20Sopenharmony_ci		w(r(loc + 1) - (i + 4), loc + 1);
2568c2ecf20Sopenharmony_ci		/* Don't touch the fixup type or data */
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		i += sizeof(uint32_t) * 3;
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic void x86_sort_relative_table(char *extab_image, int image_size)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	int i = 0;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	while (i < image_size) {
2678c2ecf20Sopenharmony_ci		uint32_t *loc = (uint32_t *)(extab_image + i);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci		w(r(loc) + i, loc);
2708c2ecf20Sopenharmony_ci		w(r(loc + 1) + i + 4, loc + 1);
2718c2ecf20Sopenharmony_ci		w(r(loc + 2) + i + 8, loc + 2);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci		i += sizeof(uint32_t) * 3;
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	qsort(extab_image, image_size / 12, 12, compare_relative_table);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	i = 0;
2798c2ecf20Sopenharmony_ci	while (i < image_size) {
2808c2ecf20Sopenharmony_ci		uint32_t *loc = (uint32_t *)(extab_image + i);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci		w(r(loc) - i, loc);
2838c2ecf20Sopenharmony_ci		w(r(loc + 1) - (i + 4), loc + 1);
2848c2ecf20Sopenharmony_ci		w(r(loc + 2) - (i + 8), loc + 2);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci		i += sizeof(uint32_t) * 3;
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistatic void s390_sort_relative_table(char *extab_image, int image_size)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	int i;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	for (i = 0; i < image_size; i += 16) {
2958c2ecf20Sopenharmony_ci		char *loc = extab_image + i;
2968c2ecf20Sopenharmony_ci		uint64_t handler;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci		w(r((uint32_t *)loc) + i, (uint32_t *)loc);
2998c2ecf20Sopenharmony_ci		w(r((uint32_t *)(loc + 4)) + (i + 4), (uint32_t *)(loc + 4));
3008c2ecf20Sopenharmony_ci		/*
3018c2ecf20Sopenharmony_ci		 * 0 is a special self-relative handler value, which means that
3028c2ecf20Sopenharmony_ci		 * handler should be ignored. It is safe, because it means that
3038c2ecf20Sopenharmony_ci		 * handler field points to itself, which should never happen.
3048c2ecf20Sopenharmony_ci		 * When creating extable-relative values, keep it as 0, since
3058c2ecf20Sopenharmony_ci		 * this should never occur either: it would mean that handler
3068c2ecf20Sopenharmony_ci		 * field points to the first extable entry.
3078c2ecf20Sopenharmony_ci		 */
3088c2ecf20Sopenharmony_ci		handler = r8((uint64_t *)(loc + 8));
3098c2ecf20Sopenharmony_ci		if (handler)
3108c2ecf20Sopenharmony_ci			handler += i + 8;
3118c2ecf20Sopenharmony_ci		w8(handler, (uint64_t *)(loc + 8));
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	qsort(extab_image, image_size / 16, 16, compare_relative_table);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	for (i = 0; i < image_size; i += 16) {
3178c2ecf20Sopenharmony_ci		char *loc = extab_image + i;
3188c2ecf20Sopenharmony_ci		uint64_t handler;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci		w(r((uint32_t *)loc) - i, (uint32_t *)loc);
3218c2ecf20Sopenharmony_ci		w(r((uint32_t *)(loc + 4)) - (i + 4), (uint32_t *)(loc + 4));
3228c2ecf20Sopenharmony_ci		handler = r8((uint64_t *)(loc + 8));
3238c2ecf20Sopenharmony_ci		if (handler)
3248c2ecf20Sopenharmony_ci			handler -= i + 8;
3258c2ecf20Sopenharmony_ci		w8(handler, (uint64_t *)(loc + 8));
3268c2ecf20Sopenharmony_ci	}
3278c2ecf20Sopenharmony_ci}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_cistatic int do_file(char const *const fname, void *addr)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	int rc = -1;
3328c2ecf20Sopenharmony_ci	Elf32_Ehdr *ehdr = addr;
3338c2ecf20Sopenharmony_ci	table_sort_t custom_sort = NULL;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	switch (ehdr->e_ident[EI_DATA]) {
3368c2ecf20Sopenharmony_ci	case ELFDATA2LSB:
3378c2ecf20Sopenharmony_ci		r	= rle;
3388c2ecf20Sopenharmony_ci		r2	= r2le;
3398c2ecf20Sopenharmony_ci		r8	= r8le;
3408c2ecf20Sopenharmony_ci		w	= wle;
3418c2ecf20Sopenharmony_ci		w2	= w2le;
3428c2ecf20Sopenharmony_ci		w8	= w8le;
3438c2ecf20Sopenharmony_ci		break;
3448c2ecf20Sopenharmony_ci	case ELFDATA2MSB:
3458c2ecf20Sopenharmony_ci		r	= rbe;
3468c2ecf20Sopenharmony_ci		r2	= r2be;
3478c2ecf20Sopenharmony_ci		r8	= r8be;
3488c2ecf20Sopenharmony_ci		w	= wbe;
3498c2ecf20Sopenharmony_ci		w2	= w2be;
3508c2ecf20Sopenharmony_ci		w8	= w8be;
3518c2ecf20Sopenharmony_ci		break;
3528c2ecf20Sopenharmony_ci	default:
3538c2ecf20Sopenharmony_ci		fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
3548c2ecf20Sopenharmony_ci			ehdr->e_ident[EI_DATA], fname);
3558c2ecf20Sopenharmony_ci		return -1;
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
3598c2ecf20Sopenharmony_ci	    (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
3608c2ecf20Sopenharmony_ci	    ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
3618c2ecf20Sopenharmony_ci		fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
3628c2ecf20Sopenharmony_ci		return -1;
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	switch (r2(&ehdr->e_machine)) {
3668c2ecf20Sopenharmony_ci	case EM_386:
3678c2ecf20Sopenharmony_ci	case EM_X86_64:
3688c2ecf20Sopenharmony_ci		custom_sort = x86_sort_relative_table;
3698c2ecf20Sopenharmony_ci		break;
3708c2ecf20Sopenharmony_ci	case EM_S390:
3718c2ecf20Sopenharmony_ci		custom_sort = s390_sort_relative_table;
3728c2ecf20Sopenharmony_ci		break;
3738c2ecf20Sopenharmony_ci	case EM_LOONGARCH:
3748c2ecf20Sopenharmony_ci		custom_sort = sort_relative_table_with_data;
3758c2ecf20Sopenharmony_ci		break;
3768c2ecf20Sopenharmony_ci	case EM_AARCH64:
3778c2ecf20Sopenharmony_ci	case EM_PARISC:
3788c2ecf20Sopenharmony_ci	case EM_PPC:
3798c2ecf20Sopenharmony_ci	case EM_ARM:
3808c2ecf20Sopenharmony_ci	case EM_PPC64:
3818c2ecf20Sopenharmony_ci		custom_sort = sort_relative_table;
3828c2ecf20Sopenharmony_ci		break;
3838c2ecf20Sopenharmony_ci	case EM_ARCOMPACT:
3848c2ecf20Sopenharmony_ci	case EM_ARCV2:
3858c2ecf20Sopenharmony_ci	case EM_MICROBLAZE:
3868c2ecf20Sopenharmony_ci	case EM_MIPS:
3878c2ecf20Sopenharmony_ci	case EM_XTENSA:
3888c2ecf20Sopenharmony_ci		break;
3898c2ecf20Sopenharmony_ci	default:
3908c2ecf20Sopenharmony_ci		fprintf(stderr, "unrecognized e_machine %d %s\n",
3918c2ecf20Sopenharmony_ci			r2(&ehdr->e_machine), fname);
3928c2ecf20Sopenharmony_ci		return -1;
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	switch (ehdr->e_ident[EI_CLASS]) {
3968c2ecf20Sopenharmony_ci	case ELFCLASS32:
3978c2ecf20Sopenharmony_ci		if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
3988c2ecf20Sopenharmony_ci		    r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
3998c2ecf20Sopenharmony_ci			fprintf(stderr,
4008c2ecf20Sopenharmony_ci				"unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
4018c2ecf20Sopenharmony_ci			break;
4028c2ecf20Sopenharmony_ci		}
4038c2ecf20Sopenharmony_ci		rc = do_sort_32(ehdr, fname, custom_sort);
4048c2ecf20Sopenharmony_ci		break;
4058c2ecf20Sopenharmony_ci	case ELFCLASS64:
4068c2ecf20Sopenharmony_ci		{
4078c2ecf20Sopenharmony_ci		Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
4088c2ecf20Sopenharmony_ci		if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
4098c2ecf20Sopenharmony_ci		    r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
4108c2ecf20Sopenharmony_ci			fprintf(stderr,
4118c2ecf20Sopenharmony_ci				"unrecognized ET_EXEC/ET_DYN file: %s\n",
4128c2ecf20Sopenharmony_ci				fname);
4138c2ecf20Sopenharmony_ci			break;
4148c2ecf20Sopenharmony_ci		}
4158c2ecf20Sopenharmony_ci		rc = do_sort_64(ghdr, fname, custom_sort);
4168c2ecf20Sopenharmony_ci		}
4178c2ecf20Sopenharmony_ci		break;
4188c2ecf20Sopenharmony_ci	default:
4198c2ecf20Sopenharmony_ci		fprintf(stderr, "unrecognized ELF class %d %s\n",
4208c2ecf20Sopenharmony_ci			ehdr->e_ident[EI_CLASS], fname);
4218c2ecf20Sopenharmony_ci		break;
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return rc;
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ciint main(int argc, char *argv[])
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	int i, n_error = 0;  /* gcc-4.3.0 false positive complaint */
4308c2ecf20Sopenharmony_ci	size_t size = 0;
4318c2ecf20Sopenharmony_ci	void *addr = NULL;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	if (argc < 2) {
4348c2ecf20Sopenharmony_ci		fprintf(stderr, "usage: sorttable vmlinux...\n");
4358c2ecf20Sopenharmony_ci		return 0;
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	/* Process each file in turn, allowing deep failure. */
4398c2ecf20Sopenharmony_ci	for (i = 1; i < argc; i++) {
4408c2ecf20Sopenharmony_ci		addr = mmap_file(argv[i], &size);
4418c2ecf20Sopenharmony_ci		if (!addr) {
4428c2ecf20Sopenharmony_ci			++n_error;
4438c2ecf20Sopenharmony_ci			continue;
4448c2ecf20Sopenharmony_ci		}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci		if (do_file(argv[i], addr))
4478c2ecf20Sopenharmony_ci			++n_error;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci		munmap(addr, size);
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	return !!n_error;
4538c2ecf20Sopenharmony_ci}
454