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