]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/LTO/LTOModule.cpp
Update bmake to version 20180919
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / LTO / LTOModule.cpp
1 //===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
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 file implements the Link Time Optimization library. This library is
11 // intended to be used by linker to optimize code at link time.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/LTO/legacy/LTOModule.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Analysis/ObjectUtils.h"
18 #include "llvm/Bitcode/BitcodeReader.h"
19 #include "llvm/CodeGen/TargetLoweringObjectFile.h"
20 #include "llvm/CodeGen/TargetSubtargetInfo.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Mangler.h"
24 #include "llvm/IR/Metadata.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCParser/MCAsmParser.h"
29 #include "llvm/MC/MCSection.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/MC/SubtargetFeature.h"
33 #include "llvm/Object/IRObjectFile.h"
34 #include "llvm/Object/ObjectFile.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/Host.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/Path.h"
39 #include "llvm/Support/SourceMgr.h"
40 #include "llvm/Support/TargetRegistry.h"
41 #include "llvm/Support/TargetSelect.h"
42 #include "llvm/Transforms/Utils/GlobalStatus.h"
43 #include <system_error>
44 using namespace llvm;
45 using namespace llvm::object;
46
47 LTOModule::LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
48                      llvm::TargetMachine *TM)
49     : Mod(std::move(M)), MBRef(MBRef), _target(TM) {
50   SymTab.addModule(Mod.get());
51 }
52
53 LTOModule::~LTOModule() {}
54
55 /// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
56 /// bitcode.
57 bool LTOModule::isBitcodeFile(const void *Mem, size_t Length) {
58   Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
59       MemoryBufferRef(StringRef((const char *)Mem, Length), "<mem>"));
60   if (!BCData) {
61     consumeError(BCData.takeError());
62     return false;
63   }
64   return true;
65 }
66
67 bool LTOModule::isBitcodeFile(StringRef Path) {
68   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
69       MemoryBuffer::getFile(Path);
70   if (!BufferOrErr)
71     return false;
72
73   Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
74       BufferOrErr.get()->getMemBufferRef());
75   if (!BCData) {
76     consumeError(BCData.takeError());
77     return false;
78   }
79   return true;
80 }
81
82 bool LTOModule::isThinLTO() {
83   Expected<BitcodeLTOInfo> Result = getBitcodeLTOInfo(MBRef);
84   if (!Result) {
85     logAllUnhandledErrors(Result.takeError(), errs(), "");
86     return false;
87   }
88   return Result->IsThinLTO;
89 }
90
91 bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer,
92                                    StringRef TriplePrefix) {
93   Expected<MemoryBufferRef> BCOrErr =
94       IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
95   if (!BCOrErr) {
96     consumeError(BCOrErr.takeError());
97     return false;
98   }
99   LLVMContext Context;
100   ErrorOr<std::string> TripleOrErr =
101       expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(*BCOrErr));
102   if (!TripleOrErr)
103     return false;
104   return StringRef(*TripleOrErr).startswith(TriplePrefix);
105 }
106
107 std::string LTOModule::getProducerString(MemoryBuffer *Buffer) {
108   Expected<MemoryBufferRef> BCOrErr =
109       IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
110   if (!BCOrErr) {
111     consumeError(BCOrErr.takeError());
112     return "";
113   }
114   LLVMContext Context;
115   ErrorOr<std::string> ProducerOrErr = expectedToErrorOrAndEmitErrors(
116       Context, getBitcodeProducerString(*BCOrErr));
117   if (!ProducerOrErr)
118     return "";
119   return *ProducerOrErr;
120 }
121
122 ErrorOr<std::unique_ptr<LTOModule>>
123 LTOModule::createFromFile(LLVMContext &Context, StringRef path,
124                           const TargetOptions &options) {
125   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
126       MemoryBuffer::getFile(path);
127   if (std::error_code EC = BufferOrErr.getError()) {
128     Context.emitError(EC.message());
129     return EC;
130   }
131   std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
132   return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
133                        /* ShouldBeLazy*/ false);
134 }
135
136 ErrorOr<std::unique_ptr<LTOModule>>
137 LTOModule::createFromOpenFile(LLVMContext &Context, int fd, StringRef path,
138                               size_t size, const TargetOptions &options) {
139   return createFromOpenFileSlice(Context, fd, path, size, 0, options);
140 }
141
142 ErrorOr<std::unique_ptr<LTOModule>>
143 LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
144                                    size_t map_size, off_t offset,
145                                    const TargetOptions &options) {
146   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
147       MemoryBuffer::getOpenFileSlice(fd, path, map_size, offset);
148   if (std::error_code EC = BufferOrErr.getError()) {
149     Context.emitError(EC.message());
150     return EC;
151   }
152   std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
153   return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
154                        /* ShouldBeLazy */ false);
155 }
156
157 ErrorOr<std::unique_ptr<LTOModule>>
158 LTOModule::createFromBuffer(LLVMContext &Context, const void *mem,
159                             size_t length, const TargetOptions &options,
160                             StringRef path) {
161   StringRef Data((const char *)mem, length);
162   MemoryBufferRef Buffer(Data, path);
163   return makeLTOModule(Buffer, options, Context, /* ShouldBeLazy */ false);
164 }
165
166 ErrorOr<std::unique_ptr<LTOModule>>
167 LTOModule::createInLocalContext(std::unique_ptr<LLVMContext> Context,
168                                 const void *mem, size_t length,
169                                 const TargetOptions &options, StringRef path) {
170   StringRef Data((const char *)mem, length);
171   MemoryBufferRef Buffer(Data, path);
172   // If we own a context, we know this is being used only for symbol extraction,
173   // not linking.  Be lazy in that case.
174   ErrorOr<std::unique_ptr<LTOModule>> Ret =
175       makeLTOModule(Buffer, options, *Context, /* ShouldBeLazy */ true);
176   if (Ret)
177     (*Ret)->OwnedContext = std::move(Context);
178   return Ret;
179 }
180
181 static ErrorOr<std::unique_ptr<Module>>
182 parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context,
183                      bool ShouldBeLazy) {
184   // Find the buffer.
185   Expected<MemoryBufferRef> MBOrErr =
186       IRObjectFile::findBitcodeInMemBuffer(Buffer);
187   if (Error E = MBOrErr.takeError()) {
188     std::error_code EC = errorToErrorCode(std::move(E));
189     Context.emitError(EC.message());
190     return EC;
191   }
192
193   if (!ShouldBeLazy) {
194     // Parse the full file.
195     return expectedToErrorOrAndEmitErrors(Context,
196                                           parseBitcodeFile(*MBOrErr, Context));
197   }
198
199   // Parse lazily.
200   return expectedToErrorOrAndEmitErrors(
201       Context,
202       getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/));
203 }
204
205 ErrorOr<std::unique_ptr<LTOModule>>
206 LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
207                          LLVMContext &Context, bool ShouldBeLazy) {
208   ErrorOr<std::unique_ptr<Module>> MOrErr =
209       parseBitcodeFileImpl(Buffer, Context, ShouldBeLazy);
210   if (std::error_code EC = MOrErr.getError())
211     return EC;
212   std::unique_ptr<Module> &M = *MOrErr;
213
214   std::string TripleStr = M->getTargetTriple();
215   if (TripleStr.empty())
216     TripleStr = sys::getDefaultTargetTriple();
217   llvm::Triple Triple(TripleStr);
218
219   // find machine architecture for this module
220   std::string errMsg;
221   const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
222   if (!march)
223     return std::unique_ptr<LTOModule>(nullptr);
224
225   // construct LTOModule, hand over ownership of module and target
226   SubtargetFeatures Features;
227   Features.getDefaultSubtargetFeatures(Triple);
228   std::string FeatureStr = Features.getString();
229   // Set a default CPU for Darwin triples.
230   std::string CPU;
231   if (Triple.isOSDarwin()) {
232     if (Triple.getArch() == llvm::Triple::x86_64)
233       CPU = "core2";
234     else if (Triple.getArch() == llvm::Triple::x86)
235       CPU = "yonah";
236     else if (Triple.getArch() == llvm::Triple::aarch64)
237       CPU = "cyclone";
238   }
239
240   TargetMachine *target =
241       march->createTargetMachine(TripleStr, CPU, FeatureStr, options, None);
242
243   std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(M), Buffer, target));
244   Ret->parseSymbols();
245   Ret->parseMetadata();
246
247   return std::move(Ret);
248 }
249
250 /// Create a MemoryBuffer from a memory range with an optional name.
251 std::unique_ptr<MemoryBuffer>
252 LTOModule::makeBuffer(const void *mem, size_t length, StringRef name) {
253   const char *startPtr = (const char*)mem;
254   return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
255 }
256
257 /// objcClassNameFromExpression - Get string that the data pointer points to.
258 bool
259 LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
260   if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
261     Constant *op = ce->getOperand(0);
262     if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
263       Constant *cn = gvn->getInitializer();
264       if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
265         if (ca->isCString()) {
266           name = (".objc_class_name_" + ca->getAsCString()).str();
267           return true;
268         }
269       }
270     }
271   }
272   return false;
273 }
274
275 /// addObjCClass - Parse i386/ppc ObjC class data structure.
276 void LTOModule::addObjCClass(const GlobalVariable *clgv) {
277   const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
278   if (!c) return;
279
280   // second slot in __OBJC,__class is pointer to superclass name
281   std::string superclassName;
282   if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
283     auto IterBool =
284         _undefines.insert(std::make_pair(superclassName, NameAndAttributes()));
285     if (IterBool.second) {
286       NameAndAttributes &info = IterBool.first->second;
287       info.name = IterBool.first->first();
288       info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
289       info.isFunction = false;
290       info.symbol = clgv;
291     }
292   }
293
294   // third slot in __OBJC,__class is pointer to class name
295   std::string className;
296   if (objcClassNameFromExpression(c->getOperand(2), className)) {
297     auto Iter = _defines.insert(className).first;
298
299     NameAndAttributes info;
300     info.name = Iter->first();
301     info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
302       LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
303     info.isFunction = false;
304     info.symbol = clgv;
305     _symbols.push_back(info);
306   }
307 }
308
309 /// addObjCCategory - Parse i386/ppc ObjC category data structure.
310 void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
311   const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
312   if (!c) return;
313
314   // second slot in __OBJC,__category is pointer to target class name
315   std::string targetclassName;
316   if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
317     return;
318
319   auto IterBool =
320       _undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));
321
322   if (!IterBool.second)
323     return;
324
325   NameAndAttributes &info = IterBool.first->second;
326   info.name = IterBool.first->first();
327   info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
328   info.isFunction = false;
329   info.symbol = clgv;
330 }
331
332 /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
333 void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
334   std::string targetclassName;
335   if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
336     return;
337
338   auto IterBool =
339       _undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));
340
341   if (!IterBool.second)
342     return;
343
344   NameAndAttributes &info = IterBool.first->second;
345   info.name = IterBool.first->first();
346   info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
347   info.isFunction = false;
348   info.symbol = clgv;
349 }
350
351 void LTOModule::addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym) {
352   SmallString<64> Buffer;
353   {
354     raw_svector_ostream OS(Buffer);
355     SymTab.printSymbolName(OS, Sym);
356     Buffer.c_str();
357   }
358
359   const GlobalValue *V = Sym.get<GlobalValue *>();
360   addDefinedDataSymbol(Buffer, V);
361 }
362
363 void LTOModule::addDefinedDataSymbol(StringRef Name, const GlobalValue *v) {
364   // Add to list of defined symbols.
365   addDefinedSymbol(Name, v, false);
366
367   if (!v->hasSection() /* || !isTargetDarwin */)
368     return;
369
370   // Special case i386/ppc ObjC data structures in magic sections:
371   // The issue is that the old ObjC object format did some strange
372   // contortions to avoid real linker symbols.  For instance, the
373   // ObjC class data structure is allocated statically in the executable
374   // that defines that class.  That data structures contains a pointer to
375   // its superclass.  But instead of just initializing that part of the
376   // struct to the address of its superclass, and letting the static and
377   // dynamic linkers do the rest, the runtime works by having that field
378   // instead point to a C-string that is the name of the superclass.
379   // At runtime the objc initialization updates that pointer and sets
380   // it to point to the actual super class.  As far as the linker
381   // knows it is just a pointer to a string.  But then someone wanted the
382   // linker to issue errors at build time if the superclass was not found.
383   // So they figured out a way in mach-o object format to use an absolute
384   // symbols (.objc_class_name_Foo = 0) and a floating reference
385   // (.reference .objc_class_name_Bar) to cause the linker into erroring when
386   // a class was missing.
387   // The following synthesizes the implicit .objc_* symbols for the linker
388   // from the ObjC data structures generated by the front end.
389
390   // special case if this data blob is an ObjC class definition
391   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(v)) {
392     StringRef Section = GV->getSection();
393     if (Section.startswith("__OBJC,__class,")) {
394       addObjCClass(GV);
395     }
396
397     // special case if this data blob is an ObjC category definition
398     else if (Section.startswith("__OBJC,__category,")) {
399       addObjCCategory(GV);
400     }
401
402     // special case if this data blob is the list of referenced classes
403     else if (Section.startswith("__OBJC,__cls_refs,")) {
404       addObjCClassRef(GV);
405     }
406   }
407 }
408
409 void LTOModule::addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym) {
410   SmallString<64> Buffer;
411   {
412     raw_svector_ostream OS(Buffer);
413     SymTab.printSymbolName(OS, Sym);
414     Buffer.c_str();
415   }
416
417   const Function *F = cast<Function>(Sym.get<GlobalValue *>());
418   addDefinedFunctionSymbol(Buffer, F);
419 }
420
421 void LTOModule::addDefinedFunctionSymbol(StringRef Name, const Function *F) {
422   // add to list of defined symbols
423   addDefinedSymbol(Name, F, true);
424 }
425
426 void LTOModule::addDefinedSymbol(StringRef Name, const GlobalValue *def,
427                                  bool isFunction) {
428   // set alignment part log2() can have rounding errors
429   uint32_t align = def->getAlignment();
430   uint32_t attr = align ? countTrailingZeros(align) : 0;
431
432   // set permissions part
433   if (isFunction) {
434     attr |= LTO_SYMBOL_PERMISSIONS_CODE;
435   } else {
436     const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
437     if (gv && gv->isConstant())
438       attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
439     else
440       attr |= LTO_SYMBOL_PERMISSIONS_DATA;
441   }
442
443   // set definition part
444   if (def->hasWeakLinkage() || def->hasLinkOnceLinkage())
445     attr |= LTO_SYMBOL_DEFINITION_WEAK;
446   else if (def->hasCommonLinkage())
447     attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
448   else
449     attr |= LTO_SYMBOL_DEFINITION_REGULAR;
450
451   // set scope part
452   if (def->hasLocalLinkage())
453     // Ignore visibility if linkage is local.
454     attr |= LTO_SYMBOL_SCOPE_INTERNAL;
455   else if (def->hasHiddenVisibility())
456     attr |= LTO_SYMBOL_SCOPE_HIDDEN;
457   else if (def->hasProtectedVisibility())
458     attr |= LTO_SYMBOL_SCOPE_PROTECTED;
459   else if (canBeOmittedFromSymbolTable(def))
460     attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
461   else
462     attr |= LTO_SYMBOL_SCOPE_DEFAULT;
463
464   if (def->hasComdat())
465     attr |= LTO_SYMBOL_COMDAT;
466
467   if (isa<GlobalAlias>(def))
468     attr |= LTO_SYMBOL_ALIAS;
469
470   auto Iter = _defines.insert(Name).first;
471
472   // fill information structure
473   NameAndAttributes info;
474   StringRef NameRef = Iter->first();
475   info.name = NameRef;
476   assert(NameRef.data()[NameRef.size()] == '\0');
477   info.attributes = attr;
478   info.isFunction = isFunction;
479   info.symbol = def;
480
481   // add to table of symbols
482   _symbols.push_back(info);
483 }
484
485 /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
486 /// defined list.
487 void LTOModule::addAsmGlobalSymbol(StringRef name,
488                                    lto_symbol_attributes scope) {
489   auto IterBool = _defines.insert(name);
490
491   // only add new define if not already defined
492   if (!IterBool.second)
493     return;
494
495   NameAndAttributes &info = _undefines[IterBool.first->first()];
496
497   if (info.symbol == nullptr) {
498     // FIXME: This is trying to take care of module ASM like this:
499     //
500     //   module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
501     //
502     // but is gross and its mother dresses it funny. Have the ASM parser give us
503     // more details for this type of situation so that we're not guessing so
504     // much.
505
506     // fill information structure
507     info.name = IterBool.first->first();
508     info.attributes =
509       LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
510     info.isFunction = false;
511     info.symbol = nullptr;
512
513     // add to table of symbols
514     _symbols.push_back(info);
515     return;
516   }
517
518   if (info.isFunction)
519     addDefinedFunctionSymbol(info.name, cast<Function>(info.symbol));
520   else
521     addDefinedDataSymbol(info.name, info.symbol);
522
523   _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
524   _symbols.back().attributes |= scope;
525 }
526
527 /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
528 /// undefined list.
529 void LTOModule::addAsmGlobalSymbolUndef(StringRef name) {
530   auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
531
532   _asm_undefines.push_back(IterBool.first->first());
533
534   // we already have the symbol
535   if (!IterBool.second)
536     return;
537
538   uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;
539   attr |= LTO_SYMBOL_SCOPE_DEFAULT;
540   NameAndAttributes &info = IterBool.first->second;
541   info.name = IterBool.first->first();
542   info.attributes = attr;
543   info.isFunction = false;
544   info.symbol = nullptr;
545 }
546
547 /// Add a symbol which isn't defined just yet to a list to be resolved later.
548 void LTOModule::addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
549                                             bool isFunc) {
550   SmallString<64> name;
551   {
552     raw_svector_ostream OS(name);
553     SymTab.printSymbolName(OS, Sym);
554     name.c_str();
555   }
556
557   auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
558
559   // we already have the symbol
560   if (!IterBool.second)
561     return;
562
563   NameAndAttributes &info = IterBool.first->second;
564
565   info.name = IterBool.first->first();
566
567   const GlobalValue *decl = Sym.dyn_cast<GlobalValue *>();
568
569   if (decl->hasExternalWeakLinkage())
570     info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
571   else
572     info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
573
574   info.isFunction = isFunc;
575   info.symbol = decl;
576 }
577
578 void LTOModule::parseSymbols() {
579   for (auto Sym : SymTab.symbols()) {
580     auto *GV = Sym.dyn_cast<GlobalValue *>();
581     uint32_t Flags = SymTab.getSymbolFlags(Sym);
582     if (Flags & object::BasicSymbolRef::SF_FormatSpecific)
583       continue;
584
585     bool IsUndefined = Flags & object::BasicSymbolRef::SF_Undefined;
586
587     if (!GV) {
588       SmallString<64> Buffer;
589       {
590         raw_svector_ostream OS(Buffer);
591         SymTab.printSymbolName(OS, Sym);
592         Buffer.c_str();
593       }
594       StringRef Name(Buffer);
595
596       if (IsUndefined)
597         addAsmGlobalSymbolUndef(Name);
598       else if (Flags & object::BasicSymbolRef::SF_Global)
599         addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_DEFAULT);
600       else
601         addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_INTERNAL);
602       continue;
603     }
604
605     auto *F = dyn_cast<Function>(GV);
606     if (IsUndefined) {
607       addPotentialUndefinedSymbol(Sym, F != nullptr);
608       continue;
609     }
610
611     if (F) {
612       addDefinedFunctionSymbol(Sym);
613       continue;
614     }
615
616     if (isa<GlobalVariable>(GV)) {
617       addDefinedDataSymbol(Sym);
618       continue;
619     }
620
621     assert(isa<GlobalAlias>(GV));
622     addDefinedDataSymbol(Sym);
623   }
624
625   // make symbols for all undefines
626   for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
627          e = _undefines.end(); u != e; ++u) {
628     // If this symbol also has a definition, then don't make an undefine because
629     // it is a tentative definition.
630     if (_defines.count(u->getKey())) continue;
631     NameAndAttributes info = u->getValue();
632     _symbols.push_back(info);
633   }
634 }
635
636 /// parseMetadata - Parse metadata from the module
637 void LTOModule::parseMetadata() {
638   raw_string_ostream OS(LinkerOpts);
639
640   // Linker Options
641   if (NamedMDNode *LinkerOptions =
642           getModule().getNamedMetadata("llvm.linker.options")) {
643     for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
644       MDNode *MDOptions = LinkerOptions->getOperand(i);
645       for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
646         MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
647         OS << " " << MDOption->getString();
648       }
649     }
650   }
651
652   // Globals - we only need to do this for COFF.
653   const Triple TT(_target->getTargetTriple());
654   if (!TT.isOSBinFormatCOFF())
655     return;
656   Mangler M;
657   for (const NameAndAttributes &Sym : _symbols) {
658     if (!Sym.symbol)
659       continue;
660     emitLinkerFlagsForGlobalCOFF(OS, Sym.symbol, TT, M);
661   }
662
663   // Add other interesting metadata here.
664 }