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