1 //===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "LTOModule.h"
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/Target/Mangler.h"
34 #include "llvm/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCContext.h"
36 #include "llvm/MC/MCExpr.h"
37 #include "llvm/MC/MCInst.h"
38 #include "llvm/MC/MCParser/MCAsmParser.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSubtargetInfo.h"
41 #include "llvm/MC/MCSymbol.h"
42 #include "llvm/MC/SubtargetFeature.h"
43 #include "llvm/MC/MCTargetAsmParser.h"
44 #include "llvm/Target/TargetMachine.h"
45 #include "llvm/Target/TargetRegisterInfo.h"
49 bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
50 return llvm::sys::IdentifyFileType((char*)mem, length)
51 == llvm::sys::Bitcode_FileType;
54 bool LTOModule::isBitcodeFile(const char *path) {
55 return llvm::sys::Path(path).isBitcodeFile();
58 bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
59 const char *triplePrefix) {
60 MemoryBuffer *buffer = makeBuffer(mem, length);
63 return isTargetMatch(buffer, triplePrefix);
67 bool LTOModule::isBitcodeFileForTarget(const char *path,
68 const char *triplePrefix) {
69 OwningPtr<MemoryBuffer> buffer;
70 if (MemoryBuffer::getFile(path, buffer))
72 return isTargetMatch(buffer.take(), triplePrefix);
75 // Takes ownership of buffer.
76 bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
77 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
79 return (strncmp(Triple.c_str(), triplePrefix,
80 strlen(triplePrefix)) == 0);
84 LTOModule::LTOModule(Module *m, TargetMachine *t)
85 : _module(m), _target(t)
89 LTOModule *LTOModule::makeLTOModule(const char *path,
90 std::string &errMsg) {
91 OwningPtr<MemoryBuffer> buffer;
92 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
93 errMsg = ec.message();
96 return makeLTOModule(buffer.take(), errMsg);
99 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
101 std::string &errMsg) {
102 return makeLTOModule(fd, path, size, size, 0, errMsg);
105 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
109 std::string &errMsg) {
110 OwningPtr<MemoryBuffer> buffer;
111 if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size,
112 map_size, offset, false)) {
113 errMsg = ec.message();
116 return makeLTOModule(buffer.take(), errMsg);
119 /// makeBuffer - Create a MemoryBuffer from a memory range.
120 MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
121 const char *startPtr = (char*)mem;
122 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
126 LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
127 std::string &errMsg) {
128 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
131 return makeLTOModule(buffer.take(), errMsg);
134 LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
135 std::string &errMsg) {
136 static bool Initialized = false;
138 InitializeAllTargets();
139 InitializeAllTargetMCs();
140 InitializeAllAsmParsers();
144 // parse bitcode buffer
145 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
152 std::string Triple = m->getTargetTriple();
154 Triple = sys::getHostTriple();
156 // find machine architecture for this module
157 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
161 // construct LTOModule, hand over ownership of module and target
162 SubtargetFeatures Features;
163 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
164 std::string FeatureStr = Features.getString();
166 TargetMachine *target = march->createTargetMachine(Triple, CPU, FeatureStr);
167 LTOModule *Ret = new LTOModule(m.take(), target);
168 bool Err = Ret->ParseSymbols(errMsg);
177 const char *LTOModule::getTargetTriple() {
178 return _module->getTargetTriple().c_str();
181 void LTOModule::setTargetTriple(const char *triple) {
182 _module->setTargetTriple(triple);
185 void LTOModule::addDefinedFunctionSymbol(Function *f, Mangler &mangler) {
186 // add to list of defined symbols
187 addDefinedSymbol(f, mangler, true);
190 // Get string that data pointer points to.
191 bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) {
192 if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
193 Constant *op = ce->getOperand(0);
194 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
195 Constant *cn = gvn->getInitializer();
196 if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) {
197 if (ca->isCString()) {
198 name = ".objc_class_name_" + ca->getAsCString();
207 // Parse i386/ppc ObjC class data structure.
208 void LTOModule::addObjCClass(GlobalVariable *clgv) {
209 if (ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
210 // second slot in __OBJC,__class is pointer to superclass name
211 std::string superclassName;
212 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
213 NameAndAttributes info;
214 StringMap<NameAndAttributes>::value_type &entry =
215 _undefines.GetOrCreateValue(superclassName);
216 if (!entry.getValue().name) {
217 const char *symbolName = entry.getKey().data();
218 info.name = symbolName;
219 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
220 entry.setValue(info);
223 // third slot in __OBJC,__class is pointer to class name
224 std::string className;
225 if (objcClassNameFromExpression(c->getOperand(2), className)) {
226 StringSet::value_type &entry =
227 _defines.GetOrCreateValue(className);
229 NameAndAttributes info;
230 info.name = entry.getKey().data();
231 info.attributes = (lto_symbol_attributes)
232 (LTO_SYMBOL_PERMISSIONS_DATA |
233 LTO_SYMBOL_DEFINITION_REGULAR |
234 LTO_SYMBOL_SCOPE_DEFAULT);
235 _symbols.push_back(info);
241 // Parse i386/ppc ObjC category data structure.
242 void LTOModule::addObjCCategory(GlobalVariable *clgv) {
243 if (ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
244 // second slot in __OBJC,__category is pointer to target class name
245 std::string targetclassName;
246 if (objcClassNameFromExpression(c->getOperand(1), targetclassName)) {
247 NameAndAttributes info;
249 StringMap<NameAndAttributes>::value_type &entry =
250 _undefines.GetOrCreateValue(targetclassName);
252 if (entry.getValue().name)
255 const char *symbolName = entry.getKey().data();
256 info.name = symbolName;
257 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
258 entry.setValue(info);
264 // Parse i386/ppc ObjC class list data structure.
265 void LTOModule::addObjCClassRef(GlobalVariable *clgv) {
266 std::string targetclassName;
267 if (objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) {
268 NameAndAttributes info;
270 StringMap<NameAndAttributes>::value_type &entry =
271 _undefines.GetOrCreateValue(targetclassName);
272 if (entry.getValue().name)
275 const char *symbolName = entry.getKey().data();
276 info.name = symbolName;
277 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
278 entry.setValue(info);
283 void LTOModule::addDefinedDataSymbol(GlobalValue *v, Mangler &mangler) {
284 // Add to list of defined symbols.
285 addDefinedSymbol(v, mangler, false);
287 // Special case i386/ppc ObjC data structures in magic sections:
288 // The issue is that the old ObjC object format did some strange
289 // contortions to avoid real linker symbols. For instance, the
290 // ObjC class data structure is allocated statically in the executable
291 // that defines that class. That data structures contains a pointer to
292 // its superclass. But instead of just initializing that part of the
293 // struct to the address of its superclass, and letting the static and
294 // dynamic linkers do the rest, the runtime works by having that field
295 // instead point to a C-string that is the name of the superclass.
296 // At runtime the objc initialization updates that pointer and sets
297 // it to point to the actual super class. As far as the linker
298 // knows it is just a pointer to a string. But then someone wanted the
299 // linker to issue errors at build time if the superclass was not found.
300 // So they figured out a way in mach-o object format to use an absolute
301 // symbols (.objc_class_name_Foo = 0) and a floating reference
302 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
303 // a class was missing.
304 // The following synthesizes the implicit .objc_* symbols for the linker
305 // from the ObjC data structures generated by the front end.
306 if (v->hasSection() /* && isTargetDarwin */) {
307 // special case if this data blob is an ObjC class definition
308 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
309 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
314 // special case if this data blob is an ObjC category definition
315 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
316 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
321 // special case if this data blob is the list of referenced classes
322 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
323 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
331 void LTOModule::addDefinedSymbol(GlobalValue *def, Mangler &mangler,
333 // ignore all llvm.* symbols
334 if (def->getName().startswith("llvm."))
337 // string is owned by _defines
338 SmallString<64> Buffer;
339 mangler.getNameWithPrefix(Buffer, def, false);
341 // set alignment part log2() can have rounding errors
342 uint32_t align = def->getAlignment();
343 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
345 // set permissions part
347 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
349 GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
350 if (gv && gv->isConstant())
351 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
353 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
356 // set definition part
357 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
358 def->hasLinkerPrivateWeakLinkage() ||
359 def->hasLinkerPrivateWeakDefAutoLinkage())
360 attr |= LTO_SYMBOL_DEFINITION_WEAK;
361 else if (def->hasCommonLinkage())
362 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
364 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
367 if (def->hasHiddenVisibility())
368 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
369 else if (def->hasProtectedVisibility())
370 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
371 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
372 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
373 def->hasLinkerPrivateWeakLinkage())
374 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
375 else if (def->hasLinkerPrivateWeakDefAutoLinkage())
376 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
378 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
380 // add to table of symbols
381 NameAndAttributes info;
382 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
385 StringRef Name = entry.getKey();
386 info.name = Name.data();
387 assert(info.name[Name.size()] == '\0');
388 info.attributes = (lto_symbol_attributes)attr;
389 _symbols.push_back(info);
392 void LTOModule::addAsmGlobalSymbol(const char *name,
393 lto_symbol_attributes scope) {
394 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
396 // only add new define if not already defined
397 if (entry.getValue())
401 const char *symbolName = entry.getKey().data();
402 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
404 NameAndAttributes info;
405 info.name = symbolName;
406 info.attributes = (lto_symbol_attributes)attr;
407 _symbols.push_back(info);
410 void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
411 StringMap<NameAndAttributes>::value_type &entry =
412 _undefines.GetOrCreateValue(name);
414 _asm_undefines.push_back(entry.getKey().data());
416 // we already have the symbol
417 if (entry.getValue().name)
420 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
421 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
422 NameAndAttributes info;
423 info.name = entry.getKey().data();
424 info.attributes = (lto_symbol_attributes)attr;
426 entry.setValue(info);
429 void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl,
431 // ignore all llvm.* symbols
432 if (decl->getName().startswith("llvm."))
435 // ignore all aliases
436 if (isa<GlobalAlias>(decl))
439 SmallString<64> name;
440 mangler.getNameWithPrefix(name, decl, false);
442 StringMap<NameAndAttributes>::value_type &entry =
443 _undefines.GetOrCreateValue(name);
445 // we already have the symbol
446 if (entry.getValue().name)
449 NameAndAttributes info;
451 info.name = entry.getKey().data();
452 if (decl->hasExternalWeakLinkage())
453 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
455 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
457 entry.setValue(info);
462 class RecordStreamer : public MCStreamer {
464 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used};
467 StringMap<State> Symbols;
469 void markDefined(const MCSymbol &Symbol) {
470 State &S = Symbols[Symbol.getName()];
483 void markGlobal(const MCSymbol &Symbol) {
484 State &S = Symbols[Symbol.getName()];
498 void markUsed(const MCSymbol &Symbol) {
499 State &S = Symbols[Symbol.getName()];
513 // FIXME: mostly copied for the obj streamer.
514 void AddValueSymbols(const MCExpr *Value) {
515 switch (Value->getKind()) {
517 // FIXME: What should we do in here?
520 case MCExpr::Constant:
523 case MCExpr::Binary: {
524 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
525 AddValueSymbols(BE->getLHS());
526 AddValueSymbols(BE->getRHS());
530 case MCExpr::SymbolRef:
531 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
535 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
541 typedef StringMap<State>::const_iterator const_iterator;
543 const_iterator begin() {
544 return Symbols.begin();
547 const_iterator end() {
548 return Symbols.end();
551 RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
553 virtual void ChangeSection(const MCSection *Section) {}
554 virtual void InitSections() {}
555 virtual void EmitLabel(MCSymbol *Symbol) {
556 Symbol->setSection(*getCurrentSection());
557 markDefined(*Symbol);
559 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
560 virtual void EmitThumbFunc(MCSymbol *Func) {}
561 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
562 // FIXME: should we handle aliases?
563 markDefined(*Symbol);
565 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
566 if (Attribute == MCSA_Global)
569 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
570 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
571 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
572 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
573 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
574 unsigned Size , unsigned ByteAlignment) {
575 markDefined(*Symbol);
577 virtual void EmitCOFFSymbolType(int Type) {}
578 virtual void EndCOFFSymbolDef() {}
579 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
580 unsigned ByteAlignment) {
581 markDefined(*Symbol);
583 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
584 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
585 unsigned ByteAlignment) {}
586 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
587 uint64_t Size, unsigned ByteAlignment) {}
588 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {}
589 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
590 unsigned AddrSpace) {}
591 virtual void EmitULEB128Value(const MCExpr *Value) {}
592 virtual void EmitSLEB128Value(const MCExpr *Value) {}
593 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
595 unsigned MaxBytesToEmit) {}
596 virtual void EmitCodeAlignment(unsigned ByteAlignment,
597 unsigned MaxBytesToEmit) {}
598 virtual void EmitValueToOffset(const MCExpr *Offset,
599 unsigned char Value ) {}
600 virtual void EmitFileDirective(StringRef Filename) {}
601 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
602 const MCSymbol *LastLabel,
603 const MCSymbol *Label,
604 unsigned PointerSize) {}
606 virtual void EmitInstruction(const MCInst &Inst) {
608 for (unsigned i = Inst.getNumOperands(); i--; )
609 if (Inst.getOperand(i).isExpr())
610 AddValueSymbols(Inst.getOperand(i).getExpr());
612 virtual void Finish() {}
616 bool LTOModule::addAsmGlobalSymbols(MCContext &Context, std::string &errMsg) {
617 const std::string &inlineAsm = _module->getModuleInlineAsm();
619 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(Context));
620 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
622 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
623 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
625 *_target->getMCAsmInfo()));
626 OwningPtr<MCSubtargetInfo> STI(_target->getTarget().
627 createMCSubtargetInfo(_target->getTargetTriple(),
628 _target->getTargetCPU(),
629 _target->getTargetFeatureString()));
630 OwningPtr<MCTargetAsmParser>
631 TAP(_target->getTarget().createMCAsmParser(*STI, *Parser.get()));
633 errMsg = "target " + std::string(_target->getTarget().getName()) +
634 " does not define AsmParser.";
638 Parser->setTargetParser(*TAP);
639 int Res = Parser->Run(false);
643 for (RecordStreamer::const_iterator i = Streamer->begin(),
644 e = Streamer->end(); i != e; ++i) {
645 StringRef Key = i->first();
646 RecordStreamer::State Value = i->second;
647 if (Value == RecordStreamer::DefinedGlobal)
648 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
649 else if (Value == RecordStreamer::Defined)
650 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
651 else if (Value == RecordStreamer::Global ||
652 Value == RecordStreamer::Used)
653 addAsmGlobalSymbolUndef(Key.data());
658 static bool isDeclaration(const GlobalValue &V) {
659 if (V.hasAvailableExternallyLinkage())
661 if (V.isMaterializable())
663 return V.isDeclaration();
666 static bool isAliasToDeclaration(const GlobalAlias &V) {
667 return isDeclaration(*V.getAliasedGlobal());
670 bool LTOModule::ParseSymbols(std::string &errMsg) {
671 // Use mangler to add GlobalPrefix to names to match linker names.
672 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
673 Mangler mangler(Context, *_target->getTargetData());
676 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
677 if (isDeclaration(*f))
678 addPotentialUndefinedSymbol(f, mangler);
680 addDefinedFunctionSymbol(f, mangler);
684 for (Module::global_iterator v = _module->global_begin(),
685 e = _module->global_end(); v != e; ++v) {
686 if (isDeclaration(*v))
687 addPotentialUndefinedSymbol(v, mangler);
689 addDefinedDataSymbol(v, mangler);
693 if (addAsmGlobalSymbols(Context, errMsg))
697 for (Module::alias_iterator i = _module->alias_begin(),
698 e = _module->alias_end(); i != e; ++i) {
699 if (isAliasToDeclaration(*i))
700 addPotentialUndefinedSymbol(i, mangler);
702 addDefinedDataSymbol(i, mangler);
705 // make symbols for all undefines
706 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
707 it != _undefines.end(); ++it) {
708 // if this symbol also has a definition, then don't make an undefine
709 // because it is a tentative definition
710 if (_defines.count(it->getKey()) == 0) {
711 NameAndAttributes info = it->getValue();
712 _symbols.push_back(info);
719 uint32_t LTOModule::getSymbolCount() {
720 return _symbols.size();
724 lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) {
725 if (index < _symbols.size())
726 return _symbols[index].attributes;
728 return lto_symbol_attributes(0);
731 const char *LTOModule::getSymbolName(uint32_t index) {
732 if (index < _symbols.size())
733 return _symbols[index].name;