blob: 1eac22cfaf1c8106ddda7d8c60a74b720f52a674 [file] [log] [blame]
Chris Lattner5ef31a02010-03-12 18:44:54 +00001//===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
Nick Kledzik77595fc2008-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 Dunbarb06913d2010-08-10 23:46:39 +00007//
Nick Kledzik77595fc2008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Daniel Dunbarb06913d2010-08-10 23:46:39 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik77595fc2008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Nick Kledzikef194ed2008-02-27 22:25:36 +000015#include "LTOModule.h"
16
Nick Kledzik3eb445f2009-06-01 20:33:09 +000017#include "llvm/Constants.h"
Owen Anderson8b477ed2009-07-01 16:58:40 +000018#include "llvm/LLVMContext.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000019#include "llvm/Module.h"
Nick Kledzikef194ed2008-02-27 22:25:36 +000020#include "llvm/ADT/OwningPtr.h"
Viktor Kutuzove823db82009-11-18 20:20:05 +000021#include "llvm/ADT/Triple.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000022#include "llvm/Bitcode/ReaderWriter.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000023#include "llvm/Support/SystemUtils.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000024#include "llvm/Support/MemoryBuffer.h"
Nick Kledzikef194ed2008-02-27 22:25:36 +000025#include "llvm/Support/MathExtras.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000026#include "llvm/Support/Host.h"
27#include "llvm/Support/Path.h"
28#include "llvm/Support/Process.h"
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000029#include "llvm/Support/system_error.h"
Chris Lattner45111d12010-01-16 21:57:06 +000030#include "llvm/Target/Mangler.h"
Bill Wendling604a8182008-06-18 06:35:30 +000031#include "llvm/Target/SubtargetFeature.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000032#include "llvm/MC/MCAsmInfo.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000033#include "llvm/MC/MCContext.h"
Daniel Dunbarff9834a2009-07-16 02:41:19 +000034#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetRegistry.h"
Nick Lewyckyd42b58b2009-07-26 22:16:39 +000036#include "llvm/Target/TargetSelect.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000037
Nick Kledzik77595fc2008-02-26 20:26:43 +000038using namespace llvm;
39
Daniel Dunbarb06913d2010-08-10 23:46:39 +000040bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
41 return llvm::sys::IdentifyFileType((char*)mem, length)
42 == llvm::sys::Bitcode_FileType;
Nick Kledzik77595fc2008-02-26 20:26:43 +000043}
44
Daniel Dunbarb06913d2010-08-10 23:46:39 +000045bool LTOModule::isBitcodeFile(const char *path) {
46 return llvm::sys::Path(path).isBitcodeFile();
Nick Kledzik77595fc2008-02-26 20:26:43 +000047}
48
Daniel Dunbarb06913d2010-08-10 23:46:39 +000049bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
50 const char *triplePrefix) {
51 MemoryBuffer *buffer = makeBuffer(mem, length);
52 if (!buffer)
Nick Kledzik3eb445f2009-06-01 20:33:09 +000053 return false;
Daniel Dunbarb06913d2010-08-10 23:46:39 +000054 return isTargetMatch(buffer, triplePrefix);
Nick Kledzik3eb445f2009-06-01 20:33:09 +000055}
56
Daniel Dunbarb06913d2010-08-10 23:46:39 +000057
58bool LTOModule::isBitcodeFileForTarget(const char *path,
59 const char *triplePrefix) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +000060 OwningPtr<MemoryBuffer> buffer;
61 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbarb06913d2010-08-10 23:46:39 +000062 return false;
Michael J. Spencer3ff95632010-12-16 03:29:14 +000063 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbarb06913d2010-08-10 23:46:39 +000064}
65
66// Takes ownership of buffer.
67bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling34711742010-10-06 01:22:42 +000068 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
69 delete buffer;
Michael J. Spencer3ff95632010-12-16 03:29:14 +000070 return (strncmp(Triple.c_str(), triplePrefix,
Bill Wendling34711742010-10-06 01:22:42 +000071 strlen(triplePrefix)) == 0);
Daniel Dunbarb06913d2010-08-10 23:46:39 +000072}
73
74
75LTOModule::LTOModule(Module *m, TargetMachine *t)
76 : _module(m), _target(t), _symbolsParsed(false)
Nick Kledzik3eb445f2009-06-01 20:33:09 +000077{
Daniel Dunbarb06913d2010-08-10 23:46:39 +000078}
79
80LTOModule *LTOModule::makeLTOModule(const char *path,
81 std::string &errMsg) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +000082 OwningPtr<MemoryBuffer> buffer;
83 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000084 errMsg = ec.message();
Daniel Dunbarb06913d2010-08-10 23:46:39 +000085 return NULL;
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000086 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +000087 return makeLTOModule(buffer.get(), errMsg);
88}
89
Rafael Espindolab4cc0312011-02-08 22:40:47 +000090LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
91 off_t size,
92 std::string &errMsg) {
93 OwningPtr<MemoryBuffer> buffer;
94 if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, size)) {
95 errMsg = ec.message();
96 return NULL;
97 }
98 return makeLTOModule(buffer.get(), errMsg);
99}
100
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000101/// makeBuffer - Create a MemoryBuffer from a memory range. MemoryBuffer
102/// requires the byte past end of the buffer to be a zero. We might get lucky
103/// and already be that way, otherwise make a copy. Also if next byte is on a
104/// different page, don't assume it is readable.
105MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
106 const char *startPtr = (char*)mem;
107 const char *endPtr = startPtr+length;
108 if (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0 ||
109 *endPtr != 0)
110 return MemoryBuffer::getMemBufferCopy(StringRef(startPtr, length));
111
112 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length));
113}
114
115
116LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
117 std::string &errMsg) {
118 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
119 if (!buffer)
120 return NULL;
121 return makeLTOModule(buffer.get(), errMsg);
122}
123
124LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
125 std::string &errMsg) {
126 InitializeAllTargets();
127
128 // parse bitcode buffer
129 OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
130 if (!m)
131 return NULL;
132
133 std::string Triple = m->getTargetTriple();
134 if (Triple.empty())
135 Triple = sys::getHostTriple();
136
137 // find machine architecture for this module
138 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
139 if (!march)
140 return NULL;
141
142 // construct LTModule, hand over ownership of module and target
143 SubtargetFeatures Features;
144 Features.getDefaultSubtargetFeatures("" /* cpu */, llvm::Triple(Triple));
145 std::string FeatureStr = Features.getString();
146 TargetMachine *target = march->createTargetMachine(Triple, FeatureStr);
147 return new LTOModule(m.take(), target);
148}
149
150
151const char *LTOModule::getTargetTriple() {
152 return _module->getTargetTriple().c_str();
153}
154
155void LTOModule::setTargetTriple(const char *triple) {
156 _module->setTargetTriple(triple);
157}
158
159void LTOModule::addDefinedFunctionSymbol(Function *f, Mangler &mangler) {
160 // add to list of defined symbols
161 addDefinedSymbol(f, mangler, true);
162
163 // add external symbols referenced by this function.
164 for (Function::iterator b = f->begin(); b != f->end(); ++b) {
165 for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) {
166 for (unsigned count = 0, total = i->getNumOperands();
167 count != total; ++count) {
168 findExternalRefs(i->getOperand(count), mangler);
169 }
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000170 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000171 }
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000172}
173
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000174// Get string that data pointer points to.
175bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) {
176 if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
177 Constant *op = ce->getOperand(0);
178 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
179 Constant *cn = gvn->getInitializer();
180 if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) {
181 if (ca->isCString()) {
182 name = ".objc_class_name_" + ca->getAsString();
183 return true;
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000184 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000185 }
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000186 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000187 }
188 return false;
189}
190
191// Parse i386/ppc ObjC class data structure.
192void LTOModule::addObjCClass(GlobalVariable *clgv) {
193 if (ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
194 // second slot in __OBJC,__class is pointer to superclass name
195 std::string superclassName;
196 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
197 NameAndAttributes info;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000198 StringMap<NameAndAttributes>::value_type &entry =
199 _undefines.GetOrCreateValue(superclassName.c_str());
200 if (!entry.getValue().name) {
201 const char *symbolName = entry.getKey().data();
Daniel Dunbar8d0843d2010-08-11 00:11:17 +0000202 info.name = symbolName;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000203 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000204 entry.setValue(info);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000205 }
206 }
207 // third slot in __OBJC,__class is pointer to class name
208 std::string className;
209 if (objcClassNameFromExpression(c->getOperand(2), className)) {
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000210 StringSet::value_type &entry =
211 _defines.GetOrCreateValue(className.c_str());
212 entry.setValue(1);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000213 NameAndAttributes info;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000214 info.name = entry.getKey().data();
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000215 info.attributes = (lto_symbol_attributes)
216 (LTO_SYMBOL_PERMISSIONS_DATA |
217 LTO_SYMBOL_DEFINITION_REGULAR |
218 LTO_SYMBOL_SCOPE_DEFAULT);
219 _symbols.push_back(info);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000220 }
221 }
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000222}
223
224
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000225// Parse i386/ppc ObjC category data structure.
226void LTOModule::addObjCCategory(GlobalVariable *clgv) {
227 if (ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
228 // second slot in __OBJC,__category is pointer to target class name
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000229 std::string targetclassName;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000230 if (objcClassNameFromExpression(c->getOperand(1), targetclassName)) {
231 NameAndAttributes info;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000232
233 StringMap<NameAndAttributes>::value_type &entry =
234 _undefines.GetOrCreateValue(targetclassName.c_str());
235
236 if (entry.getValue().name)
237 return;
238
239 const char *symbolName = entry.getKey().data();
240 info.name = symbolName;
241 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
242 entry.setValue(info);
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000243 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000244 }
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000245}
246
247
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000248// Parse i386/ppc ObjC class list data structure.
249void LTOModule::addObjCClassRef(GlobalVariable *clgv) {
250 std::string targetclassName;
251 if (objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000252 NameAndAttributes info;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000253
254 StringMap<NameAndAttributes>::value_type &entry =
255 _undefines.GetOrCreateValue(targetclassName.c_str());
256 if (entry.getValue().name)
257 return;
258
259 const char *symbolName = entry.getKey().data();
260 info.name = symbolName;
261 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
262 entry.setValue(info);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000263 }
264}
265
266
267void LTOModule::addDefinedDataSymbol(GlobalValue *v, Mangler &mangler) {
268 // Add to list of defined symbols.
269 addDefinedSymbol(v, mangler, false);
270
271 // Special case i386/ppc ObjC data structures in magic sections:
272 // The issue is that the old ObjC object format did some strange
273 // contortions to avoid real linker symbols. For instance, the
274 // ObjC class data structure is allocated statically in the executable
275 // that defines that class. That data structures contains a pointer to
276 // its superclass. But instead of just initializing that part of the
277 // struct to the address of its superclass, and letting the static and
278 // dynamic linkers do the rest, the runtime works by having that field
279 // instead point to a C-string that is the name of the superclass.
280 // At runtime the objc initialization updates that pointer and sets
281 // it to point to the actual super class. As far as the linker
282 // knows it is just a pointer to a string. But then someone wanted the
283 // linker to issue errors at build time if the superclass was not found.
284 // So they figured out a way in mach-o object format to use an absolute
285 // symbols (.objc_class_name_Foo = 0) and a floating reference
286 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
287 // a class was missing.
288 // The following synthesizes the implicit .objc_* symbols for the linker
289 // from the ObjC data structures generated by the front end.
290 if (v->hasSection() /* && isTargetDarwin */) {
291 // special case if this data blob is an ObjC class definition
292 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
293 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
294 addObjCClass(gv);
295 }
296 }
297
298 // special case if this data blob is an ObjC category definition
299 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
300 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
301 addObjCCategory(gv);
302 }
303 }
304
305 // special case if this data blob is the list of referenced classes
306 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
307 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
308 addObjCClassRef(gv);
309 }
310 }
311 }
312
313 // add external symbols referenced by this data.
314 for (unsigned count = 0, total = v->getNumOperands();
315 count != total; ++count) {
316 findExternalRefs(v->getOperand(count), mangler);
317 }
318}
319
320
321void LTOModule::addDefinedSymbol(GlobalValue *def, Mangler &mangler,
322 bool isFunction) {
323 // ignore all llvm.* symbols
324 if (def->getName().startswith("llvm."))
325 return;
326
Rafael Espindola4cb310b2011-02-01 00:41:51 +0000327 // ignore available_externally
328 if (def->hasAvailableExternallyLinkage())
329 return;
330
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000331 // string is owned by _defines
Rafael Espindolaef1860a2011-02-11 05:23:09 +0000332 SmallString<64> Buffer;
333 mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000334
335 // set alignment part log2() can have rounding errors
336 uint32_t align = def->getAlignment();
337 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
338
339 // set permissions part
340 if (isFunction)
341 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
342 else {
343 GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
344 if (gv && gv->isConstant())
345 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
346 else
347 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
348 }
349
350 // set definition part
Bill Wendling563ef5e2010-09-27 18:05:19 +0000351 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
352 def->hasLinkerPrivateWeakLinkage() ||
Bill Wendling7afea0c2010-09-27 20:17:45 +0000353 def->hasLinkerPrivateWeakDefAutoLinkage())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000354 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendling7afea0c2010-09-27 20:17:45 +0000355 else if (def->hasCommonLinkage())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000356 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendling7afea0c2010-09-27 20:17:45 +0000357 else
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000358 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000359
360 // set scope part
361 if (def->hasHiddenVisibility())
362 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
363 else if (def->hasProtectedVisibility())
364 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Bill Wendling7afea0c2010-09-27 20:17:45 +0000365 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
366 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
367 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000368 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Bill Wendling7afea0c2010-09-27 20:17:45 +0000369 else if (def->hasLinkerPrivateWeakDefAutoLinkage())
370 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000371 else
372 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
373
374 // add to table of symbols
375 NameAndAttributes info;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000376 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer.c_str());
377 entry.setValue(1);
378
379 StringRef Name = entry.getKey();
380 info.name = Name.data();
381 assert(info.name[Name.size()] == '\0');
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000382 info.attributes = (lto_symbol_attributes)attr;
383 _symbols.push_back(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000384}
385
Devang Patelc2aec572008-07-16 18:06:52 +0000386void LTOModule::addAsmGlobalSymbol(const char *name) {
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000387 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
388
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000389 // only add new define if not already defined
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000390 if (entry.getValue())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000391 return;
392
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000393 entry.setValue(1);
394 const char *symbolName = entry.getKey().data();
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000395 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
396 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
397 NameAndAttributes info;
398 info.name = symbolName;
399 info.attributes = (lto_symbol_attributes)attr;
400 _symbols.push_back(info);
Devang Patelc2aec572008-07-16 18:06:52 +0000401}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000402
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000403void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl,
404 Mangler &mangler) {
405 // ignore all llvm.* symbols
406 if (decl->getName().startswith("llvm."))
407 return;
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000408
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000409 // ignore all aliases
410 if (isa<GlobalAlias>(decl))
411 return;
Nick Lewycky485ded02009-07-09 06:03:04 +0000412
Rafael Espindolaef1860a2011-02-11 05:23:09 +0000413 SmallString<64> name;
414 mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola7431af02009-04-24 16:55:21 +0000415
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000416 StringMap<NameAndAttributes>::value_type &entry =
417 _undefines.GetOrCreateValue(name.c_str());
418
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000419 // we already have the symbol
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000420 if (entry.getValue().name)
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000421 return;
Rafael Espindola7431af02009-04-24 16:55:21 +0000422
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000423 NameAndAttributes info;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000424
425 info.name = entry.getKey().data();
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000426 if (decl->hasExternalWeakLinkage())
427 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
428 else
429 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000430
431 entry.setValue(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000432}
433
434
435
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000436// Find external symbols referenced by VALUE. This is a recursive function.
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000437void LTOModule::findExternalRefs(Value *value, Mangler &mangler) {
438 if (GlobalValue *gv = dyn_cast<GlobalValue>(value)) {
439 if (!gv->hasExternalLinkage())
440 addPotentialUndefinedSymbol(gv, mangler);
441 // If this is a variable definition, do not recursively process
442 // initializer. It might contain a reference to this variable
443 // and cause an infinite loop. The initializer will be
444 // processed in addDefinedDataSymbol().
445 return;
446 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000447
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000448 // GlobalValue, even with InternalLinkage type, may have operands with
449 // ExternalLinkage type. Do not ignore these operands.
450 if (Constant *c = dyn_cast<Constant>(value)) {
451 // Handle ConstantExpr, ConstantStruct, ConstantArry etc.
452 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
453 findExternalRefs(c->getOperand(i), mangler);
454 }
455}
456
457void LTOModule::lazyParseSymbols() {
Daniel Dunbare41d9002010-08-10 23:46:46 +0000458 if (_symbolsParsed)
459 return;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000460
Daniel Dunbare41d9002010-08-10 23:46:46 +0000461 _symbolsParsed = true;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000462
Daniel Dunbare41d9002010-08-10 23:46:46 +0000463 // Use mangler to add GlobalPrefix to names to match linker names.
Rafael Espindola89b93722010-12-10 07:39:47 +0000464 MCContext Context(*_target->getMCAsmInfo(), NULL);
Daniel Dunbare41d9002010-08-10 23:46:46 +0000465 Mangler mangler(Context, *_target->getTargetData());
Gabor Greif4136e7b2009-09-23 02:46:12 +0000466
Daniel Dunbare41d9002010-08-10 23:46:46 +0000467 // add functions
468 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
469 if (f->isDeclaration())
470 addPotentialUndefinedSymbol(f, mangler);
471 else
472 addDefinedFunctionSymbol(f, mangler);
473 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000474
Daniel Dunbare41d9002010-08-10 23:46:46 +0000475 // add data
476 for (Module::global_iterator v = _module->global_begin(),
477 e = _module->global_end(); v != e; ++v) {
478 if (v->isDeclaration())
479 addPotentialUndefinedSymbol(v, mangler);
480 else
481 addDefinedDataSymbol(v, mangler);
482 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000483
Daniel Dunbare41d9002010-08-10 23:46:46 +0000484 // add asm globals
485 const std::string &inlineAsm = _module->getModuleInlineAsm();
486 const std::string glbl = ".globl";
487 std::string asmSymbolName;
488 std::string::size_type pos = inlineAsm.find(glbl, 0);
489 while (pos != std::string::npos) {
490 // eat .globl
491 pos = pos + 6;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000492
Daniel Dunbare41d9002010-08-10 23:46:46 +0000493 // skip white space between .globl and symbol name
494 std::string::size_type pbegin = inlineAsm.find_first_not_of(' ', pos);
495 if (pbegin == std::string::npos)
496 break;
Devang Patelc2aec572008-07-16 18:06:52 +0000497
Daniel Dunbare41d9002010-08-10 23:46:46 +0000498 // find end-of-line
499 std::string::size_type pend = inlineAsm.find_first_of('\n', pbegin);
500 if (pend == std::string::npos)
501 break;
Devang Patelc2aec572008-07-16 18:06:52 +0000502
Daniel Dunbare41d9002010-08-10 23:46:46 +0000503 asmSymbolName.assign(inlineAsm, pbegin, pend - pbegin);
504 addAsmGlobalSymbol(asmSymbolName.c_str());
Devang Patelc2aec572008-07-16 18:06:52 +0000505
Daniel Dunbare41d9002010-08-10 23:46:46 +0000506 // search next .globl
507 pos = inlineAsm.find(glbl, pend);
508 }
509
Rafael Espindola02003ca2010-10-20 04:57:22 +0000510 // add aliases
511 for (Module::alias_iterator i = _module->alias_begin(),
512 e = _module->alias_end(); i != e; ++i) {
513 if (i->isDeclaration())
514 addPotentialUndefinedSymbol(i, mangler);
515 else
516 addDefinedDataSymbol(i, mangler);
517 }
518
Daniel Dunbare41d9002010-08-10 23:46:46 +0000519 // make symbols for all undefines
520 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
521 it != _undefines.end(); ++it) {
522 // if this symbol also has a definition, then don't make an undefine
523 // because it is a tentative definition
524 if (_defines.count(it->getKey()) == 0) {
525 NameAndAttributes info = it->getValue();
526 _symbols.push_back(info);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000527 }
528 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000529}
530
531
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000532uint32_t LTOModule::getSymbolCount() {
533 lazyParseSymbols();
534 return _symbols.size();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000535}
536
537
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000538lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) {
539 lazyParseSymbols();
540 if (index < _symbols.size())
541 return _symbols[index].attributes;
542 else
543 return lto_symbol_attributes(0);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000544}
545
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000546const char *LTOModule::getSymbolName(uint32_t index) {
547 lazyParseSymbols();
548 if (index < _symbols.size())
549 return _symbols[index].name;
550 else
551 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000552}