Remove the default clause from a fully-covering switch
[llvm-core.git] / lib / Object / Object.cpp
blob1d2859cfbe9d87b3a0c7203741b0e4c67a19b682
1 //===- Object.cpp - C bindings to the object file library--------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the C bindings to the file-format-independent object
11 // library.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Object.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Object/ObjectFile.h"
19 using namespace llvm;
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;
67 if (!ObjOrErr) {
68 // TODO: Actually report errors helpfully.
69 consumeError(ObjOrErr.takeError());
70 return nullptr;
73 auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
74 return wrap(Ret);
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) {
89 delete unwrap(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) {
99 ++(*unwrap(SI));
102 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
103 LLVMSymbolIteratorRef Sym) {
104 Expected<section_iterator> SecOrErr = (*unwrap(Sym))->getSection();
105 if (!SecOrErr) {
106 std::string Buf;
107 raw_string_ostream OS(Buf);
108 logAllUnhandledErrors(SecOrErr.takeError(), OS, "");
109 OS.flush();
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) {
123 delete unwrap(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) {
133 ++(*unwrap(SI));
136 // SectionRef accessors
137 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
138 StringRef ret;
139 if (std::error_code ec = (*unwrap(SI))->getName(ret))
140 report_fatal_error(ec.message());
141 return ret.data();
144 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
145 return (*unwrap(SI))->getSize();
148 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
149 StringRef ret;
150 if (std::error_code ec = (*unwrap(SI))->getContents(ret))
151 report_fatal_error(ec.message());
152 return ret.data();
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) {
171 delete unwrap(SI);
174 LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
175 LLVMRelocationIteratorRef SI) {
176 return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
179 void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
180 ++(*unwrap(SI));
184 // SymbolRef accessors
185 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
186 Expected<StringRef> Ret = (*unwrap(SI))->getName();
187 if (!Ret) {
188 std::string Buf;
189 raw_string_ostream OS(Buf);
190 logAllUnhandledErrors(Ret.takeError(), OS, "");
191 OS.flush();
192 report_fatal_error(Buf);
194 return Ret->data();
197 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
198 Expected<uint64_t> Ret = (*unwrap(SI))->getAddress();
199 if (!Ret) {
200 std::string Buf;
201 raw_string_ostream OS(Buf);
202 logAllUnhandledErrors(Ret.takeError(), OS, "");
203 OS.flush();
204 report_fatal_error(Buf);
206 return *Ret;
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);
233 return str;
236 // NOTE: Caller takes ownership of returned string.
237 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
238 return strdup("");