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
llvm_object_list_sections(void)20 int llvm_object_list_sections(void) {
21 LLVMMemoryBufferRef MB;
22 LLVMBinaryRef O;
23 LLVMSectionIteratorRef sect;
24
25 char *outBufferErr = NULL;
26 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
27 fprintf(stderr, "Error reading file: %s\n", outBufferErr);
28 free(outBufferErr);
29 exit(1);
30 }
31
32 char *outBinaryErr = NULL;
33 O = LLVMCreateBinary(MB, LLVMGetGlobalContext(), &outBinaryErr);
34 if (!O || outBinaryErr) {
35 fprintf(stderr, "Error reading object: %s\n", outBinaryErr);
36 free(outBinaryErr);
37 exit(1);
38 }
39
40 sect = LLVMObjectFileCopySectionIterator(O);
41 while (sect && !LLVMObjectFileIsSectionIteratorAtEnd(O, sect)) {
42 printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
43 LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
44
45 LLVMMoveToNextSection(sect);
46 }
47
48 LLVMDisposeSectionIterator(sect);
49
50 LLVMDisposeBinary(O);
51
52 LLVMDisposeMemoryBuffer(MB);
53
54 return 0;
55 }
56
llvm_object_list_symbols(void)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(&MB, &outBufferErr)) {
65 fprintf(stderr, "Error reading file: %s\n", outBufferErr);
66 free(outBufferErr);
67 exit(1);
68 }
69
70 char *outBinaryErr = NULL;
71 O = LLVMCreateBinary(MB, LLVMGetGlobalContext(), &outBinaryErr);
72 if (!O || outBinaryErr) {
73 fprintf(stderr, "Error reading object: %s\n", outBinaryErr);
74 free(outBinaryErr);
75 exit(1);
76 }
77
78 sect = LLVMObjectFileCopySectionIterator(O);
79 sym = LLVMObjectFileCopySymbolIterator(O);
80 while (sect && sym && !LLVMObjectFileIsSymbolIteratorAtEnd(O, sym)) {
81
82 LLVMMoveToContainingSection(sect, sym);
83 printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym),
84 LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym),
85 LLVMGetSectionName(sect));
86
87 LLVMMoveToNextSymbol(sym);
88 }
89
90 LLVMDisposeSymbolIterator(sym);
91
92 LLVMDisposeBinary(O);
93
94 LLVMDisposeMemoryBuffer(MB);
95
96 return 0;
97 }
98