blob: be6543c8f7fab412af0cd3ee8d49159902cefdc9 [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/TargetMachine.h"
31#include "llvm/Target/TargetMachineRegistry.h"
32#include "llvm/Target/TargetAsmInfo.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000033
Nick Kledzik77595fc2008-02-26 20:26:43 +000034#include <fstream>
35
36using namespace llvm;
37
38bool LTOModule::isBitcodeFile(const void* mem, size_t length)
39{
40 return ( llvm::sys::IdentifyFileType((char*)mem, length)
41 == llvm::sys::Bitcode_FileType );
42}
43
44bool LTOModule::isBitcodeFile(const char* path)
45{
46 return llvm::sys::Path(path).isBitcodeFile();
47}
48
Chris Lattner038112a2008-04-01 18:04:03 +000049bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length,
50 const char* triplePrefix)
Nick Kledzik77595fc2008-02-26 20:26:43 +000051{
Nick Kledzik90dcff72008-05-09 01:09:59 +000052 MemoryBuffer* buffer = makeBuffer(mem, length);
Nick Kledzikef194ed2008-02-27 22:25:36 +000053 if ( buffer == NULL )
54 return false;
55 return isTargetMatch(buffer, triplePrefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +000056}
57
Nick Kledzikef194ed2008-02-27 22:25:36 +000058
Nick Kledzik77595fc2008-02-26 20:26:43 +000059bool LTOModule::isBitcodeFileForTarget(const char* path,
Chris Lattner038112a2008-04-01 18:04:03 +000060 const char* triplePrefix)
Nick Kledzik77595fc2008-02-26 20:26:43 +000061{
Chris Lattner038112a2008-04-01 18:04:03 +000062 MemoryBuffer *buffer = MemoryBuffer::getFile(path);
63 if (buffer == NULL)
Nick Kledzikef194ed2008-02-27 22:25:36 +000064 return false;
65 return isTargetMatch(buffer, triplePrefix);
66}
67
68// takes ownership of buffer
69bool LTOModule::isTargetMatch(MemoryBuffer* buffer, const char* triplePrefix)
70{
Owen Anderson8b477ed2009-07-01 16:58:40 +000071 OwningPtr<ModuleProvider> mp(getBitcodeModuleProvider(buffer,
Owen Anderson0e7a5462009-07-02 00:31:14 +000072 getGlobalContext()));
Nick Kledzikef194ed2008-02-27 22:25:36 +000073 // on success, mp owns buffer and both are deleted at end of this method
74 if ( !mp ) {
75 delete buffer;
76 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +000077 }
Nick Kledzikef194ed2008-02-27 22:25:36 +000078 std::string actualTarget = mp->getModule()->getTargetTriple();
79 return ( strncmp(actualTarget.c_str(), triplePrefix,
80 strlen(triplePrefix)) == 0);
Nick Kledzik77595fc2008-02-26 20:26:43 +000081}
82
83
84LTOModule::LTOModule(Module* m, TargetMachine* t)
85 : _module(m), _target(t), _symbolsParsed(false)
86{
87}
88
Owen Anderson31895e72009-07-01 21:22:36 +000089LTOModule* LTOModule::makeLTOModule(const char* path,
Owen Anderson8b477ed2009-07-01 16:58:40 +000090 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +000091{
Chris Lattner038112a2008-04-01 18:04:03 +000092 OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, &errMsg));
Nick Kledzikef194ed2008-02-27 22:25:36 +000093 if ( !buffer )
94 return NULL;
Owen Anderson0e7a5462009-07-02 00:31:14 +000095 return makeLTOModule(buffer.get(), errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +000096}
97
Nick Kledzik6b89d922008-05-09 18:44:41 +000098/// makeBuffer - create a MemoryBuffer from a memory range.
99/// MemoryBuffer requires the byte past end of the buffer to be a zero.
100/// We might get lucky and already be that way, otherwise make a copy.
101/// Also if next byte is on a different page, don't assume it is readable.
Nick Kledzik90dcff72008-05-09 01:09:59 +0000102MemoryBuffer* LTOModule::makeBuffer(const void* mem, size_t length)
103{
Nick Kledziked185d62008-05-28 00:06:14 +0000104 const char* startPtr = (char*)mem;
105 const char* endPtr = startPtr+length;
106 if ( (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0)
107 || (*endPtr != 0) )
108 return MemoryBuffer::getMemBufferCopy(startPtr, endPtr);
109 else
110 return MemoryBuffer::getMemBuffer(startPtr, endPtr);
Nick Kledzik90dcff72008-05-09 01:09:59 +0000111}
112
113
Nick Kledzik77595fc2008-02-26 20:26:43 +0000114LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length,
Nick Lewyckyb454eab2009-02-06 07:01:00 +0000115 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000116{
Nick Kledzik90dcff72008-05-09 01:09:59 +0000117 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
Nick Kledzikef194ed2008-02-27 22:25:36 +0000118 if ( !buffer )
119 return NULL;
Owen Anderson0e7a5462009-07-02 00:31:14 +0000120 return makeLTOModule(buffer.get(), errMsg);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000121}
122
Bill Wendlinge4242542008-06-18 21:39:02 +0000123/// getFeatureString - Return a string listing the features associated with the
124/// target triple.
125///
126/// FIXME: This is an inelegant way of specifying the features of a
127/// subtarget. It would be better if we could encode this information into the
128/// IR. See <rdar://5972456>.
129std::string getFeatureString(const char *TargetTriple) {
130 SubtargetFeatures Features;
131
132 if (strncmp(TargetTriple, "powerpc-apple-", 14) == 0) {
133 Features.AddFeature("altivec", true);
134 } else if (strncmp(TargetTriple, "powerpc64-apple-", 16) == 0) {
135 Features.AddFeature("64bit", true);
136 Features.AddFeature("altivec", true);
137 }
138
139 return Features.getString();
140}
141
Owen Anderson31895e72009-07-01 21:22:36 +0000142LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer,
Owen Anderson8b477ed2009-07-01 16:58:40 +0000143 std::string& errMsg)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000144{
145 // parse bitcode buffer
Owen Anderson0e7a5462009-07-02 00:31:14 +0000146 OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
Nick Kledzikef194ed2008-02-27 22:25:36 +0000147 if ( !m )
148 return NULL;
149 // find machine architecture for this module
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000150 const Target* march =
151 TargetRegistry::getClosestStaticTargetForModule(*m, errMsg);
Bill Wendling604a8182008-06-18 06:35:30 +0000152
Nick Kledzikef194ed2008-02-27 22:25:36 +0000153 if ( march == NULL )
154 return NULL;
Bill Wendling604a8182008-06-18 06:35:30 +0000155
Nick Kledzikef194ed2008-02-27 22:25:36 +0000156 // construct LTModule, hand over ownership of module and target
Bill Wendlinge4242542008-06-18 21:39:02 +0000157 std::string FeatureStr = getFeatureString(m->getTargetTriple().c_str());
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000158 TargetMachine* target = march->createTargetMachine(*m, FeatureStr);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000159 return new LTOModule(m.take(), target);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000160}
161
162
163const char* LTOModule::getTargetTriple()
164{
165 return _module->getTargetTriple().c_str();
166}
167
Nick Kledzikef194ed2008-02-27 22:25:36 +0000168void LTOModule::addDefinedFunctionSymbol(Function* f, Mangler &mangler)
169{
170 // add to list of defined symbols
171 addDefinedSymbol(f, mangler, true);
172
173 // add external symbols referenced by this function.
174 for (Function::iterator b = f->begin(); b != f->end(); ++b) {
175 for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) {
176 for (unsigned count = 0, total = i->getNumOperands();
177 count != total; ++count) {
178 findExternalRefs(i->getOperand(count), mangler);
179 }
180 }
181 }
182}
183
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000184// get string that data pointer points to
185bool LTOModule::objcClassNameFromExpression(Constant* c, std::string& name)
186{
187 if (ConstantExpr* ce = dyn_cast<ConstantExpr>(c)) {
188 Constant* op = ce->getOperand(0);
189 if (GlobalVariable* gvn = dyn_cast<GlobalVariable>(op)) {
190 Constant* cn = gvn->getInitializer();
191 if (ConstantArray* ca = dyn_cast<ConstantArray>(cn)) {
Owen Anderson1ca29d32009-07-13 21:27:19 +0000192 if ( ca->isCString() ) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000193 name = ".objc_class_name_" + ca->getAsString();
194 return true;
195 }
196 }
197 }
198 }
199 return false;
200}
201
202// parse i386/ppc ObjC class data structure
203void LTOModule::addObjCClass(GlobalVariable* clgv)
204{
205 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
206 // second slot in __OBJC,__class is pointer to superclass name
207 std::string superclassName;
208 if ( objcClassNameFromExpression(c->getOperand(1), superclassName) ) {
209 NameAndAttributes info;
210 if ( _undefines.find(superclassName.c_str()) == _undefines.end() ) {
211 const char* symbolName = ::strdup(superclassName.c_str());
212 info.name = ::strdup(symbolName);
213 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
214 // string is owned by _undefines
215 _undefines[info.name] = info;
216 }
217 }
218 // third slot in __OBJC,__class is pointer to class name
219 std::string className;
220 if ( objcClassNameFromExpression(c->getOperand(2), className) ) {
221 const char* symbolName = ::strdup(className.c_str());
222 NameAndAttributes info;
223 info.name = symbolName;
224 info.attributes = (lto_symbol_attributes)
225 (LTO_SYMBOL_PERMISSIONS_DATA |
226 LTO_SYMBOL_DEFINITION_REGULAR |
227 LTO_SYMBOL_SCOPE_DEFAULT);
228 _symbols.push_back(info);
229 _defines[info.name] = 1;
230 }
231 }
232}
233
234
235// parse i386/ppc ObjC category data structure
236void LTOModule::addObjCCategory(GlobalVariable* clgv)
237{
238 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
239 // second slot in __OBJC,__category is pointer to target class name
240 std::string targetclassName;
241 if ( objcClassNameFromExpression(c->getOperand(1), targetclassName) ) {
242 NameAndAttributes info;
243 if ( _undefines.find(targetclassName.c_str()) == _undefines.end() ){
244 const char* symbolName = ::strdup(targetclassName.c_str());
245 info.name = ::strdup(symbolName);
246 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
247 // string is owned by _undefines
248 _undefines[info.name] = info;
249 }
250 }
251 }
252}
253
254
255// parse i386/ppc ObjC class list data structure
256void LTOModule::addObjCClassRef(GlobalVariable* clgv)
257{
258 std::string targetclassName;
259 if ( objcClassNameFromExpression(clgv->getInitializer(), targetclassName) ){
260 NameAndAttributes info;
261 if ( _undefines.find(targetclassName.c_str()) == _undefines.end() ) {
262 const char* symbolName = ::strdup(targetclassName.c_str());
263 info.name = ::strdup(symbolName);
264 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
265 // string is owned by _undefines
266 _undefines[info.name] = info;
267 }
268 }
269}
270
271
272void LTOModule::addDefinedDataSymbol(GlobalValue* v, Mangler& mangler)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000273{
274 // add to list of defined symbols
275 addDefinedSymbol(v, mangler, false);
276
Nick Kledzik4bdf7302009-06-01 23:41:09 +0000277 // Special case i386/ppc ObjC data structures in magic sections:
278 // The issue is that the old ObjC object format did some strange
279 // contortions to avoid real linker symbols. For instance, the
280 // ObjC class data structure is allocated statically in the executable
281 // that defines that class. That data structures contains a pointer to
282 // its superclass. But instead of just initializing that part of the
283 // struct to the address of its superclass, and letting the static and
284 // dynamic linkers do the rest, the runtime works by having that field
285 // instead point to a C-string that is the name of the superclass.
286 // At runtime the objc initialization updates that pointer and sets
287 // it to point to the actual super class. As far as the linker
288 // knows it is just a pointer to a string. But then someone wanted the
289 // linker to issue errors at build time if the superclass was not found.
290 // So they figured out a way in mach-o object format to use an absolute
291 // symbols (.objc_class_name_Foo = 0) and a floating reference
292 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
293 // a class was missing.
294 // The following synthesizes the implicit .objc_* symbols for the linker
295 // from the ObjC data structures generated by the front end.
296 if ( v->hasSection() /* && isTargetDarwin */ ) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000297 // special case if this data blob is an ObjC class definition
298 if ( v->getSection().compare(0, 15, "__OBJC,__class,") == 0 ) {
299 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
300 addObjCClass(gv);
301 }
302 }
303
304 // special case if this data blob is an ObjC category definition
305 else if ( v->getSection().compare(0, 18, "__OBJC,__category,") == 0 ) {
306 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
307 addObjCCategory(gv);
308 }
309 }
310
311 // special case if this data blob is the list of referenced classes
312 else if ( v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0 ) {
313 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
314 addObjCClassRef(gv);
315 }
316 }
317 }
318
Nick Kledzikef194ed2008-02-27 22:25:36 +0000319 // add external symbols referenced by this data.
Nick Kledzik9178a652008-05-27 22:07:08 +0000320 for (unsigned count = 0, total = v->getNumOperands();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000321 count != total; ++count) {
322 findExternalRefs(v->getOperand(count), mangler);
323 }
324}
325
326
Nick Kledzik77595fc2008-02-26 20:26:43 +0000327void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler,
Nick Lewycky485ded02009-07-09 06:03:04 +0000328 bool isFunction)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000329{
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000330 // ignore all llvm.* symbols
331 if ( strncmp(def->getNameStart(), "llvm.", 5) == 0 )
332 return;
333
Nick Kledzikef194ed2008-02-27 22:25:36 +0000334 // string is owned by _defines
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000335 const char* symbolName = ::strdup(mangler.getMangledName(def).c_str());
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000336
Nick Kledzik77595fc2008-02-26 20:26:43 +0000337 // set alignment part log2() can have rounding errors
338 uint32_t align = def->getAlignment();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000339 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000340
341 // set permissions part
342 if ( isFunction )
343 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
344 else {
345 GlobalVariable* gv = dyn_cast<GlobalVariable>(def);
346 if ( (gv != NULL) && gv->isConstant() )
347 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
348 else
349 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
350 }
351
352 // set definition part
353 if ( def->hasWeakLinkage() || def->hasLinkOnceLinkage() ) {
Dale Johannesened1ec3a2008-05-23 00:15:10 +0000354 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000355 }
Dale Johannesen6a6f2dd2008-05-16 22:46:40 +0000356 else if ( def->hasCommonLinkage()) {
357 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
358 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000359 else {
360 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
361 }
362
363 // set scope part
364 if ( def->hasHiddenVisibility() )
365 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
Nick Lewycky4fd40e82008-11-29 22:49:59 +0000366 else if ( def->hasProtectedVisibility() )
367 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Devang Patelf0d286b2008-07-15 00:00:11 +0000368 else if ( def->hasExternalLinkage() || def->hasWeakLinkage()
Nick Kledzikdb6535d2008-07-19 00:58:07 +0000369 || def->hasLinkOnceLinkage() || def->hasCommonLinkage() )
Nick Kledzik77595fc2008-02-26 20:26:43 +0000370 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
371 else
372 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
373
374 // add to table of symbols
375 NameAndAttributes info;
376 info.name = symbolName;
377 info.attributes = (lto_symbol_attributes)attr;
378 _symbols.push_back(info);
379 _defines[info.name] = 1;
380}
381
Devang Patelc2aec572008-07-16 18:06:52 +0000382void LTOModule::addAsmGlobalSymbol(const char *name) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000383 // only add new define if not already defined
384 if ( _defines.count(name, &name[strlen(name)+1]) == 0 )
385 return;
386
387 // string is owned by _defines
388 const char *symbolName = ::strdup(name);
389 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
390 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
391 NameAndAttributes info;
392 info.name = symbolName;
393 info.attributes = (lto_symbol_attributes)attr;
394 _symbols.push_back(info);
395 _defines[info.name] = 1;
Devang Patelc2aec572008-07-16 18:06:52 +0000396}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000397
Nick Kledzikef194ed2008-02-27 22:25:36 +0000398void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler)
399{
Nick Kledzik77595fc2008-02-26 20:26:43 +0000400 // ignore all llvm.* symbols
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000401 if ( strncmp(decl->getNameStart(), "llvm.", 5) == 0 )
402 return;
403
Nick Lewycky485ded02009-07-09 06:03:04 +0000404 // ignore all aliases
405 if (isa<GlobalAlias>(decl))
406 return;
407
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000408 const char* name = mangler.getMangledName(decl).c_str();
Rafael Espindola7431af02009-04-24 16:55:21 +0000409
410 // we already have the symbol
411 if (_undefines.find(name) != _undefines.end())
412 return;
413
414 NameAndAttributes info;
415 // string is owned by _undefines
416 info.name = ::strdup(name);
417 if (decl->hasExternalWeakLinkage())
418 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
419 else
420 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
421 _undefines[name] = info;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000422}
423
424
425
426// Find exeternal symbols referenced by VALUE. This is a recursive function.
427void LTOModule::findExternalRefs(Value* value, Mangler &mangler) {
428
429 if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) {
430 if ( !gv->hasExternalLinkage() )
Nick Kledzikef194ed2008-02-27 22:25:36 +0000431 addPotentialUndefinedSymbol(gv, mangler);
Nick Kledziked185d62008-05-28 00:06:14 +0000432 // If this is a variable definition, do not recursively process
433 // initializer. It might contain a reference to this variable
434 // and cause an infinite loop. The initializer will be
435 // processed in addDefinedDataSymbol().
436 return;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000437 }
Nick Kledziked185d62008-05-28 00:06:14 +0000438
Nick Kledzik77595fc2008-02-26 20:26:43 +0000439 // GlobalValue, even with InternalLinkage type, may have operands with
440 // ExternalLinkage type. Do not ignore these operands.
441 if (Constant* c = dyn_cast<Constant>(value)) {
442 // Handle ConstantExpr, ConstantStruct, ConstantArry etc..
443 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
444 findExternalRefs(c->getOperand(i), mangler);
445 }
446}
447
Nick Kledzikef194ed2008-02-27 22:25:36 +0000448void LTOModule::lazyParseSymbols()
Nick Kledzik77595fc2008-02-26 20:26:43 +0000449{
450 if ( !_symbolsParsed ) {
451 _symbolsParsed = true;
452
453 // Use mangler to add GlobalPrefix to names to match linker names.
454 Mangler mangler(*_module, _target->getTargetAsmInfo()->getGlobalPrefix());
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000455 // add chars used in ObjC method names so method names aren't mangled
456 mangler.markCharAcceptable('[');
457 mangler.markCharAcceptable(']');
458 mangler.markCharAcceptable('(');
459 mangler.markCharAcceptable(')');
460 mangler.markCharAcceptable('-');
461 mangler.markCharAcceptable('+');
462 mangler.markCharAcceptable(' ');
Nick Kledzik77595fc2008-02-26 20:26:43 +0000463
464 // add functions
465 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000466 if ( f->isDeclaration() )
467 addPotentialUndefinedSymbol(f, mangler);
468 else
469 addDefinedFunctionSymbol(f, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000470 }
471
472 // add data
473 for (Module::global_iterator v = _module->global_begin(),
474 e = _module->global_end(); v != e; ++v) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000475 if ( v->isDeclaration() )
476 addPotentialUndefinedSymbol(v, mangler);
477 else
478 addDefinedDataSymbol(v, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000479 }
480
Devang Patelc2aec572008-07-16 18:06:52 +0000481 // add asm globals
482 const std::string &inlineAsm = _module->getModuleInlineAsm();
483 const std::string glbl = ".globl";
484 std::string asmSymbolName;
485 std::string::size_type pos = inlineAsm.find(glbl, 0);
486 while (pos != std::string::npos) {
487 // eat .globl
488 pos = pos + 6;
489
490 // skip white space between .globl and symbol name
491 std::string::size_type pbegin = inlineAsm.find_first_not_of(' ', pos);
492 if (pbegin == std::string::npos)
493 break;
494
495 // find end-of-line
496 std::string::size_type pend = inlineAsm.find_first_of('\n', pbegin);
497 if (pend == std::string::npos)
498 break;
499
Devang Patel41390702008-07-16 19:49:09 +0000500 asmSymbolName.assign(inlineAsm, pbegin, pend - pbegin);
Devang Patelc2aec572008-07-16 18:06:52 +0000501 addAsmGlobalSymbol(asmSymbolName.c_str());
502
503 // search next .globl
504 pos = inlineAsm.find(glbl, pend);
505 }
506
Nick Kledzik77595fc2008-02-26 20:26:43 +0000507 // make symbols for all undefines
Rafael Espindola7431af02009-04-24 16:55:21 +0000508 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000509 it != _undefines.end(); ++it) {
510 // if this symbol also has a definition, then don't make an undefine
511 // because it is a tentative definition
Nick Kledzikef194ed2008-02-27 22:25:36 +0000512 if ( _defines.count(it->getKeyData(), it->getKeyData()+
513 it->getKeyLength()) == 0 ) {
Rafael Espindola7431af02009-04-24 16:55:21 +0000514 NameAndAttributes info = it->getValue();
515 _symbols.push_back(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000516 }
517 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000518 }
519}
520
521
522uint32_t LTOModule::getSymbolCount()
523{
524 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000525 return _symbols.size();
526}
527
528
529lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index)
530{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000531 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000532 if ( index < _symbols.size() )
533 return _symbols[index].attributes;
534 else
535 return lto_symbol_attributes(0);
536}
537
538const char* LTOModule::getSymbolName(uint32_t index)
539{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000540 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000541 if ( index < _symbols.size() )
542 return _symbols[index].name;
543 else
544 return NULL;
545}