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",
34 "xen_irq_disable_direct_reloc",
35 "xen_save_fl_direct_reloc",
38 static int is_safe_abs_reloc(const char* sym_name)
42 array_size = sizeof(safe_abs_relocs)/sizeof(char*);
44 for(i = 0; i < array_size; i++) {
45 if (!strcmp(sym_name, safe_abs_relocs[i]))
49 if (strncmp(sym_name, "__crc_", 6) == 0)
54 static void die(char *fmt, ...)
58 vfprintf(stderr, fmt, ap);
63 static const char *sym_type(unsigned type)
65 static const char *type_name[] = {
66 #define SYM_TYPE(X) [X] = #X
70 SYM_TYPE(STT_SECTION),
76 const char *name = "unknown sym type name";
77 if (type < ARRAY_SIZE(type_name)) {
78 name = type_name[type];
83 static const char *sym_bind(unsigned bind)
85 static const char *bind_name[] = {
86 #define SYM_BIND(X) [X] = #X
92 const char *name = "unknown sym bind name";
93 if (bind < ARRAY_SIZE(bind_name)) {
94 name = bind_name[bind];
99 static const char *sym_visibility(unsigned visibility)
101 static const char *visibility_name[] = {
102 #define SYM_VISIBILITY(X) [X] = #X
103 SYM_VISIBILITY(STV_DEFAULT),
104 SYM_VISIBILITY(STV_INTERNAL),
105 SYM_VISIBILITY(STV_HIDDEN),
106 SYM_VISIBILITY(STV_PROTECTED),
107 #undef SYM_VISIBILITY
109 const char *name = "unknown sym visibility name";
110 if (visibility < ARRAY_SIZE(visibility_name)) {
111 name = visibility_name[visibility];
116 static const char *rel_type(unsigned type)
118 static const char *type_name[] = {
119 #define REL_TYPE(X) [X] = #X
120 REL_TYPE(R_386_NONE),
122 REL_TYPE(R_386_PC32),
123 REL_TYPE(R_386_GOT32),
124 REL_TYPE(R_386_PLT32),
125 REL_TYPE(R_386_COPY),
126 REL_TYPE(R_386_GLOB_DAT),
127 REL_TYPE(R_386_JMP_SLOT),
128 REL_TYPE(R_386_RELATIVE),
129 REL_TYPE(R_386_GOTOFF),
130 REL_TYPE(R_386_GOTPC),
133 const char *name = "unknown type rel type name";
134 if (type < ARRAY_SIZE(type_name)) {
135 name = type_name[type];
140 static const char *sec_name(unsigned shndx)
142 const char *sec_strtab;
144 sec_strtab = strtab[ehdr.e_shstrndx];
146 if (shndx < ehdr.e_shnum) {
147 name = sec_strtab + shdr[shndx].sh_name;
149 else if (shndx == SHN_ABS) {
152 else if (shndx == SHN_COMMON) {
158 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
163 name = sym_strtab + sym->st_name;
166 name = sec_name(shdr[sym->st_shndx].sh_name);
173 #if BYTE_ORDER == LITTLE_ENDIAN
174 #define le16_to_cpu(val) (val)
175 #define le32_to_cpu(val) (val)
177 #if BYTE_ORDER == BIG_ENDIAN
178 #define le16_to_cpu(val) bswap_16(val)
179 #define le32_to_cpu(val) bswap_32(val)
182 static uint16_t elf16_to_cpu(uint16_t val)
184 return le16_to_cpu(val);
187 static uint32_t elf32_to_cpu(uint32_t val)
189 return le32_to_cpu(val);
192 static void read_ehdr(FILE *fp)
194 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
195 die("Cannot read ELF header: %s\n",
198 if (memcmp(ehdr.e_ident, ELFMAG, 4) != 0) {
199 die("No ELF magic\n");
201 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
202 die("Not a 32 bit executable\n");
204 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
205 die("Not a LSB ELF executable\n");
207 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
208 die("Unknown ELF version\n");
210 /* Convert the fields to native endian */
211 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
212 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
213 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
214 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
215 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
216 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
217 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
218 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
219 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
220 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
221 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
222 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
223 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
225 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
226 die("Unsupported ELF header type\n");
228 if (ehdr.e_machine != EM_386) {
229 die("Not for x86\n");
231 if (ehdr.e_version != EV_CURRENT) {
232 die("Unknown ELF version\n");
234 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
235 die("Bad Elf header size\n");
237 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
238 die("Bad program header entry\n");
240 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
241 die("Bad section header entry\n");
243 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
244 die("String table index out of bounds\n");
248 static void read_shdrs(FILE *fp)
251 if (ehdr.e_shnum > MAX_SHDRS) {
252 die("%d section headers supported: %d\n",
253 ehdr.e_shnum, MAX_SHDRS);
255 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
256 die("Seek to %d failed: %s\n",
257 ehdr.e_shoff, strerror(errno));
259 if (fread(&shdr, sizeof(shdr[0]), ehdr.e_shnum, fp) != ehdr.e_shnum) {
260 die("Cannot read ELF section headers: %s\n",
263 for(i = 0; i < ehdr.e_shnum; i++) {
264 shdr[i].sh_name = elf32_to_cpu(shdr[i].sh_name);
265 shdr[i].sh_type = elf32_to_cpu(shdr[i].sh_type);
266 shdr[i].sh_flags = elf32_to_cpu(shdr[i].sh_flags);
267 shdr[i].sh_addr = elf32_to_cpu(shdr[i].sh_addr);
268 shdr[i].sh_offset = elf32_to_cpu(shdr[i].sh_offset);
269 shdr[i].sh_size = elf32_to_cpu(shdr[i].sh_size);
270 shdr[i].sh_link = elf32_to_cpu(shdr[i].sh_link);
271 shdr[i].sh_info = elf32_to_cpu(shdr[i].sh_info);
272 shdr[i].sh_addralign = elf32_to_cpu(shdr[i].sh_addralign);
273 shdr[i].sh_entsize = elf32_to_cpu(shdr[i].sh_entsize);
278 static void read_strtabs(FILE *fp)
281 for(i = 0; i < ehdr.e_shnum; i++) {
282 if (shdr[i].sh_type != SHT_STRTAB) {
285 strtab[i] = malloc(shdr[i].sh_size);
287 die("malloc of %d bytes for strtab failed\n",
290 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
291 die("Seek to %d failed: %s\n",
292 shdr[i].sh_offset, strerror(errno));
294 if (fread(strtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
295 die("Cannot read symbol table: %s\n",
301 static void read_symtabs(FILE *fp)
304 for(i = 0; i < ehdr.e_shnum; i++) {
305 if (shdr[i].sh_type != SHT_SYMTAB) {
308 symtab[i] = malloc(shdr[i].sh_size);
310 die("malloc of %d bytes for symtab failed\n",
313 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
314 die("Seek to %d failed: %s\n",
315 shdr[i].sh_offset, strerror(errno));
317 if (fread(symtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
318 die("Cannot read symbol table: %s\n",
321 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[i][0]); j++) {
322 symtab[i][j].st_name = elf32_to_cpu(symtab[i][j].st_name);
323 symtab[i][j].st_value = elf32_to_cpu(symtab[i][j].st_value);
324 symtab[i][j].st_size = elf32_to_cpu(symtab[i][j].st_size);
325 symtab[i][j].st_shndx = elf16_to_cpu(symtab[i][j].st_shndx);
331 static void read_relocs(FILE *fp)
334 for(i = 0; i < ehdr.e_shnum; i++) {
335 if (shdr[i].sh_type != SHT_REL) {
338 reltab[i] = malloc(shdr[i].sh_size);
340 die("malloc of %d bytes for relocs failed\n",
343 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
344 die("Seek to %d failed: %s\n",
345 shdr[i].sh_offset, strerror(errno));
347 if (fread(reltab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
348 die("Cannot read symbol table: %s\n",
351 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
352 reltab[i][j].r_offset = elf32_to_cpu(reltab[i][j].r_offset);
353 reltab[i][j].r_info = elf32_to_cpu(reltab[i][j].r_info);
359 static void print_absolute_symbols(void)
362 printf("Absolute symbols\n");
363 printf(" Num: Value Size Type Bind Visibility Name\n");
364 for(i = 0; i < ehdr.e_shnum; i++) {
366 Elf32_Sym *sh_symtab;
368 if (shdr[i].sh_type != SHT_SYMTAB) {
371 sh_symtab = symtab[i];
372 sym_strtab = strtab[shdr[i].sh_link];
373 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[0][0]); j++) {
377 name = sym_name(sym_strtab, sym);
378 if (sym->st_shndx != SHN_ABS) {
381 printf("%5d %08x %5d %10s %10s %12s %s\n",
382 j, sym->st_value, sym->st_size,
383 sym_type(ELF32_ST_TYPE(sym->st_info)),
384 sym_bind(ELF32_ST_BIND(sym->st_info)),
385 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
392 static void print_absolute_relocs(void)
396 for(i = 0; i < ehdr.e_shnum; i++) {
398 Elf32_Sym *sh_symtab;
399 unsigned sec_applies, sec_symtab;
401 if (shdr[i].sh_type != SHT_REL) {
404 sec_symtab = shdr[i].sh_link;
405 sec_applies = shdr[i].sh_info;
406 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
409 sh_symtab = symtab[sec_symtab];
410 sym_strtab = strtab[shdr[sec_symtab].sh_link];
411 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
416 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
417 name = sym_name(sym_strtab, sym);
418 if (sym->st_shndx != SHN_ABS) {
422 /* Absolute symbols are not relocated if bzImage is
423 * loaded at a non-compiled address. Display a warning
424 * to user at compile time about the absolute
425 * relocations present.
427 * User need to audit the code to make sure
428 * some symbols which should have been section
429 * relative have not become absolute because of some
430 * linker optimization or wrong programming usage.
432 * Before warning check if this absolute symbol
433 * relocation is harmless.
435 if (is_safe_abs_reloc(name))
439 printf("WARNING: Absolute relocations"
441 printf("Offset Info Type Sym.Value "
446 printf("%08x %08x %10s %08x %s\n",
449 rel_type(ELF32_R_TYPE(rel->r_info)),
459 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
462 /* Walk through the relocations */
463 for(i = 0; i < ehdr.e_shnum; i++) {
465 Elf32_Sym *sh_symtab;
466 unsigned sec_applies, sec_symtab;
468 if (shdr[i].sh_type != SHT_REL) {
471 sec_symtab = shdr[i].sh_link;
472 sec_applies = shdr[i].sh_info;
473 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
476 sh_symtab = symtab[sec_symtab];
477 sym_strtab = strtab[shdr[sec_symtab].sh_link];
478 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
483 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
484 r_type = ELF32_R_TYPE(rel->r_info);
485 /* Don't visit relocations to absolute symbols */
486 if (sym->st_shndx == SHN_ABS) {
489 if (r_type == R_386_PC32) {
490 /* PC relative relocations don't need to be adjusted */
492 else if (r_type == R_386_32) {
493 /* Visit relocations that need to be adjusted */
497 die("Unsupported relocation type: %d\n", r_type);
503 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
508 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
510 /* Remember the address that needs to be adjusted. */
511 relocs[reloc_idx++] = rel->r_offset;
514 static int cmp_relocs(const void *va, const void *vb)
516 const unsigned long *a, *b;
518 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
521 static void emit_relocs(int as_text)
524 /* Count how many relocations I have and allocate space for them. */
526 walk_relocs(count_reloc);
527 relocs = malloc(reloc_count * sizeof(relocs[0]));
529 die("malloc of %d entries for relocs failed\n",
532 /* Collect up the relocations */
534 walk_relocs(collect_reloc);
536 /* Order the relocations for more efficient processing */
537 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
539 /* Print the relocations */
541 /* Print the relocations in a form suitable that
544 printf(".section \".data.reloc\",\"a\"\n");
545 printf(".balign 4\n");
546 for(i = 0; i < reloc_count; i++) {
547 printf("\t .long 0x%08lx\n", relocs[i]);
552 unsigned char buf[4];
553 buf[0] = buf[1] = buf[2] = buf[3] = 0;
555 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
556 /* Now print each relocation */
557 for(i = 0; i < reloc_count; i++) {
558 buf[0] = (relocs[i] >> 0) & 0xff;
559 buf[1] = (relocs[i] >> 8) & 0xff;
560 buf[2] = (relocs[i] >> 16) & 0xff;
561 buf[3] = (relocs[i] >> 24) & 0xff;
562 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
567 static void usage(void)
569 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
572 int main(int argc, char **argv)
574 int show_absolute_syms, show_absolute_relocs;
580 show_absolute_syms = 0;
581 show_absolute_relocs = 0;
584 for(i = 1; i < argc; i++) {
587 if (strcmp(argv[1], "--abs-syms") == 0) {
588 show_absolute_syms = 1;
592 if (strcmp(argv[1], "--abs-relocs") == 0) {
593 show_absolute_relocs = 1;
596 else if (strcmp(argv[1], "--text") == 0) {
610 fp = fopen(fname, "r");
612 die("Cannot open %s: %s\n",
613 fname, strerror(errno));
620 if (show_absolute_syms) {
621 print_absolute_symbols();
624 if (show_absolute_relocs) {
625 print_absolute_relocs();
628 emit_relocs(as_text);