2017-02-03 07:58:14 +08:00
|
|
|
//===- LTO.cpp ------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LTO.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "Symbols.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2018-05-23 04:20:25 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/TargetOptionsCommandFlags.h"
|
2017-02-03 07:58:14 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2017-09-08 08:50:50 +08:00
|
|
|
#include "llvm/LTO/Caching.h"
|
2017-02-03 07:58:14 +08:00
|
|
|
#include "llvm/LTO/Config.h"
|
|
|
|
#include "llvm/LTO/LTO.h"
|
|
|
|
#include "llvm/Object/SymbolicFile.h"
|
|
|
|
#include "llvm/Support/CodeGen.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::coff;
|
|
|
|
|
|
|
|
static std::unique_ptr<lto::LTO> createLTO() {
|
2018-05-23 04:20:25 +08:00
|
|
|
lto::Config C;
|
|
|
|
C.Options = InitTargetOptionsFromCodeGenFlags();
|
2018-03-15 04:25:41 +08:00
|
|
|
|
|
|
|
// Always emit a section per function/datum with LTO. LLVM LTO should get most
|
|
|
|
// of the benefit of linker GC, but there are still opportunities for ICF.
|
2018-05-23 04:20:25 +08:00
|
|
|
C.Options.FunctionSections = true;
|
|
|
|
C.Options.DataSections = true;
|
2018-03-15 04:25:41 +08:00
|
|
|
|
2017-10-11 08:46:58 +08:00
|
|
|
// Use static reloc model on 32-bit x86 because it usually results in more
|
|
|
|
// compact code, and because there are also known code generation bugs when
|
|
|
|
// using the PIC model (see PR34306).
|
|
|
|
if (Config->Machine == COFF::IMAGE_FILE_MACHINE_I386)
|
2018-05-23 04:20:25 +08:00
|
|
|
C.RelocModel = Reloc::Static;
|
2017-10-11 08:46:58 +08:00
|
|
|
else
|
2018-05-23 04:20:25 +08:00
|
|
|
C.RelocModel = Reloc::PIC_;
|
|
|
|
C.DisableVerify = true;
|
|
|
|
C.DiagHandler = diagnosticHandler;
|
|
|
|
C.OptLevel = Config->LTOO;
|
2017-02-09 02:36:41 +08:00
|
|
|
if (Config->SaveTemps)
|
2018-05-23 04:20:25 +08:00
|
|
|
checkError(C.addSaveTemps(std::string(Config->OutputFile) + ".",
|
|
|
|
/*UseInputModulePath*/ true));
|
2017-02-03 07:58:14 +08:00
|
|
|
lto::ThinBackend Backend;
|
2018-05-23 04:20:25 +08:00
|
|
|
if (Config->ThinLTOJobs != 0)
|
|
|
|
Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
|
|
|
|
return llvm::make_unique<lto::LTO>(std::move(C), Backend,
|
2017-02-03 07:58:14 +08:00
|
|
|
Config->LTOPartitions);
|
|
|
|
}
|
|
|
|
|
|
|
|
BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
|
|
|
|
|
|
|
|
BitcodeCompiler::~BitcodeCompiler() = default;
|
|
|
|
|
2017-11-04 06:48:47 +08:00
|
|
|
static void undefine(Symbol *S) { replaceSymbol<Undefined>(S, S->getName()); }
|
2017-02-03 07:58:14 +08:00
|
|
|
|
|
|
|
void BitcodeCompiler::add(BitcodeFile &F) {
|
|
|
|
lto::InputFile &Obj = *F.Obj;
|
|
|
|
unsigned SymNum = 0;
|
2017-11-04 05:21:47 +08:00
|
|
|
std::vector<Symbol *> SymBodies = F.getSymbols();
|
2017-02-03 07:58:14 +08:00
|
|
|
std::vector<lto::SymbolResolution> Resols(SymBodies.size());
|
|
|
|
|
|
|
|
// Provide a resolution to the LTO API for each symbol.
|
|
|
|
for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *Sym = SymBodies[SymNum];
|
2017-02-03 07:58:14 +08:00
|
|
|
lto::SymbolResolution &R = Resols[SymNum];
|
|
|
|
++SymNum;
|
|
|
|
|
|
|
|
// Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
|
|
|
|
// reports two symbols for module ASM defined. Without this check, lld
|
|
|
|
// flags an undefined in IR with a definition in ASM as prevailing.
|
|
|
|
// Once IRObjectFile is fixed to report only one symbol this hack can
|
|
|
|
// be removed.
|
2017-11-01 00:10:24 +08:00
|
|
|
R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F;
|
2017-02-03 07:58:14 +08:00
|
|
|
R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
|
|
|
|
if (R.Prevailing)
|
|
|
|
undefine(Sym);
|
|
|
|
}
|
|
|
|
checkError(LTOObj->add(std::move(F.Obj), Resols));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge all the bitcode files we have seen, codegen the result
|
2017-02-07 04:47:55 +08:00
|
|
|
// and return the resulting objects.
|
|
|
|
std::vector<StringRef> BitcodeCompiler::compile() {
|
2017-02-03 07:58:14 +08:00
|
|
|
unsigned MaxTasks = LTOObj->getMaxTasks();
|
2018-05-23 04:20:25 +08:00
|
|
|
Buf.resize(MaxTasks);
|
2017-09-08 08:50:50 +08:00
|
|
|
Files.resize(MaxTasks);
|
|
|
|
|
|
|
|
// The /lldltocache option specifies the path to a directory in which to cache
|
|
|
|
// native object files for ThinLTO incremental builds. If a path was
|
|
|
|
// specified, configure LTO to use it as the cache directory.
|
|
|
|
lto::NativeObjectCache Cache;
|
|
|
|
if (!Config->LTOCache.empty())
|
2018-02-21 04:21:59 +08:00
|
|
|
Cache = check(lto::localCache(
|
|
|
|
Config->LTOCache, [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
|
|
|
|
Files[Task] = std::move(MB);
|
|
|
|
}));
|
2017-09-08 08:50:50 +08:00
|
|
|
|
|
|
|
checkError(LTOObj->run(
|
|
|
|
[&](size_t Task) {
|
|
|
|
return llvm::make_unique<lto::NativeObjectStream>(
|
2018-05-23 04:20:25 +08:00
|
|
|
llvm::make_unique<raw_svector_ostream>(Buf[Task]));
|
2017-09-08 08:50:50 +08:00
|
|
|
},
|
|
|
|
Cache));
|
|
|
|
|
|
|
|
if (!Config->LTOCache.empty())
|
|
|
|
pruneCache(Config->LTOCache, Config->LTOCachePolicy);
|
2017-02-03 07:58:14 +08:00
|
|
|
|
2017-02-07 04:47:55 +08:00
|
|
|
std::vector<StringRef> Ret;
|
2017-02-09 02:36:41 +08:00
|
|
|
for (unsigned I = 0; I != MaxTasks; ++I) {
|
2018-05-23 04:20:25 +08:00
|
|
|
if (Buf[I].empty())
|
2017-02-09 02:36:41 +08:00
|
|
|
continue;
|
|
|
|
if (Config->SaveTemps) {
|
|
|
|
if (I == 0)
|
2018-05-23 04:20:25 +08:00
|
|
|
saveBuffer(Buf[I], Config->OutputFile + ".lto.obj");
|
2017-02-09 02:36:41 +08:00
|
|
|
else
|
2018-05-23 04:20:25 +08:00
|
|
|
saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.obj");
|
2017-02-09 02:36:41 +08:00
|
|
|
}
|
2018-05-23 04:20:25 +08:00
|
|
|
Ret.emplace_back(Buf[I].data(), Buf[I].size());
|
2017-02-09 02:36:41 +08:00
|
|
|
}
|
2017-09-08 08:50:50 +08:00
|
|
|
|
|
|
|
for (std::unique_ptr<MemoryBuffer> &File : Files)
|
|
|
|
if (File)
|
|
|
|
Ret.push_back(File->getBuffer());
|
|
|
|
|
2017-02-03 07:58:14 +08:00
|
|
|
return Ret;
|
|
|
|
}
|