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