blob: 539d485ffef3fc549071874c0ca56022802c258c [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(
Eric Christopher4367c7f2016-09-16 07:33:15 +000053 const GlobalValue *GV, const TargetMachine &TM,
Rafael Espindoladaeafb42014-02-19 17:23:20 +000054 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.") +
Eric Christopher4367c7f2016-09-16 07:33:15 +000058 TM.getSymbol(GV, getMangler())->getName());
Logan Chienc0029812014-05-30 16:48:56 +000059 if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr)
Eric Christopher4367c7f2016-09-16 07:33:15 +000060 return TM.getSymbol(GV, getMangler());
Logan Chienc0029812014-05-30 16:48:56 +000061 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(
Eric Christopher4367c7f2016-09-16 07:33:15 +000087 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
88 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Anton Korobeynikove42af362012-11-14 01:47:00 +000089
90 if (Encoding & dwarf::DW_EH_PE_indirect) {
91 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
92
Eric Christopher4367c7f2016-09-16 07:33:15 +000093 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM);
Anton Korobeynikove42af362012-11-14 01:47:00 +000094
95 // Add information about the stub reference to ELFMMI so that the stub
96 // gets emitted by the asmprinter.
Anton Korobeynikove42af362012-11-14 01:47:00 +000097 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +000098 if (!StubSym.getPointer()) {
Eric Christopher4367c7f2016-09-16 07:33:15 +000099 MCSymbol *Sym = TM.getSymbol(GV, getMangler());
Anton Korobeynikove42af362012-11-14 01:47:00 +0000100 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
101 }
102
103 return TargetLoweringObjectFile::
Jim Grosbach13760bd2015-05-30 01:25:56 +0000104 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Anton Korobeynikove42af362012-11-14 01:47:00 +0000105 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
106 }
107
Eric Christopher4367c7f2016-09-16 07:33:15 +0000108 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
109 MMI, Streamer);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000110}
111
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000112static SectionKind
113getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindola0d018b12011-05-24 03:10:31 +0000114 // N.B.: The defaults used in here are no the same ones used in MC.
115 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
116 // both gas and MC will produce a section with no flags. Given
Bill Wendlingd1634052012-07-19 00:04:14 +0000117 // section(".eh_frame") gcc will produce:
118 //
119 // .section .eh_frame,"a",@progbits
Xinliang David Li5f04f922016-01-14 18:09:45 +0000120
121 if (Name == getInstrProfCoverageSectionName(false))
122 return SectionKind::getMetadata();
123
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000124 if (Name.empty() || Name[0] != '.') return K;
125
126 // Some lame default implementation based on some magic section names.
127 if (Name == ".bss" ||
128 Name.startswith(".bss.") ||
129 Name.startswith(".gnu.linkonce.b.") ||
130 Name.startswith(".llvm.linkonce.b.") ||
131 Name == ".sbss" ||
132 Name.startswith(".sbss.") ||
133 Name.startswith(".gnu.linkonce.sb.") ||
134 Name.startswith(".llvm.linkonce.sb."))
135 return SectionKind::getBSS();
136
137 if (Name == ".tdata" ||
138 Name.startswith(".tdata.") ||
139 Name.startswith(".gnu.linkonce.td.") ||
140 Name.startswith(".llvm.linkonce.td."))
141 return SectionKind::getThreadData();
142
143 if (Name == ".tbss" ||
144 Name.startswith(".tbss.") ||
145 Name.startswith(".gnu.linkonce.tb.") ||
146 Name.startswith(".llvm.linkonce.tb."))
147 return SectionKind::getThreadBSS();
148
149 return K;
150}
151
152
153static unsigned getELFSectionType(StringRef Name, SectionKind K) {
Petr Hosek12903552016-09-20 20:21:13 +0000154 // Use SHT_NOTE for section whose name starts with ".note" to allow
155 // emitting ELF notes from C variable declaration.
156 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
157 if (Name.startswith(".note"))
158 return ELF::SHT_NOTE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000159
160 if (Name == ".init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000161 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000162
163 if (Name == ".fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000164 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000165
166 if (Name == ".preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000167 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000168
169 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolaaea49582011-01-23 04:28:49 +0000170 return ELF::SHT_NOBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000171
Rafael Espindolaaea49582011-01-23 04:28:49 +0000172 return ELF::SHT_PROGBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000173}
174
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000175static unsigned getELFSectionFlags(SectionKind K) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000176 unsigned Flags = 0;
177
178 if (!K.isMetadata())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000179 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000180
181 if (K.isText())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000182 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000183
Rafael Espindolac85e0d82011-06-07 23:26:45 +0000184 if (K.isWriteable())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000185 Flags |= ELF::SHF_WRITE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000186
187 if (K.isThreadLocal())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000188 Flags |= ELF::SHF_TLS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000189
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000190 if (K.isMergeableCString() || K.isMergeableConst())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000191 Flags |= ELF::SHF_MERGE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000192
193 if (K.isMergeableCString())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000194 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000195
196 return Flags;
197}
198
David Majnemerdad0a642014-06-27 18:19:56 +0000199static const Comdat *getELFComdat(const GlobalValue *GV) {
200 const Comdat *C = GV->getComdat();
201 if (!C)
202 return nullptr;
203
204 if (C->getSelectionKind() != Comdat::Any)
205 report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
206 C->getName() + "' cannot be lowered.");
207
208 return C;
209}
210
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000211MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000212 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
213 StringRef SectionName = GO->getSection();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000214
215 // Infer section flags from the section name if we can.
216 Kind = getELFKindForNamedSection(SectionName, Kind);
217
David Majnemerdad0a642014-06-27 18:19:56 +0000218 StringRef Group = "";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000219 unsigned Flags = getELFSectionFlags(Kind);
Peter Collingbourne67335642016-10-24 19:23:39 +0000220 if (const Comdat *C = getELFComdat(GO)) {
David Majnemerdad0a642014-06-27 18:19:56 +0000221 Group = C->getName();
222 Flags |= ELF::SHF_GROUP;
223 }
Chris Lattner80c34592010-04-08 21:34:17 +0000224 return getContext().getELFSection(SectionName,
David Majnemerdad0a642014-06-27 18:19:56 +0000225 getELFSectionType(SectionName, Kind), Flags,
Rafael Espindolaba31e272015-01-29 17:33:21 +0000226 /*EntrySize=*/0, Group);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000227}
228
Rafael Espindola25d2c202015-02-11 14:44:17 +0000229/// Return the section prefix name used by options FunctionsSections and
230/// DataSections.
David Majnemer102ff692014-06-24 16:01:53 +0000231static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
Rafael Espindola25d2c202015-02-11 14:44:17 +0000232 if (Kind.isText())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000233 return ".text";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000234 if (Kind.isReadOnly())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000235 return ".rodata";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000236 if (Kind.isBSS())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000237 return ".bss";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000238 if (Kind.isThreadData())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000239 return ".tdata";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000240 if (Kind.isThreadBSS())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000241 return ".tbss";
Rafael Espindola449711c2015-11-18 06:02:15 +0000242 if (Kind.isData())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000243 return ".data";
Chris Lattner5b212a32010-04-13 00:36:43 +0000244 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Rafael Espindola68fa2492015-02-17 20:48:01 +0000245 return ".data.rel.ro";
Chris Lattner5b212a32010-04-13 00:36:43 +0000246}
247
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000248static MCSectionELF *
Peter Collingbourne67335642016-10-24 19:23:39 +0000249selectELFSectionForGlobal(MCContext &Ctx, const GlobalObject *GO,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000250 SectionKind Kind, Mangler &Mang,
251 const TargetMachine &TM, bool EmitUniqueSection,
252 unsigned Flags, unsigned *NextUniqueID) {
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000253 unsigned EntrySize = 0;
254 if (Kind.isMergeableCString()) {
255 if (Kind.isMergeable2ByteCString()) {
256 EntrySize = 2;
257 } else if (Kind.isMergeable4ByteCString()) {
258 EntrySize = 4;
259 } else {
260 EntrySize = 1;
261 assert(Kind.isMergeable1ByteCString() && "unknown string width");
262 }
263 } else if (Kind.isMergeableConst()) {
264 if (Kind.isMergeableConst4()) {
265 EntrySize = 4;
266 } else if (Kind.isMergeableConst8()) {
267 EntrySize = 8;
David Majnemer964b70d2016-02-22 22:23:11 +0000268 } else if (Kind.isMergeableConst16()) {
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000269 EntrySize = 16;
David Majnemer964b70d2016-02-22 22:23:11 +0000270 } else {
271 assert(Kind.isMergeableConst32() && "unknown data width");
272 EntrySize = 32;
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000273 }
274 }
275
Rafael Espindola9075f772015-02-20 23:28:28 +0000276 StringRef Group = "";
Peter Collingbourne67335642016-10-24 19:23:39 +0000277 if (const Comdat *C = getELFComdat(GO)) {
Rafael Espindola9075f772015-02-20 23:28:28 +0000278 Flags |= ELF::SHF_GROUP;
279 Group = C->getName();
280 }
281
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000282 bool UniqueSectionNames = TM.getUniqueSectionNames();
283 SmallString<128> Name;
Rafael Espindolaa05b3b72015-01-28 17:54:19 +0000284 if (Kind.isMergeableCString()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000285 // We also need alignment here.
286 // FIXME: this is getting the alignment of the character, not the
287 // alignment of the global!
Peter Collingbourne67335642016-10-24 19:23:39 +0000288 unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
289 cast<GlobalVariable>(GO));
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000290
Rafael Espindolaba31e272015-01-29 17:33:21 +0000291 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000292 Name = SizeSpec + utostr(Align);
293 } else if (Kind.isMergeableConst()) {
294 Name = ".rodata.cst";
295 Name += utostr(EntrySize);
296 } else {
297 Name = getSectionPrefixForGlobal(Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000298 }
Dehao Chen302b69c2016-10-18 20:42:47 +0000299
Peter Collingbourne67335642016-10-24 19:23:39 +0000300 if (const auto *F = dyn_cast<Function>(GO)) {
Dehao Chen302b69c2016-10-18 20:42:47 +0000301 const auto &OptionalPrefix = F->getSectionPrefix();
302 if (OptionalPrefix)
303 Name += *OptionalPrefix;
304 }
Dehao Chenf84b6302016-02-23 03:39:24 +0000305
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000306 if (EmitUniqueSection && UniqueSectionNames) {
307 Name.push_back('.');
Peter Collingbourne67335642016-10-24 19:23:39 +0000308 TM.getNameWithPrefix(Name, GO, Mang, true);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000309 }
Reid Kleckner97837b72016-05-02 23:22:18 +0000310 unsigned UniqueID = MCContext::GenericSectionID;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000311 if (EmitUniqueSection && !UniqueSectionNames) {
312 UniqueID = *NextUniqueID;
313 (*NextUniqueID)++;
314 }
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000315 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000316 EntrySize, Group, UniqueID);
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000317}
318
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000319MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000320 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000321 unsigned Flags = getELFSectionFlags(Kind);
322
323 // If we have -ffunction-section or -fdata-section then we should emit the
324 // global value to a uniqued section specifically for it.
325 bool EmitUniqueSection = false;
326 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
327 if (Kind.isText())
328 EmitUniqueSection = TM.getFunctionSections();
329 else
330 EmitUniqueSection = TM.getDataSections();
331 }
Peter Collingbourne67335642016-10-24 19:23:39 +0000332 EmitUniqueSection |= GO->hasComdat();
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000333
Peter Collingbourne67335642016-10-24 19:23:39 +0000334 return selectELFSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000335 EmitUniqueSection, Flags, &NextUniqueID);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000336}
337
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000338MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000339 const Function &F, const TargetMachine &TM) const {
Rafael Espindola29786d42015-02-12 17:16:46 +0000340 // If the function can be removed, produce a unique section so that
341 // the table doesn't prevent the removal.
342 const Comdat *C = F.getComdat();
343 bool EmitUniqueSection = TM.getFunctionSections() || C;
344 if (!EmitUniqueSection)
345 return ReadOnlySection;
346
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000347 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
Eric Christopher4367c7f2016-09-16 07:33:15 +0000348 getMangler(), TM, EmitUniqueSection, ELF::SHF_ALLOC,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000349 &NextUniqueID);
Rafael Espindola29786d42015-02-12 17:16:46 +0000350}
351
Rafael Espindoladf195192015-02-17 23:34:51 +0000352bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
353 bool UsesLabelDifference, const Function &F) const {
354 // We can always create relative relocations, so use another section
355 // that can be marked non-executable.
356 return false;
357}
358
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000359/// Given a mergeable constant with the specified size and relocation
360/// information, return a section that it should be placed in.
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000361MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
David Majnemera3ea4072016-02-21 01:30:30 +0000362 const DataLayout &DL, SectionKind Kind, const Constant *C,
363 unsigned &Align) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000364 if (Kind.isMergeableConst4() && MergeableConst4Section)
365 return MergeableConst4Section;
366 if (Kind.isMergeableConst8() && MergeableConst8Section)
367 return MergeableConst8Section;
368 if (Kind.isMergeableConst16() && MergeableConst16Section)
369 return MergeableConst16Section;
David Majnemer964b70d2016-02-22 22:23:11 +0000370 if (Kind.isMergeableConst32() && MergeableConst32Section)
371 return MergeableConst32Section;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000372 if (Kind.isReadOnly())
373 return ReadOnlySection;
374
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000375 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
376 return DataRelROSection;
377}
378
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000379static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
380 bool IsCtor, unsigned Priority,
381 const MCSymbol *KeySym) {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000382 std::string Name;
383 unsigned Type;
384 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
Rafael Espindolac4b42532014-09-04 23:03:58 +0000385 StringRef COMDAT = KeySym ? KeySym->getName() : "";
386
387 if (KeySym)
388 Flags |= ELF::SHF_GROUP;
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000389
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000390 if (UseInitArray) {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000391 if (IsCtor) {
392 Type = ELF::SHT_INIT_ARRAY;
393 Name = ".init_array";
394 } else {
395 Type = ELF::SHT_FINI_ARRAY;
396 Name = ".fini_array";
397 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000398 if (Priority != 65535) {
399 Name += '.';
400 Name += utostr(Priority);
401 }
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000402 } else {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000403 // The default scheme is .ctor / .dtor, so we have to invert the priority
404 // numbering.
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000405 if (IsCtor)
406 Name = ".ctors";
407 else
408 Name = ".dtors";
Rafael Espindolac4b42532014-09-04 23:03:58 +0000409 if (Priority != 65535) {
410 Name += '.';
411 Name += utostr(65535 - Priority);
412 }
413 Type = ELF::SHT_PROGBITS;
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000414 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000415
Rafael Espindolaba31e272015-01-29 17:33:21 +0000416 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000417}
418
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000419MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000420 unsigned Priority, const MCSymbol *KeySym) const {
421 return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
422 KeySym);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000423}
424
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000425MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000426 unsigned Priority, const MCSymbol *KeySym) const {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000427 return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
428 KeySym);
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000429}
430
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000431const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000432 const GlobalValue *LHS, const GlobalValue *RHS,
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000433 const TargetMachine &TM) const {
434 // We may only use a PLT-relative relocation to refer to unnamed_addr
435 // functions.
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000436 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000437 return nullptr;
438
439 // Basic sanity checks.
440 if (LHS->getType()->getPointerAddressSpace() != 0 ||
441 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
442 RHS->isThreadLocal())
443 return nullptr;
444
445 return MCBinaryExpr::createSub(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000446 MCSymbolRefExpr::create(TM.getSymbol(LHS, getMangler()), PLTRelativeVariantKind,
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000447 getContext()),
Eric Christopher4367c7f2016-09-16 07:33:15 +0000448 MCSymbolRefExpr::create(TM.getSymbol(RHS, getMangler()), getContext()),
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000449 getContext());
450}
451
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000452void
453TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
454 UseInitArray = UseInitArray_;
Rafael Espindola46fa2312016-08-29 12:33:42 +0000455 MCContext &Ctx = getContext();
456 if (!UseInitArray) {
457 StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
458 ELF::SHF_ALLOC | ELF::SHF_WRITE);
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000459
Rafael Espindola46fa2312016-08-29 12:33:42 +0000460 StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
461 ELF::SHF_ALLOC | ELF::SHF_WRITE);
462 return;
463 }
464
465 StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
466 ELF::SHF_WRITE | ELF::SHF_ALLOC);
467 StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
468 ELF::SHF_WRITE | ELF::SHF_ALLOC);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000469}
470
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000471//===----------------------------------------------------------------------===//
472// MachO
473//===----------------------------------------------------------------------===//
474
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000475TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
476 : TargetLoweringObjectFile() {
477 SupportIndirectSymViaGOTPCRel = true;
478}
479
Rafael Espindola46fa2312016-08-29 12:33:42 +0000480void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
481 const TargetMachine &TM) {
482 TargetLoweringObjectFile::Initialize(Ctx, TM);
Rafael Espindola412a5292016-08-29 12:47:22 +0000483 if (TM.getRelocationModel() == Reloc::Static) {
Rafael Espindola46fa2312016-08-29 12:33:42 +0000484 StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
485 SectionKind::getData());
486 StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
487 SectionKind::getData());
488 } else {
489 StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
490 MachO::S_MOD_INIT_FUNC_POINTERS,
491 SectionKind::getData());
492 StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
493 MachO::S_MOD_TERM_FUNC_POINTERS,
494 SectionKind::getData());
495 }
496}
497
Daniel Dunbar95856122013-01-18 19:37:00 +0000498/// emitModuleFlags - Perform code emission for module flags.
Eric Christopher4367c7f2016-09-16 07:33:15 +0000499void TargetLoweringObjectFileMachO::emitModuleFlags(
500 MCStreamer &Streamer, ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
501 const TargetMachine &TM) const {
Bill Wendling06df7722012-02-14 21:28:13 +0000502 unsigned VersionVal = 0;
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000503 unsigned ImageInfoFlags = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000504 MDNode *LinkerOptions = nullptr;
Bill Wendling734909a2012-02-15 22:36:15 +0000505 StringRef SectionVal;
Bill Wendling06df7722012-02-14 21:28:13 +0000506
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000507 for (const auto &MFE : ModuleFlags) {
Bill Wendling06df7722012-02-14 21:28:13 +0000508 // Ignore flags with 'Require' behavior.
Bill Wendling734909a2012-02-15 22:36:15 +0000509 if (MFE.Behavior == Module::Require)
Bill Wendling06df7722012-02-14 21:28:13 +0000510 continue;
511
Bill Wendling734909a2012-02-15 22:36:15 +0000512 StringRef Key = MFE.Key->getString();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000513 Metadata *Val = MFE.Val;
Bill Wendling06df7722012-02-14 21:28:13 +0000514
Daniel Dunbar95856122013-01-18 19:37:00 +0000515 if (Key == "Objective-C Image Info Version") {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000516 VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000517 } else if (Key == "Objective-C Garbage Collection" ||
518 Key == "Objective-C GC Only" ||
Manman Renc98ec0e2014-11-21 19:24:55 +0000519 Key == "Objective-C Is Simulated" ||
Manman Renc77e0ff2016-01-29 23:51:00 +0000520 Key == "Objective-C Class Properties" ||
Manman Renc98ec0e2014-11-21 19:24:55 +0000521 Key == "Objective-C Image Swift Version") {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000522 ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000523 } else if (Key == "Objective-C Image Info Section") {
Bill Wendling734909a2012-02-15 22:36:15 +0000524 SectionVal = cast<MDString>(Val)->getString();
Daniel Dunbar95856122013-01-18 19:37:00 +0000525 } else if (Key == "Linker Options") {
526 LinkerOptions = cast<MDNode>(Val);
527 }
528 }
529
530 // Emit the linker options if present.
531 if (LinkerOptions) {
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000532 for (const auto &Option : LinkerOptions->operands()) {
Daniel Dunbar95856122013-01-18 19:37:00 +0000533 SmallVector<std::string, 4> StrOptions;
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000534 for (const auto &Piece : cast<MDNode>(Option)->operands())
535 StrOptions.push_back(cast<MDString>(Piece)->getString());
Daniel Dunbar95856122013-01-18 19:37:00 +0000536 Streamer.EmitLinkerOptions(StrOptions);
537 }
Bill Wendling06df7722012-02-14 21:28:13 +0000538 }
539
Bill Wendling734909a2012-02-15 22:36:15 +0000540 // The section is mandatory. If we don't have it, then we don't have GC info.
541 if (SectionVal.empty()) return;
Bill Wendling06df7722012-02-14 21:28:13 +0000542
Bill Wendling734909a2012-02-15 22:36:15 +0000543 StringRef Segment, Section;
544 unsigned TAA = 0, StubSize = 0;
545 bool TAAParsed;
546 std::string ErrorCode =
547 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
548 TAA, TAAParsed, StubSize);
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000549 if (!ErrorCode.empty())
Bill Wendling734909a2012-02-15 22:36:15 +0000550 // If invalid, report the error with report_fatal_error.
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000551 report_fatal_error("Invalid section specifier '" + Section + "': " +
552 ErrorCode + ".");
Bill Wendling06df7722012-02-14 21:28:13 +0000553
Bill Wendling734909a2012-02-15 22:36:15 +0000554 // Get the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000555 MCSectionMachO *S = getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000556 Segment, Section, TAA, StubSize, SectionKind::getData());
Bill Wendling734909a2012-02-15 22:36:15 +0000557 Streamer.SwitchSection(S);
558 Streamer.EmitLabel(getContext().
Jim Grosbach6f482002015-05-18 18:43:14 +0000559 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendling06df7722012-02-14 21:28:13 +0000560 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000561 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendling06df7722012-02-14 21:28:13 +0000562 Streamer.AddBlankLine();
563}
564
David Majnemerdad0a642014-06-27 18:19:56 +0000565static void checkMachOComdat(const GlobalValue *GV) {
566 const Comdat *C = GV->getComdat();
567 if (!C)
568 return;
569
570 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
571 "' cannot be lowered.");
572}
573
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000574MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000575 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000576 // Parse the section specifier and create it if valid.
577 StringRef Segment, Section;
Stuart Hastings12d53122011-03-19 02:42:31 +0000578 unsigned TAA = 0, StubSize = 0;
579 bool TAAParsed;
David Majnemerdad0a642014-06-27 18:19:56 +0000580
Peter Collingbourne67335642016-10-24 19:23:39 +0000581 checkMachOComdat(GO);
David Majnemerdad0a642014-06-27 18:19:56 +0000582
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000583 std::string ErrorCode =
Peter Collingbourne67335642016-10-24 19:23:39 +0000584 MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000585 TAA, TAAParsed, StubSize);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000586 if (!ErrorCode.empty()) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000587 // If invalid, report the error with report_fatal_error.
Peter Collingbourne67335642016-10-24 19:23:39 +0000588 report_fatal_error("Global variable '" + GO->getName() +
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000589 "' has an invalid section specifier '" +
Peter Collingbourne67335642016-10-24 19:23:39 +0000590 GO->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000591 }
592
593 // Get the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000594 MCSectionMachO *S =
595 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000596
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000597 // If TAA wasn't set by ParseSectionSpecifier() above,
598 // use the value returned by getMachOSection() as a default.
Stuart Hastings12d53122011-03-19 02:42:31 +0000599 if (!TAAParsed)
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000600 TAA = S->getTypeAndAttributes();
601
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000602 // Okay, now that we got the section, verify that the TAA & StubSize agree.
603 // If the user declared multiple globals with different section flags, we need
604 // to reject it here.
605 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000606 // If invalid, report the error with report_fatal_error.
Peter Collingbourne67335642016-10-24 19:23:39 +0000607 report_fatal_error("Global variable '" + GO->getName() +
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000608 "' section type or attributes does not match previous"
609 " section specifier");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000610 }
611
612 return S;
613}
614
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000615MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000616 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
617 checkMachOComdat(GO);
Bill Wendling25b61db2013-11-17 10:53:13 +0000618
619 // Handle thread local data.
620 if (Kind.isThreadBSS()) return TLSBSSSection;
621 if (Kind.isThreadData()) return TLSDataSection;
622
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000623 if (Kind.isText())
Peter Collingbourne67335642016-10-24 19:23:39 +0000624 return GO->isWeakForLinker() ? TextCoalSection : TextSection;
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000625
626 // If this is weak/linkonce, put this in a coalescable section, either in text
627 // or data depending on if it is writable.
Peter Collingbourne67335642016-10-24 19:23:39 +0000628 if (GO->isWeakForLinker()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000629 if (Kind.isReadOnly())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000630 return ConstTextCoalSection;
631 return DataCoalSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000632 }
633
634 // FIXME: Alignment check should be handled by section classifier.
Chris Lattneref2f8042010-03-07 04:28:09 +0000635 if (Kind.isMergeable1ByteCString() &&
Peter Collingbourne67335642016-10-24 19:23:39 +0000636 GO->getParent()->getDataLayout().getPreferredAlignment(
637 cast<GlobalVariable>(GO)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000638 return CStringSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000639
Chris Lattneref2f8042010-03-07 04:28:09 +0000640 // Do not put 16-bit arrays in the UString section if they have an
641 // externally visible label, this runs into issues with certain linker
642 // versions.
Peter Collingbourne67335642016-10-24 19:23:39 +0000643 if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
644 GO->getParent()->getDataLayout().getPreferredAlignment(
645 cast<GlobalVariable>(GO)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000646 return UStringSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000647
Rafael Espindolab43d51d2014-08-28 20:13:31 +0000648 // With MachO only variables whose corresponding symbol starts with 'l' or
649 // 'L' can be merged, so we only try merging GVs with private linkage.
Peter Collingbourne67335642016-10-24 19:23:39 +0000650 if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000651 if (Kind.isMergeableConst4())
652 return FourByteConstantSection;
653 if (Kind.isMergeableConst8())
654 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000655 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000656 return SixteenByteConstantSection;
657 }
658
659 // Otherwise, if it is readonly, but not something we can specially optimize,
660 // just drop it in .const.
661 if (Kind.isReadOnly())
662 return ReadOnlySection;
663
664 // If this is marked const, put it into a const section. But if the dynamic
665 // linker needs to write to it, put it in the data segment.
666 if (Kind.isReadOnlyWithRel())
667 return ConstDataSection;
668
669 // Put zero initialized globals with strong external linkage in the
670 // DATA, __common section with the .zerofill directive.
671 if (Kind.isBSSExtern())
672 return DataCommonSection;
673
674 // Put zero initialized globals with local linkage in __DATA,__bss directive
675 // with the .zerofill directive (aka .lcomm).
676 if (Kind.isBSSLocal())
677 return DataBSSSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000678
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000679 // Otherwise, just drop the variable in the normal data section.
680 return DataSection;
681}
682
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000683MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
David Majnemera3ea4072016-02-21 01:30:30 +0000684 const DataLayout &DL, SectionKind Kind, const Constant *C,
685 unsigned &Align) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000686 // If this constant requires a relocation, we have to put it in the data
687 // segment, not in the text segment.
Rafael Espindola449711c2015-11-18 06:02:15 +0000688 if (Kind.isData() || Kind.isReadOnlyWithRel())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000689 return ConstDataSection;
690
691 if (Kind.isMergeableConst4())
692 return FourByteConstantSection;
693 if (Kind.isMergeableConst8())
694 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000695 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000696 return SixteenByteConstantSection;
697 return ReadOnlySection; // .const
698}
699
Rafael Espindola15b26692014-02-09 14:50:44 +0000700const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000701 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
702 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000703 // The mach-o version of this method defaults to returning a stub reference.
704
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000705 if (Encoding & DW_EH_PE_indirect) {
706 MachineModuleInfoMachO &MachOMMI =
707 MMI->getObjFileInfo<MachineModuleInfoMachO>();
708
Eric Christopher4367c7f2016-09-16 07:33:15 +0000709 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000710
711 // Add information about the stub reference to MachOMMI so that the stub
712 // gets emitted by the asmprinter.
Rafael Espindola712f9572016-05-17 16:01:32 +0000713 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000714 if (!StubSym.getPointer()) {
Eric Christopher4367c7f2016-09-16 07:33:15 +0000715 MCSymbol *Sym = TM.getSymbol(GV, getMangler());
Chris Lattner2ea586b2010-03-15 20:37:38 +0000716 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000717 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000718
719 return TargetLoweringObjectFile::
Jim Grosbach13760bd2015-05-30 01:25:56 +0000720 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Anton Korobeynikove42af362012-11-14 01:47:00 +0000721 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000722 }
723
Eric Christopher4367c7f2016-09-16 07:33:15 +0000724 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
725 MMI, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000726}
727
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000728MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000729 const GlobalValue *GV, const TargetMachine &TM,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000730 MachineModuleInfo *MMI) const {
Rafael Espindola08704342011-04-27 23:08:15 +0000731 // The mach-o version of this method defaults to returning a stub reference.
732 MachineModuleInfoMachO &MachOMMI =
733 MMI->getObjFileInfo<MachineModuleInfoMachO>();
734
Eric Christopher4367c7f2016-09-16 07:33:15 +0000735 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
Rafael Espindola08704342011-04-27 23:08:15 +0000736
737 // Add information about the stub reference to MachOMMI so that the stub
738 // gets emitted by the asmprinter.
Bill Wendlinge4cc3322011-11-29 01:43:20 +0000739 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000740 if (!StubSym.getPointer()) {
Eric Christopher4367c7f2016-09-16 07:33:15 +0000741 MCSymbol *Sym = TM.getSymbol(GV, getMangler());
Rafael Espindola08704342011-04-27 23:08:15 +0000742 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
743 }
744
745 return SSym;
746}
747
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000748const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
749 const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
750 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000751 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000752 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
753 // through a non_lazy_ptr stub instead. One advantage is that it allows the
754 // computation of deltas to final external symbols. Example:
755 //
756 // _extgotequiv:
757 // .long _extfoo
758 //
759 // _delta:
760 // .long _extgotequiv-_delta
761 //
762 // is transformed to:
763 //
764 // _delta:
765 // .long L_extfoo$non_lazy_ptr-(_delta+0)
766 //
767 // .section __IMPORT,__pointers,non_lazy_symbol_pointers
768 // L_extfoo$non_lazy_ptr:
769 // .indirect_symbol _extfoo
770 // .long 0
771 //
772 MachineModuleInfoMachO &MachOMMI =
773 MMI->getObjFileInfo<MachineModuleInfoMachO>();
774 MCContext &Ctx = getContext();
775
776 // The offset must consider the original displacement from the base symbol
777 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
778 Offset = -MV.getConstant();
779 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
780
781 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
782 // non_lazy_ptr stubs.
783 SmallString<128> Name;
784 StringRef Suffix = "$non_lazy_ptr";
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000785 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000786 Name += Sym->getName();
787 Name += Suffix;
Jim Grosbach6f482002015-05-18 18:43:14 +0000788 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000789
790 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
791 if (!StubSym.getPointer())
792 StubSym = MachineModuleInfoImpl::
793 StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */);
794
795 const MCExpr *BSymExpr =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000796 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000797 const MCExpr *LHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000798 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000799
800 if (!Offset)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000801 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000802
803 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000804 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
805 return MCBinaryExpr::createSub(LHS, RHS, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000806}
807
Peter Collingbourne94d77862015-11-03 23:40:03 +0000808static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
809 const MCSection &Section) {
810 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
811 return true;
812
813 // If it is not dead stripped, it is safe to use private labels.
814 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
815 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
816 return true;
817
818 return false;
819}
820
821void TargetLoweringObjectFileMachO::getNameWithPrefix(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000822 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
Peter Collingbourne94d77862015-11-03 23:40:03 +0000823 const TargetMachine &TM) const {
Peter Collingbourne67335642016-10-24 19:23:39 +0000824 bool CannotUsePrivateLabel = true;
825 if (auto *GO = GV->getBaseObject()) {
826 SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM);
827 const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
828 CannotUsePrivateLabel =
829 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
830 }
Eric Christopher4367c7f2016-09-16 07:33:15 +0000831 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
Peter Collingbourne94d77862015-11-03 23:40:03 +0000832}
833
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000834//===----------------------------------------------------------------------===//
835// COFF
836//===----------------------------------------------------------------------===//
837
Chris Lattner87cffa92010-05-07 17:17:41 +0000838static unsigned
Renato Golinef3eb062016-06-27 14:42:20 +0000839getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) {
Chris Lattner87cffa92010-05-07 17:17:41 +0000840 unsigned Flags = 0;
Renato Golinef3eb062016-06-27 14:42:20 +0000841 bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
Chris Lattner87cffa92010-05-07 17:17:41 +0000842
Anton Korobeynikove4152302010-07-06 15:24:56 +0000843 if (K.isMetadata())
Chris Lattner02844932010-05-07 21:49:09 +0000844 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000845 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000846 else if (K.isText())
847 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000848 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer0f83d962010-10-27 18:52:29 +0000849 COFF::IMAGE_SCN_MEM_READ |
Renato Golinef3eb062016-06-27 14:42:20 +0000850 COFF::IMAGE_SCN_CNT_CODE |
851 (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0);
David Majnemerb8dbebb2014-09-20 07:31:46 +0000852 else if (K.isBSS())
Chris Lattner02844932010-05-07 21:49:09 +0000853 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000854 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
855 COFF::IMAGE_SCN_MEM_READ |
856 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000857 else if (K.isThreadLocal())
858 Flags |=
859 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
860 COFF::IMAGE_SCN_MEM_READ |
861 COFF::IMAGE_SCN_MEM_WRITE;
David Majnemerb8dbebb2014-09-20 07:31:46 +0000862 else if (K.isReadOnly() || K.isReadOnlyWithRel())
Chris Lattner87cffa92010-05-07 17:17:41 +0000863 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000864 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
865 COFF::IMAGE_SCN_MEM_READ;
Chris Lattner87cffa92010-05-07 17:17:41 +0000866 else if (K.isWriteable())
867 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000868 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
869 COFF::IMAGE_SCN_MEM_READ |
870 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000871
872 return Flags;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000873}
874
Benjamin Kramer6cbe6702014-07-07 14:47:51 +0000875static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
David Majnemerdad0a642014-06-27 18:19:56 +0000876 const Comdat *C = GV->getComdat();
877 assert(C && "expected GV to have a Comdat!");
878
879 StringRef ComdatGVName = C->getName();
880 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
881 if (!ComdatGV)
882 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
883 "' does not exist.");
884
885 if (ComdatGV->getComdat() != C)
886 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
Hans Wennborgc0f0c512014-09-19 01:14:56 +0000887 "' is not a key for its COMDAT.");
David Majnemerdad0a642014-06-27 18:19:56 +0000888
889 return ComdatGV;
890}
891
892static int getSelectionForCOFF(const GlobalValue *GV) {
893 if (const Comdat *C = GV->getComdat()) {
894 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
895 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
896 ComdatKey = GA->getBaseObject();
897 if (ComdatKey == GV) {
898 switch (C->getSelectionKind()) {
899 case Comdat::Any:
900 return COFF::IMAGE_COMDAT_SELECT_ANY;
901 case Comdat::ExactMatch:
902 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
903 case Comdat::Largest:
904 return COFF::IMAGE_COMDAT_SELECT_LARGEST;
905 case Comdat::NoDuplicates:
906 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
907 case Comdat::SameSize:
908 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
909 }
910 } else {
911 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
912 }
David Majnemerdad0a642014-06-27 18:19:56 +0000913 }
914 return 0;
915}
916
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000917MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000918 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000919 int Selection = 0;
Renato Golinef3eb062016-06-27 14:42:20 +0000920 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Peter Collingbourne67335642016-10-24 19:23:39 +0000921 StringRef Name = GO->getSection();
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000922 StringRef COMDATSymName = "";
Peter Collingbourne67335642016-10-24 19:23:39 +0000923 if (GO->hasComdat()) {
924 Selection = getSelectionForCOFF(GO);
David Majnemerdad0a642014-06-27 18:19:56 +0000925 const GlobalValue *ComdatGV;
926 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
Peter Collingbourne67335642016-10-24 19:23:39 +0000927 ComdatGV = getComdatGVForCOFF(GO);
David Majnemerdad0a642014-06-27 18:19:56 +0000928 else
Peter Collingbourne67335642016-10-24 19:23:39 +0000929 ComdatGV = GO;
David Majnemerdad0a642014-06-27 18:19:56 +0000930
931 if (!ComdatGV->hasPrivateLinkage()) {
Eric Christopher4367c7f2016-09-16 07:33:15 +0000932 MCSymbol *Sym = TM.getSymbol(ComdatGV, getMangler());
David Majnemerdad0a642014-06-27 18:19:56 +0000933 COMDATSymName = Sym->getName();
934 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
935 } else {
936 Selection = 0;
937 }
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000938 }
Reid Kleckner97837b72016-05-02 23:22:18 +0000939
940 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
Nico Riecka37acf72013-07-06 12:13:10 +0000941 Selection);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000942}
943
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000944static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000945 if (Kind.isText())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000946 return ".text";
David Majnemera9bdb322014-04-08 22:33:40 +0000947 if (Kind.isBSS())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000948 return ".bss";
949 if (Kind.isThreadLocal())
Rafael Espindola3c8e1472013-11-27 15:52:11 +0000950 return ".tls$";
David Majnemer597be2d2014-09-22 20:39:23 +0000951 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
952 return ".rdata";
953 return ".data";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000954}
955
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000956MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000957 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
David Majnemer93389842014-03-23 17:47:39 +0000958 // If we have -ffunction-sections then we should emit the global value to a
959 // uniqued section specifically for it.
David Majnemer273bff42014-03-25 06:14:26 +0000960 bool EmitUniquedSection;
961 if (Kind.isText())
962 EmitUniquedSection = TM.getFunctionSections();
963 else
964 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000965
Peter Collingbourne67335642016-10-24 19:23:39 +0000966 if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000967 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Renato Golinef3eb062016-06-27 14:42:20 +0000968 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Chris Lattner02844932010-05-07 21:49:09 +0000969
Daniel Dunbar329d2022010-07-01 20:07:24 +0000970 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Peter Collingbourne67335642016-10-24 19:23:39 +0000971 int Selection = getSelectionForCOFF(GO);
David Majnemerdad0a642014-06-27 18:19:56 +0000972 if (!Selection)
973 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
974 const GlobalValue *ComdatGV;
Peter Collingbourne67335642016-10-24 19:23:39 +0000975 if (GO->hasComdat())
976 ComdatGV = getComdatGVForCOFF(GO);
David Majnemerdad0a642014-06-27 18:19:56 +0000977 else
Peter Collingbourne67335642016-10-24 19:23:39 +0000978 ComdatGV = GO;
David Majnemerdad0a642014-06-27 18:19:56 +0000979
Reid Kleckner97837b72016-05-02 23:22:18 +0000980 unsigned UniqueID = MCContext::GenericSectionID;
981 if (EmitUniquedSection)
982 UniqueID = NextUniqueID++;
983
David Majnemerdad0a642014-06-27 18:19:56 +0000984 if (!ComdatGV->hasPrivateLinkage()) {
Eric Christopher4367c7f2016-09-16 07:33:15 +0000985 MCSymbol *Sym = TM.getSymbol(ComdatGV, getMangler());
David Majnemerdad0a642014-06-27 18:19:56 +0000986 StringRef COMDATSymName = Sym->getName();
987 return getContext().getCOFFSection(Name, Characteristics, Kind,
Reid Kleckner97837b72016-05-02 23:22:18 +0000988 COMDATSymName, Selection, UniqueID);
David Majnemer7db449a2015-03-17 23:54:51 +0000989 } else {
990 SmallString<256> TmpData;
Peter Collingbourne67335642016-10-24 19:23:39 +0000991 getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
David Majnemer7db449a2015-03-17 23:54:51 +0000992 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
Reid Kleckner97837b72016-05-02 23:22:18 +0000993 Selection, UniqueID);
David Majnemerdad0a642014-06-27 18:19:56 +0000994 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000995 }
996
997 if (Kind.isText())
David Majnemer3d96acb2013-08-13 01:23:53 +0000998 return TextSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000999
Anton Korobeynikovc6b40172012-02-11 17:26:53 +00001000 if (Kind.isThreadLocal())
David Majnemer3d96acb2013-08-13 01:23:53 +00001001 return TLSDataSection;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +00001002
David Majnemer597be2d2014-09-22 20:39:23 +00001003 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
David Majnemerf76d6b32013-08-08 01:50:52 +00001004 return ReadOnlySection;
1005
David Majnemera9bdb322014-04-08 22:33:40 +00001006 // Note: we claim that common symbols are put in BSSSection, but they are
1007 // really emitted with the magic .comm directive, which creates a symbol table
1008 // entry but not a section.
1009 if (Kind.isBSS() || Kind.isCommon())
David Majnemer3d96acb2013-08-13 01:23:53 +00001010 return BSSSection;
1011
1012 return DataSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001013}
1014
David Majnemer7db449a2015-03-17 23:54:51 +00001015void TargetLoweringObjectFileCOFF::getNameWithPrefix(
Eric Christopher4367c7f2016-09-16 07:33:15 +00001016 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
Peter Collingbourne94d77862015-11-03 23:40:03 +00001017 const TargetMachine &TM) const {
1018 bool CannotUsePrivateLabel = false;
David Majnemer7db449a2015-03-17 23:54:51 +00001019 if (GV->hasPrivateLinkage() &&
1020 ((isa<Function>(GV) && TM.getFunctionSections()) ||
1021 (isa<GlobalVariable>(GV) && TM.getDataSections())))
1022 CannotUsePrivateLabel = true;
1023
Eric Christopher4367c7f2016-09-16 07:33:15 +00001024 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
David Majnemer7db449a2015-03-17 23:54:51 +00001025}
1026
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001027MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
Eric Christopher4367c7f2016-09-16 07:33:15 +00001028 const Function &F, const TargetMachine &TM) const {
Rafael Espindolaab447e42015-03-11 19:58:37 +00001029 // If the function can be removed, produce a unique section so that
1030 // the table doesn't prevent the removal.
1031 const Comdat *C = F.getComdat();
1032 bool EmitUniqueSection = TM.getFunctionSections() || C;
1033 if (!EmitUniqueSection)
1034 return ReadOnlySection;
1035
1036 // FIXME: we should produce a symbol for F instead.
1037 if (F.hasPrivateLinkage())
1038 return ReadOnlySection;
1039
Eric Christopher4367c7f2016-09-16 07:33:15 +00001040 MCSymbol *Sym = TM.getSymbol(&F, getMangler());
Rafael Espindolaab447e42015-03-11 19:58:37 +00001041 StringRef COMDATSymName = Sym->getName();
1042
1043 SectionKind Kind = SectionKind::getReadOnly();
1044 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Renato Golinef3eb062016-06-27 14:42:20 +00001045 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Rafael Espindolaab447e42015-03-11 19:58:37 +00001046 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Reid Kleckner97837b72016-05-02 23:22:18 +00001047 unsigned UniqueID = NextUniqueID++;
Rafael Espindolaab447e42015-03-11 19:58:37 +00001048
1049 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
Reid Kleckner97837b72016-05-02 23:22:18 +00001050 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
Rafael Espindolaab447e42015-03-11 19:58:37 +00001051}
1052
Eric Christopher4367c7f2016-09-16 07:33:15 +00001053void TargetLoweringObjectFileCOFF::emitModuleFlags(
1054 MCStreamer &Streamer, ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
1055 const TargetMachine &TM) const {
Craig Topperc0196b12014-04-14 00:51:57 +00001056 MDNode *LinkerOptions = nullptr;
Reid Klecknerd973ca32013-04-25 19:34:41 +00001057
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +00001058 for (const auto &MFE : ModuleFlags) {
Reid Klecknerd973ca32013-04-25 19:34:41 +00001059 StringRef Key = MFE.Key->getString();
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +00001060 if (Key == "Linker Options")
1061 LinkerOptions = cast<MDNode>(MFE.Val);
Reid Klecknerd973ca32013-04-25 19:34:41 +00001062 }
Reid Klecknerd973ca32013-04-25 19:34:41 +00001063
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +00001064 if (LinkerOptions) {
1065 // Emit the linker options to the linker .drectve section. According to the
1066 // spec, this section is a space-separated string containing flags for
1067 // linker.
1068 MCSection *Sec = getDrectveSection();
1069 Streamer.SwitchSection(Sec);
1070 for (const auto &Option : LinkerOptions->operands()) {
1071 for (const auto &Piece : cast<MDNode>(Option)->operands()) {
1072 // Lead with a space for consistency with our dllexport implementation.
1073 std::string Directive(" ");
1074 Directive.append(cast<MDString>(Piece)->getString());
1075 Streamer.EmitBytes(Directive);
1076 }
Reid Klecknerd973ca32013-04-25 19:34:41 +00001077 }
1078 }
1079}
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001080
Rafael Espindola46fa2312016-08-29 12:33:42 +00001081void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1082 const TargetMachine &TM) {
1083 TargetLoweringObjectFile::Initialize(Ctx, TM);
1084 const Triple &T = TM.getTargetTriple();
1085 if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1086 StaticCtorSection =
1087 Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1088 COFF::IMAGE_SCN_MEM_READ,
1089 SectionKind::getReadOnly());
1090 StaticDtorSection =
1091 Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1092 COFF::IMAGE_SCN_MEM_READ,
1093 SectionKind::getReadOnly());
1094 } else {
1095 StaticCtorSection = Ctx.getCOFFSection(
1096 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1097 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1098 SectionKind::getData());
1099 StaticDtorSection = Ctx.getCOFFSection(
1100 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1101 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1102 SectionKind::getData());
1103 }
1104}
1105
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001106MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +00001107 unsigned Priority, const MCSymbol *KeySym) const {
Reid Kleckner7c4059e2014-09-04 17:42:03 +00001108 return getContext().getAssociativeCOFFSection(
Reid Kleckner97837b72016-05-02 23:22:18 +00001109 cast<MCSectionCOFF>(StaticCtorSection), KeySym, 0);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001110}
1111
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001112MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +00001113 unsigned Priority, const MCSymbol *KeySym) const {
Reid Kleckner7c4059e2014-09-04 17:42:03 +00001114 return getContext().getAssociativeCOFFSection(
Reid Kleckner97837b72016-05-02 23:22:18 +00001115 cast<MCSectionCOFF>(StaticDtorSection), KeySym, 0);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001116}
Peter Collingbourneaef36592015-06-29 22:04:09 +00001117
1118void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
Eric Christopher4367c7f2016-09-16 07:33:15 +00001119 raw_ostream &OS, const GlobalValue *GV) const {
Peter Collingbourneaef36592015-06-29 22:04:09 +00001120 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
1121 return;
1122
1123 const Triple &TT = getTargetTriple();
1124
1125 if (TT.isKnownWindowsMSVCEnvironment())
1126 OS << " /EXPORT:";
1127 else
1128 OS << " -export:";
1129
1130 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
1131 std::string Flag;
1132 raw_string_ostream FlagOS(Flag);
Eric Christopher4367c7f2016-09-16 07:33:15 +00001133 getMangler().getNameWithPrefix(FlagOS, GV, false);
Peter Collingbourneaef36592015-06-29 22:04:09 +00001134 FlagOS.flush();
Mehdi Amini5c0fa582015-07-16 06:04:17 +00001135 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
Peter Collingbourneaef36592015-06-29 22:04:09 +00001136 OS << Flag.substr(1);
1137 else
1138 OS << Flag;
1139 } else {
Eric Christopher4367c7f2016-09-16 07:33:15 +00001140 getMangler().getNameWithPrefix(OS, GV, false);
Peter Collingbourneaef36592015-06-29 22:04:09 +00001141 }
1142
1143 if (!GV->getValueType()->isFunctionTy()) {
1144 if (TT.isKnownWindowsMSVCEnvironment())
1145 OS << ",DATA";
1146 else
1147 OS << ",data";
1148 }
1149}