blob: fd28ef7dfbb9666331352f67fbbfe77853d621d1 [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
Kees Cookbf116552013-04-12 13:13:42 -070015#define ElfW(type) _ElfW(ELF_BITS, type)
16#define _ElfW(bits, type) __ElfW(bits, type)
17#define __ElfW(bits, type) Elf##bits##_##type
18
19#define ELF_BITS 32
20#define ELF_MACHINE EM_386
21#define ELF_MACHINE_NAME "i386"
22#define SHT_REL_TYPE SHT_REL
23
24#define ELF_CLASS ELFCLASS32
25#define ELF_R_SYM(val) ELF32_R_SYM(val)
26#define ELF_R_TYPE(val) ELF32_R_TYPE(val)
27#define ELF_ST_TYPE(o) ELF32_ST_TYPE(o)
28#define ELF_ST_BIND(o) ELF32_ST_BIND(o)
29#define ELF_ST_VISIBILITY(o) ELF32_ST_VISIBILITY(o)
30
31#define Elf_Rel ElfW(Rel)
32#define Elf_Ehdr ElfW(Ehdr)
33#define Elf_Phdr ElfW(Phdr)
34#define Elf_Shdr ElfW(Shdr)
35#define Elf_Sym ElfW(Sym)
36
H. Peter Anvin873b5272009-12-14 13:55:20 -080037static void die(char *fmt, ...);
Eric W. Biederman968de4f2006-12-07 02:14:04 +010038
Robert P. J. Dayca820182007-02-17 19:10:01 +010039#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Kees Cookbf116552013-04-12 13:13:42 -070040static Elf_Ehdr ehdr;
Eric W. Biederman968de4f2006-12-07 02:14:04 +010041static unsigned long reloc_count, reloc_idx;
42static unsigned long *relocs;
H. Peter Anvin6520fe52012-05-08 21:22:24 +030043static unsigned long reloc16_count, reloc16_idx;
44static unsigned long *relocs16;
Eric W. Biederman968de4f2006-12-07 02:14:04 +010045
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070046struct section {
Kees Cookbf116552013-04-12 13:13:42 -070047 Elf_Shdr shdr;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070048 struct section *link;
Kees Cookbf116552013-04-12 13:13:42 -070049 Elf_Sym *symtab;
50 Elf_Rel *reltab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070051 char *strtab;
52};
53static struct section *secs;
54
H. Peter Anvin6520fe52012-05-08 21:22:24 +030055enum symtype {
56 S_ABS,
57 S_REL,
58 S_SEG,
59 S_LIN,
60 S_NSYMTYPES
61};
62
63static const char * const sym_regex_kernel[S_NSYMTYPES] = {
Vivek Goyal6a044b32006-12-07 02:14:04 +010064/*
65 * Following symbols have been audited. There values are constant and do
66 * not change if bzImage is loaded at a different physical address than
67 * the address for which it has been compiled. Don't warn user about
68 * absolute relocations present w.r.t these symbols.
69 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +030070 [S_ABS] =
H. Peter Anvin873b5272009-12-14 13:55:20 -080071 "^(xen_irq_disable_direct_reloc$|"
72 "xen_save_fl_direct_reloc$|"
73 "VDSO|"
H. Peter Anvin6520fe52012-05-08 21:22:24 +030074 "__crc_)",
Vivek Goyal6a044b32006-12-07 02:14:04 +010075
H. Peter Anvin873b5272009-12-14 13:55:20 -080076/*
77 * These symbols are known to be relative, even if the linker marks them
78 * as absolute (typically defined outside any section in the linker script.)
79 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +030080 [S_REL] =
H. Peter Anvina3e854d2012-05-18 00:24:09 -070081 "^(__init_(begin|end)|"
82 "__x86_cpu_dev_(start|end)|"
83 "(__parainstructions|__alt_instructions)(|_end)|"
84 "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
H. Peter Anvinfd952812012-05-23 14:02:34 -070085 "__(start|end)_pci_.*|"
86 "__(start|end)_builtin_fw|"
87 "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
88 "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
89 "__(start|stop)___param|"
90 "__(start|stop)___modver|"
91 "__(start|stop)___bug_table|"
92 "__tracedata_(start|end)|"
93 "__(start|stop)_notes|"
94 "__end_rodata|"
95 "__initramfs_start|"
H. Peter Anvinea17e742012-05-24 07:01:38 -070096 "(jiffies|jiffies_64)|"
H. Peter Anvina3e854d2012-05-18 00:24:09 -070097 "_end)$"
H. Peter Anvin6520fe52012-05-08 21:22:24 +030098};
99
100
101static const char * const sym_regex_realmode[S_NSYMTYPES] = {
102/*
H. Peter Anvin2a6de312012-05-08 21:22:31 +0300103 * These symbols are known to be relative, even if the linker marks them
104 * as absolute (typically defined outside any section in the linker script.)
105 */
106 [S_REL] =
107 "^pa_",
108
109/*
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300110 * These are 16-bit segment symbols when compiling 16-bit code.
111 */
112 [S_SEG] =
113 "^real_mode_seg$",
114
115/*
116 * These are offsets belonging to segments, as opposed to linear addresses,
117 * when compiling 16-bit code.
118 */
119 [S_LIN] =
120 "^pa_",
121};
122
123static const char * const *sym_regex;
124
125static regex_t sym_regex_c[S_NSYMTYPES];
126static int is_reloc(enum symtype type, const char *sym_name)
H. Peter Anvin873b5272009-12-14 13:55:20 -0800127{
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300128 return sym_regex[type] &&
129 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
H. Peter Anvin873b5272009-12-14 13:55:20 -0800130}
131
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300132static void regex_init(int use_real_mode)
H. Peter Anvin873b5272009-12-14 13:55:20 -0800133{
134 char errbuf[128];
135 int err;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300136 int i;
H. Peter Anvin873b5272009-12-14 13:55:20 -0800137
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300138 if (use_real_mode)
139 sym_regex = sym_regex_realmode;
140 else
141 sym_regex = sym_regex_kernel;
142
143 for (i = 0; i < S_NSYMTYPES; i++) {
144 if (!sym_regex[i])
145 continue;
146
147 err = regcomp(&sym_regex_c[i], sym_regex[i],
148 REG_EXTENDED|REG_NOSUB);
149
150 if (err) {
151 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
152 die("%s", errbuf);
153 }
H. Peter Anvin873b5272009-12-14 13:55:20 -0800154 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100155}
156
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100157static void die(char *fmt, ...)
158{
159 va_list ap;
160 va_start(ap, fmt);
161 vfprintf(stderr, fmt, ap);
162 va_end(ap);
163 exit(1);
164}
165
166static const char *sym_type(unsigned type)
167{
168 static const char *type_name[] = {
169#define SYM_TYPE(X) [X] = #X
170 SYM_TYPE(STT_NOTYPE),
171 SYM_TYPE(STT_OBJECT),
172 SYM_TYPE(STT_FUNC),
173 SYM_TYPE(STT_SECTION),
174 SYM_TYPE(STT_FILE),
175 SYM_TYPE(STT_COMMON),
176 SYM_TYPE(STT_TLS),
177#undef SYM_TYPE
178 };
179 const char *name = "unknown sym type name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100180 if (type < ARRAY_SIZE(type_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100181 name = type_name[type];
182 }
183 return name;
184}
185
186static const char *sym_bind(unsigned bind)
187{
188 static const char *bind_name[] = {
189#define SYM_BIND(X) [X] = #X
190 SYM_BIND(STB_LOCAL),
191 SYM_BIND(STB_GLOBAL),
192 SYM_BIND(STB_WEAK),
193#undef SYM_BIND
194 };
195 const char *name = "unknown sym bind name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100196 if (bind < ARRAY_SIZE(bind_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100197 name = bind_name[bind];
198 }
199 return name;
200}
201
202static const char *sym_visibility(unsigned visibility)
203{
204 static const char *visibility_name[] = {
205#define SYM_VISIBILITY(X) [X] = #X
206 SYM_VISIBILITY(STV_DEFAULT),
207 SYM_VISIBILITY(STV_INTERNAL),
208 SYM_VISIBILITY(STV_HIDDEN),
209 SYM_VISIBILITY(STV_PROTECTED),
210#undef SYM_VISIBILITY
211 };
212 const char *name = "unknown sym visibility name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100213 if (visibility < ARRAY_SIZE(visibility_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100214 name = visibility_name[visibility];
215 }
216 return name;
217}
218
219static const char *rel_type(unsigned type)
220{
221 static const char *type_name[] = {
222#define REL_TYPE(X) [X] = #X
223 REL_TYPE(R_386_NONE),
224 REL_TYPE(R_386_32),
225 REL_TYPE(R_386_PC32),
226 REL_TYPE(R_386_GOT32),
227 REL_TYPE(R_386_PLT32),
228 REL_TYPE(R_386_COPY),
229 REL_TYPE(R_386_GLOB_DAT),
230 REL_TYPE(R_386_JMP_SLOT),
231 REL_TYPE(R_386_RELATIVE),
232 REL_TYPE(R_386_GOTOFF),
233 REL_TYPE(R_386_GOTPC),
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300234 REL_TYPE(R_386_8),
235 REL_TYPE(R_386_PC8),
236 REL_TYPE(R_386_16),
237 REL_TYPE(R_386_PC16),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100238#undef REL_TYPE
239 };
240 const char *name = "unknown type rel type name";
H. Peter Anvin873b5272009-12-14 13:55:20 -0800241 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100242 name = type_name[type];
243 }
244 return name;
245}
246
247static const char *sec_name(unsigned shndx)
248{
249 const char *sec_strtab;
250 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700251 sec_strtab = secs[ehdr.e_shstrndx].strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100252 name = "<noname>";
253 if (shndx < ehdr.e_shnum) {
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700254 name = sec_strtab + secs[shndx].shdr.sh_name;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100255 }
256 else if (shndx == SHN_ABS) {
257 name = "ABSOLUTE";
258 }
259 else if (shndx == SHN_COMMON) {
260 name = "COMMON";
261 }
262 return name;
263}
264
Kees Cookbf116552013-04-12 13:13:42 -0700265static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100266{
267 const char *name;
268 name = "<noname>";
269 if (sym->st_name) {
270 name = sym_strtab + sym->st_name;
271 }
272 else {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300273 name = sec_name(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100274 }
275 return name;
276}
277
278
279
Linus Torvalds13da9e22010-05-26 08:30:15 -0700280#if BYTE_ORDER == LITTLE_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100281#define le16_to_cpu(val) (val)
282#define le32_to_cpu(val) (val)
283#endif
Linus Torvalds13da9e22010-05-26 08:30:15 -0700284#if BYTE_ORDER == BIG_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100285#define le16_to_cpu(val) bswap_16(val)
286#define le32_to_cpu(val) bswap_32(val)
287#endif
288
289static uint16_t elf16_to_cpu(uint16_t val)
290{
291 return le16_to_cpu(val);
292}
293
294static uint32_t elf32_to_cpu(uint32_t val)
295{
296 return le32_to_cpu(val);
297}
298
Kees Cookbf116552013-04-12 13:13:42 -0700299#define elf_half_to_cpu(x) elf16_to_cpu(x)
300#define elf_word_to_cpu(x) elf32_to_cpu(x)
301#define elf_addr_to_cpu(x) elf32_to_cpu(x)
302#define elf_off_to_cpu(x) elf32_to_cpu(x)
303#define elf_xword_to_cpu(x) elf32_to_cpu(x)
304
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100305static void read_ehdr(FILE *fp)
306{
307 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
308 die("Cannot read ELF header: %s\n",
309 strerror(errno));
310 }
Cyrill Gorcunov8bd17962008-05-03 14:18:03 +0400311 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100312 die("No ELF magic\n");
313 }
Kees Cookbf116552013-04-12 13:13:42 -0700314 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) {
315 die("Not a %d bit executable\n", ELF_BITS);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100316 }
317 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
318 die("Not a LSB ELF executable\n");
319 }
320 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
321 die("Unknown ELF version\n");
322 }
323 /* Convert the fields to native endian */
Kees Cookbf116552013-04-12 13:13:42 -0700324 ehdr.e_type = elf_half_to_cpu(ehdr.e_type);
325 ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine);
326 ehdr.e_version = elf_word_to_cpu(ehdr.e_version);
327 ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry);
328 ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff);
329 ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff);
330 ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags);
331 ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize);
332 ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
333 ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum);
334 ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
335 ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum);
336 ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100337
338 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
339 die("Unsupported ELF header type\n");
340 }
Kees Cookbf116552013-04-12 13:13:42 -0700341 if (ehdr.e_machine != ELF_MACHINE) {
342 die("Not for %s\n", ELF_MACHINE_NAME);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100343 }
344 if (ehdr.e_version != EV_CURRENT) {
345 die("Unknown ELF version\n");
346 }
Kees Cookbf116552013-04-12 13:13:42 -0700347 if (ehdr.e_ehsize != sizeof(Elf_Ehdr)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100348 die("Bad Elf header size\n");
349 }
Kees Cookbf116552013-04-12 13:13:42 -0700350 if (ehdr.e_phentsize != sizeof(Elf_Phdr)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100351 die("Bad program header entry\n");
352 }
Kees Cookbf116552013-04-12 13:13:42 -0700353 if (ehdr.e_shentsize != sizeof(Elf_Shdr)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100354 die("Bad section header entry\n");
355 }
356 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
357 die("String table index out of bounds\n");
358 }
359}
360
361static void read_shdrs(FILE *fp)
362{
363 int i;
Kees Cookbf116552013-04-12 13:13:42 -0700364 Elf_Shdr shdr;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700365
366 secs = calloc(ehdr.e_shnum, sizeof(struct section));
367 if (!secs) {
368 die("Unable to allocate %d section headers\n",
369 ehdr.e_shnum);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100370 }
371 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
372 die("Seek to %d failed: %s\n",
373 ehdr.e_shoff, strerror(errno));
374 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700375 for (i = 0; i < ehdr.e_shnum; i++) {
376 struct section *sec = &secs[i];
377 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
378 die("Cannot read ELF section headers %d/%d: %s\n",
379 i, ehdr.e_shnum, strerror(errno));
Kees Cookbf116552013-04-12 13:13:42 -0700380 sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
381 sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
382 sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags);
383 sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr);
384 sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset);
385 sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size);
386 sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link);
387 sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info);
388 sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
389 sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize);
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700390 if (sec->shdr.sh_link < ehdr.e_shnum)
391 sec->link = &secs[sec->shdr.sh_link];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100392 }
393
394}
395
396static void read_strtabs(FILE *fp)
397{
398 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700399 for (i = 0; i < ehdr.e_shnum; i++) {
400 struct section *sec = &secs[i];
401 if (sec->shdr.sh_type != SHT_STRTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100402 continue;
403 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700404 sec->strtab = malloc(sec->shdr.sh_size);
405 if (!sec->strtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100406 die("malloc of %d bytes for strtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700407 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100408 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700409 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100410 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700411 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100412 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700413 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
414 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100415 die("Cannot read symbol table: %s\n",
416 strerror(errno));
417 }
418 }
419}
420
421static void read_symtabs(FILE *fp)
422{
423 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700424 for (i = 0; i < ehdr.e_shnum; i++) {
425 struct section *sec = &secs[i];
426 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100427 continue;
428 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700429 sec->symtab = malloc(sec->shdr.sh_size);
430 if (!sec->symtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100431 die("malloc of %d bytes for symtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700432 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100433 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700434 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100435 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700436 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100437 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700438 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
439 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100440 die("Cannot read symbol table: %s\n",
441 strerror(errno));
442 }
Kees Cookbf116552013-04-12 13:13:42 -0700443 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
444 Elf_Sym *sym = &sec->symtab[j];
445 sym->st_name = elf_word_to_cpu(sym->st_name);
446 sym->st_value = elf_addr_to_cpu(sym->st_value);
447 sym->st_size = elf_xword_to_cpu(sym->st_size);
448 sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100449 }
450 }
451}
452
453
454static void read_relocs(FILE *fp)
455{
456 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700457 for (i = 0; i < ehdr.e_shnum; i++) {
458 struct section *sec = &secs[i];
Kees Cookbf116552013-04-12 13:13:42 -0700459 if (sec->shdr.sh_type != SHT_REL_TYPE) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100460 continue;
461 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700462 sec->reltab = malloc(sec->shdr.sh_size);
463 if (!sec->reltab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100464 die("malloc of %d bytes for relocs failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700465 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100466 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700467 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100468 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700469 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100470 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700471 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
472 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100473 die("Cannot read symbol table: %s\n",
474 strerror(errno));
475 }
Kees Cookbf116552013-04-12 13:13:42 -0700476 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
477 Elf_Rel *rel = &sec->reltab[j];
478 rel->r_offset = elf_addr_to_cpu(rel->r_offset);
479 rel->r_info = elf_xword_to_cpu(rel->r_info);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100480 }
481 }
482}
483
484
485static void print_absolute_symbols(void)
486{
487 int i;
488 printf("Absolute symbols\n");
489 printf(" Num: Value Size Type Bind Visibility Name\n");
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700490 for (i = 0; i < ehdr.e_shnum; i++) {
491 struct section *sec = &secs[i];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100492 char *sym_strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100493 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700494
495 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100496 continue;
497 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700498 sym_strtab = sec->link->strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700499 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
500 Elf_Sym *sym;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100501 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700502 sym = &sec->symtab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100503 name = sym_name(sym_strtab, sym);
504 if (sym->st_shndx != SHN_ABS) {
505 continue;
506 }
507 printf("%5d %08x %5d %10s %10s %12s %s\n",
508 j, sym->st_value, sym->st_size,
Kees Cookbf116552013-04-12 13:13:42 -0700509 sym_type(ELF_ST_TYPE(sym->st_info)),
510 sym_bind(ELF_ST_BIND(sym->st_info)),
511 sym_visibility(ELF_ST_VISIBILITY(sym->st_other)),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100512 name);
513 }
514 }
515 printf("\n");
516}
517
518static void print_absolute_relocs(void)
519{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100520 int i, printed = 0;
521
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700522 for (i = 0; i < ehdr.e_shnum; i++) {
523 struct section *sec = &secs[i];
524 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100525 char *sym_strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700526 Elf_Sym *sh_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100527 int j;
Kees Cookbf116552013-04-12 13:13:42 -0700528 if (sec->shdr.sh_type != SHT_REL_TYPE) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100529 continue;
530 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700531 sec_symtab = sec->link;
532 sec_applies = &secs[sec->shdr.sh_info];
533 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100534 continue;
535 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700536 sh_symtab = sec_symtab->symtab;
537 sym_strtab = sec_symtab->link->strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700538 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
539 Elf_Rel *rel;
540 Elf_Sym *sym;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100541 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700542 rel = &sec->reltab[j];
Kees Cookbf116552013-04-12 13:13:42 -0700543 sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100544 name = sym_name(sym_strtab, sym);
545 if (sym->st_shndx != SHN_ABS) {
546 continue;
547 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100548
549 /* Absolute symbols are not relocated if bzImage is
550 * loaded at a non-compiled address. Display a warning
551 * to user at compile time about the absolute
552 * relocations present.
553 *
554 * User need to audit the code to make sure
555 * some symbols which should have been section
556 * relative have not become absolute because of some
557 * linker optimization or wrong programming usage.
558 *
559 * Before warning check if this absolute symbol
560 * relocation is harmless.
561 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300562 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
Vivek Goyal6a044b32006-12-07 02:14:04 +0100563 continue;
564
565 if (!printed) {
566 printf("WARNING: Absolute relocations"
567 " present\n");
568 printf("Offset Info Type Sym.Value "
569 "Sym.Name\n");
570 printed = 1;
571 }
572
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100573 printf("%08x %08x %10s %08x %s\n",
574 rel->r_offset,
575 rel->r_info,
Kees Cookbf116552013-04-12 13:13:42 -0700576 rel_type(ELF_R_TYPE(rel->r_info)),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100577 sym->st_value,
578 name);
579 }
580 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100581
582 if (printed)
583 printf("\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100584}
585
Kees Cookbf116552013-04-12 13:13:42 -0700586static void walk_relocs(void (*visit)(Elf_Rel *rel, Elf_Sym *sym),
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300587 int use_real_mode)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100588{
589 int i;
590 /* Walk through the relocations */
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700591 for (i = 0; i < ehdr.e_shnum; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100592 char *sym_strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700593 Elf_Sym *sh_symtab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700594 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100595 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700596 struct section *sec = &secs[i];
597
Kees Cookbf116552013-04-12 13:13:42 -0700598 if (sec->shdr.sh_type != SHT_REL_TYPE) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100599 continue;
600 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700601 sec_symtab = sec->link;
602 sec_applies = &secs[sec->shdr.sh_info];
603 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100604 continue;
605 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700606 sh_symtab = sec_symtab->symtab;
H. Peter Anvincc65f1e2008-10-03 13:00:56 -0700607 sym_strtab = sec_symtab->link->strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700608 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
609 Elf_Rel *rel;
610 Elf_Sym *sym;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100611 unsigned r_type;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300612 const char *symname;
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700613 int shn_abs;
614
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700615 rel = &sec->reltab[j];
Kees Cookbf116552013-04-12 13:13:42 -0700616 sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
617 r_type = ELF_R_TYPE(rel->r_info);
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300618
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700619 shn_abs = sym->st_shndx == SHN_ABS;
620
H. Peter Anvin873b5272009-12-14 13:55:20 -0800621 switch (r_type) {
622 case R_386_NONE:
623 case R_386_PC32:
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300624 case R_386_PC16:
625 case R_386_PC8:
Tejun Heo46176b42009-05-26 14:42:40 +0900626 /*
627 * NONE can be ignored and and PC relative
628 * relocations don't need to be adjusted.
629 */
H. Peter Anvin873b5272009-12-14 13:55:20 -0800630 break;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300631
632 case R_386_16:
633 symname = sym_name(sym_strtab, sym);
634 if (!use_real_mode)
635 goto bad;
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700636 if (shn_abs) {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300637 if (is_reloc(S_ABS, symname))
638 break;
639 else if (!is_reloc(S_SEG, symname))
640 goto bad;
641 } else {
642 if (is_reloc(S_LIN, symname))
643 goto bad;
644 else
645 break;
646 }
647 visit(rel, sym);
648 break;
649
H. Peter Anvin873b5272009-12-14 13:55:20 -0800650 case R_386_32:
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300651 symname = sym_name(sym_strtab, sym);
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700652 if (shn_abs) {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300653 if (is_reloc(S_ABS, symname))
654 break;
655 else if (!is_reloc(S_REL, symname))
656 goto bad;
657 } else {
658 if (use_real_mode &&
659 !is_reloc(S_LIN, symname))
660 break;
661 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100662 visit(rel, sym);
H. Peter Anvin873b5272009-12-14 13:55:20 -0800663 break;
664 default:
665 die("Unsupported relocation type: %s (%d)\n",
666 rel_type(r_type), r_type);
667 break;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300668 bad:
669 symname = sym_name(sym_strtab, sym);
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700670 die("Invalid %s %s relocation: %s\n",
671 shn_abs ? "absolute" : "relative",
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300672 rel_type(r_type), symname);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100673 }
674 }
675 }
676}
677
Kees Cookbf116552013-04-12 13:13:42 -0700678static void count_reloc(Elf_Rel *rel, Elf_Sym *sym)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100679{
Kees Cookbf116552013-04-12 13:13:42 -0700680 if (ELF_R_TYPE(rel->r_info) == R_386_16)
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300681 reloc16_count++;
682 else
683 reloc_count++;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100684}
685
Kees Cookbf116552013-04-12 13:13:42 -0700686static void collect_reloc(Elf_Rel *rel, Elf_Sym *sym)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100687{
688 /* Remember the address that needs to be adjusted. */
Kees Cookbf116552013-04-12 13:13:42 -0700689 if (ELF_R_TYPE(rel->r_info) == R_386_16)
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300690 relocs16[reloc16_idx++] = rel->r_offset;
691 else
692 relocs[reloc_idx++] = rel->r_offset;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100693}
694
695static int cmp_relocs(const void *va, const void *vb)
696{
697 const unsigned long *a, *b;
698 a = va; b = vb;
699 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
700}
701
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300702static int write32(unsigned int v, FILE *f)
703{
704 unsigned char buf[4];
705
706 put_unaligned_le32(v, buf);
707 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
708}
709
710static void emit_relocs(int as_text, int use_real_mode)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100711{
712 int i;
713 /* Count how many relocations I have and allocate space for them. */
714 reloc_count = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300715 walk_relocs(count_reloc, use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100716 relocs = malloc(reloc_count * sizeof(relocs[0]));
717 if (!relocs) {
718 die("malloc of %d entries for relocs failed\n",
719 reloc_count);
720 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300721
722 relocs16 = malloc(reloc16_count * sizeof(relocs[0]));
723 if (!relocs16) {
724 die("malloc of %d entries for relocs16 failed\n",
725 reloc16_count);
726 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100727 /* Collect up the relocations */
728 reloc_idx = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300729 walk_relocs(collect_reloc, use_real_mode);
730
731 if (reloc16_count && !use_real_mode)
732 die("Segment relocations found but --realmode not specified\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100733
734 /* Order the relocations for more efficient processing */
735 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300736 qsort(relocs16, reloc16_count, sizeof(relocs16[0]), cmp_relocs);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100737
738 /* Print the relocations */
739 if (as_text) {
740 /* Print the relocations in a form suitable that
741 * gas will like.
742 */
743 printf(".section \".data.reloc\",\"a\"\n");
744 printf(".balign 4\n");
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300745 if (use_real_mode) {
746 printf("\t.long %lu\n", reloc16_count);
747 for (i = 0; i < reloc16_count; i++)
748 printf("\t.long 0x%08lx\n", relocs16[i]);
749 printf("\t.long %lu\n", reloc_count);
750 for (i = 0; i < reloc_count; i++) {
751 printf("\t.long 0x%08lx\n", relocs[i]);
752 }
753 } else {
754 /* Print a stop */
755 printf("\t.long 0x%08lx\n", (unsigned long)0);
756 for (i = 0; i < reloc_count; i++) {
757 printf("\t.long 0x%08lx\n", relocs[i]);
758 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100759 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300760
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100761 printf("\n");
762 }
763 else {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300764 if (use_real_mode) {
765 write32(reloc16_count, stdout);
766 for (i = 0; i < reloc16_count; i++)
767 write32(relocs16[i], stdout);
768 write32(reloc_count, stdout);
769
770 /* Now print each relocation */
771 for (i = 0; i < reloc_count; i++)
772 write32(relocs[i], stdout);
773 } else {
774 /* Print a stop */
775 write32(0, stdout);
776
777 /* Now print each relocation */
778 for (i = 0; i < reloc_count; i++) {
779 write32(relocs[i], stdout);
780 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100781 }
782 }
783}
784
785static void usage(void)
786{
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300787 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100788}
789
790int main(int argc, char **argv)
791{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100792 int show_absolute_syms, show_absolute_relocs;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300793 int as_text, use_real_mode;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100794 const char *fname;
795 FILE *fp;
796 int i;
797
Vivek Goyal6a044b32006-12-07 02:14:04 +0100798 show_absolute_syms = 0;
799 show_absolute_relocs = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100800 as_text = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300801 use_real_mode = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100802 fname = NULL;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700803 for (i = 1; i < argc; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100804 char *arg = argv[i];
805 if (*arg == '-') {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300806 if (strcmp(arg, "--abs-syms") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100807 show_absolute_syms = 1;
808 continue;
809 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300810 if (strcmp(arg, "--abs-relocs") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100811 show_absolute_relocs = 1;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100812 continue;
813 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300814 if (strcmp(arg, "--text") == 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100815 as_text = 1;
816 continue;
817 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300818 if (strcmp(arg, "--realmode") == 0) {
819 use_real_mode = 1;
820 continue;
821 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100822 }
823 else if (!fname) {
824 fname = arg;
825 continue;
826 }
827 usage();
828 }
829 if (!fname) {
830 usage();
831 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300832 regex_init(use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100833 fp = fopen(fname, "r");
834 if (!fp) {
835 die("Cannot open %s: %s\n",
836 fname, strerror(errno));
837 }
838 read_ehdr(fp);
839 read_shdrs(fp);
840 read_strtabs(fp);
841 read_symtabs(fp);
842 read_relocs(fp);
Vivek Goyal6a044b32006-12-07 02:14:04 +0100843 if (show_absolute_syms) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100844 print_absolute_symbols();
Cong Ding65315d42013-01-14 17:13:35 +0000845 goto out;
Vivek Goyal6a044b32006-12-07 02:14:04 +0100846 }
847 if (show_absolute_relocs) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100848 print_absolute_relocs();
Cong Ding65315d42013-01-14 17:13:35 +0000849 goto out;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100850 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300851 emit_relocs(as_text, use_real_mode);
Cong Ding65315d42013-01-14 17:13:35 +0000852out:
853 fclose(fp);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100854 return 0;
855}