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