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