blob: b43cfcd9bf40f60b7c3b71a1460c9883c322d7ce [file] [log] [blame]
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001#include <stdio.h>
2#include <stdarg.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <string.h>
6#include <errno.h>
7#include <unistd.h>
8#include <elf.h>
9#include <byteswap.h>
10#define USE_BSD
11#include <endian.h>
H. Peter Anvin873b5272009-12-14 13:55:20 -080012#include <regex.h>
Matt Fleming55f97092012-02-28 13:37:21 +000013#include <tools/le_byteshift.h>
H. Peter Anvin873b5272009-12-14 13:55:20 -080014
15static void die(char *fmt, ...);
Eric W. Biederman968de4f2006-12-07 02:14:04 +010016
Robert P. J. Dayca820182007-02-17 19:10:01 +010017#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Eric W. Biederman968de4f2006-12-07 02:14:04 +010018static Elf32_Ehdr ehdr;
Eric W. Biederman968de4f2006-12-07 02:14:04 +010019static unsigned long reloc_count, reloc_idx;
20static unsigned long *relocs;
H. Peter Anvin6520fe52012-05-08 21:22:24 +030021static unsigned long reloc16_count, reloc16_idx;
22static unsigned long *relocs16;
Eric W. Biederman968de4f2006-12-07 02:14:04 +010023
H. Peter Anvin908ec7a2008-06-30 14:42:18 -070024struct section {
25 Elf32_Shdr shdr;
26 struct section *link;
27 Elf32_Sym *symtab;
28 Elf32_Rel *reltab;
29 char *strtab;
30};
31static struct section *secs;
32
H. Peter Anvin6520fe52012-05-08 21:22:24 +030033enum symtype {
34 S_ABS,
35 S_REL,
36 S_SEG,
37 S_LIN,
38 S_NSYMTYPES
39};
40
41static const char * const sym_regex_kernel[S_NSYMTYPES] = {
Vivek Goyal6a044b32006-12-07 02:14:04 +010042/*
43 * Following symbols have been audited. There values are constant and do
44 * not change if bzImage is loaded at a different physical address than
45 * the address for which it has been compiled. Don't warn user about
46 * absolute relocations present w.r.t these symbols.
47 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +030048 [S_ABS] =
H. Peter Anvin873b5272009-12-14 13:55:20 -080049 "^(xen_irq_disable_direct_reloc$|"
50 "xen_save_fl_direct_reloc$|"
51 "VDSO|"
H. Peter Anvin6520fe52012-05-08 21:22:24 +030052 "__crc_)",
Vivek Goyal6a044b32006-12-07 02:14:04 +010053
H. Peter Anvin873b5272009-12-14 13:55:20 -080054/*
55 * These symbols are known to be relative, even if the linker marks them
56 * as absolute (typically defined outside any section in the linker script.)
57 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +030058 [S_REL] =
H. Peter Anvina3e854d2012-05-18 00:24:09 -070059 "^(__init_(begin|end)|"
60 "__x86_cpu_dev_(start|end)|"
61 "(__parainstructions|__alt_instructions)(|_end)|"
62 "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
63 "_end)$"
H. Peter Anvin6520fe52012-05-08 21:22:24 +030064};
65
66
67static const char * const sym_regex_realmode[S_NSYMTYPES] = {
68/*
69 * These are 16-bit segment symbols when compiling 16-bit code.
70 */
71 [S_SEG] =
72 "^real_mode_seg$",
73
74/*
75 * These are offsets belonging to segments, as opposed to linear addresses,
76 * when compiling 16-bit code.
77 */
78 [S_LIN] =
79 "^pa_",
80};
81
82static const char * const *sym_regex;
83
84static regex_t sym_regex_c[S_NSYMTYPES];
85static int is_reloc(enum symtype type, const char *sym_name)
H. Peter Anvin873b5272009-12-14 13:55:20 -080086{
H. Peter Anvin6520fe52012-05-08 21:22:24 +030087 return sym_regex[type] &&
88 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
H. Peter Anvin873b5272009-12-14 13:55:20 -080089}
90
H. Peter Anvin6520fe52012-05-08 21:22:24 +030091static void regex_init(int use_real_mode)
H. Peter Anvin873b5272009-12-14 13:55:20 -080092{
93 char errbuf[128];
94 int err;
H. Peter Anvin6520fe52012-05-08 21:22:24 +030095 int i;
H. Peter Anvin873b5272009-12-14 13:55:20 -080096
H. Peter Anvin6520fe52012-05-08 21:22:24 +030097 if (use_real_mode)
98 sym_regex = sym_regex_realmode;
99 else
100 sym_regex = sym_regex_kernel;
101
102 for (i = 0; i < S_NSYMTYPES; i++) {
103 if (!sym_regex[i])
104 continue;
105
106 err = regcomp(&sym_regex_c[i], sym_regex[i],
107 REG_EXTENDED|REG_NOSUB);
108
109 if (err) {
110 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
111 die("%s", errbuf);
112 }
H. Peter Anvin873b5272009-12-14 13:55:20 -0800113 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100114}
115
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100116static void die(char *fmt, ...)
117{
118 va_list ap;
119 va_start(ap, fmt);
120 vfprintf(stderr, fmt, ap);
121 va_end(ap);
122 exit(1);
123}
124
125static const char *sym_type(unsigned type)
126{
127 static const char *type_name[] = {
128#define SYM_TYPE(X) [X] = #X
129 SYM_TYPE(STT_NOTYPE),
130 SYM_TYPE(STT_OBJECT),
131 SYM_TYPE(STT_FUNC),
132 SYM_TYPE(STT_SECTION),
133 SYM_TYPE(STT_FILE),
134 SYM_TYPE(STT_COMMON),
135 SYM_TYPE(STT_TLS),
136#undef SYM_TYPE
137 };
138 const char *name = "unknown sym type name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100139 if (type < ARRAY_SIZE(type_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100140 name = type_name[type];
141 }
142 return name;
143}
144
145static const char *sym_bind(unsigned bind)
146{
147 static const char *bind_name[] = {
148#define SYM_BIND(X) [X] = #X
149 SYM_BIND(STB_LOCAL),
150 SYM_BIND(STB_GLOBAL),
151 SYM_BIND(STB_WEAK),
152#undef SYM_BIND
153 };
154 const char *name = "unknown sym bind name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100155 if (bind < ARRAY_SIZE(bind_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100156 name = bind_name[bind];
157 }
158 return name;
159}
160
161static const char *sym_visibility(unsigned visibility)
162{
163 static const char *visibility_name[] = {
164#define SYM_VISIBILITY(X) [X] = #X
165 SYM_VISIBILITY(STV_DEFAULT),
166 SYM_VISIBILITY(STV_INTERNAL),
167 SYM_VISIBILITY(STV_HIDDEN),
168 SYM_VISIBILITY(STV_PROTECTED),
169#undef SYM_VISIBILITY
170 };
171 const char *name = "unknown sym visibility name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100172 if (visibility < ARRAY_SIZE(visibility_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100173 name = visibility_name[visibility];
174 }
175 return name;
176}
177
178static const char *rel_type(unsigned type)
179{
180 static const char *type_name[] = {
181#define REL_TYPE(X) [X] = #X
182 REL_TYPE(R_386_NONE),
183 REL_TYPE(R_386_32),
184 REL_TYPE(R_386_PC32),
185 REL_TYPE(R_386_GOT32),
186 REL_TYPE(R_386_PLT32),
187 REL_TYPE(R_386_COPY),
188 REL_TYPE(R_386_GLOB_DAT),
189 REL_TYPE(R_386_JMP_SLOT),
190 REL_TYPE(R_386_RELATIVE),
191 REL_TYPE(R_386_GOTOFF),
192 REL_TYPE(R_386_GOTPC),
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300193 REL_TYPE(R_386_8),
194 REL_TYPE(R_386_PC8),
195 REL_TYPE(R_386_16),
196 REL_TYPE(R_386_PC16),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100197#undef REL_TYPE
198 };
199 const char *name = "unknown type rel type name";
H. Peter Anvin873b5272009-12-14 13:55:20 -0800200 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100201 name = type_name[type];
202 }
203 return name;
204}
205
206static const char *sec_name(unsigned shndx)
207{
208 const char *sec_strtab;
209 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700210 sec_strtab = secs[ehdr.e_shstrndx].strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100211 name = "<noname>";
212 if (shndx < ehdr.e_shnum) {
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700213 name = sec_strtab + secs[shndx].shdr.sh_name;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100214 }
215 else if (shndx == SHN_ABS) {
216 name = "ABSOLUTE";
217 }
218 else if (shndx == SHN_COMMON) {
219 name = "COMMON";
220 }
221 return name;
222}
223
224static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
225{
226 const char *name;
227 name = "<noname>";
228 if (sym->st_name) {
229 name = sym_strtab + sym->st_name;
230 }
231 else {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300232 name = sec_name(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100233 }
234 return name;
235}
236
237
238
Linus Torvalds13da9e22010-05-26 08:30:15 -0700239#if BYTE_ORDER == LITTLE_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100240#define le16_to_cpu(val) (val)
241#define le32_to_cpu(val) (val)
242#endif
Linus Torvalds13da9e22010-05-26 08:30:15 -0700243#if BYTE_ORDER == BIG_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100244#define le16_to_cpu(val) bswap_16(val)
245#define le32_to_cpu(val) bswap_32(val)
246#endif
247
248static uint16_t elf16_to_cpu(uint16_t val)
249{
250 return le16_to_cpu(val);
251}
252
253static uint32_t elf32_to_cpu(uint32_t val)
254{
255 return le32_to_cpu(val);
256}
257
258static void read_ehdr(FILE *fp)
259{
260 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
261 die("Cannot read ELF header: %s\n",
262 strerror(errno));
263 }
Cyrill Gorcunov8bd17962008-05-03 14:18:03 +0400264 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100265 die("No ELF magic\n");
266 }
267 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
268 die("Not a 32 bit executable\n");
269 }
270 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
271 die("Not a LSB ELF executable\n");
272 }
273 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
274 die("Unknown ELF version\n");
275 }
276 /* Convert the fields to native endian */
277 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
278 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
279 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
280 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
281 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
282 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
283 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
284 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
285 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
286 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
287 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
288 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
289 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
290
291 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
292 die("Unsupported ELF header type\n");
293 }
294 if (ehdr.e_machine != EM_386) {
295 die("Not for x86\n");
296 }
297 if (ehdr.e_version != EV_CURRENT) {
298 die("Unknown ELF version\n");
299 }
300 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
301 die("Bad Elf header size\n");
302 }
303 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
304 die("Bad program header entry\n");
305 }
306 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
307 die("Bad section header entry\n");
308 }
309 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
310 die("String table index out of bounds\n");
311 }
312}
313
314static void read_shdrs(FILE *fp)
315{
316 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700317 Elf32_Shdr shdr;
318
319 secs = calloc(ehdr.e_shnum, sizeof(struct section));
320 if (!secs) {
321 die("Unable to allocate %d section headers\n",
322 ehdr.e_shnum);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100323 }
324 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
325 die("Seek to %d failed: %s\n",
326 ehdr.e_shoff, strerror(errno));
327 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700328 for (i = 0; i < ehdr.e_shnum; i++) {
329 struct section *sec = &secs[i];
330 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
331 die("Cannot read ELF section headers %d/%d: %s\n",
332 i, ehdr.e_shnum, strerror(errno));
333 sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name);
334 sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type);
335 sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags);
336 sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr);
337 sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset);
338 sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size);
339 sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link);
340 sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info);
341 sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign);
342 sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize);
343 if (sec->shdr.sh_link < ehdr.e_shnum)
344 sec->link = &secs[sec->shdr.sh_link];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100345 }
346
347}
348
349static void read_strtabs(FILE *fp)
350{
351 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700352 for (i = 0; i < ehdr.e_shnum; i++) {
353 struct section *sec = &secs[i];
354 if (sec->shdr.sh_type != SHT_STRTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100355 continue;
356 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700357 sec->strtab = malloc(sec->shdr.sh_size);
358 if (!sec->strtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100359 die("malloc of %d bytes for strtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700360 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100361 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700362 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100363 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700364 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100365 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700366 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
367 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100368 die("Cannot read symbol table: %s\n",
369 strerror(errno));
370 }
371 }
372}
373
374static void read_symtabs(FILE *fp)
375{
376 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700377 for (i = 0; i < ehdr.e_shnum; i++) {
378 struct section *sec = &secs[i];
379 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100380 continue;
381 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700382 sec->symtab = malloc(sec->shdr.sh_size);
383 if (!sec->symtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100384 die("malloc of %d bytes for symtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700385 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100386 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700387 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100388 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700389 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100390 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700391 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
392 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100393 die("Cannot read symbol table: %s\n",
394 strerror(errno));
395 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700396 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
397 Elf32_Sym *sym = &sec->symtab[j];
398 sym->st_name = elf32_to_cpu(sym->st_name);
399 sym->st_value = elf32_to_cpu(sym->st_value);
400 sym->st_size = elf32_to_cpu(sym->st_size);
401 sym->st_shndx = elf16_to_cpu(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100402 }
403 }
404}
405
406
407static void read_relocs(FILE *fp)
408{
409 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700410 for (i = 0; i < ehdr.e_shnum; i++) {
411 struct section *sec = &secs[i];
412 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100413 continue;
414 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700415 sec->reltab = malloc(sec->shdr.sh_size);
416 if (!sec->reltab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100417 die("malloc of %d bytes for relocs failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700418 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100419 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700420 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100421 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700422 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100423 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700424 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
425 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100426 die("Cannot read symbol table: %s\n",
427 strerror(errno));
428 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700429 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
430 Elf32_Rel *rel = &sec->reltab[j];
431 rel->r_offset = elf32_to_cpu(rel->r_offset);
432 rel->r_info = elf32_to_cpu(rel->r_info);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100433 }
434 }
435}
436
437
438static void print_absolute_symbols(void)
439{
440 int i;
441 printf("Absolute symbols\n");
442 printf(" Num: Value Size Type Bind Visibility Name\n");
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700443 for (i = 0; i < ehdr.e_shnum; i++) {
444 struct section *sec = &secs[i];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100445 char *sym_strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100446 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700447
448 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100449 continue;
450 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700451 sym_strtab = sec->link->strtab;
452 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100453 Elf32_Sym *sym;
454 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700455 sym = &sec->symtab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100456 name = sym_name(sym_strtab, sym);
457 if (sym->st_shndx != SHN_ABS) {
458 continue;
459 }
460 printf("%5d %08x %5d %10s %10s %12s %s\n",
461 j, sym->st_value, sym->st_size,
462 sym_type(ELF32_ST_TYPE(sym->st_info)),
463 sym_bind(ELF32_ST_BIND(sym->st_info)),
464 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
465 name);
466 }
467 }
468 printf("\n");
469}
470
471static void print_absolute_relocs(void)
472{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100473 int i, printed = 0;
474
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700475 for (i = 0; i < ehdr.e_shnum; i++) {
476 struct section *sec = &secs[i];
477 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100478 char *sym_strtab;
479 Elf32_Sym *sh_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100480 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700481 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100482 continue;
483 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700484 sec_symtab = sec->link;
485 sec_applies = &secs[sec->shdr.sh_info];
486 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100487 continue;
488 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700489 sh_symtab = sec_symtab->symtab;
490 sym_strtab = sec_symtab->link->strtab;
491 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100492 Elf32_Rel *rel;
493 Elf32_Sym *sym;
494 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700495 rel = &sec->reltab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100496 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
497 name = sym_name(sym_strtab, sym);
498 if (sym->st_shndx != SHN_ABS) {
499 continue;
500 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100501
502 /* Absolute symbols are not relocated if bzImage is
503 * loaded at a non-compiled address. Display a warning
504 * to user at compile time about the absolute
505 * relocations present.
506 *
507 * User need to audit the code to make sure
508 * some symbols which should have been section
509 * relative have not become absolute because of some
510 * linker optimization or wrong programming usage.
511 *
512 * Before warning check if this absolute symbol
513 * relocation is harmless.
514 */
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300515 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
Vivek Goyal6a044b32006-12-07 02:14:04 +0100516 continue;
517
518 if (!printed) {
519 printf("WARNING: Absolute relocations"
520 " present\n");
521 printf("Offset Info Type Sym.Value "
522 "Sym.Name\n");
523 printed = 1;
524 }
525
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100526 printf("%08x %08x %10s %08x %s\n",
527 rel->r_offset,
528 rel->r_info,
529 rel_type(ELF32_R_TYPE(rel->r_info)),
530 sym->st_value,
531 name);
532 }
533 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100534
535 if (printed)
536 printf("\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100537}
538
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300539static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym),
540 int use_real_mode)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100541{
542 int i;
543 /* Walk through the relocations */
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700544 for (i = 0; i < ehdr.e_shnum; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100545 char *sym_strtab;
546 Elf32_Sym *sh_symtab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700547 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100548 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700549 struct section *sec = &secs[i];
550
551 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100552 continue;
553 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700554 sec_symtab = sec->link;
555 sec_applies = &secs[sec->shdr.sh_info];
556 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100557 continue;
558 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700559 sh_symtab = sec_symtab->symtab;
H. Peter Anvincc65f1e2008-10-03 13:00:56 -0700560 sym_strtab = sec_symtab->link->strtab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700561 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100562 Elf32_Rel *rel;
563 Elf32_Sym *sym;
564 unsigned r_type;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300565 const char *symname;
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700566 int shn_abs;
567
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700568 rel = &sec->reltab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100569 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
570 r_type = ELF32_R_TYPE(rel->r_info);
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300571
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700572 shn_abs = sym->st_shndx == SHN_ABS;
573
H. Peter Anvin873b5272009-12-14 13:55:20 -0800574 switch (r_type) {
575 case R_386_NONE:
576 case R_386_PC32:
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300577 case R_386_PC16:
578 case R_386_PC8:
Tejun Heo46176b42009-05-26 14:42:40 +0900579 /*
580 * NONE can be ignored and and PC relative
581 * relocations don't need to be adjusted.
582 */
H. Peter Anvin873b5272009-12-14 13:55:20 -0800583 break;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300584
585 case R_386_16:
586 symname = sym_name(sym_strtab, sym);
587 if (!use_real_mode)
588 goto bad;
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700589 if (shn_abs) {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300590 if (is_reloc(S_ABS, symname))
591 break;
592 else if (!is_reloc(S_SEG, symname))
593 goto bad;
594 } else {
595 if (is_reloc(S_LIN, symname))
596 goto bad;
597 else
598 break;
599 }
600 visit(rel, sym);
601 break;
602
H. Peter Anvin873b5272009-12-14 13:55:20 -0800603 case R_386_32:
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300604 symname = sym_name(sym_strtab, sym);
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700605 if (shn_abs) {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300606 if (is_reloc(S_ABS, symname))
607 break;
608 else if (!is_reloc(S_REL, symname))
609 goto bad;
610 } else {
611 if (use_real_mode &&
612 !is_reloc(S_LIN, symname))
613 break;
614 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100615 visit(rel, sym);
H. Peter Anvin873b5272009-12-14 13:55:20 -0800616 break;
617 default:
618 die("Unsupported relocation type: %s (%d)\n",
619 rel_type(r_type), r_type);
620 break;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300621 bad:
622 symname = sym_name(sym_strtab, sym);
H. Peter Anvin24ab82b2012-05-18 09:52:01 -0700623 die("Invalid %s %s relocation: %s\n",
624 shn_abs ? "absolute" : "relative",
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300625 rel_type(r_type), symname);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100626 }
627 }
628 }
629}
630
631static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
632{
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300633 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
634 reloc16_count++;
635 else
636 reloc_count++;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100637}
638
639static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
640{
641 /* Remember the address that needs to be adjusted. */
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300642 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
643 relocs16[reloc16_idx++] = rel->r_offset;
644 else
645 relocs[reloc_idx++] = rel->r_offset;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100646}
647
648static int cmp_relocs(const void *va, const void *vb)
649{
650 const unsigned long *a, *b;
651 a = va; b = vb;
652 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
653}
654
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300655static int write32(unsigned int v, FILE *f)
656{
657 unsigned char buf[4];
658
659 put_unaligned_le32(v, buf);
660 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
661}
662
663static void emit_relocs(int as_text, int use_real_mode)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100664{
665 int i;
666 /* Count how many relocations I have and allocate space for them. */
667 reloc_count = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300668 walk_relocs(count_reloc, use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100669 relocs = malloc(reloc_count * sizeof(relocs[0]));
670 if (!relocs) {
671 die("malloc of %d entries for relocs failed\n",
672 reloc_count);
673 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300674
675 relocs16 = malloc(reloc16_count * sizeof(relocs[0]));
676 if (!relocs16) {
677 die("malloc of %d entries for relocs16 failed\n",
678 reloc16_count);
679 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100680 /* Collect up the relocations */
681 reloc_idx = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300682 walk_relocs(collect_reloc, use_real_mode);
683
684 if (reloc16_count && !use_real_mode)
685 die("Segment relocations found but --realmode not specified\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100686
687 /* Order the relocations for more efficient processing */
688 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300689 qsort(relocs16, reloc16_count, sizeof(relocs16[0]), cmp_relocs);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100690
691 /* Print the relocations */
692 if (as_text) {
693 /* Print the relocations in a form suitable that
694 * gas will like.
695 */
696 printf(".section \".data.reloc\",\"a\"\n");
697 printf(".balign 4\n");
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300698 if (use_real_mode) {
699 printf("\t.long %lu\n", reloc16_count);
700 for (i = 0; i < reloc16_count; i++)
701 printf("\t.long 0x%08lx\n", relocs16[i]);
702 printf("\t.long %lu\n", reloc_count);
703 for (i = 0; i < reloc_count; i++) {
704 printf("\t.long 0x%08lx\n", relocs[i]);
705 }
706 } else {
707 /* Print a stop */
708 printf("\t.long 0x%08lx\n", (unsigned long)0);
709 for (i = 0; i < reloc_count; i++) {
710 printf("\t.long 0x%08lx\n", relocs[i]);
711 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100712 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300713
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100714 printf("\n");
715 }
716 else {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300717 if (use_real_mode) {
718 write32(reloc16_count, stdout);
719 for (i = 0; i < reloc16_count; i++)
720 write32(relocs16[i], stdout);
721 write32(reloc_count, stdout);
722
723 /* Now print each relocation */
724 for (i = 0; i < reloc_count; i++)
725 write32(relocs[i], stdout);
726 } else {
727 /* Print a stop */
728 write32(0, stdout);
729
730 /* Now print each relocation */
731 for (i = 0; i < reloc_count; i++) {
732 write32(relocs[i], stdout);
733 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100734 }
735 }
736}
737
738static void usage(void)
739{
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300740 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100741}
742
743int main(int argc, char **argv)
744{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100745 int show_absolute_syms, show_absolute_relocs;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300746 int as_text, use_real_mode;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100747 const char *fname;
748 FILE *fp;
749 int i;
750
Vivek Goyal6a044b32006-12-07 02:14:04 +0100751 show_absolute_syms = 0;
752 show_absolute_relocs = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100753 as_text = 0;
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300754 use_real_mode = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100755 fname = NULL;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700756 for (i = 1; i < argc; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100757 char *arg = argv[i];
758 if (*arg == '-') {
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300759 if (strcmp(arg, "--abs-syms") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100760 show_absolute_syms = 1;
761 continue;
762 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300763 if (strcmp(arg, "--abs-relocs") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100764 show_absolute_relocs = 1;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100765 continue;
766 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300767 if (strcmp(arg, "--text") == 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100768 as_text = 1;
769 continue;
770 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300771 if (strcmp(arg, "--realmode") == 0) {
772 use_real_mode = 1;
773 continue;
774 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100775 }
776 else if (!fname) {
777 fname = arg;
778 continue;
779 }
780 usage();
781 }
782 if (!fname) {
783 usage();
784 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300785 regex_init(use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100786 fp = fopen(fname, "r");
787 if (!fp) {
788 die("Cannot open %s: %s\n",
789 fname, strerror(errno));
790 }
791 read_ehdr(fp);
792 read_shdrs(fp);
793 read_strtabs(fp);
794 read_symtabs(fp);
795 read_relocs(fp);
Vivek Goyal6a044b32006-12-07 02:14:04 +0100796 if (show_absolute_syms) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100797 print_absolute_symbols();
Vivek Goyal6a044b32006-12-07 02:14:04 +0100798 return 0;
799 }
800 if (show_absolute_relocs) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100801 print_absolute_relocs();
802 return 0;
803 }
H. Peter Anvin6520fe52012-05-08 21:22:24 +0300804 emit_relocs(as_text, use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100805 return 0;
806}