14 static Elf32_Ehdr ehdr
;
15 static Elf32_Shdr shdr
[MAX_SHDRS
];
16 static Elf32_Sym
*symtab
[MAX_SHDRS
];
17 static Elf32_Rel
*reltab
[MAX_SHDRS
];
18 static char *strtab
[MAX_SHDRS
];
19 static unsigned long reloc_count
, reloc_idx
;
20 static unsigned long *relocs
;
22 static void die(char *fmt
, ...)
26 vfprintf(stderr
, fmt
, ap
);
31 static const char *sym_type(unsigned type
)
33 static const char *type_name
[] = {
34 #define SYM_TYPE(X) [X] = #X
38 SYM_TYPE(STT_SECTION
),
44 const char *name
= "unknown sym type name";
45 if (type
< sizeof(type_name
)/sizeof(type_name
[0])) {
46 name
= type_name
[type
];
51 static const char *sym_bind(unsigned bind
)
53 static const char *bind_name
[] = {
54 #define SYM_BIND(X) [X] = #X
60 const char *name
= "unknown sym bind name";
61 if (bind
< sizeof(bind_name
)/sizeof(bind_name
[0])) {
62 name
= bind_name
[bind
];
67 static const char *sym_visibility(unsigned visibility
)
69 static const char *visibility_name
[] = {
70 #define SYM_VISIBILITY(X) [X] = #X
71 SYM_VISIBILITY(STV_DEFAULT
),
72 SYM_VISIBILITY(STV_INTERNAL
),
73 SYM_VISIBILITY(STV_HIDDEN
),
74 SYM_VISIBILITY(STV_PROTECTED
),
77 const char *name
= "unknown sym visibility name";
78 if (visibility
< sizeof(visibility_name
)/sizeof(visibility_name
[0])) {
79 name
= visibility_name
[visibility
];
84 static const char *rel_type(unsigned type
)
86 static const char *type_name
[] = {
87 #define REL_TYPE(X) [X] = #X
91 REL_TYPE(R_386_GOT32
),
92 REL_TYPE(R_386_PLT32
),
94 REL_TYPE(R_386_GLOB_DAT
),
95 REL_TYPE(R_386_JMP_SLOT
),
96 REL_TYPE(R_386_RELATIVE
),
97 REL_TYPE(R_386_GOTOFF
),
98 REL_TYPE(R_386_GOTPC
),
101 const char *name
= "unknown type rel type name";
102 if (type
< sizeof(type_name
)/sizeof(type_name
[0])) {
103 name
= type_name
[type
];
108 static const char *sec_name(unsigned shndx
)
110 const char *sec_strtab
;
112 sec_strtab
= strtab
[ehdr
.e_shstrndx
];
114 if (shndx
< ehdr
.e_shnum
) {
115 name
= sec_strtab
+ shdr
[shndx
].sh_name
;
117 else if (shndx
== SHN_ABS
) {
120 else if (shndx
== SHN_COMMON
) {
126 static const char *sym_name(const char *sym_strtab
, Elf32_Sym
*sym
)
131 name
= sym_strtab
+ sym
->st_name
;
134 name
= sec_name(shdr
[sym
->st_shndx
].sh_name
);
141 #if BYTE_ORDER == LITTLE_ENDIAN
142 #define le16_to_cpu(val) (val)
143 #define le32_to_cpu(val) (val)
145 #if BYTE_ORDER == BIG_ENDIAN
146 #define le16_to_cpu(val) bswap_16(val)
147 #define le32_to_cpu(val) bswap_32(val)
150 static uint16_t elf16_to_cpu(uint16_t val
)
152 return le16_to_cpu(val
);
155 static uint32_t elf32_to_cpu(uint32_t val
)
157 return le32_to_cpu(val
);
160 static void read_ehdr(FILE *fp
)
162 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1) {
163 die("Cannot read ELF header: %s\n",
166 if (memcmp(ehdr
.e_ident
, ELFMAG
, 4) != 0) {
167 die("No ELF magic\n");
169 if (ehdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) {
170 die("Not a 32 bit executable\n");
172 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
) {
173 die("Not a LSB ELF executable\n");
175 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
176 die("Unknown ELF version\n");
178 /* Convert the fields to native endian */
179 ehdr
.e_type
= elf16_to_cpu(ehdr
.e_type
);
180 ehdr
.e_machine
= elf16_to_cpu(ehdr
.e_machine
);
181 ehdr
.e_version
= elf32_to_cpu(ehdr
.e_version
);
182 ehdr
.e_entry
= elf32_to_cpu(ehdr
.e_entry
);
183 ehdr
.e_phoff
= elf32_to_cpu(ehdr
.e_phoff
);
184 ehdr
.e_shoff
= elf32_to_cpu(ehdr
.e_shoff
);
185 ehdr
.e_flags
= elf32_to_cpu(ehdr
.e_flags
);
186 ehdr
.e_ehsize
= elf16_to_cpu(ehdr
.e_ehsize
);
187 ehdr
.e_phentsize
= elf16_to_cpu(ehdr
.e_phentsize
);
188 ehdr
.e_phnum
= elf16_to_cpu(ehdr
.e_phnum
);
189 ehdr
.e_shentsize
= elf16_to_cpu(ehdr
.e_shentsize
);
190 ehdr
.e_shnum
= elf16_to_cpu(ehdr
.e_shnum
);
191 ehdr
.e_shstrndx
= elf16_to_cpu(ehdr
.e_shstrndx
);
193 if ((ehdr
.e_type
!= ET_EXEC
) && (ehdr
.e_type
!= ET_DYN
)) {
194 die("Unsupported ELF header type\n");
196 if (ehdr
.e_machine
!= EM_386
) {
197 die("Not for x86\n");
199 if (ehdr
.e_version
!= EV_CURRENT
) {
200 die("Unknown ELF version\n");
202 if (ehdr
.e_ehsize
!= sizeof(Elf32_Ehdr
)) {
203 die("Bad Elf header size\n");
205 if (ehdr
.e_phentsize
!= sizeof(Elf32_Phdr
)) {
206 die("Bad program header entry\n");
208 if (ehdr
.e_shentsize
!= sizeof(Elf32_Shdr
)) {
209 die("Bad section header entry\n");
211 if (ehdr
.e_shstrndx
>= ehdr
.e_shnum
) {
212 die("String table index out of bounds\n");
216 static void read_shdrs(FILE *fp
)
219 if (ehdr
.e_shnum
> MAX_SHDRS
) {
220 die("%d section headers supported: %d\n",
221 ehdr
.e_shnum
, MAX_SHDRS
);
223 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0) {
224 die("Seek to %d failed: %s\n",
225 ehdr
.e_shoff
, strerror(errno
));
227 if (fread(&shdr
, sizeof(shdr
[0]), ehdr
.e_shnum
, fp
) != ehdr
.e_shnum
) {
228 die("Cannot read ELF section headers: %s\n",
231 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
232 shdr
[i
].sh_name
= elf32_to_cpu(shdr
[i
].sh_name
);
233 shdr
[i
].sh_type
= elf32_to_cpu(shdr
[i
].sh_type
);
234 shdr
[i
].sh_flags
= elf32_to_cpu(shdr
[i
].sh_flags
);
235 shdr
[i
].sh_addr
= elf32_to_cpu(shdr
[i
].sh_addr
);
236 shdr
[i
].sh_offset
= elf32_to_cpu(shdr
[i
].sh_offset
);
237 shdr
[i
].sh_size
= elf32_to_cpu(shdr
[i
].sh_size
);
238 shdr
[i
].sh_link
= elf32_to_cpu(shdr
[i
].sh_link
);
239 shdr
[i
].sh_info
= elf32_to_cpu(shdr
[i
].sh_info
);
240 shdr
[i
].sh_addralign
= elf32_to_cpu(shdr
[i
].sh_addralign
);
241 shdr
[i
].sh_entsize
= elf32_to_cpu(shdr
[i
].sh_entsize
);
246 static void read_strtabs(FILE *fp
)
249 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
250 if (shdr
[i
].sh_type
!= SHT_STRTAB
) {
253 strtab
[i
] = malloc(shdr
[i
].sh_size
);
255 die("malloc of %d bytes for strtab failed\n",
258 if (fseek(fp
, shdr
[i
].sh_offset
, SEEK_SET
) < 0) {
259 die("Seek to %d failed: %s\n",
260 shdr
[i
].sh_offset
, strerror(errno
));
262 if (fread(strtab
[i
], 1, shdr
[i
].sh_size
, fp
) != shdr
[i
].sh_size
) {
263 die("Cannot read symbol table: %s\n",
269 static void read_symtabs(FILE *fp
)
272 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
273 if (shdr
[i
].sh_type
!= SHT_SYMTAB
) {
276 symtab
[i
] = malloc(shdr
[i
].sh_size
);
278 die("malloc of %d bytes for symtab failed\n",
281 if (fseek(fp
, shdr
[i
].sh_offset
, SEEK_SET
) < 0) {
282 die("Seek to %d failed: %s\n",
283 shdr
[i
].sh_offset
, strerror(errno
));
285 if (fread(symtab
[i
], 1, shdr
[i
].sh_size
, fp
) != shdr
[i
].sh_size
) {
286 die("Cannot read symbol table: %s\n",
289 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(symtab
[i
][0]); j
++) {
290 symtab
[i
][j
].st_name
= elf32_to_cpu(symtab
[i
][j
].st_name
);
291 symtab
[i
][j
].st_value
= elf32_to_cpu(symtab
[i
][j
].st_value
);
292 symtab
[i
][j
].st_size
= elf32_to_cpu(symtab
[i
][j
].st_size
);
293 symtab
[i
][j
].st_shndx
= elf16_to_cpu(symtab
[i
][j
].st_shndx
);
299 static void read_relocs(FILE *fp
)
302 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
303 if (shdr
[i
].sh_type
!= SHT_REL
) {
306 reltab
[i
] = malloc(shdr
[i
].sh_size
);
308 die("malloc of %d bytes for relocs failed\n",
311 if (fseek(fp
, shdr
[i
].sh_offset
, SEEK_SET
) < 0) {
312 die("Seek to %d failed: %s\n",
313 shdr
[i
].sh_offset
, strerror(errno
));
315 if (fread(reltab
[i
], 1, shdr
[i
].sh_size
, fp
) != shdr
[i
].sh_size
) {
316 die("Cannot read symbol table: %s\n",
319 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(reltab
[0][0]); j
++) {
320 reltab
[i
][j
].r_offset
= elf32_to_cpu(reltab
[i
][j
].r_offset
);
321 reltab
[i
][j
].r_info
= elf32_to_cpu(reltab
[i
][j
].r_info
);
327 static void print_absolute_symbols(void)
330 printf("Absolute symbols\n");
331 printf(" Num: Value Size Type Bind Visibility Name\n");
332 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
334 Elf32_Sym
*sh_symtab
;
336 if (shdr
[i
].sh_type
!= SHT_SYMTAB
) {
339 sh_symtab
= symtab
[i
];
340 sym_strtab
= strtab
[shdr
[i
].sh_link
];
341 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(symtab
[0][0]); j
++) {
345 name
= sym_name(sym_strtab
, sym
);
346 if (sym
->st_shndx
!= SHN_ABS
) {
349 printf("%5d %08x %5d %10s %10s %12s %s\n",
350 j
, sym
->st_value
, sym
->st_size
,
351 sym_type(ELF32_ST_TYPE(sym
->st_info
)),
352 sym_bind(ELF32_ST_BIND(sym
->st_info
)),
353 sym_visibility(ELF32_ST_VISIBILITY(sym
->st_other
)),
360 static void print_absolute_relocs(void)
363 printf("Absolute relocations\n");
364 printf("Offset Info Type Sym.Value Sym.Name\n");
365 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
367 Elf32_Sym
*sh_symtab
;
368 unsigned sec_applies
, sec_symtab
;
370 if (shdr
[i
].sh_type
!= SHT_REL
) {
373 sec_symtab
= shdr
[i
].sh_link
;
374 sec_applies
= shdr
[i
].sh_info
;
375 if (!(shdr
[sec_applies
].sh_flags
& SHF_ALLOC
)) {
378 sh_symtab
= symtab
[sec_symtab
];
379 sym_strtab
= strtab
[shdr
[sec_symtab
].sh_link
];
380 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(reltab
[0][0]); j
++) {
385 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
386 name
= sym_name(sym_strtab
, sym
);
387 if (sym
->st_shndx
!= SHN_ABS
) {
390 printf("%08x %08x %10s %08x %s\n",
393 rel_type(ELF32_R_TYPE(rel
->r_info
)),
401 static void walk_relocs(void (*visit
)(Elf32_Rel
*rel
, Elf32_Sym
*sym
))
404 /* Walk through the relocations */
405 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
407 Elf32_Sym
*sh_symtab
;
408 unsigned sec_applies
, sec_symtab
;
410 if (shdr
[i
].sh_type
!= SHT_REL
) {
413 sec_symtab
= shdr
[i
].sh_link
;
414 sec_applies
= shdr
[i
].sh_info
;
415 if (!(shdr
[sec_applies
].sh_flags
& SHF_ALLOC
)) {
418 sh_symtab
= symtab
[sec_symtab
];
419 sym_strtab
= strtab
[shdr
[sec_symtab
].sh_link
];
420 for(j
= 0; j
< shdr
[i
].sh_size
/sizeof(reltab
[0][0]); j
++) {
425 sym
= &sh_symtab
[ELF32_R_SYM(rel
->r_info
)];
426 r_type
= ELF32_R_TYPE(rel
->r_info
);
427 /* Don't visit relocations to absolute symbols */
428 if (sym
->st_shndx
== SHN_ABS
) {
431 if (r_type
== R_386_PC32
) {
432 /* PC relative relocations don't need to be adjusted */
434 else if (r_type
== R_386_32
) {
435 /* Visit relocations that need to be adjusted */
439 die("Unsupported relocation type: %d\n", r_type
);
445 static void count_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
450 static void collect_reloc(Elf32_Rel
*rel
, Elf32_Sym
*sym
)
452 /* Remember the address that needs to be adjusted. */
453 relocs
[reloc_idx
++] = rel
->r_offset
;
456 static int cmp_relocs(const void *va
, const void *vb
)
458 const unsigned long *a
, *b
;
460 return (*a
== *b
)? 0 : (*a
> *b
)? 1 : -1;
463 static void emit_relocs(int as_text
)
466 /* Count how many relocations I have and allocate space for them. */
468 walk_relocs(count_reloc
);
469 relocs
= malloc(reloc_count
* sizeof(relocs
[0]));
471 die("malloc of %d entries for relocs failed\n",
474 /* Collect up the relocations */
476 walk_relocs(collect_reloc
);
478 /* Order the relocations for more efficient processing */
479 qsort(relocs
, reloc_count
, sizeof(relocs
[0]), cmp_relocs
);
481 /* Print the relocations */
483 /* Print the relocations in a form suitable that
486 printf(".section \".data.reloc\",\"a\"\n");
487 printf(".balign 4\n");
488 for(i
= 0; i
< reloc_count
; i
++) {
489 printf("\t .long 0x%08lx\n", relocs
[i
]);
494 unsigned char buf
[4];
495 buf
[0] = buf
[1] = buf
[2] = buf
[3] = 0;
497 printf("%c%c%c%c", buf
[0], buf
[1], buf
[2], buf
[3]);
498 /* Now print each relocation */
499 for(i
= 0; i
< reloc_count
; i
++) {
500 buf
[0] = (relocs
[i
] >> 0) & 0xff;
501 buf
[1] = (relocs
[i
] >> 8) & 0xff;
502 buf
[2] = (relocs
[i
] >> 16) & 0xff;
503 buf
[3] = (relocs
[i
] >> 24) & 0xff;
504 printf("%c%c%c%c", buf
[0], buf
[1], buf
[2], buf
[3]);
509 static void usage(void)
511 die("i386_reloc [--abs | --text] vmlinux\n");
514 int main(int argc
, char **argv
)
525 for(i
= 1; i
< argc
; i
++) {
528 if (strcmp(argv
[1], "--abs") == 0) {
532 else if (strcmp(argv
[1], "--text") == 0) {
546 fp
= fopen(fname
, "r");
548 die("Cannot open %s: %s\n",
549 fname
, strerror(errno
));
557 print_absolute_symbols();
558 print_absolute_relocs();
561 emit_relocs(as_text
);