blob: 02914706e5b98dd715cd36b8dc306f4ae9880dfb [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 Anvin433de732012-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 Anvin433de732012-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 Anvin433de732012-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 Anvin433de732012-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 Anvin433de732012-05-08 21:22:24 +030058 [S_REL] =
59 "^_end$",
60};
61
62
63static const char * const sym_regex_realmode[S_NSYMTYPES] = {
64/*
65 * These are 16-bit segment symbols when compiling 16-bit code.
66 */
67 [S_SEG] =
68 "^real_mode_seg$",
69
70/*
71 * These are offsets belonging to segments, as opposed to linear addresses,
72 * when compiling 16-bit code.
73 */
74 [S_LIN] =
75 "^pa_",
76};
77
78static const char * const *sym_regex;
79
80static regex_t sym_regex_c[S_NSYMTYPES];
81static int is_reloc(enum symtype type, const char *sym_name)
H. Peter Anvin873b5272009-12-14 13:55:20 -080082{
H. Peter Anvin433de732012-05-08 21:22:24 +030083 return sym_regex[type] &&
84 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
H. Peter Anvin873b5272009-12-14 13:55:20 -080085}
86
H. Peter Anvin433de732012-05-08 21:22:24 +030087static void regex_init(int use_real_mode)
H. Peter Anvin873b5272009-12-14 13:55:20 -080088{
89 char errbuf[128];
90 int err;
H. Peter Anvin433de732012-05-08 21:22:24 +030091 int i;
H. Peter Anvin873b5272009-12-14 13:55:20 -080092
H. Peter Anvin433de732012-05-08 21:22:24 +030093 if (use_real_mode)
94 sym_regex = sym_regex_realmode;
95 else
96 sym_regex = sym_regex_kernel;
97
98 for (i = 0; i < S_NSYMTYPES; i++) {
99 if (!sym_regex[i])
100 continue;
101
102 err = regcomp(&sym_regex_c[i], sym_regex[i],
103 REG_EXTENDED|REG_NOSUB);
104
105 if (err) {
106 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
107 die("%s", errbuf);
108 }
H. Peter Anvin873b5272009-12-14 13:55:20 -0800109 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100110}
111
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100112static void die(char *fmt, ...)
113{
114 va_list ap;
115 va_start(ap, fmt);
116 vfprintf(stderr, fmt, ap);
117 va_end(ap);
118 exit(1);
119}
120
121static const char *sym_type(unsigned type)
122{
123 static const char *type_name[] = {
124#define SYM_TYPE(X) [X] = #X
125 SYM_TYPE(STT_NOTYPE),
126 SYM_TYPE(STT_OBJECT),
127 SYM_TYPE(STT_FUNC),
128 SYM_TYPE(STT_SECTION),
129 SYM_TYPE(STT_FILE),
130 SYM_TYPE(STT_COMMON),
131 SYM_TYPE(STT_TLS),
132#undef SYM_TYPE
133 };
134 const char *name = "unknown sym type name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100135 if (type < ARRAY_SIZE(type_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100136 name = type_name[type];
137 }
138 return name;
139}
140
141static const char *sym_bind(unsigned bind)
142{
143 static const char *bind_name[] = {
144#define SYM_BIND(X) [X] = #X
145 SYM_BIND(STB_LOCAL),
146 SYM_BIND(STB_GLOBAL),
147 SYM_BIND(STB_WEAK),
148#undef SYM_BIND
149 };
150 const char *name = "unknown sym bind name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100151 if (bind < ARRAY_SIZE(bind_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100152 name = bind_name[bind];
153 }
154 return name;
155}
156
157static const char *sym_visibility(unsigned visibility)
158{
159 static const char *visibility_name[] = {
160#define SYM_VISIBILITY(X) [X] = #X
161 SYM_VISIBILITY(STV_DEFAULT),
162 SYM_VISIBILITY(STV_INTERNAL),
163 SYM_VISIBILITY(STV_HIDDEN),
164 SYM_VISIBILITY(STV_PROTECTED),
165#undef SYM_VISIBILITY
166 };
167 const char *name = "unknown sym visibility name";
Robert P. J. Dayca820182007-02-17 19:10:01 +0100168 if (visibility < ARRAY_SIZE(visibility_name)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100169 name = visibility_name[visibility];
170 }
171 return name;
172}
173
174static const char *rel_type(unsigned type)
175{
176 static const char *type_name[] = {
177#define REL_TYPE(X) [X] = #X
178 REL_TYPE(R_386_NONE),
179 REL_TYPE(R_386_32),
180 REL_TYPE(R_386_PC32),
181 REL_TYPE(R_386_GOT32),
182 REL_TYPE(R_386_PLT32),
183 REL_TYPE(R_386_COPY),
184 REL_TYPE(R_386_GLOB_DAT),
185 REL_TYPE(R_386_JMP_SLOT),
186 REL_TYPE(R_386_RELATIVE),
187 REL_TYPE(R_386_GOTOFF),
188 REL_TYPE(R_386_GOTPC),
H. Peter Anvin433de732012-05-08 21:22:24 +0300189 REL_TYPE(R_386_8),
190 REL_TYPE(R_386_PC8),
191 REL_TYPE(R_386_16),
192 REL_TYPE(R_386_PC16),
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100193#undef REL_TYPE
194 };
195 const char *name = "unknown type rel type name";
H. Peter Anvin873b5272009-12-14 13:55:20 -0800196 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100197 name = type_name[type];
198 }
199 return name;
200}
201
202static const char *sec_name(unsigned shndx)
203{
204 const char *sec_strtab;
205 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700206 sec_strtab = secs[ehdr.e_shstrndx].strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100207 name = "<noname>";
208 if (shndx < ehdr.e_shnum) {
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700209 name = sec_strtab + secs[shndx].shdr.sh_name;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100210 }
211 else if (shndx == SHN_ABS) {
212 name = "ABSOLUTE";
213 }
214 else if (shndx == SHN_COMMON) {
215 name = "COMMON";
216 }
217 return name;
218}
219
220static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
221{
222 const char *name;
223 name = "<noname>";
224 if (sym->st_name) {
225 name = sym_strtab + sym->st_name;
226 }
227 else {
H. Peter Anvin433de732012-05-08 21:22:24 +0300228 name = sec_name(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100229 }
230 return name;
231}
232
233
234
Linus Torvalds13da9e22010-05-26 08:30:15 -0700235#if BYTE_ORDER == LITTLE_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100236#define le16_to_cpu(val) (val)
237#define le32_to_cpu(val) (val)
238#endif
Linus Torvalds13da9e22010-05-26 08:30:15 -0700239#if BYTE_ORDER == BIG_ENDIAN
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100240#define le16_to_cpu(val) bswap_16(val)
241#define le32_to_cpu(val) bswap_32(val)
242#endif
243
244static uint16_t elf16_to_cpu(uint16_t val)
245{
246 return le16_to_cpu(val);
247}
248
249static uint32_t elf32_to_cpu(uint32_t val)
250{
251 return le32_to_cpu(val);
252}
253
254static void read_ehdr(FILE *fp)
255{
256 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
257 die("Cannot read ELF header: %s\n",
258 strerror(errno));
259 }
Cyrill Gorcunov8bd17962008-05-03 14:18:03 +0400260 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100261 die("No ELF magic\n");
262 }
263 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
264 die("Not a 32 bit executable\n");
265 }
266 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
267 die("Not a LSB ELF executable\n");
268 }
269 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
270 die("Unknown ELF version\n");
271 }
272 /* Convert the fields to native endian */
273 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
274 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
275 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
276 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
277 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
278 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
279 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
280 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
281 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
282 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
283 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
284 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
285 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
286
287 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
288 die("Unsupported ELF header type\n");
289 }
290 if (ehdr.e_machine != EM_386) {
291 die("Not for x86\n");
292 }
293 if (ehdr.e_version != EV_CURRENT) {
294 die("Unknown ELF version\n");
295 }
296 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
297 die("Bad Elf header size\n");
298 }
299 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
300 die("Bad program header entry\n");
301 }
302 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
303 die("Bad section header entry\n");
304 }
305 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
306 die("String table index out of bounds\n");
307 }
308}
309
310static void read_shdrs(FILE *fp)
311{
312 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700313 Elf32_Shdr shdr;
314
315 secs = calloc(ehdr.e_shnum, sizeof(struct section));
316 if (!secs) {
317 die("Unable to allocate %d section headers\n",
318 ehdr.e_shnum);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100319 }
320 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
321 die("Seek to %d failed: %s\n",
322 ehdr.e_shoff, strerror(errno));
323 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700324 for (i = 0; i < ehdr.e_shnum; i++) {
325 struct section *sec = &secs[i];
326 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
327 die("Cannot read ELF section headers %d/%d: %s\n",
328 i, ehdr.e_shnum, strerror(errno));
329 sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name);
330 sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type);
331 sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags);
332 sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr);
333 sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset);
334 sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size);
335 sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link);
336 sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info);
337 sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign);
338 sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize);
339 if (sec->shdr.sh_link < ehdr.e_shnum)
340 sec->link = &secs[sec->shdr.sh_link];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100341 }
342
343}
344
345static void read_strtabs(FILE *fp)
346{
347 int i;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700348 for (i = 0; i < ehdr.e_shnum; i++) {
349 struct section *sec = &secs[i];
350 if (sec->shdr.sh_type != SHT_STRTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100351 continue;
352 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700353 sec->strtab = malloc(sec->shdr.sh_size);
354 if (!sec->strtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100355 die("malloc of %d bytes for strtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700356 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100357 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700358 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100359 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700360 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100361 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700362 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
363 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100364 die("Cannot read symbol table: %s\n",
365 strerror(errno));
366 }
367 }
368}
369
370static void read_symtabs(FILE *fp)
371{
372 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700373 for (i = 0; i < ehdr.e_shnum; i++) {
374 struct section *sec = &secs[i];
375 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100376 continue;
377 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700378 sec->symtab = malloc(sec->shdr.sh_size);
379 if (!sec->symtab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100380 die("malloc of %d bytes for symtab failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700381 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100382 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700383 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100384 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700385 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100386 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700387 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
388 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100389 die("Cannot read symbol table: %s\n",
390 strerror(errno));
391 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700392 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
393 Elf32_Sym *sym = &sec->symtab[j];
394 sym->st_name = elf32_to_cpu(sym->st_name);
395 sym->st_value = elf32_to_cpu(sym->st_value);
396 sym->st_size = elf32_to_cpu(sym->st_size);
397 sym->st_shndx = elf16_to_cpu(sym->st_shndx);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100398 }
399 }
400}
401
402
403static void read_relocs(FILE *fp)
404{
405 int i,j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700406 for (i = 0; i < ehdr.e_shnum; i++) {
407 struct section *sec = &secs[i];
408 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100409 continue;
410 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700411 sec->reltab = malloc(sec->shdr.sh_size);
412 if (!sec->reltab) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100413 die("malloc of %d bytes for relocs failed\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700414 sec->shdr.sh_size);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100415 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700416 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100417 die("Seek to %d failed: %s\n",
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700418 sec->shdr.sh_offset, strerror(errno));
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100419 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700420 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
421 != sec->shdr.sh_size) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100422 die("Cannot read symbol table: %s\n",
423 strerror(errno));
424 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700425 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
426 Elf32_Rel *rel = &sec->reltab[j];
427 rel->r_offset = elf32_to_cpu(rel->r_offset);
428 rel->r_info = elf32_to_cpu(rel->r_info);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100429 }
430 }
431}
432
433
434static void print_absolute_symbols(void)
435{
436 int i;
437 printf("Absolute symbols\n");
438 printf(" Num: Value Size Type Bind Visibility Name\n");
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700439 for (i = 0; i < ehdr.e_shnum; i++) {
440 struct section *sec = &secs[i];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100441 char *sym_strtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100442 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700443
444 if (sec->shdr.sh_type != SHT_SYMTAB) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100445 continue;
446 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700447 sym_strtab = sec->link->strtab;
448 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100449 Elf32_Sym *sym;
450 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700451 sym = &sec->symtab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100452 name = sym_name(sym_strtab, sym);
453 if (sym->st_shndx != SHN_ABS) {
454 continue;
455 }
456 printf("%5d %08x %5d %10s %10s %12s %s\n",
457 j, sym->st_value, sym->st_size,
458 sym_type(ELF32_ST_TYPE(sym->st_info)),
459 sym_bind(ELF32_ST_BIND(sym->st_info)),
460 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
461 name);
462 }
463 }
464 printf("\n");
465}
466
467static void print_absolute_relocs(void)
468{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100469 int i, printed = 0;
470
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700471 for (i = 0; i < ehdr.e_shnum; i++) {
472 struct section *sec = &secs[i];
473 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100474 char *sym_strtab;
475 Elf32_Sym *sh_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100476 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700477 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100478 continue;
479 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700480 sec_symtab = sec->link;
481 sec_applies = &secs[sec->shdr.sh_info];
482 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100483 continue;
484 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700485 sh_symtab = sec_symtab->symtab;
486 sym_strtab = sec_symtab->link->strtab;
487 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100488 Elf32_Rel *rel;
489 Elf32_Sym *sym;
490 const char *name;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700491 rel = &sec->reltab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100492 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
493 name = sym_name(sym_strtab, sym);
494 if (sym->st_shndx != SHN_ABS) {
495 continue;
496 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100497
498 /* Absolute symbols are not relocated if bzImage is
499 * loaded at a non-compiled address. Display a warning
500 * to user at compile time about the absolute
501 * relocations present.
502 *
503 * User need to audit the code to make sure
504 * some symbols which should have been section
505 * relative have not become absolute because of some
506 * linker optimization or wrong programming usage.
507 *
508 * Before warning check if this absolute symbol
509 * relocation is harmless.
510 */
H. Peter Anvin433de732012-05-08 21:22:24 +0300511 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
Vivek Goyal6a044b32006-12-07 02:14:04 +0100512 continue;
513
514 if (!printed) {
515 printf("WARNING: Absolute relocations"
516 " present\n");
517 printf("Offset Info Type Sym.Value "
518 "Sym.Name\n");
519 printed = 1;
520 }
521
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100522 printf("%08x %08x %10s %08x %s\n",
523 rel->r_offset,
524 rel->r_info,
525 rel_type(ELF32_R_TYPE(rel->r_info)),
526 sym->st_value,
527 name);
528 }
529 }
Vivek Goyal6a044b32006-12-07 02:14:04 +0100530
531 if (printed)
532 printf("\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100533}
534
H. Peter Anvin433de732012-05-08 21:22:24 +0300535static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym),
536 int use_real_mode)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100537{
538 int i;
539 /* Walk through the relocations */
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700540 for (i = 0; i < ehdr.e_shnum; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100541 char *sym_strtab;
542 Elf32_Sym *sh_symtab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700543 struct section *sec_applies, *sec_symtab;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100544 int j;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700545 struct section *sec = &secs[i];
546
547 if (sec->shdr.sh_type != SHT_REL) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100548 continue;
549 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700550 sec_symtab = sec->link;
551 sec_applies = &secs[sec->shdr.sh_info];
552 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100553 continue;
554 }
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700555 sh_symtab = sec_symtab->symtab;
H. Peter Anvincc65f1e2008-10-03 13:00:56 -0700556 sym_strtab = sec_symtab->link->strtab;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700557 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100558 Elf32_Rel *rel;
559 Elf32_Sym *sym;
560 unsigned r_type;
H. Peter Anvin433de732012-05-08 21:22:24 +0300561 const char *symname;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700562 rel = &sec->reltab[j];
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100563 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
564 r_type = ELF32_R_TYPE(rel->r_info);
H. Peter Anvin433de732012-05-08 21:22:24 +0300565
H. Peter Anvin873b5272009-12-14 13:55:20 -0800566 switch (r_type) {
567 case R_386_NONE:
568 case R_386_PC32:
H. Peter Anvin433de732012-05-08 21:22:24 +0300569 case R_386_PC16:
570 case R_386_PC8:
Tejun Heo46176b42009-05-26 14:42:40 +0900571 /*
572 * NONE can be ignored and and PC relative
573 * relocations don't need to be adjusted.
574 */
H. Peter Anvin873b5272009-12-14 13:55:20 -0800575 break;
H. Peter Anvin433de732012-05-08 21:22:24 +0300576
577 case R_386_16:
578 symname = sym_name(sym_strtab, sym);
579 if (!use_real_mode)
580 goto bad;
581 if (sym->st_shndx == SHN_ABS) {
582 if (is_reloc(S_ABS, symname))
583 break;
584 else if (!is_reloc(S_SEG, symname))
585 goto bad;
586 } else {
587 if (is_reloc(S_LIN, symname))
588 goto bad;
589 else
590 break;
591 }
592 visit(rel, sym);
593 break;
594
H. Peter Anvin873b5272009-12-14 13:55:20 -0800595 case R_386_32:
H. Peter Anvin433de732012-05-08 21:22:24 +0300596 symname = sym_name(sym_strtab, sym);
597 if (sym->st_shndx == SHN_ABS) {
598 if (is_reloc(S_ABS, symname))
599 break;
600 else if (!is_reloc(S_REL, symname))
601 goto bad;
602 } else {
603 if (use_real_mode &&
604 !is_reloc(S_LIN, symname))
605 break;
606 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100607 visit(rel, sym);
H. Peter Anvin873b5272009-12-14 13:55:20 -0800608 break;
609 default:
610 die("Unsupported relocation type: %s (%d)\n",
611 rel_type(r_type), r_type);
612 break;
H. Peter Anvin433de732012-05-08 21:22:24 +0300613 bad:
614 symname = sym_name(sym_strtab, sym);
615 die("Invalid %s relocation: %s\n",
616 rel_type(r_type), symname);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100617 }
618 }
619 }
620}
621
622static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
623{
H. Peter Anvin433de732012-05-08 21:22:24 +0300624 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
625 reloc16_count++;
626 else
627 reloc_count++;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100628}
629
630static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
631{
632 /* Remember the address that needs to be adjusted. */
H. Peter Anvin433de732012-05-08 21:22:24 +0300633 if (ELF32_R_TYPE(rel->r_info) == R_386_16)
634 relocs16[reloc16_idx++] = rel->r_offset;
635 else
636 relocs[reloc_idx++] = rel->r_offset;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100637}
638
639static int cmp_relocs(const void *va, const void *vb)
640{
641 const unsigned long *a, *b;
642 a = va; b = vb;
643 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
644}
645
H. Peter Anvin433de732012-05-08 21:22:24 +0300646static int write32(unsigned int v, FILE *f)
647{
648 unsigned char buf[4];
649
650 put_unaligned_le32(v, buf);
651 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
652}
653
654static void emit_relocs(int as_text, int use_real_mode)
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100655{
656 int i;
657 /* Count how many relocations I have and allocate space for them. */
658 reloc_count = 0;
H. Peter Anvin433de732012-05-08 21:22:24 +0300659 walk_relocs(count_reloc, use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100660 relocs = malloc(reloc_count * sizeof(relocs[0]));
661 if (!relocs) {
662 die("malloc of %d entries for relocs failed\n",
663 reloc_count);
664 }
H. Peter Anvin433de732012-05-08 21:22:24 +0300665
666 relocs16 = malloc(reloc16_count * sizeof(relocs[0]));
667 if (!relocs16) {
668 die("malloc of %d entries for relocs16 failed\n",
669 reloc16_count);
670 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100671 /* Collect up the relocations */
672 reloc_idx = 0;
H. Peter Anvin433de732012-05-08 21:22:24 +0300673 walk_relocs(collect_reloc, use_real_mode);
674
675 if (reloc16_count && !use_real_mode)
676 die("Segment relocations found but --realmode not specified\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100677
678 /* Order the relocations for more efficient processing */
679 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
H. Peter Anvin433de732012-05-08 21:22:24 +0300680 qsort(relocs16, reloc16_count, sizeof(relocs16[0]), cmp_relocs);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100681
682 /* Print the relocations */
683 if (as_text) {
684 /* Print the relocations in a form suitable that
685 * gas will like.
686 */
687 printf(".section \".data.reloc\",\"a\"\n");
688 printf(".balign 4\n");
H. Peter Anvin433de732012-05-08 21:22:24 +0300689 if (use_real_mode) {
690 printf("\t.long %lu\n", reloc16_count);
691 for (i = 0; i < reloc16_count; i++)
692 printf("\t.long 0x%08lx\n", relocs16[i]);
693 printf("\t.long %lu\n", reloc_count);
694 for (i = 0; i < reloc_count; i++) {
695 printf("\t.long 0x%08lx\n", relocs[i]);
696 }
697 } else {
698 /* Print a stop */
699 printf("\t.long 0x%08lx\n", (unsigned long)0);
700 for (i = 0; i < reloc_count; i++) {
701 printf("\t.long 0x%08lx\n", relocs[i]);
702 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100703 }
H. Peter Anvin433de732012-05-08 21:22:24 +0300704
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100705 printf("\n");
706 }
707 else {
H. Peter Anvin433de732012-05-08 21:22:24 +0300708 if (use_real_mode) {
709 write32(reloc16_count, stdout);
710 for (i = 0; i < reloc16_count; i++)
711 write32(relocs16[i], stdout);
712 write32(reloc_count, stdout);
713
714 /* Now print each relocation */
715 for (i = 0; i < reloc_count; i++)
716 write32(relocs[i], stdout);
717 } else {
718 /* Print a stop */
719 write32(0, stdout);
720
721 /* Now print each relocation */
722 for (i = 0; i < reloc_count; i++) {
723 write32(relocs[i], stdout);
724 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100725 }
726 }
727}
728
729static void usage(void)
730{
H. Peter Anvin433de732012-05-08 21:22:24 +0300731 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100732}
733
734int main(int argc, char **argv)
735{
Vivek Goyal6a044b32006-12-07 02:14:04 +0100736 int show_absolute_syms, show_absolute_relocs;
H. Peter Anvin433de732012-05-08 21:22:24 +0300737 int as_text, use_real_mode;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100738 const char *fname;
739 FILE *fp;
740 int i;
741
Vivek Goyal6a044b32006-12-07 02:14:04 +0100742 show_absolute_syms = 0;
743 show_absolute_relocs = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100744 as_text = 0;
H. Peter Anvin433de732012-05-08 21:22:24 +0300745 use_real_mode = 0;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100746 fname = NULL;
H. Peter Anvin908ec7a2008-06-30 14:42:18 -0700747 for (i = 1; i < argc; i++) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100748 char *arg = argv[i];
749 if (*arg == '-') {
H. Peter Anvin433de732012-05-08 21:22:24 +0300750 if (strcmp(arg, "--abs-syms") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100751 show_absolute_syms = 1;
752 continue;
753 }
H. Peter Anvin433de732012-05-08 21:22:24 +0300754 if (strcmp(arg, "--abs-relocs") == 0) {
Vivek Goyal6a044b32006-12-07 02:14:04 +0100755 show_absolute_relocs = 1;
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100756 continue;
757 }
H. Peter Anvin433de732012-05-08 21:22:24 +0300758 if (strcmp(arg, "--text") == 0) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100759 as_text = 1;
760 continue;
761 }
H. Peter Anvin433de732012-05-08 21:22:24 +0300762 if (strcmp(arg, "--realmode") == 0) {
763 use_real_mode = 1;
764 continue;
765 }
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100766 }
767 else if (!fname) {
768 fname = arg;
769 continue;
770 }
771 usage();
772 }
773 if (!fname) {
774 usage();
775 }
H. Peter Anvin433de732012-05-08 21:22:24 +0300776 regex_init(use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100777 fp = fopen(fname, "r");
778 if (!fp) {
779 die("Cannot open %s: %s\n",
780 fname, strerror(errno));
781 }
782 read_ehdr(fp);
783 read_shdrs(fp);
784 read_strtabs(fp);
785 read_symtabs(fp);
786 read_relocs(fp);
Vivek Goyal6a044b32006-12-07 02:14:04 +0100787 if (show_absolute_syms) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100788 print_absolute_symbols();
Vivek Goyal6a044b32006-12-07 02:14:04 +0100789 return 0;
790 }
791 if (show_absolute_relocs) {
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100792 print_absolute_relocs();
793 return 0;
794 }
H. Peter Anvin433de732012-05-08 21:22:24 +0300795 emit_relocs(as_text, use_real_mode);
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100796 return 0;
797}