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 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 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr(
65 ObjectFile::createObjectFile(Buf->getMemBufferRef()));
66 std::unique_ptr<ObjectFile> Obj;
70 auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
74 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
75 delete unwrap(ObjectFile);
78 // ObjectFile Section iterators
79 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) {
80 OwningBinary<ObjectFile> *OB = unwrap(OF);
81 section_iterator SI = OB->getBinary()->section_begin();
82 return wrap(new section_iterator(SI));
85 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
89 LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,
90 LLVMSectionIteratorRef SI) {
91 OwningBinary<ObjectFile> *OB = unwrap(OF);
92 return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
95 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
99 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
100 LLVMSymbolIteratorRef Sym) {
101 if (std::error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
102 report_fatal_error(ec.message());
105 // ObjectFile Symbol iterators
106 LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) {
107 OwningBinary<ObjectFile> *OB = unwrap(OF);
108 symbol_iterator SI = OB->getBinary()->symbol_begin();
109 return wrap(new symbol_iterator(SI));
112 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
116 LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,
117 LLVMSymbolIteratorRef SI) {
118 OwningBinary<ObjectFile> *OB = unwrap(OF);
119 return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
122 void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
126 // SectionRef accessors
127 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
129 if (std::error_code ec = (*unwrap(SI))->getName(ret))
130 report_fatal_error(ec.message());
134 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
136 if (std::error_code ec = (*unwrap(SI))->getSize(ret))
137 report_fatal_error(ec.message());
141 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
143 if (std::error_code ec = (*unwrap(SI))->getContents(ret))
144 report_fatal_error(ec.message());
148 uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
150 if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
151 report_fatal_error(ec.message());
155 LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
156 LLVMSymbolIteratorRef Sym) {
158 if (std::error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
159 report_fatal_error(ec.message());
163 // Section Relocation iterators
164 LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
165 relocation_iterator SI = (*unwrap(Section))->relocation_begin();
166 return wrap(new relocation_iterator(SI));
169 void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
173 LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
174 LLVMRelocationIteratorRef SI) {
175 return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
178 void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
183 // SymbolRef accessors
184 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
186 if (std::error_code ec = (*unwrap(SI))->getName(ret))
187 report_fatal_error(ec.message());
191 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
193 if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
194 report_fatal_error(ec.message());
198 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
200 if (std::error_code ec = (*unwrap(SI))->getSize(ret))
201 report_fatal_error(ec.message());
205 // RelocationRef accessors
206 uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
208 if (std::error_code ec = (*unwrap(RI))->getAddress(ret))
209 report_fatal_error(ec.message());
213 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
215 if (std::error_code ec = (*unwrap(RI))->getOffset(ret))
216 report_fatal_error(ec.message());
220 LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
221 symbol_iterator ret = (*unwrap(RI))->getSymbol();
222 return wrap(new symbol_iterator(ret));
225 uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
227 if (std::error_code ec = (*unwrap(RI))->getType(ret))
228 report_fatal_error(ec.message());
232 // NOTE: Caller takes ownership of returned string.
233 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
234 SmallVector<char, 0> ret;
235 if (std::error_code ec = (*unwrap(RI))->getTypeName(ret))
236 report_fatal_error(ec.message());
238 char *str = static_cast<char*>(malloc(ret.size()));
239 std::copy(ret.begin(), ret.end(), str);
243 // NOTE: Caller takes ownership of returned string.
244 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
245 SmallVector<char, 0> ret;
246 if (std::error_code ec = (*unwrap(RI))->getValueString(ret))
247 report_fatal_error(ec.message());
249 char *str = static_cast<char*>(malloc(ret.size()));
250 std::copy(ret.begin(), ret.end(), str);