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