]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/i386/boot/compressed/relocs.c
[PATCH] i386: Relocatable kernel support
[mirror_ubuntu-artful-kernel.git] / arch / i386 / boot / compressed / relocs.c
CommitLineData
968de4f0
EB
1#include <stdio.h>
2#include <stdarg.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <string.h>
6#include <errno.h>
7#include <unistd.h>
8#include <elf.h>
9#include <byteswap.h>
10#define USE_BSD
11#include <endian.h>
12
13#define MAX_SHDRS 100
14static Elf32_Ehdr ehdr;
15static Elf32_Shdr shdr[MAX_SHDRS];
16static Elf32_Sym *symtab[MAX_SHDRS];
17static Elf32_Rel *reltab[MAX_SHDRS];
18static char *strtab[MAX_SHDRS];
19static unsigned long reloc_count, reloc_idx;
20static unsigned long *relocs;
21
22static void die(char *fmt, ...)
23{
24 va_list ap;
25 va_start(ap, fmt);
26 vfprintf(stderr, fmt, ap);
27 va_end(ap);
28 exit(1);
29}
30
31static const char *sym_type(unsigned type)
32{
33 static const char *type_name[] = {
34#define SYM_TYPE(X) [X] = #X
35 SYM_TYPE(STT_NOTYPE),
36 SYM_TYPE(STT_OBJECT),
37 SYM_TYPE(STT_FUNC),
38 SYM_TYPE(STT_SECTION),
39 SYM_TYPE(STT_FILE),
40 SYM_TYPE(STT_COMMON),
41 SYM_TYPE(STT_TLS),
42#undef SYM_TYPE
43 };
44 const char *name = "unknown sym type name";
45 if (type < sizeof(type_name)/sizeof(type_name[0])) {
46 name = type_name[type];
47 }
48 return name;
49}
50
51static const char *sym_bind(unsigned bind)
52{
53 static const char *bind_name[] = {
54#define SYM_BIND(X) [X] = #X
55 SYM_BIND(STB_LOCAL),
56 SYM_BIND(STB_GLOBAL),
57 SYM_BIND(STB_WEAK),
58#undef SYM_BIND
59 };
60 const char *name = "unknown sym bind name";
61 if (bind < sizeof(bind_name)/sizeof(bind_name[0])) {
62 name = bind_name[bind];
63 }
64 return name;
65}
66
67static const char *sym_visibility(unsigned visibility)
68{
69 static const char *visibility_name[] = {
70#define SYM_VISIBILITY(X) [X] = #X
71 SYM_VISIBILITY(STV_DEFAULT),
72 SYM_VISIBILITY(STV_INTERNAL),
73 SYM_VISIBILITY(STV_HIDDEN),
74 SYM_VISIBILITY(STV_PROTECTED),
75#undef SYM_VISIBILITY
76 };
77 const char *name = "unknown sym visibility name";
78 if (visibility < sizeof(visibility_name)/sizeof(visibility_name[0])) {
79 name = visibility_name[visibility];
80 }
81 return name;
82}
83
84static const char *rel_type(unsigned type)
85{
86 static const char *type_name[] = {
87#define REL_TYPE(X) [X] = #X
88 REL_TYPE(R_386_NONE),
89 REL_TYPE(R_386_32),
90 REL_TYPE(R_386_PC32),
91 REL_TYPE(R_386_GOT32),
92 REL_TYPE(R_386_PLT32),
93 REL_TYPE(R_386_COPY),
94 REL_TYPE(R_386_GLOB_DAT),
95 REL_TYPE(R_386_JMP_SLOT),
96 REL_TYPE(R_386_RELATIVE),
97 REL_TYPE(R_386_GOTOFF),
98 REL_TYPE(R_386_GOTPC),
99#undef REL_TYPE
100 };
101 const char *name = "unknown type rel type name";
102 if (type < sizeof(type_name)/sizeof(type_name[0])) {
103 name = type_name[type];
104 }
105 return name;
106}
107
108static const char *sec_name(unsigned shndx)
109{
110 const char *sec_strtab;
111 const char *name;
112 sec_strtab = strtab[ehdr.e_shstrndx];
113 name = "<noname>";
114 if (shndx < ehdr.e_shnum) {
115 name = sec_strtab + shdr[shndx].sh_name;
116 }
117 else if (shndx == SHN_ABS) {
118 name = "ABSOLUTE";
119 }
120 else if (shndx == SHN_COMMON) {
121 name = "COMMON";
122 }
123 return name;
124}
125
126static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
127{
128 const char *name;
129 name = "<noname>";
130 if (sym->st_name) {
131 name = sym_strtab + sym->st_name;
132 }
133 else {
134 name = sec_name(shdr[sym->st_shndx].sh_name);
135 }
136 return name;
137}
138
139
140
141#if BYTE_ORDER == LITTLE_ENDIAN
142#define le16_to_cpu(val) (val)
143#define le32_to_cpu(val) (val)
144#endif
145#if BYTE_ORDER == BIG_ENDIAN
146#define le16_to_cpu(val) bswap_16(val)
147#define le32_to_cpu(val) bswap_32(val)
148#endif
149
150static uint16_t elf16_to_cpu(uint16_t val)
151{
152 return le16_to_cpu(val);
153}
154
155static uint32_t elf32_to_cpu(uint32_t val)
156{
157 return le32_to_cpu(val);
158}
159
160static void read_ehdr(FILE *fp)
161{
162 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
163 die("Cannot read ELF header: %s\n",
164 strerror(errno));
165 }
166 if (memcmp(ehdr.e_ident, ELFMAG, 4) != 0) {
167 die("No ELF magic\n");
168 }
169 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
170 die("Not a 32 bit executable\n");
171 }
172 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
173 die("Not a LSB ELF executable\n");
174 }
175 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
176 die("Unknown ELF version\n");
177 }
178 /* Convert the fields to native endian */
179 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
180 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
181 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
182 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
183 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
184 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
185 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
186 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
187 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
188 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
189 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
190 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
191 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
192
193 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
194 die("Unsupported ELF header type\n");
195 }
196 if (ehdr.e_machine != EM_386) {
197 die("Not for x86\n");
198 }
199 if (ehdr.e_version != EV_CURRENT) {
200 die("Unknown ELF version\n");
201 }
202 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
203 die("Bad Elf header size\n");
204 }
205 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
206 die("Bad program header entry\n");
207 }
208 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
209 die("Bad section header entry\n");
210 }
211 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
212 die("String table index out of bounds\n");
213 }
214}
215
216static void read_shdrs(FILE *fp)
217{
218 int i;
219 if (ehdr.e_shnum > MAX_SHDRS) {
220 die("%d section headers supported: %d\n",
221 ehdr.e_shnum, MAX_SHDRS);
222 }
223 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
224 die("Seek to %d failed: %s\n",
225 ehdr.e_shoff, strerror(errno));
226 }
227 if (fread(&shdr, sizeof(shdr[0]), ehdr.e_shnum, fp) != ehdr.e_shnum) {
228 die("Cannot read ELF section headers: %s\n",
229 strerror(errno));
230 }
231 for(i = 0; i < ehdr.e_shnum; i++) {
232 shdr[i].sh_name = elf32_to_cpu(shdr[i].sh_name);
233 shdr[i].sh_type = elf32_to_cpu(shdr[i].sh_type);
234 shdr[i].sh_flags = elf32_to_cpu(shdr[i].sh_flags);
235 shdr[i].sh_addr = elf32_to_cpu(shdr[i].sh_addr);
236 shdr[i].sh_offset = elf32_to_cpu(shdr[i].sh_offset);
237 shdr[i].sh_size = elf32_to_cpu(shdr[i].sh_size);
238 shdr[i].sh_link = elf32_to_cpu(shdr[i].sh_link);
239 shdr[i].sh_info = elf32_to_cpu(shdr[i].sh_info);
240 shdr[i].sh_addralign = elf32_to_cpu(shdr[i].sh_addralign);
241 shdr[i].sh_entsize = elf32_to_cpu(shdr[i].sh_entsize);
242 }
243
244}
245
246static void read_strtabs(FILE *fp)
247{
248 int i;
249 for(i = 0; i < ehdr.e_shnum; i++) {
250 if (shdr[i].sh_type != SHT_STRTAB) {
251 continue;
252 }
253 strtab[i] = malloc(shdr[i].sh_size);
254 if (!strtab[i]) {
255 die("malloc of %d bytes for strtab failed\n",
256 shdr[i].sh_size);
257 }
258 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
259 die("Seek to %d failed: %s\n",
260 shdr[i].sh_offset, strerror(errno));
261 }
262 if (fread(strtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
263 die("Cannot read symbol table: %s\n",
264 strerror(errno));
265 }
266 }
267}
268
269static void read_symtabs(FILE *fp)
270{
271 int i,j;
272 for(i = 0; i < ehdr.e_shnum; i++) {
273 if (shdr[i].sh_type != SHT_SYMTAB) {
274 continue;
275 }
276 symtab[i] = malloc(shdr[i].sh_size);
277 if (!symtab[i]) {
278 die("malloc of %d bytes for symtab failed\n",
279 shdr[i].sh_size);
280 }
281 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
282 die("Seek to %d failed: %s\n",
283 shdr[i].sh_offset, strerror(errno));
284 }
285 if (fread(symtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
286 die("Cannot read symbol table: %s\n",
287 strerror(errno));
288 }
289 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[i][0]); j++) {
290 symtab[i][j].st_name = elf32_to_cpu(symtab[i][j].st_name);
291 symtab[i][j].st_value = elf32_to_cpu(symtab[i][j].st_value);
292 symtab[i][j].st_size = elf32_to_cpu(symtab[i][j].st_size);
293 symtab[i][j].st_shndx = elf16_to_cpu(symtab[i][j].st_shndx);
294 }
295 }
296}
297
298
299static void read_relocs(FILE *fp)
300{
301 int i,j;
302 for(i = 0; i < ehdr.e_shnum; i++) {
303 if (shdr[i].sh_type != SHT_REL) {
304 continue;
305 }
306 reltab[i] = malloc(shdr[i].sh_size);
307 if (!reltab[i]) {
308 die("malloc of %d bytes for relocs failed\n",
309 shdr[i].sh_size);
310 }
311 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
312 die("Seek to %d failed: %s\n",
313 shdr[i].sh_offset, strerror(errno));
314 }
315 if (fread(reltab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
316 die("Cannot read symbol table: %s\n",
317 strerror(errno));
318 }
319 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
320 reltab[i][j].r_offset = elf32_to_cpu(reltab[i][j].r_offset);
321 reltab[i][j].r_info = elf32_to_cpu(reltab[i][j].r_info);
322 }
323 }
324}
325
326
327static void print_absolute_symbols(void)
328{
329 int i;
330 printf("Absolute symbols\n");
331 printf(" Num: Value Size Type Bind Visibility Name\n");
332 for(i = 0; i < ehdr.e_shnum; i++) {
333 char *sym_strtab;
334 Elf32_Sym *sh_symtab;
335 int j;
336 if (shdr[i].sh_type != SHT_SYMTAB) {
337 continue;
338 }
339 sh_symtab = symtab[i];
340 sym_strtab = strtab[shdr[i].sh_link];
341 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[0][0]); j++) {
342 Elf32_Sym *sym;
343 const char *name;
344 sym = &symtab[i][j];
345 name = sym_name(sym_strtab, sym);
346 if (sym->st_shndx != SHN_ABS) {
347 continue;
348 }
349 printf("%5d %08x %5d %10s %10s %12s %s\n",
350 j, sym->st_value, sym->st_size,
351 sym_type(ELF32_ST_TYPE(sym->st_info)),
352 sym_bind(ELF32_ST_BIND(sym->st_info)),
353 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
354 name);
355 }
356 }
357 printf("\n");
358}
359
360static void print_absolute_relocs(void)
361{
362 int i;
363 printf("Absolute relocations\n");
364 printf("Offset Info Type Sym.Value Sym.Name\n");
365 for(i = 0; i < ehdr.e_shnum; i++) {
366 char *sym_strtab;
367 Elf32_Sym *sh_symtab;
368 unsigned sec_applies, sec_symtab;
369 int j;
370 if (shdr[i].sh_type != SHT_REL) {
371 continue;
372 }
373 sec_symtab = shdr[i].sh_link;
374 sec_applies = shdr[i].sh_info;
375 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
376 continue;
377 }
378 sh_symtab = symtab[sec_symtab];
379 sym_strtab = strtab[shdr[sec_symtab].sh_link];
380 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
381 Elf32_Rel *rel;
382 Elf32_Sym *sym;
383 const char *name;
384 rel = &reltab[i][j];
385 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
386 name = sym_name(sym_strtab, sym);
387 if (sym->st_shndx != SHN_ABS) {
388 continue;
389 }
390 printf("%08x %08x %10s %08x %s\n",
391 rel->r_offset,
392 rel->r_info,
393 rel_type(ELF32_R_TYPE(rel->r_info)),
394 sym->st_value,
395 name);
396 }
397 }
398 printf("\n");
399}
400
401static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
402{
403 int i;
404 /* Walk through the relocations */
405 for(i = 0; i < ehdr.e_shnum; i++) {
406 char *sym_strtab;
407 Elf32_Sym *sh_symtab;
408 unsigned sec_applies, sec_symtab;
409 int j;
410 if (shdr[i].sh_type != SHT_REL) {
411 continue;
412 }
413 sec_symtab = shdr[i].sh_link;
414 sec_applies = shdr[i].sh_info;
415 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
416 continue;
417 }
418 sh_symtab = symtab[sec_symtab];
419 sym_strtab = strtab[shdr[sec_symtab].sh_link];
420 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
421 Elf32_Rel *rel;
422 Elf32_Sym *sym;
423 unsigned r_type;
424 rel = &reltab[i][j];
425 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
426 r_type = ELF32_R_TYPE(rel->r_info);
427 /* Don't visit relocations to absolute symbols */
428 if (sym->st_shndx == SHN_ABS) {
429 continue;
430 }
431 if (r_type == R_386_PC32) {
432 /* PC relative relocations don't need to be adjusted */
433 }
434 else if (r_type == R_386_32) {
435 /* Visit relocations that need to be adjusted */
436 visit(rel, sym);
437 }
438 else {
439 die("Unsupported relocation type: %d\n", r_type);
440 }
441 }
442 }
443}
444
445static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
446{
447 reloc_count += 1;
448}
449
450static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
451{
452 /* Remember the address that needs to be adjusted. */
453 relocs[reloc_idx++] = rel->r_offset;
454}
455
456static int cmp_relocs(const void *va, const void *vb)
457{
458 const unsigned long *a, *b;
459 a = va; b = vb;
460 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
461}
462
463static void emit_relocs(int as_text)
464{
465 int i;
466 /* Count how many relocations I have and allocate space for them. */
467 reloc_count = 0;
468 walk_relocs(count_reloc);
469 relocs = malloc(reloc_count * sizeof(relocs[0]));
470 if (!relocs) {
471 die("malloc of %d entries for relocs failed\n",
472 reloc_count);
473 }
474 /* Collect up the relocations */
475 reloc_idx = 0;
476 walk_relocs(collect_reloc);
477
478 /* Order the relocations for more efficient processing */
479 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
480
481 /* Print the relocations */
482 if (as_text) {
483 /* Print the relocations in a form suitable that
484 * gas will like.
485 */
486 printf(".section \".data.reloc\",\"a\"\n");
487 printf(".balign 4\n");
488 for(i = 0; i < reloc_count; i++) {
489 printf("\t .long 0x%08lx\n", relocs[i]);
490 }
491 printf("\n");
492 }
493 else {
494 unsigned char buf[4];
495 buf[0] = buf[1] = buf[2] = buf[3] = 0;
496 /* Print a stop */
497 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
498 /* Now print each relocation */
499 for(i = 0; i < reloc_count; i++) {
500 buf[0] = (relocs[i] >> 0) & 0xff;
501 buf[1] = (relocs[i] >> 8) & 0xff;
502 buf[2] = (relocs[i] >> 16) & 0xff;
503 buf[3] = (relocs[i] >> 24) & 0xff;
504 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
505 }
506 }
507}
508
509static void usage(void)
510{
511 die("i386_reloc [--abs | --text] vmlinux\n");
512}
513
514int main(int argc, char **argv)
515{
516 int show_absolute;
517 int as_text;
518 const char *fname;
519 FILE *fp;
520 int i;
521
522 show_absolute = 0;
523 as_text = 0;
524 fname = NULL;
525 for(i = 1; i < argc; i++) {
526 char *arg = argv[i];
527 if (*arg == '-') {
528 if (strcmp(argv[1], "--abs") == 0) {
529 show_absolute = 1;
530 continue;
531 }
532 else if (strcmp(argv[1], "--text") == 0) {
533 as_text = 1;
534 continue;
535 }
536 }
537 else if (!fname) {
538 fname = arg;
539 continue;
540 }
541 usage();
542 }
543 if (!fname) {
544 usage();
545 }
546 fp = fopen(fname, "r");
547 if (!fp) {
548 die("Cannot open %s: %s\n",
549 fname, strerror(errno));
550 }
551 read_ehdr(fp);
552 read_shdrs(fp);
553 read_strtabs(fp);
554 read_symtabs(fp);
555 read_relocs(fp);
556 if (show_absolute) {
557 print_absolute_symbols();
558 print_absolute_relocs();
559 return 0;
560 }
561 emit_relocs(as_text);
562 return 0;
563}