• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/StringSet.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/CodeGen/Analysis.h"
22 #include "llvm/CodeGen/CommandFlags.h"
23 #include "llvm/CodeGen/ParallelCG.h"
24 #include "llvm/IR/AutoUpgrade.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DiagnosticInfo.h"
27 #include "llvm/IR/DiagnosticPrinter.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/Verifier.h"
32 #include "llvm/Linker/IRMover.h"
33 #include "llvm/MC/SubtargetFeature.h"
34 #include "llvm/Object/FunctionIndexObjectFile.h"
35 #include "llvm/Object/IRObjectFile.h"
36 #include "llvm/Support/Host.h"
37 #include "llvm/Support/ManagedStatic.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/TargetRegistry.h"
40 #include "llvm/Support/TargetSelect.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Transforms/IPO.h"
43 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
44 #include "llvm/Transforms/Utils/GlobalStatus.h"
45 #include "llvm/Transforms/Utils/ModuleUtils.h"
46 #include "llvm/Transforms/Utils/ValueMapper.h"
47 #include <list>
48 #include <plugin-api.h>
49 #include <system_error>
50 #include <vector>
51 
52 #ifndef LDPO_PIE
53 // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
54 // Precise and Debian Wheezy (binutils 2.23 is required)
55 # define LDPO_PIE 3
56 #endif
57 
58 using namespace llvm;
59 
discard_message(int level,const char * format,...)60 static ld_plugin_status discard_message(int level, const char *format, ...) {
61   // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
62   // callback in the transfer vector. This should never be called.
63   abort();
64 }
65 
66 static ld_plugin_release_input_file release_input_file = nullptr;
67 static ld_plugin_get_input_file get_input_file = nullptr;
68 static ld_plugin_message message = discard_message;
69 
70 namespace {
71 struct claimed_file {
72   void *handle;
73   std::vector<ld_plugin_symbol> syms;
74 };
75 
76 /// RAII wrapper to manage opening and releasing of a ld_plugin_input_file.
77 struct PluginInputFile {
78   void *Handle;
79   ld_plugin_input_file File;
80 
PluginInputFile__anon57e56ea80111::PluginInputFile81   PluginInputFile(void *Handle) : Handle(Handle) {
82     if (get_input_file(Handle, &File) != LDPS_OK)
83       message(LDPL_FATAL, "Failed to get file information");
84   }
~PluginInputFile__anon57e56ea80111::PluginInputFile85   ~PluginInputFile() {
86     if (release_input_file(Handle) != LDPS_OK)
87       message(LDPL_FATAL, "Failed to release file information");
88   }
file__anon57e56ea80111::PluginInputFile89   ld_plugin_input_file &file() { return File; }
90 };
91 
92 struct ResolutionInfo {
93   bool IsLinkonceOdr = true;
94   bool UnnamedAddr = true;
95   GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
96   bool CommonInternal = false;
97   bool UseCommon = false;
98   unsigned CommonSize = 0;
99   unsigned CommonAlign = 0;
100   claimed_file *CommonFile = nullptr;
101 };
102 }
103 
104 static ld_plugin_add_symbols add_symbols = nullptr;
105 static ld_plugin_get_symbols get_symbols = nullptr;
106 static ld_plugin_add_input_file add_input_file = nullptr;
107 static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
108 static ld_plugin_get_view get_view = nullptr;
109 static Reloc::Model RelocationModel = Reloc::Default;
110 static std::string output_name = "";
111 static std::list<claimed_file> Modules;
112 static StringMap<ResolutionInfo> ResInfo;
113 static std::vector<std::string> Cleanup;
114 static llvm::TargetOptions TargetOpts;
115 
116 namespace options {
117   enum OutputType {
118     OT_NORMAL,
119     OT_DISABLE,
120     OT_BC_ONLY,
121     OT_SAVE_TEMPS
122   };
123   static bool generate_api_file = false;
124   static OutputType TheOutputType = OT_NORMAL;
125   static unsigned OptLevel = 2;
126   static unsigned Parallelism = 1;
127 #ifdef NDEBUG
128   static bool DisableVerify = true;
129 #else
130   static bool DisableVerify = false;
131 #endif
132   static std::string obj_path;
133   static std::string extra_library_path;
134   static std::string triple;
135   static std::string mcpu;
136   // When the thinlto plugin option is specified, only read the function
137   // the information from intermediate files and write a combined
138   // global index for the ThinLTO backends.
139   static bool thinlto = false;
140   // Additional options to pass into the code generator.
141   // Note: This array will contain all plugin options which are not claimed
142   // as plugin exclusive to pass to the code generator.
143   // For example, "generate-api-file" and "as"options are for the plugin
144   // use only and will not be passed.
145   static std::vector<const char *> extra;
146 
process_plugin_option(const char * opt_)147   static void process_plugin_option(const char *opt_)
148   {
149     if (opt_ == nullptr)
150       return;
151     llvm::StringRef opt = opt_;
152 
153     if (opt == "generate-api-file") {
154       generate_api_file = true;
155     } else if (opt.startswith("mcpu=")) {
156       mcpu = opt.substr(strlen("mcpu="));
157     } else if (opt.startswith("extra-library-path=")) {
158       extra_library_path = opt.substr(strlen("extra_library_path="));
159     } else if (opt.startswith("mtriple=")) {
160       triple = opt.substr(strlen("mtriple="));
161     } else if (opt.startswith("obj-path=")) {
162       obj_path = opt.substr(strlen("obj-path="));
163     } else if (opt == "emit-llvm") {
164       TheOutputType = OT_BC_ONLY;
165     } else if (opt == "save-temps") {
166       TheOutputType = OT_SAVE_TEMPS;
167     } else if (opt == "disable-output") {
168       TheOutputType = OT_DISABLE;
169     } else if (opt == "thinlto") {
170       thinlto = true;
171     } else if (opt.size() == 2 && opt[0] == 'O') {
172       if (opt[1] < '0' || opt[1] > '3')
173         message(LDPL_FATAL, "Optimization level must be between 0 and 3");
174       OptLevel = opt[1] - '0';
175     } else if (opt.startswith("jobs=")) {
176       if (StringRef(opt_ + 5).getAsInteger(10, Parallelism))
177         message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5);
178     } else if (opt == "disable-verify") {
179       DisableVerify = true;
180     } else {
181       // Save this option to pass to the code generator.
182       // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
183       // add that.
184       if (extra.empty())
185         extra.push_back("LLVMgold");
186 
187       extra.push_back(opt_);
188     }
189   }
190 }
191 
192 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
193                                         int *claimed);
194 static ld_plugin_status all_symbols_read_hook(void);
195 static ld_plugin_status cleanup_hook(void);
196 
197 extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
onload(ld_plugin_tv * tv)198 ld_plugin_status onload(ld_plugin_tv *tv) {
199   InitializeAllTargetInfos();
200   InitializeAllTargets();
201   InitializeAllTargetMCs();
202   InitializeAllAsmParsers();
203   InitializeAllAsmPrinters();
204 
205   // We're given a pointer to the first transfer vector. We read through them
206   // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
207   // contain pointers to functions that we need to call to register our own
208   // hooks. The others are addresses of functions we can use to call into gold
209   // for services.
210 
211   bool registeredClaimFile = false;
212   bool RegisteredAllSymbolsRead = false;
213 
214   for (; tv->tv_tag != LDPT_NULL; ++tv) {
215     switch (tv->tv_tag) {
216       case LDPT_OUTPUT_NAME:
217         output_name = tv->tv_u.tv_string;
218         break;
219       case LDPT_LINKER_OUTPUT:
220         switch (tv->tv_u.tv_val) {
221           case LDPO_REL:  // .o
222           case LDPO_DYN:  // .so
223           case LDPO_PIE:  // position independent executable
224             RelocationModel = Reloc::PIC_;
225             break;
226           case LDPO_EXEC:  // .exe
227             RelocationModel = Reloc::Static;
228             break;
229           default:
230             message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
231             return LDPS_ERR;
232         }
233         break;
234       case LDPT_OPTION:
235         options::process_plugin_option(tv->tv_u.tv_string);
236         break;
237       case LDPT_REGISTER_CLAIM_FILE_HOOK: {
238         ld_plugin_register_claim_file callback;
239         callback = tv->tv_u.tv_register_claim_file;
240 
241         if (callback(claim_file_hook) != LDPS_OK)
242           return LDPS_ERR;
243 
244         registeredClaimFile = true;
245       } break;
246       case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
247         ld_plugin_register_all_symbols_read callback;
248         callback = tv->tv_u.tv_register_all_symbols_read;
249 
250         if (callback(all_symbols_read_hook) != LDPS_OK)
251           return LDPS_ERR;
252 
253         RegisteredAllSymbolsRead = true;
254       } break;
255       case LDPT_REGISTER_CLEANUP_HOOK: {
256         ld_plugin_register_cleanup callback;
257         callback = tv->tv_u.tv_register_cleanup;
258 
259         if (callback(cleanup_hook) != LDPS_OK)
260           return LDPS_ERR;
261       } break;
262       case LDPT_GET_INPUT_FILE:
263         get_input_file = tv->tv_u.tv_get_input_file;
264         break;
265       case LDPT_RELEASE_INPUT_FILE:
266         release_input_file = tv->tv_u.tv_release_input_file;
267         break;
268       case LDPT_ADD_SYMBOLS:
269         add_symbols = tv->tv_u.tv_add_symbols;
270         break;
271       case LDPT_GET_SYMBOLS_V2:
272         get_symbols = tv->tv_u.tv_get_symbols;
273         break;
274       case LDPT_ADD_INPUT_FILE:
275         add_input_file = tv->tv_u.tv_add_input_file;
276         break;
277       case LDPT_SET_EXTRA_LIBRARY_PATH:
278         set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
279         break;
280       case LDPT_GET_VIEW:
281         get_view = tv->tv_u.tv_get_view;
282         break;
283       case LDPT_MESSAGE:
284         message = tv->tv_u.tv_message;
285         break;
286       default:
287         break;
288     }
289   }
290 
291   if (!registeredClaimFile) {
292     message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
293     return LDPS_ERR;
294   }
295   if (!add_symbols) {
296     message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
297     return LDPS_ERR;
298   }
299 
300   if (!RegisteredAllSymbolsRead)
301     return LDPS_OK;
302 
303   if (!get_input_file) {
304     message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
305     return LDPS_ERR;
306   }
307   if (!release_input_file) {
308     message(LDPL_ERROR, "relesase_input_file not passed to LLVMgold.");
309     return LDPS_ERR;
310   }
311 
312   return LDPS_OK;
313 }
314 
getBaseObject(const GlobalValue & GV)315 static const GlobalObject *getBaseObject(const GlobalValue &GV) {
316   if (auto *GA = dyn_cast<GlobalAlias>(&GV))
317     return GA->getBaseObject();
318   return cast<GlobalObject>(&GV);
319 }
320 
shouldSkip(uint32_t Symflags)321 static bool shouldSkip(uint32_t Symflags) {
322   if (!(Symflags & object::BasicSymbolRef::SF_Global))
323     return true;
324   if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
325     return true;
326   return false;
327 }
328 
diagnosticHandler(const DiagnosticInfo & DI)329 static void diagnosticHandler(const DiagnosticInfo &DI) {
330   if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) {
331     std::error_code EC = BDI->getError();
332     if (EC == BitcodeError::InvalidBitcodeSignature)
333       return;
334   }
335 
336   std::string ErrStorage;
337   {
338     raw_string_ostream OS(ErrStorage);
339     DiagnosticPrinterRawOStream DP(OS);
340     DI.print(DP);
341   }
342   ld_plugin_level Level;
343   switch (DI.getSeverity()) {
344   case DS_Error:
345     message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
346             ErrStorage.c_str());
347     llvm_unreachable("Fatal doesn't return.");
348   case DS_Warning:
349     Level = LDPL_WARNING;
350     break;
351   case DS_Note:
352   case DS_Remark:
353     Level = LDPL_INFO;
354     break;
355   }
356   message(Level, "LLVM gold plugin: %s",  ErrStorage.c_str());
357 }
358 
diagnosticHandlerForContext(const DiagnosticInfo & DI,void * Context)359 static void diagnosticHandlerForContext(const DiagnosticInfo &DI,
360                                         void *Context) {
361   diagnosticHandler(DI);
362 }
363 
364 static GlobalValue::VisibilityTypes
getMinVisibility(GlobalValue::VisibilityTypes A,GlobalValue::VisibilityTypes B)365 getMinVisibility(GlobalValue::VisibilityTypes A,
366                  GlobalValue::VisibilityTypes B) {
367   if (A == GlobalValue::HiddenVisibility)
368     return A;
369   if (B == GlobalValue::HiddenVisibility)
370     return B;
371   if (A == GlobalValue::ProtectedVisibility)
372     return A;
373   return B;
374 }
375 
376 /// Called by gold to see whether this file is one that our plugin can handle.
377 /// We'll try to open it and register all the symbols with add_symbol if
378 /// possible.
claim_file_hook(const ld_plugin_input_file * file,int * claimed)379 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
380                                         int *claimed) {
381   LLVMContext Context;
382   MemoryBufferRef BufferRef;
383   std::unique_ptr<MemoryBuffer> Buffer;
384   if (get_view) {
385     const void *view;
386     if (get_view(file->handle, &view) != LDPS_OK) {
387       message(LDPL_ERROR, "Failed to get a view of %s", file->name);
388       return LDPS_ERR;
389     }
390     BufferRef =
391         MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
392   } else {
393     int64_t offset = 0;
394     // Gold has found what might be IR part-way inside of a file, such as
395     // an .a archive.
396     if (file->offset) {
397       offset = file->offset;
398     }
399     ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
400         MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize,
401                                        offset);
402     if (std::error_code EC = BufferOrErr.getError()) {
403       message(LDPL_ERROR, EC.message().c_str());
404       return LDPS_ERR;
405     }
406     Buffer = std::move(BufferOrErr.get());
407     BufferRef = Buffer->getMemBufferRef();
408   }
409 
410   Context.setDiagnosticHandler(diagnosticHandlerForContext);
411   ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
412       object::IRObjectFile::create(BufferRef, Context);
413   std::error_code EC = ObjOrErr.getError();
414   if (EC == object::object_error::invalid_file_type ||
415       EC == object::object_error::bitcode_section_not_found)
416     return LDPS_OK;
417 
418   *claimed = 1;
419 
420   if (EC) {
421     message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s",
422             EC.message().c_str());
423     return LDPS_ERR;
424   }
425   std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
426 
427   Modules.resize(Modules.size() + 1);
428   claimed_file &cf = Modules.back();
429 
430   cf.handle = file->handle;
431 
432   // If we are doing ThinLTO compilation, don't need to process the symbols.
433   // Later we simply build a combined index file after all files are claimed.
434   if (options::thinlto)
435     return LDPS_OK;
436 
437   for (auto &Sym : Obj->symbols()) {
438     uint32_t Symflags = Sym.getFlags();
439     if (shouldSkip(Symflags))
440       continue;
441 
442     cf.syms.push_back(ld_plugin_symbol());
443     ld_plugin_symbol &sym = cf.syms.back();
444     sym.version = nullptr;
445 
446     SmallString<64> Name;
447     {
448       raw_svector_ostream OS(Name);
449       Sym.printName(OS);
450     }
451     sym.name = strdup(Name.c_str());
452 
453     const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
454 
455     ResolutionInfo &Res = ResInfo[sym.name];
456 
457     sym.visibility = LDPV_DEFAULT;
458     if (GV) {
459       Res.UnnamedAddr &= GV->hasUnnamedAddr();
460       Res.IsLinkonceOdr &= GV->hasLinkOnceLinkage();
461       if (GV->hasCommonLinkage()) {
462         Res.CommonAlign = std::max(Res.CommonAlign, GV->getAlignment());
463         const DataLayout &DL = GV->getParent()->getDataLayout();
464         uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
465         if (Size >= Res.CommonSize) {
466           Res.CommonSize = Size;
467           Res.CommonFile = &cf;
468         }
469       }
470       Res.Visibility = getMinVisibility(Res.Visibility, GV->getVisibility());
471       switch (GV->getVisibility()) {
472       case GlobalValue::DefaultVisibility:
473         sym.visibility = LDPV_DEFAULT;
474         break;
475       case GlobalValue::HiddenVisibility:
476         sym.visibility = LDPV_HIDDEN;
477         break;
478       case GlobalValue::ProtectedVisibility:
479         sym.visibility = LDPV_PROTECTED;
480         break;
481       }
482     }
483 
484     if (Symflags & object::BasicSymbolRef::SF_Undefined) {
485       sym.def = LDPK_UNDEF;
486       if (GV && GV->hasExternalWeakLinkage())
487         sym.def = LDPK_WEAKUNDEF;
488     } else {
489       sym.def = LDPK_DEF;
490       if (GV) {
491         assert(!GV->hasExternalWeakLinkage() &&
492                !GV->hasAvailableExternallyLinkage() && "Not a declaration!");
493         if (GV->hasCommonLinkage())
494           sym.def = LDPK_COMMON;
495         else if (GV->isWeakForLinker())
496           sym.def = LDPK_WEAKDEF;
497       }
498     }
499 
500     sym.size = 0;
501     sym.comdat_key = nullptr;
502     if (GV) {
503       const GlobalObject *Base = getBaseObject(*GV);
504       if (!Base)
505         message(LDPL_FATAL, "Unable to determine comdat of alias!");
506       const Comdat *C = Base->getComdat();
507       if (C)
508         sym.comdat_key = strdup(C->getName().str().c_str());
509     }
510 
511     sym.resolution = LDPR_UNKNOWN;
512   }
513 
514   if (!cf.syms.empty()) {
515     if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) {
516       message(LDPL_ERROR, "Unable to add symbols!");
517       return LDPS_ERR;
518     }
519   }
520 
521   return LDPS_OK;
522 }
523 
internalize(GlobalValue & GV)524 static void internalize(GlobalValue &GV) {
525   if (GV.isDeclarationForLinker())
526     return; // We get here if there is a matching asm definition.
527   if (!GV.hasLocalLinkage())
528     GV.setLinkage(GlobalValue::InternalLinkage);
529 }
530 
getResolutionName(ld_plugin_symbol_resolution R)531 static const char *getResolutionName(ld_plugin_symbol_resolution R) {
532   switch (R) {
533   case LDPR_UNKNOWN:
534     return "UNKNOWN";
535   case LDPR_UNDEF:
536     return "UNDEF";
537   case LDPR_PREVAILING_DEF:
538     return "PREVAILING_DEF";
539   case LDPR_PREVAILING_DEF_IRONLY:
540     return "PREVAILING_DEF_IRONLY";
541   case LDPR_PREEMPTED_REG:
542     return "PREEMPTED_REG";
543   case LDPR_PREEMPTED_IR:
544     return "PREEMPTED_IR";
545   case LDPR_RESOLVED_IR:
546     return "RESOLVED_IR";
547   case LDPR_RESOLVED_EXEC:
548     return "RESOLVED_EXEC";
549   case LDPR_RESOLVED_DYN:
550     return "RESOLVED_DYN";
551   case LDPR_PREVAILING_DEF_IRONLY_EXP:
552     return "PREVAILING_DEF_IRONLY_EXP";
553   }
554   llvm_unreachable("Unknown resolution");
555 }
556 
freeSymName(ld_plugin_symbol & Sym)557 static void freeSymName(ld_plugin_symbol &Sym) {
558   free(Sym.name);
559   free(Sym.comdat_key);
560   Sym.name = nullptr;
561   Sym.comdat_key = nullptr;
562 }
563 
564 static std::unique_ptr<FunctionInfoIndex>
getFunctionIndexForFile(claimed_file & F,ld_plugin_input_file & Info)565 getFunctionIndexForFile(claimed_file &F, ld_plugin_input_file &Info) {
566 
567   if (get_symbols(F.handle, F.syms.size(), &F.syms[0]) != LDPS_OK)
568     message(LDPL_FATAL, "Failed to get symbol information");
569 
570   const void *View;
571   if (get_view(F.handle, &View) != LDPS_OK)
572     message(LDPL_FATAL, "Failed to get a view of file");
573 
574   MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
575                             Info.name);
576 
577   // Don't bother trying to build an index if there is no summary information
578   // in this bitcode file.
579   if (!object::FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer(
580           BufferRef, diagnosticHandler))
581     return std::unique_ptr<FunctionInfoIndex>(nullptr);
582 
583   ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
584       object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler);
585 
586   if (std::error_code EC = ObjOrErr.getError())
587     message(LDPL_FATAL, "Could not read function index bitcode from file : %s",
588             EC.message().c_str());
589 
590   object::FunctionIndexObjectFile &Obj = **ObjOrErr;
591 
592   return Obj.takeIndex();
593 }
594 
595 static std::unique_ptr<Module>
getModuleForFile(LLVMContext & Context,claimed_file & F,ld_plugin_input_file & Info,raw_fd_ostream * ApiFile,StringSet<> & Internalize,StringSet<> & Maybe,std::vector<GlobalValue * > & Keep)596 getModuleForFile(LLVMContext &Context, claimed_file &F,
597                  ld_plugin_input_file &Info, raw_fd_ostream *ApiFile,
598                  StringSet<> &Internalize, StringSet<> &Maybe,
599                  std::vector<GlobalValue *> &Keep) {
600 
601   if (get_symbols(F.handle, F.syms.size(), F.syms.data()) != LDPS_OK)
602     message(LDPL_FATAL, "Failed to get symbol information");
603 
604   const void *View;
605   if (get_view(F.handle, &View) != LDPS_OK)
606     message(LDPL_FATAL, "Failed to get a view of file");
607 
608   MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
609                             Info.name);
610   ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
611       object::IRObjectFile::create(BufferRef, Context);
612 
613   if (std::error_code EC = ObjOrErr.getError())
614     message(LDPL_FATAL, "Could not read bitcode from file : %s",
615             EC.message().c_str());
616 
617   object::IRObjectFile &Obj = **ObjOrErr;
618 
619   Module &M = Obj.getModule();
620 
621   M.materializeMetadata();
622   UpgradeDebugInfo(M);
623 
624   SmallPtrSet<GlobalValue *, 8> Used;
625   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
626 
627   unsigned SymNum = 0;
628   for (auto &ObjSym : Obj.symbols()) {
629     GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl());
630     if (GV && GV->hasAppendingLinkage())
631       Keep.push_back(GV);
632 
633     if (shouldSkip(ObjSym.getFlags()))
634       continue;
635     ld_plugin_symbol &Sym = F.syms[SymNum];
636     ++SymNum;
637 
638     ld_plugin_symbol_resolution Resolution =
639         (ld_plugin_symbol_resolution)Sym.resolution;
640 
641     if (options::generate_api_file)
642       *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
643 
644     if (!GV) {
645       freeSymName(Sym);
646       continue; // Asm symbol.
647     }
648 
649     ResolutionInfo &Res = ResInfo[Sym.name];
650     if (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP && !Res.IsLinkonceOdr)
651       Resolution = LDPR_PREVAILING_DEF;
652 
653     GV->setUnnamedAddr(Res.UnnamedAddr);
654     GV->setVisibility(Res.Visibility);
655 
656     // Override gold's resolution for common symbols. We want the largest
657     // one to win.
658     if (GV->hasCommonLinkage()) {
659       cast<GlobalVariable>(GV)->setAlignment(Res.CommonAlign);
660       if (Resolution == LDPR_PREVAILING_DEF_IRONLY)
661         Res.CommonInternal = true;
662 
663       if (Resolution == LDPR_PREVAILING_DEF_IRONLY ||
664           Resolution == LDPR_PREVAILING_DEF)
665         Res.UseCommon = true;
666 
667       if (Res.CommonFile == &F && Res.UseCommon) {
668         if (Res.CommonInternal)
669           Resolution = LDPR_PREVAILING_DEF_IRONLY;
670         else
671           Resolution = LDPR_PREVAILING_DEF;
672       } else {
673         Resolution = LDPR_PREEMPTED_IR;
674       }
675     }
676 
677     switch (Resolution) {
678     case LDPR_UNKNOWN:
679       llvm_unreachable("Unexpected resolution");
680 
681     case LDPR_RESOLVED_IR:
682     case LDPR_RESOLVED_EXEC:
683     case LDPR_RESOLVED_DYN:
684     case LDPR_PREEMPTED_IR:
685     case LDPR_PREEMPTED_REG:
686       break;
687 
688     case LDPR_UNDEF:
689       if (!GV->isDeclarationForLinker())
690         assert(GV->hasComdat());
691       break;
692 
693     case LDPR_PREVAILING_DEF_IRONLY: {
694       Keep.push_back(GV);
695       // The IR linker has to be able to map this value to a declaration,
696       // so we can only internalize after linking.
697       if (!Used.count(GV))
698         Internalize.insert(GV->getName());
699       break;
700     }
701 
702     case LDPR_PREVAILING_DEF:
703       Keep.push_back(GV);
704       // There is a non IR use, so we have to force optimizations to keep this.
705       switch (GV->getLinkage()) {
706       default:
707         break;
708       case GlobalValue::LinkOnceAnyLinkage:
709         GV->setLinkage(GlobalValue::WeakAnyLinkage);
710         break;
711       case GlobalValue::LinkOnceODRLinkage:
712         GV->setLinkage(GlobalValue::WeakODRLinkage);
713         break;
714       }
715       break;
716 
717     case LDPR_PREVAILING_DEF_IRONLY_EXP: {
718       // We can only check for address uses after we merge the modules. The
719       // reason is that this GV might have a copy in another module
720       // and in that module the address might be significant, but that
721       // copy will be LDPR_PREEMPTED_IR.
722       Maybe.insert(GV->getName());
723       Keep.push_back(GV);
724       break;
725     }
726     }
727 
728     freeSymName(Sym);
729   }
730 
731   return Obj.takeModule();
732 }
733 
runLTOPasses(Module & M,TargetMachine & TM)734 static void runLTOPasses(Module &M, TargetMachine &TM) {
735   M.setDataLayout(TM.createDataLayout());
736 
737   legacy::PassManager passes;
738   passes.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
739 
740   PassManagerBuilder PMB;
741   PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple()));
742   PMB.Inliner = createFunctionInliningPass();
743   // Unconditionally verify input since it is not verified before this
744   // point and has unknown origin.
745   PMB.VerifyInput = true;
746   PMB.VerifyOutput = !options::DisableVerify;
747   PMB.LoopVectorize = true;
748   PMB.SLPVectorize = true;
749   PMB.OptLevel = options::OptLevel;
750   PMB.populateLTOPassManager(passes);
751   passes.run(M);
752 }
753 
saveBCFile(StringRef Path,Module & M)754 static void saveBCFile(StringRef Path, Module &M) {
755   std::error_code EC;
756   raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
757   if (EC)
758     message(LDPL_FATAL, "Failed to write the output file.");
759   WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
760 }
761 
codegen(std::unique_ptr<Module> M)762 static void codegen(std::unique_ptr<Module> M) {
763   const std::string &TripleStr = M->getTargetTriple();
764   Triple TheTriple(TripleStr);
765 
766   std::string ErrMsg;
767   const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
768   if (!TheTarget)
769     message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str());
770 
771   if (unsigned NumOpts = options::extra.size())
772     cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
773 
774   SubtargetFeatures Features;
775   Features.getDefaultSubtargetFeatures(TheTriple);
776   for (const std::string &A : MAttrs)
777     Features.AddFeature(A);
778 
779   TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
780   CodeGenOpt::Level CGOptLevel;
781   switch (options::OptLevel) {
782   case 0:
783     CGOptLevel = CodeGenOpt::None;
784     break;
785   case 1:
786     CGOptLevel = CodeGenOpt::Less;
787     break;
788   case 2:
789     CGOptLevel = CodeGenOpt::Default;
790     break;
791   case 3:
792     CGOptLevel = CodeGenOpt::Aggressive;
793     break;
794   }
795   std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
796       TripleStr, options::mcpu, Features.getString(), Options, RelocationModel,
797       CodeModel::Default, CGOptLevel));
798 
799   runLTOPasses(*M, *TM);
800 
801   if (options::TheOutputType == options::OT_SAVE_TEMPS)
802     saveBCFile(output_name + ".opt.bc", *M);
803 
804   SmallString<128> Filename;
805   if (!options::obj_path.empty())
806     Filename = options::obj_path;
807   else if (options::TheOutputType == options::OT_SAVE_TEMPS)
808     Filename = output_name + ".o";
809 
810   std::vector<SmallString<128>> Filenames(options::Parallelism);
811   bool TempOutFile = Filename.empty();
812   {
813     // Open a file descriptor for each backend thread. This is done in a block
814     // so that the output file descriptors are closed before gold opens them.
815     std::list<llvm::raw_fd_ostream> OSs;
816     std::vector<llvm::raw_pwrite_stream *> OSPtrs(options::Parallelism);
817     for (unsigned I = 0; I != options::Parallelism; ++I) {
818       int FD;
819       if (TempOutFile) {
820         std::error_code EC =
821             sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filenames[I]);
822         if (EC)
823           message(LDPL_FATAL, "Could not create temporary file: %s",
824                   EC.message().c_str());
825       } else {
826         Filenames[I] = Filename;
827         if (options::Parallelism != 1)
828           Filenames[I] += utostr(I);
829         std::error_code EC =
830             sys::fs::openFileForWrite(Filenames[I], FD, sys::fs::F_None);
831         if (EC)
832           message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
833       }
834       OSs.emplace_back(FD, true);
835       OSPtrs[I] = &OSs.back();
836     }
837 
838     // Run backend threads.
839     splitCodeGen(std::move(M), OSPtrs, options::mcpu, Features.getString(),
840                  Options, RelocationModel, CodeModel::Default, CGOptLevel);
841   }
842 
843   for (auto &Filename : Filenames) {
844     if (add_input_file(Filename.c_str()) != LDPS_OK)
845       message(LDPL_FATAL,
846               "Unable to add .o file to the link. File left behind in: %s",
847               Filename.c_str());
848     if (TempOutFile)
849       Cleanup.push_back(Filename.c_str());
850   }
851 }
852 
853 /// gold informs us that all symbols have been read. At this point, we use
854 /// get_symbols to see if any of our definitions have been overridden by a
855 /// native object file. Then, perform optimization and codegen.
allSymbolsReadHook(raw_fd_ostream * ApiFile)856 static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
857   if (Modules.empty())
858     return LDPS_OK;
859 
860   // If we are doing ThinLTO compilation, simply build the combined
861   // function index/summary and emit it. We don't need to parse the modules
862   // and link them in this case.
863   if (options::thinlto) {
864     FunctionInfoIndex CombinedIndex;
865     uint64_t NextModuleId = 0;
866     for (claimed_file &F : Modules) {
867       PluginInputFile InputFile(F.handle);
868 
869       std::unique_ptr<FunctionInfoIndex> Index =
870           getFunctionIndexForFile(F, InputFile.file());
871 
872       // Skip files without a function summary.
873       if (Index)
874         CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
875     }
876 
877     std::error_code EC;
878     raw_fd_ostream OS(output_name + ".thinlto.bc", EC,
879                       sys::fs::OpenFlags::F_None);
880     if (EC)
881       message(LDPL_FATAL, "Unable to open %s.thinlto.bc for writing: %s",
882               output_name.data(), EC.message().c_str());
883     WriteFunctionSummaryToFile(CombinedIndex, OS);
884     OS.close();
885 
886     cleanup_hook();
887     exit(0);
888   }
889 
890   LLVMContext Context;
891   Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true);
892 
893   std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
894   IRMover L(*Combined);
895 
896   std::string DefaultTriple = sys::getDefaultTargetTriple();
897 
898   StringSet<> Internalize;
899   StringSet<> Maybe;
900   for (claimed_file &F : Modules) {
901     PluginInputFile InputFile(F.handle);
902     std::vector<GlobalValue *> Keep;
903     std::unique_ptr<Module> M = getModuleForFile(
904         Context, F, InputFile.file(), ApiFile, Internalize, Maybe, Keep);
905     if (!options::triple.empty())
906       M->setTargetTriple(options::triple.c_str());
907     else if (M->getTargetTriple().empty())
908       M->setTargetTriple(DefaultTriple);
909 
910     if (L.move(*M, Keep, [](GlobalValue &, IRMover::ValueAdder) {}))
911       message(LDPL_FATAL, "Failed to link module");
912   }
913 
914   for (const auto &Name : Internalize) {
915     GlobalValue *GV = Combined->getNamedValue(Name.first());
916     if (GV)
917       internalize(*GV);
918   }
919 
920   for (const auto &Name : Maybe) {
921     GlobalValue *GV = Combined->getNamedValue(Name.first());
922     if (!GV)
923       continue;
924     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
925     if (canBeOmittedFromSymbolTable(GV))
926       internalize(*GV);
927   }
928 
929   if (options::TheOutputType == options::OT_DISABLE)
930     return LDPS_OK;
931 
932   if (options::TheOutputType != options::OT_NORMAL) {
933     std::string path;
934     if (options::TheOutputType == options::OT_BC_ONLY)
935       path = output_name;
936     else
937       path = output_name + ".bc";
938     saveBCFile(path, *Combined);
939     if (options::TheOutputType == options::OT_BC_ONLY)
940       return LDPS_OK;
941   }
942 
943   codegen(std::move(Combined));
944 
945   if (!options::extra_library_path.empty() &&
946       set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
947     message(LDPL_FATAL, "Unable to set the extra library path.");
948 
949   return LDPS_OK;
950 }
951 
all_symbols_read_hook(void)952 static ld_plugin_status all_symbols_read_hook(void) {
953   ld_plugin_status Ret;
954   if (!options::generate_api_file) {
955     Ret = allSymbolsReadHook(nullptr);
956   } else {
957     std::error_code EC;
958     raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None);
959     if (EC)
960       message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
961               EC.message().c_str());
962     Ret = allSymbolsReadHook(&ApiFile);
963   }
964 
965   llvm_shutdown();
966 
967   if (options::TheOutputType == options::OT_BC_ONLY ||
968       options::TheOutputType == options::OT_DISABLE) {
969     if (options::TheOutputType == options::OT_DISABLE)
970       // Remove the output file here since ld.bfd creates the output file
971       // early.
972       sys::fs::remove(output_name);
973     exit(0);
974   }
975 
976   return Ret;
977 }
978 
cleanup_hook(void)979 static ld_plugin_status cleanup_hook(void) {
980   for (std::string &Name : Cleanup) {
981     std::error_code EC = sys::fs::remove(Name);
982     if (EC)
983       message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
984               EC.message().c_str());
985   }
986 
987   return LDPS_OK;
988 }
989