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