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)|"
66 static const char * const sym_regex_realmode[S_NSYMTYPES] = {
68 * These are 16-bit segment symbols when compiling 16-bit code.
74 * These are offsets belonging to segments, as opposed to linear addresses,
75 * when compiling 16-bit code.
81 static const char * const *sym_regex;
83 static regex_t sym_regex_c[S_NSYMTYPES];
84 static int is_reloc(enum symtype type, const char *sym_name)
86 return sym_regex[type] &&
87 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
90 static void regex_init(int use_real_mode)
97 sym_regex = sym_regex_realmode;
99 sym_regex = sym_regex_kernel;
101 for (i = 0; i < S_NSYMTYPES; i++) {
105 err = regcomp(&sym_regex_c[i], sym_regex[i],
106 REG_EXTENDED|REG_NOSUB);
109 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
115 static void die(char *fmt, ...)
119 vfprintf(stderr, fmt, ap);
124 static const char *sym_type(unsigned type)
126 static const char *type_name[] = {
127 #define SYM_TYPE(X) [X] = #X
128 SYM_TYPE(STT_NOTYPE),
129 SYM_TYPE(STT_OBJECT),
131 SYM_TYPE(STT_SECTION),
133 SYM_TYPE(STT_COMMON),
137 const char *name = "unknown sym type name";
138 if (type < ARRAY_SIZE(type_name)) {
139 name = type_name[type];
144 static const char *sym_bind(unsigned bind)
146 static const char *bind_name[] = {
147 #define SYM_BIND(X) [X] = #X
149 SYM_BIND(STB_GLOBAL),
153 const char *name = "unknown sym bind name";
154 if (bind < ARRAY_SIZE(bind_name)) {
155 name = bind_name[bind];
160 static const char *sym_visibility(unsigned visibility)
162 static const char *visibility_name[] = {
163 #define SYM_VISIBILITY(X) [X] = #X
164 SYM_VISIBILITY(STV_DEFAULT),
165 SYM_VISIBILITY(STV_INTERNAL),
166 SYM_VISIBILITY(STV_HIDDEN),
167 SYM_VISIBILITY(STV_PROTECTED),
168 #undef SYM_VISIBILITY
170 const char *name = "unknown sym visibility name";
171 if (visibility < ARRAY_SIZE(visibility_name)) {
172 name = visibility_name[visibility];
177 static const char *rel_type(unsigned type)
179 static const char *type_name[] = {
180 #define REL_TYPE(X) [X] = #X
181 REL_TYPE(R_386_NONE),
183 REL_TYPE(R_386_PC32),
184 REL_TYPE(R_386_GOT32),
185 REL_TYPE(R_386_PLT32),
186 REL_TYPE(R_386_COPY),
187 REL_TYPE(R_386_GLOB_DAT),
188 REL_TYPE(R_386_JMP_SLOT),
189 REL_TYPE(R_386_RELATIVE),
190 REL_TYPE(R_386_GOTOFF),
191 REL_TYPE(R_386_GOTPC),
195 REL_TYPE(R_386_PC16),
198 const char *name = "unknown type rel type name";
199 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
200 name = type_name[type];
205 static const char *sec_name(unsigned shndx)
207 const char *sec_strtab;
209 sec_strtab = secs[ehdr.e_shstrndx].strtab;
211 if (shndx < ehdr.e_shnum) {
212 name = sec_strtab + secs[shndx].shdr.sh_name;
214 else if (shndx == SHN_ABS) {
217 else if (shndx == SHN_COMMON) {
223 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
228 name = sym_strtab + sym->st_name;
231 name = sec_name(sym->st_shndx);
238 #if BYTE_ORDER == LITTLE_ENDIAN
239 #define le16_to_cpu(val) (val)
240 #define le32_to_cpu(val) (val)
242 #if BYTE_ORDER == BIG_ENDIAN
243 #define le16_to_cpu(val) bswap_16(val)
244 #define le32_to_cpu(val) bswap_32(val)
247 static uint16_t elf16_to_cpu(uint16_t val)
249 return le16_to_cpu(val);
252 static uint32_t elf32_to_cpu(uint32_t val)
254 return le32_to_cpu(val);
257 static void read_ehdr(FILE *fp)
259 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
260 die("Cannot read ELF header: %s\n",
263 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
264 die("No ELF magic\n");
266 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
267 die("Not a 32 bit executable\n");
269 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
270 die("Not a LSB ELF executable\n");
272 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
273 die("Unknown ELF version\n");
275 /* Convert the fields to native endian */
276 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
277 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
278 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
279 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
280 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
281 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
282 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
283 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
284 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
285 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
286 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
287 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
288 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
290 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
291 die("Unsupported ELF header type\n");
293 if (ehdr.e_machine != EM_386) {
294 die("Not for x86\n");
296 if (ehdr.e_version != EV_CURRENT) {
297 die("Unknown ELF version\n");
299 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
300 die("Bad Elf header size\n");
302 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
303 die("Bad program header entry\n");
305 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
306 die("Bad section header entry\n");
308 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
309 die("String table index out of bounds\n");
313 static void read_shdrs(FILE *fp)
318 secs = calloc(ehdr.e_shnum, sizeof(struct section));
320 die("Unable to allocate %d section headers\n",
323 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
324 die("Seek to %d failed: %s\n",
325 ehdr.e_shoff, strerror(errno));
327 for (i = 0; i < ehdr.e_shnum; i++) {
328 struct section *sec = &secs[i];
329 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
330 die("Cannot read ELF section headers %d/%d: %s\n",
331 i, ehdr.e_shnum, strerror(errno));
332 sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name);
333 sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type);
334 sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags);
335 sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr);
336 sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset);
337 sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size);
338 sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link);
339 sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info);
340 sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign);
341 sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize);
342 if (sec->shdr.sh_link < ehdr.e_shnum)
343 sec->link = &secs[sec->shdr.sh_link];
348 static void read_strtabs(FILE *fp)
351 for (i = 0; i < ehdr.e_shnum; i++) {
352 struct section *sec = &secs[i];
353 if (sec->shdr.sh_type != SHT_STRTAB) {
356 sec->strtab = malloc(sec->shdr.sh_size);
358 die("malloc of %d bytes for strtab failed\n",
361 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
362 die("Seek to %d failed: %s\n",
363 sec->shdr.sh_offset, strerror(errno));
365 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
366 != sec->shdr.sh_size) {
367 die("Cannot read symbol table: %s\n",
373 static void read_symtabs(FILE *fp)
376 for (i = 0; i < ehdr.e_shnum; i++) {
377 struct section *sec = &secs[i];
378 if (sec->shdr.sh_type != SHT_SYMTAB) {
381 sec->symtab = malloc(sec->shdr.sh_size);
383 die("malloc of %d bytes for symtab failed\n",
386 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
387 die("Seek to %d failed: %s\n",
388 sec->shdr.sh_offset, strerror(errno));
390 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
391 != sec->shdr.sh_size) {
392 die("Cannot read symbol table: %s\n",
395 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
396 Elf32_Sym *sym = &sec->symtab[j];
397 sym->st_name = elf32_to_cpu(sym->st_name);
398 sym->st_value = elf32_to_cpu(sym->st_value);
399 sym->st_size = elf32_to_cpu(sym->st_size);
400 sym->st_shndx = elf16_to_cpu(sym->st_shndx);
406 static void read_relocs(FILE *fp)
409 for (i = 0; i < ehdr.e_shnum; i++) {
410 struct section *sec = &secs[i];
411 if (sec->shdr.sh_type != SHT_REL) {
414 sec->reltab = malloc(sec->shdr.sh_size);
416 die("malloc of %d bytes for relocs failed\n",
419 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
420 die("Seek to %d failed: %s\n",
421 sec->shdr.sh_offset, strerror(errno));
423 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
424 != sec->shdr.sh_size) {
425 die("Cannot read symbol table: %s\n",
428 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
429 Elf32_Rel *rel = &sec->reltab[j];
430 rel->r_offset = elf32_to_cpu(rel->r_offset);
431 rel->r_info = elf32_to_cpu(rel->r_info);
437 static void print_absolute_symbols(void)
440 printf("Absolute symbols\n");
441 printf(" Num: Value Size Type Bind Visibility Name\n");
442 for (i = 0; i < ehdr.e_shnum; i++) {
443 struct section *sec = &secs[i];
447 if (sec->shdr.sh_type != SHT_SYMTAB) {
450 sym_strtab = sec->link->strtab;
451 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
454 sym = &sec->symtab[j];
455 name = sym_name(sym_strtab, sym);
456 if (sym->st_shndx != SHN_ABS) {
459 printf("%5d %08x %5d %10s %10s %12s %s\n",
460 j, sym->st_value, sym->st_size,
461 sym_type(ELF32_ST_TYPE(sym->st_info)),
462 sym_bind(ELF32_ST_BIND(sym->st_info)),
463 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
470 static void print_absolute_relocs(void)
474 for (i = 0; i < ehdr.e_shnum; i++) {
475 struct section *sec = &secs[i];
476 struct section *sec_applies, *sec_symtab;
478 Elf32_Sym *sh_symtab;
480 if (sec->shdr.sh_type != SHT_REL) {
483 sec_symtab = sec->link;
484 sec_applies = &secs[sec->shdr.sh_info];
485 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
488 sh_symtab = sec_symtab->symtab;
489 sym_strtab = sec_symtab->link->strtab;
490 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
494 rel = &sec->reltab[j];
495 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
496 name = sym_name(sym_strtab, sym);
497 if (sym->st_shndx != SHN_ABS) {
501 /* Absolute symbols are not relocated if bzImage is
502 * loaded at a non-compiled address. Display a warning
503 * to user at compile time about the absolute
504 * relocations present.
506 * User need to audit the code to make sure
507 * some symbols which should have been section
508 * relative have not become absolute because of some
509 * linker optimization or wrong programming usage.
511 * Before warning check if this absolute symbol
512 * relocation is harmless.
514 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
518 printf("WARNING: Absolute relocations"
520 printf("Offset Info Type Sym.Value "
525 printf("%08x %08x %10s %08x %s\n",
528 rel_type(ELF32_R_TYPE(rel->r_info)),
538 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym),
542 /* Walk through the relocations */
543 for (i = 0; i < ehdr.e_shnum; i++) {
545 Elf32_Sym *sh_symtab;
546 struct section *sec_applies, *sec_symtab;
548 struct section *sec = &secs[i];
550 if (sec->shdr.sh_type != SHT_REL) {
553 sec_symtab = sec->link;
554 sec_applies = &secs[sec->shdr.sh_info];
555 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
558 sh_symtab = sec_symtab->symtab;
559 sym_strtab = sec_symtab->link->strtab;
560 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
565 rel = &sec->reltab[j];
566 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
567 r_type = ELF32_R_TYPE(rel->r_info);
575 * NONE can be ignored and and PC relative
576 * relocations don't need to be adjusted.
581 symname = sym_name(sym_strtab, sym);
584 if (sym->st_shndx == SHN_ABS) {
585 if (is_reloc(S_ABS, symname))
587 else if (!is_reloc(S_SEG, symname))
590 if (is_reloc(S_LIN, symname))
599 symname = sym_name(sym_strtab, sym);
600 if (sym->st_shndx == SHN_ABS) {
601 if (is_reloc(S_ABS, symname))
603 else if (!is_reloc(S_REL, symname))
607 !is_reloc(S_LIN, symname))
613 die("Unsupported relocation type: %s (%d)\n",
614 rel_type(r_type), r_type);
617 symname = sym_name(sym_strtab, sym);
618 die("Invalid %s relocation: %s\n",
619 rel_type(r_type), symname);
625 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
627 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
633 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
635 /* Remember the address that needs to be adjusted. */
636 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
637 relocs16[reloc16_idx++] = rel->r_offset;
639 relocs[reloc_idx++] = rel->r_offset;
642 static int cmp_relocs(const void *va, const void *vb)
644 const unsigned long *a, *b;
646 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
649 static int write32(unsigned int v, FILE *f)
651 unsigned char buf[4];
653 buf[0] = (v >> 0) & 0xff;
654 buf[1] = (v >> 8) & 0xff;
655 buf[2] = (v >> 16) & 0xff;
656 buf[3] = (v >> 24) & 0xff;
657 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
660 static void emit_relocs(int as_text, int use_real_mode)
663 /* Count how many relocations I have and allocate space for them. */
665 walk_relocs(count_reloc, use_real_mode);
666 relocs = malloc(reloc_count * sizeof(relocs[0]));
668 die("malloc of %d entries for relocs failed\n",
672 relocs16 = malloc(reloc16_count * sizeof(relocs[0]));
674 die("malloc of %d entries for relocs16 failed\n",
677 /* Collect up the relocations */
679 walk_relocs(collect_reloc, use_real_mode);
681 if (reloc16_count && !use_real_mode)
682 die("Segment relocations found but --realmode not specified\n");
684 /* Order the relocations for more efficient processing */
685 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
686 qsort(relocs16, reloc16_count, sizeof(relocs16[0]), cmp_relocs);
688 /* Print the relocations */
690 /* Print the relocations in a form suitable that
693 printf(".section \".data.reloc\",\"a\"\n");
694 printf(".balign 4\n");
696 printf("\t.long %lu\n", reloc16_count);
697 for (i = 0; i < reloc16_count; i++)
698 printf("\t.long 0x%08lx\n", relocs16[i]);
699 printf("\t.long %lu\n", reloc_count);
700 for (i = 0; i < reloc_count; i++) {
701 printf("\t.long 0x%08lx\n", relocs[i]);
705 printf("\t.long 0x%08lx\n", (unsigned long)0);
706 for (i = 0; i < reloc_count; i++) {
707 printf("\t.long 0x%08lx\n", relocs[i]);
715 write32(reloc16_count, stdout);
716 for (i = 0; i < reloc16_count; i++)
717 write32(relocs16[i], stdout);
718 write32(reloc_count, stdout);
720 /* Now print each relocation */
721 for (i = 0; i < reloc_count; i++)
722 write32(relocs[i], stdout);
727 /* Now print each relocation */
728 for (i = 0; i < reloc_count; i++) {
729 write32(relocs[i], stdout);
735 static void usage(void)
737 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
740 int main(int argc, char **argv)
742 int show_absolute_syms, show_absolute_relocs;
743 int as_text, use_real_mode;
748 show_absolute_syms = 0;
749 show_absolute_relocs = 0;
753 for (i = 1; i < argc; i++) {
756 if (strcmp(arg, "--abs-syms") == 0) {
757 show_absolute_syms = 1;
760 if (strcmp(arg, "--abs-relocs") == 0) {
761 show_absolute_relocs = 1;
764 if (strcmp(arg, "--text") == 0) {
768 if (strcmp(arg, "--realmode") == 0) {
782 regex_init(use_real_mode);
783 fp = fopen(fname, "r");
785 die("Cannot open %s: %s\n",
786 fname, strerror(errno));
793 if (show_absolute_syms) {
794 print_absolute_symbols();
797 if (show_absolute_relocs) {
798 print_absolute_relocs();
801 emit_relocs(as_text, use_real_mode);