blob: 939d0eacd1e30736a652df535c057fdcdffd94a5 [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"
Nick Kledzik77595fc2008-02-26 20:26:43 +000018#include "llvm/Module.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000019#include "llvm/ModuleProvider.h"
Nick Kledzikef194ed2008-02-27 22:25:36 +000020#include "llvm/ADT/OwningPtr.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000021#include "llvm/Bitcode/ReaderWriter.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000022#include "llvm/Support/SystemUtils.h"
23#include "llvm/Support/Mangler.h"
24#include "llvm/Support/MemoryBuffer.h"
Nick Kledzikef194ed2008-02-27 22:25:36 +000025#include "llvm/Support/MathExtras.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000026#include "llvm/System/Path.h"
Nick Kledzik90dcff72008-05-09 01:09:59 +000027#include "llvm/System/Process.h"
Bill Wendling604a8182008-06-18 06:35:30 +000028#include "llvm/Target/SubtargetFeature.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000029#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetMachineRegistry.h"
31#include "llvm/Target/TargetAsmInfo.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000032
Nick Kledzik77595fc2008-02-26 20:26:43 +000033#include <fstream>
34
35using 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{
70 OwningPtr<ModuleProvider> mp(getBitcodeModuleProvider(buffer));
71 // on success, mp owns buffer and both are deleted at end of this method
72 if ( !mp ) {
73 delete buffer;
74 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +000075 }
Nick Kledzikef194ed2008-02-27 22:25:36 +000076 std::string actualTarget = mp->getModule()->getTargetTriple();
77 return ( strncmp(actualTarget.c_str(), triplePrefix,
78 strlen(triplePrefix)) == 0);
Nick Kledzik77595fc2008-02-26 20:26:43 +000079}
80
81
82LTOModule::LTOModule(Module* m, TargetMachine* t)
83 : _module(m), _target(t), _symbolsParsed(false)
84{
85}
86
Nick Kledzik77595fc2008-02-26 20:26:43 +000087LTOModule* LTOModule::makeLTOModule(const char* path, std::string& errMsg)
88{
Chris Lattner038112a2008-04-01 18:04:03 +000089 OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, &errMsg));
Nick Kledzikef194ed2008-02-27 22:25:36 +000090 if ( !buffer )
91 return NULL;
92 return makeLTOModule(buffer.get(), errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +000093}
94
Nick Kledzik6b89d922008-05-09 18:44:41 +000095/// makeBuffer - create a MemoryBuffer from a memory range.
96/// MemoryBuffer requires the byte past end of the buffer to be a zero.
97/// We might get lucky and already be that way, otherwise make a copy.
98/// Also if next byte is on a different page, don't assume it is readable.
Nick Kledzik90dcff72008-05-09 01:09:59 +000099MemoryBuffer* LTOModule::makeBuffer(const void* mem, size_t length)
100{
Nick Kledziked185d62008-05-28 00:06:14 +0000101 const char* startPtr = (char*)mem;
102 const char* endPtr = startPtr+length;
103 if ( (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0)
104 || (*endPtr != 0) )
105 return MemoryBuffer::getMemBufferCopy(startPtr, endPtr);
106 else
107 return MemoryBuffer::getMemBuffer(startPtr, endPtr);
Nick Kledzik90dcff72008-05-09 01:09:59 +0000108}
109
110
Nick Kledzik77595fc2008-02-26 20:26:43 +0000111LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length,
Nick Lewyckyb454eab2009-02-06 07:01:00 +0000112 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000113{
Nick Kledzik90dcff72008-05-09 01:09:59 +0000114 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
Nick Kledzikef194ed2008-02-27 22:25:36 +0000115 if ( !buffer )
116 return NULL;
117 return makeLTOModule(buffer.get(), errMsg);
118}
119
Bill Wendlinge4242542008-06-18 21:39:02 +0000120/// getFeatureString - Return a string listing the features associated with the
121/// target triple.
122///
123/// FIXME: This is an inelegant way of specifying the features of a
124/// subtarget. It would be better if we could encode this information into the
125/// IR. See <rdar://5972456>.
126std::string getFeatureString(const char *TargetTriple) {
127 SubtargetFeatures Features;
128
129 if (strncmp(TargetTriple, "powerpc-apple-", 14) == 0) {
130 Features.AddFeature("altivec", true);
131 } else if (strncmp(TargetTriple, "powerpc64-apple-", 16) == 0) {
132 Features.AddFeature("64bit", true);
133 Features.AddFeature("altivec", true);
134 }
135
136 return Features.getString();
137}
138
Nick Kledzikef194ed2008-02-27 22:25:36 +0000139LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer, std::string& errMsg)
140{
141 // parse bitcode buffer
142 OwningPtr<Module> m(ParseBitcodeFile(buffer, &errMsg));
143 if ( !m )
144 return NULL;
145 // find machine architecture for this module
146 const TargetMachineRegistry::entry* march =
147 TargetMachineRegistry::getClosestStaticTargetForModule(*m, errMsg);
Bill Wendling604a8182008-06-18 06:35:30 +0000148
Nick Kledzikef194ed2008-02-27 22:25:36 +0000149 if ( march == NULL )
150 return NULL;
Bill Wendling604a8182008-06-18 06:35:30 +0000151
Nick Kledzikef194ed2008-02-27 22:25:36 +0000152 // construct LTModule, hand over ownership of module and target
Bill Wendlinge4242542008-06-18 21:39:02 +0000153 std::string FeatureStr = getFeatureString(m->getTargetTriple().c_str());
154 TargetMachine* target = march->CtorFn(*m, FeatureStr);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000155 return new LTOModule(m.take(), target);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000156}
157
158
159const char* LTOModule::getTargetTriple()
160{
161 return _module->getTargetTriple().c_str();
162}
163
Nick Kledzikef194ed2008-02-27 22:25:36 +0000164void LTOModule::addDefinedFunctionSymbol(Function* f, Mangler &mangler)
165{
166 // add to list of defined symbols
167 addDefinedSymbol(f, mangler, true);
168
169 // add external symbols referenced by this function.
170 for (Function::iterator b = f->begin(); b != f->end(); ++b) {
171 for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) {
172 for (unsigned count = 0, total = i->getNumOperands();
173 count != total; ++count) {
174 findExternalRefs(i->getOperand(count), mangler);
175 }
176 }
177 }
178}
179
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000180// get string that data pointer points to
181bool LTOModule::objcClassNameFromExpression(Constant* c, std::string& name)
182{
183 if (ConstantExpr* ce = dyn_cast<ConstantExpr>(c)) {
184 Constant* op = ce->getOperand(0);
185 if (GlobalVariable* gvn = dyn_cast<GlobalVariable>(op)) {
186 Constant* cn = gvn->getInitializer();
187 if (ConstantArray* ca = dyn_cast<ConstantArray>(cn)) {
188 if ( ca->isCString() ) {
189 name = ".objc_class_name_" + ca->getAsString();
190 return true;
191 }
192 }
193 }
194 }
195 return false;
196}
197
198// parse i386/ppc ObjC class data structure
199void LTOModule::addObjCClass(GlobalVariable* clgv)
200{
201 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
202 // second slot in __OBJC,__class is pointer to superclass name
203 std::string superclassName;
204 if ( objcClassNameFromExpression(c->getOperand(1), superclassName) ) {
205 NameAndAttributes info;
206 if ( _undefines.find(superclassName.c_str()) == _undefines.end() ) {
207 const char* symbolName = ::strdup(superclassName.c_str());
208 info.name = ::strdup(symbolName);
209 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
210 // string is owned by _undefines
211 _undefines[info.name] = info;
212 }
213 }
214 // third slot in __OBJC,__class is pointer to class name
215 std::string className;
216 if ( objcClassNameFromExpression(c->getOperand(2), className) ) {
217 const char* symbolName = ::strdup(className.c_str());
218 NameAndAttributes info;
219 info.name = symbolName;
220 info.attributes = (lto_symbol_attributes)
221 (LTO_SYMBOL_PERMISSIONS_DATA |
222 LTO_SYMBOL_DEFINITION_REGULAR |
223 LTO_SYMBOL_SCOPE_DEFAULT);
224 _symbols.push_back(info);
225 _defines[info.name] = 1;
226 }
227 }
228}
229
230
231// parse i386/ppc ObjC category data structure
232void LTOModule::addObjCCategory(GlobalVariable* clgv)
233{
234 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
235 // second slot in __OBJC,__category is pointer to target class name
236 std::string targetclassName;
237 if ( objcClassNameFromExpression(c->getOperand(1), targetclassName) ) {
238 NameAndAttributes info;
239 if ( _undefines.find(targetclassName.c_str()) == _undefines.end() ){
240 const char* symbolName = ::strdup(targetclassName.c_str());
241 info.name = ::strdup(symbolName);
242 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
243 // string is owned by _undefines
244 _undefines[info.name] = info;
245 }
246 }
247 }
248}
249
250
251// parse i386/ppc ObjC class list data structure
252void LTOModule::addObjCClassRef(GlobalVariable* clgv)
253{
254 std::string targetclassName;
255 if ( objcClassNameFromExpression(clgv->getInitializer(), targetclassName) ){
256 NameAndAttributes info;
257 if ( _undefines.find(targetclassName.c_str()) == _undefines.end() ) {
258 const char* symbolName = ::strdup(targetclassName.c_str());
259 info.name = ::strdup(symbolName);
260 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
261 // string is owned by _undefines
262 _undefines[info.name] = info;
263 }
264 }
265}
266
267
268void LTOModule::addDefinedDataSymbol(GlobalValue* v, Mangler& mangler)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000269{
270 // add to list of defined symbols
271 addDefinedSymbol(v, mangler, false);
272
Nick Kledzik4bdf7302009-06-01 23:41:09 +0000273 // Special case i386/ppc ObjC data structures in magic sections:
274 // The issue is that the old ObjC object format did some strange
275 // contortions to avoid real linker symbols. For instance, the
276 // ObjC class data structure is allocated statically in the executable
277 // that defines that class. That data structures contains a pointer to
278 // its superclass. But instead of just initializing that part of the
279 // struct to the address of its superclass, and letting the static and
280 // dynamic linkers do the rest, the runtime works by having that field
281 // instead point to a C-string that is the name of the superclass.
282 // At runtime the objc initialization updates that pointer and sets
283 // it to point to the actual super class. As far as the linker
284 // knows it is just a pointer to a string. But then someone wanted the
285 // linker to issue errors at build time if the superclass was not found.
286 // So they figured out a way in mach-o object format to use an absolute
287 // symbols (.objc_class_name_Foo = 0) and a floating reference
288 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
289 // a class was missing.
290 // The following synthesizes the implicit .objc_* symbols for the linker
291 // from the ObjC data structures generated by the front end.
292 if ( v->hasSection() /* && isTargetDarwin */ ) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000293 // special case if this data blob is an ObjC class definition
294 if ( v->getSection().compare(0, 15, "__OBJC,__class,") == 0 ) {
295 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
296 addObjCClass(gv);
297 }
298 }
299
300 // special case if this data blob is an ObjC category definition
301 else if ( v->getSection().compare(0, 18, "__OBJC,__category,") == 0 ) {
302 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
303 addObjCCategory(gv);
304 }
305 }
306
307 // special case if this data blob is the list of referenced classes
308 else if ( v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0 ) {
309 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
310 addObjCClassRef(gv);
311 }
312 }
313 }
314
Nick Kledzikef194ed2008-02-27 22:25:36 +0000315 // add external symbols referenced by this data.
Nick Kledzik9178a652008-05-27 22:07:08 +0000316 for (unsigned count = 0, total = v->getNumOperands();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000317 count != total; ++count) {
318 findExternalRefs(v->getOperand(count), mangler);
319 }
320}
321
322
Nick Kledzik77595fc2008-02-26 20:26:43 +0000323void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler,
324 bool isFunction)
325{
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000326 // ignore all llvm.* symbols
327 if ( strncmp(def->getNameStart(), "llvm.", 5) == 0 )
328 return;
329
Nick Kledzikef194ed2008-02-27 22:25:36 +0000330 // string is owned by _defines
Nick Kledzik77595fc2008-02-26 20:26:43 +0000331 const char* symbolName = ::strdup(mangler.getValueName(def).c_str());
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000332
Nick Kledzik77595fc2008-02-26 20:26:43 +0000333 // set alignment part log2() can have rounding errors
334 uint32_t align = def->getAlignment();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000335 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000336
337 // set permissions part
338 if ( isFunction )
339 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
340 else {
341 GlobalVariable* gv = dyn_cast<GlobalVariable>(def);
342 if ( (gv != NULL) && gv->isConstant() )
343 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
344 else
345 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
346 }
347
348 // set definition part
349 if ( def->hasWeakLinkage() || def->hasLinkOnceLinkage() ) {
Dale Johannesened1ec3a2008-05-23 00:15:10 +0000350 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000351 }
Dale Johannesen6a6f2dd2008-05-16 22:46:40 +0000352 else if ( def->hasCommonLinkage()) {
353 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
354 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000355 else {
356 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
357 }
358
359 // set scope part
360 if ( def->hasHiddenVisibility() )
361 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
Nick Lewycky4fd40e82008-11-29 22:49:59 +0000362 else if ( def->hasProtectedVisibility() )
363 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Devang Patelf0d286b2008-07-15 00:00:11 +0000364 else if ( def->hasExternalLinkage() || def->hasWeakLinkage()
Nick Kledzikdb6535d2008-07-19 00:58:07 +0000365 || def->hasLinkOnceLinkage() || def->hasCommonLinkage() )
Nick Kledzik77595fc2008-02-26 20:26:43 +0000366 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
367 else
368 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
369
370 // add to table of symbols
371 NameAndAttributes info;
372 info.name = symbolName;
373 info.attributes = (lto_symbol_attributes)attr;
374 _symbols.push_back(info);
375 _defines[info.name] = 1;
376}
377
Devang Patelc2aec572008-07-16 18:06:52 +0000378void LTOModule::addAsmGlobalSymbol(const char *name) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000379 // only add new define if not already defined
380 if ( _defines.count(name, &name[strlen(name)+1]) == 0 )
381 return;
382
383 // string is owned by _defines
384 const char *symbolName = ::strdup(name);
385 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
386 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
387 NameAndAttributes info;
388 info.name = symbolName;
389 info.attributes = (lto_symbol_attributes)attr;
390 _symbols.push_back(info);
391 _defines[info.name] = 1;
Devang Patelc2aec572008-07-16 18:06:52 +0000392}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000393
Nick Kledzikef194ed2008-02-27 22:25:36 +0000394void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler)
395{
Nick Kledzik77595fc2008-02-26 20:26:43 +0000396 // ignore all llvm.* symbols
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000397 if ( strncmp(decl->getNameStart(), "llvm.", 5) == 0 )
398 return;
399
400 const char* name = mangler.getValueName(decl).c_str();
Rafael Espindola7431af02009-04-24 16:55:21 +0000401
402 // we already have the symbol
403 if (_undefines.find(name) != _undefines.end())
404 return;
405
406 NameAndAttributes info;
407 // string is owned by _undefines
408 info.name = ::strdup(name);
409 if (decl->hasExternalWeakLinkage())
410 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
411 else
412 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
413 _undefines[name] = info;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000414}
415
416
417
418// Find exeternal symbols referenced by VALUE. This is a recursive function.
419void LTOModule::findExternalRefs(Value* value, Mangler &mangler) {
420
421 if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) {
422 if ( !gv->hasExternalLinkage() )
Nick Kledzikef194ed2008-02-27 22:25:36 +0000423 addPotentialUndefinedSymbol(gv, mangler);
Nick Kledziked185d62008-05-28 00:06:14 +0000424 // If this is a variable definition, do not recursively process
425 // initializer. It might contain a reference to this variable
426 // and cause an infinite loop. The initializer will be
427 // processed in addDefinedDataSymbol().
428 return;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000429 }
Nick Kledziked185d62008-05-28 00:06:14 +0000430
Nick Kledzik77595fc2008-02-26 20:26:43 +0000431 // GlobalValue, even with InternalLinkage type, may have operands with
432 // ExternalLinkage type. Do not ignore these operands.
433 if (Constant* c = dyn_cast<Constant>(value)) {
434 // Handle ConstantExpr, ConstantStruct, ConstantArry etc..
435 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
436 findExternalRefs(c->getOperand(i), mangler);
437 }
438}
439
Nick Kledzikef194ed2008-02-27 22:25:36 +0000440void LTOModule::lazyParseSymbols()
Nick Kledzik77595fc2008-02-26 20:26:43 +0000441{
442 if ( !_symbolsParsed ) {
443 _symbolsParsed = true;
444
445 // Use mangler to add GlobalPrefix to names to match linker names.
446 Mangler mangler(*_module, _target->getTargetAsmInfo()->getGlobalPrefix());
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000447 // add chars used in ObjC method names so method names aren't mangled
448 mangler.markCharAcceptable('[');
449 mangler.markCharAcceptable(']');
450 mangler.markCharAcceptable('(');
451 mangler.markCharAcceptable(')');
452 mangler.markCharAcceptable('-');
453 mangler.markCharAcceptable('+');
454 mangler.markCharAcceptable(' ');
Nick Kledzik77595fc2008-02-26 20:26:43 +0000455
456 // add functions
457 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000458 if ( f->isDeclaration() )
459 addPotentialUndefinedSymbol(f, mangler);
460 else
461 addDefinedFunctionSymbol(f, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000462 }
463
464 // add data
465 for (Module::global_iterator v = _module->global_begin(),
466 e = _module->global_end(); v != e; ++v) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000467 if ( v->isDeclaration() )
468 addPotentialUndefinedSymbol(v, mangler);
469 else
470 addDefinedDataSymbol(v, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000471 }
472
Devang Patelc2aec572008-07-16 18:06:52 +0000473 // add asm globals
474 const std::string &inlineAsm = _module->getModuleInlineAsm();
475 const std::string glbl = ".globl";
476 std::string asmSymbolName;
477 std::string::size_type pos = inlineAsm.find(glbl, 0);
478 while (pos != std::string::npos) {
479 // eat .globl
480 pos = pos + 6;
481
482 // skip white space between .globl and symbol name
483 std::string::size_type pbegin = inlineAsm.find_first_not_of(' ', pos);
484 if (pbegin == std::string::npos)
485 break;
486
487 // find end-of-line
488 std::string::size_type pend = inlineAsm.find_first_of('\n', pbegin);
489 if (pend == std::string::npos)
490 break;
491
Devang Patel41390702008-07-16 19:49:09 +0000492 asmSymbolName.assign(inlineAsm, pbegin, pend - pbegin);
Devang Patelc2aec572008-07-16 18:06:52 +0000493 addAsmGlobalSymbol(asmSymbolName.c_str());
494
495 // search next .globl
496 pos = inlineAsm.find(glbl, pend);
497 }
498
Nick Kledzik77595fc2008-02-26 20:26:43 +0000499 // make symbols for all undefines
Rafael Espindola7431af02009-04-24 16:55:21 +0000500 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000501 it != _undefines.end(); ++it) {
502 // if this symbol also has a definition, then don't make an undefine
503 // because it is a tentative definition
Nick Kledzikef194ed2008-02-27 22:25:36 +0000504 if ( _defines.count(it->getKeyData(), it->getKeyData()+
505 it->getKeyLength()) == 0 ) {
Rafael Espindola7431af02009-04-24 16:55:21 +0000506 NameAndAttributes info = it->getValue();
507 _symbols.push_back(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000508 }
509 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000510 }
511}
512
513
514uint32_t LTOModule::getSymbolCount()
515{
516 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000517 return _symbols.size();
518}
519
520
521lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index)
522{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000523 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000524 if ( index < _symbols.size() )
525 return _symbols[index].attributes;
526 else
527 return lto_symbol_attributes(0);
528}
529
530const char* LTOModule::getSymbolName(uint32_t index)
531{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000532 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000533 if ( index < _symbols.size() )
534 return _symbols[index].name;
535 else
536 return NULL;
537}
538