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