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