blob: 0d40d49dbe394e3281164a1ef999d269c6933162 [file] [log] [blame]
Chris Lattner5ef31a02010-03-12 18:44:54 +00001//===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
Nick Kledzik77595fc2008-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 Dunbarb06913d2010-08-10 23:46:39 +00007//
Nick Kledzik77595fc2008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Daniel Dunbarb06913d2010-08-10 23:46:39 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik77595fc2008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Peter Collingbourneef2acb52016-07-14 21:21:16 +000015#include "llvm/LTO/legacy/LTOModule.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000016#include "llvm/ADT/Triple.h"
Teresa Johnsona5479192016-11-11 05:34:58 +000017#include "llvm/Bitcode/BitcodeReader.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000018#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/LLVMContext.h"
Peter Collingbourne8dc921e2017-03-31 04:46:50 +000021#include "llvm/IR/Mangler.h"
Yunzhong Gaoa747cf12014-01-21 18:31:27 +000022#include "llvm/IR/Metadata.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
Rafael Espindola38c4e532011-03-02 04:14:42 +000024#include "llvm/MC/MCExpr.h"
25#include "llvm/MC/MCInst.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000026#include "llvm/MC/MCParser/MCAsmParser.h"
Rafael Espindolaa73e9202014-01-22 22:11:14 +000027#include "llvm/MC/MCSection.h"
Evan Chengffc0e732011-07-09 05:47:46 +000028#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola38c4e532011-03-02 04:14:42 +000029#include "llvm/MC/MCSymbol.h"
Bill Wendling5ff4bc22012-03-30 23:26:06 +000030#include "llvm/MC/SubtargetFeature.h"
Peter Collingbourne394be6c2014-09-18 21:28:49 +000031#include "llvm/Object/IRObjectFile.h"
32#include "llvm/Object/ObjectFile.h"
Rafael Espindola6c21feb2013-06-11 18:05:26 +000033#include "llvm/Support/FileSystem.h"
Chandler Carruth974a4452014-01-07 11:48:04 +000034#include "llvm/Support/Host.h"
Bill Wendling5ff4bc22012-03-30 23:26:06 +000035#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/Path.h"
Bill Wendling5ff4bc22012-03-30 23:26:06 +000037#include "llvm/Support/SourceMgr.h"
Bill Wendling5ff4bc22012-03-30 23:26:06 +000038#include "llvm/Support/TargetRegistry.h"
39#include "llvm/Support/TargetSelect.h"
David Blaikiefe42bd52018-03-23 23:58:19 +000040#include "llvm/Target/TargetLoweringObjectFile.h"
Rafael Espindola7e667c52013-10-31 20:51:58 +000041#include "llvm/Transforms/Utils/GlobalStatus.h"
Rafael Espindolad5132f92014-06-12 17:38:55 +000042#include <system_error>
Nick Kledzik77595fc2008-02-26 20:26:43 +000043using namespace llvm;
Peter Collingbourne394be6c2014-09-18 21:28:49 +000044using namespace llvm::object;
Nick Kledzik77595fc2008-02-26 20:26:43 +000045
Peter Collingbourne06844412016-12-13 20:01:58 +000046LTOModule::LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
Rafael Espindola1c9687e2014-07-04 18:40:36 +000047 llvm::TargetMachine *TM)
Peter Collingbourne06844412016-12-13 20:01:58 +000048 : Mod(std::move(M)), MBRef(MBRef), _target(TM) {
49 SymTab.addModule(Mod.get());
50}
Bill Wendling62cf01e2012-03-28 20:46:54 +000051
Duncan P. N. Exon Smith18a16fd2014-11-11 23:08:05 +000052LTOModule::~LTOModule() {}
53
Bill Wendling62cf01e2012-03-28 20:46:54 +000054/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
55/// bitcode.
Peter Collingbourne394be6c2014-09-18 21:28:49 +000056bool LTOModule::isBitcodeFile(const void *Mem, size_t Length) {
Rafael Espindolaf45980e2017-10-11 18:07:18 +000057 Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
Peter Collingbourne394be6c2014-09-18 21:28:49 +000058 MemoryBufferRef(StringRef((const char *)Mem, Length), "<mem>"));
Nico Weber78b36292018-01-23 19:03:13 +000059 return !errorToBool(BCData.takeError());
Nick Kledzik77595fc2008-02-26 20:26:43 +000060}
61
Mehdi Aminic16b74e2016-10-07 19:05:14 +000062bool LTOModule::isBitcodeFile(StringRef Path) {
Peter Collingbourne394be6c2014-09-18 21:28:49 +000063 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
64 MemoryBuffer::getFile(Path);
65 if (!BufferOrErr)
Rafael Espindolac3907f32013-06-12 15:13:57 +000066 return false;
Peter Collingbourne394be6c2014-09-18 21:28:49 +000067
Rafael Espindolaf45980e2017-10-11 18:07:18 +000068 Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
Peter Collingbourne394be6c2014-09-18 21:28:49 +000069 BufferOrErr.get()->getMemBufferRef());
Nico Weber78b36292018-01-23 19:03:13 +000070 return !errorToBool(BCData.takeError());
Nick Kledzik77595fc2008-02-26 20:26:43 +000071}
72
Mehdi Amini73cf01b2016-03-09 01:37:22 +000073bool LTOModule::isThinLTO() {
Peter Collingbourne95d9f1b2017-06-15 17:26:13 +000074 Expected<BitcodeLTOInfo> Result = getBitcodeLTOInfo(MBRef);
Peter Collingbourne9b252f02016-11-11 19:50:24 +000075 if (!Result) {
Jonas Devlieghere686dfe32018-11-11 01:46:03 +000076 logAllUnhandledErrors(Result.takeError(), errs());
Peter Collingbourne9b252f02016-11-11 19:50:24 +000077 return false;
78 }
Peter Collingbourne95d9f1b2017-06-15 17:26:13 +000079 return Result->IsThinLTO;
Mehdi Amini73cf01b2016-03-09 01:37:22 +000080}
81
Peter Collingbourne394be6c2014-09-18 21:28:49 +000082bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer,
83 StringRef TriplePrefix) {
Rafael Espindolaf45980e2017-10-11 18:07:18 +000084 Expected<MemoryBufferRef> BCOrErr =
Peter Collingbourne394be6c2014-09-18 21:28:49 +000085 IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
Nico Weber78b36292018-01-23 19:03:13 +000086 if (errorToBool(BCOrErr.takeError()))
Peter Collingbourne394be6c2014-09-18 21:28:49 +000087 return false;
Duncan P. N. Exon Smith18a16fd2014-11-11 23:08:05 +000088 LLVMContext Context;
Peter Collingbourne9b252f02016-11-11 19:50:24 +000089 ErrorOr<std::string> TripleOrErr =
90 expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(*BCOrErr));
91 if (!TripleOrErr)
92 return false;
93 return StringRef(*TripleOrErr).startswith(TriplePrefix);
Daniel Dunbarb06913d2010-08-10 23:46:39 +000094}
95
Mehdi Aminidd77c442015-11-09 02:46:41 +000096std::string LTOModule::getProducerString(MemoryBuffer *Buffer) {
Rafael Espindolaf45980e2017-10-11 18:07:18 +000097 Expected<MemoryBufferRef> BCOrErr =
Mehdi Aminidd77c442015-11-09 02:46:41 +000098 IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
Nico Weber78b36292018-01-23 19:03:13 +000099 if (errorToBool(BCOrErr.takeError()))
Mehdi Aminidd77c442015-11-09 02:46:41 +0000100 return "";
101 LLVMContext Context;
Peter Collingbourne9b252f02016-11-11 19:50:24 +0000102 ErrorOr<std::string> ProducerOrErr = expectedToErrorOrAndEmitErrors(
103 Context, getBitcodeProducerString(*BCOrErr));
104 if (!ProducerOrErr)
105 return "";
106 return *ProducerOrErr;
Mehdi Aminidd77c442015-11-09 02:46:41 +0000107}
108
Rafael Espindola608d7b42015-12-04 16:14:31 +0000109ErrorOr<std::unique_ptr<LTOModule>>
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000110LTOModule::createFromFile(LLVMContext &Context, StringRef path,
Benjamin Kramer36538ff2016-06-08 19:09:22 +0000111 const TargetOptions &options) {
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000112 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
113 MemoryBuffer::getFile(path);
Petr Pavlu616e99d2016-01-20 09:03:42 +0000114 if (std::error_code EC = BufferOrErr.getError()) {
115 Context.emitError(EC.message());
Rafael Espindola608d7b42015-12-04 16:14:31 +0000116 return EC;
Petr Pavlu616e99d2016-01-20 09:03:42 +0000117 }
Rafael Espindola548f2b62014-08-19 18:44:46 +0000118 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
Petr Pavlu3a3e8dd2016-03-01 13:13:49 +0000119 return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
120 /* ShouldBeLazy*/ false);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000121}
122
Rafael Espindola608d7b42015-12-04 16:14:31 +0000123ErrorOr<std::unique_ptr<LTOModule>>
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000124LTOModule::createFromOpenFile(LLVMContext &Context, int fd, StringRef path,
Benjamin Kramer36538ff2016-06-08 19:09:22 +0000125 size_t size, const TargetOptions &options) {
Rafael Espindola608d7b42015-12-04 16:14:31 +0000126 return createFromOpenFileSlice(Context, fd, path, size, 0, options);
Rafael Espindolaf21b1052011-03-17 00:36:11 +0000127}
128
Rafael Espindola608d7b42015-12-04 16:14:31 +0000129ErrorOr<std::unique_ptr<LTOModule>>
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000130LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
131 size_t map_size, off_t offset,
132 const TargetOptions &options) {
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000133 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
134 MemoryBuffer::getOpenFileSlice(fd, path, map_size, offset);
Petr Pavlu616e99d2016-01-20 09:03:42 +0000135 if (std::error_code EC = BufferOrErr.getError()) {
136 Context.emitError(EC.message());
Rafael Espindola608d7b42015-12-04 16:14:31 +0000137 return EC;
Petr Pavlu616e99d2016-01-20 09:03:42 +0000138 }
Rafael Espindola548f2b62014-08-19 18:44:46 +0000139 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
Petr Pavlu3a3e8dd2016-03-01 13:13:49 +0000140 return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
141 /* ShouldBeLazy */ false);
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000142}
143
Rafael Espindola608d7b42015-12-04 16:14:31 +0000144ErrorOr<std::unique_ptr<LTOModule>>
145LTOModule::createFromBuffer(LLVMContext &Context, const void *mem,
Benjamin Kramer36538ff2016-06-08 19:09:22 +0000146 size_t length, const TargetOptions &options,
Rafael Espindola608d7b42015-12-04 16:14:31 +0000147 StringRef path) {
Aaron Ballmana2bec692014-08-20 12:54:13 +0000148 StringRef Data((const char *)mem, length);
Rafael Espindola548f2b62014-08-19 18:44:46 +0000149 MemoryBufferRef Buffer(Data, path);
Petr Pavlu3a3e8dd2016-03-01 13:13:49 +0000150 return makeLTOModule(Buffer, options, Context, /* ShouldBeLazy */ false);
151}
152
153ErrorOr<std::unique_ptr<LTOModule>>
154LTOModule::createInLocalContext(std::unique_ptr<LLVMContext> Context,
155 const void *mem, size_t length,
Benjamin Kramer36538ff2016-06-08 19:09:22 +0000156 const TargetOptions &options, StringRef path) {
Petr Pavlu3a3e8dd2016-03-01 13:13:49 +0000157 StringRef Data((const char *)mem, length);
158 MemoryBufferRef Buffer(Data, path);
159 // If we own a context, we know this is being used only for symbol extraction,
160 // not linking. Be lazy in that case.
161 ErrorOr<std::unique_ptr<LTOModule>> Ret =
162 makeLTOModule(Buffer, options, *Context, /* ShouldBeLazy */ true);
163 if (Ret)
164 (*Ret)->OwnedContext = std::move(Context);
David Blaikied93c6392016-03-01 20:41:17 +0000165 return Ret;
Bill Wendling9ac0aaa2012-08-06 21:34:54 +0000166}
167
Rafael Espindola608d7b42015-12-04 16:14:31 +0000168static ErrorOr<std::unique_ptr<Module>>
169parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context,
170 bool ShouldBeLazy) {
Duncan P. N. Exon Smith657f8c82014-12-17 22:05:42 +0000171 // Find the buffer.
Rafael Espindolaf45980e2017-10-11 18:07:18 +0000172 Expected<MemoryBufferRef> MBOrErr =
Duncan P. N. Exon Smith657f8c82014-12-17 22:05:42 +0000173 IRObjectFile::findBitcodeInMemBuffer(Buffer);
Rafael Espindolaf45980e2017-10-11 18:07:18 +0000174 if (Error E = MBOrErr.takeError()) {
175 std::error_code EC = errorToErrorCode(std::move(E));
Petr Pavlu616e99d2016-01-20 09:03:42 +0000176 Context.emitError(EC.message());
Rafael Espindola608d7b42015-12-04 16:14:31 +0000177 return EC;
Petr Pavlu616e99d2016-01-20 09:03:42 +0000178 }
Rafael Espindola68016e02015-01-10 00:07:30 +0000179
180 if (!ShouldBeLazy) {
Duncan P. N. Exon Smith657f8c82014-12-17 22:05:42 +0000181 // Parse the full file.
Peter Collingbournedead0812016-11-13 07:00:17 +0000182 return expectedToErrorOrAndEmitErrors(Context,
183 parseBitcodeFile(*MBOrErr, Context));
Rafael Espindola68016e02015-01-10 00:07:30 +0000184 }
Duncan P. N. Exon Smith657f8c82014-12-17 22:05:42 +0000185
186 // Parse lazily.
Peter Collingbournedead0812016-11-13 07:00:17 +0000187 return expectedToErrorOrAndEmitErrors(
188 Context,
189 getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/));
Duncan P. N. Exon Smith657f8c82014-12-17 22:05:42 +0000190}
191
Rafael Espindola608d7b42015-12-04 16:14:31 +0000192ErrorOr<std::unique_ptr<LTOModule>>
Benjamin Kramer36538ff2016-06-08 19:09:22 +0000193LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
Petr Pavlu3a3e8dd2016-03-01 13:13:49 +0000194 LLVMContext &Context, bool ShouldBeLazy) {
Rafael Espindola608d7b42015-12-04 16:14:31 +0000195 ErrorOr<std::unique_ptr<Module>> MOrErr =
Petr Pavlu3a3e8dd2016-03-01 13:13:49 +0000196 parseBitcodeFileImpl(Buffer, Context, ShouldBeLazy);
Rafael Espindola608d7b42015-12-04 16:14:31 +0000197 if (std::error_code EC = MOrErr.getError())
198 return EC;
199 std::unique_ptr<Module> &M = *MOrErr;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000200
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000201 std::string TripleStr = M->getTargetTriple();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000202 if (TripleStr.empty())
203 TripleStr = sys::getDefaultTargetTriple();
204 llvm::Triple Triple(TripleStr);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000205
206 // find machine architecture for this module
Rafael Espindola608d7b42015-12-04 16:14:31 +0000207 std::string errMsg;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000208 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000209 if (!march)
Adam Nemetb0d20582018-03-13 04:37:01 +0000210 return make_error_code(object::object_error::arch_not_found);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000211
Nick Lewycky333ed452011-04-21 01:54:08 +0000212 // construct LTOModule, hand over ownership of module and target
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000213 SubtargetFeatures Features;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000214 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000215 std::string FeatureStr = Features.getString();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000216 // Set a default CPU for Darwin triples.
Evan Cheng276365d2011-06-30 01:53:36 +0000217 std::string CPU;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000218 if (Triple.isOSDarwin()) {
219 if (Triple.getArch() == llvm::Triple::x86_64)
220 CPU = "core2";
221 else if (Triple.getArch() == llvm::Triple::x86)
222 CPU = "yonah";
Tim Northover92311482014-07-23 12:32:47 +0000223 else if (Triple.getArch() == llvm::Triple::aarch64)
Tim Northover7b837d82014-03-29 10:18:08 +0000224 CPU = "cyclone";
Bob Wilson47ed8a12012-10-12 17:39:25 +0000225 }
Rafael Espindolac13c9e52013-09-30 16:39:19 +0000226
Rafael Espindolaac8db592016-05-18 22:04:49 +0000227 TargetMachine *target =
228 march->createTargetMachine(TripleStr, CPU, FeatureStr, options, None);
Rafael Espindola7e667c52013-10-31 20:51:58 +0000229
Peter Collingbourne06844412016-12-13 20:01:58 +0000230 std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(M), Buffer, target));
Rafael Espindola34254062015-12-03 23:56:42 +0000231 Ret->parseSymbols();
Yunzhong Gaoa747cf12014-01-21 18:31:27 +0000232 Ret->parseMetadata();
233
Rafael Espindola608d7b42015-12-04 16:14:31 +0000234 return std::move(Ret);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000235}
236
Manman Renf3fc8c92014-02-10 23:26:14 +0000237/// Create a MemoryBuffer from a memory range with an optional name.
Rafael Espindola2b861c32014-08-17 22:37:39 +0000238std::unique_ptr<MemoryBuffer>
239LTOModule::makeBuffer(const void *mem, size_t length, StringRef name) {
Roman Divacky59324292012-09-05 22:26:57 +0000240 const char *startPtr = (const char*)mem;
Rafael Espindola1a7f7052014-08-27 20:03:13 +0000241 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000242}
243
Bill Wendling62cf01e2012-03-28 20:46:54 +0000244/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola613abf32012-12-11 03:10:43 +0000245bool
246LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
247 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000248 Constant *op = ce->getOperand(0);
249 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
250 Constant *cn = gvn->getInitializer();
Chris Lattner18c7f802012-02-05 02:29:43 +0000251 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000252 if (ca->isCString()) {
Yaron Keren6e92e7b2015-03-30 15:42:36 +0000253 name = (".objc_class_name_" + ca->getAsCString()).str();
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000254 return true;
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000255 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000256 }
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000257 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000258 }
259 return false;
260}
261
Bill Wendling62cf01e2012-03-28 20:46:54 +0000262/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola613abf32012-12-11 03:10:43 +0000263void LTOModule::addObjCClass(const GlobalVariable *clgv) {
264 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling931d4c22011-11-04 18:48:00 +0000265 if (!c) return;
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000266
Bill Wendling931d4c22011-11-04 18:48:00 +0000267 // second slot in __OBJC,__class is pointer to superclass name
268 std::string superclassName;
269 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
David Blaikie1d4f28c2014-11-19 05:49:42 +0000270 auto IterBool =
271 _undefines.insert(std::make_pair(superclassName, NameAndAttributes()));
272 if (IterBool.second) {
273 NameAndAttributes &info = IterBool.first->second;
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000274 info.name = IterBool.first->first();
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000275 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling24b87802012-03-29 08:27:32 +0000276 info.isFunction = false;
277 info.symbol = clgv;
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000278 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000279 }
Bill Wendling931d4c22011-11-04 18:48:00 +0000280
281 // third slot in __OBJC,__class is pointer to class name
282 std::string className;
283 if (objcClassNameFromExpression(c->getOperand(2), className)) {
David Blaikie1d4f28c2014-11-19 05:49:42 +0000284 auto Iter = _defines.insert(className).first;
Bill Wendling24b87802012-03-29 08:27:32 +0000285
Bill Wendling931d4c22011-11-04 18:48:00 +0000286 NameAndAttributes info;
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000287 info.name = Iter->first();
Bill Wendling24b87802012-03-29 08:27:32 +0000288 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
289 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
290 info.isFunction = false;
291 info.symbol = clgv;
Bill Wendling931d4c22011-11-04 18:48:00 +0000292 _symbols.push_back(info);
293 }
294}
295
Bill Wendling62cf01e2012-03-28 20:46:54 +0000296/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola613abf32012-12-11 03:10:43 +0000297void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
298 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling931d4c22011-11-04 18:48:00 +0000299 if (!c) return;
300
301 // second slot in __OBJC,__category is pointer to target class name
302 std::string targetclassName;
303 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
304 return;
305
David Blaikie1d4f28c2014-11-19 05:49:42 +0000306 auto IterBool =
307 _undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));
Bill Wendling931d4c22011-11-04 18:48:00 +0000308
David Blaikie1d4f28c2014-11-19 05:49:42 +0000309 if (!IterBool.second)
Bill Wendling931d4c22011-11-04 18:48:00 +0000310 return;
311
David Blaikie1d4f28c2014-11-19 05:49:42 +0000312 NameAndAttributes &info = IterBool.first->second;
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000313 info.name = IterBool.first->first();
Bill Wendling931d4c22011-11-04 18:48:00 +0000314 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling24b87802012-03-29 08:27:32 +0000315 info.isFunction = false;
316 info.symbol = clgv;
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000317}
318
Bill Wendling62cf01e2012-03-28 20:46:54 +0000319/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola613abf32012-12-11 03:10:43 +0000320void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000321 std::string targetclassName;
Bill Wendling931d4c22011-11-04 18:48:00 +0000322 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
323 return;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000324
David Blaikie1d4f28c2014-11-19 05:49:42 +0000325 auto IterBool =
326 _undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));
327
328 if (!IterBool.second)
Bill Wendling931d4c22011-11-04 18:48:00 +0000329 return;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000330
David Blaikie1d4f28c2014-11-19 05:49:42 +0000331 NameAndAttributes &info = IterBool.first->second;
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000332 info.name = IterBool.first->first();
Bill Wendling931d4c22011-11-04 18:48:00 +0000333 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling24b87802012-03-29 08:27:32 +0000334 info.isFunction = false;
335 info.symbol = clgv;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000336}
337
Peter Collingbourne06844412016-12-13 20:01:58 +0000338void LTOModule::addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym) {
Rafael Espindola2291f8c2014-07-04 16:37:02 +0000339 SmallString<64> Buffer;
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000340 {
341 raw_svector_ostream OS(Buffer);
Peter Collingbourne06844412016-12-13 20:01:58 +0000342 SymTab.printSymbolName(OS, Sym);
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000343 Buffer.c_str();
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000344 }
345
Peter Collingbourne06844412016-12-13 20:01:58 +0000346 const GlobalValue *V = Sym.get<GlobalValue *>();
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000347 addDefinedDataSymbol(Buffer, V);
Rafael Espindola2291f8c2014-07-04 16:37:02 +0000348}
349
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000350void LTOModule::addDefinedDataSymbol(StringRef Name, const GlobalValue *v) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000351 // Add to list of defined symbols.
Rafael Espindola2291f8c2014-07-04 16:37:02 +0000352 addDefinedSymbol(Name, v, false);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000353
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000354 if (!v->hasSection() /* || !isTargetDarwin */)
355 return;
356
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000357 // Special case i386/ppc ObjC data structures in magic sections:
358 // The issue is that the old ObjC object format did some strange
359 // contortions to avoid real linker symbols. For instance, the
360 // ObjC class data structure is allocated statically in the executable
361 // that defines that class. That data structures contains a pointer to
362 // its superclass. But instead of just initializing that part of the
363 // struct to the address of its superclass, and letting the static and
364 // dynamic linkers do the rest, the runtime works by having that field
365 // instead point to a C-string that is the name of the superclass.
366 // At runtime the objc initialization updates that pointer and sets
367 // it to point to the actual super class. As far as the linker
368 // knows it is just a pointer to a string. But then someone wanted the
369 // linker to issue errors at build time if the superclass was not found.
370 // So they figured out a way in mach-o object format to use an absolute
371 // symbols (.objc_class_name_Foo = 0) and a floating reference
372 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
373 // a class was missing.
374 // The following synthesizes the implicit .objc_* symbols for the linker
375 // from the ObjC data structures generated by the front end.
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000376
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000377 // special case if this data blob is an ObjC class definition
Benjamin Kramer31342442017-12-28 18:31:19 +0000378 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(v)) {
379 StringRef Section = GV->getSection();
380 if (Section.startswith("__OBJC,__class,")) {
381 addObjCClass(GV);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000382 }
383
Benjamin Kramer31342442017-12-28 18:31:19 +0000384 // special case if this data blob is an ObjC category definition
385 else if (Section.startswith("__OBJC,__category,")) {
386 addObjCCategory(GV);
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000387 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000388
Benjamin Kramer31342442017-12-28 18:31:19 +0000389 // special case if this data blob is the list of referenced classes
390 else if (Section.startswith("__OBJC,__cls_refs,")) {
391 addObjCClassRef(GV);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000392 }
393 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000394}
395
Peter Collingbourne06844412016-12-13 20:01:58 +0000396void LTOModule::addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym) {
Rafael Espindola2291f8c2014-07-04 16:37:02 +0000397 SmallString<64> Buffer;
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000398 {
399 raw_svector_ostream OS(Buffer);
Peter Collingbourne06844412016-12-13 20:01:58 +0000400 SymTab.printSymbolName(OS, Sym);
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000401 Buffer.c_str();
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000402 }
403
Peter Collingbourne06844412016-12-13 20:01:58 +0000404 const Function *F = cast<Function>(Sym.get<GlobalValue *>());
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000405 addDefinedFunctionSymbol(Buffer, F);
Rafael Espindola2291f8c2014-07-04 16:37:02 +0000406}
407
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000408void LTOModule::addDefinedFunctionSymbol(StringRef Name, const Function *F) {
Bill Wendling62cf01e2012-03-28 20:46:54 +0000409 // add to list of defined symbols
Rafael Espindola2291f8c2014-07-04 16:37:02 +0000410 addDefinedSymbol(Name, F, true);
Bill Wendling62cf01e2012-03-28 20:46:54 +0000411}
412
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000413void LTOModule::addDefinedSymbol(StringRef Name, const GlobalValue *def,
Rafael Espindola2291f8c2014-07-04 16:37:02 +0000414 bool isFunction) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000415 // set alignment part log2() can have rounding errors
416 uint32_t align = def->getAlignment();
Rafael Espindola557a2752014-05-05 14:18:16 +0000417 uint32_t attr = align ? countTrailingZeros(align) : 0;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000418
419 // set permissions part
Bill Wendling24b87802012-03-29 08:27:32 +0000420 if (isFunction) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000421 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling24b87802012-03-29 08:27:32 +0000422 } else {
Rafael Espindola613abf32012-12-11 03:10:43 +0000423 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000424 if (gv && gv->isConstant())
425 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
426 else
427 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
428 }
429
430 // set definition part
Rafael Espindola1f21e0d2014-03-13 23:18:37 +0000431 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000432 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendling7afea0c2010-09-27 20:17:45 +0000433 else if (def->hasCommonLinkage())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000434 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendling7afea0c2010-09-27 20:17:45 +0000435 else
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000436 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000437
438 // set scope part
Duncan P. N. Exon Smith83533dd2014-05-07 22:53:14 +0000439 if (def->hasLocalLinkage())
440 // Ignore visibility if linkage is local.
441 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
442 else if (def->hasHiddenVisibility())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000443 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
444 else if (def->hasProtectedVisibility())
445 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
David Blaikied7ca9162018-03-21 19:23:45 +0000446 else if (def->canBeOmittedFromSymbolTable())
Rafael Espindola7e667c52013-10-31 20:51:58 +0000447 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000448 else
Duncan P. N. Exon Smith83533dd2014-05-07 22:53:14 +0000449 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000450
Peter Collingbourne6f69f402015-06-11 21:41:27 +0000451 if (def->hasComdat())
452 attr |= LTO_SYMBOL_COMDAT;
453
Peter Collingbournedfe47862015-07-04 03:42:35 +0000454 if (isa<GlobalAlias>(def))
455 attr |= LTO_SYMBOL_ALIAS;
456
David Blaikie1d4f28c2014-11-19 05:49:42 +0000457 auto Iter = _defines.insert(Name).first;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000458
Bill Wendling24b87802012-03-29 08:27:32 +0000459 // fill information structure
460 NameAndAttributes info;
David Blaikie1d4f28c2014-11-19 05:49:42 +0000461 StringRef NameRef = Iter->first();
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000462 info.name = NameRef;
463 assert(NameRef.data()[NameRef.size()] == '\0');
Bill Wendling24b87802012-03-29 08:27:32 +0000464 info.attributes = attr;
465 info.isFunction = isFunction;
466 info.symbol = def;
467
468 // add to table of symbols
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000469 _symbols.push_back(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000470}
471
Bill Wendling62cf01e2012-03-28 20:46:54 +0000472/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
473/// defined list.
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000474void LTOModule::addAsmGlobalSymbol(StringRef name,
Rafael Espindola38c4e532011-03-02 04:14:42 +0000475 lto_symbol_attributes scope) {
David Blaikie1d4f28c2014-11-19 05:49:42 +0000476 auto IterBool = _defines.insert(name);
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000477
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000478 // only add new define if not already defined
David Blaikie1d4f28c2014-11-19 05:49:42 +0000479 if (!IterBool.second)
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000480 return;
481
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000482 NameAndAttributes &info = _undefines[IterBool.first->first()];
Bill Wendling24b87802012-03-29 08:27:32 +0000483
Craig Topper0b6cb712014-04-15 06:32:26 +0000484 if (info.symbol == nullptr) {
Bill Wendling8ba94052012-04-02 10:01:21 +0000485 // FIXME: This is trying to take care of module ASM like this:
486 //
487 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
488 //
489 // but is gross and its mother dresses it funny. Have the ASM parser give us
490 // more details for this type of situation so that we're not guessing so
491 // much.
492
493 // fill information structure
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000494 info.name = IterBool.first->first();
Bill Wendling8ba94052012-04-02 10:01:21 +0000495 info.attributes =
496 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
497 info.isFunction = false;
Craig Topper0b6cb712014-04-15 06:32:26 +0000498 info.symbol = nullptr;
Bill Wendling8ba94052012-04-02 10:01:21 +0000499
500 // add to table of symbols
501 _symbols.push_back(info);
Bill Wendling1fcbca02012-04-02 03:33:31 +0000502 return;
503 }
504
Bill Wendling24b87802012-03-29 08:27:32 +0000505 if (info.isFunction)
Rafael Espindola2291f8c2014-07-04 16:37:02 +0000506 addDefinedFunctionSymbol(info.name, cast<Function>(info.symbol));
Bill Wendling24b87802012-03-29 08:27:32 +0000507 else
Rafael Espindola2291f8c2014-07-04 16:37:02 +0000508 addDefinedDataSymbol(info.name, info.symbol);
Bill Wendling5ff4bc22012-03-30 23:26:06 +0000509
510 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
511 _symbols.back().attributes |= scope;
Devang Patelc2aec572008-07-16 18:06:52 +0000512}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000513
Bill Wendling62cf01e2012-03-28 20:46:54 +0000514/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
515/// undefined list.
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000516void LTOModule::addAsmGlobalSymbolUndef(StringRef name) {
David Blaikie1d4f28c2014-11-19 05:49:42 +0000517 auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
Rafael Espindola38c4e532011-03-02 04:14:42 +0000518
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000519 _asm_undefines.push_back(IterBool.first->first());
Rafael Espindola38c4e532011-03-02 04:14:42 +0000520
521 // we already have the symbol
David Blaikie1d4f28c2014-11-19 05:49:42 +0000522 if (!IterBool.second)
Rafael Espindola38c4e532011-03-02 04:14:42 +0000523 return;
524
Alp Toker8f5a7de2014-04-19 23:56:35 +0000525 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola38c4e532011-03-02 04:14:42 +0000526 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
David Blaikie1d4f28c2014-11-19 05:49:42 +0000527 NameAndAttributes &info = IterBool.first->second;
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000528 info.name = IterBool.first->first();
Bill Wendling24b87802012-03-29 08:27:32 +0000529 info.attributes = attr;
530 info.isFunction = false;
Craig Topper0b6cb712014-04-15 06:32:26 +0000531 info.symbol = nullptr;
Rafael Espindola38c4e532011-03-02 04:14:42 +0000532}
533
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000534/// Add a symbol which isn't defined just yet to a list to be resolved later.
Peter Collingbourne06844412016-12-13 20:01:58 +0000535void LTOModule::addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000536 bool isFunc) {
Rafael Espindolaef1860a2011-02-11 05:23:09 +0000537 SmallString<64> name;
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000538 {
539 raw_svector_ostream OS(name);
Peter Collingbourne06844412016-12-13 20:01:58 +0000540 SymTab.printSymbolName(OS, Sym);
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000541 name.c_str();
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000542 }
Rafael Espindola7431af02009-04-24 16:55:21 +0000543
David Blaikie1d4f28c2014-11-19 05:49:42 +0000544 auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000545
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000546 // we already have the symbol
David Blaikie1d4f28c2014-11-19 05:49:42 +0000547 if (!IterBool.second)
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000548 return;
Rafael Espindola7431af02009-04-24 16:55:21 +0000549
David Blaikie1d4f28c2014-11-19 05:49:42 +0000550 NameAndAttributes &info = IterBool.first->second;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000551
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000552 info.name = IterBool.first->first();
Bill Wendling62cf01e2012-03-28 20:46:54 +0000553
Peter Collingbourne06844412016-12-13 20:01:58 +0000554 const GlobalValue *decl = Sym.dyn_cast<GlobalValue *>();
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000555
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000556 if (decl->hasExternalWeakLinkage())
557 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
558 else
559 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000560
Bill Wendling24b87802012-03-29 08:27:32 +0000561 info.isFunction = isFunc;
562 info.symbol = decl;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000563}
564
Rafael Espindola34254062015-12-03 23:56:42 +0000565void LTOModule::parseSymbols() {
Peter Collingbourne06844412016-12-13 20:01:58 +0000566 for (auto Sym : SymTab.symbols()) {
567 auto *GV = Sym.dyn_cast<GlobalValue *>();
568 uint32_t Flags = SymTab.getSymbolFlags(Sym);
Rafael Espindolae97c25b2014-07-04 19:31:27 +0000569 if (Flags & object::BasicSymbolRef::SF_FormatSpecific)
570 continue;
571
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000572 bool IsUndefined = Flags & object::BasicSymbolRef::SF_Undefined;
573
574 if (!GV) {
575 SmallString<64> Buffer;
576 {
577 raw_svector_ostream OS(Buffer);
Peter Collingbourne06844412016-12-13 20:01:58 +0000578 SymTab.printSymbolName(OS, Sym);
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000579 Buffer.c_str();
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000580 }
Mehdi Aminic16b74e2016-10-07 19:05:14 +0000581 StringRef Name(Buffer);
Rafael Espindola1c9687e2014-07-04 18:40:36 +0000582
583 if (IsUndefined)
584 addAsmGlobalSymbolUndef(Name);
585 else if (Flags & object::BasicSymbolRef::SF_Global)
586 addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_DEFAULT);
587 else
588 addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_INTERNAL);
589 continue;
590 }
591
592 auto *F = dyn_cast<Function>(GV);
593 if (IsUndefined) {
594 addPotentialUndefinedSymbol(Sym, F != nullptr);
595 continue;
596 }
597
598 if (F) {
599 addDefinedFunctionSymbol(Sym);
600 continue;
601 }
602
603 if (isa<GlobalVariable>(GV)) {
604 addDefinedDataSymbol(Sym);
605 continue;
606 }
607
608 assert(isa<GlobalAlias>(GV));
609 addDefinedDataSymbol(Sym);
Daniel Dunbare41d9002010-08-10 23:46:46 +0000610 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000611
Daniel Dunbare41d9002010-08-10 23:46:46 +0000612 // make symbols for all undefines
Bill Wendling24b87802012-03-29 08:27:32 +0000613 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
614 e = _undefines.end(); u != e; ++u) {
615 // If this symbol also has a definition, then don't make an undefine because
616 // it is a tentative definition.
617 if (_defines.count(u->getKey())) continue;
618 NameAndAttributes info = u->getValue();
619 _symbols.push_back(info);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000620 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000621}
Yunzhong Gaoa747cf12014-01-21 18:31:27 +0000622
623/// parseMetadata - Parse metadata from the module
624void LTOModule::parseMetadata() {
Peter Collingbournea6367d92015-06-29 22:04:09 +0000625 raw_string_ostream OS(LinkerOpts);
626
Yunzhong Gaoa747cf12014-01-21 18:31:27 +0000627 // Linker Options
Peter Collingbourne9283a092017-06-12 20:10:48 +0000628 if (NamedMDNode *LinkerOptions =
629 getModule().getNamedMetadata("llvm.linker.options")) {
Yunzhong Gaoa747cf12014-01-21 18:31:27 +0000630 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
Peter Collingbourne9283a092017-06-12 20:10:48 +0000631 MDNode *MDOptions = LinkerOptions->getOperand(i);
Yunzhong Gaoa747cf12014-01-21 18:31:27 +0000632 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
633 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
Peter Collingbournea6367d92015-06-29 22:04:09 +0000634 OS << " " << MDOption->getString();
Yunzhong Gaoa747cf12014-01-21 18:31:27 +0000635 }
636 }
637 }
638
Bob Haarman447e81d2017-02-04 01:28:44 +0000639 // Globals - we only need to do this for COFF.
640 const Triple TT(_target->getTargetTriple());
641 if (!TT.isOSBinFormatCOFF())
642 return;
643 Mangler M;
Peter Collingbournea6367d92015-06-29 22:04:09 +0000644 for (const NameAndAttributes &Sym : _symbols) {
645 if (!Sym.symbol)
646 continue;
Bob Haarman447e81d2017-02-04 01:28:44 +0000647 emitLinkerFlagsForGlobalCOFF(OS, Sym.symbol, TT, M);
Peter Collingbournea6367d92015-06-29 22:04:09 +0000648 }
649
Yunzhong Gaoa747cf12014-01-21 18:31:27 +0000650 // Add other interesting metadata here.
651}