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