13 #include <tools/le_byteshift.h>
15 static void die(char *fmt, ...);
17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
18 static Elf32_Ehdr ehdr;
19 static unsigned long reloc_count, reloc_idx;
20 static unsigned long *relocs;
21 static unsigned long reloc16_count, reloc16_idx;
22 static unsigned long *relocs16;
31 static struct section *secs;
41 static const char * const sym_regex_kernel[S_NSYMTYPES] = {
43 * Following symbols have been audited. There values are constant and do
44 * not change if bzImage is loaded at a different physical address than
45 * the address for which it has been compiled. Don't warn user about
46 * absolute relocations present w.r.t these symbols.
49 "^(xen_irq_disable_direct_reloc$|"
50 "xen_save_fl_direct_reloc$|"
55 * These symbols are known to be relative, even if the linker marks them
56 * as absolute (typically defined outside any section in the linker script.)
63 static const char * const sym_regex_realmode[S_NSYMTYPES] = {
65 * These symbols are known to be relative, even if the linker marks them
66 * as absolute (typically defined outside any section in the linker script.)
72 * These are 16-bit segment symbols when compiling 16-bit code.
78 * These are offsets belonging to segments, as opposed to linear addresses,
79 * when compiling 16-bit code.
85 static const char * const *sym_regex;
87 static regex_t sym_regex_c[S_NSYMTYPES];
88 static int is_reloc(enum symtype type, const char *sym_name)
90 return sym_regex[type] &&
91 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
94 static void regex_init(int use_real_mode)
101 sym_regex = sym_regex_realmode;
103 sym_regex = sym_regex_kernel;
105 for (i = 0; i < S_NSYMTYPES; i++) {
109 err = regcomp(&sym_regex_c[i], sym_regex[i],
110 REG_EXTENDED|REG_NOSUB);
113 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
119 static void die(char *fmt, ...)
123 vfprintf(stderr, fmt, ap);
128 static const char *sym_type(unsigned type)
130 static const char *type_name[] = {
131 #define SYM_TYPE(X) [X] = #X
132 SYM_TYPE(STT_NOTYPE),
133 SYM_TYPE(STT_OBJECT),
135 SYM_TYPE(STT_SECTION),
137 SYM_TYPE(STT_COMMON),
141 const char *name = "unknown sym type name";
142 if (type < ARRAY_SIZE(type_name)) {
143 name = type_name[type];
148 static const char *sym_bind(unsigned bind)
150 static const char *bind_name[] = {
151 #define SYM_BIND(X) [X] = #X
153 SYM_BIND(STB_GLOBAL),
157 const char *name = "unknown sym bind name";
158 if (bind < ARRAY_SIZE(bind_name)) {
159 name = bind_name[bind];
164 static const char *sym_visibility(unsigned visibility)
166 static const char *visibility_name[] = {
167 #define SYM_VISIBILITY(X) [X] = #X
168 SYM_VISIBILITY(STV_DEFAULT),
169 SYM_VISIBILITY(STV_INTERNAL),
170 SYM_VISIBILITY(STV_HIDDEN),
171 SYM_VISIBILITY(STV_PROTECTED),
172 #undef SYM_VISIBILITY
174 const char *name = "unknown sym visibility name";
175 if (visibility < ARRAY_SIZE(visibility_name)) {
176 name = visibility_name[visibility];
181 static const char *rel_type(unsigned type)
183 static const char *type_name[] = {
184 #define REL_TYPE(X) [X] = #X
185 REL_TYPE(R_386_NONE),
187 REL_TYPE(R_386_PC32),
188 REL_TYPE(R_386_GOT32),
189 REL_TYPE(R_386_PLT32),
190 REL_TYPE(R_386_COPY),
191 REL_TYPE(R_386_GLOB_DAT),
192 REL_TYPE(R_386_JMP_SLOT),
193 REL_TYPE(R_386_RELATIVE),
194 REL_TYPE(R_386_GOTOFF),
195 REL_TYPE(R_386_GOTPC),
199 REL_TYPE(R_386_PC16),
202 const char *name = "unknown type rel type name";
203 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
204 name = type_name[type];
209 static const char *sec_name(unsigned shndx)
211 const char *sec_strtab;
213 sec_strtab = secs[ehdr.e_shstrndx].strtab;
215 if (shndx < ehdr.e_shnum) {
216 name = sec_strtab + secs[shndx].shdr.sh_name;
218 else if (shndx == SHN_ABS) {
221 else if (shndx == SHN_COMMON) {
227 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
232 name = sym_strtab + sym->st_name;
235 name = sec_name(sym->st_shndx);
242 #if BYTE_ORDER == LITTLE_ENDIAN
243 #define le16_to_cpu(val) (val)
244 #define le32_to_cpu(val) (val)
246 #if BYTE_ORDER == BIG_ENDIAN
247 #define le16_to_cpu(val) bswap_16(val)
248 #define le32_to_cpu(val) bswap_32(val)
251 static uint16_t elf16_to_cpu(uint16_t val)
253 return le16_to_cpu(val);
256 static uint32_t elf32_to_cpu(uint32_t val)
258 return le32_to_cpu(val);
261 static void read_ehdr(FILE *fp)
263 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
264 die("Cannot read ELF header: %s\n",
267 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
268 die("No ELF magic\n");
270 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
271 die("Not a 32 bit executable\n");
273 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
274 die("Not a LSB ELF executable\n");
276 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
277 die("Unknown ELF version\n");
279 /* Convert the fields to native endian */
280 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
281 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
282 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
283 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
284 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
285 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
286 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
287 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
288 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
289 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
290 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
291 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
292 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
294 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
295 die("Unsupported ELF header type\n");
297 if (ehdr.e_machine != EM_386) {
298 die("Not for x86\n");
300 if (ehdr.e_version != EV_CURRENT) {
301 die("Unknown ELF version\n");
303 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
304 die("Bad Elf header size\n");
306 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
307 die("Bad program header entry\n");
309 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
310 die("Bad section header entry\n");
312 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
313 die("String table index out of bounds\n");
317 static void read_shdrs(FILE *fp)
322 secs = calloc(ehdr.e_shnum, sizeof(struct section));
324 die("Unable to allocate %d section headers\n",
327 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
328 die("Seek to %d failed: %s\n",
329 ehdr.e_shoff, strerror(errno));
331 for (i = 0; i < ehdr.e_shnum; i++) {
332 struct section *sec = &secs[i];
333 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
334 die("Cannot read ELF section headers %d/%d: %s\n",
335 i, ehdr.e_shnum, strerror(errno));
336 sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name);
337 sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type);
338 sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags);
339 sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr);
340 sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset);
341 sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size);
342 sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link);
343 sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info);
344 sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign);
345 sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize);
346 if (sec->shdr.sh_link < ehdr.e_shnum)
347 sec->link = &secs[sec->shdr.sh_link];
352 static void read_strtabs(FILE *fp)
355 for (i = 0; i < ehdr.e_shnum; i++) {
356 struct section *sec = &secs[i];
357 if (sec->shdr.sh_type != SHT_STRTAB) {
360 sec->strtab = malloc(sec->shdr.sh_size);
362 die("malloc of %d bytes for strtab failed\n",
365 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
366 die("Seek to %d failed: %s\n",
367 sec->shdr.sh_offset, strerror(errno));
369 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
370 != sec->shdr.sh_size) {
371 die("Cannot read symbol table: %s\n",
377 static void read_symtabs(FILE *fp)
380 for (i = 0; i < ehdr.e_shnum; i++) {
381 struct section *sec = &secs[i];
382 if (sec->shdr.sh_type != SHT_SYMTAB) {
385 sec->symtab = malloc(sec->shdr.sh_size);
387 die("malloc of %d bytes for symtab failed\n",
390 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
391 die("Seek to %d failed: %s\n",
392 sec->shdr.sh_offset, strerror(errno));
394 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
395 != sec->shdr.sh_size) {
396 die("Cannot read symbol table: %s\n",
399 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
400 Elf32_Sym *sym = &sec->symtab[j];
401 sym->st_name = elf32_to_cpu(sym->st_name);
402 sym->st_value = elf32_to_cpu(sym->st_value);
403 sym->st_size = elf32_to_cpu(sym->st_size);
404 sym->st_shndx = elf16_to_cpu(sym->st_shndx);
410 static void read_relocs(FILE *fp)
413 for (i = 0; i < ehdr.e_shnum; i++) {
414 struct section *sec = &secs[i];
415 if (sec->shdr.sh_type != SHT_REL) {
418 sec->reltab = malloc(sec->shdr.sh_size);
420 die("malloc of %d bytes for relocs failed\n",
423 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
424 die("Seek to %d failed: %s\n",
425 sec->shdr.sh_offset, strerror(errno));
427 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
428 != sec->shdr.sh_size) {
429 die("Cannot read symbol table: %s\n",
432 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
433 Elf32_Rel *rel = &sec->reltab[j];
434 rel->r_offset = elf32_to_cpu(rel->r_offset);
435 rel->r_info = elf32_to_cpu(rel->r_info);
441 static void print_absolute_symbols(void)
444 printf("Absolute symbols\n");
445 printf(" Num: Value Size Type Bind Visibility Name\n");
446 for (i = 0; i < ehdr.e_shnum; i++) {
447 struct section *sec = &secs[i];
451 if (sec->shdr.sh_type != SHT_SYMTAB) {
454 sym_strtab = sec->link->strtab;
455 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
458 sym = &sec->symtab[j];
459 name = sym_name(sym_strtab, sym);
460 if (sym->st_shndx != SHN_ABS) {
463 printf("%5d %08x %5d %10s %10s %12s %s\n",
464 j, sym->st_value, sym->st_size,
465 sym_type(ELF32_ST_TYPE(sym->st_info)),
466 sym_bind(ELF32_ST_BIND(sym->st_info)),
467 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
474 static void print_absolute_relocs(void)
478 for (i = 0; i < ehdr.e_shnum; i++) {
479 struct section *sec = &secs[i];
480 struct section *sec_applies, *sec_symtab;
482 Elf32_Sym *sh_symtab;
484 if (sec->shdr.sh_type != SHT_REL) {
487 sec_symtab = sec->link;
488 sec_applies = &secs[sec->shdr.sh_info];
489 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
492 sh_symtab = sec_symtab->symtab;
493 sym_strtab = sec_symtab->link->strtab;
494 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
498 rel = &sec->reltab[j];
499 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
500 name = sym_name(sym_strtab, sym);
501 if (sym->st_shndx != SHN_ABS) {
505 /* Absolute symbols are not relocated if bzImage is
506 * loaded at a non-compiled address. Display a warning
507 * to user at compile time about the absolute
508 * relocations present.
510 * User need to audit the code to make sure
511 * some symbols which should have been section
512 * relative have not become absolute because of some
513 * linker optimization or wrong programming usage.
515 * Before warning check if this absolute symbol
516 * relocation is harmless.
518 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
522 printf("WARNING: Absolute relocations"
524 printf("Offset Info Type Sym.Value "
529 printf("%08x %08x %10s %08x %s\n",
532 rel_type(ELF32_R_TYPE(rel->r_info)),
542 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym),
546 /* Walk through the relocations */
547 for (i = 0; i < ehdr.e_shnum; i++) {
549 Elf32_Sym *sh_symtab;
550 struct section *sec_applies, *sec_symtab;
552 struct section *sec = &secs[i];
554 if (sec->shdr.sh_type != SHT_REL) {
557 sec_symtab = sec->link;
558 sec_applies = &secs[sec->shdr.sh_info];
559 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
562 sh_symtab = sec_symtab->symtab;
563 sym_strtab = sec_symtab->link->strtab;
564 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
569 rel = &sec->reltab[j];
570 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
571 r_type = ELF32_R_TYPE(rel->r_info);
579 * NONE can be ignored and and PC relative
580 * relocations don't need to be adjusted.
585 symname = sym_name(sym_strtab, sym);
588 if (sym->st_shndx == SHN_ABS) {
589 if (is_reloc(S_ABS, symname))
591 else if (!is_reloc(S_SEG, symname))
594 if (is_reloc(S_LIN, symname))
603 symname = sym_name(sym_strtab, sym);
604 if (sym->st_shndx == SHN_ABS) {
605 if (is_reloc(S_ABS, symname))
607 else if (!is_reloc(S_REL, symname))
611 !is_reloc(S_LIN, symname))
617 die("Unsupported relocation type: %s (%d)\n",
618 rel_type(r_type), r_type);
621 symname = sym_name(sym_strtab, sym);
622 die("Invalid %s relocation: %s\n",
623 rel_type(r_type), symname);
629 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
631 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
637 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
639 /* Remember the address that needs to be adjusted. */
640 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
641 relocs16[reloc16_idx++] = rel->r_offset;
643 relocs[reloc_idx++] = rel->r_offset;
646 static int cmp_relocs(const void *va, const void *vb)
648 const unsigned long *a, *b;
650 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
653 static int write32(unsigned int v, FILE *f)
655 unsigned char buf[4];
657 put_unaligned_le32(v, buf);
658 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
661 static void emit_relocs(int as_text, int use_real_mode)
664 /* Count how many relocations I have and allocate space for them. */
666 walk_relocs(count_reloc, use_real_mode);
667 relocs = malloc(reloc_count * sizeof(relocs[0]));
669 die("malloc of %d entries for relocs failed\n",
673 relocs16 = malloc(reloc16_count * sizeof(relocs[0]));
675 die("malloc of %d entries for relocs16 failed\n",
678 /* Collect up the relocations */
680 walk_relocs(collect_reloc, use_real_mode);
682 if (reloc16_count && !use_real_mode)
683 die("Segment relocations found but --realmode not specified\n");
685 /* Order the relocations for more efficient processing */
686 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
687 qsort(relocs16, reloc16_count, sizeof(relocs16[0]), cmp_relocs);
689 /* Print the relocations */
691 /* Print the relocations in a form suitable that
694 printf(".section \".data.reloc\",\"a\"\n");
695 printf(".balign 4\n");
697 printf("\t.long %lu\n", reloc16_count);
698 for (i = 0; i < reloc16_count; i++)
699 printf("\t.long 0x%08lx\n", relocs16[i]);
700 printf("\t.long %lu\n", reloc_count);
701 for (i = 0; i < reloc_count; i++) {
702 printf("\t.long 0x%08lx\n", relocs[i]);
706 printf("\t.long 0x%08lx\n", (unsigned long)0);
707 for (i = 0; i < reloc_count; i++) {
708 printf("\t.long 0x%08lx\n", relocs[i]);
716 write32(reloc16_count, stdout);
717 for (i = 0; i < reloc16_count; i++)
718 write32(relocs16[i], stdout);
719 write32(reloc_count, stdout);
721 /* Now print each relocation */
722 for (i = 0; i < reloc_count; i++)
723 write32(relocs[i], stdout);
728 /* Now print each relocation */
729 for (i = 0; i < reloc_count; i++) {
730 write32(relocs[i], stdout);
736 static void usage(void)
738 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
741 int main(int argc, char **argv)
743 int show_absolute_syms, show_absolute_relocs;
744 int as_text, use_real_mode;
749 show_absolute_syms = 0;
750 show_absolute_relocs = 0;
754 for (i = 1; i < argc; i++) {
757 if (strcmp(arg, "--abs-syms") == 0) {
758 show_absolute_syms = 1;
761 if (strcmp(arg, "--abs-relocs") == 0) {
762 show_absolute_relocs = 1;
765 if (strcmp(arg, "--text") == 0) {
769 if (strcmp(arg, "--realmode") == 0) {
783 regex_init(use_real_mode);
784 fp = fopen(fname, "r");
786 die("Cannot open %s: %s\n",
787 fname, strerror(errno));
794 if (show_absolute_syms) {
795 print_absolute_symbols();
798 if (show_absolute_relocs) {
799 print_absolute_relocs();
802 emit_relocs(as_text, use_real_mode);