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>
36 #include <tools/be_byteshift.h>
37 #include <tools/le_byteshift.h>
40 #define EM_ARCOMPACT 93
48 #define EM_AARCH64 183
52 #define EM_MICROBLAZE 189
64 #define EM_LOONGARCH 258
67 static uint32_t (*r)(const uint32_t *);
68 static uint16_t (*r2)(const uint16_t *);
69 static uint64_t (*r8)(const uint64_t *);
70 static void (*w)(uint32_t, uint32_t *);
71 static void (*w2)(uint16_t, uint16_t *);
72 static void (*w8)(uint64_t, uint64_t *);
73 typedef void (*table_sort_t)(char *, int);
76 * Get the whole file as a programming convenience in order to avoid
77 * malloc+lseek+read+free of many pieces. If successful, then mmap
78 * avoids copying unused pieces; else just read the whole file.
79 * Open for both read and write.
81 static void *mmap_file(char const *fname, size_t *size)
87 fd = open(fname, O_RDWR);
92 if (fstat(fd, &sb) < 0) {
96 if (!S_ISREG(sb.st_mode)) {
97 fprintf(stderr, "not a regular file: %s\n", fname);
101 addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
102 if (addr == MAP_FAILED) {
103 fprintf(stderr, "Could not mmap file: %s\n", fname);
114 static uint32_t rbe(const uint32_t *x)
116 return get_unaligned_be32(x);
119 static uint16_t r2be(const uint16_t *x)
121 return get_unaligned_be16(x);
124 static uint64_t r8be(const uint64_t *x)
126 return get_unaligned_be64(x);
129 static uint32_t rle(const uint32_t *x)
131 return get_unaligned_le32(x);
134 static uint16_t r2le(const uint16_t *x)
136 return get_unaligned_le16(x);
139 static uint64_t r8le(const uint64_t *x)
141 return get_unaligned_le64(x);
144 static void wbe(uint32_t val, uint32_t *x)
146 put_unaligned_be32(val, x);
149 static void w2be(uint16_t val, uint16_t *x)
151 put_unaligned_be16(val, x);
154 static void w8be(uint64_t val, uint64_t *x)
156 put_unaligned_be64(val, x);
159 static void wle(uint32_t val, uint32_t *x)
161 put_unaligned_le32(val, x);
164 static void w2le(uint16_t val, uint16_t *x)
166 put_unaligned_le16(val, x);
169 static void w8le(uint64_t val, uint64_t *x)
171 put_unaligned_le64(val, x);
175 * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
176 * the way to -256..-1, to avoid conflicting with real section
179 #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
181 static inline int is_shndx_special(unsigned int i)
183 return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
186 /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
187 static inline unsigned int get_secindex(unsigned int shndx,
188 unsigned int sym_offs,
189 const Elf32_Word *symtab_shndx_start)
191 if (is_shndx_special(shndx))
192 return SPECIAL(shndx);
193 if (shndx != SHN_XINDEX)
195 return r(&symtab_shndx_start[sym_offs]);
198 /* 32 bit and 64 bit are very similar */
199 #include "sorttable.h"
201 #include "sorttable.h"
203 static int compare_relative_table(const void *a, const void *b)
205 int32_t av = (int32_t)r(a);
206 int32_t bv = (int32_t)r(b);
215 static void sort_relative_table(char *extab_image, int image_size)
220 * Do the same thing the runtime sort does, first normalize to
221 * being relative to the start of the section.
223 while (i < image_size) {
224 uint32_t *loc = (uint32_t *)(extab_image + i);
229 qsort(extab_image, image_size / 8, 8, compare_relative_table);
231 /* Now denormalize. */
233 while (i < image_size) {
234 uint32_t *loc = (uint32_t *)(extab_image + i);
240 static void sort_relative_table_with_data(char *extab_image, int image_size)
244 while (i < image_size) {
245 uint32_t *loc = (uint32_t *)(extab_image + i);
248 w(r(loc + 1) + i + 4, loc + 1);
249 /* Don't touch the fixup type or data */
251 i += sizeof(uint32_t) * 3;
254 qsort(extab_image, image_size / 12, 12, compare_relative_table);
257 while (i < image_size) {
258 uint32_t *loc = (uint32_t *)(extab_image + i);
261 w(r(loc + 1) - (i + 4), loc + 1);
262 /* Don't touch the fixup type or data */
264 i += sizeof(uint32_t) * 3;
268 static int do_file(char const *const fname, void *addr)
271 Elf32_Ehdr *ehdr = addr;
272 table_sort_t custom_sort = NULL;
274 switch (ehdr->e_ident[EI_DATA]) {
292 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
293 ehdr->e_ident[EI_DATA], fname);
297 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
298 (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
299 ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
300 fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
304 switch (r2(&ehdr->e_machine)) {
311 custom_sort = sort_relative_table_with_data;
316 custom_sort = sort_relative_table;
326 fprintf(stderr, "unrecognized e_machine %d %s\n",
327 r2(&ehdr->e_machine), fname);
331 switch (ehdr->e_ident[EI_CLASS]) {
333 if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
334 r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
336 "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
339 rc = do_sort_32(ehdr, fname, custom_sort);
343 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
344 if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
345 r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
347 "unrecognized ET_EXEC/ET_DYN file: %s\n",
351 rc = do_sort_64(ghdr, fname, custom_sort);
355 fprintf(stderr, "unrecognized ELF class %d %s\n",
356 ehdr->e_ident[EI_CLASS], fname);
363 int main(int argc, char *argv[])
365 int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
370 fprintf(stderr, "usage: sorttable vmlinux...\n");
374 /* Process each file in turn, allowing deep failure. */
375 for (i = 1; i < argc; i++) {
376 addr = mmap_file(argv[i], &size);
382 if (do_file(argv[i], addr))