1 | /*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\ |
2 | |* *| |
3 | |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| |
4 | |* Exceptions. *| |
5 | |* See https://llvm.org/LICENSE.txt for license information. *| |
6 | |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| |
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 | |
20 | int llvm_object_list_sections(void) { |
21 | LLVMMemoryBufferRef MB; |
22 | LLVMBinaryRef O; |
23 | LLVMSectionIteratorRef sect; |
24 | |
25 | char *outBufferErr = NULL; |
26 | if (LLVMCreateMemoryBufferWithSTDIN(OutMemBuf: &MB, OutMessage: &outBufferErr)) { |
27 | fprintf(stderr, format: "Error reading file: %s\n" , outBufferErr); |
28 | free(ptr: outBufferErr); |
29 | exit(status: 1); |
30 | } |
31 | |
32 | char *outBinaryErr = NULL; |
33 | O = LLVMCreateBinary(MemBuf: MB, Context: LLVMGetGlobalContext(), ErrorMessage: &outBinaryErr); |
34 | if (!O || outBinaryErr) { |
35 | fprintf(stderr, format: "Error reading object: %s\n" , outBinaryErr); |
36 | free(ptr: outBinaryErr); |
37 | exit(status: 1); |
38 | } |
39 | |
40 | sect = LLVMObjectFileCopySectionIterator(BR: O); |
41 | while (sect && !LLVMObjectFileIsSectionIteratorAtEnd(BR: O, SI: sect)) { |
42 | printf(format: "'%s': @0x%08" PRIx64 " +%" PRIu64 "\n" , LLVMGetSectionName(SI: sect), |
43 | LLVMGetSectionAddress(SI: sect), LLVMGetSectionSize(SI: sect)); |
44 | |
45 | LLVMMoveToNextSection(SI: sect); |
46 | } |
47 | |
48 | LLVMDisposeSectionIterator(SI: sect); |
49 | |
50 | LLVMDisposeBinary(BR: O); |
51 | |
52 | LLVMDisposeMemoryBuffer(MemBuf: MB); |
53 | |
54 | return 0; |
55 | } |
56 | |
57 | int llvm_object_list_symbols(void) { |
58 | LLVMMemoryBufferRef MB; |
59 | LLVMBinaryRef O; |
60 | LLVMSectionIteratorRef sect; |
61 | LLVMSymbolIteratorRef sym; |
62 | |
63 | char *outBufferErr = NULL; |
64 | if (LLVMCreateMemoryBufferWithSTDIN(OutMemBuf: &MB, OutMessage: &outBufferErr)) { |
65 | fprintf(stderr, format: "Error reading file: %s\n" , outBufferErr); |
66 | free(ptr: outBufferErr); |
67 | exit(status: 1); |
68 | } |
69 | |
70 | char *outBinaryErr = NULL; |
71 | O = LLVMCreateBinary(MemBuf: MB, Context: LLVMGetGlobalContext(), ErrorMessage: &outBinaryErr); |
72 | if (!O || outBinaryErr) { |
73 | fprintf(stderr, format: "Error reading object: %s\n" , outBinaryErr); |
74 | free(ptr: outBinaryErr); |
75 | exit(status: 1); |
76 | } |
77 | |
78 | sect = LLVMObjectFileCopySectionIterator(BR: O); |
79 | sym = LLVMObjectFileCopySymbolIterator(BR: O); |
80 | while (sect && sym && !LLVMObjectFileIsSymbolIteratorAtEnd(BR: O, SI: sym)) { |
81 | |
82 | LLVMMoveToContainingSection(Sect: sect, Sym: sym); |
83 | printf(format: "%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n" , LLVMGetSymbolName(SI: sym), |
84 | LLVMGetSymbolAddress(SI: sym), LLVMGetSymbolSize(SI: sym), |
85 | LLVMGetSectionName(SI: sect)); |
86 | |
87 | LLVMMoveToNextSymbol(SI: sym); |
88 | } |
89 | |
90 | LLVMDisposeSymbolIterator(SI: sym); |
91 | |
92 | LLVMDisposeBinary(BR: O); |
93 | |
94 | LLVMDisposeMemoryBuffer(MemBuf: MB); |
95 | |
96 | return 0; |
97 | } |
98 | |