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