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