xref: /linux-6.15/scripts/sorttable.c (revision 545f6cf8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * sorttable.c: Sort the kernel's table
4  *
5  * Added ORC unwind tables sort support and other updates:
6  * Copyright (C) 1999-2019 Alibaba Group Holding Limited. by:
7  * Shile Zhang <[email protected]>
8  *
9  * Copyright 2011 - 2012 Cavium, Inc.
10  *
11  * Based on code taken from recortmcount.c which is:
12  *
13  * Copyright 2009 John F. Reiser <[email protected]>.  All rights reserved.
14  *
15  * Restructured to fit Linux format, as well as other updates:
16  * Copyright 2010 Steven Rostedt <[email protected]>, Red Hat Inc.
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>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <pthread.h>
35 
36 #include <tools/be_byteshift.h>
37 #include <tools/le_byteshift.h>
38 
39 #ifndef EM_ARCOMPACT
40 #define EM_ARCOMPACT	93
41 #endif
42 
43 #ifndef EM_XTENSA
44 #define EM_XTENSA	94
45 #endif
46 
47 #ifndef EM_AARCH64
48 #define EM_AARCH64	183
49 #endif
50 
51 #ifndef EM_MICROBLAZE
52 #define EM_MICROBLAZE	189
53 #endif
54 
55 #ifndef EM_ARCV2
56 #define EM_ARCV2	195
57 #endif
58 
59 #ifndef EM_RISCV
60 #define EM_RISCV	243
61 #endif
62 
63 #ifndef EM_LOONGARCH
64 #define EM_LOONGARCH	258
65 #endif
66 
67 typedef union {
68 	Elf32_Ehdr	e32;
69 	Elf64_Ehdr	e64;
70 } Elf_Ehdr;
71 
72 typedef union {
73 	Elf32_Shdr	e32;
74 	Elf64_Shdr	e64;
75 } Elf_Shdr;
76 
77 static uint32_t (*r)(const uint32_t *);
78 static uint16_t (*r2)(const uint16_t *);
79 static uint64_t (*r8)(const uint64_t *);
80 static void (*w)(uint32_t, uint32_t *);
81 typedef void (*table_sort_t)(char *, int);
82 
83 /*
84  * Get the whole file as a programming convenience in order to avoid
85  * malloc+lseek+read+free of many pieces.  If successful, then mmap
86  * avoids copying unused pieces; else just read the whole file.
87  * Open for both read and write.
88  */
89 static void *mmap_file(char const *fname, size_t *size)
90 {
91 	int fd;
92 	struct stat sb;
93 	void *addr = NULL;
94 
95 	fd = open(fname, O_RDWR);
96 	if (fd < 0) {
97 		perror(fname);
98 		return NULL;
99 	}
100 	if (fstat(fd, &sb) < 0) {
101 		perror(fname);
102 		goto out;
103 	}
104 	if (!S_ISREG(sb.st_mode)) {
105 		fprintf(stderr, "not a regular file: %s\n", fname);
106 		goto out;
107 	}
108 
109 	addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
110 	if (addr == MAP_FAILED) {
111 		fprintf(stderr, "Could not mmap file: %s\n", fname);
112 		goto out;
113 	}
114 
115 	*size = sb.st_size;
116 
117 out:
118 	close(fd);
119 	return addr;
120 }
121 
122 static uint32_t rbe(const uint32_t *x)
123 {
124 	return get_unaligned_be32(x);
125 }
126 
127 static uint16_t r2be(const uint16_t *x)
128 {
129 	return get_unaligned_be16(x);
130 }
131 
132 static uint64_t r8be(const uint64_t *x)
133 {
134 	return get_unaligned_be64(x);
135 }
136 
137 static uint32_t rle(const uint32_t *x)
138 {
139 	return get_unaligned_le32(x);
140 }
141 
142 static uint16_t r2le(const uint16_t *x)
143 {
144 	return get_unaligned_le16(x);
145 }
146 
147 static uint64_t r8le(const uint64_t *x)
148 {
149 	return get_unaligned_le64(x);
150 }
151 
152 static void wbe(uint32_t val, uint32_t *x)
153 {
154 	put_unaligned_be32(val, x);
155 }
156 
157 static void wle(uint32_t val, uint32_t *x)
158 {
159 	put_unaligned_le32(val, x);
160 }
161 
162 /*
163  * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
164  * the way to -256..-1, to avoid conflicting with real section
165  * indices.
166  */
167 #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
168 
169 static inline int is_shndx_special(unsigned int i)
170 {
171 	return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
172 }
173 
174 /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
175 static inline unsigned int get_secindex(unsigned int shndx,
176 					unsigned int sym_offs,
177 					const Elf32_Word *symtab_shndx_start)
178 {
179 	if (is_shndx_special(shndx))
180 		return SPECIAL(shndx);
181 	if (shndx != SHN_XINDEX)
182 		return shndx;
183 	return r(&symtab_shndx_start[sym_offs]);
184 }
185 
186 static int compare_extable_32(const void *a, const void *b)
187 {
188 	Elf32_Addr av = r(a);
189 	Elf32_Addr bv = r(b);
190 
191 	if (av < bv)
192 		return -1;
193 	return av > bv;
194 }
195 
196 static int compare_extable_64(const void *a, const void *b)
197 {
198 	Elf64_Addr av = r8(a);
199 	Elf64_Addr bv = r8(b);
200 
201 	if (av < bv)
202 		return -1;
203 	return av > bv;
204 }
205 
206 static inline void *get_index(void *start, int entsize, int index)
207 {
208 	return start + (entsize * index);
209 }
210 
211 /* 32 bit and 64 bit are very similar */
212 #include "sorttable.h"
213 #define SORTTABLE_64
214 #include "sorttable.h"
215 
216 static int compare_relative_table(const void *a, const void *b)
217 {
218 	int32_t av = (int32_t)r(a);
219 	int32_t bv = (int32_t)r(b);
220 
221 	if (av < bv)
222 		return -1;
223 	if (av > bv)
224 		return 1;
225 	return 0;
226 }
227 
228 static void sort_relative_table(char *extab_image, int image_size)
229 {
230 	int i = 0;
231 
232 	/*
233 	 * Do the same thing the runtime sort does, first normalize to
234 	 * being relative to the start of the section.
235 	 */
236 	while (i < image_size) {
237 		uint32_t *loc = (uint32_t *)(extab_image + i);
238 		w(r(loc) + i, loc);
239 		i += 4;
240 	}
241 
242 	qsort(extab_image, image_size / 8, 8, compare_relative_table);
243 
244 	/* Now denormalize. */
245 	i = 0;
246 	while (i < image_size) {
247 		uint32_t *loc = (uint32_t *)(extab_image + i);
248 		w(r(loc) - i, loc);
249 		i += 4;
250 	}
251 }
252 
253 static void sort_relative_table_with_data(char *extab_image, int image_size)
254 {
255 	int i = 0;
256 
257 	while (i < image_size) {
258 		uint32_t *loc = (uint32_t *)(extab_image + i);
259 
260 		w(r(loc) + i, loc);
261 		w(r(loc + 1) + i + 4, loc + 1);
262 		/* Don't touch the fixup type or data */
263 
264 		i += sizeof(uint32_t) * 3;
265 	}
266 
267 	qsort(extab_image, image_size / 12, 12, compare_relative_table);
268 
269 	i = 0;
270 	while (i < image_size) {
271 		uint32_t *loc = (uint32_t *)(extab_image + i);
272 
273 		w(r(loc) - i, loc);
274 		w(r(loc + 1) - (i + 4), loc + 1);
275 		/* Don't touch the fixup type or data */
276 
277 		i += sizeof(uint32_t) * 3;
278 	}
279 }
280 
281 static int do_file(char const *const fname, void *addr)
282 {
283 	int rc = -1;
284 	Elf_Ehdr *ehdr = addr;
285 	table_sort_t custom_sort = NULL;
286 
287 	switch (ehdr->e32.e_ident[EI_DATA]) {
288 	case ELFDATA2LSB:
289 		r	= rle;
290 		r2	= r2le;
291 		r8	= r8le;
292 		w	= wle;
293 		break;
294 	case ELFDATA2MSB:
295 		r	= rbe;
296 		r2	= r2be;
297 		r8	= r8be;
298 		w	= wbe;
299 		break;
300 	default:
301 		fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
302 			ehdr->e32.e_ident[EI_DATA], fname);
303 		return -1;
304 	}
305 
306 	if (memcmp(ELFMAG, ehdr->e32.e_ident, SELFMAG) != 0 ||
307 	    (r2(&ehdr->e32.e_type) != ET_EXEC && r2(&ehdr->e32.e_type) != ET_DYN) ||
308 	    ehdr->e32.e_ident[EI_VERSION] != EV_CURRENT) {
309 		fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
310 		return -1;
311 	}
312 
313 	switch (r2(&ehdr->e32.e_machine)) {
314 	case EM_386:
315 	case EM_AARCH64:
316 	case EM_LOONGARCH:
317 	case EM_RISCV:
318 	case EM_S390:
319 	case EM_X86_64:
320 		custom_sort = sort_relative_table_with_data;
321 		break;
322 	case EM_PARISC:
323 	case EM_PPC:
324 	case EM_PPC64:
325 		custom_sort = sort_relative_table;
326 		break;
327 	case EM_ARCOMPACT:
328 	case EM_ARCV2:
329 	case EM_ARM:
330 	case EM_MICROBLAZE:
331 	case EM_MIPS:
332 	case EM_XTENSA:
333 		break;
334 	default:
335 		fprintf(stderr, "unrecognized e_machine %d %s\n",
336 			r2(&ehdr->e32.e_machine), fname);
337 		return -1;
338 	}
339 
340 	switch (ehdr->e32.e_ident[EI_CLASS]) {
341 	case ELFCLASS32:
342 		if (r2(&ehdr->e32.e_ehsize) != sizeof(Elf32_Ehdr) ||
343 		    r2(&ehdr->e32.e_shentsize) != sizeof(Elf32_Shdr)) {
344 			fprintf(stderr,
345 				"unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
346 			break;
347 		}
348 		rc = do_sort_32(ehdr, fname, custom_sort);
349 		break;
350 	case ELFCLASS64:
351 		{
352 		if (r2(&ehdr->e64.e_ehsize) != sizeof(Elf64_Ehdr) ||
353 		    r2(&ehdr->e64.e_shentsize) != sizeof(Elf64_Shdr)) {
354 			fprintf(stderr,
355 				"unrecognized ET_EXEC/ET_DYN file: %s\n",
356 				fname);
357 			break;
358 		}
359 		rc = do_sort_64(ehdr, fname, custom_sort);
360 		}
361 		break;
362 	default:
363 		fprintf(stderr, "unrecognized ELF class %d %s\n",
364 			ehdr->e32.e_ident[EI_CLASS], fname);
365 		break;
366 	}
367 
368 	return rc;
369 }
370 
371 int main(int argc, char *argv[])
372 {
373 	int i, n_error = 0;  /* gcc-4.3.0 false positive complaint */
374 	size_t size = 0;
375 	void *addr = NULL;
376 
377 	if (argc < 2) {
378 		fprintf(stderr, "usage: sorttable vmlinux...\n");
379 		return 0;
380 	}
381 
382 	/* Process each file in turn, allowing deep failure. */
383 	for (i = 1; i < argc; i++) {
384 		addr = mmap_file(argv[i], &size);
385 		if (!addr) {
386 			++n_error;
387 			continue;
388 		}
389 
390 		if (do_file(argv[i], addr))
391 			++n_error;
392 
393 		munmap(addr, size);
394 	}
395 
396 	return !!n_error;
397 }
398