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