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