#include "llvm/IR/Constants.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Mangler.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/MCExpr.h"
return StringRef(Triple).startswith(TriplePrefix);
}
-LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options,
- std::string &errMsg) {
+std::string LTOModule::getProducerString(MemoryBuffer *Buffer) {
+ ErrorOr<MemoryBufferRef> BCOrErr =
+ IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
+ if (!BCOrErr)
+ return "";
+ LLVMContext Context;
+ return getBitcodeProducerString(*BCOrErr, Context);
+}
+
+ErrorOr<std::unique_ptr<LTOModule>>
+LTOModule::createFromFile(LLVMContext &Context, const char *path,
+ TargetOptions options) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getFile(path);
- if (std::error_code EC = BufferOrErr.getError()) {
- errMsg = EC.message();
- return nullptr;
- }
+ if (std::error_code EC = BufferOrErr.getError())
+ return EC;
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
- return makeLTOModule(Buffer->getMemBufferRef(), options, errMsg,
- &getGlobalContext());
+ return makeLTOModule(Buffer->getMemBufferRef(), options, &Context);
}
-LTOModule *LTOModule::createFromOpenFile(int fd, const char *path, size_t size,
- TargetOptions options,
- std::string &errMsg) {
- return createFromOpenFileSlice(fd, path, size, 0, options, errMsg);
+ErrorOr<std::unique_ptr<LTOModule>>
+LTOModule::createFromOpenFile(LLVMContext &Context, int fd, const char *path,
+ size_t size, TargetOptions options) {
+ return createFromOpenFileSlice(Context, fd, path, size, 0, options);
}
-LTOModule *LTOModule::createFromOpenFileSlice(int fd, const char *path,
- size_t map_size, off_t offset,
- TargetOptions options,
- std::string &errMsg) {
+ErrorOr<std::unique_ptr<LTOModule>>
+LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd,
+ const char *path, size_t map_size,
+ off_t offset, TargetOptions options) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getOpenFileSlice(fd, path, map_size, offset);
- if (std::error_code EC = BufferOrErr.getError()) {
- errMsg = EC.message();
- return nullptr;
- }
+ if (std::error_code EC = BufferOrErr.getError())
+ return EC;
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
- return makeLTOModule(Buffer->getMemBufferRef(), options, errMsg,
- &getGlobalContext());
+ return makeLTOModule(Buffer->getMemBufferRef(), options, &Context);
}
-LTOModule *LTOModule::createFromBuffer(const void *mem, size_t length,
- TargetOptions options,
- std::string &errMsg, StringRef path) {
- return createInContext(mem, length, options, errMsg, path,
- &getGlobalContext());
+ErrorOr<std::unique_ptr<LTOModule>>
+LTOModule::createFromBuffer(LLVMContext &Context, const void *mem,
+ size_t length, TargetOptions options,
+ StringRef path) {
+ return createInContext(mem, length, options, path, &Context);
}
-LTOModule *LTOModule::createInLocalContext(const void *mem, size_t length,
- TargetOptions options,
- std::string &errMsg,
- StringRef path) {
- return createInContext(mem, length, options, errMsg, path, nullptr);
+ErrorOr<std::unique_ptr<LTOModule>>
+LTOModule::createInLocalContext(const void *mem, size_t length,
+ TargetOptions options, StringRef path) {
+ return createInContext(mem, length, options, path, nullptr);
}
-LTOModule *LTOModule::createInContext(const void *mem, size_t length,
- TargetOptions options,
- std::string &errMsg, StringRef path,
- LLVMContext *Context) {
+ErrorOr<std::unique_ptr<LTOModule>>
+LTOModule::createInContext(const void *mem, size_t length,
+ TargetOptions options, StringRef path,
+ LLVMContext *Context) {
StringRef Data((const char *)mem, length);
MemoryBufferRef Buffer(Data, path);
- return makeLTOModule(Buffer, options, errMsg, Context);
+ return makeLTOModule(Buffer, options, Context);
}
-static std::unique_ptr<Module> parseBitcodeFileImpl(MemoryBufferRef Buffer,
- LLVMContext &Context,
- bool ShouldBeLazy,
- std::string &ErrMsg) {
+static ErrorOr<std::unique_ptr<Module>>
+parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context,
+ bool ShouldBeLazy) {
// Find the buffer.
ErrorOr<MemoryBufferRef> MBOrErr =
IRObjectFile::findBitcodeInMemBuffer(Buffer);
- if (std::error_code EC = MBOrErr.getError()) {
- ErrMsg = EC.message();
- return nullptr;
- }
-
- std::function<void(const DiagnosticInfo &)> DiagnosticHandler =
- [&ErrMsg](const DiagnosticInfo &DI) {
- raw_string_ostream Stream(ErrMsg);
- DiagnosticPrinterRawOStream DP(Stream);
- DI.print(DP);
- };
+ if (std::error_code EC = MBOrErr.getError())
+ return EC;
if (!ShouldBeLazy) {
// Parse the full file.
- ErrorOr<std::unique_ptr<Module>> M =
- parseBitcodeFile(*MBOrErr, Context, DiagnosticHandler);
- if (!M)
- return nullptr;
+ ErrorOr<std::unique_ptr<Module>> M = parseBitcodeFile(*MBOrErr, Context);
+ if (std::error_code EC = M.getError())
+ return EC;
return std::move(*M);
}
// Parse lazily.
std::unique_ptr<MemoryBuffer> LightweightBuf =
MemoryBuffer::getMemBuffer(*MBOrErr, false);
- ErrorOr<std::unique_ptr<Module>> M =
- getLazyBitcodeModule(std::move(LightweightBuf), Context,
- DiagnosticHandler, true /*ShouldLazyLoadMetadata*/);
- if (!M)
- return nullptr;
+ ErrorOr<std::unique_ptr<Module>> M = getLazyBitcodeModule(
+ std::move(LightweightBuf), Context, true /*ShouldLazyLoadMetadata*/);
+ if (std::error_code EC = M.getError())
+ return EC;
return std::move(*M);
}
-LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer,
- TargetOptions options, std::string &errMsg,
- LLVMContext *Context) {
+ErrorOr<std::unique_ptr<LTOModule>>
+LTOModule::makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
+ LLVMContext *Context) {
std::unique_ptr<LLVMContext> OwnedContext;
if (!Context) {
OwnedContext = llvm::make_unique<LLVMContext>();
// If we own a context, we know this is being used only for symbol
// extraction, not linking. Be lazy in that case.
- std::unique_ptr<Module> M = parseBitcodeFileImpl(
- Buffer, *Context,
- /* ShouldBeLazy */ static_cast<bool>(OwnedContext), errMsg);
- if (!M)
- return nullptr;
+ ErrorOr<std::unique_ptr<Module>> MOrErr =
+ parseBitcodeFileImpl(Buffer, *Context,
+ /* ShouldBeLazy */ static_cast<bool>(OwnedContext));
+ if (std::error_code EC = MOrErr.getError())
+ return EC;
+ std::unique_ptr<Module> &M = *MOrErr;
std::string TripleStr = M->getTargetTriple();
if (TripleStr.empty())
llvm::Triple Triple(TripleStr);
// find machine architecture for this module
+ std::string errMsg;
const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
if (!march)
- return nullptr;
+ return std::unique_ptr<LTOModule>(nullptr);
// construct LTOModule, hand over ownership of module and target
SubtargetFeatures Features;
TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
options);
- M->setDataLayout(*target->getDataLayout());
+ M->setDataLayout(target->createDataLayout());
std::unique_ptr<object::IRObjectFile> IRObj(
new object::IRObjectFile(Buffer, std::move(M)));
- LTOModule *Ret;
+ std::unique_ptr<LTOModule> Ret;
if (OwnedContext)
- Ret = new LTOModule(std::move(IRObj), target, std::move(OwnedContext));
+ Ret.reset(new LTOModule(std::move(IRObj), target, std::move(OwnedContext)));
else
- Ret = new LTOModule(std::move(IRObj), target);
-
- if (Ret->parseSymbols(errMsg)) {
- delete Ret;
- return nullptr;
- }
+ Ret.reset(new LTOModule(std::move(IRObj), target));
+ Ret->parseSymbols();
Ret->parseMetadata();
- return Ret;
+ return std::move(Ret);
}
/// Create a MemoryBuffer from a memory range with an optional name.
if (def->hasComdat())
attr |= LTO_SYMBOL_COMDAT;
+ if (isa<GlobalAlias>(def))
+ attr |= LTO_SYMBOL_ALIAS;
+
auto Iter = _defines.insert(Name).first;
// fill information structure
info.symbol = decl;
}
-/// parseSymbols - Parse the symbols from the module and model-level ASM and add
-/// them to either the defined or undefined lists.
-bool LTOModule::parseSymbols(std::string &errMsg) {
+void LTOModule::parseSymbols() {
for (auto &Sym : IRFile->symbols()) {
const GlobalValue *GV = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
uint32_t Flags = Sym.getFlags();
NameAndAttributes info = u->getValue();
_symbols.push_back(info);
}
-
- return false;
}
/// parseMetadata - Parse metadata from the module
void LTOModule::parseMetadata() {
+ raw_string_ostream OS(LinkerOpts);
+
// Linker Options
if (Metadata *Val = getModule().getModuleFlag("Linker Options")) {
MDNode *LinkerOptions = cast<MDNode>(Val);
MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
- // FIXME: Make StringSet::insert match Self-Associative Container
- // requirements, returning <iter,bool> rather than bool, and use that
- // here.
- StringRef Op =
- _linkeropt_strings.insert(MDOption->getString()).first->first();
- StringRef DepLibName =
- _target->getObjFileLowering()->getDepLibFromLinkerOpt(Op);
- if (!DepLibName.empty())
- _deplibs.push_back(DepLibName.data());
- else if (!Op.empty())
- _linkeropts.push_back(Op.data());
+ OS << " " << MDOption->getString();
}
}
}
+ // Globals
+ Mangler Mang;
+ for (const NameAndAttributes &Sym : _symbols) {
+ if (!Sym.symbol)
+ continue;
+ _target->getObjFileLowering()->emitLinkerFlagsForGlobal(OS, Sym.symbol,
+ Mang);
+ }
+
// Add other interesting metadata here.
}