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