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