Chris Lattner | 2eff505 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 1 | //===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===// |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 7 | // |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 10 | // This file implements the Link Time Optimization library. This library is |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 11 | // intended to be used by linker to optimize code at link time. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Nick Kledzik | 91a6dcf | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 15 | #include "LTOModule.h" |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Owen Anderson | 6773d38 | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 17 | #include "llvm/LLVMContext.h" |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 19 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | 7b26fce | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCExpr.h" |
| 22 | #include "llvm/MC/MCInst.h" |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCStreamer.h" |
Evan Cheng | 91111d2 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSubtargetInfo.h" |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSymbol.h" |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCTargetAsmParser.h" |
Bill Wendling | 8f6c8a9 | 2012-03-30 23:26:06 +0000 | [diff] [blame^] | 27 | #include "llvm/MC/SubtargetFeature.h" |
| 28 | #include "llvm/MC/MCParser/MCAsmParser.h" |
Daniel Dunbar | 4580638 | 2009-07-16 02:41:19 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetRegisterInfo.h" |
Bill Wendling | 8f6c8a9 | 2012-03-30 23:26:06 +0000 | [diff] [blame^] | 31 | #include "llvm/Support/Host.h" |
| 32 | #include "llvm/Support/MathExtras.h" |
| 33 | #include "llvm/Support/MemoryBuffer.h" |
| 34 | #include "llvm/Support/Path.h" |
| 35 | #include "llvm/Support/Process.h" |
| 36 | #include "llvm/Support/SourceMgr.h" |
| 37 | #include "llvm/Support/SystemUtils.h" |
| 38 | #include "llvm/Support/TargetRegistry.h" |
| 39 | #include "llvm/Support/TargetSelect.h" |
| 40 | #include "llvm/Support/system_error.h" |
| 41 | #include "llvm/ADT/OwningPtr.h" |
| 42 | #include "llvm/ADT/Triple.h" |
| 43 | |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 46 | LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t) |
| 47 | : _module(m), _target(t), |
| 48 | _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL), |
| 49 | _mangler(_context, *_target->getTargetData()) {} |
| 50 | |
| 51 | /// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM |
| 52 | /// bitcode. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 53 | bool LTOModule::isBitcodeFile(const void *mem, size_t length) { |
| 54 | return llvm::sys::IdentifyFileType((char*)mem, length) |
| 55 | == llvm::sys::Bitcode_FileType; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 58 | bool LTOModule::isBitcodeFile(const char *path) { |
| 59 | return llvm::sys::Path(path).isBitcodeFile(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 62 | /// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is |
| 63 | /// LLVM bitcode for the specified triple. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 64 | bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length, |
| 65 | const char *triplePrefix) { |
| 66 | MemoryBuffer *buffer = makeBuffer(mem, length); |
| 67 | if (!buffer) |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 68 | return false; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 69 | return isTargetMatch(buffer, triplePrefix); |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 72 | bool LTOModule::isBitcodeFileForTarget(const char *path, |
| 73 | const char *triplePrefix) { |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 74 | OwningPtr<MemoryBuffer> buffer; |
| 75 | if (MemoryBuffer::getFile(path, buffer)) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 76 | return false; |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 77 | return isTargetMatch(buffer.take(), triplePrefix); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 80 | /// isTargetMatch - Returns 'true' if the memory buffer is for the specified |
| 81 | /// target triple. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 82 | bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) { |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 83 | std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext()); |
| 84 | delete buffer; |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 85 | return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 88 | /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of |
| 89 | /// the buffer. |
| 90 | LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) { |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 91 | OwningPtr<MemoryBuffer> buffer; |
| 92 | if (error_code ec = MemoryBuffer::getFile(path, buffer)) { |
Michael J. Spencer | d422723 | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 93 | errMsg = ec.message(); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 94 | return NULL; |
Michael J. Spencer | d422723 | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 95 | } |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 96 | return makeLTOModule(buffer.take(), errMsg); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 99 | LTOModule *LTOModule::makeLTOModule(int fd, const char *path, |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 100 | size_t size, std::string &errMsg) { |
Rafael Espindola | b39c7c7 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 101 | return makeLTOModule(fd, path, size, size, 0, errMsg); |
| 102 | } |
| 103 | |
| 104 | LTOModule *LTOModule::makeLTOModule(int fd, const char *path, |
| 105 | size_t file_size, |
| 106 | size_t map_size, |
| 107 | off_t offset, |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 108 | std::string &errMsg) { |
| 109 | OwningPtr<MemoryBuffer> buffer; |
Rafael Espindola | b39c7c7 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 110 | if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size, |
| 111 | map_size, offset, false)) { |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 112 | errMsg = ec.message(); |
| 113 | return NULL; |
| 114 | } |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 115 | return makeLTOModule(buffer.take(), errMsg); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 118 | LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length, |
| 119 | std::string &errMsg) { |
| 120 | OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length)); |
| 121 | if (!buffer) |
| 122 | return NULL; |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 123 | return makeLTOModule(buffer.take(), errMsg); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer, |
| 127 | std::string &errMsg) { |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 128 | static bool Initialized = false; |
| 129 | if (!Initialized) { |
| 130 | InitializeAllTargets(); |
Evan Cheng | 8c886a4 | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 131 | InitializeAllTargetMCs(); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 132 | InitializeAllAsmParsers(); |
| 133 | Initialized = true; |
| 134 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 135 | |
| 136 | // parse bitcode buffer |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 137 | OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(), |
| 138 | &errMsg)); |
| 139 | if (!m) { |
| 140 | delete buffer; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 141 | return NULL; |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 142 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 143 | |
| 144 | std::string Triple = m->getTargetTriple(); |
| 145 | if (Triple.empty()) |
Sebastian Pop | 94441fb | 2011-11-01 21:32:20 +0000 | [diff] [blame] | 146 | Triple = sys::getDefaultTargetTriple(); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 147 | |
| 148 | // find machine architecture for this module |
| 149 | const Target *march = TargetRegistry::lookupTarget(Triple, errMsg); |
| 150 | if (!march) |
| 151 | return NULL; |
| 152 | |
Nick Lewycky | 364c04a | 2011-04-21 01:54:08 +0000 | [diff] [blame] | 153 | // construct LTOModule, hand over ownership of module and target |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 154 | SubtargetFeatures Features; |
Evan Cheng | fe6e405 | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 155 | Features.getDefaultSubtargetFeatures(llvm::Triple(Triple)); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 156 | std::string FeatureStr = Features.getString(); |
Evan Cheng | fe6e405 | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 157 | std::string CPU; |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 158 | TargetOptions Options; |
| 159 | TargetMachine *target = march->createTargetMachine(Triple, CPU, FeatureStr, |
| 160 | Options); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 161 | LTOModule *Ret = new LTOModule(m.take(), target); |
Bill Wendling | 7e58b38 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 162 | if (Ret->parseSymbols(errMsg)) { |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 163 | delete Ret; |
| 164 | return NULL; |
| 165 | } |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 166 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 167 | return Ret; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 170 | /// makeBuffer - Create a MemoryBuffer from a memory range. |
| 171 | MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) { |
| 172 | const char *startPtr = (char*)mem; |
| 173 | return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 176 | /// objcClassNameFromExpression - Get string that the data pointer points to. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 177 | bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) { |
| 178 | if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) { |
| 179 | Constant *op = ce->getOperand(0); |
| 180 | if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) { |
| 181 | Constant *cn = gvn->getInitializer(); |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 182 | if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 183 | if (ca->isCString()) { |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 184 | name = ".objc_class_name_" + ca->getAsCString().str(); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 185 | return true; |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 186 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 187 | } |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 188 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 193 | /// addObjCClass - Parse i386/ppc ObjC class data structure. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 194 | void LTOModule::addObjCClass(GlobalVariable *clgv) { |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 195 | ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer()); |
| 196 | if (!c) return; |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 197 | |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 198 | // second slot in __OBJC,__class is pointer to superclass name |
| 199 | std::string superclassName; |
| 200 | if (objcClassNameFromExpression(c->getOperand(1), superclassName)) { |
| 201 | NameAndAttributes info; |
| 202 | StringMap<NameAndAttributes>::value_type &entry = |
| 203 | _undefines.GetOrCreateValue(superclassName); |
| 204 | if (!entry.getValue().name) { |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 205 | const char *symbolName = entry.getKey().data(); |
| 206 | info.name = symbolName; |
| 207 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 208 | info.isFunction = false; |
| 209 | info.symbol = clgv; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 210 | entry.setValue(info); |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 211 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 212 | } |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 213 | |
| 214 | // third slot in __OBJC,__class is pointer to class name |
| 215 | std::string className; |
| 216 | if (objcClassNameFromExpression(c->getOperand(2), className)) { |
| 217 | StringSet::value_type &entry = _defines.GetOrCreateValue(className); |
| 218 | entry.setValue(1); |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 219 | |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 220 | NameAndAttributes info; |
| 221 | info.name = entry.getKey().data(); |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 222 | info.attributes = LTO_SYMBOL_PERMISSIONS_DATA | |
| 223 | LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT; |
| 224 | info.isFunction = false; |
| 225 | info.symbol = clgv; |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 226 | _symbols.push_back(info); |
| 227 | } |
| 228 | } |
| 229 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 230 | /// addObjCCategory - Parse i386/ppc ObjC category data structure. |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 231 | void LTOModule::addObjCCategory(GlobalVariable *clgv) { |
| 232 | ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer()); |
| 233 | if (!c) return; |
| 234 | |
| 235 | // second slot in __OBJC,__category is pointer to target class name |
| 236 | std::string targetclassName; |
| 237 | if (!objcClassNameFromExpression(c->getOperand(1), targetclassName)) |
| 238 | return; |
| 239 | |
| 240 | NameAndAttributes info; |
| 241 | StringMap<NameAndAttributes>::value_type &entry = |
| 242 | _undefines.GetOrCreateValue(targetclassName); |
| 243 | |
| 244 | if (entry.getValue().name) |
| 245 | return; |
| 246 | |
| 247 | const char *symbolName = entry.getKey().data(); |
| 248 | info.name = symbolName; |
| 249 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 250 | info.isFunction = false; |
| 251 | info.symbol = clgv; |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 252 | entry.setValue(info); |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 255 | /// addObjCClassRef - Parse i386/ppc ObjC class list data structure. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 256 | void LTOModule::addObjCClassRef(GlobalVariable *clgv) { |
| 257 | std::string targetclassName; |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 258 | if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) |
| 259 | return; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 260 | |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 261 | NameAndAttributes info; |
| 262 | StringMap<NameAndAttributes>::value_type &entry = |
| 263 | _undefines.GetOrCreateValue(targetclassName); |
| 264 | if (entry.getValue().name) |
| 265 | return; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 266 | |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 267 | const char *symbolName = entry.getKey().data(); |
| 268 | info.name = symbolName; |
| 269 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 270 | info.isFunction = false; |
| 271 | info.symbol = clgv; |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 272 | entry.setValue(info); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 275 | /// addDefinedDataSymbol - Add a data symbol as defined to the list. |
Bill Wendling | a2af674 | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 276 | void LTOModule::addDefinedDataSymbol(GlobalValue *v) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 277 | // Add to list of defined symbols. |
Bill Wendling | a2af674 | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 278 | addDefinedSymbol(v, false); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 279 | |
| 280 | // Special case i386/ppc ObjC data structures in magic sections: |
| 281 | // The issue is that the old ObjC object format did some strange |
| 282 | // contortions to avoid real linker symbols. For instance, the |
| 283 | // ObjC class data structure is allocated statically in the executable |
| 284 | // that defines that class. That data structures contains a pointer to |
| 285 | // its superclass. But instead of just initializing that part of the |
| 286 | // struct to the address of its superclass, and letting the static and |
| 287 | // dynamic linkers do the rest, the runtime works by having that field |
| 288 | // instead point to a C-string that is the name of the superclass. |
| 289 | // At runtime the objc initialization updates that pointer and sets |
| 290 | // it to point to the actual super class. As far as the linker |
| 291 | // knows it is just a pointer to a string. But then someone wanted the |
| 292 | // linker to issue errors at build time if the superclass was not found. |
| 293 | // So they figured out a way in mach-o object format to use an absolute |
| 294 | // symbols (.objc_class_name_Foo = 0) and a floating reference |
| 295 | // (.reference .objc_class_name_Bar) to cause the linker into erroring when |
| 296 | // a class was missing. |
| 297 | // The following synthesizes the implicit .objc_* symbols for the linker |
| 298 | // from the ObjC data structures generated by the front end. |
| 299 | if (v->hasSection() /* && isTargetDarwin */) { |
| 300 | // special case if this data blob is an ObjC class definition |
| 301 | if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) { |
| 302 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 303 | addObjCClass(gv); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // special case if this data blob is an ObjC category definition |
| 308 | else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) { |
| 309 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 310 | addObjCCategory(gv); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // special case if this data blob is the list of referenced classes |
| 315 | else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) { |
| 316 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 317 | addObjCClassRef(gv); |
| 318 | } |
| 319 | } |
| 320 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 323 | /// addDefinedFunctionSymbol - Add a function symbol as defined to the list. |
| 324 | void LTOModule::addDefinedFunctionSymbol(Function *f) { |
| 325 | // add to list of defined symbols |
| 326 | addDefinedSymbol(f, true); |
| 327 | } |
| 328 | |
| 329 | /// addDefinedSymbol - Add a defined symbol to the list. |
Bill Wendling | a2af674 | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 330 | void LTOModule::addDefinedSymbol(GlobalValue *def, bool isFunction) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 331 | // ignore all llvm.* symbols |
| 332 | if (def->getName().startswith("llvm.")) |
| 333 | return; |
| 334 | |
| 335 | // string is owned by _defines |
Rafael Espindola | 34b5938 | 2011-02-11 05:23:09 +0000 | [diff] [blame] | 336 | SmallString<64> Buffer; |
Bill Wendling | a2af674 | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 337 | _mangler.getNameWithPrefix(Buffer, def, false); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 338 | |
| 339 | // set alignment part log2() can have rounding errors |
| 340 | uint32_t align = def->getAlignment(); |
| 341 | uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0; |
| 342 | |
| 343 | // set permissions part |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 344 | if (isFunction) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 345 | attr |= LTO_SYMBOL_PERMISSIONS_CODE; |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 346 | } else { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 347 | GlobalVariable *gv = dyn_cast<GlobalVariable>(def); |
| 348 | if (gv && gv->isConstant()) |
| 349 | attr |= LTO_SYMBOL_PERMISSIONS_RODATA; |
| 350 | else |
| 351 | attr |= LTO_SYMBOL_PERMISSIONS_DATA; |
| 352 | } |
| 353 | |
| 354 | // set definition part |
Bill Wendling | 2776d46 | 2010-09-27 18:05:19 +0000 | [diff] [blame] | 355 | if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() || |
| 356 | def->hasLinkerPrivateWeakLinkage() || |
Bill Wendling | dcd7c2b | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 357 | def->hasLinkerPrivateWeakDefAutoLinkage()) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 358 | attr |= LTO_SYMBOL_DEFINITION_WEAK; |
Bill Wendling | dcd7c2b | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 359 | else if (def->hasCommonLinkage()) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 360 | attr |= LTO_SYMBOL_DEFINITION_TENTATIVE; |
Bill Wendling | dcd7c2b | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 361 | else |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 362 | attr |= LTO_SYMBOL_DEFINITION_REGULAR; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 363 | |
| 364 | // set scope part |
| 365 | if (def->hasHiddenVisibility()) |
| 366 | attr |= LTO_SYMBOL_SCOPE_HIDDEN; |
| 367 | else if (def->hasProtectedVisibility()) |
| 368 | attr |= LTO_SYMBOL_SCOPE_PROTECTED; |
Bill Wendling | dcd7c2b | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 369 | else if (def->hasExternalLinkage() || def->hasWeakLinkage() || |
| 370 | def->hasLinkOnceLinkage() || def->hasCommonLinkage() || |
| 371 | def->hasLinkerPrivateWeakLinkage()) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 372 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
Bill Wendling | dcd7c2b | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 373 | else if (def->hasLinkerPrivateWeakDefAutoLinkage()) |
| 374 | attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 375 | else |
| 376 | attr |= LTO_SYMBOL_SCOPE_INTERNAL; |
| 377 | |
Chad Rosier | 772a91f | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 378 | StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer); |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 379 | entry.setValue(1); |
| 380 | |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 381 | // fill information structure |
| 382 | NameAndAttributes info; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 383 | StringRef Name = entry.getKey(); |
| 384 | info.name = Name.data(); |
| 385 | assert(info.name[Name.size()] == '\0'); |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 386 | info.attributes = attr; |
| 387 | info.isFunction = isFunction; |
| 388 | info.symbol = def; |
| 389 | |
| 390 | // add to table of symbols |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 391 | _symbols.push_back(info); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 394 | /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the |
| 395 | /// defined list. |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 396 | void LTOModule::addAsmGlobalSymbol(const char *name, |
| 397 | lto_symbol_attributes scope) { |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 398 | StringSet::value_type &entry = _defines.GetOrCreateValue(name); |
| 399 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 400 | // only add new define if not already defined |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 401 | if (entry.getValue()) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 402 | return; |
| 403 | |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 404 | entry.setValue(1); |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 405 | |
| 406 | NameAndAttributes &info = _undefines[entry.getKey().data()]; |
| 407 | |
| 408 | if (info.isFunction) |
| 409 | addDefinedFunctionSymbol(cast<Function>(info.symbol)); |
| 410 | else |
| 411 | addDefinedDataSymbol(info.symbol); |
Bill Wendling | 8f6c8a9 | 2012-03-30 23:26:06 +0000 | [diff] [blame^] | 412 | |
| 413 | _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK; |
| 414 | _symbols.back().attributes |= scope; |
Devang Patel | a59fe95 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 415 | } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 416 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 417 | /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the |
| 418 | /// undefined list. |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 419 | void LTOModule::addAsmGlobalSymbolUndef(const char *name) { |
| 420 | StringMap<NameAndAttributes>::value_type &entry = |
| 421 | _undefines.GetOrCreateValue(name); |
| 422 | |
| 423 | _asm_undefines.push_back(entry.getKey().data()); |
| 424 | |
| 425 | // we already have the symbol |
| 426 | if (entry.getValue().name) |
| 427 | return; |
| 428 | |
| 429 | uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;; |
| 430 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
| 431 | NameAndAttributes info; |
| 432 | info.name = entry.getKey().data(); |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 433 | info.attributes = attr; |
| 434 | info.isFunction = false; |
| 435 | info.symbol = 0; |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 436 | |
| 437 | entry.setValue(info); |
| 438 | } |
| 439 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 440 | /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a |
| 441 | /// list to be resolved later. |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 442 | void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl, bool isFunc) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 443 | // ignore all llvm.* symbols |
| 444 | if (decl->getName().startswith("llvm.")) |
| 445 | return; |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 446 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 447 | // ignore all aliases |
| 448 | if (isa<GlobalAlias>(decl)) |
| 449 | return; |
Nick Lewycky | 0661b93 | 2009-07-09 06:03:04 +0000 | [diff] [blame] | 450 | |
Rafael Espindola | 34b5938 | 2011-02-11 05:23:09 +0000 | [diff] [blame] | 451 | SmallString<64> name; |
Bill Wendling | a2af674 | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 452 | _mangler.getNameWithPrefix(name, decl, false); |
Rafael Espindola | 5654852 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 453 | |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 454 | StringMap<NameAndAttributes>::value_type &entry = |
Chad Rosier | 772a91f | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 455 | _undefines.GetOrCreateValue(name); |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 456 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 457 | // we already have the symbol |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 458 | if (entry.getValue().name) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 459 | return; |
Rafael Espindola | 5654852 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 460 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 461 | NameAndAttributes info; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 462 | |
| 463 | info.name = entry.getKey().data(); |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 464 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 465 | if (decl->hasExternalWeakLinkage()) |
| 466 | info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF; |
| 467 | else |
| 468 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 469 | |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 470 | info.isFunction = isFunc; |
| 471 | info.symbol = decl; |
| 472 | |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 473 | entry.setValue(info); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 476 | namespace { |
| 477 | class RecordStreamer : public MCStreamer { |
| 478 | public: |
| 479 | enum State { NeverSeen, Global, Defined, DefinedGlobal, Used}; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 480 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 481 | private: |
| 482 | StringMap<State> Symbols; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 483 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 484 | void markDefined(const MCSymbol &Symbol) { |
| 485 | State &S = Symbols[Symbol.getName()]; |
| 486 | switch (S) { |
| 487 | case DefinedGlobal: |
| 488 | case Global: |
| 489 | S = DefinedGlobal; |
| 490 | break; |
| 491 | case NeverSeen: |
| 492 | case Defined: |
| 493 | case Used: |
| 494 | S = Defined; |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | void markGlobal(const MCSymbol &Symbol) { |
| 499 | State &S = Symbols[Symbol.getName()]; |
| 500 | switch (S) { |
| 501 | case DefinedGlobal: |
| 502 | case Defined: |
| 503 | S = DefinedGlobal; |
| 504 | break; |
| 505 | |
| 506 | case NeverSeen: |
| 507 | case Global: |
| 508 | case Used: |
| 509 | S = Global; |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | void markUsed(const MCSymbol &Symbol) { |
| 514 | State &S = Symbols[Symbol.getName()]; |
| 515 | switch (S) { |
| 516 | case DefinedGlobal: |
| 517 | case Defined: |
| 518 | case Global: |
| 519 | break; |
| 520 | |
| 521 | case NeverSeen: |
| 522 | case Used: |
| 523 | S = Used; |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // FIXME: mostly copied for the obj streamer. |
| 529 | void AddValueSymbols(const MCExpr *Value) { |
| 530 | switch (Value->getKind()) { |
| 531 | case MCExpr::Target: |
| 532 | // FIXME: What should we do in here? |
| 533 | break; |
| 534 | |
| 535 | case MCExpr::Constant: |
| 536 | break; |
| 537 | |
| 538 | case MCExpr::Binary: { |
| 539 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 540 | AddValueSymbols(BE->getLHS()); |
| 541 | AddValueSymbols(BE->getRHS()); |
| 542 | break; |
| 543 | } |
| 544 | |
| 545 | case MCExpr::SymbolRef: |
| 546 | markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 547 | break; |
| 548 | |
| 549 | case MCExpr::Unary: |
| 550 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | public: |
| 556 | typedef StringMap<State>::const_iterator const_iterator; |
| 557 | |
| 558 | const_iterator begin() { |
| 559 | return Symbols.begin(); |
| 560 | } |
| 561 | |
| 562 | const_iterator end() { |
| 563 | return Symbols.end(); |
| 564 | } |
| 565 | |
| 566 | RecordStreamer(MCContext &Context) : MCStreamer(Context) {} |
| 567 | |
| 568 | virtual void ChangeSection(const MCSection *Section) {} |
| 569 | virtual void InitSections() {} |
| 570 | virtual void EmitLabel(MCSymbol *Symbol) { |
| 571 | Symbol->setSection(*getCurrentSection()); |
| 572 | markDefined(*Symbol); |
| 573 | } |
| 574 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {} |
| 575 | virtual void EmitThumbFunc(MCSymbol *Func) {} |
| 576 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 577 | // FIXME: should we handle aliases? |
| 578 | markDefined(*Symbol); |
| 579 | } |
| 580 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) { |
| 581 | if (Attribute == MCSA_Global) |
| 582 | markGlobal(*Symbol); |
| 583 | } |
| 584 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {} |
| 585 | virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {} |
| 586 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {} |
| 587 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) {} |
| 588 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
| 589 | unsigned Size , unsigned ByteAlignment) { |
| 590 | markDefined(*Symbol); |
| 591 | } |
| 592 | virtual void EmitCOFFSymbolType(int Type) {} |
| 593 | virtual void EndCOFFSymbolDef() {} |
| 594 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 595 | unsigned ByteAlignment) { |
| 596 | markDefined(*Symbol); |
| 597 | } |
| 598 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {} |
Benjamin Kramer | 6397051 | 2011-09-01 23:04:27 +0000 | [diff] [blame] | 599 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 600 | unsigned ByteAlignment) {} |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 601 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 602 | uint64_t Size, unsigned ByteAlignment) {} |
| 603 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {} |
| 604 | virtual void EmitValueImpl(const MCExpr *Value, unsigned Size, |
Rafael Espindola | fd05785 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 605 | unsigned AddrSpace) {} |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 606 | virtual void EmitULEB128Value(const MCExpr *Value) {} |
| 607 | virtual void EmitSLEB128Value(const MCExpr *Value) {} |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 608 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, |
| 609 | unsigned ValueSize, |
| 610 | unsigned MaxBytesToEmit) {} |
| 611 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 612 | unsigned MaxBytesToEmit) {} |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 613 | virtual bool EmitValueToOffset(const MCExpr *Offset, |
| 614 | unsigned char Value ) { return false; } |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 615 | virtual void EmitFileDirective(StringRef Filename) {} |
| 616 | virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta, |
| 617 | const MCSymbol *LastLabel, |
Evan Cheng | c7ac690 | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 618 | const MCSymbol *Label, |
| 619 | unsigned PointerSize) {} |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 620 | |
| 621 | virtual void EmitInstruction(const MCInst &Inst) { |
| 622 | // Scan for values. |
| 623 | for (unsigned i = Inst.getNumOperands(); i--; ) |
| 624 | if (Inst.getOperand(i).isExpr()) |
| 625 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
| 626 | } |
Rafael Espindola | 0708209 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 627 | virtual void FinishImpl() {} |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 628 | }; |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 629 | } // end anonymous namespace |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 630 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 631 | /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the |
| 632 | /// defined or undefined lists. |
Bill Wendling | ac2abde | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 633 | bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) { |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 634 | const std::string &inlineAsm = _module->getModuleInlineAsm(); |
Ivan Krasin | cc2a801 | 2011-09-08 07:38:25 +0000 | [diff] [blame] | 635 | if (inlineAsm.empty()) |
| 636 | return false; |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 637 | |
Bill Wendling | ac2abde | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 638 | OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context)); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 639 | MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm); |
| 640 | SourceMgr SrcMgr; |
| 641 | SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 642 | OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, |
Bill Wendling | ac2abde | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 643 | _context, *Streamer, |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 644 | *_target->getMCAsmInfo())); |
Evan Cheng | 91111d2 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 645 | OwningPtr<MCSubtargetInfo> STI(_target->getTarget(). |
| 646 | createMCSubtargetInfo(_target->getTargetTriple(), |
| 647 | _target->getTargetCPU(), |
| 648 | _target->getTargetFeatureString())); |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 649 | OwningPtr<MCTargetAsmParser> |
| 650 | TAP(_target->getTarget().createMCAsmParser(*STI, *Parser.get())); |
Ivan Krasin | 8149dd6 | 2011-09-08 07:36:39 +0000 | [diff] [blame] | 651 | if (!TAP) { |
| 652 | errMsg = "target " + std::string(_target->getTarget().getName()) + |
| 653 | " does not define AsmParser."; |
| 654 | return true; |
| 655 | } |
| 656 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 657 | Parser->setTargetParser(*TAP); |
| 658 | int Res = Parser->Run(false); |
| 659 | if (Res) |
| 660 | return true; |
| 661 | |
| 662 | for (RecordStreamer::const_iterator i = Streamer->begin(), |
| 663 | e = Streamer->end(); i != e; ++i) { |
| 664 | StringRef Key = i->first(); |
| 665 | RecordStreamer::State Value = i->second; |
| 666 | if (Value == RecordStreamer::DefinedGlobal) |
| 667 | addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT); |
| 668 | else if (Value == RecordStreamer::Defined) |
| 669 | addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL); |
| 670 | else if (Value == RecordStreamer::Global || |
| 671 | Value == RecordStreamer::Used) |
| 672 | addAsmGlobalSymbolUndef(Key.data()); |
| 673 | } |
| 674 | return false; |
| 675 | } |
| 676 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 677 | /// isDeclaration - Return 'true' if the global value is a declaration. |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 678 | static bool isDeclaration(const GlobalValue &V) { |
| 679 | if (V.hasAvailableExternallyLinkage()) |
| 680 | return true; |
| 681 | if (V.isMaterializable()) |
| 682 | return false; |
| 683 | return V.isDeclaration(); |
| 684 | } |
| 685 | |
Bill Wendling | 7e58b38 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 686 | /// parseSymbols - Parse the symbols from the module and model-level ASM and add |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 687 | /// them to either the defined or undefined lists. |
Bill Wendling | 7e58b38 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 688 | bool LTOModule::parseSymbols(std::string &errMsg) { |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 689 | // add functions |
Bill Wendling | 763acfc | 2012-03-29 03:34:57 +0000 | [diff] [blame] | 690 | for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) { |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 691 | if (isDeclaration(*f)) |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 692 | addPotentialUndefinedSymbol(f, true); |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 693 | else |
Bill Wendling | a2af674 | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 694 | addDefinedFunctionSymbol(f); |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 695 | } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 696 | |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 697 | // add data |
| 698 | for (Module::global_iterator v = _module->global_begin(), |
| 699 | e = _module->global_end(); v != e; ++v) { |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 700 | if (isDeclaration(*v)) |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 701 | addPotentialUndefinedSymbol(v, false); |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 702 | else |
Bill Wendling | a2af674 | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 703 | addDefinedDataSymbol(v); |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 704 | } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 705 | |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 706 | // add asm globals |
Bill Wendling | ac2abde | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 707 | if (addAsmGlobalSymbols(errMsg)) |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 708 | return true; |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 709 | |
Rafael Espindola | a8a74ec | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 710 | // add aliases |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 711 | for (Module::alias_iterator a = _module->alias_begin(), |
| 712 | e = _module->alias_end(); a != e; ++a) { |
| 713 | if (isDeclaration(*a->getAliasedGlobal())) |
Bill Wendling | d58ed73 | 2012-03-28 20:48:49 +0000 | [diff] [blame] | 714 | // Is an alias to a declaration. |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 715 | addPotentialUndefinedSymbol(a, false); |
Rafael Espindola | a8a74ec | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 716 | else |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 717 | addDefinedDataSymbol(a); |
Rafael Espindola | a8a74ec | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 720 | // make symbols for all undefines |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 721 | for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(), |
| 722 | e = _undefines.end(); u != e; ++u) { |
| 723 | // If this symbol also has a definition, then don't make an undefine because |
| 724 | // it is a tentative definition. |
| 725 | if (_defines.count(u->getKey())) continue; |
| 726 | NameAndAttributes info = u->getValue(); |
| 727 | _symbols.push_back(info); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 728 | } |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 729 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 730 | return false; |
Nick Kledzik | 91a6dcf | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 731 | } |