2011-04-03 22:34:07 +00: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-05-01 20:59:00 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2011-04-03 22:34:07 +00:00
|
|
|
#include "llvm-c/Object.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2011-04-03 22:34:07 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
2014-08-19 18:44:46 +00:00
|
|
|
inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) {
|
|
|
|
return reinterpret_cast<OwningBinary<ObjectFile> *>(OF);
|
2013-04-22 22:47:22 +00:00
|
|
|
}
|
|
|
|
|
2014-08-19 18:44:46 +00:00
|
|
|
inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) {
|
|
|
|
return reinterpret_cast<LLVMObjectFileRef>(
|
|
|
|
const_cast<OwningBinary<ObjectFile> *>(OF));
|
2013-04-22 22:47:22 +00: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-21 17:50:59 +00:00
|
|
|
// ObjectFile creation
|
2011-04-03 22:34:07 +00:00
|
|
|
LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
|
2014-06-23 22:00:37 +00:00
|
|
|
std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
|
2014-07-31 03:12:45 +00:00
|
|
|
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr(
|
2014-08-19 18:44:46 +00:00
|
|
|
ObjectFile::createObjectFile(Buf->getMemBufferRef()));
|
|
|
|
std::unique_ptr<ObjectFile> Obj;
|
2014-09-05 21:22:09 +00:00
|
|
|
if (!ObjOrErr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
|
2014-08-19 18:44:46 +00:00
|
|
|
return wrap(Ret);
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
|
|
|
|
delete unwrap(ObjectFile);
|
|
|
|
}
|
|
|
|
|
2011-10-21 17:50:59 +00:00
|
|
|
// ObjectFile Section iterators
|
2014-08-19 18:44:46 +00:00
|
|
|
LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
section_iterator SI = OB->getBinary()->section_begin();
|
2011-10-07 19:25:32 +00:00
|
|
|
return wrap(new section_iterator(SI));
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
2014-08-19 18:44:46 +00:00
|
|
|
LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,
|
|
|
|
LLVMSectionIteratorRef SI) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
|
2014-01-30 02:49:50 +00:00
|
|
|
++(*unwrap(SI));
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 18:21:22 +00:00
|
|
|
void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
|
|
|
|
LLVMSymbolIteratorRef Sym) {
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
|
2011-10-21 18:21:22 +00:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
}
|
|
|
|
|
2011-10-21 17:50:59 +00:00
|
|
|
// ObjectFile Symbol iterators
|
2014-08-19 18:44:46 +00:00
|
|
|
LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
symbol_iterator SI = OB->getBinary()->symbol_begin();
|
2011-10-21 17:50:59 +00:00
|
|
|
return wrap(new symbol_iterator(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
2014-08-19 18:44:46 +00:00
|
|
|
LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,
|
|
|
|
LLVMSymbolIteratorRef SI) {
|
|
|
|
OwningBinary<ObjectFile> *OB = unwrap(OF);
|
|
|
|
return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
|
2011-10-21 17:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
|
2014-01-30 02:49:50 +00:00
|
|
|
++(*unwrap(SI));
|
2011-10-21 17:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SectionRef accessors
|
2011-04-03 22:34:07 +00:00
|
|
|
const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
|
2011-06-25 17:55:23 +00:00
|
|
|
StringRef ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(SI))->getName(ret))
|
2011-06-25 17:55:23 +00:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret.data();
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
|
2014-10-08 15:28:58 +00:00
|
|
|
return (*unwrap(SI))->getSize();
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
|
2011-06-25 17:55:23 +00:00
|
|
|
StringRef ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(SI))->getContents(ret))
|
2011-06-25 17:55:23 +00:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret.data();
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
2011-10-21 17:50:59 +00:00
|
|
|
|
|
|
|
uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
|
2014-10-08 15:28:58 +00:00
|
|
|
return (*unwrap(SI))->getAddress();
|
2011-10-21 17:50:59 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 20:35:58 +00:00
|
|
|
LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
|
2011-10-21 17:50:59 +00:00
|
|
|
LLVMSymbolIteratorRef Sym) {
|
2014-10-08 15:28:58 +00:00
|
|
|
return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
|
2011-10-21 17:50:59 +00:00
|
|
|
}
|
|
|
|
|
2011-10-27 17:15:47 +00:00
|
|
|
// Section Relocation iterators
|
|
|
|
LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
|
2014-02-10 20:24:04 +00:00
|
|
|
relocation_iterator SI = (*unwrap(Section))->relocation_begin();
|
2011-10-27 17:15:47 +00:00
|
|
|
return wrap(new relocation_iterator(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
|
|
|
|
LLVMRelocationIteratorRef SI) {
|
2014-02-10 20:24:04 +00:00
|
|
|
return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
|
2011-10-27 17:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
|
2014-01-30 02:49:50 +00:00
|
|
|
++(*unwrap(SI));
|
2011-10-27 17:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-21 17:50:59 +00:00
|
|
|
// SymbolRef accessors
|
|
|
|
const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
|
|
|
|
StringRef ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(SI))->getName(ret))
|
2011-10-21 17:50:59 +00:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
|
|
|
|
uint64_t ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
|
2011-10-21 17:50:59 +00:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
|
2015-06-24 10:20:30 +00:00
|
|
|
return (*unwrap(SI))->getCommonSize();
|
2011-10-21 17:50:59 +00:00
|
|
|
}
|
|
|
|
|
2011-10-27 17:32:36 +00:00
|
|
|
// RelocationRef accessors
|
|
|
|
uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
|
|
|
|
uint64_t ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(RI))->getAddress(ret))
|
2011-10-27 17:32:36 +00:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-29 17:40:10 +00:00
|
|
|
uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
|
2015-06-29 23:29:12 +00:00
|
|
|
return (*unwrap(RI))->getOffset();
|
2011-11-29 17:40:10 +00:00
|
|
|
}
|
|
|
|
|
2011-10-27 17:32:36 +00:00
|
|
|
LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
|
2013-06-05 01:33:53 +00:00
|
|
|
symbol_iterator ret = (*unwrap(RI))->getSymbol();
|
2011-10-27 17:32:36 +00:00
|
|
|
return wrap(new symbol_iterator(ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
|
2015-06-30 01:53:01 +00:00
|
|
|
return (*unwrap(RI))->getType();
|
2011-10-27 17:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Caller takes ownership of returned string.
|
|
|
|
const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
|
|
|
|
SmallVector<char, 0> ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(RI))->getTypeName(ret))
|
2011-10-27 17:32:36 +00:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
|
|
|
|
char *str = static_cast<char*>(malloc(ret.size()));
|
|
|
|
std::copy(ret.begin(), ret.end(), str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Caller takes ownership of returned string.
|
|
|
|
const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
|
2015-06-03 04:48:06 +00:00
|
|
|
return strdup("");
|
2011-10-27 17:32:36 +00:00
|
|
|
}
|
|
|
|
|