2016-03-23 04:52:10 +08:00
|
|
|
//===- LTO.cpp ------------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-03-23 04:52:10 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
2018-05-03 05:40:07 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.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 <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;
|
|
|
|
|
2018-05-08 01:46:28 +08:00
|
|
|
// Creates an empty file to store a list of object files for final
|
2018-05-03 05:40:07 +08:00
|
|
|
// linking of distributed ThinLTO.
|
2018-05-08 01:46:28 +08:00
|
|
|
static std::unique_ptr<raw_fd_ostream> openFile(StringRef File) {
|
2018-05-03 05:40:07 +08:00
|
|
|
std::error_code EC;
|
2018-05-08 01:46:28 +08:00
|
|
|
auto Ret =
|
|
|
|
llvm::make_unique<raw_fd_ostream>(File, EC, sys::fs::OpenFlags::F_None);
|
2018-05-08 06:11:24 +08:00
|
|
|
if (EC) {
|
|
|
|
error("cannot open " + File + ": " + EC.message());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-05-08 01:46:28 +08:00
|
|
|
return Ret;
|
2018-05-03 05:40:07 +08:00
|
|
|
}
|
|
|
|
|
2018-05-08 06:11:24 +08:00
|
|
|
static std::string getThinLTOOutputFile(StringRef ModulePath) {
|
|
|
|
return lto::getThinLTOOutputFile(ModulePath,
|
|
|
|
Config->ThinLTOPrefixReplace.first,
|
2018-05-08 07:14:12 +08:00
|
|
|
Config->ThinLTOPrefixReplace.second);
|
2018-05-08 06:11:24 +08:00
|
|
|
}
|
|
|
|
|
2018-05-08 07:24:07 +08:00
|
|
|
static lto::Config createConfig() {
|
|
|
|
lto::Config C;
|
2016-05-16 03:29:38 +08:00
|
|
|
|
2018-08-07 04:12:12 +08:00
|
|
|
// LLD supports the new relocations and address-significance tables.
|
2018-05-08 07:24:07 +08:00
|
|
|
C.Options = InitTargetOptionsFromCodeGenFlags();
|
|
|
|
C.Options.RelaxELFRelocations = true;
|
2018-08-07 04:12:12 +08:00
|
|
|
C.Options.EmitAddrsig = 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.
|
2018-05-08 07:24:07 +08:00
|
|
|
C.Options.FunctionSections = true;
|
|
|
|
C.Options.DataSections = true;
|
2017-07-25 03:38:13 +08:00
|
|
|
|
2017-05-23 05:11:44 +08:00
|
|
|
if (Config->Relocatable)
|
2018-05-08 07:24:07 +08:00
|
|
|
C.RelocModel = None;
|
2017-05-23 05:11:44 +08:00
|
|
|
else if (Config->Pic)
|
2018-05-08 07:24:07 +08:00
|
|
|
C.RelocModel = Reloc::PIC_;
|
2017-05-23 05:11:44 +08:00
|
|
|
else
|
2018-05-08 07:24:07 +08:00
|
|
|
C.RelocModel = Reloc::Static;
|
|
|
|
|
|
|
|
C.CodeModel = GetCodeModelFromCMModel();
|
|
|
|
C.DisableVerify = Config->DisableVerify;
|
|
|
|
C.DiagHandler = diagnosticHandler;
|
|
|
|
C.OptLevel = Config->LTOO;
|
|
|
|
C.CPU = GetCPUStr();
|
Set MAttrs in LTO mode
Summary: Without this patch, MAttrs are not set.
Patch by Yin Ma
Reviewers: espindola, MaskRay, ruiu, pcc
Reviewed By: MaskRay, pcc
Subscribers: pcc, emaste, sbc100, inglorion, arichardson, aheejin, steven_wu, llvm-commits
Differential Revision: https://reviews.llvm.org/D53446
llvm-svn: 345884
2018-11-02 04:02:49 +08:00
|
|
|
C.MAttrs = GetMAttrs();
|
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.
|
2018-05-08 07:24:07 +08:00
|
|
|
C.OptPipeline = Config->LTONewPmPasses;
|
|
|
|
C.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.
|
2018-05-08 07:24:07 +08:00
|
|
|
C.RemarksFilename = Config->OptRemarksFilename;
|
|
|
|
C.RemarksWithHotness = Config->OptRemarksWithHotness;
|
|
|
|
|
|
|
|
C.SampleProfile = Config->LTOSampleProfile;
|
|
|
|
C.UseNewPM = Config->LTONewPassManager;
|
|
|
|
C.DebugPassManager = Config->LTODebugPassManager;
|
2018-07-17 01:55:48 +08:00
|
|
|
C.DwoDir = Config->DwoDir;
|
2017-02-14 01:49:18 +08:00
|
|
|
|
2018-12-15 05:58:49 +08:00
|
|
|
if (Config->EmitLLVM) {
|
|
|
|
C.PostInternalizeModuleHook = [](size_t Task, const Module &M) {
|
|
|
|
if (std::unique_ptr<raw_fd_ostream> OS = openFile(Config->OutputFile))
|
|
|
|
WriteBitcodeToFile(M, *OS, false);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
if (Config->SaveTemps)
|
2018-05-08 07:24:07 +08:00
|
|
|
checkError(C.addSaveTemps(Config->OutputFile.str() + ".",
|
|
|
|
/*UseInputModulePath*/ true));
|
|
|
|
return C;
|
|
|
|
}
|
2016-03-23 04:52:10 +08:00
|
|
|
|
2018-05-08 07:24:07 +08:00
|
|
|
BitcodeCompiler::BitcodeCompiler() {
|
2018-09-11 22:37:27 +08:00
|
|
|
// Initialize IndexFile.
|
|
|
|
if (!Config->ThinLTOIndexOnlyArg.empty())
|
|
|
|
IndexFile = openFile(Config->ThinLTOIndexOnlyArg);
|
|
|
|
|
2018-05-08 07:24:07 +08:00
|
|
|
// Initialize LTOObj.
|
2016-10-11 02:12:53 +08:00
|
|
|
lto::ThinBackend Backend;
|
2018-05-03 05:40:07 +08:00
|
|
|
if (Config->ThinLTOIndexOnly) {
|
2018-09-11 22:37:27 +08:00
|
|
|
auto OnIndexWrite = [&](StringRef S) { ThinIndices.erase(S); };
|
2018-05-08 01:59:43 +08:00
|
|
|
Backend = lto::createWriteIndexesThinBackend(
|
|
|
|
Config->ThinLTOPrefixReplace.first, Config->ThinLTOPrefixReplace.second,
|
2018-05-09 04:12:07 +08:00
|
|
|
Config->ThinLTOEmitImportsFiles, IndexFile.get(), OnIndexWrite);
|
2018-05-08 07:24:07 +08:00
|
|
|
} else if (Config->ThinLTOJobs != -1U) {
|
|
|
|
Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
|
2018-05-03 05:40:07 +08:00
|
|
|
}
|
|
|
|
|
2018-05-08 07:24:07 +08:00
|
|
|
LTOObj = llvm::make_unique<lto::LTO>(createConfig(), Backend,
|
2018-05-08 01:46:28 +08:00
|
|
|
Config->LTOPartitions);
|
|
|
|
|
2018-05-08 07:24:07 +08:00
|
|
|
// Initialize UsedStartStop.
|
2017-11-04 05:21:47 +08:00
|
|
|
for (Symbol *Sym : Symtab->getSymbols()) {
|
2018-09-11 22:37:27 +08:00
|
|
|
StringRef S = Sym->getName();
|
2017-07-27 07:39:10 +08:00
|
|
|
for (StringRef Prefix : {"__start_", "__stop_"})
|
2018-09-11 22:37:27 +08:00
|
|
|
if (S.startswith(Prefix))
|
|
|
|
UsedStartStop.insert(S.substr(Prefix.size()));
|
2017-07-27 07:39:10 +08:00
|
|
|
}
|
|
|
|
}
|
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;
|
2018-05-09 01:50:54 +08:00
|
|
|
bool IsExec = !Config->Shared && !Config->Relocatable;
|
2018-05-03 05:40:07 +08:00
|
|
|
|
2018-05-09 04:12:07 +08:00
|
|
|
if (Config->ThinLTOIndexOnly)
|
2018-09-11 22:37:27 +08:00
|
|
|
ThinIndices.insert(Obj.getName());
|
2018-05-03 05:40:07 +08:00
|
|
|
|
2018-05-09 01:50:43 +08:00
|
|
|
ArrayRef<Symbol *> Syms = F.getSymbols();
|
2018-05-09 01:50:54 +08:00
|
|
|
ArrayRef<lto::InputFile::Symbol> ObjSyms = Obj.symbols();
|
2016-09-29 08:40:08 +08:00
|
|
|
std::vector<lto::SymbolResolution> Resols(Syms.size());
|
|
|
|
|
|
|
|
// Provide a resolution to the LTO API for each symbol.
|
2018-05-09 01:50:54 +08:00
|
|
|
for (size_t I = 0, E = Syms.size(); I != E; ++I) {
|
|
|
|
Symbol *Sym = Syms[I];
|
|
|
|
const lto::InputFile::Symbol &ObjSym = ObjSyms[I];
|
|
|
|
lto::SymbolResolution &R = Resols[I];
|
2016-09-29 08:40:08 +08:00
|
|
|
|
|
|
|
// 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());
|
2018-02-07 08:49:51 +08:00
|
|
|
const auto *DR = dyn_cast<Defined>(Sym);
|
2018-01-17 00:49:05 +08:00
|
|
|
R.FinalDefinitionInLinkageUnit =
|
2018-05-09 01:50:54 +08:00
|
|
|
(IsExec || Sym->Visibility != STV_DEFAULT) && DR &&
|
2018-02-07 08:49:51 +08:00
|
|
|
// Skip absolute symbols from ELF objects, otherwise PC-rel relocations
|
|
|
|
// will be generated by for them, triggering linker errors.
|
|
|
|
// Symbol section is always null for bitcode symbols, hence the check
|
2018-02-08 12:25:52 +08:00
|
|
|
// for isElf(). Skip linker script defined symbols as well: they have
|
|
|
|
// no File defined.
|
|
|
|
!(DR->Section == nullptr && (!Sym->File || Sym->File->isElf()));
|
2018-01-17 00:49:05 +08:00
|
|
|
|
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
|
|
|
|
2018-01-30 17:04:27 +08:00
|
|
|
// We tell LTO to not apply interprocedural optimization for wrapped
|
|
|
|
// (with --wrap) symbols because otherwise LTO would inline them while
|
|
|
|
// their values are still not final.
|
|
|
|
R.LinkerRedefined = !Sym->CanInline;
|
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
|
|
|
}
|
|
|
|
|
2018-05-18 02:27:12 +08:00
|
|
|
static void createEmptyIndex(StringRef ModulePath) {
|
|
|
|
std::string Path = replaceThinLTOSuffix(getThinLTOOutputFile(ModulePath));
|
|
|
|
std::unique_ptr<raw_fd_ostream> OS = openFile(Path + ".thinlto.bc");
|
|
|
|
if (!OS)
|
|
|
|
return;
|
|
|
|
|
2018-06-07 06:22:13 +08:00
|
|
|
ModuleSummaryIndex M(/*HaveGVs*/ false);
|
2018-05-18 02:27:12 +08:00
|
|
|
M.setSkipModuleByDistributedBackend();
|
|
|
|
WriteIndexToFile(M, *OS);
|
|
|
|
|
|
|
|
if (Config->ThinLTOEmitImportsFiles)
|
|
|
|
openFile(Path + ".imports");
|
|
|
|
}
|
|
|
|
|
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-11-26 13:37:04 +08:00
|
|
|
unsigned MaxTasks = LTOObj->getMaxTasks();
|
2018-05-18 02:27:12 +08:00
|
|
|
Buf.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,
|
2018-02-21 04:21:59 +08:00
|
|
|
[&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
|
|
|
|
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>(
|
2018-05-18 02:27:12 +08:00
|
|
|
llvm::make_unique<raw_svector_ostream>(Buf[Task]));
|
2017-03-02 07:00:10 +08:00
|
|
|
},
|
|
|
|
Cache));
|
2016-04-18 07:20:08 +08:00
|
|
|
|
2018-05-09 04:12:07 +08:00
|
|
|
// Emit empty index files for non-indexed files
|
2018-09-11 22:37:27 +08:00
|
|
|
for (StringRef S : ThinIndices) {
|
|
|
|
std::string Path = getThinLTOOutputFile(S);
|
|
|
|
openFile(Path + ".thinlto.bc");
|
|
|
|
if (Config->ThinLTOEmitImportsFiles)
|
|
|
|
openFile(Path + ".imports");
|
2016-09-29 08:40:08 +08:00
|
|
|
}
|
2017-03-02 07:00:10 +08:00
|
|
|
|
2018-05-08 06:11:24 +08:00
|
|
|
// If LazyObjFile has not been added to link, emit empty index files.
|
|
|
|
// This is needed because this is what GNU gold plugin does and we have a
|
|
|
|
// distributed build system that depends on that behavior.
|
|
|
|
if (Config->ThinLTOIndexOnly) {
|
2018-05-18 02:27:12 +08:00
|
|
|
for (LazyObjFile *F : LazyObjFiles)
|
|
|
|
if (!F->AddedToLink && isBitcode(F->MB))
|
|
|
|
createEmptyIndex(F->getName());
|
2018-05-08 06:11:24 +08:00
|
|
|
|
2018-05-09 06:37:57 +08:00
|
|
|
if (!Config->LTOObjPath.empty())
|
2018-05-18 02:27:12 +08:00
|
|
|
saveBuffer(Buf[0], Config->LTOObjPath);
|
2018-05-09 04:12:07 +08:00
|
|
|
|
2018-05-08 06:11:24 +08:00
|
|
|
// ThinLTO with index only option is required to generate only the index
|
|
|
|
// files. After that, we exit from linker and ThinLTO backend runs in a
|
|
|
|
// distributed environment.
|
2018-05-08 06:11:34 +08:00
|
|
|
if (IndexFile)
|
|
|
|
IndexFile->close();
|
|
|
|
return {};
|
2018-05-08 06:11:24 +08:00
|
|
|
}
|
2018-05-18 02:27:12 +08:00
|
|
|
|
2018-05-09 04:12:07 +08:00
|
|
|
if (!Config->ThinLTOCacheDir.empty())
|
|
|
|
pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy);
|
|
|
|
|
|
|
|
std::vector<InputFile *> Ret;
|
|
|
|
for (unsigned I = 0; I != MaxTasks; ++I) {
|
2018-05-18 02:27:12 +08:00
|
|
|
if (Buf[I].empty())
|
2018-05-09 04:12:07 +08:00
|
|
|
continue;
|
|
|
|
if (Config->SaveTemps) {
|
|
|
|
if (I == 0)
|
2018-05-18 02:27:12 +08:00
|
|
|
saveBuffer(Buf[I], Config->OutputFile + ".lto.o");
|
2018-05-09 04:12:07 +08:00
|
|
|
else
|
2018-05-18 02:27:12 +08:00
|
|
|
saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.o");
|
2018-05-09 04:12:07 +08:00
|
|
|
}
|
2018-05-18 02:27:12 +08:00
|
|
|
InputFile *Obj = createObjectFile(MemoryBufferRef(Buf[I], "lto.tmp"));
|
2018-05-09 04:12:07 +08:00
|
|
|
Ret.push_back(Obj);
|
|
|
|
}
|
2018-05-03 05:40:07 +08:00
|
|
|
|
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
|
|
|
}
|