relocs_main.c 1.5 KB

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