llvm.org GIT mirror llvm / f16fe4c
[LLVM-C] Add Section and Symbol Iterator Accessors for Object File Binaries Summary: This brings us to full feature parity with the old API, so I've deprecated it and updated the tests. I'll do a follow-up patch to do some more cleanup and documentation work in this header. Reviewers: whitequark, deadalnix Reviewed By: whitequark Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D60407 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358037 91177308-0d34-0410-b5e6-96231b3b80d8 Robert Widmann 4 months ago
3 changed file(s) with 124 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
3333 */
3434
3535 // Opaque type wrappers
36 typedef struct LLVMOpaqueObjectFile *LLVMObjectFileRef;
3736 typedef struct LLVMOpaqueSectionIterator *LLVMSectionIteratorRef;
3837 typedef struct LLVMOpaqueSymbolIterator *LLVMSymbolIteratorRef;
3938 typedef struct LLVMOpaqueRelocationIterator *LLVMRelocationIteratorRef;
102101 */
103102 LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR);
104103
105
106 // ObjectFile creation
107 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf);
108 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile);
109
110 // ObjectFile Section iterators
111 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile);
104 /**
105 * Retrieve a copy of the the section iterator for this object file.
106 *
107 * If there are no sections, the result is NULL.
108 *
109 * The returned iterator is merely a shallow copy. Nevertheless, it is
110 * the responsibility of the caller to free it with
111 * \c LLVMDisposeSectionIterator.
112 *
113 * @see llvm::object::sections()
114 */
115 LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR);
116
117 /**
118 * Returns whether the given section iterator is at the end.
119 *
120 * @see llvm::object::section_end
121 */
122 LLVMBool LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,
123 LLVMSectionIteratorRef SI);
124
125 /**
126 * Retrieve a copy of the the symbol iterator for this object file.
127 *
128 * If there are no symbols, the result is NULL.
129 *
130 * The returned iterator is merely a shallow copy. Nevertheless, it is
131 * the responsibility of the caller to free it with
132 * \c LLVMDisposeSymbolIterator.
133 *
134 * @see llvm::object::symbols()
135 */
136 LLVMSymbolIteratorRef LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR);
137
138 /**
139 * Returns whether the given symbol iterator is at the end.
140 *
141 * @see llvm::object::symbol_end
142 */
143 LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
144 LLVMSymbolIteratorRef SI);
145
112146 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI);
113 LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
114 LLVMSectionIteratorRef SI);
147
115148 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI);
116149 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
117150 LLVMSymbolIteratorRef Sym);
118151
119152 // ObjectFile Symbol iterators
120 LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile);
121153 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI);
122 LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
123 LLVMSymbolIteratorRef SI);
124154 void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI);
125155
126156 // SectionRef accessors
153183 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI);
154184 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI);
155185
186 /** Deprecated: Use LLVMBinaryRef instead. */
187 typedef struct LLVMOpaqueObjectFile *LLVMObjectFileRef;
188
189 /** Deprecated: Use LLVMCreateBinary instead. */
190 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf);
191
192 /** Deprecated: Use LLVMDisposeBinary instead. */
193 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile);
194
195 /** Deprecated: Use LLVMObjectFileCopySectionIterator instead. */
196 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile);
197
198 /** Deprecated: Use LLVMObjectFileIsSectionIteratorAtEnd instead. */
199 LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
200 LLVMSectionIteratorRef SI);
201
202 /** Deprecated: Use LLVMObjectFileCopySymbolIterator instead. */
203 LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile);
204
205 /** Deprecated: Use LLVMObjectFileIsSymbolIteratorAtEnd instead. */
206 LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
207 LLVMSymbolIteratorRef SI);
156208 /**
157209 * @}
158210 */
130130 return BinaryTypeMapper::mapBinaryTypeToLLVMBinaryType(unwrap(BR)->getType());
131131 }
132132
133 LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR) {
134 auto OF = cast(unwrap(BR));
135 auto sections = OF->sections();
136 if (sections.begin() == sections.end())
137 return nullptr;
138 return wrap(new section_iterator(sections.begin()));
139 }
140
141 LLVMBool LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,
142 LLVMSectionIteratorRef SI) {
143 auto OF = cast(unwrap(BR));
144 return (*unwrap(SI) == OF->section_end()) ? 1 : 0;
145 }
146
147 LLVMSymbolIteratorRef LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR) {
148 auto OF = cast(unwrap(BR));
149 auto symbols = OF->symbols();
150 if (symbols.begin() == symbols.end())
151 return nullptr;
152 return wrap(new symbol_iterator(symbols.begin()));
153 }
154
155 LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
156 LLVMSymbolIteratorRef SI) {
157 auto OF = cast(unwrap(BR));
158 return (*unwrap(SI) == OF->symbol_end()) ? 1 : 0;
159 }
160
133161 // ObjectFile creation
134162 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
135163 std::unique_ptr Buf(unwrap(MemBuf));
1818
1919 int llvm_object_list_sections(void) {
2020 LLVMMemoryBufferRef MB;
21 LLVMObjectFileRef O;
21 LLVMBinaryRef O;
2222 LLVMSectionIteratorRef sect;
23 char *msg = NULL;
2423
25 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
26 fprintf(stderr, "Error reading file: %s\n", msg);
24 char *outBufferErr = NULL;
25 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
26 fprintf(stderr, "Error reading file: %s\n", outBufferErr);
27 free(outBufferErr);
2728 exit(1);
2829 }
2930
30 O = LLVMCreateObjectFile(MB);
31 if (!O) {
32 fprintf(stderr, "Error reading object\n");
31 char *outBinaryErr = NULL;
32 O = LLVMCreateBinary(MB, LLVMGetGlobalContext(), &outBinaryErr);
33 if (!O || outBinaryErr) {
34 fprintf(stderr, "Error reading object: %s\n", outBinaryErr);
35 free(outBinaryErr);
3336 exit(1);
3437 }
3538
36 sect = LLVMGetSections(O);
37 while (!LLVMIsSectionIteratorAtEnd(O, sect)) {
39 sect = LLVMObjectFileCopySectionIterator(O);
40 while (sect && !LLVMObjectFileIsSectionIteratorAtEnd(O, sect)) {
3841 printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
3942 LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
4043
4346
4447 LLVMDisposeSectionIterator(sect);
4548
46 LLVMDisposeObjectFile(O);
49 LLVMDisposeBinary(O);
50
51 LLVMDisposeMemoryBuffer(MB);
4752
4853 return 0;
4954 }
5055
5156 int llvm_object_list_symbols(void) {
5257 LLVMMemoryBufferRef MB;
53 LLVMObjectFileRef O;
58 LLVMBinaryRef O;
5459 LLVMSectionIteratorRef sect;
5560 LLVMSymbolIteratorRef sym;
56 char *msg = NULL;
5761
58 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
59 fprintf(stderr, "Error reading file: %s\n", msg);
62 char *outBufferErr = NULL;
63 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
64 fprintf(stderr, "Error reading file: %s\n", outBufferErr);
65 free(outBufferErr);
6066 exit(1);
6167 }
6268
63 O = LLVMCreateObjectFile(MB);
64 if (!O) {
65 fprintf(stderr, "Error reading object\n");
69 char *outBinaryErr = NULL;
70 O = LLVMCreateBinary(MB, LLVMGetGlobalContext(), &outBinaryErr);
71 if (!O || outBinaryErr) {
72 fprintf(stderr, "Error reading object: %s\n", outBinaryErr);
73 free(outBinaryErr);
6674 exit(1);
6775 }
6876
69 sect = LLVMGetSections(O);
70 sym = LLVMGetSymbols(O);
71 while (!LLVMIsSymbolIteratorAtEnd(O, sym)) {
77 sect = LLVMObjectFileCopySectionIterator(O);
78 sym = LLVMObjectFileCopySymbolIterator(O);
79 while (sect && sym && !LLVMObjectFileIsSymbolIteratorAtEnd(O, sym)) {
7280
7381 LLVMMoveToContainingSection(sect, sym);
7482 printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym),
8088
8189 LLVMDisposeSymbolIterator(sym);
8290
83 LLVMDisposeObjectFile(O);
91 LLVMDisposeBinary(O);
92
93 LLVMDisposeMemoryBuffer(MB);
8494
8595 return 0;
8696 }