15 static void die(
char *
fmt, ...);
17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
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;
41 static const char *
const sym_regex_kernel[
S_NSYMTYPES] = {
49 "^(xen_irq_disable_direct_reloc$|"
50 "xen_save_fl_direct_reloc$|"
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] = {
101 static const char *
const *sym_regex;
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;
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, ...)
144 static const char *sym_type(
unsigned type)
146 static const char *type_name[] = {
147 #define SYM_TYPE(X) [X] = #X
157 const char *
name =
"unknown sym 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
173 const char *name =
"unknown sym 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
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
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;
248 name = sym_strtab + sym->
st_name;
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)
277 static void read_ehdr(FILE *
fp)
279 if (fread(&ehdr,
sizeof(ehdr), 1, fp) != 1) {
280 die(
"Cannot read ELF header: %s\n",
284 die(
"No ELF magic\n");
287 die(
"Not a 32 bit executable\n");
290 die(
"Not a LSB ELF executable\n");
293 die(
"Unknown ELF version\n");
311 die(
"Unsupported ELF header type\n");
314 die(
"Not for x86\n");
317 die(
"Unknown ELF version\n");
320 die(
"Bad Elf header size\n");
323 die(
"Bad program header entry\n");
326 die(
"Bad section header entry\n");
329 die(
"String table index out of bounds\n");
333 static void read_shdrs(FILE *fp)
340 die(
"Unable to allocate %d section headers\n",
344 die(
"Seek to %d failed: %s\n",
347 for (i = 0; i < ehdr.
e_shnum; i++) {
349 if (fread(&shdr,
sizeof shdr, 1, fp) != 1)
350 die(
"Cannot read ELF section headers %d/%d: %s\n",
368 static void read_strtabs(FILE *fp)
371 for (i = 0; i < ehdr.
e_shnum; i++) {
378 die(
"malloc of %d bytes for strtab failed\n",
382 die(
"Seek to %d failed: %s\n",
387 die(
"Cannot read symbol table: %s\n",
393 static void read_symtabs(FILE *fp)
396 for (i = 0; i < ehdr.
e_shnum; i++) {
403 die(
"malloc of %d bytes for symtab failed\n",
407 die(
"Seek to %d failed: %s\n",
412 die(
"Cannot read symbol table: %s\n",
426 static void read_relocs(FILE *fp)
429 for (i = 0; i < ehdr.
e_shnum; i++) {
436 die(
"malloc of %d bytes for relocs failed\n",
440 die(
"Seek to %d failed: %s\n",
445 die(
"Cannot read symbol table: %s\n",
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++) {
470 sym_strtab = sec->
link->strtab;
479 printf(
"%5d %08x %5d %10s %10s %12s %s\n",
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++) {
496 struct section *sec_applies, *sec_symtab;
503 sec_symtab = sec->
link;
508 sh_symtab = sec_symtab->
symtab;
509 sym_strtab = sec_symtab->
link->strtab;
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",
563 for (i = 0; i < ehdr.
e_shnum; i++) {
566 struct section *sec_applies, *sec_symtab;
573 sec_symtab = sec->
link;
578 sh_symtab = sec_symtab->
symtab;
579 sym_strtab = sec_symtab->
link->strtab;
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);
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];
679 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
682 static void emit_relocs(
int as_text,
int use_real_mode)
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",
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");
707 qsort(relocs, reloc_count,
sizeof(relocs[0]), cmp_relocs);
708 qsort(relocs16, reloc16_count,
sizeof(relocs16[0]), cmp_relocs);
715 printf(
".section \".data.reloc\",\"a\"\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);
743 for (i = 0; i < reloc_count; i++)
744 write32(relocs[i], stdout);
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");
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",
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);