#ifndef LLVM_LTO_LEGACY_LTOMODULE_H
#define LLVM_LTO_LEGACY_LTOMODULE_H
#include "llvm-c/lto.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/IR/Module.h"
#include "llvm/LTO/LTO.h"
#include "llvm/Object/IRObjectFile.h"
#include "llvm/Object/ModuleSymbolTable.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:
struct NameAndAttributes {
StringRef name;
uint32_t attributes = 0;
bool isFunction = false;
const GlobalValue *symbol = nullptr;
};
std::unique_ptr<LLVMContext> OwnedContext;
std::string LinkerOpts;
std::unique_ptr<Module> Mod;
MemoryBufferRef MBRef;
ModuleSymbolTable SymTab;
std::unique_ptr<TargetMachine> _target;
std::vector<NameAndAttributes> _symbols;
StringSet<> _defines;
StringMap<NameAndAttributes> _undefines;
std::vector<StringRef> _asm_undefines;
LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
TargetMachine *TM);
public:
~LTOModule();
static bool isBitcodeFile(const void *mem, size_t length);
static bool isBitcodeFile(StringRef path);
bool isThinLTO();
static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
StringRef triplePrefix);
static std::string getProducerString(MemoryBuffer *Buffer);
static std::unique_ptr<MemoryBuffer>
makeBuffer(const void *mem, size_t length, StringRef name = "");
static ErrorOr<std::unique_ptr<LTOModule>>
createFromFile(LLVMContext &Context, StringRef path,
const TargetOptions &options);
static ErrorOr<std::unique_ptr<LTOModule>>
createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
const TargetOptions &options);
static ErrorOr<std::unique_ptr<LTOModule>>
createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
size_t map_size, off_t offset,
const TargetOptions &options);
static ErrorOr<std::unique_ptr<LTOModule>>
createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
const TargetOptions &options, StringRef path = "");
static ErrorOr<std::unique_ptr<LTOModule>>
createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem,
size_t length, const TargetOptions &options,
StringRef path);
const Module &getModule() const { return *Mod; }
Module &getModule() { return *Mod; }
std::unique_ptr<Module> takeModule() { return std::move(Mod); }
const std::string &getTargetTriple() {
return getModule().getTargetTriple();
}
void setTargetTriple(StringRef Triple) {
getModule().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);
}
StringRef getSymbolName(uint32_t index) {
if (index < _symbols.size())
return _symbols[index].name;
return StringRef();
}
const GlobalValue *getSymbolGV(uint32_t index) {
if (index < _symbols.size())
return _symbols[index].symbol;
return nullptr;
}
StringRef getLinkerOpts() { return LinkerOpts; }
const std::vector<StringRef> &getAsmUndefinedRefs() { return _asm_undefines; }
static lto::InputFile *createInputFile(const void *buffer, size_t buffer_size,
const char *path, std::string &out_error);
static size_t getDependentLibraryCount(lto::InputFile *input);
static const char *getDependentLibrary(lto::InputFile *input, size_t index, size_t *size);
Expected<uint32_t> getMachOCPUType() const;
Expected<uint32_t> getMachOCPUSubType() const;
bool hasCtorDtor() const;
private:
void parseMetadata();
void parseSymbols();
void addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
bool isFunc);
void addDefinedSymbol(StringRef Name, const GlobalValue *def,
bool isFunction);
void addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym);
void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym);
void addDefinedFunctionSymbol(StringRef Name, const Function *F);
void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
void addAsmGlobalSymbolUndef(StringRef);
void addObjCClass(const GlobalVariable *clgv);
void addObjCCategory(const GlobalVariable *clgv);
void addObjCClassRef(const GlobalVariable *clgv);
bool objcClassNameFromExpression(const Constant *c, std::string &name);
static ErrorOr<std::unique_ptr<LTOModule>>
makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
LLVMContext &Context, bool ShouldBeLazy);
};
}
#endif