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   DwarfTypesDWOSection =
541     Ctx->getELFSection(".debug_types.dwo", ELF::SHT_PROGBITS, 0,
542                        SectionKind::getMetadata());
543   DwarfAbbrevDWOSection =
544     Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0,
545                        SectionKind::getMetadata());
546   DwarfStrDWOSection =
547     Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS,
548                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
549                        SectionKind::getMergeable1ByteCString());
550   DwarfLineDWOSection =
551     Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0,
552                        SectionKind::getMetadata());
553   DwarfLocDWOSection =
554     Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0,
555                        SectionKind::getMetadata());
556   DwarfStrOffDWOSection =
557     Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0,
558                        SectionKind::getMetadata());
559   DwarfAddrSection =
560     Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0,
561                        SectionKind::getMetadata());
562 }
563
564
565 void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) {
566   // The object file format cannot represent common symbols with explicit
567   // alignments.
568   CommDirectiveSupportsAlignment = false;
569
570   // COFF
571   BSSSection =
572     Ctx->getCOFFSection(".bss",
573                         COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
574                         COFF::IMAGE_SCN_MEM_READ |
575                         COFF::IMAGE_SCN_MEM_WRITE,
576                         SectionKind::getBSS());
577   TextSection =
578     Ctx->getCOFFSection(".text",
579                         COFF::IMAGE_SCN_CNT_CODE |
580                         COFF::IMAGE_SCN_MEM_EXECUTE |
581                         COFF::IMAGE_SCN_MEM_READ,
582                         SectionKind::getText());
583   DataSection =
584     Ctx->getCOFFSection(".data",
585                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
586                         COFF::IMAGE_SCN_MEM_READ |
587                         COFF::IMAGE_SCN_MEM_WRITE,
588                         SectionKind::getDataRel());
589   ReadOnlySection =
590     Ctx->getCOFFSection(".rdata",
591                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
592                         COFF::IMAGE_SCN_MEM_READ,
593                         SectionKind::getReadOnly());
594   if (T.isKnownWindowsMSVCEnvironment()) {
595     StaticCtorSection =
596       Ctx->getCOFFSection(".CRT$XCU",
597                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
598                           COFF::IMAGE_SCN_MEM_READ,
599                           SectionKind::getReadOnly());
600   } else {
601     StaticCtorSection =
602       Ctx->getCOFFSection(".ctors",
603                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
604                           COFF::IMAGE_SCN_MEM_READ |
605                           COFF::IMAGE_SCN_MEM_WRITE,
606                           SectionKind::getDataRel());
607   }
608
609
610   if (T.isKnownWindowsMSVCEnvironment()) {
611     StaticDtorSection =
612       Ctx->getCOFFSection(".CRT$XTX",
613                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
614                           COFF::IMAGE_SCN_MEM_READ,
615                           SectionKind::getReadOnly());
616   } else {
617     StaticDtorSection =
618       Ctx->getCOFFSection(".dtors",
619                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
620                           COFF::IMAGE_SCN_MEM_READ |
621                           COFF::IMAGE_SCN_MEM_WRITE,
622                           SectionKind::getDataRel());
623   }
624
625   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
626   // though it contains relocatable pointers.  In PIC mode, this is probably a
627   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
628   // adjusted or this should be a data section.
629   LSDASection =
630     Ctx->getCOFFSection(".gcc_except_table",
631                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
632                         COFF::IMAGE_SCN_MEM_READ,
633                         SectionKind::getReadOnly());
634
635   // Debug info.
636   COFFDebugSymbolsSection =
637     Ctx->getCOFFSection(".debug$S",
638                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
639                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
640                         COFF::IMAGE_SCN_MEM_READ,
641                         SectionKind::getMetadata());
642
643   DwarfAbbrevSection =
644     Ctx->getCOFFSection(".debug_abbrev",
645                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
646                         COFF::IMAGE_SCN_MEM_READ,
647                         SectionKind::getMetadata());
648   DwarfInfoSection =
649     Ctx->getCOFFSection(".debug_info",
650                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
651                         COFF::IMAGE_SCN_MEM_READ,
652                         SectionKind::getMetadata());
653   DwarfLineSection =
654     Ctx->getCOFFSection(".debug_line",
655                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
656                         COFF::IMAGE_SCN_MEM_READ,
657                         SectionKind::getMetadata());
658   DwarfFrameSection =
659     Ctx->getCOFFSection(".debug_frame",
660                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
661                         COFF::IMAGE_SCN_MEM_READ,
662                         SectionKind::getMetadata());
663   DwarfPubNamesSection =
664     Ctx->getCOFFSection(".debug_pubnames",
665                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
666                         COFF::IMAGE_SCN_MEM_READ,
667                         SectionKind::getMetadata());
668   DwarfPubTypesSection =
669     Ctx->getCOFFSection(".debug_pubtypes",
670                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
671                         COFF::IMAGE_SCN_MEM_READ,
672                         SectionKind::getMetadata());
673   DwarfGnuPubNamesSection =
674     Ctx->getCOFFSection(".debug_gnu_pubnames",
675                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
676                         COFF::IMAGE_SCN_MEM_READ,
677                         SectionKind::getMetadata());
678   DwarfGnuPubTypesSection =
679     Ctx->getCOFFSection(".debug_gnu_pubtypes",
680                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
681                         COFF::IMAGE_SCN_MEM_READ,
682                         SectionKind::getMetadata());
683   DwarfStrSection =
684     Ctx->getCOFFSection(".debug_str",
685                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
686                         COFF::IMAGE_SCN_MEM_READ,
687                         SectionKind::getMetadata());
688   DwarfLocSection =
689     Ctx->getCOFFSection(".debug_loc",
690                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
691                         COFF::IMAGE_SCN_MEM_READ,
692                         SectionKind::getMetadata());
693   DwarfARangesSection =
694     Ctx->getCOFFSection(".debug_aranges",
695                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
696                         COFF::IMAGE_SCN_MEM_READ,
697                         SectionKind::getMetadata());
698   DwarfRangesSection =
699     Ctx->getCOFFSection(".debug_ranges",
700                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
701                         COFF::IMAGE_SCN_MEM_READ,
702                         SectionKind::getMetadata());
703   DwarfMacroInfoSection =
704     Ctx->getCOFFSection(".debug_macinfo",
705                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
706                         COFF::IMAGE_SCN_MEM_READ,
707                         SectionKind::getMetadata());
708   DwarfInfoDWOSection =
709       Ctx->getCOFFSection(".debug_info.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
710                                                 COFF::IMAGE_SCN_MEM_READ,
711                           SectionKind::getMetadata());
712   DwarfAbbrevDWOSection =
713       Ctx->getCOFFSection(".debug_abbrev.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
714                                                   COFF::IMAGE_SCN_MEM_READ,
715                           SectionKind::getMetadata());
716   DwarfStrDWOSection =
717       Ctx->getCOFFSection(".debug_str.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
718                                                COFF::IMAGE_SCN_MEM_READ,
719                           SectionKind::getMetadata());
720   DwarfLineDWOSection =
721       Ctx->getCOFFSection(".debug_line.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
722                                                 COFF::IMAGE_SCN_MEM_READ,
723                           SectionKind::getMetadata());
724   DwarfLocDWOSection =
725       Ctx->getCOFFSection(".debug_loc.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
726                                                COFF::IMAGE_SCN_MEM_READ,
727                           SectionKind::getMetadata());
728   DwarfStrOffDWOSection =
729       Ctx->getCOFFSection(".debug_str_offsets.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
730                                                   COFF::IMAGE_SCN_MEM_READ,
731                           SectionKind::getMetadata());
732   DwarfAddrSection = Ctx->getCOFFSection(
733       ".debug_addr", COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_MEM_READ,
734       SectionKind::getMetadata());
735
736   DrectveSection =
737     Ctx->getCOFFSection(".drectve",
738                         COFF::IMAGE_SCN_LNK_INFO,
739                         SectionKind::getMetadata());
740
741   PDataSection =
742     Ctx->getCOFFSection(".pdata",
743                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
744                         COFF::IMAGE_SCN_MEM_READ,
745                         SectionKind::getDataRel());
746
747   XDataSection =
748     Ctx->getCOFFSection(".xdata",
749                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
750                         COFF::IMAGE_SCN_MEM_READ,
751                         SectionKind::getDataRel());
752   TLSDataSection =
753     Ctx->getCOFFSection(".tls$",
754                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
755                         COFF::IMAGE_SCN_MEM_READ |
756                         COFF::IMAGE_SCN_MEM_WRITE,
757                         SectionKind::getDataRel());
758 }
759
760 void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model relocm,
761                                             CodeModel::Model cm,
762                                             MCContext &ctx) {
763   RelocM = relocm;
764   CMModel = cm;
765   Ctx = &ctx;
766
767   // Common.
768   CommDirectiveSupportsAlignment = true;
769   SupportsWeakOmittedEHFrame = true;
770   IsFunctionEHFrameSymbolPrivate = true;
771   SupportsCompactUnwindWithoutEHFrame = false;
772
773   PersonalityEncoding = LSDAEncoding = FDECFIEncoding = TTypeEncoding =
774       dwarf::DW_EH_PE_absptr;
775
776   CompactUnwindDwarfEHFrameOnly = 0;
777
778   EHFrameSection = nullptr;             // Created on demand.
779   CompactUnwindSection = nullptr;       // Used only by selected targets.
780   DwarfAccelNamesSection = nullptr;     // Used only by selected targets.
781   DwarfAccelObjCSection = nullptr;      // Used only by selected targets.
782   DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
783   DwarfAccelTypesSection = nullptr;     // Used only by selected targets.
784
785   Triple T(TT);
786   Triple::ArchType Arch = T.getArch();
787   // FIXME: Checking for Arch here to filter out bogus triples such as
788   // cellspu-apple-darwin. Perhaps we should fix in Triple?
789   if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
790        Arch == Triple::arm || Arch == Triple::thumb ||
791        Arch == Triple::arm64 ||
792        Arch == Triple::ppc || Arch == Triple::ppc64 ||
793        Arch == Triple::UnknownArch) &&
794       (T.isOSDarwin() || T.isOSBinFormatMachO())) {
795     Env = IsMachO;
796     InitMachOMCObjectFileInfo(T);
797   } else if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
798               Arch == Triple::arm || Arch == Triple::thumb) &&
799              (T.isOSWindows() && T.getObjectFormat() == Triple::COFF)) {
800     Env = IsCOFF;
801     InitCOFFMCObjectFileInfo(T);
802   } else {
803     Env = IsELF;
804     InitELFMCObjectFileInfo(T);
805   }
806 }
807
808 const MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const {
809   return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP,
810                             SectionKind::getMetadata(), 0, utostr(Hash));
811 }
812
813 void MCObjectFileInfo::InitEHFrameSection() {
814   if (Env == IsMachO)
815     EHFrameSection =
816       Ctx->getMachOSection("__TEXT", "__eh_frame",
817                            MachO::S_COALESCED |
818                            MachO::S_ATTR_NO_TOC |
819                            MachO::S_ATTR_STRIP_STATIC_SYMS |
820                            MachO::S_ATTR_LIVE_SUPPORT,
821                            SectionKind::getReadOnly());
822   else if (Env == IsELF)
823     EHFrameSection =
824       Ctx->getELFSection(".eh_frame", EHSectionType,
825                          EHSectionFlags,
826                          SectionKind::getDataRel());
827   else
828     EHFrameSection =
829       Ctx->getCOFFSection(".eh_frame",
830                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
831                           COFF::IMAGE_SCN_MEM_READ |
832                           COFF::IMAGE_SCN_MEM_WRITE,
833                           SectionKind::getDataRel());
834 }