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
[] = {
30 "xen_irq_disable_direct_reloc",
31 "xen_save_fl_direct_reloc",
34 static int is_safe_abs_reloc(const char* sym_name
)
38 for(i
= 0; i
< ARRAY_SIZE(safe_abs_relocs
); i
++) {
39 if (!strcmp(sym_name
, safe_abs_relocs
[i
]))
43 if (strncmp(sym_name
, "VDSO", 4) == 0)
45 if (strncmp(sym_name
, "__crc_", 6) == 0)
50 static void die(char *fmt
, ...)
54 vfprintf(stderr
, fmt
, ap
);
59 static const char *sym_type(unsigned type
)
61 static const char *type_name
[] = {
62 #define SYM_TYPE(X) [X] = #X
66 SYM_TYPE(STT_SECTION
),
72 const char *name
= "unknown sym type name";
73 if (type
< ARRAY_SIZE(type_name
)) {
74 name
= type_name
[type
];
79 static const char *sym_bind(unsigned bind
)
81 static const char *bind_name
[] = {
82 #define SYM_BIND(X) [X] = #X
88 const char *name
= "unknown sym bind name";
89 if (bind
< ARRAY_SIZE(bind_name
)) {
90 name
= bind_name
[bind
];
95 static const char *sym_visibility(unsigned visibility
)
97 static const char *visibility_name
[] = {
98 #define SYM_VISIBILITY(X) [X] = #X
99 SYM_VISIBILITY(STV_DEFAULT
),
100 SYM_VISIBILITY(STV_INTERNAL
),
101 SYM_VISIBILITY(STV_HIDDEN
),
102 SYM_VISIBILITY(STV_PROTECTED
),
103 #undef SYM_VISIBILITY
105 const char *name
= "unknown sym visibility name";
106 if (visibility
< ARRAY_SIZE(visibility_name
)) {
107 name
= visibility_name
[visibility
];
112 static const char *rel_type(unsigned type
)
114 static const char *type_name
[] = {
115 #define REL_TYPE(X) [X] = #X
116 REL_TYPE(R_386_NONE
),
118 REL_TYPE(R_386_PC32
),
119 REL_TYPE(R_386_GOT32
),
120 REL_TYPE(R_386_PLT32
),
121 REL_TYPE(R_386_COPY
),
122 REL_TYPE(R_386_GLOB_DAT
),
123 REL_TYPE(R_386_JMP_SLOT
),
124 REL_TYPE(R_386_RELATIVE
),
125 REL_TYPE(R_386_GOTOFF
),
126 REL_TYPE(R_386_GOTPC
),
129 const char *name
= "unknown type rel type name";
130 if (type
< ARRAY_SIZE(type_name
)) {
131 name
= type_name
[type
];
136 static const char *sec_name(unsigned shndx
)
138 const char *sec_strtab
;
140 sec_strtab
= strtab
[ehdr
.e_shstrndx
];
142 if (shndx
< ehdr
.e_shnum
) {
143 name
= sec_strtab
+ shdr
[shndx
].sh_name
;
145 else if (shndx
== SHN_ABS
) {
148 else if (shndx
== SHN_COMMON
) {
154 static const char *sym_name(const char *sym_strtab
, Elf32_Sym
*sym
)
159 name
= sym_strtab
+ sym
->st_name
;
162 name
= sec_name(shdr
[sym
->st_shndx
].sh_name
);
169 #if BYTE_ORDER == LITTLE_ENDIAN
170 #define le16_to_cpu(val) (val)
171 #define le32_to_cpu(val) (val)
173 #if BYTE_ORDER == BIG_ENDIAN
174 #define le16_to_cpu(val) bswap_16(val)
175 #define le32_to_cpu(val) bswap_32(val)
178 static uint16_t elf16_to_cpu(uint16_t val
)
180 return le16_to_cpu(val
);
183 static uint32_t elf32_to_cpu(uint32_t val
)
185 return le32_to_cpu(val
);
188 static void read_ehdr(FILE *fp
)
190 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1) {
191 die("Cannot read ELF header: %s\n",
194 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0) {
195 die("No ELF magic\n");
197 if (ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) {
198 die("Not a 32 bit executable\n");
200 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
) {
201 die("Not a LSB ELF executable\n");
203 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
204 die("Unknown ELF version\n");
206 /* Convert the fields to native endian */
207 ehdr
.e_type
= elf16_to_cpu(ehdr
.e_type
);
208 ehdr
.e_machine
= elf16_to_cpu(ehdr
.e_machine
);
209 ehdr
.e_version
= elf32_to_cpu(ehdr
.e_version
);
210 ehdr
.e_entry
= elf32_to_cpu(ehdr
.e_entry
);
211 ehdr
.e_phoff
= elf32_to_cpu(ehdr
.e_phoff
);
212 ehdr
.e_shoff
= elf32_to_cpu(ehdr
.e_shoff
);
213 ehdr
.e_flags
= elf32_to_cpu(ehdr
.e_flags
);
214 ehdr
.e_ehsize
= elf16_to_cpu(ehdr
.e_ehsize
);
215 ehdr
.e_phentsize
= elf16_to_cpu(ehdr
.e_phentsize
);
216 ehdr
.e_phnum
= elf16_to_cpu(ehdr
.e_phnum
);
217 ehdr
.e_shentsize
= elf16_to_cpu(ehdr
.e_shentsize
);
218 ehdr
.e_shnum
= elf16_to_cpu(ehdr
.e_shnum
);
219 ehdr
.e_shstrndx
= elf16_to_cpu(ehdr
.e_shstrndx
);
221 if ((ehdr
.e_type
!= ET_EXEC
) && (ehdr
.e_type
!= ET_DYN
)) {
222 die("Unsupported ELF header type\n");
224 if (ehdr
.e_machine
!= EM_386
) {
225 die("Not for x86\n");
227 if (ehdr
.e_version
!= EV_CURRENT
) {
228 die("Unknown ELF version\n");
230 if (ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
)) {
231 die("Bad Elf header size\n");
233 if (ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
)) {
234 die("Bad program header entry\n");
236 if (ehdr
.e_shentsize
!= sizeof(Elf32_Shdr
)) {
237 die("Bad section header entry\n");
239 if (ehdr
.e_shstrndx
>= ehdr
.e_shnum
) {
240 die("String table index out of bounds\n");
244 static void read_shdrs(FILE *fp
)
247 if (ehdr
.e_shnum
> MAX_SHDRS
) {
248 die("%d section headers supported: %d\n",
249 ehdr
.e_shnum
, MAX_SHDRS
);
251 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0) {
252 die("Seek to %d failed: %s\n",
253 ehdr
.e_shoff
, strerror(errno
));
255 if (fread(&shdr
, sizeof(shdr
[0]), ehdr
.e_shnum
, fp
) != ehdr
.e_shnum
) {
256 die("Cannot read ELF section headers: %s\n",
259 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
260 shdr
[i
].sh_name
= elf32_to_cpu(shdr
[i
].sh_name
);
261 shdr
[i
].sh_type
= elf32_to_cpu(shdr
[i
].sh_type
);
262 shdr
[i
].sh_flags
= elf32_to_cpu(shdr
[i
].sh_flags
);
263 shdr
[i
].sh_addr
= elf32_to_cpu(shdr
[i
].sh_addr
);
264 shdr
[i
].sh_offset
= elf32_to_cpu(shdr
[i
].sh_offset
);
265 shdr
[i
].sh_size
= elf32_to_cpu(shdr
[i
].sh_size
);
266 shdr
[i
].sh_link
= elf32_to_cpu(shdr
[i
].sh_link
);
267 shdr
[i
].sh_info
= elf32_to_cpu(shdr
[i
].sh_info
);
268 shdr
[i
].sh_addralign
= elf32_to_cpu(shdr
[i
].sh_addralign
);
269 shdr
[i
].sh_entsize
= elf32_to_cpu(shdr
[i
].sh_entsize
);
274 static void read_strtabs(FILE *fp
)
277 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
278 if (shdr
[i
].sh_type
!= SHT_STRTAB
) {
281 strtab
[i
] = malloc(shdr
[i
].sh_size
);
283 die("malloc of %d bytes for strtab failed\n",
286 if (fseek(fp
, shdr
[i
].sh_offset
, SEEK_SET
) < 0) {
287 die("Seek to %d failed: %s\n",
288 shdr
[i
].sh_offset
, strerror(errno
));
290 if (fread(strtab
[i
], 1, shdr
[i
].sh_size
, fp
) != shdr
[i
].sh_size
) {
291 die("Cannot read symbol table: %s\n",
297 static void read_symtabs(FILE *fp
)
300 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
301 if (shdr
[i
].sh_type
!= SHT_SYMTAB
) {
304 symtab
[i
] = malloc(shdr
[i
].sh_size
);
306 die("malloc of %d bytes for symtab failed\n",
309 if (fseek(fp
, shdr
[i
].sh_offset
, SEEK_SET
) < 0) {
310 die("Seek to %d failed: %s\n",
311 shdr
[i
].sh_offset
, strerror(errno
));
313 if (fread(symtab
[i
], 1, shdr
[i
].sh_size
, fp
) != shdr
[i
].sh_size
) {
314 die("Cannot read symbol table: %s\n",
317 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(symtab
[i
][0]); j
++) {
318 symtab
[i
][j
].st_name
= elf32_to_cpu(symtab
[i
][j
].st_name
);
319 symtab
[i
][j
].st_value
= elf32_to_cpu(symtab
[i
][j
].st_value
);
320 symtab
[i
][j
].st_size
= elf32_to_cpu(symtab
[i
][j
].st_size
);
321 symtab
[i
][j
].st_shndx
= elf16_to_cpu(symtab
[i
][j
].st_shndx
);
327 static void read_relocs(FILE *fp
)
330 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
331 if (shdr
[i
].sh_type
!= SHT_REL
) {
334 reltab
[i
] = malloc(shdr
[i
].sh_size
);
336 die("malloc of %d bytes for relocs failed\n",
339 if (fseek(fp
, shdr
[i
].sh_offset
, SEEK_SET
) < 0) {
340 die("Seek to %d failed: %s\n",
341 shdr
[i
].sh_offset
, strerror(errno
));
343 if (fread(reltab
[i
], 1, shdr
[i
].sh_size
, fp
) != shdr
[i
].sh_size
) {
344 die("Cannot read symbol table: %s\n",
347 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(reltab
[0][0]); j
++) {
348 reltab
[i
][j
].r_offset
= elf32_to_cpu(reltab
[i
][j
].r_offset
);
349 reltab
[i
][j
].r_info
= elf32_to_cpu(reltab
[i
][j
].r_info
);
355 static void print_absolute_symbols(void)
358 printf("Absolute symbols\n");
359 printf(" Num: Value Size Type Bind Visibility Name\n");
360 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
362 Elf32_Sym
*sh_symtab
;
364 if (shdr
[i
].sh_type
!= SHT_SYMTAB
) {
367 sh_symtab
= symtab
[i
];
368 sym_strtab
= strtab
[shdr
[i
].sh_link
];
369 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(symtab
[0][0]); j
++) {
373 name
= sym_name(sym_strtab
, sym
);
374 if (sym
->st_shndx
!= SHN_ABS
) {
377 printf("%5d %08x %5d %10s %10s %12s %s\n",
378 j
, sym
->st_value
, sym
->st_size
,
379 sym_type(ELF32_ST_TYPE(sym
->st_info
)),
380 sym_bind(ELF32_ST_BIND(sym
->st_info
)),
381 sym_visibility(ELF32_ST_VISIBILITY(sym
->st_other
)),
388 static void print_absolute_relocs(void)
392 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
394 Elf32_Sym
*sh_symtab
;
395 unsigned sec_applies
, sec_symtab
;
397 if (shdr
[i
].sh_type
!= SHT_REL
) {
400 sec_symtab
= shdr
[i
].sh_link
;
401 sec_applies
= shdr
[i
].sh_info
;
402 if (!(shdr
[sec_applies
].sh_flags
& SHF_ALLOC
)) {
405 sh_symtab
= symtab
[sec_symtab
];
406 sym_strtab
= strtab
[shdr
[sec_symtab
].sh_link
];
407 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(reltab
[0][0]); j
++) {
412 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
413 name
= sym_name(sym_strtab
, sym
);
414 if (sym
->st_shndx
!= SHN_ABS
) {
418 /* Absolute symbols are not relocated if bzImage is
419 * loaded at a non-compiled address. Display a warning
420 * to user at compile time about the absolute
421 * relocations present.
423 * User need to audit the code to make sure
424 * some symbols which should have been section
425 * relative have not become absolute because of some
426 * linker optimization or wrong programming usage.
428 * Before warning check if this absolute symbol
429 * relocation is harmless.
431 if (is_safe_abs_reloc(name
))
435 printf("WARNING: Absolute relocations"
437 printf("Offset Info Type Sym.Value "
442 printf("%08x %08x %10s %08x %s\n",
445 rel_type(ELF32_R_TYPE(rel
->r_info
)),
455 static void walk_relocs(void (*visit
)(Elf32_Rel
*rel
, Elf32_Sym
*sym
))
458 /* Walk through the relocations */
459 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
461 Elf32_Sym
*sh_symtab
;
462 unsigned sec_applies
, sec_symtab
;
464 if (shdr
[i
].sh_type
!= SHT_REL
) {
467 sec_symtab
= shdr
[i
].sh_link
;
468 sec_applies
= shdr
[i
].sh_info
;
469 if (!(shdr
[sec_applies
].sh_flags
& SHF_ALLOC
)) {
472 sh_symtab
= symtab
[sec_symtab
];
473 sym_strtab
= strtab
[shdr
[sec_symtab
].sh_link
];
474 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(reltab
[0][0]); j
++) {
479 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
480 r_type
= ELF32_R_TYPE(rel
->r_info
);
481 /* Don't visit relocations to absolute symbols */
482 if (sym
->st_shndx
== SHN_ABS
) {
485 if (r_type
== R_386_PC32
) {
486 /* PC relative relocations don't need to be adjusted */
488 else if (r_type
== R_386_32
) {
489 /* Visit relocations that need to be adjusted */
493 die("Unsupported relocation type: %d\n", r_type
);
499 static void count_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
504 static void collect_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
506 /* Remember the address that needs to be adjusted. */
507 relocs
[reloc_idx
++] = rel
->r_offset
;
510 static int cmp_relocs(const void *va
, const void *vb
)
512 const unsigned long *a
, *b
;
514 return (*a
== *b
)? 0 : (*a
> *b
)? 1 : -1;
517 static void emit_relocs(int as_text
)
520 /* Count how many relocations I have and allocate space for them. */
522 walk_relocs(count_reloc
);
523 relocs
= malloc(reloc_count
* sizeof(relocs
[0]));
525 die("malloc of %d entries for relocs failed\n",
528 /* Collect up the relocations */
530 walk_relocs(collect_reloc
);
532 /* Order the relocations for more efficient processing */
533 qsort(relocs
, reloc_count
, sizeof(relocs
[0]), cmp_relocs
);
535 /* Print the relocations */
537 /* Print the relocations in a form suitable that
540 printf(".section \".data.reloc\",\"a\"\n");
541 printf(".balign 4\n");
542 for(i
= 0; i
< reloc_count
; i
++) {
543 printf("\t .long 0x%08lx\n", relocs
[i
]);
548 unsigned char buf
[4];
549 buf
[0] = buf
[1] = buf
[2] = buf
[3] = 0;
551 printf("%c%c%c%c", buf
[0], buf
[1], buf
[2], buf
[3]);
552 /* Now print each relocation */
553 for(i
= 0; i
< reloc_count
; i
++) {
554 buf
[0] = (relocs
[i
] >> 0) & 0xff;
555 buf
[1] = (relocs
[i
] >> 8) & 0xff;
556 buf
[2] = (relocs
[i
] >> 16) & 0xff;
557 buf
[3] = (relocs
[i
] >> 24) & 0xff;
558 printf("%c%c%c%c", buf
[0], buf
[1], buf
[2], buf
[3]);
563 static void usage(void)
565 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
568 int main(int argc
, char **argv
)
570 int show_absolute_syms
, show_absolute_relocs
;
576 show_absolute_syms
= 0;
577 show_absolute_relocs
= 0;
580 for(i
= 1; i
< argc
; i
++) {
583 if (strcmp(argv
[1], "--abs-syms") == 0) {
584 show_absolute_syms
= 1;
588 if (strcmp(argv
[1], "--abs-relocs") == 0) {
589 show_absolute_relocs
= 1;
592 else if (strcmp(argv
[1], "--text") == 0) {
606 fp
= fopen(fname
, "r");
608 die("Cannot open %s: %s\n",
609 fname
, strerror(errno
));
616 if (show_absolute_syms
) {
617 print_absolute_symbols();
620 if (show_absolute_relocs
) {
621 print_absolute_relocs();
624 emit_relocs(as_text
);