scripts/sortextable: Remove dead code
[linux-2.6-block.git] / scripts / sortextable.c
CommitLineData
4317cf95 1// SPDX-License-Identifier: GPL-2.0-only
a79f248b
DD
2/*
3 * sortextable.c: Sort the kernel's exception table
4 *
d59a1683 5 * Copyright 2011 - 2012 Cavium, Inc.
a79f248b
DD
6 *
7 * Based on code taken from recortmcount.c which is:
8 *
9 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
a79f248b
DD
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>
a79f248b
DD
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
d59a1683
DD
30#include <tools/be_byteshift.h>
31#include <tools/le_byteshift.h>
32
f06d19e4
VG
33#ifndef EM_ARCOMPACT
34#define EM_ARCOMPACT 93
35#endif
36
25df8198
MF
37#ifndef EM_XTENSA
38#define EM_XTENSA 94
39#endif
40
adace895
WD
41#ifndef EM_AARCH64
42#define EM_AARCH64 183
43#endif
44
372c7209
MS
45#ifndef EM_MICROBLAZE
46#define EM_MICROBLAZE 189
47#endif
48
b3210d14
VG
49#ifndef EM_ARCV2
50#define EM_ARCV2 195
51#endif
52
6402e141
SZ
53static uint32_t (*r)(const uint32_t *);
54static uint16_t (*r2)(const uint16_t *);
55static uint64_t (*r8)(const uint64_t *);
56static void (*w)(uint32_t, uint32_t *);
57static void (*w2)(uint16_t, uint16_t *);
58static void (*w8)(uint64_t, uint64_t *);
59typedef void (*table_sort_t)(char *, int);
60
a79f248b
DD
61/*
62 * Get the whole file as a programming convenience in order to avoid
63 * malloc+lseek+read+free of many pieces. If successful, then mmap
64 * avoids copying unused pieces; else just read the whole file.
65 * Open for both read and write.
66 */
3c47b787 67static void *mmap_file(char const *fname, size_t *size)
a79f248b 68{
3c47b787
SZ
69 int fd;
70 struct stat sb;
71 void *addr = NULL;
a79f248b 72
3c47b787
SZ
73 fd = open(fname, O_RDWR);
74 if (fd < 0) {
a79f248b 75 perror(fname);
3c47b787
SZ
76 return NULL;
77 }
78 if (fstat(fd, &sb) < 0) {
79 perror(fname);
80 goto out;
a79f248b
DD
81 }
82 if (!S_ISREG(sb.st_mode)) {
83 fprintf(stderr, "not a regular file: %s\n", fname);
3c47b787 84 goto out;
a79f248b 85 }
6402e141 86
3c47b787 87 addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
a79f248b 88 if (addr == MAP_FAILED) {
a79f248b 89 fprintf(stderr, "Could not mmap file: %s\n", fname);
3c47b787 90 goto out;
a79f248b 91 }
3c47b787
SZ
92
93 *size = sb.st_size;
94
95out:
96 close(fd);
a79f248b
DD
97 return addr;
98}
99
d59a1683 100static uint32_t rbe(const uint32_t *x)
a79f248b 101{
d59a1683 102 return get_unaligned_be32(x);
a79f248b 103}
6402e141 104
d59a1683 105static uint16_t r2be(const uint16_t *x)
a79f248b 106{
d59a1683 107 return get_unaligned_be16(x);
a79f248b 108}
6402e141
SZ
109
110static uint64_t r8be(const uint64_t *x)
a79f248b 111{
6402e141 112 return get_unaligned_be64(x);
a79f248b 113}
6402e141 114
d59a1683
DD
115static uint32_t rle(const uint32_t *x)
116{
117 return get_unaligned_le32(x);
118}
6402e141 119
d59a1683 120static uint16_t r2le(const uint16_t *x)
a79f248b 121{
d59a1683 122 return get_unaligned_le16(x);
a79f248b
DD
123}
124
6402e141 125static uint64_t r8le(const uint64_t *x)
d59a1683 126{
6402e141 127 return get_unaligned_le64(x);
d59a1683 128}
6402e141 129
d59a1683
DD
130static void wbe(uint32_t val, uint32_t *x)
131{
132 put_unaligned_be32(val, x);
133}
6402e141 134
d59a1683
DD
135static void w2be(uint16_t val, uint16_t *x)
136{
137 put_unaligned_be16(val, x);
138}
6402e141
SZ
139
140static void w8be(uint64_t val, uint64_t *x)
d59a1683 141{
6402e141 142 put_unaligned_be64(val, x);
d59a1683 143}
6402e141 144
d59a1683
DD
145static void wle(uint32_t val, uint32_t *x)
146{
147 put_unaligned_le32(val, x);
148}
6402e141 149
d59a1683 150static void w2le(uint16_t val, uint16_t *x)
a79f248b 151{
d59a1683 152 put_unaligned_le16(val, x);
a79f248b
DD
153}
154
6402e141
SZ
155static void w8le(uint64_t val, uint64_t *x)
156{
157 put_unaligned_le64(val, x);
158}
a79f248b 159
59c36455
JI
160/*
161 * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
162 * the way to -256..-1, to avoid conflicting with real section
163 * indices.
164 */
165#define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
166
167static inline int is_shndx_special(unsigned int i)
168{
169 return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
170}
171
172/* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
173static inline unsigned int get_secindex(unsigned int shndx,
174 unsigned int sym_offs,
175 const Elf32_Word *symtab_shndx_start)
176{
177 if (is_shndx_special(shndx))
178 return SPECIAL(shndx);
179 if (shndx != SHN_XINDEX)
180 return shndx;
181 return r(&symtab_shndx_start[sym_offs]);
182}
183
a79f248b
DD
184/* 32 bit and 64 bit are very similar */
185#include "sortextable.h"
186#define SORTEXTABLE_64
187#include "sortextable.h"
188
eb608fb3 189static int compare_relative_table(const void *a, const void *b)
d59a1683
DD
190{
191 int32_t av = (int32_t)r(a);
192 int32_t bv = (int32_t)r(b);
193
194 if (av < bv)
195 return -1;
196 if (av > bv)
197 return 1;
198 return 0;
199}
200
6402e141 201static void sort_relative_table(char *extab_image, int image_size)
548acf19 202{
6402e141 203 int i = 0;
548acf19 204
6402e141
SZ
205 /*
206 * Do the same thing the runtime sort does, first normalize to
207 * being relative to the start of the section.
208 */
548acf19
TL
209 while (i < image_size) {
210 uint32_t *loc = (uint32_t *)(extab_image + i);
548acf19 211 w(r(loc) + i, loc);
6402e141 212 i += 4;
548acf19
TL
213 }
214
6402e141 215 qsort(extab_image, image_size / 8, 8, compare_relative_table);
548acf19 216
6402e141 217 /* Now denormalize. */
548acf19
TL
218 i = 0;
219 while (i < image_size) {
220 uint32_t *loc = (uint32_t *)(extab_image + i);
548acf19 221 w(r(loc) - i, loc);
6402e141 222 i += 4;
548acf19
TL
223 }
224}
225
6402e141 226static void x86_sort_relative_table(char *extab_image, int image_size)
d59a1683 227{
6402e141 228 int i = 0;
d59a1683 229
d59a1683
DD
230 while (i < image_size) {
231 uint32_t *loc = (uint32_t *)(extab_image + i);
6402e141 232
d59a1683 233 w(r(loc) + i, loc);
6402e141
SZ
234 w(r(loc + 1) + i + 4, loc + 1);
235 w(r(loc + 2) + i + 8, loc + 2);
236
237 i += sizeof(uint32_t) * 3;
d59a1683
DD
238 }
239
6402e141 240 qsort(extab_image, image_size / 12, 12, compare_relative_table);
d59a1683 241
d59a1683
DD
242 i = 0;
243 while (i < image_size) {
244 uint32_t *loc = (uint32_t *)(extab_image + i);
6402e141 245
d59a1683 246 w(r(loc) - i, loc);
6402e141
SZ
247 w(r(loc + 1) - (i + 4), loc + 1);
248 w(r(loc + 2) - (i + 8), loc + 2);
249
250 i += sizeof(uint32_t) * 3;
d59a1683
DD
251 }
252}
a79f248b 253
6402e141 254static int do_file(char const *const fname, void *addr)
a79f248b 255{
3c47b787 256 int rc = -1;
6402e141
SZ
257 Elf32_Ehdr *ehdr = addr;
258 table_sort_t custom_sort = NULL;
a79f248b 259
a79f248b 260 switch (ehdr->e_ident[EI_DATA]) {
a79f248b 261 case ELFDATA2LSB:
6402e141
SZ
262 r = rle;
263 r2 = r2le;
264 r8 = r8le;
265 w = wle;
266 w2 = w2le;
267 w8 = w8le;
a79f248b
DD
268 break;
269 case ELFDATA2MSB:
6402e141
SZ
270 r = rbe;
271 r2 = r2be;
272 r8 = r8be;
273 w = wbe;
274 w2 = w2be;
275 w8 = w8be;
a79f248b 276 break;
6402e141
SZ
277 default:
278 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
279 ehdr->e_ident[EI_DATA], fname);
280 return -1;
281 }
282
283 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
284 (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
285 ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
7b957b6e 286 fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
3c47b787 287 return -1;
a79f248b
DD
288 }
289
d59a1683 290 switch (r2(&ehdr->e_machine)) {
a79f248b 291 case EM_386:
a79f248b 292 case EM_X86_64:
548acf19
TL
293 custom_sort = x86_sort_relative_table;
294 break;
3193a98d 295 case EM_S390:
6c94f27a 296 case EM_AARCH64:
0de79858 297 case EM_PARISC:
5b9ff027
NP
298 case EM_PPC:
299 case EM_PPC64:
eb608fb3
HC
300 custom_sort = sort_relative_table;
301 break;
f06d19e4 302 case EM_ARCOMPACT:
b3210d14 303 case EM_ARCV2:
ee951c63 304 case EM_ARM:
372c7209 305 case EM_MICROBLAZE:
d59a1683 306 case EM_MIPS:
25df8198 307 case EM_XTENSA:
a79f248b 308 break;
6402e141
SZ
309 default:
310 fprintf(stderr, "unrecognized e_machine %d %s\n",
311 r2(&ehdr->e_machine), fname);
312 return -1;
313 }
a79f248b
DD
314
315 switch (ehdr->e_ident[EI_CLASS]) {
a79f248b 316 case ELFCLASS32:
6402e141
SZ
317 if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
318 r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
a79f248b 319 fprintf(stderr,
7b957b6e 320 "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
3c47b787 321 break;
a79f248b 322 }
3c47b787 323 rc = do32(ehdr, fname, custom_sort);
a79f248b 324 break;
6402e141
SZ
325 case ELFCLASS64:
326 {
a79f248b 327 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
6402e141
SZ
328 if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
329 r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
a79f248b 330 fprintf(stderr,
6402e141
SZ
331 "unrecognized ET_EXEC/ET_DYN file: %s\n",
332 fname);
3c47b787 333 break;
a79f248b 334 }
3c47b787 335 rc = do64(ghdr, fname, custom_sort);
6402e141
SZ
336 }
337 break;
338 default:
339 fprintf(stderr, "unrecognized ELF class %d %s\n",
340 ehdr->e_ident[EI_CLASS], fname);
a79f248b
DD
341 break;
342 }
a79f248b 343
3c47b787 344 return rc;
a79f248b
DD
345}
346
6402e141 347int main(int argc, char *argv[])
a79f248b 348{
3c47b787
SZ
349 int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
350 size_t size = 0;
351 void *addr = NULL;
a79f248b
DD
352
353 if (argc < 2) {
354 fprintf(stderr, "usage: sortextable vmlinux...\n");
355 return 0;
356 }
357
358 /* Process each file in turn, allowing deep failure. */
359 for (i = 1; i < argc; i++) {
3c47b787
SZ
360 addr = mmap_file(argv[i], &size);
361 if (!addr) {
362 ++n_error;
363 continue;
364 }
a79f248b 365
3c47b787 366 if (do_file(argv[i], addr))
a79f248b 367 ++n_error;
3c47b787
SZ
368
369 munmap(addr, size);
a79f248b 370 }
6402e141 371
a79f248b
DD
372 return !!n_error;
373}