Allow using normal .eh_frame based unwinding on ARM. Use the same
[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::arm:
264   case Triple::armeb:
265   case Triple::thumb:
266   case Triple::thumbeb:
267   case Triple::x86:
268     PersonalityEncoding = (RelocM == Reloc::PIC_)
269      ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
270      : dwarf::DW_EH_PE_absptr;
271     LSDAEncoding = (RelocM == Reloc::PIC_)
272       ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
273       : dwarf::DW_EH_PE_absptr;
274     FDEEncoding = (RelocM == Reloc::PIC_)
275       ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
276       : dwarf::DW_EH_PE_absptr;
277     TTypeEncoding = (RelocM == Reloc::PIC_)
278      ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
279      : dwarf::DW_EH_PE_absptr;
280     break;
281   case Triple::x86_64:
282     if (RelocM == Reloc::PIC_) {
283       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
284         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
285          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
286       LSDAEncoding = dwarf::DW_EH_PE_pcrel |
287         (CMModel == CodeModel::Small
288          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
289       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
290       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
291         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
292          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
293     } else {
294       PersonalityEncoding =
295         (CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
296         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
297       LSDAEncoding = (CMModel == CodeModel::Small)
298         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
299       FDEEncoding = dwarf::DW_EH_PE_udata4;
300       TTypeEncoding = (CMModel == CodeModel::Small)
301         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
302     }
303     break;
304   case Triple::aarch64:
305   case Triple::aarch64_be:
306   case Triple::arm64:
307   case Triple::arm64_be:
308     // The small model guarantees static code/data size < 4GB, but not where it
309     // will be in memory. Most of these could end up >2GB away so even a signed
310     // pc-relative 32-bit address is insufficient, theoretically.
311     if (RelocM == Reloc::PIC_) {
312       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
313         dwarf::DW_EH_PE_sdata8;
314       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
315       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
316       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
317         dwarf::DW_EH_PE_sdata8;
318     } else {
319       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
320       LSDAEncoding = dwarf::DW_EH_PE_absptr;
321       FDEEncoding = dwarf::DW_EH_PE_udata4;
322       TTypeEncoding = dwarf::DW_EH_PE_absptr;
323     }
324     break;
325   case Triple::ppc64:
326   case Triple::ppc64le:
327     PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
328       dwarf::DW_EH_PE_udata8;
329     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
330     FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
331     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
332       dwarf::DW_EH_PE_udata8;
333     break;
334   case Triple::sparc:
335     if (RelocM == Reloc::PIC_) {
336       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
337       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
338         dwarf::DW_EH_PE_sdata4;
339       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
340       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
341         dwarf::DW_EH_PE_sdata4;
342     } else {
343       LSDAEncoding = dwarf::DW_EH_PE_absptr;
344       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
345       FDEEncoding = dwarf::DW_EH_PE_udata4;
346       TTypeEncoding = dwarf::DW_EH_PE_absptr;
347     }
348     break;
349   case Triple::sparcv9:
350     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
351     if (RelocM == Reloc::PIC_) {
352       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
353         dwarf::DW_EH_PE_sdata4;
354       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
355       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
356         dwarf::DW_EH_PE_sdata4;
357     } else {
358       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
359       FDEEncoding = dwarf::DW_EH_PE_udata4;
360       TTypeEncoding = dwarf::DW_EH_PE_absptr;
361     }
362     break;
363   case Triple::systemz:
364     // All currently-defined code models guarantee that 4-byte PC-relative
365     // values will be in range.
366     if (RelocM == Reloc::PIC_) {
367       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
368         dwarf::DW_EH_PE_sdata4;
369       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
370       FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
371       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
372         dwarf::DW_EH_PE_sdata4;
373     } else {
374       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
375       LSDAEncoding = dwarf::DW_EH_PE_absptr;
376       FDEEncoding = dwarf::DW_EH_PE_absptr;
377       TTypeEncoding = dwarf::DW_EH_PE_absptr;
378     }
379     break;
380   default:
381     break;
382   }
383
384   // Solaris requires different flags for .eh_frame to seemingly every other
385   // platform.
386   EHSectionType = ELF::SHT_PROGBITS;
387   EHSectionFlags = ELF::SHF_ALLOC;
388   if (T.getOS() == Triple::Solaris) {
389     if (T.getArch() == Triple::x86_64)
390       EHSectionType = ELF::SHT_X86_64_UNWIND;
391     else
392       EHSectionFlags |= ELF::SHF_WRITE;
393   }
394
395
396   // ELF
397   BSSSection =
398     Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
399                        ELF::SHF_WRITE | ELF::SHF_ALLOC,
400                        SectionKind::getBSS());
401
402   TextSection =
403     Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
404                        ELF::SHF_EXECINSTR |
405                        ELF::SHF_ALLOC,
406                        SectionKind::getText());
407
408   DataSection =
409     Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
410                        ELF::SHF_WRITE |ELF::SHF_ALLOC,
411                        SectionKind::getDataRel());
412
413   ReadOnlySection =
414     Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS,
415                        ELF::SHF_ALLOC,
416                        SectionKind::getReadOnly());
417
418   TLSDataSection =
419     Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
420                        ELF::SHF_ALLOC | ELF::SHF_TLS |
421                        ELF::SHF_WRITE,
422                        SectionKind::getThreadData());
423
424   TLSBSSSection =
425     Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
426                        ELF::SHF_ALLOC | ELF::SHF_TLS |
427                        ELF::SHF_WRITE,
428                        SectionKind::getThreadBSS());
429
430   DataRelSection =
431     Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS,
432                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
433                        SectionKind::getDataRel());
434
435   DataRelLocalSection =
436     Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
437                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
438                        SectionKind::getDataRelLocal());
439
440   DataRelROSection =
441     Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
442                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
443                        SectionKind::getReadOnlyWithRel());
444
445   DataRelROLocalSection =
446     Ctx->getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
447                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
448                        SectionKind::getReadOnlyWithRelLocal());
449
450   MergeableConst4Section =
451     Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
452                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
453                        SectionKind::getMergeableConst4());
454
455   MergeableConst8Section =
456     Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
457                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
458                        SectionKind::getMergeableConst8());
459
460   MergeableConst16Section =
461     Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
462                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
463                        SectionKind::getMergeableConst16());
464
465   StaticCtorSection =
466     Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS,
467                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
468                        SectionKind::getDataRel());
469
470   StaticDtorSection =
471     Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS,
472                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
473                        SectionKind::getDataRel());
474
475   // Exception Handling Sections.
476
477   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
478   // it contains relocatable pointers.  In PIC mode, this is probably a big
479   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
480   // adjusted or this should be a data section.
481   LSDASection =
482     Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
483                        ELF::SHF_ALLOC,
484                        SectionKind::getReadOnly());
485
486   COFFDebugSymbolsSection = nullptr;
487
488   // Debug Info Sections.
489   DwarfAbbrevSection =
490     Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
491                        SectionKind::getMetadata());
492   DwarfInfoSection =
493     Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
494                        SectionKind::getMetadata());
495   DwarfLineSection =
496     Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
497                        SectionKind::getMetadata());
498   DwarfFrameSection =
499     Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
500                        SectionKind::getMetadata());
501   DwarfPubNamesSection =
502     Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
503                        SectionKind::getMetadata());
504   DwarfPubTypesSection =
505     Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
506                        SectionKind::getMetadata());
507   DwarfGnuPubNamesSection =
508     Ctx->getELFSection(".debug_gnu_pubnames", ELF::SHT_PROGBITS, 0,
509                        SectionKind::getMetadata());
510   DwarfGnuPubTypesSection =
511     Ctx->getELFSection(".debug_gnu_pubtypes", ELF::SHT_PROGBITS, 0,
512                        SectionKind::getMetadata());
513   DwarfStrSection =
514     Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS,
515                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
516                        SectionKind::getMergeable1ByteCString());
517   DwarfLocSection =
518     Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
519                        SectionKind::getMetadata());
520   DwarfARangesSection =
521     Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
522                        SectionKind::getMetadata());
523   DwarfRangesSection =
524     Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
525                        SectionKind::getMetadata());
526   DwarfMacroInfoSection =
527     Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
528                        SectionKind::getMetadata());
529
530   // DWARF5 Experimental Debug Info
531
532   // Accelerator Tables
533   DwarfAccelNamesSection =
534     Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0,
535                        SectionKind::getMetadata());
536   DwarfAccelObjCSection =
537     Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0,
538                        SectionKind::getMetadata());
539   DwarfAccelNamespaceSection =
540     Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0,
541                        SectionKind::getMetadata());
542   DwarfAccelTypesSection =
543     Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0,
544                        SectionKind::getMetadata());
545
546   // Fission Sections
547   DwarfInfoDWOSection =
548     Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0,
549                        SectionKind::getMetadata());
550   DwarfAbbrevDWOSection =
551     Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0,
552                        SectionKind::getMetadata());
553   DwarfStrDWOSection =
554     Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS,
555                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
556                        SectionKind::getMergeable1ByteCString());
557   DwarfLineDWOSection =
558     Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0,
559                        SectionKind::getMetadata());
560   DwarfLocDWOSection =
561     Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0,
562                        SectionKind::getMetadata());
563   DwarfStrOffDWOSection =
564     Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0,
565                        SectionKind::getMetadata());
566   DwarfAddrSection =
567     Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0,
568                        SectionKind::getMetadata());
569 }
570
571
572 void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) {
573   // The object file format cannot represent common symbols with explicit
574   // alignments.
575   CommDirectiveSupportsAlignment = false;
576
577   // COFF
578   BSSSection =
579     Ctx->getCOFFSection(".bss",
580                         COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
581                         COFF::IMAGE_SCN_MEM_READ |
582                         COFF::IMAGE_SCN_MEM_WRITE,
583                         SectionKind::getBSS());
584   TextSection =
585     Ctx->getCOFFSection(".text",
586                         COFF::IMAGE_SCN_CNT_CODE |
587                         COFF::IMAGE_SCN_MEM_EXECUTE |
588                         COFF::IMAGE_SCN_MEM_READ,
589                         SectionKind::getText());
590   DataSection =
591     Ctx->getCOFFSection(".data",
592                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
593                         COFF::IMAGE_SCN_MEM_READ |
594                         COFF::IMAGE_SCN_MEM_WRITE,
595                         SectionKind::getDataRel());
596   ReadOnlySection =
597     Ctx->getCOFFSection(".rdata",
598                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
599                         COFF::IMAGE_SCN_MEM_READ,
600                         SectionKind::getReadOnly());
601   if (T.isKnownWindowsMSVCEnvironment()) {
602     StaticCtorSection =
603       Ctx->getCOFFSection(".CRT$XCU",
604                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
605                           COFF::IMAGE_SCN_MEM_READ,
606                           SectionKind::getReadOnly());
607   } else {
608     StaticCtorSection =
609       Ctx->getCOFFSection(".ctors",
610                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
611                           COFF::IMAGE_SCN_MEM_READ |
612                           COFF::IMAGE_SCN_MEM_WRITE,
613                           SectionKind::getDataRel());
614   }
615
616
617   if (T.isKnownWindowsMSVCEnvironment()) {
618     StaticDtorSection =
619       Ctx->getCOFFSection(".CRT$XTX",
620                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
621                           COFF::IMAGE_SCN_MEM_READ,
622                           SectionKind::getReadOnly());
623   } else {
624     StaticDtorSection =
625       Ctx->getCOFFSection(".dtors",
626                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
627                           COFF::IMAGE_SCN_MEM_READ |
628                           COFF::IMAGE_SCN_MEM_WRITE,
629                           SectionKind::getDataRel());
630   }
631
632   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
633   // though it contains relocatable pointers.  In PIC mode, this is probably a
634   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
635   // adjusted or this should be a data section.
636   LSDASection =
637     Ctx->getCOFFSection(".gcc_except_table",
638                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
639                         COFF::IMAGE_SCN_MEM_READ,
640                         SectionKind::getReadOnly());
641
642   // Debug info.
643   COFFDebugSymbolsSection =
644     Ctx->getCOFFSection(".debug$S",
645                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
646                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
647                         COFF::IMAGE_SCN_MEM_READ,
648                         SectionKind::getMetadata());
649
650   DwarfAbbrevSection =
651     Ctx->getCOFFSection(".debug_abbrev",
652                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
653                         COFF::IMAGE_SCN_MEM_READ,
654                         SectionKind::getMetadata());
655   DwarfInfoSection =
656     Ctx->getCOFFSection(".debug_info",
657                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
658                         COFF::IMAGE_SCN_MEM_READ,
659                         SectionKind::getMetadata());
660   DwarfLineSection =
661     Ctx->getCOFFSection(".debug_line",
662                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
663                         COFF::IMAGE_SCN_MEM_READ,
664                         SectionKind::getMetadata());
665   DwarfFrameSection =
666     Ctx->getCOFFSection(".debug_frame",
667                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
668                         COFF::IMAGE_SCN_MEM_READ,
669                         SectionKind::getMetadata());
670   DwarfPubNamesSection =
671     Ctx->getCOFFSection(".debug_pubnames",
672                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
673                         COFF::IMAGE_SCN_MEM_READ,
674                         SectionKind::getMetadata());
675   DwarfPubTypesSection =
676     Ctx->getCOFFSection(".debug_pubtypes",
677                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
678                         COFF::IMAGE_SCN_MEM_READ,
679                         SectionKind::getMetadata());
680   DwarfGnuPubNamesSection =
681     Ctx->getCOFFSection(".debug_gnu_pubnames",
682                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
683                         COFF::IMAGE_SCN_MEM_READ,
684                         SectionKind::getMetadata());
685   DwarfGnuPubTypesSection =
686     Ctx->getCOFFSection(".debug_gnu_pubtypes",
687                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
688                         COFF::IMAGE_SCN_MEM_READ,
689                         SectionKind::getMetadata());
690   DwarfStrSection =
691     Ctx->getCOFFSection(".debug_str",
692                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
693                         COFF::IMAGE_SCN_MEM_READ,
694                         SectionKind::getMetadata());
695   DwarfLocSection =
696     Ctx->getCOFFSection(".debug_loc",
697                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
698                         COFF::IMAGE_SCN_MEM_READ,
699                         SectionKind::getMetadata());
700   DwarfARangesSection =
701     Ctx->getCOFFSection(".debug_aranges",
702                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
703                         COFF::IMAGE_SCN_MEM_READ,
704                         SectionKind::getMetadata());
705   DwarfRangesSection =
706     Ctx->getCOFFSection(".debug_ranges",
707                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
708                         COFF::IMAGE_SCN_MEM_READ,
709                         SectionKind::getMetadata());
710   DwarfMacroInfoSection =
711     Ctx->getCOFFSection(".debug_macinfo",
712                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
713                         COFF::IMAGE_SCN_MEM_READ,
714                         SectionKind::getMetadata());
715   DwarfInfoDWOSection =
716       Ctx->getCOFFSection(".debug_info.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
717                                                 COFF::IMAGE_SCN_MEM_READ,
718                           SectionKind::getMetadata());
719   DwarfAbbrevDWOSection =
720       Ctx->getCOFFSection(".debug_abbrev.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
721                                                   COFF::IMAGE_SCN_MEM_READ,
722                           SectionKind::getMetadata());
723   DwarfStrDWOSection =
724       Ctx->getCOFFSection(".debug_str.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
725                                                COFF::IMAGE_SCN_MEM_READ,
726                           SectionKind::getMetadata());
727   DwarfLineDWOSection =
728       Ctx->getCOFFSection(".debug_line.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
729                                                 COFF::IMAGE_SCN_MEM_READ,
730                           SectionKind::getMetadata());
731   DwarfLocDWOSection =
732       Ctx->getCOFFSection(".debug_loc.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
733                                                COFF::IMAGE_SCN_MEM_READ,
734                           SectionKind::getMetadata());
735   DwarfStrOffDWOSection =
736       Ctx->getCOFFSection(".debug_str_offsets.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE |
737                                                   COFF::IMAGE_SCN_MEM_READ,
738                           SectionKind::getMetadata());
739   DwarfAddrSection = Ctx->getCOFFSection(
740       ".debug_addr", COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_MEM_READ,
741       SectionKind::getMetadata());
742
743   DrectveSection =
744     Ctx->getCOFFSection(".drectve",
745                         COFF::IMAGE_SCN_LNK_INFO,
746                         SectionKind::getMetadata());
747
748   PDataSection =
749     Ctx->getCOFFSection(".pdata",
750                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
751                         COFF::IMAGE_SCN_MEM_READ,
752                         SectionKind::getDataRel());
753
754   XDataSection =
755     Ctx->getCOFFSection(".xdata",
756                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
757                         COFF::IMAGE_SCN_MEM_READ,
758                         SectionKind::getDataRel());
759   TLSDataSection =
760     Ctx->getCOFFSection(".tls$",
761                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
762                         COFF::IMAGE_SCN_MEM_READ |
763                         COFF::IMAGE_SCN_MEM_WRITE,
764                         SectionKind::getDataRel());
765 }
766
767 void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model relocm,
768                                             CodeModel::Model cm,
769                                             MCContext &ctx) {
770   RelocM = relocm;
771   CMModel = cm;
772   Ctx = &ctx;
773
774   // Common.
775   CommDirectiveSupportsAlignment = true;
776   SupportsWeakOmittedEHFrame = true;
777   IsFunctionEHFrameSymbolPrivate = true;
778   SupportsCompactUnwindWithoutEHFrame = false;
779
780   PersonalityEncoding = LSDAEncoding = FDEEncoding = FDECFIEncoding =
781     TTypeEncoding = dwarf::DW_EH_PE_absptr;
782
783   CompactUnwindDwarfEHFrameOnly = 0;
784
785   EHFrameSection = nullptr;             // Created on demand.
786   CompactUnwindSection = nullptr;       // Used only by selected targets.
787   DwarfAccelNamesSection = nullptr;     // Used only by selected targets.
788   DwarfAccelObjCSection = nullptr;      // Used only by selected targets.
789   DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
790   DwarfAccelTypesSection = nullptr;     // Used only by selected targets.
791
792   Triple T(TT);
793   Triple::ArchType Arch = T.getArch();
794   // FIXME: Checking for Arch here to filter out bogus triples such as
795   // cellspu-apple-darwin. Perhaps we should fix in Triple?
796   if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
797        Arch == Triple::arm || Arch == Triple::thumb ||
798        Arch == Triple::arm64 ||
799        Arch == Triple::ppc || Arch == Triple::ppc64 ||
800        Arch == Triple::UnknownArch) &&
801       (T.isOSDarwin() || T.isOSBinFormatMachO())) {
802     Env = IsMachO;
803     InitMachOMCObjectFileInfo(T);
804   } else if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
805               Arch == Triple::arm || Arch == Triple::thumb) &&
806              (T.isOSWindows() && T.getObjectFormat() == Triple::COFF)) {
807     Env = IsCOFF;
808     InitCOFFMCObjectFileInfo(T);
809   } else {
810     Env = IsELF;
811     InitELFMCObjectFileInfo(T);
812   }
813 }
814
815 const MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const {
816   return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP,
817                             SectionKind::getMetadata(), 0, utostr(Hash));
818 }
819
820 const MCSection *
821 MCObjectFileInfo::getDwarfTypesDWOSection(uint64_t Hash) const {
822   return Ctx->getELFSection(".debug_types.dwo", ELF::SHT_PROGBITS,
823                             ELF::SHF_GROUP, SectionKind::getMetadata(), 0,
824                             utostr(Hash));
825 }
826
827 void MCObjectFileInfo::InitEHFrameSection() {
828   if (Env == IsMachO)
829     EHFrameSection =
830       Ctx->getMachOSection("__TEXT", "__eh_frame",
831                            MachO::S_COALESCED |
832                            MachO::S_ATTR_NO_TOC |
833                            MachO::S_ATTR_STRIP_STATIC_SYMS |
834                            MachO::S_ATTR_LIVE_SUPPORT,
835                            SectionKind::getReadOnly());
836   else if (Env == IsELF)
837     EHFrameSection =
838       Ctx->getELFSection(".eh_frame", EHSectionType,
839                          EHSectionFlags,
840                          SectionKind::getDataRel());
841   else
842     EHFrameSection =
843       Ctx->getCOFFSection(".eh_frame",
844                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
845                           COFF::IMAGE_SCN_MEM_READ |
846                           COFF::IMAGE_SCN_MEM_WRITE,
847                           SectionKind::getDataRel());
848 }