blob: 7d3446ebc177e2159818eb9f0cbcd15a1fc1f086 [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"
Evan Cheng91111d22011-07-09 05:47:46 +000027#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000028#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000029#include "llvm/MC/MCTargetAsmParser.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000030#include "llvm/MC/SubtargetFeature.h"
Rafael Espindola13b69d62014-07-03 18:59:23 +000031#include "llvm/Object/RecordStreamer.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"
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +000040#include "llvm/Target/TargetLowering.h"
41#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000042#include "llvm/Target/TargetRegisterInfo.h"
Rafael Espindola282a4702013-10-31 20:51:58 +000043#include "llvm/Transforms/Utils/GlobalStatus.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000044#include <system_error>
Nick Kledzik07b4a622008-02-26 20:26:43 +000045using namespace llvm;
46
Rafael Espindola2f0647c2014-07-03 22:43:03 +000047LTOModule::LTOModule(std::unique_ptr<Module> M, TargetMachine *TM)
48 : _module(std::move(M)), _target(TM),
49 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(),
50 &ObjFileInfo),
51 _mangler(TM->getDataLayout()) {
52 ObjFileInfo.InitMCObjectFileInfo(TM->getTargetTriple(),
53 TM->getRelocationModel(), TM->getCodeModel(),
Rafael Espindolae28610d2013-12-09 20:26:40 +000054 _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) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000083 std::unique_ptr<MemoryBuffer> buffer;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000084 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000085 return false;
Ahmed Charles96c9d952014-03-05 10:19:29 +000086 return isTargetMatch(buffer.release(), 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
Peter Collingbourne63086fe2014-07-03 23:28:00 +000097LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options,
98 std::string &errMsg) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000099 std::unique_ptr<MemoryBuffer> buffer;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000100 if (std::error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000101 errMsg = ec.message();
Craig Topper2617dcc2014-04-15 06:32:26 +0000102 return nullptr;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000103 }
Rafael Espindola2f0647c2014-07-03 22:43:03 +0000104 return makeLTOModule(std::move(buffer), options, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000105}
106
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000107LTOModule *LTOModule::createFromOpenFile(int fd, const char *path, size_t size,
108 TargetOptions options,
109 std::string &errMsg) {
110 return createFromOpenFileSlice(fd, path, size, 0, options, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000111}
112
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000113LTOModule *LTOModule::createFromOpenFileSlice(int fd, const char *path,
114 size_t map_size, off_t offset,
115 TargetOptions options,
116 std::string &errMsg) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000117 std::unique_ptr<MemoryBuffer> buffer;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000118 if (std::error_code ec =
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000119 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000120 errMsg = ec.message();
Craig Topper2617dcc2014-04-15 06:32:26 +0000121 return nullptr;
Rafael Espindola56e41f72011-02-08 22:40:47 +0000122 }
Rafael Espindola2f0647c2014-07-03 22:43:03 +0000123 return makeLTOModule(std::move(buffer), options, errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000124}
125
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000126LTOModule *LTOModule::createFromBuffer(const void *mem, size_t length,
127 TargetOptions options,
128 std::string &errMsg, StringRef path) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000129 std::unique_ptr<MemoryBuffer> buffer(makeBuffer(mem, length, path));
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000130 if (!buffer)
Craig Topper2617dcc2014-04-15 06:32:26 +0000131 return nullptr;
Rafael Espindola2f0647c2014-07-03 22:43:03 +0000132 return makeLTOModule(std::move(buffer), options, errMsg);
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000133}
134
Rafael Espindola2f0647c2014-07-03 22:43:03 +0000135LTOModule *LTOModule::makeLTOModule(std::unique_ptr<MemoryBuffer> Buffer,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000136 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000137 std::string &errMsg) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000138 // parse bitcode buffer
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000139 ErrorOr<Module *> ModuleOrErr =
Rafael Espindola2f0647c2014-07-03 22:43:03 +0000140 getLazyBitcodeModule(Buffer.get(), getGlobalContext());
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000141 if (std::error_code EC = ModuleOrErr.getError()) {
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000142 errMsg = EC.message();
Craig Topper2617dcc2014-04-15 06:32:26 +0000143 return nullptr;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000144 }
Rafael Espindola2f0647c2014-07-03 22:43:03 +0000145 Buffer.release();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000146 std::unique_ptr<Module> m(ModuleOrErr.get());
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000147
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000148 std::string TripleStr = m->getTargetTriple();
149 if (TripleStr.empty())
150 TripleStr = sys::getDefaultTargetTriple();
151 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000152
153 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000154 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000155 if (!march)
Craig Topper2617dcc2014-04-15 06:32:26 +0000156 return nullptr;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000157
Nick Lewycky364c04a2011-04-21 01:54:08 +0000158 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000159 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000160 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000161 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000162 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000163 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000164 if (Triple.isOSDarwin()) {
165 if (Triple.getArch() == llvm::Triple::x86_64)
166 CPU = "core2";
167 else if (Triple.getArch() == llvm::Triple::x86)
168 CPU = "yonah";
Tim Northover3b0846e2014-05-24 12:50:23 +0000169 else if (Triple.getArch() == llvm::Triple::arm64 ||
170 Triple.getArch() == llvm::Triple::aarch64)
Tim Northover00ed9962014-03-29 10:18:08 +0000171 CPU = "cyclone";
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000172 }
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 Espindola2f0647c2014-07-03 22:43:03 +0000178 LTOModule *Ret = new LTOModule(std::move(m), 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;
Craig Topper2617dcc2014-04-15 06:32:26 +0000191 return nullptr;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000192 }
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
Rafael Espindola64c1e182014-06-03 02:41:57 +0000335 std::string Section = v->getSection();
336 if (Section.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
Rafael Espindola64c1e182014-06-03 02:41:57 +0000343 else if (Section.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
Rafael Espindola64c1e182014-06-03 02:41:57 +0000350 else if (Section.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
Duncan P. N. Exon Smith87121f82014-05-07 22:53:14 +0000421 if (def->hasLocalLinkage())
422 // Ignore visibility if linkage is local.
423 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
424 else if (def->hasHiddenVisibility())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000425 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
426 else if (def->hasProtectedVisibility())
427 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Rafael Espindola282a4702013-10-31 20:51:58 +0000428 else if (canBeHidden(def))
429 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000430 else
Duncan P. N. Exon Smith87121f82014-05-07 22:53:14 +0000431 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000432
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
Bill Wendlingfb440502012-03-28 20:46:54 +0000553/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
554/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000555bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000556 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000557 if (inlineAsm.empty())
558 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000559
Ahmed Charles56440fd2014-03-06 05:51:42 +0000560 std::unique_ptr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000561 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
562 SourceMgr SrcMgr;
563 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000564 std::unique_ptr<MCAsmParser> Parser(
565 createMCAsmParser(SrcMgr, _context, *Streamer, *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000566 const Target &T = _target->getTarget();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000567 std::unique_ptr<MCInstrInfo> MCII(T.createMCInstrInfo());
568 std::unique_ptr<MCSubtargetInfo> STI(T.createMCSubtargetInfo(
569 _target->getTargetTriple(), _target->getTargetCPU(),
570 _target->getTargetFeatureString()));
571 std::unique_ptr<MCTargetAsmParser> TAP(
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000572 T.createMCAsmParser(*STI, *Parser.get(), *MCII,
573 _target->Options.MCOptions));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000574 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000575 errMsg = "target " + std::string(T.getName()) +
576 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000577 return true;
578 }
579
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000580 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000581 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000582 return true;
583
Rafael Espindola05cef842014-06-28 18:44:59 +0000584 for (auto &KV : *Streamer) {
585 StringRef Key = KV.first();
586 RecordStreamer::State Value = KV.second;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000587 if (Value == RecordStreamer::DefinedGlobal)
588 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
589 else if (Value == RecordStreamer::Defined)
590 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
591 else if (Value == RecordStreamer::Global ||
592 Value == RecordStreamer::Used)
593 addAsmGlobalSymbolUndef(Key.data());
594 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000595
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000596 return false;
597}
598
Bill Wendlingfb440502012-03-28 20:46:54 +0000599/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000600static bool isDeclaration(const GlobalValue &V) {
601 if (V.hasAvailableExternallyLinkage())
602 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000603
Rafael Espindola5b778b22011-03-18 19:51:00 +0000604 if (V.isMaterializable())
605 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000606
Rafael Espindola5b778b22011-03-18 19:51:00 +0000607 return V.isDeclaration();
608}
609
Bill Wendling7e58b382012-03-28 23:12:18 +0000610/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000611/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000612bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000613 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000614 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000615 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000616 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000617 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000618 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000619 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000620
Daniel Dunbar919660b2010-08-10 23:46:46 +0000621 // add data
622 for (Module::global_iterator v = _module->global_begin(),
623 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000624 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000625 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000626 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000627 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000628 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000629
Daniel Dunbar919660b2010-08-10 23:46:46 +0000630 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000631 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000632 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000633
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000634 // add aliases
Rafael Espindola6314ad42014-05-23 15:18:06 +0000635 for (const auto &Alias : _module->aliases())
636 addDefinedDataSymbol(&Alias);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000637
Daniel Dunbar919660b2010-08-10 23:46:46 +0000638 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000639 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
640 e = _undefines.end(); u != e; ++u) {
641 // If this symbol also has a definition, then don't make an undefine because
642 // it is a tentative definition.
643 if (_defines.count(u->getKey())) continue;
644 NameAndAttributes info = u->getValue();
645 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000646 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000647
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000648 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000649}
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000650
651/// parseMetadata - Parse metadata from the module
652void LTOModule::parseMetadata() {
653 // Linker Options
654 if (Value *Val = _module->getModuleFlag("Linker Options")) {
655 MDNode *LinkerOptions = cast<MDNode>(Val);
656 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
657 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
658 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
659 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
660 StringRef Op = _linkeropt_strings.
661 GetOrCreateValue(MDOption->getString()).getKey();
662 StringRef DepLibName = _target->getTargetLowering()->
663 getObjFileLowering().getDepLibFromLinkerOpt(Op);
664 if (!DepLibName.empty())
665 _deplibs.push_back(DepLibName.data());
666 else if (!Op.empty())
667 _linkeropts.push_back(Op.data());
668 }
669 }
670 }
671
672 // Add other interesting metadata here.
673}