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 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 15 | #include "llvm/LTO/LTOModule.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Triple.h" |
| 17 | #include "llvm/Bitcode/ReaderWriter.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constants.h" |
| 19 | #include "llvm/IR/LLVMContext.h" |
Yunzhong Gao | a88d7ab | 2014-01-21 18:31:27 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCExpr.h" |
| 23 | #include "llvm/MC/MCInst.h" |
Joey Gouly | db6144e | 2013-09-12 12:55:29 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInstrInfo.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCParser/MCAsmParser.h" |
Rafael Espindola | 20fcda7 | 2014-01-22 22:11:14 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSection.h" |
Evan Cheng | 91111d2 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCSubtargetInfo.h" |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSymbol.h" |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCTargetAsmParser.h" |
Bill Wendling | 8f6c8a9 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 30 | #include "llvm/MC/SubtargetFeature.h" |
Rafael Espindola | 13b69d6 | 2014-07-03 18:59:23 +0000 | [diff] [blame] | 31 | #include "llvm/Object/RecordStreamer.h" |
Bill Wendling | b8dcda7 | 2012-08-06 21:34:54 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | 46ed353 | 2013-06-11 18:05:26 +0000 | [diff] [blame] | 33 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Host.h" |
Bill Wendling | 8f6c8a9 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MemoryBuffer.h" |
| 36 | #include "llvm/Support/Path.h" |
Bill Wendling | 8f6c8a9 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 37 | #include "llvm/Support/SourceMgr.h" |
Bill Wendling | 8f6c8a9 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 38 | #include "llvm/Support/TargetRegistry.h" |
| 39 | #include "llvm/Support/TargetSelect.h" |
Yunzhong Gao | a88d7ab | 2014-01-21 18:31:27 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetLowering.h" |
| 41 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetRegisterInfo.h" |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 44 | #include <system_error> |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Rafael Espindola | 2f0647c | 2014-07-03 22:43:03 +0000 | [diff] [blame] | 47 | LTOModule::LTOModule(std::unique_ptr<Module> M, TargetMachine *TM) |
| 48 | : _module(std::move(M)), _target(TM), |
| 49 | _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), |
| 50 | &ObjFileInfo), |
| 51 | _mangler(TM->getDataLayout()) { |
| 52 | ObjFileInfo.InitMCObjectFileInfo(TM->getTargetTriple(), |
| 53 | TM->getRelocationModel(), TM->getCodeModel(), |
Rafael Espindola | e28610d | 2013-12-09 20:26:40 +0000 | [diff] [blame] | 54 | _context); |
| 55 | } |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 56 | |
| 57 | /// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM |
| 58 | /// bitcode. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 59 | bool LTOModule::isBitcodeFile(const void *mem, size_t length) { |
Rafael Espindola | 46ed353 | 2013-06-11 18:05:26 +0000 | [diff] [blame] | 60 | return sys::fs::identify_magic(StringRef((const char *)mem, length)) == |
| 61 | sys::fs::file_magic::bitcode; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 64 | bool LTOModule::isBitcodeFile(const char *path) { |
Rafael Espindola | 71affba | 2013-06-12 15:13:57 +0000 | [diff] [blame] | 65 | sys::fs::file_magic type; |
| 66 | if (sys::fs::identify_magic(path, type)) |
| 67 | return false; |
| 68 | return type == sys::fs::file_magic::bitcode; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 71 | /// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is |
| 72 | /// LLVM bitcode for the specified triple. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 73 | bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length, |
| 74 | const char *triplePrefix) { |
| 75 | MemoryBuffer *buffer = makeBuffer(mem, length); |
| 76 | if (!buffer) |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 77 | return false; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 78 | return isTargetMatch(buffer, triplePrefix); |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 81 | bool LTOModule::isBitcodeFileForTarget(const char *path, |
| 82 | const char *triplePrefix) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 83 | std::unique_ptr<MemoryBuffer> buffer; |
Michael J. Spencer | 39a0ffc | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 84 | if (MemoryBuffer::getFile(path, buffer)) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 85 | return false; |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 86 | return isTargetMatch(buffer.release(), triplePrefix); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 89 | /// isTargetMatch - Returns 'true' if the memory buffer is for the specified |
| 90 | /// target triple. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 91 | bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) { |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 92 | std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext()); |
| 93 | delete buffer; |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 94 | return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Peter Collingbourne | 63086fe | 2014-07-03 23:28:00 +0000 | [diff] [blame^] | 97 | LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options, |
| 98 | std::string &errMsg) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 99 | std::unique_ptr<MemoryBuffer> buffer; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 100 | if (std::error_code ec = MemoryBuffer::getFile(path, buffer)) { |
Michael J. Spencer | d422723 | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 101 | errMsg = ec.message(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 102 | return nullptr; |
Michael J. Spencer | d422723 | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 103 | } |
Rafael Espindola | 2f0647c | 2014-07-03 22:43:03 +0000 | [diff] [blame] | 104 | return makeLTOModule(std::move(buffer), options, errMsg); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Peter Collingbourne | 63086fe | 2014-07-03 23:28:00 +0000 | [diff] [blame^] | 107 | LTOModule *LTOModule::createFromOpenFile(int fd, const char *path, size_t size, |
| 108 | TargetOptions options, |
| 109 | std::string &errMsg) { |
| 110 | return createFromOpenFileSlice(fd, path, size, 0, options, errMsg); |
Rafael Espindola | b39c7c7 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Peter Collingbourne | 63086fe | 2014-07-03 23:28:00 +0000 | [diff] [blame^] | 113 | LTOModule *LTOModule::createFromOpenFileSlice(int fd, const char *path, |
| 114 | size_t map_size, off_t offset, |
| 115 | TargetOptions options, |
| 116 | std::string &errMsg) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 117 | std::unique_ptr<MemoryBuffer> buffer; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 118 | if (std::error_code ec = |
Rafael Espindola | 3d2ac2e | 2013-07-23 20:25:01 +0000 | [diff] [blame] | 119 | MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) { |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 120 | errMsg = ec.message(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 121 | return nullptr; |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 122 | } |
Rafael Espindola | 2f0647c | 2014-07-03 22:43:03 +0000 | [diff] [blame] | 123 | return makeLTOModule(std::move(buffer), options, errMsg); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Peter Collingbourne | 63086fe | 2014-07-03 23:28:00 +0000 | [diff] [blame^] | 126 | LTOModule *LTOModule::createFromBuffer(const void *mem, size_t length, |
| 127 | TargetOptions options, |
| 128 | std::string &errMsg, StringRef path) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 129 | std::unique_ptr<MemoryBuffer> buffer(makeBuffer(mem, length, path)); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 130 | if (!buffer) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 131 | return nullptr; |
Rafael Espindola | 2f0647c | 2014-07-03 22:43:03 +0000 | [diff] [blame] | 132 | return makeLTOModule(std::move(buffer), options, errMsg); |
Bill Wendling | b8dcda7 | 2012-08-06 21:34:54 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Rafael Espindola | 2f0647c | 2014-07-03 22:43:03 +0000 | [diff] [blame] | 135 | LTOModule *LTOModule::makeLTOModule(std::unique_ptr<MemoryBuffer> Buffer, |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 136 | TargetOptions options, |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 137 | std::string &errMsg) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 138 | // parse bitcode buffer |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 139 | ErrorOr<Module *> ModuleOrErr = |
Rafael Espindola | 2f0647c | 2014-07-03 22:43:03 +0000 | [diff] [blame] | 140 | getLazyBitcodeModule(Buffer.get(), getGlobalContext()); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 141 | if (std::error_code EC = ModuleOrErr.getError()) { |
Rafael Espindola | 5b6c1e8 | 2014-01-13 18:31:04 +0000 | [diff] [blame] | 142 | errMsg = EC.message(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 143 | return nullptr; |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 144 | } |
Rafael Espindola | 2f0647c | 2014-07-03 22:43:03 +0000 | [diff] [blame] | 145 | Buffer.release(); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 146 | std::unique_ptr<Module> m(ModuleOrErr.get()); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 147 | |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 148 | std::string TripleStr = m->getTargetTriple(); |
| 149 | if (TripleStr.empty()) |
| 150 | TripleStr = sys::getDefaultTargetTriple(); |
| 151 | llvm::Triple Triple(TripleStr); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 152 | |
| 153 | // find machine architecture for this module |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 154 | const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 155 | if (!march) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 156 | return nullptr; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 157 | |
Nick Lewycky | 364c04a | 2011-04-21 01:54:08 +0000 | [diff] [blame] | 158 | // construct LTOModule, hand over ownership of module and target |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 159 | SubtargetFeatures Features; |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 160 | Features.getDefaultSubtargetFeatures(Triple); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 161 | std::string FeatureStr = Features.getString(); |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 162 | // Set a default CPU for Darwin triples. |
Evan Cheng | fe6e405 | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 163 | std::string CPU; |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 164 | if (Triple.isOSDarwin()) { |
| 165 | if (Triple.getArch() == llvm::Triple::x86_64) |
| 166 | CPU = "core2"; |
| 167 | else if (Triple.getArch() == llvm::Triple::x86) |
| 168 | CPU = "yonah"; |
Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 169 | else if (Triple.getArch() == llvm::Triple::arm64 || |
| 170 | Triple.getArch() == llvm::Triple::aarch64) |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 171 | CPU = "cyclone"; |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 172 | } |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 173 | |
Bob Wilson | 3f7e7c0 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 174 | TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr, |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 175 | options); |
Rafael Espindola | e9fab9b | 2014-01-14 23:51:27 +0000 | [diff] [blame] | 176 | m->materializeAllPermanently(); |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 177 | |
Rafael Espindola | 2f0647c | 2014-07-03 22:43:03 +0000 | [diff] [blame] | 178 | LTOModule *Ret = new LTOModule(std::move(m), target); |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 179 | |
| 180 | // We need a MCContext set up in order to get mangled names of private |
| 181 | // symbols. It is a bit odd that we need to report uses and definitions |
| 182 | // of private symbols, but it does look like ld64 expects to be informed |
| 183 | // of at least the ones with an 'l' prefix. |
| 184 | MCContext &Context = Ret->_context; |
| 185 | const TargetLoweringObjectFile &TLOF = |
| 186 | target->getTargetLowering()->getObjFileLowering(); |
| 187 | const_cast<TargetLoweringObjectFile &>(TLOF).Initialize(Context, *target); |
| 188 | |
Bill Wendling | 7e58b38 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 189 | if (Ret->parseSymbols(errMsg)) { |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 190 | delete Ret; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 191 | return nullptr; |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 192 | } |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 193 | |
Yunzhong Gao | a88d7ab | 2014-01-21 18:31:27 +0000 | [diff] [blame] | 194 | Ret->parseMetadata(); |
| 195 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 196 | return Ret; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Manman Ren | 03456a1 | 2014-02-10 23:26:14 +0000 | [diff] [blame] | 199 | /// Create a MemoryBuffer from a memory range with an optional name. |
| 200 | MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length, |
| 201 | StringRef name) { |
Roman Divacky | ad06cee | 2012-09-05 22:26:57 +0000 | [diff] [blame] | 202 | const char *startPtr = (const char*)mem; |
Manman Ren | 03456a1 | 2014-02-10 23:26:14 +0000 | [diff] [blame] | 203 | return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 206 | /// objcClassNameFromExpression - Get string that the data pointer points to. |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 207 | bool |
| 208 | LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) { |
| 209 | if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 210 | Constant *op = ce->getOperand(0); |
| 211 | if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) { |
| 212 | Constant *cn = gvn->getInitializer(); |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 213 | if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 214 | if (ca->isCString()) { |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 215 | name = ".objc_class_name_" + ca->getAsCString().str(); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 216 | return true; |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 217 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 218 | } |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 219 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 220 | } |
| 221 | return false; |
| 222 | } |
| 223 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 224 | /// addObjCClass - Parse i386/ppc ObjC class data structure. |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 225 | void LTOModule::addObjCClass(const GlobalVariable *clgv) { |
| 226 | const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer()); |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 227 | if (!c) return; |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 228 | |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 229 | // second slot in __OBJC,__class is pointer to superclass name |
| 230 | std::string superclassName; |
| 231 | if (objcClassNameFromExpression(c->getOperand(1), superclassName)) { |
| 232 | NameAndAttributes info; |
| 233 | StringMap<NameAndAttributes>::value_type &entry = |
| 234 | _undefines.GetOrCreateValue(superclassName); |
| 235 | if (!entry.getValue().name) { |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 236 | const char *symbolName = entry.getKey().data(); |
| 237 | info.name = symbolName; |
| 238 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 239 | info.isFunction = false; |
| 240 | info.symbol = clgv; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 241 | entry.setValue(info); |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 242 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 243 | } |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 244 | |
| 245 | // third slot in __OBJC,__class is pointer to class name |
| 246 | std::string className; |
| 247 | if (objcClassNameFromExpression(c->getOperand(2), className)) { |
| 248 | StringSet::value_type &entry = _defines.GetOrCreateValue(className); |
| 249 | entry.setValue(1); |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 250 | |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 251 | NameAndAttributes info; |
| 252 | info.name = entry.getKey().data(); |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 253 | info.attributes = LTO_SYMBOL_PERMISSIONS_DATA | |
| 254 | LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT; |
| 255 | info.isFunction = false; |
| 256 | info.symbol = clgv; |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 257 | _symbols.push_back(info); |
| 258 | } |
| 259 | } |
| 260 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 261 | /// addObjCCategory - Parse i386/ppc ObjC category data structure. |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 262 | void LTOModule::addObjCCategory(const GlobalVariable *clgv) { |
| 263 | const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer()); |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 264 | if (!c) return; |
| 265 | |
| 266 | // second slot in __OBJC,__category is pointer to target class name |
| 267 | std::string targetclassName; |
| 268 | if (!objcClassNameFromExpression(c->getOperand(1), targetclassName)) |
| 269 | return; |
| 270 | |
| 271 | NameAndAttributes info; |
| 272 | StringMap<NameAndAttributes>::value_type &entry = |
| 273 | _undefines.GetOrCreateValue(targetclassName); |
| 274 | |
| 275 | if (entry.getValue().name) |
| 276 | return; |
| 277 | |
| 278 | const char *symbolName = entry.getKey().data(); |
| 279 | info.name = symbolName; |
| 280 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 281 | info.isFunction = false; |
| 282 | info.symbol = clgv; |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 283 | entry.setValue(info); |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 286 | /// addObjCClassRef - Parse i386/ppc ObjC class list data structure. |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 287 | void LTOModule::addObjCClassRef(const GlobalVariable *clgv) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 288 | std::string targetclassName; |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 289 | if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) |
| 290 | return; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 291 | |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 292 | NameAndAttributes info; |
| 293 | StringMap<NameAndAttributes>::value_type &entry = |
| 294 | _undefines.GetOrCreateValue(targetclassName); |
| 295 | if (entry.getValue().name) |
| 296 | return; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 297 | |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 298 | const char *symbolName = entry.getKey().data(); |
| 299 | info.name = symbolName; |
| 300 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 301 | info.isFunction = false; |
| 302 | info.symbol = clgv; |
Bill Wendling | 5f689e7 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 303 | entry.setValue(info); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 306 | /// addDefinedDataSymbol - Add a data symbol as defined to the list. |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 307 | void LTOModule::addDefinedDataSymbol(const GlobalValue *v) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 308 | // Add to list of defined symbols. |
Bill Wendling | a2af674 | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 309 | addDefinedSymbol(v, false); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 310 | |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 311 | if (!v->hasSection() /* || !isTargetDarwin */) |
| 312 | return; |
| 313 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 314 | // Special case i386/ppc ObjC data structures in magic sections: |
| 315 | // The issue is that the old ObjC object format did some strange |
| 316 | // contortions to avoid real linker symbols. For instance, the |
| 317 | // ObjC class data structure is allocated statically in the executable |
| 318 | // that defines that class. That data structures contains a pointer to |
| 319 | // its superclass. But instead of just initializing that part of the |
| 320 | // struct to the address of its superclass, and letting the static and |
| 321 | // dynamic linkers do the rest, the runtime works by having that field |
| 322 | // instead point to a C-string that is the name of the superclass. |
| 323 | // At runtime the objc initialization updates that pointer and sets |
| 324 | // it to point to the actual super class. As far as the linker |
| 325 | // knows it is just a pointer to a string. But then someone wanted the |
| 326 | // linker to issue errors at build time if the superclass was not found. |
| 327 | // So they figured out a way in mach-o object format to use an absolute |
| 328 | // symbols (.objc_class_name_Foo = 0) and a floating reference |
| 329 | // (.reference .objc_class_name_Bar) to cause the linker into erroring when |
| 330 | // a class was missing. |
| 331 | // The following synthesizes the implicit .objc_* symbols for the linker |
| 332 | // from the ObjC data structures generated by the front end. |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 333 | |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 334 | // special case if this data blob is an ObjC class definition |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 335 | std::string Section = v->getSection(); |
| 336 | if (Section.compare(0, 15, "__OBJC,__class,") == 0) { |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 337 | if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 338 | addObjCClass(gv); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 339 | } |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 340 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 341 | |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 342 | // special case if this data blob is an ObjC category definition |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 343 | else if (Section.compare(0, 18, "__OBJC,__category,") == 0) { |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 344 | if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 345 | addObjCCategory(gv); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // special case if this data blob is the list of referenced classes |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 350 | else if (Section.compare(0, 18, "__OBJC,__cls_refs,") == 0) { |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 351 | if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
Bill Wendling | 45f74e3 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 352 | addObjCClassRef(gv); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 357 | /// addDefinedFunctionSymbol - Add a function symbol as defined to the list. |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 358 | void LTOModule::addDefinedFunctionSymbol(const Function *f) { |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 359 | // add to list of defined symbols |
| 360 | addDefinedSymbol(f, true); |
| 361 | } |
| 362 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 363 | static bool canBeHidden(const GlobalValue *GV) { |
Rafael Espindola | 66f273b | 2014-02-07 19:04:43 +0000 | [diff] [blame] | 364 | // FIXME: this is duplicated with another static function in AsmPrinter.cpp |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 365 | GlobalValue::LinkageTypes L = GV->getLinkage(); |
| 366 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 367 | if (L != GlobalValue::LinkOnceODRLinkage) |
| 368 | return false; |
| 369 | |
| 370 | if (GV->hasUnnamedAddr()) |
| 371 | return true; |
| 372 | |
Rafael Espindola | 66f273b | 2014-02-07 19:04:43 +0000 | [diff] [blame] | 373 | // If it is a non constant variable, it needs to be uniqued across shared |
| 374 | // objects. |
| 375 | if (const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV)) { |
| 376 | if (!Var->isConstant()) |
| 377 | return false; |
| 378 | } |
| 379 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 380 | GlobalStatus GS; |
| 381 | if (GlobalStatus::analyzeGlobal(GV, GS)) |
| 382 | return false; |
| 383 | |
| 384 | return !GS.IsCompared; |
| 385 | } |
| 386 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 387 | /// addDefinedSymbol - Add a defined symbol to the list. |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 388 | void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 389 | // ignore all llvm.* symbols |
| 390 | if (def->getName().startswith("llvm.")) |
| 391 | return; |
| 392 | |
| 393 | // string is owned by _defines |
Rafael Espindola | 34b5938 | 2011-02-11 05:23:09 +0000 | [diff] [blame] | 394 | SmallString<64> Buffer; |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 395 | _target->getNameWithPrefix(Buffer, def, _mangler); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 396 | |
| 397 | // set alignment part log2() can have rounding errors |
| 398 | uint32_t align = def->getAlignment(); |
Rafael Espindola | 9475117 | 2014-05-05 14:18:16 +0000 | [diff] [blame] | 399 | uint32_t attr = align ? countTrailingZeros(align) : 0; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 400 | |
| 401 | // set permissions part |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 402 | if (isFunction) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 403 | attr |= LTO_SYMBOL_PERMISSIONS_CODE; |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 404 | } else { |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 405 | const GlobalVariable *gv = dyn_cast<GlobalVariable>(def); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 406 | if (gv && gv->isConstant()) |
| 407 | attr |= LTO_SYMBOL_PERMISSIONS_RODATA; |
| 408 | else |
| 409 | attr |= LTO_SYMBOL_PERMISSIONS_DATA; |
| 410 | } |
| 411 | |
| 412 | // set definition part |
Rafael Espindola | 2fb5bc3 | 2014-03-13 23:18:37 +0000 | [diff] [blame] | 413 | if (def->hasWeakLinkage() || def->hasLinkOnceLinkage()) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 414 | attr |= LTO_SYMBOL_DEFINITION_WEAK; |
Bill Wendling | dcd7c2b | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 415 | else if (def->hasCommonLinkage()) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 416 | attr |= LTO_SYMBOL_DEFINITION_TENTATIVE; |
Bill Wendling | dcd7c2b | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 417 | else |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 418 | attr |= LTO_SYMBOL_DEFINITION_REGULAR; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 419 | |
| 420 | // set scope part |
Duncan P. N. Exon Smith | 87121f8 | 2014-05-07 22:53:14 +0000 | [diff] [blame] | 421 | if (def->hasLocalLinkage()) |
| 422 | // Ignore visibility if linkage is local. |
| 423 | attr |= LTO_SYMBOL_SCOPE_INTERNAL; |
| 424 | else if (def->hasHiddenVisibility()) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 425 | attr |= LTO_SYMBOL_SCOPE_HIDDEN; |
| 426 | else if (def->hasProtectedVisibility()) |
| 427 | attr |= LTO_SYMBOL_SCOPE_PROTECTED; |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 428 | else if (canBeHidden(def)) |
| 429 | attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 430 | else |
Duncan P. N. Exon Smith | 87121f8 | 2014-05-07 22:53:14 +0000 | [diff] [blame] | 431 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 432 | |
Chad Rosier | 772a91f | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 433 | StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer); |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 434 | entry.setValue(1); |
| 435 | |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 436 | // fill information structure |
| 437 | NameAndAttributes info; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 438 | StringRef Name = entry.getKey(); |
| 439 | info.name = Name.data(); |
| 440 | assert(info.name[Name.size()] == '\0'); |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 441 | info.attributes = attr; |
| 442 | info.isFunction = isFunction; |
| 443 | info.symbol = def; |
| 444 | |
| 445 | // add to table of symbols |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 446 | _symbols.push_back(info); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 449 | /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the |
| 450 | /// defined list. |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 451 | void LTOModule::addAsmGlobalSymbol(const char *name, |
| 452 | lto_symbol_attributes scope) { |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 453 | StringSet::value_type &entry = _defines.GetOrCreateValue(name); |
| 454 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 455 | // only add new define if not already defined |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 456 | if (entry.getValue()) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 457 | return; |
| 458 | |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 459 | entry.setValue(1); |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 460 | |
| 461 | NameAndAttributes &info = _undefines[entry.getKey().data()]; |
| 462 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 463 | if (info.symbol == nullptr) { |
Bill Wendling | 71b19bb | 2012-04-02 10:01:21 +0000 | [diff] [blame] | 464 | // FIXME: This is trying to take care of module ASM like this: |
| 465 | // |
| 466 | // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0" |
| 467 | // |
| 468 | // but is gross and its mother dresses it funny. Have the ASM parser give us |
| 469 | // more details for this type of situation so that we're not guessing so |
| 470 | // much. |
| 471 | |
| 472 | // fill information structure |
Rafael Espindola | 5f4b32f | 2012-05-11 03:42:13 +0000 | [diff] [blame] | 473 | info.name = entry.getKey().data(); |
Bill Wendling | 71b19bb | 2012-04-02 10:01:21 +0000 | [diff] [blame] | 474 | info.attributes = |
| 475 | LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope; |
| 476 | info.isFunction = false; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 477 | info.symbol = nullptr; |
Bill Wendling | 71b19bb | 2012-04-02 10:01:21 +0000 | [diff] [blame] | 478 | |
| 479 | // add to table of symbols |
| 480 | _symbols.push_back(info); |
Bill Wendling | 3a0bcf0 | 2012-04-02 03:33:31 +0000 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 484 | if (info.isFunction) |
| 485 | addDefinedFunctionSymbol(cast<Function>(info.symbol)); |
| 486 | else |
| 487 | addDefinedDataSymbol(info.symbol); |
Bill Wendling | 8f6c8a9 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 488 | |
| 489 | _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK; |
| 490 | _symbols.back().attributes |= scope; |
Devang Patel | a59fe95 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 491 | } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 492 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 493 | /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the |
| 494 | /// undefined list. |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 495 | void LTOModule::addAsmGlobalSymbolUndef(const char *name) { |
| 496 | StringMap<NameAndAttributes>::value_type &entry = |
| 497 | _undefines.GetOrCreateValue(name); |
| 498 | |
| 499 | _asm_undefines.push_back(entry.getKey().data()); |
| 500 | |
| 501 | // we already have the symbol |
| 502 | if (entry.getValue().name) |
| 503 | return; |
| 504 | |
Alp Toker | 9844434 | 2014-04-19 23:56:35 +0000 | [diff] [blame] | 505 | uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 506 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
| 507 | NameAndAttributes info; |
| 508 | info.name = entry.getKey().data(); |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 509 | info.attributes = attr; |
| 510 | info.isFunction = false; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 511 | info.symbol = nullptr; |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 512 | |
| 513 | entry.setValue(info); |
| 514 | } |
| 515 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 516 | /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a |
| 517 | /// list to be resolved later. |
Rafael Espindola | 6ee19d2 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 518 | void |
| 519 | LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) { |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 520 | // ignore all llvm.* symbols |
| 521 | if (decl->getName().startswith("llvm.")) |
| 522 | return; |
Nick Kledzik | b481c20 | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 523 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 524 | // ignore all aliases |
| 525 | if (isa<GlobalAlias>(decl)) |
| 526 | return; |
Nick Lewycky | 0661b93 | 2009-07-09 06:03:04 +0000 | [diff] [blame] | 527 | |
Rafael Espindola | 34b5938 | 2011-02-11 05:23:09 +0000 | [diff] [blame] | 528 | SmallString<64> name; |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 529 | _target->getNameWithPrefix(name, decl, _mangler); |
Rafael Espindola | 5654852 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 530 | |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 531 | StringMap<NameAndAttributes>::value_type &entry = |
Chad Rosier | 772a91f | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 532 | _undefines.GetOrCreateValue(name); |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 533 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 534 | // we already have the symbol |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 535 | if (entry.getValue().name) |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 536 | return; |
Rafael Espindola | 5654852 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 537 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 538 | NameAndAttributes info; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 539 | |
| 540 | info.name = entry.getKey().data(); |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 541 | |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 542 | if (decl->hasExternalWeakLinkage()) |
| 543 | info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF; |
| 544 | else |
| 545 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 546 | |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 547 | info.isFunction = isFunc; |
| 548 | info.symbol = decl; |
| 549 | |
Rafael Espindola | 477d11f | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 550 | entry.setValue(info); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 553 | /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the |
| 554 | /// defined or undefined lists. |
Bill Wendling | ac2abde | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 555 | bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) { |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 556 | const std::string &inlineAsm = _module->getModuleInlineAsm(); |
Ivan Krasin | cc2a801 | 2011-09-08 07:38:25 +0000 | [diff] [blame] | 557 | if (inlineAsm.empty()) |
| 558 | return false; |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 559 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 560 | std::unique_ptr<RecordStreamer> Streamer(new RecordStreamer(_context)); |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 561 | MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm); |
| 562 | SourceMgr SrcMgr; |
| 563 | SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 564 | std::unique_ptr<MCAsmParser> Parser( |
| 565 | createMCAsmParser(SrcMgr, _context, *Streamer, *_target->getMCAsmInfo())); |
Bill Wendling | 9351b3e | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 566 | const Target &T = _target->getTarget(); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 567 | std::unique_ptr<MCInstrInfo> MCII(T.createMCInstrInfo()); |
| 568 | std::unique_ptr<MCSubtargetInfo> STI(T.createMCSubtargetInfo( |
| 569 | _target->getTargetTriple(), _target->getTargetCPU(), |
| 570 | _target->getTargetFeatureString())); |
| 571 | std::unique_ptr<MCTargetAsmParser> TAP( |
Evgeniy Stepanov | 0a951b7 | 2014-04-23 11:16:03 +0000 | [diff] [blame] | 572 | T.createMCAsmParser(*STI, *Parser.get(), *MCII, |
| 573 | _target->Options.MCOptions)); |
Ivan Krasin | 8149dd6 | 2011-09-08 07:36:39 +0000 | [diff] [blame] | 574 | if (!TAP) { |
Bill Wendling | 9351b3e | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 575 | errMsg = "target " + std::string(T.getName()) + |
| 576 | " does not define AsmParser."; |
Ivan Krasin | 8149dd6 | 2011-09-08 07:36:39 +0000 | [diff] [blame] | 577 | return true; |
| 578 | } |
| 579 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 580 | Parser->setTargetParser(*TAP); |
Bill Wendling | 9351b3e | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 581 | if (Parser->Run(false)) |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 582 | return true; |
| 583 | |
Rafael Espindola | 05cef84 | 2014-06-28 18:44:59 +0000 | [diff] [blame] | 584 | for (auto &KV : *Streamer) { |
| 585 | StringRef Key = KV.first(); |
| 586 | RecordStreamer::State Value = KV.second; |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 587 | if (Value == RecordStreamer::DefinedGlobal) |
| 588 | addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT); |
| 589 | else if (Value == RecordStreamer::Defined) |
| 590 | addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL); |
| 591 | else if (Value == RecordStreamer::Global || |
| 592 | Value == RecordStreamer::Used) |
| 593 | addAsmGlobalSymbolUndef(Key.data()); |
| 594 | } |
Bill Wendling | 9351b3e | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 595 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 596 | return false; |
| 597 | } |
| 598 | |
Bill Wendling | fb44050 | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 599 | /// isDeclaration - Return 'true' if the global value is a declaration. |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 600 | static bool isDeclaration(const GlobalValue &V) { |
| 601 | if (V.hasAvailableExternallyLinkage()) |
| 602 | return true; |
Bill Wendling | 9351b3e | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 603 | |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 604 | if (V.isMaterializable()) |
| 605 | return false; |
Bill Wendling | 9351b3e | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 606 | |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 607 | return V.isDeclaration(); |
| 608 | } |
| 609 | |
Bill Wendling | 7e58b38 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 610 | /// 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] | 611 | /// them to either the defined or undefined lists. |
Bill Wendling | 7e58b38 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 612 | bool LTOModule::parseSymbols(std::string &errMsg) { |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 613 | // add functions |
Bill Wendling | 763acfc | 2012-03-29 03:34:57 +0000 | [diff] [blame] | 614 | for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) { |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 615 | if (isDeclaration(*f)) |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 616 | addPotentialUndefinedSymbol(f, true); |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 617 | else |
Bill Wendling | a2af674 | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 618 | addDefinedFunctionSymbol(f); |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 619 | } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 620 | |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 621 | // add data |
| 622 | for (Module::global_iterator v = _module->global_begin(), |
| 623 | e = _module->global_end(); v != e; ++v) { |
Rafael Espindola | 5b778b2 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 624 | if (isDeclaration(*v)) |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 625 | addPotentialUndefinedSymbol(v, false); |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 626 | else |
Bill Wendling | a2af674 | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 627 | addDefinedDataSymbol(v); |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 628 | } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 629 | |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 630 | // add asm globals |
Bill Wendling | ac2abde | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 631 | if (addAsmGlobalSymbols(errMsg)) |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 632 | return true; |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 633 | |
Rafael Espindola | a8a74ec | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 634 | // add aliases |
Rafael Espindola | 6314ad4 | 2014-05-23 15:18:06 +0000 | [diff] [blame] | 635 | for (const auto &Alias : _module->aliases()) |
| 636 | addDefinedDataSymbol(&Alias); |
Rafael Espindola | a8a74ec | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 637 | |
Daniel Dunbar | 919660b | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 638 | // make symbols for all undefines |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 639 | for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(), |
| 640 | e = _undefines.end(); u != e; ++u) { |
| 641 | // If this symbol also has a definition, then don't make an undefine because |
| 642 | // it is a tentative definition. |
| 643 | if (_defines.count(u->getKey())) continue; |
| 644 | NameAndAttributes info = u->getValue(); |
| 645 | _symbols.push_back(info); |
Daniel Dunbar | 5657e7b | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 646 | } |
Bill Wendling | 9ee2d33 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 647 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 648 | return false; |
Nick Kledzik | 91a6dcf | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 649 | } |
Yunzhong Gao | a88d7ab | 2014-01-21 18:31:27 +0000 | [diff] [blame] | 650 | |
| 651 | /// parseMetadata - Parse metadata from the module |
| 652 | void LTOModule::parseMetadata() { |
| 653 | // Linker Options |
| 654 | if (Value *Val = _module->getModuleFlag("Linker Options")) { |
| 655 | MDNode *LinkerOptions = cast<MDNode>(Val); |
| 656 | for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { |
| 657 | MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); |
| 658 | for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { |
| 659 | MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); |
| 660 | StringRef Op = _linkeropt_strings. |
| 661 | GetOrCreateValue(MDOption->getString()).getKey(); |
| 662 | StringRef DepLibName = _target->getTargetLowering()-> |
| 663 | getObjFileLowering().getDepLibFromLinkerOpt(Op); |
| 664 | if (!DepLibName.empty()) |
| 665 | _deplibs.push_back(DepLibName.data()); |
| 666 | else if (!Op.empty()) |
| 667 | _linkeropts.push_back(Op.data()); |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | // Add other interesting metadata here. |
| 673 | } |