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)|"
63 "__(start|end)_pci_.*|"
64 "__(start|end)_builtin_fw|"
65 "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
66 "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
67 "__(start|stop)___param|"
68 "__(start|stop)___modver|"
69 "__(start|stop)___bug_table|"
70 "__tracedata_(start|end)|"
71 "__(start|stop)_notes|"
74 "(jiffies|jiffies_64)|"
79 static const char * const sym_regex_realmode
[S_NSYMTYPES
] = {
81 * These symbols are known to be relative, even if the linker marks them
82 * as absolute (typically defined outside any section in the linker script.)
88 * These are 16-bit segment symbols when compiling 16-bit code.
94 * These are offsets belonging to segments, as opposed to linear addresses,
95 * when compiling 16-bit code.
101 static const char * const *sym_regex
;
103 static regex_t sym_regex_c
[S_NSYMTYPES
];
104 static int is_reloc(enum symtype type
, const char *sym_name
)
106 return sym_regex
[type
] &&
107 !regexec(&sym_regex_c
[type
], sym_name
, 0, NULL
, 0);
110 static void regex_init(int use_real_mode
)
117 sym_regex
= sym_regex_realmode
;
119 sym_regex
= sym_regex_kernel
;
121 for (i
= 0; i
< S_NSYMTYPES
; i
++) {
125 err
= regcomp(&sym_regex_c
[i
], sym_regex
[i
],
126 REG_EXTENDED
|REG_NOSUB
);
129 regerror(err
, &sym_regex_c
[i
], errbuf
, sizeof errbuf
);
135 static void die(char *fmt
, ...)
139 vfprintf(stderr
, fmt
, ap
);
144 static const char *sym_type(unsigned type
)
146 static const char *type_name
[] = {
147 #define SYM_TYPE(X) [X] = #X
148 SYM_TYPE(STT_NOTYPE
),
149 SYM_TYPE(STT_OBJECT
),
151 SYM_TYPE(STT_SECTION
),
153 SYM_TYPE(STT_COMMON
),
157 const char *name
= "unknown sym type name";
158 if (type
< ARRAY_SIZE(type_name
)) {
159 name
= type_name
[type
];
164 static const char *sym_bind(unsigned bind
)
166 static const char *bind_name
[] = {
167 #define SYM_BIND(X) [X] = #X
169 SYM_BIND(STB_GLOBAL
),
173 const char *name
= "unknown sym bind name";
174 if (bind
< ARRAY_SIZE(bind_name
)) {
175 name
= bind_name
[bind
];
180 static const char *sym_visibility(unsigned visibility
)
182 static const char *visibility_name
[] = {
183 #define SYM_VISIBILITY(X) [X] = #X
184 SYM_VISIBILITY(STV_DEFAULT
),
185 SYM_VISIBILITY(STV_INTERNAL
),
186 SYM_VISIBILITY(STV_HIDDEN
),
187 SYM_VISIBILITY(STV_PROTECTED
),
188 #undef SYM_VISIBILITY
190 const char *name
= "unknown sym visibility name";
191 if (visibility
< ARRAY_SIZE(visibility_name
)) {
192 name
= visibility_name
[visibility
];
197 static const char *rel_type(unsigned type
)
199 static const char *type_name
[] = {
200 #define REL_TYPE(X) [X] = #X
201 REL_TYPE(R_386_NONE
),
203 REL_TYPE(R_386_PC32
),
204 REL_TYPE(R_386_GOT32
),
205 REL_TYPE(R_386_PLT32
),
206 REL_TYPE(R_386_COPY
),
207 REL_TYPE(R_386_GLOB_DAT
),
208 REL_TYPE(R_386_JMP_SLOT
),
209 REL_TYPE(R_386_RELATIVE
),
210 REL_TYPE(R_386_GOTOFF
),
211 REL_TYPE(R_386_GOTPC
),
215 REL_TYPE(R_386_PC16
),
218 const char *name
= "unknown type rel type name";
219 if (type
< ARRAY_SIZE(type_name
) && type_name
[type
]) {
220 name
= type_name
[type
];
225 static const char *sec_name(unsigned shndx
)
227 const char *sec_strtab
;
229 sec_strtab
= secs
[ehdr
.e_shstrndx
].strtab
;
231 if (shndx
< ehdr
.e_shnum
) {
232 name
= sec_strtab
+ secs
[shndx
].shdr
.sh_name
;
234 else if (shndx
== SHN_ABS
) {
237 else if (shndx
== SHN_COMMON
) {
243 static const char *sym_name(const char *sym_strtab
, Elf32_Sym
*sym
)
248 name
= sym_strtab
+ sym
->st_name
;
251 name
= sec_name(sym
->st_shndx
);
258 #if BYTE_ORDER == LITTLE_ENDIAN
259 #define le16_to_cpu(val) (val)
260 #define le32_to_cpu(val) (val)
262 #if BYTE_ORDER == BIG_ENDIAN
263 #define le16_to_cpu(val) bswap_16(val)
264 #define le32_to_cpu(val) bswap_32(val)
267 static uint16_t elf16_to_cpu(uint16_t val
)
269 return le16_to_cpu(val
);
272 static uint32_t elf32_to_cpu(uint32_t val
)
274 return le32_to_cpu(val
);
277 static void read_ehdr(FILE *fp
)
279 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1) {
280 die("Cannot read ELF header: %s\n",
283 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0) {
284 die("No ELF magic\n");
286 if (ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) {
287 die("Not a 32 bit executable\n");
289 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
) {
290 die("Not a LSB ELF executable\n");
292 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
293 die("Unknown ELF version\n");
295 /* Convert the fields to native endian */
296 ehdr
.e_type
= elf16_to_cpu(ehdr
.e_type
);
297 ehdr
.e_machine
= elf16_to_cpu(ehdr
.e_machine
);
298 ehdr
.e_version
= elf32_to_cpu(ehdr
.e_version
);
299 ehdr
.e_entry
= elf32_to_cpu(ehdr
.e_entry
);
300 ehdr
.e_phoff
= elf32_to_cpu(ehdr
.e_phoff
);
301 ehdr
.e_shoff
= elf32_to_cpu(ehdr
.e_shoff
);
302 ehdr
.e_flags
= elf32_to_cpu(ehdr
.e_flags
);
303 ehdr
.e_ehsize
= elf16_to_cpu(ehdr
.e_ehsize
);
304 ehdr
.e_phentsize
= elf16_to_cpu(ehdr
.e_phentsize
);
305 ehdr
.e_phnum
= elf16_to_cpu(ehdr
.e_phnum
);
306 ehdr
.e_shentsize
= elf16_to_cpu(ehdr
.e_shentsize
);
307 ehdr
.e_shnum
= elf16_to_cpu(ehdr
.e_shnum
);
308 ehdr
.e_shstrndx
= elf16_to_cpu(ehdr
.e_shstrndx
);
310 if ((ehdr
.e_type
!= ET_EXEC
) && (ehdr
.e_type
!= ET_DYN
)) {
311 die("Unsupported ELF header type\n");
313 if (ehdr
.e_machine
!= EM_386
) {
314 die("Not for x86\n");
316 if (ehdr
.e_version
!= EV_CURRENT
) {
317 die("Unknown ELF version\n");
319 if (ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
)) {
320 die("Bad Elf header size\n");
322 if (ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
)) {
323 die("Bad program header entry\n");
325 if (ehdr
.e_shentsize
!= sizeof(Elf32_Shdr
)) {
326 die("Bad section header entry\n");
328 if (ehdr
.e_shstrndx
>= ehdr
.e_shnum
) {
329 die("String table index out of bounds\n");
333 static void read_shdrs(FILE *fp
)
338 secs
= calloc(ehdr
.e_shnum
, sizeof(struct section
));
340 die("Unable to allocate %d section headers\n",
343 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0) {
344 die("Seek to %d failed: %s\n",
345 ehdr
.e_shoff
, strerror(errno
));
347 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
348 struct section
*sec
= &secs
[i
];
349 if (fread(&shdr
, sizeof shdr
, 1, fp
) != 1)
350 die("Cannot read ELF section headers %d/%d: %s\n",
351 i
, ehdr
.e_shnum
, strerror(errno
));
352 sec
->shdr
.sh_name
= elf32_to_cpu(shdr
.sh_name
);
353 sec
->shdr
.sh_type
= elf32_to_cpu(shdr
.sh_type
);
354 sec
->shdr
.sh_flags
= elf32_to_cpu(shdr
.sh_flags
);
355 sec
->shdr
.sh_addr
= elf32_to_cpu(shdr
.sh_addr
);
356 sec
->shdr
.sh_offset
= elf32_to_cpu(shdr
.sh_offset
);
357 sec
->shdr
.sh_size
= elf32_to_cpu(shdr
.sh_size
);
358 sec
->shdr
.sh_link
= elf32_to_cpu(shdr
.sh_link
);
359 sec
->shdr
.sh_info
= elf32_to_cpu(shdr
.sh_info
);
360 sec
->shdr
.sh_addralign
= elf32_to_cpu(shdr
.sh_addralign
);
361 sec
->shdr
.sh_entsize
= elf32_to_cpu(shdr
.sh_entsize
);
362 if (sec
->shdr
.sh_link
< ehdr
.e_shnum
)
363 sec
->link
= &secs
[sec
->shdr
.sh_link
];
368 static void read_strtabs(FILE *fp
)
371 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
372 struct section
*sec
= &secs
[i
];
373 if (sec
->shdr
.sh_type
!= SHT_STRTAB
) {
376 sec
->strtab
= malloc(sec
->shdr
.sh_size
);
378 die("malloc of %d bytes for strtab 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
->strtab
, 1, sec
->shdr
.sh_size
, fp
)
386 != sec
->shdr
.sh_size
) {
387 die("Cannot read symbol table: %s\n",
393 static void read_symtabs(FILE *fp
)
396 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
397 struct section
*sec
= &secs
[i
];
398 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
401 sec
->symtab
= malloc(sec
->shdr
.sh_size
);
403 die("malloc of %d bytes for symtab failed\n",
406 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
407 die("Seek to %d failed: %s\n",
408 sec
->shdr
.sh_offset
, strerror(errno
));
410 if (fread(sec
->symtab
, 1, sec
->shdr
.sh_size
, fp
)
411 != sec
->shdr
.sh_size
) {
412 die("Cannot read symbol table: %s\n",
415 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
416 Elf32_Sym
*sym
= &sec
->symtab
[j
];
417 sym
->st_name
= elf32_to_cpu(sym
->st_name
);
418 sym
->st_value
= elf32_to_cpu(sym
->st_value
);
419 sym
->st_size
= elf32_to_cpu(sym
->st_size
);
420 sym
->st_shndx
= elf16_to_cpu(sym
->st_shndx
);
426 static void read_relocs(FILE *fp
)
429 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
430 struct section
*sec
= &secs
[i
];
431 if (sec
->shdr
.sh_type
!= SHT_REL
) {
434 sec
->reltab
= malloc(sec
->shdr
.sh_size
);
436 die("malloc of %d bytes for relocs failed\n",
439 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
440 die("Seek to %d failed: %s\n",
441 sec
->shdr
.sh_offset
, strerror(errno
));
443 if (fread(sec
->reltab
, 1, sec
->shdr
.sh_size
, fp
)
444 != sec
->shdr
.sh_size
) {
445 die("Cannot read symbol table: %s\n",
448 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
449 Elf32_Rel
*rel
= &sec
->reltab
[j
];
450 rel
->r_offset
= elf32_to_cpu(rel
->r_offset
);
451 rel
->r_info
= elf32_to_cpu(rel
->r_info
);
457 static void print_absolute_symbols(void)
460 printf("Absolute symbols\n");
461 printf(" Num: Value Size Type Bind Visibility Name\n");
462 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
463 struct section
*sec
= &secs
[i
];
467 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
470 sym_strtab
= sec
->link
->strtab
;
471 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
474 sym
= &sec
->symtab
[j
];
475 name
= sym_name(sym_strtab
, sym
);
476 if (sym
->st_shndx
!= SHN_ABS
) {
479 printf("%5d %08x %5d %10s %10s %12s %s\n",
480 j
, sym
->st_value
, sym
->st_size
,
481 sym_type(ELF32_ST_TYPE(sym
->st_info
)),
482 sym_bind(ELF32_ST_BIND(sym
->st_info
)),
483 sym_visibility(ELF32_ST_VISIBILITY(sym
->st_other
)),
490 static void print_absolute_relocs(void)
494 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
495 struct section
*sec
= &secs
[i
];
496 struct section
*sec_applies
, *sec_symtab
;
498 Elf32_Sym
*sh_symtab
;
500 if (sec
->shdr
.sh_type
!= SHT_REL
) {
503 sec_symtab
= sec
->link
;
504 sec_applies
= &secs
[sec
->shdr
.sh_info
];
505 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
508 sh_symtab
= sec_symtab
->symtab
;
509 sym_strtab
= sec_symtab
->link
->strtab
;
510 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
514 rel
= &sec
->reltab
[j
];
515 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
516 name
= sym_name(sym_strtab
, sym
);
517 if (sym
->st_shndx
!= SHN_ABS
) {
521 /* Absolute symbols are not relocated if bzImage is
522 * loaded at a non-compiled address. Display a warning
523 * to user at compile time about the absolute
524 * relocations present.
526 * User need to audit the code to make sure
527 * some symbols which should have been section
528 * relative have not become absolute because of some
529 * linker optimization or wrong programming usage.
531 * Before warning check if this absolute symbol
532 * relocation is harmless.
534 if (is_reloc(S_ABS
, name
) || is_reloc(S_REL
, name
))
538 printf("WARNING: Absolute relocations"
540 printf("Offset Info Type Sym.Value "
545 printf("%08x %08x %10s %08x %s\n",
548 rel_type(ELF32_R_TYPE(rel
->r_info
)),
558 static void walk_relocs(void (*visit
)(Elf32_Rel
*rel
, Elf32_Sym
*sym
),
562 /* Walk through the relocations */
563 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
565 Elf32_Sym
*sh_symtab
;
566 struct section
*sec_applies
, *sec_symtab
;
568 struct section
*sec
= &secs
[i
];
570 if (sec
->shdr
.sh_type
!= SHT_REL
) {
573 sec_symtab
= sec
->link
;
574 sec_applies
= &secs
[sec
->shdr
.sh_info
];
575 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
578 sh_symtab
= sec_symtab
->symtab
;
579 sym_strtab
= sec_symtab
->link
->strtab
;
580 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
587 rel
= &sec
->reltab
[j
];
588 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
589 r_type
= ELF32_R_TYPE(rel
->r_info
);
591 shn_abs
= sym
->st_shndx
== SHN_ABS
;
599 * NONE can be ignored and and PC relative
600 * relocations don't need to be adjusted.
605 symname
= sym_name(sym_strtab
, sym
);
609 if (is_reloc(S_ABS
, symname
))
611 else if (!is_reloc(S_SEG
, symname
))
614 if (is_reloc(S_LIN
, symname
))
623 symname
= sym_name(sym_strtab
, sym
);
625 if (is_reloc(S_ABS
, symname
))
627 else if (!is_reloc(S_REL
, symname
))
631 !is_reloc(S_LIN
, symname
))
637 die("Unsupported relocation type: %s (%d)\n",
638 rel_type(r_type
), r_type
);
641 symname
= sym_name(sym_strtab
, sym
);
642 die("Invalid %s %s relocation: %s\n",
643 shn_abs
? "absolute" : "relative",
644 rel_type(r_type
), symname
);
650 static void count_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
652 if (ELF32_R_TYPE(rel
->r_info
) == R_386_16
)
658 static void collect_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
660 /* Remember the address that needs to be adjusted. */
661 if (ELF32_R_TYPE(rel
->r_info
) == R_386_16
)
662 relocs16
[reloc16_idx
++] = rel
->r_offset
;
664 relocs
[reloc_idx
++] = rel
->r_offset
;
667 static int cmp_relocs(const void *va
, const void *vb
)
669 const unsigned long *a
, *b
;
671 return (*a
== *b
)? 0 : (*a
> *b
)? 1 : -1;
674 static int write32(unsigned int v
, FILE *f
)
676 unsigned char buf
[4];
678 put_unaligned_le32(v
, buf
);
679 return fwrite(buf
, 1, 4, f
) == 4 ? 0 : -1;
682 static void emit_relocs(int as_text
, int use_real_mode
)
685 /* Count how many relocations I have and allocate space for them. */
687 walk_relocs(count_reloc
, use_real_mode
);
688 relocs
= malloc(reloc_count
* sizeof(relocs
[0]));
690 die("malloc of %d entries for relocs failed\n",
694 relocs16
= malloc(reloc16_count
* sizeof(relocs
[0]));
696 die("malloc of %d entries for relocs16 failed\n",
699 /* Collect up the relocations */
701 walk_relocs(collect_reloc
, use_real_mode
);
703 if (reloc16_count
&& !use_real_mode
)
704 die("Segment relocations found but --realmode not specified\n");
706 /* Order the relocations for more efficient processing */
707 qsort(relocs
, reloc_count
, sizeof(relocs
[0]), cmp_relocs
);
708 qsort(relocs16
, reloc16_count
, sizeof(relocs16
[0]), cmp_relocs
);
710 /* Print the relocations */
712 /* Print the relocations in a form suitable that
715 printf(".section \".data.reloc\",\"a\"\n");
716 printf(".balign 4\n");
718 printf("\t.long %lu\n", reloc16_count
);
719 for (i
= 0; i
< reloc16_count
; i
++)
720 printf("\t.long 0x%08lx\n", relocs16
[i
]);
721 printf("\t.long %lu\n", reloc_count
);
722 for (i
= 0; i
< reloc_count
; i
++) {
723 printf("\t.long 0x%08lx\n", relocs
[i
]);
727 printf("\t.long 0x%08lx\n", (unsigned long)0);
728 for (i
= 0; i
< reloc_count
; i
++) {
729 printf("\t.long 0x%08lx\n", relocs
[i
]);
737 write32(reloc16_count
, stdout
);
738 for (i
= 0; i
< reloc16_count
; i
++)
739 write32(relocs16
[i
], stdout
);
740 write32(reloc_count
, stdout
);
742 /* Now print each relocation */
743 for (i
= 0; i
< reloc_count
; i
++)
744 write32(relocs
[i
], stdout
);
749 /* Now print each relocation */
750 for (i
= 0; i
< reloc_count
; i
++) {
751 write32(relocs
[i
], stdout
);
757 static void usage(void)
759 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
762 int main(int argc
, char **argv
)
764 int show_absolute_syms
, show_absolute_relocs
;
765 int as_text
, use_real_mode
;
770 show_absolute_syms
= 0;
771 show_absolute_relocs
= 0;
775 for (i
= 1; i
< argc
; i
++) {
778 if (strcmp(arg
, "--abs-syms") == 0) {
779 show_absolute_syms
= 1;
782 if (strcmp(arg
, "--abs-relocs") == 0) {
783 show_absolute_relocs
= 1;
786 if (strcmp(arg
, "--text") == 0) {
790 if (strcmp(arg
, "--realmode") == 0) {
804 regex_init(use_real_mode
);
805 fp
= fopen(fname
, "r");
807 die("Cannot open %s: %s\n",
808 fname
, strerror(errno
));
815 if (show_absolute_syms
) {
816 print_absolute_symbols();
819 if (show_absolute_relocs
) {
820 print_absolute_relocs();
823 emit_relocs(as_text
, use_real_mode
);