blob: 4229741cf5ae151b3ee6572f6ad877c4add35491 [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"
Dehao Chenf84b6302016-02-23 03:39:24 +000037#include "llvm/ProfileData/ProfileCommon.h"
Reid Kleckner1f13d472015-09-03 16:41:50 +000038#include "llvm/Support/COFF.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000039#include "llvm/Support/Dwarf.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000040#include "llvm/Support/ELF.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000041#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/raw_ostream.h"
Rafael Espindoladaeafb42014-02-19 17:23:20 +000043#include "llvm/Target/TargetLowering.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000044#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000045#include "llvm/Target/TargetSubtargetInfo.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000046using namespace llvm;
Anton Korobeynikov31a92122010-02-21 20:28:15 +000047using namespace dwarf;
Anton Korobeynikovab663a02010-02-15 22:37:53 +000048
49//===----------------------------------------------------------------------===//
50// ELF
51//===----------------------------------------------------------------------===//
Anton Korobeynikovab663a02010-02-15 22:37:53 +000052
Rafael Espindoladaeafb42014-02-19 17:23:20 +000053MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
54 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
55 MachineModuleInfo *MMI) const {
Rafael Espindolace83fc32011-04-27 23:17:57 +000056 unsigned Encoding = getPersonalityEncoding();
Logan Chienc0029812014-05-30 16:48:56 +000057 if ((Encoding & 0x80) == dwarf::DW_EH_PE_indirect)
Jim Grosbach6f482002015-05-18 18:43:14 +000058 return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000059 TM.getSymbol(GV, Mang)->getName());
Logan Chienc0029812014-05-30 16:48:56 +000060 if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr)
61 return TM.getSymbol(GV, Mang);
62 report_fatal_error("We do not support this DWARF encoding yet!");
Rafael Espindolaa83b1772011-04-16 03:51:21 +000063}
64
Mehdi Amini5c0fa582015-07-16 06:04:17 +000065void TargetLoweringObjectFileELF::emitPersonalityValue(
66 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000067 SmallString<64> NameData("DW.ref.");
68 NameData += Sym->getName();
Rafael Espindolaa8695762015-06-02 00:25:12 +000069 MCSymbolELF *Label =
70 cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
Rafael Espindola39897762011-04-27 21:29:52 +000071 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
72 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindola51d2d7a2011-06-13 03:09:13 +000073 StringRef Prefix = ".data.";
74 NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
Rafael Espindola39897762011-04-27 21:29:52 +000075 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
Rafael Espindola0709a7b2015-05-21 19:20:38 +000076 MCSection *Sec = getContext().getELFSection(NameData, ELF::SHT_PROGBITS,
77 Flags, 0, Label->getName());
Mehdi Amini5c0fa582015-07-16 06:04:17 +000078 unsigned Size = DL.getPointerSize();
Rafael Espindola39897762011-04-27 21:29:52 +000079 Streamer.SwitchSection(Sec);
Mehdi Amini5c0fa582015-07-16 06:04:17 +000080 Streamer.EmitValueToAlignment(DL.getPointerABIAlignment());
Rafael Espindola39897762011-04-27 21:29:52 +000081 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
Jim Grosbach13760bd2015-05-30 01:25:56 +000082 const MCExpr *E = MCConstantExpr::create(Size, getContext());
Rafael Espindolaa8695762015-06-02 00:25:12 +000083 Streamer.emitELFSize(Label, E);
Rafael Espindola39897762011-04-27 21:29:52 +000084 Streamer.EmitLabel(Label);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000085
Rafael Espindola39897762011-04-27 21:29:52 +000086 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000087}
88
Rafael Espindola15b26692014-02-09 14:50:44 +000089const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
90 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
Rafael Espindoladaeafb42014-02-19 17:23:20 +000091 const TargetMachine &TM, MachineModuleInfo *MMI,
92 MCStreamer &Streamer) const {
Anton Korobeynikove42af362012-11-14 01:47:00 +000093
94 if (Encoding & dwarf::DW_EH_PE_indirect) {
95 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
96
Rafael Espindoladaeafb42014-02-19 17:23:20 +000097 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang, TM);
Anton Korobeynikove42af362012-11-14 01:47:00 +000098
99 // Add information about the stub reference to ELFMMI so that the stub
100 // gets emitted by the asmprinter.
Anton Korobeynikove42af362012-11-14 01:47:00 +0000101 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000102 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000103 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000104 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
105 }
106
107 return TargetLoweringObjectFile::
Jim Grosbach13760bd2015-05-30 01:25:56 +0000108 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Anton Korobeynikove42af362012-11-14 01:47:00 +0000109 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
110 }
111
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000112 return TargetLoweringObjectFile::
113 getTTypeGlobalReference(GV, Encoding, Mang, TM, MMI, Streamer);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000114}
115
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000116static SectionKind
117getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindola0d018b12011-05-24 03:10:31 +0000118 // N.B.: The defaults used in here are no the same ones used in MC.
119 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
120 // both gas and MC will produce a section with no flags. Given
Bill Wendlingd1634052012-07-19 00:04:14 +0000121 // section(".eh_frame") gcc will produce:
122 //
123 // .section .eh_frame,"a",@progbits
Xinliang David Li5f04f922016-01-14 18:09:45 +0000124
125 if (Name == getInstrProfCoverageSectionName(false))
126 return SectionKind::getMetadata();
127
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000128 if (Name.empty() || Name[0] != '.') return K;
129
130 // Some lame default implementation based on some magic section names.
131 if (Name == ".bss" ||
132 Name.startswith(".bss.") ||
133 Name.startswith(".gnu.linkonce.b.") ||
134 Name.startswith(".llvm.linkonce.b.") ||
135 Name == ".sbss" ||
136 Name.startswith(".sbss.") ||
137 Name.startswith(".gnu.linkonce.sb.") ||
138 Name.startswith(".llvm.linkonce.sb."))
139 return SectionKind::getBSS();
140
141 if (Name == ".tdata" ||
142 Name.startswith(".tdata.") ||
143 Name.startswith(".gnu.linkonce.td.") ||
144 Name.startswith(".llvm.linkonce.td."))
145 return SectionKind::getThreadData();
146
147 if (Name == ".tbss" ||
148 Name.startswith(".tbss.") ||
149 Name.startswith(".gnu.linkonce.tb.") ||
150 Name.startswith(".llvm.linkonce.tb."))
151 return SectionKind::getThreadBSS();
152
153 return K;
154}
155
156
157static unsigned getELFSectionType(StringRef Name, SectionKind K) {
158
159 if (Name == ".init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000160 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000161
162 if (Name == ".fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000163 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000164
165 if (Name == ".preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000166 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000167
168 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolaaea49582011-01-23 04:28:49 +0000169 return ELF::SHT_NOBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000170
Rafael Espindolaaea49582011-01-23 04:28:49 +0000171 return ELF::SHT_PROGBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000172}
173
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000174static unsigned getELFSectionFlags(SectionKind K) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000175 unsigned Flags = 0;
176
177 if (!K.isMetadata())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000178 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000179
180 if (K.isText())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000181 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000182
Rafael Espindolac85e0d82011-06-07 23:26:45 +0000183 if (K.isWriteable())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000184 Flags |= ELF::SHF_WRITE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000185
186 if (K.isThreadLocal())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000187 Flags |= ELF::SHF_TLS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000188
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000189 if (K.isMergeableCString() || K.isMergeableConst())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000190 Flags |= ELF::SHF_MERGE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000191
192 if (K.isMergeableCString())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000193 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000194
195 return Flags;
196}
197
David Majnemerdad0a642014-06-27 18:19:56 +0000198static const Comdat *getELFComdat(const GlobalValue *GV) {
199 const Comdat *C = GV->getComdat();
200 if (!C)
201 return nullptr;
202
203 if (C->getSelectionKind() != Comdat::Any)
204 report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
205 C->getName() + "' cannot be lowered.");
206
207 return C;
208}
209
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000210MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000211 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
212 const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000213 StringRef SectionName = GV->getSection();
214
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);
David Majnemerdad0a642014-06-27 18:19:56 +0000220 if (const Comdat *C = getELFComdat(GV)) {
221 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
Dehao Chenf84b6302016-02-23 03:39:24 +0000248static cl::opt<bool> GroupFunctionsByHotness(
249 "group-functions-by-hotness",
250 llvm::cl::desc("Partition hot/cold functions by sections prefix"),
251 cl::init(false));
252
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000253static MCSectionELF *
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000254selectELFSectionForGlobal(MCContext &Ctx, const GlobalValue *GV,
255 SectionKind Kind, Mangler &Mang,
256 const TargetMachine &TM, bool EmitUniqueSection,
257 unsigned Flags, unsigned *NextUniqueID) {
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000258 unsigned EntrySize = 0;
259 if (Kind.isMergeableCString()) {
260 if (Kind.isMergeable2ByteCString()) {
261 EntrySize = 2;
262 } else if (Kind.isMergeable4ByteCString()) {
263 EntrySize = 4;
264 } else {
265 EntrySize = 1;
266 assert(Kind.isMergeable1ByteCString() && "unknown string width");
267 }
268 } else if (Kind.isMergeableConst()) {
269 if (Kind.isMergeableConst4()) {
270 EntrySize = 4;
271 } else if (Kind.isMergeableConst8()) {
272 EntrySize = 8;
David Majnemer964b70d2016-02-22 22:23:11 +0000273 } else if (Kind.isMergeableConst16()) {
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000274 EntrySize = 16;
David Majnemer964b70d2016-02-22 22:23:11 +0000275 } else {
276 assert(Kind.isMergeableConst32() && "unknown data width");
277 EntrySize = 32;
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000278 }
279 }
280
Rafael Espindola9075f772015-02-20 23:28:28 +0000281 StringRef Group = "";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000282 if (const Comdat *C = getELFComdat(GV)) {
Rafael Espindola9075f772015-02-20 23:28:28 +0000283 Flags |= ELF::SHF_GROUP;
284 Group = C->getName();
285 }
286
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000287 bool UniqueSectionNames = TM.getUniqueSectionNames();
288 SmallString<128> Name;
Rafael Espindolaa05b3b72015-01-28 17:54:19 +0000289 if (Kind.isMergeableCString()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000290 // We also need alignment here.
291 // FIXME: this is getting the alignment of the character, not the
292 // alignment of the global!
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000293 unsigned Align = GV->getParent()->getDataLayout().getPreferredAlignment(
294 cast<GlobalVariable>(GV));
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000295
Rafael Espindolaba31e272015-01-29 17:33:21 +0000296 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000297 Name = SizeSpec + utostr(Align);
298 } else if (Kind.isMergeableConst()) {
299 Name = ".rodata.cst";
300 Name += utostr(EntrySize);
301 } else {
302 Name = getSectionPrefixForGlobal(Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000303 }
304
Dehao Chenf84b6302016-02-23 03:39:24 +0000305 if (GroupFunctionsByHotness) {
306 if (const Function *F = dyn_cast<Function>(GV)) {
307 if (ProfileSummary::isFunctionHot(F)) {
308 Name += getHotSectionPrefix();
309 } else if (ProfileSummary::isFunctionUnlikely(F)) {
310 Name += getUnlikelySectionPrefix();
311 }
312 }
313 }
314
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000315 if (EmitUniqueSection && UniqueSectionNames) {
316 Name.push_back('.');
317 TM.getNameWithPrefix(Name, GV, Mang, true);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000318 }
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000319 unsigned UniqueID = ~0;
320 if (EmitUniqueSection && !UniqueSectionNames) {
321 UniqueID = *NextUniqueID;
322 (*NextUniqueID)++;
323 }
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000324 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000325 EntrySize, Group, UniqueID);
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000326}
327
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000328MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000329 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
330 const TargetMachine &TM) const {
331 unsigned Flags = getELFSectionFlags(Kind);
332
333 // If we have -ffunction-section or -fdata-section then we should emit the
334 // global value to a uniqued section specifically for it.
335 bool EmitUniqueSection = false;
336 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
337 if (Kind.isText())
338 EmitUniqueSection = TM.getFunctionSections();
339 else
340 EmitUniqueSection = TM.getDataSections();
341 }
342 EmitUniqueSection |= GV->hasComdat();
343
344 return selectELFSectionForGlobal(getContext(), GV, Kind, Mang, TM,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000345 EmitUniqueSection, Flags, &NextUniqueID);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000346}
347
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000348MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
Rafael Espindola29786d42015-02-12 17:16:46 +0000349 const Function &F, Mangler &Mang, const TargetMachine &TM) const {
350 // If the function can be removed, produce a unique section so that
351 // the table doesn't prevent the removal.
352 const Comdat *C = F.getComdat();
353 bool EmitUniqueSection = TM.getFunctionSections() || C;
354 if (!EmitUniqueSection)
355 return ReadOnlySection;
356
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000357 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000358 Mang, TM, EmitUniqueSection, ELF::SHF_ALLOC,
359 &NextUniqueID);
Rafael Espindola29786d42015-02-12 17:16:46 +0000360}
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
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000369/// Given a mergeable constant with the specified size and relocation
370/// information, return a section that it should be placed in.
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000371MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
David Majnemera3ea4072016-02-21 01:30:30 +0000372 const DataLayout &DL, SectionKind Kind, const Constant *C,
373 unsigned &Align) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000374 if (Kind.isMergeableConst4() && MergeableConst4Section)
375 return MergeableConst4Section;
376 if (Kind.isMergeableConst8() && MergeableConst8Section)
377 return MergeableConst8Section;
378 if (Kind.isMergeableConst16() && MergeableConst16Section)
379 return MergeableConst16Section;
David Majnemer964b70d2016-02-22 22:23:11 +0000380 if (Kind.isMergeableConst32() && MergeableConst32Section)
381 return MergeableConst32Section;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000382 if (Kind.isReadOnly())
383 return ReadOnlySection;
384
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000385 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
386 return DataRelROSection;
387}
388
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000389static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
390 bool IsCtor, unsigned Priority,
391 const MCSymbol *KeySym) {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000392 std::string Name;
393 unsigned Type;
394 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
Rafael Espindolac4b42532014-09-04 23:03:58 +0000395 StringRef COMDAT = KeySym ? KeySym->getName() : "";
396
397 if (KeySym)
398 Flags |= ELF::SHF_GROUP;
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000399
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000400 if (UseInitArray) {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000401 if (IsCtor) {
402 Type = ELF::SHT_INIT_ARRAY;
403 Name = ".init_array";
404 } else {
405 Type = ELF::SHT_FINI_ARRAY;
406 Name = ".fini_array";
407 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000408 if (Priority != 65535) {
409 Name += '.';
410 Name += utostr(Priority);
411 }
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000412 } else {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000413 // The default scheme is .ctor / .dtor, so we have to invert the priority
414 // numbering.
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000415 if (IsCtor)
416 Name = ".ctors";
417 else
418 Name = ".dtors";
Rafael Espindolac4b42532014-09-04 23:03:58 +0000419 if (Priority != 65535) {
420 Name += '.';
421 Name += utostr(65535 - Priority);
422 }
423 Type = ELF::SHT_PROGBITS;
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000424 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000425
Rafael Espindolaba31e272015-01-29 17:33:21 +0000426 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000427}
428
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000429MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000430 unsigned Priority, const MCSymbol *KeySym) const {
431 return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
432 KeySym);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000433}
434
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000435MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000436 unsigned Priority, const MCSymbol *KeySym) const {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000437 return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
438 KeySym);
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000439}
440
441void
442TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
443 UseInitArray = UseInitArray_;
444 if (!UseInitArray)
445 return;
446
Rafael Espindolaba31e272015-01-29 17:33:21 +0000447 StaticCtorSection = getContext().getELFSection(
448 ".init_array", ELF::SHT_INIT_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC);
449 StaticDtorSection = getContext().getELFSection(
450 ".fini_array", ELF::SHT_FINI_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000451}
452
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000453//===----------------------------------------------------------------------===//
454// MachO
455//===----------------------------------------------------------------------===//
456
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000457TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
458 : TargetLoweringObjectFile() {
459 SupportIndirectSymViaGOTPCRel = true;
460}
461
Daniel Dunbar95856122013-01-18 19:37:00 +0000462/// emitModuleFlags - Perform code emission for module flags.
Bill Wendling06df7722012-02-14 21:28:13 +0000463void TargetLoweringObjectFileMachO::
Bill Wendling734909a2012-02-15 22:36:15 +0000464emitModuleFlags(MCStreamer &Streamer,
465 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +0000466 Mangler &Mang, const TargetMachine &TM) const {
Bill Wendling06df7722012-02-14 21:28:13 +0000467 unsigned VersionVal = 0;
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000468 unsigned ImageInfoFlags = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000469 MDNode *LinkerOptions = nullptr;
Bill Wendling734909a2012-02-15 22:36:15 +0000470 StringRef SectionVal;
Bill Wendling06df7722012-02-14 21:28:13 +0000471
Bill Wendling734909a2012-02-15 22:36:15 +0000472 for (ArrayRef<Module::ModuleFlagEntry>::iterator
473 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
474 const Module::ModuleFlagEntry &MFE = *i;
Bill Wendling06df7722012-02-14 21:28:13 +0000475
476 // Ignore flags with 'Require' behavior.
Bill Wendling734909a2012-02-15 22:36:15 +0000477 if (MFE.Behavior == Module::Require)
Bill Wendling06df7722012-02-14 21:28:13 +0000478 continue;
479
Bill Wendling734909a2012-02-15 22:36:15 +0000480 StringRef Key = MFE.Key->getString();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000481 Metadata *Val = MFE.Val;
Bill Wendling06df7722012-02-14 21:28:13 +0000482
Daniel Dunbar95856122013-01-18 19:37:00 +0000483 if (Key == "Objective-C Image Info Version") {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000484 VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000485 } else if (Key == "Objective-C Garbage Collection" ||
486 Key == "Objective-C GC Only" ||
Manman Renc98ec0e2014-11-21 19:24:55 +0000487 Key == "Objective-C Is Simulated" ||
Manman Renc77e0ff2016-01-29 23:51:00 +0000488 Key == "Objective-C Class Properties" ||
Manman Renc98ec0e2014-11-21 19:24:55 +0000489 Key == "Objective-C Image Swift Version") {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000490 ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue();
Daniel Dunbar95856122013-01-18 19:37:00 +0000491 } else if (Key == "Objective-C Image Info Section") {
Bill Wendling734909a2012-02-15 22:36:15 +0000492 SectionVal = cast<MDString>(Val)->getString();
Daniel Dunbar95856122013-01-18 19:37:00 +0000493 } else if (Key == "Linker Options") {
494 LinkerOptions = cast<MDNode>(Val);
495 }
496 }
497
498 // Emit the linker options if present.
499 if (LinkerOptions) {
500 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
501 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
502 SmallVector<std::string, 4> StrOptions;
503
504 // Convert to strings.
505 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
506 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
507 StrOptions.push_back(MDOption->getString());
508 }
509
510 Streamer.EmitLinkerOptions(StrOptions);
511 }
Bill Wendling06df7722012-02-14 21:28:13 +0000512 }
513
Bill Wendling734909a2012-02-15 22:36:15 +0000514 // The section is mandatory. If we don't have it, then we don't have GC info.
515 if (SectionVal.empty()) return;
Bill Wendling06df7722012-02-14 21:28:13 +0000516
Bill Wendling734909a2012-02-15 22:36:15 +0000517 StringRef Segment, Section;
518 unsigned TAA = 0, StubSize = 0;
519 bool TAAParsed;
520 std::string ErrorCode =
521 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
522 TAA, TAAParsed, StubSize);
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000523 if (!ErrorCode.empty())
Bill Wendling734909a2012-02-15 22:36:15 +0000524 // If invalid, report the error with report_fatal_error.
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000525 report_fatal_error("Invalid section specifier '" + Section + "': " +
526 ErrorCode + ".");
Bill Wendling06df7722012-02-14 21:28:13 +0000527
Bill Wendling734909a2012-02-15 22:36:15 +0000528 // Get the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000529 MCSectionMachO *S = getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000530 Segment, Section, TAA, StubSize, SectionKind::getData());
Bill Wendling734909a2012-02-15 22:36:15 +0000531 Streamer.SwitchSection(S);
532 Streamer.EmitLabel(getContext().
Jim Grosbach6f482002015-05-18 18:43:14 +0000533 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendling06df7722012-02-14 21:28:13 +0000534 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000535 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendling06df7722012-02-14 21:28:13 +0000536 Streamer.AddBlankLine();
537}
538
David Majnemerdad0a642014-06-27 18:19:56 +0000539static void checkMachOComdat(const GlobalValue *GV) {
540 const Comdat *C = GV->getComdat();
541 if (!C)
542 return;
543
544 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
545 "' cannot be lowered.");
546}
547
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000548MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000549 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
550 const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000551 // Parse the section specifier and create it if valid.
552 StringRef Segment, Section;
Stuart Hastings12d53122011-03-19 02:42:31 +0000553 unsigned TAA = 0, StubSize = 0;
554 bool TAAParsed;
David Majnemerdad0a642014-06-27 18:19:56 +0000555
556 checkMachOComdat(GV);
557
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000558 std::string ErrorCode =
559 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000560 TAA, TAAParsed, StubSize);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000561 if (!ErrorCode.empty()) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000562 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000563 report_fatal_error("Global variable '" + GV->getName() +
564 "' has an invalid section specifier '" +
565 GV->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000566 }
567
568 // Get the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000569 MCSectionMachO *S =
570 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000571
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000572 // If TAA wasn't set by ParseSectionSpecifier() above,
573 // use the value returned by getMachOSection() as a default.
Stuart Hastings12d53122011-03-19 02:42:31 +0000574 if (!TAAParsed)
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000575 TAA = S->getTypeAndAttributes();
576
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000577 // Okay, now that we got the section, verify that the TAA & StubSize agree.
578 // If the user declared multiple globals with different section flags, we need
579 // to reject it here.
580 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000581 // If invalid, report the error with report_fatal_error.
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000582 report_fatal_error("Global variable '" + GV->getName() +
583 "' section type or attributes does not match previous"
584 " section specifier");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000585 }
586
587 return S;
588}
589
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000590MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
591 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
592 const TargetMachine &TM) const {
David Majnemerdad0a642014-06-27 18:19:56 +0000593 checkMachOComdat(GV);
Bill Wendling25b61db2013-11-17 10:53:13 +0000594
595 // Handle thread local data.
596 if (Kind.isThreadBSS()) return TLSBSSSection;
597 if (Kind.isThreadData()) return TLSDataSection;
598
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000599 if (Kind.isText())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000600 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
601
602 // If this is weak/linkonce, put this in a coalescable section, either in text
603 // or data depending on if it is writable.
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000604 if (GV->isWeakForLinker()) {
605 if (Kind.isReadOnly())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000606 return ConstTextCoalSection;
607 return DataCoalSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000608 }
609
610 // FIXME: Alignment check should be handled by section classifier.
Chris Lattneref2f8042010-03-07 04:28:09 +0000611 if (Kind.isMergeable1ByteCString() &&
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000612 GV->getParent()->getDataLayout().getPreferredAlignment(
613 cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000614 return CStringSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000615
Chris Lattneref2f8042010-03-07 04:28:09 +0000616 // Do not put 16-bit arrays in the UString section if they have an
617 // externally visible label, this runs into issues with certain linker
618 // versions.
619 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000620 GV->getParent()->getDataLayout().getPreferredAlignment(
621 cast<GlobalVariable>(GV)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000622 return UStringSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000623
Rafael Espindolab43d51d2014-08-28 20:13:31 +0000624 // With MachO only variables whose corresponding symbol starts with 'l' or
625 // 'L' can be merged, so we only try merging GVs with private linkage.
626 if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000627 if (Kind.isMergeableConst4())
628 return FourByteConstantSection;
629 if (Kind.isMergeableConst8())
630 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000631 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000632 return SixteenByteConstantSection;
633 }
634
635 // Otherwise, if it is readonly, but not something we can specially optimize,
636 // just drop it in .const.
637 if (Kind.isReadOnly())
638 return ReadOnlySection;
639
640 // If this is marked const, put it into a const section. But if the dynamic
641 // linker needs to write to it, put it in the data segment.
642 if (Kind.isReadOnlyWithRel())
643 return ConstDataSection;
644
645 // Put zero initialized globals with strong external linkage in the
646 // DATA, __common section with the .zerofill directive.
647 if (Kind.isBSSExtern())
648 return DataCommonSection;
649
650 // Put zero initialized globals with local linkage in __DATA,__bss directive
651 // with the .zerofill directive (aka .lcomm).
652 if (Kind.isBSSLocal())
653 return DataBSSSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000654
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000655 // Otherwise, just drop the variable in the normal data section.
656 return DataSection;
657}
658
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000659MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
David Majnemera3ea4072016-02-21 01:30:30 +0000660 const DataLayout &DL, SectionKind Kind, const Constant *C,
661 unsigned &Align) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000662 // If this constant requires a relocation, we have to put it in the data
663 // segment, not in the text segment.
Rafael Espindola449711c2015-11-18 06:02:15 +0000664 if (Kind.isData() || Kind.isReadOnlyWithRel())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000665 return ConstDataSection;
666
667 if (Kind.isMergeableConst4())
668 return FourByteConstantSection;
669 if (Kind.isMergeableConst8())
670 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000671 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000672 return SixteenByteConstantSection;
673 return ReadOnlySection; // .const
674}
675
Rafael Espindola15b26692014-02-09 14:50:44 +0000676const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
677 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000678 const TargetMachine &TM, MachineModuleInfo *MMI,
679 MCStreamer &Streamer) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000680 // The mach-o version of this method defaults to returning a stub reference.
681
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000682 if (Encoding & DW_EH_PE_indirect) {
683 MachineModuleInfoMachO &MachOMMI =
684 MMI->getObjFileInfo<MachineModuleInfoMachO>();
685
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000686 MCSymbol *SSym =
687 getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000688
689 // Add information about the stub reference to MachOMMI so that the stub
690 // gets emitted by the asmprinter.
Bill Wendling57e3aaa2011-10-24 23:05:43 +0000691 MachineModuleInfoImpl::StubValueTy &StubSym =
692 GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) :
693 MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000694 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000695 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000696 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000697 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000698
699 return TargetLoweringObjectFile::
Jim Grosbach13760bd2015-05-30 01:25:56 +0000700 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Anton Korobeynikove42af362012-11-14 01:47:00 +0000701 Encoding & ~dwarf::DW_EH_PE_indirect, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000702 }
703
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000704 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang,
705 TM, MMI, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000706}
707
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000708MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
709 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
710 MachineModuleInfo *MMI) const {
Rafael Espindola08704342011-04-27 23:08:15 +0000711 // The mach-o version of this method defaults to returning a stub reference.
712 MachineModuleInfoMachO &MachOMMI =
713 MMI->getObjFileInfo<MachineModuleInfoMachO>();
714
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000715 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM);
Rafael Espindola08704342011-04-27 23:08:15 +0000716
717 // Add information about the stub reference to MachOMMI so that the stub
718 // gets emitted by the asmprinter.
Bill Wendlinge4cc3322011-11-29 01:43:20 +0000719 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000720 if (!StubSym.getPointer()) {
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000721 MCSymbol *Sym = TM.getSymbol(GV, Mang);
Rafael Espindola08704342011-04-27 23:08:15 +0000722 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
723 }
724
725 return SSym;
726}
727
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000728const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
729 const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
730 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000731 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000732 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
733 // through a non_lazy_ptr stub instead. One advantage is that it allows the
734 // computation of deltas to final external symbols. Example:
735 //
736 // _extgotequiv:
737 // .long _extfoo
738 //
739 // _delta:
740 // .long _extgotequiv-_delta
741 //
742 // is transformed to:
743 //
744 // _delta:
745 // .long L_extfoo$non_lazy_ptr-(_delta+0)
746 //
747 // .section __IMPORT,__pointers,non_lazy_symbol_pointers
748 // L_extfoo$non_lazy_ptr:
749 // .indirect_symbol _extfoo
750 // .long 0
751 //
752 MachineModuleInfoMachO &MachOMMI =
753 MMI->getObjFileInfo<MachineModuleInfoMachO>();
754 MCContext &Ctx = getContext();
755
756 // The offset must consider the original displacement from the base symbol
757 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
758 Offset = -MV.getConstant();
759 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
760
761 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
762 // non_lazy_ptr stubs.
763 SmallString<128> Name;
764 StringRef Suffix = "$non_lazy_ptr";
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000765 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000766 Name += Sym->getName();
767 Name += Suffix;
Jim Grosbach6f482002015-05-18 18:43:14 +0000768 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000769
770 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
771 if (!StubSym.getPointer())
772 StubSym = MachineModuleInfoImpl::
773 StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */);
774
775 const MCExpr *BSymExpr =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000776 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000777 const MCExpr *LHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000778 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000779
780 if (!Offset)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000781 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000782
783 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000784 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
785 return MCBinaryExpr::createSub(LHS, RHS, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000786}
787
Peter Collingbourne94d77862015-11-03 23:40:03 +0000788static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
789 const MCSection &Section) {
790 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
791 return true;
792
793 // If it is not dead stripped, it is safe to use private labels.
794 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
795 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
796 return true;
797
798 return false;
799}
800
801void TargetLoweringObjectFileMachO::getNameWithPrefix(
802 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang,
803 const TargetMachine &TM) const {
804 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
805 const MCSection *TheSection = SectionForGlobal(GV, GVKind, Mang, TM);
806 bool CannotUsePrivateLabel =
807 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
808 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
809}
810
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000811//===----------------------------------------------------------------------===//
812// COFF
813//===----------------------------------------------------------------------===//
814
Chris Lattner87cffa92010-05-07 17:17:41 +0000815static unsigned
816getCOFFSectionFlags(SectionKind K) {
817 unsigned Flags = 0;
818
Anton Korobeynikove4152302010-07-06 15:24:56 +0000819 if (K.isMetadata())
Chris Lattner02844932010-05-07 21:49:09 +0000820 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000821 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000822 else if (K.isText())
823 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000824 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer0f83d962010-10-27 18:52:29 +0000825 COFF::IMAGE_SCN_MEM_READ |
Daniel Dunbar329d2022010-07-01 20:07:24 +0000826 COFF::IMAGE_SCN_CNT_CODE;
David Majnemerb8dbebb2014-09-20 07:31:46 +0000827 else if (K.isBSS())
Chris Lattner02844932010-05-07 21:49:09 +0000828 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000829 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
830 COFF::IMAGE_SCN_MEM_READ |
831 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000832 else if (K.isThreadLocal())
833 Flags |=
834 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
835 COFF::IMAGE_SCN_MEM_READ |
836 COFF::IMAGE_SCN_MEM_WRITE;
David Majnemerb8dbebb2014-09-20 07:31:46 +0000837 else if (K.isReadOnly() || K.isReadOnlyWithRel())
Chris Lattner87cffa92010-05-07 17:17:41 +0000838 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000839 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
840 COFF::IMAGE_SCN_MEM_READ;
Chris Lattner87cffa92010-05-07 17:17:41 +0000841 else if (K.isWriteable())
842 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000843 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
844 COFF::IMAGE_SCN_MEM_READ |
845 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000846
847 return Flags;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000848}
849
Benjamin Kramer6cbe6702014-07-07 14:47:51 +0000850static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
David Majnemerdad0a642014-06-27 18:19:56 +0000851 const Comdat *C = GV->getComdat();
852 assert(C && "expected GV to have a Comdat!");
853
854 StringRef ComdatGVName = C->getName();
855 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
856 if (!ComdatGV)
857 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
858 "' does not exist.");
859
860 if (ComdatGV->getComdat() != C)
861 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
Hans Wennborgc0f0c512014-09-19 01:14:56 +0000862 "' is not a key for its COMDAT.");
David Majnemerdad0a642014-06-27 18:19:56 +0000863
864 return ComdatGV;
865}
866
867static int getSelectionForCOFF(const GlobalValue *GV) {
868 if (const Comdat *C = GV->getComdat()) {
869 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
870 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
871 ComdatKey = GA->getBaseObject();
872 if (ComdatKey == GV) {
873 switch (C->getSelectionKind()) {
874 case Comdat::Any:
875 return COFF::IMAGE_COMDAT_SELECT_ANY;
876 case Comdat::ExactMatch:
877 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
878 case Comdat::Largest:
879 return COFF::IMAGE_COMDAT_SELECT_LARGEST;
880 case Comdat::NoDuplicates:
881 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
882 case Comdat::SameSize:
883 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
884 }
885 } else {
886 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
887 }
David Majnemerdad0a642014-06-27 18:19:56 +0000888 }
889 return 0;
890}
891
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000892MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000893 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
894 const TargetMachine &TM) const {
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000895 int Selection = 0;
896 unsigned Characteristics = getCOFFSectionFlags(Kind);
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000897 StringRef Name = GV->getSection();
898 StringRef COMDATSymName = "";
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000899 if (GV->hasComdat()) {
David Majnemerdad0a642014-06-27 18:19:56 +0000900 Selection = getSelectionForCOFF(GV);
901 const GlobalValue *ComdatGV;
902 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
903 ComdatGV = getComdatGVForCOFF(GV);
904 else
905 ComdatGV = GV;
906
907 if (!ComdatGV->hasPrivateLinkage()) {
908 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang);
909 COMDATSymName = Sym->getName();
910 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
911 } else {
912 Selection = 0;
913 }
Michael J. Spencerf1aef752012-11-13 22:04:09 +0000914 }
915 return getContext().getCOFFSection(Name,
916 Characteristics,
Nico Riecka37acf72013-07-06 12:13:10 +0000917 Kind,
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000918 COMDATSymName,
Nico Riecka37acf72013-07-06 12:13:10 +0000919 Selection);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000920}
921
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000922static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000923 if (Kind.isText())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000924 return ".text";
David Majnemera9bdb322014-04-08 22:33:40 +0000925 if (Kind.isBSS())
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000926 return ".bss";
927 if (Kind.isThreadLocal())
Rafael Espindola3c8e1472013-11-27 15:52:11 +0000928 return ".tls$";
David Majnemer597be2d2014-09-22 20:39:23 +0000929 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
930 return ".rdata";
931 return ".data";
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000932}
933
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000934MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
935 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
936 const TargetMachine &TM) const {
David Majnemer93389842014-03-23 17:47:39 +0000937 // If we have -ffunction-sections then we should emit the global value to a
938 // uniqued section specifically for it.
David Majnemer273bff42014-03-25 06:14:26 +0000939 bool EmitUniquedSection;
940 if (Kind.isText())
941 EmitUniquedSection = TM.getFunctionSections();
942 else
943 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000944
Rafael Espindola12ca34f2015-01-19 15:16:06 +0000945 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +0000946 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Chris Lattner02844932010-05-07 21:49:09 +0000947 unsigned Characteristics = getCOFFSectionFlags(Kind);
948
Daniel Dunbar329d2022010-07-01 20:07:24 +0000949 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
David Majnemerdad0a642014-06-27 18:19:56 +0000950 int Selection = getSelectionForCOFF(GV);
951 if (!Selection)
952 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
953 const GlobalValue *ComdatGV;
954 if (GV->hasComdat())
955 ComdatGV = getComdatGVForCOFF(GV);
956 else
957 ComdatGV = GV;
958
959 if (!ComdatGV->hasPrivateLinkage()) {
960 MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang);
961 StringRef COMDATSymName = Sym->getName();
962 return getContext().getCOFFSection(Name, Characteristics, Kind,
963 COMDATSymName, Selection);
David Majnemer7db449a2015-03-17 23:54:51 +0000964 } else {
965 SmallString<256> TmpData;
Peter Collingbourne94d77862015-11-03 23:40:03 +0000966 Mang.getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true);
David Majnemer7db449a2015-03-17 23:54:51 +0000967 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
968 Selection);
David Majnemerdad0a642014-06-27 18:19:56 +0000969 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000970 }
971
972 if (Kind.isText())
David Majnemer3d96acb2013-08-13 01:23:53 +0000973 return TextSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000974
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000975 if (Kind.isThreadLocal())
David Majnemer3d96acb2013-08-13 01:23:53 +0000976 return TLSDataSection;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000977
David Majnemer597be2d2014-09-22 20:39:23 +0000978 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
David Majnemerf76d6b32013-08-08 01:50:52 +0000979 return ReadOnlySection;
980
David Majnemera9bdb322014-04-08 22:33:40 +0000981 // Note: we claim that common symbols are put in BSSSection, but they are
982 // really emitted with the magic .comm directive, which creates a symbol table
983 // entry but not a section.
984 if (Kind.isBSS() || Kind.isCommon())
David Majnemer3d96acb2013-08-13 01:23:53 +0000985 return BSSSection;
986
987 return DataSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000988}
989
David Majnemer7db449a2015-03-17 23:54:51 +0000990void TargetLoweringObjectFileCOFF::getNameWithPrefix(
Peter Collingbourne94d77862015-11-03 23:40:03 +0000991 SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang,
992 const TargetMachine &TM) const {
993 bool CannotUsePrivateLabel = false;
David Majnemer7db449a2015-03-17 23:54:51 +0000994 if (GV->hasPrivateLinkage() &&
995 ((isa<Function>(GV) && TM.getFunctionSections()) ||
996 (isa<GlobalVariable>(GV) && TM.getDataSections())))
997 CannotUsePrivateLabel = true;
998
999 Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
1000}
1001
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001002MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
Rafael Espindolaab447e42015-03-11 19:58:37 +00001003 const Function &F, Mangler &Mang, const TargetMachine &TM) const {
1004 // If the function can be removed, produce a unique section so that
1005 // the table doesn't prevent the removal.
1006 const Comdat *C = F.getComdat();
1007 bool EmitUniqueSection = TM.getFunctionSections() || C;
1008 if (!EmitUniqueSection)
1009 return ReadOnlySection;
1010
1011 // FIXME: we should produce a symbol for F instead.
1012 if (F.hasPrivateLinkage())
1013 return ReadOnlySection;
1014
1015 MCSymbol *Sym = TM.getSymbol(&F, Mang);
1016 StringRef COMDATSymName = Sym->getName();
1017
1018 SectionKind Kind = SectionKind::getReadOnly();
1019 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
1020 unsigned Characteristics = getCOFFSectionFlags(Kind);
1021 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1022
1023 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
1024 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
1025}
1026
Reid Klecknerd973ca32013-04-25 19:34:41 +00001027void TargetLoweringObjectFileCOFF::
1028emitModuleFlags(MCStreamer &Streamer,
1029 ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Rafael Espindolafa0f7282014-02-08 14:53:28 +00001030 Mangler &Mang, const TargetMachine &TM) const {
Craig Topperc0196b12014-04-14 00:51:57 +00001031 MDNode *LinkerOptions = nullptr;
Reid Klecknerd973ca32013-04-25 19:34:41 +00001032
1033 // Look for the "Linker Options" flag, since it's the only one we support.
1034 for (ArrayRef<Module::ModuleFlagEntry>::iterator
1035 i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
1036 const Module::ModuleFlagEntry &MFE = *i;
1037 StringRef Key = MFE.Key->getString();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001038 Metadata *Val = MFE.Val;
Reid Klecknerd973ca32013-04-25 19:34:41 +00001039 if (Key == "Linker Options") {
1040 LinkerOptions = cast<MDNode>(Val);
1041 break;
1042 }
1043 }
1044 if (!LinkerOptions)
1045 return;
1046
1047 // Emit the linker options to the linker .drectve section. According to the
1048 // spec, this section is a space-separated string containing flags for linker.
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001049 MCSection *Sec = getDrectveSection();
Reid Klecknerd973ca32013-04-25 19:34:41 +00001050 Streamer.SwitchSection(Sec);
1051 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
1052 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
1053 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
1054 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
Reid Klecknerd973ca32013-04-25 19:34:41 +00001055 // Lead with a space for consistency with our dllexport implementation.
Michael Kupersteinfc3e6262015-02-16 11:57:17 +00001056 std::string Directive(" ");
1057 Directive.append(MDOption->getString());
1058 Streamer.EmitBytes(Directive);
Reid Klecknerd973ca32013-04-25 19:34:41 +00001059 }
1060 }
1061}
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001062
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001063MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +00001064 unsigned Priority, const MCSymbol *KeySym) const {
Reid Kleckner7c4059e2014-09-04 17:42:03 +00001065 return getContext().getAssociativeCOFFSection(
1066 cast<MCSectionCOFF>(StaticCtorSection), KeySym);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001067}
1068
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001069MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +00001070 unsigned Priority, const MCSymbol *KeySym) const {
Reid Kleckner7c4059e2014-09-04 17:42:03 +00001071 return getContext().getAssociativeCOFFSection(
1072 cast<MCSectionCOFF>(StaticDtorSection), KeySym);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001073}
Peter Collingbourneaef36592015-06-29 22:04:09 +00001074
1075void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
1076 raw_ostream &OS, const GlobalValue *GV, const Mangler &Mang) const {
1077 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
1078 return;
1079
1080 const Triple &TT = getTargetTriple();
1081
1082 if (TT.isKnownWindowsMSVCEnvironment())
1083 OS << " /EXPORT:";
1084 else
1085 OS << " -export:";
1086
1087 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
1088 std::string Flag;
1089 raw_string_ostream FlagOS(Flag);
1090 Mang.getNameWithPrefix(FlagOS, GV, false);
1091 FlagOS.flush();
Mehdi Amini5c0fa582015-07-16 06:04:17 +00001092 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
Peter Collingbourneaef36592015-06-29 22:04:09 +00001093 OS << Flag.substr(1);
1094 else
1095 OS << Flag;
1096 } else {
1097 Mang.getNameWithPrefix(OS, GV, false);
1098 }
1099
1100 if (!GV->getValueType()->isFunctionTy()) {
1101 if (TT.isKnownWindowsMSVCEnvironment())
1102 OS << ",DATA";
1103 else
1104 OS << ",data";
1105 }
1106}