Thomas Gleixner | 4317cf9 | 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 | |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 57 | static uint32_t (*r)(const uint32_t *); |
| 58 | static uint16_t (*r2)(const uint16_t *); |
| 59 | static uint64_t (*r8)(const uint64_t *); |
| 60 | static void (*w)(uint32_t, uint32_t *); |
| 61 | static void (*w2)(uint16_t, uint16_t *); |
| 62 | static void (*w8)(uint64_t, uint64_t *); |
| 63 | typedef void (*table_sort_t)(char *, int); |
| 64 | |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Get the whole file as a programming convenience in order to avoid |
| 67 | * malloc+lseek+read+free of many pieces. If successful, then mmap |
| 68 | * avoids copying unused pieces; else just read the whole file. |
| 69 | * Open for both read and write. |
| 70 | */ |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 71 | static void *mmap_file(char const *fname, size_t *size) |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 72 | { |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 73 | int fd; |
| 74 | struct stat sb; |
| 75 | void *addr = NULL; |
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 | fd = open(fname, O_RDWR); |
| 78 | if (fd < 0) { |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 79 | perror(fname); |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
| 82 | if (fstat(fd, &sb) < 0) { |
| 83 | perror(fname); |
| 84 | goto out; |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 85 | } |
| 86 | if (!S_ISREG(sb.st_mode)) { |
| 87 | fprintf(stderr, "not a regular file: %s\n", fname); |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 88 | goto out; |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 89 | } |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 90 | |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 91 | 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] | 92 | if (addr == MAP_FAILED) { |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 93 | fprintf(stderr, "Could not mmap file: %s\n", fname); |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 94 | goto out; |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 95 | } |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 96 | |
| 97 | *size = sb.st_size; |
| 98 | |
| 99 | out: |
| 100 | close(fd); |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 101 | return addr; |
| 102 | } |
| 103 | |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 104 | static uint32_t rbe(const uint32_t *x) |
| 105 | { |
| 106 | return get_unaligned_be32(x); |
| 107 | } |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 108 | |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 109 | static uint16_t r2be(const uint16_t *x) |
| 110 | { |
| 111 | return get_unaligned_be16(x); |
| 112 | } |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 113 | |
| 114 | static uint64_t r8be(const uint64_t *x) |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 115 | { |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 116 | return get_unaligned_be64(x); |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 117 | } |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 118 | |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 119 | static uint32_t rle(const uint32_t *x) |
| 120 | { |
| 121 | return get_unaligned_le32(x); |
| 122 | } |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 123 | |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 124 | static uint16_t r2le(const uint16_t *x) |
| 125 | { |
| 126 | return get_unaligned_le16(x); |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 129 | static uint64_t r8le(const uint64_t *x) |
| 130 | { |
| 131 | return get_unaligned_le64(x); |
| 132 | } |
| 133 | |
| 134 | static void wbe(uint32_t val, uint32_t *x) |
| 135 | { |
| 136 | put_unaligned_be32(val, x); |
| 137 | } |
| 138 | |
| 139 | static void w2be(uint16_t val, uint16_t *x) |
| 140 | { |
| 141 | put_unaligned_be16(val, x); |
| 142 | } |
| 143 | |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 144 | static void w8be(uint64_t val, uint64_t *x) |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 145 | { |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 146 | put_unaligned_be64(val, x); |
| 147 | } |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 148 | |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 149 | static void wle(uint32_t val, uint32_t *x) |
| 150 | { |
| 151 | put_unaligned_le32(val, x); |
| 152 | } |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 153 | |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 154 | static void w2le(uint16_t val, uint16_t *x) |
| 155 | { |
| 156 | put_unaligned_le16(val, x); |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 159 | static void w8le(uint64_t val, uint64_t *x) |
| 160 | { |
| 161 | put_unaligned_le64(val, x); |
| 162 | } |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 163 | |
Jamie Iles | 59c3645 | 2013-11-12 15:06:51 -0800 | [diff] [blame] | 164 | /* |
| 165 | * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of |
| 166 | * the way to -256..-1, to avoid conflicting with real section |
| 167 | * indices. |
| 168 | */ |
| 169 | #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1)) |
| 170 | |
| 171 | static inline int is_shndx_special(unsigned int i) |
| 172 | { |
| 173 | return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE; |
| 174 | } |
| 175 | |
| 176 | /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */ |
| 177 | static inline unsigned int get_secindex(unsigned int shndx, |
| 178 | unsigned int sym_offs, |
| 179 | const Elf32_Word *symtab_shndx_start) |
| 180 | { |
| 181 | if (is_shndx_special(shndx)) |
| 182 | return SPECIAL(shndx); |
| 183 | if (shndx != SHN_XINDEX) |
| 184 | return shndx; |
| 185 | return r(&symtab_shndx_start[sym_offs]); |
| 186 | } |
| 187 | |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 188 | /* 32 bit and 64 bit are very similar */ |
Shile Zhang | 1091670 | 2019-12-04 08:46:31 +0800 | [diff] [blame] | 189 | #include "sorttable.h" |
| 190 | #define SORTTABLE_64 |
| 191 | #include "sorttable.h" |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 192 | |
Heiko Carstens | eb608fb | 2012-09-05 13:26:11 +0200 | [diff] [blame] | 193 | static int compare_relative_table(const void *a, const void *b) |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 194 | { |
| 195 | int32_t av = (int32_t)r(a); |
| 196 | int32_t bv = (int32_t)r(b); |
| 197 | |
| 198 | if (av < bv) |
| 199 | return -1; |
| 200 | if (av > bv) |
| 201 | return 1; |
| 202 | return 0; |
| 203 | } |
| 204 | |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 205 | static void sort_relative_table(char *extab_image, int image_size) |
| 206 | { |
| 207 | int i = 0; |
| 208 | |
| 209 | /* |
| 210 | * Do the same thing the runtime sort does, first normalize to |
| 211 | * being relative to the start of the section. |
| 212 | */ |
| 213 | while (i < image_size) { |
| 214 | uint32_t *loc = (uint32_t *)(extab_image + i); |
| 215 | w(r(loc) + i, loc); |
| 216 | i += 4; |
| 217 | } |
| 218 | |
| 219 | qsort(extab_image, image_size / 8, 8, compare_relative_table); |
| 220 | |
| 221 | /* Now denormalize. */ |
| 222 | i = 0; |
| 223 | while (i < image_size) { |
| 224 | uint32_t *loc = (uint32_t *)(extab_image + i); |
| 225 | w(r(loc) - i, loc); |
| 226 | i += 4; |
| 227 | } |
| 228 | } |
| 229 | |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 230 | static void x86_sort_relative_table(char *extab_image, int image_size) |
| 231 | { |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 232 | int i = 0; |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 233 | |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 234 | while (i < image_size) { |
| 235 | uint32_t *loc = (uint32_t *)(extab_image + i); |
| 236 | |
| 237 | w(r(loc) + i, loc); |
| 238 | w(r(loc + 1) + i + 4, loc + 1); |
| 239 | w(r(loc + 2) + i + 8, loc + 2); |
| 240 | |
| 241 | i += sizeof(uint32_t) * 3; |
| 242 | } |
| 243 | |
| 244 | qsort(extab_image, image_size / 12, 12, compare_relative_table); |
| 245 | |
| 246 | i = 0; |
| 247 | while (i < image_size) { |
| 248 | uint32_t *loc = (uint32_t *)(extab_image + i); |
| 249 | |
| 250 | w(r(loc) - i, loc); |
| 251 | w(r(loc + 1) - (i + 4), loc + 1); |
| 252 | w(r(loc + 2) - (i + 8), loc + 2); |
| 253 | |
| 254 | i += sizeof(uint32_t) * 3; |
| 255 | } |
| 256 | } |
| 257 | |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 258 | static int do_file(char const *const fname, void *addr) |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 259 | { |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 260 | int rc = -1; |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 261 | Elf32_Ehdr *ehdr = addr; |
| 262 | table_sort_t custom_sort = NULL; |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 263 | |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 264 | switch (ehdr->e_ident[EI_DATA]) { |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 265 | case ELFDATA2LSB: |
| 266 | r = rle; |
| 267 | r2 = r2le; |
| 268 | r8 = r8le; |
| 269 | w = wle; |
| 270 | w2 = w2le; |
| 271 | w8 = w8le; |
| 272 | break; |
| 273 | case ELFDATA2MSB: |
| 274 | r = rbe; |
| 275 | r2 = r2be; |
| 276 | r8 = r8be; |
| 277 | w = wbe; |
| 278 | w2 = w2be; |
| 279 | w8 = w8be; |
| 280 | break; |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 281 | default: |
| 282 | fprintf(stderr, "unrecognized ELF data encoding %d: %s\n", |
| 283 | ehdr->e_ident[EI_DATA], fname); |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 284 | return -1; |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 || |
| 288 | (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) || |
| 289 | ehdr->e_ident[EI_VERSION] != EV_CURRENT) { |
Ard Biesheuvel | 7b957b6 | 2016-01-10 11:42:28 +0100 | [diff] [blame] | 290 | fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname); |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 291 | return -1; |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 292 | } |
| 293 | |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 294 | switch (r2(&ehdr->e_machine)) { |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 295 | case EM_386: |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 296 | case EM_X86_64: |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 297 | custom_sort = x86_sort_relative_table; |
| 298 | break; |
Heiko Carstens | 3193a98 | 2012-07-24 14:51:34 +0200 | [diff] [blame] | 299 | case EM_S390: |
Ard Biesheuvel | 6c94f27 | 2016-01-01 15:02:12 +0100 | [diff] [blame] | 300 | case EM_AARCH64: |
Helge Deller | 0de7985 | 2016-03-23 16:00:46 +0100 | [diff] [blame] | 301 | case EM_PARISC: |
Nicholas Piggin | 5b9ff02 | 2016-10-13 16:42:55 +1100 | [diff] [blame] | 302 | case EM_PPC: |
| 303 | case EM_PPC64: |
Heiko Carstens | eb608fb | 2012-09-05 13:26:11 +0200 | [diff] [blame] | 304 | custom_sort = sort_relative_table; |
| 305 | break; |
Vineet Gupta | f06d19e | 2013-11-15 12:08:05 +0530 | [diff] [blame] | 306 | case EM_ARCOMPACT: |
Vineet Gupta | b3210d14 | 2013-11-22 13:05:58 +0530 | [diff] [blame] | 307 | case EM_ARCV2: |
Stephen Boyd | ee951c6 | 2012-10-29 19:19:34 +0100 | [diff] [blame] | 308 | case EM_ARM: |
Michal Simek | 372c720 | 2014-01-23 15:52:46 -0800 | [diff] [blame] | 309 | case EM_MICROBLAZE: |
David Daney | d59a168 | 2012-04-24 11:23:14 -0700 | [diff] [blame] | 310 | case EM_MIPS: |
Max Filippov | 25df819 | 2014-02-18 15:29:11 +0400 | [diff] [blame] | 311 | case EM_XTENSA: |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 312 | break; |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 313 | default: |
| 314 | fprintf(stderr, "unrecognized e_machine %d %s\n", |
| 315 | r2(&ehdr->e_machine), fname); |
| 316 | return -1; |
| 317 | } |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 318 | |
| 319 | switch (ehdr->e_ident[EI_CLASS]) { |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 320 | case ELFCLASS32: |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 321 | if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) || |
| 322 | r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) { |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 323 | fprintf(stderr, |
Ard Biesheuvel | 7b957b6 | 2016-01-10 11:42:28 +0100 | [diff] [blame] | 324 | "unrecognized ET_EXEC/ET_DYN file: %s\n", fname); |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 325 | break; |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 326 | } |
Shile Zhang | 57cafdf | 2019-12-04 08:46:30 +0800 | [diff] [blame] | 327 | rc = do_sort_32(ehdr, fname, custom_sort); |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 328 | break; |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 329 | case ELFCLASS64: |
| 330 | { |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 331 | Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr; |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 332 | if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) || |
| 333 | r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) { |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 334 | fprintf(stderr, |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 335 | "unrecognized ET_EXEC/ET_DYN file: %s\n", |
| 336 | fname); |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 337 | break; |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 338 | } |
Shile Zhang | 57cafdf | 2019-12-04 08:46:30 +0800 | [diff] [blame] | 339 | rc = do_sort_64(ghdr, fname, custom_sort); |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 340 | } |
| 341 | break; |
| 342 | default: |
| 343 | fprintf(stderr, "unrecognized ELF class %d %s\n", |
| 344 | ehdr->e_ident[EI_CLASS], fname); |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 345 | break; |
| 346 | } |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 347 | |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 348 | return rc; |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 351 | int main(int argc, char *argv[]) |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 352 | { |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 353 | int i, n_error = 0; /* gcc-4.3.0 false positive complaint */ |
| 354 | size_t size = 0; |
| 355 | void *addr = NULL; |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 356 | |
| 357 | if (argc < 2) { |
Shile Zhang | 1091670 | 2019-12-04 08:46:31 +0800 | [diff] [blame] | 358 | fprintf(stderr, "usage: sorttable vmlinux...\n"); |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | /* Process each file in turn, allowing deep failure. */ |
| 363 | for (i = 1; i < argc; i++) { |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 364 | addr = mmap_file(argv[i], &size); |
| 365 | if (!addr) { |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 366 | ++n_error; |
Shile Zhang | 3c47b78 | 2019-12-04 08:46:27 +0800 | [diff] [blame] | 367 | continue; |
| 368 | } |
| 369 | |
| 370 | if (do_file(argv[i], addr)) |
| 371 | ++n_error; |
| 372 | |
| 373 | munmap(addr, size); |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 374 | } |
Shile Zhang | 6402e14 | 2019-12-04 08:46:28 +0800 | [diff] [blame] | 375 | |
David Daney | a79f248 | 2012-04-19 14:59:55 -0700 | [diff] [blame] | 376 | return !!n_error; |
| 377 | } |