1 //===-- MCObjectFileInfo.cpp - Object File Information --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCObjectFileInfo.h"
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/BinaryFormat/COFF.h"
12 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/BinaryFormat/Wasm.h"
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCSection.h"
17 #include "llvm/MC/MCSectionCOFF.h"
18 #include "llvm/MC/MCSectionDXContainer.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/MC/MCSectionGOFF.h"
21 #include "llvm/MC/MCSectionMachO.h"
22 #include "llvm/MC/MCSectionSPIRV.h"
23 #include "llvm/MC/MCSectionWasm.h"
24 #include "llvm/MC/MCSectionXCOFF.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/TargetParser/Triple.h"
27 
28 using namespace llvm;
29 
30 static bool useCompactUnwind(const Triple &T) {
31   // Only on darwin.
32   if (!T.isOSDarwin())
33     return false;
34 
35   // aarch64 always has it.
36   if (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32)
37     return true;
38 
39   // armv7k always has it.
40   if (T.isWatchABI())
41     return true;
42 
43   // Use it on newer version of OS X.
44   if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6))
45     return true;
46 
47   // And the iOS simulator.
48   if (T.isiOS() && T.isX86())
49     return true;
50 
51   // The rest of the simulators always have it.
52   if (T.isSimulatorEnvironment())
53     return true;
54 
55   return false;
56 }
57 
58 void MCObjectFileInfo::initMachOMCObjectFileInfo(const Triple &T) {
59   // MachO
60   SupportsWeakOmittedEHFrame = false;
61 
62   EHFrameSection = Ctx->getMachOSection(
63       "__TEXT", "__eh_frame",
64       MachO::S_COALESCED | MachO::S_ATTR_NO_TOC |
65           MachO::S_ATTR_STRIP_STATIC_SYMS | MachO::S_ATTR_LIVE_SUPPORT,
66       SectionKind::getReadOnly());
67 
68   if (T.isOSDarwin() &&
69       (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32 ||
70       T.isSimulatorEnvironment()))
71     SupportsCompactUnwindWithoutEHFrame = true;
72 
73   switch (Ctx->emitDwarfUnwindInfo()) {
74   case EmitDwarfUnwindType::Always:
75     OmitDwarfIfHaveCompactUnwind = false;
76     break;
77   case EmitDwarfUnwindType::NoCompactUnwind:
78     OmitDwarfIfHaveCompactUnwind = true;
79     break;
80   case EmitDwarfUnwindType::Default:
81     OmitDwarfIfHaveCompactUnwind =
82         T.isWatchABI() || SupportsCompactUnwindWithoutEHFrame;
83     break;
84   }
85 
86   FDECFIEncoding = dwarf::DW_EH_PE_pcrel;
87 
88   TextSection // .text
89     = Ctx->getMachOSection("__TEXT", "__text",
90                            MachO::S_ATTR_PURE_INSTRUCTIONS,
91                            SectionKind::getText());
92   DataSection // .data
93       = Ctx->getMachOSection("__DATA", "__data", 0, SectionKind::getData());
94 
95   // BSSSection might not be expected initialized on msvc.
96   BSSSection = nullptr;
97 
98   TLSDataSection // .tdata
99       = Ctx->getMachOSection("__DATA", "__thread_data",
100                              MachO::S_THREAD_LOCAL_REGULAR,
101                              SectionKind::getData());
102   TLSBSSSection // .tbss
103     = Ctx->getMachOSection("__DATA", "__thread_bss",
104                            MachO::S_THREAD_LOCAL_ZEROFILL,
105                            SectionKind::getThreadBSS());
106 
107   // TODO: Verify datarel below.
108   TLSTLVSection // .tlv
109       = Ctx->getMachOSection("__DATA", "__thread_vars",
110                              MachO::S_THREAD_LOCAL_VARIABLES,
111                              SectionKind::getData());
112 
113   TLSThreadInitSection = Ctx->getMachOSection(
114       "__DATA", "__thread_init", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
115       SectionKind::getData());
116 
117   CStringSection // .cstring
118     = Ctx->getMachOSection("__TEXT", "__cstring",
119                            MachO::S_CSTRING_LITERALS,
120                            SectionKind::getMergeable1ByteCString());
121   UStringSection
122     = Ctx->getMachOSection("__TEXT","__ustring", 0,
123                            SectionKind::getMergeable2ByteCString());
124   FourByteConstantSection // .literal4
125     = Ctx->getMachOSection("__TEXT", "__literal4",
126                            MachO::S_4BYTE_LITERALS,
127                            SectionKind::getMergeableConst4());
128   EightByteConstantSection // .literal8
129     = Ctx->getMachOSection("__TEXT", "__literal8",
130                            MachO::S_8BYTE_LITERALS,
131                            SectionKind::getMergeableConst8());
132 
133   SixteenByteConstantSection // .literal16
134       = Ctx->getMachOSection("__TEXT", "__literal16",
135                              MachO::S_16BYTE_LITERALS,
136                              SectionKind::getMergeableConst16());
137 
138   ReadOnlySection  // .const
139     = Ctx->getMachOSection("__TEXT", "__const", 0,
140                            SectionKind::getReadOnly());
141 
142   // If the target is not powerpc, map the coal sections to the non-coal
143   // sections.
144   //
145   // "__TEXT/__textcoal_nt" => section "__TEXT/__text"
146   // "__TEXT/__const_coal"  => section "__TEXT/__const"
147   // "__DATA/__datacoal_nt" => section "__DATA/__data"
148   Triple::ArchType ArchTy = T.getArch();
149 
150   ConstDataSection  // .const_data
151     = Ctx->getMachOSection("__DATA", "__const", 0,
152                            SectionKind::getReadOnlyWithRel());
153 
154   if (ArchTy == Triple::ppc || ArchTy == Triple::ppc64) {
155     TextCoalSection
156       = Ctx->getMachOSection("__TEXT", "__textcoal_nt",
157                              MachO::S_COALESCED |
158                              MachO::S_ATTR_PURE_INSTRUCTIONS,
159                              SectionKind::getText());
160     ConstTextCoalSection
161       = Ctx->getMachOSection("__TEXT", "__const_coal",
162                              MachO::S_COALESCED,
163                              SectionKind::getReadOnly());
164     DataCoalSection = Ctx->getMachOSection(
165         "__DATA", "__datacoal_nt", MachO::S_COALESCED, SectionKind::getData());
166     ConstDataCoalSection = DataCoalSection;
167   } else {
168     TextCoalSection = TextSection;
169     ConstTextCoalSection = ReadOnlySection;
170     DataCoalSection = DataSection;
171     ConstDataCoalSection = ConstDataSection;
172   }
173 
174   DataCommonSection
175     = Ctx->getMachOSection("__DATA","__common",
176                            MachO::S_ZEROFILL,
177                            SectionKind::getBSS());
178   DataBSSSection
179     = Ctx->getMachOSection("__DATA","__bss", MachO::S_ZEROFILL,
180                            SectionKind::getBSS());
181 
182 
183   LazySymbolPointerSection
184     = Ctx->getMachOSection("__DATA", "__la_symbol_ptr",
185                            MachO::S_LAZY_SYMBOL_POINTERS,
186                            SectionKind::getMetadata());
187   NonLazySymbolPointerSection
188     = Ctx->getMachOSection("__DATA", "__nl_symbol_ptr",
189                            MachO::S_NON_LAZY_SYMBOL_POINTERS,
190                            SectionKind::getMetadata());
191 
192   ThreadLocalPointerSection
193     = Ctx->getMachOSection("__DATA", "__thread_ptr",
194                            MachO::S_THREAD_LOCAL_VARIABLE_POINTERS,
195                            SectionKind::getMetadata());
196 
197   AddrSigSection = Ctx->getMachOSection("__DATA", "__llvm_addrsig", 0,
198                                         SectionKind::getData());
199 
200   // Exception Handling.
201   LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0,
202                                      SectionKind::getReadOnlyWithRel());
203 
204   COFFDebugSymbolsSection = nullptr;
205   COFFDebugTypesSection = nullptr;
206   COFFGlobalTypeHashesSection = nullptr;
207 
208   if (useCompactUnwind(T)) {
209     CompactUnwindSection =
210         Ctx->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG,
211                              SectionKind::getReadOnly());
212 
213     if (T.isX86())
214       CompactUnwindDwarfEHFrameOnly = 0x04000000;  // UNWIND_X86_64_MODE_DWARF
215     else if (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32)
216       CompactUnwindDwarfEHFrameOnly = 0x03000000;  // UNWIND_ARM64_MODE_DWARF
217     else if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb)
218       CompactUnwindDwarfEHFrameOnly = 0x04000000;  // UNWIND_ARM_MODE_DWARF
219   }
220 
221   // Debug Information.
222   DwarfDebugNamesSection =
223       Ctx->getMachOSection("__DWARF", "__debug_names", MachO::S_ATTR_DEBUG,
224                            SectionKind::getMetadata(), "debug_names_begin");
225   DwarfAccelNamesSection =
226       Ctx->getMachOSection("__DWARF", "__apple_names", MachO::S_ATTR_DEBUG,
227                            SectionKind::getMetadata(), "names_begin");
228   DwarfAccelObjCSection =
229       Ctx->getMachOSection("__DWARF", "__apple_objc", MachO::S_ATTR_DEBUG,
230                            SectionKind::getMetadata(), "objc_begin");
231   // 16 character section limit...
232   DwarfAccelNamespaceSection =
233       Ctx->getMachOSection("__DWARF", "__apple_namespac", MachO::S_ATTR_DEBUG,
234                            SectionKind::getMetadata(), "namespac_begin");
235   DwarfAccelTypesSection =
236       Ctx->getMachOSection("__DWARF", "__apple_types", MachO::S_ATTR_DEBUG,
237                            SectionKind::getMetadata(), "types_begin");
238 
239   DwarfSwiftASTSection =
240       Ctx->getMachOSection("__DWARF", "__swift_ast", MachO::S_ATTR_DEBUG,
241                            SectionKind::getMetadata());
242 
243   DwarfAbbrevSection =
244       Ctx->getMachOSection("__DWARF", "__debug_abbrev", MachO::S_ATTR_DEBUG,
245                            SectionKind::getMetadata(), "section_abbrev");
246   DwarfInfoSection =
247       Ctx->getMachOSection("__DWARF", "__debug_info", MachO::S_ATTR_DEBUG,
248                            SectionKind::getMetadata(), "section_info");
249   DwarfLineSection =
250       Ctx->getMachOSection("__DWARF", "__debug_line", MachO::S_ATTR_DEBUG,
251                            SectionKind::getMetadata(), "section_line");
252   DwarfLineStrSection =
253       Ctx->getMachOSection("__DWARF", "__debug_line_str", MachO::S_ATTR_DEBUG,
254                            SectionKind::getMetadata(), "section_line_str");
255   DwarfFrameSection =
256       Ctx->getMachOSection("__DWARF", "__debug_frame", MachO::S_ATTR_DEBUG,
257                            SectionKind::getMetadata(), "section_frame");
258   DwarfPubNamesSection =
259       Ctx->getMachOSection("__DWARF", "__debug_pubnames", MachO::S_ATTR_DEBUG,
260                            SectionKind::getMetadata());
261   DwarfPubTypesSection =
262       Ctx->getMachOSection("__DWARF", "__debug_pubtypes", MachO::S_ATTR_DEBUG,
263                            SectionKind::getMetadata());
264   DwarfGnuPubNamesSection =
265       Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn", MachO::S_ATTR_DEBUG,
266                            SectionKind::getMetadata());
267   DwarfGnuPubTypesSection =
268       Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt", MachO::S_ATTR_DEBUG,
269                            SectionKind::getMetadata());
270   DwarfStrSection =
271       Ctx->getMachOSection("__DWARF", "__debug_str", MachO::S_ATTR_DEBUG,
272                            SectionKind::getMetadata(), "info_string");
273   DwarfStrOffSection =
274       Ctx->getMachOSection("__DWARF", "__debug_str_offs", MachO::S_ATTR_DEBUG,
275                            SectionKind::getMetadata(), "section_str_off");
276   DwarfAddrSection =
277       Ctx->getMachOSection("__DWARF", "__debug_addr", MachO::S_ATTR_DEBUG,
278                            SectionKind::getMetadata(), "section_info");
279   DwarfLocSection =
280       Ctx->getMachOSection("__DWARF", "__debug_loc", MachO::S_ATTR_DEBUG,
281                            SectionKind::getMetadata(), "section_debug_loc");
282   DwarfLoclistsSection =
283       Ctx->getMachOSection("__DWARF", "__debug_loclists", MachO::S_ATTR_DEBUG,
284                            SectionKind::getMetadata(), "section_debug_loc");
285 
286   DwarfARangesSection =
287       Ctx->getMachOSection("__DWARF", "__debug_aranges", MachO::S_ATTR_DEBUG,
288                            SectionKind::getMetadata());
289   DwarfRangesSection =
290       Ctx->getMachOSection("__DWARF", "__debug_ranges", MachO::S_ATTR_DEBUG,
291                            SectionKind::getMetadata(), "debug_range");
292   DwarfRnglistsSection =
293       Ctx->getMachOSection("__DWARF", "__debug_rnglists", MachO::S_ATTR_DEBUG,
294                            SectionKind::getMetadata(), "debug_range");
295   DwarfMacinfoSection =
296       Ctx->getMachOSection("__DWARF", "__debug_macinfo", MachO::S_ATTR_DEBUG,
297                            SectionKind::getMetadata(), "debug_macinfo");
298   DwarfMacroSection =
299       Ctx->getMachOSection("__DWARF", "__debug_macro", MachO::S_ATTR_DEBUG,
300                            SectionKind::getMetadata(), "debug_macro");
301   DwarfDebugInlineSection =
302       Ctx->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG,
303                            SectionKind::getMetadata());
304   DwarfCUIndexSection =
305       Ctx->getMachOSection("__DWARF", "__debug_cu_index", MachO::S_ATTR_DEBUG,
306                            SectionKind::getMetadata());
307   DwarfTUIndexSection =
308       Ctx->getMachOSection("__DWARF", "__debug_tu_index", MachO::S_ATTR_DEBUG,
309                            SectionKind::getMetadata());
310   StackMapSection = Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps",
311                                          0, SectionKind::getMetadata());
312 
313   FaultMapSection = Ctx->getMachOSection("__LLVM_FAULTMAPS", "__llvm_faultmaps",
314                                          0, SectionKind::getMetadata());
315 
316   RemarksSection = Ctx->getMachOSection(
317       "__LLVM", "__remarks", MachO::S_ATTR_DEBUG, SectionKind::getMetadata());
318 
319   // The architecture of dsymutil makes it very difficult to copy the Swift
320   // reflection metadata sections into the __TEXT segment, so dsymutil creates
321   // these sections in the __DWARF segment instead.
322   if (!Ctx->getSwift5ReflectionSegmentName().empty()) {
323 #define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF)                           \
324   Swift5ReflectionSections                                                     \
325       [llvm::binaryformat::Swift5ReflectionSectionKind::KIND] =                \
326           Ctx->getMachOSection(Ctx->getSwift5ReflectionSegmentName().data(),   \
327                                MACHO, 0, SectionKind::getMetadata());
328 #include "llvm/BinaryFormat/Swift.def"
329   }
330 
331   TLSExtraDataSection = TLSTLVSection;
332 }
333 
334 void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
335   switch (T.getArch()) {
336   case Triple::mips:
337   case Triple::mipsel:
338   case Triple::mips64:
339   case Triple::mips64el:
340     // We cannot use DW_EH_PE_sdata8 for the large PositionIndependent case
341     // since there is no R_MIPS_PC64 relocation (only a 32-bit version).
342     if (PositionIndependent && !Large)
343       FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
344     else
345       FDECFIEncoding = Ctx->getAsmInfo()->getCodePointerSize() == 4
346                            ? dwarf::DW_EH_PE_sdata4
347                            : dwarf::DW_EH_PE_sdata8;
348     break;
349   case Triple::ppc64:
350   case Triple::ppc64le:
351   case Triple::aarch64:
352   case Triple::aarch64_be:
353   case Triple::x86_64:
354     FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
355                      (Large ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
356     break;
357   case Triple::bpfel:
358   case Triple::bpfeb:
359     FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
360     break;
361   case Triple::hexagon:
362     FDECFIEncoding =
363         PositionIndependent ? dwarf::DW_EH_PE_pcrel : dwarf::DW_EH_PE_absptr;
364     break;
365   case Triple::xtensa:
366     FDECFIEncoding = dwarf::DW_EH_PE_sdata4;
367     break;
368   default:
369     FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
370     break;
371   }
372 
373   unsigned EHSectionType = T.getArch() == Triple::x86_64
374                                ? ELF::SHT_X86_64_UNWIND
375                                : ELF::SHT_PROGBITS;
376 
377   // Solaris requires different flags for .eh_frame to seemingly every other
378   // platform.
379   unsigned EHSectionFlags = ELF::SHF_ALLOC;
380   if (T.isOSSolaris() && T.getArch() != Triple::x86_64)
381     EHSectionFlags |= ELF::SHF_WRITE;
382 
383   // ELF
384   BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
385                                   ELF::SHF_WRITE | ELF::SHF_ALLOC);
386 
387   TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
388                                    ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
389 
390   DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
391                                    ELF::SHF_WRITE | ELF::SHF_ALLOC);
392 
393   ReadOnlySection =
394       Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
395 
396   TLSDataSection =
397       Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
398                          ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
399 
400   TLSBSSSection = Ctx->getELFSection(
401       ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
402 
403   DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
404                                         ELF::SHF_ALLOC | ELF::SHF_WRITE);
405 
406   MergeableConst4Section =
407       Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
408                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
409 
410   MergeableConst8Section =
411       Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
412                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
413 
414   MergeableConst16Section =
415       Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
416                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
417 
418   MergeableConst32Section =
419       Ctx->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS,
420                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
421 
422   // Exception Handling Sections.
423 
424   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
425   // it contains relocatable pointers.  In PIC mode, this is probably a big
426   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
427   // adjusted or this should be a data section.
428   LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
429                                    ELF::SHF_ALLOC);
430 
431   COFFDebugSymbolsSection = nullptr;
432   COFFDebugTypesSection = nullptr;
433 
434   unsigned DebugSecType = ELF::SHT_PROGBITS;
435 
436   // MIPS .debug_* sections should have SHT_MIPS_DWARF section type
437   // to distinguish among sections contain DWARF and ECOFF debug formats.
438   // Sections with ECOFF debug format are obsoleted and marked by SHT_PROGBITS.
439   if (T.isMIPS())
440     DebugSecType = ELF::SHT_MIPS_DWARF;
441 
442   // Debug Info Sections.
443   DwarfAbbrevSection =
444       Ctx->getELFSection(".debug_abbrev", DebugSecType, 0);
445   DwarfInfoSection = Ctx->getELFSection(".debug_info", DebugSecType, 0);
446   DwarfLineSection = Ctx->getELFSection(".debug_line", DebugSecType, 0);
447   DwarfLineStrSection =
448       Ctx->getELFSection(".debug_line_str", DebugSecType,
449                          ELF::SHF_MERGE | ELF::SHF_STRINGS, 1);
450   DwarfFrameSection = Ctx->getELFSection(".debug_frame", DebugSecType, 0);
451   DwarfPubNamesSection =
452       Ctx->getELFSection(".debug_pubnames", DebugSecType, 0);
453   DwarfPubTypesSection =
454       Ctx->getELFSection(".debug_pubtypes", DebugSecType, 0);
455   DwarfGnuPubNamesSection =
456       Ctx->getELFSection(".debug_gnu_pubnames", DebugSecType, 0);
457   DwarfGnuPubTypesSection =
458       Ctx->getELFSection(".debug_gnu_pubtypes", DebugSecType, 0);
459   DwarfStrSection =
460       Ctx->getELFSection(".debug_str", DebugSecType,
461                          ELF::SHF_MERGE | ELF::SHF_STRINGS, 1);
462   DwarfLocSection = Ctx->getELFSection(".debug_loc", DebugSecType, 0);
463   DwarfARangesSection =
464       Ctx->getELFSection(".debug_aranges", DebugSecType, 0);
465   DwarfRangesSection =
466       Ctx->getELFSection(".debug_ranges", DebugSecType, 0);
467   DwarfMacinfoSection =
468       Ctx->getELFSection(".debug_macinfo", DebugSecType, 0);
469   DwarfMacroSection = Ctx->getELFSection(".debug_macro", DebugSecType, 0);
470 
471   // DWARF5 Experimental Debug Info
472 
473   // Accelerator Tables
474   DwarfDebugNamesSection =
475       Ctx->getELFSection(".debug_names", ELF::SHT_PROGBITS, 0);
476   DwarfAccelNamesSection =
477       Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0);
478   DwarfAccelObjCSection =
479       Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0);
480   DwarfAccelNamespaceSection =
481       Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0);
482   DwarfAccelTypesSection =
483       Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0);
484 
485   // String Offset and Address Sections
486   DwarfStrOffSection =
487       Ctx->getELFSection(".debug_str_offsets", DebugSecType, 0);
488   DwarfAddrSection = Ctx->getELFSection(".debug_addr", DebugSecType, 0);
489   DwarfRnglistsSection = Ctx->getELFSection(".debug_rnglists", DebugSecType, 0);
490   DwarfLoclistsSection = Ctx->getELFSection(".debug_loclists", DebugSecType, 0);
491 
492   // Fission Sections
493   DwarfInfoDWOSection =
494       Ctx->getELFSection(".debug_info.dwo", DebugSecType, ELF::SHF_EXCLUDE);
495   DwarfTypesDWOSection =
496       Ctx->getELFSection(".debug_types.dwo", DebugSecType, ELF::SHF_EXCLUDE);
497   DwarfAbbrevDWOSection =
498       Ctx->getELFSection(".debug_abbrev.dwo", DebugSecType, ELF::SHF_EXCLUDE);
499   DwarfStrDWOSection = Ctx->getELFSection(
500       ".debug_str.dwo", DebugSecType,
501       ELF::SHF_MERGE | ELF::SHF_STRINGS | ELF::SHF_EXCLUDE, 1);
502   DwarfLineDWOSection =
503       Ctx->getELFSection(".debug_line.dwo", DebugSecType, ELF::SHF_EXCLUDE);
504   DwarfLocDWOSection =
505       Ctx->getELFSection(".debug_loc.dwo", DebugSecType, ELF::SHF_EXCLUDE);
506   DwarfStrOffDWOSection = Ctx->getELFSection(".debug_str_offsets.dwo",
507                                              DebugSecType, ELF::SHF_EXCLUDE);
508   DwarfRnglistsDWOSection =
509       Ctx->getELFSection(".debug_rnglists.dwo", DebugSecType, ELF::SHF_EXCLUDE);
510   DwarfMacinfoDWOSection =
511       Ctx->getELFSection(".debug_macinfo.dwo", DebugSecType, ELF::SHF_EXCLUDE);
512   DwarfMacroDWOSection =
513       Ctx->getELFSection(".debug_macro.dwo", DebugSecType, ELF::SHF_EXCLUDE);
514 
515   DwarfLoclistsDWOSection =
516       Ctx->getELFSection(".debug_loclists.dwo", DebugSecType, ELF::SHF_EXCLUDE);
517 
518   // DWP Sections
519   DwarfCUIndexSection =
520       Ctx->getELFSection(".debug_cu_index", DebugSecType, 0);
521   DwarfTUIndexSection =
522       Ctx->getELFSection(".debug_tu_index", DebugSecType, 0);
523 
524   StackMapSection =
525       Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
526 
527   FaultMapSection =
528       Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
529 
530   EHFrameSection =
531       Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);
532 
533   StackSizesSection = Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, 0);
534 
535   PseudoProbeSection = Ctx->getELFSection(".pseudo_probe", DebugSecType, 0);
536   PseudoProbeDescSection =
537       Ctx->getELFSection(".pseudo_probe_desc", DebugSecType, 0);
538 
539   LLVMStatsSection = Ctx->getELFSection(".llvm_stats", ELF::SHT_PROGBITS, 0);
540 }
541 
542 void MCObjectFileInfo::initGOFFMCObjectFileInfo(const Triple &T) {
543   TextSection =
544       Ctx->getGOFFSection(".text", SectionKind::getText(), nullptr, nullptr);
545   BSSSection =
546       Ctx->getGOFFSection(".bss", SectionKind::getBSS(), nullptr, nullptr);
547   PPA1Section =
548       Ctx->getGOFFSection(".ppa1", SectionKind::getMetadata(), TextSection,
549                           MCConstantExpr::create(GOFF::SK_PPA1, *Ctx));
550   PPA2Section =
551       Ctx->getGOFFSection(".ppa2", SectionKind::getMetadata(), TextSection,
552                           MCConstantExpr::create(GOFF::SK_PPA2, *Ctx));
553   ADASection =
554       Ctx->getGOFFSection(".ada", SectionKind::getData(), nullptr, nullptr);
555   IDRLSection =
556       Ctx->getGOFFSection("B_IDRL", SectionKind::getData(), nullptr, nullptr);
557 }
558 
559 void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
560   EHFrameSection =
561       Ctx->getCOFFSection(".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
562                                            COFF::IMAGE_SCN_MEM_READ,
563                           SectionKind::getData());
564 
565   // Set the `IMAGE_SCN_MEM_16BIT` flag when compiling for thumb mode.  This is
566   // used to indicate to the linker that the text segment contains thumb instructions
567   // and to set the ISA selection bit for calls accordingly.
568   const bool IsThumb = T.getArch() == Triple::thumb;
569 
570   // COFF
571   BSSSection = Ctx->getCOFFSection(
572       ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
573                   COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
574       SectionKind::getBSS());
575   TextSection = Ctx->getCOFFSection(
576       ".text",
577       (IsThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) |
578           COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
579           COFF::IMAGE_SCN_MEM_READ,
580       SectionKind::getText());
581   DataSection = Ctx->getCOFFSection(
582       ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
583                    COFF::IMAGE_SCN_MEM_WRITE,
584       SectionKind::getData());
585   ReadOnlySection = Ctx->getCOFFSection(
586       ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
587       SectionKind::getReadOnly());
588 
589   if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::aarch64 ||
590       T.getArch() == Triple::arm || T.getArch() == Triple::thumb) {
591     // On Windows with SEH, the LSDA is emitted into the .xdata section
592     LSDASection = nullptr;
593   } else {
594     LSDASection = Ctx->getCOFFSection(".gcc_except_table",
595                                       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
596                                           COFF::IMAGE_SCN_MEM_READ,
597                                       SectionKind::getReadOnly());
598   }
599 
600   // Debug info.
601   COFFDebugSymbolsSection =
602       Ctx->getCOFFSection(".debug$S", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
603                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
604                                        COFF::IMAGE_SCN_MEM_READ),
605                           SectionKind::getMetadata());
606   COFFDebugTypesSection =
607       Ctx->getCOFFSection(".debug$T", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
608                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
609                                        COFF::IMAGE_SCN_MEM_READ),
610                           SectionKind::getMetadata());
611   COFFGlobalTypeHashesSection = Ctx->getCOFFSection(
612       ".debug$H",
613       (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
614        COFF::IMAGE_SCN_MEM_READ),
615       SectionKind::getMetadata());
616 
617   DwarfAbbrevSection = Ctx->getCOFFSection(
618       ".debug_abbrev",
619       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
620           COFF::IMAGE_SCN_MEM_READ,
621       SectionKind::getMetadata(), "section_abbrev");
622   DwarfInfoSection = Ctx->getCOFFSection(
623       ".debug_info",
624       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
625           COFF::IMAGE_SCN_MEM_READ,
626       SectionKind::getMetadata(), "section_info");
627   DwarfLineSection = Ctx->getCOFFSection(
628       ".debug_line",
629       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
630           COFF::IMAGE_SCN_MEM_READ,
631       SectionKind::getMetadata(), "section_line");
632   DwarfLineStrSection = Ctx->getCOFFSection(
633       ".debug_line_str",
634       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
635           COFF::IMAGE_SCN_MEM_READ,
636       SectionKind::getMetadata(), "section_line_str");
637   DwarfFrameSection = Ctx->getCOFFSection(
638       ".debug_frame",
639       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
640           COFF::IMAGE_SCN_MEM_READ,
641       SectionKind::getMetadata());
642   DwarfPubNamesSection = Ctx->getCOFFSection(
643       ".debug_pubnames",
644       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
645           COFF::IMAGE_SCN_MEM_READ,
646       SectionKind::getMetadata());
647   DwarfPubTypesSection = Ctx->getCOFFSection(
648       ".debug_pubtypes",
649       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
650           COFF::IMAGE_SCN_MEM_READ,
651       SectionKind::getMetadata());
652   DwarfGnuPubNamesSection = Ctx->getCOFFSection(
653       ".debug_gnu_pubnames",
654       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
655           COFF::IMAGE_SCN_MEM_READ,
656       SectionKind::getMetadata());
657   DwarfGnuPubTypesSection = Ctx->getCOFFSection(
658       ".debug_gnu_pubtypes",
659       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
660           COFF::IMAGE_SCN_MEM_READ,
661       SectionKind::getMetadata());
662   DwarfStrSection = Ctx->getCOFFSection(
663       ".debug_str",
664       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
665           COFF::IMAGE_SCN_MEM_READ,
666       SectionKind::getMetadata(), "info_string");
667   DwarfStrOffSection = Ctx->getCOFFSection(
668       ".debug_str_offsets",
669       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
670           COFF::IMAGE_SCN_MEM_READ,
671       SectionKind::getMetadata(), "section_str_off");
672   DwarfLocSection = Ctx->getCOFFSection(
673       ".debug_loc",
674       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
675           COFF::IMAGE_SCN_MEM_READ,
676       SectionKind::getMetadata(), "section_debug_loc");
677   DwarfLoclistsSection = Ctx->getCOFFSection(
678       ".debug_loclists",
679       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
680           COFF::IMAGE_SCN_MEM_READ,
681       SectionKind::getMetadata(), "section_debug_loclists");
682   DwarfARangesSection = Ctx->getCOFFSection(
683       ".debug_aranges",
684       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
685           COFF::IMAGE_SCN_MEM_READ,
686       SectionKind::getMetadata());
687   DwarfRangesSection = Ctx->getCOFFSection(
688       ".debug_ranges",
689       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
690           COFF::IMAGE_SCN_MEM_READ,
691       SectionKind::getMetadata(), "debug_range");
692   DwarfRnglistsSection = Ctx->getCOFFSection(
693       ".debug_rnglists",
694       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
695           COFF::IMAGE_SCN_MEM_READ,
696       SectionKind::getMetadata(), "debug_rnglists");
697   DwarfMacinfoSection = Ctx->getCOFFSection(
698       ".debug_macinfo",
699       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
700           COFF::IMAGE_SCN_MEM_READ,
701       SectionKind::getMetadata(), "debug_macinfo");
702   DwarfMacroSection = Ctx->getCOFFSection(
703       ".debug_macro",
704       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
705           COFF::IMAGE_SCN_MEM_READ,
706       SectionKind::getMetadata(), "debug_macro");
707   DwarfMacinfoDWOSection = Ctx->getCOFFSection(
708       ".debug_macinfo.dwo",
709       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
710           COFF::IMAGE_SCN_MEM_READ,
711       SectionKind::getMetadata(), "debug_macinfo.dwo");
712   DwarfMacroDWOSection = Ctx->getCOFFSection(
713       ".debug_macro.dwo",
714       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
715           COFF::IMAGE_SCN_MEM_READ,
716       SectionKind::getMetadata(), "debug_macro.dwo");
717   DwarfInfoDWOSection = Ctx->getCOFFSection(
718       ".debug_info.dwo",
719       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
720           COFF::IMAGE_SCN_MEM_READ,
721       SectionKind::getMetadata(), "section_info_dwo");
722   DwarfTypesDWOSection = Ctx->getCOFFSection(
723       ".debug_types.dwo",
724       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
725           COFF::IMAGE_SCN_MEM_READ,
726       SectionKind::getMetadata(), "section_types_dwo");
727   DwarfAbbrevDWOSection = Ctx->getCOFFSection(
728       ".debug_abbrev.dwo",
729       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
730           COFF::IMAGE_SCN_MEM_READ,
731       SectionKind::getMetadata(), "section_abbrev_dwo");
732   DwarfStrDWOSection = Ctx->getCOFFSection(
733       ".debug_str.dwo",
734       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
735           COFF::IMAGE_SCN_MEM_READ,
736       SectionKind::getMetadata(), "skel_string");
737   DwarfLineDWOSection = Ctx->getCOFFSection(
738       ".debug_line.dwo",
739       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
740           COFF::IMAGE_SCN_MEM_READ,
741       SectionKind::getMetadata());
742   DwarfLocDWOSection = Ctx->getCOFFSection(
743       ".debug_loc.dwo",
744       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
745           COFF::IMAGE_SCN_MEM_READ,
746       SectionKind::getMetadata(), "skel_loc");
747   DwarfStrOffDWOSection = Ctx->getCOFFSection(
748       ".debug_str_offsets.dwo",
749       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
750           COFF::IMAGE_SCN_MEM_READ,
751       SectionKind::getMetadata(), "section_str_off_dwo");
752   DwarfAddrSection = Ctx->getCOFFSection(
753       ".debug_addr",
754       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
755           COFF::IMAGE_SCN_MEM_READ,
756       SectionKind::getMetadata(), "addr_sec");
757   DwarfCUIndexSection = Ctx->getCOFFSection(
758       ".debug_cu_index",
759       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
760           COFF::IMAGE_SCN_MEM_READ,
761       SectionKind::getMetadata());
762   DwarfTUIndexSection = Ctx->getCOFFSection(
763       ".debug_tu_index",
764       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
765           COFF::IMAGE_SCN_MEM_READ,
766       SectionKind::getMetadata());
767   DwarfDebugNamesSection = Ctx->getCOFFSection(
768       ".debug_names",
769       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
770           COFF::IMAGE_SCN_MEM_READ,
771       SectionKind::getMetadata(), "debug_names_begin");
772   DwarfAccelNamesSection = Ctx->getCOFFSection(
773       ".apple_names",
774       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
775           COFF::IMAGE_SCN_MEM_READ,
776       SectionKind::getMetadata(), "names_begin");
777   DwarfAccelNamespaceSection = Ctx->getCOFFSection(
778       ".apple_namespaces",
779       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
780           COFF::IMAGE_SCN_MEM_READ,
781       SectionKind::getMetadata(), "namespac_begin");
782   DwarfAccelTypesSection = Ctx->getCOFFSection(
783       ".apple_types",
784       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
785           COFF::IMAGE_SCN_MEM_READ,
786       SectionKind::getMetadata(), "types_begin");
787   DwarfAccelObjCSection = Ctx->getCOFFSection(
788       ".apple_objc",
789       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
790           COFF::IMAGE_SCN_MEM_READ,
791       SectionKind::getMetadata(), "objc_begin");
792 
793   DrectveSection = Ctx->getCOFFSection(
794       ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE,
795       SectionKind::getMetadata());
796 
797   PDataSection = Ctx->getCOFFSection(
798       ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
799       SectionKind::getData());
800 
801   XDataSection = Ctx->getCOFFSection(
802       ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
803       SectionKind::getData());
804 
805   SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO,
806                                       SectionKind::getMetadata());
807 
808   GEHContSection = Ctx->getCOFFSection(".gehcont$y",
809                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
810                                            COFF::IMAGE_SCN_MEM_READ,
811                                        SectionKind::getMetadata());
812 
813   GFIDsSection = Ctx->getCOFFSection(".gfids$y",
814                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
815                                          COFF::IMAGE_SCN_MEM_READ,
816                                      SectionKind::getMetadata());
817 
818   GIATsSection = Ctx->getCOFFSection(".giats$y",
819                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
820                                          COFF::IMAGE_SCN_MEM_READ,
821                                      SectionKind::getMetadata());
822 
823   GLJMPSection = Ctx->getCOFFSection(".gljmp$y",
824                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
825                                          COFF::IMAGE_SCN_MEM_READ,
826                                      SectionKind::getMetadata());
827 
828   TLSDataSection = Ctx->getCOFFSection(
829       ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
830                    COFF::IMAGE_SCN_MEM_WRITE,
831       SectionKind::getData());
832 
833   StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps",
834                                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
835                                             COFF::IMAGE_SCN_MEM_READ,
836                                         SectionKind::getReadOnly());
837 }
838 
839 void MCObjectFileInfo::initSPIRVMCObjectFileInfo(const Triple &T) {
840   // Put everything in a single binary section.
841   TextSection = Ctx->getSPIRVSection();
842 }
843 
844 void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
845   TextSection = Ctx->getWasmSection(".text", SectionKind::getText());
846   DataSection = Ctx->getWasmSection(".data", SectionKind::getData());
847 
848   DwarfLineSection =
849       Ctx->getWasmSection(".debug_line", SectionKind::getMetadata());
850   DwarfLineStrSection =
851       Ctx->getWasmSection(".debug_line_str", SectionKind::getMetadata(),
852                           wasm::WASM_SEG_FLAG_STRINGS);
853   DwarfStrSection = Ctx->getWasmSection(
854       ".debug_str", SectionKind::getMetadata(), wasm::WASM_SEG_FLAG_STRINGS);
855   DwarfLocSection =
856       Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata());
857   DwarfAbbrevSection =
858       Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata());
859   DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata());
860   DwarfRangesSection =
861       Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata());
862   DwarfMacinfoSection =
863       Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata());
864   DwarfMacroSection =
865       Ctx->getWasmSection(".debug_macro", SectionKind::getMetadata());
866   DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
867   DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
868   DwarfInfoSection =
869       Ctx->getWasmSection(".debug_info", SectionKind::getMetadata());
870   DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata());
871   DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata());
872   DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata());
873   DwarfGnuPubNamesSection =
874       Ctx->getWasmSection(".debug_gnu_pubnames", SectionKind::getMetadata());
875   DwarfGnuPubTypesSection =
876       Ctx->getWasmSection(".debug_gnu_pubtypes", SectionKind::getMetadata());
877 
878   DwarfDebugNamesSection =
879       Ctx->getWasmSection(".debug_names", SectionKind::getMetadata());
880   DwarfStrOffSection =
881       Ctx->getWasmSection(".debug_str_offsets", SectionKind::getMetadata());
882   DwarfAddrSection =
883       Ctx->getWasmSection(".debug_addr", SectionKind::getMetadata());
884   DwarfRnglistsSection =
885       Ctx->getWasmSection(".debug_rnglists", SectionKind::getMetadata());
886   DwarfLoclistsSection =
887       Ctx->getWasmSection(".debug_loclists", SectionKind::getMetadata());
888 
889   // Fission Sections
890   DwarfInfoDWOSection =
891       Ctx->getWasmSection(".debug_info.dwo", SectionKind::getMetadata());
892   DwarfTypesDWOSection =
893       Ctx->getWasmSection(".debug_types.dwo", SectionKind::getMetadata());
894   DwarfAbbrevDWOSection =
895       Ctx->getWasmSection(".debug_abbrev.dwo", SectionKind::getMetadata());
896   DwarfStrDWOSection =
897       Ctx->getWasmSection(".debug_str.dwo", SectionKind::getMetadata(),
898                           wasm::WASM_SEG_FLAG_STRINGS);
899   DwarfLineDWOSection =
900       Ctx->getWasmSection(".debug_line.dwo", SectionKind::getMetadata());
901   DwarfLocDWOSection =
902       Ctx->getWasmSection(".debug_loc.dwo", SectionKind::getMetadata());
903   DwarfStrOffDWOSection =
904       Ctx->getWasmSection(".debug_str_offsets.dwo", SectionKind::getMetadata());
905   DwarfRnglistsDWOSection =
906       Ctx->getWasmSection(".debug_rnglists.dwo", SectionKind::getMetadata());
907   DwarfMacinfoDWOSection =
908       Ctx->getWasmSection(".debug_macinfo.dwo", SectionKind::getMetadata());
909   DwarfMacroDWOSection =
910       Ctx->getWasmSection(".debug_macro.dwo", SectionKind::getMetadata());
911 
912   DwarfLoclistsDWOSection =
913       Ctx->getWasmSection(".debug_loclists.dwo", SectionKind::getMetadata());
914 
915   // DWP Sections
916   DwarfCUIndexSection =
917       Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
918   DwarfTUIndexSection =
919       Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
920 
921   // Wasm use data section for LSDA.
922   // TODO Consider putting each function's exception table in a separate
923   // section, as in -function-sections, to facilitate lld's --gc-section.
924   LSDASection = Ctx->getWasmSection(".rodata.gcc_except_table",
925                                     SectionKind::getReadOnlyWithRel());
926 
927   // TODO: Define more sections.
928 }
929 
930 void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) {
931   // The default csect for program code. Functions without a specified section
932   // get placed into this csect. The choice of csect name is not a property of
933   // the ABI or object file format, but various tools rely on the section
934   // name being empty (considering named symbols to be "user symbol names").
935   TextSection = Ctx->getXCOFFSection(
936       "..text..", // Use a non-null name to work around an AIX assembler bug...
937       SectionKind::getText(),
938       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_PR, XCOFF::XTY_SD),
939       /* MultiSymbolsAllowed*/ true);
940 
941   // ... but use a null name when generating the symbol table.
942   MCSectionXCOFF *TS = static_cast<MCSectionXCOFF *>(TextSection);
943   TS->getQualNameSymbol()->setSymbolTableName("");
944   TS->setSymbolTableName("");
945 
946   DataSection = Ctx->getXCOFFSection(
947       ".data", SectionKind::getData(),
948       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW, XCOFF::XTY_SD),
949       /* MultiSymbolsAllowed*/ true);
950 
951   ReadOnlySection = Ctx->getXCOFFSection(
952       ".rodata", SectionKind::getReadOnly(),
953       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
954       /* MultiSymbolsAllowed*/ true);
955   ReadOnlySection->setAlignment(Align(4));
956 
957   ReadOnly8Section = Ctx->getXCOFFSection(
958       ".rodata.8", SectionKind::getReadOnly(),
959       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
960       /* MultiSymbolsAllowed*/ true);
961   ReadOnly8Section->setAlignment(Align(8));
962 
963   ReadOnly16Section = Ctx->getXCOFFSection(
964       ".rodata.16", SectionKind::getReadOnly(),
965       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
966       /* MultiSymbolsAllowed*/ true);
967   ReadOnly16Section->setAlignment(Align(16));
968 
969   TLSDataSection = Ctx->getXCOFFSection(
970       ".tdata", SectionKind::getThreadData(),
971       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_TL, XCOFF::XTY_SD),
972       /* MultiSymbolsAllowed*/ true);
973 
974   TOCBaseSection = Ctx->getXCOFFSection(
975       "TOC", SectionKind::getData(),
976       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_TC0,
977                              XCOFF::XTY_SD));
978 
979   // The TOC-base always has 0 size, but 4 byte alignment.
980   TOCBaseSection->setAlignment(Align(4));
981 
982   LSDASection = Ctx->getXCOFFSection(
983       ".gcc_except_table", SectionKind::getReadOnly(),
984       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO,
985                              XCOFF::XTY_SD));
986 
987   CompactUnwindSection = Ctx->getXCOFFSection(
988       ".eh_info_table", SectionKind::getData(),
989       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW,
990                              XCOFF::XTY_SD));
991 
992   // DWARF sections for XCOFF are not csects. They are special STYP_DWARF
993   // sections, and the individual DWARF sections are distinguished by their
994   // section subtype.
995   DwarfAbbrevSection = Ctx->getXCOFFSection(
996       ".dwabrev", SectionKind::getMetadata(),
997       /* CsectProperties */ std::nullopt,
998       /* MultiSymbolsAllowed */ true, ".dwabrev", XCOFF::SSUBTYP_DWABREV);
999 
1000   DwarfInfoSection = Ctx->getXCOFFSection(
1001       ".dwinfo", SectionKind::getMetadata(), /* CsectProperties */ std::nullopt,
1002       /* MultiSymbolsAllowed */ true, ".dwinfo", XCOFF::SSUBTYP_DWINFO);
1003 
1004   DwarfLineSection = Ctx->getXCOFFSection(
1005       ".dwline", SectionKind::getMetadata(), /* CsectProperties */ std::nullopt,
1006       /* MultiSymbolsAllowed */ true, ".dwline", XCOFF::SSUBTYP_DWLINE);
1007 
1008   DwarfFrameSection = Ctx->getXCOFFSection(
1009       ".dwframe", SectionKind::getMetadata(),
1010       /* CsectProperties */ std::nullopt,
1011       /* MultiSymbolsAllowed */ true, ".dwframe", XCOFF::SSUBTYP_DWFRAME);
1012 
1013   DwarfPubNamesSection = Ctx->getXCOFFSection(
1014       ".dwpbnms", SectionKind::getMetadata(),
1015       /* CsectProperties */ std::nullopt,
1016       /* MultiSymbolsAllowed */ true, ".dwpbnms", XCOFF::SSUBTYP_DWPBNMS);
1017 
1018   DwarfPubTypesSection = Ctx->getXCOFFSection(
1019       ".dwpbtyp", SectionKind::getMetadata(),
1020       /* CsectProperties */ std::nullopt,
1021       /* MultiSymbolsAllowed */ true, ".dwpbtyp", XCOFF::SSUBTYP_DWPBTYP);
1022 
1023   DwarfStrSection = Ctx->getXCOFFSection(
1024       ".dwstr", SectionKind::getMetadata(), /* CsectProperties */ std::nullopt,
1025       /* MultiSymbolsAllowed */ true, ".dwstr", XCOFF::SSUBTYP_DWSTR);
1026 
1027   DwarfLocSection = Ctx->getXCOFFSection(
1028       ".dwloc", SectionKind::getMetadata(), /* CsectProperties */ std::nullopt,
1029       /* MultiSymbolsAllowed */ true, ".dwloc", XCOFF::SSUBTYP_DWLOC);
1030 
1031   DwarfARangesSection = Ctx->getXCOFFSection(
1032       ".dwarnge", SectionKind::getMetadata(),
1033       /* CsectProperties */ std::nullopt,
1034       /* MultiSymbolsAllowed */ true, ".dwarnge", XCOFF::SSUBTYP_DWARNGE);
1035 
1036   DwarfRangesSection = Ctx->getXCOFFSection(
1037       ".dwrnges", SectionKind::getMetadata(),
1038       /* CsectProperties */ std::nullopt,
1039       /* MultiSymbolsAllowed */ true, ".dwrnges", XCOFF::SSUBTYP_DWRNGES);
1040 
1041   DwarfMacinfoSection = Ctx->getXCOFFSection(
1042       ".dwmac", SectionKind::getMetadata(), /* CsectProperties */ std::nullopt,
1043       /* MultiSymbolsAllowed */ true, ".dwmac", XCOFF::SSUBTYP_DWMAC);
1044 }
1045 
1046 void MCObjectFileInfo::initDXContainerObjectFileInfo(const Triple &T) {
1047   // At the moment the DXBC section should end up empty.
1048   TextSection = Ctx->getDXContainerSection("DXBC", SectionKind::getText());
1049 }
1050 
1051 MCObjectFileInfo::~MCObjectFileInfo() = default;
1052 
1053 void MCObjectFileInfo::initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
1054                                             bool LargeCodeModel) {
1055   PositionIndependent = PIC;
1056   Ctx = &MCCtx;
1057 
1058   // Common.
1059   SupportsWeakOmittedEHFrame = true;
1060   SupportsCompactUnwindWithoutEHFrame = false;
1061   OmitDwarfIfHaveCompactUnwind = false;
1062 
1063   FDECFIEncoding = dwarf::DW_EH_PE_absptr;
1064 
1065   CompactUnwindDwarfEHFrameOnly = 0;
1066 
1067   EHFrameSection = nullptr;             // Created on demand.
1068   CompactUnwindSection = nullptr;       // Used only by selected targets.
1069   DwarfAccelNamesSection = nullptr;     // Used only by selected targets.
1070   DwarfAccelObjCSection = nullptr;      // Used only by selected targets.
1071   DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
1072   DwarfAccelTypesSection = nullptr;     // Used only by selected targets.
1073 
1074   Triple TheTriple = Ctx->getTargetTriple();
1075   switch (Ctx->getObjectFileType()) {
1076   case MCContext::IsMachO:
1077     initMachOMCObjectFileInfo(TheTriple);
1078     break;
1079   case MCContext::IsCOFF:
1080     initCOFFMCObjectFileInfo(TheTriple);
1081     break;
1082   case MCContext::IsELF:
1083     initELFMCObjectFileInfo(TheTriple, LargeCodeModel);
1084     break;
1085   case MCContext::IsGOFF:
1086     initGOFFMCObjectFileInfo(TheTriple);
1087     break;
1088   case MCContext::IsSPIRV:
1089     initSPIRVMCObjectFileInfo(TheTriple);
1090     break;
1091   case MCContext::IsWasm:
1092     initWasmMCObjectFileInfo(TheTriple);
1093     break;
1094   case MCContext::IsXCOFF:
1095     initXCOFFMCObjectFileInfo(TheTriple);
1096     break;
1097   case MCContext::IsDXContainer:
1098     initDXContainerObjectFileInfo(TheTriple);
1099     break;
1100   }
1101 }
1102 
1103 MCSection *MCObjectFileInfo::getDwarfComdatSection(const char *Name,
1104                                                    uint64_t Hash) const {
1105   switch (Ctx->getTargetTriple().getObjectFormat()) {
1106   case Triple::ELF:
1107     return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, ELF::SHF_GROUP, 0,
1108                               utostr(Hash), /*IsComdat=*/true);
1109   case Triple::Wasm:
1110     return Ctx->getWasmSection(Name, SectionKind::getMetadata(), 0,
1111                                utostr(Hash), MCContext::GenericSectionID);
1112   case Triple::MachO:
1113   case Triple::COFF:
1114   case Triple::GOFF:
1115   case Triple::SPIRV:
1116   case Triple::XCOFF:
1117   case Triple::DXContainer:
1118   case Triple::UnknownObjectFormat:
1119     report_fatal_error("Cannot get DWARF comdat section for this object file "
1120                        "format: not implemented.");
1121     break;
1122   }
1123   llvm_unreachable("Unknown ObjectFormatType");
1124 }
1125 
1126 MCSection *
1127 MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
1128   if ((Ctx->getObjectFileType() != MCContext::IsELF) ||
1129       Ctx->getTargetTriple().isPS4())
1130     return StackSizesSection;
1131 
1132   const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1133   unsigned Flags = ELF::SHF_LINK_ORDER;
1134   StringRef GroupName;
1135   if (const MCSymbol *Group = ElfSec.getGroup()) {
1136     GroupName = Group->getName();
1137     Flags |= ELF::SHF_GROUP;
1138   }
1139 
1140   return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0,
1141                             GroupName, true, ElfSec.getUniqueID(),
1142                             cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1143 }
1144 
1145 MCSection *
1146 MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
1147   if (Ctx->getObjectFileType() != MCContext::IsELF)
1148     return nullptr;
1149 
1150   const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1151   unsigned Flags = ELF::SHF_LINK_ORDER;
1152   StringRef GroupName;
1153   if (const MCSymbol *Group = ElfSec.getGroup()) {
1154     GroupName = Group->getName();
1155     Flags |= ELF::SHF_GROUP;
1156   }
1157 
1158   // Use the text section's begin symbol and unique ID to create a separate
1159   // .llvm_bb_addr_map section associated with every unique text section.
1160   return Ctx->getELFSection(".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP,
1161                             Flags, 0, GroupName, true, ElfSec.getUniqueID(),
1162                             cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1163 }
1164 
1165 MCSection *
1166 MCObjectFileInfo::getKCFITrapSection(const MCSection &TextSec) const {
1167   if (Ctx->getObjectFileType() != MCContext::IsELF)
1168     return nullptr;
1169 
1170   const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1171   unsigned Flags = ELF::SHF_LINK_ORDER | ELF::SHF_ALLOC;
1172   StringRef GroupName;
1173   if (const MCSymbol *Group = ElfSec.getGroup()) {
1174     GroupName = Group->getName();
1175     Flags |= ELF::SHF_GROUP;
1176   }
1177 
1178   return Ctx->getELFSection(".kcfi_traps", ELF::SHT_PROGBITS, Flags, 0,
1179                             GroupName,
1180                             /*IsComdat=*/true, ElfSec.getUniqueID(),
1181                             cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1182 }
1183 
1184 MCSection *
1185 MCObjectFileInfo::getPseudoProbeSection(const MCSection &TextSec) const {
1186   if (Ctx->getObjectFileType() != MCContext::IsELF)
1187     return PseudoProbeSection;
1188 
1189   const auto &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1190   unsigned Flags = ELF::SHF_LINK_ORDER;
1191   StringRef GroupName;
1192   if (const MCSymbol *Group = ElfSec.getGroup()) {
1193     GroupName = Group->getName();
1194     Flags |= ELF::SHF_GROUP;
1195   }
1196 
1197   return Ctx->getELFSection(PseudoProbeSection->getName(), ELF::SHT_PROGBITS,
1198                             Flags, 0, GroupName, true, ElfSec.getUniqueID(),
1199                             cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1200 }
1201 
1202 MCSection *
1203 MCObjectFileInfo::getPseudoProbeDescSection(StringRef FuncName) const {
1204   if (Ctx->getObjectFileType() == MCContext::IsELF) {
1205     // Create a separate comdat group for each function's descriptor in order
1206     // for the linker to deduplicate. The duplication, must be from different
1207     // tranlation unit, can come from:
1208     //  1. Inline functions defined in header files;
1209     //  2. ThinLTO imported funcions;
1210     //  3. Weak-linkage definitions.
1211     // Use a concatenation of the section name and the function name as the
1212     // group name so that descriptor-only groups won't be folded with groups of
1213     // code.
1214     if (Ctx->getTargetTriple().supportsCOMDAT() && !FuncName.empty()) {
1215       auto *S = static_cast<MCSectionELF *>(PseudoProbeDescSection);
1216       auto Flags = S->getFlags() | ELF::SHF_GROUP;
1217       return Ctx->getELFSection(S->getName(), S->getType(), Flags,
1218                                 S->getEntrySize(),
1219                                 S->getName() + "_" + FuncName,
1220                                 /*IsComdat=*/true);
1221     }
1222   }
1223   return PseudoProbeDescSection;
1224 }
1225 
1226 MCSection *MCObjectFileInfo::getLLVMStatsSection() const {
1227   return LLVMStatsSection;
1228 }
1229 
1230 MCSection *MCObjectFileInfo::getPCSection(StringRef Name,
1231                                           const MCSection *TextSec) const {
1232   if (Ctx->getObjectFileType() != MCContext::IsELF)
1233     return nullptr;
1234 
1235   // SHF_WRITE for relocations, and let user post-process data in-place.
1236   unsigned Flags = ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER;
1237 
1238   if (!TextSec)
1239     TextSec = getTextSection();
1240 
1241   StringRef GroupName;
1242   const auto &ElfSec = static_cast<const MCSectionELF &>(*TextSec);
1243   if (const MCSymbol *Group = ElfSec.getGroup()) {
1244     GroupName = Group->getName();
1245     Flags |= ELF::SHF_GROUP;
1246   }
1247   return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, Flags, 0, GroupName, true,
1248                             ElfSec.getUniqueID(),
1249                             cast<MCSymbolELF>(TextSec->getBeginSymbol()));
1250 }
1251