14 #include <tools/le_byteshift.h>
16 #define ElfW(type) _ElfW(ELF_BITS, type)
17 #define _ElfW(bits, type) __ElfW(bits, type)
18 #define __ElfW(bits, type) Elf##bits##_##type
21 #define ELF_MACHINE EM_386
22 #define ELF_MACHINE_NAME "i386"
23 #define SHT_REL_TYPE SHT_REL
25 #define ELF_CLASS ELFCLASS32
26 #define ELF_R_SYM(val) ELF32_R_SYM(val)
27 #define ELF_R_TYPE(val) ELF32_R_TYPE(val)
28 #define ELF_ST_TYPE(o) ELF32_ST_TYPE(o)
29 #define ELF_ST_BIND(o) ELF32_ST_BIND(o)
30 #define ELF_ST_VISIBILITY(o) ELF32_ST_VISIBILITY(o)
32 #define Elf_Rel ElfW(Rel)
33 #define Elf_Ehdr ElfW(Ehdr)
34 #define Elf_Phdr ElfW(Phdr)
35 #define Elf_Shdr ElfW(Shdr)
36 #define Elf_Sym ElfW(Sym)
38 static void die(char *fmt, ...);
40 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
49 static struct relocs relocs16;
50 static struct relocs relocs32;
59 static struct section *secs;
69 static const char * const sym_regex_kernel[S_NSYMTYPES] = {
71 * Following symbols have been audited. There values are constant and do
72 * not change if bzImage is loaded at a different physical address than
73 * the address for which it has been compiled. Don't warn user about
74 * absolute relocations present w.r.t these symbols.
77 "^(xen_irq_disable_direct_reloc$|"
78 "xen_save_fl_direct_reloc$|"
83 * These symbols are known to be relative, even if the linker marks them
84 * as absolute (typically defined outside any section in the linker script.)
87 "^(__init_(begin|end)|"
88 "__x86_cpu_dev_(start|end)|"
89 "(__parainstructions|__alt_instructions)(|_end)|"
90 "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
91 "__(start|end)_pci_.*|"
92 "__(start|end)_builtin_fw|"
93 "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
94 "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
95 "__(start|stop)___param|"
96 "__(start|stop)___modver|"
97 "__(start|stop)___bug_table|"
98 "__tracedata_(start|end)|"
99 "__(start|stop)_notes|"
102 "(jiffies|jiffies_64)|"
107 static const char * const sym_regex_realmode[S_NSYMTYPES] = {
109 * These symbols are known to be relative, even if the linker marks them
110 * as absolute (typically defined outside any section in the linker script.)
116 * These are 16-bit segment symbols when compiling 16-bit code.
122 * These are offsets belonging to segments, as opposed to linear addresses,
123 * when compiling 16-bit code.
129 static const char * const *sym_regex;
131 static regex_t sym_regex_c[S_NSYMTYPES];
132 static int is_reloc(enum symtype type, const char *sym_name)
134 return sym_regex[type] &&
135 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
138 static void regex_init(int use_real_mode)
145 sym_regex = sym_regex_realmode;
147 sym_regex = sym_regex_kernel;
149 for (i = 0; i < S_NSYMTYPES; i++) {
153 err = regcomp(&sym_regex_c[i], sym_regex[i],
154 REG_EXTENDED|REG_NOSUB);
157 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
163 static void die(char *fmt, ...)
167 vfprintf(stderr, fmt, ap);
172 static const char *sym_type(unsigned type)
174 static const char *type_name[] = {
175 #define SYM_TYPE(X) [X] = #X
176 SYM_TYPE(STT_NOTYPE),
177 SYM_TYPE(STT_OBJECT),
179 SYM_TYPE(STT_SECTION),
181 SYM_TYPE(STT_COMMON),
185 const char *name = "unknown sym type name";
186 if (type < ARRAY_SIZE(type_name)) {
187 name = type_name[type];
192 static const char *sym_bind(unsigned bind)
194 static const char *bind_name[] = {
195 #define SYM_BIND(X) [X] = #X
197 SYM_BIND(STB_GLOBAL),
201 const char *name = "unknown sym bind name";
202 if (bind < ARRAY_SIZE(bind_name)) {
203 name = bind_name[bind];
208 static const char *sym_visibility(unsigned visibility)
210 static const char *visibility_name[] = {
211 #define SYM_VISIBILITY(X) [X] = #X
212 SYM_VISIBILITY(STV_DEFAULT),
213 SYM_VISIBILITY(STV_INTERNAL),
214 SYM_VISIBILITY(STV_HIDDEN),
215 SYM_VISIBILITY(STV_PROTECTED),
216 #undef SYM_VISIBILITY
218 const char *name = "unknown sym visibility name";
219 if (visibility < ARRAY_SIZE(visibility_name)) {
220 name = visibility_name[visibility];
225 static const char *rel_type(unsigned type)
227 static const char *type_name[] = {
228 #define REL_TYPE(X) [X] = #X
229 REL_TYPE(R_386_NONE),
231 REL_TYPE(R_386_PC32),
232 REL_TYPE(R_386_GOT32),
233 REL_TYPE(R_386_PLT32),
234 REL_TYPE(R_386_COPY),
235 REL_TYPE(R_386_GLOB_DAT),
236 REL_TYPE(R_386_JMP_SLOT),
237 REL_TYPE(R_386_RELATIVE),
238 REL_TYPE(R_386_GOTOFF),
239 REL_TYPE(R_386_GOTPC),
243 REL_TYPE(R_386_PC16),
246 const char *name = "unknown type rel type name";
247 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
248 name = type_name[type];
253 static const char *sec_name(unsigned shndx)
255 const char *sec_strtab;
257 sec_strtab = secs[ehdr.e_shstrndx].strtab;
259 if (shndx < ehdr.e_shnum) {
260 name = sec_strtab + secs[shndx].shdr.sh_name;
262 else if (shndx == SHN_ABS) {
265 else if (shndx == SHN_COMMON) {
271 static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
276 name = sym_strtab + sym->st_name;
279 name = sec_name(sym->st_shndx);
286 #if BYTE_ORDER == LITTLE_ENDIAN
287 #define le16_to_cpu(val) (val)
288 #define le32_to_cpu(val) (val)
290 #if BYTE_ORDER == BIG_ENDIAN
291 #define le16_to_cpu(val) bswap_16(val)
292 #define le32_to_cpu(val) bswap_32(val)
295 static uint16_t elf16_to_cpu(uint16_t val)
297 return le16_to_cpu(val);
300 static uint32_t elf32_to_cpu(uint32_t val)
302 return le32_to_cpu(val);
305 #define elf_half_to_cpu(x) elf16_to_cpu(x)
306 #define elf_word_to_cpu(x) elf32_to_cpu(x)
307 #define elf_addr_to_cpu(x) elf32_to_cpu(x)
308 #define elf_off_to_cpu(x) elf32_to_cpu(x)
309 #define elf_xword_to_cpu(x) elf32_to_cpu(x)
311 static void read_ehdr(FILE *fp)
313 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
314 die("Cannot read ELF header: %s\n",
317 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
318 die("No ELF magic\n");
320 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) {
321 die("Not a %d bit executable\n", ELF_BITS);
323 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
324 die("Not a LSB ELF executable\n");
326 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
327 die("Unknown ELF version\n");
329 /* Convert the fields to native endian */
330 ehdr.e_type = elf_half_to_cpu(ehdr.e_type);
331 ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine);
332 ehdr.e_version = elf_word_to_cpu(ehdr.e_version);
333 ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry);
334 ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff);
335 ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff);
336 ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags);
337 ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize);
338 ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
339 ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum);
340 ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
341 ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum);
342 ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx);
344 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
345 die("Unsupported ELF header type\n");
347 if (ehdr.e_machine != ELF_MACHINE) {
348 die("Not for %s\n", ELF_MACHINE_NAME);
350 if (ehdr.e_version != EV_CURRENT) {
351 die("Unknown ELF version\n");
353 if (ehdr.e_ehsize != sizeof(Elf_Ehdr)) {
354 die("Bad Elf header size\n");
356 if (ehdr.e_phentsize != sizeof(Elf_Phdr)) {
357 die("Bad program header entry\n");
359 if (ehdr.e_shentsize != sizeof(Elf_Shdr)) {
360 die("Bad section header entry\n");
362 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
363 die("String table index out of bounds\n");
367 static void read_shdrs(FILE *fp)
372 secs = calloc(ehdr.e_shnum, sizeof(struct section));
374 die("Unable to allocate %d section headers\n",
377 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
378 die("Seek to %d failed: %s\n",
379 ehdr.e_shoff, strerror(errno));
381 for (i = 0; i < ehdr.e_shnum; i++) {
382 struct section *sec = &secs[i];
383 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
384 die("Cannot read ELF section headers %d/%d: %s\n",
385 i, ehdr.e_shnum, strerror(errno));
386 sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
387 sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
388 sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags);
389 sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr);
390 sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset);
391 sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size);
392 sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link);
393 sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info);
394 sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
395 sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize);
396 if (sec->shdr.sh_link < ehdr.e_shnum)
397 sec->link = &secs[sec->shdr.sh_link];
402 static void read_strtabs(FILE *fp)
405 for (i = 0; i < ehdr.e_shnum; i++) {
406 struct section *sec = &secs[i];
407 if (sec->shdr.sh_type != SHT_STRTAB) {
410 sec->strtab = malloc(sec->shdr.sh_size);
412 die("malloc of %d bytes for strtab failed\n",
415 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
416 die("Seek to %d failed: %s\n",
417 sec->shdr.sh_offset, strerror(errno));
419 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
420 != sec->shdr.sh_size) {
421 die("Cannot read symbol table: %s\n",
427 static void read_symtabs(FILE *fp)
430 for (i = 0; i < ehdr.e_shnum; i++) {
431 struct section *sec = &secs[i];
432 if (sec->shdr.sh_type != SHT_SYMTAB) {
435 sec->symtab = malloc(sec->shdr.sh_size);
437 die("malloc of %d bytes for symtab failed\n",
440 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
441 die("Seek to %d failed: %s\n",
442 sec->shdr.sh_offset, strerror(errno));
444 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
445 != sec->shdr.sh_size) {
446 die("Cannot read symbol table: %s\n",
449 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
450 Elf_Sym *sym = &sec->symtab[j];
451 sym->st_name = elf_word_to_cpu(sym->st_name);
452 sym->st_value = elf_addr_to_cpu(sym->st_value);
453 sym->st_size = elf_xword_to_cpu(sym->st_size);
454 sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
460 static void read_relocs(FILE *fp)
463 for (i = 0; i < ehdr.e_shnum; i++) {
464 struct section *sec = &secs[i];
465 if (sec->shdr.sh_type != SHT_REL_TYPE) {
468 sec->reltab = malloc(sec->shdr.sh_size);
470 die("malloc of %d bytes for relocs failed\n",
473 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
474 die("Seek to %d failed: %s\n",
475 sec->shdr.sh_offset, strerror(errno));
477 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
478 != sec->shdr.sh_size) {
479 die("Cannot read symbol table: %s\n",
482 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
483 Elf_Rel *rel = &sec->reltab[j];
484 rel->r_offset = elf_addr_to_cpu(rel->r_offset);
485 rel->r_info = elf_xword_to_cpu(rel->r_info);
491 static void print_absolute_symbols(void)
494 printf("Absolute symbols\n");
495 printf(" Num: Value Size Type Bind Visibility Name\n");
496 for (i = 0; i < ehdr.e_shnum; i++) {
497 struct section *sec = &secs[i];
501 if (sec->shdr.sh_type != SHT_SYMTAB) {
504 sym_strtab = sec->link->strtab;
505 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
508 sym = &sec->symtab[j];
509 name = sym_name(sym_strtab, sym);
510 if (sym->st_shndx != SHN_ABS) {
513 printf("%5d %08x %5d %10s %10s %12s %s\n",
514 j, sym->st_value, sym->st_size,
515 sym_type(ELF_ST_TYPE(sym->st_info)),
516 sym_bind(ELF_ST_BIND(sym->st_info)),
517 sym_visibility(ELF_ST_VISIBILITY(sym->st_other)),
524 static void print_absolute_relocs(void)
528 for (i = 0; i < ehdr.e_shnum; i++) {
529 struct section *sec = &secs[i];
530 struct section *sec_applies, *sec_symtab;
534 if (sec->shdr.sh_type != SHT_REL_TYPE) {
537 sec_symtab = sec->link;
538 sec_applies = &secs[sec->shdr.sh_info];
539 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
542 sh_symtab = sec_symtab->symtab;
543 sym_strtab = sec_symtab->link->strtab;
544 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
548 rel = &sec->reltab[j];
549 sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
550 name = sym_name(sym_strtab, sym);
551 if (sym->st_shndx != SHN_ABS) {
555 /* Absolute symbols are not relocated if bzImage is
556 * loaded at a non-compiled address. Display a warning
557 * to user at compile time about the absolute
558 * relocations present.
560 * User need to audit the code to make sure
561 * some symbols which should have been section
562 * relative have not become absolute because of some
563 * linker optimization or wrong programming usage.
565 * Before warning check if this absolute symbol
566 * relocation is harmless.
568 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
572 printf("WARNING: Absolute relocations"
574 printf("Offset Info Type Sym.Value "
579 printf("%08x %08x %10s %08x %s\n",
582 rel_type(ELF_R_TYPE(rel->r_info)),
592 static void add_reloc(struct relocs *r, uint32_t offset)
594 if (r->count == r->size) {
595 unsigned long newsize = r->size + 50000;
596 void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
599 die("realloc of %ld entries for relocs failed\n",
604 r->offset[r->count++] = offset;
607 static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
608 Elf_Sym *sym, const char *symname))
611 /* Walk through the relocations */
612 for (i = 0; i < ehdr.e_shnum; i++) {
615 struct section *sec_applies, *sec_symtab;
617 struct section *sec = &secs[i];
619 if (sec->shdr.sh_type != SHT_REL_TYPE) {
622 sec_symtab = sec->link;
623 sec_applies = &secs[sec->shdr.sh_info];
624 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
627 sh_symtab = sec_symtab->symtab;
628 sym_strtab = sec_symtab->link->strtab;
629 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
630 Elf_Rel *rel = &sec->reltab[j];
631 Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
632 const char *symname = sym_name(sym_strtab, sym);
634 process(sec, rel, sym, symname);
639 static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
642 unsigned r_type = ELF32_R_TYPE(rel->r_info);
643 int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
651 * NONE can be ignored and PC relative relocations don't
652 * need to be adjusted.
659 * Whitelisted absolute symbols do not require
662 if (is_reloc(S_ABS, symname))
665 die("Invalid absolute %s relocation: %s\n",
666 rel_type(r_type), symname);
670 add_reloc(&relocs32, rel->r_offset);
674 die("Unsupported relocation type: %s (%d)\n",
675 rel_type(r_type), r_type);
682 static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
685 unsigned r_type = ELF32_R_TYPE(rel->r_info);
686 int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
694 * NONE can be ignored and PC relative relocations don't
695 * need to be adjusted.
702 * Whitelisted absolute symbols do not require
705 if (is_reloc(S_ABS, symname))
708 if (is_reloc(S_SEG, symname)) {
709 add_reloc(&relocs16, rel->r_offset);
713 if (!is_reloc(S_LIN, symname))
716 die("Invalid %s %s relocation: %s\n",
717 shn_abs ? "absolute" : "relative",
718 rel_type(r_type), symname);
724 * Whitelisted absolute symbols do not require
727 if (is_reloc(S_ABS, symname))
730 if (is_reloc(S_REL, symname)) {
731 add_reloc(&relocs32, rel->r_offset);
735 if (is_reloc(S_LIN, symname))
736 add_reloc(&relocs32, rel->r_offset);
739 die("Invalid %s %s relocation: %s\n",
740 shn_abs ? "absolute" : "relative",
741 rel_type(r_type), symname);
745 die("Unsupported relocation type: %s (%d)\n",
746 rel_type(r_type), r_type);
753 static int cmp_relocs(const void *va, const void *vb)
755 const uint32_t *a, *b;
757 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
760 static void sort_relocs(struct relocs *r)
762 qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
765 static int write32(uint32_t v, FILE *f)
767 unsigned char buf[4];
769 put_unaligned_le32(v, buf);
770 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
773 static int write32_as_text(uint32_t v, FILE *f)
775 return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
778 static void emit_relocs(int as_text, int use_real_mode)
781 int (*write_reloc)(uint32_t, FILE *) = write32;
783 /* Collect up the relocations */
784 walk_relocs(use_real_mode ? do_reloc_real : do_reloc);
786 if (relocs16.count && !use_real_mode)
787 die("Segment relocations found but --realmode not specified\n");
789 /* Order the relocations for more efficient processing */
790 sort_relocs(&relocs16);
791 sort_relocs(&relocs32);
793 /* Print the relocations */
795 /* Print the relocations in a form suitable that
798 printf(".section \".data.reloc\",\"a\"\n");
799 printf(".balign 4\n");
800 write_reloc = write32_as_text;
804 write_reloc(relocs16.count, stdout);
805 for (i = 0; i < relocs16.count; i++)
806 write_reloc(relocs16.offset[i], stdout);
808 write_reloc(relocs32.count, stdout);
809 for (i = 0; i < relocs32.count; i++)
810 write_reloc(relocs32.offset[i], stdout);
813 write_reloc(0, stdout);
815 /* Now print each relocation */
816 for (i = 0; i < relocs32.count; i++)
817 write_reloc(relocs32.offset[i], stdout);
821 static void usage(void)
823 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
826 int main(int argc, char **argv)
828 int show_absolute_syms, show_absolute_relocs;
829 int as_text, use_real_mode;
834 show_absolute_syms = 0;
835 show_absolute_relocs = 0;
839 for (i = 1; i < argc; i++) {
842 if (strcmp(arg, "--abs-syms") == 0) {
843 show_absolute_syms = 1;
846 if (strcmp(arg, "--abs-relocs") == 0) {
847 show_absolute_relocs = 1;
850 if (strcmp(arg, "--text") == 0) {
854 if (strcmp(arg, "--realmode") == 0) {
868 regex_init(use_real_mode);
869 fp = fopen(fname, "r");
871 die("Cannot open %s: %s\n",
872 fname, strerror(errno));
879 if (show_absolute_syms) {
880 print_absolute_symbols();
883 if (show_absolute_relocs) {
884 print_absolute_relocs();
887 emit_relocs(as_text, use_real_mode);