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/ADT/SmallVector.h"
16 #include "llvm-c/Object.h"
17 #include "llvm/Object/ObjectFile.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 std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
63 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr(
64 ObjectFile::createObjectFile(Buf));
66 ObjectFile *Obj = ObjOrErr ? ObjOrErr.get().release() : nullptr;
70 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
71 delete unwrap(ObjectFile);
74 // ObjectFile Section iterators
75 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
76 section_iterator SI = unwrap(ObjectFile)->section_begin();
77 return wrap(new section_iterator(SI));
80 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
84 LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
85 LLVMSectionIteratorRef SI) {
86 return (*unwrap(SI) == unwrap(ObjectFile)->section_end()) ? 1 : 0;
89 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
93 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
94 LLVMSymbolIteratorRef Sym) {
95 if (std::error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
96 report_fatal_error(ec.message());
99 // ObjectFile Symbol iterators
100 LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
101 symbol_iterator SI = unwrap(ObjectFile)->symbol_begin();
102 return wrap(new symbol_iterator(SI));
105 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
109 LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
110 LLVMSymbolIteratorRef SI) {
111 return (*unwrap(SI) == unwrap(ObjectFile)->symbol_end()) ? 1 : 0;
114 void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
118 // SectionRef accessors
119 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
121 if (std::error_code ec = (*unwrap(SI))->getName(ret))
122 report_fatal_error(ec.message());
126 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
128 if (std::error_code ec = (*unwrap(SI))->getSize(ret))
129 report_fatal_error(ec.message());
133 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
135 if (std::error_code ec = (*unwrap(SI))->getContents(ret))
136 report_fatal_error(ec.message());
140 uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
142 if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
143 report_fatal_error(ec.message());
147 LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
148 LLVMSymbolIteratorRef Sym) {
150 if (std::error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
151 report_fatal_error(ec.message());
155 // Section Relocation iterators
156 LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
157 relocation_iterator SI = (*unwrap(Section))->relocation_begin();
158 return wrap(new relocation_iterator(SI));
161 void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
165 LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
166 LLVMRelocationIteratorRef SI) {
167 return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
170 void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
175 // SymbolRef accessors
176 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
178 if (std::error_code ec = (*unwrap(SI))->getName(ret))
179 report_fatal_error(ec.message());
183 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
185 if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
186 report_fatal_error(ec.message());
190 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
192 if (std::error_code ec = (*unwrap(SI))->getSize(ret))
193 report_fatal_error(ec.message());
197 // RelocationRef accessors
198 uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
200 if (std::error_code ec = (*unwrap(RI))->getAddress(ret))
201 report_fatal_error(ec.message());
205 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
207 if (std::error_code ec = (*unwrap(RI))->getOffset(ret))
208 report_fatal_error(ec.message());
212 LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
213 symbol_iterator ret = (*unwrap(RI))->getSymbol();
214 return wrap(new symbol_iterator(ret));
217 uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
219 if (std::error_code ec = (*unwrap(RI))->getType(ret))
220 report_fatal_error(ec.message());
224 // NOTE: Caller takes ownership of returned string.
225 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
226 SmallVector<char, 0> ret;
227 if (std::error_code ec = (*unwrap(RI))->getTypeName(ret))
228 report_fatal_error(ec.message());
230 char *str = static_cast<char*>(malloc(ret.size()));
231 std::copy(ret.begin(), ret.end(), str);
235 // NOTE: Caller takes ownership of returned string.
236 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
237 SmallVector<char, 0> ret;
238 if (std::error_code ec = (*unwrap(RI))->getValueString(ret))
239 report_fatal_error(ec.message());
241 char *str = static_cast<char*>(malloc(ret.size()));
242 std::copy(ret.begin(), ret.end(), str);