blob: 4a2c5ad1dc3dcac9c6cc81c99ad462cd1a961ba1 [file] [log] [blame]
Nick Kledzik77595fc2008-02-26 20:26:43 +00001//===-LTOModule.cpp - LLVM Link Time Optimizer ----------------------------===//
2//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Link Time Optimization library. This library is
11// 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 Kledzik77595fc2008-02-26 20:26:43 +000020#include "llvm/ModuleProvider.h"
Nick Kledzikef194ed2008-02-27 22:25:36 +000021#include "llvm/ADT/OwningPtr.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"
24#include "llvm/Support/Mangler.h"
25#include "llvm/Support/MemoryBuffer.h"
Nick Kledzikef194ed2008-02-27 22:25:36 +000026#include "llvm/Support/MathExtras.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000027#include "llvm/System/Path.h"
Nick Kledzik90dcff72008-05-09 01:09:59 +000028#include "llvm/System/Process.h"
Bill Wendling604a8182008-06-18 06:35:30 +000029#include "llvm/Target/SubtargetFeature.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000030#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbarff9834a2009-07-16 02:41:19 +000031#include "llvm/Target/TargetMachine.h"
32#include "llvm/Target/TargetRegistry.h"
Nick Lewyckyd42b58b2009-07-26 22:16:39 +000033#include "llvm/Target/TargetSelect.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000034
Nick Kledzik77595fc2008-02-26 20:26:43 +000035using namespace llvm;
36
37bool LTOModule::isBitcodeFile(const void* mem, size_t length)
38{
39 return ( llvm::sys::IdentifyFileType((char*)mem, length)
40 == llvm::sys::Bitcode_FileType );
41}
42
43bool LTOModule::isBitcodeFile(const char* path)
44{
45 return llvm::sys::Path(path).isBitcodeFile();
46}
47
Chris Lattner038112a2008-04-01 18:04:03 +000048bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length,
49 const char* triplePrefix)
Nick Kledzik77595fc2008-02-26 20:26:43 +000050{
Nick Kledzik90dcff72008-05-09 01:09:59 +000051 MemoryBuffer* buffer = makeBuffer(mem, length);
Nick Kledzikef194ed2008-02-27 22:25:36 +000052 if ( buffer == NULL )
53 return false;
54 return isTargetMatch(buffer, triplePrefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +000055}
56
Nick Kledzikef194ed2008-02-27 22:25:36 +000057
Nick Kledzik77595fc2008-02-26 20:26:43 +000058bool LTOModule::isBitcodeFileForTarget(const char* path,
Chris Lattner038112a2008-04-01 18:04:03 +000059 const char* triplePrefix)
Nick Kledzik77595fc2008-02-26 20:26:43 +000060{
Chris Lattner038112a2008-04-01 18:04:03 +000061 MemoryBuffer *buffer = MemoryBuffer::getFile(path);
62 if (buffer == NULL)
Nick Kledzikef194ed2008-02-27 22:25:36 +000063 return false;
64 return isTargetMatch(buffer, triplePrefix);
65}
66
67// takes ownership of buffer
68bool LTOModule::isTargetMatch(MemoryBuffer* buffer, const char* triplePrefix)
69{
Owen Anderson8b477ed2009-07-01 16:58:40 +000070 OwningPtr<ModuleProvider> mp(getBitcodeModuleProvider(buffer,
Owen Anderson0e7a5462009-07-02 00:31:14 +000071 getGlobalContext()));
Nick Kledzikef194ed2008-02-27 22:25:36 +000072 // on success, mp owns buffer and both are deleted at end of this method
73 if ( !mp ) {
74 delete buffer;
75 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +000076 }
Nick Kledzikef194ed2008-02-27 22:25:36 +000077 std::string actualTarget = mp->getModule()->getTargetTriple();
78 return ( strncmp(actualTarget.c_str(), triplePrefix,
79 strlen(triplePrefix)) == 0);
Nick Kledzik77595fc2008-02-26 20:26:43 +000080}
81
82
83LTOModule::LTOModule(Module* m, TargetMachine* t)
84 : _module(m), _target(t), _symbolsParsed(false)
85{
86}
87
Owen Anderson31895e72009-07-01 21:22:36 +000088LTOModule* LTOModule::makeLTOModule(const char* path,
Owen Anderson8b477ed2009-07-01 16:58:40 +000089 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +000090{
Chris Lattner038112a2008-04-01 18:04:03 +000091 OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, &errMsg));
Nick Kledzikef194ed2008-02-27 22:25:36 +000092 if ( !buffer )
93 return NULL;
Owen Anderson0e7a5462009-07-02 00:31:14 +000094 return makeLTOModule(buffer.get(), errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +000095}
96
Nick Kledzik6b89d922008-05-09 18:44:41 +000097/// makeBuffer - create a MemoryBuffer from a memory range.
98/// MemoryBuffer requires the byte past end of the buffer to be a zero.
99/// We might get lucky and already be that way, otherwise make a copy.
100/// Also if next byte is on a different page, don't assume it is readable.
Nick Kledzik90dcff72008-05-09 01:09:59 +0000101MemoryBuffer* LTOModule::makeBuffer(const void* mem, size_t length)
102{
Nick Kledziked185d62008-05-28 00:06:14 +0000103 const char* startPtr = (char*)mem;
104 const char* endPtr = startPtr+length;
105 if ( (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0)
106 || (*endPtr != 0) )
107 return MemoryBuffer::getMemBufferCopy(startPtr, endPtr);
108 else
109 return MemoryBuffer::getMemBuffer(startPtr, endPtr);
Nick Kledzik90dcff72008-05-09 01:09:59 +0000110}
111
112
Nick Kledzik77595fc2008-02-26 20:26:43 +0000113LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length,
Nick Lewyckyb454eab2009-02-06 07:01:00 +0000114 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000115{
Nick Kledzik90dcff72008-05-09 01:09:59 +0000116 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
Nick Kledzikef194ed2008-02-27 22:25:36 +0000117 if ( !buffer )
118 return NULL;
Owen Anderson0e7a5462009-07-02 00:31:14 +0000119 return makeLTOModule(buffer.get(), errMsg);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000120}
121
Bill Wendlinge4242542008-06-18 21:39:02 +0000122/// getFeatureString - Return a string listing the features associated with the
123/// target triple.
124///
125/// FIXME: This is an inelegant way of specifying the features of a
126/// subtarget. It would be better if we could encode this information into the
127/// IR. See <rdar://5972456>.
128std::string getFeatureString(const char *TargetTriple) {
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000129 InitializeAllTargets();
130
Bill Wendlinge4242542008-06-18 21:39:02 +0000131 SubtargetFeatures Features;
132
133 if (strncmp(TargetTriple, "powerpc-apple-", 14) == 0) {
134 Features.AddFeature("altivec", true);
135 } else if (strncmp(TargetTriple, "powerpc64-apple-", 16) == 0) {
136 Features.AddFeature("64bit", true);
137 Features.AddFeature("altivec", true);
138 }
139
140 return Features.getString();
141}
142
Owen Anderson31895e72009-07-01 21:22:36 +0000143LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer,
Owen Anderson8b477ed2009-07-01 16:58:40 +0000144 std::string& errMsg)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000145{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000146 InitializeAllTargets();
147
Nick Kledzikef194ed2008-02-27 22:25:36 +0000148 // parse bitcode buffer
Owen Anderson0e7a5462009-07-02 00:31:14 +0000149 OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
Nick Kledzikef194ed2008-02-27 22:25:36 +0000150 if ( !m )
151 return NULL;
152 // find machine architecture for this module
Daniel Dunbara5881e32009-07-26 02:12:58 +0000153 const Target* march = TargetRegistry::lookupTarget(m->getTargetTriple(),
154 /*FallbackToHost=*/true,
155 /*RequireJIT=*/false,
156 errMsg);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000157 if ( march == NULL )
158 return NULL;
Bill Wendling604a8182008-06-18 06:35:30 +0000159
Nick Kledzikef194ed2008-02-27 22:25:36 +0000160 // construct LTModule, hand over ownership of module and target
Bill Wendlinge4242542008-06-18 21:39:02 +0000161 std::string FeatureStr = getFeatureString(m->getTargetTriple().c_str());
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000162 TargetMachine* target = march->createTargetMachine(*m, FeatureStr);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000163 return new LTOModule(m.take(), target);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000164}
165
166
167const char* LTOModule::getTargetTriple()
168{
169 return _module->getTargetTriple().c_str();
170}
171
Nick Kledzikef194ed2008-02-27 22:25:36 +0000172void LTOModule::addDefinedFunctionSymbol(Function* f, Mangler &mangler)
173{
174 // add to list of defined symbols
175 addDefinedSymbol(f, mangler, true);
176
177 // add external symbols referenced by this function.
178 for (Function::iterator b = f->begin(); b != f->end(); ++b) {
179 for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) {
180 for (unsigned count = 0, total = i->getNumOperands();
181 count != total; ++count) {
182 findExternalRefs(i->getOperand(count), mangler);
183 }
184 }
185 }
186}
187
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000188// get string that data pointer points to
189bool LTOModule::objcClassNameFromExpression(Constant* c, std::string& name)
190{
191 if (ConstantExpr* ce = dyn_cast<ConstantExpr>(c)) {
192 Constant* op = ce->getOperand(0);
193 if (GlobalVariable* gvn = dyn_cast<GlobalVariable>(op)) {
194 Constant* cn = gvn->getInitializer();
195 if (ConstantArray* ca = dyn_cast<ConstantArray>(cn)) {
Owen Anderson1ca29d32009-07-13 21:27:19 +0000196 if ( ca->isCString() ) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000197 name = ".objc_class_name_" + ca->getAsString();
198 return true;
199 }
200 }
201 }
202 }
203 return false;
204}
205
206// parse i386/ppc ObjC class data structure
207void LTOModule::addObjCClass(GlobalVariable* clgv)
208{
209 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
210 // second slot in __OBJC,__class is pointer to superclass name
211 std::string superclassName;
212 if ( objcClassNameFromExpression(c->getOperand(1), superclassName) ) {
213 NameAndAttributes info;
214 if ( _undefines.find(superclassName.c_str()) == _undefines.end() ) {
215 const char* symbolName = ::strdup(superclassName.c_str());
216 info.name = ::strdup(symbolName);
217 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
218 // string is owned by _undefines
219 _undefines[info.name] = info;
220 }
221 }
222 // third slot in __OBJC,__class is pointer to class name
223 std::string className;
224 if ( objcClassNameFromExpression(c->getOperand(2), className) ) {
225 const char* symbolName = ::strdup(className.c_str());
226 NameAndAttributes info;
227 info.name = symbolName;
228 info.attributes = (lto_symbol_attributes)
229 (LTO_SYMBOL_PERMISSIONS_DATA |
230 LTO_SYMBOL_DEFINITION_REGULAR |
231 LTO_SYMBOL_SCOPE_DEFAULT);
232 _symbols.push_back(info);
233 _defines[info.name] = 1;
234 }
235 }
236}
237
238
239// parse i386/ppc ObjC category data structure
240void LTOModule::addObjCCategory(GlobalVariable* clgv)
241{
242 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
243 // second slot in __OBJC,__category is pointer to target class name
244 std::string targetclassName;
245 if ( objcClassNameFromExpression(c->getOperand(1), targetclassName) ) {
246 NameAndAttributes info;
247 if ( _undefines.find(targetclassName.c_str()) == _undefines.end() ){
248 const char* symbolName = ::strdup(targetclassName.c_str());
249 info.name = ::strdup(symbolName);
250 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
251 // string is owned by _undefines
252 _undefines[info.name] = info;
253 }
254 }
255 }
256}
257
258
259// parse i386/ppc ObjC class list data structure
260void LTOModule::addObjCClassRef(GlobalVariable* clgv)
261{
262 std::string targetclassName;
263 if ( objcClassNameFromExpression(clgv->getInitializer(), targetclassName) ){
264 NameAndAttributes info;
265 if ( _undefines.find(targetclassName.c_str()) == _undefines.end() ) {
266 const char* symbolName = ::strdup(targetclassName.c_str());
267 info.name = ::strdup(symbolName);
268 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
269 // string is owned by _undefines
270 _undefines[info.name] = info;
271 }
272 }
273}
274
275
276void LTOModule::addDefinedDataSymbol(GlobalValue* v, Mangler& mangler)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000277{
278 // add to list of defined symbols
279 addDefinedSymbol(v, mangler, false);
280
Nick Kledzik4bdf7302009-06-01 23:41:09 +0000281 // Special case i386/ppc ObjC data structures in magic sections:
282 // The issue is that the old ObjC object format did some strange
283 // contortions to avoid real linker symbols. For instance, the
284 // ObjC class data structure is allocated statically in the executable
285 // that defines that class. That data structures contains a pointer to
286 // its superclass. But instead of just initializing that part of the
287 // struct to the address of its superclass, and letting the static and
288 // dynamic linkers do the rest, the runtime works by having that field
289 // instead point to a C-string that is the name of the superclass.
290 // At runtime the objc initialization updates that pointer and sets
291 // it to point to the actual super class. As far as the linker
292 // knows it is just a pointer to a string. But then someone wanted the
293 // linker to issue errors at build time if the superclass was not found.
294 // So they figured out a way in mach-o object format to use an absolute
295 // symbols (.objc_class_name_Foo = 0) and a floating reference
296 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
297 // a class was missing.
298 // The following synthesizes the implicit .objc_* symbols for the linker
299 // from the ObjC data structures generated by the front end.
300 if ( v->hasSection() /* && isTargetDarwin */ ) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000301 // special case if this data blob is an ObjC class definition
302 if ( v->getSection().compare(0, 15, "__OBJC,__class,") == 0 ) {
303 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
304 addObjCClass(gv);
305 }
306 }
307
308 // special case if this data blob is an ObjC category definition
309 else if ( v->getSection().compare(0, 18, "__OBJC,__category,") == 0 ) {
310 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
311 addObjCCategory(gv);
312 }
313 }
314
315 // special case if this data blob is the list of referenced classes
316 else if ( v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0 ) {
317 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
318 addObjCClassRef(gv);
319 }
320 }
321 }
322
Nick Kledzikef194ed2008-02-27 22:25:36 +0000323 // add external symbols referenced by this data.
Nick Kledzik9178a652008-05-27 22:07:08 +0000324 for (unsigned count = 0, total = v->getNumOperands();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000325 count != total; ++count) {
326 findExternalRefs(v->getOperand(count), mangler);
327 }
328}
329
330
Nick Kledzik77595fc2008-02-26 20:26:43 +0000331void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler,
Nick Lewycky485ded02009-07-09 06:03:04 +0000332 bool isFunction)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000333{
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000334 // ignore all llvm.* symbols
Daniel Dunbar460f6562009-07-26 09:48:23 +0000335 if (def->getName().startswith("llvm."))
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000336 return;
337
Nick Kledzikef194ed2008-02-27 22:25:36 +0000338 // string is owned by _defines
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000339 const char* symbolName = ::strdup(mangler.getMangledName(def).c_str());
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000340
Nick Kledzik77595fc2008-02-26 20:26:43 +0000341 // set alignment part log2() can have rounding errors
342 uint32_t align = def->getAlignment();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000343 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000344
345 // set permissions part
346 if ( isFunction )
347 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
348 else {
349 GlobalVariable* gv = dyn_cast<GlobalVariable>(def);
350 if ( (gv != NULL) && gv->isConstant() )
351 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
352 else
353 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
354 }
355
356 // set definition part
357 if ( def->hasWeakLinkage() || def->hasLinkOnceLinkage() ) {
Dale Johannesened1ec3a2008-05-23 00:15:10 +0000358 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000359 }
Dale Johannesen6a6f2dd2008-05-16 22:46:40 +0000360 else if ( def->hasCommonLinkage()) {
361 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
362 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000363 else {
364 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
365 }
366
367 // set scope part
368 if ( def->hasHiddenVisibility() )
369 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
Nick Lewycky4fd40e82008-11-29 22:49:59 +0000370 else if ( def->hasProtectedVisibility() )
371 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Devang Patelf0d286b2008-07-15 00:00:11 +0000372 else if ( def->hasExternalLinkage() || def->hasWeakLinkage()
Nick Kledzikdb6535d2008-07-19 00:58:07 +0000373 || def->hasLinkOnceLinkage() || def->hasCommonLinkage() )
Nick Kledzik77595fc2008-02-26 20:26:43 +0000374 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
375 else
376 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
377
378 // add to table of symbols
379 NameAndAttributes info;
380 info.name = symbolName;
381 info.attributes = (lto_symbol_attributes)attr;
382 _symbols.push_back(info);
383 _defines[info.name] = 1;
384}
385
Devang Patelc2aec572008-07-16 18:06:52 +0000386void LTOModule::addAsmGlobalSymbol(const char *name) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000387 // only add new define if not already defined
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000388 if ( _defines.count(name) == 0 )
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000389 return;
390
391 // string is owned by _defines
392 const char *symbolName = ::strdup(name);
393 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
394 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
395 NameAndAttributes info;
396 info.name = symbolName;
397 info.attributes = (lto_symbol_attributes)attr;
398 _symbols.push_back(info);
399 _defines[info.name] = 1;
Devang Patelc2aec572008-07-16 18:06:52 +0000400}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000401
Nick Kledzikef194ed2008-02-27 22:25:36 +0000402void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler)
403{
Nick Kledzik77595fc2008-02-26 20:26:43 +0000404 // ignore all llvm.* symbols
Daniel Dunbar460f6562009-07-26 09:48:23 +0000405 if (decl->getName().startswith("llvm."))
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000406 return;
407
Nick Lewycky485ded02009-07-09 06:03:04 +0000408 // ignore all aliases
409 if (isa<GlobalAlias>(decl))
410 return;
411
Nick Lewyckydb1e9982009-07-28 06:53:50 +0000412 std::string name = mangler.getMangledName(decl);
Rafael Espindola7431af02009-04-24 16:55:21 +0000413
414 // we already have the symbol
415 if (_undefines.find(name) != _undefines.end())
416 return;
417
418 NameAndAttributes info;
419 // string is owned by _undefines
Nick Lewyckydb1e9982009-07-28 06:53:50 +0000420 info.name = ::strdup(name.c_str());
Rafael Espindola7431af02009-04-24 16:55:21 +0000421 if (decl->hasExternalWeakLinkage())
422 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
423 else
424 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
425 _undefines[name] = info;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000426}
427
428
429
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000430// Find external symbols referenced by VALUE. This is a recursive function.
Nick Kledzik77595fc2008-02-26 20:26:43 +0000431void LTOModule::findExternalRefs(Value* value, Mangler &mangler) {
432
433 if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) {
434 if ( !gv->hasExternalLinkage() )
Nick Kledzikef194ed2008-02-27 22:25:36 +0000435 addPotentialUndefinedSymbol(gv, mangler);
Nick Kledziked185d62008-05-28 00:06:14 +0000436 // If this is a variable definition, do not recursively process
437 // initializer. It might contain a reference to this variable
438 // and cause an infinite loop. The initializer will be
439 // processed in addDefinedDataSymbol().
440 return;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000441 }
Nick Kledziked185d62008-05-28 00:06:14 +0000442
Nick Kledzik77595fc2008-02-26 20:26:43 +0000443 // GlobalValue, even with InternalLinkage type, may have operands with
444 // ExternalLinkage type. Do not ignore these operands.
445 if (Constant* c = dyn_cast<Constant>(value)) {
446 // Handle ConstantExpr, ConstantStruct, ConstantArry etc..
447 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
448 findExternalRefs(c->getOperand(i), mangler);
449 }
450}
451
Nick Kledzikef194ed2008-02-27 22:25:36 +0000452void LTOModule::lazyParseSymbols()
Nick Kledzik77595fc2008-02-26 20:26:43 +0000453{
454 if ( !_symbolsParsed ) {
455 _symbolsParsed = true;
456
457 // Use mangler to add GlobalPrefix to names to match linker names.
458 Mangler mangler(*_module, _target->getTargetAsmInfo()->getGlobalPrefix());
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000459 // add chars used in ObjC method names so method names aren't mangled
460 mangler.markCharAcceptable('[');
461 mangler.markCharAcceptable(']');
462 mangler.markCharAcceptable('(');
463 mangler.markCharAcceptable(')');
464 mangler.markCharAcceptable('-');
465 mangler.markCharAcceptable('+');
466 mangler.markCharAcceptable(' ');
Nick Kledzik77595fc2008-02-26 20:26:43 +0000467
468 // add functions
469 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000470 if ( f->isDeclaration() )
471 addPotentialUndefinedSymbol(f, mangler);
472 else
473 addDefinedFunctionSymbol(f, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000474 }
475
476 // add data
477 for (Module::global_iterator v = _module->global_begin(),
478 e = _module->global_end(); v != e; ++v) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000479 if ( v->isDeclaration() )
480 addPotentialUndefinedSymbol(v, mangler);
481 else
482 addDefinedDataSymbol(v, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000483 }
484
Devang Patelc2aec572008-07-16 18:06:52 +0000485 // add asm globals
486 const std::string &inlineAsm = _module->getModuleInlineAsm();
487 const std::string glbl = ".globl";
488 std::string asmSymbolName;
489 std::string::size_type pos = inlineAsm.find(glbl, 0);
490 while (pos != std::string::npos) {
491 // eat .globl
492 pos = pos + 6;
493
494 // skip white space between .globl and symbol name
495 std::string::size_type pbegin = inlineAsm.find_first_not_of(' ', pos);
496 if (pbegin == std::string::npos)
497 break;
498
499 // find end-of-line
500 std::string::size_type pend = inlineAsm.find_first_of('\n', pbegin);
501 if (pend == std::string::npos)
502 break;
503
Devang Patel41390702008-07-16 19:49:09 +0000504 asmSymbolName.assign(inlineAsm, pbegin, pend - pbegin);
Devang Patelc2aec572008-07-16 18:06:52 +0000505 addAsmGlobalSymbol(asmSymbolName.c_str());
506
507 // search next .globl
508 pos = inlineAsm.find(glbl, pend);
509 }
510
Nick Kledzik77595fc2008-02-26 20:26:43 +0000511 // make symbols for all undefines
Rafael Espindola7431af02009-04-24 16:55:21 +0000512 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000513 it != _undefines.end(); ++it) {
514 // if this symbol also has a definition, then don't make an undefine
515 // because it is a tentative definition
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000516 if ( _defines.count(it->getKey()) == 0 ) {
Rafael Espindola7431af02009-04-24 16:55:21 +0000517 NameAndAttributes info = it->getValue();
518 _symbols.push_back(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000519 }
520 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000521 }
522}
523
524
525uint32_t LTOModule::getSymbolCount()
526{
527 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000528 return _symbols.size();
529}
530
531
532lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index)
533{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000534 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000535 if ( index < _symbols.size() )
536 return _symbols[index].attributes;
537 else
538 return lto_symbol_attributes(0);
539}
540
541const char* LTOModule::getSymbolName(uint32_t index)
542{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000543 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000544 if ( index < _symbols.size() )
545 return _symbols[index].name;
546 else
547 return NULL;
548}