1 //===-- MCObjectFileInfo.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.isWatchABI())
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.isWatchABI())
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   COFFDebugTypesSection = nullptr;
195 
196   if (useCompactUnwind(T)) {
197     CompactUnwindSection =
198         Ctx->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG,
199                              SectionKind::getReadOnly());
200 
201     if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86)
202       CompactUnwindDwarfEHFrameOnly = 0x04000000;  // UNWIND_X86_64_MODE_DWARF
203     else if (T.getArch() == Triple::aarch64)
204       CompactUnwindDwarfEHFrameOnly = 0x03000000;  // UNWIND_ARM64_MODE_DWARF
205     else if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb)
206       CompactUnwindDwarfEHFrameOnly = 0x04000000;  // UNWIND_ARM_MODE_DWARF
207   }
208 
209   // Debug Information.
210   DwarfAccelNamesSection =
211       Ctx->getMachOSection("__DWARF", "__apple_names", MachO::S_ATTR_DEBUG,
212                            SectionKind::getMetadata(), "names_begin");
213   DwarfAccelObjCSection =
214       Ctx->getMachOSection("__DWARF", "__apple_objc", MachO::S_ATTR_DEBUG,
215                            SectionKind::getMetadata(), "objc_begin");
216   // 16 character section limit...
217   DwarfAccelNamespaceSection =
218       Ctx->getMachOSection("__DWARF", "__apple_namespac", MachO::S_ATTR_DEBUG,
219                            SectionKind::getMetadata(), "namespac_begin");
220   DwarfAccelTypesSection =
221       Ctx->getMachOSection("__DWARF", "__apple_types", MachO::S_ATTR_DEBUG,
222                            SectionKind::getMetadata(), "types_begin");
223 
224   DwarfAbbrevSection =
225       Ctx->getMachOSection("__DWARF", "__debug_abbrev", MachO::S_ATTR_DEBUG,
226                            SectionKind::getMetadata(), "section_abbrev");
227   DwarfInfoSection =
228       Ctx->getMachOSection("__DWARF", "__debug_info", MachO::S_ATTR_DEBUG,
229                            SectionKind::getMetadata(), "section_info");
230   DwarfLineSection =
231       Ctx->getMachOSection("__DWARF", "__debug_line", MachO::S_ATTR_DEBUG,
232                            SectionKind::getMetadata(), "section_line");
233   DwarfFrameSection =
234       Ctx->getMachOSection("__DWARF", "__debug_frame", MachO::S_ATTR_DEBUG,
235                            SectionKind::getMetadata());
236   DwarfPubNamesSection =
237       Ctx->getMachOSection("__DWARF", "__debug_pubnames", MachO::S_ATTR_DEBUG,
238                            SectionKind::getMetadata());
239   DwarfPubTypesSection =
240       Ctx->getMachOSection("__DWARF", "__debug_pubtypes", MachO::S_ATTR_DEBUG,
241                            SectionKind::getMetadata());
242   DwarfGnuPubNamesSection =
243       Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn", MachO::S_ATTR_DEBUG,
244                            SectionKind::getMetadata());
245   DwarfGnuPubTypesSection =
246       Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt", MachO::S_ATTR_DEBUG,
247                            SectionKind::getMetadata());
248   DwarfStrSection =
249       Ctx->getMachOSection("__DWARF", "__debug_str", MachO::S_ATTR_DEBUG,
250                            SectionKind::getMetadata(), "info_string");
251   DwarfLocSection =
252       Ctx->getMachOSection("__DWARF", "__debug_loc", MachO::S_ATTR_DEBUG,
253                            SectionKind::getMetadata(), "section_debug_loc");
254   DwarfARangesSection =
255       Ctx->getMachOSection("__DWARF", "__debug_aranges", MachO::S_ATTR_DEBUG,
256                            SectionKind::getMetadata());
257   DwarfRangesSection =
258       Ctx->getMachOSection("__DWARF", "__debug_ranges", MachO::S_ATTR_DEBUG,
259                            SectionKind::getMetadata(), "debug_range");
260   DwarfMacinfoSection =
261       Ctx->getMachOSection("__DWARF", "__debug_macinfo", MachO::S_ATTR_DEBUG,
262                            SectionKind::getMetadata(), "debug_macinfo");
263   DwarfDebugInlineSection =
264       Ctx->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG,
265                            SectionKind::getMetadata());
266   DwarfCUIndexSection =
267       Ctx->getMachOSection("__DWARF", "__debug_cu_index", MachO::S_ATTR_DEBUG,
268                            SectionKind::getMetadata());
269   DwarfTUIndexSection =
270       Ctx->getMachOSection("__DWARF", "__debug_tu_index", MachO::S_ATTR_DEBUG,
271                            SectionKind::getMetadata());
272   StackMapSection = Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps",
273                                          0, SectionKind::getMetadata());
274 
275   FaultMapSection = Ctx->getMachOSection("__LLVM_FAULTMAPS", "__llvm_faultmaps",
276                                          0, SectionKind::getMetadata());
277 
278   TLSExtraDataSection = TLSTLVSection;
279 }
280 
281 void MCObjectFileInfo::initELFMCObjectFileInfo(Triple T) {
282   switch (T.getArch()) {
283   case Triple::mips:
284   case Triple::mipsel:
285     FDECFIEncoding = dwarf::DW_EH_PE_sdata4;
286     break;
287   case Triple::mips64:
288   case Triple::mips64el:
289     FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
290     break;
291   case Triple::x86_64:
292     FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
293                      ((CMModel == CodeModel::Large) ? dwarf::DW_EH_PE_sdata8
294                                                     : dwarf::DW_EH_PE_sdata4);
295     break;
296   default:
297     FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
298     break;
299   }
300 
301   switch (T.getArch()) {
302   case Triple::arm:
303   case Triple::armeb:
304   case Triple::thumb:
305   case Triple::thumbeb:
306     if (Ctx->getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
307       break;
308     // Fallthrough if not using EHABI
309   case Triple::ppc:
310   case Triple::x86:
311     PersonalityEncoding = (RelocM == Reloc::PIC_)
312      ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
313      : dwarf::DW_EH_PE_absptr;
314     LSDAEncoding = (RelocM == Reloc::PIC_)
315       ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
316       : dwarf::DW_EH_PE_absptr;
317     TTypeEncoding = (RelocM == Reloc::PIC_)
318      ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
319      : dwarf::DW_EH_PE_absptr;
320     break;
321   case Triple::x86_64:
322     if (RelocM == Reloc::PIC_) {
323       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
324         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
325          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
326       LSDAEncoding = dwarf::DW_EH_PE_pcrel |
327         (CMModel == CodeModel::Small
328          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
329       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
330         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
331          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
332     } else {
333       PersonalityEncoding =
334         (CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
335         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
336       LSDAEncoding = (CMModel == CodeModel::Small)
337         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
338       TTypeEncoding = (CMModel == CodeModel::Small)
339         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
340     }
341     break;
342   case Triple::aarch64:
343   case Triple::aarch64_be:
344     // The small model guarantees static code/data size < 4GB, but not where it
345     // will be in memory. Most of these could end up >2GB away so even a signed
346     // pc-relative 32-bit address is insufficient, theoretically.
347     if (RelocM == Reloc::PIC_) {
348       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
349         dwarf::DW_EH_PE_sdata8;
350       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
351       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
352         dwarf::DW_EH_PE_sdata8;
353     } else {
354       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
355       LSDAEncoding = dwarf::DW_EH_PE_absptr;
356       TTypeEncoding = dwarf::DW_EH_PE_absptr;
357     }
358     break;
359   case Triple::lanai:
360     LSDAEncoding = dwarf::DW_EH_PE_absptr;
361     PersonalityEncoding = dwarf::DW_EH_PE_absptr;
362     TTypeEncoding = dwarf::DW_EH_PE_absptr;
363     break;
364   case Triple::mips:
365   case Triple::mipsel:
366   case Triple::mips64:
367   case Triple::mips64el:
368     // MIPS uses indirect pointer to refer personality functions and types, so
369     // that the eh_frame section can be read-only. DW.ref.personality will be
370     // generated for relocation.
371     PersonalityEncoding = dwarf::DW_EH_PE_indirect;
372     // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
373     //        identify N64 from just a triple.
374     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
375                     dwarf::DW_EH_PE_sdata4;
376     // We don't support PC-relative LSDA references in GAS so we use the default
377     // DW_EH_PE_absptr for those.
378     break;
379   case Triple::ppc64:
380   case Triple::ppc64le:
381     PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
382       dwarf::DW_EH_PE_udata8;
383     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
384     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
385       dwarf::DW_EH_PE_udata8;
386     break;
387   case Triple::sparcel:
388   case Triple::sparc:
389     if (RelocM == Reloc::PIC_) {
390       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
391       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
392         dwarf::DW_EH_PE_sdata4;
393       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
394         dwarf::DW_EH_PE_sdata4;
395     } else {
396       LSDAEncoding = dwarf::DW_EH_PE_absptr;
397       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
398       TTypeEncoding = dwarf::DW_EH_PE_absptr;
399     }
400     break;
401   case Triple::sparcv9:
402     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
403     if (RelocM == Reloc::PIC_) {
404       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
405         dwarf::DW_EH_PE_sdata4;
406       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
407         dwarf::DW_EH_PE_sdata4;
408     } else {
409       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
410       TTypeEncoding = dwarf::DW_EH_PE_absptr;
411     }
412     break;
413   case Triple::systemz:
414     // All currently-defined code models guarantee that 4-byte PC-relative
415     // values will be in range.
416     if (RelocM == Reloc::PIC_) {
417       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
418         dwarf::DW_EH_PE_sdata4;
419       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
420       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
421         dwarf::DW_EH_PE_sdata4;
422     } else {
423       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
424       LSDAEncoding = dwarf::DW_EH_PE_absptr;
425       TTypeEncoding = dwarf::DW_EH_PE_absptr;
426     }
427     break;
428   default:
429     break;
430   }
431 
432   unsigned EHSectionType = T.getArch() == Triple::x86_64
433                                ? ELF::SHT_X86_64_UNWIND
434                                : ELF::SHT_PROGBITS;
435 
436   // Solaris requires different flags for .eh_frame to seemingly every other
437   // platform.
438   unsigned EHSectionFlags = ELF::SHF_ALLOC;
439   if (T.isOSSolaris() && T.getArch() != Triple::x86_64)
440     EHSectionFlags |= ELF::SHF_WRITE;
441 
442   // ELF
443   BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
444                                   ELF::SHF_WRITE | ELF::SHF_ALLOC);
445 
446   TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
447                                    ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
448 
449   DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
450                                    ELF::SHF_WRITE | ELF::SHF_ALLOC);
451 
452   ReadOnlySection =
453       Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
454 
455   TLSDataSection =
456       Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
457                          ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
458 
459   TLSBSSSection = Ctx->getELFSection(
460       ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
461 
462   DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
463                                         ELF::SHF_ALLOC | ELF::SHF_WRITE);
464 
465   MergeableConst4Section =
466       Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
467                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 4, "");
468 
469   MergeableConst8Section =
470       Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
471                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 8, "");
472 
473   MergeableConst16Section =
474       Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
475                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 16, "");
476 
477   MergeableConst32Section =
478       Ctx->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS,
479                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 32, "");
480 
481   StaticCtorSection = Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS,
482                                          ELF::SHF_ALLOC | ELF::SHF_WRITE);
483 
484   StaticDtorSection = Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS,
485                                          ELF::SHF_ALLOC | ELF::SHF_WRITE);
486 
487   // Exception Handling Sections.
488 
489   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
490   // it contains relocatable pointers.  In PIC mode, this is probably a big
491   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
492   // adjusted or this should be a data section.
493   LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
494                                    ELF::SHF_ALLOC);
495 
496   COFFDebugSymbolsSection = nullptr;
497   COFFDebugTypesSection = nullptr;
498 
499   // Debug Info Sections.
500   DwarfAbbrevSection = Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
501                                           "section_abbrev");
502   DwarfInfoSection =
503       Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0, "section_info");
504   DwarfLineSection = Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0);
505   DwarfFrameSection = Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0);
506   DwarfPubNamesSection =
507       Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0);
508   DwarfPubTypesSection =
509       Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0);
510   DwarfGnuPubNamesSection =
511       Ctx->getELFSection(".debug_gnu_pubnames", ELF::SHT_PROGBITS, 0);
512   DwarfGnuPubTypesSection =
513       Ctx->getELFSection(".debug_gnu_pubtypes", ELF::SHT_PROGBITS, 0);
514   DwarfStrSection =
515       Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS,
516                          ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
517   DwarfLocSection = Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0);
518   DwarfARangesSection =
519       Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0);
520   DwarfRangesSection =
521       Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0, "debug_range");
522   DwarfMacinfoSection = Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS,
523                                            0, "debug_macinfo");
524 
525   // DWARF5 Experimental Debug Info
526 
527   // Accelerator Tables
528   DwarfAccelNamesSection =
529       Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0, "names_begin");
530   DwarfAccelObjCSection =
531       Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0, "objc_begin");
532   DwarfAccelNamespaceSection = Ctx->getELFSection(
533       ".apple_namespaces", ELF::SHT_PROGBITS, 0, "namespac_begin");
534   DwarfAccelTypesSection =
535       Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0, "types_begin");
536 
537   // Fission Sections
538   DwarfInfoDWOSection =
539       Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0);
540   DwarfTypesDWOSection =
541       Ctx->getELFSection(".debug_types.dwo", ELF::SHT_PROGBITS, 0);
542   DwarfAbbrevDWOSection =
543       Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0);
544   DwarfStrDWOSection =
545       Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS,
546                          ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
547   DwarfLineDWOSection =
548       Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0);
549   DwarfLocDWOSection =
550       Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0, "skel_loc");
551   DwarfStrOffDWOSection =
552       Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0);
553   DwarfAddrSection =
554       Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0, "addr_sec");
555 
556   // DWP Sections
557   DwarfCUIndexSection =
558       Ctx->getELFSection(".debug_cu_index", ELF::SHT_PROGBITS, 0);
559   DwarfTUIndexSection =
560       Ctx->getELFSection(".debug_tu_index", ELF::SHT_PROGBITS, 0);
561 
562   StackMapSection =
563       Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
564 
565   FaultMapSection =
566       Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
567 
568   EHFrameSection =
569       Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);
570 }
571 
572 void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) {
573   EHFrameSection = Ctx->getCOFFSection(
574       ".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
575                        COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
576       SectionKind::getData());
577 
578   bool IsWoA = T.getArch() == Triple::arm || T.getArch() == Triple::thumb;
579 
580   CommDirectiveSupportsAlignment = true;
581 
582   // COFF
583   BSSSection = Ctx->getCOFFSection(
584       ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
585                   COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
586       SectionKind::getBSS());
587   TextSection = Ctx->getCOFFSection(
588       ".text",
589       (IsWoA ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) |
590           COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
591           COFF::IMAGE_SCN_MEM_READ,
592       SectionKind::getText());
593   DataSection = Ctx->getCOFFSection(
594       ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
595                    COFF::IMAGE_SCN_MEM_WRITE,
596       SectionKind::getData());
597   ReadOnlySection = Ctx->getCOFFSection(
598       ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
599       SectionKind::getReadOnly());
600 
601   if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
602     StaticCtorSection =
603         Ctx->getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
604                                             COFF::IMAGE_SCN_MEM_READ,
605                             SectionKind::getReadOnly());
606     StaticDtorSection =
607         Ctx->getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
608                                             COFF::IMAGE_SCN_MEM_READ,
609                             SectionKind::getReadOnly());
610   } else {
611     StaticCtorSection = Ctx->getCOFFSection(
612         ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
613                       COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
614         SectionKind::getData());
615     StaticDtorSection = Ctx->getCOFFSection(
616         ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
617                       COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
618         SectionKind::getData());
619   }
620 
621   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
622   // though it contains relocatable pointers.  In PIC mode, this is probably a
623   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
624   // adjusted or this should be a data section.
625   if (T.getArch() == Triple::x86_64) {
626     // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
627     LSDASection = nullptr;
628   } else {
629     LSDASection = Ctx->getCOFFSection(".gcc_except_table",
630                                       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
631                                           COFF::IMAGE_SCN_MEM_READ,
632                                       SectionKind::getReadOnly());
633   }
634 
635   // Debug info.
636   COFFDebugSymbolsSection =
637       Ctx->getCOFFSection(".debug$S", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
638                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
639                                        COFF::IMAGE_SCN_MEM_READ),
640                           SectionKind::getMetadata());
641   COFFDebugTypesSection =
642       Ctx->getCOFFSection(".debug$T", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
643                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
644                                        COFF::IMAGE_SCN_MEM_READ),
645                           SectionKind::getMetadata());
646 
647   DwarfAbbrevSection = Ctx->getCOFFSection(
648       ".debug_abbrev",
649       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
650           COFF::IMAGE_SCN_MEM_READ,
651       SectionKind::getMetadata(), "section_abbrev");
652   DwarfInfoSection = Ctx->getCOFFSection(
653       ".debug_info",
654       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
655           COFF::IMAGE_SCN_MEM_READ,
656       SectionKind::getMetadata(), "section_info");
657   DwarfLineSection = Ctx->getCOFFSection(
658       ".debug_line",
659       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
660           COFF::IMAGE_SCN_MEM_READ,
661       SectionKind::getMetadata(), "section_line");
662 
663   DwarfFrameSection = Ctx->getCOFFSection(
664       ".debug_frame",
665       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
666           COFF::IMAGE_SCN_MEM_READ,
667       SectionKind::getMetadata());
668   DwarfPubNamesSection = Ctx->getCOFFSection(
669       ".debug_pubnames",
670       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
671           COFF::IMAGE_SCN_MEM_READ,
672       SectionKind::getMetadata());
673   DwarfPubTypesSection = Ctx->getCOFFSection(
674       ".debug_pubtypes",
675       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
676           COFF::IMAGE_SCN_MEM_READ,
677       SectionKind::getMetadata());
678   DwarfGnuPubNamesSection = Ctx->getCOFFSection(
679       ".debug_gnu_pubnames",
680       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
681           COFF::IMAGE_SCN_MEM_READ,
682       SectionKind::getMetadata());
683   DwarfGnuPubTypesSection = Ctx->getCOFFSection(
684       ".debug_gnu_pubtypes",
685       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
686           COFF::IMAGE_SCN_MEM_READ,
687       SectionKind::getMetadata());
688   DwarfStrSection = Ctx->getCOFFSection(
689       ".debug_str",
690       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
691           COFF::IMAGE_SCN_MEM_READ,
692       SectionKind::getMetadata(), "info_string");
693   DwarfLocSection = Ctx->getCOFFSection(
694       ".debug_loc",
695       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
696           COFF::IMAGE_SCN_MEM_READ,
697       SectionKind::getMetadata(), "section_debug_loc");
698   DwarfARangesSection = Ctx->getCOFFSection(
699       ".debug_aranges",
700       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
701           COFF::IMAGE_SCN_MEM_READ,
702       SectionKind::getMetadata());
703   DwarfRangesSection = Ctx->getCOFFSection(
704       ".debug_ranges",
705       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
706           COFF::IMAGE_SCN_MEM_READ,
707       SectionKind::getMetadata(), "debug_range");
708   DwarfMacinfoSection = Ctx->getCOFFSection(
709       ".debug_macinfo",
710       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
711           COFF::IMAGE_SCN_MEM_READ,
712       SectionKind::getMetadata(), "debug_macinfo");
713   DwarfInfoDWOSection = Ctx->getCOFFSection(
714       ".debug_info.dwo",
715       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
716           COFF::IMAGE_SCN_MEM_READ,
717       SectionKind::getMetadata(), "section_info_dwo");
718   DwarfTypesDWOSection = Ctx->getCOFFSection(
719       ".debug_types.dwo",
720       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
721           COFF::IMAGE_SCN_MEM_READ,
722       SectionKind::getMetadata(), "section_types_dwo");
723   DwarfAbbrevDWOSection = Ctx->getCOFFSection(
724       ".debug_abbrev.dwo",
725       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
726           COFF::IMAGE_SCN_MEM_READ,
727       SectionKind::getMetadata(), "section_abbrev_dwo");
728   DwarfStrDWOSection = Ctx->getCOFFSection(
729       ".debug_str.dwo",
730       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
731           COFF::IMAGE_SCN_MEM_READ,
732       SectionKind::getMetadata(), "skel_string");
733   DwarfLineDWOSection = Ctx->getCOFFSection(
734       ".debug_line.dwo",
735       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
736           COFF::IMAGE_SCN_MEM_READ,
737       SectionKind::getMetadata());
738   DwarfLocDWOSection = Ctx->getCOFFSection(
739       ".debug_loc.dwo",
740       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
741           COFF::IMAGE_SCN_MEM_READ,
742       SectionKind::getMetadata(), "skel_loc");
743   DwarfStrOffDWOSection = Ctx->getCOFFSection(
744       ".debug_str_offsets.dwo",
745       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
746           COFF::IMAGE_SCN_MEM_READ,
747       SectionKind::getMetadata());
748   DwarfAddrSection = Ctx->getCOFFSection(
749       ".debug_addr",
750       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
751           COFF::IMAGE_SCN_MEM_READ,
752       SectionKind::getMetadata(), "addr_sec");
753   DwarfCUIndexSection = Ctx->getCOFFSection(
754       ".debug_cu_index",
755       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
756           COFF::IMAGE_SCN_MEM_READ,
757       SectionKind::getMetadata());
758   DwarfTUIndexSection = Ctx->getCOFFSection(
759       ".debug_tu_index",
760       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
761           COFF::IMAGE_SCN_MEM_READ,
762       SectionKind::getMetadata());
763   DwarfAccelNamesSection = Ctx->getCOFFSection(
764       ".apple_names",
765       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
766           COFF::IMAGE_SCN_MEM_READ,
767       SectionKind::getMetadata(), "names_begin");
768   DwarfAccelNamespaceSection = Ctx->getCOFFSection(
769       ".apple_namespaces",
770       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
771           COFF::IMAGE_SCN_MEM_READ,
772       SectionKind::getMetadata(), "namespac_begin");
773   DwarfAccelTypesSection = Ctx->getCOFFSection(
774       ".apple_types",
775       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
776           COFF::IMAGE_SCN_MEM_READ,
777       SectionKind::getMetadata(), "types_begin");
778   DwarfAccelObjCSection = Ctx->getCOFFSection(
779       ".apple_objc",
780       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
781           COFF::IMAGE_SCN_MEM_READ,
782       SectionKind::getMetadata(), "objc_begin");
783 
784   DrectveSection = Ctx->getCOFFSection(
785       ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE,
786       SectionKind::getMetadata());
787 
788   PDataSection = Ctx->getCOFFSection(
789       ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
790       SectionKind::getData());
791 
792   XDataSection = Ctx->getCOFFSection(
793       ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
794       SectionKind::getData());
795 
796   SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO,
797                                       SectionKind::getMetadata());
798 
799   TLSDataSection = Ctx->getCOFFSection(
800       ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
801                    COFF::IMAGE_SCN_MEM_WRITE,
802       SectionKind::getData());
803 
804   StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps",
805                                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
806                                             COFF::IMAGE_SCN_MEM_READ,
807                                         SectionKind::getReadOnly());
808 }
809 
810 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple,
811                                             Reloc::Model relocm,
812                                             CodeModel::Model cm,
813                                             MCContext &ctx) {
814   RelocM = relocm;
815   CMModel = cm;
816   Ctx = &ctx;
817 
818   // Common.
819   CommDirectiveSupportsAlignment = true;
820   SupportsWeakOmittedEHFrame = true;
821   SupportsCompactUnwindWithoutEHFrame = false;
822   OmitDwarfIfHaveCompactUnwind = false;
823 
824   PersonalityEncoding = LSDAEncoding = FDECFIEncoding = TTypeEncoding =
825       dwarf::DW_EH_PE_absptr;
826 
827   CompactUnwindDwarfEHFrameOnly = 0;
828 
829   EHFrameSection = nullptr;             // Created on demand.
830   CompactUnwindSection = nullptr;       // Used only by selected targets.
831   DwarfAccelNamesSection = nullptr;     // Used only by selected targets.
832   DwarfAccelObjCSection = nullptr;      // Used only by selected targets.
833   DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
834   DwarfAccelTypesSection = nullptr;     // Used only by selected targets.
835 
836   TT = TheTriple;
837 
838   switch (TT.getObjectFormat()) {
839   case Triple::MachO:
840     Env = IsMachO;
841     initMachOMCObjectFileInfo(TT);
842     break;
843   case Triple::COFF:
844     if (!TT.isOSWindows())
845       report_fatal_error(
846           "Cannot initialize MC for non-Windows COFF object files.");
847 
848     Env = IsCOFF;
849     initCOFFMCObjectFileInfo(TT);
850     break;
851   case Triple::ELF:
852     Env = IsELF;
853     initELFMCObjectFileInfo(TT);
854     break;
855   case Triple::UnknownObjectFormat:
856     report_fatal_error("Cannot initialize MC for unknown object file format.");
857     break;
858   }
859 }
860 
861 void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model RM,
862                                             CodeModel::Model CM,
863                                             MCContext &ctx) {
864   InitMCObjectFileInfo(Triple(TT), RM, CM, ctx);
865 }
866 
867 MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const {
868   return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP,
869                             0, utostr(Hash));
870 }
871