LLVM  8.0.1
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-c/Object.h"
16 #include "llvm/ADT/SmallVector.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 
37  return reinterpret_cast<LLVMSectionIteratorRef>
38  (const_cast<section_iterator*>(SI));
39 }
40 
41 inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) {
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  // TODO: Actually report errors helpfully.
69  consumeError(ObjOrErr.takeError());
70  return nullptr;
71  }
72 
73  auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
74  return wrap(Ret);
75 }
76 
78  delete unwrap(ObjectFile);
79 }
80 
81 // ObjectFile Section iterators
83  OwningBinary<ObjectFile> *OB = unwrap(OF);
84  section_iterator SI = OB->getBinary()->section_begin();
85  return wrap(new section_iterator(SI));
86 }
87 
89  delete unwrap(SI);
90 }
91 
94  OwningBinary<ObjectFile> *OB = unwrap(OF);
95  return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
96 }
97 
99  ++(*unwrap(SI));
100 }
101 
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);
111  }
112  *unwrap(Sect) = *SecOrErr;
113 }
114 
115 // ObjectFile Symbol iterators
117  OwningBinary<ObjectFile> *OB = unwrap(OF);
118  symbol_iterator SI = OB->getBinary()->symbol_begin();
119  return wrap(new symbol_iterator(SI));
120 }
121 
123  delete unwrap(SI);
124 }
125 
128  OwningBinary<ObjectFile> *OB = unwrap(OF);
129  return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
130 }
131 
133  ++(*unwrap(SI));
134 }
135 
136 // SectionRef accessors
138  StringRef ret;
139  if (std::error_code ec = (*unwrap(SI))->getName(ret))
140  report_fatal_error(ec.message());
141  return ret.data();
142 }
143 
145  return (*unwrap(SI))->getSize();
146 }
147 
149  StringRef ret;
150  if (std::error_code ec = (*unwrap(SI))->getContents(ret))
151  report_fatal_error(ec.message());
152  return ret.data();
153 }
154 
156  return (*unwrap(SI))->getAddress();
157 }
158 
160  LLVMSymbolIteratorRef Sym) {
161  return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
162 }
163 
164 // Section Relocation iterators
166  relocation_iterator SI = (*unwrap(Section))->relocation_begin();
167  return wrap(new relocation_iterator(SI));
168 }
169 
171  delete unwrap(SI);
172 }
173 
176  return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
177 }
178 
180  ++(*unwrap(SI));
181 }
182 
183 
184 // SymbolRef accessors
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);
193  }
194  return Ret->data();
195 }
196 
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);
205  }
206  return *Ret;
207 }
208 
210  return (*unwrap(SI))->getCommonSize();
211 }
212 
213 // RelocationRef accessors
215  return (*unwrap(RI))->getOffset();
216 }
217 
219  symbol_iterator ret = (*unwrap(RI))->getSymbol();
220  return wrap(new symbol_iterator(ret));
221 }
222 
224  return (*unwrap(RI))->getType();
225 }
226 
227 // NOTE: Caller takes ownership of returned string.
230  (*unwrap(RI))->getTypeName(ret);
231  char *str = static_cast<char*>(safe_malloc(ret.size()));
232  llvm::copy(ret, str);
233  return str;
234 }
235 
236 // NOTE: Caller takes ownership of returned string.
238  return strdup("");
239 }
240 
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:161
uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI)
Definition: Object.cpp:155
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:49
LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI, LLVMSymbolIteratorRef Sym)
Definition: Object.cpp:159
StringRef getTypeName()
We provide a function which tries to compute the (demangled) name of a type statically.
Definition: TypeName.h:28
LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf)
Definition: Object.cpp:62
Error takeError()
Take ownership of the stored error.
Definition: Error.h:553
const char * LLVMGetSectionContents(LLVMSectionIteratorRef SI)
Definition: Object.cpp:148
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:195
LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section, LLVMRelocationIteratorRef SI)
Definition: Object.cpp:174
static StringRef getName(Value *V)
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI)
Definition: Object.cpp:122
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:275
void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect, LLVMSymbolIteratorRef Sym)
Definition: Object.cpp:102
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:48
struct LLVMOpaqueSectionIterator * LLVMSectionIteratorRef
Definition: Object.h:38
void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI)
Definition: Object.cpp:88
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: ELF.h:337
void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI)
Definition: Object.cpp:179
LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI)
Definition: Object.cpp:218
uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI)
Definition: Object.cpp:144
struct LLVMOpaqueSymbolIterator * LLVMSymbolIteratorRef
Definition: Object.h:39
struct LLVMOpaqueObjectFile * LLVMObjectFileRef
Definition: Object.h:37
LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF, LLVMSectionIteratorRef SI)
Definition: Object.cpp:92
int LLVMBool
Definition: Types.h:29
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:982
size_t size() const
Definition: SmallVector.h:53
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:62
LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF)
Definition: Object.cpp:116
LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section)
Definition: Object.cpp:165
const char * LLVMGetSectionName(LLVMSectionIteratorRef SI)
Definition: Object.cpp:137
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition: MemAlloc.h:26
const char * LLVMGetSymbolName(LLVMSymbolIteratorRef SI)
Definition: Object.cpp:185
reference get()
Returns a reference to the stored T value.
Definition: Error.h:533
void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI)
Definition: Object.cpp:170
LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF)
Definition: Object.cpp:82
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:190
void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI)
Definition: Object.cpp:132
uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI)
Definition: Object.cpp:197
const char * LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI)
Definition: Object.cpp:228
struct LLVMOpaqueRelocationIterator * LLVMRelocationIteratorRef
Definition: Object.h:40
uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI)
Definition: Object.cpp:214
uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI)
Definition: Object.cpp:223
void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile)
Definition: Object.cpp:77
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:483
LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF, LLVMSymbolIteratorRef SI)
Definition: Object.cpp:126
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:77
uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI)
Definition: Object.cpp:209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
const char * LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI)
Definition: Object.cpp:237
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1238
void LLVMMoveToNextSection(LLVMSectionIteratorRef SI)
Definition: Object.cpp:98