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