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