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