2011-04-04 06:34:07 +08:00
|
|
|
//===- Object.cpp - C bindings to the object file library--------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the C bindings to the file-format-independent object
|
|
|
|
// library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/Object.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2011-04-04 06:34:07 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
2014-08-20 02:44:46 +08:00
|
|
|
inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) {
|
|
|
|
return reinterpret_cast<OwningBinary<ObjectFile> *>(OF);
|
2013-04-23 06:47:22 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 02:44:46 +08:00
|
|
|
inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) {
|
|
|
|
return reinterpret_cast<LLVMObjectFileRef>(
|
|
|
|
const_cast<OwningBinary<ObjectFile> *>(OF));
|
2013-04-23 06:47:22 +08: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));
|
|
|
|
}
|
|
|
|
|
2011-10-22 01:50:59 +08:00
|
|
|
// ObjectFile creation
|
2011-04-04 06:34:07 +08:00
|
|
|
LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
|
2014-06-24 06:00:37 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
|
2014-08-20 02:44:46 +08:00
|
|
|
ObjectFile::createObjectFile(Buf->getMemBufferRef()));
|
|
|
|
std::unique_ptr<ObjectFile> Obj;
|
2016-04-07 06:14:09 +08:00
|
|
|
if (!ObjOrErr) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(ObjOrErr.takeError());
|
2014-09-06 05:22:09 +08:00
|
|
|
return nullptr;
|
2016-04-07 06:14:09 +08:00
|
|
|
}
|
2014-09-06 05:22:09 +08:00
|
|
|
|
|
|
|
auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
|
2014-08-20 02:44:46 +08:00
|
|
|
return wrap(Ret);
|
2011-04-04 06:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
|
|
|
|
delete unwrap(ObjectFile);
|
|
|
|
}
|
|
|
|
|
2011-10-22 01:50:59 +08:00
|
|
|
// ObjectFile Section iterators
|
2014-08-20 02:44:46 +08:00
|
|
|
LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
section_iterator SI = OB->getBinary()->section_begin();
|
2011-10-08 03:25:32 +08:00
|
|
|
return wrap(new section_iterator(SI));
|
2011-04-04 06:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
2014-08-20 02:44:46 +08:00
|
|
|
LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,
|
|
|
|
LLVMSectionIteratorRef SI) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
|
2011-04-04 06:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
|
2014-01-30 10:49:50 +08:00
|
|
|
++(*unwrap(SI));
|
2011-04-04 06:34:07 +08:00
|
|
|
}
|
|
|
|
|
2011-10-22 02:21:22 +08:00
|
|
|
void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
|
|
|
|
LLVMSymbolIteratorRef Sym) {
|
2016-05-03 04:28:12 +08:00
|
|
|
Expected<section_iterator> SecOrErr = (*unwrap(Sym))->getSection();
|
|
|
|
if (!SecOrErr) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
2018-11-11 09:46:03 +08:00
|
|
|
logAllUnhandledErrors(SecOrErr.takeError(), OS);
|
2016-05-03 04:28:12 +08:00
|
|
|
OS.flush();
|
|
|
|
report_fatal_error(Buf);
|
|
|
|
}
|
2015-08-08 07:27:14 +08:00
|
|
|
*unwrap(Sect) = *SecOrErr;
|
2011-10-22 02:21:22 +08:00
|
|
|
}
|
|
|
|
|
2011-10-22 01:50:59 +08:00
|
|
|
// ObjectFile Symbol iterators
|
2014-08-20 02:44:46 +08:00
|
|
|
LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
symbol_iterator SI = OB->getBinary()->symbol_begin();
|
2011-10-22 01:50:59 +08:00
|
|
|
return wrap(new symbol_iterator(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
2014-08-20 02:44:46 +08:00
|
|
|
LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,
|
|
|
|
LLVMSymbolIteratorRef SI) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
|
2011-10-22 01:50:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
|
2014-01-30 10:49:50 +08:00
|
|
|
++(*unwrap(SI));
|
2011-10-22 01:50:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SectionRef accessors
|
2011-04-04 06:34:07 +08:00
|
|
|
const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
|
2011-06-26 01:55:23 +08:00
|
|
|
StringRef ret;
|
2014-06-13 10:24:39 +08:00
|
|
|
if (std::error_code ec = (*unwrap(SI))->getName(ret))
|
2011-06-26 01:55:23 +08:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret.data();
|
2011-04-04 06:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
|
2014-10-08 23:28:58 +08:00
|
|
|
return (*unwrap(SI))->getSize();
|
2011-04-04 06:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
|
2011-06-26 01:55:23 +08:00
|
|
|
StringRef ret;
|
2014-06-13 10:24:39 +08:00
|
|
|
if (std::error_code ec = (*unwrap(SI))->getContents(ret))
|
2011-06-26 01:55:23 +08:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret.data();
|
2011-04-04 06:34:07 +08:00
|
|
|
}
|
2011-10-22 01:50:59 +08:00
|
|
|
|
|
|
|
uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
|
2014-10-08 23:28:58 +08:00
|
|
|
return (*unwrap(SI))->getAddress();
|
2011-10-22 01:50:59 +08:00
|
|
|
}
|
|
|
|
|
2011-10-22 04:35:58 +08:00
|
|
|
LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
|
2011-10-22 01:50:59 +08:00
|
|
|
LLVMSymbolIteratorRef Sym) {
|
2014-10-08 23:28:58 +08:00
|
|
|
return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
|
2011-10-22 01:50:59 +08:00
|
|
|
}
|
|
|
|
|
2011-10-28 01:15:47 +08:00
|
|
|
// Section Relocation iterators
|
|
|
|
LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
|
2014-02-11 04:24:04 +08:00
|
|
|
relocation_iterator SI = (*unwrap(Section))->relocation_begin();
|
2011-10-28 01:15:47 +08:00
|
|
|
return wrap(new relocation_iterator(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
|
|
|
|
LLVMRelocationIteratorRef SI) {
|
2014-02-11 04:24:04 +08:00
|
|
|
return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
|
2011-10-28 01:15:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
|
2014-01-30 10:49:50 +08:00
|
|
|
++(*unwrap(SI));
|
2011-10-28 01:15:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-22 01:50:59 +08:00
|
|
|
// SymbolRef accessors
|
|
|
|
const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> Ret = (*unwrap(SI))->getName();
|
|
|
|
if (!Ret) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
2018-11-11 09:46:03 +08:00
|
|
|
logAllUnhandledErrors(Ret.takeError(), OS);
|
2016-04-21 05:24:34 +08:00
|
|
|
OS.flush();
|
|
|
|
report_fatal_error(Buf);
|
|
|
|
}
|
2015-07-03 04:55:21 +08:00
|
|
|
return Ret->data();
|
2011-10-22 01:50:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
|
2016-06-25 02:24:42 +08:00
|
|
|
Expected<uint64_t> Ret = (*unwrap(SI))->getAddress();
|
|
|
|
if (!Ret) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
2018-11-11 09:46:03 +08:00
|
|
|
logAllUnhandledErrors(Ret.takeError(), OS);
|
2016-06-25 02:24:42 +08:00
|
|
|
OS.flush();
|
|
|
|
report_fatal_error(Buf);
|
|
|
|
}
|
2015-07-04 02:19:00 +08:00
|
|
|
return *Ret;
|
2011-10-22 01:50:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
|
2015-06-24 18:20:30 +08:00
|
|
|
return (*unwrap(SI))->getCommonSize();
|
2011-10-22 01:50:59 +08:00
|
|
|
}
|
|
|
|
|
2011-10-28 01:32:36 +08:00
|
|
|
// RelocationRef accessors
|
2011-11-30 01:40:10 +08:00
|
|
|
uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
|
2015-06-30 07:29:12 +08:00
|
|
|
return (*unwrap(RI))->getOffset();
|
2011-11-30 01:40:10 +08:00
|
|
|
}
|
|
|
|
|
2011-10-28 01:32:36 +08:00
|
|
|
LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
|
2013-06-05 09:33:53 +08:00
|
|
|
symbol_iterator ret = (*unwrap(RI))->getSymbol();
|
2011-10-28 01:32:36 +08:00
|
|
|
return wrap(new symbol_iterator(ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
|
2015-06-30 09:53:01 +08:00
|
|
|
return (*unwrap(RI))->getType();
|
2011-10-28 01:32:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Caller takes ownership of returned string.
|
|
|
|
const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
|
|
|
|
SmallVector<char, 0> ret;
|
2015-06-30 12:08:37 +08: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 13:41:26 +08:00
|
|
|
char *str = static_cast<char*>(safe_malloc(ret.size()));
|
2018-11-17 09:44:25 +08:00
|
|
|
llvm::copy(ret, str);
|
2011-10-28 01:32:36 +08:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Caller takes ownership of returned string.
|
|
|
|
const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
|
2015-06-03 12:48:06 +08:00
|
|
|
return strdup("");
|
2011-10-28 01:32:36 +08:00
|
|
|
}
|
|
|
|
|