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