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