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