10b57cec5SDimitry Andric //===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the Link Time Optimization library. This library is
100b57cec5SDimitry Andric // intended to be used by linker to optimize code at link time.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "llvm/LTO/legacy/LTOModule.h"
150b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeReader.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
170b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
180b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
190b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
200b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
210b57cec5SDimitry Andric #include "llvm/IR/Module.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
240b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCAsmParser.h"
250b57cec5SDimitry Andric #include "llvm/MC/MCSection.h"
260b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
28349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
290b57cec5SDimitry Andric #include "llvm/Object/IRObjectFile.h"
305ffd83dbSDimitry Andric #include "llvm/Object/MachO.h"
310b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
320b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
330b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
340b57cec5SDimitry Andric #include "llvm/Support/Path.h"
350b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h"
360b57cec5SDimitry Andric #include "llvm/Support/TargetSelect.h"
370b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
3806c3fb27SDimitry Andric #include "llvm/TargetParser/Host.h"
3906c3fb27SDimitry Andric #include "llvm/TargetParser/SubtargetFeature.h"
4006c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
410b57cec5SDimitry Andric #include "llvm/Transforms/Utils/GlobalStatus.h"
420b57cec5SDimitry Andric #include <system_error>
430b57cec5SDimitry Andric using namespace llvm;
440b57cec5SDimitry Andric using namespace llvm::object;
450b57cec5SDimitry Andric
LTOModule(std::unique_ptr<Module> M,MemoryBufferRef MBRef,llvm::TargetMachine * TM)460b57cec5SDimitry Andric LTOModule::LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
470b57cec5SDimitry Andric llvm::TargetMachine *TM)
480b57cec5SDimitry Andric : Mod(std::move(M)), MBRef(MBRef), _target(TM) {
49e8d8bef9SDimitry Andric assert(_target && "target machine is null");
500b57cec5SDimitry Andric SymTab.addModule(Mod.get());
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric
5381ad6265SDimitry Andric LTOModule::~LTOModule() = default;
540b57cec5SDimitry Andric
550b57cec5SDimitry Andric /// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
560b57cec5SDimitry Andric /// bitcode.
isBitcodeFile(const void * Mem,size_t Length)570b57cec5SDimitry Andric bool LTOModule::isBitcodeFile(const void *Mem, size_t Length) {
580b57cec5SDimitry Andric Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
590b57cec5SDimitry Andric MemoryBufferRef(StringRef((const char *)Mem, Length), "<mem>"));
600b57cec5SDimitry Andric return !errorToBool(BCData.takeError());
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
isBitcodeFile(StringRef Path)630b57cec5SDimitry Andric bool LTOModule::isBitcodeFile(StringRef Path) {
640b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
650b57cec5SDimitry Andric MemoryBuffer::getFile(Path);
660b57cec5SDimitry Andric if (!BufferOrErr)
670b57cec5SDimitry Andric return false;
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
700b57cec5SDimitry Andric BufferOrErr.get()->getMemBufferRef());
710b57cec5SDimitry Andric return !errorToBool(BCData.takeError());
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric
isThinLTO()740b57cec5SDimitry Andric bool LTOModule::isThinLTO() {
750b57cec5SDimitry Andric Expected<BitcodeLTOInfo> Result = getBitcodeLTOInfo(MBRef);
760b57cec5SDimitry Andric if (!Result) {
770b57cec5SDimitry Andric logAllUnhandledErrors(Result.takeError(), errs());
780b57cec5SDimitry Andric return false;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric return Result->IsThinLTO;
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric
isBitcodeForTarget(MemoryBuffer * Buffer,StringRef TriplePrefix)830b57cec5SDimitry Andric bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer,
840b57cec5SDimitry Andric StringRef TriplePrefix) {
850b57cec5SDimitry Andric Expected<MemoryBufferRef> BCOrErr =
860b57cec5SDimitry Andric IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
870b57cec5SDimitry Andric if (errorToBool(BCOrErr.takeError()))
880b57cec5SDimitry Andric return false;
890b57cec5SDimitry Andric LLVMContext Context;
900b57cec5SDimitry Andric ErrorOr<std::string> TripleOrErr =
910b57cec5SDimitry Andric expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(*BCOrErr));
920b57cec5SDimitry Andric if (!TripleOrErr)
930b57cec5SDimitry Andric return false;
945f757f3fSDimitry Andric return StringRef(*TripleOrErr).starts_with(TriplePrefix);
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric
getProducerString(MemoryBuffer * Buffer)970b57cec5SDimitry Andric std::string LTOModule::getProducerString(MemoryBuffer *Buffer) {
980b57cec5SDimitry Andric Expected<MemoryBufferRef> BCOrErr =
990b57cec5SDimitry Andric IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
1000b57cec5SDimitry Andric if (errorToBool(BCOrErr.takeError()))
1010b57cec5SDimitry Andric return "";
1020b57cec5SDimitry Andric LLVMContext Context;
1030b57cec5SDimitry Andric ErrorOr<std::string> ProducerOrErr = expectedToErrorOrAndEmitErrors(
1040b57cec5SDimitry Andric Context, getBitcodeProducerString(*BCOrErr));
1050b57cec5SDimitry Andric if (!ProducerOrErr)
1060b57cec5SDimitry Andric return "";
1070b57cec5SDimitry Andric return *ProducerOrErr;
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric ErrorOr<std::unique_ptr<LTOModule>>
createFromFile(LLVMContext & Context,StringRef path,const TargetOptions & options)1110b57cec5SDimitry Andric LTOModule::createFromFile(LLVMContext &Context, StringRef path,
1120b57cec5SDimitry Andric const TargetOptions &options) {
1130b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
1140b57cec5SDimitry Andric MemoryBuffer::getFile(path);
1150b57cec5SDimitry Andric if (std::error_code EC = BufferOrErr.getError()) {
1160b57cec5SDimitry Andric Context.emitError(EC.message());
1170b57cec5SDimitry Andric return EC;
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
1200b57cec5SDimitry Andric return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
1210b57cec5SDimitry Andric /* ShouldBeLazy*/ false);
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric ErrorOr<std::unique_ptr<LTOModule>>
createFromOpenFile(LLVMContext & Context,int fd,StringRef path,size_t size,const TargetOptions & options)1250b57cec5SDimitry Andric LTOModule::createFromOpenFile(LLVMContext &Context, int fd, StringRef path,
1260b57cec5SDimitry Andric size_t size, const TargetOptions &options) {
1270b57cec5SDimitry Andric return createFromOpenFileSlice(Context, fd, path, size, 0, options);
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric ErrorOr<std::unique_ptr<LTOModule>>
createFromOpenFileSlice(LLVMContext & Context,int fd,StringRef path,size_t map_size,off_t offset,const TargetOptions & options)1310b57cec5SDimitry Andric LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
1320b57cec5SDimitry Andric size_t map_size, off_t offset,
1330b57cec5SDimitry Andric const TargetOptions &options) {
1340b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
1350b57cec5SDimitry Andric MemoryBuffer::getOpenFileSlice(sys::fs::convertFDToNativeFile(fd), path,
1360b57cec5SDimitry Andric map_size, offset);
1370b57cec5SDimitry Andric if (std::error_code EC = BufferOrErr.getError()) {
1380b57cec5SDimitry Andric Context.emitError(EC.message());
1390b57cec5SDimitry Andric return EC;
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
1420b57cec5SDimitry Andric return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
1430b57cec5SDimitry Andric /* ShouldBeLazy */ false);
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric
1460b57cec5SDimitry Andric ErrorOr<std::unique_ptr<LTOModule>>
createFromBuffer(LLVMContext & Context,const void * mem,size_t length,const TargetOptions & options,StringRef path)1470b57cec5SDimitry Andric LTOModule::createFromBuffer(LLVMContext &Context, const void *mem,
1480b57cec5SDimitry Andric size_t length, const TargetOptions &options,
1490b57cec5SDimitry Andric StringRef path) {
1500b57cec5SDimitry Andric StringRef Data((const char *)mem, length);
1510b57cec5SDimitry Andric MemoryBufferRef Buffer(Data, path);
1520b57cec5SDimitry Andric return makeLTOModule(Buffer, options, Context, /* ShouldBeLazy */ false);
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric ErrorOr<std::unique_ptr<LTOModule>>
createInLocalContext(std::unique_ptr<LLVMContext> Context,const void * mem,size_t length,const TargetOptions & options,StringRef path)1560b57cec5SDimitry Andric LTOModule::createInLocalContext(std::unique_ptr<LLVMContext> Context,
1570b57cec5SDimitry Andric const void *mem, size_t length,
1580b57cec5SDimitry Andric const TargetOptions &options, StringRef path) {
1590b57cec5SDimitry Andric StringRef Data((const char *)mem, length);
1600b57cec5SDimitry Andric MemoryBufferRef Buffer(Data, path);
1610b57cec5SDimitry Andric // If we own a context, we know this is being used only for symbol extraction,
1620b57cec5SDimitry Andric // not linking. Be lazy in that case.
1630b57cec5SDimitry Andric ErrorOr<std::unique_ptr<LTOModule>> Ret =
1640b57cec5SDimitry Andric makeLTOModule(Buffer, options, *Context, /* ShouldBeLazy */ true);
1650b57cec5SDimitry Andric if (Ret)
1660b57cec5SDimitry Andric (*Ret)->OwnedContext = std::move(Context);
1670b57cec5SDimitry Andric return Ret;
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric
1700b57cec5SDimitry Andric static ErrorOr<std::unique_ptr<Module>>
parseBitcodeFileImpl(MemoryBufferRef Buffer,LLVMContext & Context,bool ShouldBeLazy)1710b57cec5SDimitry Andric parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context,
1720b57cec5SDimitry Andric bool ShouldBeLazy) {
1730b57cec5SDimitry Andric // Find the buffer.
1740b57cec5SDimitry Andric Expected<MemoryBufferRef> MBOrErr =
1750b57cec5SDimitry Andric IRObjectFile::findBitcodeInMemBuffer(Buffer);
1760b57cec5SDimitry Andric if (Error E = MBOrErr.takeError()) {
1770b57cec5SDimitry Andric std::error_code EC = errorToErrorCode(std::move(E));
1780b57cec5SDimitry Andric Context.emitError(EC.message());
1790b57cec5SDimitry Andric return EC;
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andric if (!ShouldBeLazy) {
1830b57cec5SDimitry Andric // Parse the full file.
1840b57cec5SDimitry Andric return expectedToErrorOrAndEmitErrors(Context,
1850b57cec5SDimitry Andric parseBitcodeFile(*MBOrErr, Context));
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric // Parse lazily.
1890b57cec5SDimitry Andric return expectedToErrorOrAndEmitErrors(
1900b57cec5SDimitry Andric Context,
1910b57cec5SDimitry Andric getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/));
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric ErrorOr<std::unique_ptr<LTOModule>>
makeLTOModule(MemoryBufferRef Buffer,const TargetOptions & options,LLVMContext & Context,bool ShouldBeLazy)1950b57cec5SDimitry Andric LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
1960b57cec5SDimitry Andric LLVMContext &Context, bool ShouldBeLazy) {
1970b57cec5SDimitry Andric ErrorOr<std::unique_ptr<Module>> MOrErr =
1980b57cec5SDimitry Andric parseBitcodeFileImpl(Buffer, Context, ShouldBeLazy);
1990b57cec5SDimitry Andric if (std::error_code EC = MOrErr.getError())
2000b57cec5SDimitry Andric return EC;
2010b57cec5SDimitry Andric std::unique_ptr<Module> &M = *MOrErr;
2020b57cec5SDimitry Andric
2030b57cec5SDimitry Andric std::string TripleStr = M->getTargetTriple();
2040b57cec5SDimitry Andric if (TripleStr.empty())
2050b57cec5SDimitry Andric TripleStr = sys::getDefaultTargetTriple();
2060b57cec5SDimitry Andric llvm::Triple Triple(TripleStr);
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andric // find machine architecture for this module
2090b57cec5SDimitry Andric std::string errMsg;
2100b57cec5SDimitry Andric const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
2110b57cec5SDimitry Andric if (!march)
2120b57cec5SDimitry Andric return make_error_code(object::object_error::arch_not_found);
2130b57cec5SDimitry Andric
2140b57cec5SDimitry Andric // construct LTOModule, hand over ownership of module and target
2150b57cec5SDimitry Andric SubtargetFeatures Features;
2160b57cec5SDimitry Andric Features.getDefaultSubtargetFeatures(Triple);
2170b57cec5SDimitry Andric std::string FeatureStr = Features.getString();
2180b57cec5SDimitry Andric // Set a default CPU for Darwin triples.
2190b57cec5SDimitry Andric std::string CPU;
2200b57cec5SDimitry Andric if (Triple.isOSDarwin()) {
2210b57cec5SDimitry Andric if (Triple.getArch() == llvm::Triple::x86_64)
2220b57cec5SDimitry Andric CPU = "core2";
2230b57cec5SDimitry Andric else if (Triple.getArch() == llvm::Triple::x86)
2240b57cec5SDimitry Andric CPU = "yonah";
225e8d8bef9SDimitry Andric else if (Triple.isArm64e())
226e8d8bef9SDimitry Andric CPU = "apple-a12";
2278bcb0991SDimitry Andric else if (Triple.getArch() == llvm::Triple::aarch64 ||
2288bcb0991SDimitry Andric Triple.getArch() == llvm::Triple::aarch64_32)
2290b57cec5SDimitry Andric CPU = "cyclone";
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric
232bdd1243dSDimitry Andric TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
233bdd1243dSDimitry Andric options, std::nullopt);
2340b57cec5SDimitry Andric
2350b57cec5SDimitry Andric std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(M), Buffer, target));
2360b57cec5SDimitry Andric Ret->parseSymbols();
2370b57cec5SDimitry Andric Ret->parseMetadata();
2380b57cec5SDimitry Andric
2390b57cec5SDimitry Andric return std::move(Ret);
2400b57cec5SDimitry Andric }
2410b57cec5SDimitry Andric
2420b57cec5SDimitry Andric /// Create a MemoryBuffer from a memory range with an optional name.
2430b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer>
makeBuffer(const void * mem,size_t length,StringRef name)2440b57cec5SDimitry Andric LTOModule::makeBuffer(const void *mem, size_t length, StringRef name) {
2450b57cec5SDimitry Andric const char *startPtr = (const char*)mem;
2460b57cec5SDimitry Andric return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
2470b57cec5SDimitry Andric }
2480b57cec5SDimitry Andric
2490b57cec5SDimitry Andric /// objcClassNameFromExpression - Get string that the data pointer points to.
2500b57cec5SDimitry Andric bool
objcClassNameFromExpression(const Constant * c,std::string & name)2510b57cec5SDimitry Andric LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
2520b57cec5SDimitry Andric if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
2530b57cec5SDimitry Andric Constant *op = ce->getOperand(0);
2540b57cec5SDimitry Andric if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
2550b57cec5SDimitry Andric Constant *cn = gvn->getInitializer();
2560b57cec5SDimitry Andric if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
2570b57cec5SDimitry Andric if (ca->isCString()) {
2580b57cec5SDimitry Andric name = (".objc_class_name_" + ca->getAsCString()).str();
2590b57cec5SDimitry Andric return true;
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric }
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric return false;
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric /// addObjCClass - Parse i386/ppc ObjC class data structure.
addObjCClass(const GlobalVariable * clgv)2680b57cec5SDimitry Andric void LTOModule::addObjCClass(const GlobalVariable *clgv) {
2690b57cec5SDimitry Andric const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
2700b57cec5SDimitry Andric if (!c) return;
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric // second slot in __OBJC,__class is pointer to superclass name
2730b57cec5SDimitry Andric std::string superclassName;
2740b57cec5SDimitry Andric if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
2750b57cec5SDimitry Andric auto IterBool =
2760b57cec5SDimitry Andric _undefines.insert(std::make_pair(superclassName, NameAndAttributes()));
2770b57cec5SDimitry Andric if (IterBool.second) {
2780b57cec5SDimitry Andric NameAndAttributes &info = IterBool.first->second;
2790b57cec5SDimitry Andric info.name = IterBool.first->first();
2800b57cec5SDimitry Andric info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
2810b57cec5SDimitry Andric info.isFunction = false;
2820b57cec5SDimitry Andric info.symbol = clgv;
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric
2860b57cec5SDimitry Andric // third slot in __OBJC,__class is pointer to class name
2870b57cec5SDimitry Andric std::string className;
2880b57cec5SDimitry Andric if (objcClassNameFromExpression(c->getOperand(2), className)) {
2890b57cec5SDimitry Andric auto Iter = _defines.insert(className).first;
2900b57cec5SDimitry Andric
2910b57cec5SDimitry Andric NameAndAttributes info;
2920b57cec5SDimitry Andric info.name = Iter->first();
2930b57cec5SDimitry Andric info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
2940b57cec5SDimitry Andric LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
2950b57cec5SDimitry Andric info.isFunction = false;
2960b57cec5SDimitry Andric info.symbol = clgv;
2970b57cec5SDimitry Andric _symbols.push_back(info);
2980b57cec5SDimitry Andric }
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric
3010b57cec5SDimitry Andric /// addObjCCategory - Parse i386/ppc ObjC category data structure.
addObjCCategory(const GlobalVariable * clgv)3020b57cec5SDimitry Andric void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
3030b57cec5SDimitry Andric const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
3040b57cec5SDimitry Andric if (!c) return;
3050b57cec5SDimitry Andric
3060b57cec5SDimitry Andric // second slot in __OBJC,__category is pointer to target class name
3070b57cec5SDimitry Andric std::string targetclassName;
3080b57cec5SDimitry Andric if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
3090b57cec5SDimitry Andric return;
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andric auto IterBool =
3120b57cec5SDimitry Andric _undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andric if (!IterBool.second)
3150b57cec5SDimitry Andric return;
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andric NameAndAttributes &info = IterBool.first->second;
3180b57cec5SDimitry Andric info.name = IterBool.first->first();
3190b57cec5SDimitry Andric info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
3200b57cec5SDimitry Andric info.isFunction = false;
3210b57cec5SDimitry Andric info.symbol = clgv;
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric
3240b57cec5SDimitry Andric /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
addObjCClassRef(const GlobalVariable * clgv)3250b57cec5SDimitry Andric void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
3260b57cec5SDimitry Andric std::string targetclassName;
3270b57cec5SDimitry Andric if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
3280b57cec5SDimitry Andric return;
3290b57cec5SDimitry Andric
3300b57cec5SDimitry Andric auto IterBool =
3310b57cec5SDimitry Andric _undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andric if (!IterBool.second)
3340b57cec5SDimitry Andric return;
3350b57cec5SDimitry Andric
3360b57cec5SDimitry Andric NameAndAttributes &info = IterBool.first->second;
3370b57cec5SDimitry Andric info.name = IterBool.first->first();
3380b57cec5SDimitry Andric info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
3390b57cec5SDimitry Andric info.isFunction = false;
3400b57cec5SDimitry Andric info.symbol = clgv;
3410b57cec5SDimitry Andric }
3420b57cec5SDimitry Andric
addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym)3430b57cec5SDimitry Andric void LTOModule::addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym) {
3440b57cec5SDimitry Andric SmallString<64> Buffer;
3450b57cec5SDimitry Andric {
3460b57cec5SDimitry Andric raw_svector_ostream OS(Buffer);
3470b57cec5SDimitry Andric SymTab.printSymbolName(OS, Sym);
3480b57cec5SDimitry Andric Buffer.c_str();
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric
35106c3fb27SDimitry Andric const GlobalValue *V = cast<GlobalValue *>(Sym);
3520b57cec5SDimitry Andric addDefinedDataSymbol(Buffer, V);
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric
addDefinedDataSymbol(StringRef Name,const GlobalValue * v)3550b57cec5SDimitry Andric void LTOModule::addDefinedDataSymbol(StringRef Name, const GlobalValue *v) {
3560b57cec5SDimitry Andric // Add to list of defined symbols.
3570b57cec5SDimitry Andric addDefinedSymbol(Name, v, false);
3580b57cec5SDimitry Andric
3590b57cec5SDimitry Andric if (!v->hasSection() /* || !isTargetDarwin */)
3600b57cec5SDimitry Andric return;
3610b57cec5SDimitry Andric
3620b57cec5SDimitry Andric // Special case i386/ppc ObjC data structures in magic sections:
3630b57cec5SDimitry Andric // The issue is that the old ObjC object format did some strange
3640b57cec5SDimitry Andric // contortions to avoid real linker symbols. For instance, the
3650b57cec5SDimitry Andric // ObjC class data structure is allocated statically in the executable
3660b57cec5SDimitry Andric // that defines that class. That data structures contains a pointer to
3670b57cec5SDimitry Andric // its superclass. But instead of just initializing that part of the
3680b57cec5SDimitry Andric // struct to the address of its superclass, and letting the static and
3690b57cec5SDimitry Andric // dynamic linkers do the rest, the runtime works by having that field
3700b57cec5SDimitry Andric // instead point to a C-string that is the name of the superclass.
3710b57cec5SDimitry Andric // At runtime the objc initialization updates that pointer and sets
3720b57cec5SDimitry Andric // it to point to the actual super class. As far as the linker
3730b57cec5SDimitry Andric // knows it is just a pointer to a string. But then someone wanted the
3740b57cec5SDimitry Andric // linker to issue errors at build time if the superclass was not found.
3750b57cec5SDimitry Andric // So they figured out a way in mach-o object format to use an absolute
3760b57cec5SDimitry Andric // symbols (.objc_class_name_Foo = 0) and a floating reference
3770b57cec5SDimitry Andric // (.reference .objc_class_name_Bar) to cause the linker into erroring when
3780b57cec5SDimitry Andric // a class was missing.
3790b57cec5SDimitry Andric // The following synthesizes the implicit .objc_* symbols for the linker
3800b57cec5SDimitry Andric // from the ObjC data structures generated by the front end.
3810b57cec5SDimitry Andric
3820b57cec5SDimitry Andric // special case if this data blob is an ObjC class definition
3830b57cec5SDimitry Andric if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(v)) {
3840b57cec5SDimitry Andric StringRef Section = GV->getSection();
3855f757f3fSDimitry Andric if (Section.starts_with("__OBJC,__class,")) {
3860b57cec5SDimitry Andric addObjCClass(GV);
3870b57cec5SDimitry Andric }
3880b57cec5SDimitry Andric
3890b57cec5SDimitry Andric // special case if this data blob is an ObjC category definition
3905f757f3fSDimitry Andric else if (Section.starts_with("__OBJC,__category,")) {
3910b57cec5SDimitry Andric addObjCCategory(GV);
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric
3940b57cec5SDimitry Andric // special case if this data blob is the list of referenced classes
3955f757f3fSDimitry Andric else if (Section.starts_with("__OBJC,__cls_refs,")) {
3960b57cec5SDimitry Andric addObjCClassRef(GV);
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric }
4000b57cec5SDimitry Andric
addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym)4010b57cec5SDimitry Andric void LTOModule::addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym) {
4020b57cec5SDimitry Andric SmallString<64> Buffer;
4030b57cec5SDimitry Andric {
4040b57cec5SDimitry Andric raw_svector_ostream OS(Buffer);
4050b57cec5SDimitry Andric SymTab.printSymbolName(OS, Sym);
4060b57cec5SDimitry Andric Buffer.c_str();
4070b57cec5SDimitry Andric }
4080b57cec5SDimitry Andric
40906c3fb27SDimitry Andric const Function *F = cast<Function>(cast<GlobalValue *>(Sym));
4100b57cec5SDimitry Andric addDefinedFunctionSymbol(Buffer, F);
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric
addDefinedFunctionSymbol(StringRef Name,const Function * F)4130b57cec5SDimitry Andric void LTOModule::addDefinedFunctionSymbol(StringRef Name, const Function *F) {
4140b57cec5SDimitry Andric // add to list of defined symbols
4150b57cec5SDimitry Andric addDefinedSymbol(Name, F, true);
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric
addDefinedSymbol(StringRef Name,const GlobalValue * def,bool isFunction)4180b57cec5SDimitry Andric void LTOModule::addDefinedSymbol(StringRef Name, const GlobalValue *def,
4190b57cec5SDimitry Andric bool isFunction) {
4205ffd83dbSDimitry Andric const GlobalObject *go = dyn_cast<GlobalObject>(def);
4215ffd83dbSDimitry Andric uint32_t attr = go ? Log2(go->getAlign().valueOrOne()) : 0;
4220b57cec5SDimitry Andric
4230b57cec5SDimitry Andric // set permissions part
4240b57cec5SDimitry Andric if (isFunction) {
4250b57cec5SDimitry Andric attr |= LTO_SYMBOL_PERMISSIONS_CODE;
4260b57cec5SDimitry Andric } else {
4270b57cec5SDimitry Andric const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
4280b57cec5SDimitry Andric if (gv && gv->isConstant())
4290b57cec5SDimitry Andric attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
4300b57cec5SDimitry Andric else
4310b57cec5SDimitry Andric attr |= LTO_SYMBOL_PERMISSIONS_DATA;
4320b57cec5SDimitry Andric }
4330b57cec5SDimitry Andric
4340b57cec5SDimitry Andric // set definition part
4350b57cec5SDimitry Andric if (def->hasWeakLinkage() || def->hasLinkOnceLinkage())
4360b57cec5SDimitry Andric attr |= LTO_SYMBOL_DEFINITION_WEAK;
4370b57cec5SDimitry Andric else if (def->hasCommonLinkage())
4380b57cec5SDimitry Andric attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
4390b57cec5SDimitry Andric else
4400b57cec5SDimitry Andric attr |= LTO_SYMBOL_DEFINITION_REGULAR;
4410b57cec5SDimitry Andric
4420b57cec5SDimitry Andric // set scope part
4430b57cec5SDimitry Andric if (def->hasLocalLinkage())
4440b57cec5SDimitry Andric // Ignore visibility if linkage is local.
4450b57cec5SDimitry Andric attr |= LTO_SYMBOL_SCOPE_INTERNAL;
4460b57cec5SDimitry Andric else if (def->hasHiddenVisibility())
4470b57cec5SDimitry Andric attr |= LTO_SYMBOL_SCOPE_HIDDEN;
4480b57cec5SDimitry Andric else if (def->hasProtectedVisibility())
4490b57cec5SDimitry Andric attr |= LTO_SYMBOL_SCOPE_PROTECTED;
4500b57cec5SDimitry Andric else if (def->canBeOmittedFromSymbolTable())
4510b57cec5SDimitry Andric attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
4520b57cec5SDimitry Andric else
4530b57cec5SDimitry Andric attr |= LTO_SYMBOL_SCOPE_DEFAULT;
4540b57cec5SDimitry Andric
4550b57cec5SDimitry Andric if (def->hasComdat())
4560b57cec5SDimitry Andric attr |= LTO_SYMBOL_COMDAT;
4570b57cec5SDimitry Andric
4580b57cec5SDimitry Andric if (isa<GlobalAlias>(def))
4590b57cec5SDimitry Andric attr |= LTO_SYMBOL_ALIAS;
4600b57cec5SDimitry Andric
4610b57cec5SDimitry Andric auto Iter = _defines.insert(Name).first;
4620b57cec5SDimitry Andric
4630b57cec5SDimitry Andric // fill information structure
4640b57cec5SDimitry Andric NameAndAttributes info;
4650b57cec5SDimitry Andric StringRef NameRef = Iter->first();
4660b57cec5SDimitry Andric info.name = NameRef;
4670b57cec5SDimitry Andric assert(NameRef.data()[NameRef.size()] == '\0');
4680b57cec5SDimitry Andric info.attributes = attr;
4690b57cec5SDimitry Andric info.isFunction = isFunction;
4700b57cec5SDimitry Andric info.symbol = def;
4710b57cec5SDimitry Andric
4720b57cec5SDimitry Andric // add to table of symbols
4730b57cec5SDimitry Andric _symbols.push_back(info);
4740b57cec5SDimitry Andric }
4750b57cec5SDimitry Andric
4760b57cec5SDimitry Andric /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
4770b57cec5SDimitry Andric /// defined list.
addAsmGlobalSymbol(StringRef name,lto_symbol_attributes scope)4780b57cec5SDimitry Andric void LTOModule::addAsmGlobalSymbol(StringRef name,
4790b57cec5SDimitry Andric lto_symbol_attributes scope) {
4800b57cec5SDimitry Andric auto IterBool = _defines.insert(name);
4810b57cec5SDimitry Andric
4820b57cec5SDimitry Andric // only add new define if not already defined
4830b57cec5SDimitry Andric if (!IterBool.second)
4840b57cec5SDimitry Andric return;
4850b57cec5SDimitry Andric
4860b57cec5SDimitry Andric NameAndAttributes &info = _undefines[IterBool.first->first()];
4870b57cec5SDimitry Andric
4880b57cec5SDimitry Andric if (info.symbol == nullptr) {
4890b57cec5SDimitry Andric // FIXME: This is trying to take care of module ASM like this:
4900b57cec5SDimitry Andric //
4910b57cec5SDimitry Andric // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
4920b57cec5SDimitry Andric //
4930b57cec5SDimitry Andric // but is gross and its mother dresses it funny. Have the ASM parser give us
4940b57cec5SDimitry Andric // more details for this type of situation so that we're not guessing so
4950b57cec5SDimitry Andric // much.
4960b57cec5SDimitry Andric
4970b57cec5SDimitry Andric // fill information structure
4980b57cec5SDimitry Andric info.name = IterBool.first->first();
4990b57cec5SDimitry Andric info.attributes =
5000b57cec5SDimitry Andric LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
5010b57cec5SDimitry Andric info.isFunction = false;
5020b57cec5SDimitry Andric info.symbol = nullptr;
5030b57cec5SDimitry Andric
5040b57cec5SDimitry Andric // add to table of symbols
5050b57cec5SDimitry Andric _symbols.push_back(info);
5060b57cec5SDimitry Andric return;
5070b57cec5SDimitry Andric }
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andric if (info.isFunction)
5100b57cec5SDimitry Andric addDefinedFunctionSymbol(info.name, cast<Function>(info.symbol));
5110b57cec5SDimitry Andric else
5120b57cec5SDimitry Andric addDefinedDataSymbol(info.name, info.symbol);
5130b57cec5SDimitry Andric
5140b57cec5SDimitry Andric _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
5150b57cec5SDimitry Andric _symbols.back().attributes |= scope;
5160b57cec5SDimitry Andric }
5170b57cec5SDimitry Andric
5180b57cec5SDimitry Andric /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
5190b57cec5SDimitry Andric /// undefined list.
addAsmGlobalSymbolUndef(StringRef name)5200b57cec5SDimitry Andric void LTOModule::addAsmGlobalSymbolUndef(StringRef name) {
5210b57cec5SDimitry Andric auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
5220b57cec5SDimitry Andric
5230b57cec5SDimitry Andric _asm_undefines.push_back(IterBool.first->first());
5240b57cec5SDimitry Andric
5250b57cec5SDimitry Andric // we already have the symbol
5260b57cec5SDimitry Andric if (!IterBool.second)
5270b57cec5SDimitry Andric return;
5280b57cec5SDimitry Andric
5290b57cec5SDimitry Andric uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;
5300b57cec5SDimitry Andric attr |= LTO_SYMBOL_SCOPE_DEFAULT;
5310b57cec5SDimitry Andric NameAndAttributes &info = IterBool.first->second;
5320b57cec5SDimitry Andric info.name = IterBool.first->first();
5330b57cec5SDimitry Andric info.attributes = attr;
5340b57cec5SDimitry Andric info.isFunction = false;
5350b57cec5SDimitry Andric info.symbol = nullptr;
5360b57cec5SDimitry Andric }
5370b57cec5SDimitry Andric
5380b57cec5SDimitry Andric /// Add a symbol which isn't defined just yet to a list to be resolved later.
addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,bool isFunc)5390b57cec5SDimitry Andric void LTOModule::addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
5400b57cec5SDimitry Andric bool isFunc) {
5410b57cec5SDimitry Andric SmallString<64> name;
5420b57cec5SDimitry Andric {
5430b57cec5SDimitry Andric raw_svector_ostream OS(name);
5440b57cec5SDimitry Andric SymTab.printSymbolName(OS, Sym);
5450b57cec5SDimitry Andric name.c_str();
5460b57cec5SDimitry Andric }
5470b57cec5SDimitry Andric
548fe6060f1SDimitry Andric auto IterBool =
549fe6060f1SDimitry Andric _undefines.insert(std::make_pair(name.str(), NameAndAttributes()));
5500b57cec5SDimitry Andric
5510b57cec5SDimitry Andric // we already have the symbol
5520b57cec5SDimitry Andric if (!IterBool.second)
5530b57cec5SDimitry Andric return;
5540b57cec5SDimitry Andric
5550b57cec5SDimitry Andric NameAndAttributes &info = IterBool.first->second;
5560b57cec5SDimitry Andric
5570b57cec5SDimitry Andric info.name = IterBool.first->first();
5580b57cec5SDimitry Andric
55906c3fb27SDimitry Andric const GlobalValue *decl = dyn_cast_if_present<GlobalValue *>(Sym);
5600b57cec5SDimitry Andric
5610b57cec5SDimitry Andric if (decl->hasExternalWeakLinkage())
5620b57cec5SDimitry Andric info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
5630b57cec5SDimitry Andric else
5640b57cec5SDimitry Andric info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
5650b57cec5SDimitry Andric
5660b57cec5SDimitry Andric info.isFunction = isFunc;
5670b57cec5SDimitry Andric info.symbol = decl;
5680b57cec5SDimitry Andric }
5690b57cec5SDimitry Andric
parseSymbols()5700b57cec5SDimitry Andric void LTOModule::parseSymbols() {
5710b57cec5SDimitry Andric for (auto Sym : SymTab.symbols()) {
57206c3fb27SDimitry Andric auto *GV = dyn_cast_if_present<GlobalValue *>(Sym);
5730b57cec5SDimitry Andric uint32_t Flags = SymTab.getSymbolFlags(Sym);
5740b57cec5SDimitry Andric if (Flags & object::BasicSymbolRef::SF_FormatSpecific)
5750b57cec5SDimitry Andric continue;
5760b57cec5SDimitry Andric
5770b57cec5SDimitry Andric bool IsUndefined = Flags & object::BasicSymbolRef::SF_Undefined;
5780b57cec5SDimitry Andric
5790b57cec5SDimitry Andric if (!GV) {
5800b57cec5SDimitry Andric SmallString<64> Buffer;
5810b57cec5SDimitry Andric {
5820b57cec5SDimitry Andric raw_svector_ostream OS(Buffer);
5830b57cec5SDimitry Andric SymTab.printSymbolName(OS, Sym);
5840b57cec5SDimitry Andric Buffer.c_str();
5850b57cec5SDimitry Andric }
586fe6060f1SDimitry Andric StringRef Name = Buffer;
5870b57cec5SDimitry Andric
5880b57cec5SDimitry Andric if (IsUndefined)
5890b57cec5SDimitry Andric addAsmGlobalSymbolUndef(Name);
5900b57cec5SDimitry Andric else if (Flags & object::BasicSymbolRef::SF_Global)
5910b57cec5SDimitry Andric addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_DEFAULT);
5920b57cec5SDimitry Andric else
5930b57cec5SDimitry Andric addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_INTERNAL);
5940b57cec5SDimitry Andric continue;
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric
5970b57cec5SDimitry Andric auto *F = dyn_cast<Function>(GV);
5980b57cec5SDimitry Andric if (IsUndefined) {
5990b57cec5SDimitry Andric addPotentialUndefinedSymbol(Sym, F != nullptr);
6000b57cec5SDimitry Andric continue;
6010b57cec5SDimitry Andric }
6020b57cec5SDimitry Andric
6030b57cec5SDimitry Andric if (F) {
6040b57cec5SDimitry Andric addDefinedFunctionSymbol(Sym);
6050b57cec5SDimitry Andric continue;
6060b57cec5SDimitry Andric }
6070b57cec5SDimitry Andric
6080b57cec5SDimitry Andric if (isa<GlobalVariable>(GV)) {
6090b57cec5SDimitry Andric addDefinedDataSymbol(Sym);
6100b57cec5SDimitry Andric continue;
6110b57cec5SDimitry Andric }
6120b57cec5SDimitry Andric
6130b57cec5SDimitry Andric assert(isa<GlobalAlias>(GV));
6140b57cec5SDimitry Andric addDefinedDataSymbol(Sym);
6150b57cec5SDimitry Andric }
6160b57cec5SDimitry Andric
6170b57cec5SDimitry Andric // make symbols for all undefines
6180b57cec5SDimitry Andric for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
6190b57cec5SDimitry Andric e = _undefines.end(); u != e; ++u) {
6200b57cec5SDimitry Andric // If this symbol also has a definition, then don't make an undefine because
6210b57cec5SDimitry Andric // it is a tentative definition.
6220b57cec5SDimitry Andric if (_defines.count(u->getKey())) continue;
6230b57cec5SDimitry Andric NameAndAttributes info = u->getValue();
6240b57cec5SDimitry Andric _symbols.push_back(info);
6250b57cec5SDimitry Andric }
6260b57cec5SDimitry Andric }
6270b57cec5SDimitry Andric
6280b57cec5SDimitry Andric /// parseMetadata - Parse metadata from the module
parseMetadata()6290b57cec5SDimitry Andric void LTOModule::parseMetadata() {
6300b57cec5SDimitry Andric raw_string_ostream OS(LinkerOpts);
6310b57cec5SDimitry Andric
6320b57cec5SDimitry Andric // Linker Options
6330b57cec5SDimitry Andric if (NamedMDNode *LinkerOptions =
6340b57cec5SDimitry Andric getModule().getNamedMetadata("llvm.linker.options")) {
6350b57cec5SDimitry Andric for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
6360b57cec5SDimitry Andric MDNode *MDOptions = LinkerOptions->getOperand(i);
6370b57cec5SDimitry Andric for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
6380b57cec5SDimitry Andric MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
6390b57cec5SDimitry Andric OS << " " << MDOption->getString();
6400b57cec5SDimitry Andric }
6410b57cec5SDimitry Andric }
6420b57cec5SDimitry Andric }
6430b57cec5SDimitry Andric
6440b57cec5SDimitry Andric // Globals - we only need to do this for COFF.
6450b57cec5SDimitry Andric const Triple TT(_target->getTargetTriple());
6460b57cec5SDimitry Andric if (!TT.isOSBinFormatCOFF())
6470b57cec5SDimitry Andric return;
6480b57cec5SDimitry Andric Mangler M;
6490b57cec5SDimitry Andric for (const NameAndAttributes &Sym : _symbols) {
6500b57cec5SDimitry Andric if (!Sym.symbol)
6510b57cec5SDimitry Andric continue;
6520b57cec5SDimitry Andric emitLinkerFlagsForGlobalCOFF(OS, Sym.symbol, TT, M);
6530b57cec5SDimitry Andric }
6540b57cec5SDimitry Andric }
6550b57cec5SDimitry Andric
createInputFile(const void * buffer,size_t buffer_size,const char * path,std::string & outErr)6560b57cec5SDimitry Andric lto::InputFile *LTOModule::createInputFile(const void *buffer,
6570b57cec5SDimitry Andric size_t buffer_size, const char *path,
6580b57cec5SDimitry Andric std::string &outErr) {
6590b57cec5SDimitry Andric StringRef Data((const char *)buffer, buffer_size);
6600b57cec5SDimitry Andric MemoryBufferRef BufferRef(Data, path);
6610b57cec5SDimitry Andric
6620b57cec5SDimitry Andric Expected<std::unique_ptr<lto::InputFile>> ObjOrErr =
6630b57cec5SDimitry Andric lto::InputFile::create(BufferRef);
6640b57cec5SDimitry Andric
6650b57cec5SDimitry Andric if (ObjOrErr)
6660b57cec5SDimitry Andric return ObjOrErr->release();
6670b57cec5SDimitry Andric
6680b57cec5SDimitry Andric outErr = std::string(path) +
6690b57cec5SDimitry Andric ": Could not read LTO input file: " + toString(ObjOrErr.takeError());
6700b57cec5SDimitry Andric return nullptr;
6710b57cec5SDimitry Andric }
6720b57cec5SDimitry Andric
getDependentLibraryCount(lto::InputFile * input)6730b57cec5SDimitry Andric size_t LTOModule::getDependentLibraryCount(lto::InputFile *input) {
6740b57cec5SDimitry Andric return input->getDependentLibraries().size();
6750b57cec5SDimitry Andric }
6760b57cec5SDimitry Andric
getDependentLibrary(lto::InputFile * input,size_t index,size_t * size)6770b57cec5SDimitry Andric const char *LTOModule::getDependentLibrary(lto::InputFile *input, size_t index,
6780b57cec5SDimitry Andric size_t *size) {
6790b57cec5SDimitry Andric StringRef S = input->getDependentLibraries()[index];
6800b57cec5SDimitry Andric *size = S.size();
6810b57cec5SDimitry Andric return S.data();
6820b57cec5SDimitry Andric }
6835ffd83dbSDimitry Andric
getMachOCPUType() const6845ffd83dbSDimitry Andric Expected<uint32_t> LTOModule::getMachOCPUType() const {
6855ffd83dbSDimitry Andric return MachO::getCPUType(Triple(Mod->getTargetTriple()));
6865ffd83dbSDimitry Andric }
6875ffd83dbSDimitry Andric
getMachOCPUSubType() const6885ffd83dbSDimitry Andric Expected<uint32_t> LTOModule::getMachOCPUSubType() const {
6895ffd83dbSDimitry Andric return MachO::getCPUSubType(Triple(Mod->getTargetTriple()));
6905ffd83dbSDimitry Andric }
691349cc55cSDimitry Andric
hasCtorDtor() const692349cc55cSDimitry Andric bool LTOModule::hasCtorDtor() const {
693349cc55cSDimitry Andric for (auto Sym : SymTab.symbols()) {
69406c3fb27SDimitry Andric if (auto *GV = dyn_cast_if_present<GlobalValue *>(Sym)) {
695349cc55cSDimitry Andric StringRef Name = GV->getName();
696349cc55cSDimitry Andric if (Name.consume_front("llvm.global_")) {
697349cc55cSDimitry Andric if (Name.equals("ctors") || Name.equals("dtors"))
698349cc55cSDimitry Andric return true;
699349cc55cSDimitry Andric }
700349cc55cSDimitry Andric }
701349cc55cSDimitry Andric }
702349cc55cSDimitry Andric return false;
703349cc55cSDimitry Andric }
704