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