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