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