#ifndef LTO_MODULE_H
#define LTO_MODULE_H
#include "llvm-c/lto.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/Target/TargetMachine.h"
#include <string>
#include <vector>
namespace llvm {
class Function;
class GlobalValue;
class MemoryBuffer;
class TargetOptions;
class Value;
}
struct LTOModule {
private:
typedef llvm::StringMap<uint8_t> StringSet;
struct NameAndAttributes {
const char *name;
uint32_t attributes;
bool isFunction;
const llvm::GlobalValue *symbol;
};
llvm::OwningPtr<llvm::Module> _module;
llvm::OwningPtr<llvm::TargetMachine> _target;
llvm::MCObjectFileInfo ObjFileInfo;
StringSet _linkeropt_strings;
std::vector<const char *> _deplibs;
std::vector<const char *> _linkeropts;
std::vector<NameAndAttributes> _symbols;
StringSet _defines;
llvm::StringMap<NameAndAttributes> _undefines;
std::vector<const char*> _asm_undefines;
llvm::MCContext _context;
llvm::Mangler _mangler;
LTOModule(llvm::Module *m, llvm::TargetMachine *t);
public:
static bool isBitcodeFile(const void *mem, size_t length);
static bool isBitcodeFile(const char *path);
static bool isBitcodeFileForTarget(const void *mem,
size_t length,
const char *triplePrefix);
static bool isBitcodeFileForTarget(const char *path,
const char *triplePrefix);
static LTOModule *makeLTOModule(const char* path,
llvm::TargetOptions options,
std::string &errMsg);
static LTOModule *makeLTOModule(int fd, const char *path,
size_t size, llvm::TargetOptions options,
std::string &errMsg);
static LTOModule *makeLTOModule(int fd, const char *path,
size_t map_size,
off_t offset, llvm::TargetOptions options,
std::string& errMsg);
static LTOModule *makeLTOModule(const void *mem, size_t length,
llvm::TargetOptions options,
std::string &errMsg,
llvm::StringRef path = "");
const char *getTargetTriple() {
return _module->getTargetTriple().c_str();
}
void setTargetTriple(const char *triple) {
_module->setTargetTriple(triple);
}
uint32_t getSymbolCount() {
return _symbols.size();
}
lto_symbol_attributes getSymbolAttributes(uint32_t index) {
if (index < _symbols.size())
return lto_symbol_attributes(_symbols[index].attributes);
return lto_symbol_attributes(0);
}
const char *getSymbolName(uint32_t index) {
if (index < _symbols.size())
return _symbols[index].name;
return NULL;
}
uint32_t getDependentLibraryCount() {
return _deplibs.size();
}
const char *getDependentLibrary(uint32_t index) {
if (index < _deplibs.size())
return _deplibs[index];
return NULL;
}
uint32_t getLinkerOptCount() {
return _linkeropts.size();
}
const char *getLinkerOpt(uint32_t index) {
if (index < _linkeropts.size())
return _linkeropts[index];
return NULL;
}
llvm::Module *getLLVVMModule() { return _module.get(); }
const std::vector<const char*> &getAsmUndefinedRefs() {
return _asm_undefines;
}
private:
void parseMetadata();
bool parseSymbols(std::string &errMsg);
void addPotentialUndefinedSymbol(const llvm::GlobalValue *dcl, bool isFunc);
void addDefinedSymbol(const llvm::GlobalValue *def, bool isFunction);
void addDefinedFunctionSymbol(const llvm::Function *f);
void addDefinedDataSymbol(const llvm::GlobalValue *v);
bool addAsmGlobalSymbols(std::string &errMsg);
void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
void addAsmGlobalSymbolUndef(const char *);
void addObjCClass(const llvm::GlobalVariable *clgv);
void addObjCCategory(const llvm::GlobalVariable *clgv);
void addObjCClassRef(const llvm::GlobalVariable *clgv);
bool objcClassNameFromExpression(const llvm::Constant* c, std::string &name);
static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
const char *triplePrefix);
static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
llvm::TargetOptions options,
std::string &errMsg);
static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length,
llvm::StringRef name = "");
};
#endif // LTO_MODULE_H