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