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