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