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