blob: 0ef10bfa4407b1ad3d032c2c500922782b8fc332 [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"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000027#include "llvm/MC/MCContext.h"
28#include "llvm/MC/MCExpr.h"
Chris Lattner87cffa92010-05-07 17:17:41 +000029#include "llvm/MC/MCSectionCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/MC/MCSectionELF.h"
31#include "llvm/MC/MCSectionMachO.h"
Rafael Espindolaa83b1772011-04-16 03:51:21 +000032#include "llvm/MC/MCStreamer.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000033#include "llvm/MC/MCSymbol.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000034#include "llvm/Support/Dwarf.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000035#include "llvm/Support/ELF.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
Rafael Espindoladaeafb42014-02-19 17:23:20 +000038#include "llvm/Target/TargetLowering.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000039#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000040#include "llvm/Target/TargetSubtargetInfo.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000041using namespace llvm;
Anton Korobeynikov31a92122010-02-21 20:28:15 +000042using namespace dwarf;
Anton Korobeynikovab663a02010-02-15 22:37:53 +000043
44//===----------------------------------------------------------------------===//
45// ELF
46//===----------------------------------------------------------------------===//
Anton Korobeynikovab663a02010-02-15 22:37:53 +000047
Rafael Espindoladaeafb42014-02-19 17:23:20 +000048MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
49 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
50 MachineModuleInfo *MMI) const {
Rafael Espindolace83fc32011-04-27 23:17:57 +000051 unsigned Encoding = getPersonalityEncoding();
Logan Chienc0029812014-05-30 16:48:56 +000052 if ((Encoding & 0x80) == dwarf::DW_EH_PE_indirect)
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000053 return getContext().GetOrCreateSymbol(StringRef("DW.ref.") +
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000054 TM.getSymbol(GV, Mang)->getName());
Logan Chienc0029812014-05-30 16:48:56 +000055 if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr)
56 return TM.getSymbol(GV, Mang);
57 report_fatal_error("We do not support this DWARF encoding yet!");
Rafael Espindolaa83b1772011-04-16 03:51:21 +000058}
59
60void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
61 const TargetMachine &TM,
Rafael Espindola08704342011-04-27 23:08:15 +000062 const MCSymbol *Sym) const {
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000063 SmallString<64> NameData("DW.ref.");
64 NameData += Sym->getName();
65 MCSymbol *Label = getContext().GetOrCreateSymbol(NameData);
Rafael Espindola39897762011-04-27 21:29:52 +000066 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
67 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000068 StringRef Prefix = ".data.";
69 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
Rafael Espindola39897762011-04-27 21:29:52 +000070 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
71 const MCSection *Sec = getContext().getELFSection(NameData,
72 ELF::SHT_PROGBITS,
73 Flags,
Rafael Espindola39897762011-04-27 21:29:52 +000074 0, Label->getName());
Eric Christopher8b770652015-01-26 19:03:15 +000075 unsigned Size = TM.getDataLayout()->getPointerSize();
Rafael Espindola39897762011-04-27 21:29:52 +000076 Streamer.SwitchSection(Sec);
Eric Christopher8b770652015-01-26 19:03:15 +000077 Streamer.EmitValueToAlignment(TM.getDataLayout()->getPointerABIAlignment());
Rafael Espindola39897762011-04-27 21:29:52 +000078 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
David Chisnall8fa17162012-02-17 16:05:50 +000079 const MCExpr *E = MCConstantExpr::Create(Size, getContext());
Rafael Espindola39897762011-04-27 21:29:52 +000080 Streamer.EmitELFSize(Label, E);
81 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::
105 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
106 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
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000121 if (Name.empty() || Name[0] != '.') return K;
122
123 // Some lame default implementation based on some magic section names.
124 if (Name == ".bss" ||
125 Name.startswith(".bss.") ||
126 Name.startswith(".gnu.linkonce.b.") ||
127 Name.startswith(".llvm.linkonce.b.") ||
128 Name == ".sbss" ||
129 Name.startswith(".sbss.") ||
130 Name.startswith(".gnu.linkonce.sb.") ||
131 Name.startswith(".llvm.linkonce.sb."))
132 return SectionKind::getBSS();
133
134 if (Name == ".tdata" ||
135 Name.startswith(".tdata.") ||
136 Name.startswith(".gnu.linkonce.td.") ||
137 Name.startswith(".llvm.linkonce.td."))
138 return SectionKind::getThreadData();
139
140 if (Name == ".tbss" ||
141 Name.startswith(".tbss.") ||
142 Name.startswith(".gnu.linkonce.tb.") ||
143 Name.startswith(".llvm.linkonce.tb."))
144 return SectionKind::getThreadBSS();
145
146 return K;
147}
148
149
150static unsigned getELFSectionType(StringRef Name, SectionKind K) {
151
152 if (Name == ".init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000153 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000154
155 if (Name == ".fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000156 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000157
158 if (Name == ".preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000159 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000160
161 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolaaea49582011-01-23 04:28:49 +0000162 return ELF::SHT_NOBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000163
Rafael Espindolaaea49582011-01-23 04:28:49 +0000164 return ELF::SHT_PROGBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000165}
166
167
168static unsigned
Rafael Espindolaa092f172015-02-04 21:27:24 +0000169getELFSectionFlags(SectionKind K, bool InCOMDAT) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000170 unsigned Flags = 0;
171
172 if (!K.isMetadata())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000173 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000174
175 if (K.isText())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000176 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000177
Rafael Espindolac85e0d82011-06-07 23:26:45 +0000178 if (K.isWriteable())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000179 Flags |= ELF::SHF_WRITE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000180
181 if (K.isThreadLocal())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000182 Flags |= ELF::SHF_TLS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000183
Rafael Espindolaf8d662a2015-02-05 14:57:47 +0000184 // FIXME: There is nothing in ELF preventing an SHF_MERGE from being
185 // in a comdat. We just avoid it for now because we don't print
186 // those .sections correctly.
Rafael Espindolaa092f172015-02-04 21:27:24 +0000187 if (!InCOMDAT && (K.isMergeableCString() || K.isMergeableConst()))
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000188 Flags |= ELF::SHF_MERGE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000189
190 if (K.isMergeableCString())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000191 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000192
193 return Flags;
194}
195
David Majnemerdad0a642014-06-27 18:19:56 +0000196static const Comdat *getELFComdat(const GlobalValue *GV) {
197 const Comdat *C = GV->getComdat();
198 if (!C)
199 return nullptr;
200
201 if (C->getSelectionKind() != Comdat::Any)
202 report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
203 C->getName() + "' cannot be lowered.");
204
205 return C;
206}
207
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000208const MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
209 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
210 const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000211 StringRef SectionName = GV->getSection();
212
213 // Infer section flags from the section name if we can.
214 Kind = getELFKindForNamedSection(SectionName, Kind);
215
David Majnemerdad0a642014-06-27 18:19:56 +0000216 StringRef Group = "";
Rafael Espindolaa092f172015-02-04 21:27:24 +0000217 unsigned Flags = getELFSectionFlags(Kind, GV->hasComdat());
David Majnemerdad0a642014-06-27 18:19:56 +0000218 if (const Comdat *C = getELFComdat(GV)) {
219 Group = C->getName();
220 Flags |= ELF::SHF_GROUP;
221 }
Chris Lattner80c34592010-04-08 21:34:17 +0000222 return getContext().getELFSection(SectionName,
David Majnemerdad0a642014-06-27 18:19:56 +0000223 getELFSectionType(SectionName, Kind), Flags,
Rafael Espindolaba31e272015-01-29 17:33:21 +0000224 /*EntrySize=*/0, Group);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000225}
226
Rafael Espindola25d2c202015-02-11 14:44:17 +0000227/// Return the section prefix name used by options FunctionsSections and
228/// DataSections.
David Majnemer102ff692014-06-24 16:01:53 +0000229static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
Rafael Espindola25d2c202015-02-11 14:44:17 +0000230 if (Kind.isText())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000231 return ".text";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000232 if (Kind.isReadOnly())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000233 return ".rodata";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000234 if (Kind.isBSS())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000235 return ".bss";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000236 if (Kind.isThreadData())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000237 return ".tdata";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000238 if (Kind.isThreadBSS())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000239 return ".tbss";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000240 if (Kind.isDataNoRel())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000241 return ".data";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000242 if (Kind.isDataRelLocal())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000243 return ".data.rel.local";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000244 if (Kind.isDataRel())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000245 return ".data.rel";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000246 if (Kind.isReadOnlyWithRelLocal())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000247 return ".data.rel.ro.local";
Chris Lattner5b212a32010-04-13 00:36:43 +0000248 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Rafael Espindola68fa2492015-02-17 20:48:01 +0000249 return ".data.rel.ro";
Chris Lattner5b212a32010-04-13 00:36:43 +0000250}
251
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000252const MCSection *TargetLoweringObjectFileELF::
253SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000254 Mangler &Mang, const TargetMachine &TM) const {
Rafael Espindolaa092f172015-02-04 21:27:24 +0000255 unsigned Flags = getELFSectionFlags(Kind, GV->hasComdat());
Rafael Espindolafad39012015-01-29 12:43:28 +0000256
Chris Lattner5b212a32010-04-13 00:36:43 +0000257 // If we have -ffunction-section or -fdata-section then we should emit the
258 // global value to a uniqued section specifically for it.
Rafael Espindolafad39012015-01-29 12:43:28 +0000259 bool EmitUniquedSection = false;
260 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
261 if (Kind.isText())
262 EmitUniquedSection = TM.getFunctionSections();
263 else
264 EmitUniquedSection = TM.getDataSections();
265 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000266
Rafael Espindolafad39012015-01-29 12:43:28 +0000267 if (EmitUniquedSection || GV->hasComdat()) {
David Majnemer102ff692014-06-24 16:01:53 +0000268 StringRef Prefix = getSectionPrefixForGlobal(Kind);
Chris Lattner5b212a32010-04-13 00:36:43 +0000269
David Majnemer102ff692014-06-24 16:01:53 +0000270 SmallString<128> Name(Prefix);
Rafael Espindola68fa2492015-02-17 20:48:01 +0000271 bool UniqueSectionNames = TM.getUniqueSectionNames();
272 if (UniqueSectionNames) {
273 Name.push_back('.');
274 TM.getNameWithPrefix(Name, GV, Mang, true);
275 }
Rafael Espindola70d80152011-02-14 22:23:49 +0000276 StringRef Group = "";
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000277 if (const Comdat *C = getELFComdat(GV)) {
Rafael Espindola70d80152011-02-14 22:23:49 +0000278 Flags |= ELF::SHF_GROUP;
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000279 Group = C->getName();
Rafael Espindola70d80152011-02-14 22:23:49 +0000280 }
281
Rafael Espindola68fa2492015-02-17 20:48:01 +0000282 return getContext().getELFSection(Name, getELFSectionType(Name, Kind),
283 Flags, 0, Group, !UniqueSectionNames);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000284 }
285
286 if (Kind.isText()) return TextSection;
287
Rafael Espindolaa05b3b72015-01-28 17:54:19 +0000288 if (Kind.isMergeableCString()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000289
290 // We also need alignment here.
291 // FIXME: this is getting the alignment of the character, not the
292 // alignment of the global!
293 unsigned Align =
Eric Christopher8b770652015-01-26 19:03:15 +0000294 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000295
Rafael Espindolaba31e272015-01-29 17:33:21 +0000296 unsigned EntrySize = 1;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000297 if (Kind.isMergeable2ByteCString())
Rafael Espindolaba31e272015-01-29 17:33:21 +0000298 EntrySize = 2;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000299 else if (Kind.isMergeable4ByteCString())
Rafael Espindolaba31e272015-01-29 17:33:21 +0000300 EntrySize = 4;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000301 else
302 assert(Kind.isMergeable1ByteCString() && "unknown string width");
303
Rafael Espindolaba31e272015-01-29 17:33:21 +0000304 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000305 std::string Name = SizeSpec + utostr(Align);
Rafael Espindolaba31e272015-01-29 17:33:21 +0000306 return getContext().getELFSection(
307 Name, ELF::SHT_PROGBITS,
308 ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS, EntrySize, "");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000309 }
310
311 if (Kind.isMergeableConst()) {
312 if (Kind.isMergeableConst4() && MergeableConst4Section)
313 return MergeableConst4Section;
314 if (Kind.isMergeableConst8() && MergeableConst8Section)
315 return MergeableConst8Section;
316 if (Kind.isMergeableConst16() && MergeableConst16Section)
317 return MergeableConst16Section;
318 return ReadOnlySection; // .const
319 }
320
321 if (Kind.isReadOnly()) return ReadOnlySection;
322
323 if (Kind.isThreadData()) return TLSDataSection;
324 if (Kind.isThreadBSS()) return TLSBSSSection;
325
326 // Note: we claim that common symbols are put in BSSSection, but they are
327 // really emitted with the magic .comm directive, which creates a symbol table
328 // entry but not a section.
329 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
330
331 if (Kind.isDataNoRel()) return DataSection;
332 if (Kind.isDataRelLocal()) return DataRelLocalSection;
333 if (Kind.isDataRel()) return DataRelSection;
334 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
335
336 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
337 return DataRelROSection;
338}
339
Rafael Espindola29786d42015-02-12 17:16:46 +0000340const MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
341 const Function &F, Mangler &Mang, const TargetMachine &TM) const {
342 // If the function can be removed, produce a unique section so that
343 // the table doesn't prevent the removal.
344 const Comdat *C = F.getComdat();
345 bool EmitUniqueSection = TM.getFunctionSections() || C;
346 if (!EmitUniqueSection)
347 return ReadOnlySection;
348
349 SmallString<128> Name(".rodata.");
350 TM.getNameWithPrefix(Name, &F, Mang, true);
351
352 unsigned Flags = ELF::SHF_ALLOC;
353 StringRef Group = "";
354 if (C) {
355 Flags |= ELF::SHF_GROUP;
356 Group = C->getName();
357 }
358
359 return getContext().getELFSection(Name, ELF::SHT_PROGBITS, Flags, 0, Group);
360}
361
Rafael Espindoladf195192015-02-17 23:34:51 +0000362bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
363 bool UsesLabelDifference, const Function &F) const {
364 // We can always create relative relocations, so use another section
365 // that can be marked non-executable.
366 return false;
367}
368
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000369/// getSectionForConstant - Given a mergeable constant with the
370/// specified size and relocation information, return a section that it
371/// should be placed in.
David Majnemer8bce66b2014-07-14 22:57:27 +0000372const MCSection *
373TargetLoweringObjectFileELF::getSectionForConstant(SectionKind Kind,
374 const Constant *C) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000375 if (Kind.isMergeableConst4() && MergeableConst4Section)
376 return MergeableConst4Section;
377 if (Kind.isMergeableConst8() && MergeableConst8Section)
378 return MergeableConst8Section;
379 if (Kind.isMergeableConst16() && MergeableConst16Section)
380 return MergeableConst16Section;
381 if (Kind.isReadOnly())
382 return ReadOnlySection;
383
384 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
385 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
386 return DataRelROSection;
387}
388
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000389static const MCSectionELF *getStaticStructorSection(MCContext &Ctx,
390 bool UseInitArray,
391 bool IsCtor,
392 unsigned Priority,
393 const MCSymbol *KeySym) {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000394 std::string Name;
395 unsigned Type;
396 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
Rafael Espindolac4b42532014-09-04 23:03:58 +0000397 StringRef COMDAT = KeySym ? KeySym->getName() : "";
398
399 if (KeySym)
400 Flags |= ELF::SHF_GROUP;
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000401
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000402 if (UseInitArray) {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000403 if (IsCtor) {
404 Type = ELF::SHT_INIT_ARRAY;
405 Name = ".init_array";
406 } else {
407 Type = ELF::SHT_FINI_ARRAY;
408 Name = ".fini_array";
409 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000410 if (Priority != 65535) {
411 Name += '.';
412 Name += utostr(Priority);
413 }
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000414 } else {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000415 // The default scheme is .ctor / .dtor, so we have to invert the priority
416 // numbering.
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000417 if (IsCtor)
418 Name = ".ctors";
419 else
420 Name = ".dtors";
Rafael Espindolac4b42532014-09-04 23:03:58 +0000421 if (Priority != 65535) {
422 Name += '.';
423 Name += utostr(65535 - Priority);
424 }
425 Type = ELF::SHT_PROGBITS;
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000426 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000427
Rafael Espindolaba31e272015-01-29 17:33:21 +0000428 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000429}
430
431const MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
432 unsigned Priority, const MCSymbol *KeySym) const {
433 return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
434 KeySym);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000435}
436
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000437const MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000438 unsigned Priority, const MCSymbol *KeySym) const {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000439 return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
440 KeySym);
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000441}
442
443void
444TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
445 UseInitArray = UseInitArray_;
446 if (!UseInitArray)
447 return;
448
Rafael Espindolaba31e272015-01-29 17:33:21 +0000449 StaticCtorSection = getContext().getELFSection(
450 ".init_array", ELF::SHT_INIT_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC);
451 StaticDtorSection = getContext().getELFSection(
452 ".fini_array", ELF::SHT_FINI_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000453}
454
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000455//===----------------------------------------------------------------------===//
456// MachO
457//===----------------------------------------------------------------------===//
458
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000459/// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
460/// option string. Returns StringRef() if the option does not specify a library.
461StringRef TargetLoweringObjectFileMachO::
462getDepLibFromLinkerOpt(StringRef LinkerOption) const {
463 const char *LibCmd = "-l";
464 if (LinkerOption.startswith(LibCmd))
465 return LinkerOption.substr(strlen(LibCmd));
466 return StringRef();
467}
468
Daniel Dunbar95856122013-01-18 19:37:00 +0000469/// emitModuleFlags - Perform code emission for module flags.
Bill Wendling06df7722012-02-14 21:28:13 +0000470void TargetLoweringObjectFileMachO::
Bill Wendling734909a2012-02-15 22:36:15 +0000471emitModuleFlags(MCStreamer &Streamer,
472 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000473 Mangler &Mang, const TargetMachine &TM) const {
Bill Wendling06df7722012-02-14 21:28:13 +0000474 unsigned VersionVal = 0;
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000475 unsigned ImageInfoFlags = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000476 MDNode *LinkerOptions = nullptr;
Bill Wendling734909a2012-02-15 22:36:15 +0000477 StringRef SectionVal;
Bill Wendling06df7722012-02-14 21:28:13 +0000478
Bill Wendling734909a2012-02-15 22:36:15 +0000479 for (ArrayRef<Module::ModuleFlagEntry>::iterator
480 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
481 const Module::ModuleFlagEntry &MFE = *i;
Bill Wendling06df7722012-02-14 21:28:13 +0000482
483 // Ignore flags with 'Require' behavior.
Bill Wendling734909a2012-02-15 22:36:15 +0000484 if (MFE.Behavior == Module::Require)
Bill Wendling06df7722012-02-14 21:28:13 +0000485 continue;
486
Bill Wendling734909a2012-02-15 22:36:15 +0000487 StringRef Key = MFE.Key->getString();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000488 Metadata *Val = MFE.Val;
Bill Wendling06df7722012-02-14 21:28:13 +0000489
Daniel Dunbar95856122013-01-18 19:37:00 +0000490 if (Key == "Objective-C Image Info Version") {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000491 VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000492 } else if (Key == "Objective-C Garbage Collection" ||
493 Key == "Objective-C GC Only" ||
Manman Renc98ec0e2014-11-21 19:24:55 +0000494 Key == "Objective-C Is Simulated" ||
495 Key == "Objective-C Image Swift Version") {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000496 ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000497 } else if (Key == "Objective-C Image Info Section") {
Bill Wendling734909a2012-02-15 22:36:15 +0000498 SectionVal = cast<MDString>(Val)->getString();
Daniel Dunbar95856122013-01-18 19:37:00 +0000499 } else if (Key == "Linker Options") {
500 LinkerOptions = cast<MDNode>(Val);
501 }
502 }
503
504 // Emit the linker options if present.
505 if (LinkerOptions) {
506 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
507 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
508 SmallVector<std::string, 4> StrOptions;
509
510 // Convert to strings.
511 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
512 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
513 StrOptions.push_back(MDOption->getString());
514 }
515
516 Streamer.EmitLinkerOptions(StrOptions);
517 }
Bill Wendling06df7722012-02-14 21:28:13 +0000518 }
519
Bill Wendling734909a2012-02-15 22:36:15 +0000520 // The section is mandatory. If we don't have it, then we don't have GC info.
521 if (SectionVal.empty()) return;
Bill Wendling06df7722012-02-14 21:28:13 +0000522
Bill Wendling734909a2012-02-15 22:36:15 +0000523 StringRef Segment, Section;
524 unsigned TAA = 0, StubSize = 0;
525 bool TAAParsed;
526 std::string ErrorCode =
527 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
528 TAA, TAAParsed, StubSize);
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000529 if (!ErrorCode.empty())
Bill Wendling734909a2012-02-15 22:36:15 +0000530 // If invalid, report the error with report_fatal_error.
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000531 report_fatal_error("Invalid section specifier '" + Section + "': " +
532 ErrorCode + ".");
Bill Wendling06df7722012-02-14 21:28:13 +0000533
Bill Wendling734909a2012-02-15 22:36:15 +0000534 // Get the section.
535 const MCSectionMachO *S =
536 getContext().getMachOSection(Segment, Section, TAA, StubSize,
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000537 SectionKind::getDataNoRel());
Bill Wendling734909a2012-02-15 22:36:15 +0000538 Streamer.SwitchSection(S);
539 Streamer.EmitLabel(getContext().
540 GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendling06df7722012-02-14 21:28:13 +0000541 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000542 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendling06df7722012-02-14 21:28:13 +0000543 Streamer.AddBlankLine();
544}
545
David Majnemerdad0a642014-06-27 18:19:56 +0000546static void checkMachOComdat(const GlobalValue *GV) {
547 const Comdat *C = GV->getComdat();
548 if (!C)
549 return;
550
551 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
552 "' cannot be lowered.");
553}
554
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000555const MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
556 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
557 const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000558 // Parse the section specifier and create it if valid.
559 StringRef Segment, Section;
Stuart Hastings12d53122011-03-19 02:42:31 +0000560 unsigned TAA = 0, StubSize = 0;
561 bool TAAParsed;
David Majnemerdad0a642014-06-27 18:19:56 +0000562
563 checkMachOComdat(GV);
564
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000565 std::string ErrorCode =
566 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000567 TAA, TAAParsed, StubSize);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000568 if (!ErrorCode.empty()) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000569 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000570 report_fatal_error("Global variable '" + GV->getName() +
571 "' has an invalid section specifier '" +
572 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000573 }
574
575 // Get the section.
576 const MCSectionMachO *S =
Chris Lattner433d4062010-04-08 20:40:11 +0000577 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000578
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000579 // If TAA wasn't set by ParseSectionSpecifier() above,
580 // use the value returned by getMachOSection() as a default.
Stuart Hastings12d53122011-03-19 02:42:31 +0000581 if (!TAAParsed)
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000582 TAA = S->getTypeAndAttributes();
583
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000584 // Okay, now that we got the section, verify that the TAA & StubSize agree.
585 // If the user declared multiple globals with different section flags, we need
586 // to reject it here.
587 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000588 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000589 report_fatal_error("Global variable '" + GV->getName() +
590 "' section type or attributes does not match previous"
591 " section specifier");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000592 }
593
594 return S;
595}
596
597const MCSection *TargetLoweringObjectFileMachO::
598SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000599 Mangler &Mang, const TargetMachine &TM) const {
David Majnemerdad0a642014-06-27 18:19:56 +0000600 checkMachOComdat(GV);
Bill Wendling25b61db2013-11-17 10:53:13 +0000601
602 // Handle thread local data.
603 if (Kind.isThreadBSS()) return TLSBSSSection;
604 if (Kind.isThreadData()) return TLSDataSection;
605
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000606 if (Kind.isText())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000607 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
608
609 // If this is weak/linkonce, put this in a coalescable section, either in text
610 // or data depending on if it is writable.
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000611 if (GV->isWeakForLinker()) {
612 if (Kind.isReadOnly())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000613 return ConstTextCoalSection;
614 return DataCoalSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000615 }
616
617 // FIXME: Alignment check should be handled by section classifier.
Chris Lattneref2f8042010-03-07 04:28:09 +0000618 if (Kind.isMergeable1ByteCString() &&
Eric Christopher8b770652015-01-26 19:03:15 +0000619 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000620 return CStringSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000621
Chris Lattneref2f8042010-03-07 04:28:09 +0000622 // Do not put 16-bit arrays in the UString section if they have an
623 // externally visible label, this runs into issues with certain linker
624 // versions.
625 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
Eric Christopher8b770652015-01-26 19:03:15 +0000626 TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000627 return UStringSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000628
Rafael Espindolab43d51d2014-08-28 20:13:31 +0000629 // With MachO only variables whose corresponding symbol starts with 'l' or
630 // 'L' can be merged, so we only try merging GVs with private linkage.
631 if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000632 if (Kind.isMergeableConst4())
633 return FourByteConstantSection;
634 if (Kind.isMergeableConst8())
635 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000636 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000637 return SixteenByteConstantSection;
638 }
639
640 // Otherwise, if it is readonly, but not something we can specially optimize,
641 // just drop it in .const.
642 if (Kind.isReadOnly())
643 return ReadOnlySection;
644
645 // If this is marked const, put it into a const section. But if the dynamic
646 // linker needs to write to it, put it in the data segment.
647 if (Kind.isReadOnlyWithRel())
648 return ConstDataSection;
649
650 // Put zero initialized globals with strong external linkage in the
651 // DATA, __common section with the .zerofill directive.
652 if (Kind.isBSSExtern())
653 return DataCommonSection;
654
655 // Put zero initialized globals with local linkage in __DATA,__bss directive
656 // with the .zerofill directive (aka .lcomm).
657 if (Kind.isBSSLocal())
658 return DataBSSSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000659
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000660 // Otherwise, just drop the variable in the normal data section.
661 return DataSection;
662}
663
664const MCSection *
David Majnemer8bce66b2014-07-14 22:57:27 +0000665TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind,
666 const Constant *C) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000667 // If this constant requires a relocation, we have to put it in the data
668 // segment, not in the text segment.
669 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
670 return ConstDataSection;
671
672 if (Kind.isMergeableConst4())
673 return FourByteConstantSection;
674 if (Kind.isMergeableConst8())
675 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000676 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000677 return SixteenByteConstantSection;
678 return ReadOnlySection; // .const
679}
680
Rafael Espindola15b26692014-02-09 14:50:44 +0000681const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
682 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000683 const TargetMachine &TM, MachineModuleInfo *MMI,
684 MCStreamer &Streamer) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000685 // The mach-o version of this method defaults to returning a stub reference.
686
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000687 if (Encoding & DW_EH_PE_indirect) {
688 MachineModuleInfoMachO &MachOMMI =
689 MMI->getObjFileInfo<MachineModuleInfoMachO>();
690
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000691 MCSymbol *SSym =
692 getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000693
694 // Add information about the stub reference to MachOMMI so that the stub
695 // gets emitted by the asmprinter.
Bill Wendling57e3aaa2011-10-24 23:05:43 +0000696 MachineModuleInfoImpl::StubValueTy &StubSym =
697 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
698 MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000699 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000700 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000701 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000702 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000703
704 return TargetLoweringObjectFile::
Anton Korobeynikove42af362012-11-14 01:47:00 +0000705 getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()),
706 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000707 }
708
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000709 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang,
710 TM, MMI, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000711}
712
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000713MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
714 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
715 MachineModuleInfo *MMI) const {
Rafael Espindola08704342011-04-27 23:08:15 +0000716 // The mach-o version of this method defaults to returning a stub reference.
717 MachineModuleInfoMachO &MachOMMI =
718 MMI->getObjFileInfo<MachineModuleInfoMachO>();
719
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000720 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Rafael Espindola08704342011-04-27 23:08:15 +0000721
722 // Add information about the stub reference to MachOMMI so that the stub
723 // gets emitted by the asmprinter.
Bill Wendlinge4cc3322011-11-29 01:43:20 +0000724 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000725 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000726 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Rafael Espindola08704342011-04-27 23:08:15 +0000727 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
728 }
729
730 return SSym;
731}
732
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000733//===----------------------------------------------------------------------===//
734// COFF
735//===----------------------------------------------------------------------===//
736
Chris Lattner87cffa92010-05-07 17:17:41 +0000737static unsigned
738getCOFFSectionFlags(SectionKind K) {
739 unsigned Flags = 0;
740
Anton Korobeynikove4152302010-07-06 15:24:56 +0000741 if (K.isMetadata())
Chris Lattner02844932010-05-07 21:49:09 +0000742 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000743 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000744 else if (K.isText())
745 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000746 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer0f83d962010-10-27 18:52:29 +0000747 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar329d2022010-07-01 20:07:24 +0000748 COFF::IMAGE_SCN_CNT_CODE;
David Majnemerb8dbebb2014-09-20 07:31:46 +0000749 else if (K.isBSS())
Chris Lattner02844932010-05-07 21:49:09 +0000750 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000751 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
752 COFF::IMAGE_SCN_MEM_READ |
753 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000754 else if (K.isThreadLocal())
755 Flags |=
756 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
757 COFF::IMAGE_SCN_MEM_READ |
758 COFF::IMAGE_SCN_MEM_WRITE;
David Majnemerb8dbebb2014-09-20 07:31:46 +0000759 else if (K.isReadOnly() || K.isReadOnlyWithRel())
Chris Lattner87cffa92010-05-07 17:17:41 +0000760 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000761 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
762 COFF::IMAGE_SCN_MEM_READ;
Chris Lattner87cffa92010-05-07 17:17:41 +0000763 else if (K.isWriteable())
764 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000765 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
766 COFF::IMAGE_SCN_MEM_READ |
767 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000768
769 return Flags;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000770}
771
Benjamin Kramer6cbe6702014-07-07 14:47:51 +0000772static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
David Majnemerdad0a642014-06-27 18:19:56 +0000773 const Comdat *C = GV->getComdat();
774 assert(C && "expected GV to have a Comdat!");
775
776 StringRef ComdatGVName = C->getName();
777 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
778 if (!ComdatGV)
779 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
780 "' does not exist.");
781
782 if (ComdatGV->getComdat() != C)
783 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
Hans Wennborgc0f0c512014-09-19 01:14:56 +0000784 "' is not a key for its COMDAT.");
David Majnemerdad0a642014-06-27 18:19:56 +0000785
786 return ComdatGV;
787}
788
789static int getSelectionForCOFF(const GlobalValue *GV) {
790 if (const Comdat *C = GV->getComdat()) {
791 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
792 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
793 ComdatKey = GA->getBaseObject();
794 if (ComdatKey == GV) {
795 switch (C->getSelectionKind()) {
796 case Comdat::Any:
797 return COFF::IMAGE_COMDAT_SELECT_ANY;
798 case Comdat::ExactMatch:
799 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
800 case Comdat::Largest:
801 return COFF::IMAGE_COMDAT_SELECT_LARGEST;
802 case Comdat::NoDuplicates:
803 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
804 case Comdat::SameSize:
805 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
806 }
807 } else {
808 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
809 }
810 } else if (GV->isWeakForLinker()) {
811 return COFF::IMAGE_COMDAT_SELECT_ANY;
812 }
813 return 0;
814}
815
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000816const MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
817 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
818 const TargetMachine &TM) const {
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000819 int Selection = 0;
820 unsigned Characteristics = getCOFFSectionFlags(Kind);
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000821 StringRef Name = GV->getSection();
822 StringRef COMDATSymName = "";
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000823 if (GV->hasComdat()) {
David Majnemerdad0a642014-06-27 18:19:56 +0000824 Selection = getSelectionForCOFF(GV);
825 const GlobalValue *ComdatGV;
826 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
827 ComdatGV = getComdatGVForCOFF(GV);
828 else
829 ComdatGV = GV;
830
831 if (!ComdatGV->hasPrivateLinkage()) {
832 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang);
833 COMDATSymName = Sym->getName();
834 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
835 } else {
836 Selection = 0;
837 }
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000838 }
839 return getContext().getCOFFSection(Name,
840 Characteristics,
Nico Riecka37acf72013-07-06 12:13:10 +0000841 Kind,
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000842 COMDATSymName,
Nico Riecka37acf72013-07-06 12:13:10 +0000843 Selection);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000844}
845
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000846static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000847 if (Kind.isText())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000848 return ".text";
David Majnemera9bdb322014-04-08 22:33:40 +0000849 if (Kind.isBSS())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000850 return ".bss";
851 if (Kind.isThreadLocal())
Rafael Espindola3c8e1472013-11-27 15:52:11 +0000852 return ".tls$";
David Majnemer597be2d2014-09-22 20:39:23 +0000853 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
854 return ".rdata";
855 return ".data";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000856}
857
858
859const MCSection *TargetLoweringObjectFileCOFF::
860SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000861 Mangler &Mang, const TargetMachine &TM) const {
David Majnemer93389842014-03-23 17:47:39 +0000862 // If we have -ffunction-sections then we should emit the global value to a
863 // uniqued section specifically for it.
David Majnemer273bff42014-03-25 06:14:26 +0000864 bool EmitUniquedSection;
865 if (Kind.isText())
866 EmitUniquedSection = TM.getFunctionSections();
867 else
868 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000869
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000870 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000871 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Chris Lattner02844932010-05-07 21:49:09 +0000872 unsigned Characteristics = getCOFFSectionFlags(Kind);
873
Daniel Dunbar329d2022010-07-01 20:07:24 +0000874 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
David Majnemerdad0a642014-06-27 18:19:56 +0000875 int Selection = getSelectionForCOFF(GV);
876 if (!Selection)
877 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
878 const GlobalValue *ComdatGV;
879 if (GV->hasComdat())
880 ComdatGV = getComdatGVForCOFF(GV);
881 else
882 ComdatGV = GV;
883
884 if (!ComdatGV->hasPrivateLinkage()) {
885 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang);
886 StringRef COMDATSymName = Sym->getName();
887 return getContext().getCOFFSection(Name, Characteristics, Kind,
888 COMDATSymName, Selection);
889 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000890 }
891
892 if (Kind.isText())
David Majnemer3d96acb2013-08-13 01:23:53 +0000893 return TextSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000894
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000895 if (Kind.isThreadLocal())
David Majnemer3d96acb2013-08-13 01:23:53 +0000896 return TLSDataSection;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000897
David Majnemer597be2d2014-09-22 20:39:23 +0000898 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
David Majnemerf76d6b32013-08-08 01:50:52 +0000899 return ReadOnlySection;
900
David Majnemera9bdb322014-04-08 22:33:40 +0000901 // Note: we claim that common symbols are put in BSSSection, but they are
902 // really emitted with the magic .comm directive, which creates a symbol table
903 // entry but not a section.
904 if (Kind.isBSS() || Kind.isCommon())
David Majnemer3d96acb2013-08-13 01:23:53 +0000905 return BSSSection;
906
907 return DataSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000908}
909
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000910StringRef TargetLoweringObjectFileCOFF::
911getDepLibFromLinkerOpt(StringRef LinkerOption) const {
912 const char *LibCmd = "/DEFAULTLIB:";
913 if (LinkerOption.startswith(LibCmd))
914 return LinkerOption.substr(strlen(LibCmd));
915 return StringRef();
916}
917
Reid Klecknerd973ca32013-04-25 19:34:41 +0000918void TargetLoweringObjectFileCOFF::
919emitModuleFlags(MCStreamer &Streamer,
920 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000921 Mangler &Mang, const TargetMachine &TM) const {
Craig Topperc0196b12014-04-14 00:51:57 +0000922 MDNode *LinkerOptions = nullptr;
Reid Klecknerd973ca32013-04-25 19:34:41 +0000923
924 // Look for the "Linker Options" flag, since it's the only one we support.
925 for (ArrayRef<Module::ModuleFlagEntry>::iterator
926 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
927 const Module::ModuleFlagEntry &MFE = *i;
928 StringRef Key = MFE.Key->getString();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000929 Metadata *Val = MFE.Val;
Reid Klecknerd973ca32013-04-25 19:34:41 +0000930 if (Key == "Linker Options") {
931 LinkerOptions = cast<MDNode>(Val);
932 break;
933 }
934 }
935 if (!LinkerOptions)
936 return;
937
938 // Emit the linker options to the linker .drectve section. According to the
939 // spec, this section is a space-separated string containing flags for linker.
940 const MCSection *Sec = getDrectveSection();
941 Streamer.SwitchSection(Sec);
942 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
943 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
944 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
945 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
Reid Klecknerd973ca32013-04-25 19:34:41 +0000946 // Lead with a space for consistency with our dllexport implementation.
Michael Kupersteinfc3e6262015-02-16 11:57:17 +0000947 std::string Directive(" ");
948 Directive.append(MDOption->getString());
949 Streamer.EmitBytes(Directive);
Reid Klecknerd973ca32013-04-25 19:34:41 +0000950 }
951 }
952}
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000953
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000954const MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000955 unsigned Priority, const MCSymbol *KeySym) const {
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000956 return getContext().getAssociativeCOFFSection(
957 cast<MCSectionCOFF>(StaticCtorSection), KeySym);
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000958}
959
960const MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000961 unsigned Priority, const MCSymbol *KeySym) const {
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000962 return getContext().getAssociativeCOFFSection(
963 cast<MCSectionCOFF>(StaticDtorSection), KeySym);
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000964}