1/*
2 * sortextable.c: Sort the kernel's exception table
3 *
4 * Copyright 2011 - 2012 Cavium, Inc.
5 *
6 * Based on code taken from recortmcount.c which is:
7 *
8 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>.  All rights reserved.
9 * Licensed under the GNU General Public License, version 2 (GPLv2).
10 *
11 * Restructured to fit Linux format, as well as other updates:
12 *  Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
13 */
14
15/*
16 * Strategy: alter the vmlinux file in-place.
17 */
18
19#include <sys/types.h>
20#include <sys/mman.h>
21#include <sys/stat.h>
22#include <getopt.h>
23#include <elf.h>
24#include <fcntl.h>
25#include <setjmp.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31#include <tools/be_byteshift.h>
32#include <tools/le_byteshift.h>
33
34#ifndef EM_ARCOMPACT
35#define EM_ARCOMPACT	93
36#endif
37
38#ifndef EM_XTENSA
39#define EM_XTENSA	94
40#endif
41
42#ifndef EM_AARCH64
43#define EM_AARCH64	183
44#endif
45
46#ifndef EM_MICROBLAZE
47#define EM_MICROBLAZE	189
48#endif
49
50#ifndef EM_ARCV2
51#define EM_ARCV2	195
52#endif
53
54static int fd_map;	/* File descriptor for file being modified. */
55static int mmap_failed; /* Boolean flag. */
56static void *ehdr_curr; /* current ElfXX_Ehdr *  for resource cleanup */
57static struct stat sb;	/* Remember .st_size, etc. */
58static jmp_buf jmpenv;	/* setjmp/longjmp per-file error escape */
59
60/* setjmp() return values */
61enum {
62	SJ_SETJMP = 0,  /* hardwired first return */
63	SJ_FAIL,
64	SJ_SUCCEED
65};
66
67/* Per-file resource cleanup when multiple files. */
68static void
69cleanup(void)
70{
71	if (!mmap_failed)
72		munmap(ehdr_curr, sb.st_size);
73	close(fd_map);
74}
75
76static void __attribute__((noreturn))
77fail_file(void)
78{
79	cleanup();
80	longjmp(jmpenv, SJ_FAIL);
81}
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 */
89static void *mmap_file(char const *fname)
90{
91	void *addr;
92
93	fd_map = open(fname, O_RDWR);
94	if (fd_map < 0 || fstat(fd_map, &sb) < 0) {
95		perror(fname);
96		fail_file();
97	}
98	if (!S_ISREG(sb.st_mode)) {
99		fprintf(stderr, "not a regular file: %s\n", fname);
100		fail_file();
101	}
102	addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
103		    fd_map, 0);
104	if (addr == MAP_FAILED) {
105		mmap_failed = 1;
106		fprintf(stderr, "Could not mmap file: %s\n", fname);
107		fail_file();
108	}
109	return addr;
110}
111
112static uint64_t r8be(const uint64_t *x)
113{
114	return get_unaligned_be64(x);
115}
116static uint32_t rbe(const uint32_t *x)
117{
118	return get_unaligned_be32(x);
119}
120static uint16_t r2be(const uint16_t *x)
121{
122	return get_unaligned_be16(x);
123}
124static uint64_t r8le(const uint64_t *x)
125{
126	return get_unaligned_le64(x);
127}
128static uint32_t rle(const uint32_t *x)
129{
130	return get_unaligned_le32(x);
131}
132static uint16_t r2le(const uint16_t *x)
133{
134	return get_unaligned_le16(x);
135}
136
137static void w8be(uint64_t val, uint64_t *x)
138{
139	put_unaligned_be64(val, x);
140}
141static void wbe(uint32_t val, uint32_t *x)
142{
143	put_unaligned_be32(val, x);
144}
145static void w2be(uint16_t val, uint16_t *x)
146{
147	put_unaligned_be16(val, x);
148}
149static void w8le(uint64_t val, uint64_t *x)
150{
151	put_unaligned_le64(val, x);
152}
153static void wle(uint32_t val, uint32_t *x)
154{
155	put_unaligned_le32(val, x);
156}
157static void w2le(uint16_t val, uint16_t *x)
158{
159	put_unaligned_le16(val, x);
160}
161
162static uint64_t (*r8)(const uint64_t *);
163static uint32_t (*r)(const uint32_t *);
164static uint16_t (*r2)(const uint16_t *);
165static void (*w8)(uint64_t, uint64_t *);
166static void (*w)(uint32_t, uint32_t *);
167static void (*w2)(uint16_t, uint16_t *);
168
169typedef void (*table_sort_t)(char *, int);
170
171/*
172 * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
173 * the way to -256..-1, to avoid conflicting with real section
174 * indices.
175 */
176#define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
177
178static inline int is_shndx_special(unsigned int i)
179{
180	return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
181}
182
183/* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
184static inline unsigned int get_secindex(unsigned int shndx,
185					unsigned int sym_offs,
186					const Elf32_Word *symtab_shndx_start)
187{
188	if (is_shndx_special(shndx))
189		return SPECIAL(shndx);
190	if (shndx != SHN_XINDEX)
191		return shndx;
192	return r(&symtab_shndx_start[sym_offs]);
193}
194
195/* 32 bit and 64 bit are very similar */
196#include "sortextable.h"
197#define SORTEXTABLE_64
198#include "sortextable.h"
199
200static int compare_relative_table(const void *a, const void *b)
201{
202	int32_t av = (int32_t)r(a);
203	int32_t bv = (int32_t)r(b);
204
205	if (av < bv)
206		return -1;
207	if (av > bv)
208		return 1;
209	return 0;
210}
211
212static void sort_relative_table(char *extab_image, int image_size)
213{
214	int i;
215
216	/*
217	 * Do the same thing the runtime sort does, first normalize to
218	 * being relative to the start of the section.
219	 */
220	i = 0;
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
238static void
239do_file(char const *const fname)
240{
241	table_sort_t custom_sort;
242	Elf32_Ehdr *ehdr = mmap_file(fname);
243
244	ehdr_curr = ehdr;
245	switch (ehdr->e_ident[EI_DATA]) {
246	default:
247		fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
248			ehdr->e_ident[EI_DATA], fname);
249		fail_file();
250		break;
251	case ELFDATA2LSB:
252		r = rle;
253		r2 = r2le;
254		r8 = r8le;
255		w = wle;
256		w2 = w2le;
257		w8 = w8le;
258		break;
259	case ELFDATA2MSB:
260		r = rbe;
261		r2 = r2be;
262		r8 = r8be;
263		w = wbe;
264		w2 = w2be;
265		w8 = w8be;
266		break;
267	}  /* end switch */
268	if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0
269	||  r2(&ehdr->e_type) != ET_EXEC
270	||  ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
271		fprintf(stderr, "unrecognized ET_EXEC file %s\n", fname);
272		fail_file();
273	}
274
275	custom_sort = NULL;
276	switch (r2(&ehdr->e_machine)) {
277	default:
278		fprintf(stderr, "unrecognized e_machine %d %s\n",
279			r2(&ehdr->e_machine), fname);
280		fail_file();
281		break;
282	case EM_386:
283	case EM_X86_64:
284	case EM_S390:
285		custom_sort = sort_relative_table;
286		break;
287	case EM_ARCOMPACT:
288	case EM_ARCV2:
289	case EM_ARM:
290	case EM_AARCH64:
291	case EM_MICROBLAZE:
292	case EM_MIPS:
293	case EM_XTENSA:
294		break;
295	}  /* end switch */
296
297	switch (ehdr->e_ident[EI_CLASS]) {
298	default:
299		fprintf(stderr, "unrecognized ELF class %d %s\n",
300			ehdr->e_ident[EI_CLASS], fname);
301		fail_file();
302		break;
303	case ELFCLASS32:
304		if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
305		||  r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
306			fprintf(stderr,
307				"unrecognized ET_EXEC file: %s\n", fname);
308			fail_file();
309		}
310		do32(ehdr, fname, custom_sort);
311		break;
312	case ELFCLASS64: {
313		Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
314		if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
315		||  r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
316			fprintf(stderr,
317				"unrecognized ET_EXEC file: %s\n", fname);
318			fail_file();
319		}
320		do64(ghdr, fname, custom_sort);
321		break;
322	}
323	}  /* end switch */
324
325	cleanup();
326}
327
328int
329main(int argc, char *argv[])
330{
331	int n_error = 0;  /* gcc-4.3.0 false positive complaint */
332	int i;
333
334	if (argc < 2) {
335		fprintf(stderr, "usage: sortextable vmlinux...\n");
336		return 0;
337	}
338
339	/* Process each file in turn, allowing deep failure. */
340	for (i = 1; i < argc; i++) {
341		char *file = argv[i];
342		int const sjval = setjmp(jmpenv);
343
344		switch (sjval) {
345		default:
346			fprintf(stderr, "internal error: %s\n", file);
347			exit(1);
348			break;
349		case SJ_SETJMP:    /* normal sequence */
350			/* Avoid problems if early cleanup() */
351			fd_map = -1;
352			ehdr_curr = NULL;
353			mmap_failed = 1;
354			do_file(file);
355			break;
356		case SJ_FAIL:    /* error in do_file or below */
357			++n_error;
358			break;
359		case SJ_SUCCEED:    /* premature success */
360			/* do nothing */
361			break;
362		}  /* end switch */
363	}
364	return !!n_error;
365}
366