blob: bdc5930b3a1b4a261c44ca522aeee3191763678e [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>
Kees Cook5d442e62013-04-12 13:13:43 -07005#include <inttypes.h>
Eric W. Biederman968de4f2006-12-07 02:14:04 +01006#include <string.h>
7#include <errno.h>
8#include <unistd.h>
9#include <elf.h>
10#include <byteswap.h>
11#define USE_BSD
12#include <endian.h>
H. Peter Anvin873b5272009-12-14 13:55:20 -080013#include <regex.h>
Matt Fleming55f97092012-02-28 13:37:21 +000014#include <tools/le_byteshift.h>
H. Peter Anvin873b5272009-12-14 13:55:20 -080015
Kees Cookbf116552013-04-12 13:13:42 -070016#define ElfW(type) _ElfW(ELF_BITS, type)
17#define _ElfW(bits, type) __ElfW(bits, type)
18#define __ElfW(bits, type) Elf##bits##_##type
19
20#define ELF_BITS 32
21#define ELF_MACHINE EM_386
22#define ELF_MACHINE_NAME "i386"
23#define SHT_REL_TYPE SHT_REL
24
25#define ELF_CLASS ELFCLASS32
26#define ELF_R_SYM(val) ELF32_R_SYM(val)
27#define ELF_R_TYPE(val) ELF32_R_TYPE(val)
28#define ELF_ST_TYPE(o) ELF32_ST_TYPE(o)
29#define ELF_ST_BIND(o) ELF32_ST_BIND(o)
30#define ELF_ST_VISIBILITY(o) ELF32_ST_VISIBILITY(o)
31
32#define Elf_Rel ElfW(Rel)
33#define Elf_Ehdr ElfW(Ehdr)
34#define Elf_Phdr ElfW(Phdr)
35#define Elf_Shdr ElfW(Shdr)
36#define Elf_Sym ElfW(Sym)
37
H. Peter Anvin873b5272009-12-14 13:55:20 -080038static void die(char *fmt, ...);
Eric W. Biederman968de4f2006-12-07 02:14:04 +010039
Robert P. J. Dayca820182007-02-17 19:10:01 +010040#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Kees Cookbf116552013-04-12 13:13:42 -070041static Elf_Ehdr ehdr;
Kees Cook5d442e62013-04-12 13:13:43 -070042
43struct relocs {
44 uint32_t *offset;
45 unsigned long count;
46 unsigned long size;
47};
48
49static struct relocs relocs16;
50static struct relocs relocs32;
Eric W. Biederman968de4f2006-12-07 02:14:04 +010051
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070052struct section {
Kees Cookbf116552013-04-12 13:13:42 -070053 Elf_Shdr shdr;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070054 struct section *link;
Kees Cookbf116552013-04-12 13:13:42 -070055 Elf_Sym *symtab;
56 Elf_Rel *reltab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070057 char *strtab;
58};
59static struct section *secs;
60
H. Peter Anvin6520fe52012-05-08 21:22:24 +030061enum symtype {
62 S_ABS,
63 S_REL,
64 S_SEG,
65 S_LIN,
66 S_NSYMTYPES
67};
68
69static const char * const sym_regex_kernel[S_NSYMTYPES] = {
Vivek Goyal6a044b32006-12-07 02:14:04 +010070/*
71 * Following symbols have been audited. There values are constant and do
72 * not change if bzImage is loaded at a different physical address than
73 * the address for which it has been compiled. Don't warn user about
74 * absolute relocations present w.r.t these symbols.
75 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +030076 [S_ABS] =
H. Peter Anvin873b5272009-12-14 13:55:20 -080077 "^(xen_irq_disable_direct_reloc$|"
78 "xen_save_fl_direct_reloc$|"
79 "VDSO|"
H. Peter Anvin6520fe52012-05-08 21:22:24 +030080 "__crc_)",
Vivek Goyal6a044b32006-12-07 02:14:04 +010081
H. Peter Anvin873b5272009-12-14 13:55:20 -080082/*
83 * These symbols are known to be relative, even if the linker marks them
84 * as absolute (typically defined outside any section in the linker script.)
85 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +030086 [S_REL] =
H. Peter Anvina3e854d2012-05-18 00:24:09 -070087 "^(__init_(begin|end)|"
88 "__x86_cpu_dev_(start|end)|"
89 "(__parainstructions|__alt_instructions)(|_end)|"
90 "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
H. Peter Anvinfd952812012-05-23 14:02:34 -070091 "__(start|end)_pci_.*|"
92 "__(start|end)_builtin_fw|"
93 "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
94 "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
95 "__(start|stop)___param|"
96 "__(start|stop)___modver|"
97 "__(start|stop)___bug_table|"
98 "__tracedata_(start|end)|"
99 "__(start|stop)_notes|"
100 "__end_rodata|"
101 "__initramfs_start|"
H. Peter Anvinea17e742012-05-24 07:01:38 -0700102 "(jiffies|jiffies_64)|"
H. Peter Anvina3e854d2012-05-18 00:24:09 -0700103 "_end)$"
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300104};
105
106
107static const char * const sym_regex_realmode[S_NSYMTYPES] = {
108/*
H. Peter Anvin2a6de312012-05-08 21:22:31 +0300109 * These symbols are known to be relative, even if the linker marks them
110 * as absolute (typically defined outside any section in the linker script.)
111 */
112 [S_REL] =
113 "^pa_",
114
115/*
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300116 * These are 16-bit segment symbols when compiling 16-bit code.
117 */
118 [S_SEG] =
119 "^real_mode_seg$",
120
121/*
122 * These are offsets belonging to segments, as opposed to linear addresses,
123 * when compiling 16-bit code.
124 */
125 [S_LIN] =
126 "^pa_",
127};
128
129static const char * const *sym_regex;
130
131static regex_t sym_regex_c[S_NSYMTYPES];
132static int is_reloc(enum symtype type, const char *sym_name)
H. Peter Anvin873b5272009-12-14 13:55:20 -0800133{
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300134 return sym_regex[type] &&
135 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
H. Peter Anvin873b5272009-12-14 13:55:20 -0800136}
137
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300138static void regex_init(int use_real_mode)
H. Peter Anvin873b5272009-12-14 13:55:20 -0800139{
140 char errbuf[128];
141 int err;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300142 int i;
H. Peter Anvin873b5272009-12-14 13:55:20 -0800143
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300144 if (use_real_mode)
145 sym_regex = sym_regex_realmode;
146 else
147 sym_regex = sym_regex_kernel;
148
149 for (i = 0; i < S_NSYMTYPES; i++) {
150 if (!sym_regex[i])
151 continue;
152
153 err = regcomp(&sym_regex_c[i], sym_regex[i],
154 REG_EXTENDED|REG_NOSUB);
155
156 if (err) {
157 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
158 die("%s", errbuf);
159 }
H. Peter Anvin873b5272009-12-14 13:55:20 -0800160 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100161}
162
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100163static void die(char *fmt, ...)
164{
165 va_list ap;
166 va_start(ap, fmt);
167 vfprintf(stderr, fmt, ap);
168 va_end(ap);
169 exit(1);
170}
171
172static const char *sym_type(unsigned type)
173{
174 static const char *type_name[] = {
175#define SYM_TYPE(X) [X] = #X
176 SYM_TYPE(STT_NOTYPE),
177 SYM_TYPE(STT_OBJECT),
178 SYM_TYPE(STT_FUNC),
179 SYM_TYPE(STT_SECTION),
180 SYM_TYPE(STT_FILE),
181 SYM_TYPE(STT_COMMON),
182 SYM_TYPE(STT_TLS),
183#undef SYM_TYPE
184 };
185 const char *name = "unknown sym type name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100186 if (type < ARRAY_SIZE(type_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100187 name = type_name[type];
188 }
189 return name;
190}
191
192static const char *sym_bind(unsigned bind)
193{
194 static const char *bind_name[] = {
195#define SYM_BIND(X) [X] = #X
196 SYM_BIND(STB_LOCAL),
197 SYM_BIND(STB_GLOBAL),
198 SYM_BIND(STB_WEAK),
199#undef SYM_BIND
200 };
201 const char *name = "unknown sym bind name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100202 if (bind < ARRAY_SIZE(bind_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100203 name = bind_name[bind];
204 }
205 return name;
206}
207
208static const char *sym_visibility(unsigned visibility)
209{
210 static const char *visibility_name[] = {
211#define SYM_VISIBILITY(X) [X] = #X
212 SYM_VISIBILITY(STV_DEFAULT),
213 SYM_VISIBILITY(STV_INTERNAL),
214 SYM_VISIBILITY(STV_HIDDEN),
215 SYM_VISIBILITY(STV_PROTECTED),
216#undef SYM_VISIBILITY
217 };
218 const char *name = "unknown sym visibility name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100219 if (visibility < ARRAY_SIZE(visibility_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100220 name = visibility_name[visibility];
221 }
222 return name;
223}
224
225static const char *rel_type(unsigned type)
226{
227 static const char *type_name[] = {
228#define REL_TYPE(X) [X] = #X
229 REL_TYPE(R_386_NONE),
230 REL_TYPE(R_386_32),
231 REL_TYPE(R_386_PC32),
232 REL_TYPE(R_386_GOT32),
233 REL_TYPE(R_386_PLT32),
234 REL_TYPE(R_386_COPY),
235 REL_TYPE(R_386_GLOB_DAT),
236 REL_TYPE(R_386_JMP_SLOT),
237 REL_TYPE(R_386_RELATIVE),
238 REL_TYPE(R_386_GOTOFF),
239 REL_TYPE(R_386_GOTPC),
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300240 REL_TYPE(R_386_8),
241 REL_TYPE(R_386_PC8),
242 REL_TYPE(R_386_16),
243 REL_TYPE(R_386_PC16),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100244#undef REL_TYPE
245 };
246 const char *name = "unknown type rel type name";
H. Peter Anvin873b5272009-12-14 13:55:20 -0800247 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100248 name = type_name[type];
249 }
250 return name;
251}
252
253static const char *sec_name(unsigned shndx)
254{
255 const char *sec_strtab;
256 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700257 sec_strtab = secs[ehdr.e_shstrndx].strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100258 name = "<noname>";
259 if (shndx < ehdr.e_shnum) {
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700260 name = sec_strtab + secs[shndx].shdr.sh_name;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100261 }
262 else if (shndx == SHN_ABS) {
263 name = "ABSOLUTE";
264 }
265 else if (shndx == SHN_COMMON) {
266 name = "COMMON";
267 }
268 return name;
269}
270
Kees Cookbf116552013-04-12 13:13:42 -0700271static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100272{
273 const char *name;
274 name = "<noname>";
275 if (sym->st_name) {
276 name = sym_strtab + sym->st_name;
277 }
278 else {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300279 name = sec_name(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100280 }
281 return name;
282}
283
284
285
Linus Torvalds13da9e22010-05-26 08:30:15 -0700286#if BYTE_ORDER == LITTLE_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100287#define le16_to_cpu(val) (val)
288#define le32_to_cpu(val) (val)
289#endif
Linus Torvalds13da9e22010-05-26 08:30:15 -0700290#if BYTE_ORDER == BIG_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100291#define le16_to_cpu(val) bswap_16(val)
292#define le32_to_cpu(val) bswap_32(val)
293#endif
294
295static uint16_t elf16_to_cpu(uint16_t val)
296{
297 return le16_to_cpu(val);
298}
299
300static uint32_t elf32_to_cpu(uint32_t val)
301{
302 return le32_to_cpu(val);
303}
304
Kees Cookbf116552013-04-12 13:13:42 -0700305#define elf_half_to_cpu(x) elf16_to_cpu(x)
306#define elf_word_to_cpu(x) elf32_to_cpu(x)
307#define elf_addr_to_cpu(x) elf32_to_cpu(x)
308#define elf_off_to_cpu(x) elf32_to_cpu(x)
309#define elf_xword_to_cpu(x) elf32_to_cpu(x)
310
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100311static void read_ehdr(FILE *fp)
312{
313 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
314 die("Cannot read ELF header: %s\n",
315 strerror(errno));
316 }
Cyrill Gorcunov8bd17962008-05-03 14:18:03 +0400317 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100318 die("No ELF magic\n");
319 }
Kees Cookbf116552013-04-12 13:13:42 -0700320 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) {
321 die("Not a %d bit executable\n", ELF_BITS);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100322 }
323 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
324 die("Not a LSB ELF executable\n");
325 }
326 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
327 die("Unknown ELF version\n");
328 }
329 /* Convert the fields to native endian */
Kees Cookbf116552013-04-12 13:13:42 -0700330 ehdr.e_type = elf_half_to_cpu(ehdr.e_type);
331 ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine);
332 ehdr.e_version = elf_word_to_cpu(ehdr.e_version);
333 ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry);
334 ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff);
335 ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff);
336 ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags);
337 ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize);
338 ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
339 ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum);
340 ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
341 ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum);
342 ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100343
344 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
345 die("Unsupported ELF header type\n");
346 }
Kees Cookbf116552013-04-12 13:13:42 -0700347 if (ehdr.e_machine != ELF_MACHINE) {
348 die("Not for %s\n", ELF_MACHINE_NAME);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100349 }
350 if (ehdr.e_version != EV_CURRENT) {
351 die("Unknown ELF version\n");
352 }
Kees Cookbf116552013-04-12 13:13:42 -0700353 if (ehdr.e_ehsize != sizeof(Elf_Ehdr)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100354 die("Bad Elf header size\n");
355 }
Kees Cookbf116552013-04-12 13:13:42 -0700356 if (ehdr.e_phentsize != sizeof(Elf_Phdr)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100357 die("Bad program header entry\n");
358 }
Kees Cookbf116552013-04-12 13:13:42 -0700359 if (ehdr.e_shentsize != sizeof(Elf_Shdr)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100360 die("Bad section header entry\n");
361 }
362 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
363 die("String table index out of bounds\n");
364 }
365}
366
367static void read_shdrs(FILE *fp)
368{
369 int i;
Kees Cookbf116552013-04-12 13:13:42 -0700370 Elf_Shdr shdr;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700371
372 secs = calloc(ehdr.e_shnum, sizeof(struct section));
373 if (!secs) {
374 die("Unable to allocate %d section headers\n",
375 ehdr.e_shnum);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100376 }
377 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
378 die("Seek to %d failed: %s\n",
379 ehdr.e_shoff, strerror(errno));
380 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700381 for (i = 0; i < ehdr.e_shnum; i++) {
382 struct section *sec = &secs[i];
383 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
384 die("Cannot read ELF section headers %d/%d: %s\n",
385 i, ehdr.e_shnum, strerror(errno));
Kees Cookbf116552013-04-12 13:13:42 -0700386 sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
387 sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
388 sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags);
389 sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr);
390 sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset);
391 sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size);
392 sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link);
393 sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info);
394 sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
395 sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize);
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700396 if (sec->shdr.sh_link < ehdr.e_shnum)
397 sec->link = &secs[sec->shdr.sh_link];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100398 }
399
400}
401
402static void read_strtabs(FILE *fp)
403{
404 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700405 for (i = 0; i < ehdr.e_shnum; i++) {
406 struct section *sec = &secs[i];
407 if (sec->shdr.sh_type != SHT_STRTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100408 continue;
409 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700410 sec->strtab = malloc(sec->shdr.sh_size);
411 if (!sec->strtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100412 die("malloc of %d bytes for strtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700413 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100414 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700415 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100416 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700417 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100418 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700419 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
420 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100421 die("Cannot read symbol table: %s\n",
422 strerror(errno));
423 }
424 }
425}
426
427static void read_symtabs(FILE *fp)
428{
429 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700430 for (i = 0; i < ehdr.e_shnum; i++) {
431 struct section *sec = &secs[i];
432 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100433 continue;
434 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700435 sec->symtab = malloc(sec->shdr.sh_size);
436 if (!sec->symtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100437 die("malloc of %d bytes for symtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700438 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100439 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700440 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100441 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700442 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100443 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700444 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
445 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100446 die("Cannot read symbol table: %s\n",
447 strerror(errno));
448 }
Kees Cookbf116552013-04-12 13:13:42 -0700449 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
450 Elf_Sym *sym = &sec->symtab[j];
451 sym->st_name = elf_word_to_cpu(sym->st_name);
452 sym->st_value = elf_addr_to_cpu(sym->st_value);
453 sym->st_size = elf_xword_to_cpu(sym->st_size);
454 sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100455 }
456 }
457}
458
459
460static void read_relocs(FILE *fp)
461{
462 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700463 for (i = 0; i < ehdr.e_shnum; i++) {
464 struct section *sec = &secs[i];
Kees Cookbf116552013-04-12 13:13:42 -0700465 if (sec->shdr.sh_type != SHT_REL_TYPE) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100466 continue;
467 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700468 sec->reltab = malloc(sec->shdr.sh_size);
469 if (!sec->reltab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100470 die("malloc of %d bytes for relocs failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700471 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100472 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700473 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100474 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700475 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100476 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700477 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
478 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100479 die("Cannot read symbol table: %s\n",
480 strerror(errno));
481 }
Kees Cookbf116552013-04-12 13:13:42 -0700482 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
483 Elf_Rel *rel = &sec->reltab[j];
484 rel->r_offset = elf_addr_to_cpu(rel->r_offset);
485 rel->r_info = elf_xword_to_cpu(rel->r_info);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100486 }
487 }
488}
489
490
491static void print_absolute_symbols(void)
492{
493 int i;
494 printf("Absolute symbols\n");
495 printf(" Num: Value Size Type Bind Visibility Name\n");
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700496 for (i = 0; i < ehdr.e_shnum; i++) {
497 struct section *sec = &secs[i];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100498 char *sym_strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100499 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700500
501 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100502 continue;
503 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700504 sym_strtab = sec->link->strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700505 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
506 Elf_Sym *sym;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100507 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700508 sym = &sec->symtab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100509 name = sym_name(sym_strtab, sym);
510 if (sym->st_shndx != SHN_ABS) {
511 continue;
512 }
513 printf("%5d %08x %5d %10s %10s %12s %s\n",
514 j, sym->st_value, sym->st_size,
Kees Cookbf116552013-04-12 13:13:42 -0700515 sym_type(ELF_ST_TYPE(sym->st_info)),
516 sym_bind(ELF_ST_BIND(sym->st_info)),
517 sym_visibility(ELF_ST_VISIBILITY(sym->st_other)),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100518 name);
519 }
520 }
521 printf("\n");
522}
523
524static void print_absolute_relocs(void)
525{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100526 int i, printed = 0;
527
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700528 for (i = 0; i < ehdr.e_shnum; i++) {
529 struct section *sec = &secs[i];
530 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100531 char *sym_strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700532 Elf_Sym *sh_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100533 int j;
Kees Cookbf116552013-04-12 13:13:42 -0700534 if (sec->shdr.sh_type != SHT_REL_TYPE) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100535 continue;
536 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700537 sec_symtab = sec->link;
538 sec_applies = &secs[sec->shdr.sh_info];
539 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100540 continue;
541 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700542 sh_symtab = sec_symtab->symtab;
543 sym_strtab = sec_symtab->link->strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700544 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
545 Elf_Rel *rel;
546 Elf_Sym *sym;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100547 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700548 rel = &sec->reltab[j];
Kees Cookbf116552013-04-12 13:13:42 -0700549 sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100550 name = sym_name(sym_strtab, sym);
551 if (sym->st_shndx != SHN_ABS) {
552 continue;
553 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100554
555 /* Absolute symbols are not relocated if bzImage is
556 * loaded at a non-compiled address. Display a warning
557 * to user at compile time about the absolute
558 * relocations present.
559 *
560 * User need to audit the code to make sure
561 * some symbols which should have been section
562 * relative have not become absolute because of some
563 * linker optimization or wrong programming usage.
564 *
565 * Before warning check if this absolute symbol
566 * relocation is harmless.
567 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300568 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
Vivek Goyal6a044b32006-12-07 02:14:04 +0100569 continue;
570
571 if (!printed) {
572 printf("WARNING: Absolute relocations"
573 " present\n");
574 printf("Offset Info Type Sym.Value "
575 "Sym.Name\n");
576 printed = 1;
577 }
578
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100579 printf("%08x %08x %10s %08x %s\n",
580 rel->r_offset,
581 rel->r_info,
Kees Cookbf116552013-04-12 13:13:42 -0700582 rel_type(ELF_R_TYPE(rel->r_info)),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100583 sym->st_value,
584 name);
585 }
586 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100587
588 if (printed)
589 printf("\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100590}
591
Kees Cook5d442e62013-04-12 13:13:43 -0700592static void add_reloc(struct relocs *r, uint32_t offset)
593{
594 if (r->count == r->size) {
595 unsigned long newsize = r->size + 50000;
596 void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
597
598 if (!mem)
599 die("realloc of %ld entries for relocs failed\n",
600 newsize);
601 r->offset = mem;
602 r->size = newsize;
603 }
604 r->offset[r->count++] = offset;
605}
606
607static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
608 Elf_Sym *sym, const char *symname))
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100609{
610 int i;
611 /* Walk through the relocations */
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700612 for (i = 0; i < ehdr.e_shnum; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100613 char *sym_strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700614 Elf_Sym *sh_symtab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700615 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100616 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700617 struct section *sec = &secs[i];
618
Kees Cookbf116552013-04-12 13:13:42 -0700619 if (sec->shdr.sh_type != SHT_REL_TYPE) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100620 continue;
621 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700622 sec_symtab = sec->link;
623 sec_applies = &secs[sec->shdr.sh_info];
624 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100625 continue;
626 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700627 sh_symtab = sec_symtab->symtab;
H. Peter Anvincc65f1e2008-10-03 13:00:56 -0700628 sym_strtab = sec_symtab->link->strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700629 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
Kees Cook5d442e62013-04-12 13:13:43 -0700630 Elf_Rel *rel = &sec->reltab[j];
631 Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
632 const char *symname = sym_name(sym_strtab, sym);
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700633
Kees Cook5d442e62013-04-12 13:13:43 -0700634 process(sec, rel, sym, symname);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100635 }
636 }
637}
638
Kees Cook5d442e62013-04-12 13:13:43 -0700639static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
640 const char *symname)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100641{
Kees Cook5d442e62013-04-12 13:13:43 -0700642 unsigned r_type = ELF32_R_TYPE(rel->r_info);
643 int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
644
645 switch (r_type) {
646 case R_386_NONE:
647 case R_386_PC32:
648 case R_386_PC16:
649 case R_386_PC8:
650 /*
651 * NONE can be ignored and PC relative relocations don't
652 * need to be adjusted.
653 */
654 break;
655
656 case R_386_32:
657 if (shn_abs) {
658 /*
659 * Whitelisted absolute symbols do not require
660 * relocation.
661 */
662 if (is_reloc(S_ABS, symname))
663 break;
664
665 die("Invalid absolute %s relocation: %s\n",
666 rel_type(r_type), symname);
667 break;
668 }
669
670 add_reloc(&relocs32, rel->r_offset);
671 break;
672
673 default:
674 die("Unsupported relocation type: %s (%d)\n",
675 rel_type(r_type), r_type);
676 break;
677 }
678
679 return 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100680}
681
Kees Cook5d442e62013-04-12 13:13:43 -0700682static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
683 const char *symname)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100684{
Kees Cook5d442e62013-04-12 13:13:43 -0700685 unsigned r_type = ELF32_R_TYPE(rel->r_info);
686 int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
687
688 switch (r_type) {
689 case R_386_NONE:
690 case R_386_PC32:
691 case R_386_PC16:
692 case R_386_PC8:
693 /*
694 * NONE can be ignored and PC relative relocations don't
695 * need to be adjusted.
696 */
697 break;
698
699 case R_386_16:
700 if (shn_abs) {
701 /*
702 * Whitelisted absolute symbols do not require
703 * relocation.
704 */
705 if (is_reloc(S_ABS, symname))
706 break;
707
708 if (is_reloc(S_SEG, symname)) {
709 add_reloc(&relocs16, rel->r_offset);
710 break;
711 }
712 } else {
713 if (!is_reloc(S_LIN, symname))
714 break;
715 }
716 die("Invalid %s %s relocation: %s\n",
717 shn_abs ? "absolute" : "relative",
718 rel_type(r_type), symname);
719 break;
720
721 case R_386_32:
722 if (shn_abs) {
723 /*
724 * Whitelisted absolute symbols do not require
725 * relocation.
726 */
727 if (is_reloc(S_ABS, symname))
728 break;
729
730 if (is_reloc(S_REL, symname)) {
731 add_reloc(&relocs32, rel->r_offset);
732 break;
733 }
734 } else {
735 if (is_reloc(S_LIN, symname))
736 add_reloc(&relocs32, rel->r_offset);
737 break;
738 }
739 die("Invalid %s %s relocation: %s\n",
740 shn_abs ? "absolute" : "relative",
741 rel_type(r_type), symname);
742 break;
743
744 default:
745 die("Unsupported relocation type: %s (%d)\n",
746 rel_type(r_type), r_type);
747 break;
748 }
749
750 return 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100751}
752
753static int cmp_relocs(const void *va, const void *vb)
754{
Kees Cook5d442e62013-04-12 13:13:43 -0700755 const uint32_t *a, *b;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100756 a = va; b = vb;
757 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
758}
759
Kees Cook5d442e62013-04-12 13:13:43 -0700760static void sort_relocs(struct relocs *r)
761{
762 qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
763}
764
765static int write32(uint32_t v, FILE *f)
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300766{
767 unsigned char buf[4];
768
769 put_unaligned_le32(v, buf);
770 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
771}
772
Kees Cook5d442e62013-04-12 13:13:43 -0700773static int write32_as_text(uint32_t v, FILE *f)
774{
775 return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
776}
777
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300778static void emit_relocs(int as_text, int use_real_mode)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100779{
780 int i;
Kees Cook5d442e62013-04-12 13:13:43 -0700781 int (*write_reloc)(uint32_t, FILE *) = write32;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300782
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100783 /* Collect up the relocations */
Kees Cook5d442e62013-04-12 13:13:43 -0700784 walk_relocs(use_real_mode ? do_reloc_real : do_reloc);
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300785
Kees Cook5d442e62013-04-12 13:13:43 -0700786 if (relocs16.count && !use_real_mode)
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300787 die("Segment relocations found but --realmode not specified\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100788
789 /* Order the relocations for more efficient processing */
Kees Cook5d442e62013-04-12 13:13:43 -0700790 sort_relocs(&relocs16);
791 sort_relocs(&relocs32);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100792
793 /* Print the relocations */
794 if (as_text) {
795 /* Print the relocations in a form suitable that
796 * gas will like.
797 */
798 printf(".section \".data.reloc\",\"a\"\n");
799 printf(".balign 4\n");
Kees Cook5d442e62013-04-12 13:13:43 -0700800 write_reloc = write32_as_text;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100801 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300802
Kees Cook5d442e62013-04-12 13:13:43 -0700803 if (use_real_mode) {
804 write_reloc(relocs16.count, stdout);
805 for (i = 0; i < relocs16.count; i++)
806 write_reloc(relocs16.offset[i], stdout);
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300807
Kees Cook5d442e62013-04-12 13:13:43 -0700808 write_reloc(relocs32.count, stdout);
809 for (i = 0; i < relocs32.count; i++)
810 write_reloc(relocs32.offset[i], stdout);
811 } else {
812 /* Print a stop */
813 write_reloc(0, stdout);
814
815 /* Now print each relocation */
816 for (i = 0; i < relocs32.count; i++)
817 write_reloc(relocs32.offset[i], stdout);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100818 }
819}
820
821static void usage(void)
822{
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300823 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100824}
825
826int main(int argc, char **argv)
827{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100828 int show_absolute_syms, show_absolute_relocs;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300829 int as_text, use_real_mode;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100830 const char *fname;
831 FILE *fp;
832 int i;
833
Vivek Goyal6a044b32006-12-07 02:14:04 +0100834 show_absolute_syms = 0;
835 show_absolute_relocs = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100836 as_text = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300837 use_real_mode = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100838 fname = NULL;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700839 for (i = 1; i < argc; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100840 char *arg = argv[i];
841 if (*arg == '-') {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300842 if (strcmp(arg, "--abs-syms") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100843 show_absolute_syms = 1;
844 continue;
845 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300846 if (strcmp(arg, "--abs-relocs") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100847 show_absolute_relocs = 1;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100848 continue;
849 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300850 if (strcmp(arg, "--text") == 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100851 as_text = 1;
852 continue;
853 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300854 if (strcmp(arg, "--realmode") == 0) {
855 use_real_mode = 1;
856 continue;
857 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100858 }
859 else if (!fname) {
860 fname = arg;
861 continue;
862 }
863 usage();
864 }
865 if (!fname) {
866 usage();
867 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300868 regex_init(use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100869 fp = fopen(fname, "r");
870 if (!fp) {
871 die("Cannot open %s: %s\n",
872 fname, strerror(errno));
873 }
874 read_ehdr(fp);
875 read_shdrs(fp);
876 read_strtabs(fp);
877 read_symtabs(fp);
878 read_relocs(fp);
Vivek Goyal6a044b32006-12-07 02:14:04 +0100879 if (show_absolute_syms) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100880 print_absolute_symbols();
Cong Ding65315d42013-01-14 17:13:35 +0000881 goto out;
Vivek Goyal6a044b32006-12-07 02:14:04 +0100882 }
883 if (show_absolute_relocs) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100884 print_absolute_relocs();
Cong Ding65315d42013-01-14 17:13:35 +0000885 goto out;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100886 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300887 emit_relocs(as_text, use_real_mode);
Cong Ding65315d42013-01-14 17:13:35 +0000888out:
889 fclose(fp);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100890 return 0;
891}