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