astest.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <bfd.h>
  7. #include "error.h"
  8. int verbose = 0;
  9. void
  10. process_section(bfd *abfd, asection *sect, PTR obj)
  11. {
  12. printf("Section '%s':\n", sect->name);
  13. printf("\tindex=%d, flags=%x\n", sect->index, sect->flags);
  14. #if 0
  15. printf("\tuser_set_vma=%u, reloc_done=%u, linker_mark=%u, gc_mark=%u\n",
  16. (unsigned long)sect->user_set_vma, (unsigned long)sect->reloc_done,
  17. (unsigned long)sect->linker_mark, (unsigned long)sect->gc_mark);
  18. #else
  19. printf("\tuser_set_vma=%u, reloc_done=%u\n",
  20. (unsigned int)sect->user_set_vma, (unsigned int)sect->reloc_done);
  21. #endif
  22. printf("\tvma=%08lx, lma=%08lx\n",
  23. (unsigned long)sect->vma, (unsigned long)sect->lma);
  24. printf("\tcooked_size=%ld, raw_size=%ld, output_offset=%ld\n",
  25. (long)sect->_cooked_size, (long)sect->_raw_size,
  26. (long)sect->output_offset);
  27. printf("\talignment_power=%d, reloc_count=%d, filepos=%ld\n",
  28. sect->alignment_power, sect->reloc_count, sect->filepos);
  29. printf("\trel_filepos=%ld, line_filepos=%ld, lineno_count=%d\n",
  30. sect->rel_filepos, sect->line_filepos, sect->lineno_count);
  31. printf("\tmoving_line_filepos=%ld, target_index=%d\n",
  32. sect->moving_line_filepos, sect->target_index);
  33. }
  34. int
  35. main(int ac, char **av)
  36. {
  37. int c, ifd;
  38. char *ifn;
  39. bfd *bfdp;
  40. if ((pname = strrchr(av[0], '/')) == NULL)
  41. pname = av[0];
  42. else
  43. pname++;
  44. while ((c = getopt(ac, av, "v")) != EOF)
  45. switch (c) {
  46. case 'v':
  47. verbose = 1;
  48. break;
  49. default:
  50. usage:
  51. fprintf(stderr, "Usage: %s [-v] imagefile\n", pname);
  52. exit(1);
  53. }
  54. if (optind != ac - 1)
  55. goto usage;
  56. ifn = av[optind];
  57. if (verbose)
  58. fprintf(stderr, "Opening file...\n");
  59. if ((ifd = open(ifn, O_RDONLY)) < 0)
  60. Perror("can't open image file '%s'", ifn);
  61. if ((bfdp = bfd_fdopenr(ifn, "elf32-powerpc", ifd)) == NULL) {
  62. bfd_perror(ifn);
  63. close(ifd);
  64. Error("bfd_fdopenr of file '%s' failed", ifn);
  65. }
  66. bfdp->cacheable = true;
  67. if (!bfd_check_format(bfdp, bfd_object) ||
  68. (bfd_get_file_flags(bfdp) & EXEC_P) == 0) {
  69. bfd_close(bfdp);
  70. Error("file '%s' is not an executable object file (%s,0x%x)", ifn,
  71. bfd_format_string(bfd_get_format(bfdp)), bfd_get_file_flags(bfdp));
  72. }
  73. printf("file '%s' is type '%s'...\n", ifn, bfd_get_target(bfdp));
  74. bfd_map_over_sections(bfdp, process_section, NULL);;
  75. bfd_close(bfdp);
  76. if (verbose)
  77. fprintf(stderr, "Done.\n");
  78. return (0);
  79. }