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