2016-03-23 04:52:10 +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"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "LinkerScript.h"
|
2017-07-27 07:39:10 +08:00
|
|
|
#include "SymbolTable.h"
|
2016-03-23 04:52:10 +08:00
|
|
|
#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"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/TargetOptionsCommandFlags.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-11-06 06:37:59 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2016-09-29 08:40:08 +08:00
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2017-03-02 07:00:10 +08:00
|
|
|
#include "llvm/LTO/Caching.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/LTO/Config.h"
|
2016-09-29 08:40:08 +08:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#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>
|
2016-03-23 04:52:10 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
|
|
|
|
|
|
|
// This is for use when debugging LTO.
|
2016-07-15 10:17:13 +08:00
|
|
|
static void saveBuffer(StringRef Buffer, const Twine &Path) {
|
2016-03-23 04:52:10 +08:00
|
|
|
std::error_code EC;
|
2016-07-15 10:17:13 +08:00
|
|
|
raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
|
2016-07-15 10:01:03 +08:00
|
|
|
if (EC)
|
2017-01-13 06:18:04 +08:00
|
|
|
error("cannot create " + Path + ": " + EC.message());
|
2016-03-23 04:52:10 +08:00
|
|
|
OS << Buffer;
|
|
|
|
}
|
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
static void diagnosticHandler(const DiagnosticInfo &DI) {
|
|
|
|
SmallString<128> ErrStorage;
|
|
|
|
raw_svector_ostream OS(ErrStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(OS);
|
|
|
|
DI.print(DP);
|
2016-09-30 05:00:23 +08:00
|
|
|
warn(ErrStorage);
|
2016-03-23 04:52:10 +08:00
|
|
|
}
|
|
|
|
|
2016-09-30 05:00:26 +08:00
|
|
|
static void checkError(Error E) {
|
2017-09-22 06:50:52 +08:00
|
|
|
handleAllErrors(std::move(E),
|
|
|
|
[&](ErrorInfoBase &EIB) { error(EIB.message()); });
|
2016-09-30 05:00:26 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
static std::unique_ptr<lto::LTO> createLTO() {
|
|
|
|
lto::Config Conf;
|
2016-05-16 03:29:38 +08:00
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
// LLD supports the new relocations.
|
|
|
|
Conf.Options = InitTargetOptionsFromCodeGenFlags();
|
|
|
|
Conf.Options.RelaxELFRelocations = true;
|
2016-05-16 03:29:38 +08:00
|
|
|
|
2017-07-25 04:15:07 +08:00
|
|
|
// Always emit a section per function/datum with LTO.
|
2017-07-25 03:38:13 +08:00
|
|
|
Conf.Options.FunctionSections = true;
|
2017-07-25 04:15:07 +08:00
|
|
|
Conf.Options.DataSections = true;
|
2017-07-25 03:38:13 +08:00
|
|
|
|
2017-05-23 05:11:44 +08:00
|
|
|
if (Config->Relocatable)
|
|
|
|
Conf.RelocModel = None;
|
|
|
|
else if (Config->Pic)
|
|
|
|
Conf.RelocModel = Reloc::PIC_;
|
|
|
|
else
|
|
|
|
Conf.RelocModel = Reloc::Static;
|
2017-03-01 07:43:26 +08:00
|
|
|
Conf.CodeModel = GetCodeModelFromCMModel();
|
2016-09-29 08:40:08 +08:00
|
|
|
Conf.DisableVerify = Config->DisableVerify;
|
|
|
|
Conf.DiagHandler = diagnosticHandler;
|
2016-11-26 13:37:04 +08:00
|
|
|
Conf.OptLevel = Config->LTOO;
|
2016-05-16 03:29:38 +08:00
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
// Set up a custom pipeline if we've been asked to.
|
2016-11-26 13:37:04 +08:00
|
|
|
Conf.OptPipeline = Config->LTONewPmPasses;
|
|
|
|
Conf.AAPipeline = Config->LTOAAPipeline;
|
2016-03-23 04:52:10 +08:00
|
|
|
|
2017-02-14 01:49:18 +08:00
|
|
|
// Set up optimization remarks if we've been asked to.
|
|
|
|
Conf.RemarksFilename = Config->OptRemarksFilename;
|
|
|
|
Conf.RemarksWithHotness = Config->OptRemarksWithHotness;
|
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
if (Config->SaveTemps)
|
2016-09-30 05:00:26 +08:00
|
|
|
checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".",
|
2016-10-20 16:36:42 +08:00
|
|
|
/*UseInputModulePath*/ true));
|
2016-03-23 04:52:10 +08:00
|
|
|
|
2016-10-11 02:12:53 +08:00
|
|
|
lto::ThinBackend Backend;
|
2016-11-26 13:37:04 +08:00
|
|
|
if (Config->ThinLTOJobs != -1u)
|
|
|
|
Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
|
2016-10-11 07:12:14 +08:00
|
|
|
return llvm::make_unique<lto::LTO>(std::move(Conf), Backend,
|
2016-11-26 13:37:04 +08:00
|
|
|
Config->LTOPartitions);
|
2016-04-22 04:35:25 +08:00
|
|
|
}
|
|
|
|
|
2017-07-27 07:39:10 +08:00
|
|
|
BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {
|
2017-11-04 05:21:47 +08:00
|
|
|
for (Symbol *Sym : Symtab->getSymbols()) {
|
2017-11-01 00:07:41 +08:00
|
|
|
StringRef Name = Sym->getName();
|
2017-07-27 07:39:10 +08:00
|
|
|
for (StringRef Prefix : {"__start_", "__stop_"})
|
|
|
|
if (Name.startswith(Prefix))
|
|
|
|
UsedStartStop.insert(Name.substr(Prefix.size()));
|
|
|
|
}
|
|
|
|
}
|
2016-09-29 08:40:08 +08:00
|
|
|
|
2016-11-05 09:00:56 +08:00
|
|
|
BitcodeCompiler::~BitcodeCompiler() = default;
|
2016-04-23 05:16:18 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
static void undefine(Symbol *S) {
|
2017-11-17 09:37:50 +08:00
|
|
|
replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_GLOBAL, STV_DEFAULT,
|
|
|
|
S->Type);
|
2016-05-13 03:46:14 +08:00
|
|
|
}
|
|
|
|
|
2017-02-01 18:26:03 +08:00
|
|
|
void BitcodeCompiler::add(BitcodeFile &F) {
|
2016-09-29 08:40:08 +08:00
|
|
|
lto::InputFile &Obj = *F.Obj;
|
|
|
|
unsigned SymNum = 0;
|
2017-11-04 05:21:47 +08:00
|
|
|
std::vector<Symbol *> Syms = F.getSymbols();
|
2016-09-29 08:40:08 +08:00
|
|
|
std::vector<lto::SymbolResolution> Resols(Syms.size());
|
|
|
|
|
2017-09-25 17:31:43 +08:00
|
|
|
DenseSet<StringRef> ScriptSymbols;
|
2017-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *Base : Script->SectionCommands)
|
2017-09-25 17:31:43 +08:00
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base))
|
|
|
|
ScriptSymbols.insert(Cmd->Name);
|
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
// 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 = Syms[SymNum];
|
2016-09-29 08:40:08 +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-30 06:47:35 +08:00
|
|
|
R.Prevailing = !ObjSym.isUndefined() && Sym->File == &F;
|
2016-09-29 08:40:08 +08:00
|
|
|
|
2017-08-22 16:36:54 +08:00
|
|
|
// We ask LTO to preserve following global symbols:
|
|
|
|
// 1) All symbols when doing relocatable link, so that them can be used
|
|
|
|
// for doing final link.
|
|
|
|
// 2) Symbols that are used in regular objects.
|
|
|
|
// 3) C named sections if we have corresponding __start_/__stop_ symbol.
|
|
|
|
// 4) Symbols that are defined in bitcode files and used for dynamic linking.
|
|
|
|
R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj ||
|
2017-07-26 06:51:05 +08:00
|
|
|
(R.Prevailing && Sym->includeInDynsym()) ||
|
2017-07-27 07:39:10 +08:00
|
|
|
UsedStartStop.count(ObjSym.getSectionName());
|
2016-09-29 08:40:08 +08:00
|
|
|
if (R.Prevailing)
|
2017-02-01 18:26:03 +08:00
|
|
|
undefine(Sym);
|
2017-09-25 17:31:43 +08:00
|
|
|
|
|
|
|
// We tell LTO to not apply interprocedural optimization for following
|
|
|
|
// symbols because otherwise LTO would inline them while their values are
|
|
|
|
// still not final:
|
|
|
|
// 1) Aliased (with --defsym) or wrapped (with --wrap) symbols.
|
|
|
|
// 2) Symbols redefined in linker script.
|
2017-11-01 00:07:41 +08:00
|
|
|
R.LinkerRedefined = !Sym->CanInline || ScriptSymbols.count(Sym->getName());
|
2016-07-15 10:17:13 +08:00
|
|
|
}
|
2016-11-26 13:37:04 +08:00
|
|
|
checkError(LTOObj->add(std::move(F.Obj), Resols));
|
2016-04-16 06:38:10 +08:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
// Merge all the bitcode files we have seen, codegen the result
|
2016-09-29 08:40:08 +08:00
|
|
|
// and return the resulting ObjectFile(s).
|
2016-09-14 08:05:51 +08:00
|
|
|
std::vector<InputFile *> BitcodeCompiler::compile() {
|
2016-09-29 08:40:08 +08:00
|
|
|
std::vector<InputFile *> Ret;
|
2016-11-26 13:37:04 +08:00
|
|
|
unsigned MaxTasks = LTOObj->getMaxTasks();
|
2016-09-29 08:40:08 +08:00
|
|
|
Buff.resize(MaxTasks);
|
2017-03-02 07:00:10 +08:00
|
|
|
Files.resize(MaxTasks);
|
|
|
|
|
|
|
|
// The --thinlto-cache-dir 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->ThinLTOCacheDir.empty())
|
2017-03-17 08:34:07 +08:00
|
|
|
Cache = check(
|
|
|
|
lto::localCache(Config->ThinLTOCacheDir,
|
2017-09-06 03:51:38 +08:00
|
|
|
[&](size_t Task, std::unique_ptr<MemoryBuffer> MB,
|
|
|
|
StringRef Path) { Files[Task] = std::move(MB); }));
|
2017-03-02 07:00:10 +08:00
|
|
|
|
|
|
|
checkError(LTOObj->run(
|
|
|
|
[&](size_t Task) {
|
|
|
|
return llvm::make_unique<lto::NativeObjectStream>(
|
|
|
|
llvm::make_unique<raw_svector_ostream>(Buff[Task]));
|
|
|
|
},
|
|
|
|
Cache));
|
2016-04-18 07:20:08 +08:00
|
|
|
|
2017-03-17 10:24:16 +08:00
|
|
|
if (!Config->ThinLTOCacheDir.empty())
|
|
|
|
pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy);
|
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
for (unsigned I = 0; I != MaxTasks; ++I) {
|
|
|
|
if (Buff[I].empty())
|
|
|
|
continue;
|
|
|
|
if (Config->SaveTemps) {
|
2017-01-26 10:18:28 +08:00
|
|
|
if (I == 0)
|
2016-09-29 08:40:08 +08:00
|
|
|
saveBuffer(Buff[I], Config->OutputFile + ".lto.o");
|
|
|
|
else
|
|
|
|
saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o");
|
|
|
|
}
|
2016-10-29 04:57:25 +08:00
|
|
|
InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp"));
|
2016-09-29 08:40:08 +08:00
|
|
|
Ret.push_back(Obj);
|
|
|
|
}
|
2017-03-02 07:00:10 +08:00
|
|
|
|
|
|
|
for (std::unique_ptr<MemoryBuffer> &File : Files)
|
|
|
|
if (File)
|
|
|
|
Ret.push_back(createObjectFile(*File));
|
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
return Ret;
|
2016-03-24 05:19:27 +08:00
|
|
|
}
|