blob: d386bacdfc9326c8bb99755569d0276d9d2feede [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.
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 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"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000026#include "llvm/System/Host.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"
Chris Lattner45111d12010-01-16 21:57:06 +000029#include "llvm/Target/Mangler.h"
Bill Wendling604a8182008-06-18 06:35:30 +000030#include "llvm/Target/SubtargetFeature.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000031#include "llvm/MC/MCAsmInfo.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000032#include "llvm/MC/MCContext.h"
Daniel Dunbarff9834a2009-07-16 02:41:19 +000033#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetRegistry.h"
Nick Lewyckyd42b58b2009-07-26 22:16:39 +000035#include "llvm/Target/TargetSelect.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000036
Nick Kledzik77595fc2008-02-26 20:26:43 +000037using namespace llvm;
38
39bool LTOModule::isBitcodeFile(const void* mem, size_t length)
40{
Gabor Greif4136e7b2009-09-23 02:46:12 +000041 return llvm::sys::IdentifyFileType((char*)mem, length)
42 == llvm::sys::Bitcode_FileType;
Nick Kledzik77595fc2008-02-26 20:26:43 +000043}
44
45bool LTOModule::isBitcodeFile(const char* path)
46{
47 return llvm::sys::Path(path).isBitcodeFile();
48}
49
Chris Lattner038112a2008-04-01 18:04:03 +000050bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length,
51 const char* triplePrefix)
Nick Kledzik77595fc2008-02-26 20:26:43 +000052{
Nick Kledzik90dcff72008-05-09 01:09:59 +000053 MemoryBuffer* buffer = makeBuffer(mem, length);
Gabor Greif4136e7b2009-09-23 02:46:12 +000054 if (!buffer)
Nick Kledzikef194ed2008-02-27 22:25:36 +000055 return false;
56 return isTargetMatch(buffer, triplePrefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +000057}
58
Nick Kledzikef194ed2008-02-27 22:25:36 +000059
Nick Kledzik77595fc2008-02-26 20:26:43 +000060bool LTOModule::isBitcodeFileForTarget(const char* path,
Chris Lattner038112a2008-04-01 18:04:03 +000061 const char* triplePrefix)
Nick Kledzik77595fc2008-02-26 20:26:43 +000062{
Chris Lattner038112a2008-04-01 18:04:03 +000063 MemoryBuffer *buffer = MemoryBuffer::getFile(path);
64 if (buffer == NULL)
Nick Kledzikef194ed2008-02-27 22:25:36 +000065 return false;
66 return isTargetMatch(buffer, triplePrefix);
67}
68
69// takes ownership of buffer
70bool LTOModule::isTargetMatch(MemoryBuffer* buffer, const char* triplePrefix)
71{
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000072 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext()));
73 // on success, m owns buffer and both are deleted at end of this method
74 if (!m) {
Nick Kledzikef194ed2008-02-27 22:25:36 +000075 delete buffer;
76 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +000077 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000078 std::string actualTarget = m->getTargetTriple();
Gabor Greif4136e7b2009-09-23 02:46:12 +000079 return (strncmp(actualTarget.c_str(), triplePrefix,
Nick Kledzikef194ed2008-02-27 22:25:36 +000080 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));
Gabor Greif4136e7b2009-09-23 02:46:12 +000093 if (!buffer)
Nick Kledzikef194ed2008-02-27 22:25:36 +000094 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{
Chris Lattner4c842dd2010-04-05 22:42:30 +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(StringRef(startPtr, length));
109
110 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length));
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));
Gabor Greif4136e7b2009-09-23 02:46:12 +0000118 if (!buffer)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000119 return NULL;
Owen Anderson0e7a5462009-07-02 00:31:14 +0000120 return makeLTOModule(buffer.get(), errMsg);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000121}
122
Owen Anderson31895e72009-07-01 21:22:36 +0000123LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer,
Owen Anderson8b477ed2009-07-01 16:58:40 +0000124 std::string& errMsg)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000125{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000126 InitializeAllTargets();
127
Nick Kledzikef194ed2008-02-27 22:25:36 +0000128 // parse bitcode buffer
Owen Anderson0e7a5462009-07-02 00:31:14 +0000129 OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
Gabor Greif4136e7b2009-09-23 02:46:12 +0000130 if (!m)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000131 return NULL;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000132
133 std::string Triple = m->getTargetTriple();
134 if (Triple.empty())
135 Triple = sys::getHostTriple();
136
Nick Kledzikef194ed2008-02-27 22:25:36 +0000137 // find machine architecture for this module
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000138 const Target* march = TargetRegistry::lookupTarget(Triple, errMsg);
Gabor Greif4136e7b2009-09-23 02:46:12 +0000139 if (!march)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000140 return NULL;
Bill Wendling604a8182008-06-18 06:35:30 +0000141
Nick Kledzikef194ed2008-02-27 22:25:36 +0000142 // construct LTModule, hand over ownership of module and target
Bill Wendling81043ee2010-05-11 00:30:02 +0000143 SubtargetFeatures Features;
144 Features.getDefaultSubtargetFeatures("" /* cpu */, llvm::Triple(Triple));
145 std::string FeatureStr = Features.getString();
Daniel Dunbar4b3d5722009-08-04 04:08:40 +0000146 TargetMachine* target = march->createTargetMachine(Triple, FeatureStr);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000147 return new LTOModule(m.take(), target);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000148}
149
150
151const char* LTOModule::getTargetTriple()
152{
153 return _module->getTargetTriple().c_str();
154}
155
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000156void LTOModule::setTargetTriple(const char *triple)
157{
158 _module->setTargetTriple(triple);
159}
160
Nick Kledzikef194ed2008-02-27 22:25:36 +0000161void LTOModule::addDefinedFunctionSymbol(Function* f, Mangler &mangler)
162{
163 // add to list of defined symbols
164 addDefinedSymbol(f, mangler, true);
165
166 // add external symbols referenced by this function.
167 for (Function::iterator b = f->begin(); b != f->end(); ++b) {
168 for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) {
169 for (unsigned count = 0, total = i->getNumOperands();
170 count != total; ++count) {
171 findExternalRefs(i->getOperand(count), mangler);
172 }
173 }
174 }
175}
176
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000177// get string that data pointer points to
178bool LTOModule::objcClassNameFromExpression(Constant* c, std::string& name)
179{
180 if (ConstantExpr* ce = dyn_cast<ConstantExpr>(c)) {
181 Constant* op = ce->getOperand(0);
182 if (GlobalVariable* gvn = dyn_cast<GlobalVariable>(op)) {
183 Constant* cn = gvn->getInitializer();
184 if (ConstantArray* ca = dyn_cast<ConstantArray>(cn)) {
Gabor Greif4136e7b2009-09-23 02:46:12 +0000185 if (ca->isCString()) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000186 name = ".objc_class_name_" + ca->getAsString();
187 return true;
188 }
189 }
190 }
191 }
192 return false;
193}
194
195// parse i386/ppc ObjC class data structure
196void LTOModule::addObjCClass(GlobalVariable* clgv)
197{
198 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
199 // second slot in __OBJC,__class is pointer to superclass name
200 std::string superclassName;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000201 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000202 NameAndAttributes info;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000203 if (_undefines.find(superclassName.c_str()) == _undefines.end()) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000204 const char* symbolName = ::strdup(superclassName.c_str());
205 info.name = ::strdup(symbolName);
206 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
207 // string is owned by _undefines
208 _undefines[info.name] = info;
209 }
210 }
211 // third slot in __OBJC,__class is pointer to class name
212 std::string className;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000213 if (objcClassNameFromExpression(c->getOperand(2), className)) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000214 const char* symbolName = ::strdup(className.c_str());
215 NameAndAttributes info;
216 info.name = symbolName;
217 info.attributes = (lto_symbol_attributes)
218 (LTO_SYMBOL_PERMISSIONS_DATA |
219 LTO_SYMBOL_DEFINITION_REGULAR |
220 LTO_SYMBOL_SCOPE_DEFAULT);
221 _symbols.push_back(info);
222 _defines[info.name] = 1;
223 }
224 }
225}
226
227
228// parse i386/ppc ObjC category data structure
229void LTOModule::addObjCCategory(GlobalVariable* clgv)
230{
231 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
232 // second slot in __OBJC,__category is pointer to target class name
233 std::string targetclassName;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000234 if (objcClassNameFromExpression(c->getOperand(1), targetclassName)) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000235 NameAndAttributes info;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000236 if (_undefines.find(targetclassName.c_str()) == _undefines.end()) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000237 const char* symbolName = ::strdup(targetclassName.c_str());
238 info.name = ::strdup(symbolName);
239 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
240 // string is owned by _undefines
241 _undefines[info.name] = info;
242 }
243 }
244 }
245}
246
247
248// parse i386/ppc ObjC class list data structure
249void LTOModule::addObjCClassRef(GlobalVariable* clgv)
250{
251 std::string targetclassName;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000252 if (objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000253 NameAndAttributes info;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000254 if (_undefines.find(targetclassName.c_str()) == _undefines.end()) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000255 const char* symbolName = ::strdup(targetclassName.c_str());
256 info.name = ::strdup(symbolName);
257 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
258 // string is owned by _undefines
259 _undefines[info.name] = info;
260 }
261 }
262}
263
264
265void LTOModule::addDefinedDataSymbol(GlobalValue* v, Mangler& mangler)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000266{
267 // add to list of defined symbols
268 addDefinedSymbol(v, mangler, false);
269
Nick Kledzik4bdf7302009-06-01 23:41:09 +0000270 // Special case i386/ppc ObjC data structures in magic sections:
271 // The issue is that the old ObjC object format did some strange
272 // contortions to avoid real linker symbols. For instance, the
273 // ObjC class data structure is allocated statically in the executable
274 // that defines that class. That data structures contains a pointer to
275 // its superclass. But instead of just initializing that part of the
276 // struct to the address of its superclass, and letting the static and
277 // dynamic linkers do the rest, the runtime works by having that field
278 // instead point to a C-string that is the name of the superclass.
279 // At runtime the objc initialization updates that pointer and sets
280 // it to point to the actual super class. As far as the linker
281 // knows it is just a pointer to a string. But then someone wanted the
282 // linker to issue errors at build time if the superclass was not found.
283 // So they figured out a way in mach-o object format to use an absolute
284 // symbols (.objc_class_name_Foo = 0) and a floating reference
285 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
286 // a class was missing.
287 // The following synthesizes the implicit .objc_* symbols for the linker
288 // from the ObjC data structures generated by the front end.
Gabor Greif4136e7b2009-09-23 02:46:12 +0000289 if (v->hasSection() /* && isTargetDarwin */) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000290 // special case if this data blob is an ObjC class definition
Gabor Greif4136e7b2009-09-23 02:46:12 +0000291 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000292 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
293 addObjCClass(gv);
294 }
295 }
296
297 // special case if this data blob is an ObjC category definition
Gabor Greif4136e7b2009-09-23 02:46:12 +0000298 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000299 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
300 addObjCCategory(gv);
301 }
302 }
303
304 // special case if this data blob is the list of referenced classes
Gabor Greif4136e7b2009-09-23 02:46:12 +0000305 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000306 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
307 addObjCClassRef(gv);
308 }
309 }
310 }
311
Nick Kledzikef194ed2008-02-27 22:25:36 +0000312 // add external symbols referenced by this data.
Nick Kledzik9178a652008-05-27 22:07:08 +0000313 for (unsigned count = 0, total = v->getNumOperands();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000314 count != total; ++count) {
315 findExternalRefs(v->getOperand(count), mangler);
316 }
317}
318
319
Nick Kledzik77595fc2008-02-26 20:26:43 +0000320void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler,
Nick Lewycky485ded02009-07-09 06:03:04 +0000321 bool isFunction)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000322{
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000323 // ignore all llvm.* symbols
Daniel Dunbar460f6562009-07-26 09:48:23 +0000324 if (def->getName().startswith("llvm."))
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000325 return;
326
Nick Kledzikef194ed2008-02-27 22:25:36 +0000327 // string is owned by _defines
Chris Lattner46934042010-01-16 18:12:14 +0000328 const char* symbolName = ::strdup(mangler.getNameWithPrefix(def).c_str());
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000329
Nick Kledzik77595fc2008-02-26 20:26:43 +0000330 // set alignment part log2() can have rounding errors
331 uint32_t align = def->getAlignment();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000332 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000333
334 // set permissions part
Gabor Greif4136e7b2009-09-23 02:46:12 +0000335 if (isFunction)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000336 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
337 else {
338 GlobalVariable* gv = dyn_cast<GlobalVariable>(def);
Gabor Greif4136e7b2009-09-23 02:46:12 +0000339 if (gv && gv->isConstant())
Nick Kledzik77595fc2008-02-26 20:26:43 +0000340 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
341 else
342 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
343 }
344
345 // set definition part
Gabor Greif4136e7b2009-09-23 02:46:12 +0000346 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage()) {
Dale Johannesened1ec3a2008-05-23 00:15:10 +0000347 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000348 }
Gabor Greif4136e7b2009-09-23 02:46:12 +0000349 else if (def->hasCommonLinkage()) {
Dale Johannesen6a6f2dd2008-05-16 22:46:40 +0000350 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
351 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000352 else {
353 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
354 }
355
356 // set scope part
Gabor Greif4136e7b2009-09-23 02:46:12 +0000357 if (def->hasHiddenVisibility())
Nick Kledzik77595fc2008-02-26 20:26:43 +0000358 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000359 else if (def->hasProtectedVisibility())
Nick Lewycky4fd40e82008-11-29 22:49:59 +0000360 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000361 else if (def->hasExternalLinkage() || def->hasWeakLinkage()
362 || def->hasLinkOnceLinkage() || def->hasCommonLinkage())
Nick Kledzik77595fc2008-02-26 20:26:43 +0000363 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
364 else
365 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
366
367 // add to table of symbols
368 NameAndAttributes info;
369 info.name = symbolName;
370 info.attributes = (lto_symbol_attributes)attr;
371 _symbols.push_back(info);
372 _defines[info.name] = 1;
373}
374
Devang Patelc2aec572008-07-16 18:06:52 +0000375void LTOModule::addAsmGlobalSymbol(const char *name) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000376 // only add new define if not already defined
Gabor Greif4136e7b2009-09-23 02:46:12 +0000377 if (_defines.count(name) == 0)
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000378 return;
379
380 // string is owned by _defines
381 const char *symbolName = ::strdup(name);
382 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
383 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
384 NameAndAttributes info;
385 info.name = symbolName;
386 info.attributes = (lto_symbol_attributes)attr;
387 _symbols.push_back(info);
388 _defines[info.name] = 1;
Devang Patelc2aec572008-07-16 18:06:52 +0000389}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000390
Nick Kledzikef194ed2008-02-27 22:25:36 +0000391void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler)
392{
Nick Kledzik77595fc2008-02-26 20:26:43 +0000393 // ignore all llvm.* symbols
Daniel Dunbar460f6562009-07-26 09:48:23 +0000394 if (decl->getName().startswith("llvm."))
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000395 return;
396
Nick Lewycky485ded02009-07-09 06:03:04 +0000397 // ignore all aliases
398 if (isa<GlobalAlias>(decl))
399 return;
400
Chris Lattner46934042010-01-16 18:12:14 +0000401 std::string name = mangler.getNameWithPrefix(decl);
Rafael Espindola7431af02009-04-24 16:55:21 +0000402
403 // we already have the symbol
404 if (_undefines.find(name) != _undefines.end())
405 return;
406
407 NameAndAttributes info;
408 // string is owned by _undefines
Nick Lewyckydb1e9982009-07-28 06:53:50 +0000409 info.name = ::strdup(name.c_str());
Rafael Espindola7431af02009-04-24 16:55:21 +0000410 if (decl->hasExternalWeakLinkage())
411 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
412 else
413 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
414 _undefines[name] = info;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000415}
416
417
418
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000419// Find external symbols referenced by VALUE. This is a recursive function.
Nick Kledzik77595fc2008-02-26 20:26:43 +0000420void LTOModule::findExternalRefs(Value* value, Mangler &mangler) {
421
422 if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) {
Gabor Greif4136e7b2009-09-23 02:46:12 +0000423 if (!gv->hasExternalLinkage())
Nick Kledzikef194ed2008-02-27 22:25:36 +0000424 addPotentialUndefinedSymbol(gv, mangler);
Nick Kledziked185d62008-05-28 00:06:14 +0000425 // If this is a variable definition, do not recursively process
426 // initializer. It might contain a reference to this variable
427 // and cause an infinite loop. The initializer will be
428 // processed in addDefinedDataSymbol().
429 return;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000430 }
Gabor Greif4136e7b2009-09-23 02:46:12 +0000431
Nick Kledzik77595fc2008-02-26 20:26:43 +0000432 // GlobalValue, even with InternalLinkage type, may have operands with
433 // ExternalLinkage type. Do not ignore these operands.
434 if (Constant* c = dyn_cast<Constant>(value)) {
Gabor Greif4136e7b2009-09-23 02:46:12 +0000435 // Handle ConstantExpr, ConstantStruct, ConstantArry etc.
Nick Kledzik77595fc2008-02-26 20:26:43 +0000436 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
437 findExternalRefs(c->getOperand(i), mangler);
438 }
439}
440
Nick Kledzikef194ed2008-02-27 22:25:36 +0000441void LTOModule::lazyParseSymbols()
Nick Kledzik77595fc2008-02-26 20:26:43 +0000442{
Gabor Greif4136e7b2009-09-23 02:46:12 +0000443 if (!_symbolsParsed) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000444 _symbolsParsed = true;
445
446 // Use mangler to add GlobalPrefix to names to match linker names.
Chris Lattner5ef31a02010-03-12 18:44:54 +0000447 MCContext Context(*_target->getMCAsmInfo());
Chris Lattnerb87c3052010-03-12 20:47:28 +0000448 Mangler mangler(Context, *_target->getTargetData());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000449
450 // add functions
451 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
Gabor Greif4136e7b2009-09-23 02:46:12 +0000452 if (f->isDeclaration())
Nick Kledzikef194ed2008-02-27 22:25:36 +0000453 addPotentialUndefinedSymbol(f, mangler);
454 else
455 addDefinedFunctionSymbol(f, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000456 }
457
458 // add data
459 for (Module::global_iterator v = _module->global_begin(),
460 e = _module->global_end(); v != e; ++v) {
Gabor Greif4136e7b2009-09-23 02:46:12 +0000461 if (v->isDeclaration())
Nick Kledzikef194ed2008-02-27 22:25:36 +0000462 addPotentialUndefinedSymbol(v, mangler);
463 else
464 addDefinedDataSymbol(v, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000465 }
466
Devang Patelc2aec572008-07-16 18:06:52 +0000467 // add asm globals
468 const std::string &inlineAsm = _module->getModuleInlineAsm();
469 const std::string glbl = ".globl";
470 std::string asmSymbolName;
471 std::string::size_type pos = inlineAsm.find(glbl, 0);
472 while (pos != std::string::npos) {
473 // eat .globl
474 pos = pos + 6;
475
476 // skip white space between .globl and symbol name
477 std::string::size_type pbegin = inlineAsm.find_first_not_of(' ', pos);
478 if (pbegin == std::string::npos)
479 break;
480
481 // find end-of-line
482 std::string::size_type pend = inlineAsm.find_first_of('\n', pbegin);
483 if (pend == std::string::npos)
484 break;
485
Devang Patel41390702008-07-16 19:49:09 +0000486 asmSymbolName.assign(inlineAsm, pbegin, pend - pbegin);
Devang Patelc2aec572008-07-16 18:06:52 +0000487 addAsmGlobalSymbol(asmSymbolName.c_str());
488
489 // search next .globl
490 pos = inlineAsm.find(glbl, pend);
491 }
492
Nick Kledzik77595fc2008-02-26 20:26:43 +0000493 // make symbols for all undefines
Rafael Espindola7431af02009-04-24 16:55:21 +0000494 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000495 it != _undefines.end(); ++it) {
496 // if this symbol also has a definition, then don't make an undefine
497 // because it is a tentative definition
Gabor Greif4136e7b2009-09-23 02:46:12 +0000498 if (_defines.count(it->getKey()) == 0) {
Rafael Espindola7431af02009-04-24 16:55:21 +0000499 NameAndAttributes info = it->getValue();
500 _symbols.push_back(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000501 }
502 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000503 }
504}
505
506
507uint32_t LTOModule::getSymbolCount()
508{
509 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000510 return _symbols.size();
511}
512
513
514lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index)
515{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000516 lazyParseSymbols();
Gabor Greif4136e7b2009-09-23 02:46:12 +0000517 if (index < _symbols.size())
Nick Kledzik77595fc2008-02-26 20:26:43 +0000518 return _symbols[index].attributes;
519 else
520 return lto_symbol_attributes(0);
521}
522
523const char* LTOModule::getSymbolName(uint32_t index)
524{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000525 lazyParseSymbols();
Gabor Greif4136e7b2009-09-23 02:46:12 +0000526 if (index < _symbols.size())
Nick Kledzik77595fc2008-02-26 20:26:43 +0000527 return _symbols[index].name;
528 else
529 return NULL;
530}