xref: /freebsd-src/contrib/llvm-project/llvm/lib/MC/MCObjectFileInfo.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
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 
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() &&
44       (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86))
45     return true;
46 
47   return false;
48 }
49 
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.getArch() == Triple::x86_64 || T.getArch() == Triple::x86)
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   DwarfDebugInlineSection =
281       Ctx->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG,
282                            SectionKind::getMetadata());
283   DwarfCUIndexSection =
284       Ctx->getMachOSection("__DWARF", "__debug_cu_index", MachO::S_ATTR_DEBUG,
285                            SectionKind::getMetadata());
286   DwarfTUIndexSection =
287       Ctx->getMachOSection("__DWARF", "__debug_tu_index", MachO::S_ATTR_DEBUG,
288                            SectionKind::getMetadata());
289   StackMapSection = Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps",
290                                          0, SectionKind::getMetadata());
291 
292   FaultMapSection = Ctx->getMachOSection("__LLVM_FAULTMAPS", "__llvm_faultmaps",
293                                          0, SectionKind::getMetadata());
294 
295   RemarksSection = Ctx->getMachOSection(
296       "__LLVM", "__remarks", MachO::S_ATTR_DEBUG, SectionKind::getMetadata());
297 
298   TLSExtraDataSection = TLSTLVSection;
299 }
300 
301 void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
302   switch (T.getArch()) {
303   case Triple::mips:
304   case Triple::mipsel:
305   case Triple::mips64:
306   case Triple::mips64el:
307     // We cannot use DW_EH_PE_sdata8 for the large PositionIndependent case
308     // since there is no R_MIPS_PC64 relocation (only a 32-bit version).
309     if (PositionIndependent && !Large)
310       FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
311     else
312       FDECFIEncoding = Ctx->getAsmInfo()->getCodePointerSize() == 4
313                            ? dwarf::DW_EH_PE_sdata4
314                            : dwarf::DW_EH_PE_sdata8;
315     break;
316   case Triple::ppc64:
317   case Triple::ppc64le:
318   case Triple::x86_64:
319     FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
320                      (Large ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
321     break;
322   case Triple::bpfel:
323   case Triple::bpfeb:
324     FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
325     break;
326   case Triple::hexagon:
327     FDECFIEncoding =
328         PositionIndependent ? dwarf::DW_EH_PE_pcrel : dwarf::DW_EH_PE_absptr;
329     break;
330   default:
331     FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
332     break;
333   }
334 
335   unsigned EHSectionType = T.getArch() == Triple::x86_64
336                                ? ELF::SHT_X86_64_UNWIND
337                                : ELF::SHT_PROGBITS;
338 
339   // Solaris requires different flags for .eh_frame to seemingly every other
340   // platform.
341   unsigned EHSectionFlags = ELF::SHF_ALLOC;
342   if (T.isOSSolaris() && T.getArch() != Triple::x86_64)
343     EHSectionFlags |= ELF::SHF_WRITE;
344 
345   // ELF
346   BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
347                                   ELF::SHF_WRITE | ELF::SHF_ALLOC);
348 
349   TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
350                                    ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
351 
352   DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
353                                    ELF::SHF_WRITE | ELF::SHF_ALLOC);
354 
355   ReadOnlySection =
356       Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
357 
358   TLSDataSection =
359       Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
360                          ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
361 
362   TLSBSSSection = Ctx->getELFSection(
363       ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
364 
365   DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
366                                         ELF::SHF_ALLOC | ELF::SHF_WRITE);
367 
368   MergeableConst4Section =
369       Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
370                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 4, "");
371 
372   MergeableConst8Section =
373       Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
374                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 8, "");
375 
376   MergeableConst16Section =
377       Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
378                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 16, "");
379 
380   MergeableConst32Section =
381       Ctx->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS,
382                          ELF::SHF_ALLOC | ELF::SHF_MERGE, 32, "");
383 
384   // Exception Handling Sections.
385 
386   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
387   // it contains relocatable pointers.  In PIC mode, this is probably a big
388   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
389   // adjusted or this should be a data section.
390   LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
391                                    ELF::SHF_ALLOC);
392 
393   COFFDebugSymbolsSection = nullptr;
394   COFFDebugTypesSection = nullptr;
395 
396   unsigned DebugSecType = ELF::SHT_PROGBITS;
397 
398   // MIPS .debug_* sections should have SHT_MIPS_DWARF section type
399   // to distinguish among sections contain DWARF and ECOFF debug formats.
400   // Sections with ECOFF debug format are obsoleted and marked by SHT_PROGBITS.
401   if (T.isMIPS())
402     DebugSecType = ELF::SHT_MIPS_DWARF;
403 
404   // Debug Info Sections.
405   DwarfAbbrevSection =
406       Ctx->getELFSection(".debug_abbrev", DebugSecType, 0);
407   DwarfInfoSection = Ctx->getELFSection(".debug_info", DebugSecType, 0);
408   DwarfLineSection = Ctx->getELFSection(".debug_line", DebugSecType, 0);
409   DwarfLineStrSection =
410       Ctx->getELFSection(".debug_line_str", DebugSecType,
411                          ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
412   DwarfFrameSection = Ctx->getELFSection(".debug_frame", DebugSecType, 0);
413   DwarfPubNamesSection =
414       Ctx->getELFSection(".debug_pubnames", DebugSecType, 0);
415   DwarfPubTypesSection =
416       Ctx->getELFSection(".debug_pubtypes", DebugSecType, 0);
417   DwarfGnuPubNamesSection =
418       Ctx->getELFSection(".debug_gnu_pubnames", DebugSecType, 0);
419   DwarfGnuPubTypesSection =
420       Ctx->getELFSection(".debug_gnu_pubtypes", DebugSecType, 0);
421   DwarfStrSection =
422       Ctx->getELFSection(".debug_str", DebugSecType,
423                          ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
424   DwarfLocSection = Ctx->getELFSection(".debug_loc", DebugSecType, 0);
425   DwarfARangesSection =
426       Ctx->getELFSection(".debug_aranges", DebugSecType, 0);
427   DwarfRangesSection =
428       Ctx->getELFSection(".debug_ranges", DebugSecType, 0);
429   DwarfMacinfoSection =
430       Ctx->getELFSection(".debug_macinfo", DebugSecType, 0);
431 
432   // DWARF5 Experimental Debug Info
433 
434   // Accelerator Tables
435   DwarfDebugNamesSection =
436       Ctx->getELFSection(".debug_names", ELF::SHT_PROGBITS, 0);
437   DwarfAccelNamesSection =
438       Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0);
439   DwarfAccelObjCSection =
440       Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0);
441   DwarfAccelNamespaceSection =
442       Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0);
443   DwarfAccelTypesSection =
444       Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0);
445 
446   // String Offset and Address Sections
447   DwarfStrOffSection =
448       Ctx->getELFSection(".debug_str_offsets", DebugSecType, 0);
449   DwarfAddrSection = Ctx->getELFSection(".debug_addr", DebugSecType, 0);
450   DwarfRnglistsSection = Ctx->getELFSection(".debug_rnglists", DebugSecType, 0);
451   DwarfLoclistsSection = Ctx->getELFSection(".debug_loclists", DebugSecType, 0);
452 
453   // Fission Sections
454   DwarfInfoDWOSection =
455       Ctx->getELFSection(".debug_info.dwo", DebugSecType, ELF::SHF_EXCLUDE);
456   DwarfTypesDWOSection =
457       Ctx->getELFSection(".debug_types.dwo", DebugSecType, ELF::SHF_EXCLUDE);
458   DwarfAbbrevDWOSection =
459       Ctx->getELFSection(".debug_abbrev.dwo", DebugSecType, ELF::SHF_EXCLUDE);
460   DwarfStrDWOSection = Ctx->getELFSection(
461       ".debug_str.dwo", DebugSecType,
462       ELF::SHF_MERGE | ELF::SHF_STRINGS | ELF::SHF_EXCLUDE, 1, "");
463   DwarfLineDWOSection =
464       Ctx->getELFSection(".debug_line.dwo", DebugSecType, ELF::SHF_EXCLUDE);
465   DwarfLocDWOSection =
466       Ctx->getELFSection(".debug_loc.dwo", DebugSecType, ELF::SHF_EXCLUDE);
467   DwarfStrOffDWOSection = Ctx->getELFSection(".debug_str_offsets.dwo",
468                                              DebugSecType, ELF::SHF_EXCLUDE);
469   DwarfRnglistsDWOSection =
470       Ctx->getELFSection(".debug_rnglists.dwo", DebugSecType, ELF::SHF_EXCLUDE);
471 
472   // DWP Sections
473   DwarfCUIndexSection =
474       Ctx->getELFSection(".debug_cu_index", DebugSecType, 0);
475   DwarfTUIndexSection =
476       Ctx->getELFSection(".debug_tu_index", DebugSecType, 0);
477 
478   StackMapSection =
479       Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
480 
481   FaultMapSection =
482       Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
483 
484   EHFrameSection =
485       Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);
486 
487   StackSizesSection = Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, 0);
488 
489   RemarksSection =
490       Ctx->getELFSection(".remarks", ELF::SHT_PROGBITS, ELF::SHF_EXCLUDE);
491 }
492 
493 void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
494   EHFrameSection =
495       Ctx->getCOFFSection(".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
496                                            COFF::IMAGE_SCN_MEM_READ,
497                           SectionKind::getData());
498 
499   // Set the `IMAGE_SCN_MEM_16BIT` flag when compiling for thumb mode.  This is
500   // used to indicate to the linker that the text segment contains thumb instructions
501   // and to set the ISA selection bit for calls accordingly.
502   const bool IsThumb = T.getArch() == Triple::thumb;
503 
504   CommDirectiveSupportsAlignment = true;
505 
506   // COFF
507   BSSSection = Ctx->getCOFFSection(
508       ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
509                   COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
510       SectionKind::getBSS());
511   TextSection = Ctx->getCOFFSection(
512       ".text",
513       (IsThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) |
514           COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
515           COFF::IMAGE_SCN_MEM_READ,
516       SectionKind::getText());
517   DataSection = Ctx->getCOFFSection(
518       ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
519                    COFF::IMAGE_SCN_MEM_WRITE,
520       SectionKind::getData());
521   ReadOnlySection = Ctx->getCOFFSection(
522       ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
523       SectionKind::getReadOnly());
524 
525   if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::aarch64) {
526     // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
527     LSDASection = nullptr;
528   } else {
529     LSDASection = Ctx->getCOFFSection(".gcc_except_table",
530                                       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
531                                           COFF::IMAGE_SCN_MEM_READ,
532                                       SectionKind::getReadOnly());
533   }
534 
535   // Debug info.
536   COFFDebugSymbolsSection =
537       Ctx->getCOFFSection(".debug$S", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
538                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
539                                        COFF::IMAGE_SCN_MEM_READ),
540                           SectionKind::getMetadata());
541   COFFDebugTypesSection =
542       Ctx->getCOFFSection(".debug$T", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
543                                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
544                                        COFF::IMAGE_SCN_MEM_READ),
545                           SectionKind::getMetadata());
546   COFFGlobalTypeHashesSection = Ctx->getCOFFSection(
547       ".debug$H",
548       (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
549        COFF::IMAGE_SCN_MEM_READ),
550       SectionKind::getMetadata());
551 
552   DwarfAbbrevSection = Ctx->getCOFFSection(
553       ".debug_abbrev",
554       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
555           COFF::IMAGE_SCN_MEM_READ,
556       SectionKind::getMetadata(), "section_abbrev");
557   DwarfInfoSection = Ctx->getCOFFSection(
558       ".debug_info",
559       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
560           COFF::IMAGE_SCN_MEM_READ,
561       SectionKind::getMetadata(), "section_info");
562   DwarfLineSection = Ctx->getCOFFSection(
563       ".debug_line",
564       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
565           COFF::IMAGE_SCN_MEM_READ,
566       SectionKind::getMetadata(), "section_line");
567   DwarfLineStrSection = Ctx->getCOFFSection(
568       ".debug_line_str",
569       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
570           COFF::IMAGE_SCN_MEM_READ,
571       SectionKind::getMetadata(), "section_line_str");
572   DwarfFrameSection = Ctx->getCOFFSection(
573       ".debug_frame",
574       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
575           COFF::IMAGE_SCN_MEM_READ,
576       SectionKind::getMetadata());
577   DwarfPubNamesSection = Ctx->getCOFFSection(
578       ".debug_pubnames",
579       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
580           COFF::IMAGE_SCN_MEM_READ,
581       SectionKind::getMetadata());
582   DwarfPubTypesSection = Ctx->getCOFFSection(
583       ".debug_pubtypes",
584       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
585           COFF::IMAGE_SCN_MEM_READ,
586       SectionKind::getMetadata());
587   DwarfGnuPubNamesSection = Ctx->getCOFFSection(
588       ".debug_gnu_pubnames",
589       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
590           COFF::IMAGE_SCN_MEM_READ,
591       SectionKind::getMetadata());
592   DwarfGnuPubTypesSection = Ctx->getCOFFSection(
593       ".debug_gnu_pubtypes",
594       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
595           COFF::IMAGE_SCN_MEM_READ,
596       SectionKind::getMetadata());
597   DwarfStrSection = Ctx->getCOFFSection(
598       ".debug_str",
599       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
600           COFF::IMAGE_SCN_MEM_READ,
601       SectionKind::getMetadata(), "info_string");
602   DwarfStrOffSection = Ctx->getCOFFSection(
603       ".debug_str_offsets",
604       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
605           COFF::IMAGE_SCN_MEM_READ,
606       SectionKind::getMetadata(), "section_str_off");
607   DwarfLocSection = Ctx->getCOFFSection(
608       ".debug_loc",
609       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
610           COFF::IMAGE_SCN_MEM_READ,
611       SectionKind::getMetadata(), "section_debug_loc");
612   DwarfARangesSection = Ctx->getCOFFSection(
613       ".debug_aranges",
614       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
615           COFF::IMAGE_SCN_MEM_READ,
616       SectionKind::getMetadata());
617   DwarfRangesSection = Ctx->getCOFFSection(
618       ".debug_ranges",
619       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
620           COFF::IMAGE_SCN_MEM_READ,
621       SectionKind::getMetadata(), "debug_range");
622   DwarfMacinfoSection = Ctx->getCOFFSection(
623       ".debug_macinfo",
624       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
625           COFF::IMAGE_SCN_MEM_READ,
626       SectionKind::getMetadata(), "debug_macinfo");
627   DwarfInfoDWOSection = Ctx->getCOFFSection(
628       ".debug_info.dwo",
629       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
630           COFF::IMAGE_SCN_MEM_READ,
631       SectionKind::getMetadata(), "section_info_dwo");
632   DwarfTypesDWOSection = Ctx->getCOFFSection(
633       ".debug_types.dwo",
634       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
635           COFF::IMAGE_SCN_MEM_READ,
636       SectionKind::getMetadata(), "section_types_dwo");
637   DwarfAbbrevDWOSection = Ctx->getCOFFSection(
638       ".debug_abbrev.dwo",
639       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
640           COFF::IMAGE_SCN_MEM_READ,
641       SectionKind::getMetadata(), "section_abbrev_dwo");
642   DwarfStrDWOSection = Ctx->getCOFFSection(
643       ".debug_str.dwo",
644       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
645           COFF::IMAGE_SCN_MEM_READ,
646       SectionKind::getMetadata(), "skel_string");
647   DwarfLineDWOSection = Ctx->getCOFFSection(
648       ".debug_line.dwo",
649       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
650           COFF::IMAGE_SCN_MEM_READ,
651       SectionKind::getMetadata());
652   DwarfLocDWOSection = Ctx->getCOFFSection(
653       ".debug_loc.dwo",
654       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
655           COFF::IMAGE_SCN_MEM_READ,
656       SectionKind::getMetadata(), "skel_loc");
657   DwarfStrOffDWOSection = Ctx->getCOFFSection(
658       ".debug_str_offsets.dwo",
659       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
660           COFF::IMAGE_SCN_MEM_READ,
661       SectionKind::getMetadata(), "section_str_off_dwo");
662   DwarfAddrSection = Ctx->getCOFFSection(
663       ".debug_addr",
664       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
665           COFF::IMAGE_SCN_MEM_READ,
666       SectionKind::getMetadata(), "addr_sec");
667   DwarfCUIndexSection = Ctx->getCOFFSection(
668       ".debug_cu_index",
669       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
670           COFF::IMAGE_SCN_MEM_READ,
671       SectionKind::getMetadata());
672   DwarfTUIndexSection = Ctx->getCOFFSection(
673       ".debug_tu_index",
674       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
675           COFF::IMAGE_SCN_MEM_READ,
676       SectionKind::getMetadata());
677   DwarfDebugNamesSection = Ctx->getCOFFSection(
678       ".debug_names",
679       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
680           COFF::IMAGE_SCN_MEM_READ,
681       SectionKind::getMetadata(), "debug_names_begin");
682   DwarfAccelNamesSection = Ctx->getCOFFSection(
683       ".apple_names",
684       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
685           COFF::IMAGE_SCN_MEM_READ,
686       SectionKind::getMetadata(), "names_begin");
687   DwarfAccelNamespaceSection = Ctx->getCOFFSection(
688       ".apple_namespaces",
689       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
690           COFF::IMAGE_SCN_MEM_READ,
691       SectionKind::getMetadata(), "namespac_begin");
692   DwarfAccelTypesSection = Ctx->getCOFFSection(
693       ".apple_types",
694       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
695           COFF::IMAGE_SCN_MEM_READ,
696       SectionKind::getMetadata(), "types_begin");
697   DwarfAccelObjCSection = Ctx->getCOFFSection(
698       ".apple_objc",
699       COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
700           COFF::IMAGE_SCN_MEM_READ,
701       SectionKind::getMetadata(), "objc_begin");
702 
703   DrectveSection = Ctx->getCOFFSection(
704       ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE,
705       SectionKind::getMetadata());
706 
707   PDataSection = Ctx->getCOFFSection(
708       ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
709       SectionKind::getData());
710 
711   XDataSection = Ctx->getCOFFSection(
712       ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
713       SectionKind::getData());
714 
715   SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO,
716                                       SectionKind::getMetadata());
717 
718   GFIDsSection = Ctx->getCOFFSection(".gfids$y",
719                                      COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
720                                          COFF::IMAGE_SCN_MEM_READ,
721                                      SectionKind::getMetadata());
722 
723   TLSDataSection = Ctx->getCOFFSection(
724       ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
725                    COFF::IMAGE_SCN_MEM_WRITE,
726       SectionKind::getData());
727 
728   StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps",
729                                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
730                                             COFF::IMAGE_SCN_MEM_READ,
731                                         SectionKind::getReadOnly());
732 }
733 
734 void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
735   TextSection = Ctx->getWasmSection(".text", SectionKind::getText());
736   DataSection = Ctx->getWasmSection(".data", SectionKind::getData());
737 
738   DwarfLineSection =
739       Ctx->getWasmSection(".debug_line", SectionKind::getMetadata());
740   DwarfLineStrSection =
741       Ctx->getWasmSection(".debug_line_str", SectionKind::getMetadata());
742   DwarfStrSection =
743       Ctx->getWasmSection(".debug_str", SectionKind::getMetadata());
744   DwarfLocSection =
745       Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata());
746   DwarfAbbrevSection =
747       Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata());
748   DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata());
749   DwarfRangesSection =
750       Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata());
751   DwarfMacinfoSection =
752       Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata());
753   DwarfAddrSection = Ctx->getWasmSection(".debug_addr", SectionKind::getMetadata());
754   DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
755   DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
756   DwarfInfoSection =
757       Ctx->getWasmSection(".debug_info", SectionKind::getMetadata());
758   DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata());
759   DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata());
760   DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata());
761 
762   // Wasm use data section for LSDA.
763   // TODO Consider putting each function's exception table in a separate
764   // section, as in -function-sections, to facilitate lld's --gc-section.
765   LSDASection = Ctx->getWasmSection(".rodata.gcc_except_table",
766                                     SectionKind::getReadOnlyWithRel());
767 
768   // TODO: Define more sections.
769 }
770 
771 void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) {
772   // The default csect for program code. Functions without a specified section
773   // get placed into this csect. The choice of csect name is not a property of
774   // the ABI or object file format. For example, the XL compiler uses an unnamed
775   // csect for program code.
776   TextSection = Ctx->getXCOFFSection(
777       ".text", XCOFF::StorageMappingClass::XMC_PR, XCOFF::XTY_SD,
778       XCOFF::C_HIDEXT, SectionKind::getText());
779 
780   DataSection = Ctx->getXCOFFSection(
781       ".data", XCOFF::StorageMappingClass::XMC_RW, XCOFF::XTY_SD,
782       XCOFF::C_HIDEXT, SectionKind::getData());
783 }
784 
785 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, bool PIC,
786                                             MCContext &ctx,
787                                             bool LargeCodeModel) {
788   PositionIndependent = PIC;
789   Ctx = &ctx;
790 
791   // Common.
792   CommDirectiveSupportsAlignment = true;
793   SupportsWeakOmittedEHFrame = true;
794   SupportsCompactUnwindWithoutEHFrame = false;
795   OmitDwarfIfHaveCompactUnwind = false;
796 
797   FDECFIEncoding = dwarf::DW_EH_PE_absptr;
798 
799   CompactUnwindDwarfEHFrameOnly = 0;
800 
801   EHFrameSection = nullptr;             // Created on demand.
802   CompactUnwindSection = nullptr;       // Used only by selected targets.
803   DwarfAccelNamesSection = nullptr;     // Used only by selected targets.
804   DwarfAccelObjCSection = nullptr;      // Used only by selected targets.
805   DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
806   DwarfAccelTypesSection = nullptr;     // Used only by selected targets.
807 
808   TT = TheTriple;
809 
810   switch (TT.getObjectFormat()) {
811   case Triple::MachO:
812     Env = IsMachO;
813     initMachOMCObjectFileInfo(TT);
814     break;
815   case Triple::COFF:
816     if (!TT.isOSWindows())
817       report_fatal_error(
818           "Cannot initialize MC for non-Windows COFF object files.");
819 
820     Env = IsCOFF;
821     initCOFFMCObjectFileInfo(TT);
822     break;
823   case Triple::ELF:
824     Env = IsELF;
825     initELFMCObjectFileInfo(TT, LargeCodeModel);
826     break;
827   case Triple::Wasm:
828     Env = IsWasm;
829     initWasmMCObjectFileInfo(TT);
830     break;
831   case Triple::XCOFF:
832     Env = IsXCOFF;
833     initXCOFFMCObjectFileInfo(TT);
834     break;
835   case Triple::UnknownObjectFormat:
836     report_fatal_error("Cannot initialize MC for unknown object file format.");
837     break;
838   }
839 }
840 
841 MCSection *MCObjectFileInfo::getDwarfComdatSection(const char *Name,
842                                                    uint64_t Hash) const {
843   switch (TT.getObjectFormat()) {
844   case Triple::ELF:
845     return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, ELF::SHF_GROUP, 0,
846                               utostr(Hash));
847   case Triple::MachO:
848   case Triple::COFF:
849   case Triple::Wasm:
850   case Triple::XCOFF:
851   case Triple::UnknownObjectFormat:
852     report_fatal_error("Cannot get DWARF comdat section for this object file "
853                        "format: not implemented.");
854     break;
855   }
856   llvm_unreachable("Unknown ObjectFormatType");
857 }
858 
859 MCSection *
860 MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
861   if (Env != IsELF)
862     return StackSizesSection;
863 
864   const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
865   unsigned Flags = ELF::SHF_LINK_ORDER;
866   StringRef GroupName;
867   if (const MCSymbol *Group = ElfSec.getGroup()) {
868     GroupName = Group->getName();
869     Flags |= ELF::SHF_GROUP;
870   }
871 
872   const MCSymbol *Link = TextSec.getBeginSymbol();
873   auto It = StackSizesUniquing.insert({Link, StackSizesUniquing.size()});
874   unsigned UniqueID = It.first->second;
875 
876   return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0,
877                             GroupName, UniqueID, cast<MCSymbolELF>(Link));
878 }
879