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