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