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 15 #include "llvm-c-test.h" 16 #include "llvm-c/Core.h" 17 #include "llvm-c/Object.h" 18 #include <stdio.h> 19 #include <stdlib.h> 20 21 int object_list_sections(void) { 22 LLVMMemoryBufferRef MB; 23 LLVMObjectFileRef O; 24 LLVMSectionIteratorRef sect; 25 char *msg = NULL; 26 27 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) { 28 fprintf(stderr, "Error reading file: %s\n", msg); 29 exit(1); 30 } 31 32 O = LLVMCreateObjectFile(MB); 33 if (!O) { 34 fprintf(stderr, "Error reading object\n"); 35 exit(1); 36 } 37 38 sect = LLVMGetSections(O); 39 while (!LLVMIsSectionIteratorAtEnd(O, sect)) { 40 printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect), 41 LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect)); 42 43 LLVMMoveToNextSection(sect); 44 } 45 46 LLVMDisposeSectionIterator(sect); 47 48 LLVMDisposeObjectFile(O); 49 50 return 0; 51 } 52 53 int object_list_symbols(void) { 54 LLVMMemoryBufferRef MB; 55 LLVMObjectFileRef O; 56 LLVMSectionIteratorRef sect; 57 LLVMSymbolIteratorRef sym; 58 char *msg = NULL; 59 60 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) { 61 fprintf(stderr, "Error reading file: %s\n", msg); 62 exit(1); 63 } 64 65 O = LLVMCreateObjectFile(MB); 66 if (!O) { 67 fprintf(stderr, "Error reading object\n"); 68 exit(1); 69 } 70 71 sect = LLVMGetSections(O); 72 sym = LLVMGetSymbols(O); 73 while (!LLVMIsSymbolIteratorAtEnd(O, sym)) { 74 75 LLVMMoveToContainingSection(sect, sym); 76 printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym), 77 LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym), 78 LLVMGetSectionName(sect)); 79 80 LLVMMoveToNextSymbol(sym); 81 } 82 83 LLVMDisposeSymbolIterator(sym); 84 85 LLVMDisposeObjectFile(O); 86 87 return 0; 88 } 89