]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - scripts/sorttable.c
x86/speculation/mmio: Enable CPU Fill buffer clearing on idle
[mirror_ubuntu-jammy-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
d09c3872
MC
57#ifndef EM_RISCV
58#define EM_RISCV 243
59#endif
60
6402e141
SZ
61static uint32_t (*r)(const uint32_t *);
62static uint16_t (*r2)(const uint16_t *);
63static uint64_t (*r8)(const uint64_t *);
64static void (*w)(uint32_t, uint32_t *);
65static void (*w2)(uint16_t, uint16_t *);
66static void (*w8)(uint64_t, uint64_t *);
67typedef void (*table_sort_t)(char *, int);
68
a79f248b
DD
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 */
3c47b787 75static void *mmap_file(char const *fname, size_t *size)
a79f248b 76{
3c47b787
SZ
77 int fd;
78 struct stat sb;
79 void *addr = NULL;
a79f248b 80
3c47b787
SZ
81 fd = open(fname, O_RDWR);
82 if (fd < 0) {
a79f248b 83 perror(fname);
3c47b787
SZ
84 return NULL;
85 }
86 if (fstat(fd, &sb) < 0) {
87 perror(fname);
88 goto out;
a79f248b
DD
89 }
90 if (!S_ISREG(sb.st_mode)) {
91 fprintf(stderr, "not a regular file: %s\n", fname);
3c47b787 92 goto out;
a79f248b 93 }
6402e141 94
3c47b787 95 addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
a79f248b 96 if (addr == MAP_FAILED) {
a79f248b 97 fprintf(stderr, "Could not mmap file: %s\n", fname);
3c47b787 98 goto out;
a79f248b 99 }
3c47b787
SZ
100
101 *size = sb.st_size;
102
103out:
104 close(fd);
a79f248b
DD
105 return addr;
106}
107
d59a1683 108static uint32_t rbe(const uint32_t *x)
a79f248b 109{
d59a1683 110 return get_unaligned_be32(x);
a79f248b 111}
6402e141 112
d59a1683 113static uint16_t r2be(const uint16_t *x)
a79f248b 114{
d59a1683 115 return get_unaligned_be16(x);
a79f248b 116}
6402e141
SZ
117
118static uint64_t r8be(const uint64_t *x)
a79f248b 119{
6402e141 120 return get_unaligned_be64(x);
a79f248b 121}
6402e141 122
d59a1683
DD
123static uint32_t rle(const uint32_t *x)
124{
125 return get_unaligned_le32(x);
126}
6402e141 127
d59a1683 128static uint16_t r2le(const uint16_t *x)
a79f248b 129{
d59a1683 130 return get_unaligned_le16(x);
a79f248b
DD
131}
132
6402e141 133static uint64_t r8le(const uint64_t *x)
d59a1683 134{
6402e141 135 return get_unaligned_le64(x);
d59a1683 136}
6402e141 137
d59a1683
DD
138static void wbe(uint32_t val, uint32_t *x)
139{
140 put_unaligned_be32(val, x);
141}
6402e141 142
d59a1683
DD
143static void w2be(uint16_t val, uint16_t *x)
144{
145 put_unaligned_be16(val, x);
146}
6402e141
SZ
147
148static void w8be(uint64_t val, uint64_t *x)
d59a1683 149{
6402e141 150 put_unaligned_be64(val, x);
d59a1683 151}
6402e141 152
d59a1683
DD
153static void wle(uint32_t val, uint32_t *x)
154{
155 put_unaligned_le32(val, x);
156}
6402e141 157
d59a1683 158static void w2le(uint16_t val, uint16_t *x)
a79f248b 159{
d59a1683 160 put_unaligned_le16(val, x);
a79f248b
DD
161}
162
6402e141
SZ
163static void w8le(uint64_t val, uint64_t *x)
164{
165 put_unaligned_le64(val, x);
166}
a79f248b 167
59c36455
JI
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
175static 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" */
181static 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
a79f248b 192/* 32 bit and 64 bit are very similar */
10916706
SZ
193#include "sorttable.h"
194#define SORTTABLE_64
195#include "sorttable.h"
a79f248b 196
eb608fb3 197static int compare_relative_table(const void *a, const void *b)
d59a1683
DD
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
6402e141 209static void sort_relative_table(char *extab_image, int image_size)
548acf19 210{
6402e141 211 int i = 0;
548acf19 212
6402e141
SZ
213 /*
214 * Do the same thing the runtime sort does, first normalize to
215 * being relative to the start of the section.
216 */
548acf19
TL
217 while (i < image_size) {
218 uint32_t *loc = (uint32_t *)(extab_image + i);
548acf19 219 w(r(loc) + i, loc);
6402e141 220 i += 4;
548acf19
TL
221 }
222
6402e141 223 qsort(extab_image, image_size / 8, 8, compare_relative_table);
548acf19 224
6402e141 225 /* Now denormalize. */
548acf19
TL
226 i = 0;
227 while (i < image_size) {
228 uint32_t *loc = (uint32_t *)(extab_image + i);
548acf19 229 w(r(loc) - i, loc);
6402e141 230 i += 4;
548acf19
TL
231 }
232}
233
6402e141 234static void x86_sort_relative_table(char *extab_image, int image_size)
d59a1683 235{
6402e141 236 int i = 0;
d59a1683 237
d59a1683
DD
238 while (i < image_size) {
239 uint32_t *loc = (uint32_t *)(extab_image + i);
6402e141 240
d59a1683 241 w(r(loc) + i, loc);
6402e141 242 w(r(loc + 1) + i + 4, loc + 1);
67ab2588 243 /* Don't touch the fixup type */
6402e141
SZ
244
245 i += sizeof(uint32_t) * 3;
d59a1683
DD
246 }
247
6402e141 248 qsort(extab_image, image_size / 12, 12, compare_relative_table);
d59a1683 249
d59a1683
DD
250 i = 0;
251 while (i < image_size) {
252 uint32_t *loc = (uint32_t *)(extab_image + i);
6402e141 253
d59a1683 254 w(r(loc) - i, loc);
6402e141 255 w(r(loc + 1) - (i + 4), loc + 1);
67ab2588 256 /* Don't touch the fixup type */
6402e141
SZ
257
258 i += sizeof(uint32_t) * 3;
d59a1683
DD
259 }
260}
a79f248b 261
05a68e89
IL
262static 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
6402e141 301static int do_file(char const *const fname, void *addr)
a79f248b 302{
3c47b787 303 int rc = -1;
6402e141
SZ
304 Elf32_Ehdr *ehdr = addr;
305 table_sort_t custom_sort = NULL;
a79f248b 306
a79f248b 307 switch (ehdr->e_ident[EI_DATA]) {
a79f248b 308 case ELFDATA2LSB:
6402e141
SZ
309 r = rle;
310 r2 = r2le;
311 r8 = r8le;
312 w = wle;
313 w2 = w2le;
314 w8 = w8le;
a79f248b
DD
315 break;
316 case ELFDATA2MSB:
6402e141
SZ
317 r = rbe;
318 r2 = r2be;
319 r8 = r8be;
320 w = wbe;
321 w2 = w2be;
322 w8 = w8be;
a79f248b 323 break;
6402e141
SZ
324 default:
325 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
326 ehdr->e_ident[EI_DATA], fname);
327 return -1;
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) {
7b957b6e 333 fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
3c47b787 334 return -1;
a79f248b
DD
335 }
336
d59a1683 337 switch (r2(&ehdr->e_machine)) {
a79f248b 338 case EM_386:
a79f248b 339 case EM_X86_64:
548acf19
TL
340 custom_sort = x86_sort_relative_table;
341 break;
3193a98d 342 case EM_S390:
05a68e89
IL
343 custom_sort = s390_sort_relative_table;
344 break;
6c94f27a 345 case EM_AARCH64:
0de79858 346 case EM_PARISC:
5b9ff027
NP
347 case EM_PPC:
348 case EM_PPC64:
eb608fb3
HC
349 custom_sort = sort_relative_table;
350 break;
f06d19e4 351 case EM_ARCOMPACT:
b3210d14 352 case EM_ARCV2:
ee951c63 353 case EM_ARM:
372c7209 354 case EM_MICROBLAZE:
d59a1683 355 case EM_MIPS:
54fed35f 356 case EM_RISCV:
25df8198 357 case EM_XTENSA:
a79f248b 358 break;
6402e141
SZ
359 default:
360 fprintf(stderr, "unrecognized e_machine %d %s\n",
361 r2(&ehdr->e_machine), fname);
362 return -1;
363 }
a79f248b
DD
364
365 switch (ehdr->e_ident[EI_CLASS]) {
a79f248b 366 case ELFCLASS32:
6402e141
SZ
367 if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
368 r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
a79f248b 369 fprintf(stderr,
7b957b6e 370 "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
3c47b787 371 break;
a79f248b 372 }
57cafdf2 373 rc = do_sort_32(ehdr, fname, custom_sort);
a79f248b 374 break;
6402e141
SZ
375 case ELFCLASS64:
376 {
a79f248b 377 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
6402e141
SZ
378 if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
379 r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
a79f248b 380 fprintf(stderr,
6402e141
SZ
381 "unrecognized ET_EXEC/ET_DYN file: %s\n",
382 fname);
3c47b787 383 break;
a79f248b 384 }
57cafdf2 385 rc = do_sort_64(ghdr, fname, custom_sort);
6402e141
SZ
386 }
387 break;
388 default:
389 fprintf(stderr, "unrecognized ELF class %d %s\n",
390 ehdr->e_ident[EI_CLASS], fname);
a79f248b
DD
391 break;
392 }
a79f248b 393
3c47b787 394 return rc;
a79f248b
DD
395}
396
6402e141 397int main(int argc, char *argv[])
a79f248b 398{
3c47b787
SZ
399 int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
400 size_t size = 0;
401 void *addr = NULL;
a79f248b
DD
402
403 if (argc < 2) {
10916706 404 fprintf(stderr, "usage: sorttable vmlinux...\n");
a79f248b
DD
405 return 0;
406 }
407
408 /* Process each file in turn, allowing deep failure. */
409 for (i = 1; i < argc; i++) {
3c47b787
SZ
410 addr = mmap_file(argv[i], &size);
411 if (!addr) {
412 ++n_error;
413 continue;
414 }
a79f248b 415
3c47b787 416 if (do_file(argv[i], addr))
a79f248b 417 ++n_error;
3c47b787
SZ
418
419 munmap(addr, size);
a79f248b 420 }
6402e141 421
a79f248b
DD
422 return !!n_error;
423}