13 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
14 static Elf32_Ehdr ehdr
;
15 static unsigned long reloc_count
, reloc_idx
;
16 static unsigned long *relocs
;
25 static struct section
*secs
;
28 * Following symbols have been audited. There values are constant and do
29 * not change if bzImage is loaded at a different physical address than
30 * the address for which it has been compiled. Don't warn user about
31 * absolute relocations present w.r.t these symbols.
33 static const char* safe_abs_relocs
[] = {
34 "xen_irq_disable_direct_reloc",
35 "xen_save_fl_direct_reloc",
38 static int is_safe_abs_reloc(const char* sym_name
)
42 for (i
= 0; i
< ARRAY_SIZE(safe_abs_relocs
); i
++) {
43 if (!strcmp(sym_name
, safe_abs_relocs
[i
]))
47 if (strncmp(sym_name
, "VDSO", 4) == 0)
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
= secs
[ehdr
.e_shstrndx
].strtab
;
146 if (shndx
< ehdr
.e_shnum
) {
147 name
= sec_strtab
+ secs
[shndx
].shdr
.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(secs
[sym
->st_shndx
].shdr
.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
, SELFMAG
) != 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
)
253 secs
= calloc(ehdr
.e_shnum
, sizeof(struct section
));
255 die("Unable to allocate %d section headers\n",
258 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0) {
259 die("Seek to %d failed: %s\n",
260 ehdr
.e_shoff
, strerror(errno
));
262 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
263 struct section
*sec
= &secs
[i
];
264 if (fread(&shdr
, sizeof shdr
, 1, fp
) != 1)
265 die("Cannot read ELF section headers %d/%d: %s\n",
266 i
, ehdr
.e_shnum
, strerror(errno
));
267 sec
->shdr
.sh_name
= elf32_to_cpu(shdr
.sh_name
);
268 sec
->shdr
.sh_type
= elf32_to_cpu(shdr
.sh_type
);
269 sec
->shdr
.sh_flags
= elf32_to_cpu(shdr
.sh_flags
);
270 sec
->shdr
.sh_addr
= elf32_to_cpu(shdr
.sh_addr
);
271 sec
->shdr
.sh_offset
= elf32_to_cpu(shdr
.sh_offset
);
272 sec
->shdr
.sh_size
= elf32_to_cpu(shdr
.sh_size
);
273 sec
->shdr
.sh_link
= elf32_to_cpu(shdr
.sh_link
);
274 sec
->shdr
.sh_info
= elf32_to_cpu(shdr
.sh_info
);
275 sec
->shdr
.sh_addralign
= elf32_to_cpu(shdr
.sh_addralign
);
276 sec
->shdr
.sh_entsize
= elf32_to_cpu(shdr
.sh_entsize
);
277 if (sec
->shdr
.sh_link
< ehdr
.e_shnum
)
278 sec
->link
= &secs
[sec
->shdr
.sh_link
];
283 static void read_strtabs(FILE *fp
)
286 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
287 struct section
*sec
= &secs
[i
];
288 if (sec
->shdr
.sh_type
!= SHT_STRTAB
) {
291 sec
->strtab
= malloc(sec
->shdr
.sh_size
);
293 die("malloc of %d bytes for strtab failed\n",
296 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
297 die("Seek to %d failed: %s\n",
298 sec
->shdr
.sh_offset
, strerror(errno
));
300 if (fread(sec
->strtab
, 1, sec
->shdr
.sh_size
, fp
)
301 != sec
->shdr
.sh_size
) {
302 die("Cannot read symbol table: %s\n",
308 static void read_symtabs(FILE *fp
)
311 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
312 struct section
*sec
= &secs
[i
];
313 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
316 sec
->symtab
= malloc(sec
->shdr
.sh_size
);
318 die("malloc of %d bytes for symtab failed\n",
321 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
322 die("Seek to %d failed: %s\n",
323 sec
->shdr
.sh_offset
, strerror(errno
));
325 if (fread(sec
->symtab
, 1, sec
->shdr
.sh_size
, fp
)
326 != sec
->shdr
.sh_size
) {
327 die("Cannot read symbol table: %s\n",
330 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
331 Elf32_Sym
*sym
= &sec
->symtab
[j
];
332 sym
->st_name
= elf32_to_cpu(sym
->st_name
);
333 sym
->st_value
= elf32_to_cpu(sym
->st_value
);
334 sym
->st_size
= elf32_to_cpu(sym
->st_size
);
335 sym
->st_shndx
= elf16_to_cpu(sym
->st_shndx
);
341 static void read_relocs(FILE *fp
)
344 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
345 struct section
*sec
= &secs
[i
];
346 if (sec
->shdr
.sh_type
!= SHT_REL
) {
349 sec
->reltab
= malloc(sec
->shdr
.sh_size
);
351 die("malloc of %d bytes for relocs failed\n",
354 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
355 die("Seek to %d failed: %s\n",
356 sec
->shdr
.sh_offset
, strerror(errno
));
358 if (fread(sec
->reltab
, 1, sec
->shdr
.sh_size
, fp
)
359 != sec
->shdr
.sh_size
) {
360 die("Cannot read symbol table: %s\n",
363 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
364 Elf32_Rel
*rel
= &sec
->reltab
[j
];
365 rel
->r_offset
= elf32_to_cpu(rel
->r_offset
);
366 rel
->r_info
= elf32_to_cpu(rel
->r_info
);
372 static void print_absolute_symbols(void)
375 printf("Absolute symbols\n");
376 printf(" Num: Value Size Type Bind Visibility Name\n");
377 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
378 struct section
*sec
= &secs
[i
];
380 Elf32_Sym
*sh_symtab
;
383 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
386 sh_symtab
= sec
->symtab
;
387 sym_strtab
= sec
->link
->strtab
;
388 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
391 sym
= &sec
->symtab
[j
];
392 name
= sym_name(sym_strtab
, sym
);
393 if (sym
->st_shndx
!= SHN_ABS
) {
396 printf("%5d %08x %5d %10s %10s %12s %s\n",
397 j
, sym
->st_value
, sym
->st_size
,
398 sym_type(ELF32_ST_TYPE(sym
->st_info
)),
399 sym_bind(ELF32_ST_BIND(sym
->st_info
)),
400 sym_visibility(ELF32_ST_VISIBILITY(sym
->st_other
)),
407 static void print_absolute_relocs(void)
411 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
412 struct section
*sec
= &secs
[i
];
413 struct section
*sec_applies
, *sec_symtab
;
415 Elf32_Sym
*sh_symtab
;
417 if (sec
->shdr
.sh_type
!= SHT_REL
) {
420 sec_symtab
= sec
->link
;
421 sec_applies
= &secs
[sec
->shdr
.sh_info
];
422 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
425 sh_symtab
= sec_symtab
->symtab
;
426 sym_strtab
= sec_symtab
->link
->strtab
;
427 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
431 rel
= &sec
->reltab
[j
];
432 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
433 name
= sym_name(sym_strtab
, sym
);
434 if (sym
->st_shndx
!= SHN_ABS
) {
438 /* Absolute symbols are not relocated if bzImage is
439 * loaded at a non-compiled address. Display a warning
440 * to user at compile time about the absolute
441 * relocations present.
443 * User need to audit the code to make sure
444 * some symbols which should have been section
445 * relative have not become absolute because of some
446 * linker optimization or wrong programming usage.
448 * Before warning check if this absolute symbol
449 * relocation is harmless.
451 if (is_safe_abs_reloc(name
))
455 printf("WARNING: Absolute relocations"
457 printf("Offset Info Type Sym.Value "
462 printf("%08x %08x %10s %08x %s\n",
465 rel_type(ELF32_R_TYPE(rel
->r_info
)),
475 static void walk_relocs(void (*visit
)(Elf32_Rel
*rel
, Elf32_Sym
*sym
))
478 /* Walk through the relocations */
479 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
481 Elf32_Sym
*sh_symtab
;
482 struct section
*sec_applies
, *sec_symtab
;
484 struct section
*sec
= &secs
[i
];
486 if (sec
->shdr
.sh_type
!= SHT_REL
) {
489 sec_symtab
= sec
->link
;
490 sec_applies
= &secs
[sec
->shdr
.sh_info
];
491 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
494 sh_symtab
= sec_symtab
->symtab
;
495 sym_strtab
= sec_symtab
->link
->strtab
;
496 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
500 rel
= &sec
->reltab
[j
];
501 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
502 r_type
= ELF32_R_TYPE(rel
->r_info
);
503 /* Don't visit relocations to absolute symbols */
504 if (sym
->st_shndx
== SHN_ABS
) {
507 if (r_type
== R_386_NONE
|| r_type
== R_386_PC32
) {
509 * NONE can be ignored and and PC relative
510 * relocations don't need to be adjusted.
513 else if (r_type
== R_386_32
) {
514 /* Visit relocations that need to be adjusted */
518 die("Unsupported relocation type: %d\n", r_type
);
524 static void count_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
529 static void collect_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
531 /* Remember the address that needs to be adjusted. */
532 relocs
[reloc_idx
++] = rel
->r_offset
;
535 static int cmp_relocs(const void *va
, const void *vb
)
537 const unsigned long *a
, *b
;
539 return (*a
== *b
)? 0 : (*a
> *b
)? 1 : -1;
542 static void emit_relocs(int as_text
)
545 /* Count how many relocations I have and allocate space for them. */
547 walk_relocs(count_reloc
);
548 relocs
= malloc(reloc_count
* sizeof(relocs
[0]));
550 die("malloc of %d entries for relocs failed\n",
553 /* Collect up the relocations */
555 walk_relocs(collect_reloc
);
557 /* Order the relocations for more efficient processing */
558 qsort(relocs
, reloc_count
, sizeof(relocs
[0]), cmp_relocs
);
560 /* Print the relocations */
562 /* Print the relocations in a form suitable that
565 printf(".section \".data.reloc\",\"a\"\n");
566 printf(".balign 4\n");
567 for (i
= 0; i
< reloc_count
; i
++) {
568 printf("\t .long 0x%08lx\n", relocs
[i
]);
573 unsigned char buf
[4];
574 buf
[0] = buf
[1] = buf
[2] = buf
[3] = 0;
576 printf("%c%c%c%c", buf
[0], buf
[1], buf
[2], buf
[3]);
577 /* Now print each relocation */
578 for (i
= 0; i
< reloc_count
; i
++) {
579 buf
[0] = (relocs
[i
] >> 0) & 0xff;
580 buf
[1] = (relocs
[i
] >> 8) & 0xff;
581 buf
[2] = (relocs
[i
] >> 16) & 0xff;
582 buf
[3] = (relocs
[i
] >> 24) & 0xff;
583 printf("%c%c%c%c", buf
[0], buf
[1], buf
[2], buf
[3]);
588 static void usage(void)
590 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
593 int main(int argc
, char **argv
)
595 int show_absolute_syms
, show_absolute_relocs
;
601 show_absolute_syms
= 0;
602 show_absolute_relocs
= 0;
605 for (i
= 1; i
< argc
; i
++) {
608 if (strcmp(argv
[1], "--abs-syms") == 0) {
609 show_absolute_syms
= 1;
613 if (strcmp(argv
[1], "--abs-relocs") == 0) {
614 show_absolute_relocs
= 1;
617 else if (strcmp(argv
[1], "--text") == 0) {
631 fp
= fopen(fname
, "r");
633 die("Cannot open %s: %s\n",
634 fname
, strerror(errno
));
641 if (show_absolute_syms
) {
642 print_absolute_symbols();
645 if (show_absolute_relocs
) {
646 print_absolute_relocs();
649 emit_relocs(as_text
);