Commit | Line | Data |
---|---|---|
968de4f0 EB |
1 | #include <stdio.h> |
2 | #include <stdarg.h> | |
3 | #include <stdlib.h> | |
4 | #include <stdint.h> | |
5 | #include <string.h> | |
6 | #include <errno.h> | |
7 | #include <unistd.h> | |
8 | #include <elf.h> | |
9 | #include <byteswap.h> | |
10 | #define USE_BSD | |
11 | #include <endian.h> | |
12 | ||
13 | #define MAX_SHDRS 100 | |
ca820181 | 14 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
968de4f0 EB |
15 | static Elf32_Ehdr ehdr; |
16 | static Elf32_Shdr shdr[MAX_SHDRS]; | |
17 | static Elf32_Sym *symtab[MAX_SHDRS]; | |
18 | static Elf32_Rel *reltab[MAX_SHDRS]; | |
19 | static char *strtab[MAX_SHDRS]; | |
20 | static unsigned long reloc_count, reloc_idx; | |
21 | static unsigned long *relocs; | |
22 | ||
6a044b3a VG |
23 | /* |
24 | * Following symbols have been audited. There values are constant and do | |
25 | * not change if bzImage is loaded at a different physical address than | |
26 | * the address for which it has been compiled. Don't warn user about | |
27 | * absolute relocations present w.r.t these symbols. | |
28 | */ | |
29 | static const char* safe_abs_relocs[] = { | |
30 | "__kernel_vsyscall", | |
31 | "__kernel_rt_sigreturn", | |
32 | "__kernel_sigreturn", | |
33 | "SYSENTER_RETURN", | |
34 | }; | |
35 | ||
36 | static int is_safe_abs_reloc(const char* sym_name) | |
37 | { | |
38 | int i, array_size; | |
39 | ||
40 | array_size = sizeof(safe_abs_relocs)/sizeof(char*); | |
41 | ||
42 | for(i = 0; i < array_size; i++) { | |
43 | if (!strcmp(sym_name, safe_abs_relocs[i])) | |
44 | /* Match found */ | |
45 | return 1; | |
46 | } | |
2a3d4f1f AV |
47 | if (strncmp(sym_name, "__crc_", 6) == 0) |
48 | return 1; | |
6a044b3a VG |
49 | return 0; |
50 | } | |
51 | ||
968de4f0 EB |
52 | static void die(char *fmt, ...) |
53 | { | |
54 | va_list ap; | |
55 | va_start(ap, fmt); | |
56 | vfprintf(stderr, fmt, ap); | |
57 | va_end(ap); | |
58 | exit(1); | |
59 | } | |
60 | ||
61 | static const char *sym_type(unsigned type) | |
62 | { | |
63 | static const char *type_name[] = { | |
64 | #define SYM_TYPE(X) [X] = #X | |
65 | SYM_TYPE(STT_NOTYPE), | |
66 | SYM_TYPE(STT_OBJECT), | |
67 | SYM_TYPE(STT_FUNC), | |
68 | SYM_TYPE(STT_SECTION), | |
69 | SYM_TYPE(STT_FILE), | |
70 | SYM_TYPE(STT_COMMON), | |
71 | SYM_TYPE(STT_TLS), | |
72 | #undef SYM_TYPE | |
73 | }; | |
74 | const char *name = "unknown sym type name"; | |
ca820181 | 75 | if (type < ARRAY_SIZE(type_name)) { |
968de4f0 EB |
76 | name = type_name[type]; |
77 | } | |
78 | return name; | |
79 | } | |
80 | ||
81 | static const char *sym_bind(unsigned bind) | |
82 | { | |
83 | static const char *bind_name[] = { | |
84 | #define SYM_BIND(X) [X] = #X | |
85 | SYM_BIND(STB_LOCAL), | |
86 | SYM_BIND(STB_GLOBAL), | |
87 | SYM_BIND(STB_WEAK), | |
88 | #undef SYM_BIND | |
89 | }; | |
90 | const char *name = "unknown sym bind name"; | |
ca820181 | 91 | if (bind < ARRAY_SIZE(bind_name)) { |
968de4f0 EB |
92 | name = bind_name[bind]; |
93 | } | |
94 | return name; | |
95 | } | |
96 | ||
97 | static const char *sym_visibility(unsigned visibility) | |
98 | { | |
99 | static const char *visibility_name[] = { | |
100 | #define SYM_VISIBILITY(X) [X] = #X | |
101 | SYM_VISIBILITY(STV_DEFAULT), | |
102 | SYM_VISIBILITY(STV_INTERNAL), | |
103 | SYM_VISIBILITY(STV_HIDDEN), | |
104 | SYM_VISIBILITY(STV_PROTECTED), | |
105 | #undef SYM_VISIBILITY | |
106 | }; | |
107 | const char *name = "unknown sym visibility name"; | |
ca820181 | 108 | if (visibility < ARRAY_SIZE(visibility_name)) { |
968de4f0 EB |
109 | name = visibility_name[visibility]; |
110 | } | |
111 | return name; | |
112 | } | |
113 | ||
114 | static const char *rel_type(unsigned type) | |
115 | { | |
116 | static const char *type_name[] = { | |
117 | #define REL_TYPE(X) [X] = #X | |
118 | REL_TYPE(R_386_NONE), | |
119 | REL_TYPE(R_386_32), | |
120 | REL_TYPE(R_386_PC32), | |
121 | REL_TYPE(R_386_GOT32), | |
122 | REL_TYPE(R_386_PLT32), | |
123 | REL_TYPE(R_386_COPY), | |
124 | REL_TYPE(R_386_GLOB_DAT), | |
125 | REL_TYPE(R_386_JMP_SLOT), | |
126 | REL_TYPE(R_386_RELATIVE), | |
127 | REL_TYPE(R_386_GOTOFF), | |
128 | REL_TYPE(R_386_GOTPC), | |
129 | #undef REL_TYPE | |
130 | }; | |
131 | const char *name = "unknown type rel type name"; | |
ca820181 | 132 | if (type < ARRAY_SIZE(type_name)) { |
968de4f0 EB |
133 | name = type_name[type]; |
134 | } | |
135 | return name; | |
136 | } | |
137 | ||
138 | static const char *sec_name(unsigned shndx) | |
139 | { | |
140 | const char *sec_strtab; | |
141 | const char *name; | |
142 | sec_strtab = strtab[ehdr.e_shstrndx]; | |
143 | name = "<noname>"; | |
144 | if (shndx < ehdr.e_shnum) { | |
145 | name = sec_strtab + shdr[shndx].sh_name; | |
146 | } | |
147 | else if (shndx == SHN_ABS) { | |
148 | name = "ABSOLUTE"; | |
149 | } | |
150 | else if (shndx == SHN_COMMON) { | |
151 | name = "COMMON"; | |
152 | } | |
153 | return name; | |
154 | } | |
155 | ||
156 | static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym) | |
157 | { | |
158 | const char *name; | |
159 | name = "<noname>"; | |
160 | if (sym->st_name) { | |
161 | name = sym_strtab + sym->st_name; | |
162 | } | |
163 | else { | |
164 | name = sec_name(shdr[sym->st_shndx].sh_name); | |
165 | } | |
166 | return name; | |
167 | } | |
168 | ||
169 | ||
170 | ||
171 | #if BYTE_ORDER == LITTLE_ENDIAN | |
172 | #define le16_to_cpu(val) (val) | |
173 | #define le32_to_cpu(val) (val) | |
174 | #endif | |
175 | #if BYTE_ORDER == BIG_ENDIAN | |
176 | #define le16_to_cpu(val) bswap_16(val) | |
177 | #define le32_to_cpu(val) bswap_32(val) | |
178 | #endif | |
179 | ||
180 | static uint16_t elf16_to_cpu(uint16_t val) | |
181 | { | |
182 | return le16_to_cpu(val); | |
183 | } | |
184 | ||
185 | static uint32_t elf32_to_cpu(uint32_t val) | |
186 | { | |
187 | return le32_to_cpu(val); | |
188 | } | |
189 | ||
190 | static void read_ehdr(FILE *fp) | |
191 | { | |
192 | if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) { | |
193 | die("Cannot read ELF header: %s\n", | |
194 | strerror(errno)); | |
195 | } | |
196 | if (memcmp(ehdr.e_ident, ELFMAG, 4) != 0) { | |
197 | die("No ELF magic\n"); | |
198 | } | |
199 | if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) { | |
200 | die("Not a 32 bit executable\n"); | |
201 | } | |
202 | if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) { | |
203 | die("Not a LSB ELF executable\n"); | |
204 | } | |
205 | if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) { | |
206 | die("Unknown ELF version\n"); | |
207 | } | |
208 | /* Convert the fields to native endian */ | |
209 | ehdr.e_type = elf16_to_cpu(ehdr.e_type); | |
210 | ehdr.e_machine = elf16_to_cpu(ehdr.e_machine); | |
211 | ehdr.e_version = elf32_to_cpu(ehdr.e_version); | |
212 | ehdr.e_entry = elf32_to_cpu(ehdr.e_entry); | |
213 | ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff); | |
214 | ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff); | |
215 | ehdr.e_flags = elf32_to_cpu(ehdr.e_flags); | |
216 | ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize); | |
217 | ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize); | |
218 | ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum); | |
219 | ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize); | |
220 | ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum); | |
221 | ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx); | |
222 | ||
223 | if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) { | |
224 | die("Unsupported ELF header type\n"); | |
225 | } | |
226 | if (ehdr.e_machine != EM_386) { | |
227 | die("Not for x86\n"); | |
228 | } | |
229 | if (ehdr.e_version != EV_CURRENT) { | |
230 | die("Unknown ELF version\n"); | |
231 | } | |
232 | if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) { | |
233 | die("Bad Elf header size\n"); | |
234 | } | |
235 | if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) { | |
236 | die("Bad program header entry\n"); | |
237 | } | |
238 | if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) { | |
239 | die("Bad section header entry\n"); | |
240 | } | |
241 | if (ehdr.e_shstrndx >= ehdr.e_shnum) { | |
242 | die("String table index out of bounds\n"); | |
243 | } | |
244 | } | |
245 | ||
246 | static void read_shdrs(FILE *fp) | |
247 | { | |
248 | int i; | |
249 | if (ehdr.e_shnum > MAX_SHDRS) { | |
250 | die("%d section headers supported: %d\n", | |
251 | ehdr.e_shnum, MAX_SHDRS); | |
252 | } | |
253 | if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) { | |
254 | die("Seek to %d failed: %s\n", | |
255 | ehdr.e_shoff, strerror(errno)); | |
256 | } | |
257 | if (fread(&shdr, sizeof(shdr[0]), ehdr.e_shnum, fp) != ehdr.e_shnum) { | |
258 | die("Cannot read ELF section headers: %s\n", | |
259 | strerror(errno)); | |
260 | } | |
261 | for(i = 0; i < ehdr.e_shnum; i++) { | |
262 | shdr[i].sh_name = elf32_to_cpu(shdr[i].sh_name); | |
263 | shdr[i].sh_type = elf32_to_cpu(shdr[i].sh_type); | |
264 | shdr[i].sh_flags = elf32_to_cpu(shdr[i].sh_flags); | |
265 | shdr[i].sh_addr = elf32_to_cpu(shdr[i].sh_addr); | |
266 | shdr[i].sh_offset = elf32_to_cpu(shdr[i].sh_offset); | |
267 | shdr[i].sh_size = elf32_to_cpu(shdr[i].sh_size); | |
268 | shdr[i].sh_link = elf32_to_cpu(shdr[i].sh_link); | |
269 | shdr[i].sh_info = elf32_to_cpu(shdr[i].sh_info); | |
270 | shdr[i].sh_addralign = elf32_to_cpu(shdr[i].sh_addralign); | |
271 | shdr[i].sh_entsize = elf32_to_cpu(shdr[i].sh_entsize); | |
272 | } | |
273 | ||
274 | } | |
275 | ||
276 | static void read_strtabs(FILE *fp) | |
277 | { | |
278 | int i; | |
279 | for(i = 0; i < ehdr.e_shnum; i++) { | |
280 | if (shdr[i].sh_type != SHT_STRTAB) { | |
281 | continue; | |
282 | } | |
283 | strtab[i] = malloc(shdr[i].sh_size); | |
284 | if (!strtab[i]) { | |
285 | die("malloc of %d bytes for strtab failed\n", | |
286 | shdr[i].sh_size); | |
287 | } | |
288 | if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) { | |
289 | die("Seek to %d failed: %s\n", | |
290 | shdr[i].sh_offset, strerror(errno)); | |
291 | } | |
292 | if (fread(strtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) { | |
293 | die("Cannot read symbol table: %s\n", | |
294 | strerror(errno)); | |
295 | } | |
296 | } | |
297 | } | |
298 | ||
299 | static void read_symtabs(FILE *fp) | |
300 | { | |
301 | int i,j; | |
302 | for(i = 0; i < ehdr.e_shnum; i++) { | |
303 | if (shdr[i].sh_type != SHT_SYMTAB) { | |
304 | continue; | |
305 | } | |
306 | symtab[i] = malloc(shdr[i].sh_size); | |
307 | if (!symtab[i]) { | |
308 | die("malloc of %d bytes for symtab failed\n", | |
309 | shdr[i].sh_size); | |
310 | } | |
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)); | |
314 | } | |
315 | if (fread(symtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) { | |
316 | die("Cannot read symbol table: %s\n", | |
317 | strerror(errno)); | |
318 | } | |
319 | for(j = 0; j < shdr[i].sh_size/sizeof(symtab[i][0]); j++) { | |
320 | symtab[i][j].st_name = elf32_to_cpu(symtab[i][j].st_name); | |
321 | symtab[i][j].st_value = elf32_to_cpu(symtab[i][j].st_value); | |
322 | symtab[i][j].st_size = elf32_to_cpu(symtab[i][j].st_size); | |
323 | symtab[i][j].st_shndx = elf16_to_cpu(symtab[i][j].st_shndx); | |
324 | } | |
325 | } | |
326 | } | |
327 | ||
328 | ||
329 | static void read_relocs(FILE *fp) | |
330 | { | |
331 | int i,j; | |
332 | for(i = 0; i < ehdr.e_shnum; i++) { | |
333 | if (shdr[i].sh_type != SHT_REL) { | |
334 | continue; | |
335 | } | |
336 | reltab[i] = malloc(shdr[i].sh_size); | |
337 | if (!reltab[i]) { | |
338 | die("malloc of %d bytes for relocs failed\n", | |
339 | shdr[i].sh_size); | |
340 | } | |
341 | if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) { | |
342 | die("Seek to %d failed: %s\n", | |
343 | shdr[i].sh_offset, strerror(errno)); | |
344 | } | |
345 | if (fread(reltab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) { | |
346 | die("Cannot read symbol table: %s\n", | |
347 | strerror(errno)); | |
348 | } | |
349 | for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) { | |
350 | reltab[i][j].r_offset = elf32_to_cpu(reltab[i][j].r_offset); | |
351 | reltab[i][j].r_info = elf32_to_cpu(reltab[i][j].r_info); | |
352 | } | |
353 | } | |
354 | } | |
355 | ||
356 | ||
357 | static void print_absolute_symbols(void) | |
358 | { | |
359 | int i; | |
360 | printf("Absolute symbols\n"); | |
361 | printf(" Num: Value Size Type Bind Visibility Name\n"); | |
362 | for(i = 0; i < ehdr.e_shnum; i++) { | |
363 | char *sym_strtab; | |
364 | Elf32_Sym *sh_symtab; | |
365 | int j; | |
366 | if (shdr[i].sh_type != SHT_SYMTAB) { | |
367 | continue; | |
368 | } | |
369 | sh_symtab = symtab[i]; | |
370 | sym_strtab = strtab[shdr[i].sh_link]; | |
371 | for(j = 0; j < shdr[i].sh_size/sizeof(symtab[0][0]); j++) { | |
372 | Elf32_Sym *sym; | |
373 | const char *name; | |
374 | sym = &symtab[i][j]; | |
375 | name = sym_name(sym_strtab, sym); | |
376 | if (sym->st_shndx != SHN_ABS) { | |
377 | continue; | |
378 | } | |
379 | printf("%5d %08x %5d %10s %10s %12s %s\n", | |
380 | j, sym->st_value, sym->st_size, | |
381 | sym_type(ELF32_ST_TYPE(sym->st_info)), | |
382 | sym_bind(ELF32_ST_BIND(sym->st_info)), | |
383 | sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)), | |
384 | name); | |
385 | } | |
386 | } | |
387 | printf("\n"); | |
388 | } | |
389 | ||
390 | static void print_absolute_relocs(void) | |
391 | { | |
6a044b3a VG |
392 | int i, printed = 0; |
393 | ||
968de4f0 EB |
394 | for(i = 0; i < ehdr.e_shnum; i++) { |
395 | char *sym_strtab; | |
396 | Elf32_Sym *sh_symtab; | |
397 | unsigned sec_applies, sec_symtab; | |
398 | int j; | |
399 | if (shdr[i].sh_type != SHT_REL) { | |
400 | continue; | |
401 | } | |
402 | sec_symtab = shdr[i].sh_link; | |
403 | sec_applies = shdr[i].sh_info; | |
404 | if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) { | |
405 | continue; | |
406 | } | |
407 | sh_symtab = symtab[sec_symtab]; | |
408 | sym_strtab = strtab[shdr[sec_symtab].sh_link]; | |
409 | for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) { | |
410 | Elf32_Rel *rel; | |
411 | Elf32_Sym *sym; | |
412 | const char *name; | |
413 | rel = &reltab[i][j]; | |
414 | sym = &sh_symtab[ELF32_R_SYM(rel->r_info)]; | |
415 | name = sym_name(sym_strtab, sym); | |
416 | if (sym->st_shndx != SHN_ABS) { | |
417 | continue; | |
418 | } | |
6a044b3a VG |
419 | |
420 | /* Absolute symbols are not relocated if bzImage is | |
421 | * loaded at a non-compiled address. Display a warning | |
422 | * to user at compile time about the absolute | |
423 | * relocations present. | |
424 | * | |
425 | * User need to audit the code to make sure | |
426 | * some symbols which should have been section | |
427 | * relative have not become absolute because of some | |
428 | * linker optimization or wrong programming usage. | |
429 | * | |
430 | * Before warning check if this absolute symbol | |
431 | * relocation is harmless. | |
432 | */ | |
433 | if (is_safe_abs_reloc(name)) | |
434 | continue; | |
435 | ||
436 | if (!printed) { | |
437 | printf("WARNING: Absolute relocations" | |
438 | " present\n"); | |
439 | printf("Offset Info Type Sym.Value " | |
440 | "Sym.Name\n"); | |
441 | printed = 1; | |
442 | } | |
443 | ||
968de4f0 EB |
444 | printf("%08x %08x %10s %08x %s\n", |
445 | rel->r_offset, | |
446 | rel->r_info, | |
447 | rel_type(ELF32_R_TYPE(rel->r_info)), | |
448 | sym->st_value, | |
449 | name); | |
450 | } | |
451 | } | |
6a044b3a VG |
452 | |
453 | if (printed) | |
454 | printf("\n"); | |
968de4f0 EB |
455 | } |
456 | ||
457 | static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym)) | |
458 | { | |
459 | int i; | |
460 | /* Walk through the relocations */ | |
461 | for(i = 0; i < ehdr.e_shnum; i++) { | |
462 | char *sym_strtab; | |
463 | Elf32_Sym *sh_symtab; | |
464 | unsigned sec_applies, sec_symtab; | |
465 | int j; | |
466 | if (shdr[i].sh_type != SHT_REL) { | |
467 | continue; | |
468 | } | |
469 | sec_symtab = shdr[i].sh_link; | |
470 | sec_applies = shdr[i].sh_info; | |
471 | if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) { | |
472 | continue; | |
473 | } | |
474 | sh_symtab = symtab[sec_symtab]; | |
475 | sym_strtab = strtab[shdr[sec_symtab].sh_link]; | |
476 | for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) { | |
477 | Elf32_Rel *rel; | |
478 | Elf32_Sym *sym; | |
479 | unsigned r_type; | |
480 | rel = &reltab[i][j]; | |
481 | sym = &sh_symtab[ELF32_R_SYM(rel->r_info)]; | |
482 | r_type = ELF32_R_TYPE(rel->r_info); | |
483 | /* Don't visit relocations to absolute symbols */ | |
484 | if (sym->st_shndx == SHN_ABS) { | |
485 | continue; | |
486 | } | |
487 | if (r_type == R_386_PC32) { | |
488 | /* PC relative relocations don't need to be adjusted */ | |
489 | } | |
490 | else if (r_type == R_386_32) { | |
491 | /* Visit relocations that need to be adjusted */ | |
492 | visit(rel, sym); | |
493 | } | |
494 | else { | |
495 | die("Unsupported relocation type: %d\n", r_type); | |
496 | } | |
497 | } | |
498 | } | |
499 | } | |
500 | ||
501 | static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym) | |
502 | { | |
503 | reloc_count += 1; | |
504 | } | |
505 | ||
506 | static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym) | |
507 | { | |
508 | /* Remember the address that needs to be adjusted. */ | |
509 | relocs[reloc_idx++] = rel->r_offset; | |
510 | } | |
511 | ||
512 | static int cmp_relocs(const void *va, const void *vb) | |
513 | { | |
514 | const unsigned long *a, *b; | |
515 | a = va; b = vb; | |
516 | return (*a == *b)? 0 : (*a > *b)? 1 : -1; | |
517 | } | |
518 | ||
519 | static void emit_relocs(int as_text) | |
520 | { | |
521 | int i; | |
522 | /* Count how many relocations I have and allocate space for them. */ | |
523 | reloc_count = 0; | |
524 | walk_relocs(count_reloc); | |
525 | relocs = malloc(reloc_count * sizeof(relocs[0])); | |
526 | if (!relocs) { | |
527 | die("malloc of %d entries for relocs failed\n", | |
528 | reloc_count); | |
529 | } | |
530 | /* Collect up the relocations */ | |
531 | reloc_idx = 0; | |
532 | walk_relocs(collect_reloc); | |
533 | ||
534 | /* Order the relocations for more efficient processing */ | |
535 | qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs); | |
536 | ||
537 | /* Print the relocations */ | |
538 | if (as_text) { | |
539 | /* Print the relocations in a form suitable that | |
540 | * gas will like. | |
541 | */ | |
542 | printf(".section \".data.reloc\",\"a\"\n"); | |
543 | printf(".balign 4\n"); | |
544 | for(i = 0; i < reloc_count; i++) { | |
545 | printf("\t .long 0x%08lx\n", relocs[i]); | |
546 | } | |
547 | printf("\n"); | |
548 | } | |
549 | else { | |
550 | unsigned char buf[4]; | |
551 | buf[0] = buf[1] = buf[2] = buf[3] = 0; | |
552 | /* Print a stop */ | |
553 | printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]); | |
554 | /* Now print each relocation */ | |
555 | for(i = 0; i < reloc_count; i++) { | |
556 | buf[0] = (relocs[i] >> 0) & 0xff; | |
557 | buf[1] = (relocs[i] >> 8) & 0xff; | |
558 | buf[2] = (relocs[i] >> 16) & 0xff; | |
559 | buf[3] = (relocs[i] >> 24) & 0xff; | |
560 | printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]); | |
561 | } | |
562 | } | |
563 | } | |
564 | ||
565 | static void usage(void) | |
566 | { | |
6a044b3a | 567 | die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n"); |
968de4f0 EB |
568 | } |
569 | ||
570 | int main(int argc, char **argv) | |
571 | { | |
6a044b3a | 572 | int show_absolute_syms, show_absolute_relocs; |
968de4f0 EB |
573 | int as_text; |
574 | const char *fname; | |
575 | FILE *fp; | |
576 | int i; | |
577 | ||
6a044b3a VG |
578 | show_absolute_syms = 0; |
579 | show_absolute_relocs = 0; | |
968de4f0 EB |
580 | as_text = 0; |
581 | fname = NULL; | |
582 | for(i = 1; i < argc; i++) { | |
583 | char *arg = argv[i]; | |
584 | if (*arg == '-') { | |
6a044b3a VG |
585 | if (strcmp(argv[1], "--abs-syms") == 0) { |
586 | show_absolute_syms = 1; | |
587 | continue; | |
588 | } | |
589 | ||
590 | if (strcmp(argv[1], "--abs-relocs") == 0) { | |
591 | show_absolute_relocs = 1; | |
968de4f0 EB |
592 | continue; |
593 | } | |
594 | else if (strcmp(argv[1], "--text") == 0) { | |
595 | as_text = 1; | |
596 | continue; | |
597 | } | |
598 | } | |
599 | else if (!fname) { | |
600 | fname = arg; | |
601 | continue; | |
602 | } | |
603 | usage(); | |
604 | } | |
605 | if (!fname) { | |
606 | usage(); | |
607 | } | |
608 | fp = fopen(fname, "r"); | |
609 | if (!fp) { | |
610 | die("Cannot open %s: %s\n", | |
611 | fname, strerror(errno)); | |
612 | } | |
613 | read_ehdr(fp); | |
614 | read_shdrs(fp); | |
615 | read_strtabs(fp); | |
616 | read_symtabs(fp); | |
617 | read_relocs(fp); | |
6a044b3a | 618 | if (show_absolute_syms) { |
968de4f0 | 619 | print_absolute_symbols(); |
6a044b3a VG |
620 | return 0; |
621 | } | |
622 | if (show_absolute_relocs) { | |
968de4f0 EB |
623 | print_absolute_relocs(); |
624 | return 0; | |
625 | } | |
626 | emit_relocs(as_text); | |
627 | return 0; | |
628 | } |