[ARM64] Ensure arm64_be is dealt with when emitting debug info.
[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              T.getArch() == Triple::arm64_be) {
296     // The small model guarantees static code/data size < 4GB, but not where it
297     // will be in memory. Most of these could end up >2GB away so even a signed
298     // pc-relative 32-bit address is insufficient, theoretically.
299     if (RelocM == Reloc::PIC_) {
300       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
301         dwarf::DW_EH_PE_sdata8;
302       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
303       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
304       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
305         dwarf::DW_EH_PE_sdata8;
306     } else {
307       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
308       LSDAEncoding = dwarf::DW_EH_PE_absptr;
309       FDEEncoding = dwarf::DW_EH_PE_udata4;
310       TTypeEncoding = dwarf::DW_EH_PE_absptr;
311     }
312   } else if (T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le) {
313     PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
314       dwarf::DW_EH_PE_udata8;
315     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
316     FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
317     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
318       dwarf::DW_EH_PE_udata8;
319   } else if (T.getArch() == Triple::sparc) {
320     if (RelocM == Reloc::PIC_) {
321       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
322       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
323         dwarf::DW_EH_PE_sdata4;
324       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
325       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
326         dwarf::DW_EH_PE_sdata4;
327     } else {
328       LSDAEncoding = dwarf::DW_EH_PE_absptr;
329       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
330       FDEEncoding = dwarf::DW_EH_PE_udata4;
331       TTypeEncoding = dwarf::DW_EH_PE_absptr;
332     }
333   } else if (T.getArch() == Triple::sparcv9) {
334     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
335     if (RelocM == Reloc::PIC_) {
336       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
337         dwarf::DW_EH_PE_sdata4;
338       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
339       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
340         dwarf::DW_EH_PE_sdata4;
341     } else {
342       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
343       FDEEncoding = dwarf::DW_EH_PE_udata4;
344       TTypeEncoding = dwarf::DW_EH_PE_absptr;
345     }
346   } else if (T.getArch() == Triple::systemz) {
347     // All currently-defined code models guarantee that 4-byte PC-relative
348     // values will be in range.
349     if (RelocM == Reloc::PIC_) {
350       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
351         dwarf::DW_EH_PE_sdata4;
352       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
353       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
354       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
355         dwarf::DW_EH_PE_sdata4;
356     } else {
357       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
358       LSDAEncoding = dwarf::DW_EH_PE_absptr;
359       FDEEncoding = dwarf::DW_EH_PE_absptr;
360       TTypeEncoding = dwarf::DW_EH_PE_absptr;
361     }
362   }
363
364   // Solaris requires different flags for .eh_frame to seemingly every other
365   // platform.
366   EHSectionType = ELF::SHT_PROGBITS;
367   EHSectionFlags = ELF::SHF_ALLOC;
368   if (T.getOS() == Triple::Solaris) {
369     if (T.getArch() == Triple::x86_64)
370       EHSectionType = ELF::SHT_X86_64_UNWIND;
371     else
372       EHSectionFlags |= ELF::SHF_WRITE;
373   }
374
375
376   // ELF
377   BSSSection =
378     Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
379                        ELF::SHF_WRITE | ELF::SHF_ALLOC,
380                        SectionKind::getBSS());
381
382   TextSection =
383     Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
384                        ELF::SHF_EXECINSTR |
385                        ELF::SHF_ALLOC,
386                        SectionKind::getText());
387
388   DataSection =
389     Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
390                        ELF::SHF_WRITE |ELF::SHF_ALLOC,
391                        SectionKind::getDataRel());
392
393   ReadOnlySection =
394     Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS,
395                        ELF::SHF_ALLOC,
396                        SectionKind::getReadOnly());
397
398   TLSDataSection =
399     Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
400                        ELF::SHF_ALLOC | ELF::SHF_TLS |
401                        ELF::SHF_WRITE,
402                        SectionKind::getThreadData());
403
404   TLSBSSSection =
405     Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
406                        ELF::SHF_ALLOC | ELF::SHF_TLS |
407                        ELF::SHF_WRITE,
408                        SectionKind::getThreadBSS());
409
410   DataRelSection =
411     Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS,
412                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
413                        SectionKind::getDataRel());
414
415   DataRelLocalSection =
416     Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
417                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
418                        SectionKind::getDataRelLocal());
419
420   DataRelROSection =
421     Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
422                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
423                        SectionKind::getReadOnlyWithRel());
424
425   DataRelROLocalSection =
426     Ctx->getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
427                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
428                        SectionKind::getReadOnlyWithRelLocal());
429
430   MergeableConst4Section =
431     Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
432                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
433                        SectionKind::getMergeableConst4());
434
435   MergeableConst8Section =
436     Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
437                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
438                        SectionKind::getMergeableConst8());
439
440   MergeableConst16Section =
441     Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
442                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
443                        SectionKind::getMergeableConst16());
444
445   StaticCtorSection =
446     Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS,
447                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
448                        SectionKind::getDataRel());
449
450   StaticDtorSection =
451     Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS,
452                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
453                        SectionKind::getDataRel());
454
455   // Exception Handling Sections.
456
457   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
458   // it contains relocatable pointers.  In PIC mode, this is probably a big
459   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
460   // adjusted or this should be a data section.
461   LSDASection =
462     Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
463                        ELF::SHF_ALLOC,
464                        SectionKind::getReadOnly());
465
466   COFFDebugSymbolsSection = nullptr;
467
468   // Debug Info Sections.
469   DwarfAbbrevSection =
470     Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
471                        SectionKind::getMetadata());
472   DwarfInfoSection =
473     Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
474                        SectionKind::getMetadata());
475   DwarfLineSection =
476     Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
477                        SectionKind::getMetadata());
478   DwarfFrameSection =
479     Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
480                        SectionKind::getMetadata());
481   DwarfPubNamesSection =
482     Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
483                        SectionKind::getMetadata());
484   DwarfPubTypesSection =
485     Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
486                        SectionKind::getMetadata());
487   DwarfGnuPubNamesSection =
488     Ctx->getELFSection(".debug_gnu_pubnames", ELF::SHT_PROGBITS, 0,
489                        SectionKind::getMetadata());
490   DwarfGnuPubTypesSection =
491     Ctx->getELFSection(".debug_gnu_pubtypes", ELF::SHT_PROGBITS, 0,
492                        SectionKind::getMetadata());
493   DwarfStrSection =
494     Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS,
495                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
496                        SectionKind::getMergeable1ByteCString());
497   DwarfLocSection =
498     Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
499                        SectionKind::getMetadata());
500   DwarfARangesSection =
501     Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
502                        SectionKind::getMetadata());
503   DwarfRangesSection =
504     Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
505                        SectionKind::getMetadata());
506   DwarfMacroInfoSection =
507     Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
508                        SectionKind::getMetadata());
509
510   // DWARF5 Experimental Debug Info
511
512   // Accelerator Tables
513   DwarfAccelNamesSection =
514     Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0,
515                        SectionKind::getMetadata());
516   DwarfAccelObjCSection =
517     Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0,
518                        SectionKind::getMetadata());
519   DwarfAccelNamespaceSection =
520     Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0,
521                        SectionKind::getMetadata());
522   DwarfAccelTypesSection =
523     Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0,
524                        SectionKind::getMetadata());
525
526   // Fission Sections
527   DwarfInfoDWOSection =
528     Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0,
529                        SectionKind::getMetadata());
530   DwarfAbbrevDWOSection =
531     Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0,
532                        SectionKind::getMetadata());
533   DwarfStrDWOSection =
534     Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS,
535                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
536                        SectionKind::getMergeable1ByteCString());
537   DwarfLineDWOSection =
538     Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0,
539                        SectionKind::getMetadata());
540   DwarfLocDWOSection =
541     Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0,
542                        SectionKind::getMetadata());
543   DwarfStrOffDWOSection =
544     Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0,
545                        SectionKind::getMetadata());
546   DwarfAddrSection =
547     Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0,
548                        SectionKind::getMetadata());
549 }
550
551
552 void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) {
553   // The object file format cannot represent common symbols with explicit
554   // alignments.
555   CommDirectiveSupportsAlignment = false;
556
557   // COFF
558   BSSSection =
559     Ctx->getCOFFSection(".bss",
560                         COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
561                         COFF::IMAGE_SCN_MEM_READ |
562                         COFF::IMAGE_SCN_MEM_WRITE,
563                         SectionKind::getBSS());
564   TextSection =
565     Ctx->getCOFFSection(".text",
566                         COFF::IMAGE_SCN_CNT_CODE |
567                         COFF::IMAGE_SCN_MEM_EXECUTE |
568                         COFF::IMAGE_SCN_MEM_READ,
569                         SectionKind::getText());
570   DataSection =
571     Ctx->getCOFFSection(".data",
572                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
573                         COFF::IMAGE_SCN_MEM_READ |
574                         COFF::IMAGE_SCN_MEM_WRITE,
575                         SectionKind::getDataRel());
576   ReadOnlySection =
577     Ctx->getCOFFSection(".rdata",
578                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
579                         COFF::IMAGE_SCN_MEM_READ,
580                         SectionKind::getReadOnly());
581   if (T.isKnownWindowsMSVCEnvironment()) {
582     StaticCtorSection =
583       Ctx->getCOFFSection(".CRT$XCU",
584                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
585                           COFF::IMAGE_SCN_MEM_READ,
586                           SectionKind::getReadOnly());
587   } else {
588     StaticCtorSection =
589       Ctx->getCOFFSection(".ctors",
590                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
591                           COFF::IMAGE_SCN_MEM_READ |
592                           COFF::IMAGE_SCN_MEM_WRITE,
593                           SectionKind::getDataRel());
594   }
595
596
597   if (T.isKnownWindowsMSVCEnvironment()) {
598     StaticDtorSection =
599       Ctx->getCOFFSection(".CRT$XTX",
600                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
601                           COFF::IMAGE_SCN_MEM_READ,
602                           SectionKind::getReadOnly());
603   } else {
604     StaticDtorSection =
605       Ctx->getCOFFSection(".dtors",
606                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
607                           COFF::IMAGE_SCN_MEM_READ |
608                           COFF::IMAGE_SCN_MEM_WRITE,
609                           SectionKind::getDataRel());
610   }
611
612   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
613   // though it contains relocatable pointers.  In PIC mode, this is probably a
614   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
615   // adjusted or this should be a data section.
616   LSDASection =
617     Ctx->getCOFFSection(".gcc_except_table",
618                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
619                         COFF::IMAGE_SCN_MEM_READ,
620                         SectionKind::getReadOnly());
621
622   // Debug info.
623   COFFDebugSymbolsSection =
624     Ctx->getCOFFSection(".debug$S",
625                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
626                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
627                         COFF::IMAGE_SCN_MEM_READ,
628                         SectionKind::getMetadata());
629
630   DwarfAbbrevSection =
631     Ctx->getCOFFSection(".debug_abbrev",
632                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
633                         COFF::IMAGE_SCN_MEM_READ,
634                         SectionKind::getMetadata());
635   DwarfInfoSection =
636     Ctx->getCOFFSection(".debug_info",
637                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
638                         COFF::IMAGE_SCN_MEM_READ,
639                         SectionKind::getMetadata());
640   DwarfLineSection =
641     Ctx->getCOFFSection(".debug_line",
642                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
643                         COFF::IMAGE_SCN_MEM_READ,
644                         SectionKind::getMetadata());
645   DwarfFrameSection =
646     Ctx->getCOFFSection(".debug_frame",
647                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
648                         COFF::IMAGE_SCN_MEM_READ,
649                         SectionKind::getMetadata());
650   DwarfPubNamesSection =
651     Ctx->getCOFFSection(".debug_pubnames",
652                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
653                         COFF::IMAGE_SCN_MEM_READ,
654                         SectionKind::getMetadata());
655   DwarfPubTypesSection =
656     Ctx->getCOFFSection(".debug_pubtypes",
657                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
658                         COFF::IMAGE_SCN_MEM_READ,
659                         SectionKind::getMetadata());
660   DwarfGnuPubNamesSection =
661     Ctx->getCOFFSection(".debug_gnu_pubnames",
662                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
663                         COFF::IMAGE_SCN_MEM_READ,
664                         SectionKind::getMetadata());
665   DwarfGnuPubTypesSection =
666     Ctx->getCOFFSection(".debug_gnu_pubtypes",
667                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
668                         COFF::IMAGE_SCN_MEM_READ,
669                         SectionKind::getMetadata());
670   DwarfStrSection =
671     Ctx->getCOFFSection(".debug_str",
672                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
673                         COFF::IMAGE_SCN_MEM_READ,
674                         SectionKind::getMetadata());
675   DwarfLocSection =
676     Ctx->getCOFFSection(".debug_loc",
677                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
678                         COFF::IMAGE_SCN_MEM_READ,
679                         SectionKind::getMetadata());
680   DwarfARangesSection =
681     Ctx->getCOFFSection(".debug_aranges",
682                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
683                         COFF::IMAGE_SCN_MEM_READ,
684                         SectionKind::getMetadata());
685   DwarfRangesSection =
686     Ctx->getCOFFSection(".debug_ranges",
687                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
688                         COFF::IMAGE_SCN_MEM_READ,
689                         SectionKind::getMetadata());
690   DwarfMacroInfoSection =
691     Ctx->getCOFFSection(".debug_macinfo",
692                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
693                         COFF::IMAGE_SCN_MEM_READ,
694                         SectionKind::getMetadata());
695   DwarfInfoDWOSection =
696       Ctx->getCOFFSection(".debug_info.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
697                                                 COFF::IMAGE_SCN_MEM_READ,
698                           SectionKind::getMetadata());
699   DwarfAbbrevDWOSection =
700       Ctx->getCOFFSection(".debug_abbrev.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
701                                                   COFF::IMAGE_SCN_MEM_READ,
702                           SectionKind::getMetadata());
703   DwarfStrDWOSection =
704       Ctx->getCOFFSection(".debug_str.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
705                                                COFF::IMAGE_SCN_MEM_READ,
706                           SectionKind::getMetadata());
707   DwarfLineDWOSection =
708       Ctx->getCOFFSection(".debug_line.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
709                                                 COFF::IMAGE_SCN_MEM_READ,
710                           SectionKind::getMetadata());
711   DwarfLocDWOSection =
712       Ctx->getCOFFSection(".debug_loc.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
713                                                COFF::IMAGE_SCN_MEM_READ,
714                           SectionKind::getMetadata());
715   DwarfStrOffDWOSection =
716       Ctx->getCOFFSection(".debug_str_offsets.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
717                                                   COFF::IMAGE_SCN_MEM_READ,
718                           SectionKind::getMetadata());
719   DwarfAddrSection = Ctx->getCOFFSection(
720       ".debug_addr", COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_MEM_READ,
721       SectionKind::getMetadata());
722
723   DrectveSection =
724     Ctx->getCOFFSection(".drectve",
725                         COFF::IMAGE_SCN_LNK_INFO,
726                         SectionKind::getMetadata());
727
728   PDataSection =
729     Ctx->getCOFFSection(".pdata",
730                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
731                         COFF::IMAGE_SCN_MEM_READ,
732                         SectionKind::getDataRel());
733
734   XDataSection =
735     Ctx->getCOFFSection(".xdata",
736                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
737                         COFF::IMAGE_SCN_MEM_READ,
738                         SectionKind::getDataRel());
739   TLSDataSection =
740     Ctx->getCOFFSection(".tls$",
741                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
742                         COFF::IMAGE_SCN_MEM_READ |
743                         COFF::IMAGE_SCN_MEM_WRITE,
744                         SectionKind::getDataRel());
745 }
746
747 void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model relocm,
748                                             CodeModel::Model cm,
749                                             MCContext &ctx) {
750   RelocM = relocm;
751   CMModel = cm;
752   Ctx = &ctx;
753
754   // Common.
755   CommDirectiveSupportsAlignment = true;
756   SupportsWeakOmittedEHFrame = true;
757   IsFunctionEHFrameSymbolPrivate = true;
758   SupportsCompactUnwindWithoutEHFrame = false;
759
760   PersonalityEncoding = LSDAEncoding = FDEEncoding = FDECFIEncoding =
761     TTypeEncoding = dwarf::DW_EH_PE_absptr;
762
763   CompactUnwindDwarfEHFrameOnly = 0;
764
765   EHFrameSection = nullptr;             // Created on demand.
766   CompactUnwindSection = nullptr;       // Used only by selected targets.
767   DwarfAccelNamesSection = nullptr;     // Used only by selected targets.
768   DwarfAccelObjCSection = nullptr;      // Used only by selected targets.
769   DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
770   DwarfAccelTypesSection = nullptr;     // Used only by selected targets.
771
772   Triple T(TT);
773   Triple::ArchType Arch = T.getArch();
774   // FIXME: Checking for Arch here to filter out bogus triples such as
775   // cellspu-apple-darwin. Perhaps we should fix in Triple?
776   if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
777        Arch == Triple::arm || Arch == Triple::thumb ||
778        Arch == Triple::arm64 ||
779        Arch == Triple::ppc || Arch == Triple::ppc64 ||
780        Arch == Triple::UnknownArch) &&
781       (T.isOSDarwin() || T.isOSBinFormatMachO())) {
782     Env = IsMachO;
783     InitMachOMCObjectFileInfo(T);
784   } else if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
785               Arch == Triple::arm || Arch == Triple::thumb) &&
786              (T.isOSWindows() && T.getObjectFormat() == Triple::COFF)) {
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 }