libata: implement EH fast drain
[GitHub/MotorolaMobilityLLC/kernel-slsi.git] / arch / i386 / boot / compressed / relocs.c
1 #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>
12
13 #define MAX_SHDRS 100
14 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
15 static Elf32_Ehdr ehdr;
16 static Elf32_Shdr shdr[MAX_SHDRS];
17 static Elf32_Sym *symtab[MAX_SHDRS];
18 static Elf32_Rel *reltab[MAX_SHDRS];
19 static char *strtab[MAX_SHDRS];
20 static unsigned long reloc_count, reloc_idx;
21 static unsigned long *relocs;
22
23 /*
24 * Following symbols have been audited. There values are constant and do
25 * not change if bzImage is loaded at a different physical address than
26 * the address for which it has been compiled. Don't warn user about
27 * absolute relocations present w.r.t these symbols.
28 */
29 static const char* safe_abs_relocs[] = {
30 "__kernel_vsyscall",
31 "__kernel_rt_sigreturn",
32 "__kernel_sigreturn",
33 "SYSENTER_RETURN",
34 "xen_irq_disable_direct_reloc",
35 "xen_save_fl_direct_reloc",
36 };
37
38 static int is_safe_abs_reloc(const char* sym_name)
39 {
40 int i, array_size;
41
42 array_size = sizeof(safe_abs_relocs)/sizeof(char*);
43
44 for(i = 0; i < array_size; i++) {
45 if (!strcmp(sym_name, safe_abs_relocs[i]))
46 /* Match found */
47 return 1;
48 }
49 if (strncmp(sym_name, "__crc_", 6) == 0)
50 return 1;
51 return 0;
52 }
53
54 static void die(char *fmt, ...)
55 {
56 va_list ap;
57 va_start(ap, fmt);
58 vfprintf(stderr, fmt, ap);
59 va_end(ap);
60 exit(1);
61 }
62
63 static const char *sym_type(unsigned type)
64 {
65 static const char *type_name[] = {
66 #define SYM_TYPE(X) [X] = #X
67 SYM_TYPE(STT_NOTYPE),
68 SYM_TYPE(STT_OBJECT),
69 SYM_TYPE(STT_FUNC),
70 SYM_TYPE(STT_SECTION),
71 SYM_TYPE(STT_FILE),
72 SYM_TYPE(STT_COMMON),
73 SYM_TYPE(STT_TLS),
74 #undef SYM_TYPE
75 };
76 const char *name = "unknown sym type name";
77 if (type < ARRAY_SIZE(type_name)) {
78 name = type_name[type];
79 }
80 return name;
81 }
82
83 static const char *sym_bind(unsigned bind)
84 {
85 static const char *bind_name[] = {
86 #define SYM_BIND(X) [X] = #X
87 SYM_BIND(STB_LOCAL),
88 SYM_BIND(STB_GLOBAL),
89 SYM_BIND(STB_WEAK),
90 #undef SYM_BIND
91 };
92 const char *name = "unknown sym bind name";
93 if (bind < ARRAY_SIZE(bind_name)) {
94 name = bind_name[bind];
95 }
96 return name;
97 }
98
99 static const char *sym_visibility(unsigned visibility)
100 {
101 static const char *visibility_name[] = {
102 #define SYM_VISIBILITY(X) [X] = #X
103 SYM_VISIBILITY(STV_DEFAULT),
104 SYM_VISIBILITY(STV_INTERNAL),
105 SYM_VISIBILITY(STV_HIDDEN),
106 SYM_VISIBILITY(STV_PROTECTED),
107 #undef SYM_VISIBILITY
108 };
109 const char *name = "unknown sym visibility name";
110 if (visibility < ARRAY_SIZE(visibility_name)) {
111 name = visibility_name[visibility];
112 }
113 return name;
114 }
115
116 static const char *rel_type(unsigned type)
117 {
118 static const char *type_name[] = {
119 #define REL_TYPE(X) [X] = #X
120 REL_TYPE(R_386_NONE),
121 REL_TYPE(R_386_32),
122 REL_TYPE(R_386_PC32),
123 REL_TYPE(R_386_GOT32),
124 REL_TYPE(R_386_PLT32),
125 REL_TYPE(R_386_COPY),
126 REL_TYPE(R_386_GLOB_DAT),
127 REL_TYPE(R_386_JMP_SLOT),
128 REL_TYPE(R_386_RELATIVE),
129 REL_TYPE(R_386_GOTOFF),
130 REL_TYPE(R_386_GOTPC),
131 #undef REL_TYPE
132 };
133 const char *name = "unknown type rel type name";
134 if (type < ARRAY_SIZE(type_name)) {
135 name = type_name[type];
136 }
137 return name;
138 }
139
140 static const char *sec_name(unsigned shndx)
141 {
142 const char *sec_strtab;
143 const char *name;
144 sec_strtab = strtab[ehdr.e_shstrndx];
145 name = "<noname>";
146 if (shndx < ehdr.e_shnum) {
147 name = sec_strtab + shdr[shndx].sh_name;
148 }
149 else if (shndx == SHN_ABS) {
150 name = "ABSOLUTE";
151 }
152 else if (shndx == SHN_COMMON) {
153 name = "COMMON";
154 }
155 return name;
156 }
157
158 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
159 {
160 const char *name;
161 name = "<noname>";
162 if (sym->st_name) {
163 name = sym_strtab + sym->st_name;
164 }
165 else {
166 name = sec_name(shdr[sym->st_shndx].sh_name);
167 }
168 return name;
169 }
170
171
172
173 #if BYTE_ORDER == LITTLE_ENDIAN
174 #define le16_to_cpu(val) (val)
175 #define le32_to_cpu(val) (val)
176 #endif
177 #if BYTE_ORDER == BIG_ENDIAN
178 #define le16_to_cpu(val) bswap_16(val)
179 #define le32_to_cpu(val) bswap_32(val)
180 #endif
181
182 static uint16_t elf16_to_cpu(uint16_t val)
183 {
184 return le16_to_cpu(val);
185 }
186
187 static uint32_t elf32_to_cpu(uint32_t val)
188 {
189 return le32_to_cpu(val);
190 }
191
192 static void read_ehdr(FILE *fp)
193 {
194 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
195 die("Cannot read ELF header: %s\n",
196 strerror(errno));
197 }
198 if (memcmp(ehdr.e_ident, ELFMAG, 4) != 0) {
199 die("No ELF magic\n");
200 }
201 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
202 die("Not a 32 bit executable\n");
203 }
204 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
205 die("Not a LSB ELF executable\n");
206 }
207 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
208 die("Unknown ELF version\n");
209 }
210 /* Convert the fields to native endian */
211 ehdr.e_type = elf16_to_cpu(ehdr.e_type);
212 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
213 ehdr.e_version = elf32_to_cpu(ehdr.e_version);
214 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
215 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
216 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
217 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
218 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
219 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
220 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
221 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
222 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
223 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
224
225 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
226 die("Unsupported ELF header type\n");
227 }
228 if (ehdr.e_machine != EM_386) {
229 die("Not for x86\n");
230 }
231 if (ehdr.e_version != EV_CURRENT) {
232 die("Unknown ELF version\n");
233 }
234 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
235 die("Bad Elf header size\n");
236 }
237 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
238 die("Bad program header entry\n");
239 }
240 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
241 die("Bad section header entry\n");
242 }
243 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
244 die("String table index out of bounds\n");
245 }
246 }
247
248 static void read_shdrs(FILE *fp)
249 {
250 int i;
251 if (ehdr.e_shnum > MAX_SHDRS) {
252 die("%d section headers supported: %d\n",
253 ehdr.e_shnum, MAX_SHDRS);
254 }
255 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
256 die("Seek to %d failed: %s\n",
257 ehdr.e_shoff, strerror(errno));
258 }
259 if (fread(&shdr, sizeof(shdr[0]), ehdr.e_shnum, fp) != ehdr.e_shnum) {
260 die("Cannot read ELF section headers: %s\n",
261 strerror(errno));
262 }
263 for(i = 0; i < ehdr.e_shnum; i++) {
264 shdr[i].sh_name = elf32_to_cpu(shdr[i].sh_name);
265 shdr[i].sh_type = elf32_to_cpu(shdr[i].sh_type);
266 shdr[i].sh_flags = elf32_to_cpu(shdr[i].sh_flags);
267 shdr[i].sh_addr = elf32_to_cpu(shdr[i].sh_addr);
268 shdr[i].sh_offset = elf32_to_cpu(shdr[i].sh_offset);
269 shdr[i].sh_size = elf32_to_cpu(shdr[i].sh_size);
270 shdr[i].sh_link = elf32_to_cpu(shdr[i].sh_link);
271 shdr[i].sh_info = elf32_to_cpu(shdr[i].sh_info);
272 shdr[i].sh_addralign = elf32_to_cpu(shdr[i].sh_addralign);
273 shdr[i].sh_entsize = elf32_to_cpu(shdr[i].sh_entsize);
274 }
275
276 }
277
278 static void read_strtabs(FILE *fp)
279 {
280 int i;
281 for(i = 0; i < ehdr.e_shnum; i++) {
282 if (shdr[i].sh_type != SHT_STRTAB) {
283 continue;
284 }
285 strtab[i] = malloc(shdr[i].sh_size);
286 if (!strtab[i]) {
287 die("malloc of %d bytes for strtab failed\n",
288 shdr[i].sh_size);
289 }
290 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
291 die("Seek to %d failed: %s\n",
292 shdr[i].sh_offset, strerror(errno));
293 }
294 if (fread(strtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
295 die("Cannot read symbol table: %s\n",
296 strerror(errno));
297 }
298 }
299 }
300
301 static void read_symtabs(FILE *fp)
302 {
303 int i,j;
304 for(i = 0; i < ehdr.e_shnum; i++) {
305 if (shdr[i].sh_type != SHT_SYMTAB) {
306 continue;
307 }
308 symtab[i] = malloc(shdr[i].sh_size);
309 if (!symtab[i]) {
310 die("malloc of %d bytes for symtab failed\n",
311 shdr[i].sh_size);
312 }
313 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
314 die("Seek to %d failed: %s\n",
315 shdr[i].sh_offset, strerror(errno));
316 }
317 if (fread(symtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
318 die("Cannot read symbol table: %s\n",
319 strerror(errno));
320 }
321 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[i][0]); j++) {
322 symtab[i][j].st_name = elf32_to_cpu(symtab[i][j].st_name);
323 symtab[i][j].st_value = elf32_to_cpu(symtab[i][j].st_value);
324 symtab[i][j].st_size = elf32_to_cpu(symtab[i][j].st_size);
325 symtab[i][j].st_shndx = elf16_to_cpu(symtab[i][j].st_shndx);
326 }
327 }
328 }
329
330
331 static void read_relocs(FILE *fp)
332 {
333 int i,j;
334 for(i = 0; i < ehdr.e_shnum; i++) {
335 if (shdr[i].sh_type != SHT_REL) {
336 continue;
337 }
338 reltab[i] = malloc(shdr[i].sh_size);
339 if (!reltab[i]) {
340 die("malloc of %d bytes for relocs failed\n",
341 shdr[i].sh_size);
342 }
343 if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
344 die("Seek to %d failed: %s\n",
345 shdr[i].sh_offset, strerror(errno));
346 }
347 if (fread(reltab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
348 die("Cannot read symbol table: %s\n",
349 strerror(errno));
350 }
351 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
352 reltab[i][j].r_offset = elf32_to_cpu(reltab[i][j].r_offset);
353 reltab[i][j].r_info = elf32_to_cpu(reltab[i][j].r_info);
354 }
355 }
356 }
357
358
359 static void print_absolute_symbols(void)
360 {
361 int i;
362 printf("Absolute symbols\n");
363 printf(" Num: Value Size Type Bind Visibility Name\n");
364 for(i = 0; i < ehdr.e_shnum; i++) {
365 char *sym_strtab;
366 Elf32_Sym *sh_symtab;
367 int j;
368 if (shdr[i].sh_type != SHT_SYMTAB) {
369 continue;
370 }
371 sh_symtab = symtab[i];
372 sym_strtab = strtab[shdr[i].sh_link];
373 for(j = 0; j < shdr[i].sh_size/sizeof(symtab[0][0]); j++) {
374 Elf32_Sym *sym;
375 const char *name;
376 sym = &symtab[i][j];
377 name = sym_name(sym_strtab, sym);
378 if (sym->st_shndx != SHN_ABS) {
379 continue;
380 }
381 printf("%5d %08x %5d %10s %10s %12s %s\n",
382 j, sym->st_value, sym->st_size,
383 sym_type(ELF32_ST_TYPE(sym->st_info)),
384 sym_bind(ELF32_ST_BIND(sym->st_info)),
385 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
386 name);
387 }
388 }
389 printf("\n");
390 }
391
392 static void print_absolute_relocs(void)
393 {
394 int i, printed = 0;
395
396 for(i = 0; i < ehdr.e_shnum; i++) {
397 char *sym_strtab;
398 Elf32_Sym *sh_symtab;
399 unsigned sec_applies, sec_symtab;
400 int j;
401 if (shdr[i].sh_type != SHT_REL) {
402 continue;
403 }
404 sec_symtab = shdr[i].sh_link;
405 sec_applies = shdr[i].sh_info;
406 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
407 continue;
408 }
409 sh_symtab = symtab[sec_symtab];
410 sym_strtab = strtab[shdr[sec_symtab].sh_link];
411 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
412 Elf32_Rel *rel;
413 Elf32_Sym *sym;
414 const char *name;
415 rel = &reltab[i][j];
416 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
417 name = sym_name(sym_strtab, sym);
418 if (sym->st_shndx != SHN_ABS) {
419 continue;
420 }
421
422 /* Absolute symbols are not relocated if bzImage is
423 * loaded at a non-compiled address. Display a warning
424 * to user at compile time about the absolute
425 * relocations present.
426 *
427 * User need to audit the code to make sure
428 * some symbols which should have been section
429 * relative have not become absolute because of some
430 * linker optimization or wrong programming usage.
431 *
432 * Before warning check if this absolute symbol
433 * relocation is harmless.
434 */
435 if (is_safe_abs_reloc(name))
436 continue;
437
438 if (!printed) {
439 printf("WARNING: Absolute relocations"
440 " present\n");
441 printf("Offset Info Type Sym.Value "
442 "Sym.Name\n");
443 printed = 1;
444 }
445
446 printf("%08x %08x %10s %08x %s\n",
447 rel->r_offset,
448 rel->r_info,
449 rel_type(ELF32_R_TYPE(rel->r_info)),
450 sym->st_value,
451 name);
452 }
453 }
454
455 if (printed)
456 printf("\n");
457 }
458
459 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
460 {
461 int i;
462 /* Walk through the relocations */
463 for(i = 0; i < ehdr.e_shnum; i++) {
464 char *sym_strtab;
465 Elf32_Sym *sh_symtab;
466 unsigned sec_applies, sec_symtab;
467 int j;
468 if (shdr[i].sh_type != SHT_REL) {
469 continue;
470 }
471 sec_symtab = shdr[i].sh_link;
472 sec_applies = shdr[i].sh_info;
473 if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
474 continue;
475 }
476 sh_symtab = symtab[sec_symtab];
477 sym_strtab = strtab[shdr[sec_symtab].sh_link];
478 for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
479 Elf32_Rel *rel;
480 Elf32_Sym *sym;
481 unsigned r_type;
482 rel = &reltab[i][j];
483 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
484 r_type = ELF32_R_TYPE(rel->r_info);
485 /* Don't visit relocations to absolute symbols */
486 if (sym->st_shndx == SHN_ABS) {
487 continue;
488 }
489 if (r_type == R_386_PC32) {
490 /* PC relative relocations don't need to be adjusted */
491 }
492 else if (r_type == R_386_32) {
493 /* Visit relocations that need to be adjusted */
494 visit(rel, sym);
495 }
496 else {
497 die("Unsupported relocation type: %d\n", r_type);
498 }
499 }
500 }
501 }
502
503 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
504 {
505 reloc_count += 1;
506 }
507
508 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
509 {
510 /* Remember the address that needs to be adjusted. */
511 relocs[reloc_idx++] = rel->r_offset;
512 }
513
514 static int cmp_relocs(const void *va, const void *vb)
515 {
516 const unsigned long *a, *b;
517 a = va; b = vb;
518 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
519 }
520
521 static void emit_relocs(int as_text)
522 {
523 int i;
524 /* Count how many relocations I have and allocate space for them. */
525 reloc_count = 0;
526 walk_relocs(count_reloc);
527 relocs = malloc(reloc_count * sizeof(relocs[0]));
528 if (!relocs) {
529 die("malloc of %d entries for relocs failed\n",
530 reloc_count);
531 }
532 /* Collect up the relocations */
533 reloc_idx = 0;
534 walk_relocs(collect_reloc);
535
536 /* Order the relocations for more efficient processing */
537 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
538
539 /* Print the relocations */
540 if (as_text) {
541 /* Print the relocations in a form suitable that
542 * gas will like.
543 */
544 printf(".section \".data.reloc\",\"a\"\n");
545 printf(".balign 4\n");
546 for(i = 0; i < reloc_count; i++) {
547 printf("\t .long 0x%08lx\n", relocs[i]);
548 }
549 printf("\n");
550 }
551 else {
552 unsigned char buf[4];
553 buf[0] = buf[1] = buf[2] = buf[3] = 0;
554 /* Print a stop */
555 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
556 /* Now print each relocation */
557 for(i = 0; i < reloc_count; i++) {
558 buf[0] = (relocs[i] >> 0) & 0xff;
559 buf[1] = (relocs[i] >> 8) & 0xff;
560 buf[2] = (relocs[i] >> 16) & 0xff;
561 buf[3] = (relocs[i] >> 24) & 0xff;
562 printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
563 }
564 }
565 }
566
567 static void usage(void)
568 {
569 die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
570 }
571
572 int main(int argc, char **argv)
573 {
574 int show_absolute_syms, show_absolute_relocs;
575 int as_text;
576 const char *fname;
577 FILE *fp;
578 int i;
579
580 show_absolute_syms = 0;
581 show_absolute_relocs = 0;
582 as_text = 0;
583 fname = NULL;
584 for(i = 1; i < argc; i++) {
585 char *arg = argv[i];
586 if (*arg == '-') {
587 if (strcmp(argv[1], "--abs-syms") == 0) {
588 show_absolute_syms = 1;
589 continue;
590 }
591
592 if (strcmp(argv[1], "--abs-relocs") == 0) {
593 show_absolute_relocs = 1;
594 continue;
595 }
596 else if (strcmp(argv[1], "--text") == 0) {
597 as_text = 1;
598 continue;
599 }
600 }
601 else if (!fname) {
602 fname = arg;
603 continue;
604 }
605 usage();
606 }
607 if (!fname) {
608 usage();
609 }
610 fp = fopen(fname, "r");
611 if (!fp) {
612 die("Cannot open %s: %s\n",
613 fname, strerror(errno));
614 }
615 read_ehdr(fp);
616 read_shdrs(fp);
617 read_strtabs(fp);
618 read_symtabs(fp);
619 read_relocs(fp);
620 if (show_absolute_syms) {
621 print_absolute_symbols();
622 return 0;
623 }
624 if (show_absolute_relocs) {
625 print_absolute_relocs();
626 return 0;
627 }
628 emit_relocs(as_text);
629 return 0;
630 }