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