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"
19 using namespace object;
21 // ObjectFile creation
22 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
23 return wrap(ObjectFile::createObjectFile(unwrap(MemBuf)));
26 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
27 delete unwrap(ObjectFile);
30 // ObjectFile Section iterators
31 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
32 section_iterator SI = unwrap(ObjectFile)->begin_sections();
33 return wrap(new section_iterator(SI));
36 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
40 LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
41 LLVMSectionIteratorRef SI) {
42 return (*unwrap(SI) == unwrap(ObjectFile)->end_sections()) ? 1 : 0;
45 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
47 unwrap(SI)->increment(ec);
48 if (ec) report_fatal_error("LLVMMoveToNextSection failed: " + ec.message());
51 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
52 LLVMSymbolIteratorRef Sym) {
53 if (error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
54 report_fatal_error(ec.message());
57 // ObjectFile Symbol iterators
58 LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
59 symbol_iterator SI = unwrap(ObjectFile)->begin_symbols();
60 return wrap(new symbol_iterator(SI));
63 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
67 LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
68 LLVMSymbolIteratorRef SI) {
69 return (*unwrap(SI) == unwrap(ObjectFile)->end_symbols()) ? 1 : 0;
72 void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
74 unwrap(SI)->increment(ec);
75 if (ec) report_fatal_error("LLVMMoveToNextSymbol failed: " + ec.message());
78 // SectionRef accessors
79 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
81 if (error_code ec = (*unwrap(SI))->getName(ret))
82 report_fatal_error(ec.message());
86 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
88 if (error_code ec = (*unwrap(SI))->getSize(ret))
89 report_fatal_error(ec.message());
93 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
95 if (error_code ec = (*unwrap(SI))->getContents(ret))
96 report_fatal_error(ec.message());
100 uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
102 if (error_code ec = (*unwrap(SI))->getAddress(ret))
103 report_fatal_error(ec.message());
107 LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
108 LLVMSymbolIteratorRef Sym) {
110 if (error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
111 report_fatal_error(ec.message());
115 // Section Relocation iterators
116 LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
117 relocation_iterator SI = (*unwrap(Section))->begin_relocations();
118 return wrap(new relocation_iterator(SI));
121 void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
125 LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
126 LLVMRelocationIteratorRef SI) {
127 return (*unwrap(SI) == (*unwrap(Section))->end_relocations()) ? 1 : 0;
130 void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
132 unwrap(SI)->increment(ec);
133 if (ec) report_fatal_error("LLVMMoveToNextRelocation failed: " +
138 // SymbolRef accessors
139 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
141 if (error_code ec = (*unwrap(SI))->getName(ret))
142 report_fatal_error(ec.message());
146 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
148 if (error_code ec = (*unwrap(SI))->getAddress(ret))
149 report_fatal_error(ec.message());
153 uint64_t LLVMGetSymbolFileOffset(LLVMSymbolIteratorRef SI) {
155 if (error_code ec = (*unwrap(SI))->getFileOffset(ret))
156 report_fatal_error(ec.message());
160 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
162 if (error_code ec = (*unwrap(SI))->getSize(ret))
163 report_fatal_error(ec.message());
167 // RelocationRef accessors
168 uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
170 if (error_code ec = (*unwrap(RI))->getAddress(ret))
171 report_fatal_error(ec.message());
175 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
177 if (error_code ec = (*unwrap(RI))->getOffset(ret))
178 report_fatal_error(ec.message());
182 LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
184 if (error_code ec = (*unwrap(RI))->getSymbol(ret))
185 report_fatal_error(ec.message());
187 return wrap(new symbol_iterator(ret));
190 uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
192 if (error_code ec = (*unwrap(RI))->getType(ret))
193 report_fatal_error(ec.message());
197 // NOTE: Caller takes ownership of returned string.
198 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
199 SmallVector<char, 0> ret;
200 if (error_code ec = (*unwrap(RI))->getTypeName(ret))
201 report_fatal_error(ec.message());
203 char *str = static_cast<char*>(malloc(ret.size()));
204 std::copy(ret.begin(), ret.end(), str);
208 // NOTE: Caller takes ownership of returned string.
209 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
210 SmallVector<char, 0> ret;
211 if (error_code ec = (*unwrap(RI))->getValueString(ret))
212 report_fatal_error(ec.message());
214 char *str = static_cast<char*>(malloc(ret.size()));
215 std::copy(ret.begin(), ret.end(), str);