1 //===-LTOModule.cpp - LLVM Link Time Optimizer ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "LTOModule.h"
17 #include "llvm/Constants.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Module.h"
20 #include "llvm/ModuleProvider.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/Bitcode/ReaderWriter.h"
24 #include "llvm/Support/SystemUtils.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/System/Host.h"
28 #include "llvm/System/Path.h"
29 #include "llvm/System/Process.h"
30 #include "llvm/Target/Mangler.h"
31 #include "llvm/Target/SubtargetFeature.h"
32 #include "llvm/MC/MCAsmInfo.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Target/TargetRegistry.h"
35 #include "llvm/Target/TargetSelect.h"
39 bool LTOModule::isBitcodeFile(const void* mem, size_t length)
41 return llvm::sys::IdentifyFileType((char*)mem, length)
42 == llvm::sys::Bitcode_FileType;
45 bool LTOModule::isBitcodeFile(const char* path)
47 return llvm::sys::Path(path).isBitcodeFile();
50 bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length,
51 const char* triplePrefix)
53 MemoryBuffer* buffer = makeBuffer(mem, length);
56 return isTargetMatch(buffer, triplePrefix);
60 bool LTOModule::isBitcodeFileForTarget(const char* path,
61 const char* triplePrefix)
63 MemoryBuffer *buffer = MemoryBuffer::getFile(path);
66 return isTargetMatch(buffer, triplePrefix);
69 // takes ownership of buffer
70 bool LTOModule::isTargetMatch(MemoryBuffer* buffer, const char* triplePrefix)
72 OwningPtr<ModuleProvider> mp(getBitcodeModuleProvider(buffer,
74 // on success, mp owns buffer and both are deleted at end of this method
79 std::string actualTarget = mp->getModule()->getTargetTriple();
80 return (strncmp(actualTarget.c_str(), triplePrefix,
81 strlen(triplePrefix)) == 0);
85 LTOModule::LTOModule(Module* m, TargetMachine* t)
86 : _module(m), _target(t), _symbolsParsed(false)
90 LTOModule* LTOModule::makeLTOModule(const char* path,
93 OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, &errMsg));
96 return makeLTOModule(buffer.get(), errMsg);
99 /// makeBuffer - create a MemoryBuffer from a memory range.
100 /// MemoryBuffer requires the byte past end of the buffer to be a zero.
101 /// We might get lucky and already be that way, otherwise make a copy.
102 /// Also if next byte is on a different page, don't assume it is readable.
103 MemoryBuffer* LTOModule::makeBuffer(const void* mem, size_t length)
105 const char* startPtr = (char*)mem;
106 const char* endPtr = startPtr+length;
107 if ((((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0)
109 return MemoryBuffer::getMemBufferCopy(startPtr, endPtr);
111 return MemoryBuffer::getMemBuffer(startPtr, endPtr);
115 LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length,
118 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
121 return makeLTOModule(buffer.get(), errMsg);
124 LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer,
127 InitializeAllTargets();
129 // parse bitcode buffer
130 OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
134 std::string Triple = m->getTargetTriple();
136 Triple = sys::getHostTriple();
138 // find machine architecture for this module
139 const Target* march = TargetRegistry::lookupTarget(Triple, errMsg);
143 // construct LTModule, hand over ownership of module and target
144 const std::string FeatureStr =
145 SubtargetFeatures::getDefaultSubtargetFeatures(llvm::Triple(Triple));
146 TargetMachine* target = march->createTargetMachine(Triple, FeatureStr);
147 return new LTOModule(m.take(), target);
151 const char* LTOModule::getTargetTriple()
153 return _module->getTargetTriple().c_str();
156 void LTOModule::addDefinedFunctionSymbol(Function* f, Mangler &mangler)
158 // add to list of defined symbols
159 addDefinedSymbol(f, mangler, true);
161 // add external symbols referenced by this function.
162 for (Function::iterator b = f->begin(); b != f->end(); ++b) {
163 for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) {
164 for (unsigned count = 0, total = i->getNumOperands();
165 count != total; ++count) {
166 findExternalRefs(i->getOperand(count), mangler);
172 // get string that data pointer points to
173 bool LTOModule::objcClassNameFromExpression(Constant* c, std::string& name)
175 if (ConstantExpr* ce = dyn_cast<ConstantExpr>(c)) {
176 Constant* op = ce->getOperand(0);
177 if (GlobalVariable* gvn = dyn_cast<GlobalVariable>(op)) {
178 Constant* cn = gvn->getInitializer();
179 if (ConstantArray* ca = dyn_cast<ConstantArray>(cn)) {
180 if (ca->isCString()) {
181 name = ".objc_class_name_" + ca->getAsString();
190 // parse i386/ppc ObjC class data structure
191 void LTOModule::addObjCClass(GlobalVariable* clgv)
193 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
194 // second slot in __OBJC,__class is pointer to superclass name
195 std::string superclassName;
196 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
197 NameAndAttributes info;
198 if (_undefines.find(superclassName.c_str()) == _undefines.end()) {
199 const char* symbolName = ::strdup(superclassName.c_str());
200 info.name = ::strdup(symbolName);
201 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
202 // string is owned by _undefines
203 _undefines[info.name] = info;
206 // third slot in __OBJC,__class is pointer to class name
207 std::string className;
208 if (objcClassNameFromExpression(c->getOperand(2), className)) {
209 const char* symbolName = ::strdup(className.c_str());
210 NameAndAttributes info;
211 info.name = symbolName;
212 info.attributes = (lto_symbol_attributes)
213 (LTO_SYMBOL_PERMISSIONS_DATA |
214 LTO_SYMBOL_DEFINITION_REGULAR |
215 LTO_SYMBOL_SCOPE_DEFAULT);
216 _symbols.push_back(info);
217 _defines[info.name] = 1;
223 // parse i386/ppc ObjC category data structure
224 void LTOModule::addObjCCategory(GlobalVariable* clgv)
226 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
227 // second slot in __OBJC,__category is pointer to target class name
228 std::string targetclassName;
229 if (objcClassNameFromExpression(c->getOperand(1), targetclassName)) {
230 NameAndAttributes info;
231 if (_undefines.find(targetclassName.c_str()) == _undefines.end()) {
232 const char* symbolName = ::strdup(targetclassName.c_str());
233 info.name = ::strdup(symbolName);
234 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
235 // string is owned by _undefines
236 _undefines[info.name] = info;
243 // parse i386/ppc ObjC class list data structure
244 void LTOModule::addObjCClassRef(GlobalVariable* clgv)
246 std::string targetclassName;
247 if (objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) {
248 NameAndAttributes info;
249 if (_undefines.find(targetclassName.c_str()) == _undefines.end()) {
250 const char* symbolName = ::strdup(targetclassName.c_str());
251 info.name = ::strdup(symbolName);
252 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
253 // string is owned by _undefines
254 _undefines[info.name] = info;
260 void LTOModule::addDefinedDataSymbol(GlobalValue* v, Mangler& mangler)
262 // add to list of defined symbols
263 addDefinedSymbol(v, mangler, false);
265 // Special case i386/ppc ObjC data structures in magic sections:
266 // The issue is that the old ObjC object format did some strange
267 // contortions to avoid real linker symbols. For instance, the
268 // ObjC class data structure is allocated statically in the executable
269 // that defines that class. That data structures contains a pointer to
270 // its superclass. But instead of just initializing that part of the
271 // struct to the address of its superclass, and letting the static and
272 // dynamic linkers do the rest, the runtime works by having that field
273 // instead point to a C-string that is the name of the superclass.
274 // At runtime the objc initialization updates that pointer and sets
275 // it to point to the actual super class. As far as the linker
276 // knows it is just a pointer to a string. But then someone wanted the
277 // linker to issue errors at build time if the superclass was not found.
278 // So they figured out a way in mach-o object format to use an absolute
279 // symbols (.objc_class_name_Foo = 0) and a floating reference
280 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
281 // a class was missing.
282 // The following synthesizes the implicit .objc_* symbols for the linker
283 // from the ObjC data structures generated by the front end.
284 if (v->hasSection() /* && isTargetDarwin */) {
285 // special case if this data blob is an ObjC class definition
286 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
287 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
292 // special case if this data blob is an ObjC category definition
293 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
294 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
299 // special case if this data blob is the list of referenced classes
300 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
301 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
307 // add external symbols referenced by this data.
308 for (unsigned count = 0, total = v->getNumOperands();
309 count != total; ++count) {
310 findExternalRefs(v->getOperand(count), mangler);
315 void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler,
318 // ignore all llvm.* symbols
319 if (def->getName().startswith("llvm."))
322 // string is owned by _defines
323 const char* symbolName = ::strdup(mangler.getNameWithPrefix(def).c_str());
325 // set alignment part log2() can have rounding errors
326 uint32_t align = def->getAlignment();
327 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
329 // set permissions part
331 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
333 GlobalVariable* gv = dyn_cast<GlobalVariable>(def);
334 if (gv && gv->isConstant())
335 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
337 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
340 // set definition part
341 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage()) {
342 attr |= LTO_SYMBOL_DEFINITION_WEAK;
344 else if (def->hasCommonLinkage()) {
345 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
348 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
352 if (def->hasHiddenVisibility())
353 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
354 else if (def->hasProtectedVisibility())
355 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
356 else if (def->hasExternalLinkage() || def->hasWeakLinkage()
357 || def->hasLinkOnceLinkage() || def->hasCommonLinkage())
358 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
360 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
362 // add to table of symbols
363 NameAndAttributes info;
364 info.name = symbolName;
365 info.attributes = (lto_symbol_attributes)attr;
366 _symbols.push_back(info);
367 _defines[info.name] = 1;
370 void LTOModule::addAsmGlobalSymbol(const char *name) {
371 // only add new define if not already defined
372 if (_defines.count(name) == 0)
375 // string is owned by _defines
376 const char *symbolName = ::strdup(name);
377 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
378 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
379 NameAndAttributes info;
380 info.name = symbolName;
381 info.attributes = (lto_symbol_attributes)attr;
382 _symbols.push_back(info);
383 _defines[info.name] = 1;
386 void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler)
388 // ignore all llvm.* symbols
389 if (decl->getName().startswith("llvm."))
392 // ignore all aliases
393 if (isa<GlobalAlias>(decl))
396 std::string name = mangler.getNameWithPrefix(decl);
398 // we already have the symbol
399 if (_undefines.find(name) != _undefines.end())
402 NameAndAttributes info;
403 // string is owned by _undefines
404 info.name = ::strdup(name.c_str());
405 if (decl->hasExternalWeakLinkage())
406 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
408 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
409 _undefines[name] = info;
414 // Find external symbols referenced by VALUE. This is a recursive function.
415 void LTOModule::findExternalRefs(Value* value, Mangler &mangler) {
417 if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) {
418 if (!gv->hasExternalLinkage())
419 addPotentialUndefinedSymbol(gv, mangler);
420 // If this is a variable definition, do not recursively process
421 // initializer. It might contain a reference to this variable
422 // and cause an infinite loop. The initializer will be
423 // processed in addDefinedDataSymbol().
427 // GlobalValue, even with InternalLinkage type, may have operands with
428 // ExternalLinkage type. Do not ignore these operands.
429 if (Constant* c = dyn_cast<Constant>(value)) {
430 // Handle ConstantExpr, ConstantStruct, ConstantArry etc.
431 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
432 findExternalRefs(c->getOperand(i), mangler);
436 void LTOModule::lazyParseSymbols()
438 if (!_symbolsParsed) {
439 _symbolsParsed = true;
441 // Use mangler to add GlobalPrefix to names to match linker names.
442 Mangler mangler(*_module, _target->getMCAsmInfo()->getGlobalPrefix());
445 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
446 if (f->isDeclaration())
447 addPotentialUndefinedSymbol(f, mangler);
449 addDefinedFunctionSymbol(f, mangler);
453 for (Module::global_iterator v = _module->global_begin(),
454 e = _module->global_end(); v != e; ++v) {
455 if (v->isDeclaration())
456 addPotentialUndefinedSymbol(v, mangler);
458 addDefinedDataSymbol(v, mangler);
462 const std::string &inlineAsm = _module->getModuleInlineAsm();
463 const std::string glbl = ".globl";
464 std::string asmSymbolName;
465 std::string::size_type pos = inlineAsm.find(glbl, 0);
466 while (pos != std::string::npos) {
470 // skip white space between .globl and symbol name
471 std::string::size_type pbegin = inlineAsm.find_first_not_of(' ', pos);
472 if (pbegin == std::string::npos)
476 std::string::size_type pend = inlineAsm.find_first_of('\n', pbegin);
477 if (pend == std::string::npos)
480 asmSymbolName.assign(inlineAsm, pbegin, pend - pbegin);
481 addAsmGlobalSymbol(asmSymbolName.c_str());
483 // search next .globl
484 pos = inlineAsm.find(glbl, pend);
487 // make symbols for all undefines
488 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
489 it != _undefines.end(); ++it) {
490 // if this symbol also has a definition, then don't make an undefine
491 // because it is a tentative definition
492 if (_defines.count(it->getKey()) == 0) {
493 NameAndAttributes info = it->getValue();
494 _symbols.push_back(info);
501 uint32_t LTOModule::getSymbolCount()
504 return _symbols.size();
508 lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index)
511 if (index < _symbols.size())
512 return _symbols[index].attributes;
514 return lto_symbol_attributes(0);
517 const char* LTOModule::getSymbolName(uint32_t index)
520 if (index < _symbols.size())
521 return _symbols[index].name;