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