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