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
20int llvm_object_list_sections(void) {
21 LLVMMemoryBufferRef MB;
22 LLVMSectionIteratorRef sect;
23
24 char *outBufferErr = NULL;
25 if (LLVMCreateMemoryBufferWithSTDIN(OutMemBuf: &MB, OutMessage: &outBufferErr)) {
26 fprintf(stderr, format: "Error reading file: %s\n", outBufferErr);
27 free(ptr: outBufferErr);
28 exit(status: 1);
29 }
30
31 char *outBinaryErr = NULL;
32 LLVMContextRef C = LLVMContextCreate();
33 LLVMBinaryRef O = LLVMCreateBinary(MemBuf: MB, Context: C, 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 LLVMContextDispose(C);
54
55 return 0;
56}
57
58int llvm_object_list_symbols(void) {
59 LLVMMemoryBufferRef MB;
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 LLVMContextRef C = LLVMContextCreate();
72 LLVMBinaryRef O = LLVMCreateBinary(MemBuf: MB, Context: C, ErrorMessage: &outBinaryErr);
73 if (!O || outBinaryErr) {
74 fprintf(stderr, format: "Error reading object: %s\n", outBinaryErr);
75 free(ptr: outBinaryErr);
76 exit(status: 1);
77 }
78
79 sect = LLVMObjectFileCopySectionIterator(BR: O);
80 sym = LLVMObjectFileCopySymbolIterator(BR: O);
81 while (sect && sym && !LLVMObjectFileIsSymbolIteratorAtEnd(BR: O, SI: sym)) {
82
83 LLVMMoveToContainingSection(Sect: sect, Sym: sym);
84 printf(format: "%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(SI: sym),
85 LLVMGetSymbolAddress(SI: sym), LLVMGetSymbolSize(SI: sym),
86 LLVMGetSectionName(SI: sect));
87
88 LLVMMoveToNextSymbol(SI: sym);
89 }
90
91 LLVMDisposeSymbolIterator(SI: sym);
92
93 LLVMDisposeBinary(BR: O);
94
95 LLVMDisposeMemoryBuffer(MemBuf: MB);
96 LLVMContextDispose(C);
97
98 return 0;
99}
100