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