xref: /llvm-project/llvm/tools/gold/gold-plugin.cpp (revision d047dbd95ed3ef4ace54eaa7c32fe3954317f926)
1 //===-- gold-plugin.cpp - Plugin to gold for Link Time Optimization  ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is a gold plugin for LLVM. It provides an LLVM implementation of the
10 // interface described in http://gcc.gnu.org/wiki/whopr/driver .
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/ScopeExit.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/Bitcode/BitcodeReader.h"
17 #include "llvm/Bitcode/BitcodeWriter.h"
18 #include "llvm/CodeGen/CommandFlags.h"
19 #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DiagnosticPrinter.h"
23 #include "llvm/LTO/LTO.h"
24 #include "llvm/Object/Error.h"
25 #include "llvm/Remarks/HotnessThresholdParser.h"
26 #include "llvm/Support/CachePruning.h"
27 #include "llvm/Support/Caching.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/ManagedStatic.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/Path.h"
33 #include "llvm/Support/TargetSelect.h"
34 #include "llvm/Support/Threading.h"
35 #include "llvm/Support/TimeProfiler.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/TargetParser/Host.h"
38 #include <list>
39 #include <map>
40 #include <plugin-api.h>
41 #include <string>
42 #include <system_error>
43 #include <utility>
44 #include <vector>
45 
46 // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
47 // Precise and Debian Wheezy (binutils 2.23 is required)
48 #define LDPO_PIE 3
49 
50 #define LDPT_GET_SYMBOLS_V3 28
51 
52 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
53 // required version.
54 #define LDPT_GET_WRAP_SYMBOLS 32
55 
56 using namespace llvm;
57 using namespace lto;
58 
59 static codegen::RegisterCodeGenFlags CodeGenFlags;
60 
61 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
62 // required version.
63 typedef enum ld_plugin_status (*ld_plugin_get_wrap_symbols)(
64     uint64_t *num_symbols, const char ***wrap_symbol_list);
65 
66 static ld_plugin_status discard_message(int level, const char *format, ...) {
67   // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
68   // callback in the transfer vector. This should never be called.
69   abort();
70 }
71 
72 static ld_plugin_release_input_file release_input_file = nullptr;
73 static ld_plugin_get_input_file get_input_file = nullptr;
74 static ld_plugin_message message = discard_message;
75 static ld_plugin_get_wrap_symbols get_wrap_symbols = nullptr;
76 
77 namespace {
78 struct claimed_file {
79   void *handle;
80   void *leader_handle;
81   std::vector<ld_plugin_symbol> syms;
82   off_t filesize;
83   std::string name;
84 };
85 
86 /// RAII wrapper to manage opening and releasing of a ld_plugin_input_file.
87 struct PluginInputFile {
88   void *Handle;
89   std::unique_ptr<ld_plugin_input_file> File;
90 
91   PluginInputFile(void *Handle) : Handle(Handle) {
92     File = std::make_unique<ld_plugin_input_file>();
93     if (get_input_file(Handle, File.get()) != LDPS_OK)
94       message(LDPL_FATAL, "Failed to get file information");
95   }
96   ~PluginInputFile() {
97     // File would have been reset to nullptr if we moved this object
98     // to a new owner.
99     if (File)
100       if (release_input_file(Handle) != LDPS_OK)
101         message(LDPL_FATAL, "Failed to release file information");
102   }
103 
104   ld_plugin_input_file &file() { return *File; }
105 
106   PluginInputFile(PluginInputFile &&RHS) = default;
107   PluginInputFile &operator=(PluginInputFile &&RHS) = default;
108 };
109 
110 struct ResolutionInfo {
111   bool CanOmitFromDynSym = true;
112   bool DefaultVisibility = true;
113   bool CanInline = true;
114   bool IsUsedInRegularObj = false;
115 };
116 
117 }
118 
119 static ld_plugin_add_symbols add_symbols = nullptr;
120 static ld_plugin_get_symbols get_symbols = nullptr;
121 static ld_plugin_add_input_file add_input_file = nullptr;
122 static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
123 static ld_plugin_get_view get_view = nullptr;
124 static bool IsExecutable = false;
125 static bool SplitSections = true;
126 static std::optional<Reloc::Model> RelocationModel;
127 static std::string output_name = "";
128 static std::list<claimed_file> Modules;
129 static DenseMap<int, void *> FDToLeaderHandle;
130 static StringMap<ResolutionInfo> ResInfo;
131 static std::vector<std::string> Cleanup;
132 
133 namespace options {
134   enum OutputType {
135     OT_NORMAL,
136     OT_DISABLE,
137     OT_BC_ONLY,
138     OT_ASM_ONLY,
139     OT_SAVE_TEMPS
140   };
141   static OutputType TheOutputType = OT_NORMAL;
142   static unsigned OptLevel = 2;
143   // Currently only affects ThinLTO, where the default is the max cores in the
144   // system. See llvm::get_threadpool_strategy() for acceptable values.
145   static std::string Parallelism;
146   // Default regular LTO codegen parallelism (number of partitions).
147   static unsigned ParallelCodeGenParallelismLevel = 1;
148 #ifdef NDEBUG
149   static bool DisableVerify = true;
150 #else
151   static bool DisableVerify = false;
152 #endif
153   static std::string obj_path;
154   static std::string extra_library_path;
155   static std::string triple;
156   static std::string mcpu;
157   // Tells plugin to use unified lto
158   static bool unifiedlto = false;
159   // When the thinlto plugin option is specified, only read the function
160   // the information from intermediate files and write a combined
161   // global index for the ThinLTO backends.
162   static bool thinlto = false;
163   // If false, all ThinLTO backend compilations through code gen are performed
164   // using multiple threads in the gold-plugin, before handing control back to
165   // gold. If true, write individual backend index files which reflect
166   // the import decisions, and exit afterwards. The assumption is
167   // that the build system will launch the backend processes.
168   static bool thinlto_index_only = false;
169   // If non-empty, holds the name of a file in which to write the list of
170   // oject files gold selected for inclusion in the link after symbol
171   // resolution (i.e. they had selected symbols). This will only be non-empty
172   // in the thinlto_index_only case. It is used to identify files, which may
173   // have originally been within archive libraries specified via
174   // --start-lib/--end-lib pairs, that should be included in the final
175   // native link process (since intervening function importing and inlining
176   // may change the symbol resolution detected in the final link and which
177   // files to include out of --start-lib/--end-lib libraries as a result).
178   static std::string thinlto_linked_objects_file;
179   // If true, when generating individual index files for distributed backends,
180   // also generate a "${bitcodefile}.imports" file at the same location for each
181   // bitcode file, listing the files it imports from in plain text. This is to
182   // support distributed build file staging.
183   static bool thinlto_emit_imports_files = false;
184   // Option to control where files for a distributed backend (the individual
185   // index files and optional imports files) are created.
186   // If specified, expects a string of the form "oldprefix:newprefix", and
187   // instead of generating these files in the same directory path as the
188   // corresponding bitcode file, will use a path formed by replacing the
189   // bitcode file's path prefix matching oldprefix with newprefix.
190   static std::string thinlto_prefix_replace;
191   // Option to control the name of modules encoded in the individual index
192   // files for a distributed backend. This enables the use of minimized
193   // bitcode files for the thin link, assuming the name of the full bitcode
194   // file used in the backend differs just in some part of the file suffix.
195   // If specified, expects a string of the form "oldsuffix:newsuffix".
196   static std::string thinlto_object_suffix_replace;
197   // Optional path to a directory for caching ThinLTO objects.
198   static std::string cache_dir;
199   // Optional pruning policy for ThinLTO caches.
200   static std::string cache_policy;
201   // Additional options to pass into the code generator.
202   // Note: This array will contain all plugin options which are not claimed
203   // as plugin exclusive to pass to the code generator.
204   static std::vector<const char *> extra;
205   // Sample profile file path
206   static std::string sample_profile;
207   // Debug new pass manager
208   static bool debug_pass_manager = false;
209   // Directory to store the .dwo files.
210   static std::string dwo_dir;
211   /// Statistics output filename.
212   static std::string stats_file;
213   // Asserts that LTO link has whole program visibility
214   static bool whole_program_visibility = false;
215 
216   // Optimization remarks filename, accepted passes and hotness options
217   static std::string RemarksFilename;
218   static std::string RemarksPasses;
219   static bool RemarksWithHotness = false;
220   static std::optional<uint64_t> RemarksHotnessThreshold = 0;
221   static std::string RemarksFormat;
222 
223   // Context sensitive PGO options.
224   static std::string cs_profile_path;
225   static bool cs_pgo_gen = false;
226 
227   // When true, MergeFunctions pass is used in LTO link pipeline.
228   static bool merge_functions = false;
229 
230   // Time trace options.
231   static std::string time_trace_file;
232   static unsigned time_trace_granularity = 500;
233 
234   static void process_plugin_option(const char *opt_)
235   {
236     if (opt_ == nullptr)
237       return;
238     llvm::StringRef opt = opt_;
239 
240     if (opt.consume_front("mcpu=")) {
241       mcpu = std::string(opt);
242     } else if (opt.consume_front("extra-library-path=")) {
243       extra_library_path = std::string(opt);
244     } else if (opt.consume_front("mtriple=")) {
245       triple = std::string(opt);
246     } else if (opt.consume_front("obj-path=")) {
247       obj_path = std::string(opt);
248     } else if (opt == "emit-llvm") {
249       TheOutputType = OT_BC_ONLY;
250     } else if (opt == "save-temps") {
251       TheOutputType = OT_SAVE_TEMPS;
252     } else if (opt == "disable-output") {
253       TheOutputType = OT_DISABLE;
254     } else if (opt == "emit-asm") {
255       TheOutputType = OT_ASM_ONLY;
256     } else if (opt == "unifiedlto") {
257       unifiedlto = true;
258     } else if (opt == "thinlto") {
259       thinlto = true;
260     } else if (opt == "thinlto-index-only") {
261       thinlto_index_only = true;
262     } else if (opt.consume_front("thinlto-index-only=")) {
263       thinlto_index_only = true;
264       thinlto_linked_objects_file = std::string(opt);
265     } else if (opt == "thinlto-emit-imports-files") {
266       thinlto_emit_imports_files = true;
267     } else if (opt.consume_front("thinlto-prefix-replace=")) {
268       thinlto_prefix_replace = std::string(opt);
269       if (thinlto_prefix_replace.find(';') == std::string::npos)
270         message(LDPL_FATAL, "thinlto-prefix-replace expects 'old;new' format");
271     } else if (opt.consume_front("thinlto-object-suffix-replace=")) {
272       thinlto_object_suffix_replace = std::string(opt);
273       if (thinlto_object_suffix_replace.find(';') == std::string::npos)
274         message(LDPL_FATAL,
275                 "thinlto-object-suffix-replace expects 'old;new' format");
276     } else if (opt.consume_front("cache-dir=")) {
277       cache_dir = std::string(opt);
278     } else if (opt.consume_front("cache-policy=")) {
279       cache_policy = std::string(opt);
280     } else if (opt.size() == 2 && opt[0] == 'O') {
281       if (opt[1] < '0' || opt[1] > '3')
282         message(LDPL_FATAL, "Optimization level must be between 0 and 3");
283       OptLevel = opt[1] - '0';
284     } else if (opt.consume_front("jobs=")) {
285       Parallelism = std::string(opt);
286       if (!get_threadpool_strategy(opt))
287         message(LDPL_FATAL, "Invalid parallelism level: %s",
288                 Parallelism.c_str());
289     } else if (opt.consume_front("lto-partitions=")) {
290       if (opt.getAsInteger(10, ParallelCodeGenParallelismLevel))
291         message(LDPL_FATAL, "Invalid codegen partition level: %s", opt_ + 5);
292     } else if (opt == "disable-verify") {
293       DisableVerify = true;
294     } else if (opt.consume_front("sample-profile=")) {
295       sample_profile = std::string(opt);
296     } else if (opt == "cs-profile-generate") {
297       cs_pgo_gen = true;
298     } else if (opt == "merge-functions") {
299       merge_functions = true;
300     } else if (opt.consume_front("cs-profile-path=")) {
301       cs_profile_path = std::string(opt);
302     } else if (opt == "new-pass-manager") {
303       // We always use the new pass manager.
304     } else if (opt == "debug-pass-manager") {
305       debug_pass_manager = true;
306     } else if (opt == "whole-program-visibility") {
307       whole_program_visibility = true;
308     } else if (opt.consume_front("dwo_dir=")) {
309       dwo_dir = std::string(opt);
310     } else if (opt.consume_front("opt-remarks-filename=")) {
311       RemarksFilename = std::string(opt);
312     } else if (opt.consume_front("opt-remarks-passes=")) {
313       RemarksPasses = std::string(opt);
314     } else if (opt == "opt-remarks-with-hotness") {
315       RemarksWithHotness = true;
316     } else if (opt.consume_front("opt-remarks-hotness-threshold=")) {
317       auto ResultOrErr = remarks::parseHotnessThresholdOption(opt);
318       if (!ResultOrErr)
319         message(LDPL_FATAL, "Invalid remarks hotness threshold: %s",
320                 opt.data());
321       else
322         RemarksHotnessThreshold = *ResultOrErr;
323     } else if (opt.consume_front("opt-remarks-format=")) {
324       RemarksFormat = std::string(opt);
325     } else if (opt.consume_front("stats-file=")) {
326       stats_file = std::string(opt);
327     } else if (opt.consume_front("time-trace=")) {
328       time_trace_file = std::string(opt);
329     } else if (opt.consume_front("time-trace-granularity=")) {
330       unsigned Granularity;
331       if (opt.getAsInteger(10, Granularity))
332         message(LDPL_FATAL, "Invalid time trace granularity: %s", opt.data());
333       else
334         time_trace_granularity = Granularity;
335     } else {
336       // Save this option to pass to the code generator.
337       // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
338       // add that.
339       if (extra.empty())
340         extra.push_back("LLVMgold");
341 
342       extra.push_back(opt_);
343     }
344   }
345 }
346 
347 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
348                                         int *claimed);
349 static ld_plugin_status all_symbols_read_hook(void);
350 static ld_plugin_status cleanup_hook(void);
351 
352 extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
353 ld_plugin_status onload(ld_plugin_tv *tv) {
354   InitializeAllTargetInfos();
355   InitializeAllTargets();
356   InitializeAllTargetMCs();
357   InitializeAllAsmParsers();
358   InitializeAllAsmPrinters();
359 
360   // We're given a pointer to the first transfer vector. We read through them
361   // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
362   // contain pointers to functions that we need to call to register our own
363   // hooks. The others are addresses of functions we can use to call into gold
364   // for services.
365 
366   bool registeredClaimFile = false;
367   bool RegisteredAllSymbolsRead = false;
368 
369   for (; tv->tv_tag != LDPT_NULL; ++tv) {
370     // Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for
371     // example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h
372     // header.
373     switch (static_cast<int>(tv->tv_tag)) {
374     case LDPT_OUTPUT_NAME:
375       output_name = tv->tv_u.tv_string;
376       break;
377     case LDPT_LINKER_OUTPUT:
378       switch (tv->tv_u.tv_val) {
379       case LDPO_REL: // .o
380         IsExecutable = false;
381         SplitSections = false;
382         break;
383       case LDPO_DYN: // .so
384         IsExecutable = false;
385         RelocationModel = Reloc::PIC_;
386         break;
387       case LDPO_PIE: // position independent executable
388         IsExecutable = true;
389         RelocationModel = Reloc::PIC_;
390         break;
391       case LDPO_EXEC: // .exe
392         IsExecutable = true;
393         RelocationModel = Reloc::Static;
394         break;
395       default:
396         message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
397         return LDPS_ERR;
398       }
399       break;
400     case LDPT_OPTION:
401       options::process_plugin_option(tv->tv_u.tv_string);
402       break;
403     case LDPT_REGISTER_CLAIM_FILE_HOOK: {
404       ld_plugin_register_claim_file callback;
405       callback = tv->tv_u.tv_register_claim_file;
406 
407       if (callback(claim_file_hook) != LDPS_OK)
408         return LDPS_ERR;
409 
410       registeredClaimFile = true;
411     } break;
412     case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
413       ld_plugin_register_all_symbols_read callback;
414       callback = tv->tv_u.tv_register_all_symbols_read;
415 
416       if (callback(all_symbols_read_hook) != LDPS_OK)
417         return LDPS_ERR;
418 
419       RegisteredAllSymbolsRead = true;
420     } break;
421     case LDPT_REGISTER_CLEANUP_HOOK: {
422       ld_plugin_register_cleanup callback;
423       callback = tv->tv_u.tv_register_cleanup;
424 
425       if (callback(cleanup_hook) != LDPS_OK)
426         return LDPS_ERR;
427     } break;
428     case LDPT_GET_INPUT_FILE:
429       get_input_file = tv->tv_u.tv_get_input_file;
430       break;
431     case LDPT_RELEASE_INPUT_FILE:
432       release_input_file = tv->tv_u.tv_release_input_file;
433       break;
434     case LDPT_ADD_SYMBOLS:
435       add_symbols = tv->tv_u.tv_add_symbols;
436       break;
437     case LDPT_GET_SYMBOLS_V2:
438       // Do not override get_symbols_v3 with get_symbols_v2.
439       if (!get_symbols)
440         get_symbols = tv->tv_u.tv_get_symbols;
441       break;
442     case LDPT_GET_SYMBOLS_V3:
443       get_symbols = tv->tv_u.tv_get_symbols;
444       break;
445     case LDPT_ADD_INPUT_FILE:
446       add_input_file = tv->tv_u.tv_add_input_file;
447       break;
448     case LDPT_SET_EXTRA_LIBRARY_PATH:
449       set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
450       break;
451     case LDPT_GET_VIEW:
452       get_view = tv->tv_u.tv_get_view;
453       break;
454     case LDPT_MESSAGE:
455       message = tv->tv_u.tv_message;
456       break;
457     case LDPT_GET_WRAP_SYMBOLS:
458       // FIXME: When binutils 2.31 (containing gold 1.16) is the minimum
459       // required version, this should be changed to:
460       // get_wrap_symbols = tv->tv_u.tv_get_wrap_symbols;
461 #pragma GCC diagnostic push
462 #pragma GCC diagnostic ignored "-Wcast-function-type"
463       get_wrap_symbols = (ld_plugin_get_wrap_symbols)tv->tv_u.tv_message;
464 #pragma GCC diagnostic pop
465       break;
466     default:
467       break;
468     }
469   }
470 
471   if (!registeredClaimFile) {
472     message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
473     return LDPS_ERR;
474   }
475   if (!add_symbols) {
476     message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
477     return LDPS_ERR;
478   }
479 
480   if (!RegisteredAllSymbolsRead)
481     return LDPS_OK;
482 
483   if (!get_input_file) {
484     message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
485     return LDPS_ERR;
486   }
487   if (!release_input_file) {
488     message(LDPL_ERROR, "release_input_file not passed to LLVMgold.");
489     return LDPS_ERR;
490   }
491 
492   return LDPS_OK;
493 }
494 
495 static void diagnosticHandler(const DiagnosticInfo &DI) {
496   std::string ErrStorage;
497   {
498     raw_string_ostream OS(ErrStorage);
499     DiagnosticPrinterRawOStream DP(OS);
500     DI.print(DP);
501   }
502   ld_plugin_level Level;
503   switch (DI.getSeverity()) {
504   case DS_Error:
505     Level = LDPL_FATAL;
506     break;
507   case DS_Warning:
508     Level = LDPL_WARNING;
509     break;
510   case DS_Note:
511   case DS_Remark:
512     Level = LDPL_INFO;
513     break;
514   }
515   message(Level, "LLVM gold plugin: %s",  ErrStorage.c_str());
516 }
517 
518 static void check(Error E, std::string Msg = "LLVM gold plugin") {
519   handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error {
520     message(LDPL_FATAL, "%s: %s", Msg.c_str(), EIB.message().c_str());
521     return Error::success();
522   });
523 }
524 
525 template <typename T> static T check(Expected<T> E) {
526   if (E)
527     return std::move(*E);
528   check(E.takeError());
529   return T();
530 }
531 
532 /// Called by gold to see whether this file is one that our plugin can handle.
533 /// We'll try to open it and register all the symbols with add_symbol if
534 /// possible.
535 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
536                                         int *claimed) {
537   MemoryBufferRef BufferRef;
538   std::unique_ptr<MemoryBuffer> Buffer;
539   if (get_view) {
540     const void *view;
541     if (get_view(file->handle, &view) != LDPS_OK) {
542       message(LDPL_ERROR, "Failed to get a view of %s", file->name);
543       return LDPS_ERR;
544     }
545     BufferRef =
546         MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
547   } else {
548     int64_t offset = 0;
549     // Gold has found what might be IR part-way inside of a file, such as
550     // an .a archive.
551     if (file->offset) {
552       offset = file->offset;
553     }
554     ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
555         MemoryBuffer::getOpenFileSlice(sys::fs::convertFDToNativeFile(file->fd),
556                                        file->name, file->filesize, offset);
557     if (std::error_code EC = BufferOrErr.getError()) {
558       message(LDPL_ERROR, EC.message().c_str());
559       return LDPS_ERR;
560     }
561     Buffer = std::move(BufferOrErr.get());
562     BufferRef = Buffer->getMemBufferRef();
563   }
564 
565   *claimed = 1;
566 
567   Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef);
568   if (!ObjOrErr) {
569     handleAllErrors(ObjOrErr.takeError(), [&](const ErrorInfoBase &EI) {
570       std::error_code EC = EI.convertToErrorCode();
571       if (EC == object::object_error::invalid_file_type ||
572           EC == object::object_error::bitcode_section_not_found)
573         *claimed = 0;
574       else
575         message(LDPL_FATAL,
576                 "LLVM gold plugin has failed to create LTO module: %s",
577                 EI.message().c_str());
578     });
579 
580     return *claimed ? LDPS_ERR : LDPS_OK;
581   }
582 
583   std::unique_ptr<InputFile> Obj = std::move(*ObjOrErr);
584 
585   Modules.emplace_back();
586   claimed_file &cf = Modules.back();
587 
588   cf.handle = file->handle;
589   // Keep track of the first handle for each file descriptor, since there are
590   // multiple in the case of an archive. This is used later in the case of
591   // ThinLTO parallel backends to ensure that each file is only opened and
592   // released once.
593   auto LeaderHandle =
594       FDToLeaderHandle.insert(std::make_pair(file->fd, file->handle)).first;
595   cf.leader_handle = LeaderHandle->second;
596   // Save the filesize since for parallel ThinLTO backends we can only
597   // invoke get_input_file once per archive (only for the leader handle).
598   cf.filesize = file->filesize;
599   // In the case of an archive library, all but the first member must have a
600   // non-zero offset, which we can append to the file name to obtain a
601   // unique name.
602   cf.name = file->name;
603   if (file->offset)
604     cf.name += ".llvm." + std::to_string(file->offset) + "." +
605                sys::path::filename(Obj->getSourceFileName()).str();
606 
607   for (auto &Sym : Obj->symbols()) {
608     cf.syms.push_back(ld_plugin_symbol());
609     ld_plugin_symbol &sym = cf.syms.back();
610     sym.version = nullptr;
611     StringRef Name = Sym.getName();
612     sym.name = strdup(Name.str().c_str());
613 
614     ResolutionInfo &Res = ResInfo[Name];
615 
616     Res.CanOmitFromDynSym &= Sym.canBeOmittedFromSymbolTable();
617 
618     sym.visibility = LDPV_DEFAULT;
619     GlobalValue::VisibilityTypes Vis = Sym.getVisibility();
620     if (Vis != GlobalValue::DefaultVisibility)
621       Res.DefaultVisibility = false;
622     switch (Vis) {
623     case GlobalValue::DefaultVisibility:
624       break;
625     case GlobalValue::HiddenVisibility:
626       sym.visibility = LDPV_HIDDEN;
627       break;
628     case GlobalValue::ProtectedVisibility:
629       sym.visibility = LDPV_PROTECTED;
630       break;
631     }
632 
633     if (Sym.isUndefined()) {
634       sym.def = LDPK_UNDEF;
635       if (Sym.isWeak())
636         sym.def = LDPK_WEAKUNDEF;
637     } else if (Sym.isCommon())
638       sym.def = LDPK_COMMON;
639     else if (Sym.isWeak())
640       sym.def = LDPK_WEAKDEF;
641     else
642       sym.def = LDPK_DEF;
643 
644     sym.size = 0;
645     sym.comdat_key = nullptr;
646     int CI = Sym.getComdatIndex();
647     if (CI != -1) {
648       // Not setting comdat_key for nodeduplicate ensuress we don't deduplicate.
649       std::pair<StringRef, Comdat::SelectionKind> C = Obj->getComdatTable()[CI];
650       if (C.second != Comdat::NoDeduplicate)
651         sym.comdat_key = strdup(C.first.str().c_str());
652     }
653 
654     sym.resolution = LDPR_UNKNOWN;
655   }
656 
657   if (!cf.syms.empty()) {
658     if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) {
659       message(LDPL_ERROR, "Unable to add symbols!");
660       return LDPS_ERR;
661     }
662   }
663 
664   // Handle any --wrap options passed to gold, which are than passed
665   // along to the plugin.
666   if (get_wrap_symbols) {
667     const char **wrap_symbols;
668     uint64_t count = 0;
669     if (get_wrap_symbols(&count, &wrap_symbols) != LDPS_OK) {
670       message(LDPL_ERROR, "Unable to get wrap symbols!");
671       return LDPS_ERR;
672     }
673     for (uint64_t i = 0; i < count; i++) {
674       StringRef Name = wrap_symbols[i];
675       ResolutionInfo &Res = ResInfo[Name];
676       ResolutionInfo &WrapRes = ResInfo["__wrap_" + Name.str()];
677       ResolutionInfo &RealRes = ResInfo["__real_" + Name.str()];
678       // Tell LTO not to inline symbols that will be overwritten.
679       Res.CanInline = false;
680       RealRes.CanInline = false;
681       // Tell LTO not to eliminate symbols that will be used after renaming.
682       Res.IsUsedInRegularObj = true;
683       WrapRes.IsUsedInRegularObj = true;
684     }
685   }
686 
687   return LDPS_OK;
688 }
689 
690 static void freeSymName(ld_plugin_symbol &Sym) {
691   free(Sym.name);
692   free(Sym.comdat_key);
693   Sym.name = nullptr;
694   Sym.comdat_key = nullptr;
695 }
696 
697 /// Helper to get a file's symbols and a view into it via gold callbacks.
698 static const void *getSymbolsAndView(claimed_file &F) {
699   ld_plugin_status status = get_symbols(F.handle, F.syms.size(), F.syms.data());
700   if (status == LDPS_NO_SYMS)
701     return nullptr;
702 
703   if (status != LDPS_OK)
704     message(LDPL_FATAL, "Failed to get symbol information");
705 
706   const void *View;
707   if (get_view(F.handle, &View) != LDPS_OK)
708     message(LDPL_FATAL, "Failed to get a view of file");
709 
710   return View;
711 }
712 
713 /// Parse the thinlto-object-suffix-replace option into the \p OldSuffix and
714 /// \p NewSuffix strings, if it was specified.
715 static void getThinLTOOldAndNewSuffix(std::string &OldSuffix,
716                                       std::string &NewSuffix) {
717   assert(options::thinlto_object_suffix_replace.empty() ||
718          options::thinlto_object_suffix_replace.find(';') != StringRef::npos);
719   StringRef SuffixReplace = options::thinlto_object_suffix_replace;
720   auto Split = SuffixReplace.split(';');
721   OldSuffix = std::string(Split.first);
722   NewSuffix = std::string(Split.second);
723 }
724 
725 /// Given the original \p Path to an output file, replace any filename
726 /// suffix matching \p OldSuffix with \p NewSuffix.
727 static std::string getThinLTOObjectFileName(StringRef Path, StringRef OldSuffix,
728                                             StringRef NewSuffix) {
729   if (Path.consume_back(OldSuffix))
730     return (Path + NewSuffix).str();
731   return std::string(Path);
732 }
733 
734 // Returns true if S is valid as a C language identifier.
735 static bool isValidCIdentifier(StringRef S) {
736   return !S.empty() && (isAlpha(S[0]) || S[0] == '_') &&
737          llvm::all_of(llvm::drop_begin(S),
738                       [](char C) { return C == '_' || isAlnum(C); });
739 }
740 
741 static bool isUndefined(ld_plugin_symbol &Sym) {
742   return Sym.def == LDPK_UNDEF || Sym.def == LDPK_WEAKUNDEF;
743 }
744 
745 static void addModule(LTO &Lto, claimed_file &F, const void *View,
746                       StringRef Filename) {
747   MemoryBufferRef BufferRef(StringRef((const char *)View, F.filesize),
748                             Filename);
749   Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef);
750 
751   if (!ObjOrErr)
752     message(LDPL_FATAL, "Could not read bitcode from file : %s",
753             toString(ObjOrErr.takeError()).c_str());
754 
755   unsigned SymNum = 0;
756   std::unique_ptr<InputFile> Input = std::move(ObjOrErr.get());
757   auto InputFileSyms = Input->symbols();
758   assert(InputFileSyms.size() == F.syms.size());
759   std::vector<SymbolResolution> Resols(F.syms.size());
760   for (ld_plugin_symbol &Sym : F.syms) {
761     const InputFile::Symbol &InpSym = InputFileSyms[SymNum];
762     SymbolResolution &R = Resols[SymNum++];
763 
764     ld_plugin_symbol_resolution Resolution =
765         (ld_plugin_symbol_resolution)Sym.resolution;
766 
767     ResolutionInfo &Res = ResInfo[Sym.name];
768 
769     switch (Resolution) {
770     case LDPR_UNKNOWN:
771       llvm_unreachable("Unexpected resolution");
772 
773     case LDPR_RESOLVED_IR:
774     case LDPR_RESOLVED_EXEC:
775     case LDPR_PREEMPTED_IR:
776     case LDPR_PREEMPTED_REG:
777     case LDPR_UNDEF:
778       break;
779 
780     case LDPR_RESOLVED_DYN:
781       R.ExportDynamic = true;
782       break;
783 
784     case LDPR_PREVAILING_DEF_IRONLY:
785       R.Prevailing = !isUndefined(Sym);
786       break;
787 
788     case LDPR_PREVAILING_DEF:
789       R.Prevailing = !isUndefined(Sym);
790       R.VisibleToRegularObj = true;
791       break;
792 
793     case LDPR_PREVAILING_DEF_IRONLY_EXP:
794       R.Prevailing = !isUndefined(Sym);
795       // Identify symbols exported dynamically, and that therefore could be
796       // referenced by a shared library not visible to the linker.
797       R.ExportDynamic = true;
798       if (!Res.CanOmitFromDynSym)
799         R.VisibleToRegularObj = true;
800       break;
801     }
802 
803     // If the symbol has a C identifier section name, we need to mark
804     // it as visible to a regular object so that LTO will keep it around
805     // to ensure the linker generates special __start_<secname> and
806     // __stop_<secname> symbols which may be used elsewhere.
807     if (isValidCIdentifier(InpSym.getSectionName()))
808       R.VisibleToRegularObj = true;
809 
810     if (Resolution != LDPR_RESOLVED_DYN && Resolution != LDPR_UNDEF &&
811         (IsExecutable || !Res.DefaultVisibility))
812       R.FinalDefinitionInLinkageUnit = true;
813 
814     if (!Res.CanInline)
815       R.LinkerRedefined = true;
816 
817     if (Res.IsUsedInRegularObj)
818       R.VisibleToRegularObj = true;
819 
820     freeSymName(Sym);
821   }
822 
823   check(Lto.add(std::move(Input), Resols),
824         std::string("Failed to link module ") + F.name);
825 }
826 
827 static void recordFile(const std::string &Filename, bool TempOutFile) {
828   if (add_input_file(Filename.c_str()) != LDPS_OK)
829     message(LDPL_FATAL,
830             "Unable to add .o file to the link. File left behind in: %s",
831             Filename.c_str());
832   if (TempOutFile)
833     Cleanup.push_back(Filename);
834 }
835 
836 /// Return the desired output filename given a base input name, a flag
837 /// indicating whether a temp file should be generated, and an optional task id.
838 /// The new filename generated is returned in \p NewFilename.
839 static int getOutputFileName(StringRef InFilename, bool TempOutFile,
840                              SmallString<128> &NewFilename, int TaskID) {
841   int FD = -1;
842   if (TempOutFile) {
843     std::error_code EC =
844         sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename);
845     if (EC)
846       message(LDPL_FATAL, "Could not create temporary file: %s",
847               EC.message().c_str());
848   } else {
849     NewFilename = InFilename;
850     if (TaskID > 0)
851       NewFilename += utostr(TaskID);
852     std::error_code EC =
853         sys::fs::openFileForWrite(NewFilename, FD, sys::fs::CD_CreateAlways);
854     if (EC)
855       message(LDPL_FATAL, "Could not open file %s: %s", NewFilename.c_str(),
856               EC.message().c_str());
857   }
858   return FD;
859 }
860 
861 /// Parse the thinlto_prefix_replace option into the \p OldPrefix and
862 /// \p NewPrefix strings, if it was specified.
863 static void getThinLTOOldAndNewPrefix(std::string &OldPrefix,
864                                       std::string &NewPrefix) {
865   StringRef PrefixReplace = options::thinlto_prefix_replace;
866   assert(PrefixReplace.empty() || PrefixReplace.find(';') != StringRef::npos);
867   auto Split = PrefixReplace.split(';');
868   OldPrefix = std::string(Split.first);
869   NewPrefix = std::string(Split.second);
870 }
871 
872 /// Creates instance of LTO.
873 /// OnIndexWrite is callback to let caller know when LTO writes index files.
874 /// LinkedObjectsFile is an output stream to write the list of object files for
875 /// the final ThinLTO linking. Can be nullptr.
876 static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
877                                       raw_fd_ostream *LinkedObjectsFile) {
878   Config Conf;
879   ThinBackend Backend;
880 
881   Conf.CPU = options::mcpu;
882   Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(Triple());
883 
884   // Disable the new X86 relax relocations since gold might not support them.
885   // FIXME: Check the gold version or add a new option to enable them.
886   Conf.Options.MCOptions.X86RelaxRelocations = false;
887 
888   // Toggle function/data sections.
889   if (!codegen::getExplicitFunctionSections())
890     Conf.Options.FunctionSections = SplitSections;
891   if (!codegen::getExplicitDataSections())
892     Conf.Options.DataSections = SplitSections;
893 
894   Conf.MAttrs = codegen::getMAttrs();
895   Conf.RelocModel = RelocationModel;
896   Conf.CodeModel = codegen::getExplicitCodeModel();
897   std::optional<CodeGenOptLevel> CGOptLevelOrNone =
898       CodeGenOpt::getLevel(options::OptLevel);
899   assert(CGOptLevelOrNone && "Invalid optimization level");
900   Conf.CGOptLevel = *CGOptLevelOrNone;
901   Conf.DisableVerify = options::DisableVerify;
902   Conf.OptLevel = options::OptLevel;
903   Conf.PTO.LoopVectorization = options::OptLevel > 1;
904   Conf.PTO.SLPVectorization = options::OptLevel > 1;
905   Conf.PTO.MergeFunctions = options::merge_functions;
906   Conf.PTO.UnifiedLTO = options::unifiedlto;
907   Conf.AlwaysEmitRegularLTOObj = !options::obj_path.empty();
908 
909   if (options::thinlto_index_only) {
910     std::string OldPrefix, NewPrefix;
911     getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
912     Backend = createWriteIndexesThinBackend(
913         llvm::hardware_concurrency(options::Parallelism), OldPrefix, NewPrefix,
914         // TODO: Add support for optional native object path in
915         // thinlto_prefix_replace option to match lld.
916         /*NativeObjectPrefix=*/"", options::thinlto_emit_imports_files,
917         LinkedObjectsFile, OnIndexWrite);
918   } else {
919     Backend = createInProcessThinBackend(
920         llvm::heavyweight_hardware_concurrency(options::Parallelism));
921   }
922 
923   Conf.OverrideTriple = options::triple;
924   Conf.DefaultTriple = sys::getDefaultTargetTriple();
925 
926   Conf.DiagHandler = diagnosticHandler;
927 
928   switch (options::TheOutputType) {
929   case options::OT_NORMAL:
930     break;
931 
932   case options::OT_DISABLE:
933     Conf.PreOptModuleHook = [](size_t Task, const Module &M) { return false; };
934     break;
935 
936   case options::OT_BC_ONLY:
937     Conf.PostInternalizeModuleHook = [](size_t Task, const Module &M) {
938       std::error_code EC;
939       SmallString<128> TaskFilename;
940       getOutputFileName(output_name, /* TempOutFile */ false, TaskFilename,
941                         Task);
942       raw_fd_ostream OS(TaskFilename, EC, sys::fs::OpenFlags::OF_None);
943       if (EC)
944         message(LDPL_FATAL, "Failed to write the output file.");
945       WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ false);
946       return false;
947     };
948     break;
949 
950   case options::OT_SAVE_TEMPS:
951     check(Conf.addSaveTemps(output_name + ".",
952                             /* UseInputModulePath */ true));
953     break;
954   case options::OT_ASM_ONLY:
955     Conf.CGFileType = CodeGenFileType::AssemblyFile;
956     Conf.Options.MCOptions.AsmVerbose = true;
957     break;
958   }
959 
960   if (!options::sample_profile.empty())
961     Conf.SampleProfile = options::sample_profile;
962 
963   if (!options::cs_profile_path.empty())
964     Conf.CSIRProfile = options::cs_profile_path;
965   Conf.RunCSIRInstr = options::cs_pgo_gen;
966 
967   Conf.DwoDir = options::dwo_dir;
968 
969   // Set up optimization remarks handling.
970   Conf.RemarksFilename = options::RemarksFilename;
971   Conf.RemarksPasses = options::RemarksPasses;
972   Conf.RemarksWithHotness = options::RemarksWithHotness;
973   Conf.RemarksHotnessThreshold = options::RemarksHotnessThreshold;
974   Conf.RemarksFormat = options::RemarksFormat;
975 
976   // Debug new pass manager if requested
977   Conf.DebugPassManager = options::debug_pass_manager;
978 
979   Conf.HasWholeProgramVisibility = options::whole_program_visibility;
980 
981   Conf.StatsFile = options::stats_file;
982 
983   Conf.TimeTraceEnabled = !options::time_trace_file.empty();
984   Conf.TimeTraceGranularity = options::time_trace_granularity;
985 
986   LTO::LTOKind ltoKind = LTO::LTOK_Default;
987   if (options::unifiedlto)
988     ltoKind =
989         options::thinlto ? LTO::LTOK_UnifiedThin : LTO::LTOK_UnifiedRegular;
990   return std::make_unique<LTO>(std::move(Conf), Backend,
991                                options::ParallelCodeGenParallelismLevel,
992                                ltoKind);
993 }
994 
995 // Write empty files that may be expected by a distributed build
996 // system when invoked with thinlto_index_only. This is invoked when
997 // the linker has decided not to include the given module in the
998 // final link. Frequently the distributed build system will want to
999 // confirm that all expected outputs are created based on all of the
1000 // modules provided to the linker.
1001 // If SkipModule is true then .thinlto.bc should contain just
1002 // SkipModuleByDistributedBackend flag which requests distributed backend
1003 // to skip the compilation of the corresponding module and produce an empty
1004 // object file.
1005 static void writeEmptyDistributedBuildOutputs(const std::string &ModulePath,
1006                                               const std::string &OldPrefix,
1007                                               const std::string &NewPrefix,
1008                                               bool SkipModule) {
1009   std::string NewModulePath =
1010       getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
1011   std::error_code EC;
1012   {
1013     raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
1014                       sys::fs::OpenFlags::OF_None);
1015     if (EC)
1016       message(LDPL_FATAL, "Failed to write '%s': %s",
1017               (NewModulePath + ".thinlto.bc").c_str(), EC.message().c_str());
1018 
1019     if (SkipModule) {
1020       ModuleSummaryIndex Index(/*HaveGVs*/ false);
1021       Index.setSkipModuleByDistributedBackend();
1022       writeIndexToFile(Index, OS, nullptr);
1023     }
1024   }
1025   if (options::thinlto_emit_imports_files) {
1026     raw_fd_ostream OS(NewModulePath + ".imports", EC,
1027                       sys::fs::OpenFlags::OF_None);
1028     if (EC)
1029       message(LDPL_FATAL, "Failed to write '%s': %s",
1030               (NewModulePath + ".imports").c_str(), EC.message().c_str());
1031   }
1032 }
1033 
1034 // Creates and returns output stream with a list of object files for final
1035 // linking of distributed ThinLTO.
1036 static std::unique_ptr<raw_fd_ostream> CreateLinkedObjectsFile() {
1037   if (options::thinlto_linked_objects_file.empty())
1038     return nullptr;
1039   assert(options::thinlto_index_only);
1040   std::error_code EC;
1041   auto LinkedObjectsFile = std::make_unique<raw_fd_ostream>(
1042       options::thinlto_linked_objects_file, EC, sys::fs::OpenFlags::OF_None);
1043   if (EC)
1044     message(LDPL_FATAL, "Failed to create '%s': %s",
1045             options::thinlto_linked_objects_file.c_str(), EC.message().c_str());
1046   return LinkedObjectsFile;
1047 }
1048 
1049 /// Runs LTO and return a list of pairs <FileName, IsTemporary>.
1050 static std::vector<std::pair<SmallString<128>, bool>> runLTO() {
1051   // Map to own RAII objects that manage the file opening and releasing
1052   // interfaces with gold. This is needed only for ThinLTO mode, since
1053   // unlike regular LTO, where addModule will result in the opened file
1054   // being merged into a new combined module, we need to keep these files open
1055   // through Lto->run().
1056   DenseMap<void *, std::unique_ptr<PluginInputFile>> HandleToInputFile;
1057 
1058   // Owns string objects and tells if index file was already created.
1059   StringMap<bool> ObjectToIndexFileState;
1060 
1061   std::unique_ptr<raw_fd_ostream> LinkedObjects = CreateLinkedObjectsFile();
1062   std::unique_ptr<LTO> Lto = createLTO(
1063       [&ObjectToIndexFileState](const std::string &Identifier) {
1064         ObjectToIndexFileState[Identifier] = true;
1065       },
1066       LinkedObjects.get());
1067 
1068   std::string OldPrefix, NewPrefix;
1069   if (options::thinlto_index_only)
1070     getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
1071 
1072   std::string OldSuffix, NewSuffix;
1073   getThinLTOOldAndNewSuffix(OldSuffix, NewSuffix);
1074 
1075   for (claimed_file &F : Modules) {
1076     if (options::thinlto) {
1077       auto [It, Inserted] = HandleToInputFile.try_emplace(F.leader_handle);
1078       if (Inserted)
1079         It->second = std::make_unique<PluginInputFile>(F.handle);
1080     }
1081     // In case we are thin linking with a minimized bitcode file, ensure
1082     // the module paths encoded in the index reflect where the backends
1083     // will locate the full bitcode files for compiling/importing.
1084     std::string Identifier =
1085         getThinLTOObjectFileName(F.name, OldSuffix, NewSuffix);
1086     auto ObjFilename = ObjectToIndexFileState.insert({Identifier, false});
1087     assert(ObjFilename.second);
1088     if (const void *View = getSymbolsAndView(F))
1089       addModule(*Lto, F, View, ObjFilename.first->first());
1090     else if (options::thinlto_index_only) {
1091       ObjFilename.first->second = true;
1092       writeEmptyDistributedBuildOutputs(Identifier, OldPrefix, NewPrefix,
1093                                         /* SkipModule */ true);
1094     }
1095   }
1096 
1097   SmallString<128> Filename;
1098   // Note that getOutputFileName will append a unique ID for each task
1099   if (!options::obj_path.empty())
1100     Filename = options::obj_path;
1101   else if (options::TheOutputType == options::OT_SAVE_TEMPS)
1102     Filename = output_name + ".lto.o";
1103   else if (options::TheOutputType == options::OT_ASM_ONLY)
1104     Filename = output_name;
1105   bool SaveTemps = !Filename.empty();
1106 
1107   size_t MaxTasks = Lto->getMaxTasks();
1108   std::vector<std::pair<SmallString<128>, bool>> Files(MaxTasks);
1109 
1110   auto AddStream =
1111       [&](size_t Task,
1112           const Twine &ModuleName) -> std::unique_ptr<CachedFileStream> {
1113     Files[Task].second = !SaveTemps;
1114     int FD = getOutputFileName(Filename, /* TempOutFile */ !SaveTemps,
1115                                Files[Task].first, Task);
1116     return std::make_unique<CachedFileStream>(
1117         std::make_unique<llvm::raw_fd_ostream>(FD, true));
1118   };
1119 
1120   auto AddBuffer = [&](size_t Task, const Twine &moduleName,
1121                        std::unique_ptr<MemoryBuffer> MB) {
1122     *AddStream(Task, moduleName)->OS << MB->getBuffer();
1123   };
1124 
1125   FileCache Cache;
1126   if (!options::cache_dir.empty())
1127     Cache = check(localCache("ThinLTO", "Thin", options::cache_dir, AddBuffer));
1128 
1129   check(Lto->run(AddStream, Cache));
1130 
1131   // Write empty output files that may be expected by the distributed build
1132   // system.
1133   if (options::thinlto_index_only)
1134     for (auto &Identifier : ObjectToIndexFileState)
1135       if (!Identifier.getValue())
1136         writeEmptyDistributedBuildOutputs(std::string(Identifier.getKey()),
1137                                           OldPrefix, NewPrefix,
1138                                           /* SkipModule */ false);
1139 
1140   return Files;
1141 }
1142 
1143 /// gold informs us that all symbols have been read. At this point, we use
1144 /// get_symbols to see if any of our definitions have been overridden by a
1145 /// native object file. Then, perform optimization and codegen.
1146 static ld_plugin_status allSymbolsReadHook() {
1147   if (Modules.empty())
1148     return LDPS_OK;
1149 
1150   if (unsigned NumOpts = options::extra.size())
1151     cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
1152 
1153   // Initialize time trace profiler
1154   if (!options::time_trace_file.empty())
1155     llvm::timeTraceProfilerInitialize(options::time_trace_granularity,
1156                                       options::extra.size() ? options::extra[0]
1157                                                             : "LLVMgold");
1158   auto FinalizeTimeTrace = llvm::make_scope_exit([&]() {
1159     if (!llvm::timeTraceProfilerEnabled())
1160       return;
1161     assert(!options::time_trace_file.empty());
1162     check(llvm::timeTraceProfilerWrite(options::time_trace_file, output_name));
1163     llvm::timeTraceProfilerCleanup();
1164   });
1165 
1166   std::vector<std::pair<SmallString<128>, bool>> Files = runLTO();
1167 
1168   if (options::TheOutputType == options::OT_DISABLE ||
1169       options::TheOutputType == options::OT_BC_ONLY ||
1170       options::TheOutputType == options::OT_ASM_ONLY)
1171     return LDPS_OK;
1172 
1173   if (options::thinlto_index_only) {
1174     llvm_shutdown();
1175     cleanup_hook();
1176     exit(0);
1177   }
1178 
1179   for (const auto &F : Files)
1180     if (!F.first.empty())
1181       recordFile(std::string(F.first.str()), F.second);
1182 
1183   if (!options::extra_library_path.empty() &&
1184       set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
1185     message(LDPL_FATAL, "Unable to set the extra library path.");
1186 
1187   return LDPS_OK;
1188 }
1189 
1190 static ld_plugin_status all_symbols_read_hook(void) {
1191   ld_plugin_status Ret = allSymbolsReadHook();
1192   llvm_shutdown();
1193 
1194   if (options::TheOutputType == options::OT_BC_ONLY ||
1195       options::TheOutputType == options::OT_ASM_ONLY ||
1196       options::TheOutputType == options::OT_DISABLE) {
1197     if (options::TheOutputType == options::OT_DISABLE) {
1198       // Remove the output file here since ld.bfd creates the output file
1199       // early.
1200       std::error_code EC = sys::fs::remove(output_name);
1201       if (EC)
1202         message(LDPL_ERROR, "Failed to delete '%s': %s", output_name.c_str(),
1203                 EC.message().c_str());
1204     }
1205     exit(0);
1206   }
1207 
1208   return Ret;
1209 }
1210 
1211 static ld_plugin_status cleanup_hook(void) {
1212   for (std::string &Name : Cleanup) {
1213     std::error_code EC = sys::fs::remove(Name);
1214     if (EC)
1215       message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
1216               EC.message().c_str());
1217   }
1218 
1219   // Prune cache
1220   if (!options::cache_dir.empty()) {
1221     CachePruningPolicy policy = check(parseCachePruningPolicy(options::cache_policy));
1222     pruneCache(options::cache_dir, policy);
1223   }
1224 
1225   return LDPS_OK;
1226 }
1227