blob: a4a54e1fb24f8b485a890df8ebf4561434aa48e3 [file] [log] [blame]
Eugene Zelenkofa912a72017-02-27 22:45:06 +00001//===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===//
Anton Korobeynikovab663a02010-02-15 22:37:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements classes used to handle lowerings specific to common
11// object file formats.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallString.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000017#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000019#include "llvm/ADT/StringRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000021#include "llvm/BinaryFormat/COFF.h"
22#include "llvm/BinaryFormat/Dwarf.h"
23#include "llvm/BinaryFormat/ELF.h"
24#include "llvm/BinaryFormat/MachO.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000027#include "llvm/IR/Comdat.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Constants.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000032#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalObject.h"
34#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/GlobalVariable.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000036#include "llvm/IR/Mangler.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000037#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/Module.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000039#include "llvm/IR/Type.h"
Peter Collingbourne94d77862015-11-03 23:40:03 +000040#include "llvm/MC/MCAsmInfo.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000041#include "llvm/MC/MCContext.h"
42#include "llvm/MC/MCExpr.h"
Chris Lattner87cffa92010-05-07 17:17:41 +000043#include "llvm/MC/MCSectionCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000044#include "llvm/MC/MCSectionELF.h"
45#include "llvm/MC/MCSectionMachO.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000046#include "llvm/MC/MCSectionWasm.h"
Rafael Espindolaa83b1772011-04-16 03:51:21 +000047#include "llvm/MC/MCStreamer.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000048#include "llvm/MC/MCSymbol.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000049#include "llvm/MC/MCSymbolELF.h"
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +000050#include "llvm/MC/MCValue.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000051#include "llvm/MC/SectionKind.h"
Xinliang David Li5f04f922016-01-14 18:09:45 +000052#include "llvm/ProfileData/InstrProf.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000053#include "llvm/Support/Casting.h"
54#include "llvm/Support/CodeGen.h"
Martin Storsjo04b68442017-11-28 08:07:18 +000055#include "llvm/Support/Format.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000056#include "llvm/Support/ErrorHandling.h"
57#include "llvm/Support/raw_ostream.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000058#include "llvm/Target/TargetMachine.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000059#include <cassert>
60#include <string>
61
Anton Korobeynikovab663a02010-02-15 22:37:53 +000062using namespace llvm;
Anton Korobeynikov31a92122010-02-21 20:28:15 +000063using namespace dwarf;
Anton Korobeynikovab663a02010-02-15 22:37:53 +000064
Peter Collingbourne89061b22017-06-12 20:10:48 +000065static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
Saleem Abdulrasool4c474342017-06-05 21:26:39 +000066 StringRef &Section) {
Peter Collingbourne89061b22017-06-12 20:10:48 +000067 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
68 M.getModuleFlagsMetadata(ModuleFlags);
69
Saleem Abdulrasool4c474342017-06-05 21:26:39 +000070 for (const auto &MFE: ModuleFlags) {
71 // Ignore flags with 'Require' behaviour.
72 if (MFE.Behavior == Module::Require)
73 continue;
74
75 StringRef Key = MFE.Key->getString();
76 if (Key == "Objective-C Image Info Version") {
77 Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
78 } else if (Key == "Objective-C Garbage Collection" ||
79 Key == "Objective-C GC Only" ||
80 Key == "Objective-C Is Simulated" ||
81 Key == "Objective-C Class Properties" ||
82 Key == "Objective-C Image Swift Version") {
83 Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
84 } else if (Key == "Objective-C Image Info Section") {
85 Section = cast<MDString>(MFE.Val)->getString();
86 }
87 }
88}
89
Anton Korobeynikovab663a02010-02-15 22:37:53 +000090//===----------------------------------------------------------------------===//
91// ELF
92//===----------------------------------------------------------------------===//
Anton Korobeynikovab663a02010-02-15 22:37:53 +000093
Peter Collingbourne89061b22017-06-12 20:10:48 +000094void TargetLoweringObjectFileELF::emitModuleMetadata(
95 MCStreamer &Streamer, Module &M, const TargetMachine &TM) const {
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +000096 auto &C = getContext();
97
98 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
99 auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
100 ELF::SHF_EXCLUDE);
101
102 Streamer.SwitchSection(S);
103
104 for (const auto &Operand : LinkerOptions->operands()) {
105 if (cast<MDNode>(Operand)->getNumOperands() != 2)
106 report_fatal_error("invalid llvm.linker.options");
107 for (const auto &Option : cast<MDNode>(Operand)->operands()) {
108 Streamer.EmitBytes(cast<MDString>(Option)->getString());
109 Streamer.EmitIntValue(0, 1);
110 }
111 }
112 }
113
Saleem Abdulrasool4c474342017-06-05 21:26:39 +0000114 unsigned Version = 0;
115 unsigned Flags = 0;
116 StringRef Section;
117
Peter Collingbourne89061b22017-06-12 20:10:48 +0000118 GetObjCImageInfo(M, Version, Flags, Section);
Saleem Abdulrasool4c474342017-06-05 21:26:39 +0000119 if (Section.empty())
120 return;
121
Saleem Abdulrasool4c474342017-06-05 21:26:39 +0000122 auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
123 Streamer.SwitchSection(S);
124 Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
125 Streamer.EmitIntValue(Version, 4);
126 Streamer.EmitIntValue(Flags, 4);
127 Streamer.AddBlankLine();
128}
129
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000130MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000131 const GlobalValue *GV, const TargetMachine &TM,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000132 MachineModuleInfo *MMI) const {
Rafael Espindolace83fc32011-04-27 23:17:57 +0000133 unsigned Encoding = getPersonalityEncoding();
Eugene Zelenkofa912a72017-02-27 22:45:06 +0000134 if ((Encoding & 0x80) == DW_EH_PE_indirect)
Jim Grosbach6f482002015-05-18 18:43:14 +0000135 return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
Tim Northoverb64fb452016-11-22 16:17:20 +0000136 TM.getSymbol(GV)->getName());
Eugene Zelenkofa912a72017-02-27 22:45:06 +0000137 if ((Encoding & 0x70) == DW_EH_PE_absptr)
Tim Northoverb64fb452016-11-22 16:17:20 +0000138 return TM.getSymbol(GV);
Logan Chienc0029812014-05-30 16:48:56 +0000139 report_fatal_error("We do not support this DWARF encoding yet!");
Rafael Espindolaa83b1772011-04-16 03:51:21 +0000140}
141
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000142void TargetLoweringObjectFileELF::emitPersonalityValue(
143 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
Rafael Espindola51d2d7a2011-06-13 03:09:13 +0000144 SmallString<64> NameData("DW.ref.");
145 NameData += Sym->getName();
Rafael Espindolaa8695762015-06-02 00:25:12 +0000146 MCSymbolELF *Label =
147 cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
Rafael Espindola39897762011-04-27 21:29:52 +0000148 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
149 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindola39897762011-04-27 21:29:52 +0000150 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
Eric Christopher36e601c2016-07-01 06:07:38 +0000151 MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
152 ELF::SHT_PROGBITS, Flags, 0);
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000153 unsigned Size = DL.getPointerSize();
Rafael Espindola39897762011-04-27 21:29:52 +0000154 Streamer.SwitchSection(Sec);
Fangrui Songe73534462017-11-15 06:17:32 +0000155 Streamer.EmitValueToAlignment(DL.getPointerABIAlignment(0));
Rafael Espindola39897762011-04-27 21:29:52 +0000156 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000157 const MCExpr *E = MCConstantExpr::create(Size, getContext());
Rafael Espindolaa8695762015-06-02 00:25:12 +0000158 Streamer.emitELFSize(Label, E);
Rafael Espindola39897762011-04-27 21:29:52 +0000159 Streamer.EmitLabel(Label);
Rafael Espindolaa83b1772011-04-16 03:51:21 +0000160
Rafael Espindola39897762011-04-27 21:29:52 +0000161 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindolaa83b1772011-04-16 03:51:21 +0000162}
163
Rafael Espindola15b26692014-02-09 14:50:44 +0000164const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000165 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
166 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Eugene Zelenkofa912a72017-02-27 22:45:06 +0000167 if (Encoding & DW_EH_PE_indirect) {
Anton Korobeynikove42af362012-11-14 01:47:00 +0000168 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
169
Eric Christopher4367c7f2016-09-16 07:33:15 +0000170 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000171
172 // Add information about the stub reference to ELFMMI so that the stub
173 // gets emitted by the asmprinter.
Anton Korobeynikove42af362012-11-14 01:47:00 +0000174 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000175 if (!StubSym.getPointer()) {
Tim Northoverb64fb452016-11-22 16:17:20 +0000176 MCSymbol *Sym = TM.getSymbol(GV);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000177 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
178 }
179
180 return TargetLoweringObjectFile::
Jim Grosbach13760bd2015-05-30 01:25:56 +0000181 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Eugene Zelenkofa912a72017-02-27 22:45:06 +0000182 Encoding & ~DW_EH_PE_indirect, Streamer);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000183 }
184
Eric Christopher4367c7f2016-09-16 07:33:15 +0000185 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
186 MMI, Streamer);
Anton Korobeynikove42af362012-11-14 01:47:00 +0000187}
188
Sam Clegg12fd3da2017-10-20 21:28:38 +0000189static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
Rafael Espindola0d018b12011-05-24 03:10:31 +0000190 // N.B.: The defaults used in here are no the same ones used in MC.
191 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
192 // both gas and MC will produce a section with no flags. Given
Bill Wendlingd1634052012-07-19 00:04:14 +0000193 // section(".eh_frame") gcc will produce:
194 //
195 // .section .eh_frame,"a",@progbits
Xinliang David Li4a5ddf82017-04-14 17:48:40 +0000196
Vedant Kumar1a6a2b62017-04-15 00:09:57 +0000197 if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF,
198 /*AddSegmentInfo=*/false))
Xinliang David Li5f04f922016-01-14 18:09:45 +0000199 return SectionKind::getMetadata();
200
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000201 if (Name.empty() || Name[0] != '.') return K;
202
203 // Some lame default implementation based on some magic section names.
204 if (Name == ".bss" ||
205 Name.startswith(".bss.") ||
206 Name.startswith(".gnu.linkonce.b.") ||
207 Name.startswith(".llvm.linkonce.b.") ||
208 Name == ".sbss" ||
209 Name.startswith(".sbss.") ||
210 Name.startswith(".gnu.linkonce.sb.") ||
211 Name.startswith(".llvm.linkonce.sb."))
212 return SectionKind::getBSS();
213
214 if (Name == ".tdata" ||
215 Name.startswith(".tdata.") ||
216 Name.startswith(".gnu.linkonce.td.") ||
217 Name.startswith(".llvm.linkonce.td."))
218 return SectionKind::getThreadData();
219
220 if (Name == ".tbss" ||
221 Name.startswith(".tbss.") ||
222 Name.startswith(".gnu.linkonce.tb.") ||
223 Name.startswith(".llvm.linkonce.tb."))
224 return SectionKind::getThreadBSS();
225
226 return K;
227}
228
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000229static unsigned getELFSectionType(StringRef Name, SectionKind K) {
Petr Hosek12903552016-09-20 20:21:13 +0000230 // Use SHT_NOTE for section whose name starts with ".note" to allow
231 // emitting ELF notes from C variable declaration.
232 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
233 if (Name.startswith(".note"))
234 return ELF::SHT_NOTE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000235
236 if (Name == ".init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000237 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000238
239 if (Name == ".fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000240 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000241
242 if (Name == ".preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000243 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000244
245 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolaaea49582011-01-23 04:28:49 +0000246 return ELF::SHT_NOBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000247
Rafael Espindolaaea49582011-01-23 04:28:49 +0000248 return ELF::SHT_PROGBITS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000249}
250
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000251static unsigned getELFSectionFlags(SectionKind K) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000252 unsigned Flags = 0;
253
254 if (!K.isMetadata())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000255 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000256
257 if (K.isText())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000258 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000259
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000260 if (K.isExecuteOnly())
261 Flags |= ELF::SHF_ARM_PURECODE;
262
Rafael Espindolac85e0d82011-06-07 23:26:45 +0000263 if (K.isWriteable())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000264 Flags |= ELF::SHF_WRITE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000265
266 if (K.isThreadLocal())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000267 Flags |= ELF::SHF_TLS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000268
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000269 if (K.isMergeableCString() || K.isMergeableConst())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000270 Flags |= ELF::SHF_MERGE;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000271
272 if (K.isMergeableCString())
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000273 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000274
275 return Flags;
276}
277
David Majnemerdad0a642014-06-27 18:19:56 +0000278static const Comdat *getELFComdat(const GlobalValue *GV) {
279 const Comdat *C = GV->getComdat();
280 if (!C)
281 return nullptr;
282
283 if (C->getSelectionKind() != Comdat::Any)
284 report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
285 C->getName() + "' cannot be lowered.");
286
287 return C;
288}
289
Evgeniy Stepanov51c962f722017-03-17 22:17:24 +0000290static const MCSymbolELF *getAssociatedSymbol(const GlobalObject *GO,
291 const TargetMachine &TM) {
292 MDNode *MD = GO->getMetadata(LLVMContext::MD_associated);
293 if (!MD)
294 return nullptr;
295
Evgeniy Stepanovf7e8acf2017-05-08 23:46:20 +0000296 const MDOperand &Op = MD->getOperand(0);
297 if (!Op.get())
298 return nullptr;
299
300 auto *VM = dyn_cast<ValueAsMetadata>(Op);
Evgeniy Stepanov51c962f722017-03-17 22:17:24 +0000301 if (!VM)
302 report_fatal_error("MD_associated operand is not ValueAsMetadata");
303
304 GlobalObject *OtherGO = dyn_cast<GlobalObject>(VM->getValue());
305 return OtherGO ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGO)) : nullptr;
306}
307
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000308MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000309 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
310 StringRef SectionName = GO->getSection();
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000311
Javed Absarb16d1462017-06-05 10:09:13 +0000312 // Check if '#pragma clang section' name is applicable.
313 // Note that pragma directive overrides -ffunction-section, -fdata-section
314 // and so section name is exactly as user specified and not uniqued.
315 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
316 if (GV && GV->hasImplicitSection()) {
317 auto Attrs = GV->getAttributes();
318 if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
319 SectionName = Attrs.getAttribute("bss-section").getValueAsString();
320 } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
321 SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
322 } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
323 SectionName = Attrs.getAttribute("data-section").getValueAsString();
324 }
325 }
326 const Function *F = dyn_cast<Function>(GO);
327 if (F && F->hasFnAttribute("implicit-section-name")) {
328 SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
329 }
330
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000331 // Infer section flags from the section name if we can.
332 Kind = getELFKindForNamedSection(SectionName, Kind);
333
David Majnemerdad0a642014-06-27 18:19:56 +0000334 StringRef Group = "";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000335 unsigned Flags = getELFSectionFlags(Kind);
Peter Collingbourne67335642016-10-24 19:23:39 +0000336 if (const Comdat *C = getELFComdat(GO)) {
David Majnemerdad0a642014-06-27 18:19:56 +0000337 Group = C->getName();
338 Flags |= ELF::SHF_GROUP;
339 }
Evgeniy Stepanov51c962f722017-03-17 22:17:24 +0000340
341 // A section can have at most one associated section. Put each global with
342 // MD_associated in a unique section.
343 unsigned UniqueID = MCContext::GenericSectionID;
344 const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
345 if (AssociatedSymbol) {
346 UniqueID = NextUniqueID++;
347 Flags |= ELF::SHF_LINK_ORDER;
348 }
349
350 MCSectionELF *Section = getContext().getELFSection(
351 SectionName, getELFSectionType(SectionName, Kind), Flags,
352 /*EntrySize=*/0, Group, UniqueID, AssociatedSymbol);
353 // Make sure that we did not get some other section with incompatible sh_link.
354 // This should not be possible due to UniqueID code above.
355 assert(Section->getAssociatedSymbol() == AssociatedSymbol);
356 return Section;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000357}
358
Rafael Espindola25d2c202015-02-11 14:44:17 +0000359/// Return the section prefix name used by options FunctionsSections and
360/// DataSections.
David Majnemer102ff692014-06-24 16:01:53 +0000361static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
Rafael Espindola25d2c202015-02-11 14:44:17 +0000362 if (Kind.isText())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000363 return ".text";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000364 if (Kind.isReadOnly())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000365 return ".rodata";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000366 if (Kind.isBSS())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000367 return ".bss";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000368 if (Kind.isThreadData())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000369 return ".tdata";
Rafael Espindola25d2c202015-02-11 14:44:17 +0000370 if (Kind.isThreadBSS())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000371 return ".tbss";
Rafael Espindola449711c2015-11-18 06:02:15 +0000372 if (Kind.isData())
Rafael Espindola68fa2492015-02-17 20:48:01 +0000373 return ".data";
Chris Lattner5b212a32010-04-13 00:36:43 +0000374 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Rafael Espindola68fa2492015-02-17 20:48:01 +0000375 return ".data.rel.ro";
Chris Lattner5b212a32010-04-13 00:36:43 +0000376}
377
Evgeniy Stepanov51c962f722017-03-17 22:17:24 +0000378static MCSectionELF *selectELFSectionForGlobal(
379 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
380 const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
381 unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) {
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000382 unsigned EntrySize = 0;
383 if (Kind.isMergeableCString()) {
384 if (Kind.isMergeable2ByteCString()) {
385 EntrySize = 2;
386 } else if (Kind.isMergeable4ByteCString()) {
387 EntrySize = 4;
388 } else {
389 EntrySize = 1;
390 assert(Kind.isMergeable1ByteCString() && "unknown string width");
391 }
392 } else if (Kind.isMergeableConst()) {
393 if (Kind.isMergeableConst4()) {
394 EntrySize = 4;
395 } else if (Kind.isMergeableConst8()) {
396 EntrySize = 8;
David Majnemer964b70d2016-02-22 22:23:11 +0000397 } else if (Kind.isMergeableConst16()) {
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000398 EntrySize = 16;
David Majnemer964b70d2016-02-22 22:23:11 +0000399 } else {
400 assert(Kind.isMergeableConst32() && "unknown data width");
401 EntrySize = 32;
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000402 }
403 }
404
Rafael Espindola9075f772015-02-20 23:28:28 +0000405 StringRef Group = "";
Peter Collingbourne67335642016-10-24 19:23:39 +0000406 if (const Comdat *C = getELFComdat(GO)) {
Rafael Espindola9075f772015-02-20 23:28:28 +0000407 Flags |= ELF::SHF_GROUP;
408 Group = C->getName();
409 }
410
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000411 bool UniqueSectionNames = TM.getUniqueSectionNames();
412 SmallString<128> Name;
Rafael Espindolaa05b3b72015-01-28 17:54:19 +0000413 if (Kind.isMergeableCString()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000414 // We also need alignment here.
415 // FIXME: this is getting the alignment of the character, not the
416 // alignment of the global!
Peter Collingbourne67335642016-10-24 19:23:39 +0000417 unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
418 cast<GlobalVariable>(GO));
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000419
Rafael Espindolaba31e272015-01-29 17:33:21 +0000420 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000421 Name = SizeSpec + utostr(Align);
422 } else if (Kind.isMergeableConst()) {
423 Name = ".rodata.cst";
424 Name += utostr(EntrySize);
425 } else {
426 Name = getSectionPrefixForGlobal(Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000427 }
Dehao Chen302b69c2016-10-18 20:42:47 +0000428
Peter Collingbourne67335642016-10-24 19:23:39 +0000429 if (const auto *F = dyn_cast<Function>(GO)) {
Dehao Chen302b69c2016-10-18 20:42:47 +0000430 const auto &OptionalPrefix = F->getSectionPrefix();
431 if (OptionalPrefix)
432 Name += *OptionalPrefix;
433 }
Dehao Chenf84b6302016-02-23 03:39:24 +0000434
Rafael Espindola8bc9ccc2015-02-25 00:52:15 +0000435 if (EmitUniqueSection && UniqueSectionNames) {
436 Name.push_back('.');
Peter Collingbourne67335642016-10-24 19:23:39 +0000437 TM.getNameWithPrefix(Name, GO, Mang, true);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000438 }
Reid Kleckner97837b72016-05-02 23:22:18 +0000439 unsigned UniqueID = MCContext::GenericSectionID;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000440 if (EmitUniqueSection && !UniqueSectionNames) {
441 UniqueID = *NextUniqueID;
442 (*NextUniqueID)++;
443 }
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000444 // Use 0 as the unique ID for execute-only text
445 if (Kind.isExecuteOnly())
446 UniqueID = 0;
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000447 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
Evgeniy Stepanov51c962f722017-03-17 22:17:24 +0000448 EntrySize, Group, UniqueID, AssociatedSymbol);
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000449}
450
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000451MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000452 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000453 unsigned Flags = getELFSectionFlags(Kind);
454
455 // If we have -ffunction-section or -fdata-section then we should emit the
456 // global value to a uniqued section specifically for it.
457 bool EmitUniqueSection = false;
458 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
459 if (Kind.isText())
460 EmitUniqueSection = TM.getFunctionSections();
461 else
462 EmitUniqueSection = TM.getDataSections();
463 }
Peter Collingbourne67335642016-10-24 19:23:39 +0000464 EmitUniqueSection |= GO->hasComdat();
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000465
Evgeniy Stepanov51c962f722017-03-17 22:17:24 +0000466 const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
467 if (AssociatedSymbol) {
468 EmitUniqueSection = true;
469 Flags |= ELF::SHF_LINK_ORDER;
470 }
471
472 MCSectionELF *Section = selectELFSectionForGlobal(
473 getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags,
474 &NextUniqueID, AssociatedSymbol);
475 assert(Section->getAssociatedSymbol() == AssociatedSymbol);
476 return Section;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000477}
478
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000479MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000480 const Function &F, const TargetMachine &TM) const {
Rafael Espindola29786d42015-02-12 17:16:46 +0000481 // If the function can be removed, produce a unique section so that
482 // the table doesn't prevent the removal.
483 const Comdat *C = F.getComdat();
484 bool EmitUniqueSection = TM.getFunctionSections() || C;
485 if (!EmitUniqueSection)
486 return ReadOnlySection;
487
Rafael Espindola4491d0d2015-02-26 23:55:11 +0000488 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
Evgeniy Stepanov51c962f722017-03-17 22:17:24 +0000489 getMangler(), TM, EmitUniqueSection,
490 ELF::SHF_ALLOC, &NextUniqueID,
491 /* AssociatedSymbol */ nullptr);
Rafael Espindola29786d42015-02-12 17:16:46 +0000492}
493
Rafael Espindoladf195192015-02-17 23:34:51 +0000494bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
495 bool UsesLabelDifference, const Function &F) const {
496 // We can always create relative relocations, so use another section
497 // that can be marked non-executable.
498 return false;
499}
500
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000501/// Given a mergeable constant with the specified size and relocation
502/// information, return a section that it should be placed in.
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000503MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
David Majnemera3ea4072016-02-21 01:30:30 +0000504 const DataLayout &DL, SectionKind Kind, const Constant *C,
505 unsigned &Align) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000506 if (Kind.isMergeableConst4() && MergeableConst4Section)
507 return MergeableConst4Section;
508 if (Kind.isMergeableConst8() && MergeableConst8Section)
509 return MergeableConst8Section;
510 if (Kind.isMergeableConst16() && MergeableConst16Section)
511 return MergeableConst16Section;
David Majnemer964b70d2016-02-22 22:23:11 +0000512 if (Kind.isMergeableConst32() && MergeableConst32Section)
513 return MergeableConst32Section;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000514 if (Kind.isReadOnly())
515 return ReadOnlySection;
516
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000517 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
518 return DataRelROSection;
519}
520
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000521static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
522 bool IsCtor, unsigned Priority,
523 const MCSymbol *KeySym) {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000524 std::string Name;
525 unsigned Type;
526 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
Rafael Espindolac4b42532014-09-04 23:03:58 +0000527 StringRef COMDAT = KeySym ? KeySym->getName() : "";
528
529 if (KeySym)
530 Flags |= ELF::SHF_GROUP;
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000531
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000532 if (UseInitArray) {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000533 if (IsCtor) {
534 Type = ELF::SHT_INIT_ARRAY;
535 Name = ".init_array";
536 } else {
537 Type = ELF::SHT_FINI_ARRAY;
538 Name = ".fini_array";
539 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000540 if (Priority != 65535) {
541 Name += '.';
542 Name += utostr(Priority);
543 }
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000544 } else {
Rafael Espindolac4b42532014-09-04 23:03:58 +0000545 // The default scheme is .ctor / .dtor, so we have to invert the priority
546 // numbering.
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000547 if (IsCtor)
548 Name = ".ctors";
549 else
550 Name = ".dtors";
Martin Storsjo04b68442017-11-28 08:07:18 +0000551 if (Priority != 65535)
552 raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
Rafael Espindolac4b42532014-09-04 23:03:58 +0000553 Type = ELF::SHT_PROGBITS;
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000554 }
Rafael Espindolac4b42532014-09-04 23:03:58 +0000555
Rafael Espindolaba31e272015-01-29 17:33:21 +0000556 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000557}
558
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000559MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000560 unsigned Priority, const MCSymbol *KeySym) const {
561 return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
562 KeySym);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000563}
564
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000565MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +0000566 unsigned Priority, const MCSymbol *KeySym) const {
Rafael Espindola7c7d7b92014-09-05 00:02:50 +0000567 return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
568 KeySym);
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000569}
570
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000571const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000572 const GlobalValue *LHS, const GlobalValue *RHS,
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000573 const TargetMachine &TM) const {
574 // We may only use a PLT-relative relocation to refer to unnamed_addr
575 // functions.
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000576 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000577 return nullptr;
578
579 // Basic sanity checks.
580 if (LHS->getType()->getPointerAddressSpace() != 0 ||
581 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
582 RHS->isThreadLocal())
583 return nullptr;
584
585 return MCBinaryExpr::createSub(
Tim Northoverb64fb452016-11-22 16:17:20 +0000586 MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind,
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000587 getContext()),
Tim Northoverb64fb452016-11-22 16:17:20 +0000588 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
Peter Collingbourne265ebd72016-04-22 20:40:10 +0000589}
590
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000591void
592TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
593 UseInitArray = UseInitArray_;
Rafael Espindola46fa2312016-08-29 12:33:42 +0000594 MCContext &Ctx = getContext();
595 if (!UseInitArray) {
596 StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
597 ELF::SHF_ALLOC | ELF::SHF_WRITE);
Rafael Espindolaca3e0ee2012-06-19 00:48:28 +0000598
Rafael Espindola46fa2312016-08-29 12:33:42 +0000599 StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
600 ELF::SHF_ALLOC | ELF::SHF_WRITE);
601 return;
602 }
603
604 StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
605 ELF::SHF_WRITE | ELF::SHF_ALLOC);
606 StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
607 ELF::SHF_WRITE | ELF::SHF_ALLOC);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +0000608}
609
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000610//===----------------------------------------------------------------------===//
611// MachO
612//===----------------------------------------------------------------------===//
613
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000614TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
615 : TargetLoweringObjectFile() {
616 SupportIndirectSymViaGOTPCRel = true;
617}
618
Rafael Espindola46fa2312016-08-29 12:33:42 +0000619void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
620 const TargetMachine &TM) {
621 TargetLoweringObjectFile::Initialize(Ctx, TM);
Rafael Espindola412a5292016-08-29 12:47:22 +0000622 if (TM.getRelocationModel() == Reloc::Static) {
Rafael Espindola46fa2312016-08-29 12:33:42 +0000623 StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
624 SectionKind::getData());
625 StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
626 SectionKind::getData());
627 } else {
628 StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
629 MachO::S_MOD_INIT_FUNC_POINTERS,
630 SectionKind::getData());
631 StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
632 MachO::S_MOD_TERM_FUNC_POINTERS,
633 SectionKind::getData());
634 }
635}
636
Peter Collingbourne89061b22017-06-12 20:10:48 +0000637void TargetLoweringObjectFileMachO::emitModuleMetadata(
638 MCStreamer &Streamer, Module &M, const TargetMachine &TM) const {
Daniel Dunbar95856122013-01-18 19:37:00 +0000639 // Emit the linker options if present.
Peter Collingbourne89061b22017-06-12 20:10:48 +0000640 if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000641 for (const auto &Option : LinkerOptions->operands()) {
Daniel Dunbar95856122013-01-18 19:37:00 +0000642 SmallVector<std::string, 4> StrOptions;
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +0000643 for (const auto &Piece : cast<MDNode>(Option)->operands())
644 StrOptions.push_back(cast<MDString>(Piece)->getString());
Daniel Dunbar95856122013-01-18 19:37:00 +0000645 Streamer.EmitLinkerOptions(StrOptions);
646 }
Bill Wendling06df7722012-02-14 21:28:13 +0000647 }
648
Saleem Abdulrasool4c474342017-06-05 21:26:39 +0000649 unsigned VersionVal = 0;
650 unsigned ImageInfoFlags = 0;
651 StringRef SectionVal;
Peter Collingbourne89061b22017-06-12 20:10:48 +0000652
653 GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal);
Saleem Abdulrasool4c474342017-06-05 21:26:39 +0000654
Bill Wendling734909a2012-02-15 22:36:15 +0000655 // The section is mandatory. If we don't have it, then we don't have GC info.
Saleem Abdulrasool4c474342017-06-05 21:26:39 +0000656 if (SectionVal.empty())
657 return;
Bill Wendling06df7722012-02-14 21:28:13 +0000658
Bill Wendling734909a2012-02-15 22:36:15 +0000659 StringRef Segment, Section;
660 unsigned TAA = 0, StubSize = 0;
661 bool TAAParsed;
662 std::string ErrorCode =
663 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
664 TAA, TAAParsed, StubSize);
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000665 if (!ErrorCode.empty())
Bill Wendling734909a2012-02-15 22:36:15 +0000666 // If invalid, report the error with report_fatal_error.
Bill Wendlinga0009ee2012-02-15 22:47:53 +0000667 report_fatal_error("Invalid section specifier '" + Section + "': " +
668 ErrorCode + ".");
Bill Wendling06df7722012-02-14 21:28:13 +0000669
Bill Wendling734909a2012-02-15 22:36:15 +0000670 // Get the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000671 MCSectionMachO *S = getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000672 Segment, Section, TAA, StubSize, SectionKind::getData());
Bill Wendling734909a2012-02-15 22:36:15 +0000673 Streamer.SwitchSection(S);
674 Streamer.EmitLabel(getContext().
Jim Grosbach6f482002015-05-18 18:43:14 +0000675 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendling06df7722012-02-14 21:28:13 +0000676 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingf1b14b72012-04-24 11:03:50 +0000677 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendling06df7722012-02-14 21:28:13 +0000678 Streamer.AddBlankLine();
679}
680
David Majnemerdad0a642014-06-27 18:19:56 +0000681static void checkMachOComdat(const GlobalValue *GV) {
682 const Comdat *C = GV->getComdat();
683 if (!C)
684 return;
685
686 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
687 "' cannot be lowered.");
688}
689
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000690MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000691 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000692 // Parse the section specifier and create it if valid.
693 StringRef Segment, Section;
Stuart Hastings12d53122011-03-19 02:42:31 +0000694 unsigned TAA = 0, StubSize = 0;
695 bool TAAParsed;
David Majnemerdad0a642014-06-27 18:19:56 +0000696
Peter Collingbourne67335642016-10-24 19:23:39 +0000697 checkMachOComdat(GO);
David Majnemerdad0a642014-06-27 18:19:56 +0000698
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000699 std::string ErrorCode =
Peter Collingbourne67335642016-10-24 19:23:39 +0000700 MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000701 TAA, TAAParsed, StubSize);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000702 if (!ErrorCode.empty()) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000703 // If invalid, report the error with report_fatal_error.
Peter Collingbourne67335642016-10-24 19:23:39 +0000704 report_fatal_error("Global variable '" + GO->getName() +
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000705 "' has an invalid section specifier '" +
Peter Collingbourne67335642016-10-24 19:23:39 +0000706 GO->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000707 }
708
709 // Get the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000710 MCSectionMachO *S =
711 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000712
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000713 // If TAA wasn't set by ParseSectionSpecifier() above,
714 // use the value returned by getMachOSection() as a default.
Stuart Hastings12d53122011-03-19 02:42:31 +0000715 if (!TAAParsed)
Stuart Hastingsb4863a42011-02-21 17:27:17 +0000716 TAA = S->getTypeAndAttributes();
717
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000718 // Okay, now that we got the section, verify that the TAA & StubSize agree.
719 // If the user declared multiple globals with different section flags, we need
720 // to reject it here.
721 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000722 // If invalid, report the error with report_fatal_error.
Peter Collingbourne67335642016-10-24 19:23:39 +0000723 report_fatal_error("Global variable '" + GO->getName() +
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000724 "' section type or attributes does not match previous"
725 " section specifier");
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000726 }
727
728 return S;
729}
730
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000731MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +0000732 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
733 checkMachOComdat(GO);
Bill Wendling25b61db2013-11-17 10:53:13 +0000734
735 // Handle thread local data.
736 if (Kind.isThreadBSS()) return TLSBSSSection;
737 if (Kind.isThreadData()) return TLSDataSection;
738
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000739 if (Kind.isText())
Peter Collingbourne67335642016-10-24 19:23:39 +0000740 return GO->isWeakForLinker() ? TextCoalSection : TextSection;
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000741
742 // If this is weak/linkonce, put this in a coalescable section, either in text
743 // or data depending on if it is writable.
Peter Collingbourne67335642016-10-24 19:23:39 +0000744 if (GO->isWeakForLinker()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000745 if (Kind.isReadOnly())
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000746 return ConstTextCoalSection;
Steven Wud0804aa2018-04-10 20:16:35 +0000747 if (Kind.isReadOnlyWithRel())
748 return ConstDataCoalSection;
Arnold Schwaighoferc31c2de2013-08-08 21:04:16 +0000749 return DataCoalSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000750 }
751
752 // FIXME: Alignment check should be handled by section classifier.
Chris Lattneref2f8042010-03-07 04:28:09 +0000753 if (Kind.isMergeable1ByteCString() &&
Peter Collingbourne67335642016-10-24 19:23:39 +0000754 GO->getParent()->getDataLayout().getPreferredAlignment(
755 cast<GlobalVariable>(GO)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000756 return CStringSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000757
Chris Lattneref2f8042010-03-07 04:28:09 +0000758 // Do not put 16-bit arrays in the UString section if they have an
759 // externally visible label, this runs into issues with certain linker
760 // versions.
Peter Collingbourne67335642016-10-24 19:23:39 +0000761 if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
762 GO->getParent()->getDataLayout().getPreferredAlignment(
763 cast<GlobalVariable>(GO)) < 32)
Chris Lattneref2f8042010-03-07 04:28:09 +0000764 return UStringSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000765
Rafael Espindolab43d51d2014-08-28 20:13:31 +0000766 // With MachO only variables whose corresponding symbol starts with 'l' or
767 // 'L' can be merged, so we only try merging GVs with private linkage.
Peter Collingbourne67335642016-10-24 19:23:39 +0000768 if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000769 if (Kind.isMergeableConst4())
770 return FourByteConstantSection;
771 if (Kind.isMergeableConst8())
772 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000773 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000774 return SixteenByteConstantSection;
775 }
776
777 // Otherwise, if it is readonly, but not something we can specially optimize,
778 // just drop it in .const.
779 if (Kind.isReadOnly())
780 return ReadOnlySection;
781
782 // If this is marked const, put it into a const section. But if the dynamic
783 // linker needs to write to it, put it in the data segment.
784 if (Kind.isReadOnlyWithRel())
785 return ConstDataSection;
786
787 // Put zero initialized globals with strong external linkage in the
788 // DATA, __common section with the .zerofill directive.
789 if (Kind.isBSSExtern())
790 return DataCommonSection;
791
792 // Put zero initialized globals with local linkage in __DATA,__bss directive
793 // with the .zerofill directive (aka .lcomm).
794 if (Kind.isBSSLocal())
795 return DataBSSSection;
Michael J. Spencerfbdab0d2010-10-27 18:52:20 +0000796
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000797 // Otherwise, just drop the variable in the normal data section.
798 return DataSection;
799}
800
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000801MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
David Majnemera3ea4072016-02-21 01:30:30 +0000802 const DataLayout &DL, SectionKind Kind, const Constant *C,
803 unsigned &Align) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000804 // If this constant requires a relocation, we have to put it in the data
805 // segment, not in the text segment.
Rafael Espindola449711c2015-11-18 06:02:15 +0000806 if (Kind.isData() || Kind.isReadOnlyWithRel())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000807 return ConstDataSection;
808
809 if (Kind.isMergeableConst4())
810 return FourByteConstantSection;
811 if (Kind.isMergeableConst8())
812 return EightByteConstantSection;
Rafael Espindola1f3de492014-02-13 23:16:11 +0000813 if (Kind.isMergeableConst16())
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000814 return SixteenByteConstantSection;
815 return ReadOnlySection; // .const
816}
817
Rafael Espindola15b26692014-02-09 14:50:44 +0000818const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000819 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
820 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000821 // The mach-o version of this method defaults to returning a stub reference.
822
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000823 if (Encoding & DW_EH_PE_indirect) {
824 MachineModuleInfoMachO &MachOMMI =
825 MMI->getObjFileInfo<MachineModuleInfoMachO>();
826
Eric Christopher4367c7f2016-09-16 07:33:15 +0000827 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000828
829 // Add information about the stub reference to MachOMMI so that the stub
830 // gets emitted by the asmprinter.
Rafael Espindola712f9572016-05-17 16:01:32 +0000831 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000832 if (!StubSym.getPointer()) {
Tim Northoverb64fb452016-11-22 16:17:20 +0000833 MCSymbol *Sym = TM.getSymbol(GV);
Chris Lattner2ea586b2010-03-15 20:37:38 +0000834 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov31a92122010-02-21 20:28:15 +0000835 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000836
837 return TargetLoweringObjectFile::
Jim Grosbach13760bd2015-05-30 01:25:56 +0000838 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Eugene Zelenkofa912a72017-02-27 22:45:06 +0000839 Encoding & ~DW_EH_PE_indirect, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000840 }
841
Eric Christopher4367c7f2016-09-16 07:33:15 +0000842 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
843 MMI, Streamer);
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000844}
845
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000846MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000847 const GlobalValue *GV, const TargetMachine &TM,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000848 MachineModuleInfo *MMI) const {
Rafael Espindola08704342011-04-27 23:08:15 +0000849 // The mach-o version of this method defaults to returning a stub reference.
850 MachineModuleInfoMachO &MachOMMI =
851 MMI->getObjFileInfo<MachineModuleInfoMachO>();
852
Eric Christopher4367c7f2016-09-16 07:33:15 +0000853 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
Rafael Espindola08704342011-04-27 23:08:15 +0000854
855 // Add information about the stub reference to MachOMMI so that the stub
856 // gets emitted by the asmprinter.
Bill Wendlinge4cc3322011-11-29 01:43:20 +0000857 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topperc0196b12014-04-14 00:51:57 +0000858 if (!StubSym.getPointer()) {
Tim Northoverb64fb452016-11-22 16:17:20 +0000859 MCSymbol *Sym = TM.getSymbol(GV);
Rafael Espindola08704342011-04-27 23:08:15 +0000860 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
861 }
862
863 return SSym;
864}
865
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000866const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
867 const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
868 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000869 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000870 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
871 // through a non_lazy_ptr stub instead. One advantage is that it allows the
872 // computation of deltas to final external symbols. Example:
873 //
874 // _extgotequiv:
875 // .long _extfoo
876 //
877 // _delta:
878 // .long _extgotequiv-_delta
879 //
880 // is transformed to:
881 //
882 // _delta:
883 // .long L_extfoo$non_lazy_ptr-(_delta+0)
884 //
885 // .section __IMPORT,__pointers,non_lazy_symbol_pointers
886 // L_extfoo$non_lazy_ptr:
887 // .indirect_symbol _extfoo
888 // .long 0
889 //
890 MachineModuleInfoMachO &MachOMMI =
891 MMI->getObjFileInfo<MachineModuleInfoMachO>();
892 MCContext &Ctx = getContext();
893
894 // The offset must consider the original displacement from the base symbol
895 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
896 Offset = -MV.getConstant();
897 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
898
899 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
900 // non_lazy_ptr stubs.
901 SmallString<128> Name;
902 StringRef Suffix = "$non_lazy_ptr";
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000903 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000904 Name += Sym->getName();
905 Name += Suffix;
Jim Grosbach6f482002015-05-18 18:43:14 +0000906 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000907
908 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
909 if (!StubSym.getPointer())
910 StubSym = MachineModuleInfoImpl::
911 StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */);
912
913 const MCExpr *BSymExpr =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000914 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000915 const MCExpr *LHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000916 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000917
918 if (!Offset)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000919 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000920
921 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000922 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
923 return MCBinaryExpr::createSub(LHS, RHS, Ctx);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +0000924}
925
Peter Collingbourne94d77862015-11-03 23:40:03 +0000926static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
927 const MCSection &Section) {
928 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
929 return true;
930
931 // If it is not dead stripped, it is safe to use private labels.
932 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
933 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
934 return true;
935
936 return false;
937}
938
939void TargetLoweringObjectFileMachO::getNameWithPrefix(
Eric Christopher4367c7f2016-09-16 07:33:15 +0000940 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
Peter Collingbourne94d77862015-11-03 23:40:03 +0000941 const TargetMachine &TM) const {
Peter Collingbourne67335642016-10-24 19:23:39 +0000942 bool CannotUsePrivateLabel = true;
943 if (auto *GO = GV->getBaseObject()) {
944 SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM);
945 const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
946 CannotUsePrivateLabel =
947 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
948 }
Eric Christopher4367c7f2016-09-16 07:33:15 +0000949 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
Peter Collingbourne94d77862015-11-03 23:40:03 +0000950}
951
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000952//===----------------------------------------------------------------------===//
953// COFF
954//===----------------------------------------------------------------------===//
955
Chris Lattner87cffa92010-05-07 17:17:41 +0000956static unsigned
Renato Golinef3eb062016-06-27 14:42:20 +0000957getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) {
Chris Lattner87cffa92010-05-07 17:17:41 +0000958 unsigned Flags = 0;
Renato Golinef3eb062016-06-27 14:42:20 +0000959 bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
Chris Lattner87cffa92010-05-07 17:17:41 +0000960
Anton Korobeynikove4152302010-07-06 15:24:56 +0000961 if (K.isMetadata())
Chris Lattner02844932010-05-07 21:49:09 +0000962 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000963 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000964 else if (K.isText())
965 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000966 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer0f83d962010-10-27 18:52:29 +0000967 COFF::IMAGE_SCN_MEM_READ |
Renato Golinef3eb062016-06-27 14:42:20 +0000968 COFF::IMAGE_SCN_CNT_CODE |
969 (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0);
David Majnemerb8dbebb2014-09-20 07:31:46 +0000970 else if (K.isBSS())
Chris Lattner02844932010-05-07 21:49:09 +0000971 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000972 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
973 COFF::IMAGE_SCN_MEM_READ |
974 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +0000975 else if (K.isThreadLocal())
976 Flags |=
977 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
978 COFF::IMAGE_SCN_MEM_READ |
979 COFF::IMAGE_SCN_MEM_WRITE;
David Majnemerb8dbebb2014-09-20 07:31:46 +0000980 else if (K.isReadOnly() || K.isReadOnlyWithRel())
Chris Lattner87cffa92010-05-07 17:17:41 +0000981 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000982 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
983 COFF::IMAGE_SCN_MEM_READ;
Chris Lattner87cffa92010-05-07 17:17:41 +0000984 else if (K.isWriteable())
985 Flags |=
Daniel Dunbar329d2022010-07-01 20:07:24 +0000986 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
987 COFF::IMAGE_SCN_MEM_READ |
988 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattner87cffa92010-05-07 17:17:41 +0000989
990 return Flags;
Anton Korobeynikovab663a02010-02-15 22:37:53 +0000991}
992
Benjamin Kramer6cbe6702014-07-07 14:47:51 +0000993static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
David Majnemerdad0a642014-06-27 18:19:56 +0000994 const Comdat *C = GV->getComdat();
995 assert(C && "expected GV to have a Comdat!");
996
997 StringRef ComdatGVName = C->getName();
998 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
999 if (!ComdatGV)
1000 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
1001 "' does not exist.");
1002
1003 if (ComdatGV->getComdat() != C)
1004 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
Hans Wennborgc0f0c512014-09-19 01:14:56 +00001005 "' is not a key for its COMDAT.");
David Majnemerdad0a642014-06-27 18:19:56 +00001006
1007 return ComdatGV;
1008}
1009
1010static int getSelectionForCOFF(const GlobalValue *GV) {
1011 if (const Comdat *C = GV->getComdat()) {
1012 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
1013 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
1014 ComdatKey = GA->getBaseObject();
1015 if (ComdatKey == GV) {
1016 switch (C->getSelectionKind()) {
1017 case Comdat::Any:
1018 return COFF::IMAGE_COMDAT_SELECT_ANY;
1019 case Comdat::ExactMatch:
1020 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
1021 case Comdat::Largest:
1022 return COFF::IMAGE_COMDAT_SELECT_LARGEST;
1023 case Comdat::NoDuplicates:
1024 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
1025 case Comdat::SameSize:
1026 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
1027 }
1028 } else {
1029 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
1030 }
David Majnemerdad0a642014-06-27 18:19:56 +00001031 }
1032 return 0;
1033}
1034
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001035MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +00001036 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Michael J. Spencerf1aef752012-11-13 22:04:09 +00001037 int Selection = 0;
Renato Golinef3eb062016-06-27 14:42:20 +00001038 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Peter Collingbourne67335642016-10-24 19:23:39 +00001039 StringRef Name = GO->getSection();
Rafael Espindola2d30ae22013-11-27 01:18:37 +00001040 StringRef COMDATSymName = "";
Peter Collingbourne67335642016-10-24 19:23:39 +00001041 if (GO->hasComdat()) {
1042 Selection = getSelectionForCOFF(GO);
David Majnemerdad0a642014-06-27 18:19:56 +00001043 const GlobalValue *ComdatGV;
1044 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
Peter Collingbourne67335642016-10-24 19:23:39 +00001045 ComdatGV = getComdatGVForCOFF(GO);
David Majnemerdad0a642014-06-27 18:19:56 +00001046 else
Peter Collingbourne67335642016-10-24 19:23:39 +00001047 ComdatGV = GO;
David Majnemerdad0a642014-06-27 18:19:56 +00001048
1049 if (!ComdatGV->hasPrivateLinkage()) {
Tim Northoverb64fb452016-11-22 16:17:20 +00001050 MCSymbol *Sym = TM.getSymbol(ComdatGV);
David Majnemerdad0a642014-06-27 18:19:56 +00001051 COMDATSymName = Sym->getName();
1052 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1053 } else {
1054 Selection = 0;
1055 }
Michael J. Spencerf1aef752012-11-13 22:04:09 +00001056 }
Reid Kleckner97837b72016-05-02 23:22:18 +00001057
1058 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
Nico Riecka37acf72013-07-06 12:13:10 +00001059 Selection);
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001060}
1061
Rafael Espindola2d30ae22013-11-27 01:18:37 +00001062static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001063 if (Kind.isText())
Rafael Espindola2d30ae22013-11-27 01:18:37 +00001064 return ".text";
David Majnemera9bdb322014-04-08 22:33:40 +00001065 if (Kind.isBSS())
Rafael Espindola2d30ae22013-11-27 01:18:37 +00001066 return ".bss";
1067 if (Kind.isThreadLocal())
Rafael Espindola3c8e1472013-11-27 15:52:11 +00001068 return ".tls$";
David Majnemer597be2d2014-09-22 20:39:23 +00001069 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1070 return ".rdata";
1071 return ".data";
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001072}
1073
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001074MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +00001075 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
David Majnemer93389842014-03-23 17:47:39 +00001076 // If we have -ffunction-sections then we should emit the global value to a
1077 // uniqued section specifically for it.
David Majnemer273bff42014-03-25 06:14:26 +00001078 bool EmitUniquedSection;
1079 if (Kind.isText())
1080 EmitUniquedSection = TM.getFunctionSections();
1081 else
1082 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001083
Peter Collingbourne67335642016-10-24 19:23:39 +00001084 if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
Rafael Espindola2d30ae22013-11-27 01:18:37 +00001085 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Renato Golinef3eb062016-06-27 14:42:20 +00001086 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Chris Lattner02844932010-05-07 21:49:09 +00001087
Daniel Dunbar329d2022010-07-01 20:07:24 +00001088 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Peter Collingbourne67335642016-10-24 19:23:39 +00001089 int Selection = getSelectionForCOFF(GO);
David Majnemerdad0a642014-06-27 18:19:56 +00001090 if (!Selection)
1091 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
1092 const GlobalValue *ComdatGV;
Peter Collingbourne67335642016-10-24 19:23:39 +00001093 if (GO->hasComdat())
1094 ComdatGV = getComdatGVForCOFF(GO);
David Majnemerdad0a642014-06-27 18:19:56 +00001095 else
Peter Collingbourne67335642016-10-24 19:23:39 +00001096 ComdatGV = GO;
David Majnemerdad0a642014-06-27 18:19:56 +00001097
Reid Kleckner97837b72016-05-02 23:22:18 +00001098 unsigned UniqueID = MCContext::GenericSectionID;
1099 if (EmitUniquedSection)
1100 UniqueID = NextUniqueID++;
1101
David Majnemerdad0a642014-06-27 18:19:56 +00001102 if (!ComdatGV->hasPrivateLinkage()) {
Tim Northoverb64fb452016-11-22 16:17:20 +00001103 MCSymbol *Sym = TM.getSymbol(ComdatGV);
David Majnemerdad0a642014-06-27 18:19:56 +00001104 StringRef COMDATSymName = Sym->getName();
1105 return getContext().getCOFFSection(Name, Characteristics, Kind,
Reid Kleckner97837b72016-05-02 23:22:18 +00001106 COMDATSymName, Selection, UniqueID);
David Majnemer7db449a2015-03-17 23:54:51 +00001107 } else {
1108 SmallString<256> TmpData;
Peter Collingbourne67335642016-10-24 19:23:39 +00001109 getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
David Majnemer7db449a2015-03-17 23:54:51 +00001110 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
Reid Kleckner97837b72016-05-02 23:22:18 +00001111 Selection, UniqueID);
David Majnemerdad0a642014-06-27 18:19:56 +00001112 }
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001113 }
1114
1115 if (Kind.isText())
David Majnemer3d96acb2013-08-13 01:23:53 +00001116 return TextSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001117
Anton Korobeynikovc6b40172012-02-11 17:26:53 +00001118 if (Kind.isThreadLocal())
David Majnemer3d96acb2013-08-13 01:23:53 +00001119 return TLSDataSection;
Anton Korobeynikovc6b40172012-02-11 17:26:53 +00001120
David Majnemer597be2d2014-09-22 20:39:23 +00001121 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
David Majnemerf76d6b32013-08-08 01:50:52 +00001122 return ReadOnlySection;
1123
David Majnemera9bdb322014-04-08 22:33:40 +00001124 // Note: we claim that common symbols are put in BSSSection, but they are
1125 // really emitted with the magic .comm directive, which creates a symbol table
1126 // entry but not a section.
1127 if (Kind.isBSS() || Kind.isCommon())
David Majnemer3d96acb2013-08-13 01:23:53 +00001128 return BSSSection;
1129
1130 return DataSection;
Anton Korobeynikovab663a02010-02-15 22:37:53 +00001131}
1132
David Majnemer7db449a2015-03-17 23:54:51 +00001133void TargetLoweringObjectFileCOFF::getNameWithPrefix(
Eric Christopher4367c7f2016-09-16 07:33:15 +00001134 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
Peter Collingbourne94d77862015-11-03 23:40:03 +00001135 const TargetMachine &TM) const {
1136 bool CannotUsePrivateLabel = false;
David Majnemer7db449a2015-03-17 23:54:51 +00001137 if (GV->hasPrivateLinkage() &&
1138 ((isa<Function>(GV) && TM.getFunctionSections()) ||
1139 (isa<GlobalVariable>(GV) && TM.getDataSections())))
1140 CannotUsePrivateLabel = true;
1141
Eric Christopher4367c7f2016-09-16 07:33:15 +00001142 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
David Majnemer7db449a2015-03-17 23:54:51 +00001143}
1144
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001145MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
Eric Christopher4367c7f2016-09-16 07:33:15 +00001146 const Function &F, const TargetMachine &TM) const {
Rafael Espindolaab447e42015-03-11 19:58:37 +00001147 // If the function can be removed, produce a unique section so that
1148 // the table doesn't prevent the removal.
1149 const Comdat *C = F.getComdat();
1150 bool EmitUniqueSection = TM.getFunctionSections() || C;
1151 if (!EmitUniqueSection)
1152 return ReadOnlySection;
1153
1154 // FIXME: we should produce a symbol for F instead.
1155 if (F.hasPrivateLinkage())
1156 return ReadOnlySection;
1157
Tim Northoverb64fb452016-11-22 16:17:20 +00001158 MCSymbol *Sym = TM.getSymbol(&F);
Rafael Espindolaab447e42015-03-11 19:58:37 +00001159 StringRef COMDATSymName = Sym->getName();
1160
1161 SectionKind Kind = SectionKind::getReadOnly();
1162 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
Renato Golinef3eb062016-06-27 14:42:20 +00001163 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Rafael Espindolaab447e42015-03-11 19:58:37 +00001164 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Reid Kleckner97837b72016-05-02 23:22:18 +00001165 unsigned UniqueID = NextUniqueID++;
Rafael Espindolaab447e42015-03-11 19:58:37 +00001166
1167 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
Reid Kleckner97837b72016-05-02 23:22:18 +00001168 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
Rafael Espindolaab447e42015-03-11 19:58:37 +00001169}
1170
Peter Collingbourne89061b22017-06-12 20:10:48 +00001171void TargetLoweringObjectFileCOFF::emitModuleMetadata(
1172 MCStreamer &Streamer, Module &M, const TargetMachine &TM) const {
1173 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
Saleem Abdulrasoole0f0c0e2016-04-30 18:15:34 +00001174 // Emit the linker options to the linker .drectve section. According to the
1175 // spec, this section is a space-separated string containing flags for
1176 // linker.
1177 MCSection *Sec = getDrectveSection();
1178 Streamer.SwitchSection(Sec);
1179 for (const auto &Option : LinkerOptions->operands()) {
1180 for (const auto &Piece : cast<MDNode>(Option)->operands()) {
1181 // Lead with a space for consistency with our dllexport implementation.
1182 std::string Directive(" ");
1183 Directive.append(cast<MDString>(Piece)->getString());
1184 Streamer.EmitBytes(Directive);
1185 }
Reid Klecknerd973ca32013-04-25 19:34:41 +00001186 }
1187 }
Saleem Abdulrasool4c474342017-06-05 21:26:39 +00001188
1189 unsigned Version = 0;
1190 unsigned Flags = 0;
1191 StringRef Section;
1192
Peter Collingbourne89061b22017-06-12 20:10:48 +00001193 GetObjCImageInfo(M, Version, Flags, Section);
Saleem Abdulrasool4c474342017-06-05 21:26:39 +00001194 if (Section.empty())
1195 return;
1196
1197 auto &C = getContext();
1198 auto *S = C.getCOFFSection(
1199 Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
1200 SectionKind::getReadOnly());
1201 Streamer.SwitchSection(S);
1202 Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1203 Streamer.EmitIntValue(Version, 4);
1204 Streamer.EmitIntValue(Flags, 4);
1205 Streamer.AddBlankLine();
Reid Klecknerd973ca32013-04-25 19:34:41 +00001206}
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001207
Rafael Espindola46fa2312016-08-29 12:33:42 +00001208void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1209 const TargetMachine &TM) {
1210 TargetLoweringObjectFile::Initialize(Ctx, TM);
1211 const Triple &T = TM.getTargetTriple();
1212 if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1213 StaticCtorSection =
1214 Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1215 COFF::IMAGE_SCN_MEM_READ,
1216 SectionKind::getReadOnly());
1217 StaticDtorSection =
1218 Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1219 COFF::IMAGE_SCN_MEM_READ,
1220 SectionKind::getReadOnly());
1221 } else {
1222 StaticCtorSection = Ctx.getCOFFSection(
1223 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1224 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1225 SectionKind::getData());
1226 StaticDtorSection = Ctx.getCOFFSection(
1227 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1228 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1229 SectionKind::getData());
1230 }
1231}
1232
Martin Storsjo04b68442017-11-28 08:07:18 +00001233static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
1234 const Triple &T, bool IsCtor,
1235 unsigned Priority,
1236 const MCSymbol *KeySym,
1237 MCSectionCOFF *Default) {
1238 if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment())
1239 return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
1240
1241 std::string Name = IsCtor ? ".ctors" : ".dtors";
1242 if (Priority != 65535)
1243 raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
1244
1245 return Ctx.getAssociativeCOFFSection(
1246 Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1247 COFF::IMAGE_SCN_MEM_READ |
1248 COFF::IMAGE_SCN_MEM_WRITE,
1249 SectionKind::getData()),
1250 KeySym, 0);
1251}
1252
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001253MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +00001254 unsigned Priority, const MCSymbol *KeySym) const {
Martin Storsjo04b68442017-11-28 08:07:18 +00001255 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true,
1256 Priority, KeySym,
1257 cast<MCSectionCOFF>(StaticCtorSection));
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001258}
1259
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001260MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
Rafael Espindola0766ae02014-06-06 19:26:12 +00001261 unsigned Priority, const MCSymbol *KeySym) const {
Martin Storsjo04b68442017-11-28 08:07:18 +00001262 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false,
1263 Priority, KeySym,
1264 cast<MCSectionCOFF>(StaticDtorSection));
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001265}
Peter Collingbourneaef36592015-06-29 22:04:09 +00001266
1267void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
Eric Christopher4367c7f2016-09-16 07:33:15 +00001268 raw_ostream &OS, const GlobalValue *GV) const {
Bob Haarmandd4ebc12017-02-02 23:00:49 +00001269 emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler());
Peter Collingbourneaef36592015-06-29 22:04:09 +00001270}
Dan Gohman18eafb62017-02-22 01:23:18 +00001271
Saleem Abdulrasool99f479a2018-01-20 00:28:02 +00001272void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed(
1273 raw_ostream &OS, const GlobalValue *GV) const {
1274 emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler());
1275}
1276
Dan Gohman18eafb62017-02-22 01:23:18 +00001277//===----------------------------------------------------------------------===//
1278// Wasm
1279//===----------------------------------------------------------------------===//
1280
Sam Cleggea7cace2018-01-09 23:43:14 +00001281static const Comdat *getWasmComdat(const GlobalValue *GV) {
Dan Gohmand934cb82017-02-24 23:18:00 +00001282 const Comdat *C = GV->getComdat();
1283 if (!C)
Sam Cleggea7cace2018-01-09 23:43:14 +00001284 return nullptr;
Dan Gohmand934cb82017-02-24 23:18:00 +00001285
Sam Cleggea7cace2018-01-09 23:43:14 +00001286 if (C->getSelectionKind() != Comdat::Any)
1287 report_fatal_error("WebAssembly COMDATs only support "
1288 "SelectionKind::Any, '" + C->getName() + "' cannot be "
1289 "lowered.");
1290
1291 return C;
Dan Gohmand934cb82017-02-24 23:18:00 +00001292}
1293
Sam Clegge1694f92017-12-07 02:55:51 +00001294static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) {
1295 // If we're told we have function data, then use that.
1296 if (K.isText())
1297 return SectionKind::getText();
1298
1299 // Otherwise, ignore whatever section type the generic impl detected and use
1300 // a plain data section.
1301 return SectionKind::getData();
1302}
1303
Dan Gohman18eafb62017-02-22 01:23:18 +00001304MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
1305 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Sam Clegg759631c2017-09-15 20:54:59 +00001306 StringRef Name = GO->getSection();
Sam Cleggea7cace2018-01-09 23:43:14 +00001307
Sam Clegge1694f92017-12-07 02:55:51 +00001308 Kind = getWasmKindForNamedSection(Name, Kind);
Sam Cleggea7cace2018-01-09 23:43:14 +00001309
1310 StringRef Group = "";
1311 if (const Comdat *C = getWasmComdat(GO)) {
1312 Group = C->getName();
1313 }
1314
1315 return getContext().getWasmSection(Name, Kind, Group,
1316 MCContext::GenericSectionID);
Dan Gohman18eafb62017-02-22 01:23:18 +00001317}
1318
Sam Clegg2176a9f2017-09-12 18:31:24 +00001319static MCSectionWasm *selectWasmSectionForGlobal(
1320 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
1321 const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) {
Dan Gohmand934cb82017-02-24 23:18:00 +00001322 StringRef Group = "";
Sam Cleggea7cace2018-01-09 23:43:14 +00001323 if (const Comdat *C = getWasmComdat(GO)) {
1324 Group = C->getName();
1325 }
Dan Gohmand934cb82017-02-24 23:18:00 +00001326
1327 bool UniqueSectionNames = TM.getUniqueSectionNames();
1328 SmallString<128> Name = getSectionPrefixForGlobal(Kind);
1329
1330 if (const auto *F = dyn_cast<Function>(GO)) {
1331 const auto &OptionalPrefix = F->getSectionPrefix();
1332 if (OptionalPrefix)
1333 Name += *OptionalPrefix;
1334 }
1335
1336 if (EmitUniqueSection && UniqueSectionNames) {
1337 Name.push_back('.');
1338 TM.getNameWithPrefix(Name, GO, Mang, true);
1339 }
1340 unsigned UniqueID = MCContext::GenericSectionID;
1341 if (EmitUniqueSection && !UniqueSectionNames) {
1342 UniqueID = *NextUniqueID;
1343 (*NextUniqueID)++;
1344 }
Sam Clegg12fd3da2017-10-20 21:28:38 +00001345 return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
Dan Gohmand934cb82017-02-24 23:18:00 +00001346}
1347
Dan Gohman18eafb62017-02-22 01:23:18 +00001348MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(
1349 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Dan Gohmand934cb82017-02-24 23:18:00 +00001350
1351 if (Kind.isCommon())
1352 report_fatal_error("mergable sections not supported yet on wasm");
1353
1354 // If we have -ffunction-section or -fdata-section then we should emit the
1355 // global value to a uniqued section specifically for it.
1356 bool EmitUniqueSection = false;
Dan Gohman18eafb62017-02-22 01:23:18 +00001357 if (Kind.isText())
Dan Gohmand934cb82017-02-24 23:18:00 +00001358 EmitUniqueSection = TM.getFunctionSections();
1359 else
1360 EmitUniqueSection = TM.getDataSections();
1361 EmitUniqueSection |= GO->hasComdat();
1362
1363 return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
Sam Clegg2176a9f2017-09-12 18:31:24 +00001364 EmitUniqueSection, &NextUniqueID);
Dan Gohman18eafb62017-02-22 01:23:18 +00001365}
1366
1367bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
1368 bool UsesLabelDifference, const Function &F) const {
1369 // We can always create relative relocations, so use another section
1370 // that can be marked non-executable.
1371 return false;
1372}
1373
1374const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference(
1375 const GlobalValue *LHS, const GlobalValue *RHS,
1376 const TargetMachine &TM) const {
1377 // We may only use a PLT-relative relocation to refer to unnamed_addr
1378 // functions.
1379 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
1380 return nullptr;
1381
1382 // Basic sanity checks.
1383 if (LHS->getType()->getPointerAddressSpace() != 0 ||
1384 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
1385 RHS->isThreadLocal())
1386 return nullptr;
1387
1388 return MCBinaryExpr::createSub(
1389 MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None,
1390 getContext()),
1391 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
1392}
1393
Sam Cleggb2b019f2017-10-03 11:20:28 +00001394void TargetLoweringObjectFileWasm::InitializeWasm() {
1395 StaticCtorSection =
Sam Clegg12fd3da2017-10-20 21:28:38 +00001396 getContext().getWasmSection(".init_array", SectionKind::getData());
Sam Cleggbafe6902017-12-15 00:17:10 +00001397}
1398
1399MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection(
1400 unsigned Priority, const MCSymbol *KeySym) const {
1401 return Priority == UINT16_MAX ?
1402 StaticCtorSection :
1403 getContext().getWasmSection(".init_array." + utostr(Priority),
1404 SectionKind::getData());
1405}
1406
1407MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection(
1408 unsigned Priority, const MCSymbol *KeySym) const {
1409 llvm_unreachable("@llvm.global_dtors should have been lowered already");
1410 return nullptr;
Dan Gohman18eafb62017-02-22 01:23:18 +00001411}