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