relocs_main.c 1.5 KB

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