]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - scripts/sorttable.c
UBUNTU: SAUCE: can: j1939: delay release of j1939_priv after synchronize_rcu
[mirror_ubuntu-hirsute-kernel.git] / scripts / sorttable.c
CommitLineData
4317cf95 1// SPDX-License-Identifier: GPL-2.0-only
a79f248b 2/*
10916706 3 * sorttable.c: Sort the kernel's table
a79f248b 4 *
57fa1899
SZ
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 *
d59a1683 9 * Copyright 2011 - 2012 Cavium, Inc.
a79f248b
DD
10 *
11 * Based on code taken from recortmcount.c which is:
12 *
13 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
a79f248b
DD
14 *
15 * Restructured to fit Linux format, as well as other updates:
57fa1899 16 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
a79f248b
DD
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>
a79f248b
DD
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
d59a1683
DD
34#include <tools/be_byteshift.h>
35#include <tools/le_byteshift.h>
36
f06d19e4
VG
37#ifndef EM_ARCOMPACT
38#define EM_ARCOMPACT 93
39#endif
40
25df8198
MF
41#ifndef EM_XTENSA
42#define EM_XTENSA 94
43#endif
44
adace895
WD
45#ifndef EM_AARCH64
46#define EM_AARCH64 183
47#endif
48
372c7209
MS
49#ifndef EM_MICROBLAZE
50#define EM_MICROBLAZE 189
51#endif
52
b3210d14
VG
53#ifndef EM_ARCV2
54#define EM_ARCV2 195
55#endif
56
6402e141
SZ
57static uint32_t (*r)(const uint32_t *);
58static uint16_t (*r2)(const uint16_t *);
59static uint64_t (*r8)(const uint64_t *);
60static void (*w)(uint32_t, uint32_t *);
61static void (*w2)(uint16_t, uint16_t *);
62static void (*w8)(uint64_t, uint64_t *);
63typedef void (*table_sort_t)(char *, int);
64
a79f248b
DD
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 */
3c47b787 71static void *mmap_file(char const *fname, size_t *size)
a79f248b 72{
3c47b787
SZ
73 int fd;
74 struct stat sb;
75 void *addr = NULL;
a79f248b 76
3c47b787
SZ
77 fd = open(fname, O_RDWR);
78 if (fd < 0) {
a79f248b 79 perror(fname);
3c47b787
SZ
80 return NULL;
81 }
82 if (fstat(fd, &sb) < 0) {
83 perror(fname);
84 goto out;
a79f248b
DD
85 }
86 if (!S_ISREG(sb.st_mode)) {
87 fprintf(stderr, "not a regular file: %s\n", fname);
3c47b787 88 goto out;
a79f248b 89 }
6402e141 90
3c47b787 91 addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
a79f248b 92 if (addr == MAP_FAILED) {
a79f248b 93 fprintf(stderr, "Could not mmap file: %s\n", fname);
3c47b787 94 goto out;
a79f248b 95 }
3c47b787
SZ
96
97 *size = sb.st_size;
98
99out:
100 close(fd);
a79f248b
DD
101 return addr;
102}
103
d59a1683 104static uint32_t rbe(const uint32_t *x)
a79f248b 105{
d59a1683 106 return get_unaligned_be32(x);
a79f248b 107}
6402e141 108
d59a1683 109static uint16_t r2be(const uint16_t *x)
a79f248b 110{
d59a1683 111 return get_unaligned_be16(x);
a79f248b 112}
6402e141
SZ
113
114static uint64_t r8be(const uint64_t *x)
a79f248b 115{
6402e141 116 return get_unaligned_be64(x);
a79f248b 117}
6402e141 118
d59a1683
DD
119static uint32_t rle(const uint32_t *x)
120{
121 return get_unaligned_le32(x);
122}
6402e141 123
d59a1683 124static uint16_t r2le(const uint16_t *x)
a79f248b 125{
d59a1683 126 return get_unaligned_le16(x);
a79f248b
DD
127}
128
6402e141 129static uint64_t r8le(const uint64_t *x)
d59a1683 130{
6402e141 131 return get_unaligned_le64(x);
d59a1683 132}
6402e141 133
d59a1683
DD
134static void wbe(uint32_t val, uint32_t *x)
135{
136 put_unaligned_be32(val, x);
137}
6402e141 138
d59a1683
DD
139static void w2be(uint16_t val, uint16_t *x)
140{
141 put_unaligned_be16(val, x);
142}
6402e141
SZ
143
144static void w8be(uint64_t val, uint64_t *x)
d59a1683 145{
6402e141 146 put_unaligned_be64(val, x);
d59a1683 147}
6402e141 148
d59a1683
DD
149static void wle(uint32_t val, uint32_t *x)
150{
151 put_unaligned_le32(val, x);
152}
6402e141 153
d59a1683 154static void w2le(uint16_t val, uint16_t *x)
a79f248b 155{
d59a1683 156 put_unaligned_le16(val, x);
a79f248b
DD
157}
158
6402e141
SZ
159static void w8le(uint64_t val, uint64_t *x)
160{
161 put_unaligned_le64(val, x);
162}
a79f248b 163
59c36455
JI
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
171static 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" */
177static 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
a79f248b 188/* 32 bit and 64 bit are very similar */
10916706
SZ
189#include "sorttable.h"
190#define SORTTABLE_64
191#include "sorttable.h"
a79f248b 192
eb608fb3 193static int compare_relative_table(const void *a, const void *b)
d59a1683
DD
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
6402e141 205static void sort_relative_table(char *extab_image, int image_size)
548acf19 206{
6402e141 207 int i = 0;
548acf19 208
6402e141
SZ
209 /*
210 * Do the same thing the runtime sort does, first normalize to
211 * being relative to the start of the section.
212 */
548acf19
TL
213 while (i < image_size) {
214 uint32_t *loc = (uint32_t *)(extab_image + i);
548acf19 215 w(r(loc) + i, loc);
6402e141 216 i += 4;
548acf19
TL
217 }
218
6402e141 219 qsort(extab_image, image_size / 8, 8, compare_relative_table);
548acf19 220
6402e141 221 /* Now denormalize. */
548acf19
TL
222 i = 0;
223 while (i < image_size) {
224 uint32_t *loc = (uint32_t *)(extab_image + i);
548acf19 225 w(r(loc) - i, loc);
6402e141 226 i += 4;
548acf19
TL
227 }
228}
229
6402e141 230static void x86_sort_relative_table(char *extab_image, int image_size)
d59a1683 231{
6402e141 232 int i = 0;
d59a1683 233
d59a1683
DD
234 while (i < image_size) {
235 uint32_t *loc = (uint32_t *)(extab_image + i);
6402e141 236
d59a1683 237 w(r(loc) + i, loc);
6402e141
SZ
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;
d59a1683
DD
242 }
243
6402e141 244 qsort(extab_image, image_size / 12, 12, compare_relative_table);
d59a1683 245
d59a1683
DD
246 i = 0;
247 while (i < image_size) {
248 uint32_t *loc = (uint32_t *)(extab_image + i);
6402e141 249
d59a1683 250 w(r(loc) - i, loc);
6402e141
SZ
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;
d59a1683
DD
255 }
256}
a79f248b 257
05a68e89
IL
258static void s390_sort_relative_table(char *extab_image, int image_size)
259{
260 int i;
261
262 for (i = 0; i < image_size; i += 16) {
263 char *loc = extab_image + i;
264 uint64_t handler;
265
266 w(r((uint32_t *)loc) + i, (uint32_t *)loc);
267 w(r((uint32_t *)(loc + 4)) + (i + 4), (uint32_t *)(loc + 4));
268 /*
269 * 0 is a special self-relative handler value, which means that
270 * handler should be ignored. It is safe, because it means that
271 * handler field points to itself, which should never happen.
272 * When creating extable-relative values, keep it as 0, since
273 * this should never occur either: it would mean that handler
274 * field points to the first extable entry.
275 */
276 handler = r8((uint64_t *)(loc + 8));
277 if (handler)
278 handler += i + 8;
279 w8(handler, (uint64_t *)(loc + 8));
280 }
281
282 qsort(extab_image, image_size / 16, 16, compare_relative_table);
283
284 for (i = 0; i < image_size; i += 16) {
285 char *loc = extab_image + i;
286 uint64_t handler;
287
288 w(r((uint32_t *)loc) - i, (uint32_t *)loc);
289 w(r((uint32_t *)(loc + 4)) - (i + 4), (uint32_t *)(loc + 4));
290 handler = r8((uint64_t *)(loc + 8));
291 if (handler)
292 handler -= i + 8;
293 w8(handler, (uint64_t *)(loc + 8));
294 }
295}
296
6402e141 297static int do_file(char const *const fname, void *addr)
a79f248b 298{
3c47b787 299 int rc = -1;
6402e141
SZ
300 Elf32_Ehdr *ehdr = addr;
301 table_sort_t custom_sort = NULL;
a79f248b 302
a79f248b 303 switch (ehdr->e_ident[EI_DATA]) {
a79f248b 304 case ELFDATA2LSB:
6402e141
SZ
305 r = rle;
306 r2 = r2le;
307 r8 = r8le;
308 w = wle;
309 w2 = w2le;
310 w8 = w8le;
a79f248b
DD
311 break;
312 case ELFDATA2MSB:
6402e141
SZ
313 r = rbe;
314 r2 = r2be;
315 r8 = r8be;
316 w = wbe;
317 w2 = w2be;
318 w8 = w8be;
a79f248b 319 break;
6402e141
SZ
320 default:
321 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
322 ehdr->e_ident[EI_DATA], fname);
323 return -1;
324 }
325
326 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
327 (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
328 ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
7b957b6e 329 fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
3c47b787 330 return -1;
a79f248b
DD
331 }
332
d59a1683 333 switch (r2(&ehdr->e_machine)) {
a79f248b 334 case EM_386:
a79f248b 335 case EM_X86_64:
548acf19
TL
336 custom_sort = x86_sort_relative_table;
337 break;
3193a98d 338 case EM_S390:
05a68e89
IL
339 custom_sort = s390_sort_relative_table;
340 break;
6c94f27a 341 case EM_AARCH64:
0de79858 342 case EM_PARISC:
5b9ff027
NP
343 case EM_PPC:
344 case EM_PPC64:
eb608fb3
HC
345 custom_sort = sort_relative_table;
346 break;
f06d19e4 347 case EM_ARCOMPACT:
b3210d14 348 case EM_ARCV2:
ee951c63 349 case EM_ARM:
372c7209 350 case EM_MICROBLAZE:
d59a1683 351 case EM_MIPS:
25df8198 352 case EM_XTENSA:
a79f248b 353 break;
6402e141
SZ
354 default:
355 fprintf(stderr, "unrecognized e_machine %d %s\n",
356 r2(&ehdr->e_machine), fname);
357 return -1;
358 }
a79f248b
DD
359
360 switch (ehdr->e_ident[EI_CLASS]) {
a79f248b 361 case ELFCLASS32:
6402e141
SZ
362 if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
363 r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
a79f248b 364 fprintf(stderr,
7b957b6e 365 "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
3c47b787 366 break;
a79f248b 367 }
57cafdf2 368 rc = do_sort_32(ehdr, fname, custom_sort);
a79f248b 369 break;
6402e141
SZ
370 case ELFCLASS64:
371 {
a79f248b 372 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
6402e141
SZ
373 if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
374 r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
a79f248b 375 fprintf(stderr,
6402e141
SZ
376 "unrecognized ET_EXEC/ET_DYN file: %s\n",
377 fname);
3c47b787 378 break;
a79f248b 379 }
57cafdf2 380 rc = do_sort_64(ghdr, fname, custom_sort);
6402e141
SZ
381 }
382 break;
383 default:
384 fprintf(stderr, "unrecognized ELF class %d %s\n",
385 ehdr->e_ident[EI_CLASS], fname);
a79f248b
DD
386 break;
387 }
a79f248b 388
3c47b787 389 return rc;
a79f248b
DD
390}
391
6402e141 392int main(int argc, char *argv[])
a79f248b 393{
3c47b787
SZ
394 int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
395 size_t size = 0;
396 void *addr = NULL;
a79f248b
DD
397
398 if (argc < 2) {
10916706 399 fprintf(stderr, "usage: sorttable vmlinux...\n");
a79f248b
DD
400 return 0;
401 }
402
403 /* Process each file in turn, allowing deep failure. */
404 for (i = 1; i < argc; i++) {
3c47b787
SZ
405 addr = mmap_file(argv[i], &size);
406 if (!addr) {
407 ++n_error;
408 continue;
409 }
a79f248b 410
3c47b787 411 if (do_file(argv[i], addr))
a79f248b 412 ++n_error;
3c47b787
SZ
413
414 munmap(addr, size);
a79f248b 415 }
6402e141 416
a79f248b
DD
417 return !!n_error;
418}