2011-04-04 00:34:07 +02:00
|
|
|
//===- Object.cpp - C bindings to the object file library--------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-04-04 00:34:07 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the C bindings to the file-format-independent object
|
|
|
|
// library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/Object.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2019-04-05 23:36:50 +02:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2014-01-07 12:48:04 +01:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2011-04-04 00:34:07 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
2014-08-19 20:44:46 +02:00
|
|
|
inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) {
|
|
|
|
return reinterpret_cast<OwningBinary<ObjectFile> *>(OF);
|
2013-04-23 00:47:22 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 20:44:46 +02:00
|
|
|
inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) {
|
|
|
|
return reinterpret_cast<LLVMObjectFileRef>(
|
|
|
|
const_cast<OwningBinary<ObjectFile> *>(OF));
|
2013-04-23 00:47:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
|
|
|
|
return reinterpret_cast<section_iterator*>(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline LLVMSectionIteratorRef
|
|
|
|
wrap(const section_iterator *SI) {
|
|
|
|
return reinterpret_cast<LLVMSectionIteratorRef>
|
|
|
|
(const_cast<section_iterator*>(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) {
|
|
|
|
return reinterpret_cast<symbol_iterator*>(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline LLVMSymbolIteratorRef
|
|
|
|
wrap(const symbol_iterator *SI) {
|
|
|
|
return reinterpret_cast<LLVMSymbolIteratorRef>
|
|
|
|
(const_cast<symbol_iterator*>(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) {
|
|
|
|
return reinterpret_cast<relocation_iterator*>(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline LLVMRelocationIteratorRef
|
|
|
|
wrap(const relocation_iterator *SI) {
|
|
|
|
return reinterpret_cast<LLVMRelocationIteratorRef>
|
|
|
|
(const_cast<relocation_iterator*>(SI));
|
|
|
|
}
|
|
|
|
|
2019-04-05 23:36:50 +02:00
|
|
|
/*--.. Operations on binary files ..........................................--*/
|
|
|
|
|
|
|
|
LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf,
|
|
|
|
LLVMContextRef Context,
|
|
|
|
char **ErrorMessage) {
|
|
|
|
auto maybeContext = Context ? unwrap(Context) : nullptr;
|
|
|
|
Expected<std::unique_ptr<Binary>> ObjOrErr(
|
|
|
|
createBinary(unwrap(MemBuf)->getMemBufferRef(), maybeContext));
|
|
|
|
if (!ObjOrErr) {
|
|
|
|
*ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrap(ObjOrErr.get().release());
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR) {
|
|
|
|
auto Buf = unwrap(BR)->getMemoryBufferRef();
|
|
|
|
return wrap(llvm::MemoryBuffer::getMemBuffer(
|
|
|
|
Buf.getBuffer(), Buf.getBufferIdentifier(),
|
|
|
|
/*RequiresNullTerminator*/false).release());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeBinary(LLVMBinaryRef BR) {
|
|
|
|
delete unwrap(BR);
|
|
|
|
}
|
|
|
|
|
2019-04-07 20:18:42 +02:00
|
|
|
LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR) {
|
|
|
|
class BinaryTypeMapper final : public Binary {
|
|
|
|
public:
|
|
|
|
static LLVMBinaryType mapBinaryTypeToLLVMBinaryType(unsigned Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case ID_Archive:
|
|
|
|
return LLVMBinaryTypeArchive;
|
|
|
|
case ID_MachOUniversalBinary:
|
|
|
|
return LLVMBinaryTypeMachOUniversalBinary;
|
|
|
|
case ID_COFFImportFile:
|
|
|
|
return LLVMBinaryTypeCOFFImportFile;
|
|
|
|
case ID_IR:
|
|
|
|
return LLVMBinaryTypeIR;
|
|
|
|
case ID_WinRes:
|
|
|
|
return LLVMBinaryTypeWinRes;
|
|
|
|
case ID_COFF:
|
|
|
|
return LLVMBinaryTypeCOFF;
|
|
|
|
case ID_ELF32L:
|
|
|
|
return LLVMBinaryTypeELF32L;
|
|
|
|
case ID_ELF32B:
|
|
|
|
return LLVMBinaryTypeELF32B;
|
|
|
|
case ID_ELF64L:
|
|
|
|
return LLVMBinaryTypeELF64L;
|
|
|
|
case ID_ELF64B:
|
|
|
|
return LLVMBinaryTypeELF64B;
|
|
|
|
case ID_MachO32L:
|
|
|
|
return LLVMBinaryTypeMachO32L;
|
|
|
|
case ID_MachO32B:
|
|
|
|
return LLVMBinaryTypeMachO32B;
|
|
|
|
case ID_MachO64L:
|
|
|
|
return LLVMBinaryTypeMachO64L;
|
|
|
|
case ID_MachO64B:
|
|
|
|
return LLVMBinaryTypeMachO64B;
|
|
|
|
case ID_Wasm:
|
|
|
|
return LLVMBinaryTypeWasm;
|
|
|
|
case ID_StartObjects:
|
|
|
|
case ID_EndObjects:
|
|
|
|
llvm_unreachable("Marker types are not valid binary kinds!");
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown binary kind!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return BinaryTypeMapper::mapBinaryTypeToLLVMBinaryType(unwrap(BR)->getType());
|
|
|
|
}
|
|
|
|
|
2019-04-09 23:53:31 +02:00
|
|
|
LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR) {
|
|
|
|
auto OF = cast<ObjectFile>(unwrap(BR));
|
|
|
|
auto sections = OF->sections();
|
|
|
|
if (sections.begin() == sections.end())
|
|
|
|
return nullptr;
|
|
|
|
return wrap(new section_iterator(sections.begin()));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,
|
|
|
|
LLVMSectionIteratorRef SI) {
|
|
|
|
auto OF = cast<ObjectFile>(unwrap(BR));
|
|
|
|
return (*unwrap(SI) == OF->section_end()) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMSymbolIteratorRef LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR) {
|
|
|
|
auto OF = cast<ObjectFile>(unwrap(BR));
|
|
|
|
auto symbols = OF->symbols();
|
|
|
|
if (symbols.begin() == symbols.end())
|
|
|
|
return nullptr;
|
|
|
|
return wrap(new symbol_iterator(symbols.begin()));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
|
|
|
|
LLVMSymbolIteratorRef SI) {
|
|
|
|
auto OF = cast<ObjectFile>(unwrap(BR));
|
|
|
|
return (*unwrap(SI) == OF->symbol_end()) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2011-10-21 19:50:59 +02:00
|
|
|
// ObjectFile creation
|
2011-04-04 00:34:07 +02:00
|
|
|
LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
|
2014-06-24 00:00:37 +02:00
|
|
|
std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
|
2016-04-07 00:14:09 +02:00
|
|
|
Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
|
2014-08-19 20:44:46 +02:00
|
|
|
ObjectFile::createObjectFile(Buf->getMemBufferRef()));
|
|
|
|
std::unique_ptr<ObjectFile> Obj;
|
2016-04-07 00:14:09 +02:00
|
|
|
if (!ObjOrErr) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(ObjOrErr.takeError());
|
2014-09-05 23:22:09 +02:00
|
|
|
return nullptr;
|
2016-04-07 00:14:09 +02:00
|
|
|
}
|
2014-09-05 23:22:09 +02:00
|
|
|
|
|
|
|
auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
|
2014-08-19 20:44:46 +02:00
|
|
|
return wrap(Ret);
|
2011-04-04 00:34:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
|
|
|
|
delete unwrap(ObjectFile);
|
|
|
|
}
|
|
|
|
|
2011-10-21 19:50:59 +02:00
|
|
|
// ObjectFile Section iterators
|
2014-08-19 20:44:46 +02:00
|
|
|
LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
section_iterator SI = OB->getBinary()->section_begin();
|
2011-10-07 21:25:32 +02:00
|
|
|
return wrap(new section_iterator(SI));
|
2011-04-04 00:34:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
2014-08-19 20:44:46 +02:00
|
|
|
LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,
|
|
|
|
LLVMSectionIteratorRef SI) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
|
2011-04-04 00:34:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
|
2014-01-30 03:49:50 +01:00
|
|
|
++(*unwrap(SI));
|
2011-04-04 00:34:07 +02:00
|
|
|
}
|
|
|
|
|
2011-10-21 20:21:22 +02:00
|
|
|
void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
|
|
|
|
LLVMSymbolIteratorRef Sym) {
|
2016-05-02 22:28:12 +02:00
|
|
|
Expected<section_iterator> SecOrErr = (*unwrap(Sym))->getSection();
|
|
|
|
if (!SecOrErr) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
2018-11-11 02:46:03 +01:00
|
|
|
logAllUnhandledErrors(SecOrErr.takeError(), OS);
|
2016-05-02 22:28:12 +02:00
|
|
|
OS.flush();
|
|
|
|
report_fatal_error(Buf);
|
|
|
|
}
|
2015-08-08 01:27:14 +02:00
|
|
|
*unwrap(Sect) = *SecOrErr;
|
2011-10-21 20:21:22 +02:00
|
|
|
}
|
|
|
|
|
2011-10-21 19:50:59 +02:00
|
|
|
// ObjectFile Symbol iterators
|
2014-08-19 20:44:46 +02:00
|
|
|
LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
symbol_iterator SI = OB->getBinary()->symbol_begin();
|
2011-10-21 19:50:59 +02:00
|
|
|
return wrap(new symbol_iterator(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
2014-08-19 20:44:46 +02:00
|
|
|
LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,
|
|
|
|
LLVMSymbolIteratorRef SI) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
|
2011-10-21 19:50:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
|
2014-01-30 03:49:50 +01:00
|
|
|
++(*unwrap(SI));
|
2011-10-21 19:50:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SectionRef accessors
|
2011-04-04 00:34:07 +02:00
|
|
|
const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
|
2011-06-25 19:55:23 +02:00
|
|
|
StringRef ret;
|
2014-06-13 04:24:39 +02:00
|
|
|
if (std::error_code ec = (*unwrap(SI))->getName(ret))
|
2011-06-25 19:55:23 +02:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret.data();
|
2011-04-04 00:34:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
|
2014-10-08 17:28:58 +02:00
|
|
|
return (*unwrap(SI))->getSize();
|
2011-04-04 00:34:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
|
2019-05-16 15:24:04 +02:00
|
|
|
if (Expected<StringRef> E = (*unwrap(SI))->getContents())
|
|
|
|
return E->data();
|
|
|
|
else
|
|
|
|
report_fatal_error(E.takeError());
|
2011-04-04 00:34:07 +02:00
|
|
|
}
|
2011-10-21 19:50:59 +02:00
|
|
|
|
|
|
|
uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
|
2014-10-08 17:28:58 +02:00
|
|
|
return (*unwrap(SI))->getAddress();
|
2011-10-21 19:50:59 +02:00
|
|
|
}
|
|
|
|
|
2011-10-21 22:35:58 +02:00
|
|
|
LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
|
2011-10-21 19:50:59 +02:00
|
|
|
LLVMSymbolIteratorRef Sym) {
|
2014-10-08 17:28:58 +02:00
|
|
|
return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
|
2011-10-21 19:50:59 +02:00
|
|
|
}
|
|
|
|
|
2011-10-27 19:15:47 +02:00
|
|
|
// Section Relocation iterators
|
|
|
|
LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
|
2014-02-10 21:24:04 +01:00
|
|
|
relocation_iterator SI = (*unwrap(Section))->relocation_begin();
|
2011-10-27 19:15:47 +02:00
|
|
|
return wrap(new relocation_iterator(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
|
|
|
|
LLVMRelocationIteratorRef SI) {
|
2014-02-10 21:24:04 +01:00
|
|
|
return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
|
2011-10-27 19:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
|
2014-01-30 03:49:50 +01:00
|
|
|
++(*unwrap(SI));
|
2011-10-27 19:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-21 19:50:59 +02:00
|
|
|
// SymbolRef accessors
|
|
|
|
const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
|
2016-04-20 23:24:34 +02:00
|
|
|
Expected<StringRef> Ret = (*unwrap(SI))->getName();
|
|
|
|
if (!Ret) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
2018-11-11 02:46:03 +01:00
|
|
|
logAllUnhandledErrors(Ret.takeError(), OS);
|
2016-04-20 23:24:34 +02:00
|
|
|
OS.flush();
|
|
|
|
report_fatal_error(Buf);
|
|
|
|
}
|
2015-07-02 22:55:21 +02:00
|
|
|
return Ret->data();
|
2011-10-21 19:50:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
|
2016-06-24 20:24:42 +02:00
|
|
|
Expected<uint64_t> Ret = (*unwrap(SI))->getAddress();
|
|
|
|
if (!Ret) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
2018-11-11 02:46:03 +01:00
|
|
|
logAllUnhandledErrors(Ret.takeError(), OS);
|
2016-06-24 20:24:42 +02:00
|
|
|
OS.flush();
|
|
|
|
report_fatal_error(Buf);
|
|
|
|
}
|
2015-07-03 20:19:00 +02:00
|
|
|
return *Ret;
|
2011-10-21 19:50:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
|
2015-06-24 12:20:30 +02:00
|
|
|
return (*unwrap(SI))->getCommonSize();
|
2011-10-21 19:50:59 +02:00
|
|
|
}
|
|
|
|
|
2011-10-27 19:32:36 +02:00
|
|
|
// RelocationRef accessors
|
2011-11-29 18:40:10 +01:00
|
|
|
uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
|
2015-06-30 01:29:12 +02:00
|
|
|
return (*unwrap(RI))->getOffset();
|
2011-11-29 18:40:10 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 19:32:36 +02:00
|
|
|
LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
|
2013-06-05 03:33:53 +02:00
|
|
|
symbol_iterator ret = (*unwrap(RI))->getSymbol();
|
2011-10-27 19:32:36 +02:00
|
|
|
return wrap(new symbol_iterator(ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
|
2015-06-30 03:53:01 +02:00
|
|
|
return (*unwrap(RI))->getType();
|
2011-10-27 19:32:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Caller takes ownership of returned string.
|
|
|
|
const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
|
|
|
|
SmallVector<char, 0> ret;
|
2015-06-30 06:08:37 +02:00
|
|
|
(*unwrap(RI))->getTypeName(ret);
|
Report fatal error in the case of out of memory
This is the second part of recommit of r325224. The previous part was
committed in r325426, which deals with C++ memory allocation. Solution
for C memory allocation involved functions `llvm::malloc` and similar.
This was a fragile solution because it caused ambiguity errors in some
cases. In this commit the new functions have names like `llvm::safe_malloc`.
The relevant part of original comment is below, updated for new function
names.
Analysis of fails in the case of out of memory errors can be tricky on
Windows. Such error emerges at the point where memory allocation function
fails, but manifests itself when null pointer is used. These two points
may be distant from each other. Besides, next runs may not exhibit
allocation error.
In some cases memory is allocated by a call to some of C allocation
functions, malloc, calloc and realloc. They are used for interoperability
with C code, when allocated object has variable size and when it is
necessary to avoid call of constructors. In many calls the result is not
checked for null pointer. To simplify checks, new functions are defined
in the namespace 'llvm': `safe_malloc`, `safe_calloc` and `safe_realloc`.
They behave as corresponding standard functions but produce fatal error if
allocation fails. This change replaces the standard functions like 'malloc'
in the cases when the result of the allocation function is not checked
for null pointer.
Finally, there are plain C code, that uses malloc and similar functions. If
the result is not checked, assert statement is added.
Differential Revision: https://reviews.llvm.org/D43010
llvm-svn: 325551
2018-02-20 06:41:26 +01:00
|
|
|
char *str = static_cast<char*>(safe_malloc(ret.size()));
|
2018-11-17 02:44:25 +01:00
|
|
|
llvm::copy(ret, str);
|
2011-10-27 19:32:36 +02:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Caller takes ownership of returned string.
|
|
|
|
const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
|
2015-06-03 06:48:06 +02:00
|
|
|
return strdup("");
|
2011-10-27 19:32:36 +02:00
|
|
|
}
|
|
|
|
|