Anders Waldenborg | 2bef1a6 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 2e13053 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 20 | int llvm_object_list_sections(void) { |
Anders Waldenborg | 2bef1a6 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 21 | LLVMMemoryBufferRef MB; |
| 22 | LLVMObjectFileRef O; |
NAKAMURA Takumi | 90fd79a | 2013-10-23 17:56:37 +0000 | [diff] [blame] | 23 | LLVMSectionIteratorRef sect; |
Anders Waldenborg | 2bef1a6 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 24 | char *msg = NULL; |
| 25 | |
| 26 | if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) { |
| 27 | fprintf(stderr, "Error reading file: %s\n", msg); |
| 28 | exit(1); |
| 29 | } |
| 30 | |
| 31 | O = LLVMCreateObjectFile(MB); |
| 32 | if (!O) { |
| 33 | fprintf(stderr, "Error reading object\n"); |
| 34 | exit(1); |
| 35 | } |
| 36 | |
NAKAMURA Takumi | 90fd79a | 2013-10-23 17:56:37 +0000 | [diff] [blame] | 37 | sect = LLVMGetSections(O); |
Anders Waldenborg | 2bef1a6 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 38 | while (!LLVMIsSectionIteratorAtEnd(O, sect)) { |
| 39 | printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect), |
| 40 | LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect)); |
| 41 | |
| 42 | LLVMMoveToNextSection(sect); |
| 43 | } |
| 44 | |
| 45 | LLVMDisposeSectionIterator(sect); |
| 46 | |
| 47 | LLVMDisposeObjectFile(O); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Benjamin Kramer | 2e13053 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 52 | int llvm_object_list_symbols(void) { |
Anders Waldenborg | 2bef1a6 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 53 | LLVMMemoryBufferRef MB; |
| 54 | LLVMObjectFileRef O; |
NAKAMURA Takumi | 90fd79a | 2013-10-23 17:56:37 +0000 | [diff] [blame] | 55 | LLVMSectionIteratorRef sect; |
| 56 | LLVMSymbolIteratorRef sym; |
Anders Waldenborg | 2bef1a6 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 57 | char *msg = NULL; |
| 58 | |
| 59 | if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) { |
| 60 | fprintf(stderr, "Error reading file: %s\n", msg); |
| 61 | exit(1); |
| 62 | } |
| 63 | |
| 64 | O = LLVMCreateObjectFile(MB); |
| 65 | if (!O) { |
| 66 | fprintf(stderr, "Error reading object\n"); |
| 67 | exit(1); |
| 68 | } |
| 69 | |
NAKAMURA Takumi | 90fd79a | 2013-10-23 17:56:37 +0000 | [diff] [blame] | 70 | sect = LLVMGetSections(O); |
| 71 | sym = LLVMGetSymbols(O); |
Anders Waldenborg | 2bef1a6 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 72 | while (!LLVMIsSymbolIteratorAtEnd(O, sym)) { |
| 73 | |
| 74 | LLVMMoveToContainingSection(sect, sym); |
Rafael Espindola | 7426771 | 2014-04-21 13:45:32 +0000 | [diff] [blame] | 75 | printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym), |
| 76 | LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym), |
Anders Waldenborg | 2bef1a6 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 77 | LLVMGetSectionName(sect)); |
| 78 | |
| 79 | LLVMMoveToNextSymbol(sym); |
| 80 | } |
| 81 | |
| 82 | LLVMDisposeSymbolIterator(sym); |
| 83 | |
| 84 | LLVMDisposeObjectFile(O); |
| 85 | |
| 86 | return 0; |
| 87 | } |