LLVM  3.7.0
Object.cpp
Go to the documentation of this file.
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 
22 inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) {
23  return reinterpret_cast<OwningBinary<ObjectFile> *>(OF);
24 }
25 
26 inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) {
27  return reinterpret_cast<LLVMObjectFileRef>(
28  const_cast<OwningBinary<ObjectFile> *>(OF));
29 }
30 
32  return reinterpret_cast<section_iterator*>(SI);
33 }
34 
36 wrap(const section_iterator *SI) {
37  return reinterpret_cast<LLVMSectionIteratorRef>
38  (const_cast<section_iterator*>(SI));
39 }
40 
42  return reinterpret_cast<symbol_iterator*>(SI);
43 }
44 
46 wrap(const symbol_iterator *SI) {
47  return reinterpret_cast<LLVMSymbolIteratorRef>
48  (const_cast<symbol_iterator*>(SI));
49 }
50 
52  return reinterpret_cast<relocation_iterator*>(SI);
53 }
54 
57  return reinterpret_cast<LLVMRelocationIteratorRef>
58  (const_cast<relocation_iterator*>(SI));
59 }
60 
61 // ObjectFile creation
63  std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
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 
75  delete unwrap(ObjectFile);
76 }
77 
78 // ObjectFile Section iterators
80  OwningBinary<ObjectFile> *OB = unwrap(OF);
81  section_iterator SI = OB->getBinary()->section_begin();
82  return wrap(new section_iterator(SI));
83 }
84 
86  delete unwrap(SI);
87 }
88 
91  OwningBinary<ObjectFile> *OB = unwrap(OF);
92  return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
93 }
94 
96  ++(*unwrap(SI));
97 }
98 
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
107  OwningBinary<ObjectFile> *OB = unwrap(OF);
108  symbol_iterator SI = OB->getBinary()->symbol_begin();
109  return wrap(new symbol_iterator(SI));
110 }
111 
113  delete unwrap(SI);
114 }
115 
118  OwningBinary<ObjectFile> *OB = unwrap(OF);
119  return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
120 }
121 
123  ++(*unwrap(SI));
124 }
125 
126 // SectionRef accessors
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 
135  return (*unwrap(SI))->getSize();
136 }
137 
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 
146  return (*unwrap(SI))->getAddress();
147 }
148 
150  LLVMSymbolIteratorRef Sym) {
151  return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
152 }
153 
154 // Section Relocation iterators
156  relocation_iterator SI = (*unwrap(Section))->relocation_begin();
157  return wrap(new relocation_iterator(SI));
158 }
159 
161  delete unwrap(SI);
162 }
163 
166  return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
167 }
168 
170  ++(*unwrap(SI));
171 }
172 
173 
174 // SymbolRef accessors
176  ErrorOr<StringRef> Ret = (*unwrap(SI))->getName();
177  if (std::error_code EC = Ret.getError())
178  report_fatal_error(EC.message());
179  return Ret->data();
180 }
181 
183  ErrorOr<uint64_t> Ret = (*unwrap(SI))->getAddress();
184  if (std::error_code EC = Ret.getError())
185  report_fatal_error(EC.message());
186  return *Ret;
187 }
188 
190  return (*unwrap(SI))->getCommonSize();
191 }
192 
193 // RelocationRef accessors
195  return (*unwrap(RI))->getOffset();
196 }
197 
199  symbol_iterator ret = (*unwrap(RI))->getSymbol();
200  return wrap(new symbol_iterator(ret));
201 }
202 
204  return (*unwrap(RI))->getType();
205 }
206 
207 // NOTE: Caller takes ownership of returned string.
210  (*unwrap(RI))->getTypeName(ret);
211  char *str = static_cast<char*>(malloc(ret.size()));
212  std::copy(ret.begin(), ret.end(), str);
213  return str;
214 }
215 
216 // NOTE: Caller takes ownership of returned string.
218  return strdup("");
219 }
220 
std::error_code getError() const
Definition: ErrorOr.h:178
Represents either an error or a value T.
Definition: ErrorOr.h:82
uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI)
Definition: Object.cpp:145
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
Used to pass regions of memory through LLVM interfaces.
Definition: Support.h:36
LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI, LLVMSymbolIteratorRef Sym)
Definition: Object.cpp:149
This class is the base class for all object file types.
Definition: ObjectFile.h:176
LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf)
Definition: Object.cpp:62
const char * LLVMGetSectionContents(LLVMSectionIteratorRef SI)
Definition: Object.cpp:138
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section, LLVMRelocationIteratorRef SI)
Definition: Object.cpp:164
static StringRef getName(Value *V)
void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI)
Definition: Object.cpp:112
void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect, LLVMSymbolIteratorRef Sym)
Definition: Object.cpp:99
LLVMTargetDataRef wrap(const DataLayout *P)
Definition: DataLayout.h:469
struct LLVMOpaqueSectionIterator * LLVMSectionIteratorRef
Definition: Object.h:38
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:107
void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI)
Definition: Object.cpp:85
void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI)
Definition: Object.cpp:169
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:35
LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI)
Definition: Object.cpp:198
DataLayout * unwrap(LLVMTargetDataRef P)
Definition: DataLayout.h:465
uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI)
Definition: Object.cpp:134
struct LLVMOpaqueSymbolIterator * LLVMSymbolIteratorRef
Definition: Object.h:39
struct LLVMOpaqueObjectFile * LLVMObjectFileRef
Definition: Object.h:37
LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF, LLVMSectionIteratorRef SI)
Definition: Object.cpp:89
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:65
int LLVMBool
Definition: Support.h:29
LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF)
Definition: Object.cpp:106
LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section)
Definition: Object.cpp:155
const char * LLVMGetSectionName(LLVMSectionIteratorRef SI)
Definition: Object.cpp:127
const char * LLVMGetSymbolName(LLVMSymbolIteratorRef SI)
Definition: Object.cpp:175
void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI)
Definition: Object.cpp:160
LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF)
Definition: Object.cpp:79
void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI)
Definition: Object.cpp:122
uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI)
Definition: Object.cpp:182
const char * LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI)
Definition: Object.cpp:208
struct LLVMOpaqueRelocationIterator * LLVMRelocationIteratorRef
Definition: Object.h:40
uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI)
Definition: Object.cpp:194
uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI)
Definition: Object.cpp:203
void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile)
Definition: Object.cpp:74
LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF, LLVMSymbolIteratorRef SI)
Definition: Object.cpp:116
static ErrorOr< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:102
uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI)
Definition: Object.cpp:189
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
const char * LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI)
Definition: Object.cpp:217
void LLVMMoveToNextSection(LLVMSectionIteratorRef SI)
Definition: Object.cpp:95
reference get()
Definition: ErrorOr.h:175