1c56d2e8fSEric Christopher //===- Object.cpp - C bindings to the object file library--------*- C++ -*-===//
2c56d2e8fSEric Christopher //
3c56d2e8fSEric Christopher //                     The LLVM Compiler Infrastructure
4c56d2e8fSEric Christopher //
5c56d2e8fSEric Christopher // This file is distributed under the University of Illinois Open Source
6c56d2e8fSEric Christopher // License. See LICENSE.TXT for details.
7c56d2e8fSEric Christopher //
8c56d2e8fSEric Christopher //===----------------------------------------------------------------------===//
9c56d2e8fSEric Christopher //
10c56d2e8fSEric Christopher // This file defines the C bindings to the file-format-independent object
11c56d2e8fSEric Christopher // library.
12c56d2e8fSEric Christopher //
13c56d2e8fSEric Christopher //===----------------------------------------------------------------------===//
14c56d2e8fSEric Christopher 
15dec20e43SFilip Pizlo #include "llvm/ADT/SmallVector.h"
16c56d2e8fSEric Christopher #include "llvm-c/Object.h"
178a8cd2baSChandler Carruth #include "llvm/Object/ObjectFile.h"
18c56d2e8fSEric Christopher 
19c56d2e8fSEric Christopher using namespace llvm;
20c56d2e8fSEric Christopher using namespace object;
21c56d2e8fSEric Christopher 
2204d4e931SEric Christopher inline ObjectFile *unwrap(LLVMObjectFileRef OF) {
2304d4e931SEric Christopher   return reinterpret_cast<ObjectFile*>(OF);
2404d4e931SEric Christopher }
2504d4e931SEric Christopher 
2604d4e931SEric Christopher inline LLVMObjectFileRef wrap(const ObjectFile *OF) {
2704d4e931SEric Christopher   return reinterpret_cast<LLVMObjectFileRef>(const_cast<ObjectFile*>(OF));
2804d4e931SEric Christopher }
2904d4e931SEric Christopher 
3004d4e931SEric Christopher inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
3104d4e931SEric Christopher   return reinterpret_cast<section_iterator*>(SI);
3204d4e931SEric Christopher }
3304d4e931SEric Christopher 
3404d4e931SEric Christopher inline LLVMSectionIteratorRef
3504d4e931SEric Christopher wrap(const section_iterator *SI) {
3604d4e931SEric Christopher   return reinterpret_cast<LLVMSectionIteratorRef>
3704d4e931SEric Christopher     (const_cast<section_iterator*>(SI));
3804d4e931SEric Christopher }
3904d4e931SEric Christopher 
4004d4e931SEric Christopher inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) {
4104d4e931SEric Christopher   return reinterpret_cast<symbol_iterator*>(SI);
4204d4e931SEric Christopher }
4304d4e931SEric Christopher 
4404d4e931SEric Christopher inline LLVMSymbolIteratorRef
4504d4e931SEric Christopher wrap(const symbol_iterator *SI) {
4604d4e931SEric Christopher   return reinterpret_cast<LLVMSymbolIteratorRef>
4704d4e931SEric Christopher     (const_cast<symbol_iterator*>(SI));
4804d4e931SEric Christopher }
4904d4e931SEric Christopher 
5004d4e931SEric Christopher inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) {
5104d4e931SEric Christopher   return reinterpret_cast<relocation_iterator*>(SI);
5204d4e931SEric Christopher }
5304d4e931SEric Christopher 
5404d4e931SEric Christopher inline LLVMRelocationIteratorRef
5504d4e931SEric Christopher wrap(const relocation_iterator *SI) {
5604d4e931SEric Christopher   return reinterpret_cast<LLVMRelocationIteratorRef>
5704d4e931SEric Christopher     (const_cast<relocation_iterator*>(SI));
5804d4e931SEric Christopher }
5904d4e931SEric Christopher 
60f239db40SOwen Anderson // ObjectFile creation
61c56d2e8fSEric Christopher LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
6251cc3602SRafael Espindola   ErrorOr<ObjectFile*> ObjOrErr(ObjectFile::createObjectFile(unwrap(MemBuf)));
632617dcceSCraig Topper   ObjectFile *Obj = ObjOrErr ? ObjOrErr.get() : nullptr;
6451cc3602SRafael Espindola   return wrap(Obj);
65c56d2e8fSEric Christopher }
66c56d2e8fSEric Christopher 
67c56d2e8fSEric Christopher void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
68c56d2e8fSEric Christopher   delete unwrap(ObjectFile);
69c56d2e8fSEric Christopher }
70c56d2e8fSEric Christopher 
71f239db40SOwen Anderson // ObjectFile Section iterators
72c56d2e8fSEric Christopher LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
73b5155a57SRafael Espindola   section_iterator SI = unwrap(ObjectFile)->section_begin();
74e5fd0047SMichael J. Spencer   return wrap(new section_iterator(SI));
75c56d2e8fSEric Christopher }
76c56d2e8fSEric Christopher 
77c56d2e8fSEric Christopher void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
78c56d2e8fSEric Christopher   delete unwrap(SI);
79c56d2e8fSEric Christopher }
80c56d2e8fSEric Christopher 
81c56d2e8fSEric Christopher LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
82c56d2e8fSEric Christopher                                 LLVMSectionIteratorRef SI) {
83b5155a57SRafael Espindola   return (*unwrap(SI) == unwrap(ObjectFile)->section_end()) ? 1 : 0;
84c56d2e8fSEric Christopher }
85c56d2e8fSEric Christopher 
86c56d2e8fSEric Christopher void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
875e812afaSRafael Espindola   ++(*unwrap(SI));
88c56d2e8fSEric Christopher }
89c56d2e8fSEric Christopher 
9007bfdbb2SOwen Anderson void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
9107bfdbb2SOwen Anderson                                  LLVMSymbolIteratorRef Sym) {
92*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
9307bfdbb2SOwen Anderson     report_fatal_error(ec.message());
9407bfdbb2SOwen Anderson }
9507bfdbb2SOwen Anderson 
96f239db40SOwen Anderson // ObjectFile Symbol iterators
97f239db40SOwen Anderson LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
98b5155a57SRafael Espindola   symbol_iterator SI = unwrap(ObjectFile)->symbol_begin();
99f239db40SOwen Anderson   return wrap(new symbol_iterator(SI));
100f239db40SOwen Anderson }
101f239db40SOwen Anderson 
102f239db40SOwen Anderson void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
103f239db40SOwen Anderson   delete unwrap(SI);
104f239db40SOwen Anderson }
105f239db40SOwen Anderson 
106f239db40SOwen Anderson LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
107f239db40SOwen Anderson                                 LLVMSymbolIteratorRef SI) {
108b5155a57SRafael Espindola   return (*unwrap(SI) == unwrap(ObjectFile)->symbol_end()) ? 1 : 0;
109f239db40SOwen Anderson }
110f239db40SOwen Anderson 
111f239db40SOwen Anderson void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
1125e812afaSRafael Espindola   ++(*unwrap(SI));
113f239db40SOwen Anderson }
114f239db40SOwen Anderson 
115f239db40SOwen Anderson // SectionRef accessors
116c56d2e8fSEric Christopher const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
1171d6167fdSMichael J. Spencer   StringRef ret;
118*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(SI))->getName(ret))
1191d6167fdSMichael J. Spencer    report_fatal_error(ec.message());
1201d6167fdSMichael J. Spencer   return ret.data();
121c56d2e8fSEric Christopher }
122c56d2e8fSEric Christopher 
123c56d2e8fSEric Christopher uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
1241d6167fdSMichael J. Spencer   uint64_t ret;
125*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(SI))->getSize(ret))
1261d6167fdSMichael J. Spencer     report_fatal_error(ec.message());
1271d6167fdSMichael J. Spencer   return ret;
128c56d2e8fSEric Christopher }
129c56d2e8fSEric Christopher 
130c56d2e8fSEric Christopher const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
1311d6167fdSMichael J. Spencer   StringRef ret;
132*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(SI))->getContents(ret))
1331d6167fdSMichael J. Spencer     report_fatal_error(ec.message());
1341d6167fdSMichael J. Spencer   return ret.data();
135c56d2e8fSEric Christopher }
136f239db40SOwen Anderson 
137f239db40SOwen Anderson uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
138f239db40SOwen Anderson   uint64_t ret;
139*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
140f239db40SOwen Anderson     report_fatal_error(ec.message());
141f239db40SOwen Anderson   return ret;
142f239db40SOwen Anderson }
143f239db40SOwen Anderson 
144500ebeb8SOwen Anderson LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
145f239db40SOwen Anderson                                  LLVMSymbolIteratorRef Sym) {
146f239db40SOwen Anderson   bool ret;
147*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
148f239db40SOwen Anderson     report_fatal_error(ec.message());
149f239db40SOwen Anderson   return ret;
150f239db40SOwen Anderson }
151f239db40SOwen Anderson 
152e245af65SOwen Anderson // Section Relocation iterators
153e245af65SOwen Anderson LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
154b5155a57SRafael Espindola   relocation_iterator SI = (*unwrap(Section))->relocation_begin();
155e245af65SOwen Anderson   return wrap(new relocation_iterator(SI));
156e245af65SOwen Anderson }
157e245af65SOwen Anderson 
158e245af65SOwen Anderson void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
159e245af65SOwen Anderson   delete unwrap(SI);
160e245af65SOwen Anderson }
161e245af65SOwen Anderson 
162e245af65SOwen Anderson LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
163e245af65SOwen Anderson                                        LLVMRelocationIteratorRef SI) {
164b5155a57SRafael Espindola   return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
165e245af65SOwen Anderson }
166e245af65SOwen Anderson 
167e245af65SOwen Anderson void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
1685e812afaSRafael Espindola   ++(*unwrap(SI));
169e245af65SOwen Anderson }
170e245af65SOwen Anderson 
171e245af65SOwen Anderson 
172f239db40SOwen Anderson // SymbolRef accessors
173f239db40SOwen Anderson const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
174f239db40SOwen Anderson   StringRef ret;
175*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(SI))->getName(ret))
176f239db40SOwen Anderson     report_fatal_error(ec.message());
177f239db40SOwen Anderson   return ret.data();
178f239db40SOwen Anderson }
179f239db40SOwen Anderson 
180f239db40SOwen Anderson uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
181f239db40SOwen Anderson   uint64_t ret;
182*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
183f239db40SOwen Anderson     report_fatal_error(ec.message());
184f239db40SOwen Anderson   return ret;
185f239db40SOwen Anderson }
186f239db40SOwen Anderson 
187f239db40SOwen Anderson uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
188f239db40SOwen Anderson   uint64_t ret;
189*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(SI))->getSize(ret))
190f239db40SOwen Anderson     report_fatal_error(ec.message());
191f239db40SOwen Anderson   return ret;
192f239db40SOwen Anderson }
193f239db40SOwen Anderson 
194f7a89d06SOwen Anderson // RelocationRef accessors
195f7a89d06SOwen Anderson uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
196f7a89d06SOwen Anderson   uint64_t ret;
197*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(RI))->getAddress(ret))
198f7a89d06SOwen Anderson     report_fatal_error(ec.message());
199f7a89d06SOwen Anderson   return ret;
200f7a89d06SOwen Anderson }
201f7a89d06SOwen Anderson 
202cbe72fc9SDanil Malyshev uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
203cbe72fc9SDanil Malyshev   uint64_t ret;
204*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(RI))->getOffset(ret))
205cbe72fc9SDanil Malyshev     report_fatal_error(ec.message());
206cbe72fc9SDanil Malyshev   return ret;
207cbe72fc9SDanil Malyshev }
208cbe72fc9SDanil Malyshev 
209f7a89d06SOwen Anderson LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
210806f0064SRafael Espindola   symbol_iterator ret = (*unwrap(RI))->getSymbol();
211f7a89d06SOwen Anderson   return wrap(new symbol_iterator(ret));
212f7a89d06SOwen Anderson }
213f7a89d06SOwen Anderson 
214f7a89d06SOwen Anderson uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
215f7a89d06SOwen Anderson   uint64_t ret;
216*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(RI))->getType(ret))
217f7a89d06SOwen Anderson     report_fatal_error(ec.message());
218f7a89d06SOwen Anderson   return ret;
219f7a89d06SOwen Anderson }
220f7a89d06SOwen Anderson 
221f7a89d06SOwen Anderson // NOTE: Caller takes ownership of returned string.
222f7a89d06SOwen Anderson const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
223f7a89d06SOwen Anderson   SmallVector<char, 0> ret;
224*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(RI))->getTypeName(ret))
225f7a89d06SOwen Anderson     report_fatal_error(ec.message());
226f7a89d06SOwen Anderson 
227f7a89d06SOwen Anderson   char *str = static_cast<char*>(malloc(ret.size()));
228f7a89d06SOwen Anderson   std::copy(ret.begin(), ret.end(), str);
229f7a89d06SOwen Anderson   return str;
230f7a89d06SOwen Anderson }
231f7a89d06SOwen Anderson 
232f7a89d06SOwen Anderson // NOTE: Caller takes ownership of returned string.
233f7a89d06SOwen Anderson const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
234f7a89d06SOwen Anderson   SmallVector<char, 0> ret;
235*db4ed0bdSRafael Espindola   if (std::error_code ec = (*unwrap(RI))->getValueString(ret))
236f7a89d06SOwen Anderson     report_fatal_error(ec.message());
237f7a89d06SOwen Anderson 
238f7a89d06SOwen Anderson   char *str = static_cast<char*>(malloc(ret.size()));
239f7a89d06SOwen Anderson   std::copy(ret.begin(), ret.end(), str);
240f7a89d06SOwen Anderson   return str;
241f7a89d06SOwen Anderson }
242f7a89d06SOwen Anderson 
243