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;
21 using std::error_code;
23 inline ObjectFile *unwrap(LLVMObjectFileRef OF) {
24 return reinterpret_cast<ObjectFile*>(OF);
27 inline LLVMObjectFileRef wrap(const ObjectFile *OF) {
28 return reinterpret_cast<LLVMObjectFileRef>(const_cast<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 ErrorOr<ObjectFile*> ObjOrErr(ObjectFile::createObjectFile(unwrap(MemBuf)));
64 ObjectFile *Obj = ObjOrErr ? ObjOrErr.get() : nullptr;
68 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
69 delete unwrap(ObjectFile);
72 // ObjectFile Section iterators
73 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
74 section_iterator SI = unwrap(ObjectFile)->section_begin();
75 return wrap(new section_iterator(SI));
78 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
82 LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
83 LLVMSectionIteratorRef SI) {
84 return (*unwrap(SI) == unwrap(ObjectFile)->section_end()) ? 1 : 0;
87 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
91 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
92 LLVMSymbolIteratorRef Sym) {
93 if (error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
94 report_fatal_error(ec.message());
97 // ObjectFile Symbol iterators
98 LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
99 symbol_iterator SI = unwrap(ObjectFile)->symbol_begin();
100 return wrap(new symbol_iterator(SI));
103 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
107 LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
108 LLVMSymbolIteratorRef SI) {
109 return (*unwrap(SI) == unwrap(ObjectFile)->symbol_end()) ? 1 : 0;
112 void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
116 // SectionRef accessors
117 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
119 if (error_code ec = (*unwrap(SI))->getName(ret))
120 report_fatal_error(ec.message());
124 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
126 if (error_code ec = (*unwrap(SI))->getSize(ret))
127 report_fatal_error(ec.message());
131 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
133 if (error_code ec = (*unwrap(SI))->getContents(ret))
134 report_fatal_error(ec.message());
138 uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
140 if (error_code ec = (*unwrap(SI))->getAddress(ret))
141 report_fatal_error(ec.message());
145 LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
146 LLVMSymbolIteratorRef Sym) {
148 if (error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
149 report_fatal_error(ec.message());
153 // Section Relocation iterators
154 LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
155 relocation_iterator SI = (*unwrap(Section))->relocation_begin();
156 return wrap(new relocation_iterator(SI));
159 void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
163 LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
164 LLVMRelocationIteratorRef SI) {
165 return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
168 void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
173 // SymbolRef accessors
174 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
176 if (error_code ec = (*unwrap(SI))->getName(ret))
177 report_fatal_error(ec.message());
181 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
183 if (error_code ec = (*unwrap(SI))->getAddress(ret))
184 report_fatal_error(ec.message());
188 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
190 if (error_code ec = (*unwrap(SI))->getSize(ret))
191 report_fatal_error(ec.message());
195 // RelocationRef accessors
196 uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
198 if (error_code ec = (*unwrap(RI))->getAddress(ret))
199 report_fatal_error(ec.message());
203 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
205 if (error_code ec = (*unwrap(RI))->getOffset(ret))
206 report_fatal_error(ec.message());
210 LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
211 symbol_iterator ret = (*unwrap(RI))->getSymbol();
212 return wrap(new symbol_iterator(ret));
215 uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
217 if (error_code ec = (*unwrap(RI))->getType(ret))
218 report_fatal_error(ec.message());
222 // NOTE: Caller takes ownership of returned string.
223 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
224 SmallVector<char, 0> ret;
225 if (error_code ec = (*unwrap(RI))->getTypeName(ret))
226 report_fatal_error(ec.message());
228 char *str = static_cast<char*>(malloc(ret.size()));
229 std::copy(ret.begin(), ret.end(), str);
233 // NOTE: Caller takes ownership of returned string.
234 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
235 SmallVector<char, 0> ret;
236 if (error_code ec = (*unwrap(RI))->getValueString(ret))
237 report_fatal_error(ec.message());
239 char *str = static_cast<char*>(malloc(ret.size()));
240 std::copy(ret.begin(), ret.end(), str);