blob: b8f7c65fc40c8559fc3412ff0240c9573ece3196 [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>
H. Peter Anvin873b5272009-12-14 13:55:20 -080012#include <regex.h>
Matt Fleming55f97092012-02-28 13:37:21 +000013#include <tools/le_byteshift.h>
H. Peter Anvin873b5272009-12-14 13:55:20 -080014
15static void die(char *fmt, ...);
Eric W. Biederman968de4f2006-12-07 02:14:04 +010016
Robert P. J. Dayca820182007-02-17 19:10:01 +010017#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Eric W. Biederman968de4f2006-12-07 02:14:04 +010018static Elf32_Ehdr ehdr;
Eric W. Biederman968de4f2006-12-07 02:14:04 +010019static unsigned long reloc_count, reloc_idx;
20static unsigned long *relocs;
H. Peter Anvin6520fe52012-05-08 21:22:24 +030021static unsigned long reloc16_count, reloc16_idx;
22static unsigned long *relocs16;
Eric W. Biederman968de4f2006-12-07 02:14:04 +010023
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070024struct section {
25 Elf32_Shdr shdr;
26 struct section *link;
27 Elf32_Sym *symtab;
28 Elf32_Rel *reltab;
29 char *strtab;
30};
31static struct section *secs;
32
H. Peter Anvin6520fe52012-05-08 21:22:24 +030033enum symtype {
34 S_ABS,
35 S_REL,
36 S_SEG,
37 S_LIN,
38 S_NSYMTYPES
39};
40
41static const char * const sym_regex_kernel[S_NSYMTYPES] = {
Vivek Goyal6a044b32006-12-07 02:14:04 +010042/*
43 * Following symbols have been audited. There values are constant and do
44 * not change if bzImage is loaded at a different physical address than
45 * the address for which it has been compiled. Don't warn user about
46 * absolute relocations present w.r.t these symbols.
47 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +030048 [S_ABS] =
H. Peter Anvin873b5272009-12-14 13:55:20 -080049 "^(xen_irq_disable_direct_reloc$|"
50 "xen_save_fl_direct_reloc$|"
51 "VDSO|"
H. Peter Anvin6520fe52012-05-08 21:22:24 +030052 "__crc_)",
Vivek Goyal6a044b32006-12-07 02:14:04 +010053
H. Peter Anvin873b5272009-12-14 13:55:20 -080054/*
55 * These symbols are known to be relative, even if the linker marks them
56 * as absolute (typically defined outside any section in the linker script.)
57 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +030058 [S_REL] =
H. Peter Anvina3e854d2012-05-18 00:24:09 -070059 "^(__init_(begin|end)|"
60 "__x86_cpu_dev_(start|end)|"
61 "(__parainstructions|__alt_instructions)(|_end)|"
62 "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
H. Peter Anvinfd952812012-05-23 14:02:34 -070063 "__(start|end)_pci_.*|"
64 "__(start|end)_builtin_fw|"
65 "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
66 "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
67 "__(start|stop)___param|"
68 "__(start|stop)___modver|"
69 "__(start|stop)___bug_table|"
70 "__tracedata_(start|end)|"
71 "__(start|stop)_notes|"
72 "__end_rodata|"
73 "__initramfs_start|"
H. Peter Anvina3e854d2012-05-18 00:24:09 -070074 "_end)$"
H. Peter Anvin6520fe52012-05-08 21:22:24 +030075};
76
77
78static const char * const sym_regex_realmode[S_NSYMTYPES] = {
79/*
80 * These are 16-bit segment symbols when compiling 16-bit code.
81 */
82 [S_SEG] =
83 "^real_mode_seg$",
84
85/*
86 * These are offsets belonging to segments, as opposed to linear addresses,
87 * when compiling 16-bit code.
88 */
89 [S_LIN] =
90 "^pa_",
91};
92
93static const char * const *sym_regex;
94
95static regex_t sym_regex_c[S_NSYMTYPES];
96static int is_reloc(enum symtype type, const char *sym_name)
H. Peter Anvin873b5272009-12-14 13:55:20 -080097{
H. Peter Anvin6520fe52012-05-08 21:22:24 +030098 return sym_regex[type] &&
99 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
H. Peter Anvin873b5272009-12-14 13:55:20 -0800100}
101
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300102static void regex_init(int use_real_mode)
H. Peter Anvin873b5272009-12-14 13:55:20 -0800103{
104 char errbuf[128];
105 int err;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300106 int i;
H. Peter Anvin873b5272009-12-14 13:55:20 -0800107
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300108 if (use_real_mode)
109 sym_regex = sym_regex_realmode;
110 else
111 sym_regex = sym_regex_kernel;
112
113 for (i = 0; i < S_NSYMTYPES; i++) {
114 if (!sym_regex[i])
115 continue;
116
117 err = regcomp(&sym_regex_c[i], sym_regex[i],
118 REG_EXTENDED|REG_NOSUB);
119
120 if (err) {
121 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
122 die("%s", errbuf);
123 }
H. Peter Anvin873b5272009-12-14 13:55:20 -0800124 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100125}
126
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100127static void die(char *fmt, ...)
128{
129 va_list ap;
130 va_start(ap, fmt);
131 vfprintf(stderr, fmt, ap);
132 va_end(ap);
133 exit(1);
134}
135
136static const char *sym_type(unsigned type)
137{
138 static const char *type_name[] = {
139#define SYM_TYPE(X) [X] = #X
140 SYM_TYPE(STT_NOTYPE),
141 SYM_TYPE(STT_OBJECT),
142 SYM_TYPE(STT_FUNC),
143 SYM_TYPE(STT_SECTION),
144 SYM_TYPE(STT_FILE),
145 SYM_TYPE(STT_COMMON),
146 SYM_TYPE(STT_TLS),
147#undef SYM_TYPE
148 };
149 const char *name = "unknown sym type name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100150 if (type < ARRAY_SIZE(type_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100151 name = type_name[type];
152 }
153 return name;
154}
155
156static const char *sym_bind(unsigned bind)
157{
158 static const char *bind_name[] = {
159#define SYM_BIND(X) [X] = #X
160 SYM_BIND(STB_LOCAL),
161 SYM_BIND(STB_GLOBAL),
162 SYM_BIND(STB_WEAK),
163#undef SYM_BIND
164 };
165 const char *name = "unknown sym bind name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100166 if (bind < ARRAY_SIZE(bind_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100167 name = bind_name[bind];
168 }
169 return name;
170}
171
172static const char *sym_visibility(unsigned visibility)
173{
174 static const char *visibility_name[] = {
175#define SYM_VISIBILITY(X) [X] = #X
176 SYM_VISIBILITY(STV_DEFAULT),
177 SYM_VISIBILITY(STV_INTERNAL),
178 SYM_VISIBILITY(STV_HIDDEN),
179 SYM_VISIBILITY(STV_PROTECTED),
180#undef SYM_VISIBILITY
181 };
182 const char *name = "unknown sym visibility name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100183 if (visibility < ARRAY_SIZE(visibility_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100184 name = visibility_name[visibility];
185 }
186 return name;
187}
188
189static const char *rel_type(unsigned type)
190{
191 static const char *type_name[] = {
192#define REL_TYPE(X) [X] = #X
193 REL_TYPE(R_386_NONE),
194 REL_TYPE(R_386_32),
195 REL_TYPE(R_386_PC32),
196 REL_TYPE(R_386_GOT32),
197 REL_TYPE(R_386_PLT32),
198 REL_TYPE(R_386_COPY),
199 REL_TYPE(R_386_GLOB_DAT),
200 REL_TYPE(R_386_JMP_SLOT),
201 REL_TYPE(R_386_RELATIVE),
202 REL_TYPE(R_386_GOTOFF),
203 REL_TYPE(R_386_GOTPC),
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300204 REL_TYPE(R_386_8),
205 REL_TYPE(R_386_PC8),
206 REL_TYPE(R_386_16),
207 REL_TYPE(R_386_PC16),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100208#undef REL_TYPE
209 };
210 const char *name = "unknown type rel type name";
H. Peter Anvin873b5272009-12-14 13:55:20 -0800211 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100212 name = type_name[type];
213 }
214 return name;
215}
216
217static const char *sec_name(unsigned shndx)
218{
219 const char *sec_strtab;
220 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700221 sec_strtab = secs[ehdr.e_shstrndx].strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100222 name = "<noname>";
223 if (shndx < ehdr.e_shnum) {
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700224 name = sec_strtab + secs[shndx].shdr.sh_name;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100225 }
226 else if (shndx == SHN_ABS) {
227 name = "ABSOLUTE";
228 }
229 else if (shndx == SHN_COMMON) {
230 name = "COMMON";
231 }
232 return name;
233}
234
235static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
236{
237 const char *name;
238 name = "<noname>";
239 if (sym->st_name) {
240 name = sym_strtab + sym->st_name;
241 }
242 else {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300243 name = sec_name(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100244 }
245 return name;
246}
247
248
249
Linus Torvalds13da9e22010-05-26 08:30:15 -0700250#if BYTE_ORDER == LITTLE_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100251#define le16_to_cpu(val) (val)
252#define le32_to_cpu(val) (val)
253#endif
Linus Torvalds13da9e22010-05-26 08:30:15 -0700254#if BYTE_ORDER == BIG_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100255#define le16_to_cpu(val) bswap_16(val)
256#define le32_to_cpu(val) bswap_32(val)
257#endif
258
259static uint16_t elf16_to_cpu(uint16_t val)
260{
261 return le16_to_cpu(val);
262}
263
264static uint32_t elf32_to_cpu(uint32_t val)
265{
266 return le32_to_cpu(val);
267}
268
269static void read_ehdr(FILE *fp)
270{
271 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
272 die("Cannot read ELF header: %s\n",
273 strerror(errno));
274 }
Cyrill Gorcunov8bd17962008-05-03 14:18:03 +0400275 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100276 die("No ELF magic\n");
277 }
278 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
279 die("Not a 32 bit executable\n");
280 }
281 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
282 die("Not a LSB ELF executable\n");
283 }
284 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
285 die("Unknown ELF version\n");
286 }
287 /* Convert the fields to native endian */
288 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
289 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
290 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
291 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
292 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
293 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
294 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
295 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
296 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
297 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
298 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
299 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
300 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
301
302 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
303 die("Unsupported ELF header type\n");
304 }
305 if (ehdr.e_machine != EM_386) {
306 die("Not for x86\n");
307 }
308 if (ehdr.e_version != EV_CURRENT) {
309 die("Unknown ELF version\n");
310 }
311 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
312 die("Bad Elf header size\n");
313 }
314 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
315 die("Bad program header entry\n");
316 }
317 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
318 die("Bad section header entry\n");
319 }
320 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
321 die("String table index out of bounds\n");
322 }
323}
324
325static void read_shdrs(FILE *fp)
326{
327 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700328 Elf32_Shdr shdr;
329
330 secs = calloc(ehdr.e_shnum, sizeof(struct section));
331 if (!secs) {
332 die("Unable to allocate %d section headers\n",
333 ehdr.e_shnum);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100334 }
335 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
336 die("Seek to %d failed: %s\n",
337 ehdr.e_shoff, strerror(errno));
338 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700339 for (i = 0; i < ehdr.e_shnum; i++) {
340 struct section *sec = &secs[i];
341 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
342 die("Cannot read ELF section headers %d/%d: %s\n",
343 i, ehdr.e_shnum, strerror(errno));
344 sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name);
345 sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type);
346 sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags);
347 sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr);
348 sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset);
349 sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size);
350 sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link);
351 sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info);
352 sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign);
353 sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize);
354 if (sec->shdr.sh_link < ehdr.e_shnum)
355 sec->link = &secs[sec->shdr.sh_link];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100356 }
357
358}
359
360static void read_strtabs(FILE *fp)
361{
362 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700363 for (i = 0; i < ehdr.e_shnum; i++) {
364 struct section *sec = &secs[i];
365 if (sec->shdr.sh_type != SHT_STRTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100366 continue;
367 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700368 sec->strtab = malloc(sec->shdr.sh_size);
369 if (!sec->strtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100370 die("malloc of %d bytes for strtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700371 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100372 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700373 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100374 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700375 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100376 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700377 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
378 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100379 die("Cannot read symbol table: %s\n",
380 strerror(errno));
381 }
382 }
383}
384
385static void read_symtabs(FILE *fp)
386{
387 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700388 for (i = 0; i < ehdr.e_shnum; i++) {
389 struct section *sec = &secs[i];
390 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100391 continue;
392 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700393 sec->symtab = malloc(sec->shdr.sh_size);
394 if (!sec->symtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100395 die("malloc of %d bytes for symtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700396 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100397 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700398 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100399 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700400 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100401 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700402 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
403 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100404 die("Cannot read symbol table: %s\n",
405 strerror(errno));
406 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700407 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
408 Elf32_Sym *sym = &sec->symtab[j];
409 sym->st_name = elf32_to_cpu(sym->st_name);
410 sym->st_value = elf32_to_cpu(sym->st_value);
411 sym->st_size = elf32_to_cpu(sym->st_size);
412 sym->st_shndx = elf16_to_cpu(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100413 }
414 }
415}
416
417
418static void read_relocs(FILE *fp)
419{
420 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700421 for (i = 0; i < ehdr.e_shnum; i++) {
422 struct section *sec = &secs[i];
423 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100424 continue;
425 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700426 sec->reltab = malloc(sec->shdr.sh_size);
427 if (!sec->reltab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100428 die("malloc of %d bytes for relocs failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700429 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100430 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700431 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100432 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700433 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100434 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700435 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
436 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100437 die("Cannot read symbol table: %s\n",
438 strerror(errno));
439 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700440 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
441 Elf32_Rel *rel = &sec->reltab[j];
442 rel->r_offset = elf32_to_cpu(rel->r_offset);
443 rel->r_info = elf32_to_cpu(rel->r_info);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100444 }
445 }
446}
447
448
449static void print_absolute_symbols(void)
450{
451 int i;
452 printf("Absolute symbols\n");
453 printf(" Num: Value Size Type Bind Visibility Name\n");
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700454 for (i = 0; i < ehdr.e_shnum; i++) {
455 struct section *sec = &secs[i];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100456 char *sym_strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100457 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700458
459 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100460 continue;
461 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700462 sym_strtab = sec->link->strtab;
463 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100464 Elf32_Sym *sym;
465 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700466 sym = &sec->symtab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100467 name = sym_name(sym_strtab, sym);
468 if (sym->st_shndx != SHN_ABS) {
469 continue;
470 }
471 printf("%5d %08x %5d %10s %10s %12s %s\n",
472 j, sym->st_value, sym->st_size,
473 sym_type(ELF32_ST_TYPE(sym->st_info)),
474 sym_bind(ELF32_ST_BIND(sym->st_info)),
475 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
476 name);
477 }
478 }
479 printf("\n");
480}
481
482static void print_absolute_relocs(void)
483{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100484 int i, printed = 0;
485
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700486 for (i = 0; i < ehdr.e_shnum; i++) {
487 struct section *sec = &secs[i];
488 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100489 char *sym_strtab;
490 Elf32_Sym *sh_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100491 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700492 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100493 continue;
494 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700495 sec_symtab = sec->link;
496 sec_applies = &secs[sec->shdr.sh_info];
497 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100498 continue;
499 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700500 sh_symtab = sec_symtab->symtab;
501 sym_strtab = sec_symtab->link->strtab;
502 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100503 Elf32_Rel *rel;
504 Elf32_Sym *sym;
505 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700506 rel = &sec->reltab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100507 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
508 name = sym_name(sym_strtab, sym);
509 if (sym->st_shndx != SHN_ABS) {
510 continue;
511 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100512
513 /* Absolute symbols are not relocated if bzImage is
514 * loaded at a non-compiled address. Display a warning
515 * to user at compile time about the absolute
516 * relocations present.
517 *
518 * User need to audit the code to make sure
519 * some symbols which should have been section
520 * relative have not become absolute because of some
521 * linker optimization or wrong programming usage.
522 *
523 * Before warning check if this absolute symbol
524 * relocation is harmless.
525 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300526 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
Vivek Goyal6a044b32006-12-07 02:14:04 +0100527 continue;
528
529 if (!printed) {
530 printf("WARNING: Absolute relocations"
531 " present\n");
532 printf("Offset Info Type Sym.Value "
533 "Sym.Name\n");
534 printed = 1;
535 }
536
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100537 printf("%08x %08x %10s %08x %s\n",
538 rel->r_offset,
539 rel->r_info,
540 rel_type(ELF32_R_TYPE(rel->r_info)),
541 sym->st_value,
542 name);
543 }
544 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100545
546 if (printed)
547 printf("\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100548}
549
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300550static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym),
551 int use_real_mode)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100552{
553 int i;
554 /* Walk through the relocations */
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700555 for (i = 0; i < ehdr.e_shnum; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100556 char *sym_strtab;
557 Elf32_Sym *sh_symtab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700558 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100559 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700560 struct section *sec = &secs[i];
561
562 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100563 continue;
564 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700565 sec_symtab = sec->link;
566 sec_applies = &secs[sec->shdr.sh_info];
567 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100568 continue;
569 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700570 sh_symtab = sec_symtab->symtab;
H. Peter Anvincc65f1e2008-10-03 13:00:56 -0700571 sym_strtab = sec_symtab->link->strtab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700572 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100573 Elf32_Rel *rel;
574 Elf32_Sym *sym;
575 unsigned r_type;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300576 const char *symname;
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700577 int shn_abs;
578
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700579 rel = &sec->reltab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100580 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
581 r_type = ELF32_R_TYPE(rel->r_info);
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300582
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700583 shn_abs = sym->st_shndx == SHN_ABS;
584
H. Peter Anvin873b5272009-12-14 13:55:20 -0800585 switch (r_type) {
586 case R_386_NONE:
587 case R_386_PC32:
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300588 case R_386_PC16:
589 case R_386_PC8:
Tejun Heo46176b42009-05-26 14:42:40 +0900590 /*
591 * NONE can be ignored and and PC relative
592 * relocations don't need to be adjusted.
593 */
H. Peter Anvin873b5272009-12-14 13:55:20 -0800594 break;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300595
596 case R_386_16:
597 symname = sym_name(sym_strtab, sym);
598 if (!use_real_mode)
599 goto bad;
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700600 if (shn_abs) {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300601 if (is_reloc(S_ABS, symname))
602 break;
603 else if (!is_reloc(S_SEG, symname))
604 goto bad;
605 } else {
606 if (is_reloc(S_LIN, symname))
607 goto bad;
608 else
609 break;
610 }
611 visit(rel, sym);
612 break;
613
H. Peter Anvin873b5272009-12-14 13:55:20 -0800614 case R_386_32:
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300615 symname = sym_name(sym_strtab, sym);
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700616 if (shn_abs) {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300617 if (is_reloc(S_ABS, symname))
618 break;
619 else if (!is_reloc(S_REL, symname))
620 goto bad;
621 } else {
622 if (use_real_mode &&
623 !is_reloc(S_LIN, symname))
624 break;
625 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100626 visit(rel, sym);
H. Peter Anvin873b5272009-12-14 13:55:20 -0800627 break;
628 default:
629 die("Unsupported relocation type: %s (%d)\n",
630 rel_type(r_type), r_type);
631 break;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300632 bad:
633 symname = sym_name(sym_strtab, sym);
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700634 die("Invalid %s %s relocation: %s\n",
635 shn_abs ? "absolute" : "relative",
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300636 rel_type(r_type), symname);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100637 }
638 }
639 }
640}
641
642static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
643{
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300644 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
645 reloc16_count++;
646 else
647 reloc_count++;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100648}
649
650static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
651{
652 /* Remember the address that needs to be adjusted. */
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300653 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
654 relocs16[reloc16_idx++] = rel->r_offset;
655 else
656 relocs[reloc_idx++] = rel->r_offset;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100657}
658
659static int cmp_relocs(const void *va, const void *vb)
660{
661 const unsigned long *a, *b;
662 a = va; b = vb;
663 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
664}
665
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300666static int write32(unsigned int v, FILE *f)
667{
668 unsigned char buf[4];
669
670 put_unaligned_le32(v, buf);
671 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
672}
673
674static void emit_relocs(int as_text, int use_real_mode)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100675{
676 int i;
677 /* Count how many relocations I have and allocate space for them. */
678 reloc_count = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300679 walk_relocs(count_reloc, use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100680 relocs = malloc(reloc_count * sizeof(relocs[0]));
681 if (!relocs) {
682 die("malloc of %d entries for relocs failed\n",
683 reloc_count);
684 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300685
686 relocs16 = malloc(reloc16_count * sizeof(relocs[0]));
687 if (!relocs16) {
688 die("malloc of %d entries for relocs16 failed\n",
689 reloc16_count);
690 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100691 /* Collect up the relocations */
692 reloc_idx = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300693 walk_relocs(collect_reloc, use_real_mode);
694
695 if (reloc16_count && !use_real_mode)
696 die("Segment relocations found but --realmode not specified\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100697
698 /* Order the relocations for more efficient processing */
699 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300700 qsort(relocs16, reloc16_count, sizeof(relocs16[0]), cmp_relocs);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100701
702 /* Print the relocations */
703 if (as_text) {
704 /* Print the relocations in a form suitable that
705 * gas will like.
706 */
707 printf(".section \".data.reloc\",\"a\"\n");
708 printf(".balign 4\n");
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300709 if (use_real_mode) {
710 printf("\t.long %lu\n", reloc16_count);
711 for (i = 0; i < reloc16_count; i++)
712 printf("\t.long 0x%08lx\n", relocs16[i]);
713 printf("\t.long %lu\n", reloc_count);
714 for (i = 0; i < reloc_count; i++) {
715 printf("\t.long 0x%08lx\n", relocs[i]);
716 }
717 } else {
718 /* Print a stop */
719 printf("\t.long 0x%08lx\n", (unsigned long)0);
720 for (i = 0; i < reloc_count; i++) {
721 printf("\t.long 0x%08lx\n", relocs[i]);
722 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100723 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300724
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100725 printf("\n");
726 }
727 else {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300728 if (use_real_mode) {
729 write32(reloc16_count, stdout);
730 for (i = 0; i < reloc16_count; i++)
731 write32(relocs16[i], stdout);
732 write32(reloc_count, stdout);
733
734 /* Now print each relocation */
735 for (i = 0; i < reloc_count; i++)
736 write32(relocs[i], stdout);
737 } else {
738 /* Print a stop */
739 write32(0, stdout);
740
741 /* Now print each relocation */
742 for (i = 0; i < reloc_count; i++) {
743 write32(relocs[i], stdout);
744 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100745 }
746 }
747}
748
749static void usage(void)
750{
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300751 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100752}
753
754int main(int argc, char **argv)
755{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100756 int show_absolute_syms, show_absolute_relocs;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300757 int as_text, use_real_mode;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100758 const char *fname;
759 FILE *fp;
760 int i;
761
Vivek Goyal6a044b32006-12-07 02:14:04 +0100762 show_absolute_syms = 0;
763 show_absolute_relocs = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100764 as_text = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300765 use_real_mode = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100766 fname = NULL;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700767 for (i = 1; i < argc; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100768 char *arg = argv[i];
769 if (*arg == '-') {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300770 if (strcmp(arg, "--abs-syms") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100771 show_absolute_syms = 1;
772 continue;
773 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300774 if (strcmp(arg, "--abs-relocs") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100775 show_absolute_relocs = 1;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100776 continue;
777 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300778 if (strcmp(arg, "--text") == 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100779 as_text = 1;
780 continue;
781 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300782 if (strcmp(arg, "--realmode") == 0) {
783 use_real_mode = 1;
784 continue;
785 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100786 }
787 else if (!fname) {
788 fname = arg;
789 continue;
790 }
791 usage();
792 }
793 if (!fname) {
794 usage();
795 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300796 regex_init(use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100797 fp = fopen(fname, "r");
798 if (!fp) {
799 die("Cannot open %s: %s\n",
800 fname, strerror(errno));
801 }
802 read_ehdr(fp);
803 read_shdrs(fp);
804 read_strtabs(fp);
805 read_symtabs(fp);
806 read_relocs(fp);
Vivek Goyal6a044b32006-12-07 02:14:04 +0100807 if (show_absolute_syms) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100808 print_absolute_symbols();
Vivek Goyal6a044b32006-12-07 02:14:04 +0100809 return 0;
810 }
811 if (show_absolute_relocs) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100812 print_absolute_relocs();
813 return 0;
814 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300815 emit_relocs(as_text, use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100816 return 0;
817}