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