#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/CodeGen/Analysis.h"
#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 makeLTOModule(Buffer, options, errMsg, Context);
}
-static ErrorOr<Module *> parseBitcodeFileImpl(MemoryBufferRef Buffer,
- LLVMContext &Context,
- bool ShouldBeLazy) {
+static std::unique_ptr<Module> parseBitcodeFileImpl(MemoryBufferRef Buffer,
+ LLVMContext &Context,
+ bool ShouldBeLazy,
+ std::string &ErrMsg) {
+
// Find the buffer.
ErrorOr<MemoryBufferRef> MBOrErr =
IRObjectFile::findBitcodeInMemBuffer(Buffer);
- if (std::error_code EC = MBOrErr.getError())
- return EC;
+ 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 (!ShouldBeLazy)
+ if (!ShouldBeLazy) {
// Parse the full file.
- return parseBitcodeFile(*MBOrErr, Context);
+ ErrorOr<std::unique_ptr<Module>> M =
+ parseBitcodeFile(*MBOrErr, Context, DiagnosticHandler);
+ if (!M)
+ return nullptr;
+ return std::move(*M);
+ }
// Parse lazily.
std::unique_ptr<MemoryBuffer> LightweightBuf =
MemoryBuffer::getMemBuffer(*MBOrErr, false);
- return getLazyBitcodeModule(std::move(LightweightBuf), Context);
+ ErrorOr<std::unique_ptr<Module>> M =
+ getLazyBitcodeModule(std::move(LightweightBuf), Context,
+ DiagnosticHandler, true /*ShouldLazyLoadMetadata*/);
+ if (!M)
+ return nullptr;
+ return std::move(*M);
}
LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer,
// If we own a context, we know this is being used only for symbol
// extraction, not linking. Be lazy in that case.
- ErrorOr<Module *> MOrErr = parseBitcodeFileImpl(
- Buffer, *Context, /* ShouldBeLazy */ static_cast<bool>(OwnedContext));
- if (std::error_code EC = MOrErr.getError()) {
- errMsg = EC.message();
+ std::unique_ptr<Module> M = parseBitcodeFileImpl(
+ Buffer, *Context,
+ /* ShouldBeLazy */ static_cast<bool>(OwnedContext), errMsg);
+ if (!M)
return nullptr;
- }
- std::unique_ptr<Module> M(MOrErr.get());
std::string TripleStr = M->getTargetTriple();
if (TripleStr.empty())
TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
options);
- M->setDataLayout(target->getSubtargetImpl()->getDataLayout());
+ M->setDataLayout(*target->getDataLayout());
std::unique_ptr<object::IRObjectFile> IRObj(
new object::IRObjectFile(Buffer, std::move(M)));
Constant *cn = gvn->getInitializer();
if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
if (ca->isCString()) {
- name = ".objc_class_name_" + ca->getAsCString().str();
+ name = (".objc_class_name_" + ca->getAsCString()).str();
return true;
}
}
else
attr |= LTO_SYMBOL_SCOPE_DEFAULT;
+ if (def->hasComdat())
+ attr |= LTO_SYMBOL_COMDAT;
+
+ if (isa<GlobalAlias>(def))
+ attr |= LTO_SYMBOL_ALIAS;
+
auto Iter = _defines.insert(Name).first;
// fill information structure
/// 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->getSubtargetImpl()
- ->getTargetLowering()
- ->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.
}