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)));
|
2014-04-15 06:32:26 +00:00
|
|
|
ObjectFile *Obj = ObjOrErr ? ObjOrErr.get() : nullptr;
|
2014-01-22 00:14:49 +00:00
|
|
|
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) {
|
2014-02-10 20:24:04 +00:00
|
|
|
section_iterator SI = unwrap(ObjectFile)->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);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
|
|
|
|
LLVMSectionIteratorRef SI) {
|
2014-02-10 20:24:04 +00:00
|
|
|
return (*unwrap(SI) == unwrap(ObjectFile)->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
|
|
|
|
LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
|
2014-02-10 20:24:04 +00:00
|
|
|
symbol_iterator SI = unwrap(ObjectFile)->symbol_begin();
|
2011-10-21 17:50:59 +00:00
|
|
|
return wrap(new symbol_iterator(SI));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
|
|
|
|
delete unwrap(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
|
|
|
|
LLVMSymbolIteratorRef SI) {
|
2014-02-10 20:24:04 +00:00
|
|
|
return (*unwrap(SI) == unwrap(ObjectFile)->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) {
|
2011-06-25 17:55:23 +00:00
|
|
|
uint64_t ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(SI))->getSize(ret))
|
2011-06-25 17:55:23 +00:00
|
|
|
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;
|
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) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-10-21 20:35:58 +00:00
|
|
|
LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
|
2011-10-21 17:50:59 +00:00
|
|
|
LLVMSymbolIteratorRef Sym) {
|
|
|
|
bool ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
|
2011-10-21 17:50:59 +00:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
uint64_t ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(SI))->getSize(ret))
|
2011-10-21 17:50:59 +00:00
|
|
|
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;
|
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) {
|
|
|
|
uint64_t ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(RI))->getOffset(ret))
|
2011-11-29 17:40:10 +00:00
|
|
|
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;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(RI))->getType(ret))
|
2011-10-27 17:32:36 +00:00
|
|
|
report_fatal_error(ec.message());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
SmallVector<char, 0> ret;
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code ec = (*unwrap(RI))->getValueString(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;
|
|
|
|
}
|
|
|
|
|