xref: /linux-6.15/scripts/sorttable.c (revision 4f48a28b)
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 /* 32 bit and 64 bit are very similar */
177 #include "sorttable.h"
178 #define SORTTABLE_64
179 #include "sorttable.h"
180 
181 static int compare_relative_table(const void *a, const void *b)
182 {
183 	int32_t av = (int32_t)r(a);
184 	int32_t bv = (int32_t)r(b);
185 
186 	if (av < bv)
187 		return -1;
188 	if (av > bv)
189 		return 1;
190 	return 0;
191 }
192 
193 static void sort_relative_table(char *extab_image, int image_size)
194 {
195 	int i = 0;
196 
197 	/*
198 	 * Do the same thing the runtime sort does, first normalize to
199 	 * being relative to the start of the section.
200 	 */
201 	while (i < image_size) {
202 		uint32_t *loc = (uint32_t *)(extab_image + i);
203 		w(r(loc) + i, loc);
204 		i += 4;
205 	}
206 
207 	qsort(extab_image, image_size / 8, 8, compare_relative_table);
208 
209 	/* Now denormalize. */
210 	i = 0;
211 	while (i < image_size) {
212 		uint32_t *loc = (uint32_t *)(extab_image + i);
213 		w(r(loc) - i, loc);
214 		i += 4;
215 	}
216 }
217 
218 static void sort_relative_table_with_data(char *extab_image, int image_size)
219 {
220 	int i = 0;
221 
222 	while (i < image_size) {
223 		uint32_t *loc = (uint32_t *)(extab_image + i);
224 
225 		w(r(loc) + i, loc);
226 		w(r(loc + 1) + i + 4, loc + 1);
227 		/* Don't touch the fixup type or data */
228 
229 		i += sizeof(uint32_t) * 3;
230 	}
231 
232 	qsort(extab_image, image_size / 12, 12, compare_relative_table);
233 
234 	i = 0;
235 	while (i < image_size) {
236 		uint32_t *loc = (uint32_t *)(extab_image + i);
237 
238 		w(r(loc) - i, loc);
239 		w(r(loc + 1) - (i + 4), loc + 1);
240 		/* Don't touch the fixup type or data */
241 
242 		i += sizeof(uint32_t) * 3;
243 	}
244 }
245 
246 static int do_file(char const *const fname, void *addr)
247 {
248 	int rc = -1;
249 	Elf32_Ehdr *ehdr = addr;
250 	table_sort_t custom_sort = NULL;
251 
252 	switch (ehdr->e_ident[EI_DATA]) {
253 	case ELFDATA2LSB:
254 		r	= rle;
255 		r2	= r2le;
256 		r8	= r8le;
257 		w	= wle;
258 		break;
259 	case ELFDATA2MSB:
260 		r	= rbe;
261 		r2	= r2be;
262 		r8	= r8be;
263 		w	= wbe;
264 		break;
265 	default:
266 		fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
267 			ehdr->e_ident[EI_DATA], fname);
268 		return -1;
269 	}
270 
271 	if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
272 	    (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
273 	    ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
274 		fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
275 		return -1;
276 	}
277 
278 	switch (r2(&ehdr->e_machine)) {
279 	case EM_386:
280 	case EM_AARCH64:
281 	case EM_LOONGARCH:
282 	case EM_RISCV:
283 	case EM_S390:
284 	case EM_X86_64:
285 		custom_sort = sort_relative_table_with_data;
286 		break;
287 	case EM_PARISC:
288 	case EM_PPC:
289 	case EM_PPC64:
290 		custom_sort = sort_relative_table;
291 		break;
292 	case EM_ARCOMPACT:
293 	case EM_ARCV2:
294 	case EM_ARM:
295 	case EM_MICROBLAZE:
296 	case EM_MIPS:
297 	case EM_XTENSA:
298 		break;
299 	default:
300 		fprintf(stderr, "unrecognized e_machine %d %s\n",
301 			r2(&ehdr->e_machine), fname);
302 		return -1;
303 	}
304 
305 	switch (ehdr->e_ident[EI_CLASS]) {
306 	case ELFCLASS32:
307 		if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
308 		    r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
309 			fprintf(stderr,
310 				"unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
311 			break;
312 		}
313 		rc = do_sort_32(ehdr, fname, custom_sort);
314 		break;
315 	case ELFCLASS64:
316 		{
317 		Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
318 		if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
319 		    r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
320 			fprintf(stderr,
321 				"unrecognized ET_EXEC/ET_DYN file: %s\n",
322 				fname);
323 			break;
324 		}
325 		rc = do_sort_64(ghdr, fname, custom_sort);
326 		}
327 		break;
328 	default:
329 		fprintf(stderr, "unrecognized ELF class %d %s\n",
330 			ehdr->e_ident[EI_CLASS], fname);
331 		break;
332 	}
333 
334 	return rc;
335 }
336 
337 int main(int argc, char *argv[])
338 {
339 	int i, n_error = 0;  /* gcc-4.3.0 false positive complaint */
340 	size_t size = 0;
341 	void *addr = NULL;
342 
343 	if (argc < 2) {
344 		fprintf(stderr, "usage: sorttable vmlinux...\n");
345 		return 0;
346 	}
347 
348 	/* Process each file in turn, allowing deep failure. */
349 	for (i = 1; i < argc; i++) {
350 		addr = mmap_file(argv[i], &size);
351 		if (!addr) {
352 			++n_error;
353 			continue;
354 		}
355 
356 		if (do_file(argv[i], addr))
357 			++n_error;
358 
359 		munmap(addr, size);
360 	}
361 
362 	return !!n_error;
363 }
364