Adding upstream version 4.00~pre61+dfsg.
[syslinux-debian/hramrach.git] / com32 / tools / relocs.c
blobbe57bf7ecddc5eef0326d1f4569bb3f9d82caa2c
1 /*
2 * This file is taken from the Linux kernel and is distributed under GPL v2.
3 */
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <unistd.h>
11 #include <elf.h>
12 #include <byteswap.h>
13 #define USE_BSD
14 #include <endian.h>
15 #include <regex.h>
16 #include <sys/types.h>
18 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
19 static Elf32_Ehdr ehdr;
20 static unsigned long reloc_count, reloc_idx;
21 static unsigned long *relocs;
23 struct section {
24 Elf32_Shdr shdr;
25 struct section *link;
26 Elf32_Sym *symtab;
27 Elf32_Rel *reltab;
28 char *strtab;
30 static struct section *secs;
32 static void die(char *fmt, ...)
34 va_list ap;
35 va_start(ap, fmt);
36 vfprintf(stderr, fmt, ap);
37 va_end(ap);
38 exit(1);
42 * Following symbols have been audited. Don't warn user about
43 * absolute relocations present w.r.t these symbols.
46 /* True absolute relocations */
48 static const char safe_abs_regex[] =
49 "^(__.*_len|__.*_dwords)$";
50 static regex_t safe_abs_regex_c;
52 static int is_safe_abs_reloc(const char *sym_name)
54 return !regexec(&safe_abs_regex_c, sym_name, 0, NULL, 0);
57 /* These are relative even though the linker marks them absolute */
59 static const char safe_rel_regex[] =
60 "^(__.*_start|__.*_end|_end|_[se](text|data))$";
61 static regex_t safe_rel_regex_c;
63 static int is_safe_rel_reloc(const char *sym_name)
65 return !regexec(&safe_rel_regex_c, sym_name, 0, NULL, 0);
68 static void regex_init(void)
70 char errbuf[128];
71 int err;
73 err = regcomp(&safe_abs_regex_c, safe_abs_regex,
74 REG_EXTENDED|REG_NOSUB);
75 if (err) {
76 regerror(err, &safe_abs_regex_c, errbuf, sizeof errbuf);
77 die("%s", errbuf);
80 err = regcomp(&safe_rel_regex_c, safe_rel_regex,
81 REG_EXTENDED|REG_NOSUB);
82 if (err) {
83 regerror(err, &safe_rel_regex_c, errbuf, sizeof errbuf);
84 die("%s", errbuf);
88 static const char *sym_type(unsigned type)
90 static const char *type_name[] = {
91 #define SYM_TYPE(X) [X] = #X
92 SYM_TYPE(STT_NOTYPE),
93 SYM_TYPE(STT_OBJECT),
94 SYM_TYPE(STT_FUNC),
95 SYM_TYPE(STT_SECTION),
96 SYM_TYPE(STT_FILE),
97 SYM_TYPE(STT_COMMON),
98 SYM_TYPE(STT_TLS),
99 #undef SYM_TYPE
101 const char *name = "unknown sym type name";
102 if (type < ARRAY_SIZE(type_name)) {
103 name = type_name[type];
105 return name;
108 static const char *sym_bind(unsigned bind)
110 static const char *bind_name[] = {
111 #define SYM_BIND(X) [X] = #X
112 SYM_BIND(STB_LOCAL),
113 SYM_BIND(STB_GLOBAL),
114 SYM_BIND(STB_WEAK),
115 #undef SYM_BIND
117 const char *name = "unknown sym bind name";
118 if (bind < ARRAY_SIZE(bind_name)) {
119 name = bind_name[bind];
121 return name;
124 static const char *sym_visibility(unsigned visibility)
126 static const char *visibility_name[] = {
127 #define SYM_VISIBILITY(X) [X] = #X
128 SYM_VISIBILITY(STV_DEFAULT),
129 SYM_VISIBILITY(STV_INTERNAL),
130 SYM_VISIBILITY(STV_HIDDEN),
131 SYM_VISIBILITY(STV_PROTECTED),
132 #undef SYM_VISIBILITY
134 const char *name = "unknown sym visibility name";
135 if (visibility < ARRAY_SIZE(visibility_name)) {
136 name = visibility_name[visibility];
138 return name;
141 static const char *rel_type(unsigned type)
143 static const char *type_name[] = {
144 #define REL_TYPE(X) [X] = #X
145 REL_TYPE(R_386_NONE),
146 REL_TYPE(R_386_32),
147 REL_TYPE(R_386_PC32),
148 REL_TYPE(R_386_GOT32),
149 REL_TYPE(R_386_PLT32),
150 REL_TYPE(R_386_COPY),
151 REL_TYPE(R_386_GLOB_DAT),
152 REL_TYPE(R_386_JMP_SLOT),
153 REL_TYPE(R_386_RELATIVE),
154 REL_TYPE(R_386_GOTOFF),
155 REL_TYPE(R_386_GOTPC),
156 #undef REL_TYPE
158 const char *name = NULL;
159 if (type < ARRAY_SIZE(type_name))
160 name = type_name[type];
161 if (!name)
162 name = "unknown";
163 return name;
166 static const char *sec_name(unsigned shndx)
168 const char *sec_strtab;
169 const char *name;
170 sec_strtab = secs[ehdr.e_shstrndx].strtab;
171 name = "<noname>";
172 if (shndx < ehdr.e_shnum) {
173 name = sec_strtab + secs[shndx].shdr.sh_name;
175 else if (shndx == SHN_ABS) {
176 name = "ABSOLUTE";
178 else if (shndx == SHN_COMMON) {
179 name = "COMMON";
181 return name;
184 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
186 const char *name;
187 name = "<noname>";
188 if (sym->st_name) {
189 name = sym_strtab + sym->st_name;
191 else {
192 name = sec_name(secs[sym->st_shndx].shdr.sh_name);
194 return name;
199 #if BYTE_ORDER == LITTLE_ENDIAN
200 #define le16_to_cpu(val) (val)
201 #define le32_to_cpu(val) (val)
202 #endif
203 #if BYTE_ORDER == BIG_ENDIAN
204 #define le16_to_cpu(val) bswap_16(val)
205 #define le32_to_cpu(val) bswap_32(val)
206 #endif
208 static uint16_t elf16_to_cpu(uint16_t val)
210 return le16_to_cpu(val);
213 static uint32_t elf32_to_cpu(uint32_t val)
215 return le32_to_cpu(val);
218 static void read_ehdr(FILE *fp)
220 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
221 die("Cannot read ELF header: %s\n",
222 strerror(errno));
224 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
225 die("No ELF magic\n");
227 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
228 die("Not a 32 bit executable\n");
230 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
231 die("Not a LSB ELF executable\n");
233 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
234 die("Unknown ELF version\n");
236 /* Convert the fields to native endian */
237 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
238 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
239 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
240 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
241 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
242 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
243 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
244 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
245 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
246 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
247 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
248 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
249 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
251 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
252 die("Unsupported ELF header type\n");
254 if (ehdr.e_machine != EM_386) {
255 die("Not for x86\n");
257 if (ehdr.e_version != EV_CURRENT) {
258 die("Unknown ELF version\n");
260 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
261 die("Bad Elf header size\n");
263 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
264 die("Bad program header entry\n");
266 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
267 die("Bad section header entry\n");
269 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
270 die("String table index out of bounds\n");
274 static void read_shdrs(FILE *fp)
276 int i;
277 Elf32_Shdr shdr;
279 secs = calloc(ehdr.e_shnum, sizeof(struct section));
280 if (!secs) {
281 die("Unable to allocate %d section headers\n",
282 ehdr.e_shnum);
284 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
285 die("Seek to %d failed: %s\n",
286 ehdr.e_shoff, strerror(errno));
288 for (i = 0; i < ehdr.e_shnum; i++) {
289 struct section *sec = &secs[i];
290 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
291 die("Cannot read ELF section headers %d/%d: %s\n",
292 i, ehdr.e_shnum, strerror(errno));
293 sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name);
294 sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type);
295 sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags);
296 sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr);
297 sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset);
298 sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size);
299 sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link);
300 sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info);
301 sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign);
302 sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize);
303 if (sec->shdr.sh_link < ehdr.e_shnum)
304 sec->link = &secs[sec->shdr.sh_link];
309 static void read_strtabs(FILE *fp)
311 int i;
312 for (i = 0; i < ehdr.e_shnum; i++) {
313 struct section *sec = &secs[i];
314 if (sec->shdr.sh_type != SHT_STRTAB) {
315 continue;
317 sec->strtab = malloc(sec->shdr.sh_size);
318 if (!sec->strtab) {
319 die("malloc of %d bytes for strtab failed\n",
320 sec->shdr.sh_size);
322 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
323 die("Seek to %d failed: %s\n",
324 sec->shdr.sh_offset, strerror(errno));
326 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
327 != sec->shdr.sh_size) {
328 die("Cannot read symbol table: %s\n",
329 strerror(errno));
334 static void read_symtabs(FILE *fp)
336 int i,j;
337 for (i = 0; i < ehdr.e_shnum; i++) {
338 struct section *sec = &secs[i];
339 if (sec->shdr.sh_type != SHT_SYMTAB) {
340 continue;
342 sec->symtab = malloc(sec->shdr.sh_size);
343 if (!sec->symtab) {
344 die("malloc of %d bytes for symtab failed\n",
345 sec->shdr.sh_size);
347 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
348 die("Seek to %d failed: %s\n",
349 sec->shdr.sh_offset, strerror(errno));
351 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
352 != sec->shdr.sh_size) {
353 die("Cannot read symbol table: %s\n",
354 strerror(errno));
356 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
357 Elf32_Sym *sym = &sec->symtab[j];
358 sym->st_name = elf32_to_cpu(sym->st_name);
359 sym->st_value = elf32_to_cpu(sym->st_value);
360 sym->st_size = elf32_to_cpu(sym->st_size);
361 sym->st_shndx = elf16_to_cpu(sym->st_shndx);
367 static void read_relocs(FILE *fp)
369 int i,j;
370 for (i = 0; i < ehdr.e_shnum; i++) {
371 struct section *sec = &secs[i];
372 if (sec->shdr.sh_type != SHT_REL) {
373 continue;
375 sec->reltab = malloc(sec->shdr.sh_size);
376 if (!sec->reltab) {
377 die("malloc of %d bytes for relocs failed\n",
378 sec->shdr.sh_size);
380 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
381 die("Seek to %d failed: %s\n",
382 sec->shdr.sh_offset, strerror(errno));
384 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
385 != sec->shdr.sh_size) {
386 die("Cannot read symbol table: %s\n",
387 strerror(errno));
389 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
390 Elf32_Rel *rel = &sec->reltab[j];
391 rel->r_offset = elf32_to_cpu(rel->r_offset);
392 rel->r_info = elf32_to_cpu(rel->r_info);
398 static void print_absolute_symbols(void)
400 int i;
401 printf("Absolute symbols\n");
402 printf(" Num: Value Size Type Bind Visibility Name\n");
403 for (i = 0; i < ehdr.e_shnum; i++) {
404 struct section *sec = &secs[i];
405 char *sym_strtab;
406 Elf32_Sym *sh_symtab;
407 int j;
409 if (sec->shdr.sh_type != SHT_SYMTAB) {
410 continue;
412 sh_symtab = sec->symtab;
413 sym_strtab = sec->link->strtab;
414 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
415 Elf32_Sym *sym;
416 const char *name;
417 sym = &sec->symtab[j];
418 name = sym_name(sym_strtab, sym);
419 if (sym->st_shndx != SHN_ABS) {
420 continue;
422 printf("%5d %08x %5d %10s %10s %12s %s\n",
423 j, sym->st_value, sym->st_size,
424 sym_type(ELF32_ST_TYPE(sym->st_info)),
425 sym_bind(ELF32_ST_BIND(sym->st_info)),
426 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
427 name);
430 printf("\n");
433 static int print_absolute_relocs(FILE *f)
435 int i, printed = 0;
437 for (i = 0; i < ehdr.e_shnum; i++) {
438 struct section *sec = &secs[i];
439 struct section *sec_applies, *sec_symtab;
440 char *sym_strtab;
441 Elf32_Sym *sh_symtab;
442 int j;
443 if (sec->shdr.sh_type != SHT_REL) {
444 continue;
446 sec_symtab = sec->link;
447 sec_applies = &secs[sec->shdr.sh_info];
448 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
449 continue;
451 sh_symtab = sec_symtab->symtab;
452 sym_strtab = sec_symtab->link->strtab;
453 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
454 Elf32_Rel *rel;
455 Elf32_Sym *sym;
456 const char *name;
457 rel = &sec->reltab[j];
458 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
459 name = sym_name(sym_strtab, sym);
460 if (sym->st_shndx != SHN_ABS) {
461 continue;
464 /* Absolute symbols are not relocated if bzImage is
465 * loaded at a non-compiled address. Display a warning
466 * to user at compile time about the absolute
467 * relocations present.
469 * User need to audit the code to make sure
470 * some symbols which should have been section
471 * relative have not become absolute because of some
472 * linker optimization or wrong programming usage.
474 * Before warning check if this absolute symbol
475 * relocation is harmless.
477 if (is_safe_abs_reloc(name) ||
478 is_safe_rel_reloc(name))
479 continue;
481 if (!printed) {
482 fprintf(f, "Unknown absolute relocations present\n");
483 fprintf(f, "Offset Info Type Sym.Value Sym.Name\n");
484 printed = 1;
487 fprintf(f, "%08x %08x %10s %08x %s\n",
488 rel->r_offset,
489 rel->r_info,
490 rel_type(ELF32_R_TYPE(rel->r_info)),
491 sym->st_value,
492 name);
496 if (printed)
497 fputc('\n', f);
499 return printed;
502 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
504 int i;
505 /* Walk through the relocations */
506 for (i = 0; i < ehdr.e_shnum; i++) {
507 char *sym_strtab;
508 Elf32_Sym *sh_symtab;
509 struct section *sec_applies, *sec_symtab;
510 int j;
511 struct section *sec = &secs[i];
513 if (sec->shdr.sh_type != SHT_REL) {
514 continue;
516 sec_symtab = sec->link;
517 sec_applies = &secs[sec->shdr.sh_info];
518 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
519 continue;
521 sh_symtab = sec_symtab->symtab;
522 sym_strtab = sec_symtab->link->strtab;
523 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
524 Elf32_Rel *rel;
525 Elf32_Sym *sym;
526 unsigned r_type;
527 rel = &sec->reltab[j];
528 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
529 r_type = ELF32_R_TYPE(rel->r_info);
530 /* Don't visit relocations to absolute symbols */
531 if (sym->st_shndx == SHN_ABS &&
532 !is_safe_rel_reloc(sym_name(sym_strtab, sym)))
533 continue;
535 switch (r_type) {
536 case R_386_NONE:
537 case R_386_PC32:
538 case R_386_GOTPC:
539 case R_386_GOTOFF:
540 case R_386_GOT32:
541 case R_386_PLT32:
542 /* Relative relocations don't need to
543 be adjusted */
544 break;
545 case R_386_32:
546 /* Visit relocations that need adjustment */
547 visit(rel, sym);
548 break;
549 default:
550 die("Unsupported relocation type: %s (%d)\n",
551 rel_type(r_type), r_type);
557 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
559 (void)rel; (void)sym;
560 reloc_count += 1;
563 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
565 (void)sym;
567 /* Remember the address that needs to be adjusted. */
568 relocs[reloc_idx++] = rel->r_offset;
571 static int cmp_relocs(const void *va, const void *vb)
573 const unsigned long *a, *b;
574 a = va; b = vb;
575 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
578 static void emit_relocs(int as_text)
580 int i;
581 /* Count how many relocations I have and allocate space for them. */
582 reloc_count = 0;
583 walk_relocs(count_reloc);
584 relocs = malloc(reloc_count * sizeof(relocs[0]));
585 if (!relocs) {
586 die("malloc of %d entries for relocs failed\n",
587 reloc_count);
589 /* Collect up the relocations */
590 reloc_idx = 0;
591 walk_relocs(collect_reloc);
593 /* Order the relocations for more efficient processing */
594 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
596 /* Print the relocations */
597 if (as_text) {
598 /* Print the relocations in a form suitable that
599 * gas will like.
601 printf(".section \".data.reloc\",\"a\"\n");
602 printf(".balign 4\n");
603 for (i = 0; i < reloc_count; i++) {
604 printf("\t .long 0x%08lx\n", relocs[i]);
606 printf("\n");
608 else {
609 unsigned char buf[4];
610 /* Now print each relocation */
611 for (i = 0; i < reloc_count; i++) {
612 buf[0] = (relocs[i] >> 0) & 0xff;
613 buf[1] = (relocs[i] >> 8) & 0xff;
614 buf[2] = (relocs[i] >> 16) & 0xff;
615 buf[3] = (relocs[i] >> 24) & 0xff;
616 fwrite(buf, 4, 1, stdout);
618 /* Print a stop */
619 memset(buf, 0, sizeof buf);
620 fwrite(buf, 4, 1, stdout);
624 static void usage(void)
626 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
629 int main(int argc, char **argv)
631 int show_absolute_syms, show_absolute_relocs;
632 int as_text;
633 const char *fname;
634 FILE *fp;
635 int i;
636 int err = 0;
638 show_absolute_syms = 0;
639 show_absolute_relocs = 0;
640 as_text = 0;
641 fname = NULL;
642 for (i = 1; i < argc; i++) {
643 char *arg = argv[i];
644 if (*arg == '-') {
645 if (strcmp(argv[1], "--abs-syms") == 0) {
646 show_absolute_syms = 1;
647 continue;
650 if (strcmp(argv[1], "--abs-relocs") == 0) {
651 show_absolute_relocs = 1;
652 continue;
654 else if (strcmp(argv[1], "--text") == 0) {
655 as_text = 1;
656 continue;
659 else if (!fname) {
660 fname = arg;
661 continue;
663 usage();
665 if (!fname) {
666 usage();
670 regex_init();
672 fp = fopen(fname, "r");
673 if (!fp) {
674 die("Cannot open %s: %s\n",
675 fname, strerror(errno));
677 read_ehdr(fp);
678 read_shdrs(fp);
679 read_strtabs(fp);
680 read_symtabs(fp);
681 read_relocs(fp);
682 if (show_absolute_syms) {
683 print_absolute_symbols();
684 return 0;
686 if (show_absolute_relocs) {
687 print_absolute_relocs(stdout);
688 return 0;
690 err = print_absolute_relocs(stderr);
691 emit_relocs(as_text);
692 return err;