blob: b687f29c6598ae161687ce3a3548c587e8b14a42 [file] [log] [blame]
Chris Lattner2eff5052010-03-12 18:44:54 +00001//===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
Nick Kledzik07b4a622008-02-26 20:26:43 +00002//
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 Dunbar5657e7b2010-08-10 23:46:39 +00007//
Nick Kledzik07b4a622008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik07b4a622008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000015#include "llvm/LTO/LTOModule.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000016#include "llvm/ADT/Triple.h"
17#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Constants.h"
19#include "llvm/IR/LLVMContext.h"
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +000020#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Module.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000022#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
Joey Goulydb6144e2013-09-12 12:55:29 +000024#include "llvm/MC/MCInstrInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000025#include "llvm/MC/MCParser/MCAsmParser.h"
Rafael Espindola20fcda72014-01-22 22:11:14 +000026#include "llvm/MC/MCSection.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000027#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000028#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000029#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000030#include "llvm/MC/MCTargetAsmParser.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000031#include "llvm/MC/SubtargetFeature.h"
Bill Wendlingb8dcda72012-08-06 21:34:54 +000032#include "llvm/Support/CommandLine.h"
Rafael Espindola46ed3532013-06-11 18:05:26 +000033#include "llvm/Support/FileSystem.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000034#include "llvm/Support/Host.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000035#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/Path.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000037#include "llvm/Support/SourceMgr.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000038#include "llvm/Support/TargetRegistry.h"
39#include "llvm/Support/TargetSelect.h"
40#include "llvm/Support/system_error.h"
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +000041#include "llvm/Target/TargetLowering.h"
42#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000043#include "llvm/Target/TargetRegisterInfo.h"
Rafael Espindola282a4702013-10-31 20:51:58 +000044#include "llvm/Transforms/Utils/GlobalStatus.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000045using namespace llvm;
46
Bill Wendlingfb440502012-03-28 20:46:54 +000047LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
48 : _module(m), _target(t),
Rafael Espindolae28610d2013-12-09 20:26:40 +000049 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), &ObjFileInfo),
Rafael Espindola58873562014-01-03 19:21:54 +000050 _mangler(t->getDataLayout()) {
Rafael Espindolae28610d2013-12-09 20:26:40 +000051 ObjFileInfo.InitMCObjectFileInfo(t->getTargetTriple(),
52 t->getRelocationModel(), t->getCodeModel(),
53 _context);
54}
Bill Wendlingfb440502012-03-28 20:46:54 +000055
56/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
57/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000058bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +000059 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
60 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000061}
62
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000063bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +000064 sys::fs::file_magic type;
65 if (sys::fs::identify_magic(path, type))
66 return false;
67 return type == sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000068}
69
Bill Wendlingfb440502012-03-28 20:46:54 +000070/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
71/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000072bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
73 const char *triplePrefix) {
74 MemoryBuffer *buffer = makeBuffer(mem, length);
75 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +000076 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000077 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +000078}
79
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000080bool LTOModule::isBitcodeFileForTarget(const char *path,
81 const char *triplePrefix) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000082 std::unique_ptr<MemoryBuffer> buffer;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000083 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000084 return false;
Ahmed Charles96c9d952014-03-05 10:19:29 +000085 return isTargetMatch(buffer.release(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000086}
87
Bill Wendlingfb440502012-03-28 20:46:54 +000088/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
89/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000090bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +000091 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
92 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +000093 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000094}
95
Bill Wendlingfb440502012-03-28 20:46:54 +000096/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
97/// the buffer.
Rafael Espindola0b385c72013-09-30 16:39:19 +000098LTOModule *LTOModule::makeLTOModule(const char *path, TargetOptions options,
99 std::string &errMsg) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000100 std::unique_ptr<MemoryBuffer> buffer;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000101 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000102 errMsg = ec.message();
Craig Topper2617dcc2014-04-15 06:32:26 +0000103 return nullptr;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000104 }
Ahmed Charles96c9d952014-03-05 10:19:29 +0000105 return makeLTOModule(buffer.release(), options, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000106}
107
Rafael Espindola56e41f72011-02-08 22:40:47 +0000108LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000109 size_t size, TargetOptions options,
110 std::string &errMsg) {
111 return makeLTOModule(fd, path, size, 0, options, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000112}
113
114LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000115 size_t map_size,
116 off_t offset,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000117 TargetOptions options,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000118 std::string &errMsg) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000119 std::unique_ptr<MemoryBuffer> buffer;
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000120 if (error_code ec =
121 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000122 errMsg = ec.message();
Craig Topper2617dcc2014-04-15 06:32:26 +0000123 return nullptr;
Rafael Espindola56e41f72011-02-08 22:40:47 +0000124 }
Ahmed Charles96c9d952014-03-05 10:19:29 +0000125 return makeLTOModule(buffer.release(), options, errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000126}
127
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000128LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000129 TargetOptions options,
Manman Ren03456a12014-02-10 23:26:14 +0000130 std::string &errMsg, StringRef path) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000131 std::unique_ptr<MemoryBuffer> buffer(makeBuffer(mem, length, path));
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000132 if (!buffer)
Craig Topper2617dcc2014-04-15 06:32:26 +0000133 return nullptr;
Ahmed Charles96c9d952014-03-05 10:19:29 +0000134 return makeLTOModule(buffer.release(), options, errMsg);
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000135}
136
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000137LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000138 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000139 std::string &errMsg) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000140 // parse bitcode buffer
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000141 ErrorOr<Module *> ModuleOrErr =
142 getLazyBitcodeModule(buffer, getGlobalContext());
143 if (error_code EC = ModuleOrErr.getError()) {
144 errMsg = EC.message();
Rafael Espindola5b778b22011-03-18 19:51:00 +0000145 delete buffer;
Craig Topper2617dcc2014-04-15 06:32:26 +0000146 return nullptr;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000147 }
Ahmed Charles56440fd2014-03-06 05:51:42 +0000148 std::unique_ptr<Module> m(ModuleOrErr.get());
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000149
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000150 std::string TripleStr = m->getTargetTriple();
151 if (TripleStr.empty())
152 TripleStr = sys::getDefaultTargetTriple();
153 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000154
155 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000156 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000157 if (!march)
Craig Topper2617dcc2014-04-15 06:32:26 +0000158 return nullptr;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000159
Nick Lewycky364c04a2011-04-21 01:54:08 +0000160 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000161 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000162 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000163 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000164 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000165 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000166 if (Triple.isOSDarwin()) {
167 if (Triple.getArch() == llvm::Triple::x86_64)
168 CPU = "core2";
169 else if (Triple.getArch() == llvm::Triple::x86)
170 CPU = "yonah";
Tim Northover00ed9962014-03-29 10:18:08 +0000171 else if (Triple.getArch() == llvm::Triple::arm64)
172 CPU = "cyclone";
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000173 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000174
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000175 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000176 options);
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000177 m->materializeAllPermanently();
Rafael Espindola282a4702013-10-31 20:51:58 +0000178
Ahmed Charles96c9d952014-03-05 10:19:29 +0000179 LTOModule *Ret = new LTOModule(m.release(), target);
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000180
181 // We need a MCContext set up in order to get mangled names of private
182 // symbols. It is a bit odd that we need to report uses and definitions
183 // of private symbols, but it does look like ld64 expects to be informed
184 // of at least the ones with an 'l' prefix.
185 MCContext &Context = Ret->_context;
186 const TargetLoweringObjectFile &TLOF =
187 target->getTargetLowering()->getObjFileLowering();
188 const_cast<TargetLoweringObjectFile &>(TLOF).Initialize(Context, *target);
189
Bill Wendling7e58b382012-03-28 23:12:18 +0000190 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000191 delete Ret;
Craig Topper2617dcc2014-04-15 06:32:26 +0000192 return nullptr;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000193 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000194
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000195 Ret->parseMetadata();
196
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000197 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000198}
199
Manman Ren03456a12014-02-10 23:26:14 +0000200/// Create a MemoryBuffer from a memory range with an optional name.
201MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length,
202 StringRef name) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000203 const char *startPtr = (const char*)mem;
Manman Ren03456a12014-02-10 23:26:14 +0000204 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000205}
206
Bill Wendlingfb440502012-03-28 20:46:54 +0000207/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000208bool
209LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
210 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000211 Constant *op = ce->getOperand(0);
212 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
213 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000214 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000215 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000216 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000217 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000218 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000219 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000220 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000221 }
222 return false;
223}
224
Bill Wendlingfb440502012-03-28 20:46:54 +0000225/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000226void LTOModule::addObjCClass(const GlobalVariable *clgv) {
227 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000228 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000229
Bill Wendling5f689e72011-11-04 18:48:00 +0000230 // second slot in __OBJC,__class is pointer to superclass name
231 std::string superclassName;
232 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
233 NameAndAttributes info;
234 StringMap<NameAndAttributes>::value_type &entry =
235 _undefines.GetOrCreateValue(superclassName);
236 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000237 const char *symbolName = entry.getKey().data();
238 info.name = symbolName;
239 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000240 info.isFunction = false;
241 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000242 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000243 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000244 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000245
246 // third slot in __OBJC,__class is pointer to class name
247 std::string className;
248 if (objcClassNameFromExpression(c->getOperand(2), className)) {
249 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
250 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000251
Bill Wendling5f689e72011-11-04 18:48:00 +0000252 NameAndAttributes info;
253 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000254 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
255 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
256 info.isFunction = false;
257 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000258 _symbols.push_back(info);
259 }
260}
261
Bill Wendlingfb440502012-03-28 20:46:54 +0000262/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000263void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
264 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000265 if (!c) return;
266
267 // second slot in __OBJC,__category is pointer to target class name
268 std::string targetclassName;
269 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
270 return;
271
272 NameAndAttributes info;
273 StringMap<NameAndAttributes>::value_type &entry =
274 _undefines.GetOrCreateValue(targetclassName);
275
276 if (entry.getValue().name)
277 return;
278
279 const char *symbolName = entry.getKey().data();
280 info.name = symbolName;
281 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000282 info.isFunction = false;
283 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000284 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000285}
286
Bill Wendlingfb440502012-03-28 20:46:54 +0000287/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000288void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000289 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000290 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
291 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000292
Bill Wendling5f689e72011-11-04 18:48:00 +0000293 NameAndAttributes info;
294 StringMap<NameAndAttributes>::value_type &entry =
295 _undefines.GetOrCreateValue(targetclassName);
296 if (entry.getValue().name)
297 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000298
Bill Wendling5f689e72011-11-04 18:48:00 +0000299 const char *symbolName = entry.getKey().data();
300 info.name = symbolName;
301 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000302 info.isFunction = false;
303 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000304 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000305}
306
Bill Wendlingfb440502012-03-28 20:46:54 +0000307/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000308void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000309 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000310 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000311
Bill Wendling45f74e32012-08-06 22:52:45 +0000312 if (!v->hasSection() /* || !isTargetDarwin */)
313 return;
314
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000315 // Special case i386/ppc ObjC data structures in magic sections:
316 // The issue is that the old ObjC object format did some strange
317 // contortions to avoid real linker symbols. For instance, the
318 // ObjC class data structure is allocated statically in the executable
319 // that defines that class. That data structures contains a pointer to
320 // its superclass. But instead of just initializing that part of the
321 // struct to the address of its superclass, and letting the static and
322 // dynamic linkers do the rest, the runtime works by having that field
323 // instead point to a C-string that is the name of the superclass.
324 // At runtime the objc initialization updates that pointer and sets
325 // it to point to the actual super class. As far as the linker
326 // knows it is just a pointer to a string. But then someone wanted the
327 // linker to issue errors at build time if the superclass was not found.
328 // So they figured out a way in mach-o object format to use an absolute
329 // symbols (.objc_class_name_Foo = 0) and a floating reference
330 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
331 // a class was missing.
332 // The following synthesizes the implicit .objc_* symbols for the linker
333 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000334
Bill Wendling45f74e32012-08-06 22:52:45 +0000335 // special case if this data blob is an ObjC class definition
336 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000337 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000338 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000339 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000340 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000341
Bill Wendling45f74e32012-08-06 22:52:45 +0000342 // special case if this data blob is an ObjC category definition
343 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000344 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000345 addObjCCategory(gv);
346 }
347 }
348
349 // special case if this data blob is the list of referenced classes
350 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000351 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000352 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000353 }
354 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000355}
356
Bill Wendlingfb440502012-03-28 20:46:54 +0000357/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000358void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000359 // add to list of defined symbols
360 addDefinedSymbol(f, true);
361}
362
Rafael Espindola282a4702013-10-31 20:51:58 +0000363static bool canBeHidden(const GlobalValue *GV) {
Rafael Espindola66f273b2014-02-07 19:04:43 +0000364 // FIXME: this is duplicated with another static function in AsmPrinter.cpp
Rafael Espindola282a4702013-10-31 20:51:58 +0000365 GlobalValue::LinkageTypes L = GV->getLinkage();
366
Rafael Espindola282a4702013-10-31 20:51:58 +0000367 if (L != GlobalValue::LinkOnceODRLinkage)
368 return false;
369
370 if (GV->hasUnnamedAddr())
371 return true;
372
Rafael Espindola66f273b2014-02-07 19:04:43 +0000373 // 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 Espindola282a4702013-10-31 20:51:58 +0000380 GlobalStatus GS;
381 if (GlobalStatus::analyzeGlobal(GV, GS))
382 return false;
383
384 return !GS.IsCompared;
385}
386
Bill Wendlingfb440502012-03-28 20:46:54 +0000387/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000388void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000389 // ignore all llvm.* symbols
390 if (def->getName().startswith("llvm."))
391 return;
392
393 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000394 SmallString<64> Buffer;
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000395 _target->getNameWithPrefix(Buffer, def, _mangler);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000396
397 // set alignment part log2() can have rounding errors
398 uint32_t align = def->getAlignment();
Rafael Espindola94751172014-05-05 14:18:16 +0000399 uint32_t attr = align ? countTrailingZeros(align) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000400
401 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000402 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000403 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000404 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000405 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000406 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 Espindola2fb5bc32014-03-13 23:18:37 +0000413 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000414 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000415 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000416 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000417 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000418 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000419
420 // set scope part
421 if (def->hasHiddenVisibility())
422 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
423 else if (def->hasProtectedVisibility())
424 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Rafael Espindola282a4702013-10-31 20:51:58 +0000425 else if (canBeHidden(def))
426 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000427 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
Rafael Espindola2fb5bc32014-03-13 23:18:37 +0000428 def->hasLinkOnceLinkage() || def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000429 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
430 else
431 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
432
Chad Rosier772a91f2011-06-28 18:26:12 +0000433 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000434 entry.setValue(1);
435
Bill Wendling9ee2d332012-03-29 08:27:32 +0000436 // fill information structure
437 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000438 StringRef Name = entry.getKey();
439 info.name = Name.data();
440 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000441 info.attributes = attr;
442 info.isFunction = isFunction;
443 info.symbol = def;
444
445 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000446 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000447}
448
Bill Wendlingfb440502012-03-28 20:46:54 +0000449/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
450/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000451void LTOModule::addAsmGlobalSymbol(const char *name,
452 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000453 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
454
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000455 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000456 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000457 return;
458
Rafael Espindola477d11f2011-02-20 16:27:25 +0000459 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000460
461 NameAndAttributes &info = _undefines[entry.getKey().data()];
462
Craig Topper2617dcc2014-04-15 06:32:26 +0000463 if (info.symbol == nullptr) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000464 // 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 Espindola5f4b32f2012-05-11 03:42:13 +0000473 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000474 info.attributes =
475 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
476 info.isFunction = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000477 info.symbol = nullptr;
Bill Wendling71b19bb2012-04-02 10:01:21 +0000478
479 // add to table of symbols
480 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000481 return;
482 }
483
Bill Wendling9ee2d332012-03-29 08:27:32 +0000484 if (info.isFunction)
485 addDefinedFunctionSymbol(cast<Function>(info.symbol));
486 else
487 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000488
489 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
490 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000491}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000492
Bill Wendlingfb440502012-03-28 20:46:54 +0000493/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
494/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000495void 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 Toker98444342014-04-19 23:56:35 +0000505 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000506 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
507 NameAndAttributes info;
508 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000509 info.attributes = attr;
510 info.isFunction = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000511 info.symbol = nullptr;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000512
513 entry.setValue(info);
514}
515
Bill Wendlingfb440502012-03-28 20:46:54 +0000516/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
517/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000518void
519LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000520 // ignore all llvm.* symbols
521 if (decl->getName().startswith("llvm."))
522 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000523
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000524 // ignore all aliases
525 if (isa<GlobalAlias>(decl))
526 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000527
Rafael Espindola34b59382011-02-11 05:23:09 +0000528 SmallString<64> name;
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000529 _target->getNameWithPrefix(name, decl, _mangler);
Rafael Espindola56548522009-04-24 16:55:21 +0000530
Rafael Espindola477d11f2011-02-20 16:27:25 +0000531 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000532 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000533
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000534 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000535 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000536 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000537
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000538 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000539
540 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000541
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000542 if (decl->hasExternalWeakLinkage())
543 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
544 else
545 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000546
Bill Wendling9ee2d332012-03-29 08:27:32 +0000547 info.isFunction = isFunc;
548 info.symbol = decl;
549
Rafael Espindola477d11f2011-02-20 16:27:25 +0000550 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000551}
552
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000553namespace {
Rafael Espindola20fcda72014-01-22 22:11:14 +0000554
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000555 class RecordStreamer : public MCStreamer {
556 public:
Bill Wendling32867652012-04-03 03:56:52 +0000557 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000558
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000559 private:
560 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000561
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000562 void markDefined(const MCSymbol &Symbol) {
563 State &S = Symbols[Symbol.getName()];
564 switch (S) {
565 case DefinedGlobal:
566 case Global:
567 S = DefinedGlobal;
568 break;
569 case NeverSeen:
570 case Defined:
571 case Used:
572 S = Defined;
573 break;
574 }
575 }
576 void markGlobal(const MCSymbol &Symbol) {
577 State &S = Symbols[Symbol.getName()];
578 switch (S) {
579 case DefinedGlobal:
580 case Defined:
581 S = DefinedGlobal;
582 break;
583
584 case NeverSeen:
585 case Global:
586 case Used:
587 S = Global;
588 break;
589 }
590 }
591 void markUsed(const MCSymbol &Symbol) {
592 State &S = Symbols[Symbol.getName()];
593 switch (S) {
594 case DefinedGlobal:
595 case Defined:
596 case Global:
597 break;
598
599 case NeverSeen:
600 case Used:
601 S = Used;
602 break;
603 }
604 }
605
606 // FIXME: mostly copied for the obj streamer.
607 void AddValueSymbols(const MCExpr *Value) {
608 switch (Value->getKind()) {
609 case MCExpr::Target:
610 // FIXME: What should we do in here?
611 break;
612
613 case MCExpr::Constant:
614 break;
615
616 case MCExpr::Binary: {
617 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
618 AddValueSymbols(BE->getLHS());
619 AddValueSymbols(BE->getRHS());
620 break;
621 }
622
623 case MCExpr::SymbolRef:
624 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
625 break;
626
627 case MCExpr::Unary:
628 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
629 break;
630 }
631 }
632
633 public:
634 typedef StringMap<State>::const_iterator const_iterator;
635
636 const_iterator begin() {
637 return Symbols.begin();
638 }
639
640 const_iterator end() {
641 return Symbols.end();
642 }
643
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000644 RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000645
Craig Topperb51ff602014-03-08 07:51:20 +0000646 void EmitInstruction(const MCInst &Inst,
647 const MCSubtargetInfo &STI) override {
Bill Wendling32867652012-04-03 03:56:52 +0000648 // Scan for values.
649 for (unsigned i = Inst.getNumOperands(); i--; )
650 if (Inst.getOperand(i).isExpr())
651 AddValueSymbols(Inst.getOperand(i).getExpr());
652 }
Craig Topperb51ff602014-03-08 07:51:20 +0000653 void EmitLabel(MCSymbol *Symbol) override {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000654 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000655 markDefined(*Symbol);
656 }
Craig Topperb51ff602014-03-08 07:51:20 +0000657 void EmitDebugLabel(MCSymbol *Symbol) override {
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000658 EmitLabel(Symbol);
659 }
Craig Topperb51ff602014-03-08 07:51:20 +0000660 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000661 // FIXME: should we handle aliases?
662 markDefined(*Symbol);
Tom Roedered0e88c2014-03-31 16:59:13 +0000663 AddValueSymbols(Value);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000664 }
Craig Topperb51ff602014-03-08 07:51:20 +0000665 bool EmitSymbolAttribute(MCSymbol *Symbol,
666 MCSymbolAttr Attribute) override {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000667 if (Attribute == MCSA_Global)
668 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000669 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000670 }
Craig Topperb51ff602014-03-08 07:51:20 +0000671 void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
672 uint64_t Size , unsigned ByteAlignment) override {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000673 markDefined(*Symbol);
674 }
Craig Topperb51ff602014-03-08 07:51:20 +0000675 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
676 unsigned ByteAlignment) override {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000677 markDefined(*Symbol);
678 }
Bill Wendling32867652012-04-03 03:56:52 +0000679
Craig Topperb51ff602014-03-08 07:51:20 +0000680 void EmitBundleAlignMode(unsigned AlignPow2) override {}
681 void EmitBundleLock(bool AlignToEnd) override {}
682 void EmitBundleUnlock() override {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000683
Bill Wendling32867652012-04-03 03:56:52 +0000684 // Noop calls.
Craig Topperb51ff602014-03-08 07:51:20 +0000685 void ChangeSection(const MCSection *Section,
686 const MCExpr *Subsection) override {}
687 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {}
688 void EmitThumbFunc(MCSymbol *Func) override {}
689 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override {}
690 void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override {}
691 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
692 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
693 void EmitCOFFSymbolType(int Type) override {}
694 void EndCOFFSymbolDef() override {}
695 void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override {}
696 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
697 unsigned ByteAlignment) override {}
698 void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
699 uint64_t Size, unsigned ByteAlignment) override {}
700 void EmitBytes(StringRef Data) override {}
Kevin Enderby96918bc2014-04-22 17:27:29 +0000701 void EmitValueImpl(const MCExpr *Value, unsigned Size,
702 const SMLoc &Loc) override {}
Craig Topperb51ff602014-03-08 07:51:20 +0000703 void EmitULEB128Value(const MCExpr *Value) override {}
704 void EmitSLEB128Value(const MCExpr *Value) override {}
705 void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
706 unsigned ValueSize,
707 unsigned MaxBytesToEmit) override {}
708 void EmitCodeAlignment(unsigned ByteAlignment,
709 unsigned MaxBytesToEmit) override {}
710 bool EmitValueToOffset(const MCExpr *Offset,
711 unsigned char Value) override { return false; }
712 void EmitFileDirective(StringRef Filename) override {}
713 void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
714 const MCSymbol *Label,
715 unsigned PointerSize) override {}
716 void FinishImpl() override {}
717 void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000718 RecordProcEnd(Frame);
719 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000720 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000721} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000722
Bill Wendlingfb440502012-03-28 20:46:54 +0000723/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
724/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000725bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000726 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000727 if (inlineAsm.empty())
728 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000729
Ahmed Charles56440fd2014-03-06 05:51:42 +0000730 std::unique_ptr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000731 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
732 SourceMgr SrcMgr;
733 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000734 std::unique_ptr<MCAsmParser> Parser(
735 createMCAsmParser(SrcMgr, _context, *Streamer, *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000736 const Target &T = _target->getTarget();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000737 std::unique_ptr<MCInstrInfo> MCII(T.createMCInstrInfo());
738 std::unique_ptr<MCSubtargetInfo> STI(T.createMCSubtargetInfo(
739 _target->getTargetTriple(), _target->getTargetCPU(),
740 _target->getTargetFeatureString()));
741 std::unique_ptr<MCTargetAsmParser> TAP(
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000742 T.createMCAsmParser(*STI, *Parser.get(), *MCII,
743 _target->Options.MCOptions));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000744 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000745 errMsg = "target " + std::string(T.getName()) +
746 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000747 return true;
748 }
749
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000750 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000751 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000752 return true;
753
754 for (RecordStreamer::const_iterator i = Streamer->begin(),
755 e = Streamer->end(); i != e; ++i) {
756 StringRef Key = i->first();
757 RecordStreamer::State Value = i->second;
758 if (Value == RecordStreamer::DefinedGlobal)
759 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
760 else if (Value == RecordStreamer::Defined)
761 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
762 else if (Value == RecordStreamer::Global ||
763 Value == RecordStreamer::Used)
764 addAsmGlobalSymbolUndef(Key.data());
765 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000766
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000767 return false;
768}
769
Bill Wendlingfb440502012-03-28 20:46:54 +0000770/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000771static bool isDeclaration(const GlobalValue &V) {
772 if (V.hasAvailableExternallyLinkage())
773 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000774
Rafael Espindola5b778b22011-03-18 19:51:00 +0000775 if (V.isMaterializable())
776 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000777
Rafael Espindola5b778b22011-03-18 19:51:00 +0000778 return V.isDeclaration();
779}
780
Bill Wendling7e58b382012-03-28 23:12:18 +0000781/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000782/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000783bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000784 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000785 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000786 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000787 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000788 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000789 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000790 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000791
Daniel Dunbar919660b2010-08-10 23:46:46 +0000792 // add data
793 for (Module::global_iterator v = _module->global_begin(),
794 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000795 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000796 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000797 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000798 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000799 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000800
Daniel Dunbar919660b2010-08-10 23:46:46 +0000801 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000802 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000803 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000804
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000805 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000806 for (Module::alias_iterator a = _module->alias_begin(),
807 e = _module->alias_end(); a != e; ++a) {
808 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000809 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000810 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000811 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000812 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000813 }
814
Daniel Dunbar919660b2010-08-10 23:46:46 +0000815 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000816 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
817 e = _undefines.end(); u != e; ++u) {
818 // If this symbol also has a definition, then don't make an undefine because
819 // it is a tentative definition.
820 if (_defines.count(u->getKey())) continue;
821 NameAndAttributes info = u->getValue();
822 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000823 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000824
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000825 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000826}
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000827
828/// parseMetadata - Parse metadata from the module
829void LTOModule::parseMetadata() {
830 // Linker Options
831 if (Value *Val = _module->getModuleFlag("Linker Options")) {
832 MDNode *LinkerOptions = cast<MDNode>(Val);
833 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
834 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
835 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
836 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
837 StringRef Op = _linkeropt_strings.
838 GetOrCreateValue(MDOption->getString()).getKey();
839 StringRef DepLibName = _target->getTargetLowering()->
840 getObjFileLowering().getDepLibFromLinkerOpt(Op);
841 if (!DepLibName.empty())
842 _deplibs.push_back(DepLibName.data());
843 else if (!Op.empty())
844 _linkeropts.push_back(Op.data());
845 }
846 }
847 }
848
849 // Add other interesting metadata here.
850}