14 static void die(char *fmt, ...);
16 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
17 static Elf32_Ehdr ehdr;
18 static unsigned long reloc_count, reloc_idx;
19 static unsigned long *relocs;
20 static unsigned long reloc16_count, reloc16_idx;
21 static unsigned long *relocs16;
30 static struct section *secs;
40 static const char * const sym_regex_kernel[S_NSYMTYPES] = {
42 * Following symbols have been audited. There values are constant and do
43 * not change if bzImage is loaded at a different physical address than
44 * the address for which it has been compiled. Don't warn user about
45 * absolute relocations present w.r.t these symbols.
48 "^(xen_irq_disable_direct_reloc$|"
49 "xen_save_fl_direct_reloc$|"
54 * These symbols are known to be relative, even if the linker marks them
55 * as absolute (typically defined outside any section in the linker script.)
58 "^(__init_(begin|end)|"
59 "__x86_cpu_dev_(start|end)|"
60 "(__parainstructions|__alt_instructions)(|_end)|"
61 "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
62 "__(start|end)_pci_.*|"
63 "__(start|end)_builtin_fw|"
64 "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
65 "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
66 "__(start|stop)___param|"
67 "__(start|stop)___modver|"
68 "__(start|stop)___bug_table|"
69 "__tracedata_(start|end)|"
70 "__(start|stop)_notes|"
73 "(jiffies|jiffies_64)|"
78 static const char * const sym_regex_realmode[S_NSYMTYPES] = {
80 * These are 16-bit segment symbols when compiling 16-bit code.
86 * These are offsets belonging to segments, as opposed to linear addresses,
87 * when compiling 16-bit code.
93 static const char * const *sym_regex;
95 static regex_t sym_regex_c[S_NSYMTYPES];
96 static int is_reloc(enum symtype type, const char *sym_name)
98 return sym_regex[type] &&
99 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
102 static void regex_init(int use_real_mode)
109 sym_regex = sym_regex_realmode;
111 sym_regex = sym_regex_kernel;
113 for (i = 0; i < S_NSYMTYPES; i++) {
117 err = regcomp(&sym_regex_c[i], sym_regex[i],
118 REG_EXTENDED|REG_NOSUB);
121 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
127 static void die(char *fmt, ...)
131 vfprintf(stderr, fmt, ap);
136 static const char *sym_type(unsigned type)
138 static const char *type_name[] = {
139 #define SYM_TYPE(X) [X] = #X
140 SYM_TYPE(STT_NOTYPE),
141 SYM_TYPE(STT_OBJECT),
143 SYM_TYPE(STT_SECTION),
145 SYM_TYPE(STT_COMMON),
149 const char *name = "unknown sym type name";
150 if (type < ARRAY_SIZE(type_name)) {
151 name = type_name[type];
156 static const char *sym_bind(unsigned bind)
158 static const char *bind_name[] = {
159 #define SYM_BIND(X) [X] = #X
161 SYM_BIND(STB_GLOBAL),
165 const char *name = "unknown sym bind name";
166 if (bind < ARRAY_SIZE(bind_name)) {
167 name = bind_name[bind];
172 static const char *sym_visibility(unsigned visibility)
174 static const char *visibility_name[] = {
175 #define SYM_VISIBILITY(X) [X] = #X
176 SYM_VISIBILITY(STV_DEFAULT),
177 SYM_VISIBILITY(STV_INTERNAL),
178 SYM_VISIBILITY(STV_HIDDEN),
179 SYM_VISIBILITY(STV_PROTECTED),
180 #undef SYM_VISIBILITY
182 const char *name = "unknown sym visibility name";
183 if (visibility < ARRAY_SIZE(visibility_name)) {
184 name = visibility_name[visibility];
189 static const char *rel_type(unsigned type)
191 static const char *type_name[] = {
192 #define REL_TYPE(X) [X] = #X
193 REL_TYPE(R_386_NONE),
195 REL_TYPE(R_386_PC32),
196 REL_TYPE(R_386_GOT32),
197 REL_TYPE(R_386_PLT32),
198 REL_TYPE(R_386_COPY),
199 REL_TYPE(R_386_GLOB_DAT),
200 REL_TYPE(R_386_JMP_SLOT),
201 REL_TYPE(R_386_RELATIVE),
202 REL_TYPE(R_386_GOTOFF),
203 REL_TYPE(R_386_GOTPC),
207 REL_TYPE(R_386_PC16),
210 const char *name = "unknown type rel type name";
211 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
212 name = type_name[type];
217 static const char *sec_name(unsigned shndx)
219 const char *sec_strtab;
221 sec_strtab = secs[ehdr.e_shstrndx].strtab;
223 if (shndx < ehdr.e_shnum) {
224 name = sec_strtab + secs[shndx].shdr.sh_name;
226 else if (shndx == SHN_ABS) {
229 else if (shndx == SHN_COMMON) {
235 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
240 name = sym_strtab + sym->st_name;
243 name = sec_name(sym->st_shndx);
250 #if BYTE_ORDER == LITTLE_ENDIAN
251 #define le16_to_cpu(val) (val)
252 #define le32_to_cpu(val) (val)
254 #if BYTE_ORDER == BIG_ENDIAN
255 #define le16_to_cpu(val) bswap_16(val)
256 #define le32_to_cpu(val) bswap_32(val)
259 static uint16_t elf16_to_cpu(uint16_t val)
261 return le16_to_cpu(val);
264 static uint32_t elf32_to_cpu(uint32_t val)
266 return le32_to_cpu(val);
269 static void read_ehdr(FILE *fp)
271 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
272 die("Cannot read ELF header: %s\n",
275 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
276 die("No ELF magic\n");
278 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
279 die("Not a 32 bit executable\n");
281 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
282 die("Not a LSB ELF executable\n");
284 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
285 die("Unknown ELF version\n");
287 /* Convert the fields to native endian */
288 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
289 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
290 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
291 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
292 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
293 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
294 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
295 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
296 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
297 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
298 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
299 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
300 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
302 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
303 die("Unsupported ELF header type\n");
305 if (ehdr.e_machine != EM_386) {
306 die("Not for x86\n");
308 if (ehdr.e_version != EV_CURRENT) {
309 die("Unknown ELF version\n");
311 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
312 die("Bad Elf header size\n");
314 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
315 die("Bad program header entry\n");
317 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
318 die("Bad section header entry\n");
320 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
321 die("String table index out of bounds\n");
325 static void read_shdrs(FILE *fp)
330 secs = calloc(ehdr.e_shnum, sizeof(struct section));
332 die("Unable to allocate %d section headers\n",
335 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
336 die("Seek to %d failed: %s\n",
337 ehdr.e_shoff, strerror(errno));
339 for (i = 0; i < ehdr.e_shnum; i++) {
340 struct section *sec = &secs[i];
341 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
342 die("Cannot read ELF section headers %d/%d: %s\n",
343 i, ehdr.e_shnum, strerror(errno));
344 sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name);
345 sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type);
346 sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags);
347 sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr);
348 sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset);
349 sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size);
350 sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link);
351 sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info);
352 sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign);
353 sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize);
354 if (sec->shdr.sh_link < ehdr.e_shnum)
355 sec->link = &secs[sec->shdr.sh_link];
360 static void read_strtabs(FILE *fp)
363 for (i = 0; i < ehdr.e_shnum; i++) {
364 struct section *sec = &secs[i];
365 if (sec->shdr.sh_type != SHT_STRTAB) {
368 sec->strtab = malloc(sec->shdr.sh_size);
370 die("malloc of %d bytes for strtab failed\n",
373 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
374 die("Seek to %d failed: %s\n",
375 sec->shdr.sh_offset, strerror(errno));
377 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
378 != sec->shdr.sh_size) {
379 die("Cannot read symbol table: %s\n",
385 static void read_symtabs(FILE *fp)
388 for (i = 0; i < ehdr.e_shnum; i++) {
389 struct section *sec = &secs[i];
390 if (sec->shdr.sh_type != SHT_SYMTAB) {
393 sec->symtab = malloc(sec->shdr.sh_size);
395 die("malloc of %d bytes for symtab failed\n",
398 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
399 die("Seek to %d failed: %s\n",
400 sec->shdr.sh_offset, strerror(errno));
402 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
403 != sec->shdr.sh_size) {
404 die("Cannot read symbol table: %s\n",
407 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
408 Elf32_Sym *sym = &sec->symtab[j];
409 sym->st_name = elf32_to_cpu(sym->st_name);
410 sym->st_value = elf32_to_cpu(sym->st_value);
411 sym->st_size = elf32_to_cpu(sym->st_size);
412 sym->st_shndx = elf16_to_cpu(sym->st_shndx);
418 static void read_relocs(FILE *fp)
421 for (i = 0; i < ehdr.e_shnum; i++) {
422 struct section *sec = &secs[i];
423 if (sec->shdr.sh_type != SHT_REL) {
426 sec->reltab = malloc(sec->shdr.sh_size);
428 die("malloc of %d bytes for relocs failed\n",
431 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
432 die("Seek to %d failed: %s\n",
433 sec->shdr.sh_offset, strerror(errno));
435 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
436 != sec->shdr.sh_size) {
437 die("Cannot read symbol table: %s\n",
440 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
441 Elf32_Rel *rel = &sec->reltab[j];
442 rel->r_offset = elf32_to_cpu(rel->r_offset);
443 rel->r_info = elf32_to_cpu(rel->r_info);
449 static void print_absolute_symbols(void)
452 printf("Absolute symbols\n");
453 printf(" Num: Value Size Type Bind Visibility Name\n");
454 for (i = 0; i < ehdr.e_shnum; i++) {
455 struct section *sec = &secs[i];
459 if (sec->shdr.sh_type != SHT_SYMTAB) {
462 sym_strtab = sec->link->strtab;
463 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
466 sym = &sec->symtab[j];
467 name = sym_name(sym_strtab, sym);
468 if (sym->st_shndx != SHN_ABS) {
471 printf("%5d %08x %5d %10s %10s %12s %s\n",
472 j, sym->st_value, sym->st_size,
473 sym_type(ELF32_ST_TYPE(sym->st_info)),
474 sym_bind(ELF32_ST_BIND(sym->st_info)),
475 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
482 static void print_absolute_relocs(void)
486 for (i = 0; i < ehdr.e_shnum; i++) {
487 struct section *sec = &secs[i];
488 struct section *sec_applies, *sec_symtab;
490 Elf32_Sym *sh_symtab;
492 if (sec->shdr.sh_type != SHT_REL) {
495 sec_symtab = sec->link;
496 sec_applies = &secs[sec->shdr.sh_info];
497 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
500 sh_symtab = sec_symtab->symtab;
501 sym_strtab = sec_symtab->link->strtab;
502 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
506 rel = &sec->reltab[j];
507 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
508 name = sym_name(sym_strtab, sym);
509 if (sym->st_shndx != SHN_ABS) {
513 /* Absolute symbols are not relocated if bzImage is
514 * loaded at a non-compiled address. Display a warning
515 * to user at compile time about the absolute
516 * relocations present.
518 * User need to audit the code to make sure
519 * some symbols which should have been section
520 * relative have not become absolute because of some
521 * linker optimization or wrong programming usage.
523 * Before warning check if this absolute symbol
524 * relocation is harmless.
526 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
530 printf("WARNING: Absolute relocations"
532 printf("Offset Info Type Sym.Value "
537 printf("%08x %08x %10s %08x %s\n",
540 rel_type(ELF32_R_TYPE(rel->r_info)),
550 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym),
554 /* Walk through the relocations */
555 for (i = 0; i < ehdr.e_shnum; i++) {
557 Elf32_Sym *sh_symtab;
558 struct section *sec_applies, *sec_symtab;
560 struct section *sec = &secs[i];
562 if (sec->shdr.sh_type != SHT_REL) {
565 sec_symtab = sec->link;
566 sec_applies = &secs[sec->shdr.sh_info];
567 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
570 sh_symtab = sec_symtab->symtab;
571 sym_strtab = sec_symtab->link->strtab;
572 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
579 rel = &sec->reltab[j];
580 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
581 r_type = ELF32_R_TYPE(rel->r_info);
583 shn_abs = sym->st_shndx == SHN_ABS;
591 * NONE can be ignored and and PC relative
592 * relocations don't need to be adjusted.
597 symname = sym_name(sym_strtab, sym);
601 if (is_reloc(S_ABS, symname))
603 else if (!is_reloc(S_SEG, symname))
606 if (is_reloc(S_LIN, symname))
615 symname = sym_name(sym_strtab, sym);
617 if (is_reloc(S_ABS, symname))
619 else if (!is_reloc(S_REL, symname))
623 !is_reloc(S_LIN, symname))
629 die("Unsupported relocation type: %s (%d)\n",
630 rel_type(r_type), r_type);
633 symname = sym_name(sym_strtab, sym);
634 die("Invalid %s %s relocation: %s\n",
635 shn_abs ? "absolute" : "relative",
636 rel_type(r_type), symname);
642 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
644 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
650 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
652 /* Remember the address that needs to be adjusted. */
653 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
654 relocs16[reloc16_idx++] = rel->r_offset;
656 relocs[reloc_idx++] = rel->r_offset;
659 static int cmp_relocs(const void *va, const void *vb)
661 const unsigned long *a, *b;
663 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
666 static int write32(unsigned int v, FILE *f)
668 unsigned char buf[4];
670 buf[0] = (v >> 0) & 0xff;
671 buf[1] = (v >> 8) & 0xff;
672 buf[2] = (v >> 16) & 0xff;
673 buf[3] = (v >> 24) & 0xff;
674 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
677 static void emit_relocs(int as_text, int use_real_mode)
680 /* Count how many relocations I have and allocate space for them. */
682 walk_relocs(count_reloc, use_real_mode);
683 relocs = malloc(reloc_count * sizeof(relocs[0]));
685 die("malloc of %d entries for relocs failed\n",
689 relocs16 = malloc(reloc16_count * sizeof(relocs[0]));
691 die("malloc of %d entries for relocs16 failed\n",
694 /* Collect up the relocations */
696 walk_relocs(collect_reloc, use_real_mode);
698 if (reloc16_count && !use_real_mode)
699 die("Segment relocations found but --realmode not specified\n");
701 /* Order the relocations for more efficient processing */
702 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
703 qsort(relocs16, reloc16_count, sizeof(relocs16[0]), cmp_relocs);
705 /* Print the relocations */
707 /* Print the relocations in a form suitable that
710 printf(".section \".data.reloc\",\"a\"\n");
711 printf(".balign 4\n");
713 printf("\t.long %lu\n", reloc16_count);
714 for (i = 0; i < reloc16_count; i++)
715 printf("\t.long 0x%08lx\n", relocs16[i]);
716 printf("\t.long %lu\n", reloc_count);
717 for (i = 0; i < reloc_count; i++) {
718 printf("\t.long 0x%08lx\n", relocs[i]);
722 printf("\t.long 0x%08lx\n", (unsigned long)0);
723 for (i = 0; i < reloc_count; i++) {
724 printf("\t.long 0x%08lx\n", relocs[i]);
732 write32(reloc16_count, stdout);
733 for (i = 0; i < reloc16_count; i++)
734 write32(relocs16[i], stdout);
735 write32(reloc_count, stdout);
737 /* Now print each relocation */
738 for (i = 0; i < reloc_count; i++)
739 write32(relocs[i], stdout);
744 /* Now print each relocation */
745 for (i = 0; i < reloc_count; i++) {
746 write32(relocs[i], stdout);
752 static void usage(void)
754 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
757 int main(int argc, char **argv)
759 int show_absolute_syms, show_absolute_relocs;
760 int as_text, use_real_mode;
765 show_absolute_syms = 0;
766 show_absolute_relocs = 0;
770 for (i = 1; i < argc; i++) {
773 if (strcmp(arg, "--abs-syms") == 0) {
774 show_absolute_syms = 1;
777 if (strcmp(arg, "--abs-relocs") == 0) {
778 show_absolute_relocs = 1;
781 if (strcmp(arg, "--text") == 0) {
785 if (strcmp(arg, "--realmode") == 0) {
799 regex_init(use_real_mode);
800 fp = fopen(fname, "r");
802 die("Cannot open %s: %s\n",
803 fname, strerror(errno));
810 if (show_absolute_syms) {
811 print_absolute_symbols();
814 if (show_absolute_relocs) {
815 print_absolute_relocs();
818 emit_relocs(as_text, use_real_mode);