2 * This file is taken from the Linux kernel and is distributed under GPL v2.
16 #include <tools/le_byteshift.h>
18 static void die(char *fmt
, ...);
20 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
21 static Elf32_Ehdr ehdr
;
22 static unsigned long reloc_count
, reloc_idx
;
23 static unsigned long *relocs
;
24 static unsigned long reloc16_count
, reloc16_idx
;
25 static unsigned long *relocs16
;
34 static struct section
*secs
;
44 static const char * const sym_regex_kernel
[S_NSYMTYPES
] = {
46 * Following symbols have been audited. Don't warn user about
47 * absolute relocations present w.r.t these symbols.
50 "^(__.*_len|__.*_dwords)$",
53 * These symbols are known to be relative, even if the linker marks them
54 * as absolute (typically defined outside any section in the linker script.)
57 "^(__.*_start|__.*_end|_end|_[se](text|data))$",
61 static const char * const sym_regex_realmode
[S_NSYMTYPES
] = {
63 * These are 16-bit segment symbols when compiling 16-bit code.
69 * These are offsets belonging to segments, as opposed to linear addresses,
70 * when compiling 16-bit code.
76 static const char * const *sym_regex
;
78 static regex_t sym_regex_c
[S_NSYMTYPES
];
79 static int is_reloc(enum symtype type
, const char *sym_name
)
81 return sym_regex
[type
] &&
82 !regexec(&sym_regex_c
[type
], sym_name
, 0, NULL
, 0);
85 static void regex_init(int use_real_mode
)
92 sym_regex
= sym_regex_realmode
;
94 sym_regex
= sym_regex_kernel
;
96 for (i
= 0; i
< S_NSYMTYPES
; i
++) {
100 err
= regcomp(&sym_regex_c
[i
], sym_regex
[i
],
101 REG_EXTENDED
|REG_NOSUB
);
104 regerror(err
, &sym_regex_c
[i
], errbuf
, sizeof errbuf
);
110 static void die(char *fmt
, ...)
114 vfprintf(stderr
, fmt
, ap
);
119 static const char *sym_type(unsigned type
)
121 static const char *type_name
[] = {
122 #define SYM_TYPE(X) [X] = #X
123 SYM_TYPE(STT_NOTYPE
),
124 SYM_TYPE(STT_OBJECT
),
126 SYM_TYPE(STT_SECTION
),
128 SYM_TYPE(STT_COMMON
),
132 const char *name
= "unknown sym type name";
133 if (type
< ARRAY_SIZE(type_name
)) {
134 name
= type_name
[type
];
139 static const char *sym_bind(unsigned bind
)
141 static const char *bind_name
[] = {
142 #define SYM_BIND(X) [X] = #X
144 SYM_BIND(STB_GLOBAL
),
148 const char *name
= "unknown sym bind name";
149 if (bind
< ARRAY_SIZE(bind_name
)) {
150 name
= bind_name
[bind
];
155 static const char *sym_visibility(unsigned visibility
)
157 static const char *visibility_name
[] = {
158 #define SYM_VISIBILITY(X) [X] = #X
159 SYM_VISIBILITY(STV_DEFAULT
),
160 SYM_VISIBILITY(STV_INTERNAL
),
161 SYM_VISIBILITY(STV_HIDDEN
),
162 SYM_VISIBILITY(STV_PROTECTED
),
163 #undef SYM_VISIBILITY
165 const char *name
= "unknown sym visibility name";
166 if (visibility
< ARRAY_SIZE(visibility_name
)) {
167 name
= visibility_name
[visibility
];
172 static const char *rel_type(unsigned type
)
174 static const char *type_name
[] = {
175 #define REL_TYPE(X) [X] = #X
176 REL_TYPE(R_386_NONE
),
178 REL_TYPE(R_386_PC32
),
179 REL_TYPE(R_386_GOT32
),
180 REL_TYPE(R_386_PLT32
),
181 REL_TYPE(R_386_COPY
),
182 REL_TYPE(R_386_GLOB_DAT
),
183 REL_TYPE(R_386_JMP_SLOT
),
184 REL_TYPE(R_386_RELATIVE
),
185 REL_TYPE(R_386_GOTOFF
),
186 REL_TYPE(R_386_GOTPC
),
190 REL_TYPE(R_386_PC16
),
193 const char *name
= "unknown type rel type name";
194 if (type
< ARRAY_SIZE(type_name
) && type_name
[type
]) {
195 name
= type_name
[type
];
200 static const char *sec_name(unsigned shndx
)
202 const char *sec_strtab
;
204 sec_strtab
= secs
[ehdr
.e_shstrndx
].strtab
;
206 if (shndx
< ehdr
.e_shnum
) {
207 name
= sec_strtab
+ secs
[shndx
].shdr
.sh_name
;
209 else if (shndx
== SHN_ABS
) {
212 else if (shndx
== SHN_COMMON
) {
218 static const char *sym_name(const char *sym_strtab
, Elf32_Sym
*sym
)
223 name
= sym_strtab
+ sym
->st_name
;
226 name
= sec_name(sym
->st_shndx
);
233 #if BYTE_ORDER == LITTLE_ENDIAN
234 #define le16_to_cpu(val) (val)
235 #define le32_to_cpu(val) (val)
237 #if BYTE_ORDER == BIG_ENDIAN
238 #define le16_to_cpu(val) bswap_16(val)
239 #define le32_to_cpu(val) bswap_32(val)
242 static uint16_t elf16_to_cpu(uint16_t val
)
244 return le16_to_cpu(val
);
247 static uint32_t elf32_to_cpu(uint32_t val
)
249 return le32_to_cpu(val
);
252 static void read_ehdr(FILE *fp
)
254 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1) {
255 die("Cannot read ELF header: %s\n",
258 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0) {
259 die("No ELF magic\n");
261 if (ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) {
262 die("Not a 32 bit executable\n");
264 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
) {
265 die("Not a LSB ELF executable\n");
267 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
268 die("Unknown ELF version\n");
270 /* Convert the fields to native endian */
271 ehdr
.e_type
= elf16_to_cpu(ehdr
.e_type
);
272 ehdr
.e_machine
= elf16_to_cpu(ehdr
.e_machine
);
273 ehdr
.e_version
= elf32_to_cpu(ehdr
.e_version
);
274 ehdr
.e_entry
= elf32_to_cpu(ehdr
.e_entry
);
275 ehdr
.e_phoff
= elf32_to_cpu(ehdr
.e_phoff
);
276 ehdr
.e_shoff
= elf32_to_cpu(ehdr
.e_shoff
);
277 ehdr
.e_flags
= elf32_to_cpu(ehdr
.e_flags
);
278 ehdr
.e_ehsize
= elf16_to_cpu(ehdr
.e_ehsize
);
279 ehdr
.e_phentsize
= elf16_to_cpu(ehdr
.e_phentsize
);
280 ehdr
.e_phnum
= elf16_to_cpu(ehdr
.e_phnum
);
281 ehdr
.e_shentsize
= elf16_to_cpu(ehdr
.e_shentsize
);
282 ehdr
.e_shnum
= elf16_to_cpu(ehdr
.e_shnum
);
283 ehdr
.e_shstrndx
= elf16_to_cpu(ehdr
.e_shstrndx
);
285 if ((ehdr
.e_type
!= ET_EXEC
) && (ehdr
.e_type
!= ET_DYN
)) {
286 die("Unsupported ELF header type\n");
288 if (ehdr
.e_machine
!= EM_386
) {
289 die("Not for x86\n");
291 if (ehdr
.e_version
!= EV_CURRENT
) {
292 die("Unknown ELF version\n");
294 if (ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
)) {
295 die("Bad Elf header size\n");
297 if (ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
)) {
298 die("Bad program header entry\n");
300 if (ehdr
.e_shentsize
!= sizeof(Elf32_Shdr
)) {
301 die("Bad section header entry\n");
303 if (ehdr
.e_shstrndx
>= ehdr
.e_shnum
) {
304 die("String table index out of bounds\n");
308 static void read_shdrs(FILE *fp
)
313 secs
= calloc(ehdr
.e_shnum
, sizeof(struct section
));
315 die("Unable to allocate %d section headers\n",
318 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0) {
319 die("Seek to %d failed: %s\n",
320 ehdr
.e_shoff
, strerror(errno
));
322 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
323 struct section
*sec
= &secs
[i
];
324 if (fread(&shdr
, sizeof shdr
, 1, fp
) != 1)
325 die("Cannot read ELF section headers %d/%d: %s\n",
326 i
, ehdr
.e_shnum
, strerror(errno
));
327 sec
->shdr
.sh_name
= elf32_to_cpu(shdr
.sh_name
);
328 sec
->shdr
.sh_type
= elf32_to_cpu(shdr
.sh_type
);
329 sec
->shdr
.sh_flags
= elf32_to_cpu(shdr
.sh_flags
);
330 sec
->shdr
.sh_addr
= elf32_to_cpu(shdr
.sh_addr
);
331 sec
->shdr
.sh_offset
= elf32_to_cpu(shdr
.sh_offset
);
332 sec
->shdr
.sh_size
= elf32_to_cpu(shdr
.sh_size
);
333 sec
->shdr
.sh_link
= elf32_to_cpu(shdr
.sh_link
);
334 sec
->shdr
.sh_info
= elf32_to_cpu(shdr
.sh_info
);
335 sec
->shdr
.sh_addralign
= elf32_to_cpu(shdr
.sh_addralign
);
336 sec
->shdr
.sh_entsize
= elf32_to_cpu(shdr
.sh_entsize
);
337 if (sec
->shdr
.sh_link
< ehdr
.e_shnum
)
338 sec
->link
= &secs
[sec
->shdr
.sh_link
];
343 static void read_strtabs(FILE *fp
)
346 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
347 struct section
*sec
= &secs
[i
];
348 if (sec
->shdr
.sh_type
!= SHT_STRTAB
) {
351 sec
->strtab
= malloc(sec
->shdr
.sh_size
);
353 die("malloc of %d bytes for strtab failed\n",
356 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
357 die("Seek to %d failed: %s\n",
358 sec
->shdr
.sh_offset
, strerror(errno
));
360 if (fread(sec
->strtab
, 1, sec
->shdr
.sh_size
, fp
)
361 != sec
->shdr
.sh_size
) {
362 die("Cannot read symbol table: %s\n",
368 static void read_symtabs(FILE *fp
)
371 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
372 struct section
*sec
= &secs
[i
];
373 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
376 sec
->symtab
= malloc(sec
->shdr
.sh_size
);
378 die("malloc of %d bytes for symtab failed\n",
381 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
382 die("Seek to %d failed: %s\n",
383 sec
->shdr
.sh_offset
, strerror(errno
));
385 if (fread(sec
->symtab
, 1, sec
->shdr
.sh_size
, fp
)
386 != sec
->shdr
.sh_size
) {
387 die("Cannot read symbol table: %s\n",
390 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
391 Elf32_Sym
*sym
= &sec
->symtab
[j
];
392 sym
->st_name
= elf32_to_cpu(sym
->st_name
);
393 sym
->st_value
= elf32_to_cpu(sym
->st_value
);
394 sym
->st_size
= elf32_to_cpu(sym
->st_size
);
395 sym
->st_shndx
= elf16_to_cpu(sym
->st_shndx
);
401 static void read_relocs(FILE *fp
)
404 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
405 struct section
*sec
= &secs
[i
];
406 if (sec
->shdr
.sh_type
!= SHT_REL
) {
409 sec
->reltab
= malloc(sec
->shdr
.sh_size
);
411 die("malloc of %d bytes for relocs failed\n",
414 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
415 die("Seek to %d failed: %s\n",
416 sec
->shdr
.sh_offset
, strerror(errno
));
418 if (fread(sec
->reltab
, 1, sec
->shdr
.sh_size
, fp
)
419 != sec
->shdr
.sh_size
) {
420 die("Cannot read symbol table: %s\n",
423 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
424 Elf32_Rel
*rel
= &sec
->reltab
[j
];
425 rel
->r_offset
= elf32_to_cpu(rel
->r_offset
);
426 rel
->r_info
= elf32_to_cpu(rel
->r_info
);
432 static void print_absolute_symbols(void)
435 printf("Absolute symbols\n");
436 printf(" Num: Value Size Type Bind Visibility Name\n");
437 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
438 struct section
*sec
= &secs
[i
];
442 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
445 sym_strtab
= sec
->link
->strtab
;
446 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
449 sym
= &sec
->symtab
[j
];
450 name
= sym_name(sym_strtab
, sym
);
451 if (sym
->st_shndx
!= SHN_ABS
) {
454 printf("%5d %08x %5d %10s %10s %12s %s\n",
455 j
, sym
->st_value
, sym
->st_size
,
456 sym_type(ELF32_ST_TYPE(sym
->st_info
)),
457 sym_bind(ELF32_ST_BIND(sym
->st_info
)),
458 sym_visibility(ELF32_ST_VISIBILITY(sym
->st_other
)),
465 static void print_absolute_relocs(void)
469 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
470 struct section
*sec
= &secs
[i
];
471 struct section
*sec_applies
, *sec_symtab
;
473 Elf32_Sym
*sh_symtab
;
475 if (sec
->shdr
.sh_type
!= SHT_REL
) {
478 sec_symtab
= sec
->link
;
479 sec_applies
= &secs
[sec
->shdr
.sh_info
];
480 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
483 sh_symtab
= sec_symtab
->symtab
;
484 sym_strtab
= sec_symtab
->link
->strtab
;
485 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
489 rel
= &sec
->reltab
[j
];
490 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
491 name
= sym_name(sym_strtab
, sym
);
492 if (sym
->st_shndx
!= SHN_ABS
) {
496 /* Absolute symbols are not relocated if bzImage is
497 * loaded at a non-compiled address. Display a warning
498 * to user at compile time about the absolute
499 * relocations present.
501 * User need to audit the code to make sure
502 * some symbols which should have been section
503 * relative have not become absolute because of some
504 * linker optimization or wrong programming usage.
506 * Before warning check if this absolute symbol
507 * relocation is harmless.
509 if (is_reloc(S_ABS
, name
) || is_reloc(S_REL
, name
))
513 printf("WARNING: Absolute relocations"
515 printf("Offset Info Type Sym.Value "
520 printf("%08x %08x %10s %08x %s\n",
523 rel_type(ELF32_R_TYPE(rel
->r_info
)),
533 static void walk_relocs(void (*visit
)(Elf32_Rel
*rel
, Elf32_Sym
*sym
),
537 /* Walk through the relocations */
538 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
540 Elf32_Sym
*sh_symtab
;
541 struct section
*sec_applies
, *sec_symtab
;
543 struct section
*sec
= &secs
[i
];
545 if (sec
->shdr
.sh_type
!= SHT_REL
) {
548 sec_symtab
= sec
->link
;
549 sec_applies
= &secs
[sec
->shdr
.sh_info
];
550 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
553 sh_symtab
= sec_symtab
->symtab
;
554 sym_strtab
= sec_symtab
->link
->strtab
;
555 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
562 rel
= &sec
->reltab
[j
];
563 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
564 r_type
= ELF32_R_TYPE(rel
->r_info
);
566 shn_abs
= sym
->st_shndx
== SHN_ABS
;
578 * NONE can be ignored and and PC relative
579 * relocations don't need to be adjusted.
584 symname
= sym_name(sym_strtab
, sym
);
588 if (is_reloc(S_ABS
, symname
))
590 else if (!is_reloc(S_SEG
, symname
))
593 if (is_reloc(S_LIN
, symname
))
602 symname
= sym_name(sym_strtab
, sym
);
604 if (is_reloc(S_ABS
, symname
))
606 else if (!is_reloc(S_REL
, symname
))
610 !is_reloc(S_LIN
, symname
))
616 die("Unsupported relocation type: %s (%d)\n",
617 rel_type(r_type
), r_type
);
620 symname
= sym_name(sym_strtab
, sym
);
621 die("Invalid %s %s relocation: %s\n",
622 shn_abs
? "absolute" : "relative",
623 rel_type(r_type
), symname
);
629 static void count_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
633 if (ELF32_R_TYPE(rel
->r_info
) == R_386_16
)
639 static void collect_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
643 /* Remember the address that needs to be adjusted. */
644 if (ELF32_R_TYPE(rel
->r_info
) == R_386_16
)
645 relocs16
[reloc16_idx
++] = rel
->r_offset
;
647 relocs
[reloc_idx
++] = rel
->r_offset
;
650 static int cmp_relocs(const void *va
, const void *vb
)
652 const unsigned long *a
, *b
;
654 return (*a
== *b
)? 0 : (*a
> *b
)? 1 : -1;
657 static int write32(unsigned int v
, FILE *f
)
659 unsigned char buf
[4];
661 put_unaligned_le32(v
, buf
);
662 return fwrite(buf
, 1, 4, f
) == 4 ? 0 : -1;
665 static void emit_relocs(int as_text
, int use_real_mode
)
668 /* Count how many relocations I have and allocate space for them. */
670 walk_relocs(count_reloc
, use_real_mode
);
671 relocs
= malloc(reloc_count
* sizeof(relocs
[0]));
673 die("malloc of %d entries for relocs failed\n",
677 relocs16
= malloc(reloc16_count
* sizeof(relocs
[0]));
679 die("malloc of %d entries for relocs16 failed\n",
682 /* Collect up the relocations */
684 walk_relocs(collect_reloc
, use_real_mode
);
686 if (reloc16_count
&& !use_real_mode
)
687 die("Segment relocations found but --realmode not specified\n");
689 /* Order the relocations for more efficient processing */
690 qsort(relocs
, reloc_count
, sizeof(relocs
[0]), cmp_relocs
);
691 qsort(relocs16
, reloc16_count
, sizeof(relocs16
[0]), cmp_relocs
);
693 /* Print the relocations */
695 /* Print the relocations in a form suitable that
698 printf(".section \".data.reloc\",\"a\"\n");
699 printf(".balign 4\n");
701 printf("\t.long %lu\n", reloc16_count
);
702 for (i
= 0; i
< reloc16_count
; i
++)
703 printf("\t.long 0x%08lx\n", relocs16
[i
]);
704 printf("\t.long %lu\n", reloc_count
);
705 for (i
= 0; i
< reloc_count
; i
++) {
706 printf("\t.long 0x%08lx\n", relocs
[i
]);
709 for (i
= 0; i
< reloc_count
; i
++) {
710 printf("\t.long 0x%08lx\n", relocs
[i
]);
713 printf("\t.long 0x%08lx\n", (unsigned long)0);
720 write32(reloc16_count
, stdout
);
721 for (i
= 0; i
< reloc16_count
; i
++)
722 write32(relocs16
[i
], stdout
);
723 write32(reloc_count
, stdout
);
725 /* Now print each relocation */
726 for (i
= 0; i
< reloc_count
; i
++)
727 write32(relocs
[i
], stdout
);
729 /* Now print each relocation */
730 for (i
= 0; i
< reloc_count
; i
++) {
731 write32(relocs
[i
], stdout
);
740 static void usage(void)
742 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
745 int main(int argc
, char **argv
)
747 int show_absolute_syms
, show_absolute_relocs
;
748 int as_text
, use_real_mode
;
753 show_absolute_syms
= 0;
754 show_absolute_relocs
= 0;
758 for (i
= 1; i
< argc
; i
++) {
761 if (strcmp(arg
, "--abs-syms") == 0) {
762 show_absolute_syms
= 1;
765 if (strcmp(arg
, "--abs-relocs") == 0) {
766 show_absolute_relocs
= 1;
769 if (strcmp(arg
, "--text") == 0) {
773 if (strcmp(arg
, "--realmode") == 0) {
787 regex_init(use_real_mode
);
788 fp
= fopen(fname
, "r");
790 die("Cannot open %s: %s\n",
791 fname
, strerror(errno
));
798 if (show_absolute_syms
) {
799 print_absolute_symbols();
802 if (show_absolute_relocs
) {
803 print_absolute_relocs();
806 emit_relocs(as_text
, use_real_mode
);