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 are 16-bit segment symbols when compiling 16-bit code.
71 * These are offsets belonging to segments, as opposed to linear addresses,
72 * when compiling 16-bit code.
78 static const char * const *sym_regex;
80 static regex_t sym_regex_c[S_NSYMTYPES];
81 static int is_reloc(enum symtype type, const char *sym_name)
83 return sym_regex[type] &&
84 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
87 static void regex_init(int use_real_mode)
94 sym_regex = sym_regex_realmode;
96 sym_regex = sym_regex_kernel;
98 for (i = 0; i < S_NSYMTYPES; i++) {
102 err = regcomp(&sym_regex_c[i], sym_regex[i],
103 REG_EXTENDED|REG_NOSUB);
106 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
112 static void die(char *fmt, ...)
116 vfprintf(stderr, fmt, ap);
121 static const char *sym_type(unsigned type)
123 static const char *type_name[] = {
124 #define SYM_TYPE(X) [X] = #X
125 SYM_TYPE(STT_NOTYPE),
126 SYM_TYPE(STT_OBJECT),
128 SYM_TYPE(STT_SECTION),
130 SYM_TYPE(STT_COMMON),
134 const char *name = "unknown sym type name";
135 if (type < ARRAY_SIZE(type_name)) {
136 name = type_name[type];
141 static const char *sym_bind(unsigned bind)
143 static const char *bind_name[] = {
144 #define SYM_BIND(X) [X] = #X
146 SYM_BIND(STB_GLOBAL),
150 const char *name = "unknown sym bind name";
151 if (bind < ARRAY_SIZE(bind_name)) {
152 name = bind_name[bind];
157 static const char *sym_visibility(unsigned visibility)
159 static const char *visibility_name[] = {
160 #define SYM_VISIBILITY(X) [X] = #X
161 SYM_VISIBILITY(STV_DEFAULT),
162 SYM_VISIBILITY(STV_INTERNAL),
163 SYM_VISIBILITY(STV_HIDDEN),
164 SYM_VISIBILITY(STV_PROTECTED),
165 #undef SYM_VISIBILITY
167 const char *name = "unknown sym visibility name";
168 if (visibility < ARRAY_SIZE(visibility_name)) {
169 name = visibility_name[visibility];
174 static const char *rel_type(unsigned type)
176 static const char *type_name[] = {
177 #define REL_TYPE(X) [X] = #X
178 REL_TYPE(R_386_NONE),
180 REL_TYPE(R_386_PC32),
181 REL_TYPE(R_386_GOT32),
182 REL_TYPE(R_386_PLT32),
183 REL_TYPE(R_386_COPY),
184 REL_TYPE(R_386_GLOB_DAT),
185 REL_TYPE(R_386_JMP_SLOT),
186 REL_TYPE(R_386_RELATIVE),
187 REL_TYPE(R_386_GOTOFF),
188 REL_TYPE(R_386_GOTPC),
192 REL_TYPE(R_386_PC16),
195 const char *name = "unknown type rel type name";
196 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
197 name = type_name[type];
202 static const char *sec_name(unsigned shndx)
204 const char *sec_strtab;
206 sec_strtab = secs[ehdr.e_shstrndx].strtab;
208 if (shndx < ehdr.e_shnum) {
209 name = sec_strtab + secs[shndx].shdr.sh_name;
211 else if (shndx == SHN_ABS) {
214 else if (shndx == SHN_COMMON) {
220 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
225 name = sym_strtab + sym->st_name;
228 name = sec_name(sym->st_shndx);
235 #if BYTE_ORDER == LITTLE_ENDIAN
236 #define le16_to_cpu(val) (val)
237 #define le32_to_cpu(val) (val)
239 #if BYTE_ORDER == BIG_ENDIAN
240 #define le16_to_cpu(val) bswap_16(val)
241 #define le32_to_cpu(val) bswap_32(val)
244 static uint16_t elf16_to_cpu(uint16_t val)
246 return le16_to_cpu(val);
249 static uint32_t elf32_to_cpu(uint32_t val)
251 return le32_to_cpu(val);
254 static void read_ehdr(FILE *fp)
256 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
257 die("Cannot read ELF header: %s\n",
260 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
261 die("No ELF magic\n");
263 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
264 die("Not a 32 bit executable\n");
266 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
267 die("Not a LSB ELF executable\n");
269 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
270 die("Unknown ELF version\n");
272 /* Convert the fields to native endian */
273 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
274 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
275 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
276 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
277 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
278 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
279 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
280 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
281 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
282 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
283 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
284 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
285 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
287 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
288 die("Unsupported ELF header type\n");
290 if (ehdr.e_machine != EM_386) {
291 die("Not for x86\n");
293 if (ehdr.e_version != EV_CURRENT) {
294 die("Unknown ELF version\n");
296 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
297 die("Bad Elf header size\n");
299 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
300 die("Bad program header entry\n");
302 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
303 die("Bad section header entry\n");
305 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
306 die("String table index out of bounds\n");
310 static void read_shdrs(FILE *fp)
315 secs = calloc(ehdr.e_shnum, sizeof(struct section));
317 die("Unable to allocate %d section headers\n",
320 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
321 die("Seek to %d failed: %s\n",
322 ehdr.e_shoff, strerror(errno));
324 for (i = 0; i < ehdr.e_shnum; i++) {
325 struct section *sec = &secs[i];
326 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
327 die("Cannot read ELF section headers %d/%d: %s\n",
328 i, ehdr.e_shnum, strerror(errno));
329 sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name);
330 sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type);
331 sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags);
332 sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr);
333 sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset);
334 sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size);
335 sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link);
336 sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info);
337 sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign);
338 sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize);
339 if (sec->shdr.sh_link < ehdr.e_shnum)
340 sec->link = &secs[sec->shdr.sh_link];
345 static void read_strtabs(FILE *fp)
348 for (i = 0; i < ehdr.e_shnum; i++) {
349 struct section *sec = &secs[i];
350 if (sec->shdr.sh_type != SHT_STRTAB) {
353 sec->strtab = malloc(sec->shdr.sh_size);
355 die("malloc of %d bytes for strtab failed\n",
358 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
359 die("Seek to %d failed: %s\n",
360 sec->shdr.sh_offset, strerror(errno));
362 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
363 != sec->shdr.sh_size) {
364 die("Cannot read symbol table: %s\n",
370 static void read_symtabs(FILE *fp)
373 for (i = 0; i < ehdr.e_shnum; i++) {
374 struct section *sec = &secs[i];
375 if (sec->shdr.sh_type != SHT_SYMTAB) {
378 sec->symtab = malloc(sec->shdr.sh_size);
380 die("malloc of %d bytes for symtab failed\n",
383 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
384 die("Seek to %d failed: %s\n",
385 sec->shdr.sh_offset, strerror(errno));
387 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
388 != sec->shdr.sh_size) {
389 die("Cannot read symbol table: %s\n",
392 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
393 Elf32_Sym *sym = &sec->symtab[j];
394 sym->st_name = elf32_to_cpu(sym->st_name);
395 sym->st_value = elf32_to_cpu(sym->st_value);
396 sym->st_size = elf32_to_cpu(sym->st_size);
397 sym->st_shndx = elf16_to_cpu(sym->st_shndx);
403 static void read_relocs(FILE *fp)
406 for (i = 0; i < ehdr.e_shnum; i++) {
407 struct section *sec = &secs[i];
408 if (sec->shdr.sh_type != SHT_REL) {
411 sec->reltab = malloc(sec->shdr.sh_size);
413 die("malloc of %d bytes for relocs failed\n",
416 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
417 die("Seek to %d failed: %s\n",
418 sec->shdr.sh_offset, strerror(errno));
420 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
421 != sec->shdr.sh_size) {
422 die("Cannot read symbol table: %s\n",
425 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
426 Elf32_Rel *rel = &sec->reltab[j];
427 rel->r_offset = elf32_to_cpu(rel->r_offset);
428 rel->r_info = elf32_to_cpu(rel->r_info);
434 static void print_absolute_symbols(void)
437 printf("Absolute symbols\n");
438 printf(" Num: Value Size Type Bind Visibility Name\n");
439 for (i = 0; i < ehdr.e_shnum; i++) {
440 struct section *sec = &secs[i];
444 if (sec->shdr.sh_type != SHT_SYMTAB) {
447 sym_strtab = sec->link->strtab;
448 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
451 sym = &sec->symtab[j];
452 name = sym_name(sym_strtab, sym);
453 if (sym->st_shndx != SHN_ABS) {
456 printf("%5d %08x %5d %10s %10s %12s %s\n",
457 j, sym->st_value, sym->st_size,
458 sym_type(ELF32_ST_TYPE(sym->st_info)),
459 sym_bind(ELF32_ST_BIND(sym->st_info)),
460 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
467 static void print_absolute_relocs(void)
471 for (i = 0; i < ehdr.e_shnum; i++) {
472 struct section *sec = &secs[i];
473 struct section *sec_applies, *sec_symtab;
475 Elf32_Sym *sh_symtab;
477 if (sec->shdr.sh_type != SHT_REL) {
480 sec_symtab = sec->link;
481 sec_applies = &secs[sec->shdr.sh_info];
482 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
485 sh_symtab = sec_symtab->symtab;
486 sym_strtab = sec_symtab->link->strtab;
487 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
491 rel = &sec->reltab[j];
492 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
493 name = sym_name(sym_strtab, sym);
494 if (sym->st_shndx != SHN_ABS) {
498 /* Absolute symbols are not relocated if bzImage is
499 * loaded at a non-compiled address. Display a warning
500 * to user at compile time about the absolute
501 * relocations present.
503 * User need to audit the code to make sure
504 * some symbols which should have been section
505 * relative have not become absolute because of some
506 * linker optimization or wrong programming usage.
508 * Before warning check if this absolute symbol
509 * relocation is harmless.
511 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
515 printf("WARNING: Absolute relocations"
517 printf("Offset Info Type Sym.Value "
522 printf("%08x %08x %10s %08x %s\n",
525 rel_type(ELF32_R_TYPE(rel->r_info)),
535 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym),
539 /* Walk through the relocations */
540 for (i = 0; i < ehdr.e_shnum; i++) {
542 Elf32_Sym *sh_symtab;
543 struct section *sec_applies, *sec_symtab;
545 struct section *sec = &secs[i];
547 if (sec->shdr.sh_type != SHT_REL) {
550 sec_symtab = sec->link;
551 sec_applies = &secs[sec->shdr.sh_info];
552 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
555 sh_symtab = sec_symtab->symtab;
556 sym_strtab = sec_symtab->link->strtab;
557 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
562 rel = &sec->reltab[j];
563 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
564 r_type = ELF32_R_TYPE(rel->r_info);
572 * NONE can be ignored and and PC relative
573 * relocations don't need to be adjusted.
578 symname = sym_name(sym_strtab, sym);
581 if (sym->st_shndx == SHN_ABS) {
582 if (is_reloc(S_ABS, symname))
584 else if (!is_reloc(S_SEG, symname))
587 if (is_reloc(S_LIN, symname))
596 symname = sym_name(sym_strtab, sym);
597 if (sym->st_shndx == SHN_ABS) {
598 if (is_reloc(S_ABS, symname))
600 else if (!is_reloc(S_REL, symname))
604 !is_reloc(S_LIN, symname))
610 die("Unsupported relocation type: %s (%d)\n",
611 rel_type(r_type), r_type);
614 symname = sym_name(sym_strtab, sym);
615 die("Invalid %s relocation: %s\n",
616 rel_type(r_type), symname);
622 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
624 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
630 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
632 /* Remember the address that needs to be adjusted. */
633 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
634 relocs16[reloc16_idx++] = rel->r_offset;
636 relocs[reloc_idx++] = rel->r_offset;
639 static int cmp_relocs(const void *va, const void *vb)
641 const unsigned long *a, *b;
643 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
646 static int write32(unsigned int v, FILE *f)
648 unsigned char buf[4];
650 put_unaligned_le32(v, buf);
651 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
654 static void emit_relocs(int as_text, int use_real_mode)
657 /* Count how many relocations I have and allocate space for them. */
659 walk_relocs(count_reloc, use_real_mode);
660 relocs = malloc(reloc_count * sizeof(relocs[0]));
662 die("malloc of %d entries for relocs failed\n",
666 relocs16 = malloc(reloc16_count * sizeof(relocs[0]));
668 die("malloc of %d entries for relocs16 failed\n",
671 /* Collect up the relocations */
673 walk_relocs(collect_reloc, use_real_mode);
675 if (reloc16_count && !use_real_mode)
676 die("Segment relocations found but --realmode not specified\n");
678 /* Order the relocations for more efficient processing */
679 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
680 qsort(relocs16, reloc16_count, sizeof(relocs16[0]), cmp_relocs);
682 /* Print the relocations */
684 /* Print the relocations in a form suitable that
687 printf(".section \".data.reloc\",\"a\"\n");
688 printf(".balign 4\n");
690 printf("\t.long %lu\n", reloc16_count);
691 for (i = 0; i < reloc16_count; i++)
692 printf("\t.long 0x%08lx\n", relocs16[i]);
693 printf("\t.long %lu\n", reloc_count);
694 for (i = 0; i < reloc_count; i++) {
695 printf("\t.long 0x%08lx\n", relocs[i]);
699 printf("\t.long 0x%08lx\n", (unsigned long)0);
700 for (i = 0; i < reloc_count; i++) {
701 printf("\t.long 0x%08lx\n", relocs[i]);
709 write32(reloc16_count, stdout);
710 for (i = 0; i < reloc16_count; i++)
711 write32(relocs16[i], stdout);
712 write32(reloc_count, stdout);
714 /* Now print each relocation */
715 for (i = 0; i < reloc_count; i++)
716 write32(relocs[i], stdout);
721 /* Now print each relocation */
722 for (i = 0; i < reloc_count; i++) {
723 write32(relocs[i], stdout);
729 static void usage(void)
731 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
734 int main(int argc, char **argv)
736 int show_absolute_syms, show_absolute_relocs;
737 int as_text, use_real_mode;
742 show_absolute_syms = 0;
743 show_absolute_relocs = 0;
747 for (i = 1; i < argc; i++) {
750 if (strcmp(arg, "--abs-syms") == 0) {
751 show_absolute_syms = 1;
754 if (strcmp(arg, "--abs-relocs") == 0) {
755 show_absolute_relocs = 1;
758 if (strcmp(arg, "--text") == 0) {
762 if (strcmp(arg, "--realmode") == 0) {
776 regex_init(use_real_mode);
777 fp = fopen(fname, "r");
779 die("Cannot open %s: %s\n",
780 fname, strerror(errno));
787 if (show_absolute_syms) {
788 print_absolute_symbols();
791 if (show_absolute_relocs) {
792 print_absolute_relocs();
795 emit_relocs(as_text, use_real_mode);