Path: blob/main/contrib/llvm-project/llvm/lib/LTO/LTOModule.cpp
35233 views
//===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the Link Time Optimization library. This library is9// intended to be used by linker to optimize code at link time.10//11//===----------------------------------------------------------------------===//1213#include "llvm/LTO/legacy/LTOModule.h"14#include "llvm/Bitcode/BitcodeReader.h"15#include "llvm/CodeGen/TargetSubtargetInfo.h"16#include "llvm/IR/Constants.h"17#include "llvm/IR/LLVMContext.h"18#include "llvm/IR/Mangler.h"19#include "llvm/IR/Metadata.h"20#include "llvm/IR/Module.h"21#include "llvm/MC/MCExpr.h"22#include "llvm/MC/MCInst.h"23#include "llvm/MC/MCParser/MCAsmParser.h"24#include "llvm/MC/MCSection.h"25#include "llvm/MC/MCSubtargetInfo.h"26#include "llvm/MC/MCSymbol.h"27#include "llvm/MC/TargetRegistry.h"28#include "llvm/Object/IRObjectFile.h"29#include "llvm/Object/MachO.h"30#include "llvm/Object/ObjectFile.h"31#include "llvm/Support/FileSystem.h"32#include "llvm/Support/MemoryBuffer.h"33#include "llvm/Support/Path.h"34#include "llvm/Support/SourceMgr.h"35#include "llvm/Support/TargetSelect.h"36#include "llvm/Target/TargetLoweringObjectFile.h"37#include "llvm/TargetParser/Host.h"38#include "llvm/TargetParser/SubtargetFeature.h"39#include "llvm/TargetParser/Triple.h"40#include "llvm/Transforms/Utils/GlobalStatus.h"41#include <system_error>42using namespace llvm;43using namespace llvm::object;4445LTOModule::LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,46llvm::TargetMachine *TM)47: Mod(std::move(M)), MBRef(MBRef), _target(TM) {48assert(_target && "target machine is null");49SymTab.addModule(Mod.get());50}5152LTOModule::~LTOModule() = default;5354/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM55/// bitcode.56bool LTOModule::isBitcodeFile(const void *Mem, size_t Length) {57Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(58MemoryBufferRef(StringRef((const char *)Mem, Length), "<mem>"));59return !errorToBool(BCData.takeError());60}6162bool LTOModule::isBitcodeFile(StringRef Path) {63ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =64MemoryBuffer::getFile(Path);65if (!BufferOrErr)66return false;6768Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(69BufferOrErr.get()->getMemBufferRef());70return !errorToBool(BCData.takeError());71}7273bool LTOModule::isThinLTO() {74Expected<BitcodeLTOInfo> Result = getBitcodeLTOInfo(MBRef);75if (!Result) {76logAllUnhandledErrors(Result.takeError(), errs());77return false;78}79return Result->IsThinLTO;80}8182bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer,83StringRef TriplePrefix) {84Expected<MemoryBufferRef> BCOrErr =85IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());86if (errorToBool(BCOrErr.takeError()))87return false;88LLVMContext Context;89ErrorOr<std::string> TripleOrErr =90expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(*BCOrErr));91if (!TripleOrErr)92return false;93return StringRef(*TripleOrErr).starts_with(TriplePrefix);94}9596std::string LTOModule::getProducerString(MemoryBuffer *Buffer) {97Expected<MemoryBufferRef> BCOrErr =98IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());99if (errorToBool(BCOrErr.takeError()))100return "";101LLVMContext Context;102ErrorOr<std::string> ProducerOrErr = expectedToErrorOrAndEmitErrors(103Context, getBitcodeProducerString(*BCOrErr));104if (!ProducerOrErr)105return "";106return *ProducerOrErr;107}108109ErrorOr<std::unique_ptr<LTOModule>>110LTOModule::createFromFile(LLVMContext &Context, StringRef path,111const TargetOptions &options) {112ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =113MemoryBuffer::getFile(path);114if (std::error_code EC = BufferOrErr.getError()) {115Context.emitError(EC.message());116return EC;117}118std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());119return makeLTOModule(Buffer->getMemBufferRef(), options, Context,120/* ShouldBeLazy*/ false);121}122123ErrorOr<std::unique_ptr<LTOModule>>124LTOModule::createFromOpenFile(LLVMContext &Context, int fd, StringRef path,125size_t size, const TargetOptions &options) {126return createFromOpenFileSlice(Context, fd, path, size, 0, options);127}128129ErrorOr<std::unique_ptr<LTOModule>>130LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,131size_t map_size, off_t offset,132const TargetOptions &options) {133ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =134MemoryBuffer::getOpenFileSlice(sys::fs::convertFDToNativeFile(fd), path,135map_size, offset);136if (std::error_code EC = BufferOrErr.getError()) {137Context.emitError(EC.message());138return EC;139}140std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());141return makeLTOModule(Buffer->getMemBufferRef(), options, Context,142/* ShouldBeLazy */ false);143}144145ErrorOr<std::unique_ptr<LTOModule>>146LTOModule::createFromBuffer(LLVMContext &Context, const void *mem,147size_t length, const TargetOptions &options,148StringRef path) {149StringRef Data((const char *)mem, length);150MemoryBufferRef Buffer(Data, path);151return makeLTOModule(Buffer, options, Context, /* ShouldBeLazy */ false);152}153154ErrorOr<std::unique_ptr<LTOModule>>155LTOModule::createInLocalContext(std::unique_ptr<LLVMContext> Context,156const void *mem, size_t length,157const TargetOptions &options, StringRef path) {158StringRef Data((const char *)mem, length);159MemoryBufferRef Buffer(Data, path);160// If we own a context, we know this is being used only for symbol extraction,161// not linking. Be lazy in that case.162ErrorOr<std::unique_ptr<LTOModule>> Ret =163makeLTOModule(Buffer, options, *Context, /* ShouldBeLazy */ true);164if (Ret)165(*Ret)->OwnedContext = std::move(Context);166return Ret;167}168169static ErrorOr<std::unique_ptr<Module>>170parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context,171bool ShouldBeLazy) {172// Find the buffer.173Expected<MemoryBufferRef> MBOrErr =174IRObjectFile::findBitcodeInMemBuffer(Buffer);175if (Error E = MBOrErr.takeError()) {176std::error_code EC = errorToErrorCode(std::move(E));177Context.emitError(EC.message());178return EC;179}180181if (!ShouldBeLazy) {182// Parse the full file.183return expectedToErrorOrAndEmitErrors(Context,184parseBitcodeFile(*MBOrErr, Context));185}186187// Parse lazily.188return expectedToErrorOrAndEmitErrors(189Context,190getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/));191}192193ErrorOr<std::unique_ptr<LTOModule>>194LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,195LLVMContext &Context, bool ShouldBeLazy) {196ErrorOr<std::unique_ptr<Module>> MOrErr =197parseBitcodeFileImpl(Buffer, Context, ShouldBeLazy);198if (std::error_code EC = MOrErr.getError())199return EC;200std::unique_ptr<Module> &M = *MOrErr;201202std::string TripleStr = M->getTargetTriple();203if (TripleStr.empty())204TripleStr = sys::getDefaultTargetTriple();205llvm::Triple Triple(TripleStr);206207// find machine architecture for this module208std::string errMsg;209const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);210if (!march)211return make_error_code(object::object_error::arch_not_found);212213// construct LTOModule, hand over ownership of module and target214SubtargetFeatures Features;215Features.getDefaultSubtargetFeatures(Triple);216std::string FeatureStr = Features.getString();217// Set a default CPU for Darwin triples.218std::string CPU;219if (Triple.isOSDarwin()) {220if (Triple.getArch() == llvm::Triple::x86_64)221CPU = "core2";222else if (Triple.getArch() == llvm::Triple::x86)223CPU = "yonah";224else if (Triple.isArm64e())225CPU = "apple-a12";226else if (Triple.getArch() == llvm::Triple::aarch64 ||227Triple.getArch() == llvm::Triple::aarch64_32)228CPU = "cyclone";229}230231TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,232options, std::nullopt);233234std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(M), Buffer, target));235Ret->parseSymbols();236Ret->parseMetadata();237238return std::move(Ret);239}240241/// Create a MemoryBuffer from a memory range with an optional name.242std::unique_ptr<MemoryBuffer>243LTOModule::makeBuffer(const void *mem, size_t length, StringRef name) {244const char *startPtr = (const char*)mem;245return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);246}247248/// objcClassNameFromExpression - Get string that the data pointer points to.249bool250LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {251if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {252Constant *op = ce->getOperand(0);253if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {254Constant *cn = gvn->getInitializer();255if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {256if (ca->isCString()) {257name = (".objc_class_name_" + ca->getAsCString()).str();258return true;259}260}261}262}263return false;264}265266/// addObjCClass - Parse i386/ppc ObjC class data structure.267void LTOModule::addObjCClass(const GlobalVariable *clgv) {268const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());269if (!c) return;270271// second slot in __OBJC,__class is pointer to superclass name272std::string superclassName;273if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {274auto IterBool =275_undefines.insert(std::make_pair(superclassName, NameAndAttributes()));276if (IterBool.second) {277NameAndAttributes &info = IterBool.first->second;278info.name = IterBool.first->first();279info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;280info.isFunction = false;281info.symbol = clgv;282}283}284285// third slot in __OBJC,__class is pointer to class name286std::string className;287if (objcClassNameFromExpression(c->getOperand(2), className)) {288auto Iter = _defines.insert(className).first;289290NameAndAttributes info;291info.name = Iter->first();292info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |293LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;294info.isFunction = false;295info.symbol = clgv;296_symbols.push_back(info);297}298}299300/// addObjCCategory - Parse i386/ppc ObjC category data structure.301void LTOModule::addObjCCategory(const GlobalVariable *clgv) {302const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());303if (!c) return;304305// second slot in __OBJC,__category is pointer to target class name306std::string targetclassName;307if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))308return;309310auto IterBool =311_undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));312313if (!IterBool.second)314return;315316NameAndAttributes &info = IterBool.first->second;317info.name = IterBool.first->first();318info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;319info.isFunction = false;320info.symbol = clgv;321}322323/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.324void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {325std::string targetclassName;326if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))327return;328329auto IterBool =330_undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));331332if (!IterBool.second)333return;334335NameAndAttributes &info = IterBool.first->second;336info.name = IterBool.first->first();337info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;338info.isFunction = false;339info.symbol = clgv;340}341342void LTOModule::addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym) {343SmallString<64> Buffer;344{345raw_svector_ostream OS(Buffer);346SymTab.printSymbolName(OS, Sym);347Buffer.c_str();348}349350const GlobalValue *V = cast<GlobalValue *>(Sym);351addDefinedDataSymbol(Buffer, V);352}353354void LTOModule::addDefinedDataSymbol(StringRef Name, const GlobalValue *v) {355// Add to list of defined symbols.356addDefinedSymbol(Name, v, false);357358if (!v->hasSection() /* || !isTargetDarwin */)359return;360361// Special case i386/ppc ObjC data structures in magic sections:362// The issue is that the old ObjC object format did some strange363// contortions to avoid real linker symbols. For instance, the364// ObjC class data structure is allocated statically in the executable365// that defines that class. That data structures contains a pointer to366// its superclass. But instead of just initializing that part of the367// struct to the address of its superclass, and letting the static and368// dynamic linkers do the rest, the runtime works by having that field369// instead point to a C-string that is the name of the superclass.370// At runtime the objc initialization updates that pointer and sets371// it to point to the actual super class. As far as the linker372// knows it is just a pointer to a string. But then someone wanted the373// linker to issue errors at build time if the superclass was not found.374// So they figured out a way in mach-o object format to use an absolute375// symbols (.objc_class_name_Foo = 0) and a floating reference376// (.reference .objc_class_name_Bar) to cause the linker into erroring when377// a class was missing.378// The following synthesizes the implicit .objc_* symbols for the linker379// from the ObjC data structures generated by the front end.380381// special case if this data blob is an ObjC class definition382if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(v)) {383StringRef Section = GV->getSection();384if (Section.starts_with("__OBJC,__class,")) {385addObjCClass(GV);386}387388// special case if this data blob is an ObjC category definition389else if (Section.starts_with("__OBJC,__category,")) {390addObjCCategory(GV);391}392393// special case if this data blob is the list of referenced classes394else if (Section.starts_with("__OBJC,__cls_refs,")) {395addObjCClassRef(GV);396}397}398}399400void LTOModule::addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym) {401SmallString<64> Buffer;402{403raw_svector_ostream OS(Buffer);404SymTab.printSymbolName(OS, Sym);405Buffer.c_str();406}407408const Function *F = cast<Function>(cast<GlobalValue *>(Sym));409addDefinedFunctionSymbol(Buffer, F);410}411412void LTOModule::addDefinedFunctionSymbol(StringRef Name, const Function *F) {413// add to list of defined symbols414addDefinedSymbol(Name, F, true);415}416417void LTOModule::addDefinedSymbol(StringRef Name, const GlobalValue *def,418bool isFunction) {419const GlobalObject *go = dyn_cast<GlobalObject>(def);420uint32_t attr = go ? Log2(go->getAlign().valueOrOne()) : 0;421422// set permissions part423if (isFunction) {424attr |= LTO_SYMBOL_PERMISSIONS_CODE;425} else {426const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);427if (gv && gv->isConstant())428attr |= LTO_SYMBOL_PERMISSIONS_RODATA;429else430attr |= LTO_SYMBOL_PERMISSIONS_DATA;431}432433// set definition part434if (def->hasWeakLinkage() || def->hasLinkOnceLinkage())435attr |= LTO_SYMBOL_DEFINITION_WEAK;436else if (def->hasCommonLinkage())437attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;438else439attr |= LTO_SYMBOL_DEFINITION_REGULAR;440441// set scope part442if (def->hasLocalLinkage())443// Ignore visibility if linkage is local.444attr |= LTO_SYMBOL_SCOPE_INTERNAL;445else if (def->hasHiddenVisibility())446attr |= LTO_SYMBOL_SCOPE_HIDDEN;447else if (def->hasProtectedVisibility())448attr |= LTO_SYMBOL_SCOPE_PROTECTED;449else if (def->canBeOmittedFromSymbolTable())450attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;451else452attr |= LTO_SYMBOL_SCOPE_DEFAULT;453454if (def->hasComdat())455attr |= LTO_SYMBOL_COMDAT;456457if (isa<GlobalAlias>(def))458attr |= LTO_SYMBOL_ALIAS;459460auto Iter = _defines.insert(Name).first;461462// fill information structure463NameAndAttributes info;464StringRef NameRef = Iter->first();465info.name = NameRef;466assert(NameRef.data()[NameRef.size()] == '\0');467info.attributes = attr;468info.isFunction = isFunction;469info.symbol = def;470471// add to table of symbols472_symbols.push_back(info);473}474475/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the476/// defined list.477void LTOModule::addAsmGlobalSymbol(StringRef name,478lto_symbol_attributes scope) {479auto IterBool = _defines.insert(name);480481// only add new define if not already defined482if (!IterBool.second)483return;484485NameAndAttributes &info = _undefines[IterBool.first->first()];486487if (info.symbol == nullptr) {488// FIXME: This is trying to take care of module ASM like this:489//490// module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"491//492// but is gross and its mother dresses it funny. Have the ASM parser give us493// more details for this type of situation so that we're not guessing so494// much.495496// fill information structure497info.name = IterBool.first->first();498info.attributes =499LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;500info.isFunction = false;501info.symbol = nullptr;502503// add to table of symbols504_symbols.push_back(info);505return;506}507508if (info.isFunction)509addDefinedFunctionSymbol(info.name, cast<Function>(info.symbol));510else511addDefinedDataSymbol(info.name, info.symbol);512513_symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;514_symbols.back().attributes |= scope;515}516517/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the518/// undefined list.519void LTOModule::addAsmGlobalSymbolUndef(StringRef name) {520auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));521522_asm_undefines.push_back(IterBool.first->first());523524// we already have the symbol525if (!IterBool.second)526return;527528uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;529attr |= LTO_SYMBOL_SCOPE_DEFAULT;530NameAndAttributes &info = IterBool.first->second;531info.name = IterBool.first->first();532info.attributes = attr;533info.isFunction = false;534info.symbol = nullptr;535}536537/// Add a symbol which isn't defined just yet to a list to be resolved later.538void LTOModule::addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,539bool isFunc) {540SmallString<64> name;541{542raw_svector_ostream OS(name);543SymTab.printSymbolName(OS, Sym);544name.c_str();545}546547auto IterBool =548_undefines.insert(std::make_pair(name.str(), NameAndAttributes()));549550// we already have the symbol551if (!IterBool.second)552return;553554NameAndAttributes &info = IterBool.first->second;555556info.name = IterBool.first->first();557558const GlobalValue *decl = dyn_cast_if_present<GlobalValue *>(Sym);559560if (decl->hasExternalWeakLinkage())561info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;562else563info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;564565info.isFunction = isFunc;566info.symbol = decl;567}568569void LTOModule::parseSymbols() {570for (auto Sym : SymTab.symbols()) {571auto *GV = dyn_cast_if_present<GlobalValue *>(Sym);572uint32_t Flags = SymTab.getSymbolFlags(Sym);573if (Flags & object::BasicSymbolRef::SF_FormatSpecific)574continue;575576bool IsUndefined = Flags & object::BasicSymbolRef::SF_Undefined;577578if (!GV) {579SmallString<64> Buffer;580{581raw_svector_ostream OS(Buffer);582SymTab.printSymbolName(OS, Sym);583Buffer.c_str();584}585StringRef Name = Buffer;586587if (IsUndefined)588addAsmGlobalSymbolUndef(Name);589else if (Flags & object::BasicSymbolRef::SF_Global)590addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_DEFAULT);591else592addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_INTERNAL);593continue;594}595596auto *F = dyn_cast<Function>(GV);597if (IsUndefined) {598addPotentialUndefinedSymbol(Sym, F != nullptr);599continue;600}601602if (F) {603addDefinedFunctionSymbol(Sym);604continue;605}606607if (isa<GlobalVariable>(GV)) {608addDefinedDataSymbol(Sym);609continue;610}611612assert(isa<GlobalAlias>(GV));613addDefinedDataSymbol(Sym);614}615616// make symbols for all undefines617for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),618e = _undefines.end(); u != e; ++u) {619// If this symbol also has a definition, then don't make an undefine because620// it is a tentative definition.621if (_defines.count(u->getKey())) continue;622NameAndAttributes info = u->getValue();623_symbols.push_back(info);624}625}626627/// parseMetadata - Parse metadata from the module628void LTOModule::parseMetadata() {629raw_string_ostream OS(LinkerOpts);630631// Linker Options632if (NamedMDNode *LinkerOptions =633getModule().getNamedMetadata("llvm.linker.options")) {634for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {635MDNode *MDOptions = LinkerOptions->getOperand(i);636for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {637MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));638OS << " " << MDOption->getString();639}640}641}642643// Globals - we only need to do this for COFF.644const Triple TT(_target->getTargetTriple());645if (!TT.isOSBinFormatCOFF())646return;647Mangler M;648for (const NameAndAttributes &Sym : _symbols) {649if (!Sym.symbol)650continue;651emitLinkerFlagsForGlobalCOFF(OS, Sym.symbol, TT, M);652}653}654655lto::InputFile *LTOModule::createInputFile(const void *buffer,656size_t buffer_size, const char *path,657std::string &outErr) {658StringRef Data((const char *)buffer, buffer_size);659MemoryBufferRef BufferRef(Data, path);660661Expected<std::unique_ptr<lto::InputFile>> ObjOrErr =662lto::InputFile::create(BufferRef);663664if (ObjOrErr)665return ObjOrErr->release();666667outErr = std::string(path) +668": Could not read LTO input file: " + toString(ObjOrErr.takeError());669return nullptr;670}671672size_t LTOModule::getDependentLibraryCount(lto::InputFile *input) {673return input->getDependentLibraries().size();674}675676const char *LTOModule::getDependentLibrary(lto::InputFile *input, size_t index,677size_t *size) {678StringRef S = input->getDependentLibraries()[index];679*size = S.size();680return S.data();681}682683Expected<uint32_t> LTOModule::getMachOCPUType() const {684return MachO::getCPUType(Triple(Mod->getTargetTriple()));685}686687Expected<uint32_t> LTOModule::getMachOCPUSubType() const {688return MachO::getCPUSubType(Triple(Mod->getTargetTriple()));689}690691bool LTOModule::hasCtorDtor() const {692for (auto Sym : SymTab.symbols()) {693if (auto *GV = dyn_cast_if_present<GlobalValue *>(Sym)) {694StringRef Name = GV->getName();695if (Name.consume_front("llvm.global_")) {696if (Name == "ctors" || Name == "dtors")697return true;698}699}700}701return false;702}703704705