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