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