8ea680d53c53c7e5c4e9f9aa5350c4292759f95a
[oota-llvm.git] / tools / 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 "LTOModule.h"
16
17 #include "llvm/Constants.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Bitcode/ReaderWriter.h"
23 #include "llvm/Support/SystemUtils.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/Host.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/Process.h"
29 #include "llvm/Support/SourceMgr.h"
30 #include "llvm/Support/TargetRegistry.h"
31 #include "llvm/Support/TargetSelect.h"
32 #include "llvm/Support/system_error.h"
33 #include "llvm/MC/MCAsmInfo.h"
34 #include "llvm/MC/MCExpr.h"
35 #include "llvm/MC/MCInst.h"
36 #include "llvm/MC/MCParser/MCAsmParser.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSubtargetInfo.h"
39 #include "llvm/MC/MCSymbol.h"
40 #include "llvm/MC/SubtargetFeature.h"
41 #include "llvm/MC/MCTargetAsmParser.h"
42 #include "llvm/Target/TargetMachine.h"
43 #include "llvm/Target/TargetRegisterInfo.h"
44
45 using namespace llvm;
46
47 bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
48   return llvm::sys::IdentifyFileType((char*)mem, length)
49     == llvm::sys::Bitcode_FileType;
50 }
51
52 bool LTOModule::isBitcodeFile(const char *path) {
53   return llvm::sys::Path(path).isBitcodeFile();
54 }
55
56 bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
57                                        const char *triplePrefix) {
58   MemoryBuffer *buffer = makeBuffer(mem, length);
59   if (!buffer)
60     return false;
61   return isTargetMatch(buffer, triplePrefix);
62 }
63
64 bool LTOModule::isBitcodeFileForTarget(const char *path,
65                                        const char *triplePrefix) {
66   OwningPtr<MemoryBuffer> buffer;
67   if (MemoryBuffer::getFile(path, buffer))
68     return false;
69   return isTargetMatch(buffer.take(), triplePrefix);
70 }
71
72 // Takes ownership of buffer.
73 bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
74   std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
75   delete buffer;
76   return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
77 }
78
79
80 LTOModule::LTOModule(Module *m, TargetMachine *t)
81   : _module(m), _target(t),
82     _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL),
83     _mangler(_context, *_target->getTargetData())
84 {
85 }
86
87 LTOModule *LTOModule::makeLTOModule(const char *path,
88                                     std::string &errMsg) {
89   OwningPtr<MemoryBuffer> buffer;
90   if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
91     errMsg = ec.message();
92     return NULL;
93   }
94   return makeLTOModule(buffer.take(), errMsg);
95 }
96
97 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
98                                     size_t size, std::string &errMsg) {
99   return makeLTOModule(fd, path, size, size, 0, errMsg);
100 }
101
102 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
103                                     size_t file_size,
104                                     size_t map_size,
105                                     off_t offset,
106                                     std::string &errMsg) {
107   OwningPtr<MemoryBuffer> buffer;
108   if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size,
109                                                 map_size, offset, false)) {
110     errMsg = ec.message();
111     return NULL;
112   }
113   return makeLTOModule(buffer.take(), errMsg);
114 }
115
116 /// makeBuffer - Create a MemoryBuffer from a memory range.
117 MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
118   const char *startPtr = (char*)mem;
119   return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
120 }
121
122 LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
123                                     std::string &errMsg) {
124   OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
125   if (!buffer)
126     return NULL;
127   return makeLTOModule(buffer.take(), errMsg);
128 }
129
130 LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
131                                     std::string &errMsg) {
132   static bool Initialized = false;
133   if (!Initialized) {
134     InitializeAllTargets();
135     InitializeAllTargetMCs();
136     InitializeAllAsmParsers();
137     Initialized = true;
138   }
139
140   // parse bitcode buffer
141   OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
142                                            &errMsg));
143   if (!m) {
144     delete buffer;
145     return NULL;
146   }
147
148   std::string Triple = m->getTargetTriple();
149   if (Triple.empty())
150     Triple = sys::getDefaultTargetTriple();
151
152   // find machine architecture for this module
153   const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
154   if (!march)
155     return NULL;
156
157   // construct LTOModule, hand over ownership of module and target
158   SubtargetFeatures Features;
159   Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
160   std::string FeatureStr = Features.getString();
161   std::string CPU;
162   TargetMachine *target = march->createTargetMachine(Triple, CPU, FeatureStr);
163   LTOModule *Ret = new LTOModule(m.take(), target);
164   if (Ret->ParseSymbols(errMsg)) {
165     delete Ret;
166     return NULL;
167   }
168
169   return Ret;
170 }
171
172 const char *LTOModule::getTargetTriple() {
173   return _module->getTargetTriple().c_str();
174 }
175
176 void LTOModule::setTargetTriple(const char *triple) {
177   _module->setTargetTriple(triple);
178 }
179
180 void LTOModule::addDefinedFunctionSymbol(Function *f) {
181   // add to list of defined symbols
182   addDefinedSymbol(f, true);
183 }
184
185 // Get string that data pointer points to.
186 bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) {
187   if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
188     Constant *op = ce->getOperand(0);
189     if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
190       Constant *cn = gvn->getInitializer();
191       if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) {
192         if (ca->isCString()) {
193           name = ".objc_class_name_" + ca->getAsCString();
194           return true;
195         }
196       }
197     }
198   }
199   return false;
200 }
201
202 // Parse i386/ppc ObjC class data structure.
203 void LTOModule::addObjCClass(GlobalVariable *clgv) {
204   ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
205   if (!c) return;
206
207   // second slot in __OBJC,__class is pointer to superclass name
208   std::string superclassName;
209   if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
210     NameAndAttributes info;
211     StringMap<NameAndAttributes>::value_type &entry =
212       _undefines.GetOrCreateValue(superclassName);
213     if (!entry.getValue().name) {
214       const char *symbolName = entry.getKey().data();
215       info.name = symbolName;
216       info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
217       entry.setValue(info);
218     }
219   }
220
221   // third slot in __OBJC,__class is pointer to class name
222   std::string className;
223   if (objcClassNameFromExpression(c->getOperand(2), className)) {
224     StringSet::value_type &entry = _defines.GetOrCreateValue(className);
225     entry.setValue(1);
226     NameAndAttributes info;
227     info.name = entry.getKey().data();
228     info.attributes = lto_symbol_attributes(LTO_SYMBOL_PERMISSIONS_DATA |
229                                             LTO_SYMBOL_DEFINITION_REGULAR |
230                                             LTO_SYMBOL_SCOPE_DEFAULT);
231     _symbols.push_back(info);
232   }
233 }
234
235
236 // Parse i386/ppc ObjC category data structure.
237 void LTOModule::addObjCCategory(GlobalVariable *clgv) {
238   ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
239   if (!c) return;
240
241   // second slot in __OBJC,__category is pointer to target class name
242   std::string targetclassName;
243   if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
244     return;
245
246   NameAndAttributes info;
247   StringMap<NameAndAttributes>::value_type &entry =
248     _undefines.GetOrCreateValue(targetclassName);
249
250   if (entry.getValue().name)
251     return;
252
253   const char *symbolName = entry.getKey().data();
254   info.name = symbolName;
255   info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
256   entry.setValue(info);
257 }
258
259
260 // Parse i386/ppc ObjC class list data structure.
261 void LTOModule::addObjCClassRef(GlobalVariable *clgv) {
262   std::string targetclassName;
263   if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
264     return;
265
266   NameAndAttributes info;
267   StringMap<NameAndAttributes>::value_type &entry =
268     _undefines.GetOrCreateValue(targetclassName);
269   if (entry.getValue().name)
270     return;
271
272   const char *symbolName = entry.getKey().data();
273   info.name = symbolName;
274   info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
275   entry.setValue(info);
276 }
277
278
279 void LTOModule::addDefinedDataSymbol(GlobalValue *v) {
280   // Add to list of defined symbols.
281   addDefinedSymbol(v, false);
282
283   // Special case i386/ppc ObjC data structures in magic sections:
284   // The issue is that the old ObjC object format did some strange
285   // contortions to avoid real linker symbols.  For instance, the
286   // ObjC class data structure is allocated statically in the executable
287   // that defines that class.  That data structures contains a pointer to
288   // its superclass.  But instead of just initializing that part of the
289   // struct to the address of its superclass, and letting the static and
290   // dynamic linkers do the rest, the runtime works by having that field
291   // instead point to a C-string that is the name of the superclass.
292   // At runtime the objc initialization updates that pointer and sets
293   // it to point to the actual super class.  As far as the linker
294   // knows it is just a pointer to a string.  But then someone wanted the
295   // linker to issue errors at build time if the superclass was not found.
296   // So they figured out a way in mach-o object format to use an absolute
297   // symbols (.objc_class_name_Foo = 0) and a floating reference
298   // (.reference .objc_class_name_Bar) to cause the linker into erroring when
299   // a class was missing.
300   // The following synthesizes the implicit .objc_* symbols for the linker
301   // from the ObjC data structures generated by the front end.
302   if (v->hasSection() /* && isTargetDarwin */) {
303     // special case if this data blob is an ObjC class definition
304     if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
305       if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
306         addObjCClass(gv);
307       }
308     }
309
310     // special case if this data blob is an ObjC category definition
311     else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
312       if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
313         addObjCCategory(gv);
314       }
315     }
316
317     // special case if this data blob is the list of referenced classes
318     else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
319       if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
320         addObjCClassRef(gv);
321       }
322     }
323   }
324 }
325
326 void LTOModule::addDefinedSymbol(GlobalValue *def, bool isFunction) {
327   // ignore all llvm.* symbols
328   if (def->getName().startswith("llvm."))
329     return;
330
331   // string is owned by _defines
332   SmallString<64> Buffer;
333   _mangler.getNameWithPrefix(Buffer, def, false);
334
335   // set alignment part log2() can have rounding errors
336   uint32_t align = def->getAlignment();
337   uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
338
339   // set permissions part
340   if (isFunction)
341     attr |= LTO_SYMBOL_PERMISSIONS_CODE;
342   else {
343     GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
344     if (gv && gv->isConstant())
345       attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
346     else
347       attr |= LTO_SYMBOL_PERMISSIONS_DATA;
348   }
349
350   // set definition part
351   if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
352       def->hasLinkerPrivateWeakLinkage() ||
353       def->hasLinkerPrivateWeakDefAutoLinkage())
354     attr |= LTO_SYMBOL_DEFINITION_WEAK;
355   else if (def->hasCommonLinkage())
356     attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
357   else
358     attr |= LTO_SYMBOL_DEFINITION_REGULAR;
359
360   // set scope part
361   if (def->hasHiddenVisibility())
362     attr |= LTO_SYMBOL_SCOPE_HIDDEN;
363   else if (def->hasProtectedVisibility())
364     attr |= LTO_SYMBOL_SCOPE_PROTECTED;
365   else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
366            def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
367            def->hasLinkerPrivateWeakLinkage())
368     attr |= LTO_SYMBOL_SCOPE_DEFAULT;
369   else if (def->hasLinkerPrivateWeakDefAutoLinkage())
370     attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
371   else
372     attr |= LTO_SYMBOL_SCOPE_INTERNAL;
373
374   // add to table of symbols
375   NameAndAttributes info;
376   StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
377   entry.setValue(1);
378
379   StringRef Name = entry.getKey();
380   info.name = Name.data();
381   assert(info.name[Name.size()] == '\0');
382   info.attributes = (lto_symbol_attributes)attr;
383   _symbols.push_back(info);
384 }
385
386 void LTOModule::addAsmGlobalSymbol(const char *name,
387                                    lto_symbol_attributes scope) {
388   StringSet::value_type &entry = _defines.GetOrCreateValue(name);
389
390   // only add new define if not already defined
391   if (entry.getValue())
392     return;
393
394   entry.setValue(1);
395   const char *symbolName = entry.getKey().data();
396   uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
397   attr |= scope;
398   NameAndAttributes info;
399   info.name = symbolName;
400   info.attributes = (lto_symbol_attributes)attr;
401   _symbols.push_back(info);
402 }
403
404 void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
405   StringMap<NameAndAttributes>::value_type &entry =
406     _undefines.GetOrCreateValue(name);
407
408   _asm_undefines.push_back(entry.getKey().data());
409
410   // we already have the symbol
411   if (entry.getValue().name)
412     return;
413
414   uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
415   attr |= LTO_SYMBOL_SCOPE_DEFAULT;
416   NameAndAttributes info;
417   info.name = entry.getKey().data();
418   info.attributes = (lto_symbol_attributes)attr;
419
420   entry.setValue(info);
421 }
422
423 void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl) {
424   // ignore all llvm.* symbols
425   if (decl->getName().startswith("llvm."))
426     return;
427
428   // ignore all aliases
429   if (isa<GlobalAlias>(decl))
430     return;
431
432   SmallString<64> name;
433   _mangler.getNameWithPrefix(name, decl, false);
434
435   StringMap<NameAndAttributes>::value_type &entry =
436     _undefines.GetOrCreateValue(name);
437
438   // we already have the symbol
439   if (entry.getValue().name)
440     return;
441
442   NameAndAttributes info;
443
444   info.name = entry.getKey().data();
445   if (decl->hasExternalWeakLinkage())
446     info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
447   else
448     info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
449
450   entry.setValue(info);
451 }
452
453 namespace {
454   class RecordStreamer : public MCStreamer {
455   public:
456     enum State { NeverSeen, Global, Defined, DefinedGlobal, Used};
457
458   private:
459     StringMap<State> Symbols;
460
461     void markDefined(const MCSymbol &Symbol) {
462       State &S = Symbols[Symbol.getName()];
463       switch (S) {
464       case DefinedGlobal:
465       case Global:
466         S = DefinedGlobal;
467         break;
468       case NeverSeen:
469       case Defined:
470       case Used:
471         S = Defined;
472         break;
473       }
474     }
475     void markGlobal(const MCSymbol &Symbol) {
476       State &S = Symbols[Symbol.getName()];
477       switch (S) {
478       case DefinedGlobal:
479       case Defined:
480         S = DefinedGlobal;
481         break;
482
483       case NeverSeen:
484       case Global:
485       case Used:
486         S = Global;
487         break;
488       }
489     }
490     void markUsed(const MCSymbol &Symbol) {
491       State &S = Symbols[Symbol.getName()];
492       switch (S) {
493       case DefinedGlobal:
494       case Defined:
495       case Global:
496         break;
497
498       case NeverSeen:
499       case Used:
500         S = Used;
501         break;
502       }
503     }
504
505     // FIXME: mostly copied for the obj streamer.
506     void AddValueSymbols(const MCExpr *Value) {
507       switch (Value->getKind()) {
508       case MCExpr::Target:
509         // FIXME: What should we do in here?
510         break;
511
512       case MCExpr::Constant:
513         break;
514
515       case MCExpr::Binary: {
516         const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
517         AddValueSymbols(BE->getLHS());
518         AddValueSymbols(BE->getRHS());
519         break;
520       }
521
522       case MCExpr::SymbolRef:
523         markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
524         break;
525
526       case MCExpr::Unary:
527         AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
528         break;
529       }
530     }
531
532   public:
533     typedef StringMap<State>::const_iterator const_iterator;
534
535     const_iterator begin() {
536       return Symbols.begin();
537     }
538
539     const_iterator end() {
540       return Symbols.end();
541     }
542
543     RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
544
545     virtual void ChangeSection(const MCSection *Section) {}
546     virtual void InitSections() {}
547     virtual void EmitLabel(MCSymbol *Symbol) {
548       Symbol->setSection(*getCurrentSection());
549       markDefined(*Symbol);
550     }
551     virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
552     virtual void EmitThumbFunc(MCSymbol *Func) {}
553     virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
554       // FIXME: should we handle aliases?
555       markDefined(*Symbol);
556     }
557     virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
558       if (Attribute == MCSA_Global)
559         markGlobal(*Symbol);
560     }
561     virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
562     virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
563     virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
564     virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
565     virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
566                               unsigned Size , unsigned ByteAlignment) {
567       markDefined(*Symbol);
568     }
569     virtual void EmitCOFFSymbolType(int Type) {}
570     virtual void EndCOFFSymbolDef() {}
571     virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
572                                   unsigned ByteAlignment) {
573       markDefined(*Symbol);
574     }
575     virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
576     virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
577                                        unsigned ByteAlignment) {}
578     virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
579                                 uint64_t Size, unsigned ByteAlignment) {}
580     virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {}
581     virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
582                                unsigned AddrSpace) {}
583     virtual void EmitULEB128Value(const MCExpr *Value) {}
584     virtual void EmitSLEB128Value(const MCExpr *Value) {}
585     virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
586                                       unsigned ValueSize,
587                                       unsigned MaxBytesToEmit) {}
588     virtual void EmitCodeAlignment(unsigned ByteAlignment,
589                                    unsigned MaxBytesToEmit) {}
590     virtual void EmitValueToOffset(const MCExpr *Offset,
591                                    unsigned char Value ) {}
592     virtual void EmitFileDirective(StringRef Filename) {}
593     virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
594                                           const MCSymbol *LastLabel,
595                                           const MCSymbol *Label,
596                                           unsigned PointerSize) {}
597
598     virtual void EmitInstruction(const MCInst &Inst) {
599       // Scan for values.
600       for (unsigned i = Inst.getNumOperands(); i--; )
601         if (Inst.getOperand(i).isExpr())
602           AddValueSymbols(Inst.getOperand(i).getExpr());
603     }
604     virtual void Finish() {}
605   };
606 }
607
608 bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
609   const std::string &inlineAsm = _module->getModuleInlineAsm();
610   if (inlineAsm.empty())
611     return false;
612
613   OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
614   MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
615   SourceMgr SrcMgr;
616   SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
617   OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
618                                                   _context, *Streamer,
619                                                   *_target->getMCAsmInfo()));
620   OwningPtr<MCSubtargetInfo> STI(_target->getTarget().
621                       createMCSubtargetInfo(_target->getTargetTriple(),
622                                             _target->getTargetCPU(),
623                                             _target->getTargetFeatureString()));
624   OwningPtr<MCTargetAsmParser>
625     TAP(_target->getTarget().createMCAsmParser(*STI, *Parser.get()));
626   if (!TAP) {
627     errMsg = "target " + std::string(_target->getTarget().getName()) +
628         " does not define AsmParser.";
629     return true;
630   }
631
632   Parser->setTargetParser(*TAP);
633   int Res = Parser->Run(false);
634   if (Res)
635     return true;
636
637   for (RecordStreamer::const_iterator i = Streamer->begin(),
638          e = Streamer->end(); i != e; ++i) {
639     StringRef Key = i->first();
640     RecordStreamer::State Value = i->second;
641     if (Value == RecordStreamer::DefinedGlobal)
642       addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
643     else if (Value == RecordStreamer::Defined)
644       addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
645     else if (Value == RecordStreamer::Global ||
646              Value == RecordStreamer::Used)
647       addAsmGlobalSymbolUndef(Key.data());
648   }
649   return false;
650 }
651
652 static bool isDeclaration(const GlobalValue &V) {
653   if (V.hasAvailableExternallyLinkage())
654     return true;
655   if (V.isMaterializable())
656     return false;
657   return V.isDeclaration();
658 }
659
660 static bool isAliasToDeclaration(const GlobalAlias &V) {
661   return isDeclaration(*V.getAliasedGlobal());
662 }
663
664 bool LTOModule::ParseSymbols(std::string &errMsg) {
665   // add functions
666   for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
667     if (isDeclaration(*f))
668       addPotentialUndefinedSymbol(f);
669     else
670       addDefinedFunctionSymbol(f);
671   }
672
673   // add data
674   for (Module::global_iterator v = _module->global_begin(),
675          e = _module->global_end(); v !=  e; ++v) {
676     if (isDeclaration(*v))
677       addPotentialUndefinedSymbol(v);
678     else
679       addDefinedDataSymbol(v);
680   }
681
682   // add asm globals
683   if (addAsmGlobalSymbols(errMsg))
684     return true;
685
686   // add aliases
687   for (Module::alias_iterator i = _module->alias_begin(),
688          e = _module->alias_end(); i != e; ++i) {
689     if (isAliasToDeclaration(*i))
690       addPotentialUndefinedSymbol(i);
691     else
692       addDefinedDataSymbol(i);
693   }
694
695   // make symbols for all undefines
696   for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
697        it != _undefines.end(); ++it) {
698     // if this symbol also has a definition, then don't make an undefine
699     // because it is a tentative definition
700     if (_defines.count(it->getKey()) == 0) {
701       NameAndAttributes info = it->getValue();
702       _symbols.push_back(info);
703     }
704   }
705   return false;
706 }
707
708 uint32_t LTOModule::getSymbolCount() {
709   return _symbols.size();
710 }
711
712 lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) {
713   if (index < _symbols.size())
714     return _symbols[index].attributes;
715   else
716     return lto_symbol_attributes(0);
717 }
718
719 const char *LTOModule::getSymbolName(uint32_t index) {
720   if (index < _symbols.size())
721     return _symbols[index].name;
722   else
723     return NULL;
724 }