blob: cd9762ba4467438306f742acde67e66218b47c0c [file] [log] [blame]
Thomas Gleixner4317cf92019-05-31 01:09:38 -07001// SPDX-License-Identifier: GPL-2.0-only
David Daneya79f2482012-04-19 14:59:55 -07002/*
3 * sortextable.c: Sort the kernel's exception table
4 *
David Daneyd59a1682012-04-24 11:23:14 -07005 * Copyright 2011 - 2012 Cavium, Inc.
David Daneya79f2482012-04-19 14:59:55 -07006 *
7 * Based on code taken from recortmcount.c which is:
8 *
9 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
David Daneya79f2482012-04-19 14:59:55 -070010 *
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>
David Daneya79f2482012-04-19 14:59:55 -070025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
David Daneyd59a1682012-04-24 11:23:14 -070030#include <tools/be_byteshift.h>
31#include <tools/le_byteshift.h>
32
Vineet Guptaf06d19e2013-11-15 12:08:05 +053033#ifndef EM_ARCOMPACT
34#define EM_ARCOMPACT 93
35#endif
36
Max Filippov25df8192014-02-18 15:29:11 +040037#ifndef EM_XTENSA
38#define EM_XTENSA 94
39#endif
40
Will Deaconadace892013-05-08 17:29:24 +010041#ifndef EM_AARCH64
42#define EM_AARCH64 183
43#endif
44
Michal Simek372c7202014-01-23 15:52:46 -080045#ifndef EM_MICROBLAZE
46#define EM_MICROBLAZE 189
47#endif
48
Vineet Guptab3210d142013-11-22 13:05:58 +053049#ifndef EM_ARCV2
50#define EM_ARCV2 195
51#endif
52
David Daneya79f2482012-04-19 14:59:55 -070053/*
54 * Get the whole file as a programming convenience in order to avoid
55 * malloc+lseek+read+free of many pieces. If successful, then mmap
56 * avoids copying unused pieces; else just read the whole file.
57 * Open for both read and write.
58 */
Shile Zhang3c47b782019-12-04 08:46:27 +080059static void *mmap_file(char const *fname, size_t *size)
David Daneya79f2482012-04-19 14:59:55 -070060{
Shile Zhang3c47b782019-12-04 08:46:27 +080061 int fd;
62 struct stat sb;
63 void *addr = NULL;
David Daneya79f2482012-04-19 14:59:55 -070064
Shile Zhang3c47b782019-12-04 08:46:27 +080065 fd = open(fname, O_RDWR);
66 if (fd < 0) {
David Daneya79f2482012-04-19 14:59:55 -070067 perror(fname);
Shile Zhang3c47b782019-12-04 08:46:27 +080068 return NULL;
69 }
70 if (fstat(fd, &sb) < 0) {
71 perror(fname);
72 goto out;
David Daneya79f2482012-04-19 14:59:55 -070073 }
74 if (!S_ISREG(sb.st_mode)) {
75 fprintf(stderr, "not a regular file: %s\n", fname);
Shile Zhang3c47b782019-12-04 08:46:27 +080076 goto out;
David Daneya79f2482012-04-19 14:59:55 -070077 }
Shile Zhang3c47b782019-12-04 08:46:27 +080078 addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
David Daneya79f2482012-04-19 14:59:55 -070079 if (addr == MAP_FAILED) {
David Daneya79f2482012-04-19 14:59:55 -070080 fprintf(stderr, "Could not mmap file: %s\n", fname);
Shile Zhang3c47b782019-12-04 08:46:27 +080081 goto out;
David Daneya79f2482012-04-19 14:59:55 -070082 }
Shile Zhang3c47b782019-12-04 08:46:27 +080083
84 *size = sb.st_size;
85
86out:
87 close(fd);
David Daneya79f2482012-04-19 14:59:55 -070088 return addr;
89}
90
David Daneyd59a1682012-04-24 11:23:14 -070091static uint64_t r8be(const uint64_t *x)
David Daneya79f2482012-04-19 14:59:55 -070092{
David Daneyd59a1682012-04-24 11:23:14 -070093 return get_unaligned_be64(x);
94}
95static uint32_t rbe(const uint32_t *x)
96{
97 return get_unaligned_be32(x);
98}
99static uint16_t r2be(const uint16_t *x)
100{
101 return get_unaligned_be16(x);
102}
103static uint64_t r8le(const uint64_t *x)
104{
105 return get_unaligned_le64(x);
106}
107static uint32_t rle(const uint32_t *x)
108{
109 return get_unaligned_le32(x);
110}
111static uint16_t r2le(const uint16_t *x)
112{
113 return get_unaligned_le16(x);
David Daneya79f2482012-04-19 14:59:55 -0700114}
115
David Daneyd59a1682012-04-24 11:23:14 -0700116static void w8be(uint64_t val, uint64_t *x)
David Daneya79f2482012-04-19 14:59:55 -0700117{
David Daneyd59a1682012-04-24 11:23:14 -0700118 put_unaligned_be64(val, x);
119}
120static void wbe(uint32_t val, uint32_t *x)
121{
122 put_unaligned_be32(val, x);
123}
124static void w2be(uint16_t val, uint16_t *x)
125{
126 put_unaligned_be16(val, x);
127}
128static void w8le(uint64_t val, uint64_t *x)
129{
130 put_unaligned_le64(val, x);
131}
132static void wle(uint32_t val, uint32_t *x)
133{
134 put_unaligned_le32(val, x);
135}
136static void w2le(uint16_t val, uint16_t *x)
137{
138 put_unaligned_le16(val, x);
David Daneya79f2482012-04-19 14:59:55 -0700139}
140
David Daneyd59a1682012-04-24 11:23:14 -0700141static uint64_t (*r8)(const uint64_t *);
142static uint32_t (*r)(const uint32_t *);
143static uint16_t (*r2)(const uint16_t *);
144static void (*w8)(uint64_t, uint64_t *);
145static void (*w)(uint32_t, uint32_t *);
146static void (*w2)(uint16_t, uint16_t *);
David Daneya79f2482012-04-19 14:59:55 -0700147
David Daneyd59a1682012-04-24 11:23:14 -0700148typedef void (*table_sort_t)(char *, int);
David Daneya79f2482012-04-19 14:59:55 -0700149
Jamie Iles59c36452013-11-12 15:06:51 -0800150/*
151 * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
152 * the way to -256..-1, to avoid conflicting with real section
153 * indices.
154 */
155#define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
156
157static inline int is_shndx_special(unsigned int i)
158{
159 return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
160}
161
162/* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
163static inline unsigned int get_secindex(unsigned int shndx,
164 unsigned int sym_offs,
165 const Elf32_Word *symtab_shndx_start)
166{
167 if (is_shndx_special(shndx))
168 return SPECIAL(shndx);
169 if (shndx != SHN_XINDEX)
170 return shndx;
171 return r(&symtab_shndx_start[sym_offs]);
172}
173
David Daneya79f2482012-04-19 14:59:55 -0700174/* 32 bit and 64 bit are very similar */
175#include "sortextable.h"
176#define SORTEXTABLE_64
177#include "sortextable.h"
178
Heiko Carstenseb608fb2012-09-05 13:26:11 +0200179static int compare_relative_table(const void *a, const void *b)
David Daneyd59a1682012-04-24 11:23:14 -0700180{
181 int32_t av = (int32_t)r(a);
182 int32_t bv = (int32_t)r(b);
183
184 if (av < bv)
185 return -1;
186 if (av > bv)
187 return 1;
188 return 0;
189}
190
Tony Luck548acf12016-02-17 10:20:12 -0800191static void x86_sort_relative_table(char *extab_image, int image_size)
192{
193 int i;
194
195 i = 0;
196 while (i < image_size) {
197 uint32_t *loc = (uint32_t *)(extab_image + i);
198
199 w(r(loc) + i, loc);
200 w(r(loc + 1) + i + 4, loc + 1);
201 w(r(loc + 2) + i + 8, loc + 2);
202
203 i += sizeof(uint32_t) * 3;
204 }
205
206 qsort(extab_image, image_size / 12, 12, compare_relative_table);
207
208 i = 0;
209 while (i < image_size) {
210 uint32_t *loc = (uint32_t *)(extab_image + i);
211
212 w(r(loc) - i, loc);
213 w(r(loc + 1) - (i + 4), loc + 1);
214 w(r(loc + 2) - (i + 8), loc + 2);
215
216 i += sizeof(uint32_t) * 3;
217 }
218}
219
Heiko Carstenseb608fb2012-09-05 13:26:11 +0200220static void sort_relative_table(char *extab_image, int image_size)
David Daneyd59a1682012-04-24 11:23:14 -0700221{
222 int i;
223
224 /*
225 * Do the same thing the runtime sort does, first normalize to
226 * being relative to the start of the section.
227 */
228 i = 0;
229 while (i < image_size) {
230 uint32_t *loc = (uint32_t *)(extab_image + i);
231 w(r(loc) + i, loc);
232 i += 4;
233 }
234
Heiko Carstenseb608fb2012-09-05 13:26:11 +0200235 qsort(extab_image, image_size / 8, 8, compare_relative_table);
David Daneyd59a1682012-04-24 11:23:14 -0700236
237 /* Now denormalize. */
238 i = 0;
239 while (i < image_size) {
240 uint32_t *loc = (uint32_t *)(extab_image + i);
241 w(r(loc) - i, loc);
242 i += 4;
243 }
244}
David Daneya79f2482012-04-19 14:59:55 -0700245
Shile Zhang3c47b782019-12-04 08:46:27 +0800246static int
247do_file(char const *const fname, void *addr)
David Daneya79f2482012-04-19 14:59:55 -0700248{
Shile Zhang3c47b782019-12-04 08:46:27 +0800249 table_sort_t custom_sort = NULL;
250 Elf32_Ehdr *ehdr = addr;
251 int rc = -1;
David Daneya79f2482012-04-19 14:59:55 -0700252
David Daneya79f2482012-04-19 14:59:55 -0700253 switch (ehdr->e_ident[EI_DATA]) {
David Daneya79f2482012-04-19 14:59:55 -0700254 default:
255 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
256 ehdr->e_ident[EI_DATA], fname);
Shile Zhang3c47b782019-12-04 08:46:27 +0800257 return -1;
David Daneya79f2482012-04-19 14:59:55 -0700258 case ELFDATA2LSB:
David Daneyd59a1682012-04-24 11:23:14 -0700259 r = rle;
260 r2 = r2le;
261 r8 = r8le;
262 w = wle;
263 w2 = w2le;
264 w8 = w8le;
David Daneya79f2482012-04-19 14:59:55 -0700265 break;
266 case ELFDATA2MSB:
David Daneyd59a1682012-04-24 11:23:14 -0700267 r = rbe;
268 r2 = r2be;
269 r8 = r8be;
270 w = wbe;
271 w2 = w2be;
272 w8 = w8be;
David Daneya79f2482012-04-19 14:59:55 -0700273 break;
274 } /* end switch */
275 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0
Ard Biesheuvel7b957b62016-01-10 11:42:28 +0100276 || (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN)
David Daneya79f2482012-04-19 14:59:55 -0700277 || ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
Ard Biesheuvel7b957b62016-01-10 11:42:28 +0100278 fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
Shile Zhang3c47b782019-12-04 08:46:27 +0800279 return -1;
David Daneya79f2482012-04-19 14:59:55 -0700280 }
281
David Daneyd59a1682012-04-24 11:23:14 -0700282 custom_sort = NULL;
283 switch (r2(&ehdr->e_machine)) {
David Daneya79f2482012-04-19 14:59:55 -0700284 default:
285 fprintf(stderr, "unrecognized e_machine %d %s\n",
David Daneyd59a1682012-04-24 11:23:14 -0700286 r2(&ehdr->e_machine), fname);
David Daneya79f2482012-04-19 14:59:55 -0700287 break;
288 case EM_386:
David Daneya79f2482012-04-19 14:59:55 -0700289 case EM_X86_64:
Tony Luck548acf12016-02-17 10:20:12 -0800290 custom_sort = x86_sort_relative_table;
291 break;
292
Heiko Carstens3193a982012-07-24 14:51:34 +0200293 case EM_S390:
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100294 case EM_AARCH64:
Helge Deller0de79852016-03-23 16:00:46 +0100295 case EM_PARISC:
Nicholas Piggin5b9ff022016-10-13 16:42:55 +1100296 case EM_PPC:
297 case EM_PPC64:
Heiko Carstenseb608fb2012-09-05 13:26:11 +0200298 custom_sort = sort_relative_table;
299 break;
Vineet Guptaf06d19e2013-11-15 12:08:05 +0530300 case EM_ARCOMPACT:
Vineet Guptab3210d142013-11-22 13:05:58 +0530301 case EM_ARCV2:
Stephen Boydee951c62012-10-29 19:19:34 +0100302 case EM_ARM:
Michal Simek372c7202014-01-23 15:52:46 -0800303 case EM_MICROBLAZE:
David Daneyd59a1682012-04-24 11:23:14 -0700304 case EM_MIPS:
Max Filippov25df8192014-02-18 15:29:11 +0400305 case EM_XTENSA:
David Daneya79f2482012-04-19 14:59:55 -0700306 break;
307 } /* end switch */
308
309 switch (ehdr->e_ident[EI_CLASS]) {
310 default:
311 fprintf(stderr, "unrecognized ELF class %d %s\n",
312 ehdr->e_ident[EI_CLASS], fname);
David Daneya79f2482012-04-19 14:59:55 -0700313 break;
314 case ELFCLASS32:
David Daneyd59a1682012-04-24 11:23:14 -0700315 if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
316 || r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
David Daneya79f2482012-04-19 14:59:55 -0700317 fprintf(stderr,
Ard Biesheuvel7b957b62016-01-10 11:42:28 +0100318 "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
Shile Zhang3c47b782019-12-04 08:46:27 +0800319 break;
David Daneya79f2482012-04-19 14:59:55 -0700320 }
Shile Zhang3c47b782019-12-04 08:46:27 +0800321 rc = do32(ehdr, fname, custom_sort);
David Daneya79f2482012-04-19 14:59:55 -0700322 break;
323 case ELFCLASS64: {
324 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
David Daneyd59a1682012-04-24 11:23:14 -0700325 if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
326 || r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
David Daneya79f2482012-04-19 14:59:55 -0700327 fprintf(stderr,
Ard Biesheuvel7b957b62016-01-10 11:42:28 +0100328 "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
Shile Zhang3c47b782019-12-04 08:46:27 +0800329 break;
David Daneya79f2482012-04-19 14:59:55 -0700330 }
Shile Zhang3c47b782019-12-04 08:46:27 +0800331 rc = do64(ghdr, fname, custom_sort);
David Daneya79f2482012-04-19 14:59:55 -0700332 break;
333 }
334 } /* end switch */
335
Shile Zhang3c47b782019-12-04 08:46:27 +0800336 return rc;
David Daneya79f2482012-04-19 14:59:55 -0700337}
338
339int
340main(int argc, char *argv[])
341{
Shile Zhang3c47b782019-12-04 08:46:27 +0800342 int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
343 size_t size = 0;
344 void *addr = NULL;
David Daneya79f2482012-04-19 14:59:55 -0700345
346 if (argc < 2) {
347 fprintf(stderr, "usage: sortextable vmlinux...\n");
348 return 0;
349 }
350
351 /* Process each file in turn, allowing deep failure. */
352 for (i = 1; i < argc; i++) {
Shile Zhang3c47b782019-12-04 08:46:27 +0800353 addr = mmap_file(argv[i], &size);
354 if (!addr) {
David Daneya79f2482012-04-19 14:59:55 -0700355 ++n_error;
Shile Zhang3c47b782019-12-04 08:46:27 +0800356 continue;
357 }
358
359 if (do_file(argv[i], addr))
360 ++n_error;
361
362 munmap(addr, size);
David Daneya79f2482012-04-19 14:59:55 -0700363 }
364 return !!n_error;
365}