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