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