13 #include <tools/le_byteshift.h>
15 static void die(char *fmt
, ...);
17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
18 static Elf32_Ehdr ehdr
;
19 static unsigned long reloc_count
, reloc_idx
;
20 static unsigned long *relocs
;
29 static struct section
*secs
;
32 * Following symbols have been audited. There values are constant and do
33 * not change if bzImage is loaded at a different physical address than
34 * the address for which it has been compiled. Don't warn user about
35 * absolute relocations present w.r.t these symbols.
37 static const char abs_sym_regex
[] =
38 "^(xen_irq_disable_direct_reloc$|"
39 "xen_save_fl_direct_reloc$|"
42 static regex_t abs_sym_regex_c
;
43 static int is_abs_reloc(const char *sym_name
)
45 return !regexec(&abs_sym_regex_c
, sym_name
, 0, NULL
, 0);
49 * These symbols are known to be relative, even if the linker marks them
50 * as absolute (typically defined outside any section in the linker script.)
52 static const char rel_sym_regex
[] =
54 static regex_t rel_sym_regex_c
;
55 static int is_rel_reloc(const char *sym_name
)
57 return !regexec(&rel_sym_regex_c
, sym_name
, 0, NULL
, 0);
60 static void regex_init(void)
65 err
= regcomp(&abs_sym_regex_c
, abs_sym_regex
,
66 REG_EXTENDED
|REG_NOSUB
);
68 regerror(err
, &abs_sym_regex_c
, errbuf
, sizeof errbuf
);
72 err
= regcomp(&rel_sym_regex_c
, rel_sym_regex
,
73 REG_EXTENDED
|REG_NOSUB
);
75 regerror(err
, &rel_sym_regex_c
, errbuf
, sizeof errbuf
);
80 static void die(char *fmt
, ...)
84 vfprintf(stderr
, fmt
, ap
);
89 static const char *sym_type(unsigned type
)
91 static const char *type_name
[] = {
92 #define SYM_TYPE(X) [X] = #X
96 SYM_TYPE(STT_SECTION
),
102 const char *name
= "unknown sym type name";
103 if (type
< ARRAY_SIZE(type_name
)) {
104 name
= type_name
[type
];
109 static const char *sym_bind(unsigned bind
)
111 static const char *bind_name
[] = {
112 #define SYM_BIND(X) [X] = #X
114 SYM_BIND(STB_GLOBAL
),
118 const char *name
= "unknown sym bind name";
119 if (bind
< ARRAY_SIZE(bind_name
)) {
120 name
= bind_name
[bind
];
125 static const char *sym_visibility(unsigned visibility
)
127 static const char *visibility_name
[] = {
128 #define SYM_VISIBILITY(X) [X] = #X
129 SYM_VISIBILITY(STV_DEFAULT
),
130 SYM_VISIBILITY(STV_INTERNAL
),
131 SYM_VISIBILITY(STV_HIDDEN
),
132 SYM_VISIBILITY(STV_PROTECTED
),
133 #undef SYM_VISIBILITY
135 const char *name
= "unknown sym visibility name";
136 if (visibility
< ARRAY_SIZE(visibility_name
)) {
137 name
= visibility_name
[visibility
];
142 static const char *rel_type(unsigned type
)
144 static const char *type_name
[] = {
145 #define REL_TYPE(X) [X] = #X
146 REL_TYPE(R_386_NONE
),
148 REL_TYPE(R_386_PC32
),
149 REL_TYPE(R_386_GOT32
),
150 REL_TYPE(R_386_PLT32
),
151 REL_TYPE(R_386_COPY
),
152 REL_TYPE(R_386_GLOB_DAT
),
153 REL_TYPE(R_386_JMP_SLOT
),
154 REL_TYPE(R_386_RELATIVE
),
155 REL_TYPE(R_386_GOTOFF
),
156 REL_TYPE(R_386_GOTPC
),
159 const char *name
= "unknown type rel type name";
160 if (type
< ARRAY_SIZE(type_name
) && type_name
[type
]) {
161 name
= type_name
[type
];
166 static const char *sec_name(unsigned shndx
)
168 const char *sec_strtab
;
170 sec_strtab
= secs
[ehdr
.e_shstrndx
].strtab
;
172 if (shndx
< ehdr
.e_shnum
) {
173 name
= sec_strtab
+ secs
[shndx
].shdr
.sh_name
;
175 else if (shndx
== SHN_ABS
) {
178 else if (shndx
== SHN_COMMON
) {
184 static const char *sym_name(const char *sym_strtab
, Elf32_Sym
*sym
)
189 name
= sym_strtab
+ sym
->st_name
;
192 name
= sec_name(secs
[sym
->st_shndx
].shdr
.sh_name
);
199 #if BYTE_ORDER == LITTLE_ENDIAN
200 #define le16_to_cpu(val) (val)
201 #define le32_to_cpu(val) (val)
203 #if BYTE_ORDER == BIG_ENDIAN
204 #define le16_to_cpu(val) bswap_16(val)
205 #define le32_to_cpu(val) bswap_32(val)
208 static uint16_t elf16_to_cpu(uint16_t val
)
210 return le16_to_cpu(val
);
213 static uint32_t elf32_to_cpu(uint32_t val
)
215 return le32_to_cpu(val
);
218 static void read_ehdr(FILE *fp
)
220 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1) {
221 die("Cannot read ELF header: %s\n",
224 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0) {
225 die("No ELF magic\n");
227 if (ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) {
228 die("Not a 32 bit executable\n");
230 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
) {
231 die("Not a LSB ELF executable\n");
233 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
234 die("Unknown ELF version\n");
236 /* Convert the fields to native endian */
237 ehdr
.e_type
= elf16_to_cpu(ehdr
.e_type
);
238 ehdr
.e_machine
= elf16_to_cpu(ehdr
.e_machine
);
239 ehdr
.e_version
= elf32_to_cpu(ehdr
.e_version
);
240 ehdr
.e_entry
= elf32_to_cpu(ehdr
.e_entry
);
241 ehdr
.e_phoff
= elf32_to_cpu(ehdr
.e_phoff
);
242 ehdr
.e_shoff
= elf32_to_cpu(ehdr
.e_shoff
);
243 ehdr
.e_flags
= elf32_to_cpu(ehdr
.e_flags
);
244 ehdr
.e_ehsize
= elf16_to_cpu(ehdr
.e_ehsize
);
245 ehdr
.e_phentsize
= elf16_to_cpu(ehdr
.e_phentsize
);
246 ehdr
.e_phnum
= elf16_to_cpu(ehdr
.e_phnum
);
247 ehdr
.e_shentsize
= elf16_to_cpu(ehdr
.e_shentsize
);
248 ehdr
.e_shnum
= elf16_to_cpu(ehdr
.e_shnum
);
249 ehdr
.e_shstrndx
= elf16_to_cpu(ehdr
.e_shstrndx
);
251 if ((ehdr
.e_type
!= ET_EXEC
) && (ehdr
.e_type
!= ET_DYN
)) {
252 die("Unsupported ELF header type\n");
254 if (ehdr
.e_machine
!= EM_386
) {
255 die("Not for x86\n");
257 if (ehdr
.e_version
!= EV_CURRENT
) {
258 die("Unknown ELF version\n");
260 if (ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
)) {
261 die("Bad Elf header size\n");
263 if (ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
)) {
264 die("Bad program header entry\n");
266 if (ehdr
.e_shentsize
!= sizeof(Elf32_Shdr
)) {
267 die("Bad section header entry\n");
269 if (ehdr
.e_shstrndx
>= ehdr
.e_shnum
) {
270 die("String table index out of bounds\n");
274 static void read_shdrs(FILE *fp
)
279 secs
= calloc(ehdr
.e_shnum
, sizeof(struct section
));
281 die("Unable to allocate %d section headers\n",
284 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0) {
285 die("Seek to %d failed: %s\n",
286 ehdr
.e_shoff
, strerror(errno
));
288 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
289 struct section
*sec
= &secs
[i
];
290 if (fread(&shdr
, sizeof shdr
, 1, fp
) != 1)
291 die("Cannot read ELF section headers %d/%d: %s\n",
292 i
, ehdr
.e_shnum
, strerror(errno
));
293 sec
->shdr
.sh_name
= elf32_to_cpu(shdr
.sh_name
);
294 sec
->shdr
.sh_type
= elf32_to_cpu(shdr
.sh_type
);
295 sec
->shdr
.sh_flags
= elf32_to_cpu(shdr
.sh_flags
);
296 sec
->shdr
.sh_addr
= elf32_to_cpu(shdr
.sh_addr
);
297 sec
->shdr
.sh_offset
= elf32_to_cpu(shdr
.sh_offset
);
298 sec
->shdr
.sh_size
= elf32_to_cpu(shdr
.sh_size
);
299 sec
->shdr
.sh_link
= elf32_to_cpu(shdr
.sh_link
);
300 sec
->shdr
.sh_info
= elf32_to_cpu(shdr
.sh_info
);
301 sec
->shdr
.sh_addralign
= elf32_to_cpu(shdr
.sh_addralign
);
302 sec
->shdr
.sh_entsize
= elf32_to_cpu(shdr
.sh_entsize
);
303 if (sec
->shdr
.sh_link
< ehdr
.e_shnum
)
304 sec
->link
= &secs
[sec
->shdr
.sh_link
];
309 static void read_strtabs(FILE *fp
)
312 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
313 struct section
*sec
= &secs
[i
];
314 if (sec
->shdr
.sh_type
!= SHT_STRTAB
) {
317 sec
->strtab
= malloc(sec
->shdr
.sh_size
);
319 die("malloc of %d bytes for strtab failed\n",
322 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
323 die("Seek to %d failed: %s\n",
324 sec
->shdr
.sh_offset
, strerror(errno
));
326 if (fread(sec
->strtab
, 1, sec
->shdr
.sh_size
, fp
)
327 != sec
->shdr
.sh_size
) {
328 die("Cannot read symbol table: %s\n",
334 static void read_symtabs(FILE *fp
)
337 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
338 struct section
*sec
= &secs
[i
];
339 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
342 sec
->symtab
= malloc(sec
->shdr
.sh_size
);
344 die("malloc of %d bytes for symtab failed\n",
347 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
348 die("Seek to %d failed: %s\n",
349 sec
->shdr
.sh_offset
, strerror(errno
));
351 if (fread(sec
->symtab
, 1, sec
->shdr
.sh_size
, fp
)
352 != sec
->shdr
.sh_size
) {
353 die("Cannot read symbol table: %s\n",
356 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
357 Elf32_Sym
*sym
= &sec
->symtab
[j
];
358 sym
->st_name
= elf32_to_cpu(sym
->st_name
);
359 sym
->st_value
= elf32_to_cpu(sym
->st_value
);
360 sym
->st_size
= elf32_to_cpu(sym
->st_size
);
361 sym
->st_shndx
= elf16_to_cpu(sym
->st_shndx
);
367 static void read_relocs(FILE *fp
)
370 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
371 struct section
*sec
= &secs
[i
];
372 if (sec
->shdr
.sh_type
!= SHT_REL
) {
375 sec
->reltab
= malloc(sec
->shdr
.sh_size
);
377 die("malloc of %d bytes for relocs failed\n",
380 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
381 die("Seek to %d failed: %s\n",
382 sec
->shdr
.sh_offset
, strerror(errno
));
384 if (fread(sec
->reltab
, 1, sec
->shdr
.sh_size
, fp
)
385 != sec
->shdr
.sh_size
) {
386 die("Cannot read symbol table: %s\n",
389 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
390 Elf32_Rel
*rel
= &sec
->reltab
[j
];
391 rel
->r_offset
= elf32_to_cpu(rel
->r_offset
);
392 rel
->r_info
= elf32_to_cpu(rel
->r_info
);
398 static void print_absolute_symbols(void)
401 printf("Absolute symbols\n");
402 printf(" Num: Value Size Type Bind Visibility Name\n");
403 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
404 struct section
*sec
= &secs
[i
];
408 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
411 sym_strtab
= sec
->link
->strtab
;
412 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
415 sym
= &sec
->symtab
[j
];
416 name
= sym_name(sym_strtab
, sym
);
417 if (sym
->st_shndx
!= SHN_ABS
) {
420 printf("%5d %08x %5d %10s %10s %12s %s\n",
421 j
, sym
->st_value
, sym
->st_size
,
422 sym_type(ELF32_ST_TYPE(sym
->st_info
)),
423 sym_bind(ELF32_ST_BIND(sym
->st_info
)),
424 sym_visibility(ELF32_ST_VISIBILITY(sym
->st_other
)),
431 static void print_absolute_relocs(void)
435 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
436 struct section
*sec
= &secs
[i
];
437 struct section
*sec_applies
, *sec_symtab
;
439 Elf32_Sym
*sh_symtab
;
441 if (sec
->shdr
.sh_type
!= SHT_REL
) {
444 sec_symtab
= sec
->link
;
445 sec_applies
= &secs
[sec
->shdr
.sh_info
];
446 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
449 sh_symtab
= sec_symtab
->symtab
;
450 sym_strtab
= sec_symtab
->link
->strtab
;
451 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
455 rel
= &sec
->reltab
[j
];
456 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
457 name
= sym_name(sym_strtab
, sym
);
458 if (sym
->st_shndx
!= SHN_ABS
) {
462 /* Absolute symbols are not relocated if bzImage is
463 * loaded at a non-compiled address. Display a warning
464 * to user at compile time about the absolute
465 * relocations present.
467 * User need to audit the code to make sure
468 * some symbols which should have been section
469 * relative have not become absolute because of some
470 * linker optimization or wrong programming usage.
472 * Before warning check if this absolute symbol
473 * relocation is harmless.
475 if (is_abs_reloc(name
) || is_rel_reloc(name
))
479 printf("WARNING: Absolute relocations"
481 printf("Offset Info Type Sym.Value "
486 printf("%08x %08x %10s %08x %s\n",
489 rel_type(ELF32_R_TYPE(rel
->r_info
)),
499 static void walk_relocs(void (*visit
)(Elf32_Rel
*rel
, Elf32_Sym
*sym
))
502 /* Walk through the relocations */
503 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
505 Elf32_Sym
*sh_symtab
;
506 struct section
*sec_applies
, *sec_symtab
;
508 struct section
*sec
= &secs
[i
];
510 if (sec
->shdr
.sh_type
!= SHT_REL
) {
513 sec_symtab
= sec
->link
;
514 sec_applies
= &secs
[sec
->shdr
.sh_info
];
515 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
518 sh_symtab
= sec_symtab
->symtab
;
519 sym_strtab
= sec_symtab
->link
->strtab
;
520 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
524 rel
= &sec
->reltab
[j
];
525 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
526 r_type
= ELF32_R_TYPE(rel
->r_info
);
527 /* Don't visit relocations to absolute symbols */
528 if (sym
->st_shndx
== SHN_ABS
&&
529 !is_rel_reloc(sym_name(sym_strtab
, sym
))) {
536 * NONE can be ignored and and PC relative
537 * relocations don't need to be adjusted.
541 /* Visit relocations that need to be adjusted */
545 die("Unsupported relocation type: %s (%d)\n",
546 rel_type(r_type
), r_type
);
553 static void count_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
558 static void collect_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
560 /* Remember the address that needs to be adjusted. */
561 relocs
[reloc_idx
++] = rel
->r_offset
;
564 static int cmp_relocs(const void *va
, const void *vb
)
566 const unsigned long *a
, *b
;
568 return (*a
== *b
)? 0 : (*a
> *b
)? 1 : -1;
571 static void emit_relocs(int as_text
)
574 /* Count how many relocations I have and allocate space for them. */
576 walk_relocs(count_reloc
);
577 relocs
= malloc(reloc_count
* sizeof(relocs
[0]));
579 die("malloc of %d entries for relocs failed\n",
582 /* Collect up the relocations */
584 walk_relocs(collect_reloc
);
586 /* Order the relocations for more efficient processing */
587 qsort(relocs
, reloc_count
, sizeof(relocs
[0]), cmp_relocs
);
589 /* Print the relocations */
591 /* Print the relocations in a form suitable that
594 printf(".section \".data.reloc\",\"a\"\n");
595 printf(".balign 4\n");
596 for (i
= 0; i
< reloc_count
; i
++) {
597 printf("\t .long 0x%08lx\n", relocs
[i
]);
602 unsigned char buf
[4];
604 fwrite("\0\0\0\0", 4, 1, stdout
);
605 /* Now print each relocation */
606 for (i
= 0; i
< reloc_count
; i
++) {
607 put_unaligned_le32(relocs
[i
], buf
);
608 fwrite(buf
, 4, 1, stdout
);
613 static void usage(void)
615 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
618 int main(int argc
, char **argv
)
620 int show_absolute_syms
, show_absolute_relocs
;
628 show_absolute_syms
= 0;
629 show_absolute_relocs
= 0;
632 for (i
= 1; i
< argc
; i
++) {
635 if (strcmp(argv
[1], "--abs-syms") == 0) {
636 show_absolute_syms
= 1;
640 if (strcmp(argv
[1], "--abs-relocs") == 0) {
641 show_absolute_relocs
= 1;
644 else if (strcmp(argv
[1], "--text") == 0) {
658 fp
= fopen(fname
, "r");
660 die("Cannot open %s: %s\n",
661 fname
, strerror(errno
));
668 if (show_absolute_syms
) {
669 print_absolute_symbols();
672 if (show_absolute_relocs
) {
673 print_absolute_relocs();
676 emit_relocs(as_text
);