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