object.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\
  2. |* *|
  3. |* The LLVM Compiler Infrastructure *|
  4. |* *|
  5. |* This file is distributed under the University of Illinois Open Source *|
  6. |* License. See LICENSE.TXT for details. *|
  7. |* *|
  8. |*===----------------------------------------------------------------------===*|
  9. |* *|
  10. |* This file implements the --object-list-sections and --object-list-symbols *|
  11. |* commands in llvm-c-test. *|
  12. |* *|
  13. \*===----------------------------------------------------------------------===*/
  14. #include "llvm-c-test.h"
  15. #include "llvm-c/Object.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. int object_list_sections(void) {
  19. LLVMMemoryBufferRef MB;
  20. LLVMObjectFileRef O;
  21. LLVMSectionIteratorRef sect;
  22. char *msg = NULL;
  23. if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
  24. fprintf(stderr, "Error reading file: %s\n", msg);
  25. exit(1);
  26. }
  27. O = LLVMCreateObjectFile(MB);
  28. if (!O) {
  29. fprintf(stderr, "Error reading object\n");
  30. exit(1);
  31. }
  32. sect = LLVMGetSections(O);
  33. while (!LLVMIsSectionIteratorAtEnd(O, sect)) {
  34. printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
  35. LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
  36. LLVMMoveToNextSection(sect);
  37. }
  38. LLVMDisposeSectionIterator(sect);
  39. LLVMDisposeObjectFile(O);
  40. return 0;
  41. }
  42. int object_list_symbols(void) {
  43. LLVMMemoryBufferRef MB;
  44. LLVMObjectFileRef O;
  45. LLVMSectionIteratorRef sect;
  46. LLVMSymbolIteratorRef sym;
  47. char *msg = NULL;
  48. if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
  49. fprintf(stderr, "Error reading file: %s\n", msg);
  50. exit(1);
  51. }
  52. O = LLVMCreateObjectFile(MB);
  53. if (!O) {
  54. fprintf(stderr, "Error reading object\n");
  55. exit(1);
  56. }
  57. sect = LLVMGetSections(O);
  58. sym = LLVMGetSymbols(O);
  59. while (!LLVMIsSymbolIteratorAtEnd(O, sym)) {
  60. LLVMMoveToContainingSection(sect, sym);
  61. printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym),
  62. LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym),
  63. LLVMGetSectionName(sect));
  64. LLVMMoveToNextSymbol(sym);
  65. }
  66. LLVMDisposeSymbolIterator(sym);
  67. LLVMDisposeObjectFile(O);
  68. return 0;
  69. }