1 // SPDX-License-Identifier: GPL-2.0-only
3 * sorttable.c: Sort the kernel's table
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>
9 * Copyright 2011 - 2012 Cavium, Inc.
11 * Based on code taken from recortmcount.c which is:
13 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
15 * Restructured to fit Linux format, as well as other updates:
16 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
20 * Strategy: alter the vmlinux file in-place.
23 #include <sys/types.h>
34 #include <tools/be_byteshift.h>
35 #include <tools/le_byteshift.h>
38 #define EM_ARCOMPACT 93
46 #define EM_AARCH64 183
50 #define EM_MICROBLAZE 189
61 static uint32_t (*r)(const uint32_t *);
62 static uint16_t (*r2)(const uint16_t *);
63 static uint64_t (*r8)(const uint64_t *);
64 static void (*w)(uint32_t, uint32_t *);
65 static void (*w2)(uint16_t, uint16_t *);
66 static void (*w8)(uint64_t, uint64_t *);
67 typedef void (*table_sort_t)(char *, int);
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.
75 static void *mmap_file(char const *fname, size_t *size)
81 fd = open(fname, O_RDWR);
86 if (fstat(fd, &sb) < 0) {
90 if (!S_ISREG(sb.st_mode)) {
91 fprintf(stderr, "not a regular file: %s\n", fname);
95 addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
96 if (addr == MAP_FAILED) {
97 fprintf(stderr, "Could not mmap file: %s\n", fname);
108 static uint32_t rbe(const uint32_t *x)
110 return get_unaligned_be32(x);
113 static uint16_t r2be(const uint16_t *x)
115 return get_unaligned_be16(x);
118 static uint64_t r8be(const uint64_t *x)
120 return get_unaligned_be64(x);
123 static uint32_t rle(const uint32_t *x)
125 return get_unaligned_le32(x);
128 static uint16_t r2le(const uint16_t *x)
130 return get_unaligned_le16(x);
133 static uint64_t r8le(const uint64_t *x)
135 return get_unaligned_le64(x);
138 static void wbe(uint32_t val, uint32_t *x)
140 put_unaligned_be32(val, x);
143 static void w2be(uint16_t val, uint16_t *x)
145 put_unaligned_be16(val, x);
148 static void w8be(uint64_t val, uint64_t *x)
150 put_unaligned_be64(val, x);
153 static void wle(uint32_t val, uint32_t *x)
155 put_unaligned_le32(val, x);
158 static void w2le(uint16_t val, uint16_t *x)
160 put_unaligned_le16(val, x);
163 static void w8le(uint64_t val, uint64_t *x)
165 put_unaligned_le64(val, x);
169 * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
170 * the way to -256..-1, to avoid conflicting with real section
173 #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
175 static inline int is_shndx_special(unsigned int i)
177 return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
180 /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
181 static inline unsigned int get_secindex(unsigned int shndx,
182 unsigned int sym_offs,
183 const Elf32_Word *symtab_shndx_start)
185 if (is_shndx_special(shndx))
186 return SPECIAL(shndx);
187 if (shndx != SHN_XINDEX)
189 return r(&symtab_shndx_start[sym_offs]);
192 /* 32 bit and 64 bit are very similar */
193 #include "sorttable.h"
195 #include "sorttable.h"
197 static int compare_relative_table(const void *a, const void *b)
199 int32_t av = (int32_t)r(a);
200 int32_t bv = (int32_t)r(b);
209 static void sort_relative_table(char *extab_image, int image_size)
214 * Do the same thing the runtime sort does, first normalize to
215 * being relative to the start of the section.
217 while (i < image_size) {
218 uint32_t *loc = (uint32_t *)(extab_image + i);
223 qsort(extab_image, image_size / 8, 8, compare_relative_table);
225 /* Now denormalize. */
227 while (i < image_size) {
228 uint32_t *loc = (uint32_t *)(extab_image + i);
234 static void arm64_sort_relative_table(char *extab_image, int image_size)
238 while (i < image_size) {
239 uint32_t *loc = (uint32_t *)(extab_image + i);
242 w(r(loc + 1) + i + 4, loc + 1);
243 /* Don't touch the fixup type or data */
245 i += sizeof(uint32_t) * 3;
248 qsort(extab_image, image_size / 12, 12, compare_relative_table);
251 while (i < image_size) {
252 uint32_t *loc = (uint32_t *)(extab_image + i);
255 w(r(loc + 1) - (i + 4), loc + 1);
256 /* Don't touch the fixup type or data */
258 i += sizeof(uint32_t) * 3;
262 static void x86_sort_relative_table(char *extab_image, int image_size)
266 while (i < image_size) {
267 uint32_t *loc = (uint32_t *)(extab_image + i);
270 w(r(loc + 1) + i + 4, loc + 1);
271 /* Don't touch the fixup type */
273 i += sizeof(uint32_t) * 3;
276 qsort(extab_image, image_size / 12, 12, compare_relative_table);
279 while (i < image_size) {
280 uint32_t *loc = (uint32_t *)(extab_image + i);
283 w(r(loc + 1) - (i + 4), loc + 1);
284 /* Don't touch the fixup type */
286 i += sizeof(uint32_t) * 3;
290 static void s390_sort_relative_table(char *extab_image, int image_size)
294 for (i = 0; i < image_size; i += 16) {
295 char *loc = extab_image + i;
298 w(r((uint32_t *)loc) + i, (uint32_t *)loc);
299 w(r((uint32_t *)(loc + 4)) + (i + 4), (uint32_t *)(loc + 4));
301 * 0 is a special self-relative handler value, which means that
302 * handler should be ignored. It is safe, because it means that
303 * handler field points to itself, which should never happen.
304 * When creating extable-relative values, keep it as 0, since
305 * this should never occur either: it would mean that handler
306 * field points to the first extable entry.
308 handler = r8((uint64_t *)(loc + 8));
311 w8(handler, (uint64_t *)(loc + 8));
314 qsort(extab_image, image_size / 16, 16, compare_relative_table);
316 for (i = 0; i < image_size; i += 16) {
317 char *loc = extab_image + i;
320 w(r((uint32_t *)loc) - i, (uint32_t *)loc);
321 w(r((uint32_t *)(loc + 4)) - (i + 4), (uint32_t *)(loc + 4));
322 handler = r8((uint64_t *)(loc + 8));
325 w8(handler, (uint64_t *)(loc + 8));
329 static int do_file(char const *const fname, void *addr)
332 Elf32_Ehdr *ehdr = addr;
333 table_sort_t custom_sort = NULL;
335 switch (ehdr->e_ident[EI_DATA]) {
353 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
354 ehdr->e_ident[EI_DATA], fname);
358 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
359 (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
360 ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
361 fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
365 switch (r2(&ehdr->e_machine)) {
368 custom_sort = x86_sort_relative_table;
371 custom_sort = s390_sort_relative_table;
374 custom_sort = arm64_sort_relative_table;
379 custom_sort = sort_relative_table;
390 fprintf(stderr, "unrecognized e_machine %d %s\n",
391 r2(&ehdr->e_machine), fname);
395 switch (ehdr->e_ident[EI_CLASS]) {
397 if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
398 r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
400 "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
403 rc = do_sort_32(ehdr, fname, custom_sort);
407 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
408 if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
409 r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
411 "unrecognized ET_EXEC/ET_DYN file: %s\n",
415 rc = do_sort_64(ghdr, fname, custom_sort);
419 fprintf(stderr, "unrecognized ELF class %d %s\n",
420 ehdr->e_ident[EI_CLASS], fname);
427 int main(int argc, char *argv[])
429 int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
434 fprintf(stderr, "usage: sorttable vmlinux...\n");
438 /* Process each file in turn, allowing deep failure. */
439 for (i = 1; i < argc; i++) {
440 addr = mmap_file(argv[i], &size);
446 if (do_file(argv[i], addr))