xref: /linux-6.15/scripts/sorttable.c (revision 1dfb59a2)
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 typedef union {
78 	Elf32_Sym	e32;
79 	Elf64_Sym	e64;
80 } Elf_Sym;
81 
82 static uint32_t (*r)(const uint32_t *);
83 static uint16_t (*r2)(const uint16_t *);
84 static uint64_t (*r8)(const uint64_t *);
85 static void (*w)(uint32_t, uint32_t *);
86 typedef void (*table_sort_t)(char *, int);
87 
88 static uint64_t ehdr64_shoff(Elf_Ehdr *ehdr)
89 {
90 	return r8(&ehdr->e64.e_shoff);
91 }
92 
93 static uint64_t ehdr32_shoff(Elf_Ehdr *ehdr)
94 {
95 	return r(&ehdr->e32.e_shoff);
96 }
97 
98 #define EHDR_HALF(fn_name)				\
99 static uint16_t ehdr64_##fn_name(Elf_Ehdr *ehdr)	\
100 {							\
101 	return r2(&ehdr->e64.e_##fn_name);		\
102 }							\
103 							\
104 static uint16_t ehdr32_##fn_name(Elf_Ehdr *ehdr)	\
105 {							\
106 	return r2(&ehdr->e32.e_##fn_name);		\
107 }
108 
109 EHDR_HALF(shentsize)
110 EHDR_HALF(shstrndx)
111 EHDR_HALF(shnum)
112 
113 /*
114  * Get the whole file as a programming convenience in order to avoid
115  * malloc+lseek+read+free of many pieces.  If successful, then mmap
116  * avoids copying unused pieces; else just read the whole file.
117  * Open for both read and write.
118  */
119 static void *mmap_file(char const *fname, size_t *size)
120 {
121 	int fd;
122 	struct stat sb;
123 	void *addr = NULL;
124 
125 	fd = open(fname, O_RDWR);
126 	if (fd < 0) {
127 		perror(fname);
128 		return NULL;
129 	}
130 	if (fstat(fd, &sb) < 0) {
131 		perror(fname);
132 		goto out;
133 	}
134 	if (!S_ISREG(sb.st_mode)) {
135 		fprintf(stderr, "not a regular file: %s\n", fname);
136 		goto out;
137 	}
138 
139 	addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
140 	if (addr == MAP_FAILED) {
141 		fprintf(stderr, "Could not mmap file: %s\n", fname);
142 		goto out;
143 	}
144 
145 	*size = sb.st_size;
146 
147 out:
148 	close(fd);
149 	return addr;
150 }
151 
152 static uint32_t rbe(const uint32_t *x)
153 {
154 	return get_unaligned_be32(x);
155 }
156 
157 static uint16_t r2be(const uint16_t *x)
158 {
159 	return get_unaligned_be16(x);
160 }
161 
162 static uint64_t r8be(const uint64_t *x)
163 {
164 	return get_unaligned_be64(x);
165 }
166 
167 static uint32_t rle(const uint32_t *x)
168 {
169 	return get_unaligned_le32(x);
170 }
171 
172 static uint16_t r2le(const uint16_t *x)
173 {
174 	return get_unaligned_le16(x);
175 }
176 
177 static uint64_t r8le(const uint64_t *x)
178 {
179 	return get_unaligned_le64(x);
180 }
181 
182 static void wbe(uint32_t val, uint32_t *x)
183 {
184 	put_unaligned_be32(val, x);
185 }
186 
187 static void wle(uint32_t val, uint32_t *x)
188 {
189 	put_unaligned_le32(val, x);
190 }
191 
192 /*
193  * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
194  * the way to -256..-1, to avoid conflicting with real section
195  * indices.
196  */
197 #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
198 
199 static inline int is_shndx_special(unsigned int i)
200 {
201 	return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
202 }
203 
204 /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
205 static inline unsigned int get_secindex(unsigned int shndx,
206 					unsigned int sym_offs,
207 					const Elf32_Word *symtab_shndx_start)
208 {
209 	if (is_shndx_special(shndx))
210 		return SPECIAL(shndx);
211 	if (shndx != SHN_XINDEX)
212 		return shndx;
213 	return r(&symtab_shndx_start[sym_offs]);
214 }
215 
216 static int compare_extable_32(const void *a, const void *b)
217 {
218 	Elf32_Addr av = r(a);
219 	Elf32_Addr bv = r(b);
220 
221 	if (av < bv)
222 		return -1;
223 	return av > bv;
224 }
225 
226 static int compare_extable_64(const void *a, const void *b)
227 {
228 	Elf64_Addr av = r8(a);
229 	Elf64_Addr bv = r8(b);
230 
231 	if (av < bv)
232 		return -1;
233 	return av > bv;
234 }
235 
236 static inline void *get_index(void *start, int entsize, int index)
237 {
238 	return start + (entsize * index);
239 }
240 
241 /* 32 bit and 64 bit are very similar */
242 #include "sorttable.h"
243 #define SORTTABLE_64
244 #include "sorttable.h"
245 
246 static int compare_relative_table(const void *a, const void *b)
247 {
248 	int32_t av = (int32_t)r(a);
249 	int32_t bv = (int32_t)r(b);
250 
251 	if (av < bv)
252 		return -1;
253 	if (av > bv)
254 		return 1;
255 	return 0;
256 }
257 
258 static void sort_relative_table(char *extab_image, int image_size)
259 {
260 	int i = 0;
261 
262 	/*
263 	 * Do the same thing the runtime sort does, first normalize to
264 	 * being relative to the start of the section.
265 	 */
266 	while (i < image_size) {
267 		uint32_t *loc = (uint32_t *)(extab_image + i);
268 		w(r(loc) + i, loc);
269 		i += 4;
270 	}
271 
272 	qsort(extab_image, image_size / 8, 8, compare_relative_table);
273 
274 	/* Now denormalize. */
275 	i = 0;
276 	while (i < image_size) {
277 		uint32_t *loc = (uint32_t *)(extab_image + i);
278 		w(r(loc) - i, loc);
279 		i += 4;
280 	}
281 }
282 
283 static void sort_relative_table_with_data(char *extab_image, int image_size)
284 {
285 	int i = 0;
286 
287 	while (i < image_size) {
288 		uint32_t *loc = (uint32_t *)(extab_image + i);
289 
290 		w(r(loc) + i, loc);
291 		w(r(loc + 1) + i + 4, loc + 1);
292 		/* Don't touch the fixup type or data */
293 
294 		i += sizeof(uint32_t) * 3;
295 	}
296 
297 	qsort(extab_image, image_size / 12, 12, compare_relative_table);
298 
299 	i = 0;
300 	while (i < image_size) {
301 		uint32_t *loc = (uint32_t *)(extab_image + i);
302 
303 		w(r(loc) - i, loc);
304 		w(r(loc + 1) - (i + 4), loc + 1);
305 		/* Don't touch the fixup type or data */
306 
307 		i += sizeof(uint32_t) * 3;
308 	}
309 }
310 
311 static int do_file(char const *const fname, void *addr)
312 {
313 	int rc = -1;
314 	Elf_Ehdr *ehdr = addr;
315 	table_sort_t custom_sort = NULL;
316 
317 	switch (ehdr->e32.e_ident[EI_DATA]) {
318 	case ELFDATA2LSB:
319 		r	= rle;
320 		r2	= r2le;
321 		r8	= r8le;
322 		w	= wle;
323 		break;
324 	case ELFDATA2MSB:
325 		r	= rbe;
326 		r2	= r2be;
327 		r8	= r8be;
328 		w	= wbe;
329 		break;
330 	default:
331 		fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
332 			ehdr->e32.e_ident[EI_DATA], fname);
333 		return -1;
334 	}
335 
336 	if (memcmp(ELFMAG, ehdr->e32.e_ident, SELFMAG) != 0 ||
337 	    (r2(&ehdr->e32.e_type) != ET_EXEC && r2(&ehdr->e32.e_type) != ET_DYN) ||
338 	    ehdr->e32.e_ident[EI_VERSION] != EV_CURRENT) {
339 		fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
340 		return -1;
341 	}
342 
343 	switch (r2(&ehdr->e32.e_machine)) {
344 	case EM_386:
345 	case EM_AARCH64:
346 	case EM_LOONGARCH:
347 	case EM_RISCV:
348 	case EM_S390:
349 	case EM_X86_64:
350 		custom_sort = sort_relative_table_with_data;
351 		break;
352 	case EM_PARISC:
353 	case EM_PPC:
354 	case EM_PPC64:
355 		custom_sort = sort_relative_table;
356 		break;
357 	case EM_ARCOMPACT:
358 	case EM_ARCV2:
359 	case EM_ARM:
360 	case EM_MICROBLAZE:
361 	case EM_MIPS:
362 	case EM_XTENSA:
363 		break;
364 	default:
365 		fprintf(stderr, "unrecognized e_machine %d %s\n",
366 			r2(&ehdr->e32.e_machine), fname);
367 		return -1;
368 	}
369 
370 	switch (ehdr->e32.e_ident[EI_CLASS]) {
371 	case ELFCLASS32:
372 		if (r2(&ehdr->e32.e_ehsize) != sizeof(Elf32_Ehdr) ||
373 		    r2(&ehdr->e32.e_shentsize) != sizeof(Elf32_Shdr)) {
374 			fprintf(stderr,
375 				"unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
376 			break;
377 		}
378 		rc = do_sort_32(ehdr, fname, custom_sort);
379 		break;
380 	case ELFCLASS64:
381 		{
382 		if (r2(&ehdr->e64.e_ehsize) != sizeof(Elf64_Ehdr) ||
383 		    r2(&ehdr->e64.e_shentsize) != sizeof(Elf64_Shdr)) {
384 			fprintf(stderr,
385 				"unrecognized ET_EXEC/ET_DYN file: %s\n",
386 				fname);
387 			break;
388 		}
389 		rc = do_sort_64(ehdr, fname, custom_sort);
390 		}
391 		break;
392 	default:
393 		fprintf(stderr, "unrecognized ELF class %d %s\n",
394 			ehdr->e32.e_ident[EI_CLASS], fname);
395 		break;
396 	}
397 
398 	return rc;
399 }
400 
401 int main(int argc, char *argv[])
402 {
403 	int i, n_error = 0;  /* gcc-4.3.0 false positive complaint */
404 	size_t size = 0;
405 	void *addr = NULL;
406 
407 	if (argc < 2) {
408 		fprintf(stderr, "usage: sorttable vmlinux...\n");
409 		return 0;
410 	}
411 
412 	/* Process each file in turn, allowing deep failure. */
413 	for (i = 1; i < argc; i++) {
414 		addr = mmap_file(argv[i], &size);
415 		if (!addr) {
416 			++n_error;
417 			continue;
418 		}
419 
420 		if (do_file(argv[i], addr))
421 			++n_error;
422 
423 		munmap(addr, size);
424 	}
425 
426 	return !!n_error;
427 }
428