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-c/Object.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Object/ObjectFile.h"
20 using namespace object
;
22 inline OwningBinary
<ObjectFile
> *unwrap(LLVMObjectFileRef OF
) {
23 return reinterpret_cast<OwningBinary
<ObjectFile
> *>(OF
);
26 inline LLVMObjectFileRef
wrap(const OwningBinary
<ObjectFile
> *OF
) {
27 return reinterpret_cast<LLVMObjectFileRef
>(
28 const_cast<OwningBinary
<ObjectFile
> *>(OF
));
31 inline section_iterator
*unwrap(LLVMSectionIteratorRef SI
) {
32 return reinterpret_cast<section_iterator
*>(SI
);
35 inline LLVMSectionIteratorRef
36 wrap(const section_iterator
*SI
) {
37 return reinterpret_cast<LLVMSectionIteratorRef
>
38 (const_cast<section_iterator
*>(SI
));
41 inline symbol_iterator
*unwrap(LLVMSymbolIteratorRef SI
) {
42 return reinterpret_cast<symbol_iterator
*>(SI
);
45 inline LLVMSymbolIteratorRef
46 wrap(const symbol_iterator
*SI
) {
47 return reinterpret_cast<LLVMSymbolIteratorRef
>
48 (const_cast<symbol_iterator
*>(SI
));
51 inline relocation_iterator
*unwrap(LLVMRelocationIteratorRef SI
) {
52 return reinterpret_cast<relocation_iterator
*>(SI
);
55 inline LLVMRelocationIteratorRef
56 wrap(const relocation_iterator
*SI
) {
57 return reinterpret_cast<LLVMRelocationIteratorRef
>
58 (const_cast<relocation_iterator
*>(SI
));
61 // ObjectFile creation
62 LLVMObjectFileRef
LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf
) {
63 std::unique_ptr
<MemoryBuffer
> Buf(unwrap(MemBuf
));
64 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr(
65 ObjectFile::createObjectFile(Buf
->getMemBufferRef()));
66 std::unique_ptr
<ObjectFile
> Obj
;
68 // TODO: Actually report errors helpfully.
69 consumeError(ObjOrErr
.takeError());
73 auto *Ret
= new OwningBinary
<ObjectFile
>(std::move(ObjOrErr
.get()), std::move(Buf
));
77 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile
) {
78 delete unwrap(ObjectFile
);
81 // ObjectFile Section iterators
82 LLVMSectionIteratorRef
LLVMGetSections(LLVMObjectFileRef OF
) {
83 OwningBinary
<ObjectFile
> *OB
= unwrap(OF
);
84 section_iterator SI
= OB
->getBinary()->section_begin();
85 return wrap(new section_iterator(SI
));
88 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI
) {
92 LLVMBool
LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF
,
93 LLVMSectionIteratorRef SI
) {
94 OwningBinary
<ObjectFile
> *OB
= unwrap(OF
);
95 return (*unwrap(SI
) == OB
->getBinary()->section_end()) ? 1 : 0;
98 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI
) {
102 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect
,
103 LLVMSymbolIteratorRef Sym
) {
104 Expected
<section_iterator
> SecOrErr
= (*unwrap(Sym
))->getSection();
107 raw_string_ostream
OS(Buf
);
108 logAllUnhandledErrors(SecOrErr
.takeError(), OS
, "");
110 report_fatal_error(Buf
);
112 *unwrap(Sect
) = *SecOrErr
;
115 // ObjectFile Symbol iterators
116 LLVMSymbolIteratorRef
LLVMGetSymbols(LLVMObjectFileRef OF
) {
117 OwningBinary
<ObjectFile
> *OB
= unwrap(OF
);
118 symbol_iterator SI
= OB
->getBinary()->symbol_begin();
119 return wrap(new symbol_iterator(SI
));
122 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI
) {
126 LLVMBool
LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF
,
127 LLVMSymbolIteratorRef SI
) {
128 OwningBinary
<ObjectFile
> *OB
= unwrap(OF
);
129 return (*unwrap(SI
) == OB
->getBinary()->symbol_end()) ? 1 : 0;
132 void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI
) {
136 // SectionRef accessors
137 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI
) {
139 if (std::error_code ec
= (*unwrap(SI
))->getName(ret
))
140 report_fatal_error(ec
.message());
144 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI
) {
145 return (*unwrap(SI
))->getSize();
148 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI
) {
150 if (std::error_code ec
= (*unwrap(SI
))->getContents(ret
))
151 report_fatal_error(ec
.message());
155 uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI
) {
156 return (*unwrap(SI
))->getAddress();
159 LLVMBool
LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI
,
160 LLVMSymbolIteratorRef Sym
) {
161 return (*unwrap(SI
))->containsSymbol(**unwrap(Sym
));
164 // Section Relocation iterators
165 LLVMRelocationIteratorRef
LLVMGetRelocations(LLVMSectionIteratorRef Section
) {
166 relocation_iterator SI
= (*unwrap(Section
))->relocation_begin();
167 return wrap(new relocation_iterator(SI
));
170 void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI
) {
174 LLVMBool
LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section
,
175 LLVMRelocationIteratorRef SI
) {
176 return (*unwrap(SI
) == (*unwrap(Section
))->relocation_end()) ? 1 : 0;
179 void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI
) {
184 // SymbolRef accessors
185 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI
) {
186 Expected
<StringRef
> Ret
= (*unwrap(SI
))->getName();
189 raw_string_ostream
OS(Buf
);
190 logAllUnhandledErrors(Ret
.takeError(), OS
, "");
192 report_fatal_error(Buf
);
197 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI
) {
198 Expected
<uint64_t> Ret
= (*unwrap(SI
))->getAddress();
201 raw_string_ostream
OS(Buf
);
202 logAllUnhandledErrors(Ret
.takeError(), OS
, "");
204 report_fatal_error(Buf
);
209 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI
) {
210 return (*unwrap(SI
))->getCommonSize();
213 // RelocationRef accessors
214 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI
) {
215 return (*unwrap(RI
))->getOffset();
218 LLVMSymbolIteratorRef
LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI
) {
219 symbol_iterator ret
= (*unwrap(RI
))->getSymbol();
220 return wrap(new symbol_iterator(ret
));
223 uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI
) {
224 return (*unwrap(RI
))->getType();
227 // NOTE: Caller takes ownership of returned string.
228 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI
) {
229 SmallVector
<char, 0> ret
;
230 (*unwrap(RI
))->getTypeName(ret
);
231 char *str
= static_cast<char*>(malloc(ret
.size()));
232 std::copy(ret
.begin(), ret
.end(), str
);
236 // NOTE: Caller takes ownership of returned string.
237 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI
) {