blob: 15fb3f549b6805b19a8a20b68dd28d33a924e0f2 [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 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"
Daniel Dunbarff9834a2009-07-16 02:41:19 +000032#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetRegistry.h"
Nick Lewyckyd42b58b2009-07-26 22:16:39 +000034#include "llvm/Target/TargetSelect.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000035
Nick Kledzik77595fc2008-02-26 20:26:43 +000036using namespace llvm;
37
38bool LTOModule::isBitcodeFile(const void* mem, size_t length)
39{
Gabor Greif4136e7b2009-09-23 02:46:12 +000040 return llvm::sys::IdentifyFileType((char*)mem, length)
41 == llvm::sys::Bitcode_FileType;
Nick Kledzik77595fc2008-02-26 20:26:43 +000042}
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);
Gabor Greif4136e7b2009-09-23 02:46:12 +000053 if (!buffer)
Nick Kledzikef194ed2008-02-27 22:25:36 +000054 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{
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000071 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext()));
72 // on success, m owns buffer and both are deleted at end of this method
73 if (!m) {
Nick Kledzikef194ed2008-02-27 22:25:36 +000074 delete buffer;
75 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +000076 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000077 std::string actualTarget = m->getTargetTriple();
Gabor Greif4136e7b2009-09-23 02:46:12 +000078 return (strncmp(actualTarget.c_str(), triplePrefix,
Nick Kledzikef194ed2008-02-27 22:25:36 +000079 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));
Gabor Greif4136e7b2009-09-23 02:46:12 +000092 if (!buffer)
Nick Kledzikef194ed2008-02-27 22:25:36 +000093 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;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000105 if ((((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0)
106 || (*endPtr != 0))
Nick Kledziked185d62008-05-28 00:06:14 +0000107 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));
Gabor Greif4136e7b2009-09-23 02:46:12 +0000117 if (!buffer)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000118 return NULL;
Owen Anderson0e7a5462009-07-02 00:31:14 +0000119 return makeLTOModule(buffer.get(), errMsg);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000120}
121
Owen Anderson31895e72009-07-01 21:22:36 +0000122LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer,
Owen Anderson8b477ed2009-07-01 16:58:40 +0000123 std::string& errMsg)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000124{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000125 InitializeAllTargets();
126
Nick Kledzikef194ed2008-02-27 22:25:36 +0000127 // parse bitcode buffer
Owen Anderson0e7a5462009-07-02 00:31:14 +0000128 OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
Gabor Greif4136e7b2009-09-23 02:46:12 +0000129 if (!m)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000130 return NULL;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000131
132 std::string Triple = m->getTargetTriple();
133 if (Triple.empty())
134 Triple = sys::getHostTriple();
135
Nick Kledzikef194ed2008-02-27 22:25:36 +0000136 // find machine architecture for this module
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000137 const Target* march = TargetRegistry::lookupTarget(Triple, errMsg);
Gabor Greif4136e7b2009-09-23 02:46:12 +0000138 if (!march)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000139 return NULL;
Bill Wendling604a8182008-06-18 06:35:30 +0000140
Nick Kledzikef194ed2008-02-27 22:25:36 +0000141 // construct LTModule, hand over ownership of module and target
Viktor Kutuzove823db82009-11-18 20:20:05 +0000142 const std::string FeatureStr =
143 SubtargetFeatures::getDefaultSubtargetFeatures(llvm::Triple(Triple));
Daniel Dunbar4b3d5722009-08-04 04:08:40 +0000144 TargetMachine* target = march->createTargetMachine(Triple, FeatureStr);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000145 return new LTOModule(m.take(), target);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000146}
147
148
149const char* LTOModule::getTargetTriple()
150{
151 return _module->getTargetTriple().c_str();
152}
153
Nick Kledzikef194ed2008-02-27 22:25:36 +0000154void LTOModule::addDefinedFunctionSymbol(Function* f, Mangler &mangler)
155{
156 // add to list of defined symbols
157 addDefinedSymbol(f, mangler, true);
158
159 // add external symbols referenced by this function.
160 for (Function::iterator b = f->begin(); b != f->end(); ++b) {
161 for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) {
162 for (unsigned count = 0, total = i->getNumOperands();
163 count != total; ++count) {
164 findExternalRefs(i->getOperand(count), mangler);
165 }
166 }
167 }
168}
169
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000170// get string that data pointer points to
171bool LTOModule::objcClassNameFromExpression(Constant* c, std::string& name)
172{
173 if (ConstantExpr* ce = dyn_cast<ConstantExpr>(c)) {
174 Constant* op = ce->getOperand(0);
175 if (GlobalVariable* gvn = dyn_cast<GlobalVariable>(op)) {
176 Constant* cn = gvn->getInitializer();
177 if (ConstantArray* ca = dyn_cast<ConstantArray>(cn)) {
Gabor Greif4136e7b2009-09-23 02:46:12 +0000178 if (ca->isCString()) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000179 name = ".objc_class_name_" + ca->getAsString();
180 return true;
181 }
182 }
183 }
184 }
185 return false;
186}
187
188// parse i386/ppc ObjC class data structure
189void LTOModule::addObjCClass(GlobalVariable* clgv)
190{
191 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
192 // second slot in __OBJC,__class is pointer to superclass name
193 std::string superclassName;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000194 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000195 NameAndAttributes info;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000196 if (_undefines.find(superclassName.c_str()) == _undefines.end()) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000197 const char* symbolName = ::strdup(superclassName.c_str());
198 info.name = ::strdup(symbolName);
199 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
200 // string is owned by _undefines
201 _undefines[info.name] = info;
202 }
203 }
204 // third slot in __OBJC,__class is pointer to class name
205 std::string className;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000206 if (objcClassNameFromExpression(c->getOperand(2), className)) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000207 const char* symbolName = ::strdup(className.c_str());
208 NameAndAttributes info;
209 info.name = symbolName;
210 info.attributes = (lto_symbol_attributes)
211 (LTO_SYMBOL_PERMISSIONS_DATA |
212 LTO_SYMBOL_DEFINITION_REGULAR |
213 LTO_SYMBOL_SCOPE_DEFAULT);
214 _symbols.push_back(info);
215 _defines[info.name] = 1;
216 }
217 }
218}
219
220
221// parse i386/ppc ObjC category data structure
222void LTOModule::addObjCCategory(GlobalVariable* clgv)
223{
224 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
225 // second slot in __OBJC,__category is pointer to target class name
226 std::string targetclassName;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000227 if (objcClassNameFromExpression(c->getOperand(1), targetclassName)) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000228 NameAndAttributes info;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000229 if (_undefines.find(targetclassName.c_str()) == _undefines.end()) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000230 const char* symbolName = ::strdup(targetclassName.c_str());
231 info.name = ::strdup(symbolName);
232 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
233 // string is owned by _undefines
234 _undefines[info.name] = info;
235 }
236 }
237 }
238}
239
240
241// parse i386/ppc ObjC class list data structure
242void LTOModule::addObjCClassRef(GlobalVariable* clgv)
243{
244 std::string targetclassName;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000245 if (objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000246 NameAndAttributes info;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000247 if (_undefines.find(targetclassName.c_str()) == _undefines.end()) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000248 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
258void LTOModule::addDefinedDataSymbol(GlobalValue* v, Mangler& mangler)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000259{
260 // add to list of defined symbols
261 addDefinedSymbol(v, mangler, false);
262
Nick Kledzik4bdf7302009-06-01 23:41:09 +0000263 // Special case i386/ppc ObjC data structures in magic sections:
264 // The issue is that the old ObjC object format did some strange
265 // contortions to avoid real linker symbols. For instance, the
266 // ObjC class data structure is allocated statically in the executable
267 // that defines that class. That data structures contains a pointer to
268 // its superclass. But instead of just initializing that part of the
269 // struct to the address of its superclass, and letting the static and
270 // dynamic linkers do the rest, the runtime works by having that field
271 // instead point to a C-string that is the name of the superclass.
272 // At runtime the objc initialization updates that pointer and sets
273 // it to point to the actual super class. As far as the linker
274 // knows it is just a pointer to a string. But then someone wanted the
275 // linker to issue errors at build time if the superclass was not found.
276 // So they figured out a way in mach-o object format to use an absolute
277 // symbols (.objc_class_name_Foo = 0) and a floating reference
278 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
279 // a class was missing.
280 // The following synthesizes the implicit .objc_* symbols for the linker
281 // from the ObjC data structures generated by the front end.
Gabor Greif4136e7b2009-09-23 02:46:12 +0000282 if (v->hasSection() /* && isTargetDarwin */) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000283 // special case if this data blob is an ObjC class definition
Gabor Greif4136e7b2009-09-23 02:46:12 +0000284 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000285 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
286 addObjCClass(gv);
287 }
288 }
289
290 // special case if this data blob is an ObjC category definition
Gabor Greif4136e7b2009-09-23 02:46:12 +0000291 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000292 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
293 addObjCCategory(gv);
294 }
295 }
296
297 // special case if this data blob is the list of referenced classes
Gabor Greif4136e7b2009-09-23 02:46:12 +0000298 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000299 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
300 addObjCClassRef(gv);
301 }
302 }
303 }
304
Nick Kledzikef194ed2008-02-27 22:25:36 +0000305 // add external symbols referenced by this data.
Nick Kledzik9178a652008-05-27 22:07:08 +0000306 for (unsigned count = 0, total = v->getNumOperands();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000307 count != total; ++count) {
308 findExternalRefs(v->getOperand(count), mangler);
309 }
310}
311
312
Nick Kledzik77595fc2008-02-26 20:26:43 +0000313void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler,
Nick Lewycky485ded02009-07-09 06:03:04 +0000314 bool isFunction)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000315{
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000316 // ignore all llvm.* symbols
Daniel Dunbar460f6562009-07-26 09:48:23 +0000317 if (def->getName().startswith("llvm."))
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000318 return;
319
Nick Kledzikef194ed2008-02-27 22:25:36 +0000320 // string is owned by _defines
Chris Lattner46934042010-01-16 18:12:14 +0000321 const char* symbolName = ::strdup(mangler.getNameWithPrefix(def).c_str());
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000322
Nick Kledzik77595fc2008-02-26 20:26:43 +0000323 // set alignment part log2() can have rounding errors
324 uint32_t align = def->getAlignment();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000325 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000326
327 // set permissions part
Gabor Greif4136e7b2009-09-23 02:46:12 +0000328 if (isFunction)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000329 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
330 else {
331 GlobalVariable* gv = dyn_cast<GlobalVariable>(def);
Gabor Greif4136e7b2009-09-23 02:46:12 +0000332 if (gv && gv->isConstant())
Nick Kledzik77595fc2008-02-26 20:26:43 +0000333 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
334 else
335 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
336 }
337
338 // set definition part
Gabor Greif4136e7b2009-09-23 02:46:12 +0000339 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage()) {
Dale Johannesened1ec3a2008-05-23 00:15:10 +0000340 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000341 }
Gabor Greif4136e7b2009-09-23 02:46:12 +0000342 else if (def->hasCommonLinkage()) {
Dale Johannesen6a6f2dd2008-05-16 22:46:40 +0000343 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
344 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000345 else {
346 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
347 }
348
349 // set scope part
Gabor Greif4136e7b2009-09-23 02:46:12 +0000350 if (def->hasHiddenVisibility())
Nick Kledzik77595fc2008-02-26 20:26:43 +0000351 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000352 else if (def->hasProtectedVisibility())
Nick Lewycky4fd40e82008-11-29 22:49:59 +0000353 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Gabor Greif4136e7b2009-09-23 02:46:12 +0000354 else if (def->hasExternalLinkage() || def->hasWeakLinkage()
355 || def->hasLinkOnceLinkage() || def->hasCommonLinkage())
Nick Kledzik77595fc2008-02-26 20:26:43 +0000356 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
357 else
358 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
359
360 // add to table of symbols
361 NameAndAttributes info;
362 info.name = symbolName;
363 info.attributes = (lto_symbol_attributes)attr;
364 _symbols.push_back(info);
365 _defines[info.name] = 1;
366}
367
Devang Patelc2aec572008-07-16 18:06:52 +0000368void LTOModule::addAsmGlobalSymbol(const char *name) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000369 // only add new define if not already defined
Gabor Greif4136e7b2009-09-23 02:46:12 +0000370 if (_defines.count(name) == 0)
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000371 return;
372
373 // string is owned by _defines
374 const char *symbolName = ::strdup(name);
375 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
376 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
377 NameAndAttributes info;
378 info.name = symbolName;
379 info.attributes = (lto_symbol_attributes)attr;
380 _symbols.push_back(info);
381 _defines[info.name] = 1;
Devang Patelc2aec572008-07-16 18:06:52 +0000382}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000383
Nick Kledzikef194ed2008-02-27 22:25:36 +0000384void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler)
385{
Nick Kledzik77595fc2008-02-26 20:26:43 +0000386 // ignore all llvm.* symbols
Daniel Dunbar460f6562009-07-26 09:48:23 +0000387 if (decl->getName().startswith("llvm."))
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000388 return;
389
Nick Lewycky485ded02009-07-09 06:03:04 +0000390 // ignore all aliases
391 if (isa<GlobalAlias>(decl))
392 return;
393
Chris Lattner46934042010-01-16 18:12:14 +0000394 std::string name = mangler.getNameWithPrefix(decl);
Rafael Espindola7431af02009-04-24 16:55:21 +0000395
396 // we already have the symbol
397 if (_undefines.find(name) != _undefines.end())
398 return;
399
400 NameAndAttributes info;
401 // string is owned by _undefines
Nick Lewyckydb1e9982009-07-28 06:53:50 +0000402 info.name = ::strdup(name.c_str());
Rafael Espindola7431af02009-04-24 16:55:21 +0000403 if (decl->hasExternalWeakLinkage())
404 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
405 else
406 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
407 _undefines[name] = info;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000408}
409
410
411
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000412// Find external symbols referenced by VALUE. This is a recursive function.
Nick Kledzik77595fc2008-02-26 20:26:43 +0000413void LTOModule::findExternalRefs(Value* value, Mangler &mangler) {
414
415 if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) {
Gabor Greif4136e7b2009-09-23 02:46:12 +0000416 if (!gv->hasExternalLinkage())
Nick Kledzikef194ed2008-02-27 22:25:36 +0000417 addPotentialUndefinedSymbol(gv, mangler);
Nick Kledziked185d62008-05-28 00:06:14 +0000418 // If this is a variable definition, do not recursively process
419 // initializer. It might contain a reference to this variable
420 // and cause an infinite loop. The initializer will be
421 // processed in addDefinedDataSymbol().
422 return;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000423 }
Gabor Greif4136e7b2009-09-23 02:46:12 +0000424
Nick Kledzik77595fc2008-02-26 20:26:43 +0000425 // GlobalValue, even with InternalLinkage type, may have operands with
426 // ExternalLinkage type. Do not ignore these operands.
427 if (Constant* c = dyn_cast<Constant>(value)) {
Gabor Greif4136e7b2009-09-23 02:46:12 +0000428 // Handle ConstantExpr, ConstantStruct, ConstantArry etc.
Nick Kledzik77595fc2008-02-26 20:26:43 +0000429 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
430 findExternalRefs(c->getOperand(i), mangler);
431 }
432}
433
Nick Kledzikef194ed2008-02-27 22:25:36 +0000434void LTOModule::lazyParseSymbols()
Nick Kledzik77595fc2008-02-26 20:26:43 +0000435{
Gabor Greif4136e7b2009-09-23 02:46:12 +0000436 if (!_symbolsParsed) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000437 _symbolsParsed = true;
438
439 // Use mangler to add GlobalPrefix to names to match linker names.
Chris Lattnerc0dba722010-01-17 18:22:35 +0000440 Mangler mangler(*_target->getMCAsmInfo());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000441
442 // add functions
443 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
Gabor Greif4136e7b2009-09-23 02:46:12 +0000444 if (f->isDeclaration())
Nick Kledzikef194ed2008-02-27 22:25:36 +0000445 addPotentialUndefinedSymbol(f, mangler);
446 else
447 addDefinedFunctionSymbol(f, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000448 }
449
450 // add data
451 for (Module::global_iterator v = _module->global_begin(),
452 e = _module->global_end(); v != e; ++v) {
Gabor Greif4136e7b2009-09-23 02:46:12 +0000453 if (v->isDeclaration())
Nick Kledzikef194ed2008-02-27 22:25:36 +0000454 addPotentialUndefinedSymbol(v, mangler);
455 else
456 addDefinedDataSymbol(v, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000457 }
458
Devang Patelc2aec572008-07-16 18:06:52 +0000459 // add asm globals
460 const std::string &inlineAsm = _module->getModuleInlineAsm();
461 const std::string glbl = ".globl";
462 std::string asmSymbolName;
463 std::string::size_type pos = inlineAsm.find(glbl, 0);
464 while (pos != std::string::npos) {
465 // eat .globl
466 pos = pos + 6;
467
468 // skip white space between .globl and symbol name
469 std::string::size_type pbegin = inlineAsm.find_first_not_of(' ', pos);
470 if (pbegin == std::string::npos)
471 break;
472
473 // find end-of-line
474 std::string::size_type pend = inlineAsm.find_first_of('\n', pbegin);
475 if (pend == std::string::npos)
476 break;
477
Devang Patel41390702008-07-16 19:49:09 +0000478 asmSymbolName.assign(inlineAsm, pbegin, pend - pbegin);
Devang Patelc2aec572008-07-16 18:06:52 +0000479 addAsmGlobalSymbol(asmSymbolName.c_str());
480
481 // search next .globl
482 pos = inlineAsm.find(glbl, pend);
483 }
484
Nick Kledzik77595fc2008-02-26 20:26:43 +0000485 // make symbols for all undefines
Rafael Espindola7431af02009-04-24 16:55:21 +0000486 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000487 it != _undefines.end(); ++it) {
488 // if this symbol also has a definition, then don't make an undefine
489 // because it is a tentative definition
Gabor Greif4136e7b2009-09-23 02:46:12 +0000490 if (_defines.count(it->getKey()) == 0) {
Rafael Espindola7431af02009-04-24 16:55:21 +0000491 NameAndAttributes info = it->getValue();
492 _symbols.push_back(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000493 }
494 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000495 }
496}
497
498
499uint32_t LTOModule::getSymbolCount()
500{
501 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000502 return _symbols.size();
503}
504
505
506lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index)
507{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000508 lazyParseSymbols();
Gabor Greif4136e7b2009-09-23 02:46:12 +0000509 if (index < _symbols.size())
Nick Kledzik77595fc2008-02-26 20:26:43 +0000510 return _symbols[index].attributes;
511 else
512 return lto_symbol_attributes(0);
513}
514
515const char* LTOModule::getSymbolName(uint32_t index)
516{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000517 lazyParseSymbols();
Gabor Greif4136e7b2009-09-23 02:46:12 +0000518 if (index < _symbols.size())
Nick Kledzik77595fc2008-02-26 20:26:43 +0000519 return _symbols[index].name;
520 else
521 return NULL;
522}