1 //===- LTO.cpp ------------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "InputFiles.h"
13 #include "LinkerScript.h"
14 #include "SymbolTable.h"
16 #include "lld/Common/ErrorHandler.h"
17 #include "lld/Common/TargetOptionsCommandFlags.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/BinaryFormat/ELF.h"
23 #include "llvm/Bitcode/BitcodeReader.h"
24 #include "llvm/Bitcode/BitcodeWriter.h"
25 #include "llvm/IR/DiagnosticPrinter.h"
26 #include "llvm/LTO/Caching.h"
27 #include "llvm/LTO/Config.h"
28 #include "llvm/LTO/LTO.h"
29 #include "llvm/Object/SymbolicFile.h"
30 #include "llvm/Support/CodeGen.h"
31 #include "llvm/Support/Error.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/MemoryBuffer.h"
38 #include <system_error>
42 using namespace llvm::object;
43 using namespace llvm::ELF;
46 using namespace lld::elf;
48 // Creates an empty file to store a list of object files for final
49 // linking of distributed ThinLTO.
50 static std::unique_ptr<raw_fd_ostream> openFile(StringRef File) {
53 llvm::make_unique<raw_fd_ostream>(File, EC, sys::fs::OpenFlags::F_None);
55 error("cannot open " + File + ": " + EC.message());
61 static std::string getThinLTOOutputFile(StringRef ModulePath) {
62 return lto::getThinLTOOutputFile(ModulePath,
63 Config->ThinLTOPrefixReplace.first,
64 Config->ThinLTOPrefixReplace.second);
67 static lto::Config createConfig() {
70 // LLD supports the new relocations and address-significance tables.
71 C.Options = InitTargetOptionsFromCodeGenFlags();
72 C.Options.RelaxELFRelocations = true;
73 C.Options.EmitAddrsig = true;
75 // Always emit a section per function/datum with LTO.
76 C.Options.FunctionSections = true;
77 C.Options.DataSections = true;
79 if (Config->Relocatable)
82 C.RelocModel = Reloc::PIC_;
84 C.RelocModel = Reloc::Static;
86 C.CodeModel = GetCodeModelFromCMModel();
87 C.DisableVerify = Config->DisableVerify;
88 C.DiagHandler = diagnosticHandler;
89 C.OptLevel = Config->LTOO;
91 C.MAttrs = GetMAttrs();
93 // Set up a custom pipeline if we've been asked to.
94 C.OptPipeline = Config->LTONewPmPasses;
95 C.AAPipeline = Config->LTOAAPipeline;
97 // Set up optimization remarks if we've been asked to.
98 C.RemarksFilename = Config->OptRemarksFilename;
99 C.RemarksWithHotness = Config->OptRemarksWithHotness;
101 C.SampleProfile = Config->LTOSampleProfile;
102 C.UseNewPM = Config->LTONewPassManager;
103 C.DebugPassManager = Config->LTODebugPassManager;
104 C.DwoDir = Config->DwoDir;
106 if (Config->EmitLLVM) {
107 C.PostInternalizeModuleHook = [](size_t Task, const Module &M) {
108 if (std::unique_ptr<raw_fd_ostream> OS = openFile(Config->OutputFile))
109 WriteBitcodeToFile(M, *OS, false);
114 if (Config->SaveTemps)
115 checkError(C.addSaveTemps(Config->OutputFile.str() + ".",
116 /*UseInputModulePath*/ true));
120 BitcodeCompiler::BitcodeCompiler() {
121 // Initialize IndexFile.
122 if (!Config->ThinLTOIndexOnlyArg.empty())
123 IndexFile = openFile(Config->ThinLTOIndexOnlyArg);
125 // Initialize LTOObj.
126 lto::ThinBackend Backend;
127 if (Config->ThinLTOIndexOnly) {
128 auto OnIndexWrite = [&](StringRef S) { ThinIndices.erase(S); };
129 Backend = lto::createWriteIndexesThinBackend(
130 Config->ThinLTOPrefixReplace.first, Config->ThinLTOPrefixReplace.second,
131 Config->ThinLTOEmitImportsFiles, IndexFile.get(), OnIndexWrite);
132 } else if (Config->ThinLTOJobs != -1U) {
133 Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
136 LTOObj = llvm::make_unique<lto::LTO>(createConfig(), Backend,
137 Config->LTOPartitions);
139 // Initialize UsedStartStop.
140 for (Symbol *Sym : Symtab->getSymbols()) {
141 StringRef S = Sym->getName();
142 for (StringRef Prefix : {"__start_", "__stop_"})
143 if (S.startswith(Prefix))
144 UsedStartStop.insert(S.substr(Prefix.size()));
148 BitcodeCompiler::~BitcodeCompiler() = default;
150 static void undefine(Symbol *S) {
151 replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_GLOBAL, STV_DEFAULT,
155 void BitcodeCompiler::add(BitcodeFile &F) {
156 lto::InputFile &Obj = *F.Obj;
157 bool IsExec = !Config->Shared && !Config->Relocatable;
159 if (Config->ThinLTOIndexOnly)
160 ThinIndices.insert(Obj.getName());
162 ArrayRef<Symbol *> Syms = F.getSymbols();
163 ArrayRef<lto::InputFile::Symbol> ObjSyms = Obj.symbols();
164 std::vector<lto::SymbolResolution> Resols(Syms.size());
166 // Provide a resolution to the LTO API for each symbol.
167 for (size_t I = 0, E = Syms.size(); I != E; ++I) {
168 Symbol *Sym = Syms[I];
169 const lto::InputFile::Symbol &ObjSym = ObjSyms[I];
170 lto::SymbolResolution &R = Resols[I];
172 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
173 // reports two symbols for module ASM defined. Without this check, lld
174 // flags an undefined in IR with a definition in ASM as prevailing.
175 // Once IRObjectFile is fixed to report only one symbol this hack can
177 R.Prevailing = !ObjSym.isUndefined() && Sym->File == &F;
179 // We ask LTO to preserve following global symbols:
180 // 1) All symbols when doing relocatable link, so that them can be used
181 // for doing final link.
182 // 2) Symbols that are used in regular objects.
183 // 3) C named sections if we have corresponding __start_/__stop_ symbol.
184 // 4) Symbols that are defined in bitcode files and used for dynamic linking.
185 R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj ||
186 (R.Prevailing && Sym->includeInDynsym()) ||
187 UsedStartStop.count(ObjSym.getSectionName());
188 const auto *DR = dyn_cast<Defined>(Sym);
189 R.FinalDefinitionInLinkageUnit =
190 (IsExec || Sym->Visibility != STV_DEFAULT) && DR &&
191 // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
192 // will be generated by for them, triggering linker errors.
193 // Symbol section is always null for bitcode symbols, hence the check
194 // for isElf(). Skip linker script defined symbols as well: they have
196 !(DR->Section == nullptr && (!Sym->File || Sym->File->isElf()));
201 // We tell LTO to not apply interprocedural optimization for wrapped
202 // (with --wrap) symbols because otherwise LTO would inline them while
203 // their values are still not final.
204 R.LinkerRedefined = !Sym->CanInline;
206 checkError(LTOObj->add(std::move(F.Obj), Resols));
209 static void createEmptyIndex(StringRef ModulePath) {
210 std::string Path = replaceThinLTOSuffix(getThinLTOOutputFile(ModulePath));
211 std::unique_ptr<raw_fd_ostream> OS = openFile(Path + ".thinlto.bc");
215 ModuleSummaryIndex M(/*HaveGVs*/ false);
216 M.setSkipModuleByDistributedBackend();
217 WriteIndexToFile(M, *OS);
219 if (Config->ThinLTOEmitImportsFiles)
220 openFile(Path + ".imports");
223 // Merge all the bitcode files we have seen, codegen the result
224 // and return the resulting ObjectFile(s).
225 std::vector<InputFile *> BitcodeCompiler::compile() {
226 unsigned MaxTasks = LTOObj->getMaxTasks();
227 Buf.resize(MaxTasks);
228 Files.resize(MaxTasks);
230 // The --thinlto-cache-dir option specifies the path to a directory in which
231 // to cache native object files for ThinLTO incremental builds. If a path was
232 // specified, configure LTO to use it as the cache directory.
233 lto::NativeObjectCache Cache;
234 if (!Config->ThinLTOCacheDir.empty())
236 lto::localCache(Config->ThinLTOCacheDir,
237 [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
238 Files[Task] = std::move(MB);
241 checkError(LTOObj->run(
243 return llvm::make_unique<lto::NativeObjectStream>(
244 llvm::make_unique<raw_svector_ostream>(Buf[Task]));
248 // Emit empty index files for non-indexed files
249 for (StringRef S : ThinIndices) {
250 std::string Path = getThinLTOOutputFile(S);
251 openFile(Path + ".thinlto.bc");
252 if (Config->ThinLTOEmitImportsFiles)
253 openFile(Path + ".imports");
256 // If LazyObjFile has not been added to link, emit empty index files.
257 // This is needed because this is what GNU gold plugin does and we have a
258 // distributed build system that depends on that behavior.
259 if (Config->ThinLTOIndexOnly) {
260 for (LazyObjFile *F : LazyObjFiles)
261 if (!F->AddedToLink && isBitcode(F->MB))
262 createEmptyIndex(F->getName());
264 if (!Config->LTOObjPath.empty())
265 saveBuffer(Buf[0], Config->LTOObjPath);
267 // ThinLTO with index only option is required to generate only the index
268 // files. After that, we exit from linker and ThinLTO backend runs in a
269 // distributed environment.
275 if (!Config->ThinLTOCacheDir.empty())
276 pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy);
278 std::vector<InputFile *> Ret;
279 for (unsigned I = 0; I != MaxTasks; ++I) {
282 if (Config->SaveTemps) {
284 saveBuffer(Buf[I], Config->OutputFile + ".lto.o");
286 saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.o");
288 InputFile *Obj = createObjectFile(MemoryBufferRef(Buf[I], "lto.tmp"));
292 for (std::unique_ptr<MemoryBuffer> &File : Files)
294 Ret.push_back(createObjectFile(*File));