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