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;
|
|
|
|
|
2013-04-22 22:47:22 +00:00
|
|
|
inline ObjectFile *unwrap(LLVMObjectFileRef OF) {
|
|
|
|
return reinterpret_cast<ObjectFile*>(OF);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline LLVMObjectFileRef wrap(const ObjectFile *OF) {
|
|
|
|
return reinterpret_cast<LLVMObjectFileRef>(const_cast<ObjectFile*>(OF));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-22 00:14:49 +00:00
|
|
|
ErrorOr<ObjectFile*> ObjOrErr(ObjectFile::createObjectFile(unwrap(MemBuf)));
|
|
|
|
ObjectFile *Obj = ObjOrErr ? ObjOrErr.get() : 0;
|
|
|
|
return wrap(Obj);
|
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
|
2011-04-03 22:34:07 +00:00
|
|
|
LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
|
2011-10-07 19:25:32 +00:00
|
|
|
section_iterator SI = unwrap(ObjectFile)->begin_sections();
|
|
|
|
return wrap(new section_iterator(SI));
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
|
|
|
|
LLVMSectionIteratorRef SI) {
|
|
|
|
return (*unwrap(SI) == unwrap(ObjectFile)->end_sections()) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
|
2011-06-25 17:55:23 +00:00
|
|
|
error_code ec;
|
|
|
|
unwrap(SI)->increment(ec);
|
|
|
|
if (ec) report_fatal_error("LLVMMoveToNextSection failed: " + ec.message());
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 18:21:22 +00:00
|
|
|
void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
|
|
|
|
LLVMSymbolIteratorRef Sym) {
|
|
|
|
if (error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
}
|
|
|
|
|
2011-10-21 17:50:59 +00:00
|
|
|
// ObjectFile Symbol iterators
|
|
|
|
LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
|
|
|
|
symbol_iterator SI = unwrap(ObjectFile)->begin_symbols();
|
|
|
|
return wrap(new symbol_iterator(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
|
|
|
|
LLVMSymbolIteratorRef SI) {
|
|
|
|
return (*unwrap(SI) == unwrap(ObjectFile)->end_symbols()) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
|
|
|
|
error_code ec;
|
|
|
|
unwrap(SI)->increment(ec);
|
|
|
|
if (ec) report_fatal_error("LLVMMoveToNextSymbol failed: " + ec.message());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
if (error_code ec = (*unwrap(SI))->getName(ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret.data();
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
|
2011-06-25 17:55:23 +00:00
|
|
|
uint64_t ret;
|
|
|
|
if (error_code ec = (*unwrap(SI))->getSize(ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
2011-04-03 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
|
2011-06-25 17:55:23 +00:00
|
|
|
StringRef ret;
|
|
|
|
if (error_code ec = (*unwrap(SI))->getContents(ret))
|
|
|
|
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) {
|
|
|
|
uint64_t ret;
|
|
|
|
if (error_code ec = (*unwrap(SI))->getAddress(ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-21 20:35:58 +00:00
|
|
|
LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
|
2011-10-21 17:50:59 +00:00
|
|
|
LLVMSymbolIteratorRef Sym) {
|
|
|
|
bool ret;
|
|
|
|
if (error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-27 17:15:47 +00:00
|
|
|
// Section Relocation iterators
|
|
|
|
LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
|
|
|
|
relocation_iterator SI = (*unwrap(Section))->begin_relocations();
|
|
|
|
return wrap(new relocation_iterator(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
|
|
|
|
LLVMRelocationIteratorRef SI) {
|
|
|
|
return (*unwrap(SI) == (*unwrap(Section))->end_relocations()) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
|
|
|
|
error_code ec;
|
|
|
|
unwrap(SI)->increment(ec);
|
|
|
|
if (ec) report_fatal_error("LLVMMoveToNextRelocation failed: " +
|
|
|
|
ec.message());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-21 17:50:59 +00:00
|
|
|
// SymbolRef accessors
|
|
|
|
const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
|
|
|
|
StringRef ret;
|
|
|
|
if (error_code ec = (*unwrap(SI))->getName(ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
|
|
|
|
uint64_t ret;
|
|
|
|
if (error_code ec = (*unwrap(SI))->getAddress(ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-29 17:40:10 +00:00
|
|
|
uint64_t LLVMGetSymbolFileOffset(LLVMSymbolIteratorRef SI) {
|
2011-10-21 17:50:59 +00:00
|
|
|
uint64_t ret;
|
2011-11-29 17:40:10 +00:00
|
|
|
if (error_code ec = (*unwrap(SI))->getFileOffset(ret))
|
2011-10-21 17:50:59 +00:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
|
|
|
|
uint64_t ret;
|
|
|
|
if (error_code ec = (*unwrap(SI))->getSize(ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-27 17:32:36 +00:00
|
|
|
// RelocationRef accessors
|
|
|
|
uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
|
|
|
|
uint64_t ret;
|
|
|
|
if (error_code ec = (*unwrap(RI))->getAddress(ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-29 17:40:10 +00:00
|
|
|
uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
|
|
|
|
uint64_t ret;
|
|
|
|
if (error_code ec = (*unwrap(RI))->getOffset(ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
uint64_t ret;
|
|
|
|
if (error_code ec = (*unwrap(RI))->getType(ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Caller takes ownership of returned string.
|
|
|
|
const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
|
|
|
|
SmallVector<char, 0> ret;
|
|
|
|
if (error_code ec = (*unwrap(RI))->getTypeName(ret))
|
|
|
|
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) {
|
|
|
|
SmallVector<char, 0> ret;
|
|
|
|
if (error_code ec = (*unwrap(RI))->getValueString(ret))
|
|
|
|
report_fatal_error(ec.message());
|
|
|
|
|
|
|
|
char *str = static_cast<char*>(malloc(ret.size()));
|
|
|
|
std::copy(ret.begin(), ret.end(), str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|