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",
36 static int is_safe_abs_reloc(const char* sym_name
)
40 array_size
= sizeof(safe_abs_relocs
)/sizeof(char*);
42 for(i
= 0; i
< array_size
; i
++) {
43 if (!strcmp(sym_name
, safe_abs_relocs
[i
]))
47 if (strncmp(sym_name
, "__crc_", 6) == 0)
52 static void die(char *fmt
, ...)
56 vfprintf(stderr
, fmt
, ap
);
61 static const char *sym_type(unsigned type
)
63 static const char *type_name
[] = {
64 #define SYM_TYPE(X) [X] = #X
68 SYM_TYPE(STT_SECTION
),
74 const char *name
= "unknown sym type name";
75 if (type
< ARRAY_SIZE(type_name
)) {
76 name
= type_name
[type
];
81 static const char *sym_bind(unsigned bind
)
83 static const char *bind_name
[] = {
84 #define SYM_BIND(X) [X] = #X
90 const char *name
= "unknown sym bind name";
91 if (bind
< ARRAY_SIZE(bind_name
)) {
92 name
= bind_name
[bind
];
97 static const char *sym_visibility(unsigned visibility
)
99 static const char *visibility_name
[] = {
100 #define SYM_VISIBILITY(X) [X] = #X
101 SYM_VISIBILITY(STV_DEFAULT
),
102 SYM_VISIBILITY(STV_INTERNAL
),
103 SYM_VISIBILITY(STV_HIDDEN
),
104 SYM_VISIBILITY(STV_PROTECTED
),
105 #undef SYM_VISIBILITY
107 const char *name
= "unknown sym visibility name";
108 if (visibility
< ARRAY_SIZE(visibility_name
)) {
109 name
= visibility_name
[visibility
];
114 static const char *rel_type(unsigned type
)
116 static const char *type_name
[] = {
117 #define REL_TYPE(X) [X] = #X
118 REL_TYPE(R_386_NONE
),
120 REL_TYPE(R_386_PC32
),
121 REL_TYPE(R_386_GOT32
),
122 REL_TYPE(R_386_PLT32
),
123 REL_TYPE(R_386_COPY
),
124 REL_TYPE(R_386_GLOB_DAT
),
125 REL_TYPE(R_386_JMP_SLOT
),
126 REL_TYPE(R_386_RELATIVE
),
127 REL_TYPE(R_386_GOTOFF
),
128 REL_TYPE(R_386_GOTPC
),
131 const char *name
= "unknown type rel type name";
132 if (type
< ARRAY_SIZE(type_name
)) {
133 name
= type_name
[type
];
138 static const char *sec_name(unsigned shndx
)
140 const char *sec_strtab
;
142 sec_strtab
= strtab
[ehdr
.e_shstrndx
];
144 if (shndx
< ehdr
.e_shnum
) {
145 name
= sec_strtab
+ shdr
[shndx
].sh_name
;
147 else if (shndx
== SHN_ABS
) {
150 else if (shndx
== SHN_COMMON
) {
156 static const char *sym_name(const char *sym_strtab
, Elf32_Sym
*sym
)
161 name
= sym_strtab
+ sym
->st_name
;
164 name
= sec_name(shdr
[sym
->st_shndx
].sh_name
);
171 #if BYTE_ORDER == LITTLE_ENDIAN
172 #define le16_to_cpu(val) (val)
173 #define le32_to_cpu(val) (val)
175 #if BYTE_ORDER == BIG_ENDIAN
176 #define le16_to_cpu(val) bswap_16(val)
177 #define le32_to_cpu(val) bswap_32(val)
180 static uint16_t elf16_to_cpu(uint16_t val
)
182 return le16_to_cpu(val
);
185 static uint32_t elf32_to_cpu(uint32_t val
)
187 return le32_to_cpu(val
);
190 static void read_ehdr(FILE *fp
)
192 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1) {
193 die("Cannot read ELF header: %s\n",
196 if (memcmp(ehdr
.e_ident
, ELFMAG
, 4) != 0) {
197 die("No ELF magic\n");
199 if (ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) {
200 die("Not a 32 bit executable\n");
202 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
) {
203 die("Not a LSB ELF executable\n");
205 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
206 die("Unknown ELF version\n");
208 /* Convert the fields to native endian */
209 ehdr
.e_type
= elf16_to_cpu(ehdr
.e_type
);
210 ehdr
.e_machine
= elf16_to_cpu(ehdr
.e_machine
);
211 ehdr
.e_version
= elf32_to_cpu(ehdr
.e_version
);
212 ehdr
.e_entry
= elf32_to_cpu(ehdr
.e_entry
);
213 ehdr
.e_phoff
= elf32_to_cpu(ehdr
.e_phoff
);
214 ehdr
.e_shoff
= elf32_to_cpu(ehdr
.e_shoff
);
215 ehdr
.e_flags
= elf32_to_cpu(ehdr
.e_flags
);
216 ehdr
.e_ehsize
= elf16_to_cpu(ehdr
.e_ehsize
);
217 ehdr
.e_phentsize
= elf16_to_cpu(ehdr
.e_phentsize
);
218 ehdr
.e_phnum
= elf16_to_cpu(ehdr
.e_phnum
);
219 ehdr
.e_shentsize
= elf16_to_cpu(ehdr
.e_shentsize
);
220 ehdr
.e_shnum
= elf16_to_cpu(ehdr
.e_shnum
);
221 ehdr
.e_shstrndx
= elf16_to_cpu(ehdr
.e_shstrndx
);
223 if ((ehdr
.e_type
!= ET_EXEC
) && (ehdr
.e_type
!= ET_DYN
)) {
224 die("Unsupported ELF header type\n");
226 if (ehdr
.e_machine
!= EM_386
) {
227 die("Not for x86\n");
229 if (ehdr
.e_version
!= EV_CURRENT
) {
230 die("Unknown ELF version\n");
232 if (ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
)) {
233 die("Bad Elf header size\n");
235 if (ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
)) {
236 die("Bad program header entry\n");
238 if (ehdr
.e_shentsize
!= sizeof(Elf32_Shdr
)) {
239 die("Bad section header entry\n");
241 if (ehdr
.e_shstrndx
>= ehdr
.e_shnum
) {
242 die("String table index out of bounds\n");
246 static void read_shdrs(FILE *fp
)
249 if (ehdr
.e_shnum
> MAX_SHDRS
) {
250 die("%d section headers supported: %d\n",
251 ehdr
.e_shnum
, MAX_SHDRS
);
253 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0) {
254 die("Seek to %d failed: %s\n",
255 ehdr
.e_shoff
, strerror(errno
));
257 if (fread(&shdr
, sizeof(shdr
[0]), ehdr
.e_shnum
, fp
) != ehdr
.e_shnum
) {
258 die("Cannot read ELF section headers: %s\n",
261 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
262 shdr
[i
].sh_name
= elf32_to_cpu(shdr
[i
].sh_name
);
263 shdr
[i
].sh_type
= elf32_to_cpu(shdr
[i
].sh_type
);
264 shdr
[i
].sh_flags
= elf32_to_cpu(shdr
[i
].sh_flags
);
265 shdr
[i
].sh_addr
= elf32_to_cpu(shdr
[i
].sh_addr
);
266 shdr
[i
].sh_offset
= elf32_to_cpu(shdr
[i
].sh_offset
);
267 shdr
[i
].sh_size
= elf32_to_cpu(shdr
[i
].sh_size
);
268 shdr
[i
].sh_link
= elf32_to_cpu(shdr
[i
].sh_link
);
269 shdr
[i
].sh_info
= elf32_to_cpu(shdr
[i
].sh_info
);
270 shdr
[i
].sh_addralign
= elf32_to_cpu(shdr
[i
].sh_addralign
);
271 shdr
[i
].sh_entsize
= elf32_to_cpu(shdr
[i
].sh_entsize
);
276 static void read_strtabs(FILE *fp
)
279 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
280 if (shdr
[i
].sh_type
!= SHT_STRTAB
) {
283 strtab
[i
] = malloc(shdr
[i
].sh_size
);
285 die("malloc of %d bytes for strtab failed\n",
288 if (fseek(fp
, shdr
[i
].sh_offset
, SEEK_SET
) < 0) {
289 die("Seek to %d failed: %s\n",
290 shdr
[i
].sh_offset
, strerror(errno
));
292 if (fread(strtab
[i
], 1, shdr
[i
].sh_size
, fp
) != shdr
[i
].sh_size
) {
293 die("Cannot read symbol table: %s\n",
299 static void read_symtabs(FILE *fp
)
302 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
303 if (shdr
[i
].sh_type
!= SHT_SYMTAB
) {
306 symtab
[i
] = malloc(shdr
[i
].sh_size
);
308 die("malloc of %d bytes for symtab failed\n",
311 if (fseek(fp
, shdr
[i
].sh_offset
, SEEK_SET
) < 0) {
312 die("Seek to %d failed: %s\n",
313 shdr
[i
].sh_offset
, strerror(errno
));
315 if (fread(symtab
[i
], 1, shdr
[i
].sh_size
, fp
) != shdr
[i
].sh_size
) {
316 die("Cannot read symbol table: %s\n",
319 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(symtab
[i
][0]); j
++) {
320 symtab
[i
][j
].st_name
= elf32_to_cpu(symtab
[i
][j
].st_name
);
321 symtab
[i
][j
].st_value
= elf32_to_cpu(symtab
[i
][j
].st_value
);
322 symtab
[i
][j
].st_size
= elf32_to_cpu(symtab
[i
][j
].st_size
);
323 symtab
[i
][j
].st_shndx
= elf16_to_cpu(symtab
[i
][j
].st_shndx
);
329 static void read_relocs(FILE *fp
)
332 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
333 if (shdr
[i
].sh_type
!= SHT_REL
) {
336 reltab
[i
] = malloc(shdr
[i
].sh_size
);
338 die("malloc of %d bytes for relocs failed\n",
341 if (fseek(fp
, shdr
[i
].sh_offset
, SEEK_SET
) < 0) {
342 die("Seek to %d failed: %s\n",
343 shdr
[i
].sh_offset
, strerror(errno
));
345 if (fread(reltab
[i
], 1, shdr
[i
].sh_size
, fp
) != shdr
[i
].sh_size
) {
346 die("Cannot read symbol table: %s\n",
349 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(reltab
[0][0]); j
++) {
350 reltab
[i
][j
].r_offset
= elf32_to_cpu(reltab
[i
][j
].r_offset
);
351 reltab
[i
][j
].r_info
= elf32_to_cpu(reltab
[i
][j
].r_info
);
357 static void print_absolute_symbols(void)
360 printf("Absolute symbols\n");
361 printf(" Num: Value Size Type Bind Visibility Name\n");
362 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
364 Elf32_Sym
*sh_symtab
;
366 if (shdr
[i
].sh_type
!= SHT_SYMTAB
) {
369 sh_symtab
= symtab
[i
];
370 sym_strtab
= strtab
[shdr
[i
].sh_link
];
371 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(symtab
[0][0]); j
++) {
375 name
= sym_name(sym_strtab
, sym
);
376 if (sym
->st_shndx
!= SHN_ABS
) {
379 printf("%5d %08x %5d %10s %10s %12s %s\n",
380 j
, sym
->st_value
, sym
->st_size
,
381 sym_type(ELF32_ST_TYPE(sym
->st_info
)),
382 sym_bind(ELF32_ST_BIND(sym
->st_info
)),
383 sym_visibility(ELF32_ST_VISIBILITY(sym
->st_other
)),
390 static void print_absolute_relocs(void)
394 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
396 Elf32_Sym
*sh_symtab
;
397 unsigned sec_applies
, sec_symtab
;
399 if (shdr
[i
].sh_type
!= SHT_REL
) {
402 sec_symtab
= shdr
[i
].sh_link
;
403 sec_applies
= shdr
[i
].sh_info
;
404 if (!(shdr
[sec_applies
].sh_flags
& SHF_ALLOC
)) {
407 sh_symtab
= symtab
[sec_symtab
];
408 sym_strtab
= strtab
[shdr
[sec_symtab
].sh_link
];
409 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(reltab
[0][0]); j
++) {
414 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
415 name
= sym_name(sym_strtab
, sym
);
416 if (sym
->st_shndx
!= SHN_ABS
) {
420 /* Absolute symbols are not relocated if bzImage is
421 * loaded at a non-compiled address. Display a warning
422 * to user at compile time about the absolute
423 * relocations present.
425 * User need to audit the code to make sure
426 * some symbols which should have been section
427 * relative have not become absolute because of some
428 * linker optimization or wrong programming usage.
430 * Before warning check if this absolute symbol
431 * relocation is harmless.
433 if (is_safe_abs_reloc(name
))
437 printf("WARNING: Absolute relocations"
439 printf("Offset Info Type Sym.Value "
444 printf("%08x %08x %10s %08x %s\n",
447 rel_type(ELF32_R_TYPE(rel
->r_info
)),
457 static void walk_relocs(void (*visit
)(Elf32_Rel
*rel
, Elf32_Sym
*sym
))
460 /* Walk through the relocations */
461 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
463 Elf32_Sym
*sh_symtab
;
464 unsigned sec_applies
, sec_symtab
;
466 if (shdr
[i
].sh_type
!= SHT_REL
) {
469 sec_symtab
= shdr
[i
].sh_link
;
470 sec_applies
= shdr
[i
].sh_info
;
471 if (!(shdr
[sec_applies
].sh_flags
& SHF_ALLOC
)) {
474 sh_symtab
= symtab
[sec_symtab
];
475 sym_strtab
= strtab
[shdr
[sec_symtab
].sh_link
];
476 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(reltab
[0][0]); j
++) {
481 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
482 r_type
= ELF32_R_TYPE(rel
->r_info
);
483 /* Don't visit relocations to absolute symbols */
484 if (sym
->st_shndx
== SHN_ABS
) {
487 if (r_type
== R_386_PC32
) {
488 /* PC relative relocations don't need to be adjusted */
490 else if (r_type
== R_386_32
) {
491 /* Visit relocations that need to be adjusted */
495 die("Unsupported relocation type: %d\n", r_type
);
501 static void count_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
506 static void collect_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
508 /* Remember the address that needs to be adjusted. */
509 relocs
[reloc_idx
++] = rel
->r_offset
;
512 static int cmp_relocs(const void *va
, const void *vb
)
514 const unsigned long *a
, *b
;
516 return (*a
== *b
)? 0 : (*a
> *b
)? 1 : -1;
519 static void emit_relocs(int as_text
)
522 /* Count how many relocations I have and allocate space for them. */
524 walk_relocs(count_reloc
);
525 relocs
= malloc(reloc_count
* sizeof(relocs
[0]));
527 die("malloc of %d entries for relocs failed\n",
530 /* Collect up the relocations */
532 walk_relocs(collect_reloc
);
534 /* Order the relocations for more efficient processing */
535 qsort(relocs
, reloc_count
, sizeof(relocs
[0]), cmp_relocs
);
537 /* Print the relocations */
539 /* Print the relocations in a form suitable that
542 printf(".section \".data.reloc\",\"a\"\n");
543 printf(".balign 4\n");
544 for(i
= 0; i
< reloc_count
; i
++) {
545 printf("\t .long 0x%08lx\n", relocs
[i
]);
550 unsigned char buf
[4];
551 buf
[0] = buf
[1] = buf
[2] = buf
[3] = 0;
553 printf("%c%c%c%c", buf
[0], buf
[1], buf
[2], buf
[3]);
554 /* Now print each relocation */
555 for(i
= 0; i
< reloc_count
; i
++) {
556 buf
[0] = (relocs
[i
] >> 0) & 0xff;
557 buf
[1] = (relocs
[i
] >> 8) & 0xff;
558 buf
[2] = (relocs
[i
] >> 16) & 0xff;
559 buf
[3] = (relocs
[i
] >> 24) & 0xff;
560 printf("%c%c%c%c", buf
[0], buf
[1], buf
[2], buf
[3]);
565 static void usage(void)
567 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
570 int main(int argc
, char **argv
)
572 int show_absolute_syms
, show_absolute_relocs
;
578 show_absolute_syms
= 0;
579 show_absolute_relocs
= 0;
582 for(i
= 1; i
< argc
; i
++) {
585 if (strcmp(argv
[1], "--abs-syms") == 0) {
586 show_absolute_syms
= 1;
590 if (strcmp(argv
[1], "--abs-relocs") == 0) {
591 show_absolute_relocs
= 1;
594 else if (strcmp(argv
[1], "--text") == 0) {
608 fp
= fopen(fname
, "r");
610 die("Cannot open %s: %s\n",
611 fname
, strerror(errno
));
618 if (show_absolute_syms
) {
619 print_absolute_symbols();
622 if (show_absolute_relocs
) {
623 print_absolute_relocs();
626 emit_relocs(as_text
);