xref: /src/contrib/llvm-project/llvm/lib/Object/Object.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
16b943ff3SDimitry Andric //===- Object.cpp - C bindings to the object file library--------*- C++ -*-===//
26b943ff3SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66b943ff3SDimitry Andric //
76b943ff3SDimitry Andric //===----------------------------------------------------------------------===//
86b943ff3SDimitry Andric //
96b943ff3SDimitry Andric // This file defines the C bindings to the file-format-independent object
106b943ff3SDimitry Andric // library.
116b943ff3SDimitry Andric //
126b943ff3SDimitry Andric //===----------------------------------------------------------------------===//
136b943ff3SDimitry Andric 
146b943ff3SDimitry Andric #include "llvm-c/Object.h"
157ab83427SDimitry Andric #include "llvm/ADT/SmallVector.h"
16e6d15924SDimitry Andric #include "llvm/IR/LLVMContext.h"
175ca98fd9SDimitry Andric #include "llvm/Object/ObjectFile.h"
18e6d15924SDimitry Andric #include "llvm/Object/MachOUniversal.h"
196f8fc217SDimitry Andric #include "llvm/Support/MemAlloc.h"
206b943ff3SDimitry Andric 
216b943ff3SDimitry Andric using namespace llvm;
226b943ff3SDimitry Andric using namespace object;
236b943ff3SDimitry Andric 
unwrap(LLVMObjectFileRef OF)2467c32a98SDimitry Andric inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) {
2567c32a98SDimitry Andric   return reinterpret_cast<OwningBinary<ObjectFile> *>(OF);
2659d6cff9SDimitry Andric }
2759d6cff9SDimitry Andric 
wrap(const OwningBinary<ObjectFile> * OF)2867c32a98SDimitry Andric inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) {
2967c32a98SDimitry Andric   return reinterpret_cast<LLVMObjectFileRef>(
3067c32a98SDimitry Andric       const_cast<OwningBinary<ObjectFile> *>(OF));
3159d6cff9SDimitry Andric }
3259d6cff9SDimitry Andric 
unwrap(LLVMSectionIteratorRef SI)3359d6cff9SDimitry Andric inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
3459d6cff9SDimitry Andric   return reinterpret_cast<section_iterator*>(SI);
3559d6cff9SDimitry Andric }
3659d6cff9SDimitry Andric 
3759d6cff9SDimitry Andric inline LLVMSectionIteratorRef
wrap(const section_iterator * SI)3859d6cff9SDimitry Andric wrap(const section_iterator *SI) {
3959d6cff9SDimitry Andric   return reinterpret_cast<LLVMSectionIteratorRef>
4059d6cff9SDimitry Andric     (const_cast<section_iterator*>(SI));
4159d6cff9SDimitry Andric }
4259d6cff9SDimitry Andric 
unwrap(LLVMSymbolIteratorRef SI)4359d6cff9SDimitry Andric inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) {
4459d6cff9SDimitry Andric   return reinterpret_cast<symbol_iterator*>(SI);
4559d6cff9SDimitry Andric }
4659d6cff9SDimitry Andric 
4759d6cff9SDimitry Andric inline LLVMSymbolIteratorRef
wrap(const symbol_iterator * SI)4859d6cff9SDimitry Andric wrap(const symbol_iterator *SI) {
4959d6cff9SDimitry Andric   return reinterpret_cast<LLVMSymbolIteratorRef>
5059d6cff9SDimitry Andric     (const_cast<symbol_iterator*>(SI));
5159d6cff9SDimitry Andric }
5259d6cff9SDimitry Andric 
unwrap(LLVMRelocationIteratorRef SI)5359d6cff9SDimitry Andric inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) {
5459d6cff9SDimitry Andric   return reinterpret_cast<relocation_iterator*>(SI);
5559d6cff9SDimitry Andric }
5659d6cff9SDimitry Andric 
5759d6cff9SDimitry Andric inline LLVMRelocationIteratorRef
wrap(const relocation_iterator * SI)5859d6cff9SDimitry Andric wrap(const relocation_iterator *SI) {
5959d6cff9SDimitry Andric   return reinterpret_cast<LLVMRelocationIteratorRef>
6059d6cff9SDimitry Andric     (const_cast<relocation_iterator*>(SI));
6159d6cff9SDimitry Andric }
6259d6cff9SDimitry Andric 
63e6d15924SDimitry Andric /*--.. Operations on binary files ..........................................--*/
64e6d15924SDimitry Andric 
LLVMCreateBinary(LLVMMemoryBufferRef MemBuf,LLVMContextRef Context,char ** ErrorMessage)65e6d15924SDimitry Andric LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf,
66e6d15924SDimitry Andric                                LLVMContextRef Context,
67e6d15924SDimitry Andric                                char **ErrorMessage) {
68e6d15924SDimitry Andric   auto maybeContext = Context ? unwrap(Context) : nullptr;
69e6d15924SDimitry Andric   Expected<std::unique_ptr<Binary>> ObjOrErr(
70e6d15924SDimitry Andric       createBinary(unwrap(MemBuf)->getMemBufferRef(), maybeContext));
71e6d15924SDimitry Andric   if (!ObjOrErr) {
72e6d15924SDimitry Andric     *ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str());
73e6d15924SDimitry Andric     return nullptr;
74e6d15924SDimitry Andric   }
75e6d15924SDimitry Andric 
76e6d15924SDimitry Andric   return wrap(ObjOrErr.get().release());
77e6d15924SDimitry Andric }
78e6d15924SDimitry Andric 
LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR)79e6d15924SDimitry Andric LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR) {
80e6d15924SDimitry Andric   auto Buf = unwrap(BR)->getMemoryBufferRef();
81e6d15924SDimitry Andric   return wrap(llvm::MemoryBuffer::getMemBuffer(
82e6d15924SDimitry Andric                 Buf.getBuffer(), Buf.getBufferIdentifier(),
83e6d15924SDimitry Andric                 /*RequiresNullTerminator*/false).release());
84e6d15924SDimitry Andric }
85e6d15924SDimitry Andric 
LLVMDisposeBinary(LLVMBinaryRef BR)86e6d15924SDimitry Andric void LLVMDisposeBinary(LLVMBinaryRef BR) {
87e6d15924SDimitry Andric   delete unwrap(BR);
88e6d15924SDimitry Andric }
89e6d15924SDimitry Andric 
LLVMBinaryGetType(LLVMBinaryRef BR)90e6d15924SDimitry Andric LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR) {
91e6d15924SDimitry Andric   class BinaryTypeMapper final : public Binary {
92e6d15924SDimitry Andric   public:
93e6d15924SDimitry Andric     static LLVMBinaryType mapBinaryTypeToLLVMBinaryType(unsigned Kind) {
94e6d15924SDimitry Andric       switch (Kind) {
95e6d15924SDimitry Andric       case ID_Archive:
96e6d15924SDimitry Andric         return LLVMBinaryTypeArchive;
97e6d15924SDimitry Andric       case ID_MachOUniversalBinary:
98e6d15924SDimitry Andric         return LLVMBinaryTypeMachOUniversalBinary;
99e6d15924SDimitry Andric       case ID_COFFImportFile:
100e6d15924SDimitry Andric         return LLVMBinaryTypeCOFFImportFile;
101e6d15924SDimitry Andric       case ID_IR:
102e6d15924SDimitry Andric         return LLVMBinaryTypeIR;
103e6d15924SDimitry Andric       case ID_WinRes:
104e6d15924SDimitry Andric         return LLVMBinaryTypeWinRes;
105e6d15924SDimitry Andric       case ID_COFF:
106e6d15924SDimitry Andric         return LLVMBinaryTypeCOFF;
107e6d15924SDimitry Andric       case ID_ELF32L:
108e6d15924SDimitry Andric         return LLVMBinaryTypeELF32L;
109e6d15924SDimitry Andric       case ID_ELF32B:
110e6d15924SDimitry Andric         return LLVMBinaryTypeELF32B;
111e6d15924SDimitry Andric       case ID_ELF64L:
112e6d15924SDimitry Andric         return LLVMBinaryTypeELF64L;
113e6d15924SDimitry Andric       case ID_ELF64B:
114e6d15924SDimitry Andric         return LLVMBinaryTypeELF64B;
115e6d15924SDimitry Andric       case ID_MachO32L:
116e6d15924SDimitry Andric         return LLVMBinaryTypeMachO32L;
117e6d15924SDimitry Andric       case ID_MachO32B:
118e6d15924SDimitry Andric         return LLVMBinaryTypeMachO32B;
119e6d15924SDimitry Andric       case ID_MachO64L:
120e6d15924SDimitry Andric         return LLVMBinaryTypeMachO64L;
121e6d15924SDimitry Andric       case ID_MachO64B:
122e6d15924SDimitry Andric         return LLVMBinaryTypeMachO64B;
123145449b1SDimitry Andric       case ID_Offload:
124145449b1SDimitry Andric         return LLVMBinaryTypeOffload;
125e6d15924SDimitry Andric       case ID_Wasm:
126e6d15924SDimitry Andric         return LLVMBinaryTypeWasm;
127e6d15924SDimitry Andric       case ID_StartObjects:
128e6d15924SDimitry Andric       case ID_EndObjects:
129e6d15924SDimitry Andric         llvm_unreachable("Marker types are not valid binary kinds!");
130e6d15924SDimitry Andric       default:
131e6d15924SDimitry Andric         llvm_unreachable("Unknown binary kind!");
132e6d15924SDimitry Andric       }
133e6d15924SDimitry Andric     }
134e6d15924SDimitry Andric   };
135e6d15924SDimitry Andric   return BinaryTypeMapper::mapBinaryTypeToLLVMBinaryType(unwrap(BR)->getType());
136e6d15924SDimitry Andric }
137e6d15924SDimitry Andric 
LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR,const char * Arch,size_t ArchLen,char ** ErrorMessage)138e6d15924SDimitry Andric LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR,
139e6d15924SDimitry Andric                                                         const char *Arch,
140e6d15924SDimitry Andric                                                         size_t ArchLen,
141e6d15924SDimitry Andric                                                         char **ErrorMessage) {
142e6d15924SDimitry Andric   auto universal = cast<MachOUniversalBinary>(unwrap(BR));
143e6d15924SDimitry Andric   Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
1441d5ae102SDimitry Andric       universal->getMachOObjectForArch({Arch, ArchLen}));
145e6d15924SDimitry Andric   if (!ObjOrErr) {
146e6d15924SDimitry Andric     *ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str());
147e6d15924SDimitry Andric     return nullptr;
148e6d15924SDimitry Andric   }
149e6d15924SDimitry Andric   return wrap(ObjOrErr.get().release());
150e6d15924SDimitry Andric }
151e6d15924SDimitry Andric 
LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR)152e6d15924SDimitry Andric LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR) {
153e6d15924SDimitry Andric   auto OF = cast<ObjectFile>(unwrap(BR));
154e6d15924SDimitry Andric   auto sections = OF->sections();
155e6d15924SDimitry Andric   if (sections.begin() == sections.end())
156e6d15924SDimitry Andric     return nullptr;
157e6d15924SDimitry Andric   return wrap(new section_iterator(sections.begin()));
158e6d15924SDimitry Andric }
159e6d15924SDimitry Andric 
LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,LLVMSectionIteratorRef SI)160e6d15924SDimitry Andric LLVMBool LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,
161e6d15924SDimitry Andric                                               LLVMSectionIteratorRef SI) {
162e6d15924SDimitry Andric   auto OF = cast<ObjectFile>(unwrap(BR));
163e6d15924SDimitry Andric   return (*unwrap(SI) == OF->section_end()) ? 1 : 0;
164e6d15924SDimitry Andric }
165e6d15924SDimitry Andric 
LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR)166e6d15924SDimitry Andric LLVMSymbolIteratorRef LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR) {
167e6d15924SDimitry Andric   auto OF = cast<ObjectFile>(unwrap(BR));
168e6d15924SDimitry Andric   auto symbols = OF->symbols();
169e6d15924SDimitry Andric   if (symbols.begin() == symbols.end())
170e6d15924SDimitry Andric     return nullptr;
171e6d15924SDimitry Andric   return wrap(new symbol_iterator(symbols.begin()));
172e6d15924SDimitry Andric }
173e6d15924SDimitry Andric 
LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,LLVMSymbolIteratorRef SI)174e6d15924SDimitry Andric LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
175e6d15924SDimitry Andric                                              LLVMSymbolIteratorRef SI) {
176e6d15924SDimitry Andric   auto OF = cast<ObjectFile>(unwrap(BR));
177e6d15924SDimitry Andric   return (*unwrap(SI) == OF->symbol_end()) ? 1 : 0;
178e6d15924SDimitry Andric }
179e6d15924SDimitry Andric 
18063faed5bSDimitry Andric // ObjectFile creation
LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf)1816b943ff3SDimitry Andric LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
1825ca98fd9SDimitry Andric   std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
18301095a5dSDimitry Andric   Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
18467c32a98SDimitry Andric       ObjectFile::createObjectFile(Buf->getMemBufferRef()));
18567c32a98SDimitry Andric   std::unique_ptr<ObjectFile> Obj;
18601095a5dSDimitry Andric   if (!ObjOrErr) {
18701095a5dSDimitry Andric     // TODO: Actually report errors helpfully.
18801095a5dSDimitry Andric     consumeError(ObjOrErr.takeError());
18967c32a98SDimitry Andric     return nullptr;
19001095a5dSDimitry Andric   }
19167c32a98SDimitry Andric 
19267c32a98SDimitry Andric   auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
19367c32a98SDimitry Andric   return wrap(Ret);
1946b943ff3SDimitry Andric }
1956b943ff3SDimitry Andric 
LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile)1966b943ff3SDimitry Andric void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
1976b943ff3SDimitry Andric   delete unwrap(ObjectFile);
1986b943ff3SDimitry Andric }
1996b943ff3SDimitry Andric 
20063faed5bSDimitry Andric // ObjectFile Section iterators
LLVMGetSections(LLVMObjectFileRef OF)20167c32a98SDimitry Andric LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) {
20267c32a98SDimitry Andric   OwningBinary<ObjectFile> *OB = unwrap(OF);
20367c32a98SDimitry Andric   section_iterator SI = OB->getBinary()->section_begin();
20430815c53SDimitry Andric   return wrap(new section_iterator(SI));
2056b943ff3SDimitry Andric }
2066b943ff3SDimitry Andric 
LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI)2076b943ff3SDimitry Andric void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
2086b943ff3SDimitry Andric   delete unwrap(SI);
2096b943ff3SDimitry Andric }
2106b943ff3SDimitry Andric 
LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,LLVMSectionIteratorRef SI)21167c32a98SDimitry Andric LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,
2126b943ff3SDimitry Andric                                     LLVMSectionIteratorRef SI) {
21367c32a98SDimitry Andric   OwningBinary<ObjectFile> *OB = unwrap(OF);
21467c32a98SDimitry Andric   return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
2156b943ff3SDimitry Andric }
2166b943ff3SDimitry Andric 
LLVMMoveToNextSection(LLVMSectionIteratorRef SI)2176b943ff3SDimitry Andric void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
2185ca98fd9SDimitry Andric   ++(*unwrap(SI));
2196b943ff3SDimitry Andric }
2206b943ff3SDimitry Andric 
LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,LLVMSymbolIteratorRef Sym)22163faed5bSDimitry Andric void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
22263faed5bSDimitry Andric                                  LLVMSymbolIteratorRef Sym) {
22301095a5dSDimitry Andric   Expected<section_iterator> SecOrErr = (*unwrap(Sym))->getSection();
22401095a5dSDimitry Andric   if (!SecOrErr) {
22501095a5dSDimitry Andric    std::string Buf;
22601095a5dSDimitry Andric    raw_string_ostream OS(Buf);
227d8e91e46SDimitry Andric    logAllUnhandledErrors(SecOrErr.takeError(), OS);
228c0981da4SDimitry Andric    report_fatal_error(Twine(OS.str()));
22901095a5dSDimitry Andric   }
230dd58ef01SDimitry Andric   *unwrap(Sect) = *SecOrErr;
23163faed5bSDimitry Andric }
23263faed5bSDimitry Andric 
23363faed5bSDimitry Andric // ObjectFile Symbol iterators
LLVMGetSymbols(LLVMObjectFileRef OF)23467c32a98SDimitry Andric LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) {
23567c32a98SDimitry Andric   OwningBinary<ObjectFile> *OB = unwrap(OF);
23667c32a98SDimitry Andric   symbol_iterator SI = OB->getBinary()->symbol_begin();
23763faed5bSDimitry Andric   return wrap(new symbol_iterator(SI));
23863faed5bSDimitry Andric }
23963faed5bSDimitry Andric 
LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI)24063faed5bSDimitry Andric void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
24163faed5bSDimitry Andric   delete unwrap(SI);
24263faed5bSDimitry Andric }
24363faed5bSDimitry Andric 
LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,LLVMSymbolIteratorRef SI)24467c32a98SDimitry Andric LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,
24563faed5bSDimitry Andric                                    LLVMSymbolIteratorRef SI) {
24667c32a98SDimitry Andric   OwningBinary<ObjectFile> *OB = unwrap(OF);
24767c32a98SDimitry Andric   return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
24863faed5bSDimitry Andric }
24963faed5bSDimitry Andric 
LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI)25063faed5bSDimitry Andric void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
2515ca98fd9SDimitry Andric   ++(*unwrap(SI));
25263faed5bSDimitry Andric }
25363faed5bSDimitry Andric 
25463faed5bSDimitry Andric // SectionRef accessors
LLVMGetSectionName(LLVMSectionIteratorRef SI)2556b943ff3SDimitry Andric const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
2561d5ae102SDimitry Andric   auto NameOrErr = (*unwrap(SI))->getName();
2571d5ae102SDimitry Andric   if (!NameOrErr)
2581d5ae102SDimitry Andric     report_fatal_error(NameOrErr.takeError());
2591d5ae102SDimitry Andric   return NameOrErr->data();
2606b943ff3SDimitry Andric }
2616b943ff3SDimitry Andric 
LLVMGetSectionSize(LLVMSectionIteratorRef SI)2626b943ff3SDimitry Andric uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
26367c32a98SDimitry Andric   return (*unwrap(SI))->getSize();
2646b943ff3SDimitry Andric }
2656b943ff3SDimitry Andric 
LLVMGetSectionContents(LLVMSectionIteratorRef SI)2666b943ff3SDimitry Andric const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
267e6d15924SDimitry Andric   if (Expected<StringRef> E = (*unwrap(SI))->getContents())
268e6d15924SDimitry Andric     return E->data();
269e6d15924SDimitry Andric   else
270e6d15924SDimitry Andric     report_fatal_error(E.takeError());
2716b943ff3SDimitry Andric }
27263faed5bSDimitry Andric 
LLVMGetSectionAddress(LLVMSectionIteratorRef SI)27363faed5bSDimitry Andric uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
27467c32a98SDimitry Andric   return (*unwrap(SI))->getAddress();
27563faed5bSDimitry Andric }
27663faed5bSDimitry Andric 
LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,LLVMSymbolIteratorRef Sym)27763faed5bSDimitry Andric LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
27863faed5bSDimitry Andric                                  LLVMSymbolIteratorRef Sym) {
27967c32a98SDimitry Andric   return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
28063faed5bSDimitry Andric }
28163faed5bSDimitry Andric 
28263faed5bSDimitry Andric // Section Relocation iterators
LLVMGetRelocations(LLVMSectionIteratorRef Section)28363faed5bSDimitry Andric LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
2845ca98fd9SDimitry Andric   relocation_iterator SI = (*unwrap(Section))->relocation_begin();
28563faed5bSDimitry Andric   return wrap(new relocation_iterator(SI));
28663faed5bSDimitry Andric }
28763faed5bSDimitry Andric 
LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI)28863faed5bSDimitry Andric void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
28963faed5bSDimitry Andric   delete unwrap(SI);
29063faed5bSDimitry Andric }
29163faed5bSDimitry Andric 
LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,LLVMRelocationIteratorRef SI)29263faed5bSDimitry Andric LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
29363faed5bSDimitry Andric                                        LLVMRelocationIteratorRef SI) {
2945ca98fd9SDimitry Andric   return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
29563faed5bSDimitry Andric }
29663faed5bSDimitry Andric 
LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI)29763faed5bSDimitry Andric void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
2985ca98fd9SDimitry Andric   ++(*unwrap(SI));
29963faed5bSDimitry Andric }
30063faed5bSDimitry Andric 
30163faed5bSDimitry Andric 
30263faed5bSDimitry Andric // SymbolRef accessors
LLVMGetSymbolName(LLVMSymbolIteratorRef SI)30363faed5bSDimitry Andric const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
30401095a5dSDimitry Andric   Expected<StringRef> Ret = (*unwrap(SI))->getName();
30501095a5dSDimitry Andric   if (!Ret) {
30601095a5dSDimitry Andric     std::string Buf;
30701095a5dSDimitry Andric     raw_string_ostream OS(Buf);
308d8e91e46SDimitry Andric     logAllUnhandledErrors(Ret.takeError(), OS);
309c0981da4SDimitry Andric     report_fatal_error(Twine(OS.str()));
31001095a5dSDimitry Andric   }
3111a82d4c0SDimitry Andric   return Ret->data();
31263faed5bSDimitry Andric }
31363faed5bSDimitry Andric 
LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI)31463faed5bSDimitry Andric uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
31501095a5dSDimitry Andric   Expected<uint64_t> Ret = (*unwrap(SI))->getAddress();
31601095a5dSDimitry Andric   if (!Ret) {
31701095a5dSDimitry Andric     std::string Buf;
31801095a5dSDimitry Andric     raw_string_ostream OS(Buf);
319d8e91e46SDimitry Andric     logAllUnhandledErrors(Ret.takeError(), OS);
320c0981da4SDimitry Andric     report_fatal_error(Twine(OS.str()));
32101095a5dSDimitry Andric   }
322ee8648bdSDimitry Andric   return *Ret;
32363faed5bSDimitry Andric }
32463faed5bSDimitry Andric 
LLVMGetSymbolSize(LLVMSymbolIteratorRef SI)32563faed5bSDimitry Andric uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
3261a82d4c0SDimitry Andric   return (*unwrap(SI))->getCommonSize();
32763faed5bSDimitry Andric }
32863faed5bSDimitry Andric 
32963faed5bSDimitry Andric // RelocationRef accessors
LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI)33063faed5bSDimitry Andric uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
3311a82d4c0SDimitry Andric   return (*unwrap(RI))->getOffset();
33263faed5bSDimitry Andric }
33363faed5bSDimitry Andric 
LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI)33463faed5bSDimitry Andric LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
335f8af5cf6SDimitry Andric   symbol_iterator ret = (*unwrap(RI))->getSymbol();
33663faed5bSDimitry Andric   return wrap(new symbol_iterator(ret));
33763faed5bSDimitry Andric }
33863faed5bSDimitry Andric 
LLVMGetRelocationType(LLVMRelocationIteratorRef RI)33963faed5bSDimitry Andric uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
3401a82d4c0SDimitry Andric   return (*unwrap(RI))->getType();
34163faed5bSDimitry Andric }
34263faed5bSDimitry Andric 
34363faed5bSDimitry Andric // NOTE: Caller takes ownership of returned string.
LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI)34463faed5bSDimitry Andric const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
34563faed5bSDimitry Andric   SmallVector<char, 0> ret;
3461a82d4c0SDimitry Andric   (*unwrap(RI))->getTypeName(ret);
347eb11fae6SDimitry Andric   char *str = static_cast<char*>(safe_malloc(ret.size()));
348d8e91e46SDimitry Andric   llvm::copy(ret, str);
34963faed5bSDimitry Andric   return str;
35063faed5bSDimitry Andric }
35163faed5bSDimitry Andric 
35263faed5bSDimitry Andric // NOTE: Caller takes ownership of returned string.
LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI)35363faed5bSDimitry Andric const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
35485d8b2bbSDimitry Andric   return strdup("");
35563faed5bSDimitry Andric }
35663faed5bSDimitry Andric 
357