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