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