14 static Elf32_Ehdr ehdr;
15 static Elf32_Shdr shdr[MAX_SHDRS];
16 static Elf32_Sym *symtab[MAX_SHDRS];
17 static Elf32_Rel *reltab[MAX_SHDRS];
18 static char *strtab[MAX_SHDRS];
19 static unsigned long reloc_count, reloc_idx;
20 static unsigned long *relocs;
23 * Following symbols have been audited. There values are constant and do
24 * not change if bzImage is loaded at a different physical address than
25 * the address for which it has been compiled. Don't warn user about
26 * absolute relocations present w.r.t these symbols.
28 static const char* safe_abs_relocs[] = {
30 "__kernel_rt_sigreturn",
35 static int is_safe_abs_reloc(const char* sym_name)
39 array_size = sizeof(safe_abs_relocs)/sizeof(char*);
41 for(i = 0; i < array_size; i++) {
42 if (!strcmp(sym_name, safe_abs_relocs[i]))
46 if (strncmp(sym_name, "__crc_", 6) == 0)
51 static void die(char *fmt, ...)
55 vfprintf(stderr, fmt, ap);
60 static const char *sym_type(unsigned type)
62 static const char *type_name[] = {
63 #define SYM_TYPE(X) [X] = #X
67 SYM_TYPE(STT_SECTION),
73 const char *name = "unknown sym type name";
74 if (type < sizeof(type_name)/sizeof(type_name[0])) {
75 name = type_name[type];
80 static const char *sym_bind(unsigned bind)
82 static const char *bind_name[] = {
83 #define SYM_BIND(X) [X] = #X
89 const char *name = "unknown sym bind name";
90 if (bind < sizeof(bind_name)/sizeof(bind_name[0])) {
91 name = bind_name[bind];
96 static const char *sym_visibility(unsigned visibility)
98 static const char *visibility_name[] = {
99 #define SYM_VISIBILITY(X) [X] = #X
100 SYM_VISIBILITY(STV_DEFAULT),
101 SYM_VISIBILITY(STV_INTERNAL),
102 SYM_VISIBILITY(STV_HIDDEN),
103 SYM_VISIBILITY(STV_PROTECTED),
104 #undef SYM_VISIBILITY
106 const char *name = "unknown sym visibility name";
107 if (visibility < sizeof(visibility_name)/sizeof(visibility_name[0])) {
108 name = visibility_name[visibility];
113 static const char *rel_type(unsigned type)
115 static const char *type_name[] = {
116 #define REL_TYPE(X) [X] = #X
117 REL_TYPE(R_386_NONE),
119 REL_TYPE(R_386_PC32),
120 REL_TYPE(R_386_GOT32),
121 REL_TYPE(R_386_PLT32),
122 REL_TYPE(R_386_COPY),
123 REL_TYPE(R_386_GLOB_DAT),
124 REL_TYPE(R_386_JMP_SLOT),
125 REL_TYPE(R_386_RELATIVE),
126 REL_TYPE(R_386_GOTOFF),
127 REL_TYPE(R_386_GOTPC),
130 const char *name = "unknown type rel type name";
131 if (type < sizeof(type_name)/sizeof(type_name[0])) {
132 name = type_name[type];
137 static const char *sec_name(unsigned shndx)
139 const char *sec_strtab;
141 sec_strtab = strtab[ehdr.e_shstrndx];
143 if (shndx < ehdr.e_shnum) {
144 name = sec_strtab + shdr[shndx].sh_name;
146 else if (shndx == SHN_ABS) {
149 else if (shndx == SHN_COMMON) {
155 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
160 name = sym_strtab + sym->st_name;
163 name = sec_name(shdr[sym->st_shndx].sh_name);
170 #if BYTE_ORDER == LITTLE_ENDIAN
171 #define le16_to_cpu(val) (val)
172 #define le32_to_cpu(val) (val)
174 #if BYTE_ORDER == BIG_ENDIAN
175 #define le16_to_cpu(val) bswap_16(val)
176 #define le32_to_cpu(val) bswap_32(val)
179 static uint16_t elf16_to_cpu(uint16_t val)
181 return le16_to_cpu(val);
184 static uint32_t elf32_to_cpu(uint32_t val)
186 return le32_to_cpu(val);
189 static void read_ehdr(FILE *fp)
191 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
192 die("Cannot read ELF header: %s\n",
195 if (memcmp(ehdr.e_ident, ELFMAG, 4) != 0) {
196 die("No ELF magic\n");
198 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
199 die("Not a 32 bit executable\n");
201 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
202 die("Not a LSB ELF executable\n");
204 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
205 die("Unknown ELF version\n");
207 /* Convert the fields to native endian */
208 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
209 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
210 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
211 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
212 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
213 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
214 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
215 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
216 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
217 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
218 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
219 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
220 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
222 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
223 die("Unsupported ELF header type\n");
225 if (ehdr.e_machine != EM_386) {
226 die("Not for x86\n");
228 if (ehdr.e_version != EV_CURRENT) {
229 die("Unknown ELF version\n");
231 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
232 die("Bad Elf header size\n");
234 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
235 die("Bad program header entry\n");
237 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
238 die("Bad section header entry\n");
240 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
241 die("String table index out of bounds\n");
245 static void read_shdrs(FILE *fp)
248 if (ehdr.e_shnum > MAX_SHDRS) {
249 die("%d section headers supported: %d\n",
250 ehdr.e_shnum, MAX_SHDRS);
252 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
253 die("Seek to %d failed: %s\n",
254 ehdr.e_shoff, strerror(errno));
256 if (fread(&shdr, sizeof(shdr[0]), ehdr.e_shnum, fp) != ehdr.e_shnum) {
257 die("Cannot read ELF section headers: %s\n",
260 for(i = 0; i < ehdr.e_shnum; i++) {
261 shdr[i].sh_name = elf32_to_cpu(shdr[i].sh_name);
262 shdr[i].sh_type = elf32_to_cpu(shdr[i].sh_type);
263 shdr[i].sh_flags = elf32_to_cpu(shdr[i].sh_flags);
264 shdr[i].sh_addr = elf32_to_cpu(shdr[i].sh_addr);
265 shdr[i].sh_offset = elf32_to_cpu(shdr[i].sh_offset);
266 shdr[i].sh_size = elf32_to_cpu(shdr[i].sh_size);
267 shdr[i].sh_link = elf32_to_cpu(shdr[i].sh_link);
268 shdr[i].sh_info = elf32_to_cpu(shdr[i].sh_info);
269 shdr[i].sh_addralign = elf32_to_cpu(shdr[i].sh_addralign);
270 shdr[i].sh_entsize = elf32_to_cpu(shdr[i].sh_entsize);
275 static void read_strtabs(FILE *fp)
278 for(i = 0; i < ehdr.e_shnum; i++) {
279 if (shdr[i].sh_type != SHT_STRTAB) {
282 strtab[i] = malloc(shdr[i].sh_size);
284 die("malloc of %d bytes for strtab failed\n",
287 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
288 die("Seek to %d failed: %s\n",
289 shdr[i].sh_offset, strerror(errno));
291 if (fread(strtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
292 die("Cannot read symbol table: %s\n",
298 static void read_symtabs(FILE *fp)
301 for(i = 0; i < ehdr.e_shnum; i++) {
302 if (shdr[i].sh_type != SHT_SYMTAB) {
305 symtab[i] = malloc(shdr[i].sh_size);
307 die("malloc of %d bytes for symtab failed\n",
310 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
311 die("Seek to %d failed: %s\n",
312 shdr[i].sh_offset, strerror(errno));
314 if (fread(symtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
315 die("Cannot read symbol table: %s\n",
318 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[i][0]); j++) {
319 symtab[i][j].st_name = elf32_to_cpu(symtab[i][j].st_name);
320 symtab[i][j].st_value = elf32_to_cpu(symtab[i][j].st_value);
321 symtab[i][j].st_size = elf32_to_cpu(symtab[i][j].st_size);
322 symtab[i][j].st_shndx = elf16_to_cpu(symtab[i][j].st_shndx);
328 static void read_relocs(FILE *fp)
331 for(i = 0; i < ehdr.e_shnum; i++) {
332 if (shdr[i].sh_type != SHT_REL) {
335 reltab[i] = malloc(shdr[i].sh_size);
337 die("malloc of %d bytes for relocs failed\n",
340 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
341 die("Seek to %d failed: %s\n",
342 shdr[i].sh_offset, strerror(errno));
344 if (fread(reltab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
345 die("Cannot read symbol table: %s\n",
348 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
349 reltab[i][j].r_offset = elf32_to_cpu(reltab[i][j].r_offset);
350 reltab[i][j].r_info = elf32_to_cpu(reltab[i][j].r_info);
356 static void print_absolute_symbols(void)
359 printf("Absolute symbols\n");
360 printf(" Num: Value Size Type Bind Visibility Name\n");
361 for(i = 0; i < ehdr.e_shnum; i++) {
363 Elf32_Sym *sh_symtab;
365 if (shdr[i].sh_type != SHT_SYMTAB) {
368 sh_symtab = symtab[i];
369 sym_strtab = strtab[shdr[i].sh_link];
370 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[0][0]); j++) {
374 name = sym_name(sym_strtab, sym);
375 if (sym->st_shndx != SHN_ABS) {
378 printf("%5d %08x %5d %10s %10s %12s %s\n",
379 j, sym->st_value, sym->st_size,
380 sym_type(ELF32_ST_TYPE(sym->st_info)),
381 sym_bind(ELF32_ST_BIND(sym->st_info)),
382 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
389 static void print_absolute_relocs(void)
393 for(i = 0; i < ehdr.e_shnum; i++) {
395 Elf32_Sym *sh_symtab;
396 unsigned sec_applies, sec_symtab;
398 if (shdr[i].sh_type != SHT_REL) {
401 sec_symtab = shdr[i].sh_link;
402 sec_applies = shdr[i].sh_info;
403 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
406 sh_symtab = symtab[sec_symtab];
407 sym_strtab = strtab[shdr[sec_symtab].sh_link];
408 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
413 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
414 name = sym_name(sym_strtab, sym);
415 if (sym->st_shndx != SHN_ABS) {
419 /* Absolute symbols are not relocated if bzImage is
420 * loaded at a non-compiled address. Display a warning
421 * to user at compile time about the absolute
422 * relocations present.
424 * User need to audit the code to make sure
425 * some symbols which should have been section
426 * relative have not become absolute because of some
427 * linker optimization or wrong programming usage.
429 * Before warning check if this absolute symbol
430 * relocation is harmless.
432 if (is_safe_abs_reloc(name))
436 printf("WARNING: Absolute relocations"
438 printf("Offset Info Type Sym.Value "
443 printf("%08x %08x %10s %08x %s\n",
446 rel_type(ELF32_R_TYPE(rel->r_info)),
456 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
459 /* Walk through the relocations */
460 for(i = 0; i < ehdr.e_shnum; i++) {
462 Elf32_Sym *sh_symtab;
463 unsigned sec_applies, sec_symtab;
465 if (shdr[i].sh_type != SHT_REL) {
468 sec_symtab = shdr[i].sh_link;
469 sec_applies = shdr[i].sh_info;
470 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
473 sh_symtab = symtab[sec_symtab];
474 sym_strtab = strtab[shdr[sec_symtab].sh_link];
475 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
480 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
481 r_type = ELF32_R_TYPE(rel->r_info);
482 /* Don't visit relocations to absolute symbols */
483 if (sym->st_shndx == SHN_ABS) {
486 if (r_type == R_386_PC32) {
487 /* PC relative relocations don't need to be adjusted */
489 else if (r_type == R_386_32) {
490 /* Visit relocations that need to be adjusted */
494 die("Unsupported relocation type: %d\n", r_type);
500 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
505 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
507 /* Remember the address that needs to be adjusted. */
508 relocs[reloc_idx++] = rel->r_offset;
511 static int cmp_relocs(const void *va, const void *vb)
513 const unsigned long *a, *b;
515 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
518 static void emit_relocs(int as_text)
521 /* Count how many relocations I have and allocate space for them. */
523 walk_relocs(count_reloc);
524 relocs = malloc(reloc_count * sizeof(relocs[0]));
526 die("malloc of %d entries for relocs failed\n",
529 /* Collect up the relocations */
531 walk_relocs(collect_reloc);
533 /* Order the relocations for more efficient processing */
534 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
536 /* Print the relocations */
538 /* Print the relocations in a form suitable that
541 printf(".section \".data.reloc\",\"a\"\n");
542 printf(".balign 4\n");
543 for(i = 0; i < reloc_count; i++) {
544 printf("\t .long 0x%08lx\n", relocs[i]);
549 unsigned char buf[4];
550 buf[0] = buf[1] = buf[2] = buf[3] = 0;
552 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
553 /* Now print each relocation */
554 for(i = 0; i < reloc_count; i++) {
555 buf[0] = (relocs[i] >> 0) & 0xff;
556 buf[1] = (relocs[i] >> 8) & 0xff;
557 buf[2] = (relocs[i] >> 16) & 0xff;
558 buf[3] = (relocs[i] >> 24) & 0xff;
559 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
564 static void usage(void)
566 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
569 int main(int argc, char **argv)
571 int show_absolute_syms, show_absolute_relocs;
577 show_absolute_syms = 0;
578 show_absolute_relocs = 0;
581 for(i = 1; i < argc; i++) {
584 if (strcmp(argv[1], "--abs-syms") == 0) {
585 show_absolute_syms = 1;
589 if (strcmp(argv[1], "--abs-relocs") == 0) {
590 show_absolute_relocs = 1;
593 else if (strcmp(argv[1], "--text") == 0) {
607 fp = fopen(fname, "r");
609 die("Cannot open %s: %s\n",
610 fname, strerror(errno));
617 if (show_absolute_syms) {
618 print_absolute_symbols();
621 if (show_absolute_relocs) {
622 print_absolute_relocs();
625 emit_relocs(as_text);