xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCObjectFileInfo.cpp (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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   ADASection =
551       Ctx->getGOFFSection(".ada", SectionKind::getData(), nullptr, nullptr);
552 }
553 
554 void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
555   EHFrameSection =
556       Ctx->getCOFFSection(".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
557                                            COFF::IMAGE_SCN_MEM_READ,
558                           SectionKind::getData());
559 
560   // Set the `IMAGE_SCN_MEM_16BIT` flag when compiling for thumb mode.  This is
561   // used to indicate to the linker that the text segment contains thumb instructions
562   // and to set the ISA selection bit for calls accordingly.
563   const bool IsThumb = T.getArch() == Triple::thumb;
564 
565   // COFF
566   BSSSection = Ctx->getCOFFSection(
567       ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
568                   COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
569       SectionKind::getBSS());
570   TextSection = Ctx->getCOFFSection(
571       ".text",
572       (IsThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) |
573           COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
574           COFF::IMAGE_SCN_MEM_READ,
575       SectionKind::getText());
576   DataSection = Ctx->getCOFFSection(
577       ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
578                    COFF::IMAGE_SCN_MEM_WRITE,
579       SectionKind::getData());
580   ReadOnlySection = Ctx->getCOFFSection(
581       ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
582       SectionKind::getReadOnly());
583 
584   if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::aarch64 ||
585       T.getArch() == Triple::arm || T.getArch() == Triple::thumb) {
586     // On Windows with SEH, the LSDA is emitted into the .xdata section
587     LSDASection = nullptr;
588   } else {
589     LSDASection = Ctx->getCOFFSection(".gcc_except_table",
590                                       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
591                                           COFF::IMAGE_SCN_MEM_READ,
592                                       SectionKind::getReadOnly());
593   }
594 
595   // Debug info.
596   COFFDebugSymbolsSection =
597       Ctx->getCOFFSection(".debug$S", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
598                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
599                                        COFF::IMAGE_SCN_MEM_READ),
600                           SectionKind::getMetadata());
601   COFFDebugTypesSection =
602       Ctx->getCOFFSection(".debug$T", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
603                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
604                                        COFF::IMAGE_SCN_MEM_READ),
605                           SectionKind::getMetadata());
606   COFFGlobalTypeHashesSection = Ctx->getCOFFSection(
607       ".debug$H",
608       (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
609        COFF::IMAGE_SCN_MEM_READ),
610       SectionKind::getMetadata());
611 
612   DwarfAbbrevSection = Ctx->getCOFFSection(
613       ".debug_abbrev",
614       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
615           COFF::IMAGE_SCN_MEM_READ,
616       SectionKind::getMetadata(), "section_abbrev");
617   DwarfInfoSection = Ctx->getCOFFSection(
618       ".debug_info",
619       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
620           COFF::IMAGE_SCN_MEM_READ,
621       SectionKind::getMetadata(), "section_info");
622   DwarfLineSection = Ctx->getCOFFSection(
623       ".debug_line",
624       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
625           COFF::IMAGE_SCN_MEM_READ,
626       SectionKind::getMetadata(), "section_line");
627   DwarfLineStrSection = Ctx->getCOFFSection(
628       ".debug_line_str",
629       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
630           COFF::IMAGE_SCN_MEM_READ,
631       SectionKind::getMetadata(), "section_line_str");
632   DwarfFrameSection = Ctx->getCOFFSection(
633       ".debug_frame",
634       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
635           COFF::IMAGE_SCN_MEM_READ,
636       SectionKind::getMetadata());
637   DwarfPubNamesSection = Ctx->getCOFFSection(
638       ".debug_pubnames",
639       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
640           COFF::IMAGE_SCN_MEM_READ,
641       SectionKind::getMetadata());
642   DwarfPubTypesSection = Ctx->getCOFFSection(
643       ".debug_pubtypes",
644       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
645           COFF::IMAGE_SCN_MEM_READ,
646       SectionKind::getMetadata());
647   DwarfGnuPubNamesSection = Ctx->getCOFFSection(
648       ".debug_gnu_pubnames",
649       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
650           COFF::IMAGE_SCN_MEM_READ,
651       SectionKind::getMetadata());
652   DwarfGnuPubTypesSection = Ctx->getCOFFSection(
653       ".debug_gnu_pubtypes",
654       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
655           COFF::IMAGE_SCN_MEM_READ,
656       SectionKind::getMetadata());
657   DwarfStrSection = Ctx->getCOFFSection(
658       ".debug_str",
659       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
660           COFF::IMAGE_SCN_MEM_READ,
661       SectionKind::getMetadata(), "info_string");
662   DwarfStrOffSection = Ctx->getCOFFSection(
663       ".debug_str_offsets",
664       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
665           COFF::IMAGE_SCN_MEM_READ,
666       SectionKind::getMetadata(), "section_str_off");
667   DwarfLocSection = Ctx->getCOFFSection(
668       ".debug_loc",
669       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
670           COFF::IMAGE_SCN_MEM_READ,
671       SectionKind::getMetadata(), "section_debug_loc");
672   DwarfLoclistsSection = Ctx->getCOFFSection(
673       ".debug_loclists",
674       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
675           COFF::IMAGE_SCN_MEM_READ,
676       SectionKind::getMetadata(), "section_debug_loclists");
677   DwarfARangesSection = Ctx->getCOFFSection(
678       ".debug_aranges",
679       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
680           COFF::IMAGE_SCN_MEM_READ,
681       SectionKind::getMetadata());
682   DwarfRangesSection = Ctx->getCOFFSection(
683       ".debug_ranges",
684       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
685           COFF::IMAGE_SCN_MEM_READ,
686       SectionKind::getMetadata(), "debug_range");
687   DwarfRnglistsSection = Ctx->getCOFFSection(
688       ".debug_rnglists",
689       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
690           COFF::IMAGE_SCN_MEM_READ,
691       SectionKind::getMetadata(), "debug_rnglists");
692   DwarfMacinfoSection = Ctx->getCOFFSection(
693       ".debug_macinfo",
694       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
695           COFF::IMAGE_SCN_MEM_READ,
696       SectionKind::getMetadata(), "debug_macinfo");
697   DwarfMacroSection = Ctx->getCOFFSection(
698       ".debug_macro",
699       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
700           COFF::IMAGE_SCN_MEM_READ,
701       SectionKind::getMetadata(), "debug_macro");
702   DwarfMacinfoDWOSection = Ctx->getCOFFSection(
703       ".debug_macinfo.dwo",
704       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
705           COFF::IMAGE_SCN_MEM_READ,
706       SectionKind::getMetadata(), "debug_macinfo.dwo");
707   DwarfMacroDWOSection = Ctx->getCOFFSection(
708       ".debug_macro.dwo",
709       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
710           COFF::IMAGE_SCN_MEM_READ,
711       SectionKind::getMetadata(), "debug_macro.dwo");
712   DwarfInfoDWOSection = Ctx->getCOFFSection(
713       ".debug_info.dwo",
714       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
715           COFF::IMAGE_SCN_MEM_READ,
716       SectionKind::getMetadata(), "section_info_dwo");
717   DwarfTypesDWOSection = Ctx->getCOFFSection(
718       ".debug_types.dwo",
719       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
720           COFF::IMAGE_SCN_MEM_READ,
721       SectionKind::getMetadata(), "section_types_dwo");
722   DwarfAbbrevDWOSection = Ctx->getCOFFSection(
723       ".debug_abbrev.dwo",
724       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
725           COFF::IMAGE_SCN_MEM_READ,
726       SectionKind::getMetadata(), "section_abbrev_dwo");
727   DwarfStrDWOSection = Ctx->getCOFFSection(
728       ".debug_str.dwo",
729       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
730           COFF::IMAGE_SCN_MEM_READ,
731       SectionKind::getMetadata(), "skel_string");
732   DwarfLineDWOSection = Ctx->getCOFFSection(
733       ".debug_line.dwo",
734       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
735           COFF::IMAGE_SCN_MEM_READ,
736       SectionKind::getMetadata());
737   DwarfLocDWOSection = Ctx->getCOFFSection(
738       ".debug_loc.dwo",
739       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
740           COFF::IMAGE_SCN_MEM_READ,
741       SectionKind::getMetadata(), "skel_loc");
742   DwarfStrOffDWOSection = Ctx->getCOFFSection(
743       ".debug_str_offsets.dwo",
744       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
745           COFF::IMAGE_SCN_MEM_READ,
746       SectionKind::getMetadata(), "section_str_off_dwo");
747   DwarfAddrSection = Ctx->getCOFFSection(
748       ".debug_addr",
749       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
750           COFF::IMAGE_SCN_MEM_READ,
751       SectionKind::getMetadata(), "addr_sec");
752   DwarfCUIndexSection = Ctx->getCOFFSection(
753       ".debug_cu_index",
754       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
755           COFF::IMAGE_SCN_MEM_READ,
756       SectionKind::getMetadata());
757   DwarfTUIndexSection = Ctx->getCOFFSection(
758       ".debug_tu_index",
759       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
760           COFF::IMAGE_SCN_MEM_READ,
761       SectionKind::getMetadata());
762   DwarfDebugNamesSection = Ctx->getCOFFSection(
763       ".debug_names",
764       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
765           COFF::IMAGE_SCN_MEM_READ,
766       SectionKind::getMetadata(), "debug_names_begin");
767   DwarfAccelNamesSection = Ctx->getCOFFSection(
768       ".apple_names",
769       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
770           COFF::IMAGE_SCN_MEM_READ,
771       SectionKind::getMetadata(), "names_begin");
772   DwarfAccelNamespaceSection = Ctx->getCOFFSection(
773       ".apple_namespaces",
774       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
775           COFF::IMAGE_SCN_MEM_READ,
776       SectionKind::getMetadata(), "namespac_begin");
777   DwarfAccelTypesSection = Ctx->getCOFFSection(
778       ".apple_types",
779       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
780           COFF::IMAGE_SCN_MEM_READ,
781       SectionKind::getMetadata(), "types_begin");
782   DwarfAccelObjCSection = Ctx->getCOFFSection(
783       ".apple_objc",
784       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
785           COFF::IMAGE_SCN_MEM_READ,
786       SectionKind::getMetadata(), "objc_begin");
787 
788   DrectveSection = Ctx->getCOFFSection(
789       ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE,
790       SectionKind::getMetadata());
791 
792   PDataSection = Ctx->getCOFFSection(
793       ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
794       SectionKind::getData());
795 
796   XDataSection = Ctx->getCOFFSection(
797       ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
798       SectionKind::getData());
799 
800   SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO,
801                                       SectionKind::getMetadata());
802 
803   GEHContSection = Ctx->getCOFFSection(".gehcont$y",
804                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
805                                            COFF::IMAGE_SCN_MEM_READ,
806                                        SectionKind::getMetadata());
807 
808   GFIDsSection = Ctx->getCOFFSection(".gfids$y",
809                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
810                                          COFF::IMAGE_SCN_MEM_READ,
811                                      SectionKind::getMetadata());
812 
813   GIATsSection = Ctx->getCOFFSection(".giats$y",
814                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
815                                          COFF::IMAGE_SCN_MEM_READ,
816                                      SectionKind::getMetadata());
817 
818   GLJMPSection = Ctx->getCOFFSection(".gljmp$y",
819                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
820                                          COFF::IMAGE_SCN_MEM_READ,
821                                      SectionKind::getMetadata());
822 
823   TLSDataSection = Ctx->getCOFFSection(
824       ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
825                    COFF::IMAGE_SCN_MEM_WRITE,
826       SectionKind::getData());
827 
828   StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps",
829                                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
830                                             COFF::IMAGE_SCN_MEM_READ,
831                                         SectionKind::getReadOnly());
832 }
833 
834 void MCObjectFileInfo::initSPIRVMCObjectFileInfo(const Triple &T) {
835   // Put everything in a single binary section.
836   TextSection = Ctx->getSPIRVSection();
837 }
838 
839 void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
840   TextSection = Ctx->getWasmSection(".text", SectionKind::getText());
841   DataSection = Ctx->getWasmSection(".data", SectionKind::getData());
842 
843   DwarfLineSection =
844       Ctx->getWasmSection(".debug_line", SectionKind::getMetadata());
845   DwarfLineStrSection =
846       Ctx->getWasmSection(".debug_line_str", SectionKind::getMetadata(),
847                           wasm::WASM_SEG_FLAG_STRINGS);
848   DwarfStrSection = Ctx->getWasmSection(
849       ".debug_str", SectionKind::getMetadata(), wasm::WASM_SEG_FLAG_STRINGS);
850   DwarfLocSection =
851       Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata());
852   DwarfAbbrevSection =
853       Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata());
854   DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata());
855   DwarfRangesSection =
856       Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata());
857   DwarfMacinfoSection =
858       Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata());
859   DwarfMacroSection =
860       Ctx->getWasmSection(".debug_macro", SectionKind::getMetadata());
861   DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
862   DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
863   DwarfInfoSection =
864       Ctx->getWasmSection(".debug_info", SectionKind::getMetadata());
865   DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata());
866   DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata());
867   DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata());
868   DwarfGnuPubNamesSection =
869       Ctx->getWasmSection(".debug_gnu_pubnames", SectionKind::getMetadata());
870   DwarfGnuPubTypesSection =
871       Ctx->getWasmSection(".debug_gnu_pubtypes", SectionKind::getMetadata());
872 
873   DwarfDebugNamesSection =
874       Ctx->getWasmSection(".debug_names", SectionKind::getMetadata());
875   DwarfStrOffSection =
876       Ctx->getWasmSection(".debug_str_offsets", SectionKind::getMetadata());
877   DwarfAddrSection =
878       Ctx->getWasmSection(".debug_addr", SectionKind::getMetadata());
879   DwarfRnglistsSection =
880       Ctx->getWasmSection(".debug_rnglists", SectionKind::getMetadata());
881   DwarfLoclistsSection =
882       Ctx->getWasmSection(".debug_loclists", SectionKind::getMetadata());
883 
884   // Fission Sections
885   DwarfInfoDWOSection =
886       Ctx->getWasmSection(".debug_info.dwo", SectionKind::getMetadata());
887   DwarfTypesDWOSection =
888       Ctx->getWasmSection(".debug_types.dwo", SectionKind::getMetadata());
889   DwarfAbbrevDWOSection =
890       Ctx->getWasmSection(".debug_abbrev.dwo", SectionKind::getMetadata());
891   DwarfStrDWOSection =
892       Ctx->getWasmSection(".debug_str.dwo", SectionKind::getMetadata(),
893                           wasm::WASM_SEG_FLAG_STRINGS);
894   DwarfLineDWOSection =
895       Ctx->getWasmSection(".debug_line.dwo", SectionKind::getMetadata());
896   DwarfLocDWOSection =
897       Ctx->getWasmSection(".debug_loc.dwo", SectionKind::getMetadata());
898   DwarfStrOffDWOSection =
899       Ctx->getWasmSection(".debug_str_offsets.dwo", SectionKind::getMetadata());
900   DwarfRnglistsDWOSection =
901       Ctx->getWasmSection(".debug_rnglists.dwo", SectionKind::getMetadata());
902   DwarfMacinfoDWOSection =
903       Ctx->getWasmSection(".debug_macinfo.dwo", SectionKind::getMetadata());
904   DwarfMacroDWOSection =
905       Ctx->getWasmSection(".debug_macro.dwo", SectionKind::getMetadata());
906 
907   DwarfLoclistsDWOSection =
908       Ctx->getWasmSection(".debug_loclists.dwo", SectionKind::getMetadata());
909 
910   // DWP Sections
911   DwarfCUIndexSection =
912       Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
913   DwarfTUIndexSection =
914       Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
915 
916   // Wasm use data section for LSDA.
917   // TODO Consider putting each function's exception table in a separate
918   // section, as in -function-sections, to facilitate lld's --gc-section.
919   LSDASection = Ctx->getWasmSection(".rodata.gcc_except_table",
920                                     SectionKind::getReadOnlyWithRel());
921 
922   // TODO: Define more sections.
923 }
924 
925 void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) {
926   // The default csect for program code. Functions without a specified section
927   // get placed into this csect. The choice of csect name is not a property of
928   // the ABI or object file format, but various tools rely on the section
929   // name being empty (considering named symbols to be "user symbol names").
930   TextSection = Ctx->getXCOFFSection(
931       "", SectionKind::getText(),
932       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_PR, XCOFF::XTY_SD),
933       /* MultiSymbolsAllowed*/ true);
934 
935   DataSection = Ctx->getXCOFFSection(
936       ".data", SectionKind::getData(),
937       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW, XCOFF::XTY_SD),
938       /* MultiSymbolsAllowed*/ true);
939 
940   ReadOnlySection = Ctx->getXCOFFSection(
941       ".rodata", SectionKind::getReadOnly(),
942       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
943       /* MultiSymbolsAllowed*/ true);
944   ReadOnlySection->setAlignment(Align(4));
945 
946   ReadOnly8Section = Ctx->getXCOFFSection(
947       ".rodata.8", SectionKind::getReadOnly(),
948       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
949       /* MultiSymbolsAllowed*/ true);
950   ReadOnly8Section->setAlignment(Align(8));
951 
952   ReadOnly16Section = Ctx->getXCOFFSection(
953       ".rodata.16", SectionKind::getReadOnly(),
954       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
955       /* MultiSymbolsAllowed*/ true);
956   ReadOnly16Section->setAlignment(Align(16));
957 
958   TLSDataSection = Ctx->getXCOFFSection(
959       ".tdata", SectionKind::getThreadData(),
960       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_TL, XCOFF::XTY_SD),
961       /* MultiSymbolsAllowed*/ true);
962 
963   TOCBaseSection = Ctx->getXCOFFSection(
964       "TOC", SectionKind::getData(),
965       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_TC0,
966                              XCOFF::XTY_SD));
967 
968   // The TOC-base always has 0 size, but 4 byte alignment.
969   TOCBaseSection->setAlignment(Align(4));
970 
971   LSDASection = Ctx->getXCOFFSection(
972       ".gcc_except_table", SectionKind::getReadOnly(),
973       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO,
974                              XCOFF::XTY_SD));
975 
976   CompactUnwindSection = Ctx->getXCOFFSection(
977       ".eh_info_table", SectionKind::getData(),
978       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW,
979                              XCOFF::XTY_SD));
980 
981   // DWARF sections for XCOFF are not csects. They are special STYP_DWARF
982   // sections, and the individual DWARF sections are distinguished by their
983   // section subtype.
984   DwarfAbbrevSection = Ctx->getXCOFFSection(
985       ".dwabrev", SectionKind::getMetadata(),
986       /* CsectProperties */ std::nullopt,
987       /* MultiSymbolsAllowed */ true, ".dwabrev", XCOFF::SSUBTYP_DWABREV);
988 
989   DwarfInfoSection = Ctx->getXCOFFSection(
990       ".dwinfo", SectionKind::getMetadata(), /* CsectProperties */ std::nullopt,
991       /* MultiSymbolsAllowed */ true, ".dwinfo", XCOFF::SSUBTYP_DWINFO);
992 
993   DwarfLineSection = Ctx->getXCOFFSection(
994       ".dwline", SectionKind::getMetadata(), /* CsectProperties */ std::nullopt,
995       /* MultiSymbolsAllowed */ true, ".dwline", XCOFF::SSUBTYP_DWLINE);
996 
997   DwarfFrameSection = Ctx->getXCOFFSection(
998       ".dwframe", SectionKind::getMetadata(),
999       /* CsectProperties */ std::nullopt,
1000       /* MultiSymbolsAllowed */ true, ".dwframe", XCOFF::SSUBTYP_DWFRAME);
1001 
1002   DwarfPubNamesSection = Ctx->getXCOFFSection(
1003       ".dwpbnms", SectionKind::getMetadata(),
1004       /* CsectProperties */ std::nullopt,
1005       /* MultiSymbolsAllowed */ true, ".dwpbnms", XCOFF::SSUBTYP_DWPBNMS);
1006 
1007   DwarfPubTypesSection = Ctx->getXCOFFSection(
1008       ".dwpbtyp", SectionKind::getMetadata(),
1009       /* CsectProperties */ std::nullopt,
1010       /* MultiSymbolsAllowed */ true, ".dwpbtyp", XCOFF::SSUBTYP_DWPBTYP);
1011 
1012   DwarfStrSection = Ctx->getXCOFFSection(
1013       ".dwstr", SectionKind::getMetadata(), /* CsectProperties */ std::nullopt,
1014       /* MultiSymbolsAllowed */ true, ".dwstr", XCOFF::SSUBTYP_DWSTR);
1015 
1016   DwarfLocSection = Ctx->getXCOFFSection(
1017       ".dwloc", SectionKind::getMetadata(), /* CsectProperties */ std::nullopt,
1018       /* MultiSymbolsAllowed */ true, ".dwloc", XCOFF::SSUBTYP_DWLOC);
1019 
1020   DwarfARangesSection = Ctx->getXCOFFSection(
1021       ".dwarnge", SectionKind::getMetadata(),
1022       /* CsectProperties */ std::nullopt,
1023       /* MultiSymbolsAllowed */ true, ".dwarnge", XCOFF::SSUBTYP_DWARNGE);
1024 
1025   DwarfRangesSection = Ctx->getXCOFFSection(
1026       ".dwrnges", SectionKind::getMetadata(),
1027       /* CsectProperties */ std::nullopt,
1028       /* MultiSymbolsAllowed */ true, ".dwrnges", XCOFF::SSUBTYP_DWRNGES);
1029 
1030   DwarfMacinfoSection = Ctx->getXCOFFSection(
1031       ".dwmac", SectionKind::getMetadata(), /* CsectProperties */ std::nullopt,
1032       /* MultiSymbolsAllowed */ true, ".dwmac", XCOFF::SSUBTYP_DWMAC);
1033 }
1034 
1035 void MCObjectFileInfo::initDXContainerObjectFileInfo(const Triple &T) {
1036   // At the moment the DXBC section should end up empty.
1037   TextSection = Ctx->getDXContainerSection("DXBC", SectionKind::getText());
1038 }
1039 
1040 MCObjectFileInfo::~MCObjectFileInfo() = default;
1041 
1042 void MCObjectFileInfo::initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
1043                                             bool LargeCodeModel) {
1044   PositionIndependent = PIC;
1045   Ctx = &MCCtx;
1046 
1047   // Common.
1048   SupportsWeakOmittedEHFrame = true;
1049   SupportsCompactUnwindWithoutEHFrame = false;
1050   OmitDwarfIfHaveCompactUnwind = false;
1051 
1052   FDECFIEncoding = dwarf::DW_EH_PE_absptr;
1053 
1054   CompactUnwindDwarfEHFrameOnly = 0;
1055 
1056   EHFrameSection = nullptr;             // Created on demand.
1057   CompactUnwindSection = nullptr;       // Used only by selected targets.
1058   DwarfAccelNamesSection = nullptr;     // Used only by selected targets.
1059   DwarfAccelObjCSection = nullptr;      // Used only by selected targets.
1060   DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
1061   DwarfAccelTypesSection = nullptr;     // Used only by selected targets.
1062 
1063   Triple TheTriple = Ctx->getTargetTriple();
1064   switch (Ctx->getObjectFileType()) {
1065   case MCContext::IsMachO:
1066     initMachOMCObjectFileInfo(TheTriple);
1067     break;
1068   case MCContext::IsCOFF:
1069     initCOFFMCObjectFileInfo(TheTriple);
1070     break;
1071   case MCContext::IsELF:
1072     initELFMCObjectFileInfo(TheTriple, LargeCodeModel);
1073     break;
1074   case MCContext::IsGOFF:
1075     initGOFFMCObjectFileInfo(TheTriple);
1076     break;
1077   case MCContext::IsSPIRV:
1078     initSPIRVMCObjectFileInfo(TheTriple);
1079     break;
1080   case MCContext::IsWasm:
1081     initWasmMCObjectFileInfo(TheTriple);
1082     break;
1083   case MCContext::IsXCOFF:
1084     initXCOFFMCObjectFileInfo(TheTriple);
1085     break;
1086   case MCContext::IsDXContainer:
1087     initDXContainerObjectFileInfo(TheTriple);
1088     break;
1089   }
1090 }
1091 
1092 MCSection *MCObjectFileInfo::getDwarfComdatSection(const char *Name,
1093                                                    uint64_t Hash) const {
1094   switch (Ctx->getTargetTriple().getObjectFormat()) {
1095   case Triple::ELF:
1096     return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, ELF::SHF_GROUP, 0,
1097                               utostr(Hash), /*IsComdat=*/true);
1098   case Triple::Wasm:
1099     return Ctx->getWasmSection(Name, SectionKind::getMetadata(), 0,
1100                                utostr(Hash), MCContext::GenericSectionID);
1101   case Triple::MachO:
1102   case Triple::COFF:
1103   case Triple::GOFF:
1104   case Triple::SPIRV:
1105   case Triple::XCOFF:
1106   case Triple::DXContainer:
1107   case Triple::UnknownObjectFormat:
1108     report_fatal_error("Cannot get DWARF comdat section for this object file "
1109                        "format: not implemented.");
1110     break;
1111   }
1112   llvm_unreachable("Unknown ObjectFormatType");
1113 }
1114 
1115 MCSection *
1116 MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
1117   if ((Ctx->getObjectFileType() != MCContext::IsELF) ||
1118       Ctx->getTargetTriple().isPS4())
1119     return StackSizesSection;
1120 
1121   const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1122   unsigned Flags = ELF::SHF_LINK_ORDER;
1123   StringRef GroupName;
1124   if (const MCSymbol *Group = ElfSec.getGroup()) {
1125     GroupName = Group->getName();
1126     Flags |= ELF::SHF_GROUP;
1127   }
1128 
1129   return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0,
1130                             GroupName, true, ElfSec.getUniqueID(),
1131                             cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1132 }
1133 
1134 MCSection *
1135 MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
1136   if (Ctx->getObjectFileType() != MCContext::IsELF)
1137     return nullptr;
1138 
1139   const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1140   unsigned Flags = ELF::SHF_LINK_ORDER;
1141   StringRef GroupName;
1142   if (const MCSymbol *Group = ElfSec.getGroup()) {
1143     GroupName = Group->getName();
1144     Flags |= ELF::SHF_GROUP;
1145   }
1146 
1147   // Use the text section's begin symbol and unique ID to create a separate
1148   // .llvm_bb_addr_map section associated with every unique text section.
1149   return Ctx->getELFSection(".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP,
1150                             Flags, 0, GroupName, true, ElfSec.getUniqueID(),
1151                             cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1152 }
1153 
1154 MCSection *
1155 MCObjectFileInfo::getKCFITrapSection(const MCSection &TextSec) const {
1156   if (Ctx->getObjectFileType() != MCContext::IsELF)
1157     return nullptr;
1158 
1159   const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1160   unsigned Flags = ELF::SHF_LINK_ORDER | ELF::SHF_ALLOC;
1161   StringRef GroupName;
1162   if (const MCSymbol *Group = ElfSec.getGroup()) {
1163     GroupName = Group->getName();
1164     Flags |= ELF::SHF_GROUP;
1165   }
1166 
1167   return Ctx->getELFSection(".kcfi_traps", ELF::SHT_PROGBITS, Flags, 0,
1168                             GroupName,
1169                             /*IsComdat=*/true, ElfSec.getUniqueID(),
1170                             cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1171 }
1172 
1173 MCSection *
1174 MCObjectFileInfo::getPseudoProbeSection(const MCSection &TextSec) const {
1175   if (Ctx->getObjectFileType() != MCContext::IsELF)
1176     return PseudoProbeSection;
1177 
1178   const auto &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1179   unsigned Flags = ELF::SHF_LINK_ORDER;
1180   StringRef GroupName;
1181   if (const MCSymbol *Group = ElfSec.getGroup()) {
1182     GroupName = Group->getName();
1183     Flags |= ELF::SHF_GROUP;
1184   }
1185 
1186   return Ctx->getELFSection(PseudoProbeSection->getName(), ELF::SHT_PROGBITS,
1187                             Flags, 0, GroupName, true, ElfSec.getUniqueID(),
1188                             cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1189 }
1190 
1191 MCSection *
1192 MCObjectFileInfo::getPseudoProbeDescSection(StringRef FuncName) const {
1193   if (Ctx->getObjectFileType() == MCContext::IsELF) {
1194     // Create a separate comdat group for each function's descriptor in order
1195     // for the linker to deduplicate. The duplication, must be from different
1196     // tranlation unit, can come from:
1197     //  1. Inline functions defined in header files;
1198     //  2. ThinLTO imported funcions;
1199     //  3. Weak-linkage definitions.
1200     // Use a concatenation of the section name and the function name as the
1201     // group name so that descriptor-only groups won't be folded with groups of
1202     // code.
1203     if (Ctx->getTargetTriple().supportsCOMDAT() && !FuncName.empty()) {
1204       auto *S = static_cast<MCSectionELF *>(PseudoProbeDescSection);
1205       auto Flags = S->getFlags() | ELF::SHF_GROUP;
1206       return Ctx->getELFSection(S->getName(), S->getType(), Flags,
1207                                 S->getEntrySize(),
1208                                 S->getName() + "_" + FuncName,
1209                                 /*IsComdat=*/true);
1210     }
1211   }
1212   return PseudoProbeDescSection;
1213 }
1214 
1215 MCSection *MCObjectFileInfo::getLLVMStatsSection() const {
1216   return LLVMStatsSection;
1217 }
1218 
1219 MCSection *MCObjectFileInfo::getPCSection(StringRef Name,
1220                                           const MCSection *TextSec) const {
1221   if (Ctx->getObjectFileType() != MCContext::IsELF)
1222     return nullptr;
1223 
1224   // SHF_WRITE for relocations, and let user post-process data in-place.
1225   unsigned Flags = ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER;
1226 
1227   if (!TextSec)
1228     TextSec = getTextSection();
1229 
1230   StringRef GroupName;
1231   const auto &ElfSec = static_cast<const MCSectionELF &>(*TextSec);
1232   if (const MCSymbol *Group = ElfSec.getGroup()) {
1233     GroupName = Group->getName();
1234     Flags |= ELF::SHF_GROUP;
1235   }
1236   return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, Flags, 0, GroupName, true,
1237                             ElfSec.getUniqueID(),
1238                             cast<MCSymbolELF>(TextSec->getBeginSymbol()));
1239 }
1240