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]))
49 static void die(char *fmt, ...)
53 vfprintf(stderr, fmt, ap);
58 static const char *sym_type(unsigned type)
60 static const char *type_name[] = {
61 #define SYM_TYPE(X) [X] = #X
65 SYM_TYPE(STT_SECTION),
71 const char *name = "unknown sym type name";
72 if (type < sizeof(type_name)/sizeof(type_name[0])) {
73 name = type_name[type];
78 static const char *sym_bind(unsigned bind)
80 static const char *bind_name[] = {
81 #define SYM_BIND(X) [X] = #X
87 const char *name = "unknown sym bind name";
88 if (bind < sizeof(bind_name)/sizeof(bind_name[0])) {
89 name = bind_name[bind];
94 static const char *sym_visibility(unsigned visibility)
96 static const char *visibility_name[] = {
97 #define SYM_VISIBILITY(X) [X] = #X
98 SYM_VISIBILITY(STV_DEFAULT),
99 SYM_VISIBILITY(STV_INTERNAL),
100 SYM_VISIBILITY(STV_HIDDEN),
101 SYM_VISIBILITY(STV_PROTECTED),
102 #undef SYM_VISIBILITY
104 const char *name = "unknown sym visibility name";
105 if (visibility < sizeof(visibility_name)/sizeof(visibility_name[0])) {
106 name = visibility_name[visibility];
111 static const char *rel_type(unsigned type)
113 static const char *type_name[] = {
114 #define REL_TYPE(X) [X] = #X
115 REL_TYPE(R_386_NONE),
117 REL_TYPE(R_386_PC32),
118 REL_TYPE(R_386_GOT32),
119 REL_TYPE(R_386_PLT32),
120 REL_TYPE(R_386_COPY),
121 REL_TYPE(R_386_GLOB_DAT),
122 REL_TYPE(R_386_JMP_SLOT),
123 REL_TYPE(R_386_RELATIVE),
124 REL_TYPE(R_386_GOTOFF),
125 REL_TYPE(R_386_GOTPC),
128 const char *name = "unknown type rel type name";
129 if (type < sizeof(type_name)/sizeof(type_name[0])) {
130 name = type_name[type];
135 static const char *sec_name(unsigned shndx)
137 const char *sec_strtab;
139 sec_strtab = strtab[ehdr.e_shstrndx];
141 if (shndx < ehdr.e_shnum) {
142 name = sec_strtab + shdr[shndx].sh_name;
144 else if (shndx == SHN_ABS) {
147 else if (shndx == SHN_COMMON) {
153 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
158 name = sym_strtab + sym->st_name;
161 name = sec_name(shdr[sym->st_shndx].sh_name);
168 #if BYTE_ORDER == LITTLE_ENDIAN
169 #define le16_to_cpu(val) (val)
170 #define le32_to_cpu(val) (val)
172 #if BYTE_ORDER == BIG_ENDIAN
173 #define le16_to_cpu(val) bswap_16(val)
174 #define le32_to_cpu(val) bswap_32(val)
177 static uint16_t elf16_to_cpu(uint16_t val)
179 return le16_to_cpu(val);
182 static uint32_t elf32_to_cpu(uint32_t val)
184 return le32_to_cpu(val);
187 static void read_ehdr(FILE *fp)
189 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
190 die("Cannot read ELF header: %s\n",
193 if (memcmp(ehdr.e_ident, ELFMAG, 4) != 0) {
194 die("No ELF magic\n");
196 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
197 die("Not a 32 bit executable\n");
199 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
200 die("Not a LSB ELF executable\n");
202 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
203 die("Unknown ELF version\n");
205 /* Convert the fields to native endian */
206 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
207 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
208 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
209 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
210 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
211 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
212 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
213 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
214 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
215 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
216 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
217 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
218 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
220 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
221 die("Unsupported ELF header type\n");
223 if (ehdr.e_machine != EM_386) {
224 die("Not for x86\n");
226 if (ehdr.e_version != EV_CURRENT) {
227 die("Unknown ELF version\n");
229 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
230 die("Bad Elf header size\n");
232 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
233 die("Bad program header entry\n");
235 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
236 die("Bad section header entry\n");
238 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
239 die("String table index out of bounds\n");
243 static void read_shdrs(FILE *fp)
246 if (ehdr.e_shnum > MAX_SHDRS) {
247 die("%d section headers supported: %d\n",
248 ehdr.e_shnum, MAX_SHDRS);
250 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
251 die("Seek to %d failed: %s\n",
252 ehdr.e_shoff, strerror(errno));
254 if (fread(&shdr, sizeof(shdr[0]), ehdr.e_shnum, fp) != ehdr.e_shnum) {
255 die("Cannot read ELF section headers: %s\n",
258 for(i = 0; i < ehdr.e_shnum; i++) {
259 shdr[i].sh_name = elf32_to_cpu(shdr[i].sh_name);
260 shdr[i].sh_type = elf32_to_cpu(shdr[i].sh_type);
261 shdr[i].sh_flags = elf32_to_cpu(shdr[i].sh_flags);
262 shdr[i].sh_addr = elf32_to_cpu(shdr[i].sh_addr);
263 shdr[i].sh_offset = elf32_to_cpu(shdr[i].sh_offset);
264 shdr[i].sh_size = elf32_to_cpu(shdr[i].sh_size);
265 shdr[i].sh_link = elf32_to_cpu(shdr[i].sh_link);
266 shdr[i].sh_info = elf32_to_cpu(shdr[i].sh_info);
267 shdr[i].sh_addralign = elf32_to_cpu(shdr[i].sh_addralign);
268 shdr[i].sh_entsize = elf32_to_cpu(shdr[i].sh_entsize);
273 static void read_strtabs(FILE *fp)
276 for(i = 0; i < ehdr.e_shnum; i++) {
277 if (shdr[i].sh_type != SHT_STRTAB) {
280 strtab[i] = malloc(shdr[i].sh_size);
282 die("malloc of %d bytes for strtab failed\n",
285 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
286 die("Seek to %d failed: %s\n",
287 shdr[i].sh_offset, strerror(errno));
289 if (fread(strtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
290 die("Cannot read symbol table: %s\n",
296 static void read_symtabs(FILE *fp)
299 for(i = 0; i < ehdr.e_shnum; i++) {
300 if (shdr[i].sh_type != SHT_SYMTAB) {
303 symtab[i] = malloc(shdr[i].sh_size);
305 die("malloc of %d bytes for symtab failed\n",
308 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
309 die("Seek to %d failed: %s\n",
310 shdr[i].sh_offset, strerror(errno));
312 if (fread(symtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
313 die("Cannot read symbol table: %s\n",
316 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[i][0]); j++) {
317 symtab[i][j].st_name = elf32_to_cpu(symtab[i][j].st_name);
318 symtab[i][j].st_value = elf32_to_cpu(symtab[i][j].st_value);
319 symtab[i][j].st_size = elf32_to_cpu(symtab[i][j].st_size);
320 symtab[i][j].st_shndx = elf16_to_cpu(symtab[i][j].st_shndx);
326 static void read_relocs(FILE *fp)
329 for(i = 0; i < ehdr.e_shnum; i++) {
330 if (shdr[i].sh_type != SHT_REL) {
333 reltab[i] = malloc(shdr[i].sh_size);
335 die("malloc of %d bytes for relocs failed\n",
338 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
339 die("Seek to %d failed: %s\n",
340 shdr[i].sh_offset, strerror(errno));
342 if (fread(reltab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
343 die("Cannot read symbol table: %s\n",
346 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
347 reltab[i][j].r_offset = elf32_to_cpu(reltab[i][j].r_offset);
348 reltab[i][j].r_info = elf32_to_cpu(reltab[i][j].r_info);
354 static void print_absolute_symbols(void)
357 printf("Absolute symbols\n");
358 printf(" Num: Value Size Type Bind Visibility Name\n");
359 for(i = 0; i < ehdr.e_shnum; i++) {
361 Elf32_Sym *sh_symtab;
363 if (shdr[i].sh_type != SHT_SYMTAB) {
366 sh_symtab = symtab[i];
367 sym_strtab = strtab[shdr[i].sh_link];
368 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[0][0]); j++) {
372 name = sym_name(sym_strtab, sym);
373 if (sym->st_shndx != SHN_ABS) {
376 printf("%5d %08x %5d %10s %10s %12s %s\n",
377 j, sym->st_value, sym->st_size,
378 sym_type(ELF32_ST_TYPE(sym->st_info)),
379 sym_bind(ELF32_ST_BIND(sym->st_info)),
380 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
387 static void print_absolute_relocs(void)
391 for(i = 0; i < ehdr.e_shnum; i++) {
393 Elf32_Sym *sh_symtab;
394 unsigned sec_applies, sec_symtab;
396 if (shdr[i].sh_type != SHT_REL) {
399 sec_symtab = shdr[i].sh_link;
400 sec_applies = shdr[i].sh_info;
401 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
404 sh_symtab = symtab[sec_symtab];
405 sym_strtab = strtab[shdr[sec_symtab].sh_link];
406 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
411 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
412 name = sym_name(sym_strtab, sym);
413 if (sym->st_shndx != SHN_ABS) {
417 /* Absolute symbols are not relocated if bzImage is
418 * loaded at a non-compiled address. Display a warning
419 * to user at compile time about the absolute
420 * relocations present.
422 * User need to audit the code to make sure
423 * some symbols which should have been section
424 * relative have not become absolute because of some
425 * linker optimization or wrong programming usage.
427 * Before warning check if this absolute symbol
428 * relocation is harmless.
430 if (is_safe_abs_reloc(name))
434 printf("WARNING: Absolute relocations"
436 printf("Offset Info Type Sym.Value "
441 printf("%08x %08x %10s %08x %s\n",
444 rel_type(ELF32_R_TYPE(rel->r_info)),
454 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
457 /* Walk through the relocations */
458 for(i = 0; i < ehdr.e_shnum; i++) {
460 Elf32_Sym *sh_symtab;
461 unsigned sec_applies, sec_symtab;
463 if (shdr[i].sh_type != SHT_REL) {
466 sec_symtab = shdr[i].sh_link;
467 sec_applies = shdr[i].sh_info;
468 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
471 sh_symtab = symtab[sec_symtab];
472 sym_strtab = strtab[shdr[sec_symtab].sh_link];
473 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
478 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
479 r_type = ELF32_R_TYPE(rel->r_info);
480 /* Don't visit relocations to absolute symbols */
481 if (sym->st_shndx == SHN_ABS) {
484 if (r_type == R_386_PC32) {
485 /* PC relative relocations don't need to be adjusted */
487 else if (r_type == R_386_32) {
488 /* Visit relocations that need to be adjusted */
492 die("Unsupported relocation type: %d\n", r_type);
498 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
503 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
505 /* Remember the address that needs to be adjusted. */
506 relocs[reloc_idx++] = rel->r_offset;
509 static int cmp_relocs(const void *va, const void *vb)
511 const unsigned long *a, *b;
513 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
516 static void emit_relocs(int as_text)
519 /* Count how many relocations I have and allocate space for them. */
521 walk_relocs(count_reloc);
522 relocs = malloc(reloc_count * sizeof(relocs[0]));
524 die("malloc of %d entries for relocs failed\n",
527 /* Collect up the relocations */
529 walk_relocs(collect_reloc);
531 /* Order the relocations for more efficient processing */
532 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
534 /* Print the relocations */
536 /* Print the relocations in a form suitable that
539 printf(".section \".data.reloc\",\"a\"\n");
540 printf(".balign 4\n");
541 for(i = 0; i < reloc_count; i++) {
542 printf("\t .long 0x%08lx\n", relocs[i]);
547 unsigned char buf[4];
548 buf[0] = buf[1] = buf[2] = buf[3] = 0;
550 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
551 /* Now print each relocation */
552 for(i = 0; i < reloc_count; i++) {
553 buf[0] = (relocs[i] >> 0) & 0xff;
554 buf[1] = (relocs[i] >> 8) & 0xff;
555 buf[2] = (relocs[i] >> 16) & 0xff;
556 buf[3] = (relocs[i] >> 24) & 0xff;
557 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
562 static void usage(void)
564 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
567 int main(int argc, char **argv)
569 int show_absolute_syms, show_absolute_relocs;
575 show_absolute_syms = 0;
576 show_absolute_relocs = 0;
579 for(i = 1; i < argc; i++) {
582 if (strcmp(argv[1], "--abs-syms") == 0) {
583 show_absolute_syms = 1;
587 if (strcmp(argv[1], "--abs-relocs") == 0) {
588 show_absolute_relocs = 1;
591 else if (strcmp(argv[1], "--text") == 0) {
605 fp = fopen(fname, "r");
607 die("Cannot open %s: %s\n",
608 fname, strerror(errno));
615 if (show_absolute_syms) {
616 print_absolute_symbols();
619 if (show_absolute_relocs) {
620 print_absolute_relocs();
623 emit_relocs(as_text);