1 /*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
5 |* See https://llvm.org/LICENSE.txt for license information. *|
6 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
8 |*===----------------------------------------------------------------------===*|
10 |* This file implements the --object-list-sections and --object-list-symbols *|
11 |* commands in llvm-c-test. *|
13 \*===----------------------------------------------------------------------===*/
15 #include "llvm-c-test.h"
16 #include "llvm-c/Object.h"
20 int llvm_object_list_sections(void) {
21 LLVMMemoryBufferRef MB
;
23 LLVMSectionIteratorRef sect
;
25 char *outBufferErr
= NULL
;
26 if (LLVMCreateMemoryBufferWithSTDIN(&MB
, &outBufferErr
)) {
27 fprintf(stderr
, "Error reading file: %s\n", outBufferErr
);
32 char *outBinaryErr
= NULL
;
33 O
= LLVMCreateBinary(MB
, LLVMGetGlobalContext(), &outBinaryErr
);
34 if (!O
|| outBinaryErr
) {
35 fprintf(stderr
, "Error reading object: %s\n", outBinaryErr
);
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
));
45 LLVMMoveToNextSection(sect
);
48 LLVMDisposeSectionIterator(sect
);
52 LLVMDisposeMemoryBuffer(MB
);
57 int llvm_object_list_symbols(void) {
58 LLVMMemoryBufferRef MB
;
60 LLVMSectionIteratorRef sect
;
61 LLVMSymbolIteratorRef sym
;
63 char *outBufferErr
= NULL
;
64 if (LLVMCreateMemoryBufferWithSTDIN(&MB
, &outBufferErr
)) {
65 fprintf(stderr
, "Error reading file: %s\n", outBufferErr
);
70 char *outBinaryErr
= NULL
;
71 O
= LLVMCreateBinary(MB
, LLVMGetGlobalContext(), &outBinaryErr
);
72 if (!O
|| outBinaryErr
) {
73 fprintf(stderr
, "Error reading object: %s\n", outBinaryErr
);
78 sect
= LLVMObjectFileCopySectionIterator(O
);
79 sym
= LLVMObjectFileCopySymbolIterator(O
);
80 while (sect
&& sym
&& !LLVMObjectFileIsSymbolIteratorAtEnd(O
, sym
)) {
82 LLVMMoveToContainingSection(sect
, sym
);
83 printf("%s @0x%08" PRIx64
" +%" PRIu64
" (%s)\n", LLVMGetSymbolName(sym
),
84 LLVMGetSymbolAddress(sym
), LLVMGetSymbolSize(sym
),
85 LLVMGetSectionName(sect
));
87 LLVMMoveToNextSymbol(sym
);
90 LLVMDisposeSymbolIterator(sym
);
94 LLVMDisposeMemoryBuffer(MB
);