blob: 0ef3abfc4a51b9fe52f7e7ddc2e94d912c18eb17 [file] [log] [blame]
Thomas Gleixner4317cf952019-05-31 01:09:38 -07001// SPDX-License-Identifier: GPL-2.0-only
David Daneya79f2482012-04-19 14:59:55 -07002/*
Shile Zhang10916702019-12-04 08:46:31 +08003 * sorttable.c: Sort the kernel's table
David Daneya79f2482012-04-19 14:59:55 -07004 *
Shile Zhang57fa1892019-12-04 08:46:32 +08005 * 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 Daneyd59a1682012-04-24 11:23:14 -07009 * Copyright 2011 - 2012 Cavium, Inc.
David Daneya79f2482012-04-19 14:59:55 -070010 *
11 * Based on code taken from recortmcount.c which is:
12 *
13 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
David Daneya79f2482012-04-19 14:59:55 -070014 *
15 * Restructured to fit Linux format, as well as other updates:
Shile Zhang57fa1892019-12-04 08:46:32 +080016 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
David Daneya79f2482012-04-19 14:59:55 -070017 */
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 Daneya79f2482012-04-19 14:59:55 -070029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
David Daneyd59a1682012-04-24 11:23:14 -070034#include <tools/be_byteshift.h>
35#include <tools/le_byteshift.h>
36
Vineet Guptaf06d19e2013-11-15 12:08:05 +053037#ifndef EM_ARCOMPACT
38#define EM_ARCOMPACT 93
39#endif
40
Max Filippov25df8192014-02-18 15:29:11 +040041#ifndef EM_XTENSA
42#define EM_XTENSA 94
43#endif
44
Will Deaconadace892013-05-08 17:29:24 +010045#ifndef EM_AARCH64
46#define EM_AARCH64 183
47#endif
48
Michal Simek372c7202014-01-23 15:52:46 -080049#ifndef EM_MICROBLAZE
50#define EM_MICROBLAZE 189
51#endif
52
Vineet Guptab3210d142013-11-22 13:05:58 +053053#ifndef EM_ARCV2
54#define EM_ARCV2 195
55#endif
56
Shile Zhang6402e142019-12-04 08:46:28 +080057static 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
David Daneya79f2482012-04-19 14:59:55 -070065/*
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 Zhang3c47b782019-12-04 08:46:27 +080071static void *mmap_file(char const *fname, size_t *size)
David Daneya79f2482012-04-19 14:59:55 -070072{
Shile Zhang3c47b782019-12-04 08:46:27 +080073 int fd;
74 struct stat sb;
75 void *addr = NULL;
David Daneya79f2482012-04-19 14:59:55 -070076
Shile Zhang3c47b782019-12-04 08:46:27 +080077 fd = open(fname, O_RDWR);
78 if (fd < 0) {
David Daneya79f2482012-04-19 14:59:55 -070079 perror(fname);
Shile Zhang3c47b782019-12-04 08:46:27 +080080 return NULL;
81 }
82 if (fstat(fd, &sb) < 0) {
83 perror(fname);
84 goto out;
David Daneya79f2482012-04-19 14:59:55 -070085 }
86 if (!S_ISREG(sb.st_mode)) {
87 fprintf(stderr, "not a regular file: %s\n", fname);
Shile Zhang3c47b782019-12-04 08:46:27 +080088 goto out;
David Daneya79f2482012-04-19 14:59:55 -070089 }
Shile Zhang6402e142019-12-04 08:46:28 +080090
Shile Zhang3c47b782019-12-04 08:46:27 +080091 addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
David Daneya79f2482012-04-19 14:59:55 -070092 if (addr == MAP_FAILED) {
David Daneya79f2482012-04-19 14:59:55 -070093 fprintf(stderr, "Could not mmap file: %s\n", fname);
Shile Zhang3c47b782019-12-04 08:46:27 +080094 goto out;
David Daneya79f2482012-04-19 14:59:55 -070095 }
Shile Zhang3c47b782019-12-04 08:46:27 +080096
97 *size = sb.st_size;
98
99out:
100 close(fd);
David Daneya79f2482012-04-19 14:59:55 -0700101 return addr;
102}
103
David Daneyd59a1682012-04-24 11:23:14 -0700104static uint32_t rbe(const uint32_t *x)
105{
106 return get_unaligned_be32(x);
107}
Shile Zhang6402e142019-12-04 08:46:28 +0800108
David Daneyd59a1682012-04-24 11:23:14 -0700109static uint16_t r2be(const uint16_t *x)
110{
111 return get_unaligned_be16(x);
112}
Shile Zhang6402e142019-12-04 08:46:28 +0800113
114static uint64_t r8be(const uint64_t *x)
David Daneyd59a1682012-04-24 11:23:14 -0700115{
Shile Zhang6402e142019-12-04 08:46:28 +0800116 return get_unaligned_be64(x);
David Daneyd59a1682012-04-24 11:23:14 -0700117}
Shile Zhang6402e142019-12-04 08:46:28 +0800118
David Daneyd59a1682012-04-24 11:23:14 -0700119static uint32_t rle(const uint32_t *x)
120{
121 return get_unaligned_le32(x);
122}
Shile Zhang6402e142019-12-04 08:46:28 +0800123
David Daneyd59a1682012-04-24 11:23:14 -0700124static uint16_t r2le(const uint16_t *x)
125{
126 return get_unaligned_le16(x);
David Daneya79f2482012-04-19 14:59:55 -0700127}
128
Shile Zhang6402e142019-12-04 08:46:28 +0800129static uint64_t r8le(const uint64_t *x)
130{
131 return get_unaligned_le64(x);
132}
133
134static void wbe(uint32_t val, uint32_t *x)
135{
136 put_unaligned_be32(val, x);
137}
138
139static void w2be(uint16_t val, uint16_t *x)
140{
141 put_unaligned_be16(val, x);
142}
143
David Daneyd59a1682012-04-24 11:23:14 -0700144static void w8be(uint64_t val, uint64_t *x)
David Daneya79f2482012-04-19 14:59:55 -0700145{
David Daneyd59a1682012-04-24 11:23:14 -0700146 put_unaligned_be64(val, x);
147}
Shile Zhang6402e142019-12-04 08:46:28 +0800148
David Daneyd59a1682012-04-24 11:23:14 -0700149static void wle(uint32_t val, uint32_t *x)
150{
151 put_unaligned_le32(val, x);
152}
Shile Zhang6402e142019-12-04 08:46:28 +0800153
David Daneyd59a1682012-04-24 11:23:14 -0700154static void w2le(uint16_t val, uint16_t *x)
155{
156 put_unaligned_le16(val, x);
David Daneya79f2482012-04-19 14:59:55 -0700157}
158
Shile Zhang6402e142019-12-04 08:46:28 +0800159static void w8le(uint64_t val, uint64_t *x)
160{
161 put_unaligned_le64(val, x);
162}
David Daneya79f2482012-04-19 14:59:55 -0700163
Jamie Iles59c36452013-11-12 15:06:51 -0800164/*
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
David Daneya79f2482012-04-19 14:59:55 -0700188/* 32 bit and 64 bit are very similar */
Shile Zhang10916702019-12-04 08:46:31 +0800189#include "sorttable.h"
190#define SORTTABLE_64
191#include "sorttable.h"
David Daneya79f2482012-04-19 14:59:55 -0700192
Heiko Carstenseb608fb2012-09-05 13:26:11 +0200193static int compare_relative_table(const void *a, const void *b)
David Daneyd59a1682012-04-24 11:23:14 -0700194{
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 Zhang6402e142019-12-04 08:46:28 +0800205static 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 Luck548acf12016-02-17 10:20:12 -0800230static void x86_sort_relative_table(char *extab_image, int image_size)
231{
Shile Zhang6402e142019-12-04 08:46:28 +0800232 int i = 0;
Tony Luck548acf12016-02-17 10:20:12 -0800233
Tony Luck548acf12016-02-17 10:20:12 -0800234 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
Ilya Leoshkevich05a68e82020-06-30 20:52:03 +0200258static 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
Shile Zhang6402e142019-12-04 08:46:28 +0800297static int do_file(char const *const fname, void *addr)
David Daneyd59a1682012-04-24 11:23:14 -0700298{
Shile Zhang3c47b782019-12-04 08:46:27 +0800299 int rc = -1;
Shile Zhang6402e142019-12-04 08:46:28 +0800300 Elf32_Ehdr *ehdr = addr;
301 table_sort_t custom_sort = NULL;
David Daneya79f2482012-04-19 14:59:55 -0700302
David Daneya79f2482012-04-19 14:59:55 -0700303 switch (ehdr->e_ident[EI_DATA]) {
Shile Zhang6402e142019-12-04 08:46:28 +0800304 case ELFDATA2LSB:
305 r = rle;
306 r2 = r2le;
307 r8 = r8le;
308 w = wle;
309 w2 = w2le;
310 w8 = w8le;
311 break;
312 case ELFDATA2MSB:
313 r = rbe;
314 r2 = r2be;
315 r8 = r8be;
316 w = wbe;
317 w2 = w2be;
318 w8 = w8be;
319 break;
David Daneya79f2482012-04-19 14:59:55 -0700320 default:
321 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
322 ehdr->e_ident[EI_DATA], fname);
Shile Zhang3c47b782019-12-04 08:46:27 +0800323 return -1;
Shile Zhang6402e142019-12-04 08:46:28 +0800324 }
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) {
Ard Biesheuvel7b957b62016-01-10 11:42:28 +0100329 fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
Shile Zhang3c47b782019-12-04 08:46:27 +0800330 return -1;
David Daneya79f2482012-04-19 14:59:55 -0700331 }
332
David Daneyd59a1682012-04-24 11:23:14 -0700333 switch (r2(&ehdr->e_machine)) {
David Daneya79f2482012-04-19 14:59:55 -0700334 case EM_386:
David Daneya79f2482012-04-19 14:59:55 -0700335 case EM_X86_64:
Tony Luck548acf12016-02-17 10:20:12 -0800336 custom_sort = x86_sort_relative_table;
337 break;
Heiko Carstens3193a982012-07-24 14:51:34 +0200338 case EM_S390:
Ilya Leoshkevich05a68e82020-06-30 20:52:03 +0200339 custom_sort = s390_sort_relative_table;
340 break;
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100341 case EM_AARCH64:
Helge Deller0de79852016-03-23 16:00:46 +0100342 case EM_PARISC:
Nicholas Piggin5b9ff022016-10-13 16:42:55 +1100343 case EM_PPC:
344 case EM_PPC64:
Heiko Carstenseb608fb2012-09-05 13:26:11 +0200345 custom_sort = sort_relative_table;
346 break;
Vineet Guptaf06d19e2013-11-15 12:08:05 +0530347 case EM_ARCOMPACT:
Vineet Guptab3210d142013-11-22 13:05:58 +0530348 case EM_ARCV2:
Stephen Boydee951c62012-10-29 19:19:34 +0100349 case EM_ARM:
Michal Simek372c7202014-01-23 15:52:46 -0800350 case EM_MICROBLAZE:
David Daneyd59a1682012-04-24 11:23:14 -0700351 case EM_MIPS:
Max Filippov25df8192014-02-18 15:29:11 +0400352 case EM_XTENSA:
David Daneya79f2482012-04-19 14:59:55 -0700353 break;
Shile Zhang6402e142019-12-04 08:46:28 +0800354 default:
355 fprintf(stderr, "unrecognized e_machine %d %s\n",
356 r2(&ehdr->e_machine), fname);
357 return -1;
358 }
David Daneya79f2482012-04-19 14:59:55 -0700359
360 switch (ehdr->e_ident[EI_CLASS]) {
David Daneya79f2482012-04-19 14:59:55 -0700361 case ELFCLASS32:
Shile Zhang6402e142019-12-04 08:46:28 +0800362 if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
363 r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
David Daneya79f2482012-04-19 14:59:55 -0700364 fprintf(stderr,
Ard Biesheuvel7b957b62016-01-10 11:42:28 +0100365 "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
Shile Zhang3c47b782019-12-04 08:46:27 +0800366 break;
David Daneya79f2482012-04-19 14:59:55 -0700367 }
Shile Zhang57cafdf2019-12-04 08:46:30 +0800368 rc = do_sort_32(ehdr, fname, custom_sort);
David Daneya79f2482012-04-19 14:59:55 -0700369 break;
Shile Zhang6402e142019-12-04 08:46:28 +0800370 case ELFCLASS64:
371 {
David Daneya79f2482012-04-19 14:59:55 -0700372 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
Shile Zhang6402e142019-12-04 08:46:28 +0800373 if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
374 r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
David Daneya79f2482012-04-19 14:59:55 -0700375 fprintf(stderr,
Shile Zhang6402e142019-12-04 08:46:28 +0800376 "unrecognized ET_EXEC/ET_DYN file: %s\n",
377 fname);
Shile Zhang3c47b782019-12-04 08:46:27 +0800378 break;
David Daneya79f2482012-04-19 14:59:55 -0700379 }
Shile Zhang57cafdf2019-12-04 08:46:30 +0800380 rc = do_sort_64(ghdr, fname, custom_sort);
Shile Zhang6402e142019-12-04 08:46:28 +0800381 }
382 break;
383 default:
384 fprintf(stderr, "unrecognized ELF class %d %s\n",
385 ehdr->e_ident[EI_CLASS], fname);
David Daneya79f2482012-04-19 14:59:55 -0700386 break;
387 }
David Daneya79f2482012-04-19 14:59:55 -0700388
Shile Zhang3c47b782019-12-04 08:46:27 +0800389 return rc;
David Daneya79f2482012-04-19 14:59:55 -0700390}
391
Shile Zhang6402e142019-12-04 08:46:28 +0800392int main(int argc, char *argv[])
David Daneya79f2482012-04-19 14:59:55 -0700393{
Shile Zhang3c47b782019-12-04 08:46:27 +0800394 int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
395 size_t size = 0;
396 void *addr = NULL;
David Daneya79f2482012-04-19 14:59:55 -0700397
398 if (argc < 2) {
Shile Zhang10916702019-12-04 08:46:31 +0800399 fprintf(stderr, "usage: sorttable vmlinux...\n");
David Daneya79f2482012-04-19 14:59:55 -0700400 return 0;
401 }
402
403 /* Process each file in turn, allowing deep failure. */
404 for (i = 1; i < argc; i++) {
Shile Zhang3c47b782019-12-04 08:46:27 +0800405 addr = mmap_file(argv[i], &size);
406 if (!addr) {
David Daneya79f2482012-04-19 14:59:55 -0700407 ++n_error;
Shile Zhang3c47b782019-12-04 08:46:27 +0800408 continue;
409 }
410
411 if (do_file(argv[i], addr))
412 ++n_error;
413
414 munmap(addr, size);
David Daneya79f2482012-04-19 14:59:55 -0700415 }
Shile Zhang6402e142019-12-04 08:46:28 +0800416
David Daneya79f2482012-04-19 14:59:55 -0700417 return !!n_error;
418}