blob: 29756549fd7ccca5f1c156b71ceae64f870038b8 [file] [log] [blame]
Chris Lattner2eff5052010-03-12 18:44:54 +00001//===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
Nick Kledzik07b4a622008-02-26 20:26:43 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +00007//
Nick Kledzik07b4a622008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik07b4a622008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Nick Kledzik91a6dcf2008-02-27 22:25:36 +000015#include "LTOModule.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000016#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/Triple.h"
18#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/Module.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000022#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000024#include "llvm/MC/MCParser/MCAsmParser.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000025#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000026#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000027#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000028#include "llvm/MC/MCTargetAsmParser.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000029#include "llvm/MC/SubtargetFeature.h"
Bill Wendlingb8dcda72012-08-06 21:34:54 +000030#include "llvm/Support/CommandLine.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000031#include "llvm/Support/Host.h"
Rafael Espindola46ed3532013-06-11 18:05:26 +000032#include "llvm/Support/FileSystem.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000033#include "llvm/Support/MemoryBuffer.h"
34#include "llvm/Support/Path.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000035#include "llvm/Support/SourceMgr.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000036#include "llvm/Support/TargetRegistry.h"
37#include "llvm/Support/TargetSelect.h"
38#include "llvm/Support/system_error.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000039#include "llvm/Target/TargetRegisterInfo.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000040using namespace llvm;
41
Bill Wendlingb8dcda72012-08-06 21:34:54 +000042static cl::opt<bool>
43EnableFPMAD("enable-fp-mad",
44 cl::desc("Enable less precise MAD instructions to be generated"),
45 cl::init(false));
46
47static cl::opt<bool>
48DisableFPElim("disable-fp-elim",
49 cl::desc("Disable frame pointer elimination optimization"),
50 cl::init(false));
51
52static cl::opt<bool>
53DisableFPElimNonLeaf("disable-non-leaf-fp-elim",
54 cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"),
55 cl::init(false));
56
57static cl::opt<bool>
58EnableUnsafeFPMath("enable-unsafe-fp-math",
59 cl::desc("Enable optimizations that may decrease FP precision"),
60 cl::init(false));
61
62static cl::opt<bool>
63EnableNoInfsFPMath("enable-no-infs-fp-math",
64 cl::desc("Enable FP math optimizations that assume no +-Infs"),
65 cl::init(false));
66
67static cl::opt<bool>
68EnableNoNaNsFPMath("enable-no-nans-fp-math",
69 cl::desc("Enable FP math optimizations that assume no NaNs"),
70 cl::init(false));
71
72static cl::opt<bool>
73EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
74 cl::Hidden,
75 cl::desc("Force codegen to assume rounding mode can change dynamically"),
76 cl::init(false));
77
78static cl::opt<bool>
79GenerateSoftFloatCalls("soft-float",
80 cl::desc("Generate software floating point library calls"),
81 cl::init(false));
82
83static cl::opt<llvm::FloatABI::ABIType>
84FloatABIForCalls("float-abi",
85 cl::desc("Choose float ABI type"),
86 cl::init(FloatABI::Default),
87 cl::values(
88 clEnumValN(FloatABI::Default, "default",
89 "Target default float ABI type"),
90 clEnumValN(FloatABI::Soft, "soft",
91 "Soft float ABI (implied by -soft-float)"),
92 clEnumValN(FloatABI::Hard, "hard",
93 "Hard float ABI (uses FP registers)"),
94 clEnumValEnd));
95
96static cl::opt<llvm::FPOpFusion::FPOpFusionMode>
97FuseFPOps("fp-contract",
98 cl::desc("Enable aggresive formation of fused FP ops"),
99 cl::init(FPOpFusion::Standard),
100 cl::values(
101 clEnumValN(FPOpFusion::Fast, "fast",
102 "Fuse FP ops whenever profitable"),
103 clEnumValN(FPOpFusion::Standard, "on",
104 "Only fuse 'blessed' FP ops."),
105 clEnumValN(FPOpFusion::Strict, "off",
106 "Only fuse FP ops when the result won't be effected."),
107 clEnumValEnd));
108
109static cl::opt<bool>
110DontPlaceZerosInBSS("nozero-initialized-in-bss",
111 cl::desc("Don't place zero-initialized symbols into bss section"),
112 cl::init(false));
113
114static cl::opt<bool>
115EnableGuaranteedTailCallOpt("tailcallopt",
116 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
117 cl::init(false));
118
119static cl::opt<bool>
120DisableTailCalls("disable-tail-calls",
121 cl::desc("Never emit tail calls"),
122 cl::init(false));
123
124static cl::opt<unsigned>
125OverrideStackAlignment("stack-alignment",
126 cl::desc("Override default stack alignment"),
127 cl::init(0));
128
129static cl::opt<bool>
130EnableRealignStack("realign-stack",
131 cl::desc("Realign stack if needed"),
132 cl::init(true));
133
134static cl::opt<std::string>
135TrapFuncName("trap-func", cl::Hidden,
136 cl::desc("Emit a call to trap function rather than a trap instruction"),
137 cl::init(""));
138
139static cl::opt<bool>
140EnablePIE("enable-pie",
141 cl::desc("Assume the creation of a position independent executable."),
142 cl::init(false));
143
144static cl::opt<bool>
145SegmentedStacks("segmented-stacks",
146 cl::desc("Use segmented stacks if possible."),
147 cl::init(false));
148
149static cl::opt<bool>
150UseInitArray("use-init-array",
151 cl::desc("Use .init_array instead of .ctors."),
152 cl::init(false));
153
Chad Rosierd269bd82012-08-21 16:15:24 +0000154static cl::opt<unsigned>
155SSPBufferSize("stack-protector-buffer-size", cl::init(8),
156 cl::desc("Lower bound for a buffer to be considered for "
157 "stack protection"));
158
Bill Wendlingfb440502012-03-28 20:46:54 +0000159LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
160 : _module(m), _target(t),
Bill Wendlingbc07a892013-06-18 07:20:20 +0000161 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL),
Bill Wendling70b14002013-05-29 20:37:19 +0000162 _mangler(_context, t) {}
Bill Wendlingfb440502012-03-28 20:46:54 +0000163
164/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
165/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000166bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +0000167 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
168 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000169}
170
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000171bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +0000172 sys::fs::file_magic type;
173 if (sys::fs::identify_magic(path, type))
174 return false;
175 return type == sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000176}
177
Bill Wendlingfb440502012-03-28 20:46:54 +0000178/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
179/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000180bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
181 const char *triplePrefix) {
182 MemoryBuffer *buffer = makeBuffer(mem, length);
183 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +0000184 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000185 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +0000186}
187
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000188bool LTOModule::isBitcodeFileForTarget(const char *path,
189 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000190 OwningPtr<MemoryBuffer> buffer;
191 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000192 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000193 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000194}
195
Bill Wendlingfb440502012-03-28 20:46:54 +0000196/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
197/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000198bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +0000199 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
200 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +0000201 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000202}
203
Bill Wendlingfb440502012-03-28 20:46:54 +0000204/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
205/// the buffer.
206LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000207 OwningPtr<MemoryBuffer> buffer;
208 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000209 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000210 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000211 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000212 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000213}
214
Rafael Espindola56e41f72011-02-08 22:40:47 +0000215LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Bill Wendling5f689e72011-11-04 18:48:00 +0000216 size_t size, std::string &errMsg) {
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000217 return makeLTOModule(fd, path, size, size, 0, errMsg);
218}
219
220LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
221 size_t file_size,
222 size_t map_size,
223 off_t offset,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000224 std::string &errMsg) {
225 OwningPtr<MemoryBuffer> buffer;
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000226 if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size,
227 map_size, offset, false)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000228 errMsg = ec.message();
229 return NULL;
230 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000231 return makeLTOModule(buffer.take(), errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000232}
233
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000234LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
235 std::string &errMsg) {
236 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
237 if (!buffer)
238 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000239 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000240}
241
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000242void LTOModule::getTargetOptions(TargetOptions &Options) {
243 Options.LessPreciseFPMADOption = EnableFPMAD;
244 Options.NoFramePointerElim = DisableFPElim;
245 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
246 Options.AllowFPOpFusion = FuseFPOps;
247 Options.UnsafeFPMath = EnableUnsafeFPMath;
248 Options.NoInfsFPMath = EnableNoInfsFPMath;
249 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
250 Options.HonorSignDependentRoundingFPMathOption =
251 EnableHonorSignDependentRoundingFPMath;
252 Options.UseSoftFloat = GenerateSoftFloatCalls;
253 if (FloatABIForCalls != FloatABI::Default)
254 Options.FloatABIType = FloatABIForCalls;
255 Options.NoZerosInBSS = DontPlaceZerosInBSS;
256 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
257 Options.DisableTailCalls = DisableTailCalls;
258 Options.StackAlignmentOverride = OverrideStackAlignment;
259 Options.RealignStack = EnableRealignStack;
260 Options.TrapFuncName = TrapFuncName;
261 Options.PositionIndependentExecutable = EnablePIE;
262 Options.EnableSegmentedStacks = SegmentedStacks;
263 Options.UseInitArray = UseInitArray;
Chad Rosierd269bd82012-08-21 16:15:24 +0000264 Options.SSPBufferSize = SSPBufferSize;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000265}
266
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000267LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
268 std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000269 static bool Initialized = false;
270 if (!Initialized) {
271 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000272 InitializeAllTargetMCs();
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000273 InitializeAllAsmParsers();
274 Initialized = true;
275 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000276
277 // parse bitcode buffer
Rafael Espindola5b778b22011-03-18 19:51:00 +0000278 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
279 &errMsg));
280 if (!m) {
281 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000282 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000283 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000284
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000285 std::string TripleStr = m->getTargetTriple();
286 if (TripleStr.empty())
287 TripleStr = sys::getDefaultTargetTriple();
288 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000289
290 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000291 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000292 if (!march)
293 return NULL;
294
Nick Lewycky364c04a2011-04-21 01:54:08 +0000295 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000296 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000297 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000298 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000299 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000300 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000301 if (Triple.isOSDarwin()) {
302 if (Triple.getArch() == llvm::Triple::x86_64)
303 CPU = "core2";
304 else if (Triple.getArch() == llvm::Triple::x86)
305 CPU = "yonah";
306 }
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000307 TargetOptions Options;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000308 getTargetOptions(Options);
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000309 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000310 Options);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000311 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000312 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000313 delete Ret;
314 return NULL;
315 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000316
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000317 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000318}
319
Bill Wendlingfb440502012-03-28 20:46:54 +0000320/// makeBuffer - Create a MemoryBuffer from a memory range.
321MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000322 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000323 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000324}
325
Bill Wendlingfb440502012-03-28 20:46:54 +0000326/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000327bool
328LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
329 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000330 Constant *op = ce->getOperand(0);
331 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
332 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000333 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000334 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000335 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000336 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000337 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000338 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000339 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000340 }
341 return false;
342}
343
Bill Wendlingfb440502012-03-28 20:46:54 +0000344/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000345void LTOModule::addObjCClass(const GlobalVariable *clgv) {
346 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000347 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000348
Bill Wendling5f689e72011-11-04 18:48:00 +0000349 // second slot in __OBJC,__class is pointer to superclass name
350 std::string superclassName;
351 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
352 NameAndAttributes info;
353 StringMap<NameAndAttributes>::value_type &entry =
354 _undefines.GetOrCreateValue(superclassName);
355 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000356 const char *symbolName = entry.getKey().data();
357 info.name = symbolName;
358 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000359 info.isFunction = false;
360 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000361 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000362 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000363 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000364
365 // third slot in __OBJC,__class is pointer to class name
366 std::string className;
367 if (objcClassNameFromExpression(c->getOperand(2), className)) {
368 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
369 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000370
Bill Wendling5f689e72011-11-04 18:48:00 +0000371 NameAndAttributes info;
372 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000373 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
374 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
375 info.isFunction = false;
376 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000377 _symbols.push_back(info);
378 }
379}
380
Bill Wendlingfb440502012-03-28 20:46:54 +0000381/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000382void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
383 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000384 if (!c) return;
385
386 // second slot in __OBJC,__category is pointer to target class name
387 std::string targetclassName;
388 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
389 return;
390
391 NameAndAttributes info;
392 StringMap<NameAndAttributes>::value_type &entry =
393 _undefines.GetOrCreateValue(targetclassName);
394
395 if (entry.getValue().name)
396 return;
397
398 const char *symbolName = entry.getKey().data();
399 info.name = symbolName;
400 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000401 info.isFunction = false;
402 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000403 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000404}
405
Bill Wendlingfb440502012-03-28 20:46:54 +0000406/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000407void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000408 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000409 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
410 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000411
Bill Wendling5f689e72011-11-04 18:48:00 +0000412 NameAndAttributes info;
413 StringMap<NameAndAttributes>::value_type &entry =
414 _undefines.GetOrCreateValue(targetclassName);
415 if (entry.getValue().name)
416 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000417
Bill Wendling5f689e72011-11-04 18:48:00 +0000418 const char *symbolName = entry.getKey().data();
419 info.name = symbolName;
420 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000421 info.isFunction = false;
422 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000423 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000424}
425
Bill Wendlingfb440502012-03-28 20:46:54 +0000426/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000427void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000428 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000429 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000430
Bill Wendling45f74e32012-08-06 22:52:45 +0000431 if (!v->hasSection() /* || !isTargetDarwin */)
432 return;
433
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000434 // Special case i386/ppc ObjC data structures in magic sections:
435 // The issue is that the old ObjC object format did some strange
436 // contortions to avoid real linker symbols. For instance, the
437 // ObjC class data structure is allocated statically in the executable
438 // that defines that class. That data structures contains a pointer to
439 // its superclass. But instead of just initializing that part of the
440 // struct to the address of its superclass, and letting the static and
441 // dynamic linkers do the rest, the runtime works by having that field
442 // instead point to a C-string that is the name of the superclass.
443 // At runtime the objc initialization updates that pointer and sets
444 // it to point to the actual super class. As far as the linker
445 // knows it is just a pointer to a string. But then someone wanted the
446 // linker to issue errors at build time if the superclass was not found.
447 // So they figured out a way in mach-o object format to use an absolute
448 // symbols (.objc_class_name_Foo = 0) and a floating reference
449 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
450 // a class was missing.
451 // The following synthesizes the implicit .objc_* symbols for the linker
452 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000453
Bill Wendling45f74e32012-08-06 22:52:45 +0000454 // special case if this data blob is an ObjC class definition
455 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000456 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000457 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000458 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000459 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000460
Bill Wendling45f74e32012-08-06 22:52:45 +0000461 // special case if this data blob is an ObjC category definition
462 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000463 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000464 addObjCCategory(gv);
465 }
466 }
467
468 // special case if this data blob is the list of referenced classes
469 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000470 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000471 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000472 }
473 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000474}
475
Bill Wendlingfb440502012-03-28 20:46:54 +0000476/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000477void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000478 // add to list of defined symbols
479 addDefinedSymbol(f, true);
480}
481
482/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000483void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000484 // ignore all llvm.* symbols
485 if (def->getName().startswith("llvm."))
486 return;
487
488 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000489 SmallString<64> Buffer;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000490 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000491
492 // set alignment part log2() can have rounding errors
493 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000494 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000495
496 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000497 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000498 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000499 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000500 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000501 if (gv && gv->isConstant())
502 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
503 else
504 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
505 }
506
507 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000508 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000509 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000510 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000511 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000512 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000513 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000514 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000515
516 // set scope part
517 if (def->hasHiddenVisibility())
518 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
519 else if (def->hasProtectedVisibility())
520 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000521 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
522 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
523 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000524 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Bill Wendling34bc34e2012-08-17 18:33:14 +0000525 else if (def->hasLinkOnceODRAutoHideLinkage())
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000526 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000527 else
528 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
529
Chad Rosier772a91f2011-06-28 18:26:12 +0000530 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000531 entry.setValue(1);
532
Bill Wendling9ee2d332012-03-29 08:27:32 +0000533 // fill information structure
534 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000535 StringRef Name = entry.getKey();
536 info.name = Name.data();
537 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000538 info.attributes = attr;
539 info.isFunction = isFunction;
540 info.symbol = def;
541
542 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000543 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000544}
545
Bill Wendlingfb440502012-03-28 20:46:54 +0000546/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
547/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000548void LTOModule::addAsmGlobalSymbol(const char *name,
549 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000550 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
551
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000552 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000553 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000554 return;
555
Rafael Espindola477d11f2011-02-20 16:27:25 +0000556 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000557
558 NameAndAttributes &info = _undefines[entry.getKey().data()];
559
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000560 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000561 // FIXME: This is trying to take care of module ASM like this:
562 //
563 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
564 //
565 // but is gross and its mother dresses it funny. Have the ASM parser give us
566 // more details for this type of situation so that we're not guessing so
567 // much.
568
569 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000570 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000571 info.attributes =
572 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
573 info.isFunction = false;
574 info.symbol = 0;
575
576 // add to table of symbols
577 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000578 return;
579 }
580
Bill Wendling9ee2d332012-03-29 08:27:32 +0000581 if (info.isFunction)
582 addDefinedFunctionSymbol(cast<Function>(info.symbol));
583 else
584 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000585
586 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
587 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000588}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000589
Bill Wendlingfb440502012-03-28 20:46:54 +0000590/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
591/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000592void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
593 StringMap<NameAndAttributes>::value_type &entry =
594 _undefines.GetOrCreateValue(name);
595
596 _asm_undefines.push_back(entry.getKey().data());
597
598 // we already have the symbol
599 if (entry.getValue().name)
600 return;
601
602 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
603 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
604 NameAndAttributes info;
605 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000606 info.attributes = attr;
607 info.isFunction = false;
608 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000609
610 entry.setValue(info);
611}
612
Bill Wendlingfb440502012-03-28 20:46:54 +0000613/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
614/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000615void
616LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000617 // ignore all llvm.* symbols
618 if (decl->getName().startswith("llvm."))
619 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000620
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000621 // ignore all aliases
622 if (isa<GlobalAlias>(decl))
623 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000624
Rafael Espindola34b59382011-02-11 05:23:09 +0000625 SmallString<64> name;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000626 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola56548522009-04-24 16:55:21 +0000627
Rafael Espindola477d11f2011-02-20 16:27:25 +0000628 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000629 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000630
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000631 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000632 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000633 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000634
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000635 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000636
637 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000638
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000639 if (decl->hasExternalWeakLinkage())
640 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
641 else
642 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000643
Bill Wendling9ee2d332012-03-29 08:27:32 +0000644 info.isFunction = isFunc;
645 info.symbol = decl;
646
Rafael Espindola477d11f2011-02-20 16:27:25 +0000647 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000648}
649
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000650namespace {
651 class RecordStreamer : public MCStreamer {
652 public:
Bill Wendling32867652012-04-03 03:56:52 +0000653 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000654
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000655 private:
656 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000657
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000658 void markDefined(const MCSymbol &Symbol) {
659 State &S = Symbols[Symbol.getName()];
660 switch (S) {
661 case DefinedGlobal:
662 case Global:
663 S = DefinedGlobal;
664 break;
665 case NeverSeen:
666 case Defined:
667 case Used:
668 S = Defined;
669 break;
670 }
671 }
672 void markGlobal(const MCSymbol &Symbol) {
673 State &S = Symbols[Symbol.getName()];
674 switch (S) {
675 case DefinedGlobal:
676 case Defined:
677 S = DefinedGlobal;
678 break;
679
680 case NeverSeen:
681 case Global:
682 case Used:
683 S = Global;
684 break;
685 }
686 }
687 void markUsed(const MCSymbol &Symbol) {
688 State &S = Symbols[Symbol.getName()];
689 switch (S) {
690 case DefinedGlobal:
691 case Defined:
692 case Global:
693 break;
694
695 case NeverSeen:
696 case Used:
697 S = Used;
698 break;
699 }
700 }
701
702 // FIXME: mostly copied for the obj streamer.
703 void AddValueSymbols(const MCExpr *Value) {
704 switch (Value->getKind()) {
705 case MCExpr::Target:
706 // FIXME: What should we do in here?
707 break;
708
709 case MCExpr::Constant:
710 break;
711
712 case MCExpr::Binary: {
713 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
714 AddValueSymbols(BE->getLHS());
715 AddValueSymbols(BE->getRHS());
716 break;
717 }
718
719 case MCExpr::SymbolRef:
720 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
721 break;
722
723 case MCExpr::Unary:
724 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
725 break;
726 }
727 }
728
729 public:
730 typedef StringMap<State>::const_iterator const_iterator;
731
732 const_iterator begin() {
733 return Symbols.begin();
734 }
735
736 const_iterator end() {
737 return Symbols.end();
738 }
739
Chandler Carruthde093ef2013-01-31 23:29:57 +0000740 RecordStreamer(MCContext &Context)
741 : MCStreamer(SK_RecordStreamer, Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000742
Bill Wendling32867652012-04-03 03:56:52 +0000743 virtual void EmitInstruction(const MCInst &Inst) {
744 // Scan for values.
745 for (unsigned i = Inst.getNumOperands(); i--; )
746 if (Inst.getOperand(i).isExpr())
747 AddValueSymbols(Inst.getOperand(i).getExpr());
748 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000749 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000750 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000751 markDefined(*Symbol);
752 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000753 virtual void EmitDebugLabel(MCSymbol *Symbol) {
754 EmitLabel(Symbol);
755 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000756 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
757 // FIXME: should we handle aliases?
758 markDefined(*Symbol);
759 }
760 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
761 if (Attribute == MCSA_Global)
762 markGlobal(*Symbol);
763 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000764 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000765 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000766 markDefined(*Symbol);
767 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000768 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
769 unsigned ByteAlignment) {
770 markDefined(*Symbol);
771 }
Bill Wendling32867652012-04-03 03:56:52 +0000772
Eli Benderskyf483ff92012-12-20 19:05:53 +0000773 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000774 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000775 virtual void EmitBundleUnlock() {}
776
Bill Wendling32867652012-04-03 03:56:52 +0000777 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000778 virtual void ChangeSection(const MCSection *Section,
779 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000780 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000781 virtual void InitSections() {}
782 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
783 virtual void EmitThumbFunc(MCSymbol *Func) {}
784 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
785 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
786 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
787 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
788 virtual void EmitCOFFSymbolType(int Type) {}
789 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000790 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000791 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
792 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000793 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
794 uint64_t Size, unsigned ByteAlignment) {}
795 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {}
796 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindolafd057852011-05-01 03:50:49 +0000797 unsigned AddrSpace) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000798 virtual void EmitULEB128Value(const MCExpr *Value) {}
799 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000800 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
801 unsigned ValueSize,
802 unsigned MaxBytesToEmit) {}
803 virtual void EmitCodeAlignment(unsigned ByteAlignment,
804 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000805 virtual bool EmitValueToOffset(const MCExpr *Offset,
806 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000807 virtual void EmitFileDirective(StringRef Filename) {}
808 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
809 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000810 const MCSymbol *Label,
811 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000812 virtual void FinishImpl() {}
Chandler Carruthde093ef2013-01-31 23:29:57 +0000813
814 static bool classof(const MCStreamer *S) {
Chandler Carruth30cfaa22013-01-31 23:34:47 +0000815 return S->getKind() == SK_RecordStreamer;
Chandler Carruthde093ef2013-01-31 23:29:57 +0000816 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000817 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000818} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000819
Bill Wendlingfb440502012-03-28 20:46:54 +0000820/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
821/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000822bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000823 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000824 if (inlineAsm.empty())
825 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000826
Bill Wendlingac2abde2011-11-04 09:24:40 +0000827 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000828 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
829 SourceMgr SrcMgr;
830 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000831 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000832 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000833 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000834 const Target &T = _target->getTarget();
835 OwningPtr<MCSubtargetInfo>
836 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
837 _target->getTargetCPU(),
838 _target->getTargetFeatureString()));
839 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get()));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000840 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000841 errMsg = "target " + std::string(T.getName()) +
842 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000843 return true;
844 }
845
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000846 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000847 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000848 return true;
849
850 for (RecordStreamer::const_iterator i = Streamer->begin(),
851 e = Streamer->end(); i != e; ++i) {
852 StringRef Key = i->first();
853 RecordStreamer::State Value = i->second;
854 if (Value == RecordStreamer::DefinedGlobal)
855 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
856 else if (Value == RecordStreamer::Defined)
857 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
858 else if (Value == RecordStreamer::Global ||
859 Value == RecordStreamer::Used)
860 addAsmGlobalSymbolUndef(Key.data());
861 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000862
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000863 return false;
864}
865
Bill Wendlingfb440502012-03-28 20:46:54 +0000866/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000867static bool isDeclaration(const GlobalValue &V) {
868 if (V.hasAvailableExternallyLinkage())
869 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000870
Rafael Espindola5b778b22011-03-18 19:51:00 +0000871 if (V.isMaterializable())
872 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000873
Rafael Espindola5b778b22011-03-18 19:51:00 +0000874 return V.isDeclaration();
875}
876
Bill Wendling7e58b382012-03-28 23:12:18 +0000877/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000878/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000879bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000880 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000881 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000882 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000883 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000884 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000885 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000886 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000887
Daniel Dunbar919660b2010-08-10 23:46:46 +0000888 // add data
889 for (Module::global_iterator v = _module->global_begin(),
890 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000891 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000892 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000893 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000894 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000895 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000896
Daniel Dunbar919660b2010-08-10 23:46:46 +0000897 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000898 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000899 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000900
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000901 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000902 for (Module::alias_iterator a = _module->alias_begin(),
903 e = _module->alias_end(); a != e; ++a) {
904 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000905 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000906 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000907 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000908 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000909 }
910
Daniel Dunbar919660b2010-08-10 23:46:46 +0000911 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000912 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
913 e = _undefines.end(); u != e; ++u) {
914 // If this symbol also has a definition, then don't make an undefine because
915 // it is a tentative definition.
916 if (_defines.count(u->getKey())) continue;
917 NameAndAttributes info = u->getValue();
918 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000919 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000920
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000921 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000922}