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.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm-c/Object.h"
17 #include "llvm/Object/ObjectFile.h"
18 
19 using namespace llvm;
20 using namespace object;
21 
unwrap(LLVMObjectFileRef OF)22 inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) {
23   return reinterpret_cast<OwningBinary<ObjectFile> *>(OF);
24 }
25 
wrap(const OwningBinary<ObjectFile> * OF)26 inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) {
27   return reinterpret_cast<LLVMObjectFileRef>(
28       const_cast<OwningBinary<ObjectFile> *>(OF));
29 }
30 
unwrap(LLVMSectionIteratorRef SI)31 inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
32   return reinterpret_cast<section_iterator*>(SI);
33 }
34 
35 inline LLVMSectionIteratorRef
wrap(const section_iterator * SI)36 wrap(const section_iterator *SI) {
37   return reinterpret_cast<LLVMSectionIteratorRef>
38     (const_cast<section_iterator*>(SI));
39 }
40 
unwrap(LLVMSymbolIteratorRef SI)41 inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) {
42   return reinterpret_cast<symbol_iterator*>(SI);
43 }
44 
45 inline LLVMSymbolIteratorRef
wrap(const symbol_iterator * SI)46 wrap(const symbol_iterator *SI) {
47   return reinterpret_cast<LLVMSymbolIteratorRef>
48     (const_cast<symbol_iterator*>(SI));
49 }
50 
unwrap(LLVMRelocationIteratorRef SI)51 inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) {
52   return reinterpret_cast<relocation_iterator*>(SI);
53 }
54 
55 inline LLVMRelocationIteratorRef
wrap(const relocation_iterator * SI)56 wrap(const relocation_iterator *SI) {
57   return reinterpret_cast<LLVMRelocationIteratorRef>
58     (const_cast<relocation_iterator*>(SI));
59 }
60 
61 // ObjectFile creation
LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf)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;
67   if (!ObjOrErr)
68     return nullptr;
69 
70   auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
71   return wrap(Ret);
72 }
73 
LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile)74 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
75   delete unwrap(ObjectFile);
76 }
77 
78 // ObjectFile Section iterators
LLVMGetSections(LLVMObjectFileRef OF)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));
83 }
84 
LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI)85 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
86   delete unwrap(SI);
87 }
88 
LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,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;
93 }
94 
LLVMMoveToNextSection(LLVMSectionIteratorRef SI)95 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
96   ++(*unwrap(SI));
97 }
98 
LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,LLVMSymbolIteratorRef Sym)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());
103 }
104 
105 // ObjectFile Symbol iterators
LLVMGetSymbols(LLVMObjectFileRef OF)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));
110 }
111 
LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI)112 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
113   delete unwrap(SI);
114 }
115 
LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,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;
120 }
121 
LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI)122 void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
123   ++(*unwrap(SI));
124 }
125 
126 // SectionRef accessors
LLVMGetSectionName(LLVMSectionIteratorRef SI)127 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
128   StringRef ret;
129   if (std::error_code ec = (*unwrap(SI))->getName(ret))
130    report_fatal_error(ec.message());
131   return ret.data();
132 }
133 
LLVMGetSectionSize(LLVMSectionIteratorRef SI)134 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
135   return (*unwrap(SI))->getSize();
136 }
137 
LLVMGetSectionContents(LLVMSectionIteratorRef SI)138 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
139   StringRef ret;
140   if (std::error_code ec = (*unwrap(SI))->getContents(ret))
141     report_fatal_error(ec.message());
142   return ret.data();
143 }
144 
LLVMGetSectionAddress(LLVMSectionIteratorRef SI)145 uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
146   return (*unwrap(SI))->getAddress();
147 }
148 
LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,LLVMSymbolIteratorRef Sym)149 LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
150                                  LLVMSymbolIteratorRef Sym) {
151   return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
152 }
153 
154 // Section Relocation iterators
LLVMGetRelocations(LLVMSectionIteratorRef Section)155 LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
156   relocation_iterator SI = (*unwrap(Section))->relocation_begin();
157   return wrap(new relocation_iterator(SI));
158 }
159 
LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI)160 void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
161   delete unwrap(SI);
162 }
163 
LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,LLVMRelocationIteratorRef SI)164 LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
165                                        LLVMRelocationIteratorRef SI) {
166   return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
167 }
168 
LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI)169 void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
170   ++(*unwrap(SI));
171 }
172 
173 
174 // SymbolRef accessors
LLVMGetSymbolName(LLVMSymbolIteratorRef SI)175 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
176   StringRef ret;
177   if (std::error_code ec = (*unwrap(SI))->getName(ret))
178     report_fatal_error(ec.message());
179   return ret.data();
180 }
181 
LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI)182 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
183   uint64_t ret;
184   if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
185     report_fatal_error(ec.message());
186   return ret;
187 }
188 
LLVMGetSymbolSize(LLVMSymbolIteratorRef SI)189 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
190   uint64_t ret;
191   if (std::error_code ec = (*unwrap(SI))->getSize(ret))
192     report_fatal_error(ec.message());
193   return ret;
194 }
195 
196 // RelocationRef accessors
LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI)197 uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
198   uint64_t ret;
199   if (std::error_code ec = (*unwrap(RI))->getAddress(ret))
200     report_fatal_error(ec.message());
201   return ret;
202 }
203 
LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI)204 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
205   uint64_t ret;
206   if (std::error_code ec = (*unwrap(RI))->getOffset(ret))
207     report_fatal_error(ec.message());
208   return ret;
209 }
210 
LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI)211 LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
212   symbol_iterator ret = (*unwrap(RI))->getSymbol();
213   return wrap(new symbol_iterator(ret));
214 }
215 
LLVMGetRelocationType(LLVMRelocationIteratorRef RI)216 uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
217   uint64_t ret;
218   if (std::error_code ec = (*unwrap(RI))->getType(ret))
219     report_fatal_error(ec.message());
220   return ret;
221 }
222 
223 // NOTE: Caller takes ownership of returned string.
LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI)224 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
225   SmallVector<char, 0> ret;
226   if (std::error_code ec = (*unwrap(RI))->getTypeName(ret))
227     report_fatal_error(ec.message());
228 
229   char *str = static_cast<char*>(malloc(ret.size()));
230   std::copy(ret.begin(), ret.end(), str);
231   return str;
232 }
233 
234 // NOTE: Caller takes ownership of returned string.
LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI)235 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
236   SmallVector<char, 0> ret;
237   if (std::error_code ec = (*unwrap(RI))->getValueString(ret))
238     report_fatal_error(ec.message());
239 
240   char *str = static_cast<char*>(malloc(ret.size()));
241   std::copy(ret.begin(), ret.end(), str);
242   return str;
243 }
244 
245