1 //===- Object.cpp - C bindings to the object file library--------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the C bindings to the file-format-independent object
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Object/ObjectFile.h"
16 #include "llvm-c/Object.h"
17 #include "llvm/Wrap.h"
20 using namespace object;
22 inline ObjectFile *unwrap(LLVMObjectFileRef OF) {
23 return reinterpret_cast<ObjectFile*>(OF);
26 inline LLVMObjectFileRef wrap(const ObjectFile *OF) {
27 return reinterpret_cast<LLVMObjectFileRef>(const_cast<ObjectFile*>(OF));
30 inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
31 return reinterpret_cast<section_iterator*>(SI);
34 inline LLVMSectionIteratorRef
35 wrap(const section_iterator *SI) {
36 return reinterpret_cast<LLVMSectionIteratorRef>
37 (const_cast<section_iterator*>(SI));
40 inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) {
41 return reinterpret_cast<symbol_iterator*>(SI);
44 inline LLVMSymbolIteratorRef
45 wrap(const symbol_iterator *SI) {
46 return reinterpret_cast<LLVMSymbolIteratorRef>
47 (const_cast<symbol_iterator*>(SI));
50 inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) {
51 return reinterpret_cast<relocation_iterator*>(SI);
54 inline LLVMRelocationIteratorRef
55 wrap(const relocation_iterator *SI) {
56 return reinterpret_cast<LLVMRelocationIteratorRef>
57 (const_cast<relocation_iterator*>(SI));
60 // ObjectFile creation
61 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
62 return wrap(ObjectFile::createObjectFile(unwrap(MemBuf)));
65 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
66 delete unwrap(ObjectFile);
69 // ObjectFile Section iterators
70 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
71 section_iterator SI = unwrap(ObjectFile)->begin_sections();
72 return wrap(new section_iterator(SI));
75 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
79 LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
80 LLVMSectionIteratorRef SI) {
81 return (*unwrap(SI) == unwrap(ObjectFile)->end_sections()) ? 1 : 0;
84 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
86 unwrap(SI)->increment(ec);
87 if (ec) report_fatal_error("LLVMMoveToNextSection failed: " + ec.message());
90 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
91 LLVMSymbolIteratorRef Sym) {
92 if (error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
93 report_fatal_error(ec.message());
96 // ObjectFile Symbol iterators
97 LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
98 symbol_iterator SI = unwrap(ObjectFile)->begin_symbols();
99 return wrap(new symbol_iterator(SI));
102 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
106 LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
107 LLVMSymbolIteratorRef SI) {
108 return (*unwrap(SI) == unwrap(ObjectFile)->end_symbols()) ? 1 : 0;
111 void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
113 unwrap(SI)->increment(ec);
114 if (ec) report_fatal_error("LLVMMoveToNextSymbol failed: " + ec.message());
117 // SectionRef accessors
118 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
120 if (error_code ec = (*unwrap(SI))->getName(ret))
121 report_fatal_error(ec.message());
125 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
127 if (error_code ec = (*unwrap(SI))->getSize(ret))
128 report_fatal_error(ec.message());
132 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
134 if (error_code ec = (*unwrap(SI))->getContents(ret))
135 report_fatal_error(ec.message());
139 uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
141 if (error_code ec = (*unwrap(SI))->getAddress(ret))
142 report_fatal_error(ec.message());
146 LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
147 LLVMSymbolIteratorRef Sym) {
149 if (error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
150 report_fatal_error(ec.message());
154 // Section Relocation iterators
155 LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
156 relocation_iterator SI = (*unwrap(Section))->begin_relocations();
157 return wrap(new relocation_iterator(SI));
160 void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
164 LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
165 LLVMRelocationIteratorRef SI) {
166 return (*unwrap(SI) == (*unwrap(Section))->end_relocations()) ? 1 : 0;
169 void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
171 unwrap(SI)->increment(ec);
172 if (ec) report_fatal_error("LLVMMoveToNextRelocation failed: " +
177 // SymbolRef accessors
178 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
180 if (error_code ec = (*unwrap(SI))->getName(ret))
181 report_fatal_error(ec.message());
185 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
187 if (error_code ec = (*unwrap(SI))->getAddress(ret))
188 report_fatal_error(ec.message());
192 uint64_t LLVMGetSymbolFileOffset(LLVMSymbolIteratorRef SI) {
194 if (error_code ec = (*unwrap(SI))->getFileOffset(ret))
195 report_fatal_error(ec.message());
199 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
201 if (error_code ec = (*unwrap(SI))->getSize(ret))
202 report_fatal_error(ec.message());
206 // RelocationRef accessors
207 uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
209 if (error_code ec = (*unwrap(RI))->getAddress(ret))
210 report_fatal_error(ec.message());
214 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
216 if (error_code ec = (*unwrap(RI))->getOffset(ret))
217 report_fatal_error(ec.message());
221 LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
223 if (error_code ec = (*unwrap(RI))->getSymbol(ret))
224 report_fatal_error(ec.message());
226 return wrap(new symbol_iterator(ret));
229 uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
231 if (error_code ec = (*unwrap(RI))->getType(ret))
232 report_fatal_error(ec.message());
236 // NOTE: Caller takes ownership of returned string.
237 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
238 SmallVector<char, 0> ret;
239 if (error_code ec = (*unwrap(RI))->getTypeName(ret))
240 report_fatal_error(ec.message());
242 char *str = static_cast<char*>(malloc(ret.size()));
243 std::copy(ret.begin(), ret.end(), str);
247 // NOTE: Caller takes ownership of returned string.
248 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
249 SmallVector<char, 0> ret;
250 if (error_code ec = (*unwrap(RI))->getValueString(ret))
251 report_fatal_error(ec.message());
253 char *str = static_cast<char*>(malloc(ret.size()));
254 std::copy(ret.begin(), ret.end(), str);