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