Allow FDE references outside the +/-2GB range supported by PC relative
[oota-llvm.git] / lib / MC / MCObjectFileInfo.cpp
1 //===-- MObjectFileInfo.cpp - Object File Information ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCObjectFileInfo.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCSection.h"
16 #include "llvm/MC/MCSectionCOFF.h"
17 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 using namespace llvm;
20
21 static bool useCompactUnwind(const Triple &T) {
22   // Only on darwin.
23   if (!T.isOSDarwin())
24     return false;
25
26   // aarch64 always has it.
27   if (T.getArch() == Triple::aarch64)
28     return true;
29
30   // Use it on newer version of OS X.
31   if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6))
32     return true;
33
34   // And the iOS simulator.
35   if (T.isiOS() &&
36       (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86))
37     return true;
38
39   return false;
40 }
41
42 void MCObjectFileInfo::InitMachOMCObjectFileInfo(Triple T) {
43   // MachO
44   SupportsWeakOmittedEHFrame = false;
45
46   if (T.isOSDarwin() && T.getArch() == Triple::aarch64)
47     SupportsCompactUnwindWithoutEHFrame = true;
48
49   PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel
50     | dwarf::DW_EH_PE_sdata4;
51   LSDAEncoding = FDECFIEncoding = dwarf::DW_EH_PE_pcrel;
52   TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
53     dwarf::DW_EH_PE_sdata4;
54
55   // .comm doesn't support alignment before Leopard.
56   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
57     CommDirectiveSupportsAlignment = false;
58
59   TextSection // .text
60     = Ctx->getMachOSection("__TEXT", "__text",
61                            MachO::S_ATTR_PURE_INSTRUCTIONS,
62                            SectionKind::getText());
63   DataSection // .data
64     = Ctx->getMachOSection("__DATA", "__data", 0,
65                            SectionKind::getDataRel());
66
67   // BSSSection might not be expected initialized on msvc.
68   BSSSection = nullptr;
69
70   TLSDataSection // .tdata
71     = Ctx->getMachOSection("__DATA", "__thread_data",
72                            MachO::S_THREAD_LOCAL_REGULAR,
73                            SectionKind::getDataRel());
74   TLSBSSSection // .tbss
75     = Ctx->getMachOSection("__DATA", "__thread_bss",
76                            MachO::S_THREAD_LOCAL_ZEROFILL,
77                            SectionKind::getThreadBSS());
78
79   // TODO: Verify datarel below.
80   TLSTLVSection // .tlv
81     = Ctx->getMachOSection("__DATA", "__thread_vars",
82                            MachO::S_THREAD_LOCAL_VARIABLES,
83                            SectionKind::getDataRel());
84
85   TLSThreadInitSection
86     = Ctx->getMachOSection("__DATA", "__thread_init",
87                           MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
88                           SectionKind::getDataRel());
89
90   CStringSection // .cstring
91     = Ctx->getMachOSection("__TEXT", "__cstring",
92                            MachO::S_CSTRING_LITERALS,
93                            SectionKind::getMergeable1ByteCString());
94   UStringSection
95     = Ctx->getMachOSection("__TEXT","__ustring", 0,
96                            SectionKind::getMergeable2ByteCString());
97   FourByteConstantSection // .literal4
98     = Ctx->getMachOSection("__TEXT", "__literal4",
99                            MachO::S_4BYTE_LITERALS,
100                            SectionKind::getMergeableConst4());
101   EightByteConstantSection // .literal8
102     = Ctx->getMachOSection("__TEXT", "__literal8",
103                            MachO::S_8BYTE_LITERALS,
104                            SectionKind::getMergeableConst8());
105
106   SixteenByteConstantSection // .literal16
107       = Ctx->getMachOSection("__TEXT", "__literal16",
108                              MachO::S_16BYTE_LITERALS,
109                              SectionKind::getMergeableConst16());
110
111   ReadOnlySection  // .const
112     = Ctx->getMachOSection("__TEXT", "__const", 0,
113                            SectionKind::getReadOnly());
114
115   TextCoalSection
116     = Ctx->getMachOSection("__TEXT", "__textcoal_nt",
117                            MachO::S_COALESCED |
118                            MachO::S_ATTR_PURE_INSTRUCTIONS,
119                            SectionKind::getText());
120   ConstTextCoalSection
121     = Ctx->getMachOSection("__TEXT", "__const_coal",
122                            MachO::S_COALESCED,
123                            SectionKind::getReadOnly());
124   ConstDataSection  // .const_data
125     = Ctx->getMachOSection("__DATA", "__const", 0,
126                            SectionKind::getReadOnlyWithRel());
127   DataCoalSection
128     = Ctx->getMachOSection("__DATA","__datacoal_nt",
129                            MachO::S_COALESCED,
130                            SectionKind::getDataRel());
131   DataCommonSection
132     = Ctx->getMachOSection("__DATA","__common",
133                            MachO::S_ZEROFILL,
134                            SectionKind::getBSS());
135   DataBSSSection
136     = Ctx->getMachOSection("__DATA","__bss", MachO::S_ZEROFILL,
137                            SectionKind::getBSS());
138
139
140   LazySymbolPointerSection
141     = Ctx->getMachOSection("__DATA", "__la_symbol_ptr",
142                            MachO::S_LAZY_SYMBOL_POINTERS,
143                            SectionKind::getMetadata());
144   NonLazySymbolPointerSection
145     = Ctx->getMachOSection("__DATA", "__nl_symbol_ptr",
146                            MachO::S_NON_LAZY_SYMBOL_POINTERS,
147                            SectionKind::getMetadata());
148
149   if (RelocM == Reloc::Static) {
150     StaticCtorSection
151       = Ctx->getMachOSection("__TEXT", "__constructor", 0,
152                              SectionKind::getDataRel());
153     StaticDtorSection
154       = Ctx->getMachOSection("__TEXT", "__destructor", 0,
155                              SectionKind::getDataRel());
156   } else {
157     StaticCtorSection
158       = Ctx->getMachOSection("__DATA", "__mod_init_func",
159                              MachO::S_MOD_INIT_FUNC_POINTERS,
160                              SectionKind::getDataRel());
161     StaticDtorSection
162       = Ctx->getMachOSection("__DATA", "__mod_term_func",
163                              MachO::S_MOD_TERM_FUNC_POINTERS,
164                              SectionKind::getDataRel());
165   }
166
167   // Exception Handling.
168   LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0,
169                                      SectionKind::getReadOnlyWithRel());
170
171   COFFDebugSymbolsSection = nullptr;
172
173   if (useCompactUnwind(T)) {
174     CompactUnwindSection =
175         Ctx->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG,
176                              SectionKind::getReadOnly());
177
178     if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86)
179       CompactUnwindDwarfEHFrameOnly = 0x04000000;
180     else if (T.getArch() == Triple::aarch64)
181       CompactUnwindDwarfEHFrameOnly = 0x03000000;
182   }
183
184   // Debug Information.
185   DwarfAccelNamesSection =
186     Ctx->getMachOSection("__DWARF", "__apple_names",
187                          MachO::S_ATTR_DEBUG,
188                          SectionKind::getMetadata());
189   DwarfAccelObjCSection =
190     Ctx->getMachOSection("__DWARF", "__apple_objc",
191                          MachO::S_ATTR_DEBUG,
192                          SectionKind::getMetadata());
193   // 16 character section limit...
194   DwarfAccelNamespaceSection =
195     Ctx->getMachOSection("__DWARF", "__apple_namespac",
196                          MachO::S_ATTR_DEBUG,
197                          SectionKind::getMetadata());
198   DwarfAccelTypesSection =
199     Ctx->getMachOSection("__DWARF", "__apple_types",
200                          MachO::S_ATTR_DEBUG,
201                          SectionKind::getMetadata());
202
203   DwarfAbbrevSection =
204     Ctx->getMachOSection("__DWARF", "__debug_abbrev",
205                          MachO::S_ATTR_DEBUG,
206                          SectionKind::getMetadata());
207   DwarfInfoSection =
208     Ctx->getMachOSection("__DWARF", "__debug_info",
209                          MachO::S_ATTR_DEBUG,
210                          SectionKind::getMetadata());
211   DwarfLineSection =
212     Ctx->getMachOSection("__DWARF", "__debug_line",
213                          MachO::S_ATTR_DEBUG,
214                          SectionKind::getMetadata());
215   DwarfFrameSection =
216     Ctx->getMachOSection("__DWARF", "__debug_frame",
217                          MachO::S_ATTR_DEBUG,
218                          SectionKind::getMetadata());
219   DwarfPubNamesSection =
220     Ctx->getMachOSection("__DWARF", "__debug_pubnames",
221                          MachO::S_ATTR_DEBUG,
222                          SectionKind::getMetadata());
223   DwarfPubTypesSection =
224     Ctx->getMachOSection("__DWARF", "__debug_pubtypes",
225                          MachO::S_ATTR_DEBUG,
226                          SectionKind::getMetadata());
227   DwarfGnuPubNamesSection =
228     Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn",
229                          MachO::S_ATTR_DEBUG,
230                          SectionKind::getMetadata());
231   DwarfGnuPubTypesSection =
232     Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt",
233                          MachO::S_ATTR_DEBUG,
234                          SectionKind::getMetadata());
235   DwarfStrSection =
236     Ctx->getMachOSection("__DWARF", "__debug_str",
237                          MachO::S_ATTR_DEBUG,
238                          SectionKind::getMetadata());
239   DwarfLocSection =
240     Ctx->getMachOSection("__DWARF", "__debug_loc",
241                          MachO::S_ATTR_DEBUG,
242                          SectionKind::getMetadata());
243   DwarfARangesSection =
244     Ctx->getMachOSection("__DWARF", "__debug_aranges",
245                          MachO::S_ATTR_DEBUG,
246                          SectionKind::getMetadata());
247   DwarfRangesSection =
248     Ctx->getMachOSection("__DWARF", "__debug_ranges",
249                          MachO::S_ATTR_DEBUG,
250                          SectionKind::getMetadata());
251   DwarfMacroInfoSection =
252     Ctx->getMachOSection("__DWARF", "__debug_macinfo",
253                          MachO::S_ATTR_DEBUG,
254                          SectionKind::getMetadata());
255   DwarfDebugInlineSection =
256     Ctx->getMachOSection("__DWARF", "__debug_inlined",
257                          MachO::S_ATTR_DEBUG,
258                          SectionKind::getMetadata());
259   StackMapSection =
260     Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps", 0,
261                          SectionKind::getMetadata());
262
263   TLSExtraDataSection = TLSTLVSection;
264 }
265
266 void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) {
267   switch (T.getArch()) {
268   case Triple::mips:
269   case Triple::mipsel:
270     FDECFIEncoding = dwarf::DW_EH_PE_sdata4;
271     break;
272   case Triple::mips64:
273   case Triple::mips64el:
274     FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
275     break;
276   case Triple::x86_64:
277     if (RelocM == Reloc::PIC_) {
278       FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
279         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
280          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
281     } else {
282       FDECFIEncoding =
283         (CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
284         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
285     }
286     break;
287   default:
288     FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
289     break;
290   }
291
292   switch (T.getArch()) {
293   case Triple::arm:
294   case Triple::armeb:
295   case Triple::thumb:
296   case Triple::thumbeb:
297     if (Ctx->getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
298       break;
299     // Fallthrough if not using EHABI
300   case Triple::ppc:
301   case Triple::x86:
302     PersonalityEncoding = (RelocM == Reloc::PIC_)
303      ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
304      : dwarf::DW_EH_PE_absptr;
305     LSDAEncoding = (RelocM == Reloc::PIC_)
306       ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
307       : dwarf::DW_EH_PE_absptr;
308     TTypeEncoding = (RelocM == Reloc::PIC_)
309      ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
310      : dwarf::DW_EH_PE_absptr;
311     break;
312   case Triple::x86_64:
313     if (RelocM == Reloc::PIC_) {
314       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
315         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
316          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
317       LSDAEncoding = dwarf::DW_EH_PE_pcrel |
318         (CMModel == CodeModel::Small
319          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
320       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
321         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
322          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
323     } else {
324       PersonalityEncoding =
325         (CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
326         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
327       LSDAEncoding = (CMModel == CodeModel::Small)
328         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
329       TTypeEncoding = (CMModel == CodeModel::Small)
330         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
331     }
332     break;
333   case Triple::aarch64:
334   case Triple::aarch64_be:
335     // The small model guarantees static code/data size < 4GB, but not where it
336     // will be in memory. Most of these could end up >2GB away so even a signed
337     // pc-relative 32-bit address is insufficient, theoretically.
338     if (RelocM == Reloc::PIC_) {
339       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
340         dwarf::DW_EH_PE_sdata8;
341       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
342       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
343         dwarf::DW_EH_PE_sdata8;
344     } else {
345       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
346       LSDAEncoding = dwarf::DW_EH_PE_absptr;
347       TTypeEncoding = dwarf::DW_EH_PE_absptr;
348     }
349     break;
350   case Triple::mips:
351   case Triple::mipsel:
352   case Triple::mips64:
353   case Triple::mips64el:
354     // MIPS uses indirect pointer to refer personality functions, so that the
355     // eh_frame section can be read-only.  DW.ref.personality will be generated
356     // for relocation.
357     PersonalityEncoding = dwarf::DW_EH_PE_indirect;
358     break;
359   case Triple::ppc64:
360   case Triple::ppc64le:
361     PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
362       dwarf::DW_EH_PE_udata8;
363     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
364     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
365       dwarf::DW_EH_PE_udata8;
366     break;
367   case Triple::sparc:
368     if (RelocM == Reloc::PIC_) {
369       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
370       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
371         dwarf::DW_EH_PE_sdata4;
372       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
373         dwarf::DW_EH_PE_sdata4;
374     } else {
375       LSDAEncoding = dwarf::DW_EH_PE_absptr;
376       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
377       TTypeEncoding = dwarf::DW_EH_PE_absptr;
378     }
379     break;
380   case Triple::sparcv9:
381     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
382     if (RelocM == Reloc::PIC_) {
383       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
384         dwarf::DW_EH_PE_sdata4;
385       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
386         dwarf::DW_EH_PE_sdata4;
387     } else {
388       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
389       TTypeEncoding = dwarf::DW_EH_PE_absptr;
390     }
391     break;
392   case Triple::systemz:
393     // All currently-defined code models guarantee that 4-byte PC-relative
394     // values will be in range.
395     if (RelocM == Reloc::PIC_) {
396       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
397         dwarf::DW_EH_PE_sdata4;
398       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
399       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
400         dwarf::DW_EH_PE_sdata4;
401     } else {
402       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
403       LSDAEncoding = dwarf::DW_EH_PE_absptr;
404       TTypeEncoding = dwarf::DW_EH_PE_absptr;
405     }
406     break;
407   default:
408     break;
409   }
410
411   // Solaris requires different flags for .eh_frame to seemingly every other
412   // platform.
413   EHSectionType = ELF::SHT_PROGBITS;
414   EHSectionFlags = ELF::SHF_ALLOC;
415   if (T.getOS() == Triple::Solaris) {
416     if (T.getArch() == Triple::x86_64)
417       EHSectionType = ELF::SHT_X86_64_UNWIND;
418     else
419       EHSectionFlags |= ELF::SHF_WRITE;
420   }
421
422
423   // ELF
424   BSSSection =
425     Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
426                        ELF::SHF_WRITE | ELF::SHF_ALLOC,
427                        SectionKind::getBSS());
428
429   TextSection =
430     Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
431                        ELF::SHF_EXECINSTR |
432                        ELF::SHF_ALLOC,
433                        SectionKind::getText());
434
435   DataSection =
436     Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
437                        ELF::SHF_WRITE |ELF::SHF_ALLOC,
438                        SectionKind::getDataRel());
439
440   ReadOnlySection =
441     Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS,
442                        ELF::SHF_ALLOC,
443                        SectionKind::getReadOnly());
444
445   TLSDataSection =
446     Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
447                        ELF::SHF_ALLOC | ELF::SHF_TLS |
448                        ELF::SHF_WRITE,
449                        SectionKind::getThreadData());
450
451   TLSBSSSection =
452     Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
453                        ELF::SHF_ALLOC | ELF::SHF_TLS |
454                        ELF::SHF_WRITE,
455                        SectionKind::getThreadBSS());
456
457   DataRelSection =
458     Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS,
459                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
460                        SectionKind::getDataRel());
461
462   DataRelLocalSection =
463     Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
464                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
465                        SectionKind::getDataRelLocal());
466
467   DataRelROSection =
468     Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
469                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
470                        SectionKind::getReadOnlyWithRel());
471
472   DataRelROLocalSection =
473     Ctx->getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
474                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
475                        SectionKind::getReadOnlyWithRelLocal());
476
477   MergeableConst4Section =
478     Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
479                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
480                        SectionKind::getMergeableConst4());
481
482   MergeableConst8Section =
483     Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
484                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
485                        SectionKind::getMergeableConst8());
486
487   MergeableConst16Section =
488     Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
489                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
490                        SectionKind::getMergeableConst16());
491
492   StaticCtorSection =
493     Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS,
494                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
495                        SectionKind::getDataRel());
496
497   StaticDtorSection =
498     Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS,
499                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
500                        SectionKind::getDataRel());
501
502   // Exception Handling Sections.
503
504   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
505   // it contains relocatable pointers.  In PIC mode, this is probably a big
506   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
507   // adjusted or this should be a data section.
508   LSDASection =
509     Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
510                        ELF::SHF_ALLOC,
511                        SectionKind::getReadOnly());
512
513   COFFDebugSymbolsSection = nullptr;
514
515   // Debug Info Sections.
516   DwarfAbbrevSection =
517     Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
518                        SectionKind::getMetadata());
519   DwarfInfoSection =
520     Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
521                        SectionKind::getMetadata());
522   DwarfLineSection =
523     Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
524                        SectionKind::getMetadata());
525   DwarfFrameSection =
526     Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
527                        SectionKind::getMetadata());
528   DwarfPubNamesSection =
529     Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
530                        SectionKind::getMetadata());
531   DwarfPubTypesSection =
532     Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
533                        SectionKind::getMetadata());
534   DwarfGnuPubNamesSection =
535     Ctx->getELFSection(".debug_gnu_pubnames", ELF::SHT_PROGBITS, 0,
536                        SectionKind::getMetadata());
537   DwarfGnuPubTypesSection =
538     Ctx->getELFSection(".debug_gnu_pubtypes", ELF::SHT_PROGBITS, 0,
539                        SectionKind::getMetadata());
540   DwarfStrSection =
541     Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS,
542                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
543                        SectionKind::getMergeable1ByteCString());
544   DwarfLocSection =
545     Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
546                        SectionKind::getMetadata());
547   DwarfARangesSection =
548     Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
549                        SectionKind::getMetadata());
550   DwarfRangesSection =
551     Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
552                        SectionKind::getMetadata());
553   DwarfMacroInfoSection =
554     Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
555                        SectionKind::getMetadata());
556
557   // DWARF5 Experimental Debug Info
558
559   // Accelerator Tables
560   DwarfAccelNamesSection =
561     Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0,
562                        SectionKind::getMetadata());
563   DwarfAccelObjCSection =
564     Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0,
565                        SectionKind::getMetadata());
566   DwarfAccelNamespaceSection =
567     Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0,
568                        SectionKind::getMetadata());
569   DwarfAccelTypesSection =
570     Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0,
571                        SectionKind::getMetadata());
572
573   // Fission Sections
574   DwarfInfoDWOSection =
575     Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0,
576                        SectionKind::getMetadata());
577   DwarfTypesDWOSection =
578     Ctx->getELFSection(".debug_types.dwo", ELF::SHT_PROGBITS, 0,
579                        SectionKind::getMetadata());
580   DwarfAbbrevDWOSection =
581     Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0,
582                        SectionKind::getMetadata());
583   DwarfStrDWOSection =
584     Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS,
585                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
586                        SectionKind::getMergeable1ByteCString());
587   DwarfLineDWOSection =
588     Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0,
589                        SectionKind::getMetadata());
590   DwarfLocDWOSection =
591     Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0,
592                        SectionKind::getMetadata());
593   DwarfStrOffDWOSection =
594     Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0,
595                        SectionKind::getMetadata());
596   DwarfAddrSection =
597     Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0,
598                        SectionKind::getMetadata());
599
600   StackMapSection =
601     Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS,
602                        ELF::SHF_ALLOC,
603                        SectionKind::getMetadata());
604
605 }
606
607
608 void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) {
609   bool IsWoA = T.getArch() == Triple::arm || T.getArch() == Triple::thumb;
610
611   CommDirectiveSupportsAlignment = true;
612
613   // COFF
614   BSSSection =
615     Ctx->getCOFFSection(".bss",
616                         COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
617                         COFF::IMAGE_SCN_MEM_READ |
618                         COFF::IMAGE_SCN_MEM_WRITE,
619                         SectionKind::getBSS());
620   TextSection =
621     Ctx->getCOFFSection(".text",
622                         (IsWoA ? COFF::IMAGE_SCN_MEM_16BIT
623                                : (COFF::SectionCharacteristics)0) |
624                         COFF::IMAGE_SCN_CNT_CODE |
625                         COFF::IMAGE_SCN_MEM_EXECUTE |
626                         COFF::IMAGE_SCN_MEM_READ,
627                         SectionKind::getText());
628   DataSection =
629     Ctx->getCOFFSection(".data",
630                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
631                         COFF::IMAGE_SCN_MEM_READ |
632                         COFF::IMAGE_SCN_MEM_WRITE,
633                         SectionKind::getDataRel());
634   ReadOnlySection =
635     Ctx->getCOFFSection(".rdata",
636                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
637                         COFF::IMAGE_SCN_MEM_READ,
638                         SectionKind::getReadOnly());
639
640   if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
641     StaticCtorSection =
642       Ctx->getCOFFSection(".CRT$XCU",
643                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
644                           COFF::IMAGE_SCN_MEM_READ,
645                           SectionKind::getReadOnly());
646     StaticDtorSection =
647       Ctx->getCOFFSection(".CRT$XTX",
648                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
649                           COFF::IMAGE_SCN_MEM_READ,
650                           SectionKind::getReadOnly());
651   } else {
652     StaticCtorSection =
653       Ctx->getCOFFSection(".ctors",
654                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
655                           COFF::IMAGE_SCN_MEM_READ |
656                           COFF::IMAGE_SCN_MEM_WRITE,
657                           SectionKind::getDataRel());
658     StaticDtorSection =
659       Ctx->getCOFFSection(".dtors",
660                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
661                           COFF::IMAGE_SCN_MEM_READ |
662                           COFF::IMAGE_SCN_MEM_WRITE,
663                           SectionKind::getDataRel());
664   }
665
666   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
667   // though it contains relocatable pointers.  In PIC mode, this is probably a
668   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
669   // adjusted or this should be a data section.
670   assert(T.isOSWindows() && "Windows is the only supported COFF target");
671   if (T.getArch() == Triple::x86_64) {
672     // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
673     LSDASection = 0;
674   } else {
675     LSDASection = Ctx->getCOFFSection(".gcc_except_table",
676                                       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
677                                           COFF::IMAGE_SCN_MEM_READ,
678                                       SectionKind::getReadOnly());
679   }
680
681   // Debug info.
682   COFFDebugSymbolsSection =
683     Ctx->getCOFFSection(".debug$S",
684                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
685                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
686                         COFF::IMAGE_SCN_MEM_READ,
687                         SectionKind::getMetadata());
688
689   DwarfAbbrevSection =
690     Ctx->getCOFFSection(".debug_abbrev",
691                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
692                         COFF::IMAGE_SCN_MEM_READ,
693                         SectionKind::getMetadata());
694   DwarfInfoSection =
695     Ctx->getCOFFSection(".debug_info",
696                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
697                         COFF::IMAGE_SCN_MEM_READ,
698                         SectionKind::getMetadata());
699   DwarfLineSection =
700     Ctx->getCOFFSection(".debug_line",
701                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
702                         COFF::IMAGE_SCN_MEM_READ,
703                         SectionKind::getMetadata());
704   DwarfFrameSection =
705     Ctx->getCOFFSection(".debug_frame",
706                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
707                         COFF::IMAGE_SCN_MEM_READ,
708                         SectionKind::getMetadata());
709   DwarfPubNamesSection =
710     Ctx->getCOFFSection(".debug_pubnames",
711                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
712                         COFF::IMAGE_SCN_MEM_READ,
713                         SectionKind::getMetadata());
714   DwarfPubTypesSection =
715     Ctx->getCOFFSection(".debug_pubtypes",
716                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
717                         COFF::IMAGE_SCN_MEM_READ,
718                         SectionKind::getMetadata());
719   DwarfGnuPubNamesSection =
720     Ctx->getCOFFSection(".debug_gnu_pubnames",
721                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
722                         COFF::IMAGE_SCN_MEM_READ,
723                         SectionKind::getMetadata());
724   DwarfGnuPubTypesSection =
725     Ctx->getCOFFSection(".debug_gnu_pubtypes",
726                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
727                         COFF::IMAGE_SCN_MEM_READ,
728                         SectionKind::getMetadata());
729   DwarfStrSection =
730     Ctx->getCOFFSection(".debug_str",
731                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
732                         COFF::IMAGE_SCN_MEM_READ,
733                         SectionKind::getMetadata());
734   DwarfLocSection =
735     Ctx->getCOFFSection(".debug_loc",
736                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
737                         COFF::IMAGE_SCN_MEM_READ,
738                         SectionKind::getMetadata());
739   DwarfARangesSection =
740     Ctx->getCOFFSection(".debug_aranges",
741                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
742                         COFF::IMAGE_SCN_MEM_READ,
743                         SectionKind::getMetadata());
744   DwarfRangesSection =
745     Ctx->getCOFFSection(".debug_ranges",
746                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
747                         COFF::IMAGE_SCN_MEM_READ,
748                         SectionKind::getMetadata());
749   DwarfMacroInfoSection =
750     Ctx->getCOFFSection(".debug_macinfo",
751                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
752                         COFF::IMAGE_SCN_MEM_READ,
753                         SectionKind::getMetadata());
754   DwarfInfoDWOSection =
755       Ctx->getCOFFSection(".debug_info.dwo",
756                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
757                           COFF::IMAGE_SCN_MEM_READ,
758                           SectionKind::getMetadata());
759   DwarfTypesDWOSection =
760       Ctx->getCOFFSection(".debug_types.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
761                                                   COFF::IMAGE_SCN_MEM_READ,
762                           SectionKind::getMetadata());
763   DwarfAbbrevDWOSection =
764       Ctx->getCOFFSection(".debug_abbrev.dwo",
765                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
766                           COFF::IMAGE_SCN_MEM_READ,
767                           SectionKind::getMetadata());
768   DwarfStrDWOSection =
769       Ctx->getCOFFSection(".debug_str.dwo",
770                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
771                           COFF::IMAGE_SCN_MEM_READ,
772                           SectionKind::getMetadata());
773   DwarfLineDWOSection =
774       Ctx->getCOFFSection(".debug_line.dwo",
775                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
776                           COFF::IMAGE_SCN_MEM_READ,
777                           SectionKind::getMetadata());
778   DwarfLocDWOSection =
779       Ctx->getCOFFSection(".debug_loc.dwo",
780                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
781                           COFF::IMAGE_SCN_MEM_READ,
782                           SectionKind::getMetadata());
783   DwarfStrOffDWOSection =
784       Ctx->getCOFFSection(".debug_str_offsets.dwo",
785                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
786                           COFF::IMAGE_SCN_MEM_READ,
787                           SectionKind::getMetadata());
788
789   DwarfAddrSection =
790     Ctx->getCOFFSection(".debug_addr",
791                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
792                         COFF::IMAGE_SCN_MEM_READ,
793                         SectionKind::getMetadata());
794
795   DwarfAccelNamesSection =
796       Ctx->getCOFFSection(".apple_names",
797                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
798                           COFF::IMAGE_SCN_MEM_READ,
799                           SectionKind::getMetadata());
800   DwarfAccelNamespaceSection =
801       Ctx->getCOFFSection(".apple_namespaces",
802                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
803                           COFF::IMAGE_SCN_MEM_READ,
804                           SectionKind::getMetadata());
805   DwarfAccelTypesSection =
806       Ctx->getCOFFSection(".apple_types",
807                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
808                           COFF::IMAGE_SCN_MEM_READ,
809                           SectionKind::getMetadata());
810   DwarfAccelObjCSection =
811       Ctx->getCOFFSection(".apple_objc",
812                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
813                           COFF::IMAGE_SCN_MEM_READ,
814                           SectionKind::getMetadata());
815
816   DrectveSection =
817     Ctx->getCOFFSection(".drectve",
818                         COFF::IMAGE_SCN_LNK_INFO |
819                         COFF::IMAGE_SCN_LNK_REMOVE,
820                         SectionKind::getMetadata());
821
822   PDataSection =
823     Ctx->getCOFFSection(".pdata",
824                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
825                         COFF::IMAGE_SCN_MEM_READ,
826                         SectionKind::getDataRel());
827
828   XDataSection =
829     Ctx->getCOFFSection(".xdata",
830                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
831                         COFF::IMAGE_SCN_MEM_READ,
832                         SectionKind::getDataRel());
833
834   TLSDataSection =
835     Ctx->getCOFFSection(".tls$",
836                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
837                         COFF::IMAGE_SCN_MEM_READ |
838                         COFF::IMAGE_SCN_MEM_WRITE,
839                         SectionKind::getDataRel());
840 }
841
842 void MCObjectFileInfo::InitMCObjectFileInfo(StringRef T, Reloc::Model relocm,
843                                             CodeModel::Model cm,
844                                             MCContext &ctx) {
845   RelocM = relocm;
846   CMModel = cm;
847   Ctx = &ctx;
848
849   // Common.
850   CommDirectiveSupportsAlignment = true;
851   SupportsWeakOmittedEHFrame = true;
852   SupportsCompactUnwindWithoutEHFrame = false;
853
854   PersonalityEncoding = LSDAEncoding = FDECFIEncoding = TTypeEncoding =
855       dwarf::DW_EH_PE_absptr;
856
857   CompactUnwindDwarfEHFrameOnly = 0;
858
859   EHFrameSection = nullptr;             // Created on demand.
860   CompactUnwindSection = nullptr;       // Used only by selected targets.
861   DwarfAccelNamesSection = nullptr;     // Used only by selected targets.
862   DwarfAccelObjCSection = nullptr;      // Used only by selected targets.
863   DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
864   DwarfAccelTypesSection = nullptr;     // Used only by selected targets.
865
866   TT = Triple(T);
867
868   Triple::ArchType Arch = TT.getArch();
869   // FIXME: Checking for Arch here to filter out bogus triples such as
870   // cellspu-apple-darwin. Perhaps we should fix in Triple?
871   if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
872        Arch == Triple::arm || Arch == Triple::thumb ||
873        Arch == Triple::aarch64 ||
874        Arch == Triple::ppc || Arch == Triple::ppc64 ||
875        Arch == Triple::UnknownArch) &&
876       (TT.isOSDarwin() || TT.isOSBinFormatMachO())) {
877     Env = IsMachO;
878     InitMachOMCObjectFileInfo(TT);
879   } else if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
880               Arch == Triple::arm || Arch == Triple::thumb) &&
881              (TT.isOSWindows() && TT.getObjectFormat() == Triple::COFF)) {
882     Env = IsCOFF;
883     InitCOFFMCObjectFileInfo(TT);
884   } else {
885     Env = IsELF;
886     InitELFMCObjectFileInfo(TT);
887   }
888 }
889
890 const MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const {
891   return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP,
892                             SectionKind::getMetadata(), 0, utostr(Hash));
893 }
894
895 void MCObjectFileInfo::InitEHFrameSection() {
896   if (Env == IsMachO)
897     EHFrameSection =
898       Ctx->getMachOSection("__TEXT", "__eh_frame",
899                            MachO::S_COALESCED |
900                            MachO::S_ATTR_NO_TOC |
901                            MachO::S_ATTR_STRIP_STATIC_SYMS |
902                            MachO::S_ATTR_LIVE_SUPPORT,
903                            SectionKind::getReadOnly());
904   else if (Env == IsELF)
905     EHFrameSection =
906       Ctx->getELFSection(".eh_frame", EHSectionType,
907                          EHSectionFlags,
908                          SectionKind::getDataRel());
909   else
910     EHFrameSection =
911       Ctx->getCOFFSection(".eh_frame",
912                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
913                           COFF::IMAGE_SCN_MEM_READ |
914                           COFF::IMAGE_SCN_MEM_WRITE,
915                           SectionKind::getDataRel());
916 }