xref: /llvm-project-15.0.7/lld/ELF/LTO.cpp (revision f489e2bf)
1 //===- LTO.cpp ------------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "LTO.h"
11 #include "Config.h"
12 #include "InputFiles.h"
13 #include "LinkerScript.h"
14 #include "SymbolTable.h"
15 #include "Symbols.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"
34 #include <algorithm>
35 #include <cstddef>
36 #include <memory>
37 #include <string>
38 #include <system_error>
39 #include <vector>
40 
41 using namespace llvm;
42 using namespace llvm::object;
43 using namespace llvm::ELF;
44 
45 using namespace lld;
46 using namespace lld::elf;
47 
48 // This is for use when debugging LTO.
49 static void saveBuffer(StringRef Buffer, const Twine &Path) {
50   std::error_code EC;
51   raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
52   if (EC)
53     error("cannot create " + Path + ": " + EC.message());
54   OS << Buffer;
55 }
56 
57 static void diagnosticHandler(const DiagnosticInfo &DI) {
58   SmallString<128> S;
59   raw_svector_ostream OS(S);
60   DiagnosticPrinterRawOStream DP(OS);
61   DI.print(DP);
62   warn(S);
63 }
64 
65 static void checkError(Error E) {
66   handleAllErrors(std::move(E),
67                   [&](ErrorInfoBase &EIB) { error(EIB.message()); });
68 }
69 
70 // Creates an empty file to store a list of object files for final
71 // linking of distributed ThinLTO.
72 static std::unique_ptr<raw_fd_ostream> openFile(StringRef File) {
73   std::error_code EC;
74   auto Ret =
75       llvm::make_unique<raw_fd_ostream>(File, EC, sys::fs::OpenFlags::F_None);
76   if (EC) {
77     error("cannot open " + File + ": " + EC.message());
78     return nullptr;
79   }
80   return Ret;
81 }
82 
83 static std::string getThinLTOOutputFile(StringRef ModulePath) {
84   return lto::getThinLTOOutputFile(ModulePath,
85                                    Config->ThinLTOPrefixReplace.first,
86                                    Config->ThinLTOPrefixReplace.second);
87 }
88 
89 static lto::Config createConfig() {
90   lto::Config C;
91 
92   // LLD supports the new relocations.
93   C.Options = InitTargetOptionsFromCodeGenFlags();
94   C.Options.RelaxELFRelocations = true;
95 
96   // Always emit a section per function/datum with LTO.
97   C.Options.FunctionSections = true;
98   C.Options.DataSections = true;
99 
100   if (Config->Relocatable)
101     C.RelocModel = None;
102   else if (Config->Pic)
103     C.RelocModel = Reloc::PIC_;
104   else
105     C.RelocModel = Reloc::Static;
106 
107   C.CodeModel = GetCodeModelFromCMModel();
108   C.DisableVerify = Config->DisableVerify;
109   C.DiagHandler = diagnosticHandler;
110   C.OptLevel = Config->LTOO;
111   C.CPU = GetCPUStr();
112 
113   // Set up a custom pipeline if we've been asked to.
114   C.OptPipeline = Config->LTONewPmPasses;
115   C.AAPipeline = Config->LTOAAPipeline;
116 
117   // Set up optimization remarks if we've been asked to.
118   C.RemarksFilename = Config->OptRemarksFilename;
119   C.RemarksWithHotness = Config->OptRemarksWithHotness;
120 
121   C.SampleProfile = Config->LTOSampleProfile;
122   C.UseNewPM = Config->LTONewPassManager;
123   C.DebugPassManager = Config->LTODebugPassManager;
124 
125   if (Config->SaveTemps)
126     checkError(C.addSaveTemps(Config->OutputFile.str() + ".",
127                               /*UseInputModulePath*/ true));
128   return C;
129 }
130 
131 BitcodeCompiler::BitcodeCompiler() {
132   // Initialize LTOObj.
133   lto::ThinBackend Backend;
134 
135   if (Config->ThinLTOIndexOnly) {
136     StringRef Path = Config->ThinLTOIndexOnlyArg;
137     if (!Path.empty())
138       IndexFile = openFile(Path);
139 
140     auto OnIndexWrite = [&](const std::string &Identifier) {
141       ObjectToIndexFileState[Identifier] = true;
142     };
143 
144     Backend = lto::createWriteIndexesThinBackend(
145         Config->ThinLTOPrefixReplace.first, Config->ThinLTOPrefixReplace.second,
146         Config->ThinLTOEmitImportsFiles, IndexFile.get(), OnIndexWrite);
147   } else if (Config->ThinLTOJobs != -1U) {
148     Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
149   }
150 
151   LTOObj = llvm::make_unique<lto::LTO>(createConfig(), Backend,
152                                        Config->LTOPartitions);
153 
154   // Initialize UsedStartStop.
155   for (Symbol *Sym : Symtab->getSymbols()) {
156     StringRef Name = Sym->getName();
157     for (StringRef Prefix : {"__start_", "__stop_"})
158       if (Name.startswith(Prefix))
159         UsedStartStop.insert(Name.substr(Prefix.size()));
160   }
161 }
162 
163 BitcodeCompiler::~BitcodeCompiler() = default;
164 
165 static void undefine(Symbol *S) {
166   replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_GLOBAL, STV_DEFAULT,
167                            S->Type);
168 }
169 
170 void BitcodeCompiler::add(BitcodeFile &F) {
171   lto::InputFile &Obj = *F.Obj;
172   bool IsExec = !Config->Shared && !Config->Relocatable;
173 
174   if (Config->ThinLTOIndexOnly)
175     ObjectToIndexFileState.insert({Obj.getName(), false});
176 
177   ArrayRef<Symbol *> Syms = F.getSymbols();
178   ArrayRef<lto::InputFile::Symbol> ObjSyms = Obj.symbols();
179   std::vector<lto::SymbolResolution> Resols(Syms.size());
180 
181   // Provide a resolution to the LTO API for each symbol.
182   for (size_t I = 0, E = Syms.size(); I != E; ++I) {
183     Symbol *Sym = Syms[I];
184     const lto::InputFile::Symbol &ObjSym = ObjSyms[I];
185     lto::SymbolResolution &R = Resols[I];
186 
187     // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
188     // reports two symbols for module ASM defined. Without this check, lld
189     // flags an undefined in IR with a definition in ASM as prevailing.
190     // Once IRObjectFile is fixed to report only one symbol this hack can
191     // be removed.
192     R.Prevailing = !ObjSym.isUndefined() && Sym->File == &F;
193 
194     // We ask LTO to preserve following global symbols:
195     // 1) All symbols when doing relocatable link, so that them can be used
196     //    for doing final link.
197     // 2) Symbols that are used in regular objects.
198     // 3) C named sections if we have corresponding __start_/__stop_ symbol.
199     // 4) Symbols that are defined in bitcode files and used for dynamic linking.
200     R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj ||
201                             (R.Prevailing && Sym->includeInDynsym()) ||
202                             UsedStartStop.count(ObjSym.getSectionName());
203     const auto *DR = dyn_cast<Defined>(Sym);
204     R.FinalDefinitionInLinkageUnit =
205         (IsExec || Sym->Visibility != STV_DEFAULT) && DR &&
206         // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
207         // will be generated by for them, triggering linker errors.
208         // Symbol section is always null for bitcode symbols, hence the check
209         // for isElf(). Skip linker script defined symbols as well: they have
210         // no File defined.
211         !(DR->Section == nullptr && (!Sym->File || Sym->File->isElf()));
212 
213     if (R.Prevailing)
214       undefine(Sym);
215 
216     // We tell LTO to not apply interprocedural optimization for wrapped
217     // (with --wrap) symbols because otherwise LTO would inline them while
218     // their values are still not final.
219     R.LinkerRedefined = !Sym->CanInline;
220   }
221   checkError(LTOObj->add(std::move(F.Obj), Resols));
222 }
223 
224 // Merge all the bitcode files we have seen, codegen the result
225 // and return the resulting ObjectFile(s).
226 std::vector<InputFile *> BitcodeCompiler::compile() {
227   unsigned MaxTasks = LTOObj->getMaxTasks();
228   Buff.resize(MaxTasks);
229   Files.resize(MaxTasks);
230 
231   // The --thinlto-cache-dir option specifies the path to a directory in which
232   // to cache native object files for ThinLTO incremental builds. If a path was
233   // specified, configure LTO to use it as the cache directory.
234   lto::NativeObjectCache Cache;
235   if (!Config->ThinLTOCacheDir.empty())
236     Cache = check(
237         lto::localCache(Config->ThinLTOCacheDir,
238                         [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
239                           Files[Task] = std::move(MB);
240                         }));
241 
242   checkError(LTOObj->run(
243       [&](size_t Task) {
244         return llvm::make_unique<lto::NativeObjectStream>(
245             llvm::make_unique<raw_svector_ostream>(Buff[Task]));
246       },
247       Cache));
248 
249   // Emit empty index files for non-indexed files
250   if (Config->ThinLTOIndexOnly) {
251     for (auto &Identifier : ObjectToIndexFileState)
252       if (!Identifier.getValue()) {
253         std::string Path = getThinLTOOutputFile(Identifier.getKey());
254         openFile(Path + ".thinlto.bc");
255 
256         if (Config->ThinLTOEmitImportsFiles)
257           openFile(Path + ".imports");
258       }
259   }
260 
261   // If LazyObjFile has not been added to link, emit empty index files.
262   // This is needed because this is what GNU gold plugin does and we have a
263   // distributed build system that depends on that behavior.
264   if (Config->ThinLTOIndexOnly) {
265     for (LazyObjFile *F : LazyObjFiles) {
266       if (F->AddedToLink || !isBitcode(F->MB))
267         continue;
268 
269       std::string Path = getThinLTOOutputFile(F->getName());
270       std::unique_ptr<raw_fd_ostream> OS = openFile(Path + ".thinlto.bc");
271       if (!OS)
272         continue;
273 
274       ModuleSummaryIndex M(false);
275       M.setSkipModuleByDistributedBackend();
276       WriteIndexToFile(M, *OS);
277 
278       if (Config->ThinLTOEmitImportsFiles)
279         openFile(Path + ".imports");
280     }
281 
282     if (!Config->LTOObjPath.empty())
283       saveBuffer(Buff[0], Config->LTOObjPath);
284 
285     // ThinLTO with index only option is required to generate only the index
286     // files. After that, we exit from linker and ThinLTO backend runs in a
287     // distributed environment.
288     if (IndexFile)
289       IndexFile->close();
290     return {};
291   }
292   if (!Config->ThinLTOCacheDir.empty())
293     pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy);
294 
295   std::vector<InputFile *> Ret;
296   for (unsigned I = 0; I != MaxTasks; ++I) {
297     if (Buff[I].empty())
298       continue;
299     if (Config->SaveTemps) {
300       if (I == 0)
301         saveBuffer(Buff[I], Config->OutputFile + ".lto.o");
302       else
303         saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o");
304     }
305     InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp"));
306     Ret.push_back(Obj);
307   }
308 
309   for (std::unique_ptr<MemoryBuffer> &File : Files)
310     if (File)
311       Ret.push_back(createObjectFile(*File));
312   return Ret;
313 }
314