blob: d1175142651b1cf7ab8db600eb710c64e19eb678 [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 Northover3b0846e2014-05-24 12:50:23 +0000171 else if (Triple.getArch() == llvm::Triple::arm64 ||
172 Triple.getArch() == llvm::Triple::aarch64)
Tim Northover00ed9962014-03-29 10:18:08 +0000173 CPU = "cyclone";
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000174 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000175
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000176 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000177 options);
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000178 m->materializeAllPermanently();
Rafael Espindola282a4702013-10-31 20:51:58 +0000179
Ahmed Charles96c9d952014-03-05 10:19:29 +0000180 LTOModule *Ret = new LTOModule(m.release(), target);
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000181
182 // We need a MCContext set up in order to get mangled names of private
183 // symbols. It is a bit odd that we need to report uses and definitions
184 // of private symbols, but it does look like ld64 expects to be informed
185 // of at least the ones with an 'l' prefix.
186 MCContext &Context = Ret->_context;
187 const TargetLoweringObjectFile &TLOF =
188 target->getTargetLowering()->getObjFileLowering();
189 const_cast<TargetLoweringObjectFile &>(TLOF).Initialize(Context, *target);
190
Bill Wendling7e58b382012-03-28 23:12:18 +0000191 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000192 delete Ret;
Craig Topper2617dcc2014-04-15 06:32:26 +0000193 return nullptr;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000194 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000195
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000196 Ret->parseMetadata();
197
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000198 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000199}
200
Manman Ren03456a12014-02-10 23:26:14 +0000201/// Create a MemoryBuffer from a memory range with an optional name.
202MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length,
203 StringRef name) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000204 const char *startPtr = (const char*)mem;
Manman Ren03456a12014-02-10 23:26:14 +0000205 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000206}
207
Bill Wendlingfb440502012-03-28 20:46:54 +0000208/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000209bool
210LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
211 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000212 Constant *op = ce->getOperand(0);
213 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
214 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000215 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000216 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000217 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000218 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000219 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000220 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000221 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000222 }
223 return false;
224}
225
Bill Wendlingfb440502012-03-28 20:46:54 +0000226/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000227void LTOModule::addObjCClass(const GlobalVariable *clgv) {
228 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000229 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000230
Bill Wendling5f689e72011-11-04 18:48:00 +0000231 // second slot in __OBJC,__class is pointer to superclass name
232 std::string superclassName;
233 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
234 NameAndAttributes info;
235 StringMap<NameAndAttributes>::value_type &entry =
236 _undefines.GetOrCreateValue(superclassName);
237 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000238 const char *symbolName = entry.getKey().data();
239 info.name = symbolName;
240 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000241 info.isFunction = false;
242 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000243 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000244 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000245 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000246
247 // third slot in __OBJC,__class is pointer to class name
248 std::string className;
249 if (objcClassNameFromExpression(c->getOperand(2), className)) {
250 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
251 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000252
Bill Wendling5f689e72011-11-04 18:48:00 +0000253 NameAndAttributes info;
254 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000255 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
256 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
257 info.isFunction = false;
258 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000259 _symbols.push_back(info);
260 }
261}
262
Bill Wendlingfb440502012-03-28 20:46:54 +0000263/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000264void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
265 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000266 if (!c) return;
267
268 // second slot in __OBJC,__category is pointer to target class name
269 std::string targetclassName;
270 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
271 return;
272
273 NameAndAttributes info;
274 StringMap<NameAndAttributes>::value_type &entry =
275 _undefines.GetOrCreateValue(targetclassName);
276
277 if (entry.getValue().name)
278 return;
279
280 const char *symbolName = entry.getKey().data();
281 info.name = symbolName;
282 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000283 info.isFunction = false;
284 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000285 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000286}
287
Bill Wendlingfb440502012-03-28 20:46:54 +0000288/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000289void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000290 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000291 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
292 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000293
Bill Wendling5f689e72011-11-04 18:48:00 +0000294 NameAndAttributes info;
295 StringMap<NameAndAttributes>::value_type &entry =
296 _undefines.GetOrCreateValue(targetclassName);
297 if (entry.getValue().name)
298 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000299
Bill Wendling5f689e72011-11-04 18:48:00 +0000300 const char *symbolName = entry.getKey().data();
301 info.name = symbolName;
302 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000303 info.isFunction = false;
304 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000305 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000306}
307
Bill Wendlingfb440502012-03-28 20:46:54 +0000308/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000309void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000310 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000311 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000312
Bill Wendling45f74e32012-08-06 22:52:45 +0000313 if (!v->hasSection() /* || !isTargetDarwin */)
314 return;
315
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000316 // Special case i386/ppc ObjC data structures in magic sections:
317 // The issue is that the old ObjC object format did some strange
318 // contortions to avoid real linker symbols. For instance, the
319 // ObjC class data structure is allocated statically in the executable
320 // that defines that class. That data structures contains a pointer to
321 // its superclass. But instead of just initializing that part of the
322 // struct to the address of its superclass, and letting the static and
323 // dynamic linkers do the rest, the runtime works by having that field
324 // instead point to a C-string that is the name of the superclass.
325 // At runtime the objc initialization updates that pointer and sets
326 // it to point to the actual super class. As far as the linker
327 // knows it is just a pointer to a string. But then someone wanted the
328 // linker to issue errors at build time if the superclass was not found.
329 // So they figured out a way in mach-o object format to use an absolute
330 // symbols (.objc_class_name_Foo = 0) and a floating reference
331 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
332 // a class was missing.
333 // The following synthesizes the implicit .objc_* symbols for the linker
334 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000335
Bill Wendling45f74e32012-08-06 22:52:45 +0000336 // special case if this data blob is an ObjC class definition
337 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000338 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000339 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000340 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000341 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000342
Bill Wendling45f74e32012-08-06 22:52:45 +0000343 // special case if this data blob is an ObjC category definition
344 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000345 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000346 addObjCCategory(gv);
347 }
348 }
349
350 // special case if this data blob is the list of referenced classes
351 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000352 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000353 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000354 }
355 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000356}
357
Bill Wendlingfb440502012-03-28 20:46:54 +0000358/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000359void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000360 // add to list of defined symbols
361 addDefinedSymbol(f, true);
362}
363
Rafael Espindola282a4702013-10-31 20:51:58 +0000364static bool canBeHidden(const GlobalValue *GV) {
Rafael Espindola66f273b2014-02-07 19:04:43 +0000365 // FIXME: this is duplicated with another static function in AsmPrinter.cpp
Rafael Espindola282a4702013-10-31 20:51:58 +0000366 GlobalValue::LinkageTypes L = GV->getLinkage();
367
Rafael Espindola282a4702013-10-31 20:51:58 +0000368 if (L != GlobalValue::LinkOnceODRLinkage)
369 return false;
370
371 if (GV->hasUnnamedAddr())
372 return true;
373
Rafael Espindola66f273b2014-02-07 19:04:43 +0000374 // If it is a non constant variable, it needs to be uniqued across shared
375 // objects.
376 if (const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV)) {
377 if (!Var->isConstant())
378 return false;
379 }
380
Rafael Espindola282a4702013-10-31 20:51:58 +0000381 GlobalStatus GS;
382 if (GlobalStatus::analyzeGlobal(GV, GS))
383 return false;
384
385 return !GS.IsCompared;
386}
387
Bill Wendlingfb440502012-03-28 20:46:54 +0000388/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000389void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000390 // ignore all llvm.* symbols
391 if (def->getName().startswith("llvm."))
392 return;
393
394 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000395 SmallString<64> Buffer;
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000396 _target->getNameWithPrefix(Buffer, def, _mangler);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000397
398 // set alignment part log2() can have rounding errors
399 uint32_t align = def->getAlignment();
Rafael Espindola94751172014-05-05 14:18:16 +0000400 uint32_t attr = align ? countTrailingZeros(align) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000401
402 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000403 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000404 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000405 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000406 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000407 if (gv && gv->isConstant())
408 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
409 else
410 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
411 }
412
413 // set definition part
Rafael Espindola2fb5bc32014-03-13 23:18:37 +0000414 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000415 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000416 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000417 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000418 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000419 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000420
421 // set scope part
Duncan P. N. Exon Smith87121f82014-05-07 22:53:14 +0000422 if (def->hasLocalLinkage())
423 // Ignore visibility if linkage is local.
424 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
425 else if (def->hasHiddenVisibility())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000426 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
427 else if (def->hasProtectedVisibility())
428 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Rafael Espindola282a4702013-10-31 20:51:58 +0000429 else if (canBeHidden(def))
430 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000431 else
Duncan P. N. Exon Smith87121f82014-05-07 22:53:14 +0000432 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000433
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
Craig Topper2617dcc2014-04-15 06:32:26 +0000464 if (info.symbol == nullptr) {
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;
Craig Topper2617dcc2014-04-15 06:32:26 +0000478 info.symbol = nullptr;
Bill Wendling71b19bb2012-04-02 10:01:21 +0000479
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
Alp Toker98444342014-04-19 23:56:35 +0000506 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000507 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;
Craig Topper2617dcc2014-04-15 06:32:26 +0000512 info.symbol = nullptr;
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
Craig Topperb51ff602014-03-08 07:51:20 +0000647 void EmitInstruction(const MCInst &Inst,
648 const MCSubtargetInfo &STI) override {
Bill Wendling32867652012-04-03 03:56:52 +0000649 // Scan for values.
650 for (unsigned i = Inst.getNumOperands(); i--; )
651 if (Inst.getOperand(i).isExpr())
652 AddValueSymbols(Inst.getOperand(i).getExpr());
653 }
Craig Topperb51ff602014-03-08 07:51:20 +0000654 void EmitLabel(MCSymbol *Symbol) override {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000655 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000656 markDefined(*Symbol);
657 }
Craig Topperb51ff602014-03-08 07:51:20 +0000658 void EmitDebugLabel(MCSymbol *Symbol) override {
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000659 EmitLabel(Symbol);
660 }
Craig Topperb51ff602014-03-08 07:51:20 +0000661 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000662 // FIXME: should we handle aliases?
663 markDefined(*Symbol);
Tom Roedered0e88c2014-03-31 16:59:13 +0000664 AddValueSymbols(Value);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000665 }
Craig Topperb51ff602014-03-08 07:51:20 +0000666 bool EmitSymbolAttribute(MCSymbol *Symbol,
667 MCSymbolAttr Attribute) override {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000668 if (Attribute == MCSA_Global)
669 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000670 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000671 }
Craig Topperb51ff602014-03-08 07:51:20 +0000672 void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
673 uint64_t Size , unsigned ByteAlignment) override {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000674 markDefined(*Symbol);
675 }
Craig Topperb51ff602014-03-08 07:51:20 +0000676 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
677 unsigned ByteAlignment) override {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000678 markDefined(*Symbol);
679 }
Bill Wendling32867652012-04-03 03:56:52 +0000680
Craig Topperb51ff602014-03-08 07:51:20 +0000681 void EmitBundleAlignMode(unsigned AlignPow2) override {}
682 void EmitBundleLock(bool AlignToEnd) override {}
683 void EmitBundleUnlock() override {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000684
Bill Wendling32867652012-04-03 03:56:52 +0000685 // Noop calls.
Craig Topperb51ff602014-03-08 07:51:20 +0000686 void ChangeSection(const MCSection *Section,
687 const MCExpr *Subsection) override {}
688 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {}
689 void EmitThumbFunc(MCSymbol *Func) override {}
690 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override {}
691 void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override {}
692 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
693 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
694 void EmitCOFFSymbolType(int Type) override {}
695 void EndCOFFSymbolDef() override {}
696 void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override {}
697 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
698 unsigned ByteAlignment) override {}
699 void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
700 uint64_t Size, unsigned ByteAlignment) override {}
701 void EmitBytes(StringRef Data) override {}
Kevin Enderby96918bc2014-04-22 17:27:29 +0000702 void EmitValueImpl(const MCExpr *Value, unsigned Size,
703 const SMLoc &Loc) override {}
Craig Topperb51ff602014-03-08 07:51:20 +0000704 void EmitULEB128Value(const MCExpr *Value) override {}
705 void EmitSLEB128Value(const MCExpr *Value) override {}
706 void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
707 unsigned ValueSize,
708 unsigned MaxBytesToEmit) override {}
709 void EmitCodeAlignment(unsigned ByteAlignment,
710 unsigned MaxBytesToEmit) override {}
711 bool EmitValueToOffset(const MCExpr *Offset,
712 unsigned char Value) override { return false; }
713 void EmitFileDirective(StringRef Filename) override {}
Craig Topperb51ff602014-03-08 07:51:20 +0000714 void FinishImpl() override {}
715 void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000716 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
Ahmed Charles56440fd2014-03-06 05:51:42 +0000728 std::unique_ptr<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());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000732 std::unique_ptr<MCAsmParser> Parser(
733 createMCAsmParser(SrcMgr, _context, *Streamer, *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000734 const Target &T = _target->getTarget();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000735 std::unique_ptr<MCInstrInfo> MCII(T.createMCInstrInfo());
736 std::unique_ptr<MCSubtargetInfo> STI(T.createMCSubtargetInfo(
737 _target->getTargetTriple(), _target->getTargetCPU(),
738 _target->getTargetFeatureString()));
739 std::unique_ptr<MCTargetAsmParser> TAP(
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000740 T.createMCAsmParser(*STI, *Parser.get(), *MCII,
741 _target->Options.MCOptions));
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
Rafael Espindola6314ad42014-05-23 15:18:06 +0000804 for (const auto &Alias : _module->aliases())
805 addDefinedDataSymbol(&Alias);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000806
Daniel Dunbar919660b2010-08-10 23:46:46 +0000807 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000808 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
809 e = _undefines.end(); u != e; ++u) {
810 // If this symbol also has a definition, then don't make an undefine because
811 // it is a tentative definition.
812 if (_defines.count(u->getKey())) continue;
813 NameAndAttributes info = u->getValue();
814 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000815 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000816
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000817 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000818}
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000819
820/// parseMetadata - Parse metadata from the module
821void LTOModule::parseMetadata() {
822 // Linker Options
823 if (Value *Val = _module->getModuleFlag("Linker Options")) {
824 MDNode *LinkerOptions = cast<MDNode>(Val);
825 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
826 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
827 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
828 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
829 StringRef Op = _linkeropt_strings.
830 GetOrCreateValue(MDOption->getString()).getKey();
831 StringRef DepLibName = _target->getTargetLowering()->
832 getObjFileLowering().getDepLibFromLinkerOpt(Op);
833 if (!DepLibName.empty())
834 _deplibs.push_back(DepLibName.data());
835 else if (!Op.empty())
836 _linkeropts.push_back(Op.data());
837 }
838 }
839 }
840
841 // Add other interesting metadata here.
842}