14 static void die(char *fmt
, ...);
16 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
17 static Elf32_Ehdr ehdr
;
18 static unsigned long reloc_count
, reloc_idx
;
19 static unsigned long *relocs
;
28 static struct section
*secs
;
31 * Following symbols have been audited. There values are constant and do
32 * not change if bzImage is loaded at a different physical address than
33 * the address for which it has been compiled. Don't warn user about
34 * absolute relocations present w.r.t these symbols.
36 static const char abs_sym_regex
[] =
37 "^(xen_irq_disable_direct_reloc$|"
38 "xen_save_fl_direct_reloc$|"
41 static regex_t abs_sym_regex_c
;
42 static int is_abs_reloc(const char *sym_name
)
44 return !regexec(&abs_sym_regex_c
, sym_name
, 0, NULL
, 0);
48 * These symbols are known to be relative, even if the linker marks them
49 * as absolute (typically defined outside any section in the linker script.)
51 static const char rel_sym_regex
[] =
53 static regex_t rel_sym_regex_c
;
54 static int is_rel_reloc(const char *sym_name
)
56 return !regexec(&rel_sym_regex_c
, sym_name
, 0, NULL
, 0);
59 static void regex_init(void)
64 err
= regcomp(&abs_sym_regex_c
, abs_sym_regex
,
65 REG_EXTENDED
|REG_NOSUB
);
67 regerror(err
, &abs_sym_regex_c
, errbuf
, sizeof errbuf
);
71 err
= regcomp(&rel_sym_regex_c
, rel_sym_regex
,
72 REG_EXTENDED
|REG_NOSUB
);
74 regerror(err
, &rel_sym_regex_c
, errbuf
, sizeof errbuf
);
79 static void die(char *fmt
, ...)
83 vfprintf(stderr
, fmt
, ap
);
88 static const char *sym_type(unsigned type
)
90 static const char *type_name
[] = {
91 #define SYM_TYPE(X) [X] = #X
95 SYM_TYPE(STT_SECTION
),
101 const char *name
= "unknown sym type name";
102 if (type
< ARRAY_SIZE(type_name
)) {
103 name
= type_name
[type
];
108 static const char *sym_bind(unsigned bind
)
110 static const char *bind_name
[] = {
111 #define SYM_BIND(X) [X] = #X
113 SYM_BIND(STB_GLOBAL
),
117 const char *name
= "unknown sym bind name";
118 if (bind
< ARRAY_SIZE(bind_name
)) {
119 name
= bind_name
[bind
];
124 static const char *sym_visibility(unsigned visibility
)
126 static const char *visibility_name
[] = {
127 #define SYM_VISIBILITY(X) [X] = #X
128 SYM_VISIBILITY(STV_DEFAULT
),
129 SYM_VISIBILITY(STV_INTERNAL
),
130 SYM_VISIBILITY(STV_HIDDEN
),
131 SYM_VISIBILITY(STV_PROTECTED
),
132 #undef SYM_VISIBILITY
134 const char *name
= "unknown sym visibility name";
135 if (visibility
< ARRAY_SIZE(visibility_name
)) {
136 name
= visibility_name
[visibility
];
141 static const char *rel_type(unsigned type
)
143 static const char *type_name
[] = {
144 #define REL_TYPE(X) [X] = #X
145 REL_TYPE(R_386_NONE
),
147 REL_TYPE(R_386_PC32
),
148 REL_TYPE(R_386_GOT32
),
149 REL_TYPE(R_386_PLT32
),
150 REL_TYPE(R_386_COPY
),
151 REL_TYPE(R_386_GLOB_DAT
),
152 REL_TYPE(R_386_JMP_SLOT
),
153 REL_TYPE(R_386_RELATIVE
),
154 REL_TYPE(R_386_GOTOFF
),
155 REL_TYPE(R_386_GOTPC
),
158 const char *name
= "unknown type rel type name";
159 if (type
< ARRAY_SIZE(type_name
) && type_name
[type
]) {
160 name
= type_name
[type
];
165 static const char *sec_name(unsigned shndx
)
167 const char *sec_strtab
;
169 sec_strtab
= secs
[ehdr
.e_shstrndx
].strtab
;
171 if (shndx
< ehdr
.e_shnum
) {
172 name
= sec_strtab
+ secs
[shndx
].shdr
.sh_name
;
174 else if (shndx
== SHN_ABS
) {
177 else if (shndx
== SHN_COMMON
) {
183 static const char *sym_name(const char *sym_strtab
, Elf32_Sym
*sym
)
188 name
= sym_strtab
+ sym
->st_name
;
191 name
= sec_name(secs
[sym
->st_shndx
].shdr
.sh_name
);
198 #if BYTE_ORDER == LITTLE_ENDIAN
199 #define le16_to_cpu(val) (val)
200 #define le32_to_cpu(val) (val)
202 #if BYTE_ORDER == BIG_ENDIAN
203 #define le16_to_cpu(val) bswap_16(val)
204 #define le32_to_cpu(val) bswap_32(val)
207 static uint16_t elf16_to_cpu(uint16_t val
)
209 return le16_to_cpu(val
);
212 static uint32_t elf32_to_cpu(uint32_t val
)
214 return le32_to_cpu(val
);
217 static void read_ehdr(FILE *fp
)
219 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1) {
220 die("Cannot read ELF header: %s\n",
223 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0) {
224 die("No ELF magic\n");
226 if (ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) {
227 die("Not a 32 bit executable\n");
229 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
) {
230 die("Not a LSB ELF executable\n");
232 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
233 die("Unknown ELF version\n");
235 /* Convert the fields to native endian */
236 ehdr
.e_type
= elf16_to_cpu(ehdr
.e_type
);
237 ehdr
.e_machine
= elf16_to_cpu(ehdr
.e_machine
);
238 ehdr
.e_version
= elf32_to_cpu(ehdr
.e_version
);
239 ehdr
.e_entry
= elf32_to_cpu(ehdr
.e_entry
);
240 ehdr
.e_phoff
= elf32_to_cpu(ehdr
.e_phoff
);
241 ehdr
.e_shoff
= elf32_to_cpu(ehdr
.e_shoff
);
242 ehdr
.e_flags
= elf32_to_cpu(ehdr
.e_flags
);
243 ehdr
.e_ehsize
= elf16_to_cpu(ehdr
.e_ehsize
);
244 ehdr
.e_phentsize
= elf16_to_cpu(ehdr
.e_phentsize
);
245 ehdr
.e_phnum
= elf16_to_cpu(ehdr
.e_phnum
);
246 ehdr
.e_shentsize
= elf16_to_cpu(ehdr
.e_shentsize
);
247 ehdr
.e_shnum
= elf16_to_cpu(ehdr
.e_shnum
);
248 ehdr
.e_shstrndx
= elf16_to_cpu(ehdr
.e_shstrndx
);
250 if ((ehdr
.e_type
!= ET_EXEC
) && (ehdr
.e_type
!= ET_DYN
)) {
251 die("Unsupported ELF header type\n");
253 if (ehdr
.e_machine
!= EM_386
) {
254 die("Not for x86\n");
256 if (ehdr
.e_version
!= EV_CURRENT
) {
257 die("Unknown ELF version\n");
259 if (ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
)) {
260 die("Bad Elf header size\n");
262 if (ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
)) {
263 die("Bad program header entry\n");
265 if (ehdr
.e_shentsize
!= sizeof(Elf32_Shdr
)) {
266 die("Bad section header entry\n");
268 if (ehdr
.e_shstrndx
>= ehdr
.e_shnum
) {
269 die("String table index out of bounds\n");
273 static void read_shdrs(FILE *fp
)
278 secs
= calloc(ehdr
.e_shnum
, sizeof(struct section
));
280 die("Unable to allocate %d section headers\n",
283 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0) {
284 die("Seek to %d failed: %s\n",
285 ehdr
.e_shoff
, strerror(errno
));
287 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
288 struct section
*sec
= &secs
[i
];
289 if (fread(&shdr
, sizeof shdr
, 1, fp
) != 1)
290 die("Cannot read ELF section headers %d/%d: %s\n",
291 i
, ehdr
.e_shnum
, strerror(errno
));
292 sec
->shdr
.sh_name
= elf32_to_cpu(shdr
.sh_name
);
293 sec
->shdr
.sh_type
= elf32_to_cpu(shdr
.sh_type
);
294 sec
->shdr
.sh_flags
= elf32_to_cpu(shdr
.sh_flags
);
295 sec
->shdr
.sh_addr
= elf32_to_cpu(shdr
.sh_addr
);
296 sec
->shdr
.sh_offset
= elf32_to_cpu(shdr
.sh_offset
);
297 sec
->shdr
.sh_size
= elf32_to_cpu(shdr
.sh_size
);
298 sec
->shdr
.sh_link
= elf32_to_cpu(shdr
.sh_link
);
299 sec
->shdr
.sh_info
= elf32_to_cpu(shdr
.sh_info
);
300 sec
->shdr
.sh_addralign
= elf32_to_cpu(shdr
.sh_addralign
);
301 sec
->shdr
.sh_entsize
= elf32_to_cpu(shdr
.sh_entsize
);
302 if (sec
->shdr
.sh_link
< ehdr
.e_shnum
)
303 sec
->link
= &secs
[sec
->shdr
.sh_link
];
308 static void read_strtabs(FILE *fp
)
311 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
312 struct section
*sec
= &secs
[i
];
313 if (sec
->shdr
.sh_type
!= SHT_STRTAB
) {
316 sec
->strtab
= malloc(sec
->shdr
.sh_size
);
318 die("malloc of %d bytes for strtab failed\n",
321 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
322 die("Seek to %d failed: %s\n",
323 sec
->shdr
.sh_offset
, strerror(errno
));
325 if (fread(sec
->strtab
, 1, sec
->shdr
.sh_size
, fp
)
326 != sec
->shdr
.sh_size
) {
327 die("Cannot read symbol table: %s\n",
333 static void read_symtabs(FILE *fp
)
336 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
337 struct section
*sec
= &secs
[i
];
338 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
341 sec
->symtab
= malloc(sec
->shdr
.sh_size
);
343 die("malloc of %d bytes for symtab failed\n",
346 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
347 die("Seek to %d failed: %s\n",
348 sec
->shdr
.sh_offset
, strerror(errno
));
350 if (fread(sec
->symtab
, 1, sec
->shdr
.sh_size
, fp
)
351 != sec
->shdr
.sh_size
) {
352 die("Cannot read symbol table: %s\n",
355 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
356 Elf32_Sym
*sym
= &sec
->symtab
[j
];
357 sym
->st_name
= elf32_to_cpu(sym
->st_name
);
358 sym
->st_value
= elf32_to_cpu(sym
->st_value
);
359 sym
->st_size
= elf32_to_cpu(sym
->st_size
);
360 sym
->st_shndx
= elf16_to_cpu(sym
->st_shndx
);
366 static void read_relocs(FILE *fp
)
369 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
370 struct section
*sec
= &secs
[i
];
371 if (sec
->shdr
.sh_type
!= SHT_REL
) {
374 sec
->reltab
= malloc(sec
->shdr
.sh_size
);
376 die("malloc of %d bytes for relocs failed\n",
379 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
380 die("Seek to %d failed: %s\n",
381 sec
->shdr
.sh_offset
, strerror(errno
));
383 if (fread(sec
->reltab
, 1, sec
->shdr
.sh_size
, fp
)
384 != sec
->shdr
.sh_size
) {
385 die("Cannot read symbol table: %s\n",
388 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
389 Elf32_Rel
*rel
= &sec
->reltab
[j
];
390 rel
->r_offset
= elf32_to_cpu(rel
->r_offset
);
391 rel
->r_info
= elf32_to_cpu(rel
->r_info
);
397 static void print_absolute_symbols(void)
400 printf("Absolute symbols\n");
401 printf(" Num: Value Size Type Bind Visibility Name\n");
402 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
403 struct section
*sec
= &secs
[i
];
407 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
410 sym_strtab
= sec
->link
->strtab
;
411 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Sym
); j
++) {
414 sym
= &sec
->symtab
[j
];
415 name
= sym_name(sym_strtab
, sym
);
416 if (sym
->st_shndx
!= SHN_ABS
) {
419 printf("%5d %08x %5d %10s %10s %12s %s\n",
420 j
, sym
->st_value
, sym
->st_size
,
421 sym_type(ELF32_ST_TYPE(sym
->st_info
)),
422 sym_bind(ELF32_ST_BIND(sym
->st_info
)),
423 sym_visibility(ELF32_ST_VISIBILITY(sym
->st_other
)),
430 static void print_absolute_relocs(void)
434 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
435 struct section
*sec
= &secs
[i
];
436 struct section
*sec_applies
, *sec_symtab
;
438 Elf32_Sym
*sh_symtab
;
440 if (sec
->shdr
.sh_type
!= SHT_REL
) {
443 sec_symtab
= sec
->link
;
444 sec_applies
= &secs
[sec
->shdr
.sh_info
];
445 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
448 sh_symtab
= sec_symtab
->symtab
;
449 sym_strtab
= sec_symtab
->link
->strtab
;
450 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
454 rel
= &sec
->reltab
[j
];
455 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
456 name
= sym_name(sym_strtab
, sym
);
457 if (sym
->st_shndx
!= SHN_ABS
) {
461 /* Absolute symbols are not relocated if bzImage is
462 * loaded at a non-compiled address. Display a warning
463 * to user at compile time about the absolute
464 * relocations present.
466 * User need to audit the code to make sure
467 * some symbols which should have been section
468 * relative have not become absolute because of some
469 * linker optimization or wrong programming usage.
471 * Before warning check if this absolute symbol
472 * relocation is harmless.
474 if (is_abs_reloc(name
) || is_rel_reloc(name
))
478 printf("WARNING: Absolute relocations"
480 printf("Offset Info Type Sym.Value "
485 printf("%08x %08x %10s %08x %s\n",
488 rel_type(ELF32_R_TYPE(rel
->r_info
)),
498 static void walk_relocs(void (*visit
)(Elf32_Rel
*rel
, Elf32_Sym
*sym
))
501 /* Walk through the relocations */
502 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
504 Elf32_Sym
*sh_symtab
;
505 struct section
*sec_applies
, *sec_symtab
;
507 struct section
*sec
= &secs
[i
];
509 if (sec
->shdr
.sh_type
!= SHT_REL
) {
512 sec_symtab
= sec
->link
;
513 sec_applies
= &secs
[sec
->shdr
.sh_info
];
514 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
517 sh_symtab
= sec_symtab
->symtab
;
518 sym_strtab
= sec_symtab
->link
->strtab
;
519 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf32_Rel
); j
++) {
523 rel
= &sec
->reltab
[j
];
524 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
525 r_type
= ELF32_R_TYPE(rel
->r_info
);
526 /* Don't visit relocations to absolute symbols */
527 if (sym
->st_shndx
== SHN_ABS
&&
528 !is_rel_reloc(sym_name(sym_strtab
, sym
))) {
535 * NONE can be ignored and and PC relative
536 * relocations don't need to be adjusted.
540 /* Visit relocations that need to be adjusted */
544 die("Unsupported relocation type: %s (%d)\n",
545 rel_type(r_type
), r_type
);
552 static void count_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
557 static void collect_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
559 /* Remember the address that needs to be adjusted. */
560 relocs
[reloc_idx
++] = rel
->r_offset
;
563 static int cmp_relocs(const void *va
, const void *vb
)
565 const unsigned long *a
, *b
;
567 return (*a
== *b
)? 0 : (*a
> *b
)? 1 : -1;
570 static void emit_relocs(int as_text
)
573 /* Count how many relocations I have and allocate space for them. */
575 walk_relocs(count_reloc
);
576 relocs
= malloc(reloc_count
* sizeof(relocs
[0]));
578 die("malloc of %d entries for relocs failed\n",
581 /* Collect up the relocations */
583 walk_relocs(collect_reloc
);
585 /* Order the relocations for more efficient processing */
586 qsort(relocs
, reloc_count
, sizeof(relocs
[0]), cmp_relocs
);
588 /* Print the relocations */
590 /* Print the relocations in a form suitable that
593 printf(".section \".data.reloc\",\"a\"\n");
594 printf(".balign 4\n");
595 for (i
= 0; i
< reloc_count
; i
++) {
596 printf("\t .long 0x%08lx\n", relocs
[i
]);
601 unsigned char buf
[4];
603 fwrite("\0\0\0\0", 4, 1, stdout
);
604 /* Now print each relocation */
605 for (i
= 0; i
< reloc_count
; i
++) {
606 buf
[0] = (relocs
[i
] >> 0) & 0xff;
607 buf
[1] = (relocs
[i
] >> 8) & 0xff;
608 buf
[2] = (relocs
[i
] >> 16) & 0xff;
609 buf
[3] = (relocs
[i
] >> 24) & 0xff;
610 fwrite(buf
, 4, 1, stdout
);
615 static void usage(void)
617 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
620 int main(int argc
, char **argv
)
622 int show_absolute_syms
, show_absolute_relocs
;
630 show_absolute_syms
= 0;
631 show_absolute_relocs
= 0;
634 for (i
= 1; i
< argc
; i
++) {
637 if (strcmp(argv
[1], "--abs-syms") == 0) {
638 show_absolute_syms
= 1;
642 if (strcmp(argv
[1], "--abs-relocs") == 0) {
643 show_absolute_relocs
= 1;
646 else if (strcmp(argv
[1], "--text") == 0) {
660 fp
= fopen(fname
, "r");
662 die("Cannot open %s: %s\n",
663 fname
, strerror(errno
));
670 if (show_absolute_syms
) {
671 print_absolute_symbols();
674 if (show_absolute_relocs
) {
675 print_absolute_relocs();
678 emit_relocs(as_text
);