4317cf95ca024 scripts/sortextable.c (Thomas Gleixner 2019-05-31 01:09:38 -0700 1) // SPDX-License-Identifier: GPL-2.0-only
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 2) /*
1091670637be8 scripts/sorttable.c (Shile Zhang 2019-12-04 08:46:31 +0800 3) * sorttable.c: Sort the kernel's table
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 4) *
57fa189942853 scripts/sorttable.c (Shile Zhang 2019-12-04 08:46:32 +0800 5) * Added ORC unwind tables sort support and other updates:
57fa189942853 scripts/sorttable.c (Shile Zhang 2019-12-04 08:46:32 +0800 6) * Copyright (C) 1999-2019 Alibaba Group Holding Limited. by:
57fa189942853 scripts/sorttable.c (Shile Zhang 2019-12-04 08:46:32 +0800 7) * Shile Zhang <shile.zhang@linux.alibaba.com>
57fa189942853 scripts/sorttable.c (Shile Zhang 2019-12-04 08:46:32 +0800 8) *
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 9) * Copyright 2011 - 2012 Cavium, Inc.
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 10) *
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 11) * Based on code taken from recortmcount.c which is:
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 12) *
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 13) * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 14) *
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 15) * Restructured to fit Linux format, as well as other updates:
57fa189942853 scripts/sorttable.c (Shile Zhang 2019-12-04 08:46:32 +0800 16) * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 17) */
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 18)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 19) /*
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 20) * Strategy: alter the vmlinux file in-place.
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 21) */
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 22)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 23) #include <sys/types.h>
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 24) #include <sys/mman.h>
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 25) #include <sys/stat.h>
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 26) #include <getopt.h>
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 27) #include <elf.h>
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 28) #include <fcntl.h>
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 29) #include <stdio.h>
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 30) #include <stdlib.h>
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 31) #include <string.h>
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 32) #include <unistd.h>
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 33)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 34) #include <tools/be_byteshift.h>
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 35) #include <tools/le_byteshift.h>
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 36)
f06d19e460cdd scripts/sortextable.c (Vineet Gupta 2013-11-15 12:08:05 +0530 37) #ifndef EM_ARCOMPACT
f06d19e460cdd scripts/sortextable.c (Vineet Gupta 2013-11-15 12:08:05 +0530 38) #define EM_ARCOMPACT 93
f06d19e460cdd scripts/sortextable.c (Vineet Gupta 2013-11-15 12:08:05 +0530 39) #endif
f06d19e460cdd scripts/sortextable.c (Vineet Gupta 2013-11-15 12:08:05 +0530 40)
25df8198f4b25 scripts/sortextable.c (Max Filippov 2014-02-18 15:29:11 +0400 41) #ifndef EM_XTENSA
25df8198f4b25 scripts/sortextable.c (Max Filippov 2014-02-18 15:29:11 +0400 42) #define EM_XTENSA 94
25df8198f4b25 scripts/sortextable.c (Max Filippov 2014-02-18 15:29:11 +0400 43) #endif
25df8198f4b25 scripts/sortextable.c (Max Filippov 2014-02-18 15:29:11 +0400 44)
adace89562c7a scripts/sortextable.c (Will Deacon 2013-05-08 17:29:24 +0100 45) #ifndef EM_AARCH64
adace89562c7a scripts/sortextable.c (Will Deacon 2013-05-08 17:29:24 +0100 46) #define EM_AARCH64 183
adace89562c7a scripts/sortextable.c (Will Deacon 2013-05-08 17:29:24 +0100 47) #endif
adace89562c7a scripts/sortextable.c (Will Deacon 2013-05-08 17:29:24 +0100 48)
372c7209d6a05 scripts/sortextable.c (Michal Simek 2014-01-23 15:52:46 -0800 49) #ifndef EM_MICROBLAZE
372c7209d6a05 scripts/sortextable.c (Michal Simek 2014-01-23 15:52:46 -0800 50) #define EM_MICROBLAZE 189
372c7209d6a05 scripts/sortextable.c (Michal Simek 2014-01-23 15:52:46 -0800 51) #endif
372c7209d6a05 scripts/sortextable.c (Michal Simek 2014-01-23 15:52:46 -0800 52)
b3210d141f5ec scripts/sortextable.c (Vineet Gupta 2013-11-22 13:05:58 +0530 53) #ifndef EM_ARCV2
b3210d141f5ec scripts/sortextable.c (Vineet Gupta 2013-11-22 13:05:58 +0530 54) #define EM_ARCV2 195
b3210d141f5ec scripts/sortextable.c (Vineet Gupta 2013-11-22 13:05:58 +0530 55) #endif
b3210d141f5ec scripts/sortextable.c (Vineet Gupta 2013-11-22 13:05:58 +0530 56)
d09c38726c78e scripts/sorttable.c (Miles Chen 2021-09-24 15:43:38 -0700 57) #ifndef EM_RISCV
d09c38726c78e scripts/sorttable.c (Miles Chen 2021-09-24 15:43:38 -0700 58) #define EM_RISCV 243
d09c38726c78e scripts/sorttable.c (Miles Chen 2021-09-24 15:43:38 -0700 59) #endif
d09c38726c78e scripts/sorttable.c (Miles Chen 2021-09-24 15:43:38 -0700 60)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 61) static uint32_t (*r)(const uint32_t *);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 62) static uint16_t (*r2)(const uint16_t *);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 63) static uint64_t (*r8)(const uint64_t *);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 64) static void (*w)(uint32_t, uint32_t *);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 65) static void (*w2)(uint16_t, uint16_t *);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 66) static void (*w8)(uint64_t, uint64_t *);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 67) typedef void (*table_sort_t)(char *, int);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 68)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 69) /*
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 70) * Get the whole file as a programming convenience in order to avoid
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 71) * malloc+lseek+read+free of many pieces. If successful, then mmap
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 72) * avoids copying unused pieces; else just read the whole file.
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 73) * Open for both read and write.
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 74) */
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 75) static void *mmap_file(char const *fname, size_t *size)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 76) {
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 77) int fd;
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 78) struct stat sb;
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 79) void *addr = NULL;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 80)
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 81) fd = open(fname, O_RDWR);
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 82) if (fd < 0) {
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 83) perror(fname);
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 84) return NULL;
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 85) }
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 86) if (fstat(fd, &sb) < 0) {
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 87) perror(fname);
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 88) goto out;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 89) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 90) if (!S_ISREG(sb.st_mode)) {
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 91) fprintf(stderr, "not a regular file: %s\n", fname);
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 92) goto out;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 93) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 94)
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 95) addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 96) if (addr == MAP_FAILED) {
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 97) fprintf(stderr, "Could not mmap file: %s\n", fname);
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 98) goto out;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 99) }
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 100)
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 101) *size = sb.st_size;
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 102)
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 103) out:
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 104) close(fd);
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 105) return addr;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 106) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 107)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 108) static uint32_t rbe(const uint32_t *x)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 109) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 110) return get_unaligned_be32(x);
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 111) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 112)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 113) static uint16_t r2be(const uint16_t *x)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 114) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 115) return get_unaligned_be16(x);
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 116) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 117)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 118) static uint64_t r8be(const uint64_t *x)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 119) {
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 120) return get_unaligned_be64(x);
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 121) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 122)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 123) static uint32_t rle(const uint32_t *x)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 124) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 125) return get_unaligned_le32(x);
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 126) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 127)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 128) static uint16_t r2le(const uint16_t *x)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 129) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 130) return get_unaligned_le16(x);
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 131) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 132)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 133) static uint64_t r8le(const uint64_t *x)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 134) {
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 135) return get_unaligned_le64(x);
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 136) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 137)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 138) static void wbe(uint32_t val, uint32_t *x)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 139) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 140) put_unaligned_be32(val, x);
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 141) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 142)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 143) static void w2be(uint16_t val, uint16_t *x)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 144) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 145) put_unaligned_be16(val, x);
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 146) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 147)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 148) static void w8be(uint64_t val, uint64_t *x)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 149) {
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 150) put_unaligned_be64(val, x);
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 151) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 152)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 153) static void wle(uint32_t val, uint32_t *x)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 154) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 155) put_unaligned_le32(val, x);
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 156) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 157)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 158) static void w2le(uint16_t val, uint16_t *x)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 159) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 160) put_unaligned_le16(val, x);
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 161) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 162)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 163) static void w8le(uint64_t val, uint64_t *x)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 164) {
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 165) put_unaligned_le64(val, x);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 166) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 167)
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 168) /*
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 169) * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 170) * the way to -256..-1, to avoid conflicting with real section
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 171) * indices.
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 172) */
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 173) #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 174)
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 175) static inline int is_shndx_special(unsigned int i)
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 176) {
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 177) return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 178) }
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 179)
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 180) /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 181) static inline unsigned int get_secindex(unsigned int shndx,
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 182) unsigned int sym_offs,
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 183) const Elf32_Word *symtab_shndx_start)
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 184) {
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 185) if (is_shndx_special(shndx))
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 186) return SPECIAL(shndx);
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 187) if (shndx != SHN_XINDEX)
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 188) return shndx;
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 189) return r(&symtab_shndx_start[sym_offs]);
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 190) }
59c36455d061e scripts/sortextable.c (Jamie Iles 2013-11-12 15:06:51 -0800 191)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 192) /* 32 bit and 64 bit are very similar */
1091670637be8 scripts/sorttable.c (Shile Zhang 2019-12-04 08:46:31 +0800 193) #include "sorttable.h"
1091670637be8 scripts/sorttable.c (Shile Zhang 2019-12-04 08:46:31 +0800 194) #define SORTTABLE_64
1091670637be8 scripts/sorttable.c (Shile Zhang 2019-12-04 08:46:31 +0800 195) #include "sorttable.h"
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 196)
eb608fb366de1 scripts/sortextable.c (Heiko Carstens 2012-09-05 13:26:11 +0200 197) static int compare_relative_table(const void *a, const void *b)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 198) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 199) int32_t av = (int32_t)r(a);
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 200) int32_t bv = (int32_t)r(b);
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 201)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 202) if (av < bv)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 203) return -1;
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 204) if (av > bv)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 205) return 1;
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 206) return 0;
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 207) }
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 208)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 209) static void sort_relative_table(char *extab_image, int image_size)
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 210) {
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 211) int i = 0;
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 212)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 213) /*
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 214) * Do the same thing the runtime sort does, first normalize to
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 215) * being relative to the start of the section.
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 216) */
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 217) while (i < image_size) {
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 218) uint32_t *loc = (uint32_t *)(extab_image + i);
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 219) w(r(loc) + i, loc);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 220) i += 4;
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 221) }
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 222)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 223) qsort(extab_image, image_size / 8, 8, compare_relative_table);
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 224)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 225) /* Now denormalize. */
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 226) i = 0;
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 227) while (i < image_size) {
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 228) uint32_t *loc = (uint32_t *)(extab_image + i);
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 229) w(r(loc) - i, loc);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 230) i += 4;
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 231) }
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 232) }
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 233)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 234) static void x86_sort_relative_table(char *extab_image, int image_size)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 235) {
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 236) int i = 0;
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 237)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 238) while (i < image_size) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 239) uint32_t *loc = (uint32_t *)(extab_image + i);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 240)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 241) w(r(loc) + i, loc);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 242) w(r(loc + 1) + i + 4, loc + 1);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 243) w(r(loc + 2) + i + 8, loc + 2);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 244)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 245) i += sizeof(uint32_t) * 3;
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 246) }
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 247)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 248) qsort(extab_image, image_size / 12, 12, compare_relative_table);
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 249)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 250) i = 0;
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 251) while (i < image_size) {
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 252) uint32_t *loc = (uint32_t *)(extab_image + i);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 253)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 254) w(r(loc) - i, loc);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 255) w(r(loc + 1) - (i + 4), loc + 1);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 256) w(r(loc + 2) - (i + 8), loc + 2);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 257)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 258) i += sizeof(uint32_t) * 3;
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 259) }
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 260) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 261)
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 262) static void s390_sort_relative_table(char *extab_image, int image_size)
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 263) {
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 264) int i;
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 265)
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 266) for (i = 0; i < image_size; i += 16) {
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 267) char *loc = extab_image + i;
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 268) uint64_t handler;
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 269)
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 270) w(r((uint32_t *)loc) + i, (uint32_t *)loc);
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 271) w(r((uint32_t *)(loc + 4)) + (i + 4), (uint32_t *)(loc + 4));
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 272) /*
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 273) * 0 is a special self-relative handler value, which means that
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 274) * handler should be ignored. It is safe, because it means that
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 275) * handler field points to itself, which should never happen.
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 276) * When creating extable-relative values, keep it as 0, since
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 277) * this should never occur either: it would mean that handler
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 278) * field points to the first extable entry.
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 279) */
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 280) handler = r8((uint64_t *)(loc + 8));
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 281) if (handler)
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 282) handler += i + 8;
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 283) w8(handler, (uint64_t *)(loc + 8));
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 284) }
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 285)
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 286) qsort(extab_image, image_size / 16, 16, compare_relative_table);
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 287)
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 288) for (i = 0; i < image_size; i += 16) {
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 289) char *loc = extab_image + i;
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 290) uint64_t handler;
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 291)
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 292) w(r((uint32_t *)loc) - i, (uint32_t *)loc);
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 293) w(r((uint32_t *)(loc + 4)) - (i + 4), (uint32_t *)(loc + 4));
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 294) handler = r8((uint64_t *)(loc + 8));
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 295) if (handler)
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 296) handler -= i + 8;
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 297) w8(handler, (uint64_t *)(loc + 8));
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 298) }
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 299) }
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 300)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 301) static int do_file(char const *const fname, void *addr)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 302) {
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 303) int rc = -1;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 304) Elf32_Ehdr *ehdr = addr;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 305) table_sort_t custom_sort = NULL;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 306)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 307) switch (ehdr->e_ident[EI_DATA]) {
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 308) case ELFDATA2LSB:
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 309) r = rle;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 310) r2 = r2le;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 311) r8 = r8le;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 312) w = wle;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 313) w2 = w2le;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 314) w8 = w8le;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 315) break;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 316) case ELFDATA2MSB:
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 317) r = rbe;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 318) r2 = r2be;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 319) r8 = r8be;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 320) w = wbe;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 321) w2 = w2be;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 322) w8 = w8be;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 323) break;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 324) default:
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 325) fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 326) ehdr->e_ident[EI_DATA], fname);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 327) return -1;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 328) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 329)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 330) if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 331) (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 332) ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
7b957b6e60362 scripts/sortextable.c (Ard Biesheuvel 2016-01-10 11:42:28 +0100 333) fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 334) return -1;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 335) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 336)
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 337) switch (r2(&ehdr->e_machine)) {
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 338) case EM_386:
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 339) case EM_X86_64:
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 340) custom_sort = x86_sort_relative_table;
548acf19234db scripts/sortextable.c (Tony Luck 2016-02-17 10:20:12 -0800 341) break;
3193a98dc8777 scripts/sortextable.c (Heiko Carstens 2012-07-24 14:51:34 +0200 342) case EM_S390:
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 343) custom_sort = s390_sort_relative_table;
05a68e892e89c scripts/sorttable.c (Ilya Leoshkevich 2020-06-30 20:52:03 +0200 344) break;
6c94f27ac847f scripts/sortextable.c (Ard Biesheuvel 2016-01-01 15:02:12 +0100 345) case EM_AARCH64:
0de798584bded scripts/sortextable.c (Helge Deller 2016-03-23 16:00:46 +0100 346) case EM_PARISC:
5b9ff02785986 scripts/sortextable.c (Nicholas Piggin 2016-10-13 16:42:55 +1100 347) case EM_PPC:
5b9ff02785986 scripts/sortextable.c (Nicholas Piggin 2016-10-13 16:42:55 +1100 348) case EM_PPC64:
eb608fb366de1 scripts/sortextable.c (Heiko Carstens 2012-09-05 13:26:11 +0200 349) custom_sort = sort_relative_table;
eb608fb366de1 scripts/sortextable.c (Heiko Carstens 2012-09-05 13:26:11 +0200 350) break;
f06d19e460cdd scripts/sortextable.c (Vineet Gupta 2013-11-15 12:08:05 +0530 351) case EM_ARCOMPACT:
b3210d141f5ec scripts/sortextable.c (Vineet Gupta 2013-11-22 13:05:58 +0530 352) case EM_ARCV2:
ee951c630c5ce scripts/sortextable.c (Stephen Boyd 2012-10-29 19:19:34 +0100 353) case EM_ARM:
372c7209d6a05 scripts/sortextable.c (Michal Simek 2014-01-23 15:52:46 -0800 354) case EM_MICROBLAZE:
d59a16836d917 scripts/sortextable.c (David Daney 2012-04-24 11:23:14 -0700 355) case EM_MIPS:
54fed35fd3939 scripts/sorttable.c (Jisheng Zhang 2021-08-26 22:10:29 +0800 356) case EM_RISCV:
25df8198f4b25 scripts/sortextable.c (Max Filippov 2014-02-18 15:29:11 +0400 357) case EM_XTENSA:
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 358) break;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 359) default:
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 360) fprintf(stderr, "unrecognized e_machine %d %s\n",
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 361) r2(&ehdr->e_machine), fname);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 362) return -1;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 363) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 364)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 365) switch (ehdr->e_ident[EI_CLASS]) {
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 366) case ELFCLASS32:
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 367) if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 368) r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 369) fprintf(stderr,
7b957b6e60362 scripts/sortextable.c (Ard Biesheuvel 2016-01-10 11:42:28 +0100 370) "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 371) break;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 372) }
57cafdf2a04e1 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:30 +0800 373) rc = do_sort_32(ehdr, fname, custom_sort);
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 374) break;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 375) case ELFCLASS64:
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 376) {
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 377) Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 378) if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 379) r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 380) fprintf(stderr,
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 381) "unrecognized ET_EXEC/ET_DYN file: %s\n",
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 382) fname);
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 383) break;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 384) }
57cafdf2a04e1 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:30 +0800 385) rc = do_sort_64(ghdr, fname, custom_sort);
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 386) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 387) break;
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 388) default:
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 389) fprintf(stderr, "unrecognized ELF class %d %s\n",
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 390) ehdr->e_ident[EI_CLASS], fname);
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 391) break;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 392) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 393)
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 394) return rc;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 395) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 396)
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 397) int main(int argc, char *argv[])
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 398) {
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 399) int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 400) size_t size = 0;
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 401) void *addr = NULL;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 402)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 403) if (argc < 2) {
1091670637be8 scripts/sorttable.c (Shile Zhang 2019-12-04 08:46:31 +0800 404) fprintf(stderr, "usage: sorttable vmlinux...\n");
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 405) return 0;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 406) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 407)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 408) /* Process each file in turn, allowing deep failure. */
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 409) for (i = 1; i < argc; i++) {
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 410) addr = mmap_file(argv[i], &size);
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 411) if (!addr) {
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 412) ++n_error;
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 413) continue;
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 414) }
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 415)
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 416) if (do_file(argv[i], addr))
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 417) ++n_error;
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 418)
3c47b787b6516 scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:27 +0800 419) munmap(addr, size);
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 420) }
6402e1416255a scripts/sortextable.c (Shile Zhang 2019-12-04 08:46:28 +0800 421)
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 422) return !!n_error;
a79f248b9b309 scripts/sortextable.c (David Daney 2012-04-19 14:59:55 -0700 423) }