blob: fb20806634878fd9382981167aba20bfcc727fa4 [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/OwningPtr.h"
17#include "llvm/ADT/Triple.h"
18#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/LLVMContext.h"
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +000021#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000023#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCInst.h"
Joey Goulydb6144e2013-09-12 12:55:29 +000025#include "llvm/MC/MCInstrInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000026#include "llvm/MC/MCParser/MCAsmParser.h"
Rafael Espindola20fcda72014-01-22 22:11:14 +000027#include "llvm/MC/MCSection.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000028#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000029#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000030#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000031#include "llvm/MC/MCTargetAsmParser.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000032#include "llvm/MC/SubtargetFeature.h"
Bill Wendlingb8dcda72012-08-06 21:34:54 +000033#include "llvm/Support/CommandLine.h"
Rafael Espindola46ed3532013-06-11 18:05:26 +000034#include "llvm/Support/FileSystem.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000035#include "llvm/Support/Host.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000036#include "llvm/Support/MemoryBuffer.h"
37#include "llvm/Support/Path.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000038#include "llvm/Support/SourceMgr.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000039#include "llvm/Support/TargetRegistry.h"
40#include "llvm/Support/TargetSelect.h"
41#include "llvm/Support/system_error.h"
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +000042#include "llvm/Target/TargetLowering.h"
43#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000044#include "llvm/Target/TargetRegisterInfo.h"
Rafael Espindola282a4702013-10-31 20:51:58 +000045#include "llvm/Transforms/Utils/GlobalStatus.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000046using namespace llvm;
47
Bill Wendlingfb440502012-03-28 20:46:54 +000048LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
49 : _module(m), _target(t),
Rafael Espindolae28610d2013-12-09 20:26:40 +000050 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), &ObjFileInfo),
Rafael Espindola58873562014-01-03 19:21:54 +000051 _mangler(t->getDataLayout()) {
Rafael Espindolae28610d2013-12-09 20:26:40 +000052 ObjFileInfo.InitMCObjectFileInfo(t->getTargetTriple(),
53 t->getRelocationModel(), t->getCodeModel(),
54 _context);
55}
Bill Wendlingfb440502012-03-28 20:46:54 +000056
57/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
58/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000059bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +000060 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
61 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000062}
63
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000064bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +000065 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 Kledzik07b4a622008-02-26 20:26:43 +000069}
70
Bill Wendlingfb440502012-03-28 20:46:54 +000071/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
72/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000073bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
74 const char *triplePrefix) {
75 MemoryBuffer *buffer = makeBuffer(mem, length);
76 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +000077 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000078 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +000079}
80
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000081bool LTOModule::isBitcodeFileForTarget(const char *path,
82 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000083 OwningPtr<MemoryBuffer> buffer;
84 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000085 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000086 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000087}
88
Bill Wendlingfb440502012-03-28 20:46:54 +000089/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
90/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000091bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +000092 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
93 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +000094 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000095}
96
Bill Wendlingfb440502012-03-28 20:46:54 +000097/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
98/// the buffer.
Rafael Espindola0b385c72013-09-30 16:39:19 +000099LTOModule *LTOModule::makeLTOModule(const char *path, TargetOptions options,
100 std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000101 OwningPtr<MemoryBuffer> buffer;
102 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000103 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000104 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000105 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000106 return makeLTOModule(buffer.take(), options, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000107}
108
Rafael Espindola56e41f72011-02-08 22:40:47 +0000109LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000110 size_t size, TargetOptions options,
111 std::string &errMsg) {
112 return makeLTOModule(fd, path, size, 0, options, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000113}
114
115LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000116 size_t map_size,
117 off_t offset,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000118 TargetOptions options,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000119 std::string &errMsg) {
120 OwningPtr<MemoryBuffer> buffer;
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000121 if (error_code ec =
122 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000123 errMsg = ec.message();
124 return NULL;
125 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000126 return makeLTOModule(buffer.take(), options, errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000127}
128
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000129LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000130 TargetOptions options,
Manman Ren03456a12014-02-10 23:26:14 +0000131 std::string &errMsg, StringRef path) {
132 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length, path));
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000133 if (!buffer)
134 return NULL;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000135 return makeLTOModule(buffer.take(), options, errMsg);
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000136}
137
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000138LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000139 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000140 std::string &errMsg) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000141 // parse bitcode buffer
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000142 ErrorOr<Module *> ModuleOrErr =
143 getLazyBitcodeModule(buffer, getGlobalContext());
144 if (error_code EC = ModuleOrErr.getError()) {
145 errMsg = EC.message();
Rafael Espindola5b778b22011-03-18 19:51:00 +0000146 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000147 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000148 }
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000149 OwningPtr<Module> m(ModuleOrErr.get());
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000150
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000151 std::string TripleStr = m->getTargetTriple();
152 if (TripleStr.empty())
153 TripleStr = sys::getDefaultTargetTriple();
154 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000155
156 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000157 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000158 if (!march)
159 return NULL;
160
Nick Lewycky364c04a2011-04-21 01:54:08 +0000161 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000162 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000163 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000164 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000165 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000166 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000167 if (Triple.isOSDarwin()) {
168 if (Triple.getArch() == llvm::Triple::x86_64)
169 CPU = "core2";
170 else if (Triple.getArch() == llvm::Triple::x86)
171 CPU = "yonah";
172 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000173
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000174 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000175 options);
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000176 m->materializeAllPermanently();
Rafael Espindola282a4702013-10-31 20:51:58 +0000177
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000178 LTOModule *Ret = new LTOModule(m.take(), target);
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000179
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 Wendling7e58b382012-03-28 23:12:18 +0000189 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000190 delete Ret;
191 return NULL;
192 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000193
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000194 Ret->parseMetadata();
195
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000196 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000197}
198
Manman Ren03456a12014-02-10 23:26:14 +0000199/// Create a MemoryBuffer from a memory range with an optional name.
200MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length,
201 StringRef name) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000202 const char *startPtr = (const char*)mem;
Manman Ren03456a12014-02-10 23:26:14 +0000203 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000204}
205
Bill Wendlingfb440502012-03-28 20:46:54 +0000206/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000207bool
208LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
209 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000210 Constant *op = ce->getOperand(0);
211 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
212 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000213 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000214 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000215 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000216 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000217 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000218 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000219 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000220 }
221 return false;
222}
223
Bill Wendlingfb440502012-03-28 20:46:54 +0000224/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000225void LTOModule::addObjCClass(const GlobalVariable *clgv) {
226 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000227 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000228
Bill Wendling5f689e72011-11-04 18:48:00 +0000229 // 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 Espindola477d11f2011-02-20 16:27:25 +0000236 const char *symbolName = entry.getKey().data();
237 info.name = symbolName;
238 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000239 info.isFunction = false;
240 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000241 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000242 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000243 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000244
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 Wendling9ee2d332012-03-29 08:27:32 +0000250
Bill Wendling5f689e72011-11-04 18:48:00 +0000251 NameAndAttributes info;
252 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000253 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
254 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
255 info.isFunction = false;
256 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000257 _symbols.push_back(info);
258 }
259}
260
Bill Wendlingfb440502012-03-28 20:46:54 +0000261/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000262void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
263 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000264 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 Wendling9ee2d332012-03-29 08:27:32 +0000281 info.isFunction = false;
282 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000283 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000284}
285
Bill Wendlingfb440502012-03-28 20:46:54 +0000286/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000287void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000288 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000289 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
290 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000291
Bill Wendling5f689e72011-11-04 18:48:00 +0000292 NameAndAttributes info;
293 StringMap<NameAndAttributes>::value_type &entry =
294 _undefines.GetOrCreateValue(targetclassName);
295 if (entry.getValue().name)
296 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000297
Bill Wendling5f689e72011-11-04 18:48:00 +0000298 const char *symbolName = entry.getKey().data();
299 info.name = symbolName;
300 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000301 info.isFunction = false;
302 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000303 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000304}
305
Bill Wendlingfb440502012-03-28 20:46:54 +0000306/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000307void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000308 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000309 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000310
Bill Wendling45f74e32012-08-06 22:52:45 +0000311 if (!v->hasSection() /* || !isTargetDarwin */)
312 return;
313
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000314 // 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 Dunbar5657e7b2010-08-10 23:46:39 +0000333
Bill Wendling45f74e32012-08-06 22:52:45 +0000334 // special case if this data blob is an ObjC class definition
335 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000336 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000337 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000338 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000339 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000340
Bill Wendling45f74e32012-08-06 22:52:45 +0000341 // special case if this data blob is an ObjC category definition
342 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000343 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000344 addObjCCategory(gv);
345 }
346 }
347
348 // special case if this data blob is the list of referenced classes
349 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000350 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000351 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000352 }
353 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000354}
355
Bill Wendlingfb440502012-03-28 20:46:54 +0000356/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000357void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000358 // add to list of defined symbols
359 addDefinedSymbol(f, true);
360}
361
Rafael Espindola282a4702013-10-31 20:51:58 +0000362static bool canBeHidden(const GlobalValue *GV) {
Rafael Espindola66f273b2014-02-07 19:04:43 +0000363 // FIXME: this is duplicated with another static function in AsmPrinter.cpp
Rafael Espindola282a4702013-10-31 20:51:58 +0000364 GlobalValue::LinkageTypes L = GV->getLinkage();
365
Rafael Espindola282a4702013-10-31 20:51:58 +0000366 if (L != GlobalValue::LinkOnceODRLinkage)
367 return false;
368
369 if (GV->hasUnnamedAddr())
370 return true;
371
Rafael Espindola66f273b2014-02-07 19:04:43 +0000372 // If it is a non constant variable, it needs to be uniqued across shared
373 // objects.
374 if (const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV)) {
375 if (!Var->isConstant())
376 return false;
377 }
378
Rafael Espindola282a4702013-10-31 20:51:58 +0000379 GlobalStatus GS;
380 if (GlobalStatus::analyzeGlobal(GV, GS))
381 return false;
382
383 return !GS.IsCompared;
384}
385
Bill Wendlingfb440502012-03-28 20:46:54 +0000386/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000387void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000388 // ignore all llvm.* symbols
389 if (def->getName().startswith("llvm."))
390 return;
391
392 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000393 SmallString<64> Buffer;
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000394 _target->getNameWithPrefix(Buffer, def, _mangler);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000395
396 // set alignment part log2() can have rounding errors
397 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000398 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000399
400 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000401 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000402 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000403 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000404 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000405 if (gv && gv->isConstant())
406 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
407 else
408 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
409 }
410
411 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000412 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000413 def->hasLinkerPrivateWeakLinkage())
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() ||
428 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
429 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000430 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
431 else
432 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
433
Chad Rosier772a91f2011-06-28 18:26:12 +0000434 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000435 entry.setValue(1);
436
Bill Wendling9ee2d332012-03-29 08:27:32 +0000437 // fill information structure
438 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000439 StringRef Name = entry.getKey();
440 info.name = Name.data();
441 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000442 info.attributes = attr;
443 info.isFunction = isFunction;
444 info.symbol = def;
445
446 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000447 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000448}
449
Bill Wendlingfb440502012-03-28 20:46:54 +0000450/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
451/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000452void LTOModule::addAsmGlobalSymbol(const char *name,
453 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000454 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
455
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000456 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000457 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000458 return;
459
Rafael Espindola477d11f2011-02-20 16:27:25 +0000460 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000461
462 NameAndAttributes &info = _undefines[entry.getKey().data()];
463
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000464 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000465 // FIXME: This is trying to take care of module ASM like this:
466 //
467 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
468 //
469 // but is gross and its mother dresses it funny. Have the ASM parser give us
470 // more details for this type of situation so that we're not guessing so
471 // much.
472
473 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000474 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000475 info.attributes =
476 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
477 info.isFunction = false;
478 info.symbol = 0;
479
480 // add to table of symbols
481 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000482 return;
483 }
484
Bill Wendling9ee2d332012-03-29 08:27:32 +0000485 if (info.isFunction)
486 addDefinedFunctionSymbol(cast<Function>(info.symbol));
487 else
488 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000489
490 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
491 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000492}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000493
Bill Wendlingfb440502012-03-28 20:46:54 +0000494/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
495/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000496void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
497 StringMap<NameAndAttributes>::value_type &entry =
498 _undefines.GetOrCreateValue(name);
499
500 _asm_undefines.push_back(entry.getKey().data());
501
502 // we already have the symbol
503 if (entry.getValue().name)
504 return;
505
506 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
507 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
508 NameAndAttributes info;
509 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000510 info.attributes = attr;
511 info.isFunction = false;
512 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000513
514 entry.setValue(info);
515}
516
Bill Wendlingfb440502012-03-28 20:46:54 +0000517/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
518/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000519void
520LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000521 // ignore all llvm.* symbols
522 if (decl->getName().startswith("llvm."))
523 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000524
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000525 // ignore all aliases
526 if (isa<GlobalAlias>(decl))
527 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000528
Rafael Espindola34b59382011-02-11 05:23:09 +0000529 SmallString<64> name;
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000530 _target->getNameWithPrefix(name, decl, _mangler);
Rafael Espindola56548522009-04-24 16:55:21 +0000531
Rafael Espindola477d11f2011-02-20 16:27:25 +0000532 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000533 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000534
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000535 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000536 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000537 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000538
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000539 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000540
541 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000542
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000543 if (decl->hasExternalWeakLinkage())
544 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
545 else
546 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000547
Bill Wendling9ee2d332012-03-29 08:27:32 +0000548 info.isFunction = isFunc;
549 info.symbol = decl;
550
Rafael Espindola477d11f2011-02-20 16:27:25 +0000551 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000552}
553
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000554namespace {
Rafael Espindola20fcda72014-01-22 22:11:14 +0000555
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000556 class RecordStreamer : public MCStreamer {
557 public:
Bill Wendling32867652012-04-03 03:56:52 +0000558 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000559
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000560 private:
561 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000562
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000563 void markDefined(const MCSymbol &Symbol) {
564 State &S = Symbols[Symbol.getName()];
565 switch (S) {
566 case DefinedGlobal:
567 case Global:
568 S = DefinedGlobal;
569 break;
570 case NeverSeen:
571 case Defined:
572 case Used:
573 S = Defined;
574 break;
575 }
576 }
577 void markGlobal(const MCSymbol &Symbol) {
578 State &S = Symbols[Symbol.getName()];
579 switch (S) {
580 case DefinedGlobal:
581 case Defined:
582 S = DefinedGlobal;
583 break;
584
585 case NeverSeen:
586 case Global:
587 case Used:
588 S = Global;
589 break;
590 }
591 }
592 void markUsed(const MCSymbol &Symbol) {
593 State &S = Symbols[Symbol.getName()];
594 switch (S) {
595 case DefinedGlobal:
596 case Defined:
597 case Global:
598 break;
599
600 case NeverSeen:
601 case Used:
602 S = Used;
603 break;
604 }
605 }
606
607 // FIXME: mostly copied for the obj streamer.
608 void AddValueSymbols(const MCExpr *Value) {
609 switch (Value->getKind()) {
610 case MCExpr::Target:
611 // FIXME: What should we do in here?
612 break;
613
614 case MCExpr::Constant:
615 break;
616
617 case MCExpr::Binary: {
618 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
619 AddValueSymbols(BE->getLHS());
620 AddValueSymbols(BE->getRHS());
621 break;
622 }
623
624 case MCExpr::SymbolRef:
625 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
626 break;
627
628 case MCExpr::Unary:
629 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
630 break;
631 }
632 }
633
634 public:
635 typedef StringMap<State>::const_iterator const_iterator;
636
637 const_iterator begin() {
638 return Symbols.begin();
639 }
640
641 const_iterator end() {
642 return Symbols.end();
643 }
644
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000645 RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000646
David Woodhousee6c13e42014-01-28 23:12:42 +0000647 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) {
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 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000653 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000654 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000655 markDefined(*Symbol);
656 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000657 virtual void EmitDebugLabel(MCSymbol *Symbol) {
658 EmitLabel(Symbol);
659 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000660 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
661 // FIXME: should we handle aliases?
662 markDefined(*Symbol);
663 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000664 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000665 if (Attribute == MCSA_Global)
666 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000667 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000668 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000669 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000670 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000671 markDefined(*Symbol);
672 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000673 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
674 unsigned ByteAlignment) {
675 markDefined(*Symbol);
676 }
Bill Wendling32867652012-04-03 03:56:52 +0000677
Eli Benderskyf483ff92012-12-20 19:05:53 +0000678 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000679 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000680 virtual void EmitBundleUnlock() {}
681
Bill Wendling32867652012-04-03 03:56:52 +0000682 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000683 virtual void ChangeSection(const MCSection *Section,
684 const MCExpr *Subsection) {}
Bill Wendling32867652012-04-03 03:56:52 +0000685 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
686 virtual void EmitThumbFunc(MCSymbol *Func) {}
687 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
688 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
689 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
690 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
691 virtual void EmitCOFFSymbolType(int Type) {}
692 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000693 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000694 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
695 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000696 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
697 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000698 virtual void EmitBytes(StringRef Data) {}
699 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000700 virtual void EmitULEB128Value(const MCExpr *Value) {}
701 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000702 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
703 unsigned ValueSize,
704 unsigned MaxBytesToEmit) {}
705 virtual void EmitCodeAlignment(unsigned ByteAlignment,
706 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000707 virtual bool EmitValueToOffset(const MCExpr *Offset,
708 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000709 virtual void EmitFileDirective(StringRef Filename) {}
710 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
711 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000712 const MCSymbol *Label,
713 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000714 virtual void FinishImpl() {}
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000715 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
716 RecordProcEnd(Frame);
717 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000718 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000719} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000720
Bill Wendlingfb440502012-03-28 20:46:54 +0000721/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
722/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000723bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000724 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000725 if (inlineAsm.empty())
726 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000727
Bill Wendlingac2abde2011-11-04 09:24:40 +0000728 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000729 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
730 SourceMgr SrcMgr;
731 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000732 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000733 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000734 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000735 const Target &T = _target->getTarget();
Joey Goulydb6144e2013-09-12 12:55:29 +0000736 OwningPtr<MCInstrInfo> MCII(T.createMCInstrInfo());
Bill Wendling9351b3e2012-08-08 22:01:55 +0000737 OwningPtr<MCSubtargetInfo>
738 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
739 _target->getTargetCPU(),
740 _target->getTargetFeatureString()));
Joey Goulydb6144e2013-09-12 12:55:29 +0000741 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get(), *MCII));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000742 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000743 errMsg = "target " + std::string(T.getName()) +
744 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000745 return true;
746 }
747
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000748 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000749 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000750 return true;
751
752 for (RecordStreamer::const_iterator i = Streamer->begin(),
753 e = Streamer->end(); i != e; ++i) {
754 StringRef Key = i->first();
755 RecordStreamer::State Value = i->second;
756 if (Value == RecordStreamer::DefinedGlobal)
757 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
758 else if (Value == RecordStreamer::Defined)
759 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
760 else if (Value == RecordStreamer::Global ||
761 Value == RecordStreamer::Used)
762 addAsmGlobalSymbolUndef(Key.data());
763 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000764
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000765 return false;
766}
767
Bill Wendlingfb440502012-03-28 20:46:54 +0000768/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000769static bool isDeclaration(const GlobalValue &V) {
770 if (V.hasAvailableExternallyLinkage())
771 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000772
Rafael Espindola5b778b22011-03-18 19:51:00 +0000773 if (V.isMaterializable())
774 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000775
Rafael Espindola5b778b22011-03-18 19:51:00 +0000776 return V.isDeclaration();
777}
778
Bill Wendling7e58b382012-03-28 23:12:18 +0000779/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000780/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000781bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000782 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000783 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000784 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000785 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000786 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000787 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000788 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000789
Daniel Dunbar919660b2010-08-10 23:46:46 +0000790 // add data
791 for (Module::global_iterator v = _module->global_begin(),
792 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000793 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000794 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000795 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000796 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000797 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000798
Daniel Dunbar919660b2010-08-10 23:46:46 +0000799 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000800 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000801 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000802
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000803 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000804 for (Module::alias_iterator a = _module->alias_begin(),
805 e = _module->alias_end(); a != e; ++a) {
806 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000807 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000808 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000809 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000810 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000811 }
812
Daniel Dunbar919660b2010-08-10 23:46:46 +0000813 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000814 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
815 e = _undefines.end(); u != e; ++u) {
816 // If this symbol also has a definition, then don't make an undefine because
817 // it is a tentative definition.
818 if (_defines.count(u->getKey())) continue;
819 NameAndAttributes info = u->getValue();
820 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000821 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000822
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000823 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000824}
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000825
826/// parseMetadata - Parse metadata from the module
827void LTOModule::parseMetadata() {
828 // Linker Options
829 if (Value *Val = _module->getModuleFlag("Linker Options")) {
830 MDNode *LinkerOptions = cast<MDNode>(Val);
831 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
832 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
833 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
834 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
835 StringRef Op = _linkeropt_strings.
836 GetOrCreateValue(MDOption->getString()).getKey();
837 StringRef DepLibName = _target->getTargetLowering()->
838 getObjFileLowering().getDepLibFromLinkerOpt(Op);
839 if (!DepLibName.empty())
840 _deplibs.push_back(DepLibName.data());
841 else if (!Op.empty())
842 _linkeropts.push_back(Op.data());
843 }
844 }
845 }
846
847 // Add other interesting metadata here.
848}