14 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
15 static Elf32_Ehdr ehdr;
16 static Elf32_Shdr shdr[MAX_SHDRS];
17 static Elf32_Sym *symtab[MAX_SHDRS];
18 static Elf32_Rel *reltab[MAX_SHDRS];
19 static char *strtab[MAX_SHDRS];
20 static unsigned long reloc_count, reloc_idx;
21 static unsigned long *relocs;
24 * Following symbols have been audited. There values are constant and do
25 * not change if bzImage is loaded at a different physical address than
26 * the address for which it has been compiled. Don't warn user about
27 * absolute relocations present w.r.t these symbols.
29 static const char* safe_abs_relocs[] = {
31 "__kernel_rt_sigreturn",
35 "xen_irq_disable_direct_reloc",
36 "xen_save_fl_direct_reloc",
39 static int is_safe_abs_reloc(const char* sym_name)
43 array_size = sizeof(safe_abs_relocs)/sizeof(char*);
45 for(i = 0; i < array_size; i++) {
46 if (!strcmp(sym_name, safe_abs_relocs[i]))
50 if (strncmp(sym_name, "__crc_", 6) == 0)
55 static void die(char *fmt, ...)
59 vfprintf(stderr, fmt, ap);
64 static const char *sym_type(unsigned type)
66 static const char *type_name[] = {
67 #define SYM_TYPE(X) [X] = #X
71 SYM_TYPE(STT_SECTION),
77 const char *name = "unknown sym type name";
78 if (type < ARRAY_SIZE(type_name)) {
79 name = type_name[type];
84 static const char *sym_bind(unsigned bind)
86 static const char *bind_name[] = {
87 #define SYM_BIND(X) [X] = #X
93 const char *name = "unknown sym bind name";
94 if (bind < ARRAY_SIZE(bind_name)) {
95 name = bind_name[bind];
100 static const char *sym_visibility(unsigned visibility)
102 static const char *visibility_name[] = {
103 #define SYM_VISIBILITY(X) [X] = #X
104 SYM_VISIBILITY(STV_DEFAULT),
105 SYM_VISIBILITY(STV_INTERNAL),
106 SYM_VISIBILITY(STV_HIDDEN),
107 SYM_VISIBILITY(STV_PROTECTED),
108 #undef SYM_VISIBILITY
110 const char *name = "unknown sym visibility name";
111 if (visibility < ARRAY_SIZE(visibility_name)) {
112 name = visibility_name[visibility];
117 static const char *rel_type(unsigned type)
119 static const char *type_name[] = {
120 #define REL_TYPE(X) [X] = #X
121 REL_TYPE(R_386_NONE),
123 REL_TYPE(R_386_PC32),
124 REL_TYPE(R_386_GOT32),
125 REL_TYPE(R_386_PLT32),
126 REL_TYPE(R_386_COPY),
127 REL_TYPE(R_386_GLOB_DAT),
128 REL_TYPE(R_386_JMP_SLOT),
129 REL_TYPE(R_386_RELATIVE),
130 REL_TYPE(R_386_GOTOFF),
131 REL_TYPE(R_386_GOTPC),
134 const char *name = "unknown type rel type name";
135 if (type < ARRAY_SIZE(type_name)) {
136 name = type_name[type];
141 static const char *sec_name(unsigned shndx)
143 const char *sec_strtab;
145 sec_strtab = strtab[ehdr.e_shstrndx];
147 if (shndx < ehdr.e_shnum) {
148 name = sec_strtab + shdr[shndx].sh_name;
150 else if (shndx == SHN_ABS) {
153 else if (shndx == SHN_COMMON) {
159 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
164 name = sym_strtab + sym->st_name;
167 name = sec_name(shdr[sym->st_shndx].sh_name);
174 #if BYTE_ORDER == LITTLE_ENDIAN
175 #define le16_to_cpu(val) (val)
176 #define le32_to_cpu(val) (val)
178 #if BYTE_ORDER == BIG_ENDIAN
179 #define le16_to_cpu(val) bswap_16(val)
180 #define le32_to_cpu(val) bswap_32(val)
183 static uint16_t elf16_to_cpu(uint16_t val)
185 return le16_to_cpu(val);
188 static uint32_t elf32_to_cpu(uint32_t val)
190 return le32_to_cpu(val);
193 static void read_ehdr(FILE *fp)
195 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
196 die("Cannot read ELF header: %s\n",
199 if (memcmp(ehdr.e_ident, ELFMAG, 4) != 0) {
200 die("No ELF magic\n");
202 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
203 die("Not a 32 bit executable\n");
205 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
206 die("Not a LSB ELF executable\n");
208 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
209 die("Unknown ELF version\n");
211 /* Convert the fields to native endian */
212 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
213 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
214 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
215 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
216 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
217 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
218 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
219 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
220 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
221 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
222 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
223 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
224 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
226 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
227 die("Unsupported ELF header type\n");
229 if (ehdr.e_machine != EM_386) {
230 die("Not for x86\n");
232 if (ehdr.e_version != EV_CURRENT) {
233 die("Unknown ELF version\n");
235 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
236 die("Bad Elf header size\n");
238 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
239 die("Bad program header entry\n");
241 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
242 die("Bad section header entry\n");
244 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
245 die("String table index out of bounds\n");
249 static void read_shdrs(FILE *fp)
252 if (ehdr.e_shnum > MAX_SHDRS) {
253 die("%d section headers supported: %d\n",
254 ehdr.e_shnum, MAX_SHDRS);
256 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
257 die("Seek to %d failed: %s\n",
258 ehdr.e_shoff, strerror(errno));
260 if (fread(&shdr, sizeof(shdr[0]), ehdr.e_shnum, fp) != ehdr.e_shnum) {
261 die("Cannot read ELF section headers: %s\n",
264 for(i = 0; i < ehdr.e_shnum; i++) {
265 shdr[i].sh_name = elf32_to_cpu(shdr[i].sh_name);
266 shdr[i].sh_type = elf32_to_cpu(shdr[i].sh_type);
267 shdr[i].sh_flags = elf32_to_cpu(shdr[i].sh_flags);
268 shdr[i].sh_addr = elf32_to_cpu(shdr[i].sh_addr);
269 shdr[i].sh_offset = elf32_to_cpu(shdr[i].sh_offset);
270 shdr[i].sh_size = elf32_to_cpu(shdr[i].sh_size);
271 shdr[i].sh_link = elf32_to_cpu(shdr[i].sh_link);
272 shdr[i].sh_info = elf32_to_cpu(shdr[i].sh_info);
273 shdr[i].sh_addralign = elf32_to_cpu(shdr[i].sh_addralign);
274 shdr[i].sh_entsize = elf32_to_cpu(shdr[i].sh_entsize);
279 static void read_strtabs(FILE *fp)
282 for(i = 0; i < ehdr.e_shnum; i++) {
283 if (shdr[i].sh_type != SHT_STRTAB) {
286 strtab[i] = malloc(shdr[i].sh_size);
288 die("malloc of %d bytes for strtab failed\n",
291 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
292 die("Seek to %d failed: %s\n",
293 shdr[i].sh_offset, strerror(errno));
295 if (fread(strtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
296 die("Cannot read symbol table: %s\n",
302 static void read_symtabs(FILE *fp)
305 for(i = 0; i < ehdr.e_shnum; i++) {
306 if (shdr[i].sh_type != SHT_SYMTAB) {
309 symtab[i] = malloc(shdr[i].sh_size);
311 die("malloc of %d bytes for symtab failed\n",
314 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
315 die("Seek to %d failed: %s\n",
316 shdr[i].sh_offset, strerror(errno));
318 if (fread(symtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
319 die("Cannot read symbol table: %s\n",
322 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[i][0]); j++) {
323 symtab[i][j].st_name = elf32_to_cpu(symtab[i][j].st_name);
324 symtab[i][j].st_value = elf32_to_cpu(symtab[i][j].st_value);
325 symtab[i][j].st_size = elf32_to_cpu(symtab[i][j].st_size);
326 symtab[i][j].st_shndx = elf16_to_cpu(symtab[i][j].st_shndx);
332 static void read_relocs(FILE *fp)
335 for(i = 0; i < ehdr.e_shnum; i++) {
336 if (shdr[i].sh_type != SHT_REL) {
339 reltab[i] = malloc(shdr[i].sh_size);
341 die("malloc of %d bytes for relocs failed\n",
344 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
345 die("Seek to %d failed: %s\n",
346 shdr[i].sh_offset, strerror(errno));
348 if (fread(reltab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
349 die("Cannot read symbol table: %s\n",
352 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
353 reltab[i][j].r_offset = elf32_to_cpu(reltab[i][j].r_offset);
354 reltab[i][j].r_info = elf32_to_cpu(reltab[i][j].r_info);
360 static void print_absolute_symbols(void)
363 printf("Absolute symbols\n");
364 printf(" Num: Value Size Type Bind Visibility Name\n");
365 for(i = 0; i < ehdr.e_shnum; i++) {
367 Elf32_Sym *sh_symtab;
369 if (shdr[i].sh_type != SHT_SYMTAB) {
372 sh_symtab = symtab[i];
373 sym_strtab = strtab[shdr[i].sh_link];
374 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[0][0]); j++) {
378 name = sym_name(sym_strtab, sym);
379 if (sym->st_shndx != SHN_ABS) {
382 printf("%5d %08x %5d %10s %10s %12s %s\n",
383 j, sym->st_value, sym->st_size,
384 sym_type(ELF32_ST_TYPE(sym->st_info)),
385 sym_bind(ELF32_ST_BIND(sym->st_info)),
386 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
393 static void print_absolute_relocs(void)
397 for(i = 0; i < ehdr.e_shnum; i++) {
399 Elf32_Sym *sh_symtab;
400 unsigned sec_applies, sec_symtab;
402 if (shdr[i].sh_type != SHT_REL) {
405 sec_symtab = shdr[i].sh_link;
406 sec_applies = shdr[i].sh_info;
407 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
410 sh_symtab = symtab[sec_symtab];
411 sym_strtab = strtab[shdr[sec_symtab].sh_link];
412 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
417 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
418 name = sym_name(sym_strtab, sym);
419 if (sym->st_shndx != SHN_ABS) {
423 /* Absolute symbols are not relocated if bzImage is
424 * loaded at a non-compiled address. Display a warning
425 * to user at compile time about the absolute
426 * relocations present.
428 * User need to audit the code to make sure
429 * some symbols which should have been section
430 * relative have not become absolute because of some
431 * linker optimization or wrong programming usage.
433 * Before warning check if this absolute symbol
434 * relocation is harmless.
436 if (is_safe_abs_reloc(name))
440 printf("WARNING: Absolute relocations"
442 printf("Offset Info Type Sym.Value "
447 printf("%08x %08x %10s %08x %s\n",
450 rel_type(ELF32_R_TYPE(rel->r_info)),
460 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
463 /* Walk through the relocations */
464 for(i = 0; i < ehdr.e_shnum; i++) {
466 Elf32_Sym *sh_symtab;
467 unsigned sec_applies, sec_symtab;
469 if (shdr[i].sh_type != SHT_REL) {
472 sec_symtab = shdr[i].sh_link;
473 sec_applies = shdr[i].sh_info;
474 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
477 sh_symtab = symtab[sec_symtab];
478 sym_strtab = strtab[shdr[sec_symtab].sh_link];
479 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
484 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
485 r_type = ELF32_R_TYPE(rel->r_info);
486 /* Don't visit relocations to absolute symbols */
487 if (sym->st_shndx == SHN_ABS) {
490 if (r_type == R_386_PC32) {
491 /* PC relative relocations don't need to be adjusted */
493 else if (r_type == R_386_32) {
494 /* Visit relocations that need to be adjusted */
498 die("Unsupported relocation type: %d\n", r_type);
504 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
509 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
511 /* Remember the address that needs to be adjusted. */
512 relocs[reloc_idx++] = rel->r_offset;
515 static int cmp_relocs(const void *va, const void *vb)
517 const unsigned long *a, *b;
519 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
522 static void emit_relocs(int as_text)
525 /* Count how many relocations I have and allocate space for them. */
527 walk_relocs(count_reloc);
528 relocs = malloc(reloc_count * sizeof(relocs[0]));
530 die("malloc of %d entries for relocs failed\n",
533 /* Collect up the relocations */
535 walk_relocs(collect_reloc);
537 /* Order the relocations for more efficient processing */
538 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
540 /* Print the relocations */
542 /* Print the relocations in a form suitable that
545 printf(".section \".data.reloc\",\"a\"\n");
546 printf(".balign 4\n");
547 for(i = 0; i < reloc_count; i++) {
548 printf("\t .long 0x%08lx\n", relocs[i]);
553 unsigned char buf[4];
554 buf[0] = buf[1] = buf[2] = buf[3] = 0;
556 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
557 /* Now print each relocation */
558 for(i = 0; i < reloc_count; i++) {
559 buf[0] = (relocs[i] >> 0) & 0xff;
560 buf[1] = (relocs[i] >> 8) & 0xff;
561 buf[2] = (relocs[i] >> 16) & 0xff;
562 buf[3] = (relocs[i] >> 24) & 0xff;
563 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
568 static void usage(void)
570 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
573 int main(int argc, char **argv)
575 int show_absolute_syms, show_absolute_relocs;
581 show_absolute_syms = 0;
582 show_absolute_relocs = 0;
585 for(i = 1; i < argc; i++) {
588 if (strcmp(argv[1], "--abs-syms") == 0) {
589 show_absolute_syms = 1;
593 if (strcmp(argv[1], "--abs-relocs") == 0) {
594 show_absolute_relocs = 1;
597 else if (strcmp(argv[1], "--text") == 0) {
611 fp = fopen(fname, "r");
613 die("Cannot open %s: %s\n",
614 fname, strerror(errno));
621 if (show_absolute_syms) {
622 print_absolute_symbols();
625 if (show_absolute_relocs) {
626 print_absolute_relocs();
629 emit_relocs(as_text);