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
;
21 static unsigned long reloc16_count
, reloc16_idx
;
22 static unsigned long *relocs16
;
31 static struct section
*secs
;
41 static const char * const sym_regex_kernel
[S_NSYMTYPES
] = {
43 * Following symbols have been audited. There values are constant and do
44 * not change if bzImage is loaded at a different physical address than
45 * the address for which it has been compiled. Don't warn user about
46 * absolute relocations present w.r.t these symbols.
49 "^(xen_irq_disable_direct_reloc$|"
50 "xen_save_fl_direct_reloc$|"
55 * These symbols are known to be relative, even if the linker marks them
56 * as absolute (typically defined outside any section in the linker script.)
59 "^(__init_(begin|end)|"
60 "__x86_cpu_dev_(start|end)|"
61 "(__parainstructions|__alt_instructions)(|_end)|"
62 "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
67 static const char * const sym_regex_realmode
[S_NSYMTYPES
] = {
69 * These are 16-bit segment symbols when compiling 16-bit code.
75 * These are offsets belonging to segments, as opposed to linear addresses,
76 * when compiling 16-bit code.
82 static const char * const *sym_regex
;
84 static regex_t sym_regex_c
[S_NSYMTYPES
];
85 static int is_reloc(enum symtype type
, const char *sym_name
)
87 return sym_regex
[type
] &&
88 !regexec(&sym_regex_c
[type
], sym_name
, 0, NULL
, 0);
91 static void regex_init(int use_real_mode
)
98 sym_regex
= sym_regex_realmode
;
100 sym_regex
= sym_regex_kernel
;
102 for (i
= 0; i
< S_NSYMTYPES
; i
++) {
106 err
= regcomp(&sym_regex_c
[i
], sym_regex
[i
],
107 REG_EXTENDED
|REG_NOSUB
);
110 regerror(err
, &sym_regex_c
[i
], errbuf
, sizeof errbuf
);
116 static void die(char *fmt
, ...)
120 vfprintf(stderr
, fmt
, ap
);
125 static const char *sym_type(unsigned type
)
127 static const char *type_name
[] = {
128 #define SYM_TYPE(X) [X] = #X
129 SYM_TYPE(STT_NOTYPE
),
130 SYM_TYPE(STT_OBJECT
),
132 SYM_TYPE(STT_SECTION
),
134 SYM_TYPE(STT_COMMON
),
138 const char *name
= "unknown sym type name";
139 if (type
< ARRAY_SIZE(type_name
)) {
140 name
= type_name
[type
];
145 static const char *sym_bind(unsigned bind
)
147 static const char *bind_name
[] = {
148 #define SYM_BIND(X) [X] = #X
150 SYM_BIND(STB_GLOBAL
),
154 const char *name
= "unknown sym bind name";
155 if (bind
< ARRAY_SIZE(bind_name
)) {
156 name
= bind_name
[bind
];
161 static const char *sym_visibility(unsigned visibility
)
163 static const char *visibility_name
[] = {
164 #define SYM_VISIBILITY(X) [X] = #X
165 SYM_VISIBILITY(STV_DEFAULT
),
166 SYM_VISIBILITY(STV_INTERNAL
),
167 SYM_VISIBILITY(STV_HIDDEN
),
168 SYM_VISIBILITY(STV_PROTECTED
),
169 #undef SYM_VISIBILITY
171 const char *name
= "unknown sym visibility name";
172 if (visibility
< ARRAY_SIZE(visibility_name
)) {
173 name
= visibility_name
[visibility
];
178 static const char *rel_type(unsigned type
)
180 static const char *type_name
[] = {
181 #define REL_TYPE(X) [X] = #X
182 REL_TYPE(R_386_NONE
),
184 REL_TYPE(R_386_PC32
),
185 REL_TYPE(R_386_GOT32
),
186 REL_TYPE(R_386_PLT32
),
187 REL_TYPE(R_386_COPY
),
188 REL_TYPE(R_386_GLOB_DAT
),
189 REL_TYPE(R_386_JMP_SLOT
),
190 REL_TYPE(R_386_RELATIVE
),
191 REL_TYPE(R_386_GOTOFF
),
192 REL_TYPE(R_386_GOTPC
),
196 REL_TYPE(R_386_PC16
),
199 const char *name
= "unknown type rel type name";
200 if (type
< ARRAY_SIZE(type_name
) && type_name
[type
]) {
201 name
= type_name
[type
];
206 static const char *sec_name(unsigned shndx
)
208 const char *sec_strtab
;
210 sec_strtab
= secs
[ehdr
.e_shstrndx
].strtab
;
212 if (shndx
< ehdr
.e_shnum
) {
213 name
= sec_strtab
+ secs
[shndx
].shdr
.sh_name
;
215 else if (shndx
== SHN_ABS
) {
218 else if (shndx
== SHN_COMMON
) {
224 static const char *sym_name(const char *sym_strtab
, Elf32_Sym
*sym
)
229 name
= sym_strtab
+ sym
->st_name
;
232 name
= sec_name(sym
->st_shndx
);
239 #if BYTE_ORDER == LITTLE_ENDIAN
240 #define le16_to_cpu(val) (val)
241 #define le32_to_cpu(val) (val)
243 #if BYTE_ORDER == BIG_ENDIAN
244 #define le16_to_cpu(val) bswap_16(val)
245 #define le32_to_cpu(val) bswap_32(val)
248 static uint16_t elf16_to_cpu(uint16_t val
)
250 return le16_to_cpu(val
);
253 static uint32_t elf32_to_cpu(uint32_t val
)
255 return le32_to_cpu(val
);
258 static void read_ehdr(FILE *fp
)
260 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1) {
261 die("Cannot read ELF header: %s\n",
264 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0) {
265 die("No ELF magic\n");
267 if (ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) {
268 die("Not a 32 bit executable\n");
270 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
) {
271 die("Not a LSB ELF executable\n");
273 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
274 die("Unknown ELF version\n");
276 /* Convert the fields to native endian */
277 ehdr
.e_type
= elf16_to_cpu(ehdr
.e_type
);
278 ehdr
.e_machine
= elf16_to_cpu(ehdr
.e_machine
);
279 ehdr
.e_version
= elf32_to_cpu(ehdr
.e_version
);
280 ehdr
.e_entry
= elf32_to_cpu(ehdr
.e_entry
);
281 ehdr
.e_phoff
= elf32_to_cpu(ehdr
.e_phoff
);
282 ehdr
.e_shoff
= elf32_to_cpu(ehdr
.e_shoff
);
283 ehdr
.e_flags
= elf32_to_cpu(ehdr
.e_flags
);
284 ehdr
.e_ehsize
= elf16_to_cpu(ehdr
.e_ehsize
);
285 ehdr
.e_phentsize
= elf16_to_cpu(ehdr
.e_phentsize
);
286 ehdr
.e_phnum
= elf16_to_cpu(ehdr
.e_phnum
);
287 ehdr
.e_shentsize
= elf16_to_cpu(ehdr
.e_shentsize
);
288 ehdr
.e_shnum
= elf16_to_cpu(ehdr
.e_shnum
);
289 ehdr
.e_shstrndx
= elf16_to_cpu(ehdr
.e_shstrndx
);
291 if ((ehdr
.e_type
!= ET_EXEC
) && (ehdr
.e_type
!= ET_DYN
)) {
292 die("Unsupported ELF header type\n");
294 if (ehdr
.e_machine
!= EM_386
) {
295 die("Not for x86\n");
297 if (ehdr
.e_version
!= EV_CURRENT
) {
298 die("Unknown ELF version\n");
300 if (ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
)) {
301 die("Bad Elf header size\n");
303 if (ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
)) {
304 die("Bad program header entry\n");
306 if (ehdr
.e_shentsize
!= sizeof(Elf32_Shdr
)) {
307 die("Bad section header entry\n");
309 if (ehdr
.e_shstrndx
>= ehdr
.e_shnum
) {
310 die("String table index out of bounds\n");
314 static void read_shdrs(FILE *fp
)
319 secs
= calloc(ehdr
.e_shnum
, sizeof(struct section
));
321 die("Unable to allocate %d section headers\n",
324 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0) {
325 die("Seek to %d failed: %s\n",
326 ehdr
.e_shoff
, strerror(errno
));
328 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
329 struct section
*sec
= &secs
[i
];
330 if (fread(&shdr
, sizeof shdr
, 1, fp
) != 1)
331 die("Cannot read ELF section headers %d/%d: %s\n",
332 i
, ehdr
.e_shnum
, strerror(errno
));
333 sec
->shdr
.sh_name
= elf32_to_cpu(shdr
.sh_name
);
334 sec
->shdr
.sh_type
= elf32_to_cpu(shdr
.sh_type
);
335 sec
->shdr
.sh_flags
= elf32_to_cpu(shdr
.sh_flags
);
336 sec
->shdr
.sh_addr
= elf32_to_cpu(shdr
.sh_addr
);
337 sec
->shdr
.sh_offset
= elf32_to_cpu(shdr
.sh_offset
);
338 sec
->shdr
.sh_size
= elf32_to_cpu(shdr
.sh_size
);
339 sec
->shdr
.sh_link
= elf32_to_cpu(shdr
.sh_link
);
340 sec
->shdr
.sh_info
= elf32_to_cpu(shdr
.sh_info
);
341 sec
->shdr
.sh_addralign
= elf32_to_cpu(shdr
.sh_addralign
);
342 sec
->shdr
.sh_entsize
= elf32_to_cpu(shdr
.sh_entsize
);
343 if (sec
->shdr
.sh_link
< ehdr
.e_shnum
)
344 sec
->link
= &secs
[sec
->shdr
.sh_link
];
349 static void read_strtabs(FILE *fp
)
352 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
353 struct section
*sec
= &secs
[i
];
354 if (sec
->shdr
.sh_type
!= SHT_STRTAB
) {
357 sec
->strtab
= malloc(sec
->shdr
.sh_size
);
359 die("malloc of %d bytes for strtab failed\n",
362 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
363 die("Seek to %d failed: %s\n",
364 sec
->shdr
.sh_offset
, strerror(errno
));
366 if (fread(sec
->strtab
, 1, sec
->shdr
.sh_size
, fp
)
367 != sec
->shdr
.sh_size
) {
368 die("Cannot read symbol table: %s\n",
374 static void read_symtabs(FILE *fp
)
377 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
378 struct section
*sec
= &secs
[i
];
379 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
382 sec
->symtab
= malloc(sec
->shdr
.sh_size
);
384 die("malloc of %d bytes for symtab failed\n",
387 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
388 die("Seek to %d failed: %s\n",
389 sec
->shdr
.sh_offset
, strerror(errno
));
391 if (fread(sec
->symtab
, 1, sec
->shdr
.sh_size
, fp
)
392 != sec
->shdr
.sh_size
) {
393 die("Cannot read symbol table: %s\n",
396 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
397 Elf32_Sym
*sym
= &sec
->symtab
[j
];
398 sym
->st_name
= elf32_to_cpu(sym
->st_name
);
399 sym
->st_value
= elf32_to_cpu(sym
->st_value
);
400 sym
->st_size
= elf32_to_cpu(sym
->st_size
);
401 sym
->st_shndx
= elf16_to_cpu(sym
->st_shndx
);
407 static void read_relocs(FILE *fp
)
410 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
411 struct section
*sec
= &secs
[i
];
412 if (sec
->shdr
.sh_type
!= SHT_REL
) {
415 sec
->reltab
= malloc(sec
->shdr
.sh_size
);
417 die("malloc of %d bytes for relocs failed\n",
420 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
421 die("Seek to %d failed: %s\n",
422 sec
->shdr
.sh_offset
, strerror(errno
));
424 if (fread(sec
->reltab
, 1, sec
->shdr
.sh_size
, fp
)
425 != sec
->shdr
.sh_size
) {
426 die("Cannot read symbol table: %s\n",
429 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
430 Elf32_Rel
*rel
= &sec
->reltab
[j
];
431 rel
->r_offset
= elf32_to_cpu(rel
->r_offset
);
432 rel
->r_info
= elf32_to_cpu(rel
->r_info
);
438 static void print_absolute_symbols(void)
441 printf("Absolute symbols\n");
442 printf(" Num: Value Size Type Bind Visibility Name\n");
443 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
444 struct section
*sec
= &secs
[i
];
448 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
451 sym_strtab
= sec
->link
->strtab
;
452 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
455 sym
= &sec
->symtab
[j
];
456 name
= sym_name(sym_strtab
, sym
);
457 if (sym
->st_shndx
!= SHN_ABS
) {
460 printf("%5d %08x %5d %10s %10s %12s %s\n",
461 j
, sym
->st_value
, sym
->st_size
,
462 sym_type(ELF32_ST_TYPE(sym
->st_info
)),
463 sym_bind(ELF32_ST_BIND(sym
->st_info
)),
464 sym_visibility(ELF32_ST_VISIBILITY(sym
->st_other
)),
471 static void print_absolute_relocs(void)
475 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
476 struct section
*sec
= &secs
[i
];
477 struct section
*sec_applies
, *sec_symtab
;
479 Elf32_Sym
*sh_symtab
;
481 if (sec
->shdr
.sh_type
!= SHT_REL
) {
484 sec_symtab
= sec
->link
;
485 sec_applies
= &secs
[sec
->shdr
.sh_info
];
486 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
489 sh_symtab
= sec_symtab
->symtab
;
490 sym_strtab
= sec_symtab
->link
->strtab
;
491 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
495 rel
= &sec
->reltab
[j
];
496 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
497 name
= sym_name(sym_strtab
, sym
);
498 if (sym
->st_shndx
!= SHN_ABS
) {
502 /* Absolute symbols are not relocated if bzImage is
503 * loaded at a non-compiled address. Display a warning
504 * to user at compile time about the absolute
505 * relocations present.
507 * User need to audit the code to make sure
508 * some symbols which should have been section
509 * relative have not become absolute because of some
510 * linker optimization or wrong programming usage.
512 * Before warning check if this absolute symbol
513 * relocation is harmless.
515 if (is_reloc(S_ABS
, name
) || is_reloc(S_REL
, name
))
519 printf("WARNING: Absolute relocations"
521 printf("Offset Info Type Sym.Value "
526 printf("%08x %08x %10s %08x %s\n",
529 rel_type(ELF32_R_TYPE(rel
->r_info
)),
539 static void walk_relocs(void (*visit
)(Elf32_Rel
*rel
, Elf32_Sym
*sym
),
543 /* Walk through the relocations */
544 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
546 Elf32_Sym
*sh_symtab
;
547 struct section
*sec_applies
, *sec_symtab
;
549 struct section
*sec
= &secs
[i
];
551 if (sec
->shdr
.sh_type
!= SHT_REL
) {
554 sec_symtab
= sec
->link
;
555 sec_applies
= &secs
[sec
->shdr
.sh_info
];
556 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
559 sh_symtab
= sec_symtab
->symtab
;
560 sym_strtab
= sec_symtab
->link
->strtab
;
561 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
568 rel
= &sec
->reltab
[j
];
569 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
570 r_type
= ELF32_R_TYPE(rel
->r_info
);
572 shn_abs
= sym
->st_shndx
== SHN_ABS
;
580 * NONE can be ignored and and PC relative
581 * relocations don't need to be adjusted.
586 symname
= sym_name(sym_strtab
, sym
);
590 if (is_reloc(S_ABS
, symname
))
592 else if (!is_reloc(S_SEG
, symname
))
595 if (is_reloc(S_LIN
, symname
))
604 symname
= sym_name(sym_strtab
, sym
);
606 if (is_reloc(S_ABS
, symname
))
608 else if (!is_reloc(S_REL
, symname
))
612 !is_reloc(S_LIN
, symname
))
618 die("Unsupported relocation type: %s (%d)\n",
619 rel_type(r_type
), r_type
);
622 symname
= sym_name(sym_strtab
, sym
);
623 die("Invalid %s %s relocation: %s\n",
624 shn_abs
? "absolute" : "relative",
625 rel_type(r_type
), symname
);
631 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
)
641 /* Remember the address that needs to be adjusted. */
642 if (ELF32_R_TYPE(rel
->r_info
) == R_386_16
)
643 relocs16
[reloc16_idx
++] = rel
->r_offset
;
645 relocs
[reloc_idx
++] = rel
->r_offset
;
648 static int cmp_relocs(const void *va
, const void *vb
)
650 const unsigned long *a
, *b
;
652 return (*a
== *b
)? 0 : (*a
> *b
)? 1 : -1;
655 static int write32(unsigned int v
, FILE *f
)
657 unsigned char buf
[4];
659 put_unaligned_le32(v
, buf
);
660 return fwrite(buf
, 1, 4, f
) == 4 ? 0 : -1;
663 static void emit_relocs(int as_text
, int use_real_mode
)
666 /* Count how many relocations I have and allocate space for them. */
668 walk_relocs(count_reloc
, use_real_mode
);
669 relocs
= malloc(reloc_count
* sizeof(relocs
[0]));
671 die("malloc of %d entries for relocs failed\n",
675 relocs16
= malloc(reloc16_count
* sizeof(relocs
[0]));
677 die("malloc of %d entries for relocs16 failed\n",
680 /* Collect up the relocations */
682 walk_relocs(collect_reloc
, use_real_mode
);
684 if (reloc16_count
&& !use_real_mode
)
685 die("Segment relocations found but --realmode not specified\n");
687 /* Order the relocations for more efficient processing */
688 qsort(relocs
, reloc_count
, sizeof(relocs
[0]), cmp_relocs
);
689 qsort(relocs16
, reloc16_count
, sizeof(relocs16
[0]), cmp_relocs
);
691 /* Print the relocations */
693 /* Print the relocations in a form suitable that
696 printf(".section \".data.reloc\",\"a\"\n");
697 printf(".balign 4\n");
699 printf("\t.long %lu\n", reloc16_count
);
700 for (i
= 0; i
< reloc16_count
; i
++)
701 printf("\t.long 0x%08lx\n", relocs16
[i
]);
702 printf("\t.long %lu\n", reloc_count
);
703 for (i
= 0; i
< reloc_count
; i
++) {
704 printf("\t.long 0x%08lx\n", relocs
[i
]);
708 printf("\t.long 0x%08lx\n", (unsigned long)0);
709 for (i
= 0; i
< reloc_count
; i
++) {
710 printf("\t.long 0x%08lx\n", relocs
[i
]);
718 write32(reloc16_count
, stdout
);
719 for (i
= 0; i
< reloc16_count
; i
++)
720 write32(relocs16
[i
], stdout
);
721 write32(reloc_count
, stdout
);
723 /* Now print each relocation */
724 for (i
= 0; i
< reloc_count
; i
++)
725 write32(relocs
[i
], stdout
);
730 /* Now print each relocation */
731 for (i
= 0; i
< reloc_count
; i
++) {
732 write32(relocs
[i
], stdout
);
738 static void usage(void)
740 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
743 int main(int argc
, char **argv
)
745 int show_absolute_syms
, show_absolute_relocs
;
746 int as_text
, use_real_mode
;
751 show_absolute_syms
= 0;
752 show_absolute_relocs
= 0;
756 for (i
= 1; i
< argc
; i
++) {
759 if (strcmp(arg
, "--abs-syms") == 0) {
760 show_absolute_syms
= 1;
763 if (strcmp(arg
, "--abs-relocs") == 0) {
764 show_absolute_relocs
= 1;
767 if (strcmp(arg
, "--text") == 0) {
771 if (strcmp(arg
, "--realmode") == 0) {
785 regex_init(use_real_mode
);
786 fp
= fopen(fname
, "r");
788 die("Cannot open %s: %s\n",
789 fname
, strerror(errno
));
796 if (show_absolute_syms
) {
797 print_absolute_symbols();
800 if (show_absolute_relocs
) {
801 print_absolute_relocs();
804 emit_relocs(as_text
, use_real_mode
);