xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCObjectFileInfo.cpp (revision c7a063741720ef81d4caa4613242579d12f1d605)
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                                                     \
308       [llvm::binaryformat::Swift5ReflectionSectionKind::KIND] =                \
309           Ctx->getMachOSection(Ctx->getSwift5ReflectionSegmentName().data(),   \
310                                MACHO, 0, SectionKind::getMetadata());
311 #include "llvm/BinaryFormat/Swift.def"
312   }
313 
314   TLSExtraDataSection = TLSTLVSection;
315 }
316 
317 void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
318   switch (T.getArch()) {
319   case Triple::mips:
320   case Triple::mipsel:
321   case Triple::mips64:
322   case Triple::mips64el:
323     // We cannot use DW_EH_PE_sdata8 for the large PositionIndependent case
324     // since there is no R_MIPS_PC64 relocation (only a 32-bit version).
325     if (PositionIndependent && !Large)
326       FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
327     else
328       FDECFIEncoding = Ctx->getAsmInfo()->getCodePointerSize() == 4
329                            ? dwarf::DW_EH_PE_sdata4
330                            : dwarf::DW_EH_PE_sdata8;
331     break;
332   case Triple::ppc64:
333   case Triple::ppc64le:
334   case Triple::aarch64:
335   case Triple::aarch64_be:
336   case Triple::x86_64:
337     FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
338                      (Large ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
339     break;
340   case Triple::bpfel:
341   case Triple::bpfeb:
342     FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
343     break;
344   case Triple::hexagon:
345     FDECFIEncoding =
346         PositionIndependent ? dwarf::DW_EH_PE_pcrel : dwarf::DW_EH_PE_absptr;
347     break;
348   default:
349     FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
350     break;
351   }
352 
353   unsigned EHSectionType = T.getArch() == Triple::x86_64
354                                ? ELF::SHT_X86_64_UNWIND
355                                : ELF::SHT_PROGBITS;
356 
357   // Solaris requires different flags for .eh_frame to seemingly every other
358   // platform.
359   unsigned EHSectionFlags = ELF::SHF_ALLOC;
360   if (T.isOSSolaris() && T.getArch() != Triple::x86_64)
361     EHSectionFlags |= ELF::SHF_WRITE;
362 
363   // ELF
364   BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
365                                   ELF::SHF_WRITE | ELF::SHF_ALLOC);
366 
367   TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
368                                    ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
369 
370   DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
371                                    ELF::SHF_WRITE | ELF::SHF_ALLOC);
372 
373   ReadOnlySection =
374       Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
375 
376   TLSDataSection =
377       Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
378                          ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
379 
380   TLSBSSSection = Ctx->getELFSection(
381       ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
382 
383   DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
384                                         ELF::SHF_ALLOC | ELF::SHF_WRITE);
385 
386   MergeableConst4Section =
387       Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
388                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
389 
390   MergeableConst8Section =
391       Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
392                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
393 
394   MergeableConst16Section =
395       Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
396                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
397 
398   MergeableConst32Section =
399       Ctx->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS,
400                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
401 
402   // Exception Handling Sections.
403 
404   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
405   // it contains relocatable pointers.  In PIC mode, this is probably a big
406   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
407   // adjusted or this should be a data section.
408   LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
409                                    ELF::SHF_ALLOC);
410 
411   COFFDebugSymbolsSection = nullptr;
412   COFFDebugTypesSection = nullptr;
413 
414   unsigned DebugSecType = ELF::SHT_PROGBITS;
415 
416   // MIPS .debug_* sections should have SHT_MIPS_DWARF section type
417   // to distinguish among sections contain DWARF and ECOFF debug formats.
418   // Sections with ECOFF debug format are obsoleted and marked by SHT_PROGBITS.
419   if (T.isMIPS())
420     DebugSecType = ELF::SHT_MIPS_DWARF;
421 
422   // Debug Info Sections.
423   DwarfAbbrevSection =
424       Ctx->getELFSection(".debug_abbrev", DebugSecType, 0);
425   DwarfInfoSection = Ctx->getELFSection(".debug_info", DebugSecType, 0);
426   DwarfLineSection = Ctx->getELFSection(".debug_line", DebugSecType, 0);
427   DwarfLineStrSection =
428       Ctx->getELFSection(".debug_line_str", DebugSecType,
429                          ELF::SHF_MERGE | ELF::SHF_STRINGS, 1);
430   DwarfFrameSection = Ctx->getELFSection(".debug_frame", DebugSecType, 0);
431   DwarfPubNamesSection =
432       Ctx->getELFSection(".debug_pubnames", DebugSecType, 0);
433   DwarfPubTypesSection =
434       Ctx->getELFSection(".debug_pubtypes", DebugSecType, 0);
435   DwarfGnuPubNamesSection =
436       Ctx->getELFSection(".debug_gnu_pubnames", DebugSecType, 0);
437   DwarfGnuPubTypesSection =
438       Ctx->getELFSection(".debug_gnu_pubtypes", DebugSecType, 0);
439   DwarfStrSection =
440       Ctx->getELFSection(".debug_str", DebugSecType,
441                          ELF::SHF_MERGE | ELF::SHF_STRINGS, 1);
442   DwarfLocSection = Ctx->getELFSection(".debug_loc", DebugSecType, 0);
443   DwarfARangesSection =
444       Ctx->getELFSection(".debug_aranges", DebugSecType, 0);
445   DwarfRangesSection =
446       Ctx->getELFSection(".debug_ranges", DebugSecType, 0);
447   DwarfMacinfoSection =
448       Ctx->getELFSection(".debug_macinfo", DebugSecType, 0);
449   DwarfMacroSection = Ctx->getELFSection(".debug_macro", DebugSecType, 0);
450 
451   // DWARF5 Experimental Debug Info
452 
453   // Accelerator Tables
454   DwarfDebugNamesSection =
455       Ctx->getELFSection(".debug_names", ELF::SHT_PROGBITS, 0);
456   DwarfAccelNamesSection =
457       Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0);
458   DwarfAccelObjCSection =
459       Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0);
460   DwarfAccelNamespaceSection =
461       Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0);
462   DwarfAccelTypesSection =
463       Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0);
464 
465   // String Offset and Address Sections
466   DwarfStrOffSection =
467       Ctx->getELFSection(".debug_str_offsets", DebugSecType, 0);
468   DwarfAddrSection = Ctx->getELFSection(".debug_addr", DebugSecType, 0);
469   DwarfRnglistsSection = Ctx->getELFSection(".debug_rnglists", DebugSecType, 0);
470   DwarfLoclistsSection = Ctx->getELFSection(".debug_loclists", DebugSecType, 0);
471 
472   // Fission Sections
473   DwarfInfoDWOSection =
474       Ctx->getELFSection(".debug_info.dwo", DebugSecType, ELF::SHF_EXCLUDE);
475   DwarfTypesDWOSection =
476       Ctx->getELFSection(".debug_types.dwo", DebugSecType, ELF::SHF_EXCLUDE);
477   DwarfAbbrevDWOSection =
478       Ctx->getELFSection(".debug_abbrev.dwo", DebugSecType, ELF::SHF_EXCLUDE);
479   DwarfStrDWOSection = Ctx->getELFSection(
480       ".debug_str.dwo", DebugSecType,
481       ELF::SHF_MERGE | ELF::SHF_STRINGS | ELF::SHF_EXCLUDE, 1);
482   DwarfLineDWOSection =
483       Ctx->getELFSection(".debug_line.dwo", DebugSecType, ELF::SHF_EXCLUDE);
484   DwarfLocDWOSection =
485       Ctx->getELFSection(".debug_loc.dwo", DebugSecType, ELF::SHF_EXCLUDE);
486   DwarfStrOffDWOSection = Ctx->getELFSection(".debug_str_offsets.dwo",
487                                              DebugSecType, ELF::SHF_EXCLUDE);
488   DwarfRnglistsDWOSection =
489       Ctx->getELFSection(".debug_rnglists.dwo", DebugSecType, ELF::SHF_EXCLUDE);
490   DwarfMacinfoDWOSection =
491       Ctx->getELFSection(".debug_macinfo.dwo", DebugSecType, ELF::SHF_EXCLUDE);
492   DwarfMacroDWOSection =
493       Ctx->getELFSection(".debug_macro.dwo", DebugSecType, ELF::SHF_EXCLUDE);
494 
495   DwarfLoclistsDWOSection =
496       Ctx->getELFSection(".debug_loclists.dwo", DebugSecType, ELF::SHF_EXCLUDE);
497 
498   // DWP Sections
499   DwarfCUIndexSection =
500       Ctx->getELFSection(".debug_cu_index", DebugSecType, 0);
501   DwarfTUIndexSection =
502       Ctx->getELFSection(".debug_tu_index", DebugSecType, 0);
503 
504   StackMapSection =
505       Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
506 
507   FaultMapSection =
508       Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
509 
510   EHFrameSection =
511       Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);
512 
513   StackSizesSection = Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, 0);
514 
515   PseudoProbeSection = Ctx->getELFSection(".pseudo_probe", DebugSecType, 0);
516   PseudoProbeDescSection =
517       Ctx->getELFSection(".pseudo_probe_desc", DebugSecType, 0);
518 }
519 
520 void MCObjectFileInfo::initGOFFMCObjectFileInfo(const Triple &T) {
521   TextSection = Ctx->getGOFFSection(".text", SectionKind::getText());
522   BSSSection = Ctx->getGOFFSection(".bss", SectionKind::getBSS());
523 }
524 
525 void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
526   EHFrameSection =
527       Ctx->getCOFFSection(".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
528                                            COFF::IMAGE_SCN_MEM_READ,
529                           SectionKind::getData());
530 
531   // Set the `IMAGE_SCN_MEM_16BIT` flag when compiling for thumb mode.  This is
532   // used to indicate to the linker that the text segment contains thumb instructions
533   // and to set the ISA selection bit for calls accordingly.
534   const bool IsThumb = T.getArch() == Triple::thumb;
535 
536   CommDirectiveSupportsAlignment = true;
537 
538   // COFF
539   BSSSection = Ctx->getCOFFSection(
540       ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
541                   COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
542       SectionKind::getBSS());
543   TextSection = Ctx->getCOFFSection(
544       ".text",
545       (IsThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) |
546           COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
547           COFF::IMAGE_SCN_MEM_READ,
548       SectionKind::getText());
549   DataSection = Ctx->getCOFFSection(
550       ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
551                    COFF::IMAGE_SCN_MEM_WRITE,
552       SectionKind::getData());
553   ReadOnlySection = Ctx->getCOFFSection(
554       ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
555       SectionKind::getReadOnly());
556 
557   if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::aarch64) {
558     // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
559     LSDASection = nullptr;
560   } else {
561     LSDASection = Ctx->getCOFFSection(".gcc_except_table",
562                                       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
563                                           COFF::IMAGE_SCN_MEM_READ,
564                                       SectionKind::getReadOnly());
565   }
566 
567   // Debug info.
568   COFFDebugSymbolsSection =
569       Ctx->getCOFFSection(".debug$S", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
570                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
571                                        COFF::IMAGE_SCN_MEM_READ),
572                           SectionKind::getMetadata());
573   COFFDebugTypesSection =
574       Ctx->getCOFFSection(".debug$T", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
575                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
576                                        COFF::IMAGE_SCN_MEM_READ),
577                           SectionKind::getMetadata());
578   COFFGlobalTypeHashesSection = Ctx->getCOFFSection(
579       ".debug$H",
580       (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
581        COFF::IMAGE_SCN_MEM_READ),
582       SectionKind::getMetadata());
583 
584   DwarfAbbrevSection = Ctx->getCOFFSection(
585       ".debug_abbrev",
586       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
587           COFF::IMAGE_SCN_MEM_READ,
588       SectionKind::getMetadata(), "section_abbrev");
589   DwarfInfoSection = Ctx->getCOFFSection(
590       ".debug_info",
591       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
592           COFF::IMAGE_SCN_MEM_READ,
593       SectionKind::getMetadata(), "section_info");
594   DwarfLineSection = Ctx->getCOFFSection(
595       ".debug_line",
596       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
597           COFF::IMAGE_SCN_MEM_READ,
598       SectionKind::getMetadata(), "section_line");
599   DwarfLineStrSection = Ctx->getCOFFSection(
600       ".debug_line_str",
601       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
602           COFF::IMAGE_SCN_MEM_READ,
603       SectionKind::getMetadata(), "section_line_str");
604   DwarfFrameSection = Ctx->getCOFFSection(
605       ".debug_frame",
606       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
607           COFF::IMAGE_SCN_MEM_READ,
608       SectionKind::getMetadata());
609   DwarfPubNamesSection = Ctx->getCOFFSection(
610       ".debug_pubnames",
611       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
612           COFF::IMAGE_SCN_MEM_READ,
613       SectionKind::getMetadata());
614   DwarfPubTypesSection = Ctx->getCOFFSection(
615       ".debug_pubtypes",
616       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
617           COFF::IMAGE_SCN_MEM_READ,
618       SectionKind::getMetadata());
619   DwarfGnuPubNamesSection = Ctx->getCOFFSection(
620       ".debug_gnu_pubnames",
621       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
622           COFF::IMAGE_SCN_MEM_READ,
623       SectionKind::getMetadata());
624   DwarfGnuPubTypesSection = Ctx->getCOFFSection(
625       ".debug_gnu_pubtypes",
626       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
627           COFF::IMAGE_SCN_MEM_READ,
628       SectionKind::getMetadata());
629   DwarfStrSection = Ctx->getCOFFSection(
630       ".debug_str",
631       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
632           COFF::IMAGE_SCN_MEM_READ,
633       SectionKind::getMetadata(), "info_string");
634   DwarfStrOffSection = Ctx->getCOFFSection(
635       ".debug_str_offsets",
636       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
637           COFF::IMAGE_SCN_MEM_READ,
638       SectionKind::getMetadata(), "section_str_off");
639   DwarfLocSection = Ctx->getCOFFSection(
640       ".debug_loc",
641       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
642           COFF::IMAGE_SCN_MEM_READ,
643       SectionKind::getMetadata(), "section_debug_loc");
644   DwarfLoclistsSection = Ctx->getCOFFSection(
645       ".debug_loclists",
646       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
647           COFF::IMAGE_SCN_MEM_READ,
648       SectionKind::getMetadata(), "section_debug_loclists");
649   DwarfARangesSection = Ctx->getCOFFSection(
650       ".debug_aranges",
651       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
652           COFF::IMAGE_SCN_MEM_READ,
653       SectionKind::getMetadata());
654   DwarfRangesSection = Ctx->getCOFFSection(
655       ".debug_ranges",
656       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
657           COFF::IMAGE_SCN_MEM_READ,
658       SectionKind::getMetadata(), "debug_range");
659   DwarfRnglistsSection = Ctx->getCOFFSection(
660       ".debug_rnglists",
661       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
662           COFF::IMAGE_SCN_MEM_READ,
663       SectionKind::getMetadata(), "debug_rnglists");
664   DwarfMacinfoSection = Ctx->getCOFFSection(
665       ".debug_macinfo",
666       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
667           COFF::IMAGE_SCN_MEM_READ,
668       SectionKind::getMetadata(), "debug_macinfo");
669   DwarfMacroSection = Ctx->getCOFFSection(
670       ".debug_macro",
671       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
672           COFF::IMAGE_SCN_MEM_READ,
673       SectionKind::getMetadata(), "debug_macro");
674   DwarfMacinfoDWOSection = Ctx->getCOFFSection(
675       ".debug_macinfo.dwo",
676       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
677           COFF::IMAGE_SCN_MEM_READ,
678       SectionKind::getMetadata(), "debug_macinfo.dwo");
679   DwarfMacroDWOSection = Ctx->getCOFFSection(
680       ".debug_macro.dwo",
681       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
682           COFF::IMAGE_SCN_MEM_READ,
683       SectionKind::getMetadata(), "debug_macro.dwo");
684   DwarfInfoDWOSection = Ctx->getCOFFSection(
685       ".debug_info.dwo",
686       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
687           COFF::IMAGE_SCN_MEM_READ,
688       SectionKind::getMetadata(), "section_info_dwo");
689   DwarfTypesDWOSection = Ctx->getCOFFSection(
690       ".debug_types.dwo",
691       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
692           COFF::IMAGE_SCN_MEM_READ,
693       SectionKind::getMetadata(), "section_types_dwo");
694   DwarfAbbrevDWOSection = Ctx->getCOFFSection(
695       ".debug_abbrev.dwo",
696       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
697           COFF::IMAGE_SCN_MEM_READ,
698       SectionKind::getMetadata(), "section_abbrev_dwo");
699   DwarfStrDWOSection = Ctx->getCOFFSection(
700       ".debug_str.dwo",
701       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
702           COFF::IMAGE_SCN_MEM_READ,
703       SectionKind::getMetadata(), "skel_string");
704   DwarfLineDWOSection = Ctx->getCOFFSection(
705       ".debug_line.dwo",
706       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
707           COFF::IMAGE_SCN_MEM_READ,
708       SectionKind::getMetadata());
709   DwarfLocDWOSection = Ctx->getCOFFSection(
710       ".debug_loc.dwo",
711       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
712           COFF::IMAGE_SCN_MEM_READ,
713       SectionKind::getMetadata(), "skel_loc");
714   DwarfStrOffDWOSection = Ctx->getCOFFSection(
715       ".debug_str_offsets.dwo",
716       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
717           COFF::IMAGE_SCN_MEM_READ,
718       SectionKind::getMetadata(), "section_str_off_dwo");
719   DwarfAddrSection = Ctx->getCOFFSection(
720       ".debug_addr",
721       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
722           COFF::IMAGE_SCN_MEM_READ,
723       SectionKind::getMetadata(), "addr_sec");
724   DwarfCUIndexSection = Ctx->getCOFFSection(
725       ".debug_cu_index",
726       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
727           COFF::IMAGE_SCN_MEM_READ,
728       SectionKind::getMetadata());
729   DwarfTUIndexSection = Ctx->getCOFFSection(
730       ".debug_tu_index",
731       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
732           COFF::IMAGE_SCN_MEM_READ,
733       SectionKind::getMetadata());
734   DwarfDebugNamesSection = Ctx->getCOFFSection(
735       ".debug_names",
736       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
737           COFF::IMAGE_SCN_MEM_READ,
738       SectionKind::getMetadata(), "debug_names_begin");
739   DwarfAccelNamesSection = Ctx->getCOFFSection(
740       ".apple_names",
741       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
742           COFF::IMAGE_SCN_MEM_READ,
743       SectionKind::getMetadata(), "names_begin");
744   DwarfAccelNamespaceSection = Ctx->getCOFFSection(
745       ".apple_namespaces",
746       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
747           COFF::IMAGE_SCN_MEM_READ,
748       SectionKind::getMetadata(), "namespac_begin");
749   DwarfAccelTypesSection = Ctx->getCOFFSection(
750       ".apple_types",
751       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
752           COFF::IMAGE_SCN_MEM_READ,
753       SectionKind::getMetadata(), "types_begin");
754   DwarfAccelObjCSection = Ctx->getCOFFSection(
755       ".apple_objc",
756       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
757           COFF::IMAGE_SCN_MEM_READ,
758       SectionKind::getMetadata(), "objc_begin");
759 
760   DrectveSection = Ctx->getCOFFSection(
761       ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE,
762       SectionKind::getMetadata());
763 
764   PDataSection = Ctx->getCOFFSection(
765       ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
766       SectionKind::getData());
767 
768   XDataSection = Ctx->getCOFFSection(
769       ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
770       SectionKind::getData());
771 
772   SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO,
773                                       SectionKind::getMetadata());
774 
775   GEHContSection = Ctx->getCOFFSection(".gehcont$y",
776                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
777                                            COFF::IMAGE_SCN_MEM_READ,
778                                        SectionKind::getMetadata());
779 
780   GFIDsSection = Ctx->getCOFFSection(".gfids$y",
781                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
782                                          COFF::IMAGE_SCN_MEM_READ,
783                                      SectionKind::getMetadata());
784 
785   GIATsSection = Ctx->getCOFFSection(".giats$y",
786                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
787                                          COFF::IMAGE_SCN_MEM_READ,
788                                      SectionKind::getMetadata());
789 
790   GLJMPSection = Ctx->getCOFFSection(".gljmp$y",
791                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
792                                          COFF::IMAGE_SCN_MEM_READ,
793                                      SectionKind::getMetadata());
794 
795   TLSDataSection = Ctx->getCOFFSection(
796       ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
797                    COFF::IMAGE_SCN_MEM_WRITE,
798       SectionKind::getData());
799 
800   StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps",
801                                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
802                                             COFF::IMAGE_SCN_MEM_READ,
803                                         SectionKind::getReadOnly());
804 }
805 
806 void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
807   TextSection = Ctx->getWasmSection(".text", SectionKind::getText());
808   DataSection = Ctx->getWasmSection(".data", SectionKind::getData());
809 
810   DwarfLineSection =
811       Ctx->getWasmSection(".debug_line", SectionKind::getMetadata());
812   DwarfLineStrSection =
813       Ctx->getWasmSection(".debug_line_str", SectionKind::getMetadata(),
814                           wasm::WASM_SEG_FLAG_STRINGS);
815   DwarfStrSection = Ctx->getWasmSection(
816       ".debug_str", SectionKind::getMetadata(), wasm::WASM_SEG_FLAG_STRINGS);
817   DwarfLocSection =
818       Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata());
819   DwarfAbbrevSection =
820       Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata());
821   DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata());
822   DwarfRangesSection =
823       Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata());
824   DwarfMacinfoSection =
825       Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata());
826   DwarfMacroSection =
827       Ctx->getWasmSection(".debug_macro", SectionKind::getMetadata());
828   DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
829   DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
830   DwarfInfoSection =
831       Ctx->getWasmSection(".debug_info", SectionKind::getMetadata());
832   DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata());
833   DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata());
834   DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata());
835   DwarfGnuPubNamesSection =
836       Ctx->getWasmSection(".debug_gnu_pubnames", SectionKind::getMetadata());
837   DwarfGnuPubTypesSection =
838       Ctx->getWasmSection(".debug_gnu_pubtypes", SectionKind::getMetadata());
839 
840   DwarfDebugNamesSection =
841       Ctx->getWasmSection(".debug_names", SectionKind::getMetadata());
842   DwarfStrOffSection =
843       Ctx->getWasmSection(".debug_str_offsets", SectionKind::getMetadata());
844   DwarfAddrSection =
845       Ctx->getWasmSection(".debug_addr", SectionKind::getMetadata());
846   DwarfRnglistsSection =
847       Ctx->getWasmSection(".debug_rnglists", SectionKind::getMetadata());
848   DwarfLoclistsSection =
849       Ctx->getWasmSection(".debug_loclists", SectionKind::getMetadata());
850 
851   // Fission Sections
852   DwarfInfoDWOSection =
853       Ctx->getWasmSection(".debug_info.dwo", SectionKind::getMetadata());
854   DwarfTypesDWOSection =
855       Ctx->getWasmSection(".debug_types.dwo", SectionKind::getMetadata());
856   DwarfAbbrevDWOSection =
857       Ctx->getWasmSection(".debug_abbrev.dwo", SectionKind::getMetadata());
858   DwarfStrDWOSection =
859       Ctx->getWasmSection(".debug_str.dwo", SectionKind::getMetadata(),
860                           wasm::WASM_SEG_FLAG_STRINGS);
861   DwarfLineDWOSection =
862       Ctx->getWasmSection(".debug_line.dwo", SectionKind::getMetadata());
863   DwarfLocDWOSection =
864       Ctx->getWasmSection(".debug_loc.dwo", SectionKind::getMetadata());
865   DwarfStrOffDWOSection =
866       Ctx->getWasmSection(".debug_str_offsets.dwo", SectionKind::getMetadata());
867   DwarfRnglistsDWOSection =
868       Ctx->getWasmSection(".debug_rnglists.dwo", SectionKind::getMetadata());
869   DwarfMacinfoDWOSection =
870       Ctx->getWasmSection(".debug_macinfo.dwo", SectionKind::getMetadata());
871   DwarfMacroDWOSection =
872       Ctx->getWasmSection(".debug_macro.dwo", SectionKind::getMetadata());
873 
874   DwarfLoclistsDWOSection =
875       Ctx->getWasmSection(".debug_loclists.dwo", SectionKind::getMetadata());
876 
877   // DWP Sections
878   DwarfCUIndexSection =
879       Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
880   DwarfTUIndexSection =
881       Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
882 
883   // Wasm use data section for LSDA.
884   // TODO Consider putting each function's exception table in a separate
885   // section, as in -function-sections, to facilitate lld's --gc-section.
886   LSDASection = Ctx->getWasmSection(".rodata.gcc_except_table",
887                                     SectionKind::getReadOnlyWithRel());
888 
889   // TODO: Define more sections.
890 }
891 
892 void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) {
893   // The default csect for program code. Functions without a specified section
894   // get placed into this csect. The choice of csect name is not a property of
895   // the ABI or object file format. For example, the XL compiler uses an unnamed
896   // csect for program code.
897   TextSection = Ctx->getXCOFFSection(
898       ".text", SectionKind::getText(),
899       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_PR, XCOFF::XTY_SD),
900       /* MultiSymbolsAllowed*/ true);
901 
902   DataSection = Ctx->getXCOFFSection(
903       ".data", SectionKind::getData(),
904       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW, XCOFF::XTY_SD),
905       /* MultiSymbolsAllowed*/ true);
906 
907   ReadOnlySection = Ctx->getXCOFFSection(
908       ".rodata", SectionKind::getReadOnly(),
909       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
910       /* MultiSymbolsAllowed*/ true);
911   ReadOnlySection->setAlignment(Align(4));
912 
913   ReadOnly8Section = Ctx->getXCOFFSection(
914       ".rodata.8", SectionKind::getReadOnly(),
915       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
916       /* MultiSymbolsAllowed*/ true);
917   ReadOnly8Section->setAlignment(Align(8));
918 
919   ReadOnly16Section = Ctx->getXCOFFSection(
920       ".rodata.16", SectionKind::getReadOnly(),
921       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
922       /* MultiSymbolsAllowed*/ true);
923   ReadOnly16Section->setAlignment(Align(16));
924 
925   TLSDataSection = Ctx->getXCOFFSection(
926       ".tdata", SectionKind::getThreadData(),
927       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_TL, XCOFF::XTY_SD),
928       /* MultiSymbolsAllowed*/ true);
929 
930   TOCBaseSection = Ctx->getXCOFFSection(
931       "TOC", SectionKind::getData(),
932       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_TC0,
933                              XCOFF::XTY_SD));
934 
935   // The TOC-base always has 0 size, but 4 byte alignment.
936   TOCBaseSection->setAlignment(Align(4));
937 
938   LSDASection = Ctx->getXCOFFSection(
939       ".gcc_except_table", SectionKind::getReadOnly(),
940       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO,
941                              XCOFF::XTY_SD));
942 
943   CompactUnwindSection = Ctx->getXCOFFSection(
944       ".eh_info_table", SectionKind::getData(),
945       XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW,
946                              XCOFF::XTY_SD));
947 
948   // DWARF sections for XCOFF are not csects. They are special STYP_DWARF
949   // sections, and the individual DWARF sections are distinguished by their
950   // section subtype.
951   DwarfAbbrevSection = Ctx->getXCOFFSection(
952       ".dwabrev", SectionKind::getMetadata(), /* CsectProperties */ None,
953       /* MultiSymbolsAllowed */ true, ".dwabrev", XCOFF::SSUBTYP_DWABREV);
954 
955   DwarfInfoSection = Ctx->getXCOFFSection(
956       ".dwinfo", SectionKind::getMetadata(), /* CsectProperties */ None,
957       /* MultiSymbolsAllowed */ true, ".dwinfo", XCOFF::SSUBTYP_DWINFO);
958 
959   DwarfLineSection = Ctx->getXCOFFSection(
960       ".dwline", SectionKind::getMetadata(), /* CsectProperties */ None,
961       /* MultiSymbolsAllowed */ true, ".dwline", XCOFF::SSUBTYP_DWLINE);
962 
963   DwarfFrameSection = Ctx->getXCOFFSection(
964       ".dwframe", SectionKind::getMetadata(), /* CsectProperties */ None,
965       /* MultiSymbolsAllowed */ true, ".dwframe", XCOFF::SSUBTYP_DWFRAME);
966 
967   DwarfPubNamesSection = Ctx->getXCOFFSection(
968       ".dwpbnms", SectionKind::getMetadata(), /* CsectProperties */ None,
969       /* MultiSymbolsAllowed */ true, ".dwpbnms", XCOFF::SSUBTYP_DWPBNMS);
970 
971   DwarfPubTypesSection = Ctx->getXCOFFSection(
972       ".dwpbtyp", SectionKind::getMetadata(), /* CsectProperties */ None,
973       /* MultiSymbolsAllowed */ true, ".dwpbtyp", XCOFF::SSUBTYP_DWPBTYP);
974 
975   DwarfStrSection = Ctx->getXCOFFSection(
976       ".dwstr", SectionKind::getMetadata(), /* CsectProperties */ None,
977       /* MultiSymbolsAllowed */ true, ".dwstr", XCOFF::SSUBTYP_DWSTR);
978 
979   DwarfLocSection = Ctx->getXCOFFSection(
980       ".dwloc", SectionKind::getMetadata(), /* CsectProperties */ None,
981       /* MultiSymbolsAllowed */ true, ".dwloc", XCOFF::SSUBTYP_DWLOC);
982 
983   DwarfARangesSection = Ctx->getXCOFFSection(
984       ".dwarnge", SectionKind::getMetadata(), /* CsectProperties */ None,
985       /* MultiSymbolsAllowed */ true, ".dwarnge", XCOFF::SSUBTYP_DWARNGE);
986 
987   DwarfRangesSection = Ctx->getXCOFFSection(
988       ".dwrnges", SectionKind::getMetadata(), /* CsectProperties */ None,
989       /* MultiSymbolsAllowed */ true, ".dwrnges", XCOFF::SSUBTYP_DWRNGES);
990 
991   DwarfMacinfoSection = Ctx->getXCOFFSection(
992       ".dwmac", SectionKind::getMetadata(), /* CsectProperties */ None,
993       /* MultiSymbolsAllowed */ true, ".dwmac", XCOFF::SSUBTYP_DWMAC);
994 }
995 
996 MCObjectFileInfo::~MCObjectFileInfo() {}
997 
998 void MCObjectFileInfo::initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
999                                             bool LargeCodeModel) {
1000   PositionIndependent = PIC;
1001   Ctx = &MCCtx;
1002 
1003   // Common.
1004   CommDirectiveSupportsAlignment = true;
1005   SupportsWeakOmittedEHFrame = true;
1006   SupportsCompactUnwindWithoutEHFrame = false;
1007   OmitDwarfIfHaveCompactUnwind = false;
1008 
1009   FDECFIEncoding = dwarf::DW_EH_PE_absptr;
1010 
1011   CompactUnwindDwarfEHFrameOnly = 0;
1012 
1013   EHFrameSection = nullptr;             // Created on demand.
1014   CompactUnwindSection = nullptr;       // Used only by selected targets.
1015   DwarfAccelNamesSection = nullptr;     // Used only by selected targets.
1016   DwarfAccelObjCSection = nullptr;      // Used only by selected targets.
1017   DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
1018   DwarfAccelTypesSection = nullptr;     // Used only by selected targets.
1019 
1020   Triple TheTriple = Ctx->getTargetTriple();
1021   switch (Ctx->getObjectFileType()) {
1022   case MCContext::IsMachO:
1023     initMachOMCObjectFileInfo(TheTriple);
1024     break;
1025   case MCContext::IsCOFF:
1026     initCOFFMCObjectFileInfo(TheTriple);
1027     break;
1028   case MCContext::IsELF:
1029     initELFMCObjectFileInfo(TheTriple, LargeCodeModel);
1030     break;
1031   case MCContext::IsGOFF:
1032     initGOFFMCObjectFileInfo(TheTriple);
1033     break;
1034   case MCContext::IsWasm:
1035     initWasmMCObjectFileInfo(TheTriple);
1036     break;
1037   case MCContext::IsXCOFF:
1038     initXCOFFMCObjectFileInfo(TheTriple);
1039     break;
1040   }
1041 }
1042 
1043 MCSection *MCObjectFileInfo::getDwarfComdatSection(const char *Name,
1044                                                    uint64_t Hash) const {
1045   switch (Ctx->getTargetTriple().getObjectFormat()) {
1046   case Triple::ELF:
1047     return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, ELF::SHF_GROUP, 0,
1048                               utostr(Hash), /*IsComdat=*/true);
1049   case Triple::Wasm:
1050     return Ctx->getWasmSection(Name, SectionKind::getMetadata(), 0,
1051                                utostr(Hash), MCContext::GenericSectionID);
1052   case Triple::MachO:
1053   case Triple::COFF:
1054   case Triple::GOFF:
1055   case Triple::XCOFF:
1056   case Triple::UnknownObjectFormat:
1057     report_fatal_error("Cannot get DWARF comdat section for this object file "
1058                        "format: not implemented.");
1059     break;
1060   }
1061   llvm_unreachable("Unknown ObjectFormatType");
1062 }
1063 
1064 MCSection *
1065 MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
1066   if (Ctx->getObjectFileType() != MCContext::IsELF)
1067     return StackSizesSection;
1068 
1069   const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1070   unsigned Flags = ELF::SHF_LINK_ORDER;
1071   StringRef GroupName;
1072   if (const MCSymbol *Group = ElfSec.getGroup()) {
1073     GroupName = Group->getName();
1074     Flags |= ELF::SHF_GROUP;
1075   }
1076 
1077   return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0,
1078                             GroupName, true, ElfSec.getUniqueID(),
1079                             cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1080 }
1081 
1082 MCSection *
1083 MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
1084   if (Ctx->getObjectFileType() != MCContext::IsELF)
1085     return nullptr;
1086 
1087   const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1088   unsigned Flags = ELF::SHF_LINK_ORDER;
1089   StringRef GroupName;
1090   if (const MCSymbol *Group = ElfSec.getGroup()) {
1091     GroupName = Group->getName();
1092     Flags |= ELF::SHF_GROUP;
1093   }
1094 
1095   // Use the text section's begin symbol and unique ID to create a separate
1096   // .llvm_bb_addr_map section associated with every unique text section.
1097   return Ctx->getELFSection(".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP,
1098                             Flags, 0, GroupName, true, ElfSec.getUniqueID(),
1099                             cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1100 }
1101 
1102 MCSection *
1103 MCObjectFileInfo::getPseudoProbeSection(const MCSection *TextSec) const {
1104   if (Ctx->getObjectFileType() == MCContext::IsELF) {
1105     const auto *ElfSec = static_cast<const MCSectionELF *>(TextSec);
1106     // Create a separate section for probes that comes with a comdat function.
1107     if (const MCSymbol *Group = ElfSec->getGroup()) {
1108       auto *S = static_cast<MCSectionELF *>(PseudoProbeSection);
1109       auto Flags = S->getFlags() | ELF::SHF_GROUP;
1110       return Ctx->getELFSection(S->getName(), S->getType(), Flags,
1111                                 S->getEntrySize(), Group->getName(),
1112                                 /*IsComdat=*/true);
1113     }
1114   }
1115   return PseudoProbeSection;
1116 }
1117 
1118 MCSection *
1119 MCObjectFileInfo::getPseudoProbeDescSection(StringRef FuncName) const {
1120   if (Ctx->getObjectFileType() == MCContext::IsELF) {
1121     // Create a separate comdat group for each function's descriptor in order
1122     // for the linker to deduplicate. The duplication, must be from different
1123     // tranlation unit, can come from:
1124     //  1. Inline functions defined in header files;
1125     //  2. ThinLTO imported funcions;
1126     //  3. Weak-linkage definitions.
1127     // Use a concatenation of the section name and the function name as the
1128     // group name so that descriptor-only groups won't be folded with groups of
1129     // code.
1130     if (Ctx->getTargetTriple().supportsCOMDAT() && !FuncName.empty()) {
1131       auto *S = static_cast<MCSectionELF *>(PseudoProbeDescSection);
1132       auto Flags = S->getFlags() | ELF::SHF_GROUP;
1133       return Ctx->getELFSection(S->getName(), S->getType(), Flags,
1134                                 S->getEntrySize(),
1135                                 S->getName() + "_" + FuncName,
1136                                 /*IsComdat=*/true);
1137     }
1138   }
1139   return PseudoProbeDescSection;
1140 }
1141