MIPS: tools: Add relocs tool
[cascardo/linux.git] / arch / mips / boot / tools / relocs_main.c
1
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <stdarg.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <endian.h>
9 #include <elf.h>
10
11 #include "relocs.h"
12
13 void die(char *fmt, ...)
14 {
15         va_list ap;
16
17         va_start(ap, fmt);
18         vfprintf(stderr, fmt, ap);
19         va_end(ap);
20         exit(1);
21 }
22
23 static void usage(void)
24 {
25         die("relocs [--reloc-info|--text|--bin|--keep] vmlinux\n");
26 }
27
28 int main(int argc, char **argv)
29 {
30         int show_reloc_info, as_text, as_bin, keep_relocs;
31         const char *fname;
32         FILE *fp;
33         int i;
34         unsigned char e_ident[EI_NIDENT];
35
36         show_reloc_info = 0;
37         as_text = 0;
38         as_bin = 0;
39         keep_relocs = 0;
40         fname = NULL;
41         for (i = 1; i < argc; i++) {
42                 char *arg = argv[i];
43
44                 if (*arg == '-') {
45                         if (strcmp(arg, "--reloc-info") == 0) {
46                                 show_reloc_info = 1;
47                                 continue;
48                         }
49                         if (strcmp(arg, "--text") == 0) {
50                                 as_text = 1;
51                                 continue;
52                         }
53                         if (strcmp(arg, "--bin") == 0) {
54                                 as_bin = 1;
55                                 continue;
56                         }
57                         if (strcmp(arg, "--keep") == 0) {
58                                 keep_relocs = 1;
59                                 continue;
60                         }
61                 } else if (!fname) {
62                         fname = arg;
63                         continue;
64                 }
65                 usage();
66         }
67         if (!fname)
68                 usage();
69
70         fp = fopen(fname, "r+");
71         if (!fp)
72                 die("Cannot open %s: %s\n", fname, strerror(errno));
73
74         if (fread(&e_ident, 1, EI_NIDENT, fp) != EI_NIDENT)
75                 die("Cannot read %s: %s", fname, strerror(errno));
76
77         rewind(fp);
78         if (e_ident[EI_CLASS] == ELFCLASS64)
79                 process_64(fp, as_text,  as_bin, show_reloc_info, keep_relocs);
80         else
81                 process_32(fp, as_text, as_bin, show_reloc_info, keep_relocs);
82         fclose(fp);
83         return 0;
84 }