blob: 3bbf9469a895ccfc73653f50351ff581e7338436 [file] [log] [blame]
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001//===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===//
2//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements classes used to handle lowerings specific to common
11// object file formats.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/ADT/Triple.h"
19#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
21#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/GlobalVariable.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000025#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Module.h"
Peter Collingbourne94d77862015-11-03 23:40:03 +000027#include "llvm/MC/MCAsmInfo.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000028#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCExpr.h"
Chris Lattner87cffa92010-05-07 17:17:41 +000030#include "llvm/MC/MCSectionCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/MC/MCSectionELF.h"
32#include "llvm/MC/MCSectionMachO.h"
Rafael Espindolaa83b1772011-04-16 03:51:21 +000033#include "llvm/MC/MCStreamer.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000034#include "llvm/MC/MCSymbolELF.h"
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +000035#include "llvm/MC/MCValue.h"
Xinliang David Li5f04f922016-01-14 18:09:45 +000036#include "llvm/ProfileData/InstrProf.h"
Reid Kleckner1f13d472015-09-03 16:41:50 +000037#include "llvm/Support/COFF.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000038#include "llvm/Support/Dwarf.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000039#include "llvm/Support/ELF.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000040#include "llvm/Support/ErrorHandling.h"
41#include "llvm/Support/raw_ostream.h"
Rafael Espindoladaeafb42014-02-19 17:23:20 +000042#include "llvm/Target/TargetLowering.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000043#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000044#include "llvm/Target/TargetSubtargetInfo.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000045using namespace llvm;
Anton Korobeynikov31a92122010-02-21 20:28:15 +000046using namespace dwarf;
Anton Korobeynikovab663a02010-02-15 22:37:53 +000047
48//===----------------------------------------------------------------------===//
49// ELF
50//===----------------------------------------------------------------------===//
Anton Korobeynikovab663a02010-02-15 22:37:53 +000051
Rafael Espindoladaeafb42014-02-19 17:23:20 +000052MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
53 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
54 MachineModuleInfo *MMI) const {
Rafael Espindolace83fc32011-04-27 23:17:57 +000055 unsigned Encoding = getPersonalityEncoding();
Logan Chienc0029812014-05-30 16:48:56 +000056 if ((Encoding & 0x80) == dwarf::DW_EH_PE_indirect)
Jim Grosbach6f482002015-05-18 18:43:14 +000057 return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000058 TM.getSymbol(GV, Mang)->getName());
Logan Chienc0029812014-05-30 16:48:56 +000059 if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr)
60 return TM.getSymbol(GV, Mang);
61 report_fatal_error("We do not support this DWARF encoding yet!");
Rafael Espindolaa83b1772011-04-16 03:51:21 +000062}
63
Mehdi Amini5c0fa582015-07-16 06:04:17 +000064void TargetLoweringObjectFileELF::emitPersonalityValue(
65 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000066 SmallString<64> NameData("DW.ref.");
67 NameData += Sym->getName();
Rafael Espindolaa8695762015-06-02 00:25:12 +000068 MCSymbolELF *Label =
69 cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
Rafael Espindola39897762011-04-27 21:29:52 +000070 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
71 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindola39897762011-04-27 21:29:52 +000072 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
Eric Christopher36e601c2016-07-01 06:07:38 +000073 MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
74 ELF::SHT_PROGBITS, Flags, 0);
Mehdi Amini5c0fa582015-07-16 06:04:17 +000075 unsigned Size = DL.getPointerSize();
Rafael Espindola39897762011-04-27 21:29:52 +000076 Streamer.SwitchSection(Sec);
Mehdi Amini5c0fa582015-07-16 06:04:17 +000077 Streamer.EmitValueToAlignment(DL.getPointerABIAlignment());
Rafael Espindola39897762011-04-27 21:29:52 +000078 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
Jim Grosbach13760bd2015-05-30 01:25:56 +000079 const MCExpr *E = MCConstantExpr::create(Size, getContext());
Rafael Espindolaa8695762015-06-02 00:25:12 +000080 Streamer.emitELFSize(Label, E);
Rafael Espindola39897762011-04-27 21:29:52 +000081 Streamer.EmitLabel(Label);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000082
Rafael Espindola39897762011-04-27 21:29:52 +000083 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000084}
85
Rafael Espindola15b26692014-02-09 14:50:44 +000086const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
87 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
Rafael Espindoladaeafb42014-02-19 17:23:20 +000088 const TargetMachine &TM, MachineModuleInfo *MMI,
89 MCStreamer &Streamer) const {
Anton Korobeynikove42af362012-11-14 01:47:00 +000090
91 if (Encoding & dwarf::DW_EH_PE_indirect) {
92 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
93
Rafael Espindoladaeafb42014-02-19 17:23:20 +000094 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang, TM);
Anton Korobeynikove42af362012-11-14 01:47:00 +000095
96 // Add information about the stub reference to ELFMMI so that the stub
97 // gets emitted by the asmprinter.
Anton Korobeynikove42af362012-11-14 01:47:00 +000098 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +000099 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000100 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000101 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
102 }
103
104 return TargetLoweringObjectFile::
Jim Grosbach13760bd2015-05-30 01:25:56 +0000105 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Anton Korobeynikove42af362012-11-14 01:47:00 +0000106 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
107 }
108
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000109 return TargetLoweringObjectFile::
110 getTTypeGlobalReference(GV, Encoding, Mang, TM, MMI, Streamer);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000111}
112
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000113static SectionKind
114getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindola0d018b12011-05-24 03:10:31 +0000115 // N.B.: The defaults used in here are no the same ones used in MC.
116 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
117 // both gas and MC will produce a section with no flags. Given
Bill Wendlingd1634052012-07-19 00:04:14 +0000118 // section(".eh_frame") gcc will produce:
119 //
120 // .section .eh_frame,"a",@progbits
Xinliang David Li5f04f922016-01-14 18:09:45 +0000121
122 if (Name == getInstrProfCoverageSectionName(false))
123 return SectionKind::getMetadata();
124
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000125 if (Name.empty() || Name[0] != '.') return K;
126
127 // Some lame default implementation based on some magic section names.
128 if (Name == ".bss" ||
129 Name.startswith(".bss.") ||
130 Name.startswith(".gnu.linkonce.b.") ||
131 Name.startswith(".llvm.linkonce.b.") ||
132 Name == ".sbss" ||
133 Name.startswith(".sbss.") ||
134 Name.startswith(".gnu.linkonce.sb.") ||
135 Name.startswith(".llvm.linkonce.sb."))
136 return SectionKind::getBSS();
137
138 if (Name == ".tdata" ||
139 Name.startswith(".tdata.") ||
140 Name.startswith(".gnu.linkonce.td.") ||
141 Name.startswith(".llvm.linkonce.td."))
142 return SectionKind::getThreadData();
143
144 if (Name == ".tbss" ||
145 Name.startswith(".tbss.") ||
146 Name.startswith(".gnu.linkonce.tb.") ||
147 Name.startswith(".llvm.linkonce.tb."))
148 return SectionKind::getThreadBSS();
149
150 return K;
151}
152
153
154static unsigned getELFSectionType(StringRef Name, SectionKind K) {
155
156 if (Name == ".init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000157 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000158
159 if (Name == ".fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000160 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000161
162 if (Name == ".preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000163 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000164
165 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolaaea49582011-01-23 04:28:49 +0000166 return ELF::SHT_NOBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000167
Rafael Espindolaaea49582011-01-23 04:28:49 +0000168 return ELF::SHT_PROGBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000169}
170
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000171static unsigned getELFSectionFlags(SectionKind K) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000172 unsigned Flags = 0;
173
174 if (!K.isMetadata())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000175 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000176
177 if (K.isText())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000178 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000179
Rafael Espindolac85e0d82011-06-07 23:26:45 +0000180 if (K.isWriteable())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000181 Flags |= ELF::SHF_WRITE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000182
183 if (K.isThreadLocal())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000184 Flags |= ELF::SHF_TLS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000185
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000186 if (K.isMergeableCString() || K.isMergeableConst())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000187 Flags |= ELF::SHF_MERGE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000188
189 if (K.isMergeableCString())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000190 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000191
192 return Flags;
193}
194
David Majnemerdad0a642014-06-27 18:19:56 +0000195static const Comdat *getELFComdat(const GlobalValue *GV) {
196 const Comdat *C = GV->getComdat();
197 if (!C)
198 return nullptr;
199
200 if (C->getSelectionKind() != Comdat::Any)
201 report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
202 C->getName() + "' cannot be lowered.");
203
204 return C;
205}
206
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000207MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000208 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
209 const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000210 StringRef SectionName = GV->getSection();
211
212 // Infer section flags from the section name if we can.
213 Kind = getELFKindForNamedSection(SectionName, Kind);
214
David Majnemerdad0a642014-06-27 18:19:56 +0000215 StringRef Group = "";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000216 unsigned Flags = getELFSectionFlags(Kind);
David Majnemerdad0a642014-06-27 18:19:56 +0000217 if (const Comdat *C = getELFComdat(GV)) {
218 Group = C->getName();
219 Flags |= ELF::SHF_GROUP;
220 }
Chris Lattner80c34592010-04-08 21:34:17 +0000221 return getContext().getELFSection(SectionName,
David Majnemerdad0a642014-06-27 18:19:56 +0000222 getELFSectionType(SectionName, Kind), Flags,
Rafael Espindolaba31e272015-01-29 17:33:21 +0000223 /*EntrySize=*/0, Group);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000224}
225
Rafael Espindola25d2c202015-02-11 14:44:17 +0000226/// Return the section prefix name used by options FunctionsSections and
227/// DataSections.
David Majnemer102ff692014-06-24 16:01:53 +0000228static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
Rafael Espindola25d2c202015-02-11 14:44:17 +0000229 if (Kind.isText())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000230 return ".text";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000231 if (Kind.isReadOnly())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000232 return ".rodata";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000233 if (Kind.isBSS())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000234 return ".bss";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000235 if (Kind.isThreadData())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000236 return ".tdata";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000237 if (Kind.isThreadBSS())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000238 return ".tbss";
Rafael Espindola449711c2015-11-18 06:02:15 +0000239 if (Kind.isData())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000240 return ".data";
Chris Lattner5b212a32010-04-13 00:36:43 +0000241 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Rafael Espindola68fa2492015-02-17 20:48:01 +0000242 return ".data.rel.ro";
Chris Lattner5b212a32010-04-13 00:36:43 +0000243}
244
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000245static MCSectionELF *
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000246selectELFSectionForGlobal(MCContext &Ctx, const GlobalValue *GV,
247 SectionKind Kind, Mangler &Mang,
248 const TargetMachine &TM, bool EmitUniqueSection,
249 unsigned Flags, unsigned *NextUniqueID) {
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000250 unsigned EntrySize = 0;
251 if (Kind.isMergeableCString()) {
252 if (Kind.isMergeable2ByteCString()) {
253 EntrySize = 2;
254 } else if (Kind.isMergeable4ByteCString()) {
255 EntrySize = 4;
256 } else {
257 EntrySize = 1;
258 assert(Kind.isMergeable1ByteCString() && "unknown string width");
259 }
260 } else if (Kind.isMergeableConst()) {
261 if (Kind.isMergeableConst4()) {
262 EntrySize = 4;
263 } else if (Kind.isMergeableConst8()) {
264 EntrySize = 8;
David Majnemer964b70d2016-02-22 22:23:11 +0000265 } else if (Kind.isMergeableConst16()) {
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000266 EntrySize = 16;
David Majnemer964b70d2016-02-22 22:23:11 +0000267 } else {
268 assert(Kind.isMergeableConst32() && "unknown data width");
269 EntrySize = 32;
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000270 }
271 }
272
Rafael Espindola9075f772015-02-20 23:28:28 +0000273 StringRef Group = "";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000274 if (const Comdat *C = getELFComdat(GV)) {
Rafael Espindola9075f772015-02-20 23:28:28 +0000275 Flags |= ELF::SHF_GROUP;
276 Group = C->getName();
277 }
278
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000279 bool UniqueSectionNames = TM.getUniqueSectionNames();
280 SmallString<128> Name;
Rafael Espindolaa05b3b72015-01-28 17:54:19 +0000281 if (Kind.isMergeableCString()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000282 // We also need alignment here.
283 // FIXME: this is getting the alignment of the character, not the
284 // alignment of the global!
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000285 unsigned Align = GV->getParent()->getDataLayout().getPreferredAlignment(
286 cast<GlobalVariable>(GV));
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000287
Rafael Espindolaba31e272015-01-29 17:33:21 +0000288 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000289 Name = SizeSpec + utostr(Align);
290 } else if (Kind.isMergeableConst()) {
291 Name = ".rodata.cst";
292 Name += utostr(EntrySize);
293 } else {
294 Name = getSectionPrefixForGlobal(Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000295 }
Easwaran Raman01d98ba2016-05-16 23:59:04 +0000296 // FIXME: Extend the section prefix to include hotness catagories such as .hot
297 // or .unlikely for functions.
Dehao Chenf84b6302016-02-23 03:39:24 +0000298
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000299 if (EmitUniqueSection && UniqueSectionNames) {
300 Name.push_back('.');
301 TM.getNameWithPrefix(Name, GV, Mang, true);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000302 }
Reid Kleckner97837b72016-05-02 23:22:18 +0000303 unsigned UniqueID = MCContext::GenericSectionID;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000304 if (EmitUniqueSection && !UniqueSectionNames) {
305 UniqueID = *NextUniqueID;
306 (*NextUniqueID)++;
307 }
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000308 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000309 EntrySize, Group, UniqueID);
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000310}
311
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000312MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000313 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
314 const TargetMachine &TM) const {
315 unsigned Flags = getELFSectionFlags(Kind);
316
317 // If we have -ffunction-section or -fdata-section then we should emit the
318 // global value to a uniqued section specifically for it.
319 bool EmitUniqueSection = false;
320 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
321 if (Kind.isText())
322 EmitUniqueSection = TM.getFunctionSections();
323 else
324 EmitUniqueSection = TM.getDataSections();
325 }
326 EmitUniqueSection |= GV->hasComdat();
327
328 return selectELFSectionForGlobal(getContext(), GV, Kind, Mang, TM,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000329 EmitUniqueSection, Flags, &NextUniqueID);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000330}
331
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000332MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
Rafael Espindola29786d42015-02-12 17:16:46 +0000333 const Function &F, Mangler &Mang, const TargetMachine &TM) const {
334 // If the function can be removed, produce a unique section so that
335 // the table doesn't prevent the removal.
336 const Comdat *C = F.getComdat();
337 bool EmitUniqueSection = TM.getFunctionSections() || C;
338 if (!EmitUniqueSection)
339 return ReadOnlySection;
340
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000341 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000342 Mang, TM, EmitUniqueSection, ELF::SHF_ALLOC,
343 &NextUniqueID);
Rafael Espindola29786d42015-02-12 17:16:46 +0000344}
345
Rafael Espindoladf195192015-02-17 23:34:51 +0000346bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
347 bool UsesLabelDifference, const Function &F) const {
348 // We can always create relative relocations, so use another section
349 // that can be marked non-executable.
350 return false;
351}
352
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000353/// Given a mergeable constant with the specified size and relocation
354/// information, return a section that it should be placed in.
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000355MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
David Majnemera3ea4072016-02-21 01:30:30 +0000356 const DataLayout &DL, SectionKind Kind, const Constant *C,
357 unsigned &Align) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000358 if (Kind.isMergeableConst4() && MergeableConst4Section)
359 return MergeableConst4Section;
360 if (Kind.isMergeableConst8() && MergeableConst8Section)
361 return MergeableConst8Section;
362 if (Kind.isMergeableConst16() && MergeableConst16Section)
363 return MergeableConst16Section;
David Majnemer964b70d2016-02-22 22:23:11 +0000364 if (Kind.isMergeableConst32() && MergeableConst32Section)
365 return MergeableConst32Section;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000366 if (Kind.isReadOnly())
367 return ReadOnlySection;
368
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000369 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
370 return DataRelROSection;
371}
372
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000373static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
374 bool IsCtor, unsigned Priority,
375 const MCSymbol *KeySym) {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000376 std::string Name;
377 unsigned Type;
378 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
Rafael Espindolac4b42532014-09-04 23:03:58 +0000379 StringRef COMDAT = KeySym ? KeySym->getName() : "";
380
381 if (KeySym)
382 Flags |= ELF::SHF_GROUP;
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000383
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000384 if (UseInitArray) {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000385 if (IsCtor) {
386 Type = ELF::SHT_INIT_ARRAY;
387 Name = ".init_array";
388 } else {
389 Type = ELF::SHT_FINI_ARRAY;
390 Name = ".fini_array";
391 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000392 if (Priority != 65535) {
393 Name += '.';
394 Name += utostr(Priority);
395 }
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000396 } else {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000397 // The default scheme is .ctor / .dtor, so we have to invert the priority
398 // numbering.
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000399 if (IsCtor)
400 Name = ".ctors";
401 else
402 Name = ".dtors";
Rafael Espindolac4b42532014-09-04 23:03:58 +0000403 if (Priority != 65535) {
404 Name += '.';
405 Name += utostr(65535 - Priority);
406 }
407 Type = ELF::SHT_PROGBITS;
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000408 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000409
Rafael Espindolaba31e272015-01-29 17:33:21 +0000410 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000411}
412
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000413MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000414 unsigned Priority, const MCSymbol *KeySym) const {
415 return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
416 KeySym);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000417}
418
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000419MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000420 unsigned Priority, const MCSymbol *KeySym) const {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000421 return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
422 KeySym);
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000423}
424
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000425const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
426 const GlobalValue *LHS, const GlobalValue *RHS, Mangler &Mang,
427 const TargetMachine &TM) const {
428 // We may only use a PLT-relative relocation to refer to unnamed_addr
429 // functions.
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000430 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000431 return nullptr;
432
433 // Basic sanity checks.
434 if (LHS->getType()->getPointerAddressSpace() != 0 ||
435 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
436 RHS->isThreadLocal())
437 return nullptr;
438
439 return MCBinaryExpr::createSub(
440 MCSymbolRefExpr::create(TM.getSymbol(LHS, Mang), PLTRelativeVariantKind,
441 getContext()),
442 MCSymbolRefExpr::create(TM.getSymbol(RHS, Mang), getContext()),
443 getContext());
444}
445
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000446void
447TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
448 UseInitArray = UseInitArray_;
Rafael Espindola46fa2312016-08-29 12:33:42 +0000449 MCContext &Ctx = getContext();
450 if (!UseInitArray) {
451 StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
452 ELF::SHF_ALLOC | ELF::SHF_WRITE);
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000453
Rafael Espindola46fa2312016-08-29 12:33:42 +0000454 StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
455 ELF::SHF_ALLOC | ELF::SHF_WRITE);
456 return;
457 }
458
459 StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
460 ELF::SHF_WRITE | ELF::SHF_ALLOC);
461 StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
462 ELF::SHF_WRITE | ELF::SHF_ALLOC);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000463}
464
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000465//===----------------------------------------------------------------------===//
466// MachO
467//===----------------------------------------------------------------------===//
468
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000469TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
470 : TargetLoweringObjectFile() {
471 SupportIndirectSymViaGOTPCRel = true;
472}
473
Rafael Espindola46fa2312016-08-29 12:33:42 +0000474void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
475 const TargetMachine &TM) {
476 TargetLoweringObjectFile::Initialize(Ctx, TM);
Rafael Espindola412a5292016-08-29 12:47:22 +0000477 if (TM.getRelocationModel() == Reloc::Static) {
Rafael Espindola46fa2312016-08-29 12:33:42 +0000478 StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
479 SectionKind::getData());
480 StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
481 SectionKind::getData());
482 } else {
483 StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
484 MachO::S_MOD_INIT_FUNC_POINTERS,
485 SectionKind::getData());
486 StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
487 MachO::S_MOD_TERM_FUNC_POINTERS,
488 SectionKind::getData());
489 }
490}
491
Daniel Dunbar95856122013-01-18 19:37:00 +0000492/// emitModuleFlags - Perform code emission for module flags.
Bill Wendling06df7722012-02-14 21:28:13 +0000493void TargetLoweringObjectFileMachO::
Bill Wendling734909a2012-02-15 22:36:15 +0000494emitModuleFlags(MCStreamer &Streamer,
495 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000496 Mangler &Mang, const TargetMachine &TM) const {
Bill Wendling06df7722012-02-14 21:28:13 +0000497 unsigned VersionVal = 0;
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000498 unsigned ImageInfoFlags = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000499 MDNode *LinkerOptions = nullptr;
Bill Wendling734909a2012-02-15 22:36:15 +0000500 StringRef SectionVal;
Bill Wendling06df7722012-02-14 21:28:13 +0000501
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000502 for (const auto &MFE : ModuleFlags) {
Bill Wendling06df7722012-02-14 21:28:13 +0000503 // Ignore flags with 'Require' behavior.
Bill Wendling734909a2012-02-15 22:36:15 +0000504 if (MFE.Behavior == Module::Require)
Bill Wendling06df7722012-02-14 21:28:13 +0000505 continue;
506
Bill Wendling734909a2012-02-15 22:36:15 +0000507 StringRef Key = MFE.Key->getString();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000508 Metadata *Val = MFE.Val;
Bill Wendling06df7722012-02-14 21:28:13 +0000509
Daniel Dunbar95856122013-01-18 19:37:00 +0000510 if (Key == "Objective-C Image Info Version") {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000511 VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000512 } else if (Key == "Objective-C Garbage Collection" ||
513 Key == "Objective-C GC Only" ||
Manman Renc98ec0e2014-11-21 19:24:55 +0000514 Key == "Objective-C Is Simulated" ||
Manman Renc77e0ff2016-01-29 23:51:00 +0000515 Key == "Objective-C Class Properties" ||
Manman Renc98ec0e2014-11-21 19:24:55 +0000516 Key == "Objective-C Image Swift Version") {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000517 ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000518 } else if (Key == "Objective-C Image Info Section") {
Bill Wendling734909a2012-02-15 22:36:15 +0000519 SectionVal = cast<MDString>(Val)->getString();
Daniel Dunbar95856122013-01-18 19:37:00 +0000520 } else if (Key == "Linker Options") {
521 LinkerOptions = cast<MDNode>(Val);
522 }
523 }
524
525 // Emit the linker options if present.
526 if (LinkerOptions) {
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000527 for (const auto &Option : LinkerOptions->operands()) {
Daniel Dunbar95856122013-01-18 19:37:00 +0000528 SmallVector<std::string, 4> StrOptions;
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000529 for (const auto &Piece : cast<MDNode>(Option)->operands())
530 StrOptions.push_back(cast<MDString>(Piece)->getString());
Daniel Dunbar95856122013-01-18 19:37:00 +0000531 Streamer.EmitLinkerOptions(StrOptions);
532 }
Bill Wendling06df7722012-02-14 21:28:13 +0000533 }
534
Bill Wendling734909a2012-02-15 22:36:15 +0000535 // The section is mandatory. If we don't have it, then we don't have GC info.
536 if (SectionVal.empty()) return;
Bill Wendling06df7722012-02-14 21:28:13 +0000537
Bill Wendling734909a2012-02-15 22:36:15 +0000538 StringRef Segment, Section;
539 unsigned TAA = 0, StubSize = 0;
540 bool TAAParsed;
541 std::string ErrorCode =
542 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
543 TAA, TAAParsed, StubSize);
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000544 if (!ErrorCode.empty())
Bill Wendling734909a2012-02-15 22:36:15 +0000545 // If invalid, report the error with report_fatal_error.
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000546 report_fatal_error("Invalid section specifier '" + Section + "': " +
547 ErrorCode + ".");
Bill Wendling06df7722012-02-14 21:28:13 +0000548
Bill Wendling734909a2012-02-15 22:36:15 +0000549 // Get the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000550 MCSectionMachO *S = getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000551 Segment, Section, TAA, StubSize, SectionKind::getData());
Bill Wendling734909a2012-02-15 22:36:15 +0000552 Streamer.SwitchSection(S);
553 Streamer.EmitLabel(getContext().
Jim Grosbach6f482002015-05-18 18:43:14 +0000554 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendling06df7722012-02-14 21:28:13 +0000555 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000556 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendling06df7722012-02-14 21:28:13 +0000557 Streamer.AddBlankLine();
558}
559
David Majnemerdad0a642014-06-27 18:19:56 +0000560static void checkMachOComdat(const GlobalValue *GV) {
561 const Comdat *C = GV->getComdat();
562 if (!C)
563 return;
564
565 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
566 "' cannot be lowered.");
567}
568
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000569MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000570 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
571 const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000572 // Parse the section specifier and create it if valid.
573 StringRef Segment, Section;
Stuart Hastings12d53122011-03-19 02:42:31 +0000574 unsigned TAA = 0, StubSize = 0;
575 bool TAAParsed;
David Majnemerdad0a642014-06-27 18:19:56 +0000576
577 checkMachOComdat(GV);
578
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000579 std::string ErrorCode =
580 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000581 TAA, TAAParsed, StubSize);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000582 if (!ErrorCode.empty()) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000583 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000584 report_fatal_error("Global variable '" + GV->getName() +
585 "' has an invalid section specifier '" +
586 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000587 }
588
589 // Get the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000590 MCSectionMachO *S =
591 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000592
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000593 // If TAA wasn't set by ParseSectionSpecifier() above,
594 // use the value returned by getMachOSection() as a default.
Stuart Hastings12d53122011-03-19 02:42:31 +0000595 if (!TAAParsed)
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000596 TAA = S->getTypeAndAttributes();
597
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000598 // Okay, now that we got the section, verify that the TAA & StubSize agree.
599 // If the user declared multiple globals with different section flags, we need
600 // to reject it here.
601 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000602 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000603 report_fatal_error("Global variable '" + GV->getName() +
604 "' section type or attributes does not match previous"
605 " section specifier");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000606 }
607
608 return S;
609}
610
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000611MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
612 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
613 const TargetMachine &TM) const {
David Majnemerdad0a642014-06-27 18:19:56 +0000614 checkMachOComdat(GV);
Bill Wendling25b61db2013-11-17 10:53:13 +0000615
616 // Handle thread local data.
617 if (Kind.isThreadBSS()) return TLSBSSSection;
618 if (Kind.isThreadData()) return TLSDataSection;
619
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000620 if (Kind.isText())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000621 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
622
623 // If this is weak/linkonce, put this in a coalescable section, either in text
624 // or data depending on if it is writable.
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000625 if (GV->isWeakForLinker()) {
626 if (Kind.isReadOnly())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000627 return ConstTextCoalSection;
628 return DataCoalSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000629 }
630
631 // FIXME: Alignment check should be handled by section classifier.
Chris Lattneref2f8042010-03-07 04:28:09 +0000632 if (Kind.isMergeable1ByteCString() &&
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000633 GV->getParent()->getDataLayout().getPreferredAlignment(
634 cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000635 return CStringSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000636
Chris Lattneref2f8042010-03-07 04:28:09 +0000637 // Do not put 16-bit arrays in the UString section if they have an
638 // externally visible label, this runs into issues with certain linker
639 // versions.
640 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000641 GV->getParent()->getDataLayout().getPreferredAlignment(
642 cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000643 return UStringSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000644
Rafael Espindolab43d51d2014-08-28 20:13:31 +0000645 // With MachO only variables whose corresponding symbol starts with 'l' or
646 // 'L' can be merged, so we only try merging GVs with private linkage.
647 if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000648 if (Kind.isMergeableConst4())
649 return FourByteConstantSection;
650 if (Kind.isMergeableConst8())
651 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000652 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000653 return SixteenByteConstantSection;
654 }
655
656 // Otherwise, if it is readonly, but not something we can specially optimize,
657 // just drop it in .const.
658 if (Kind.isReadOnly())
659 return ReadOnlySection;
660
661 // If this is marked const, put it into a const section. But if the dynamic
662 // linker needs to write to it, put it in the data segment.
663 if (Kind.isReadOnlyWithRel())
664 return ConstDataSection;
665
666 // Put zero initialized globals with strong external linkage in the
667 // DATA, __common section with the .zerofill directive.
668 if (Kind.isBSSExtern())
669 return DataCommonSection;
670
671 // Put zero initialized globals with local linkage in __DATA,__bss directive
672 // with the .zerofill directive (aka .lcomm).
673 if (Kind.isBSSLocal())
674 return DataBSSSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000675
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000676 // Otherwise, just drop the variable in the normal data section.
677 return DataSection;
678}
679
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000680MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
David Majnemera3ea4072016-02-21 01:30:30 +0000681 const DataLayout &DL, SectionKind Kind, const Constant *C,
682 unsigned &Align) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000683 // If this constant requires a relocation, we have to put it in the data
684 // segment, not in the text segment.
Rafael Espindola449711c2015-11-18 06:02:15 +0000685 if (Kind.isData() || Kind.isReadOnlyWithRel())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000686 return ConstDataSection;
687
688 if (Kind.isMergeableConst4())
689 return FourByteConstantSection;
690 if (Kind.isMergeableConst8())
691 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000692 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000693 return SixteenByteConstantSection;
694 return ReadOnlySection; // .const
695}
696
Rafael Espindola15b26692014-02-09 14:50:44 +0000697const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
698 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000699 const TargetMachine &TM, MachineModuleInfo *MMI,
700 MCStreamer &Streamer) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000701 // The mach-o version of this method defaults to returning a stub reference.
702
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000703 if (Encoding & DW_EH_PE_indirect) {
704 MachineModuleInfoMachO &MachOMMI =
705 MMI->getObjFileInfo<MachineModuleInfoMachO>();
706
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000707 MCSymbol *SSym =
708 getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000709
710 // Add information about the stub reference to MachOMMI so that the stub
711 // gets emitted by the asmprinter.
Rafael Espindola712f9572016-05-17 16:01:32 +0000712 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000713 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000714 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000715 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000716 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000717
718 return TargetLoweringObjectFile::
Jim Grosbach13760bd2015-05-30 01:25:56 +0000719 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Anton Korobeynikove42af362012-11-14 01:47:00 +0000720 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000721 }
722
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000723 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang,
724 TM, MMI, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000725}
726
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000727MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
728 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
729 MachineModuleInfo *MMI) const {
Rafael Espindola08704342011-04-27 23:08:15 +0000730 // The mach-o version of this method defaults to returning a stub reference.
731 MachineModuleInfoMachO &MachOMMI =
732 MMI->getObjFileInfo<MachineModuleInfoMachO>();
733
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000734 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Rafael Espindola08704342011-04-27 23:08:15 +0000735
736 // Add information about the stub reference to MachOMMI so that the stub
737 // gets emitted by the asmprinter.
Bill Wendlinge4cc3322011-11-29 01:43:20 +0000738 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000739 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000740 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Rafael Espindola08704342011-04-27 23:08:15 +0000741 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
742 }
743
744 return SSym;
745}
746
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000747const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
748 const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
749 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000750 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000751 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
752 // through a non_lazy_ptr stub instead. One advantage is that it allows the
753 // computation of deltas to final external symbols. Example:
754 //
755 // _extgotequiv:
756 // .long _extfoo
757 //
758 // _delta:
759 // .long _extgotequiv-_delta
760 //
761 // is transformed to:
762 //
763 // _delta:
764 // .long L_extfoo$non_lazy_ptr-(_delta+0)
765 //
766 // .section __IMPORT,__pointers,non_lazy_symbol_pointers
767 // L_extfoo$non_lazy_ptr:
768 // .indirect_symbol _extfoo
769 // .long 0
770 //
771 MachineModuleInfoMachO &MachOMMI =
772 MMI->getObjFileInfo<MachineModuleInfoMachO>();
773 MCContext &Ctx = getContext();
774
775 // The offset must consider the original displacement from the base symbol
776 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
777 Offset = -MV.getConstant();
778 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
779
780 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
781 // non_lazy_ptr stubs.
782 SmallString<128> Name;
783 StringRef Suffix = "$non_lazy_ptr";
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000784 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000785 Name += Sym->getName();
786 Name += Suffix;
Jim Grosbach6f482002015-05-18 18:43:14 +0000787 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000788
789 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
790 if (!StubSym.getPointer())
791 StubSym = MachineModuleInfoImpl::
792 StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */);
793
794 const MCExpr *BSymExpr =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000795 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000796 const MCExpr *LHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000797 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000798
799 if (!Offset)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000800 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000801
802 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000803 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
804 return MCBinaryExpr::createSub(LHS, RHS, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000805}
806
Peter Collingbourne94d77862015-11-03 23:40:03 +0000807static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
808 const MCSection &Section) {
809 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
810 return true;
811
812 // If it is not dead stripped, it is safe to use private labels.
813 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
814 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
815 return true;
816
817 return false;
818}
819
820void TargetLoweringObjectFileMachO::getNameWithPrefix(
821 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang,
822 const TargetMachine &TM) const {
823 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
824 const MCSection *TheSection = SectionForGlobal(GV, GVKind, Mang, TM);
825 bool CannotUsePrivateLabel =
826 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
827 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
828}
829
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000830//===----------------------------------------------------------------------===//
831// COFF
832//===----------------------------------------------------------------------===//
833
Chris Lattner87cffa92010-05-07 17:17:41 +0000834static unsigned
Renato Golinef3eb062016-06-27 14:42:20 +0000835getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) {
Chris Lattner87cffa92010-05-07 17:17:41 +0000836 unsigned Flags = 0;
Renato Golinef3eb062016-06-27 14:42:20 +0000837 bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
Chris Lattner87cffa92010-05-07 17:17:41 +0000838
Anton Korobeynikove4152302010-07-06 15:24:56 +0000839 if (K.isMetadata())
Chris Lattner02844932010-05-07 21:49:09 +0000840 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000841 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000842 else if (K.isText())
843 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000844 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer0f83d962010-10-27 18:52:29 +0000845 COFF::IMAGE_SCN_MEM_READ |
Renato Golinef3eb062016-06-27 14:42:20 +0000846 COFF::IMAGE_SCN_CNT_CODE |
847 (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0);
David Majnemerb8dbebb2014-09-20 07:31:46 +0000848 else if (K.isBSS())
Chris Lattner02844932010-05-07 21:49:09 +0000849 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000850 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
851 COFF::IMAGE_SCN_MEM_READ |
852 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000853 else if (K.isThreadLocal())
854 Flags |=
855 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
856 COFF::IMAGE_SCN_MEM_READ |
857 COFF::IMAGE_SCN_MEM_WRITE;
David Majnemerb8dbebb2014-09-20 07:31:46 +0000858 else if (K.isReadOnly() || K.isReadOnlyWithRel())
Chris Lattner87cffa92010-05-07 17:17:41 +0000859 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000860 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
861 COFF::IMAGE_SCN_MEM_READ;
Chris Lattner87cffa92010-05-07 17:17:41 +0000862 else if (K.isWriteable())
863 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000864 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
865 COFF::IMAGE_SCN_MEM_READ |
866 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000867
868 return Flags;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000869}
870
Benjamin Kramer6cbe6702014-07-07 14:47:51 +0000871static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
David Majnemerdad0a642014-06-27 18:19:56 +0000872 const Comdat *C = GV->getComdat();
873 assert(C && "expected GV to have a Comdat!");
874
875 StringRef ComdatGVName = C->getName();
876 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
877 if (!ComdatGV)
878 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
879 "' does not exist.");
880
881 if (ComdatGV->getComdat() != C)
882 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
Hans Wennborgc0f0c512014-09-19 01:14:56 +0000883 "' is not a key for its COMDAT.");
David Majnemerdad0a642014-06-27 18:19:56 +0000884
885 return ComdatGV;
886}
887
888static int getSelectionForCOFF(const GlobalValue *GV) {
889 if (const Comdat *C = GV->getComdat()) {
890 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
891 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
892 ComdatKey = GA->getBaseObject();
893 if (ComdatKey == GV) {
894 switch (C->getSelectionKind()) {
895 case Comdat::Any:
896 return COFF::IMAGE_COMDAT_SELECT_ANY;
897 case Comdat::ExactMatch:
898 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
899 case Comdat::Largest:
900 return COFF::IMAGE_COMDAT_SELECT_LARGEST;
901 case Comdat::NoDuplicates:
902 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
903 case Comdat::SameSize:
904 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
905 }
906 } else {
907 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
908 }
David Majnemerdad0a642014-06-27 18:19:56 +0000909 }
910 return 0;
911}
912
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000913MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000914 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
915 const TargetMachine &TM) const {
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000916 int Selection = 0;
Renato Golinef3eb062016-06-27 14:42:20 +0000917 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000918 StringRef Name = GV->getSection();
919 StringRef COMDATSymName = "";
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000920 if (GV->hasComdat()) {
David Majnemerdad0a642014-06-27 18:19:56 +0000921 Selection = getSelectionForCOFF(GV);
922 const GlobalValue *ComdatGV;
923 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
924 ComdatGV = getComdatGVForCOFF(GV);
925 else
926 ComdatGV = GV;
927
928 if (!ComdatGV->hasPrivateLinkage()) {
929 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang);
930 COMDATSymName = Sym->getName();
931 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
932 } else {
933 Selection = 0;
934 }
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000935 }
Reid Kleckner97837b72016-05-02 23:22:18 +0000936
937 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
Nico Riecka37acf72013-07-06 12:13:10 +0000938 Selection);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000939}
940
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000941static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000942 if (Kind.isText())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000943 return ".text";
David Majnemera9bdb322014-04-08 22:33:40 +0000944 if (Kind.isBSS())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000945 return ".bss";
946 if (Kind.isThreadLocal())
Rafael Espindola3c8e1472013-11-27 15:52:11 +0000947 return ".tls$";
David Majnemer597be2d2014-09-22 20:39:23 +0000948 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
949 return ".rdata";
950 return ".data";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000951}
952
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000953MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
954 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
955 const TargetMachine &TM) const {
David Majnemer93389842014-03-23 17:47:39 +0000956 // If we have -ffunction-sections then we should emit the global value to a
957 // uniqued section specifically for it.
David Majnemer273bff42014-03-25 06:14:26 +0000958 bool EmitUniquedSection;
959 if (Kind.isText())
960 EmitUniquedSection = TM.getFunctionSections();
961 else
962 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000963
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000964 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000965 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Renato Golinef3eb062016-06-27 14:42:20 +0000966 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Chris Lattner02844932010-05-07 21:49:09 +0000967
Daniel Dunbar329d2022010-07-01 20:07:24 +0000968 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
David Majnemerdad0a642014-06-27 18:19:56 +0000969 int Selection = getSelectionForCOFF(GV);
970 if (!Selection)
971 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
972 const GlobalValue *ComdatGV;
973 if (GV->hasComdat())
974 ComdatGV = getComdatGVForCOFF(GV);
975 else
976 ComdatGV = GV;
977
Reid Kleckner97837b72016-05-02 23:22:18 +0000978 unsigned UniqueID = MCContext::GenericSectionID;
979 if (EmitUniquedSection)
980 UniqueID = NextUniqueID++;
981
David Majnemerdad0a642014-06-27 18:19:56 +0000982 if (!ComdatGV->hasPrivateLinkage()) {
983 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang);
984 StringRef COMDATSymName = Sym->getName();
985 return getContext().getCOFFSection(Name, Characteristics, Kind,
Reid Kleckner97837b72016-05-02 23:22:18 +0000986 COMDATSymName, Selection, UniqueID);
David Majnemer7db449a2015-03-17 23:54:51 +0000987 } else {
988 SmallString<256> TmpData;
Peter Collingbourne94d77862015-11-03 23:40:03 +0000989 Mang.getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true);
David Majnemer7db449a2015-03-17 23:54:51 +0000990 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
Reid Kleckner97837b72016-05-02 23:22:18 +0000991 Selection, UniqueID);
David Majnemerdad0a642014-06-27 18:19:56 +0000992 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000993 }
994
995 if (Kind.isText())
David Majnemer3d96acb2013-08-13 01:23:53 +0000996 return TextSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000997
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000998 if (Kind.isThreadLocal())
David Majnemer3d96acb2013-08-13 01:23:53 +0000999 return TLSDataSection;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +00001000
David Majnemer597be2d2014-09-22 20:39:23 +00001001 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
David Majnemerf76d6b32013-08-08 01:50:52 +00001002 return ReadOnlySection;
1003
David Majnemera9bdb322014-04-08 22:33:40 +00001004 // Note: we claim that common symbols are put in BSSSection, but they are
1005 // really emitted with the magic .comm directive, which creates a symbol table
1006 // entry but not a section.
1007 if (Kind.isBSS() || Kind.isCommon())
David Majnemer3d96acb2013-08-13 01:23:53 +00001008 return BSSSection;
1009
1010 return DataSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001011}
1012
David Majnemer7db449a2015-03-17 23:54:51 +00001013void TargetLoweringObjectFileCOFF::getNameWithPrefix(
Peter Collingbourne94d77862015-11-03 23:40:03 +00001014 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang,
1015 const TargetMachine &TM) const {
1016 bool CannotUsePrivateLabel = false;
David Majnemer7db449a2015-03-17 23:54:51 +00001017 if (GV->hasPrivateLinkage() &&
1018 ((isa<Function>(GV) && TM.getFunctionSections()) ||
1019 (isa<GlobalVariable>(GV) && TM.getDataSections())))
1020 CannotUsePrivateLabel = true;
1021
1022 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1023}
1024
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001025MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
Rafael Espindolaab447e42015-03-11 19:58:37 +00001026 const Function &F, Mangler &Mang, const TargetMachine &TM) const {
1027 // If the function can be removed, produce a unique section so that
1028 // the table doesn't prevent the removal.
1029 const Comdat *C = F.getComdat();
1030 bool EmitUniqueSection = TM.getFunctionSections() || C;
1031 if (!EmitUniqueSection)
1032 return ReadOnlySection;
1033
1034 // FIXME: we should produce a symbol for F instead.
1035 if (F.hasPrivateLinkage())
1036 return ReadOnlySection;
1037
1038 MCSymbol *Sym = TM.getSymbol(&F, Mang);
1039 StringRef COMDATSymName = Sym->getName();
1040
1041 SectionKind Kind = SectionKind::getReadOnly();
1042 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Renato Golinef3eb062016-06-27 14:42:20 +00001043 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Rafael Espindolaab447e42015-03-11 19:58:37 +00001044 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Reid Kleckner97837b72016-05-02 23:22:18 +00001045 unsigned UniqueID = NextUniqueID++;
Rafael Espindolaab447e42015-03-11 19:58:37 +00001046
1047 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
Reid Kleckner97837b72016-05-02 23:22:18 +00001048 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
Rafael Espindolaab447e42015-03-11 19:58:37 +00001049}
1050
Reid Klecknerd973ca32013-04-25 19:34:41 +00001051void TargetLoweringObjectFileCOFF::
1052emitModuleFlags(MCStreamer &Streamer,
1053 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +00001054 Mangler &Mang, const TargetMachine &TM) const {
Craig Topperc0196b12014-04-14 00:51:57 +00001055 MDNode *LinkerOptions = nullptr;
Reid Klecknerd973ca32013-04-25 19:34:41 +00001056
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +00001057 for (const auto &MFE : ModuleFlags) {
Reid Klecknerd973ca32013-04-25 19:34:41 +00001058 StringRef Key = MFE.Key->getString();
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +00001059 if (Key == "Linker Options")
1060 LinkerOptions = cast<MDNode>(MFE.Val);
Reid Klecknerd973ca32013-04-25 19:34:41 +00001061 }
Reid Klecknerd973ca32013-04-25 19:34:41 +00001062
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +00001063 if (LinkerOptions) {
1064 // Emit the linker options to the linker .drectve section. According to the
1065 // spec, this section is a space-separated string containing flags for
1066 // linker.
1067 MCSection *Sec = getDrectveSection();
1068 Streamer.SwitchSection(Sec);
1069 for (const auto &Option : LinkerOptions->operands()) {
1070 for (const auto &Piece : cast<MDNode>(Option)->operands()) {
1071 // Lead with a space for consistency with our dllexport implementation.
1072 std::string Directive(" ");
1073 Directive.append(cast<MDString>(Piece)->getString());
1074 Streamer.EmitBytes(Directive);
1075 }
Reid Klecknerd973ca32013-04-25 19:34:41 +00001076 }
1077 }
1078}
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001079
Rafael Espindola46fa2312016-08-29 12:33:42 +00001080void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1081 const TargetMachine &TM) {
1082 TargetLoweringObjectFile::Initialize(Ctx, TM);
1083 const Triple &T = TM.getTargetTriple();
1084 if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1085 StaticCtorSection =
1086 Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1087 COFF::IMAGE_SCN_MEM_READ,
1088 SectionKind::getReadOnly());
1089 StaticDtorSection =
1090 Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1091 COFF::IMAGE_SCN_MEM_READ,
1092 SectionKind::getReadOnly());
1093 } else {
1094 StaticCtorSection = Ctx.getCOFFSection(
1095 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1096 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1097 SectionKind::getData());
1098 StaticDtorSection = Ctx.getCOFFSection(
1099 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1100 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1101 SectionKind::getData());
1102 }
1103}
1104
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001105MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +00001106 unsigned Priority, const MCSymbol *KeySym) const {
Reid Kleckner7c4059e2014-09-04 17:42:03 +00001107 return getContext().getAssociativeCOFFSection(
Reid Kleckner97837b72016-05-02 23:22:18 +00001108 cast<MCSectionCOFF>(StaticCtorSection), KeySym, 0);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001109}
1110
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001111MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +00001112 unsigned Priority, const MCSymbol *KeySym) const {
Reid Kleckner7c4059e2014-09-04 17:42:03 +00001113 return getContext().getAssociativeCOFFSection(
Reid Kleckner97837b72016-05-02 23:22:18 +00001114 cast<MCSectionCOFF>(StaticDtorSection), KeySym, 0);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001115}
Peter Collingbourneaef36592015-06-29 22:04:09 +00001116
1117void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
1118 raw_ostream &OS, const GlobalValue *GV, const Mangler &Mang) const {
1119 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
1120 return;
1121
1122 const Triple &TT = getTargetTriple();
1123
1124 if (TT.isKnownWindowsMSVCEnvironment())
1125 OS << " /EXPORT:";
1126 else
1127 OS << " -export:";
1128
1129 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
1130 std::string Flag;
1131 raw_string_ostream FlagOS(Flag);
1132 Mang.getNameWithPrefix(FlagOS, GV, false);
1133 FlagOS.flush();
Mehdi Amini5c0fa582015-07-16 06:04:17 +00001134 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
Peter Collingbourneaef36592015-06-29 22:04:09 +00001135 OS << Flag.substr(1);
1136 else
1137 OS << Flag;
1138 } else {
1139 Mang.getNameWithPrefix(OS, GV, false);
1140 }
1141
1142 if (!GV->getValueType()->isFunctionTy()) {
1143 if (TT.isKnownWindowsMSVCEnvironment())
1144 OS << ",DATA";
1145 else
1146 OS << ",data";
1147 }
1148}