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