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