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 "llvm/LTO/LTOModule.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Metadata.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCParser/MCAsmParser.h"
26 #include "llvm/MC/MCSection.h"
27 #include "llvm/MC/MCSubtargetInfo.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/MC/MCTargetAsmParser.h"
30 #include "llvm/MC/SubtargetFeature.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/Host.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/SourceMgr.h"
37 #include "llvm/Support/TargetRegistry.h"
38 #include "llvm/Support/TargetSelect.h"
39 #include "llvm/Target/TargetLowering.h"
40 #include "llvm/Target/TargetLoweringObjectFile.h"
41 #include "llvm/Target/TargetRegisterInfo.h"
42 #include "llvm/Transforms/Utils/GlobalStatus.h"
43 #include <system_error>
46 LTOModule::LTOModule(std::unique_ptr<object::IRObjectFile> Obj,
47 llvm::TargetMachine *TM)
48 : IRFile(std::move(Obj)), _target(TM) {}
50 /// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
52 bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
53 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
54 sys::fs::file_magic::bitcode;
57 bool LTOModule::isBitcodeFile(const char *path) {
58 sys::fs::file_magic type;
59 if (sys::fs::identify_magic(path, type))
61 return type == sys::fs::file_magic::bitcode;
64 bool LTOModule::isBitcodeForTarget(MemoryBuffer *buffer,
65 StringRef triplePrefix) {
66 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
67 return StringRef(Triple).startswith(triplePrefix);
70 LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options,
71 std::string &errMsg) {
72 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
73 MemoryBuffer::getFile(path);
74 if (std::error_code EC = BufferOrErr.getError()) {
75 errMsg = EC.message();
78 return makeLTOModule(std::move(BufferOrErr.get()), options, errMsg);
81 LTOModule *LTOModule::createFromOpenFile(int fd, const char *path, size_t size,
82 TargetOptions options,
83 std::string &errMsg) {
84 return createFromOpenFileSlice(fd, path, size, 0, options, errMsg);
87 LTOModule *LTOModule::createFromOpenFileSlice(int fd, const char *path,
88 size_t map_size, off_t offset,
89 TargetOptions options,
90 std::string &errMsg) {
91 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
92 MemoryBuffer::getOpenFileSlice(fd, path, map_size, offset);
93 if (std::error_code EC = BufferOrErr.getError()) {
94 errMsg = EC.message();
97 return makeLTOModule(std::move(BufferOrErr.get()), options, errMsg);
100 LTOModule *LTOModule::createFromBuffer(const void *mem, size_t length,
101 TargetOptions options,
102 std::string &errMsg, StringRef path) {
103 std::unique_ptr<MemoryBuffer> buffer(makeBuffer(mem, length, path));
106 return makeLTOModule(std::move(buffer), options, errMsg);
109 LTOModule *LTOModule::makeLTOModule(std::unique_ptr<MemoryBuffer> Buffer,
110 TargetOptions options,
111 std::string &errMsg) {
112 ErrorOr<Module *> MOrErr =
113 getLazyBitcodeModule(Buffer.get(), getGlobalContext());
114 if (std::error_code EC = MOrErr.getError()) {
115 errMsg = EC.message();
118 std::unique_ptr<Module> M(MOrErr.get());
120 std::string TripleStr = M->getTargetTriple();
121 if (TripleStr.empty())
122 TripleStr = sys::getDefaultTargetTriple();
123 llvm::Triple Triple(TripleStr);
125 // find machine architecture for this module
126 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
130 // construct LTOModule, hand over ownership of module and target
131 SubtargetFeatures Features;
132 Features.getDefaultSubtargetFeatures(Triple);
133 std::string FeatureStr = Features.getString();
134 // Set a default CPU for Darwin triples.
136 if (Triple.isOSDarwin()) {
137 if (Triple.getArch() == llvm::Triple::x86_64)
139 else if (Triple.getArch() == llvm::Triple::x86)
141 else if (Triple.getArch() == llvm::Triple::arm64 ||
142 Triple.getArch() == llvm::Triple::aarch64)
146 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
148 M->materializeAllPermanently(true);
149 M->setDataLayout(target->getDataLayout());
151 std::unique_ptr<object::IRObjectFile> IRObj(
152 new object::IRObjectFile(std::move(Buffer), std::move(M)));
154 LTOModule *Ret = new LTOModule(std::move(IRObj), target);
156 if (Ret->parseSymbols(errMsg)) {
161 Ret->parseMetadata();
166 /// Create a MemoryBuffer from a memory range with an optional name.
167 MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length,
169 const char *startPtr = (const char*)mem;
170 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
173 /// objcClassNameFromExpression - Get string that the data pointer points to.
175 LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
176 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
177 Constant *op = ce->getOperand(0);
178 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
179 Constant *cn = gvn->getInitializer();
180 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
181 if (ca->isCString()) {
182 name = ".objc_class_name_" + ca->getAsCString().str();
191 /// addObjCClass - Parse i386/ppc ObjC class data structure.
192 void LTOModule::addObjCClass(const GlobalVariable *clgv) {
193 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
196 // second slot in __OBJC,__class is pointer to superclass name
197 std::string superclassName;
198 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
199 NameAndAttributes info;
200 StringMap<NameAndAttributes>::value_type &entry =
201 _undefines.GetOrCreateValue(superclassName);
202 if (!entry.getValue().name) {
203 const char *symbolName = entry.getKey().data();
204 info.name = symbolName;
205 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
206 info.isFunction = false;
208 entry.setValue(info);
212 // third slot in __OBJC,__class is pointer to class name
213 std::string className;
214 if (objcClassNameFromExpression(c->getOperand(2), className)) {
215 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
218 NameAndAttributes info;
219 info.name = entry.getKey().data();
220 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
221 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
222 info.isFunction = false;
224 _symbols.push_back(info);
228 /// addObjCCategory - Parse i386/ppc ObjC category data structure.
229 void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
230 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
233 // second slot in __OBJC,__category is pointer to target class name
234 std::string targetclassName;
235 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
238 NameAndAttributes info;
239 StringMap<NameAndAttributes>::value_type &entry =
240 _undefines.GetOrCreateValue(targetclassName);
242 if (entry.getValue().name)
245 const char *symbolName = entry.getKey().data();
246 info.name = symbolName;
247 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
248 info.isFunction = false;
250 entry.setValue(info);
253 /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
254 void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
255 std::string targetclassName;
256 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
259 NameAndAttributes info;
260 StringMap<NameAndAttributes>::value_type &entry =
261 _undefines.GetOrCreateValue(targetclassName);
262 if (entry.getValue().name)
265 const char *symbolName = entry.getKey().data();
266 info.name = symbolName;
267 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
268 info.isFunction = false;
270 entry.setValue(info);
273 void LTOModule::addDefinedDataSymbol(const object::BasicSymbolRef &Sym) {
274 SmallString<64> Buffer;
276 raw_svector_ostream OS(Buffer);
280 const GlobalValue *V = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
281 addDefinedDataSymbol(Buffer.c_str(), V);
284 void LTOModule::addDefinedDataSymbol(const char *Name, const GlobalValue *v) {
285 // Add to list of defined symbols.
286 addDefinedSymbol(Name, v, false);
288 if (!v->hasSection() /* || !isTargetDarwin */)
291 // Special case i386/ppc ObjC data structures in magic sections:
292 // The issue is that the old ObjC object format did some strange
293 // contortions to avoid real linker symbols. For instance, the
294 // ObjC class data structure is allocated statically in the executable
295 // that defines that class. That data structures contains a pointer to
296 // its superclass. But instead of just initializing that part of the
297 // struct to the address of its superclass, and letting the static and
298 // dynamic linkers do the rest, the runtime works by having that field
299 // instead point to a C-string that is the name of the superclass.
300 // At runtime the objc initialization updates that pointer and sets
301 // it to point to the actual super class. As far as the linker
302 // knows it is just a pointer to a string. But then someone wanted the
303 // linker to issue errors at build time if the superclass was not found.
304 // So they figured out a way in mach-o object format to use an absolute
305 // symbols (.objc_class_name_Foo = 0) and a floating reference
306 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
307 // a class was missing.
308 // The following synthesizes the implicit .objc_* symbols for the linker
309 // from the ObjC data structures generated by the front end.
311 // special case if this data blob is an ObjC class definition
312 std::string Section = v->getSection();
313 if (Section.compare(0, 15, "__OBJC,__class,") == 0) {
314 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
319 // special case if this data blob is an ObjC category definition
320 else if (Section.compare(0, 18, "__OBJC,__category,") == 0) {
321 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
326 // special case if this data blob is the list of referenced classes
327 else if (Section.compare(0, 18, "__OBJC,__cls_refs,") == 0) {
328 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
334 void LTOModule::addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym) {
335 SmallString<64> Buffer;
337 raw_svector_ostream OS(Buffer);
342 cast<Function>(IRFile->getSymbolGV(Sym.getRawDataRefImpl()));
343 addDefinedFunctionSymbol(Buffer.c_str(), F);
346 void LTOModule::addDefinedFunctionSymbol(const char *Name, const Function *F) {
347 // add to list of defined symbols
348 addDefinedSymbol(Name, F, true);
351 static bool canBeHidden(const GlobalValue *GV) {
352 // FIXME: this is duplicated with another static function in AsmPrinter.cpp
353 GlobalValue::LinkageTypes L = GV->getLinkage();
355 if (L != GlobalValue::LinkOnceODRLinkage)
358 if (GV->hasUnnamedAddr())
361 // If it is a non constant variable, it needs to be uniqued across shared
363 if (const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV)) {
364 if (!Var->isConstant())
369 if (GlobalStatus::analyzeGlobal(GV, GS))
372 return !GS.IsCompared;
375 void LTOModule::addDefinedSymbol(const char *Name, const GlobalValue *def,
377 // set alignment part log2() can have rounding errors
378 uint32_t align = def->getAlignment();
379 uint32_t attr = align ? countTrailingZeros(align) : 0;
381 // set permissions part
383 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
385 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
386 if (gv && gv->isConstant())
387 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
389 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
392 // set definition part
393 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage())
394 attr |= LTO_SYMBOL_DEFINITION_WEAK;
395 else if (def->hasCommonLinkage())
396 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
398 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
401 if (def->hasLocalLinkage())
402 // Ignore visibility if linkage is local.
403 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
404 else if (def->hasHiddenVisibility())
405 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
406 else if (def->hasProtectedVisibility())
407 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
408 else if (canBeHidden(def))
409 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
411 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
413 StringSet::value_type &entry = _defines.GetOrCreateValue(Name);
416 // fill information structure
417 NameAndAttributes info;
418 StringRef NameRef = entry.getKey();
419 info.name = NameRef.data();
420 assert(info.name[NameRef.size()] == '\0');
421 info.attributes = attr;
422 info.isFunction = isFunction;
425 // add to table of symbols
426 _symbols.push_back(info);
429 /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
431 void LTOModule::addAsmGlobalSymbol(const char *name,
432 lto_symbol_attributes scope) {
433 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
435 // only add new define if not already defined
436 if (entry.getValue())
441 NameAndAttributes &info = _undefines[entry.getKey().data()];
443 if (info.symbol == nullptr) {
444 // FIXME: This is trying to take care of module ASM like this:
446 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
448 // but is gross and its mother dresses it funny. Have the ASM parser give us
449 // more details for this type of situation so that we're not guessing so
452 // fill information structure
453 info.name = entry.getKey().data();
455 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
456 info.isFunction = false;
457 info.symbol = nullptr;
459 // add to table of symbols
460 _symbols.push_back(info);
465 addDefinedFunctionSymbol(info.name, cast<Function>(info.symbol));
467 addDefinedDataSymbol(info.name, info.symbol);
469 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
470 _symbols.back().attributes |= scope;
473 /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
475 void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
476 StringMap<NameAndAttributes>::value_type &entry =
477 _undefines.GetOrCreateValue(name);
479 _asm_undefines.push_back(entry.getKey().data());
481 // we already have the symbol
482 if (entry.getValue().name)
485 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;
486 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
487 NameAndAttributes info;
488 info.name = entry.getKey().data();
489 info.attributes = attr;
490 info.isFunction = false;
491 info.symbol = nullptr;
493 entry.setValue(info);
496 /// Add a symbol which isn't defined just yet to a list to be resolved later.
497 void LTOModule::addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
499 SmallString<64> name;
501 raw_svector_ostream OS(name);
505 StringMap<NameAndAttributes>::value_type &entry =
506 _undefines.GetOrCreateValue(name);
508 // we already have the symbol
509 if (entry.getValue().name)
512 NameAndAttributes info;
514 info.name = entry.getKey().data();
516 const GlobalValue *decl = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
518 if (decl->hasExternalWeakLinkage())
519 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
521 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
523 info.isFunction = isFunc;
526 entry.setValue(info);
529 /// parseSymbols - Parse the symbols from the module and model-level ASM and add
530 /// them to either the defined or undefined lists.
531 bool LTOModule::parseSymbols(std::string &errMsg) {
532 for (auto &Sym : IRFile->symbols()) {
533 const GlobalValue *GV = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
534 uint32_t Flags = Sym.getFlags();
535 if (Flags & object::BasicSymbolRef::SF_FormatSpecific)
538 bool IsUndefined = Flags & object::BasicSymbolRef::SF_Undefined;
541 SmallString<64> Buffer;
543 raw_svector_ostream OS(Buffer);
546 const char *Name = Buffer.c_str();
549 addAsmGlobalSymbolUndef(Name);
550 else if (Flags & object::BasicSymbolRef::SF_Global)
551 addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_DEFAULT);
553 addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_INTERNAL);
557 auto *F = dyn_cast<Function>(GV);
559 addPotentialUndefinedSymbol(Sym, F != nullptr);
564 addDefinedFunctionSymbol(Sym);
568 if (isa<GlobalVariable>(GV)) {
569 addDefinedDataSymbol(Sym);
573 assert(isa<GlobalAlias>(GV));
574 addDefinedDataSymbol(Sym);
577 // make symbols for all undefines
578 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
579 e = _undefines.end(); u != e; ++u) {
580 // If this symbol also has a definition, then don't make an undefine because
581 // it is a tentative definition.
582 if (_defines.count(u->getKey())) continue;
583 NameAndAttributes info = u->getValue();
584 _symbols.push_back(info);
590 /// parseMetadata - Parse metadata from the module
591 void LTOModule::parseMetadata() {
593 if (Value *Val = getModule().getModuleFlag("Linker Options")) {
594 MDNode *LinkerOptions = cast<MDNode>(Val);
595 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
596 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
597 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
598 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
599 StringRef Op = _linkeropt_strings.
600 GetOrCreateValue(MDOption->getString()).getKey();
601 StringRef DepLibName = _target->getTargetLowering()->
602 getObjFileLowering().getDepLibFromLinkerOpt(Op);
603 if (!DepLibName.empty())
604 _deplibs.push_back(DepLibName.data());
605 else if (!Op.empty())
606 _linkeropts.push_back(Op.data());
611 // Add other interesting metadata here.