blob: a1310c52fc0c6ac6bf97feb276700e9f0662793e [file] [log] [blame]
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001#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
Robert P. J. Dayca820182007-02-17 19:10:01 +010013#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Eric W. Biederman968de4f2006-12-07 02:14:04 +010014static Elf32_Ehdr ehdr;
Eric W. Biederman968de4f2006-12-07 02:14:04 +010015static unsigned long reloc_count, reloc_idx;
16static unsigned long *relocs;
17
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070018struct section {
19 Elf32_Shdr shdr;
20 struct section *link;
21 Elf32_Sym *symtab;
22 Elf32_Rel *reltab;
23 char *strtab;
24};
25static struct section *secs;
26
Vivek Goyal6a044b32006-12-07 02:14:04 +010027/*
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 */
33static const char* safe_abs_relocs[] = {
Jeremy Fitzhardinge600b2fc2007-07-17 18:37:07 -070034 "xen_irq_disable_direct_reloc",
35 "xen_save_fl_direct_reloc",
Vivek Goyal6a044b32006-12-07 02:14:04 +010036};
37
38static int is_safe_abs_reloc(const char* sym_name)
39{
Alejandro Martinez Ruiz4d022ad2007-10-17 14:38:58 +020040 int i;
Vivek Goyal6a044b32006-12-07 02:14:04 +010041
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070042 for (i = 0; i < ARRAY_SIZE(safe_abs_relocs); i++) {
Vivek Goyal6a044b32006-12-07 02:14:04 +010043 if (!strcmp(sym_name, safe_abs_relocs[i]))
44 /* Match found */
45 return 1;
46 }
Roland McGrath4707c472008-01-30 13:30:42 +010047 if (strncmp(sym_name, "VDSO", 4) == 0)
48 return 1;
Al Viro2a3d4f12007-02-01 13:52:23 +000049 if (strncmp(sym_name, "__crc_", 6) == 0)
50 return 1;
Vivek Goyal6a044b32006-12-07 02:14:04 +010051 return 0;
52}
53
Eric W. Biederman968de4f2006-12-07 02:14:04 +010054static 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
63static 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";
Robert P. J. Dayca820182007-02-17 19:10:01 +010077 if (type < ARRAY_SIZE(type_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +010078 name = type_name[type];
79 }
80 return name;
81}
82
83static 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";
Robert P. J. Dayca820182007-02-17 19:10:01 +010093 if (bind < ARRAY_SIZE(bind_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +010094 name = bind_name[bind];
95 }
96 return name;
97}
98
99static 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";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100110 if (visibility < ARRAY_SIZE(visibility_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100111 name = visibility_name[visibility];
112 }
113 return name;
114}
115
116static 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";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100134 if (type < ARRAY_SIZE(type_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100135 name = type_name[type];
136 }
137 return name;
138}
139
140static const char *sec_name(unsigned shndx)
141{
142 const char *sec_strtab;
143 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700144 sec_strtab = secs[ehdr.e_shstrndx].strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100145 name = "<noname>";
146 if (shndx < ehdr.e_shnum) {
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700147 name = sec_strtab + secs[shndx].shdr.sh_name;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100148 }
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
158static 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 {
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700166 name = sec_name(secs[sym->st_shndx].shdr.sh_name);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100167 }
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
182static uint16_t elf16_to_cpu(uint16_t val)
183{
184 return le16_to_cpu(val);
185}
186
187static uint32_t elf32_to_cpu(uint32_t val)
188{
189 return le32_to_cpu(val);
190}
191
192static 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 }
Cyrill Gorcunov8bd17962008-05-03 14:18:03 +0400198 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100199 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
248static void read_shdrs(FILE *fp)
249{
250 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700251 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);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100257 }
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 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700262 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];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100279 }
280
281}
282
283static void read_strtabs(FILE *fp)
284{
285 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700286 for (i = 0; i < ehdr.e_shnum; i++) {
287 struct section *sec = &secs[i];
288 if (sec->shdr.sh_type != SHT_STRTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100289 continue;
290 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700291 sec->strtab = malloc(sec->shdr.sh_size);
292 if (!sec->strtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100293 die("malloc of %d bytes for strtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700294 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100295 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700296 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100297 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700298 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100299 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700300 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
301 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100302 die("Cannot read symbol table: %s\n",
303 strerror(errno));
304 }
305 }
306}
307
308static void read_symtabs(FILE *fp)
309{
310 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700311 for (i = 0; i < ehdr.e_shnum; i++) {
312 struct section *sec = &secs[i];
313 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100314 continue;
315 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700316 sec->symtab = malloc(sec->shdr.sh_size);
317 if (!sec->symtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100318 die("malloc of %d bytes for symtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700319 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100320 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700321 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100322 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700323 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100324 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700325 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
326 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100327 die("Cannot read symbol table: %s\n",
328 strerror(errno));
329 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700330 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);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100336 }
337 }
338}
339
340
341static void read_relocs(FILE *fp)
342{
343 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700344 for (i = 0; i < ehdr.e_shnum; i++) {
345 struct section *sec = &secs[i];
346 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100347 continue;
348 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700349 sec->reltab = malloc(sec->shdr.sh_size);
350 if (!sec->reltab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100351 die("malloc of %d bytes for relocs failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700352 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100353 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700354 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100355 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700356 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100357 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700358 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
359 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100360 die("Cannot read symbol table: %s\n",
361 strerror(errno));
362 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700363 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);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100367 }
368 }
369}
370
371
372static void print_absolute_symbols(void)
373{
374 int i;
375 printf("Absolute symbols\n");
376 printf(" Num: Value Size Type Bind Visibility Name\n");
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700377 for (i = 0; i < ehdr.e_shnum; i++) {
378 struct section *sec = &secs[i];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100379 char *sym_strtab;
380 Elf32_Sym *sh_symtab;
381 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700382
383 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100384 continue;
385 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700386 sh_symtab = sec->symtab;
387 sym_strtab = sec->link->strtab;
388 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100389 Elf32_Sym *sym;
390 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700391 sym = &sec->symtab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100392 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
407static void print_absolute_relocs(void)
408{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100409 int i, printed = 0;
410
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700411 for (i = 0; i < ehdr.e_shnum; i++) {
412 struct section *sec = &secs[i];
413 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100414 char *sym_strtab;
415 Elf32_Sym *sh_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100416 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700417 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100418 continue;
419 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700420 sec_symtab = sec->link;
421 sec_applies = &secs[sec->shdr.sh_info];
422 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100423 continue;
424 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700425 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++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100428 Elf32_Rel *rel;
429 Elf32_Sym *sym;
430 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700431 rel = &sec->reltab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100432 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 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100437
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
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100462 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 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100470
471 if (printed)
472 printf("\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100473}
474
475static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
476{
477 int i;
478 /* Walk through the relocations */
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700479 for (i = 0; i < ehdr.e_shnum; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100480 char *sym_strtab;
481 Elf32_Sym *sh_symtab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700482 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100483 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700484 struct section *sec = &secs[i];
485
486 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100487 continue;
488 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700489 sec_symtab = sec->link;
490 sec_applies = &secs[sec->shdr.sh_info];
491 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100492 continue;
493 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700494 sh_symtab = sec_symtab->symtab;
495 sym_strtab = sec->link->strtab;
496 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100497 Elf32_Rel *rel;
498 Elf32_Sym *sym;
499 unsigned r_type;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700500 rel = &sec->reltab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100501 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
521static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
522{
523 reloc_count += 1;
524}
525
526static 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
532static 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
539static 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");
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700564 for (i = 0; i < reloc_count; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100565 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 */
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700575 for (i = 0; i < reloc_count; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100576 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
585static void usage(void)
586{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100587 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100588}
589
590int main(int argc, char **argv)
591{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100592 int show_absolute_syms, show_absolute_relocs;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100593 int as_text;
594 const char *fname;
595 FILE *fp;
596 int i;
597
Vivek Goyal6a044b32006-12-07 02:14:04 +0100598 show_absolute_syms = 0;
599 show_absolute_relocs = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100600 as_text = 0;
601 fname = NULL;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700602 for (i = 1; i < argc; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100603 char *arg = argv[i];
604 if (*arg == '-') {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100605 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;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100612 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);
Vivek Goyal6a044b32006-12-07 02:14:04 +0100638 if (show_absolute_syms) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100639 print_absolute_symbols();
Vivek Goyal6a044b32006-12-07 02:14:04 +0100640 return 0;
641 }
642 if (show_absolute_relocs) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100643 print_absolute_relocs();
644 return 0;
645 }
646 emit_relocs(as_text);
647 return 0;
648}