blob: 1f7ff3d11a0581616b8c8ca81f89b514fda60e2d [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
Kees Cook946166a2013-04-12 13:13:44 -070020#ifndef ELF_BITS
Kees Cookbf116552013-04-12 13:13:42 -070021#define ELF_BITS 32
Kees Cook946166a2013-04-12 13:13:44 -070022#endif
23
24#if (ELF_BITS == 64)
25#define ELF_MACHINE EM_X86_64
26#define ELF_MACHINE_NAME "x86_64"
27#define SHT_REL_TYPE SHT_RELA
28#define Elf_Rel Elf64_Rela
29#else
Kees Cookbf116552013-04-12 13:13:42 -070030#define ELF_MACHINE EM_386
31#define ELF_MACHINE_NAME "i386"
32#define SHT_REL_TYPE SHT_REL
Kees Cook946166a2013-04-12 13:13:44 -070033#define Elf_Rel ElfW(Rel)
34#endif
Kees Cookbf116552013-04-12 13:13:42 -070035
Kees Cook946166a2013-04-12 13:13:44 -070036#if (ELF_BITS == 64)
37#define ELF_CLASS ELFCLASS64
38#define ELF_R_SYM(val) ELF64_R_SYM(val)
39#define ELF_R_TYPE(val) ELF64_R_TYPE(val)
40#define ELF_ST_TYPE(o) ELF64_ST_TYPE(o)
41#define ELF_ST_BIND(o) ELF64_ST_BIND(o)
42#define ELF_ST_VISIBILITY(o) ELF64_ST_VISIBILITY(o)
43#else
Kees Cookbf116552013-04-12 13:13:42 -070044#define ELF_CLASS ELFCLASS32
45#define ELF_R_SYM(val) ELF32_R_SYM(val)
46#define ELF_R_TYPE(val) ELF32_R_TYPE(val)
47#define ELF_ST_TYPE(o) ELF32_ST_TYPE(o)
48#define ELF_ST_BIND(o) ELF32_ST_BIND(o)
49#define ELF_ST_VISIBILITY(o) ELF32_ST_VISIBILITY(o)
Kees Cook946166a2013-04-12 13:13:44 -070050#endif
Kees Cookbf116552013-04-12 13:13:42 -070051
Kees Cook946166a2013-04-12 13:13:44 -070052#define Elf_Addr ElfW(Addr)
Kees Cookbf116552013-04-12 13:13:42 -070053#define Elf_Ehdr ElfW(Ehdr)
54#define Elf_Phdr ElfW(Phdr)
55#define Elf_Shdr ElfW(Shdr)
56#define Elf_Sym ElfW(Sym)
57
H. Peter Anvin873b5272009-12-14 13:55:20 -080058static void die(char *fmt, ...);
Eric W. Biederman968de4f2006-12-07 02:14:04 +010059
Robert P. J. Dayca820182007-02-17 19:10:01 +010060#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Kees Cookbf116552013-04-12 13:13:42 -070061static Elf_Ehdr ehdr;
Kees Cook5d442e62013-04-12 13:13:43 -070062
63struct relocs {
64 uint32_t *offset;
65 unsigned long count;
66 unsigned long size;
67};
68
69static struct relocs relocs16;
70static struct relocs relocs32;
Kees Cook946166a2013-04-12 13:13:44 -070071static struct relocs relocs64;
Eric W. Biederman968de4f2006-12-07 02:14:04 +010072
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070073struct section {
Kees Cookbf116552013-04-12 13:13:42 -070074 Elf_Shdr shdr;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070075 struct section *link;
Kees Cookbf116552013-04-12 13:13:42 -070076 Elf_Sym *symtab;
77 Elf_Rel *reltab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070078 char *strtab;
79};
80static struct section *secs;
81
H. Peter Anvin6520fe52012-05-08 21:22:24 +030082enum symtype {
83 S_ABS,
84 S_REL,
85 S_SEG,
86 S_LIN,
87 S_NSYMTYPES
88};
89
90static const char * const sym_regex_kernel[S_NSYMTYPES] = {
Vivek Goyal6a044b32006-12-07 02:14:04 +010091/*
92 * Following symbols have been audited. There values are constant and do
93 * not change if bzImage is loaded at a different physical address than
94 * the address for which it has been compiled. Don't warn user about
95 * absolute relocations present w.r.t these symbols.
96 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +030097 [S_ABS] =
H. Peter Anvin873b5272009-12-14 13:55:20 -080098 "^(xen_irq_disable_direct_reloc$|"
99 "xen_save_fl_direct_reloc$|"
100 "VDSO|"
Kees Cook946166a2013-04-12 13:13:44 -0700101#if (ELF_BITS == 64)
102 "__vvar_page|"
103#endif
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300104 "__crc_)",
Vivek Goyal6a044b32006-12-07 02:14:04 +0100105
H. Peter Anvin873b5272009-12-14 13:55:20 -0800106/*
107 * These symbols are known to be relative, even if the linker marks them
108 * as absolute (typically defined outside any section in the linker script.)
109 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300110 [S_REL] =
H. Peter Anvina3e854d2012-05-18 00:24:09 -0700111 "^(__init_(begin|end)|"
112 "__x86_cpu_dev_(start|end)|"
113 "(__parainstructions|__alt_instructions)(|_end)|"
114 "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
H. Peter Anvinfd952812012-05-23 14:02:34 -0700115 "__(start|end)_pci_.*|"
116 "__(start|end)_builtin_fw|"
117 "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
118 "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
119 "__(start|stop)___param|"
120 "__(start|stop)___modver|"
121 "__(start|stop)___bug_table|"
122 "__tracedata_(start|end)|"
123 "__(start|stop)_notes|"
124 "__end_rodata|"
125 "__initramfs_start|"
H. Peter Anvinea17e742012-05-24 07:01:38 -0700126 "(jiffies|jiffies_64)|"
Kees Cook946166a2013-04-12 13:13:44 -0700127#if (ELF_BITS == 64)
128 "__per_cpu_load|"
129 "init_per_cpu__.*|"
130 "__end_rodata_hpage_align|"
131#endif
H. Peter Anvina3e854d2012-05-18 00:24:09 -0700132 "_end)$"
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300133};
134
135
136static const char * const sym_regex_realmode[S_NSYMTYPES] = {
137/*
H. Peter Anvin2a6de312012-05-08 21:22:31 +0300138 * These symbols are known to be relative, even if the linker marks them
139 * as absolute (typically defined outside any section in the linker script.)
140 */
141 [S_REL] =
142 "^pa_",
143
144/*
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300145 * These are 16-bit segment symbols when compiling 16-bit code.
146 */
147 [S_SEG] =
148 "^real_mode_seg$",
149
150/*
151 * These are offsets belonging to segments, as opposed to linear addresses,
152 * when compiling 16-bit code.
153 */
154 [S_LIN] =
155 "^pa_",
156};
157
158static const char * const *sym_regex;
159
160static regex_t sym_regex_c[S_NSYMTYPES];
161static int is_reloc(enum symtype type, const char *sym_name)
H. Peter Anvin873b5272009-12-14 13:55:20 -0800162{
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300163 return sym_regex[type] &&
164 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
H. Peter Anvin873b5272009-12-14 13:55:20 -0800165}
166
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300167static void regex_init(int use_real_mode)
H. Peter Anvin873b5272009-12-14 13:55:20 -0800168{
169 char errbuf[128];
170 int err;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300171 int i;
H. Peter Anvin873b5272009-12-14 13:55:20 -0800172
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300173 if (use_real_mode)
174 sym_regex = sym_regex_realmode;
175 else
176 sym_regex = sym_regex_kernel;
177
178 for (i = 0; i < S_NSYMTYPES; i++) {
179 if (!sym_regex[i])
180 continue;
181
182 err = regcomp(&sym_regex_c[i], sym_regex[i],
183 REG_EXTENDED|REG_NOSUB);
184
185 if (err) {
186 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
187 die("%s", errbuf);
188 }
H. Peter Anvin873b5272009-12-14 13:55:20 -0800189 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100190}
191
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100192static void die(char *fmt, ...)
193{
194 va_list ap;
195 va_start(ap, fmt);
196 vfprintf(stderr, fmt, ap);
197 va_end(ap);
198 exit(1);
199}
200
201static const char *sym_type(unsigned type)
202{
203 static const char *type_name[] = {
204#define SYM_TYPE(X) [X] = #X
205 SYM_TYPE(STT_NOTYPE),
206 SYM_TYPE(STT_OBJECT),
207 SYM_TYPE(STT_FUNC),
208 SYM_TYPE(STT_SECTION),
209 SYM_TYPE(STT_FILE),
210 SYM_TYPE(STT_COMMON),
211 SYM_TYPE(STT_TLS),
212#undef SYM_TYPE
213 };
214 const char *name = "unknown sym type name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100215 if (type < ARRAY_SIZE(type_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100216 name = type_name[type];
217 }
218 return name;
219}
220
221static const char *sym_bind(unsigned bind)
222{
223 static const char *bind_name[] = {
224#define SYM_BIND(X) [X] = #X
225 SYM_BIND(STB_LOCAL),
226 SYM_BIND(STB_GLOBAL),
227 SYM_BIND(STB_WEAK),
228#undef SYM_BIND
229 };
230 const char *name = "unknown sym bind name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100231 if (bind < ARRAY_SIZE(bind_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100232 name = bind_name[bind];
233 }
234 return name;
235}
236
237static const char *sym_visibility(unsigned visibility)
238{
239 static const char *visibility_name[] = {
240#define SYM_VISIBILITY(X) [X] = #X
241 SYM_VISIBILITY(STV_DEFAULT),
242 SYM_VISIBILITY(STV_INTERNAL),
243 SYM_VISIBILITY(STV_HIDDEN),
244 SYM_VISIBILITY(STV_PROTECTED),
245#undef SYM_VISIBILITY
246 };
247 const char *name = "unknown sym visibility name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100248 if (visibility < ARRAY_SIZE(visibility_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100249 name = visibility_name[visibility];
250 }
251 return name;
252}
253
254static const char *rel_type(unsigned type)
255{
256 static const char *type_name[] = {
257#define REL_TYPE(X) [X] = #X
Kees Cook946166a2013-04-12 13:13:44 -0700258#if (ELF_BITS == 64)
259 REL_TYPE(R_X86_64_NONE),
260 REL_TYPE(R_X86_64_64),
261 REL_TYPE(R_X86_64_PC32),
262 REL_TYPE(R_X86_64_GOT32),
263 REL_TYPE(R_X86_64_PLT32),
264 REL_TYPE(R_X86_64_COPY),
265 REL_TYPE(R_X86_64_GLOB_DAT),
266 REL_TYPE(R_X86_64_JUMP_SLOT),
267 REL_TYPE(R_X86_64_RELATIVE),
268 REL_TYPE(R_X86_64_GOTPCREL),
269 REL_TYPE(R_X86_64_32),
270 REL_TYPE(R_X86_64_32S),
271 REL_TYPE(R_X86_64_16),
272 REL_TYPE(R_X86_64_PC16),
273 REL_TYPE(R_X86_64_8),
274 REL_TYPE(R_X86_64_PC8),
275#else
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100276 REL_TYPE(R_386_NONE),
277 REL_TYPE(R_386_32),
278 REL_TYPE(R_386_PC32),
279 REL_TYPE(R_386_GOT32),
280 REL_TYPE(R_386_PLT32),
281 REL_TYPE(R_386_COPY),
282 REL_TYPE(R_386_GLOB_DAT),
283 REL_TYPE(R_386_JMP_SLOT),
284 REL_TYPE(R_386_RELATIVE),
285 REL_TYPE(R_386_GOTOFF),
286 REL_TYPE(R_386_GOTPC),
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300287 REL_TYPE(R_386_8),
288 REL_TYPE(R_386_PC8),
289 REL_TYPE(R_386_16),
290 REL_TYPE(R_386_PC16),
Kees Cook946166a2013-04-12 13:13:44 -0700291#endif
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100292#undef REL_TYPE
293 };
294 const char *name = "unknown type rel type name";
H. Peter Anvin873b5272009-12-14 13:55:20 -0800295 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100296 name = type_name[type];
297 }
298 return name;
299}
300
301static const char *sec_name(unsigned shndx)
302{
303 const char *sec_strtab;
304 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700305 sec_strtab = secs[ehdr.e_shstrndx].strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100306 name = "<noname>";
307 if (shndx < ehdr.e_shnum) {
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700308 name = sec_strtab + secs[shndx].shdr.sh_name;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100309 }
310 else if (shndx == SHN_ABS) {
311 name = "ABSOLUTE";
312 }
313 else if (shndx == SHN_COMMON) {
314 name = "COMMON";
315 }
316 return name;
317}
318
Kees Cookbf116552013-04-12 13:13:42 -0700319static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100320{
321 const char *name;
322 name = "<noname>";
323 if (sym->st_name) {
324 name = sym_strtab + sym->st_name;
325 }
326 else {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300327 name = sec_name(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100328 }
329 return name;
330}
331
Kees Cook946166a2013-04-12 13:13:44 -0700332static Elf_Sym *sym_lookup(const char *symname)
333{
334 int i;
335 for (i = 0; i < ehdr.e_shnum; i++) {
336 struct section *sec = &secs[i];
337 long nsyms;
338 char *strtab;
339 Elf_Sym *symtab;
340 Elf_Sym *sym;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100341
Kees Cook946166a2013-04-12 13:13:44 -0700342 if (sec->shdr.sh_type != SHT_SYMTAB)
343 continue;
344
345 nsyms = sec->shdr.sh_size/sizeof(Elf_Sym);
346 symtab = sec->symtab;
347 strtab = sec->link->strtab;
348
349 for (sym = symtab; --nsyms >= 0; sym++) {
350 if (!sym->st_name)
351 continue;
352 if (strcmp(symname, strtab + sym->st_name) == 0)
353 return sym;
354 }
355 }
356 return 0;
357}
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100358
Linus Torvalds13da9e22010-05-26 08:30:15 -0700359#if BYTE_ORDER == LITTLE_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100360#define le16_to_cpu(val) (val)
361#define le32_to_cpu(val) (val)
Kees Cook946166a2013-04-12 13:13:44 -0700362#define le64_to_cpu(val) (val)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100363#endif
Linus Torvalds13da9e22010-05-26 08:30:15 -0700364#if BYTE_ORDER == BIG_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100365#define le16_to_cpu(val) bswap_16(val)
366#define le32_to_cpu(val) bswap_32(val)
Kees Cook946166a2013-04-12 13:13:44 -0700367#define le64_to_cpu(val) bswap_64(val)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100368#endif
369
370static uint16_t elf16_to_cpu(uint16_t val)
371{
372 return le16_to_cpu(val);
373}
374
375static uint32_t elf32_to_cpu(uint32_t val)
376{
377 return le32_to_cpu(val);
378}
379
Kees Cookbf116552013-04-12 13:13:42 -0700380#define elf_half_to_cpu(x) elf16_to_cpu(x)
381#define elf_word_to_cpu(x) elf32_to_cpu(x)
Kees Cook946166a2013-04-12 13:13:44 -0700382
383#if (ELF_BITS == 64)
384static uint64_t elf64_to_cpu(uint64_t val)
385{
386 return le64_to_cpu(val);
387}
388#define elf_addr_to_cpu(x) elf64_to_cpu(x)
389#define elf_off_to_cpu(x) elf64_to_cpu(x)
390#define elf_xword_to_cpu(x) elf64_to_cpu(x)
391#else
Kees Cookbf116552013-04-12 13:13:42 -0700392#define elf_addr_to_cpu(x) elf32_to_cpu(x)
393#define elf_off_to_cpu(x) elf32_to_cpu(x)
394#define elf_xword_to_cpu(x) elf32_to_cpu(x)
Kees Cook946166a2013-04-12 13:13:44 -0700395#endif
Kees Cookbf116552013-04-12 13:13:42 -0700396
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100397static void read_ehdr(FILE *fp)
398{
399 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
400 die("Cannot read ELF header: %s\n",
401 strerror(errno));
402 }
Cyrill Gorcunov8bd17962008-05-03 14:18:03 +0400403 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100404 die("No ELF magic\n");
405 }
Kees Cookbf116552013-04-12 13:13:42 -0700406 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) {
407 die("Not a %d bit executable\n", ELF_BITS);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100408 }
409 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
410 die("Not a LSB ELF executable\n");
411 }
412 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
413 die("Unknown ELF version\n");
414 }
415 /* Convert the fields to native endian */
Kees Cookbf116552013-04-12 13:13:42 -0700416 ehdr.e_type = elf_half_to_cpu(ehdr.e_type);
417 ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine);
418 ehdr.e_version = elf_word_to_cpu(ehdr.e_version);
419 ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry);
420 ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff);
421 ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff);
422 ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags);
423 ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize);
424 ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
425 ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum);
426 ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
427 ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum);
428 ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100429
430 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
431 die("Unsupported ELF header type\n");
432 }
Kees Cookbf116552013-04-12 13:13:42 -0700433 if (ehdr.e_machine != ELF_MACHINE) {
434 die("Not for %s\n", ELF_MACHINE_NAME);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100435 }
436 if (ehdr.e_version != EV_CURRENT) {
437 die("Unknown ELF version\n");
438 }
Kees Cookbf116552013-04-12 13:13:42 -0700439 if (ehdr.e_ehsize != sizeof(Elf_Ehdr)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100440 die("Bad Elf header size\n");
441 }
Kees Cookbf116552013-04-12 13:13:42 -0700442 if (ehdr.e_phentsize != sizeof(Elf_Phdr)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100443 die("Bad program header entry\n");
444 }
Kees Cookbf116552013-04-12 13:13:42 -0700445 if (ehdr.e_shentsize != sizeof(Elf_Shdr)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100446 die("Bad section header entry\n");
447 }
448 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
449 die("String table index out of bounds\n");
450 }
451}
452
453static void read_shdrs(FILE *fp)
454{
455 int i;
Kees Cookbf116552013-04-12 13:13:42 -0700456 Elf_Shdr shdr;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700457
458 secs = calloc(ehdr.e_shnum, sizeof(struct section));
459 if (!secs) {
460 die("Unable to allocate %d section headers\n",
461 ehdr.e_shnum);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100462 }
463 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
464 die("Seek to %d failed: %s\n",
465 ehdr.e_shoff, strerror(errno));
466 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700467 for (i = 0; i < ehdr.e_shnum; i++) {
468 struct section *sec = &secs[i];
469 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
470 die("Cannot read ELF section headers %d/%d: %s\n",
471 i, ehdr.e_shnum, strerror(errno));
Kees Cookbf116552013-04-12 13:13:42 -0700472 sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
473 sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
474 sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags);
475 sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr);
476 sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset);
477 sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size);
478 sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link);
479 sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info);
480 sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
481 sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize);
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700482 if (sec->shdr.sh_link < ehdr.e_shnum)
483 sec->link = &secs[sec->shdr.sh_link];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100484 }
485
486}
487
488static void read_strtabs(FILE *fp)
489{
490 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700491 for (i = 0; i < ehdr.e_shnum; i++) {
492 struct section *sec = &secs[i];
493 if (sec->shdr.sh_type != SHT_STRTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100494 continue;
495 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700496 sec->strtab = malloc(sec->shdr.sh_size);
497 if (!sec->strtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100498 die("malloc of %d bytes for strtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700499 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100500 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700501 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100502 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700503 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100504 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700505 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
506 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100507 die("Cannot read symbol table: %s\n",
508 strerror(errno));
509 }
510 }
511}
512
513static void read_symtabs(FILE *fp)
514{
515 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700516 for (i = 0; i < ehdr.e_shnum; i++) {
517 struct section *sec = &secs[i];
518 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100519 continue;
520 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700521 sec->symtab = malloc(sec->shdr.sh_size);
522 if (!sec->symtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100523 die("malloc of %d bytes for symtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700524 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100525 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700526 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100527 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700528 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100529 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700530 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
531 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100532 die("Cannot read symbol table: %s\n",
533 strerror(errno));
534 }
Kees Cookbf116552013-04-12 13:13:42 -0700535 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
536 Elf_Sym *sym = &sec->symtab[j];
537 sym->st_name = elf_word_to_cpu(sym->st_name);
538 sym->st_value = elf_addr_to_cpu(sym->st_value);
539 sym->st_size = elf_xword_to_cpu(sym->st_size);
540 sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100541 }
542 }
543}
544
545
546static void read_relocs(FILE *fp)
547{
548 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700549 for (i = 0; i < ehdr.e_shnum; i++) {
550 struct section *sec = &secs[i];
Kees Cookbf116552013-04-12 13:13:42 -0700551 if (sec->shdr.sh_type != SHT_REL_TYPE) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100552 continue;
553 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700554 sec->reltab = malloc(sec->shdr.sh_size);
555 if (!sec->reltab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100556 die("malloc of %d bytes for relocs failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700557 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100558 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700559 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100560 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700561 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100562 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700563 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
564 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100565 die("Cannot read symbol table: %s\n",
566 strerror(errno));
567 }
Kees Cookbf116552013-04-12 13:13:42 -0700568 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
569 Elf_Rel *rel = &sec->reltab[j];
570 rel->r_offset = elf_addr_to_cpu(rel->r_offset);
571 rel->r_info = elf_xword_to_cpu(rel->r_info);
Kees Cook946166a2013-04-12 13:13:44 -0700572#if (SHT_REL_TYPE == SHT_RELA)
573 rel->r_addend = elf_xword_to_cpu(rel->r_addend);
574#endif
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100575 }
576 }
577}
578
579
580static void print_absolute_symbols(void)
581{
582 int i;
Kees Cook946166a2013-04-12 13:13:44 -0700583 const char *format;
584
585 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
586 format = "%5d %016"PRIx64" %5"PRId64" %10s %10s %12s %s\n";
587 else
588 format = "%5d %08"PRIx32" %5"PRId32" %10s %10s %12s %s\n";
589
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100590 printf("Absolute symbols\n");
591 printf(" Num: Value Size Type Bind Visibility Name\n");
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700592 for (i = 0; i < ehdr.e_shnum; i++) {
593 struct section *sec = &secs[i];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100594 char *sym_strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100595 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700596
597 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100598 continue;
599 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700600 sym_strtab = sec->link->strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700601 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
602 Elf_Sym *sym;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100603 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700604 sym = &sec->symtab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100605 name = sym_name(sym_strtab, sym);
606 if (sym->st_shndx != SHN_ABS) {
607 continue;
608 }
Kees Cook946166a2013-04-12 13:13:44 -0700609 printf(format,
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100610 j, sym->st_value, sym->st_size,
Kees Cookbf116552013-04-12 13:13:42 -0700611 sym_type(ELF_ST_TYPE(sym->st_info)),
612 sym_bind(ELF_ST_BIND(sym->st_info)),
613 sym_visibility(ELF_ST_VISIBILITY(sym->st_other)),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100614 name);
615 }
616 }
617 printf("\n");
618}
619
620static void print_absolute_relocs(void)
621{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100622 int i, printed = 0;
Kees Cook946166a2013-04-12 13:13:44 -0700623 const char *format;
624
625 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
626 format = "%016"PRIx64" %016"PRIx64" %10s %016"PRIx64" %s\n";
627 else
628 format = "%08"PRIx32" %08"PRIx32" %10s %08"PRIx32" %s\n";
Vivek Goyal6a044b32006-12-07 02:14:04 +0100629
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700630 for (i = 0; i < ehdr.e_shnum; i++) {
631 struct section *sec = &secs[i];
632 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100633 char *sym_strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700634 Elf_Sym *sh_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100635 int j;
Kees Cookbf116552013-04-12 13:13:42 -0700636 if (sec->shdr.sh_type != SHT_REL_TYPE) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100637 continue;
638 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700639 sec_symtab = sec->link;
640 sec_applies = &secs[sec->shdr.sh_info];
641 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100642 continue;
643 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700644 sh_symtab = sec_symtab->symtab;
645 sym_strtab = sec_symtab->link->strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700646 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
647 Elf_Rel *rel;
648 Elf_Sym *sym;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100649 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700650 rel = &sec->reltab[j];
Kees Cookbf116552013-04-12 13:13:42 -0700651 sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100652 name = sym_name(sym_strtab, sym);
653 if (sym->st_shndx != SHN_ABS) {
654 continue;
655 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100656
657 /* Absolute symbols are not relocated if bzImage is
658 * loaded at a non-compiled address. Display a warning
659 * to user at compile time about the absolute
660 * relocations present.
661 *
662 * User need to audit the code to make sure
663 * some symbols which should have been section
664 * relative have not become absolute because of some
665 * linker optimization or wrong programming usage.
666 *
667 * Before warning check if this absolute symbol
668 * relocation is harmless.
669 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300670 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
Vivek Goyal6a044b32006-12-07 02:14:04 +0100671 continue;
672
673 if (!printed) {
674 printf("WARNING: Absolute relocations"
675 " present\n");
676 printf("Offset Info Type Sym.Value "
677 "Sym.Name\n");
678 printed = 1;
679 }
680
Kees Cook946166a2013-04-12 13:13:44 -0700681 printf(format,
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100682 rel->r_offset,
683 rel->r_info,
Kees Cookbf116552013-04-12 13:13:42 -0700684 rel_type(ELF_R_TYPE(rel->r_info)),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100685 sym->st_value,
686 name);
687 }
688 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100689
690 if (printed)
691 printf("\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100692}
693
Kees Cook5d442e62013-04-12 13:13:43 -0700694static void add_reloc(struct relocs *r, uint32_t offset)
695{
696 if (r->count == r->size) {
697 unsigned long newsize = r->size + 50000;
698 void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
699
700 if (!mem)
701 die("realloc of %ld entries for relocs failed\n",
702 newsize);
703 r->offset = mem;
704 r->size = newsize;
705 }
706 r->offset[r->count++] = offset;
707}
708
709static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
710 Elf_Sym *sym, const char *symname))
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100711{
712 int i;
713 /* Walk through the relocations */
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700714 for (i = 0; i < ehdr.e_shnum; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100715 char *sym_strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700716 Elf_Sym *sh_symtab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700717 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100718 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700719 struct section *sec = &secs[i];
720
Kees Cookbf116552013-04-12 13:13:42 -0700721 if (sec->shdr.sh_type != SHT_REL_TYPE) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100722 continue;
723 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700724 sec_symtab = sec->link;
725 sec_applies = &secs[sec->shdr.sh_info];
726 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100727 continue;
728 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700729 sh_symtab = sec_symtab->symtab;
H. Peter Anvincc65f1e2008-10-03 13:00:56 -0700730 sym_strtab = sec_symtab->link->strtab;
Kees Cookbf116552013-04-12 13:13:42 -0700731 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
Kees Cook5d442e62013-04-12 13:13:43 -0700732 Elf_Rel *rel = &sec->reltab[j];
733 Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
734 const char *symname = sym_name(sym_strtab, sym);
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700735
Kees Cook5d442e62013-04-12 13:13:43 -0700736 process(sec, rel, sym, symname);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100737 }
738 }
739}
740
Kees Cook946166a2013-04-12 13:13:44 -0700741/*
742 * The .data..percpu section is a special case for x86_64 SMP kernels.
743 * It is used to initialize the actual per_cpu areas and to provide
744 * definitions for the per_cpu variables that correspond to their offsets
745 * within the percpu area. Since the values of all of the symbols need
746 * to be offsets from the start of the per_cpu area the virtual address
747 * (sh_addr) of .data..percpu is 0 in SMP kernels.
748 *
749 * This means that:
750 *
751 * Relocations that reference symbols in the per_cpu area do not
752 * need further relocation (since the value is an offset relative
753 * to the start of the per_cpu area that does not change).
754 *
755 * Relocations that apply to the per_cpu area need to have their
756 * offset adjusted by by the value of __per_cpu_load to make them
757 * point to the correct place in the loaded image (because the
758 * virtual address of .data..percpu is 0).
759 *
760 * For non SMP kernels .data..percpu is linked as part of the normal
761 * kernel data and does not require special treatment.
762 *
763 */
764static int per_cpu_shndx = -1;
765Elf_Addr per_cpu_load_addr;
766
767static void percpu_init(void)
768{
769 int i;
770 for (i = 0; i < ehdr.e_shnum; i++) {
771 ElfW(Sym) *sym;
772 if (strcmp(sec_name(i), ".data..percpu"))
773 continue;
774
775 if (secs[i].shdr.sh_addr != 0) /* non SMP kernel */
776 return;
777
778 sym = sym_lookup("__per_cpu_load");
779 if (!sym)
780 die("can't find __per_cpu_load\n");
781
782 per_cpu_shndx = i;
783 per_cpu_load_addr = sym->st_value;
784 return;
785 }
786}
787
788/*
789 * Check to see if a symbol lies in the .data..percpu section.
790 * For some as yet not understood reason the "__init_begin"
791 * symbol which immediately preceeds the .data..percpu section
792 * also shows up as it it were part of it so we do an explict
793 * check for that symbol name and ignore it.
794 */
795static int is_percpu_sym(ElfW(Sym) *sym, const char *symname)
796{
797 return (sym->st_shndx == per_cpu_shndx) &&
798 strcmp(symname, "__init_begin");
799}
800
801static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
802 const char *symname)
803{
804 unsigned r_type = ELF64_R_TYPE(rel->r_info);
805 ElfW(Addr) offset = rel->r_offset;
806 int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
807
808 if (sym->st_shndx == SHN_UNDEF)
809 return 0;
810
811 /*
812 * Adjust the offset if this reloc applies to the percpu section.
813 */
814 if (sec->shdr.sh_info == per_cpu_shndx)
815 offset += per_cpu_load_addr;
816
817 switch (r_type) {
818 case R_X86_64_NONE:
819 case R_X86_64_PC32:
820 /*
821 * NONE can be ignored and PC relative relocations don't
822 * need to be adjusted.
823 */
824 break;
825
826 case R_X86_64_32:
827 case R_X86_64_32S:
828 case R_X86_64_64:
829 /*
830 * References to the percpu area don't need to be adjusted.
831 */
832 if (is_percpu_sym(sym, symname))
833 break;
834
835 if (shn_abs) {
836 /*
837 * Whitelisted absolute symbols do not require
838 * relocation.
839 */
840 if (is_reloc(S_ABS, symname))
841 break;
842
843 die("Invalid absolute %s relocation: %s\n",
844 rel_type(r_type), symname);
845 break;
846 }
847
848 /*
849 * Relocation offsets for 64 bit kernels are output
850 * as 32 bits and sign extended back to 64 bits when
851 * the relocations are processed.
852 * Make sure that the offset will fit.
853 */
854 if ((int32_t)offset != (int64_t)offset)
855 die("Relocation offset doesn't fit in 32 bits\n");
856
857 if (r_type == R_X86_64_64)
858 add_reloc(&relocs64, offset);
859 else
860 add_reloc(&relocs32, offset);
861 break;
862
863 default:
864 die("Unsupported relocation type: %s (%d)\n",
865 rel_type(r_type), r_type);
866 break;
867 }
868
869 return 0;
870}
871
872
873static int do_reloc32(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
874 const char *symname)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100875{
Kees Cook5d442e62013-04-12 13:13:43 -0700876 unsigned r_type = ELF32_R_TYPE(rel->r_info);
877 int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
878
879 switch (r_type) {
880 case R_386_NONE:
881 case R_386_PC32:
882 case R_386_PC16:
883 case R_386_PC8:
884 /*
885 * NONE can be ignored and PC relative relocations don't
886 * need to be adjusted.
887 */
888 break;
889
890 case R_386_32:
891 if (shn_abs) {
892 /*
893 * Whitelisted absolute symbols do not require
894 * relocation.
895 */
896 if (is_reloc(S_ABS, symname))
897 break;
898
899 die("Invalid absolute %s relocation: %s\n",
900 rel_type(r_type), symname);
901 break;
902 }
903
904 add_reloc(&relocs32, rel->r_offset);
905 break;
906
907 default:
908 die("Unsupported relocation type: %s (%d)\n",
909 rel_type(r_type), r_type);
910 break;
911 }
912
913 return 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100914}
915
Kees Cook5d442e62013-04-12 13:13:43 -0700916static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
917 const char *symname)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100918{
Kees Cook5d442e62013-04-12 13:13:43 -0700919 unsigned r_type = ELF32_R_TYPE(rel->r_info);
920 int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
921
922 switch (r_type) {
923 case R_386_NONE:
924 case R_386_PC32:
925 case R_386_PC16:
926 case R_386_PC8:
927 /*
928 * NONE can be ignored and PC relative relocations don't
929 * need to be adjusted.
930 */
931 break;
932
933 case R_386_16:
934 if (shn_abs) {
935 /*
936 * Whitelisted absolute symbols do not require
937 * relocation.
938 */
939 if (is_reloc(S_ABS, symname))
940 break;
941
942 if (is_reloc(S_SEG, symname)) {
943 add_reloc(&relocs16, rel->r_offset);
944 break;
945 }
946 } else {
947 if (!is_reloc(S_LIN, symname))
948 break;
949 }
950 die("Invalid %s %s relocation: %s\n",
951 shn_abs ? "absolute" : "relative",
952 rel_type(r_type), symname);
953 break;
954
955 case R_386_32:
956 if (shn_abs) {
957 /*
958 * Whitelisted absolute symbols do not require
959 * relocation.
960 */
961 if (is_reloc(S_ABS, symname))
962 break;
963
964 if (is_reloc(S_REL, symname)) {
965 add_reloc(&relocs32, rel->r_offset);
966 break;
967 }
968 } else {
969 if (is_reloc(S_LIN, symname))
970 add_reloc(&relocs32, rel->r_offset);
971 break;
972 }
973 die("Invalid %s %s relocation: %s\n",
974 shn_abs ? "absolute" : "relative",
975 rel_type(r_type), symname);
976 break;
977
978 default:
979 die("Unsupported relocation type: %s (%d)\n",
980 rel_type(r_type), r_type);
981 break;
982 }
983
984 return 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100985}
986
987static int cmp_relocs(const void *va, const void *vb)
988{
Kees Cook5d442e62013-04-12 13:13:43 -0700989 const uint32_t *a, *b;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100990 a = va; b = vb;
991 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
992}
993
Kees Cook5d442e62013-04-12 13:13:43 -0700994static void sort_relocs(struct relocs *r)
995{
996 qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
997}
998
999static int write32(uint32_t v, FILE *f)
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001000{
1001 unsigned char buf[4];
1002
1003 put_unaligned_le32(v, buf);
1004 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
1005}
1006
Kees Cook5d442e62013-04-12 13:13:43 -07001007static int write32_as_text(uint32_t v, FILE *f)
1008{
1009 return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
1010}
1011
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001012static void emit_relocs(int as_text, int use_real_mode)
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001013{
1014 int i;
Kees Cook5d442e62013-04-12 13:13:43 -07001015 int (*write_reloc)(uint32_t, FILE *) = write32;
Kees Cook946166a2013-04-12 13:13:44 -07001016 int (*do_reloc)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
1017 const char *symname);
1018
1019 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
1020 do_reloc = do_reloc64;
1021 else if (!use_real_mode)
1022 do_reloc = do_reloc32;
1023 else
1024 do_reloc = do_reloc_real;
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001025
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001026 /* Collect up the relocations */
Kees Cook946166a2013-04-12 13:13:44 -07001027 walk_relocs(do_reloc);
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001028
Kees Cook5d442e62013-04-12 13:13:43 -07001029 if (relocs16.count && !use_real_mode)
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001030 die("Segment relocations found but --realmode not specified\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001031
1032 /* Order the relocations for more efficient processing */
Kees Cook5d442e62013-04-12 13:13:43 -07001033 sort_relocs(&relocs16);
1034 sort_relocs(&relocs32);
Kees Cook946166a2013-04-12 13:13:44 -07001035 sort_relocs(&relocs64);
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001036
1037 /* Print the relocations */
1038 if (as_text) {
1039 /* Print the relocations in a form suitable that
1040 * gas will like.
1041 */
1042 printf(".section \".data.reloc\",\"a\"\n");
1043 printf(".balign 4\n");
Kees Cook5d442e62013-04-12 13:13:43 -07001044 write_reloc = write32_as_text;
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001045 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001046
Kees Cook5d442e62013-04-12 13:13:43 -07001047 if (use_real_mode) {
1048 write_reloc(relocs16.count, stdout);
1049 for (i = 0; i < relocs16.count; i++)
1050 write_reloc(relocs16.offset[i], stdout);
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001051
Kees Cook5d442e62013-04-12 13:13:43 -07001052 write_reloc(relocs32.count, stdout);
1053 for (i = 0; i < relocs32.count; i++)
1054 write_reloc(relocs32.offset[i], stdout);
1055 } else {
Kees Cook946166a2013-04-12 13:13:44 -07001056 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1057 /* Print a stop */
1058 write_reloc(0, stdout);
1059
1060 /* Now print each relocation */
1061 for (i = 0; i < relocs64.count; i++)
1062 write_reloc(relocs64.offset[i], stdout);
1063 }
1064
Kees Cook5d442e62013-04-12 13:13:43 -07001065 /* Print a stop */
1066 write_reloc(0, stdout);
1067
1068 /* Now print each relocation */
1069 for (i = 0; i < relocs32.count; i++)
1070 write_reloc(relocs32.offset[i], stdout);
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001071 }
1072}
1073
1074static void usage(void)
1075{
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001076 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001077}
1078
1079int main(int argc, char **argv)
1080{
Vivek Goyal6a044b32006-12-07 02:14:04 +01001081 int show_absolute_syms, show_absolute_relocs;
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001082 int as_text, use_real_mode;
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001083 const char *fname;
1084 FILE *fp;
1085 int i;
1086
Vivek Goyal6a044b32006-12-07 02:14:04 +01001087 show_absolute_syms = 0;
1088 show_absolute_relocs = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001089 as_text = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001090 use_real_mode = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001091 fname = NULL;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -07001092 for (i = 1; i < argc; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001093 char *arg = argv[i];
1094 if (*arg == '-') {
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001095 if (strcmp(arg, "--abs-syms") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +01001096 show_absolute_syms = 1;
1097 continue;
1098 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001099 if (strcmp(arg, "--abs-relocs") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +01001100 show_absolute_relocs = 1;
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001101 continue;
1102 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001103 if (strcmp(arg, "--text") == 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001104 as_text = 1;
1105 continue;
1106 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001107 if (strcmp(arg, "--realmode") == 0) {
1108 use_real_mode = 1;
1109 continue;
1110 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001111 }
1112 else if (!fname) {
1113 fname = arg;
1114 continue;
1115 }
1116 usage();
1117 }
1118 if (!fname) {
1119 usage();
1120 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001121 regex_init(use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001122 fp = fopen(fname, "r");
1123 if (!fp) {
1124 die("Cannot open %s: %s\n",
1125 fname, strerror(errno));
1126 }
1127 read_ehdr(fp);
1128 read_shdrs(fp);
1129 read_strtabs(fp);
1130 read_symtabs(fp);
1131 read_relocs(fp);
Kees Cook946166a2013-04-12 13:13:44 -07001132 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
1133 percpu_init();
Vivek Goyal6a044b32006-12-07 02:14:04 +01001134 if (show_absolute_syms) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001135 print_absolute_symbols();
Cong Ding65315d42013-01-14 17:13:35 +00001136 goto out;
Vivek Goyal6a044b32006-12-07 02:14:04 +01001137 }
1138 if (show_absolute_relocs) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001139 print_absolute_relocs();
Cong Ding65315d42013-01-14 17:13:35 +00001140 goto out;
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001141 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +03001142 emit_relocs(as_text, use_real_mode);
Cong Ding65315d42013-01-14 17:13:35 +00001143out:
1144 fclose(fp);
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001145 return 0;
1146}