bdc5930b3a1b4a261c44ca522aeee3191763678e
[pandora-kernel.git] / arch / x86 / tools / relocs.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <inttypes.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <unistd.h>
9 #include <elf.h>
10 #include <byteswap.h>
11 #define USE_BSD
12 #include <endian.h>
13 #include <regex.h>
14 #include <tools/le_byteshift.h>
15
16 #define ElfW(type)              _ElfW(ELF_BITS, type)
17 #define _ElfW(bits, type)       __ElfW(bits, type)
18 #define __ElfW(bits, type)      Elf##bits##_##type
19
20 #define ELF_BITS                32
21 #define ELF_MACHINE             EM_386
22 #define ELF_MACHINE_NAME        "i386"
23 #define SHT_REL_TYPE            SHT_REL
24
25 #define ELF_CLASS               ELFCLASS32
26 #define ELF_R_SYM(val)          ELF32_R_SYM(val)
27 #define ELF_R_TYPE(val)         ELF32_R_TYPE(val)
28 #define ELF_ST_TYPE(o)          ELF32_ST_TYPE(o)
29 #define ELF_ST_BIND(o)          ELF32_ST_BIND(o)
30 #define ELF_ST_VISIBILITY(o)    ELF32_ST_VISIBILITY(o)
31
32 #define Elf_Rel                 ElfW(Rel)
33 #define Elf_Ehdr                ElfW(Ehdr)
34 #define Elf_Phdr                ElfW(Phdr)
35 #define Elf_Shdr                ElfW(Shdr)
36 #define Elf_Sym                 ElfW(Sym)
37
38 static void die(char *fmt, ...);
39
40 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
41 static Elf_Ehdr ehdr;
42
43 struct relocs {
44         uint32_t        *offset;
45         unsigned long   count;
46         unsigned long   size;
47 };
48
49 static struct relocs relocs16;
50 static struct relocs relocs32;
51
52 struct section {
53         Elf_Shdr       shdr;
54         struct section *link;
55         Elf_Sym        *symtab;
56         Elf_Rel        *reltab;
57         char           *strtab;
58 };
59 static struct section *secs;
60
61 enum symtype {
62         S_ABS,
63         S_REL,
64         S_SEG,
65         S_LIN,
66         S_NSYMTYPES
67 };
68
69 static const char * const sym_regex_kernel[S_NSYMTYPES] = {
70 /*
71  * Following symbols have been audited. There values are constant and do
72  * not change if bzImage is loaded at a different physical address than
73  * the address for which it has been compiled. Don't warn user about
74  * absolute relocations present w.r.t these symbols.
75  */
76         [S_ABS] =
77         "^(xen_irq_disable_direct_reloc$|"
78         "xen_save_fl_direct_reloc$|"
79         "VDSO|"
80         "__crc_)",
81
82 /*
83  * These symbols are known to be relative, even if the linker marks them
84  * as absolute (typically defined outside any section in the linker script.)
85  */
86         [S_REL] =
87         "^(__init_(begin|end)|"
88         "__x86_cpu_dev_(start|end)|"
89         "(__parainstructions|__alt_instructions)(|_end)|"
90         "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
91         "__(start|end)_pci_.*|"
92         "__(start|end)_builtin_fw|"
93         "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
94         "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
95         "__(start|stop)___param|"
96         "__(start|stop)___modver|"
97         "__(start|stop)___bug_table|"
98         "__tracedata_(start|end)|"
99         "__(start|stop)_notes|"
100         "__end_rodata|"
101         "__initramfs_start|"
102         "(jiffies|jiffies_64)|"
103         "_end)$"
104 };
105
106
107 static const char * const sym_regex_realmode[S_NSYMTYPES] = {
108 /*
109  * These symbols are known to be relative, even if the linker marks them
110  * as absolute (typically defined outside any section in the linker script.)
111  */
112         [S_REL] =
113         "^pa_",
114
115 /*
116  * These are 16-bit segment symbols when compiling 16-bit code.
117  */
118         [S_SEG] =
119         "^real_mode_seg$",
120
121 /*
122  * These are offsets belonging to segments, as opposed to linear addresses,
123  * when compiling 16-bit code.
124  */
125         [S_LIN] =
126         "^pa_",
127 };
128
129 static const char * const *sym_regex;
130
131 static regex_t sym_regex_c[S_NSYMTYPES];
132 static int is_reloc(enum symtype type, const char *sym_name)
133 {
134         return sym_regex[type] &&
135                 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
136 }
137
138 static void regex_init(int use_real_mode)
139 {
140         char errbuf[128];
141         int err;
142         int i;
143
144         if (use_real_mode)
145                 sym_regex = sym_regex_realmode;
146         else
147                 sym_regex = sym_regex_kernel;
148
149         for (i = 0; i < S_NSYMTYPES; i++) {
150                 if (!sym_regex[i])
151                         continue;
152
153                 err = regcomp(&sym_regex_c[i], sym_regex[i],
154                               REG_EXTENDED|REG_NOSUB);
155
156                 if (err) {
157                         regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf);
158                         die("%s", errbuf);
159                 }
160         }
161 }
162
163 static void die(char *fmt, ...)
164 {
165         va_list ap;
166         va_start(ap, fmt);
167         vfprintf(stderr, fmt, ap);
168         va_end(ap);
169         exit(1);
170 }
171
172 static const char *sym_type(unsigned type)
173 {
174         static const char *type_name[] = {
175 #define SYM_TYPE(X) [X] = #X
176                 SYM_TYPE(STT_NOTYPE),
177                 SYM_TYPE(STT_OBJECT),
178                 SYM_TYPE(STT_FUNC),
179                 SYM_TYPE(STT_SECTION),
180                 SYM_TYPE(STT_FILE),
181                 SYM_TYPE(STT_COMMON),
182                 SYM_TYPE(STT_TLS),
183 #undef SYM_TYPE
184         };
185         const char *name = "unknown sym type name";
186         if (type < ARRAY_SIZE(type_name)) {
187                 name = type_name[type];
188         }
189         return name;
190 }
191
192 static const char *sym_bind(unsigned bind)
193 {
194         static const char *bind_name[] = {
195 #define SYM_BIND(X) [X] = #X
196                 SYM_BIND(STB_LOCAL),
197                 SYM_BIND(STB_GLOBAL),
198                 SYM_BIND(STB_WEAK),
199 #undef SYM_BIND
200         };
201         const char *name = "unknown sym bind name";
202         if (bind < ARRAY_SIZE(bind_name)) {
203                 name = bind_name[bind];
204         }
205         return name;
206 }
207
208 static const char *sym_visibility(unsigned visibility)
209 {
210         static const char *visibility_name[] = {
211 #define SYM_VISIBILITY(X) [X] = #X
212                 SYM_VISIBILITY(STV_DEFAULT),
213                 SYM_VISIBILITY(STV_INTERNAL),
214                 SYM_VISIBILITY(STV_HIDDEN),
215                 SYM_VISIBILITY(STV_PROTECTED),
216 #undef SYM_VISIBILITY
217         };
218         const char *name = "unknown sym visibility name";
219         if (visibility < ARRAY_SIZE(visibility_name)) {
220                 name = visibility_name[visibility];
221         }
222         return name;
223 }
224
225 static const char *rel_type(unsigned type)
226 {
227         static const char *type_name[] = {
228 #define REL_TYPE(X) [X] = #X
229                 REL_TYPE(R_386_NONE),
230                 REL_TYPE(R_386_32),
231                 REL_TYPE(R_386_PC32),
232                 REL_TYPE(R_386_GOT32),
233                 REL_TYPE(R_386_PLT32),
234                 REL_TYPE(R_386_COPY),
235                 REL_TYPE(R_386_GLOB_DAT),
236                 REL_TYPE(R_386_JMP_SLOT),
237                 REL_TYPE(R_386_RELATIVE),
238                 REL_TYPE(R_386_GOTOFF),
239                 REL_TYPE(R_386_GOTPC),
240                 REL_TYPE(R_386_8),
241                 REL_TYPE(R_386_PC8),
242                 REL_TYPE(R_386_16),
243                 REL_TYPE(R_386_PC16),
244 #undef REL_TYPE
245         };
246         const char *name = "unknown type rel type name";
247         if (type < ARRAY_SIZE(type_name) && type_name[type]) {
248                 name = type_name[type];
249         }
250         return name;
251 }
252
253 static const char *sec_name(unsigned shndx)
254 {
255         const char *sec_strtab;
256         const char *name;
257         sec_strtab = secs[ehdr.e_shstrndx].strtab;
258         name = "<noname>";
259         if (shndx < ehdr.e_shnum) {
260                 name = sec_strtab + secs[shndx].shdr.sh_name;
261         }
262         else if (shndx == SHN_ABS) {
263                 name = "ABSOLUTE";
264         }
265         else if (shndx == SHN_COMMON) {
266                 name = "COMMON";
267         }
268         return name;
269 }
270
271 static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
272 {
273         const char *name;
274         name = "<noname>";
275         if (sym->st_name) {
276                 name = sym_strtab + sym->st_name;
277         }
278         else {
279                 name = sec_name(sym->st_shndx);
280         }
281         return name;
282 }
283
284
285
286 #if BYTE_ORDER == LITTLE_ENDIAN
287 #define le16_to_cpu(val) (val)
288 #define le32_to_cpu(val) (val)
289 #endif
290 #if BYTE_ORDER == BIG_ENDIAN
291 #define le16_to_cpu(val) bswap_16(val)
292 #define le32_to_cpu(val) bswap_32(val)
293 #endif
294
295 static uint16_t elf16_to_cpu(uint16_t val)
296 {
297         return le16_to_cpu(val);
298 }
299
300 static uint32_t elf32_to_cpu(uint32_t val)
301 {
302         return le32_to_cpu(val);
303 }
304
305 #define elf_half_to_cpu(x)      elf16_to_cpu(x)
306 #define elf_word_to_cpu(x)      elf32_to_cpu(x)
307 #define elf_addr_to_cpu(x)      elf32_to_cpu(x)
308 #define elf_off_to_cpu(x)       elf32_to_cpu(x)
309 #define elf_xword_to_cpu(x)     elf32_to_cpu(x)
310
311 static void read_ehdr(FILE *fp)
312 {
313         if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
314                 die("Cannot read ELF header: %s\n",
315                         strerror(errno));
316         }
317         if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
318                 die("No ELF magic\n");
319         }
320         if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) {
321                 die("Not a %d bit executable\n", ELF_BITS);
322         }
323         if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
324                 die("Not a LSB ELF executable\n");
325         }
326         if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
327                 die("Unknown ELF version\n");
328         }
329         /* Convert the fields to native endian */
330         ehdr.e_type      = elf_half_to_cpu(ehdr.e_type);
331         ehdr.e_machine   = elf_half_to_cpu(ehdr.e_machine);
332         ehdr.e_version   = elf_word_to_cpu(ehdr.e_version);
333         ehdr.e_entry     = elf_addr_to_cpu(ehdr.e_entry);
334         ehdr.e_phoff     = elf_off_to_cpu(ehdr.e_phoff);
335         ehdr.e_shoff     = elf_off_to_cpu(ehdr.e_shoff);
336         ehdr.e_flags     = elf_word_to_cpu(ehdr.e_flags);
337         ehdr.e_ehsize    = elf_half_to_cpu(ehdr.e_ehsize);
338         ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
339         ehdr.e_phnum     = elf_half_to_cpu(ehdr.e_phnum);
340         ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
341         ehdr.e_shnum     = elf_half_to_cpu(ehdr.e_shnum);
342         ehdr.e_shstrndx  = elf_half_to_cpu(ehdr.e_shstrndx);
343
344         if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
345                 die("Unsupported ELF header type\n");
346         }
347         if (ehdr.e_machine != ELF_MACHINE) {
348                 die("Not for %s\n", ELF_MACHINE_NAME);
349         }
350         if (ehdr.e_version != EV_CURRENT) {
351                 die("Unknown ELF version\n");
352         }
353         if (ehdr.e_ehsize != sizeof(Elf_Ehdr)) {
354                 die("Bad Elf header size\n");
355         }
356         if (ehdr.e_phentsize != sizeof(Elf_Phdr)) {
357                 die("Bad program header entry\n");
358         }
359         if (ehdr.e_shentsize != sizeof(Elf_Shdr)) {
360                 die("Bad section header entry\n");
361         }
362         if (ehdr.e_shstrndx >= ehdr.e_shnum) {
363                 die("String table index out of bounds\n");
364         }
365 }
366
367 static void read_shdrs(FILE *fp)
368 {
369         int i;
370         Elf_Shdr shdr;
371
372         secs = calloc(ehdr.e_shnum, sizeof(struct section));
373         if (!secs) {
374                 die("Unable to allocate %d section headers\n",
375                     ehdr.e_shnum);
376         }
377         if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
378                 die("Seek to %d failed: %s\n",
379                         ehdr.e_shoff, strerror(errno));
380         }
381         for (i = 0; i < ehdr.e_shnum; i++) {
382                 struct section *sec = &secs[i];
383                 if (fread(&shdr, sizeof shdr, 1, fp) != 1)
384                         die("Cannot read ELF section headers %d/%d: %s\n",
385                             i, ehdr.e_shnum, strerror(errno));
386                 sec->shdr.sh_name      = elf_word_to_cpu(shdr.sh_name);
387                 sec->shdr.sh_type      = elf_word_to_cpu(shdr.sh_type);
388                 sec->shdr.sh_flags     = elf_xword_to_cpu(shdr.sh_flags);
389                 sec->shdr.sh_addr      = elf_addr_to_cpu(shdr.sh_addr);
390                 sec->shdr.sh_offset    = elf_off_to_cpu(shdr.sh_offset);
391                 sec->shdr.sh_size      = elf_xword_to_cpu(shdr.sh_size);
392                 sec->shdr.sh_link      = elf_word_to_cpu(shdr.sh_link);
393                 sec->shdr.sh_info      = elf_word_to_cpu(shdr.sh_info);
394                 sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
395                 sec->shdr.sh_entsize   = elf_xword_to_cpu(shdr.sh_entsize);
396                 if (sec->shdr.sh_link < ehdr.e_shnum)
397                         sec->link = &secs[sec->shdr.sh_link];
398         }
399
400 }
401
402 static void read_strtabs(FILE *fp)
403 {
404         int i;
405         for (i = 0; i < ehdr.e_shnum; i++) {
406                 struct section *sec = &secs[i];
407                 if (sec->shdr.sh_type != SHT_STRTAB) {
408                         continue;
409                 }
410                 sec->strtab = malloc(sec->shdr.sh_size);
411                 if (!sec->strtab) {
412                         die("malloc of %d bytes for strtab failed\n",
413                                 sec->shdr.sh_size);
414                 }
415                 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
416                         die("Seek to %d failed: %s\n",
417                                 sec->shdr.sh_offset, strerror(errno));
418                 }
419                 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
420                     != sec->shdr.sh_size) {
421                         die("Cannot read symbol table: %s\n",
422                                 strerror(errno));
423                 }
424         }
425 }
426
427 static void read_symtabs(FILE *fp)
428 {
429         int i,j;
430         for (i = 0; i < ehdr.e_shnum; i++) {
431                 struct section *sec = &secs[i];
432                 if (sec->shdr.sh_type != SHT_SYMTAB) {
433                         continue;
434                 }
435                 sec->symtab = malloc(sec->shdr.sh_size);
436                 if (!sec->symtab) {
437                         die("malloc of %d bytes for symtab failed\n",
438                                 sec->shdr.sh_size);
439                 }
440                 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
441                         die("Seek to %d failed: %s\n",
442                                 sec->shdr.sh_offset, strerror(errno));
443                 }
444                 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
445                     != sec->shdr.sh_size) {
446                         die("Cannot read symbol table: %s\n",
447                                 strerror(errno));
448                 }
449                 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
450                         Elf_Sym *sym = &sec->symtab[j];
451                         sym->st_name  = elf_word_to_cpu(sym->st_name);
452                         sym->st_value = elf_addr_to_cpu(sym->st_value);
453                         sym->st_size  = elf_xword_to_cpu(sym->st_size);
454                         sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
455                 }
456         }
457 }
458
459
460 static void read_relocs(FILE *fp)
461 {
462         int i,j;
463         for (i = 0; i < ehdr.e_shnum; i++) {
464                 struct section *sec = &secs[i];
465                 if (sec->shdr.sh_type != SHT_REL_TYPE) {
466                         continue;
467                 }
468                 sec->reltab = malloc(sec->shdr.sh_size);
469                 if (!sec->reltab) {
470                         die("malloc of %d bytes for relocs failed\n",
471                                 sec->shdr.sh_size);
472                 }
473                 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
474                         die("Seek to %d failed: %s\n",
475                                 sec->shdr.sh_offset, strerror(errno));
476                 }
477                 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
478                     != sec->shdr.sh_size) {
479                         die("Cannot read symbol table: %s\n",
480                                 strerror(errno));
481                 }
482                 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
483                         Elf_Rel *rel = &sec->reltab[j];
484                         rel->r_offset = elf_addr_to_cpu(rel->r_offset);
485                         rel->r_info   = elf_xword_to_cpu(rel->r_info);
486                 }
487         }
488 }
489
490
491 static void print_absolute_symbols(void)
492 {
493         int i;
494         printf("Absolute symbols\n");
495         printf(" Num:    Value Size  Type       Bind        Visibility  Name\n");
496         for (i = 0; i < ehdr.e_shnum; i++) {
497                 struct section *sec = &secs[i];
498                 char *sym_strtab;
499                 int j;
500
501                 if (sec->shdr.sh_type != SHT_SYMTAB) {
502                         continue;
503                 }
504                 sym_strtab = sec->link->strtab;
505                 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
506                         Elf_Sym *sym;
507                         const char *name;
508                         sym = &sec->symtab[j];
509                         name = sym_name(sym_strtab, sym);
510                         if (sym->st_shndx != SHN_ABS) {
511                                 continue;
512                         }
513                         printf("%5d %08x %5d %10s %10s %12s %s\n",
514                                 j, sym->st_value, sym->st_size,
515                                 sym_type(ELF_ST_TYPE(sym->st_info)),
516                                 sym_bind(ELF_ST_BIND(sym->st_info)),
517                                 sym_visibility(ELF_ST_VISIBILITY(sym->st_other)),
518                                 name);
519                 }
520         }
521         printf("\n");
522 }
523
524 static void print_absolute_relocs(void)
525 {
526         int i, printed = 0;
527
528         for (i = 0; i < ehdr.e_shnum; i++) {
529                 struct section *sec = &secs[i];
530                 struct section *sec_applies, *sec_symtab;
531                 char *sym_strtab;
532                 Elf_Sym *sh_symtab;
533                 int j;
534                 if (sec->shdr.sh_type != SHT_REL_TYPE) {
535                         continue;
536                 }
537                 sec_symtab  = sec->link;
538                 sec_applies = &secs[sec->shdr.sh_info];
539                 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
540                         continue;
541                 }
542                 sh_symtab  = sec_symtab->symtab;
543                 sym_strtab = sec_symtab->link->strtab;
544                 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
545                         Elf_Rel *rel;
546                         Elf_Sym *sym;
547                         const char *name;
548                         rel = &sec->reltab[j];
549                         sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
550                         name = sym_name(sym_strtab, sym);
551                         if (sym->st_shndx != SHN_ABS) {
552                                 continue;
553                         }
554
555                         /* Absolute symbols are not relocated if bzImage is
556                          * loaded at a non-compiled address. Display a warning
557                          * to user at compile time about the absolute
558                          * relocations present.
559                          *
560                          * User need to audit the code to make sure
561                          * some symbols which should have been section
562                          * relative have not become absolute because of some
563                          * linker optimization or wrong programming usage.
564                          *
565                          * Before warning check if this absolute symbol
566                          * relocation is harmless.
567                          */
568                         if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
569                                 continue;
570
571                         if (!printed) {
572                                 printf("WARNING: Absolute relocations"
573                                         " present\n");
574                                 printf("Offset     Info     Type     Sym.Value "
575                                         "Sym.Name\n");
576                                 printed = 1;
577                         }
578
579                         printf("%08x %08x %10s %08x  %s\n",
580                                 rel->r_offset,
581                                 rel->r_info,
582                                 rel_type(ELF_R_TYPE(rel->r_info)),
583                                 sym->st_value,
584                                 name);
585                 }
586         }
587
588         if (printed)
589                 printf("\n");
590 }
591
592 static void add_reloc(struct relocs *r, uint32_t offset)
593 {
594         if (r->count == r->size) {
595                 unsigned long newsize = r->size + 50000;
596                 void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
597
598                 if (!mem)
599                         die("realloc of %ld entries for relocs failed\n",
600                                 newsize);
601                 r->offset = mem;
602                 r->size = newsize;
603         }
604         r->offset[r->count++] = offset;
605 }
606
607 static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
608                         Elf_Sym *sym, const char *symname))
609 {
610         int i;
611         /* Walk through the relocations */
612         for (i = 0; i < ehdr.e_shnum; i++) {
613                 char *sym_strtab;
614                 Elf_Sym *sh_symtab;
615                 struct section *sec_applies, *sec_symtab;
616                 int j;
617                 struct section *sec = &secs[i];
618
619                 if (sec->shdr.sh_type != SHT_REL_TYPE) {
620                         continue;
621                 }
622                 sec_symtab  = sec->link;
623                 sec_applies = &secs[sec->shdr.sh_info];
624                 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
625                         continue;
626                 }
627                 sh_symtab = sec_symtab->symtab;
628                 sym_strtab = sec_symtab->link->strtab;
629                 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
630                         Elf_Rel *rel = &sec->reltab[j];
631                         Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
632                         const char *symname = sym_name(sym_strtab, sym);
633
634                         process(sec, rel, sym, symname);
635                 }
636         }
637 }
638
639 static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
640                     const char *symname)
641 {
642         unsigned r_type = ELF32_R_TYPE(rel->r_info);
643         int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
644
645         switch (r_type) {
646         case R_386_NONE:
647         case R_386_PC32:
648         case R_386_PC16:
649         case R_386_PC8:
650                 /*
651                  * NONE can be ignored and PC relative relocations don't
652                  * need to be adjusted.
653                  */
654                 break;
655
656         case R_386_32:
657                 if (shn_abs) {
658                         /*
659                          * Whitelisted absolute symbols do not require
660                          * relocation.
661                          */
662                         if (is_reloc(S_ABS, symname))
663                                 break;
664
665                         die("Invalid absolute %s relocation: %s\n",
666                             rel_type(r_type), symname);
667                         break;
668                 }
669
670                 add_reloc(&relocs32, rel->r_offset);
671                 break;
672
673         default:
674                 die("Unsupported relocation type: %s (%d)\n",
675                     rel_type(r_type), r_type);
676                 break;
677         }
678
679         return 0;
680 }
681
682 static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
683                          const char *symname)
684 {
685         unsigned r_type = ELF32_R_TYPE(rel->r_info);
686         int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
687
688         switch (r_type) {
689         case R_386_NONE:
690         case R_386_PC32:
691         case R_386_PC16:
692         case R_386_PC8:
693                 /*
694                  * NONE can be ignored and PC relative relocations don't
695                  * need to be adjusted.
696                  */
697                 break;
698
699         case R_386_16:
700                 if (shn_abs) {
701                         /*
702                          * Whitelisted absolute symbols do not require
703                          * relocation.
704                          */
705                         if (is_reloc(S_ABS, symname))
706                                 break;
707
708                         if (is_reloc(S_SEG, symname)) {
709                                 add_reloc(&relocs16, rel->r_offset);
710                                 break;
711                         }
712                 } else {
713                         if (!is_reloc(S_LIN, symname))
714                                 break;
715                 }
716                 die("Invalid %s %s relocation: %s\n",
717                     shn_abs ? "absolute" : "relative",
718                     rel_type(r_type), symname);
719                 break;
720
721         case R_386_32:
722                 if (shn_abs) {
723                         /*
724                          * Whitelisted absolute symbols do not require
725                          * relocation.
726                          */
727                         if (is_reloc(S_ABS, symname))
728                                 break;
729
730                         if (is_reloc(S_REL, symname)) {
731                                 add_reloc(&relocs32, rel->r_offset);
732                                 break;
733                         }
734                 } else {
735                         if (is_reloc(S_LIN, symname))
736                                 add_reloc(&relocs32, rel->r_offset);
737                         break;
738                 }
739                 die("Invalid %s %s relocation: %s\n",
740                     shn_abs ? "absolute" : "relative",
741                     rel_type(r_type), symname);
742                 break;
743
744         default:
745                 die("Unsupported relocation type: %s (%d)\n",
746                     rel_type(r_type), r_type);
747                 break;
748         }
749
750         return 0;
751 }
752
753 static int cmp_relocs(const void *va, const void *vb)
754 {
755         const uint32_t *a, *b;
756         a = va; b = vb;
757         return (*a == *b)? 0 : (*a > *b)? 1 : -1;
758 }
759
760 static void sort_relocs(struct relocs *r)
761 {
762         qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
763 }
764
765 static int write32(uint32_t v, FILE *f)
766 {
767         unsigned char buf[4];
768
769         put_unaligned_le32(v, buf);
770         return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
771 }
772
773 static int write32_as_text(uint32_t v, FILE *f)
774 {
775         return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
776 }
777
778 static void emit_relocs(int as_text, int use_real_mode)
779 {
780         int i;
781         int (*write_reloc)(uint32_t, FILE *) = write32;
782
783         /* Collect up the relocations */
784         walk_relocs(use_real_mode ? do_reloc_real : do_reloc);
785
786         if (relocs16.count && !use_real_mode)
787                 die("Segment relocations found but --realmode not specified\n");
788
789         /* Order the relocations for more efficient processing */
790         sort_relocs(&relocs16);
791         sort_relocs(&relocs32);
792
793         /* Print the relocations */
794         if (as_text) {
795                 /* Print the relocations in a form suitable that
796                  * gas will like.
797                  */
798                 printf(".section \".data.reloc\",\"a\"\n");
799                 printf(".balign 4\n");
800                 write_reloc = write32_as_text;
801         }
802
803         if (use_real_mode) {
804                 write_reloc(relocs16.count, stdout);
805                 for (i = 0; i < relocs16.count; i++)
806                         write_reloc(relocs16.offset[i], stdout);
807
808                 write_reloc(relocs32.count, stdout);
809                 for (i = 0; i < relocs32.count; i++)
810                         write_reloc(relocs32.offset[i], stdout);
811         } else {
812                 /* Print a stop */
813                 write_reloc(0, stdout);
814
815                 /* Now print each relocation */
816                 for (i = 0; i < relocs32.count; i++)
817                         write_reloc(relocs32.offset[i], stdout);
818         }
819 }
820
821 static void usage(void)
822 {
823         die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n");
824 }
825
826 int main(int argc, char **argv)
827 {
828         int show_absolute_syms, show_absolute_relocs;
829         int as_text, use_real_mode;
830         const char *fname;
831         FILE *fp;
832         int i;
833
834         show_absolute_syms = 0;
835         show_absolute_relocs = 0;
836         as_text = 0;
837         use_real_mode = 0;
838         fname = NULL;
839         for (i = 1; i < argc; i++) {
840                 char *arg = argv[i];
841                 if (*arg == '-') {
842                         if (strcmp(arg, "--abs-syms") == 0) {
843                                 show_absolute_syms = 1;
844                                 continue;
845                         }
846                         if (strcmp(arg, "--abs-relocs") == 0) {
847                                 show_absolute_relocs = 1;
848                                 continue;
849                         }
850                         if (strcmp(arg, "--text") == 0) {
851                                 as_text = 1;
852                                 continue;
853                         }
854                         if (strcmp(arg, "--realmode") == 0) {
855                                 use_real_mode = 1;
856                                 continue;
857                         }
858                 }
859                 else if (!fname) {
860                         fname = arg;
861                         continue;
862                 }
863                 usage();
864         }
865         if (!fname) {
866                 usage();
867         }
868         regex_init(use_real_mode);
869         fp = fopen(fname, "r");
870         if (!fp) {
871                 die("Cannot open %s: %s\n",
872                         fname, strerror(errno));
873         }
874         read_ehdr(fp);
875         read_shdrs(fp);
876         read_strtabs(fp);
877         read_symtabs(fp);
878         read_relocs(fp);
879         if (show_absolute_syms) {
880                 print_absolute_symbols();
881                 goto out;
882         }
883         if (show_absolute_relocs) {
884                 print_absolute_relocs();
885                 goto out;
886         }
887         emit_relocs(as_text, use_real_mode);
888 out:
889         fclose(fp);
890         return 0;
891 }