0001
0002
0003 #include <stdio.h>
0004 #include <stdint.h>
0005 #include <stdarg.h>
0006 #include <stdlib.h>
0007 #include <string.h>
0008 #include <errno.h>
0009 #include <endian.h>
0010 #include <elf.h>
0011
0012 #include "relocs.h"
0013
0014 void die(char *fmt, ...)
0015 {
0016 va_list ap;
0017
0018 va_start(ap, fmt);
0019 vfprintf(stderr, fmt, ap);
0020 va_end(ap);
0021 exit(1);
0022 }
0023
0024 static void usage(void)
0025 {
0026 die("relocs [--reloc-info|--text|--bin|--keep] vmlinux\n");
0027 }
0028
0029 int main(int argc, char **argv)
0030 {
0031 int show_reloc_info, as_text, as_bin, keep_relocs;
0032 const char *fname;
0033 FILE *fp;
0034 int i;
0035 unsigned char e_ident[EI_NIDENT];
0036
0037 show_reloc_info = 0;
0038 as_text = 0;
0039 as_bin = 0;
0040 keep_relocs = 0;
0041 fname = NULL;
0042 for (i = 1; i < argc; i++) {
0043 char *arg = argv[i];
0044
0045 if (*arg == '-') {
0046 if (strcmp(arg, "--reloc-info") == 0) {
0047 show_reloc_info = 1;
0048 continue;
0049 }
0050 if (strcmp(arg, "--text") == 0) {
0051 as_text = 1;
0052 continue;
0053 }
0054 if (strcmp(arg, "--bin") == 0) {
0055 as_bin = 1;
0056 continue;
0057 }
0058 if (strcmp(arg, "--keep") == 0) {
0059 keep_relocs = 1;
0060 continue;
0061 }
0062 } else if (!fname) {
0063 fname = arg;
0064 continue;
0065 }
0066 usage();
0067 }
0068 if (!fname)
0069 usage();
0070
0071 fp = fopen(fname, "r+");
0072 if (!fp)
0073 die("Cannot open %s: %s\n", fname, strerror(errno));
0074
0075 if (fread(&e_ident, 1, EI_NIDENT, fp) != EI_NIDENT)
0076 die("Cannot read %s: %s", fname, strerror(errno));
0077
0078 rewind(fp);
0079 if (e_ident[EI_CLASS] == ELFCLASS64)
0080 process_64(fp, as_text, as_bin, show_reloc_info, keep_relocs);
0081 else
0082 process_32(fp, as_text, as_bin, show_reloc_info, keep_relocs);
0083 fclose(fp);
0084 return 0;
0085 }