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