blob: cb2fe691d7021a7ec07907eb6168cdefec40fee4 [file] [log] [blame]
Eugene Zelenko90d99202017-02-27 22:45:06 +00001//===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===//
Anton Korobeynikov362dd0b2010-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 Carruthe3e43d92017-06-06 11:49:48 +000015#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/SmallString.h"
Eugene Zelenko90d99202017-02-27 22:45:06 +000017#include "llvm/ADT/SmallVector.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenko90d99202017-02-27 22:45:06 +000019#include "llvm/ADT/StringRef.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ADT/Triple.h"
Zachary Turner19ca2b02017-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 Zelenko90d99202017-02-27 22:45:06 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Eugene Zelenko90d99202017-02-27 22:45:06 +000027#include "llvm/IR/Comdat.h"
Chandler Carruth0b8c9a82013-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 Zelenko90d99202017-02-27 22:45:06 +000032#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalObject.h"
34#include "llvm/IR/GlobalValue.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000035#include "llvm/IR/GlobalVariable.h"
Rafael Espindolab56c57b2014-01-07 21:19:40 +000036#include "llvm/IR/Mangler.h"
Eugene Zelenko90d99202017-02-27 22:45:06 +000037#include "llvm/IR/Metadata.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000038#include "llvm/IR/Module.h"
Eugene Zelenko90d99202017-02-27 22:45:06 +000039#include "llvm/IR/Type.h"
Peter Collingbourne05f66f32015-11-03 23:40:03 +000040#include "llvm/MC/MCAsmInfo.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000041#include "llvm/MC/MCContext.h"
42#include "llvm/MC/MCExpr.h"
Chris Lattnereb40a0f2010-05-07 17:17:41 +000043#include "llvm/MC/MCSectionCOFF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000044#include "llvm/MC/MCSectionELF.h"
45#include "llvm/MC/MCSectionMachO.h"
Dan Gohmand660a5d2017-02-22 01:23:18 +000046#include "llvm/MC/MCSectionWasm.h"
Rafael Espindola30deafc2011-04-16 03:51:21 +000047#include "llvm/MC/MCStreamer.h"
Eugene Zelenko90d99202017-02-27 22:45:06 +000048#include "llvm/MC/MCSymbol.h"
Rafael Espindolaf7e06852015-06-02 00:25:12 +000049#include "llvm/MC/MCSymbolELF.h"
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +000050#include "llvm/MC/MCValue.h"
Eugene Zelenko90d99202017-02-27 22:45:06 +000051#include "llvm/MC/SectionKind.h"
Xinliang David Lic4dee262016-01-14 18:09:45 +000052#include "llvm/ProfileData/InstrProf.h"
Eugene Zelenko90d99202017-02-27 22:45:06 +000053#include "llvm/Support/Casting.h"
54#include "llvm/Support/CodeGen.h"
Martin Storsjo9ab4c272017-11-28 08:07:18 +000055#include "llvm/Support/Format.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000056#include "llvm/Support/ErrorHandling.h"
57#include "llvm/Support/raw_ostream.h"
Chandler Carruth1decd562014-03-04 10:07:28 +000058#include "llvm/Target/TargetMachine.h"
Eugene Zelenko90d99202017-02-27 22:45:06 +000059#include <cassert>
60#include <string>
61
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000062using namespace llvm;
Anton Korobeynikov293d5922010-02-21 20:28:15 +000063using namespace dwarf;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000064
Peter Collingbourne9283a092017-06-12 20:10:48 +000065static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
Saleem Abdulrasool5f204902017-06-05 21:26:39 +000066 StringRef &Section) {
Peter Collingbourne9283a092017-06-12 20:10:48 +000067 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
68 M.getModuleFlagsMetadata(ModuleFlags);
69
Saleem Abdulrasool5f204902017-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 Korobeynikov362dd0b2010-02-15 22:37:53 +000090//===----------------------------------------------------------------------===//
91// ELF
92//===----------------------------------------------------------------------===//
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000093
Michael J. Spencer3da6ce52018-07-16 00:28:24 +000094void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
95 const TargetMachine &TgtM) {
96 TargetLoweringObjectFile::Initialize(Ctx, TgtM);
97 TM = &TgtM;
Reid Kleckner3b9733f2018-08-09 22:24:04 +000098
Lei Liufa3003a2018-08-13 06:06:53 +000099 CodeModel::Model CM = TgtM.getCodeModel();
Reid Kleckner3b9733f2018-08-09 22:24:04 +0000100
101 switch (TgtM.getTargetTriple().getArch()) {
102 case Triple::arm:
103 case Triple::armeb:
104 case Triple::thumb:
105 case Triple::thumbeb:
106 if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
107 break;
108 // Fallthrough if not using EHABI
109 LLVM_FALLTHROUGH;
110 case Triple::ppc:
111 case Triple::x86:
112 PersonalityEncoding = isPositionIndependent()
113 ? dwarf::DW_EH_PE_indirect |
114 dwarf::DW_EH_PE_pcrel |
115 dwarf::DW_EH_PE_sdata4
116 : dwarf::DW_EH_PE_absptr;
117 LSDAEncoding = isPositionIndependent()
118 ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
119 : dwarf::DW_EH_PE_absptr;
120 TTypeEncoding = isPositionIndependent()
121 ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
122 dwarf::DW_EH_PE_sdata4
123 : dwarf::DW_EH_PE_absptr;
124 break;
125 case Triple::x86_64:
126 if (isPositionIndependent()) {
Lei Liufa3003a2018-08-13 06:06:53 +0000127 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
128 ((CM == CodeModel::Small || CM == CodeModel::Medium)
129 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
Reid Kleckner3b9733f2018-08-09 22:24:04 +0000130 LSDAEncoding = dwarf::DW_EH_PE_pcrel |
Lei Liufa3003a2018-08-13 06:06:53 +0000131 (CM == CodeModel::Small
132 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
Reid Kleckner3b9733f2018-08-09 22:24:04 +0000133 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
Lei Liufa3003a2018-08-13 06:06:53 +0000134 ((CM == CodeModel::Small || CM == CodeModel::Medium)
135 ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
Reid Kleckner3b9733f2018-08-09 22:24:04 +0000136 } else {
137 PersonalityEncoding =
Lei Liufa3003a2018-08-13 06:06:53 +0000138 (CM == CodeModel::Small || CM == CodeModel::Medium)
139 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
140 LSDAEncoding = (CM == CodeModel::Small)
141 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
142 TTypeEncoding = (CM == CodeModel::Small)
143 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
Reid Kleckner3b9733f2018-08-09 22:24:04 +0000144 }
145 break;
146 case Triple::hexagon:
147 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
148 LSDAEncoding = dwarf::DW_EH_PE_absptr;
149 TTypeEncoding = dwarf::DW_EH_PE_absptr;
150 if (isPositionIndependent()) {
151 PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
152 LSDAEncoding |= dwarf::DW_EH_PE_pcrel;
153 TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
154 }
155 break;
156 case Triple::aarch64:
157 case Triple::aarch64_be:
158 // The small model guarantees static code/data size < 4GB, but not where it
159 // will be in memory. Most of these could end up >2GB away so even a signed
160 // pc-relative 32-bit address is insufficient, theoretically.
161 if (isPositionIndependent()) {
162 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
163 dwarf::DW_EH_PE_sdata8;
164 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
165 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
166 dwarf::DW_EH_PE_sdata8;
167 } else {
168 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
169 LSDAEncoding = dwarf::DW_EH_PE_absptr;
170 TTypeEncoding = dwarf::DW_EH_PE_absptr;
171 }
172 break;
173 case Triple::lanai:
174 LSDAEncoding = dwarf::DW_EH_PE_absptr;
175 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
176 TTypeEncoding = dwarf::DW_EH_PE_absptr;
177 break;
178 case Triple::mips:
179 case Triple::mipsel:
180 case Triple::mips64:
181 case Triple::mips64el:
182 // MIPS uses indirect pointer to refer personality functions and types, so
183 // that the eh_frame section can be read-only. DW.ref.personality will be
184 // generated for relocation.
185 PersonalityEncoding = dwarf::DW_EH_PE_indirect;
186 // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
187 // identify N64 from just a triple.
188 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
189 dwarf::DW_EH_PE_sdata4;
190 // We don't support PC-relative LSDA references in GAS so we use the default
191 // DW_EH_PE_absptr for those.
192
193 // FreeBSD must be explicit about the data size and using pcrel since it's
194 // assembler/linker won't do the automatic conversion that the Linux tools
195 // do.
196 if (TgtM.getTargetTriple().isOSFreeBSD()) {
197 PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
198 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
199 }
200 break;
201 case Triple::ppc64:
202 case Triple::ppc64le:
203 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
204 dwarf::DW_EH_PE_udata8;
205 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
206 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
207 dwarf::DW_EH_PE_udata8;
208 break;
209 case Triple::sparcel:
210 case Triple::sparc:
211 if (isPositionIndependent()) {
212 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
213 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
214 dwarf::DW_EH_PE_sdata4;
215 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
216 dwarf::DW_EH_PE_sdata4;
217 } else {
218 LSDAEncoding = dwarf::DW_EH_PE_absptr;
219 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
220 TTypeEncoding = dwarf::DW_EH_PE_absptr;
221 }
222 break;
223 case Triple::sparcv9:
224 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
225 if (isPositionIndependent()) {
226 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
227 dwarf::DW_EH_PE_sdata4;
228 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
229 dwarf::DW_EH_PE_sdata4;
230 } else {
231 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
232 TTypeEncoding = dwarf::DW_EH_PE_absptr;
233 }
234 break;
235 case Triple::systemz:
236 // All currently-defined code models guarantee that 4-byte PC-relative
237 // values will be in range.
238 if (isPositionIndependent()) {
239 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
240 dwarf::DW_EH_PE_sdata4;
241 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
242 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
243 dwarf::DW_EH_PE_sdata4;
244 } else {
245 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
246 LSDAEncoding = dwarf::DW_EH_PE_absptr;
247 TTypeEncoding = dwarf::DW_EH_PE_absptr;
248 }
249 break;
250 default:
251 break;
252 }
Michael J. Spencer3da6ce52018-07-16 00:28:24 +0000253}
254
Eric Christopher26c5a962018-04-20 19:07:57 +0000255void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
256 Module &M) const {
Saleem Abdulrasoole7676fe2018-01-30 16:29:29 +0000257 auto &C = getContext();
258
259 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
260 auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
261 ELF::SHF_EXCLUDE);
262
263 Streamer.SwitchSection(S);
264
265 for (const auto &Operand : LinkerOptions->operands()) {
266 if (cast<MDNode>(Operand)->getNumOperands() != 2)
267 report_fatal_error("invalid llvm.linker.options");
268 for (const auto &Option : cast<MDNode>(Operand)->operands()) {
269 Streamer.EmitBytes(cast<MDString>(Option)->getString());
270 Streamer.EmitIntValue(0, 1);
271 }
272 }
273 }
274
Saleem Abdulrasool5f204902017-06-05 21:26:39 +0000275 unsigned Version = 0;
276 unsigned Flags = 0;
277 StringRef Section;
278
Peter Collingbourne9283a092017-06-12 20:10:48 +0000279 GetObjCImageInfo(M, Version, Flags, Section);
Michael J. Spencer3da6ce52018-07-16 00:28:24 +0000280 if (!Section.empty()) {
281 auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
282 Streamer.SwitchSection(S);
283 Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
284 Streamer.EmitIntValue(Version, 4);
285 Streamer.EmitIntValue(Flags, 4);
286 Streamer.AddBlankLine();
287 }
288
289 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
290 M.getModuleFlagsMetadata(ModuleFlags);
291
292 MDNode *CFGProfile = nullptr;
293
294 for (const auto &MFE : ModuleFlags) {
295 StringRef Key = MFE.Key->getString();
296 if (Key == "CG Profile") {
297 CFGProfile = cast<MDNode>(MFE.Val);
298 break;
299 }
300 }
301
302 if (!CFGProfile)
Saleem Abdulrasool5f204902017-06-05 21:26:39 +0000303 return;
304
Michael J. Spencer3da6ce52018-07-16 00:28:24 +0000305 auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
306 if (!MDO)
307 return nullptr;
308 auto V = cast<ValueAsMetadata>(MDO);
309 const Function *F = cast<Function>(V->getValue());
310 return TM->getSymbol(F);
311 };
312
313 for (const auto &Edge : CFGProfile->operands()) {
314 MDNode *E = cast<MDNode>(Edge);
315 const MCSymbol *From = GetSym(E->getOperand(0));
316 const MCSymbol *To = GetSym(E->getOperand(1));
317 // Skip null functions. This can happen if functions are dead stripped after
318 // the CGProfile pass has been run.
319 if (!From || !To)
320 continue;
321 uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
322 ->getValue()
323 ->getUniqueInteger()
324 .getZExtValue();
325 Streamer.emitCGProfileEntry(
326 MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
327 MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
328 }
Saleem Abdulrasool5f204902017-06-05 21:26:39 +0000329}
330
Rafael Espindola737c9f62014-02-19 17:23:20 +0000331MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
Eric Christopher88a23b62016-09-16 07:33:15 +0000332 const GlobalValue *GV, const TargetMachine &TM,
Rafael Espindola737c9f62014-02-19 17:23:20 +0000333 MachineModuleInfo *MMI) const {
Rafael Espindola60246a92011-04-27 23:17:57 +0000334 unsigned Encoding = getPersonalityEncoding();
Eugene Zelenko90d99202017-02-27 22:45:06 +0000335 if ((Encoding & 0x80) == DW_EH_PE_indirect)
Jim Grosbach19696da2015-05-18 18:43:14 +0000336 return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
Tim Northoverbc347f22016-11-22 16:17:20 +0000337 TM.getSymbol(GV)->getName());
Eugene Zelenko90d99202017-02-27 22:45:06 +0000338 if ((Encoding & 0x70) == DW_EH_PE_absptr)
Tim Northoverbc347f22016-11-22 16:17:20 +0000339 return TM.getSymbol(GV);
Logan Chien731d23e2014-05-30 16:48:56 +0000340 report_fatal_error("We do not support this DWARF encoding yet!");
Rafael Espindola30deafc2011-04-16 03:51:21 +0000341}
342
Mehdi Aminia5574d62015-07-16 06:04:17 +0000343void TargetLoweringObjectFileELF::emitPersonalityValue(
344 MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
Rafael Espindolafb66f472011-06-13 03:09:13 +0000345 SmallString<64> NameData("DW.ref.");
346 NameData += Sym->getName();
Rafael Espindolaf7e06852015-06-02 00:25:12 +0000347 MCSymbolELF *Label =
348 cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
Rafael Espindola018e38c2011-04-27 21:29:52 +0000349 Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
350 Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
Rafael Espindola018e38c2011-04-27 21:29:52 +0000351 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
Eric Christopherdea5ff12016-07-01 06:07:38 +0000352 MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
353 ELF::SHT_PROGBITS, Flags, 0);
Mehdi Aminia5574d62015-07-16 06:04:17 +0000354 unsigned Size = DL.getPointerSize();
Rafael Espindola018e38c2011-04-27 21:29:52 +0000355 Streamer.SwitchSection(Sec);
Fangrui Song0b5d88f2017-11-15 06:17:32 +0000356 Streamer.EmitValueToAlignment(DL.getPointerABIAlignment(0));
Rafael Espindola018e38c2011-04-27 21:29:52 +0000357 Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
Jim Grosbach586c0042015-05-30 01:25:56 +0000358 const MCExpr *E = MCConstantExpr::create(Size, getContext());
Rafael Espindolaf7e06852015-06-02 00:25:12 +0000359 Streamer.emitELFSize(Label, E);
Rafael Espindola018e38c2011-04-27 21:29:52 +0000360 Streamer.EmitLabel(Label);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000361
Rafael Espindola018e38c2011-04-27 21:29:52 +0000362 Streamer.EmitSymbolValue(Sym, Size);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000363}
364
Rafael Espindola965e3bc2014-02-09 14:50:44 +0000365const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
Eric Christopher88a23b62016-09-16 07:33:15 +0000366 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
367 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Eugene Zelenko90d99202017-02-27 22:45:06 +0000368 if (Encoding & DW_EH_PE_indirect) {
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000369 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
370
Eric Christopher88a23b62016-09-16 07:33:15 +0000371 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM);
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000372
373 // Add information about the stub reference to ELFMMI so that the stub
374 // gets emitted by the asmprinter.
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000375 MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
Craig Topper4ba84432014-04-14 00:51:57 +0000376 if (!StubSym.getPointer()) {
Tim Northoverbc347f22016-11-22 16:17:20 +0000377 MCSymbol *Sym = TM.getSymbol(GV);
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000378 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
379 }
380
381 return TargetLoweringObjectFile::
Jim Grosbach586c0042015-05-30 01:25:56 +0000382 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Eugene Zelenko90d99202017-02-27 22:45:06 +0000383 Encoding & ~DW_EH_PE_indirect, Streamer);
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000384 }
385
Eric Christopher88a23b62016-09-16 07:33:15 +0000386 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
387 MMI, Streamer);
Anton Korobeynikov25efd6d2012-11-14 01:47:00 +0000388}
389
Sam Clegg2b6b6ac2017-10-20 21:28:38 +0000390static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
Eric Christopherbc809c32018-05-16 20:34:00 +0000391 // N.B.: The defaults used in here are not the same ones used in MC.
Rafael Espindolae6657982011-05-24 03:10:31 +0000392 // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
393 // both gas and MC will produce a section with no flags. Given
Bill Wendling96cb1122012-07-19 00:04:14 +0000394 // section(".eh_frame") gcc will produce:
395 //
396 // .section .eh_frame,"a",@progbits
Xinliang David Lie8752f42017-04-14 17:48:40 +0000397
Vedant Kumar737d8b62017-04-15 00:09:57 +0000398 if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF,
399 /*AddSegmentInfo=*/false))
Xinliang David Lic4dee262016-01-14 18:09:45 +0000400 return SectionKind::getMetadata();
401
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000402 if (Name.empty() || Name[0] != '.') return K;
403
Eric Christopher3de27402018-05-18 02:39:57 +0000404 // Default implementation based on some magic section names.
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000405 if (Name == ".bss" ||
406 Name.startswith(".bss.") ||
407 Name.startswith(".gnu.linkonce.b.") ||
408 Name.startswith(".llvm.linkonce.b.") ||
409 Name == ".sbss" ||
410 Name.startswith(".sbss.") ||
411 Name.startswith(".gnu.linkonce.sb.") ||
412 Name.startswith(".llvm.linkonce.sb."))
413 return SectionKind::getBSS();
414
415 if (Name == ".tdata" ||
416 Name.startswith(".tdata.") ||
417 Name.startswith(".gnu.linkonce.td.") ||
418 Name.startswith(".llvm.linkonce.td."))
419 return SectionKind::getThreadData();
420
421 if (Name == ".tbss" ||
422 Name.startswith(".tbss.") ||
423 Name.startswith(".gnu.linkonce.tb.") ||
424 Name.startswith(".llvm.linkonce.tb."))
425 return SectionKind::getThreadBSS();
426
427 return K;
428}
429
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000430static unsigned getELFSectionType(StringRef Name, SectionKind K) {
Petr Hosek38ea3162016-09-20 20:21:13 +0000431 // Use SHT_NOTE for section whose name starts with ".note" to allow
432 // emitting ELF notes from C variable declaration.
433 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
434 if (Name.startswith(".note"))
435 return ELF::SHT_NOTE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000436
437 if (Name == ".init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000438 return ELF::SHT_INIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000439
440 if (Name == ".fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000441 return ELF::SHT_FINI_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000442
443 if (Name == ".preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000444 return ELF::SHT_PREINIT_ARRAY;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000445
446 if (K.isBSS() || K.isThreadBSS())
Rafael Espindolac85dca62011-01-23 04:28:49 +0000447 return ELF::SHT_NOBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000448
Rafael Espindolac85dca62011-01-23 04:28:49 +0000449 return ELF::SHT_PROGBITS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000450}
451
Rafael Espindola76bdd012015-02-25 00:52:15 +0000452static unsigned getELFSectionFlags(SectionKind K) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000453 unsigned Flags = 0;
454
455 if (!K.isMetadata())
Rafael Espindola1c130262011-01-23 04:43:11 +0000456 Flags |= ELF::SHF_ALLOC;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000457
458 if (K.isText())
Rafael Espindola1c130262011-01-23 04:43:11 +0000459 Flags |= ELF::SHF_EXECINSTR;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000460
Prakhar Bahugunaaf672fb2016-12-15 07:59:08 +0000461 if (K.isExecuteOnly())
462 Flags |= ELF::SHF_ARM_PURECODE;
463
Rafael Espindolad846e3f2011-06-07 23:26:45 +0000464 if (K.isWriteable())
Rafael Espindola1c130262011-01-23 04:43:11 +0000465 Flags |= ELF::SHF_WRITE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000466
467 if (K.isThreadLocal())
Rafael Espindola1c130262011-01-23 04:43:11 +0000468 Flags |= ELF::SHF_TLS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000469
Rafael Espindola76bdd012015-02-25 00:52:15 +0000470 if (K.isMergeableCString() || K.isMergeableConst())
Rafael Espindola1c130262011-01-23 04:43:11 +0000471 Flags |= ELF::SHF_MERGE;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000472
473 if (K.isMergeableCString())
Rafael Espindola1c130262011-01-23 04:43:11 +0000474 Flags |= ELF::SHF_STRINGS;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000475
476 return Flags;
477}
478
David Majnemerc8a11692014-06-27 18:19:56 +0000479static const Comdat *getELFComdat(const GlobalValue *GV) {
480 const Comdat *C = GV->getComdat();
481 if (!C)
482 return nullptr;
483
484 if (C->getSelectionKind() != Comdat::Any)
485 report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
486 C->getName() + "' cannot be lowered.");
487
488 return C;
489}
490
Evgeniy Stepanovdf808fe2017-03-17 22:17:24 +0000491static const MCSymbolELF *getAssociatedSymbol(const GlobalObject *GO,
492 const TargetMachine &TM) {
493 MDNode *MD = GO->getMetadata(LLVMContext::MD_associated);
494 if (!MD)
495 return nullptr;
496
Evgeniy Stepanovfa138d22017-05-08 23:46:20 +0000497 const MDOperand &Op = MD->getOperand(0);
498 if (!Op.get())
499 return nullptr;
500
501 auto *VM = dyn_cast<ValueAsMetadata>(Op);
Evgeniy Stepanovdf808fe2017-03-17 22:17:24 +0000502 if (!VM)
503 report_fatal_error("MD_associated operand is not ValueAsMetadata");
504
505 GlobalObject *OtherGO = dyn_cast<GlobalObject>(VM->getValue());
506 return OtherGO ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGO)) : nullptr;
507}
508
Nick Desaulniersb6091352018-10-12 16:35:44 +0000509static unsigned getEntrySizeForKind(SectionKind Kind) {
510 if (Kind.isMergeable1ByteCString())
511 return 1;
512 else if (Kind.isMergeable2ByteCString())
513 return 2;
514 else if (Kind.isMergeable4ByteCString())
515 return 4;
516 else if (Kind.isMergeableConst4())
517 return 4;
518 else if (Kind.isMergeableConst8())
519 return 8;
520 else if (Kind.isMergeableConst16())
521 return 16;
522 else if (Kind.isMergeableConst32())
523 return 32;
524 else {
525 // We shouldn't have mergeable C strings or mergeable constants that we
526 // didn't handle above.
527 assert(!Kind.isMergeableCString() && "unknown string width");
528 assert(!Kind.isMergeableConst() && "unknown data width");
529 return 0;
530 }
531}
532
Rafael Espindola75219642015-05-21 19:20:38 +0000533MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000534 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
535 StringRef SectionName = GO->getSection();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000536
Javed Absar13aa0772017-06-05 10:09:13 +0000537 // Check if '#pragma clang section' name is applicable.
538 // Note that pragma directive overrides -ffunction-section, -fdata-section
539 // and so section name is exactly as user specified and not uniqued.
540 const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
541 if (GV && GV->hasImplicitSection()) {
542 auto Attrs = GV->getAttributes();
543 if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
544 SectionName = Attrs.getAttribute("bss-section").getValueAsString();
545 } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
546 SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
547 } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
548 SectionName = Attrs.getAttribute("data-section").getValueAsString();
549 }
550 }
551 const Function *F = dyn_cast<Function>(GO);
552 if (F && F->hasFnAttribute("implicit-section-name")) {
553 SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
554 }
555
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000556 // Infer section flags from the section name if we can.
557 Kind = getELFKindForNamedSection(SectionName, Kind);
558
David Majnemerc8a11692014-06-27 18:19:56 +0000559 StringRef Group = "";
Rafael Espindola76bdd012015-02-25 00:52:15 +0000560 unsigned Flags = getELFSectionFlags(Kind);
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000561 if (const Comdat *C = getELFComdat(GO)) {
David Majnemerc8a11692014-06-27 18:19:56 +0000562 Group = C->getName();
563 Flags |= ELF::SHF_GROUP;
564 }
Evgeniy Stepanovdf808fe2017-03-17 22:17:24 +0000565
566 // A section can have at most one associated section. Put each global with
567 // MD_associated in a unique section.
568 unsigned UniqueID = MCContext::GenericSectionID;
569 const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
570 if (AssociatedSymbol) {
571 UniqueID = NextUniqueID++;
572 Flags |= ELF::SHF_LINK_ORDER;
573 }
574
575 MCSectionELF *Section = getContext().getELFSection(
576 SectionName, getELFSectionType(SectionName, Kind), Flags,
Nick Desaulniersb6091352018-10-12 16:35:44 +0000577 getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol);
Evgeniy Stepanovdf808fe2017-03-17 22:17:24 +0000578 // Make sure that we did not get some other section with incompatible sh_link.
579 // This should not be possible due to UniqueID code above.
Eric Christopher54da16e2018-06-21 07:15:14 +0000580 assert(Section->getAssociatedSymbol() == AssociatedSymbol &&
581 "Associated symbol mismatch between sections");
Evgeniy Stepanovdf808fe2017-03-17 22:17:24 +0000582 return Section;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000583}
584
Rafael Espindola83185522015-02-11 14:44:17 +0000585/// Return the section prefix name used by options FunctionsSections and
586/// DataSections.
David Majnemerfde22c12014-06-24 16:01:53 +0000587static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
Rafael Espindola83185522015-02-11 14:44:17 +0000588 if (Kind.isText())
Rafael Espindolae0a25412015-02-17 20:48:01 +0000589 return ".text";
Rafael Espindola83185522015-02-11 14:44:17 +0000590 if (Kind.isReadOnly())
Rafael Espindolae0a25412015-02-17 20:48:01 +0000591 return ".rodata";
Rafael Espindola83185522015-02-11 14:44:17 +0000592 if (Kind.isBSS())
Rafael Espindolae0a25412015-02-17 20:48:01 +0000593 return ".bss";
Rafael Espindola83185522015-02-11 14:44:17 +0000594 if (Kind.isThreadData())
Rafael Espindolae0a25412015-02-17 20:48:01 +0000595 return ".tdata";
Rafael Espindola83185522015-02-11 14:44:17 +0000596 if (Kind.isThreadBSS())
Rafael Espindolae0a25412015-02-17 20:48:01 +0000597 return ".tbss";
Rafael Espindolacfc74b72015-11-18 06:02:15 +0000598 if (Kind.isData())
Rafael Espindolae0a25412015-02-17 20:48:01 +0000599 return ".data";
Chris Lattner43ac7212010-04-13 00:36:43 +0000600 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Rafael Espindolae0a25412015-02-17 20:48:01 +0000601 return ".data.rel.ro";
Chris Lattner43ac7212010-04-13 00:36:43 +0000602}
603
Evgeniy Stepanovdf808fe2017-03-17 22:17:24 +0000604static MCSectionELF *selectELFSectionForGlobal(
605 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
606 const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
607 unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) {
Rafael Espindola76bdd012015-02-25 00:52:15 +0000608
Rafael Espindolac0939732015-02-20 23:28:28 +0000609 StringRef Group = "";
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000610 if (const Comdat *C = getELFComdat(GO)) {
Rafael Espindolac0939732015-02-20 23:28:28 +0000611 Flags |= ELF::SHF_GROUP;
612 Group = C->getName();
613 }
614
Eric Christopher5311a6e2018-08-01 01:29:30 +0000615 // Get the section entry size based on the kind.
616 unsigned EntrySize = getEntrySizeForKind(Kind);
617
Rafael Espindola76bdd012015-02-25 00:52:15 +0000618 SmallString<128> Name;
Rafael Espindolaf43ce562015-01-28 17:54:19 +0000619 if (Kind.isMergeableCString()) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000620 // We also need alignment here.
621 // FIXME: this is getting the alignment of the character, not the
622 // alignment of the global!
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000623 unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
624 cast<GlobalVariable>(GO));
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000625
Rafael Espindola9936b802015-01-29 17:33:21 +0000626 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
Rafael Espindola76bdd012015-02-25 00:52:15 +0000627 Name = SizeSpec + utostr(Align);
628 } else if (Kind.isMergeableConst()) {
629 Name = ".rodata.cst";
630 Name += utostr(EntrySize);
631 } else {
632 Name = getSectionPrefixForGlobal(Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000633 }
Dehao Chen977fc822016-10-18 20:42:47 +0000634
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000635 if (const auto *F = dyn_cast<Function>(GO)) {
Dehao Chen977fc822016-10-18 20:42:47 +0000636 const auto &OptionalPrefix = F->getSectionPrefix();
637 if (OptionalPrefix)
638 Name += *OptionalPrefix;
639 }
Dehao Chen5261d132016-02-23 03:39:24 +0000640
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000641 unsigned UniqueID = MCContext::GenericSectionID;
Eric Christopherbf9b77d2018-08-01 01:03:34 +0000642 if (EmitUniqueSection) {
643 if (TM.getUniqueSectionNames()) {
644 Name.push_back('.');
645 TM.getNameWithPrefix(Name, GO, Mang, true /*MayAlwaysUsePrivate*/);
646 } else {
647 UniqueID = *NextUniqueID;
648 (*NextUniqueID)++;
649 }
Rafael Espindola903f4a22015-04-04 18:02:01 +0000650 }
Eric Christopherbf9b77d2018-08-01 01:03:34 +0000651 // Use 0 as the unique ID for execute-only text.
Prakhar Bahugunaaf672fb2016-12-15 07:59:08 +0000652 if (Kind.isExecuteOnly())
653 UniqueID = 0;
Rafael Espindolafc0ad8d2015-02-26 23:55:11 +0000654 return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
Evgeniy Stepanovdf808fe2017-03-17 22:17:24 +0000655 EntrySize, Group, UniqueID, AssociatedSymbol);
Rafael Espindolafc0ad8d2015-02-26 23:55:11 +0000656}
657
Rafael Espindola75219642015-05-21 19:20:38 +0000658MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000659 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Rafael Espindolafc0ad8d2015-02-26 23:55:11 +0000660 unsigned Flags = getELFSectionFlags(Kind);
661
662 // If we have -ffunction-section or -fdata-section then we should emit the
663 // global value to a uniqued section specifically for it.
664 bool EmitUniqueSection = false;
665 if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
666 if (Kind.isText())
667 EmitUniqueSection = TM.getFunctionSections();
668 else
669 EmitUniqueSection = TM.getDataSections();
670 }
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000671 EmitUniqueSection |= GO->hasComdat();
Rafael Espindolafc0ad8d2015-02-26 23:55:11 +0000672
Evgeniy Stepanovdf808fe2017-03-17 22:17:24 +0000673 const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
674 if (AssociatedSymbol) {
675 EmitUniqueSection = true;
676 Flags |= ELF::SHF_LINK_ORDER;
677 }
678
679 MCSectionELF *Section = selectELFSectionForGlobal(
680 getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags,
681 &NextUniqueID, AssociatedSymbol);
682 assert(Section->getAssociatedSymbol() == AssociatedSymbol);
683 return Section;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000684}
685
Rafael Espindola75219642015-05-21 19:20:38 +0000686MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
Eric Christopher88a23b62016-09-16 07:33:15 +0000687 const Function &F, const TargetMachine &TM) const {
Rafael Espindola8eeedf72015-02-12 17:16:46 +0000688 // If the function can be removed, produce a unique section so that
689 // the table doesn't prevent the removal.
690 const Comdat *C = F.getComdat();
691 bool EmitUniqueSection = TM.getFunctionSections() || C;
692 if (!EmitUniqueSection)
693 return ReadOnlySection;
694
Rafael Espindolafc0ad8d2015-02-26 23:55:11 +0000695 return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
Evgeniy Stepanovdf808fe2017-03-17 22:17:24 +0000696 getMangler(), TM, EmitUniqueSection,
697 ELF::SHF_ALLOC, &NextUniqueID,
698 /* AssociatedSymbol */ nullptr);
Rafael Espindola8eeedf72015-02-12 17:16:46 +0000699}
700
Rafael Espindola3b75cfe2015-02-17 23:34:51 +0000701bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
702 bool UsesLabelDifference, const Function &F) const {
703 // We can always create relative relocations, so use another section
704 // that can be marked non-executable.
705 return false;
706}
707
Rafael Espindola75219642015-05-21 19:20:38 +0000708/// Given a mergeable constant with the specified size and relocation
709/// information, return a section that it should be placed in.
Mehdi Aminia5574d62015-07-16 06:04:17 +0000710MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
David Majnemer36935fd2016-02-21 01:30:30 +0000711 const DataLayout &DL, SectionKind Kind, const Constant *C,
712 unsigned &Align) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000713 if (Kind.isMergeableConst4() && MergeableConst4Section)
714 return MergeableConst4Section;
715 if (Kind.isMergeableConst8() && MergeableConst8Section)
716 return MergeableConst8Section;
717 if (Kind.isMergeableConst16() && MergeableConst16Section)
718 return MergeableConst16Section;
David Majnemerb23b0ff2016-02-22 22:23:11 +0000719 if (Kind.isMergeableConst32() && MergeableConst32Section)
720 return MergeableConst32Section;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000721 if (Kind.isReadOnly())
722 return ReadOnlySection;
723
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000724 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
725 return DataRelROSection;
726}
727
Rafael Espindola75219642015-05-21 19:20:38 +0000728static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
729 bool IsCtor, unsigned Priority,
730 const MCSymbol *KeySym) {
Rafael Espindola295a0082014-09-04 23:03:58 +0000731 std::string Name;
732 unsigned Type;
733 unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
Rafael Espindola295a0082014-09-04 23:03:58 +0000734 StringRef COMDAT = KeySym ? KeySym->getName() : "";
735
736 if (KeySym)
737 Flags |= ELF::SHF_GROUP;
Anton Korobeynikov4a99f592012-01-25 22:24:19 +0000738
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000739 if (UseInitArray) {
Rafael Espindola27589ac2014-09-05 00:02:50 +0000740 if (IsCtor) {
741 Type = ELF::SHT_INIT_ARRAY;
742 Name = ".init_array";
743 } else {
744 Type = ELF::SHT_FINI_ARRAY;
745 Name = ".fini_array";
746 }
Rafael Espindola295a0082014-09-04 23:03:58 +0000747 if (Priority != 65535) {
748 Name += '.';
749 Name += utostr(Priority);
750 }
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000751 } else {
Rafael Espindola295a0082014-09-04 23:03:58 +0000752 // The default scheme is .ctor / .dtor, so we have to invert the priority
753 // numbering.
Rafael Espindola27589ac2014-09-05 00:02:50 +0000754 if (IsCtor)
755 Name = ".ctors";
756 else
757 Name = ".dtors";
Martin Storsjo9ab4c272017-11-28 08:07:18 +0000758 if (Priority != 65535)
759 raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
Rafael Espindola295a0082014-09-04 23:03:58 +0000760 Type = ELF::SHT_PROGBITS;
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000761 }
Rafael Espindola295a0082014-09-04 23:03:58 +0000762
Rafael Espindola9936b802015-01-29 17:33:21 +0000763 return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
Rafael Espindola27589ac2014-09-05 00:02:50 +0000764}
765
Rafael Espindola75219642015-05-21 19:20:38 +0000766MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
Rafael Espindola27589ac2014-09-05 00:02:50 +0000767 unsigned Priority, const MCSymbol *KeySym) const {
768 return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
769 KeySym);
Anton Korobeynikov4a99f592012-01-25 22:24:19 +0000770}
771
Rafael Espindola75219642015-05-21 19:20:38 +0000772MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
Rafael Espindola013321a2014-06-06 19:26:12 +0000773 unsigned Priority, const MCSymbol *KeySym) const {
Rafael Espindola27589ac2014-09-05 00:02:50 +0000774 return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
775 KeySym);
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000776}
777
Peter Collingbourne50119132016-04-22 20:40:10 +0000778const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
Eric Christopher88a23b62016-09-16 07:33:15 +0000779 const GlobalValue *LHS, const GlobalValue *RHS,
Peter Collingbourne50119132016-04-22 20:40:10 +0000780 const TargetMachine &TM) const {
781 // We may only use a PLT-relative relocation to refer to unnamed_addr
782 // functions.
Peter Collingbourne63b34cd2016-06-14 21:01:22 +0000783 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
Peter Collingbourne50119132016-04-22 20:40:10 +0000784 return nullptr;
785
786 // Basic sanity checks.
787 if (LHS->getType()->getPointerAddressSpace() != 0 ||
788 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
789 RHS->isThreadLocal())
790 return nullptr;
791
792 return MCBinaryExpr::createSub(
Tim Northoverbc347f22016-11-22 16:17:20 +0000793 MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind,
Peter Collingbourne50119132016-04-22 20:40:10 +0000794 getContext()),
Tim Northoverbc347f22016-11-22 16:17:20 +0000795 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
Peter Collingbourne50119132016-04-22 20:40:10 +0000796}
797
Scott Linderb9a4d1d2018-12-14 15:38:15 +0000798MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const {
799 // Use ".GCC.command.line" since this feature is to support clang's
800 // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the
801 // same name.
802 return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS,
803 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
804}
805
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000806void
807TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
808 UseInitArray = UseInitArray_;
Rafael Espindola1a569482016-08-29 12:33:42 +0000809 MCContext &Ctx = getContext();
810 if (!UseInitArray) {
811 StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
812 ELF::SHF_ALLOC | ELF::SHF_WRITE);
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000813
Rafael Espindola1a569482016-08-29 12:33:42 +0000814 StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
815 ELF::SHF_ALLOC | ELF::SHF_WRITE);
816 return;
817 }
818
819 StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
820 ELF::SHF_WRITE | ELF::SHF_ALLOC);
821 StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
822 ELF::SHF_WRITE | ELF::SHF_ALLOC);
Anton Korobeynikov4a99f592012-01-25 22:24:19 +0000823}
824
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000825//===----------------------------------------------------------------------===//
826// MachO
827//===----------------------------------------------------------------------===//
828
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +0000829TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
830 : TargetLoweringObjectFile() {
831 SupportIndirectSymViaGOTPCRel = true;
832}
833
Rafael Espindola1a569482016-08-29 12:33:42 +0000834void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
835 const TargetMachine &TM) {
836 TargetLoweringObjectFile::Initialize(Ctx, TM);
Rafael Espindola6e61a442016-08-29 12:47:22 +0000837 if (TM.getRelocationModel() == Reloc::Static) {
Rafael Espindola1a569482016-08-29 12:33:42 +0000838 StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
839 SectionKind::getData());
840 StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
841 SectionKind::getData());
842 } else {
843 StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
844 MachO::S_MOD_INIT_FUNC_POINTERS,
845 SectionKind::getData());
846 StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
847 MachO::S_MOD_TERM_FUNC_POINTERS,
848 SectionKind::getData());
849 }
Reid Kleckner3b9733f2018-08-09 22:24:04 +0000850
851 PersonalityEncoding =
852 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
853 LSDAEncoding = dwarf::DW_EH_PE_pcrel;
854 TTypeEncoding =
855 dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
Rafael Espindola1a569482016-08-29 12:33:42 +0000856}
857
Eric Christopher26c5a962018-04-20 19:07:57 +0000858void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer,
859 Module &M) const {
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000860 // Emit the linker options if present.
Peter Collingbourne9283a092017-06-12 20:10:48 +0000861 if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
Saleem Abdulrasool5ac1e612016-04-30 18:15:34 +0000862 for (const auto &Option : LinkerOptions->operands()) {
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000863 SmallVector<std::string, 4> StrOptions;
Saleem Abdulrasool5ac1e612016-04-30 18:15:34 +0000864 for (const auto &Piece : cast<MDNode>(Option)->operands())
865 StrOptions.push_back(cast<MDString>(Piece)->getString());
Daniel Dunbar6d49b682013-01-18 19:37:00 +0000866 Streamer.EmitLinkerOptions(StrOptions);
867 }
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000868 }
869
Saleem Abdulrasool5f204902017-06-05 21:26:39 +0000870 unsigned VersionVal = 0;
871 unsigned ImageInfoFlags = 0;
872 StringRef SectionVal;
Peter Collingbourne9283a092017-06-12 20:10:48 +0000873
874 GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal);
Saleem Abdulrasool5f204902017-06-05 21:26:39 +0000875
Bill Wendling057d5212012-02-15 22:36:15 +0000876 // The section is mandatory. If we don't have it, then we don't have GC info.
Saleem Abdulrasool5f204902017-06-05 21:26:39 +0000877 if (SectionVal.empty())
878 return;
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000879
Bill Wendling057d5212012-02-15 22:36:15 +0000880 StringRef Segment, Section;
881 unsigned TAA = 0, StubSize = 0;
882 bool TAAParsed;
883 std::string ErrorCode =
884 MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
885 TAA, TAAParsed, StubSize);
Bill Wendling2de3ff52012-02-15 22:47:53 +0000886 if (!ErrorCode.empty())
Bill Wendling057d5212012-02-15 22:36:15 +0000887 // If invalid, report the error with report_fatal_error.
Bill Wendling2de3ff52012-02-15 22:47:53 +0000888 report_fatal_error("Invalid section specifier '" + Section + "': " +
889 ErrorCode + ".");
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000890
Bill Wendling057d5212012-02-15 22:36:15 +0000891 // Get the section.
Rafael Espindola75219642015-05-21 19:20:38 +0000892 MCSectionMachO *S = getContext().getMachOSection(
Rafael Espindolacfc74b72015-11-18 06:02:15 +0000893 Segment, Section, TAA, StubSize, SectionKind::getData());
Bill Wendling057d5212012-02-15 22:36:15 +0000894 Streamer.SwitchSection(S);
895 Streamer.EmitLabel(getContext().
Jim Grosbach19696da2015-05-18 18:43:14 +0000896 getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000897 Streamer.EmitIntValue(VersionVal, 4);
Bill Wendlingadb082c2012-04-24 11:03:50 +0000898 Streamer.EmitIntValue(ImageInfoFlags, 4);
Bill Wendlingb464d3f2012-02-14 21:28:13 +0000899 Streamer.AddBlankLine();
900}
901
David Majnemerc8a11692014-06-27 18:19:56 +0000902static void checkMachOComdat(const GlobalValue *GV) {
903 const Comdat *C = GV->getComdat();
904 if (!C)
905 return;
906
907 report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
908 "' cannot be lowered.");
909}
910
Rafael Espindola75219642015-05-21 19:20:38 +0000911MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000912 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000913 // Parse the section specifier and create it if valid.
914 StringRef Segment, Section;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000915 unsigned TAA = 0, StubSize = 0;
916 bool TAAParsed;
David Majnemerc8a11692014-06-27 18:19:56 +0000917
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000918 checkMachOComdat(GO);
David Majnemerc8a11692014-06-27 18:19:56 +0000919
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000920 std::string ErrorCode =
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000921 MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000922 TAA, TAAParsed, StubSize);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000923 if (!ErrorCode.empty()) {
Chris Lattner75361b62010-04-07 22:58:41 +0000924 // If invalid, report the error with report_fatal_error.
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000925 report_fatal_error("Global variable '" + GO->getName() +
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000926 "' has an invalid section specifier '" +
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000927 GO->getSection() + "': " + ErrorCode + ".");
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000928 }
929
930 // Get the section.
Rafael Espindola75219642015-05-21 19:20:38 +0000931 MCSectionMachO *S =
932 getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000933
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000934 // If TAA wasn't set by ParseSectionSpecifier() above,
935 // use the value returned by getMachOSection() as a default.
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000936 if (!TAAParsed)
Stuart Hastings6ad82d82011-02-21 17:27:17 +0000937 TAA = S->getTypeAndAttributes();
938
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000939 // Okay, now that we got the section, verify that the TAA & StubSize agree.
940 // If the user declared multiple globals with different section flags, we need
941 // to reject it here.
942 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
Chris Lattner75361b62010-04-07 22:58:41 +0000943 // If invalid, report the error with report_fatal_error.
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000944 report_fatal_error("Global variable '" + GO->getName() +
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000945 "' section type or attributes does not match previous"
946 " section specifier");
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000947 }
948
949 return S;
950}
951
Rafael Espindola75219642015-05-21 19:20:38 +0000952MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000953 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
954 checkMachOComdat(GO);
Bill Wendlingfe128c62013-11-17 10:53:13 +0000955
956 // Handle thread local data.
957 if (Kind.isThreadBSS()) return TLSBSSSection;
958 if (Kind.isThreadData()) return TLSDataSection;
959
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000960 if (Kind.isText())
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000961 return GO->isWeakForLinker() ? TextCoalSection : TextSection;
Arnold Schwaighofer9b19dfc2013-08-08 21:04:16 +0000962
963 // If this is weak/linkonce, put this in a coalescable section, either in text
964 // or data depending on if it is writable.
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000965 if (GO->isWeakForLinker()) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000966 if (Kind.isReadOnly())
Arnold Schwaighofer9b19dfc2013-08-08 21:04:16 +0000967 return ConstTextCoalSection;
Steven Wubba76862018-04-10 20:16:35 +0000968 if (Kind.isReadOnlyWithRel())
969 return ConstDataCoalSection;
Arnold Schwaighofer9b19dfc2013-08-08 21:04:16 +0000970 return DataCoalSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000971 }
972
973 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner98f15d22010-03-07 04:28:09 +0000974 if (Kind.isMergeable1ByteCString() &&
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000975 GO->getParent()->getDataLayout().getPreferredAlignment(
976 cast<GlobalVariable>(GO)) < 32)
Chris Lattner98f15d22010-03-07 04:28:09 +0000977 return CStringSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +0000978
Chris Lattner98f15d22010-03-07 04:28:09 +0000979 // Do not put 16-bit arrays in the UString section if they have an
980 // externally visible label, this runs into issues with certain linker
981 // versions.
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000982 if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
983 GO->getParent()->getDataLayout().getPreferredAlignment(
984 cast<GlobalVariable>(GO)) < 32)
Chris Lattner98f15d22010-03-07 04:28:09 +0000985 return UStringSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000986
Rafael Espindola4bb53502014-08-28 20:13:31 +0000987 // With MachO only variables whose corresponding symbol starts with 'l' or
988 // 'L' can be merged, so we only try merging GVs with private linkage.
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000989 if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000990 if (Kind.isMergeableConst4())
991 return FourByteConstantSection;
992 if (Kind.isMergeableConst8())
993 return EightByteConstantSection;
Rafael Espindolacac25de2014-02-13 23:16:11 +0000994 if (Kind.isMergeableConst16())
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +0000995 return SixteenByteConstantSection;
996 }
997
998 // Otherwise, if it is readonly, but not something we can specially optimize,
999 // just drop it in .const.
1000 if (Kind.isReadOnly())
1001 return ReadOnlySection;
1002
1003 // If this is marked const, put it into a const section. But if the dynamic
1004 // linker needs to write to it, put it in the data segment.
1005 if (Kind.isReadOnlyWithRel())
1006 return ConstDataSection;
1007
1008 // Put zero initialized globals with strong external linkage in the
1009 // DATA, __common section with the .zerofill directive.
1010 if (Kind.isBSSExtern())
1011 return DataCommonSection;
1012
1013 // Put zero initialized globals with local linkage in __DATA,__bss directive
1014 // with the .zerofill directive (aka .lcomm).
1015 if (Kind.isBSSLocal())
1016 return DataBSSSection;
Michael J. Spencer579d7a32010-10-27 18:52:20 +00001017
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001018 // Otherwise, just drop the variable in the normal data section.
1019 return DataSection;
1020}
1021
Mehdi Aminia5574d62015-07-16 06:04:17 +00001022MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
David Majnemer36935fd2016-02-21 01:30:30 +00001023 const DataLayout &DL, SectionKind Kind, const Constant *C,
1024 unsigned &Align) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001025 // If this constant requires a relocation, we have to put it in the data
1026 // segment, not in the text segment.
Rafael Espindolacfc74b72015-11-18 06:02:15 +00001027 if (Kind.isData() || Kind.isReadOnlyWithRel())
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001028 return ConstDataSection;
1029
1030 if (Kind.isMergeableConst4())
1031 return FourByteConstantSection;
1032 if (Kind.isMergeableConst8())
1033 return EightByteConstantSection;
Rafael Espindolacac25de2014-02-13 23:16:11 +00001034 if (Kind.isMergeableConst16())
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001035 return SixteenByteConstantSection;
1036 return ReadOnlySection; // .const
1037}
1038
Rafael Espindola965e3bc2014-02-09 14:50:44 +00001039const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
Eric Christopher88a23b62016-09-16 07:33:15 +00001040 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
1041 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001042 // The mach-o version of this method defaults to returning a stub reference.
1043
Anton Korobeynikov293d5922010-02-21 20:28:15 +00001044 if (Encoding & DW_EH_PE_indirect) {
1045 MachineModuleInfoMachO &MachOMMI =
1046 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1047
Eric Christopher88a23b62016-09-16 07:33:15 +00001048 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
Anton Korobeynikov293d5922010-02-21 20:28:15 +00001049
1050 // Add information about the stub reference to MachOMMI so that the stub
1051 // gets emitted by the asmprinter.
Rafael Espindola6a70b9b2016-05-17 16:01:32 +00001052 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topper4ba84432014-04-14 00:51:57 +00001053 if (!StubSym.getPointer()) {
Tim Northoverbc347f22016-11-22 16:17:20 +00001054 MCSymbol *Sym = TM.getSymbol(GV);
Chris Lattner4c6741f2010-03-15 20:37:38 +00001055 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
Anton Korobeynikov293d5922010-02-21 20:28:15 +00001056 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001057
1058 return TargetLoweringObjectFile::
Jim Grosbach586c0042015-05-30 01:25:56 +00001059 getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
Eugene Zelenko90d99202017-02-27 22:45:06 +00001060 Encoding & ~DW_EH_PE_indirect, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001061 }
1062
Eric Christopher88a23b62016-09-16 07:33:15 +00001063 return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
1064 MMI, Streamer);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001065}
1066
Rafael Espindola737c9f62014-02-19 17:23:20 +00001067MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
Eric Christopher88a23b62016-09-16 07:33:15 +00001068 const GlobalValue *GV, const TargetMachine &TM,
Rafael Espindola737c9f62014-02-19 17:23:20 +00001069 MachineModuleInfo *MMI) const {
Rafael Espindola7afec9c2011-04-27 23:08:15 +00001070 // The mach-o version of this method defaults to returning a stub reference.
1071 MachineModuleInfoMachO &MachOMMI =
1072 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1073
Eric Christopher88a23b62016-09-16 07:33:15 +00001074 MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
Rafael Espindola7afec9c2011-04-27 23:08:15 +00001075
1076 // Add information about the stub reference to MachOMMI so that the stub
1077 // gets emitted by the asmprinter.
Bill Wendlingd7c24942011-11-29 01:43:20 +00001078 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
Craig Topper4ba84432014-04-14 00:51:57 +00001079 if (!StubSym.getPointer()) {
Tim Northoverbc347f22016-11-22 16:17:20 +00001080 MCSymbol *Sym = TM.getSymbol(GV);
Rafael Espindola7afec9c2011-04-27 23:08:15 +00001081 StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
1082 }
1083
1084 return SSym;
1085}
1086
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +00001087const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
1088 const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
1089 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
Benjamin Kramerd3c712e2015-08-08 18:27:36 +00001090 // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +00001091 // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
1092 // through a non_lazy_ptr stub instead. One advantage is that it allows the
1093 // computation of deltas to final external symbols. Example:
1094 //
1095 // _extgotequiv:
1096 // .long _extfoo
1097 //
1098 // _delta:
1099 // .long _extgotequiv-_delta
1100 //
1101 // is transformed to:
1102 //
1103 // _delta:
1104 // .long L_extfoo$non_lazy_ptr-(_delta+0)
1105 //
1106 // .section __IMPORT,__pointers,non_lazy_symbol_pointers
1107 // L_extfoo$non_lazy_ptr:
1108 // .indirect_symbol _extfoo
1109 // .long 0
1110 //
Francis Visoiu Mistrih4e270992018-12-13 17:23:30 +00001111 // The indirect symbol table (and sections of non_lazy_symbol_pointers type)
1112 // may point to both local (same translation unit) and global (other
1113 // translation units) symbols. Example:
1114 //
1115 // .section __DATA,__pointers,non_lazy_symbol_pointers
1116 // L1:
1117 // .indirect_symbol _myGlobal
1118 // .long 0
1119 // L2:
1120 // .indirect_symbol _myLocal
1121 // .long _myLocal
1122 //
1123 // If the symbol is local, instead of the symbol's index, the assembler
1124 // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table.
1125 // Then the linker will notice the constant in the table and will look at the
1126 // content of the symbol.
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +00001127 MachineModuleInfoMachO &MachOMMI =
1128 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1129 MCContext &Ctx = getContext();
1130
1131 // The offset must consider the original displacement from the base symbol
1132 // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
1133 Offset = -MV.getConstant();
1134 const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
1135
1136 // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
1137 // non_lazy_ptr stubs.
1138 SmallString<128> Name;
1139 StringRef Suffix = "$non_lazy_ptr";
Mehdi Aminia5574d62015-07-16 06:04:17 +00001140 Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +00001141 Name += Sym->getName();
1142 Name += Suffix;
Jim Grosbach19696da2015-05-18 18:43:14 +00001143 MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +00001144
1145 MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
Francis Visoiu Mistrih4e270992018-12-13 17:23:30 +00001146 if (!StubSym.getPointer()) {
1147 bool IsIndirectLocal = Sym->isDefined() && !Sym->isExternal();
1148 // With the assumption that IsIndirectLocal == GV->hasLocalLinkage().
1149 StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym),
1150 !IsIndirectLocal);
1151 }
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +00001152
1153 const MCExpr *BSymExpr =
Jim Grosbach586c0042015-05-30 01:25:56 +00001154 MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +00001155 const MCExpr *LHS =
Jim Grosbach586c0042015-05-30 01:25:56 +00001156 MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +00001157
1158 if (!Offset)
Jim Grosbach586c0042015-05-30 01:25:56 +00001159 return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +00001160
1161 const MCExpr *RHS =
Jim Grosbach586c0042015-05-30 01:25:56 +00001162 MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
1163 return MCBinaryExpr::createSub(LHS, RHS, Ctx);
Bruno Cardoso Lopesdfc63832015-03-06 13:49:05 +00001164}
1165
Peter Collingbourne05f66f32015-11-03 23:40:03 +00001166static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
1167 const MCSection &Section) {
1168 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
1169 return true;
1170
1171 // If it is not dead stripped, it is safe to use private labels.
1172 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
1173 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
1174 return true;
1175
1176 return false;
1177}
1178
1179void TargetLoweringObjectFileMachO::getNameWithPrefix(
Eric Christopher88a23b62016-09-16 07:33:15 +00001180 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
Peter Collingbourne05f66f32015-11-03 23:40:03 +00001181 const TargetMachine &TM) const {
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001182 bool CannotUsePrivateLabel = true;
1183 if (auto *GO = GV->getBaseObject()) {
1184 SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM);
1185 const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
1186 CannotUsePrivateLabel =
1187 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
1188 }
Eric Christopher88a23b62016-09-16 07:33:15 +00001189 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
Peter Collingbourne05f66f32015-11-03 23:40:03 +00001190}
1191
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001192//===----------------------------------------------------------------------===//
1193// COFF
1194//===----------------------------------------------------------------------===//
1195
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001196static unsigned
Renato Golin4a905812016-06-27 14:42:20 +00001197getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001198 unsigned Flags = 0;
Renato Golin4a905812016-06-27 14:42:20 +00001199 bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001200
Anton Korobeynikov36335be2010-07-06 15:24:56 +00001201 if (K.isMetadata())
Chris Lattner6e5ce282010-05-07 21:49:09 +00001202 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001203 COFF::IMAGE_SCN_MEM_DISCARDABLE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001204 else if (K.isText())
1205 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001206 COFF::IMAGE_SCN_MEM_EXECUTE |
Michael J. Spencer3931b542010-10-27 18:52:29 +00001207 COFF::IMAGE_SCN_MEM_READ |
Renato Golin4a905812016-06-27 14:42:20 +00001208 COFF::IMAGE_SCN_CNT_CODE |
1209 (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0);
David Majnemer3f34ae92014-09-20 07:31:46 +00001210 else if (K.isBSS())
Chris Lattner6e5ce282010-05-07 21:49:09 +00001211 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001212 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
1213 COFF::IMAGE_SCN_MEM_READ |
1214 COFF::IMAGE_SCN_MEM_WRITE;
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00001215 else if (K.isThreadLocal())
1216 Flags |=
1217 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1218 COFF::IMAGE_SCN_MEM_READ |
1219 COFF::IMAGE_SCN_MEM_WRITE;
David Majnemer3f34ae92014-09-20 07:31:46 +00001220 else if (K.isReadOnly() || K.isReadOnlyWithRel())
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001221 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001222 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1223 COFF::IMAGE_SCN_MEM_READ;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001224 else if (K.isWriteable())
1225 Flags |=
Daniel Dunbar94610582010-07-01 20:07:24 +00001226 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1227 COFF::IMAGE_SCN_MEM_READ |
1228 COFF::IMAGE_SCN_MEM_WRITE;
Chris Lattnereb40a0f2010-05-07 17:17:41 +00001229
1230 return Flags;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001231}
1232
Benjamin Kramer0e278ad2014-07-07 14:47:51 +00001233static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
David Majnemerc8a11692014-06-27 18:19:56 +00001234 const Comdat *C = GV->getComdat();
1235 assert(C && "expected GV to have a Comdat!");
1236
1237 StringRef ComdatGVName = C->getName();
1238 const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
1239 if (!ComdatGV)
1240 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
1241 "' does not exist.");
1242
1243 if (ComdatGV->getComdat() != C)
1244 report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
Hans Wennborg2ee31bc2014-09-19 01:14:56 +00001245 "' is not a key for its COMDAT.");
David Majnemerc8a11692014-06-27 18:19:56 +00001246
1247 return ComdatGV;
1248}
1249
1250static int getSelectionForCOFF(const GlobalValue *GV) {
1251 if (const Comdat *C = GV->getComdat()) {
1252 const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
1253 if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
1254 ComdatKey = GA->getBaseObject();
1255 if (ComdatKey == GV) {
1256 switch (C->getSelectionKind()) {
1257 case Comdat::Any:
1258 return COFF::IMAGE_COMDAT_SELECT_ANY;
1259 case Comdat::ExactMatch:
1260 return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
1261 case Comdat::Largest:
1262 return COFF::IMAGE_COMDAT_SELECT_LARGEST;
1263 case Comdat::NoDuplicates:
1264 return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
1265 case Comdat::SameSize:
1266 return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
1267 }
1268 } else {
1269 return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
1270 }
David Majnemerc8a11692014-06-27 18:19:56 +00001271 }
1272 return 0;
1273}
1274
Rafael Espindola75219642015-05-21 19:20:38 +00001275MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001276 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Michael J. Spencer4de58722012-11-13 22:04:09 +00001277 int Selection = 0;
Renato Golin4a905812016-06-27 14:42:20 +00001278 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001279 StringRef Name = GO->getSection();
Rafael Espindola823c9c72013-11-27 01:18:37 +00001280 StringRef COMDATSymName = "";
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001281 if (GO->hasComdat()) {
1282 Selection = getSelectionForCOFF(GO);
David Majnemerc8a11692014-06-27 18:19:56 +00001283 const GlobalValue *ComdatGV;
1284 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001285 ComdatGV = getComdatGVForCOFF(GO);
David Majnemerc8a11692014-06-27 18:19:56 +00001286 else
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001287 ComdatGV = GO;
David Majnemerc8a11692014-06-27 18:19:56 +00001288
1289 if (!ComdatGV->hasPrivateLinkage()) {
Tim Northoverbc347f22016-11-22 16:17:20 +00001290 MCSymbol *Sym = TM.getSymbol(ComdatGV);
David Majnemerc8a11692014-06-27 18:19:56 +00001291 COMDATSymName = Sym->getName();
1292 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1293 } else {
1294 Selection = 0;
1295 }
Michael J. Spencer4de58722012-11-13 22:04:09 +00001296 }
Reid Kleckner7f90dd22016-05-02 23:22:18 +00001297
1298 return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
Nico Rieck80646282013-07-06 12:13:10 +00001299 Selection);
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001300}
1301
Reid Kleckner845b4392018-06-21 20:27:38 +00001302static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001303 if (Kind.isText())
Rafael Espindola823c9c72013-11-27 01:18:37 +00001304 return ".text";
David Majnemere4d89ec2014-04-08 22:33:40 +00001305 if (Kind.isBSS())
Rafael Espindola823c9c72013-11-27 01:18:37 +00001306 return ".bss";
1307 if (Kind.isThreadLocal())
Rafael Espindola0de62552013-11-27 15:52:11 +00001308 return ".tls$";
David Majnemerd80fc692014-09-22 20:39:23 +00001309 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
1310 return ".rdata";
1311 return ".data";
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001312}
1313
Rafael Espindola75219642015-05-21 19:20:38 +00001314MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001315 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
David Majnemer77df5162014-03-23 17:47:39 +00001316 // If we have -ffunction-sections then we should emit the global value to a
1317 // uniqued section specifically for it.
David Majnemer4fd5cd02014-03-25 06:14:26 +00001318 bool EmitUniquedSection;
1319 if (Kind.isText())
1320 EmitUniquedSection = TM.getFunctionSections();
1321 else
1322 EmitUniquedSection = TM.getDataSections();
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001323
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001324 if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
Reid Kleckner845b4392018-06-21 20:27:38 +00001325 SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind);
1326
Renato Golin4a905812016-06-27 14:42:20 +00001327 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Chris Lattner6e5ce282010-05-07 21:49:09 +00001328
Daniel Dunbar94610582010-07-01 20:07:24 +00001329 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001330 int Selection = getSelectionForCOFF(GO);
David Majnemerc8a11692014-06-27 18:19:56 +00001331 if (!Selection)
1332 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
1333 const GlobalValue *ComdatGV;
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001334 if (GO->hasComdat())
1335 ComdatGV = getComdatGVForCOFF(GO);
David Majnemerc8a11692014-06-27 18:19:56 +00001336 else
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001337 ComdatGV = GO;
David Majnemerc8a11692014-06-27 18:19:56 +00001338
Reid Kleckner7f90dd22016-05-02 23:22:18 +00001339 unsigned UniqueID = MCContext::GenericSectionID;
1340 if (EmitUniquedSection)
1341 UniqueID = NextUniqueID++;
1342
David Majnemerc8a11692014-06-27 18:19:56 +00001343 if (!ComdatGV->hasPrivateLinkage()) {
Tim Northoverbc347f22016-11-22 16:17:20 +00001344 MCSymbol *Sym = TM.getSymbol(ComdatGV);
David Majnemerc8a11692014-06-27 18:19:56 +00001345 StringRef COMDATSymName = Sym->getName();
Reid Klecknerb9c0a172018-06-21 23:06:33 +00001346
Reid Klecknera71fc882018-11-21 22:01:10 +00001347 // Append "$symbol" to the section name *before* IR-level mangling is
1348 // applied when targetting mingw. This is what GCC does, and the ld.bfd
Reid Klecknerb9c0a172018-06-21 23:06:33 +00001349 // COFF linker will not properly handle comdats otherwise.
1350 if (getTargetTriple().isWindowsGNUEnvironment())
Reid Klecknera71fc882018-11-21 22:01:10 +00001351 raw_svector_ostream(Name) << '$' << ComdatGV->getName();
Reid Klecknerb9c0a172018-06-21 23:06:33 +00001352
David Majnemerc8a11692014-06-27 18:19:56 +00001353 return getContext().getCOFFSection(Name, Characteristics, Kind,
Reid Kleckner7f90dd22016-05-02 23:22:18 +00001354 COMDATSymName, Selection, UniqueID);
David Majnemer7605cdd2015-03-17 23:54:51 +00001355 } else {
1356 SmallString<256> TmpData;
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +00001357 getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
David Majnemer7605cdd2015-03-17 23:54:51 +00001358 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
Reid Kleckner7f90dd22016-05-02 23:22:18 +00001359 Selection, UniqueID);
David Majnemerc8a11692014-06-27 18:19:56 +00001360 }
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001361 }
1362
1363 if (Kind.isText())
David Majnemer6aa93152013-08-13 01:23:53 +00001364 return TextSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001365
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00001366 if (Kind.isThreadLocal())
David Majnemer6aa93152013-08-13 01:23:53 +00001367 return TLSDataSection;
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00001368
David Majnemerd80fc692014-09-22 20:39:23 +00001369 if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
David Majnemer9706d432013-08-08 01:50:52 +00001370 return ReadOnlySection;
1371
David Majnemere4d89ec2014-04-08 22:33:40 +00001372 // Note: we claim that common symbols are put in BSSSection, but they are
1373 // really emitted with the magic .comm directive, which creates a symbol table
1374 // entry but not a section.
1375 if (Kind.isBSS() || Kind.isCommon())
David Majnemer6aa93152013-08-13 01:23:53 +00001376 return BSSSection;
1377
1378 return DataSection;
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +00001379}
1380
David Majnemer7605cdd2015-03-17 23:54:51 +00001381void TargetLoweringObjectFileCOFF::getNameWithPrefix(
Eric Christopher88a23b62016-09-16 07:33:15 +00001382 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
Peter Collingbourne05f66f32015-11-03 23:40:03 +00001383 const TargetMachine &TM) const {
1384 bool CannotUsePrivateLabel = false;
David Majnemer7605cdd2015-03-17 23:54:51 +00001385 if (GV->hasPrivateLinkage() &&
1386 ((isa<Function>(GV) && TM.getFunctionSections()) ||
1387 (isa<GlobalVariable>(GV) && TM.getDataSections())))
1388 CannotUsePrivateLabel = true;
1389
Eric Christopher88a23b62016-09-16 07:33:15 +00001390 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
David Majnemer7605cdd2015-03-17 23:54:51 +00001391}
1392
Rafael Espindola75219642015-05-21 19:20:38 +00001393MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
Eric Christopher88a23b62016-09-16 07:33:15 +00001394 const Function &F, const TargetMachine &TM) const {
Rafael Espindola0c785832015-03-11 19:58:37 +00001395 // If the function can be removed, produce a unique section so that
1396 // the table doesn't prevent the removal.
1397 const Comdat *C = F.getComdat();
1398 bool EmitUniqueSection = TM.getFunctionSections() || C;
1399 if (!EmitUniqueSection)
1400 return ReadOnlySection;
1401
1402 // FIXME: we should produce a symbol for F instead.
1403 if (F.hasPrivateLinkage())
1404 return ReadOnlySection;
1405
Tim Northoverbc347f22016-11-22 16:17:20 +00001406 MCSymbol *Sym = TM.getSymbol(&F);
Rafael Espindola0c785832015-03-11 19:58:37 +00001407 StringRef COMDATSymName = Sym->getName();
1408
1409 SectionKind Kind = SectionKind::getReadOnly();
Reid Kleckner845b4392018-06-21 20:27:38 +00001410 StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind);
Renato Golin4a905812016-06-27 14:42:20 +00001411 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
Rafael Espindola0c785832015-03-11 19:58:37 +00001412 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Reid Kleckner7f90dd22016-05-02 23:22:18 +00001413 unsigned UniqueID = NextUniqueID++;
Rafael Espindola0c785832015-03-11 19:58:37 +00001414
Reid Kleckner845b4392018-06-21 20:27:38 +00001415 return getContext().getCOFFSection(
1416 SecName, Characteristics, Kind, COMDATSymName,
1417 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
Rafael Espindola0c785832015-03-11 19:58:37 +00001418}
1419
Eric Christopher26c5a962018-04-20 19:07:57 +00001420void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
1421 Module &M) const {
Peter Collingbourne9283a092017-06-12 20:10:48 +00001422 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
Saleem Abdulrasool5ac1e612016-04-30 18:15:34 +00001423 // Emit the linker options to the linker .drectve section. According to the
1424 // spec, this section is a space-separated string containing flags for
1425 // linker.
1426 MCSection *Sec = getDrectveSection();
1427 Streamer.SwitchSection(Sec);
1428 for (const auto &Option : LinkerOptions->operands()) {
1429 for (const auto &Piece : cast<MDNode>(Option)->operands()) {
1430 // Lead with a space for consistency with our dllexport implementation.
1431 std::string Directive(" ");
1432 Directive.append(cast<MDString>(Piece)->getString());
1433 Streamer.EmitBytes(Directive);
1434 }
Reid Kleckner4b558152013-04-25 19:34:41 +00001435 }
1436 }
Saleem Abdulrasool5f204902017-06-05 21:26:39 +00001437
1438 unsigned Version = 0;
1439 unsigned Flags = 0;
1440 StringRef Section;
1441
Peter Collingbourne9283a092017-06-12 20:10:48 +00001442 GetObjCImageInfo(M, Version, Flags, Section);
Saleem Abdulrasool5f204902017-06-05 21:26:39 +00001443 if (Section.empty())
1444 return;
1445
1446 auto &C = getContext();
1447 auto *S = C.getCOFFSection(
1448 Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
1449 SectionKind::getReadOnly());
1450 Streamer.SwitchSection(S);
1451 Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1452 Streamer.EmitIntValue(Version, 4);
1453 Streamer.EmitIntValue(Flags, 4);
1454 Streamer.AddBlankLine();
Reid Kleckner4b558152013-04-25 19:34:41 +00001455}
Reid Kleckner1ce30882014-05-16 20:39:27 +00001456
Rafael Espindola1a569482016-08-29 12:33:42 +00001457void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1458 const TargetMachine &TM) {
1459 TargetLoweringObjectFile::Initialize(Ctx, TM);
1460 const Triple &T = TM.getTargetTriple();
1461 if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1462 StaticCtorSection =
1463 Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1464 COFF::IMAGE_SCN_MEM_READ,
1465 SectionKind::getReadOnly());
1466 StaticDtorSection =
1467 Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1468 COFF::IMAGE_SCN_MEM_READ,
1469 SectionKind::getReadOnly());
1470 } else {
1471 StaticCtorSection = Ctx.getCOFFSection(
1472 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1473 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1474 SectionKind::getData());
1475 StaticDtorSection = Ctx.getCOFFSection(
1476 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1477 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1478 SectionKind::getData());
1479 }
1480}
1481
Martin Storsjo9ab4c272017-11-28 08:07:18 +00001482static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
1483 const Triple &T, bool IsCtor,
1484 unsigned Priority,
1485 const MCSymbol *KeySym,
1486 MCSectionCOFF *Default) {
Reid Kleckner443a8ce2018-09-07 23:07:55 +00001487 if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1488 // If the priority is the default, use .CRT$XCU, possibly associative.
1489 if (Priority == 65535)
1490 return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
1491
1492 // Otherwise, we need to compute a new section name. Low priorities should
1493 // run earlier. The linker will sort sections ASCII-betically, and we need a
1494 // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
1495 // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
1496 // low priorities need to sort before 'L', since the CRT uses that
1497 // internally, so we use ".CRT$XCA00001" for them.
1498 SmallString<24> Name;
1499 raw_svector_ostream OS(Name);
1500 OS << ".CRT$XC" << (Priority < 200 ? 'A' : 'T') << format("%05u", Priority);
1501 MCSectionCOFF *Sec = Ctx.getCOFFSection(
1502 Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
1503 SectionKind::getReadOnly());
1504 return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0);
1505 }
Martin Storsjo9ab4c272017-11-28 08:07:18 +00001506
1507 std::string Name = IsCtor ? ".ctors" : ".dtors";
1508 if (Priority != 65535)
1509 raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
1510
1511 return Ctx.getAssociativeCOFFSection(
1512 Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1513 COFF::IMAGE_SCN_MEM_READ |
1514 COFF::IMAGE_SCN_MEM_WRITE,
1515 SectionKind::getData()),
1516 KeySym, 0);
1517}
1518
Rafael Espindola75219642015-05-21 19:20:38 +00001519MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
Rafael Espindola013321a2014-06-06 19:26:12 +00001520 unsigned Priority, const MCSymbol *KeySym) const {
Martin Storsjo9ab4c272017-11-28 08:07:18 +00001521 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true,
1522 Priority, KeySym,
1523 cast<MCSectionCOFF>(StaticCtorSection));
Reid Kleckner1ce30882014-05-16 20:39:27 +00001524}
1525
Rafael Espindola75219642015-05-21 19:20:38 +00001526MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
Rafael Espindola013321a2014-06-06 19:26:12 +00001527 unsigned Priority, const MCSymbol *KeySym) const {
Martin Storsjo9ab4c272017-11-28 08:07:18 +00001528 return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false,
1529 Priority, KeySym,
1530 cast<MCSectionCOFF>(StaticDtorSection));
Reid Kleckner1ce30882014-05-16 20:39:27 +00001531}
Peter Collingbournea6367d92015-06-29 22:04:09 +00001532
1533void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
Eric Christopher88a23b62016-09-16 07:33:15 +00001534 raw_ostream &OS, const GlobalValue *GV) const {
Bob Haarmanea591502017-02-02 23:00:49 +00001535 emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler());
Peter Collingbournea6367d92015-06-29 22:04:09 +00001536}
Dan Gohmand660a5d2017-02-22 01:23:18 +00001537
Saleem Abdulrasool860652c2018-01-20 00:28:02 +00001538void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed(
1539 raw_ostream &OS, const GlobalValue *GV) const {
1540 emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler());
1541}
1542
Reid Klecknerff373df2018-06-12 18:56:05 +00001543const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference(
1544 const GlobalValue *LHS, const GlobalValue *RHS,
1545 const TargetMachine &TM) const {
1546 const Triple &T = TM.getTargetTriple();
1547 if (!T.isKnownWindowsMSVCEnvironment() &&
1548 !T.isWindowsItaniumEnvironment() &&
1549 !T.isWindowsCoreCLREnvironment())
1550 return nullptr;
1551
1552 // Our symbols should exist in address space zero, cowardly no-op if
1553 // otherwise.
1554 if (LHS->getType()->getPointerAddressSpace() != 0 ||
1555 RHS->getType()->getPointerAddressSpace() != 0)
1556 return nullptr;
1557
1558 // Both ptrtoint instructions must wrap global objects:
1559 // - Only global variables are eligible for image relative relocations.
1560 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
1561 // We expect __ImageBase to be a global variable without a section, externally
1562 // defined.
1563 //
1564 // It should look something like this: @__ImageBase = external constant i8
1565 if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
1566 LHS->isThreadLocal() || RHS->isThreadLocal() ||
1567 RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
1568 cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
1569 return nullptr;
1570
1571 return MCSymbolRefExpr::create(TM.getSymbol(LHS),
1572 MCSymbolRefExpr::VK_COFF_IMGREL32,
1573 getContext());
1574}
1575
1576static std::string APIntToHexString(const APInt &AI) {
1577 unsigned Width = (AI.getBitWidth() / 8) * 2;
1578 std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
1579 unsigned Size = HexString.size();
1580 assert(Width >= Size && "hex string is too large!");
1581 HexString.insert(HexString.begin(), Width - Size, '0');
1582
1583 return HexString;
1584}
1585
1586static std::string scalarConstantToHexString(const Constant *C) {
1587 Type *Ty = C->getType();
1588 if (isa<UndefValue>(C)) {
1589 return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
1590 } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
1591 return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
1592 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
1593 return APIntToHexString(CI->getValue());
1594 } else {
1595 unsigned NumElements;
1596 if (isa<VectorType>(Ty))
1597 NumElements = Ty->getVectorNumElements();
1598 else
1599 NumElements = Ty->getArrayNumElements();
1600 std::string HexString;
1601 for (int I = NumElements - 1, E = -1; I != E; --I)
1602 HexString += scalarConstantToHexString(C->getAggregateElement(I));
1603 return HexString;
1604 }
1605}
1606
1607MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant(
1608 const DataLayout &DL, SectionKind Kind, const Constant *C,
1609 unsigned &Align) const {
Martin Storsjo078cc9c2018-07-26 10:48:20 +00001610 if (Kind.isMergeableConst() && C &&
1611 getContext().getAsmInfo()->hasCOFFComdatConstants()) {
1612 // This creates comdat sections with the given symbol name, but unless
1613 // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol
1614 // will be created with a null storage class, which makes GNU binutils
1615 // error out.
Reid Klecknerff373df2018-06-12 18:56:05 +00001616 const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1617 COFF::IMAGE_SCN_MEM_READ |
1618 COFF::IMAGE_SCN_LNK_COMDAT;
1619 std::string COMDATSymName;
1620 if (Kind.isMergeableConst4()) {
1621 if (Align <= 4) {
1622 COMDATSymName = "__real@" + scalarConstantToHexString(C);
1623 Align = 4;
1624 }
1625 } else if (Kind.isMergeableConst8()) {
1626 if (Align <= 8) {
1627 COMDATSymName = "__real@" + scalarConstantToHexString(C);
1628 Align = 8;
1629 }
1630 } else if (Kind.isMergeableConst16()) {
1631 // FIXME: These may not be appropriate for non-x86 architectures.
1632 if (Align <= 16) {
1633 COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
1634 Align = 16;
1635 }
1636 } else if (Kind.isMergeableConst32()) {
1637 if (Align <= 32) {
1638 COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
1639 Align = 32;
1640 }
1641 }
1642
1643 if (!COMDATSymName.empty())
1644 return getContext().getCOFFSection(".rdata", Characteristics, Kind,
1645 COMDATSymName,
1646 COFF::IMAGE_COMDAT_SELECT_ANY);
1647 }
1648
1649 return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
1650}
1651
1652
Dan Gohmand660a5d2017-02-22 01:23:18 +00001653//===----------------------------------------------------------------------===//
1654// Wasm
1655//===----------------------------------------------------------------------===//
1656
Sam Clegge92250a2018-01-09 23:43:14 +00001657static const Comdat *getWasmComdat(const GlobalValue *GV) {
Dan Gohman53ff96a2017-02-24 23:18:00 +00001658 const Comdat *C = GV->getComdat();
1659 if (!C)
Sam Clegge92250a2018-01-09 23:43:14 +00001660 return nullptr;
Dan Gohman53ff96a2017-02-24 23:18:00 +00001661
Sam Clegge92250a2018-01-09 23:43:14 +00001662 if (C->getSelectionKind() != Comdat::Any)
1663 report_fatal_error("WebAssembly COMDATs only support "
1664 "SelectionKind::Any, '" + C->getName() + "' cannot be "
1665 "lowered.");
1666
1667 return C;
Dan Gohman53ff96a2017-02-24 23:18:00 +00001668}
1669
Sam Clegg93101232017-12-07 02:55:51 +00001670static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) {
1671 // If we're told we have function data, then use that.
1672 if (K.isText())
1673 return SectionKind::getText();
1674
1675 // Otherwise, ignore whatever section type the generic impl detected and use
1676 // a plain data section.
1677 return SectionKind::getData();
1678}
1679
Dan Gohmand660a5d2017-02-22 01:23:18 +00001680MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
1681 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Sam Clegg10cde002018-06-14 18:48:19 +00001682 // We don't support explict section names for functions in the wasm object
1683 // format. Each function has to be in its own unique section.
1684 if (isa<Function>(GO)) {
1685 return SelectSectionForGlobal(GO, Kind, TM);
1686 }
1687
Sam Clegga9217f62017-09-15 20:54:59 +00001688 StringRef Name = GO->getSection();
Sam Clegge92250a2018-01-09 23:43:14 +00001689
Sam Clegg93101232017-12-07 02:55:51 +00001690 Kind = getWasmKindForNamedSection(Name, Kind);
Sam Clegge92250a2018-01-09 23:43:14 +00001691
1692 StringRef Group = "";
1693 if (const Comdat *C = getWasmComdat(GO)) {
1694 Group = C->getName();
1695 }
1696
1697 return getContext().getWasmSection(Name, Kind, Group,
1698 MCContext::GenericSectionID);
Dan Gohmand660a5d2017-02-22 01:23:18 +00001699}
1700
Sam Cleggc6a72152017-09-12 18:31:24 +00001701static MCSectionWasm *selectWasmSectionForGlobal(
1702 MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
1703 const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) {
Dan Gohman53ff96a2017-02-24 23:18:00 +00001704 StringRef Group = "";
Sam Clegge92250a2018-01-09 23:43:14 +00001705 if (const Comdat *C = getWasmComdat(GO)) {
1706 Group = C->getName();
1707 }
Dan Gohman53ff96a2017-02-24 23:18:00 +00001708
1709 bool UniqueSectionNames = TM.getUniqueSectionNames();
1710 SmallString<128> Name = getSectionPrefixForGlobal(Kind);
1711
1712 if (const auto *F = dyn_cast<Function>(GO)) {
1713 const auto &OptionalPrefix = F->getSectionPrefix();
1714 if (OptionalPrefix)
1715 Name += *OptionalPrefix;
1716 }
1717
1718 if (EmitUniqueSection && UniqueSectionNames) {
1719 Name.push_back('.');
1720 TM.getNameWithPrefix(Name, GO, Mang, true);
1721 }
1722 unsigned UniqueID = MCContext::GenericSectionID;
1723 if (EmitUniqueSection && !UniqueSectionNames) {
1724 UniqueID = *NextUniqueID;
1725 (*NextUniqueID)++;
1726 }
Sam Clegg2b6b6ac2017-10-20 21:28:38 +00001727 return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
Dan Gohman53ff96a2017-02-24 23:18:00 +00001728}
1729
Dan Gohmand660a5d2017-02-22 01:23:18 +00001730MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(
1731 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Dan Gohman53ff96a2017-02-24 23:18:00 +00001732
1733 if (Kind.isCommon())
1734 report_fatal_error("mergable sections not supported yet on wasm");
1735
1736 // If we have -ffunction-section or -fdata-section then we should emit the
1737 // global value to a uniqued section specifically for it.
1738 bool EmitUniqueSection = false;
Dan Gohmand660a5d2017-02-22 01:23:18 +00001739 if (Kind.isText())
Dan Gohman53ff96a2017-02-24 23:18:00 +00001740 EmitUniqueSection = TM.getFunctionSections();
1741 else
1742 EmitUniqueSection = TM.getDataSections();
1743 EmitUniqueSection |= GO->hasComdat();
1744
1745 return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
Sam Cleggc6a72152017-09-12 18:31:24 +00001746 EmitUniqueSection, &NextUniqueID);
Dan Gohmand660a5d2017-02-22 01:23:18 +00001747}
1748
1749bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
1750 bool UsesLabelDifference, const Function &F) const {
1751 // We can always create relative relocations, so use another section
1752 // that can be marked non-executable.
1753 return false;
1754}
1755
1756const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference(
1757 const GlobalValue *LHS, const GlobalValue *RHS,
1758 const TargetMachine &TM) const {
1759 // We may only use a PLT-relative relocation to refer to unnamed_addr
1760 // functions.
1761 if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
1762 return nullptr;
1763
1764 // Basic sanity checks.
1765 if (LHS->getType()->getPointerAddressSpace() != 0 ||
1766 RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
1767 RHS->isThreadLocal())
1768 return nullptr;
1769
1770 return MCBinaryExpr::createSub(
1771 MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None,
1772 getContext()),
1773 MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
1774}
1775
Sam Clegg7fa35702017-10-03 11:20:28 +00001776void TargetLoweringObjectFileWasm::InitializeWasm() {
1777 StaticCtorSection =
Sam Clegg2b6b6ac2017-10-20 21:28:38 +00001778 getContext().getWasmSection(".init_array", SectionKind::getData());
Heejin Ahn397841e2018-10-25 23:55:10 +00001779
1780 // We don't use PersonalityEncoding and LSDAEncoding because we don't emit
1781 // .cfi directives. We use TTypeEncoding to encode typeinfo global variables.
1782 TTypeEncoding = dwarf::DW_EH_PE_absptr;
Sam Clegg53341802017-12-15 00:17:10 +00001783}
1784
1785MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection(
1786 unsigned Priority, const MCSymbol *KeySym) const {
1787 return Priority == UINT16_MAX ?
1788 StaticCtorSection :
1789 getContext().getWasmSection(".init_array." + utostr(Priority),
1790 SectionKind::getData());
1791}
1792
1793MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection(
1794 unsigned Priority, const MCSymbol *KeySym) const {
1795 llvm_unreachable("@llvm.global_dtors should have been lowered already");
1796 return nullptr;
Dan Gohmand660a5d2017-02-22 01:23:18 +00001797}