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