1 //===-- MCObjectFileInfo.cpp - Object File Information --------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
24 static bool useCompactUnwind(const Triple
&T
) {
29 // aarch64 always has it.
30 if (T
.getArch() == Triple::aarch64
)
33 // armv7k always has it.
37 // Use it on newer version of OS X.
38 if (T
.isMacOSX() && !T
.isMacOSXVersionLT(10, 6))
41 // And the iOS simulator.
43 (T
.getArch() == Triple::x86_64
|| T
.getArch() == Triple::x86
))
49 void MCObjectFileInfo::initMachOMCObjectFileInfo(const Triple
&T
) {
51 SupportsWeakOmittedEHFrame
= false;
53 EHFrameSection
= Ctx
->getMachOSection(
54 "__TEXT", "__eh_frame",
55 MachO::S_COALESCED
| MachO::S_ATTR_NO_TOC
|
56 MachO::S_ATTR_STRIP_STATIC_SYMS
| MachO::S_ATTR_LIVE_SUPPORT
,
57 SectionKind::getReadOnly());
59 if (T
.isOSDarwin() && T
.getArch() == Triple::aarch64
)
60 SupportsCompactUnwindWithoutEHFrame
= true;
63 OmitDwarfIfHaveCompactUnwind
= true;
65 FDECFIEncoding
= dwarf::DW_EH_PE_pcrel
;
67 // .comm doesn't support alignment before Leopard.
68 if (T
.isMacOSX() && T
.isMacOSXVersionLT(10, 5))
69 CommDirectiveSupportsAlignment
= false;
72 = Ctx
->getMachOSection("__TEXT", "__text",
73 MachO::S_ATTR_PURE_INSTRUCTIONS
,
74 SectionKind::getText());
76 = Ctx
->getMachOSection("__DATA", "__data", 0, SectionKind::getData());
78 // BSSSection might not be expected initialized on msvc.
81 TLSDataSection
// .tdata
82 = Ctx
->getMachOSection("__DATA", "__thread_data",
83 MachO::S_THREAD_LOCAL_REGULAR
,
84 SectionKind::getData());
85 TLSBSSSection
// .tbss
86 = Ctx
->getMachOSection("__DATA", "__thread_bss",
87 MachO::S_THREAD_LOCAL_ZEROFILL
,
88 SectionKind::getThreadBSS());
90 // TODO: Verify datarel below.
92 = Ctx
->getMachOSection("__DATA", "__thread_vars",
93 MachO::S_THREAD_LOCAL_VARIABLES
,
94 SectionKind::getData());
96 TLSThreadInitSection
= Ctx
->getMachOSection(
97 "__DATA", "__thread_init", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
,
98 SectionKind::getData());
100 CStringSection
// .cstring
101 = Ctx
->getMachOSection("__TEXT", "__cstring",
102 MachO::S_CSTRING_LITERALS
,
103 SectionKind::getMergeable1ByteCString());
105 = Ctx
->getMachOSection("__TEXT","__ustring", 0,
106 SectionKind::getMergeable2ByteCString());
107 FourByteConstantSection
// .literal4
108 = Ctx
->getMachOSection("__TEXT", "__literal4",
109 MachO::S_4BYTE_LITERALS
,
110 SectionKind::getMergeableConst4());
111 EightByteConstantSection
// .literal8
112 = Ctx
->getMachOSection("__TEXT", "__literal8",
113 MachO::S_8BYTE_LITERALS
,
114 SectionKind::getMergeableConst8());
116 SixteenByteConstantSection
// .literal16
117 = Ctx
->getMachOSection("__TEXT", "__literal16",
118 MachO::S_16BYTE_LITERALS
,
119 SectionKind::getMergeableConst16());
121 ReadOnlySection
// .const
122 = Ctx
->getMachOSection("__TEXT", "__const", 0,
123 SectionKind::getReadOnly());
125 // If the target is not powerpc, map the coal sections to the non-coal
128 // "__TEXT/__textcoal_nt" => section "__TEXT/__text"
129 // "__TEXT/__const_coal" => section "__TEXT/__const"
130 // "__DATA/__datacoal_nt" => section "__DATA/__data"
131 Triple::ArchType ArchTy
= T
.getArch();
133 ConstDataSection
// .const_data
134 = Ctx
->getMachOSection("__DATA", "__const", 0,
135 SectionKind::getReadOnlyWithRel());
137 if (ArchTy
== Triple::ppc
|| ArchTy
== Triple::ppc64
) {
139 = Ctx
->getMachOSection("__TEXT", "__textcoal_nt",
141 MachO::S_ATTR_PURE_INSTRUCTIONS
,
142 SectionKind::getText());
144 = Ctx
->getMachOSection("__TEXT", "__const_coal",
146 SectionKind::getReadOnly());
147 DataCoalSection
= Ctx
->getMachOSection(
148 "__DATA", "__datacoal_nt", MachO::S_COALESCED
, SectionKind::getData());
149 ConstDataCoalSection
= DataCoalSection
;
151 TextCoalSection
= TextSection
;
152 ConstTextCoalSection
= ReadOnlySection
;
153 DataCoalSection
= DataSection
;
154 ConstDataCoalSection
= ConstDataSection
;
158 = Ctx
->getMachOSection("__DATA","__common",
160 SectionKind::getBSS());
162 = Ctx
->getMachOSection("__DATA","__bss", MachO::S_ZEROFILL
,
163 SectionKind::getBSS());
166 LazySymbolPointerSection
167 = Ctx
->getMachOSection("__DATA", "__la_symbol_ptr",
168 MachO::S_LAZY_SYMBOL_POINTERS
,
169 SectionKind::getMetadata());
170 NonLazySymbolPointerSection
171 = Ctx
->getMachOSection("__DATA", "__nl_symbol_ptr",
172 MachO::S_NON_LAZY_SYMBOL_POINTERS
,
173 SectionKind::getMetadata());
175 ThreadLocalPointerSection
176 = Ctx
->getMachOSection("__DATA", "__thread_ptr",
177 MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
,
178 SectionKind::getMetadata());
180 // Exception Handling.
181 LSDASection
= Ctx
->getMachOSection("__TEXT", "__gcc_except_tab", 0,
182 SectionKind::getReadOnlyWithRel());
184 COFFDebugSymbolsSection
= nullptr;
185 COFFDebugTypesSection
= nullptr;
186 COFFGlobalTypeHashesSection
= nullptr;
188 if (useCompactUnwind(T
)) {
189 CompactUnwindSection
=
190 Ctx
->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG
,
191 SectionKind::getReadOnly());
193 if (T
.getArch() == Triple::x86_64
|| T
.getArch() == Triple::x86
)
194 CompactUnwindDwarfEHFrameOnly
= 0x04000000; // UNWIND_X86_64_MODE_DWARF
195 else if (T
.getArch() == Triple::aarch64
)
196 CompactUnwindDwarfEHFrameOnly
= 0x03000000; // UNWIND_ARM64_MODE_DWARF
197 else if (T
.getArch() == Triple::arm
|| T
.getArch() == Triple::thumb
)
198 CompactUnwindDwarfEHFrameOnly
= 0x04000000; // UNWIND_ARM_MODE_DWARF
201 // Debug Information.
202 DwarfDebugNamesSection
=
203 Ctx
->getMachOSection("__DWARF", "__debug_names", MachO::S_ATTR_DEBUG
,
204 SectionKind::getMetadata(), "debug_names_begin");
205 DwarfAccelNamesSection
=
206 Ctx
->getMachOSection("__DWARF", "__apple_names", MachO::S_ATTR_DEBUG
,
207 SectionKind::getMetadata(), "names_begin");
208 DwarfAccelObjCSection
=
209 Ctx
->getMachOSection("__DWARF", "__apple_objc", MachO::S_ATTR_DEBUG
,
210 SectionKind::getMetadata(), "objc_begin");
211 // 16 character section limit...
212 DwarfAccelNamespaceSection
=
213 Ctx
->getMachOSection("__DWARF", "__apple_namespac", MachO::S_ATTR_DEBUG
,
214 SectionKind::getMetadata(), "namespac_begin");
215 DwarfAccelTypesSection
=
216 Ctx
->getMachOSection("__DWARF", "__apple_types", MachO::S_ATTR_DEBUG
,
217 SectionKind::getMetadata(), "types_begin");
219 DwarfSwiftASTSection
=
220 Ctx
->getMachOSection("__DWARF", "__swift_ast", MachO::S_ATTR_DEBUG
,
221 SectionKind::getMetadata());
224 Ctx
->getMachOSection("__DWARF", "__debug_abbrev", MachO::S_ATTR_DEBUG
,
225 SectionKind::getMetadata(), "section_abbrev");
227 Ctx
->getMachOSection("__DWARF", "__debug_info", MachO::S_ATTR_DEBUG
,
228 SectionKind::getMetadata(), "section_info");
230 Ctx
->getMachOSection("__DWARF", "__debug_line", MachO::S_ATTR_DEBUG
,
231 SectionKind::getMetadata(), "section_line");
232 DwarfLineStrSection
=
233 Ctx
->getMachOSection("__DWARF", "__debug_line_str", MachO::S_ATTR_DEBUG
,
234 SectionKind::getMetadata(), "section_line_str");
236 Ctx
->getMachOSection("__DWARF", "__debug_frame", MachO::S_ATTR_DEBUG
,
237 SectionKind::getMetadata());
238 DwarfPubNamesSection
=
239 Ctx
->getMachOSection("__DWARF", "__debug_pubnames", MachO::S_ATTR_DEBUG
,
240 SectionKind::getMetadata());
241 DwarfPubTypesSection
=
242 Ctx
->getMachOSection("__DWARF", "__debug_pubtypes", MachO::S_ATTR_DEBUG
,
243 SectionKind::getMetadata());
244 DwarfGnuPubNamesSection
=
245 Ctx
->getMachOSection("__DWARF", "__debug_gnu_pubn", MachO::S_ATTR_DEBUG
,
246 SectionKind::getMetadata());
247 DwarfGnuPubTypesSection
=
248 Ctx
->getMachOSection("__DWARF", "__debug_gnu_pubt", MachO::S_ATTR_DEBUG
,
249 SectionKind::getMetadata());
251 Ctx
->getMachOSection("__DWARF", "__debug_str", MachO::S_ATTR_DEBUG
,
252 SectionKind::getMetadata(), "info_string");
254 Ctx
->getMachOSection("__DWARF", "__debug_str_offs", MachO::S_ATTR_DEBUG
,
255 SectionKind::getMetadata(), "section_str_off");
257 Ctx
->getMachOSection("__DWARF", "__debug_addr", MachO::S_ATTR_DEBUG
,
258 SectionKind::getMetadata(), "section_info");
260 Ctx
->getMachOSection("__DWARF", "__debug_loc", MachO::S_ATTR_DEBUG
,
261 SectionKind::getMetadata(), "section_debug_loc");
262 DwarfLoclistsSection
=
263 Ctx
->getMachOSection("__DWARF", "__debug_loclists", MachO::S_ATTR_DEBUG
,
264 SectionKind::getMetadata(), "section_debug_loc");
266 DwarfARangesSection
=
267 Ctx
->getMachOSection("__DWARF", "__debug_aranges", MachO::S_ATTR_DEBUG
,
268 SectionKind::getMetadata());
270 Ctx
->getMachOSection("__DWARF", "__debug_ranges", MachO::S_ATTR_DEBUG
,
271 SectionKind::getMetadata(), "debug_range");
272 DwarfRnglistsSection
=
273 Ctx
->getMachOSection("__DWARF", "__debug_rnglists", MachO::S_ATTR_DEBUG
,
274 SectionKind::getMetadata(), "debug_range");
275 DwarfMacinfoSection
=
276 Ctx
->getMachOSection("__DWARF", "__debug_macinfo", MachO::S_ATTR_DEBUG
,
277 SectionKind::getMetadata(), "debug_macinfo");
278 DwarfDebugInlineSection
=
279 Ctx
->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG
,
280 SectionKind::getMetadata());
281 DwarfCUIndexSection
=
282 Ctx
->getMachOSection("__DWARF", "__debug_cu_index", MachO::S_ATTR_DEBUG
,
283 SectionKind::getMetadata());
284 DwarfTUIndexSection
=
285 Ctx
->getMachOSection("__DWARF", "__debug_tu_index", MachO::S_ATTR_DEBUG
,
286 SectionKind::getMetadata());
287 StackMapSection
= Ctx
->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps",
288 0, SectionKind::getMetadata());
290 FaultMapSection
= Ctx
->getMachOSection("__LLVM_FAULTMAPS", "__llvm_faultmaps",
291 0, SectionKind::getMetadata());
293 TLSExtraDataSection
= TLSTLVSection
;
296 void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple
&T
, bool Large
) {
297 switch (T
.getArch()) {
301 case Triple::mips64el
:
302 FDECFIEncoding
= Ctx
->getAsmInfo()->getCodePointerSize() == 4
303 ? dwarf::DW_EH_PE_sdata4
304 : dwarf::DW_EH_PE_sdata8
;
307 case Triple::ppc64le
:
309 FDECFIEncoding
= dwarf::DW_EH_PE_pcrel
|
310 (Large
? dwarf::DW_EH_PE_sdata8
: dwarf::DW_EH_PE_sdata4
);
314 FDECFIEncoding
= dwarf::DW_EH_PE_sdata8
;
316 case Triple::hexagon
:
318 PositionIndependent
? dwarf::DW_EH_PE_pcrel
: dwarf::DW_EH_PE_absptr
;
321 FDECFIEncoding
= dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
;
325 unsigned EHSectionType
= T
.getArch() == Triple::x86_64
326 ? ELF::SHT_X86_64_UNWIND
329 // Solaris requires different flags for .eh_frame to seemingly every other
331 unsigned EHSectionFlags
= ELF::SHF_ALLOC
;
332 if (T
.isOSSolaris() && T
.getArch() != Triple::x86_64
)
333 EHSectionFlags
|= ELF::SHF_WRITE
;
336 BSSSection
= Ctx
->getELFSection(".bss", ELF::SHT_NOBITS
,
337 ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
339 TextSection
= Ctx
->getELFSection(".text", ELF::SHT_PROGBITS
,
340 ELF::SHF_EXECINSTR
| ELF::SHF_ALLOC
);
342 DataSection
= Ctx
->getELFSection(".data", ELF::SHT_PROGBITS
,
343 ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
346 Ctx
->getELFSection(".rodata", ELF::SHT_PROGBITS
, ELF::SHF_ALLOC
);
349 Ctx
->getELFSection(".tdata", ELF::SHT_PROGBITS
,
350 ELF::SHF_ALLOC
| ELF::SHF_TLS
| ELF::SHF_WRITE
);
352 TLSBSSSection
= Ctx
->getELFSection(
353 ".tbss", ELF::SHT_NOBITS
, ELF::SHF_ALLOC
| ELF::SHF_TLS
| ELF::SHF_WRITE
);
355 DataRelROSection
= Ctx
->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS
,
356 ELF::SHF_ALLOC
| ELF::SHF_WRITE
);
358 MergeableConst4Section
=
359 Ctx
->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS
,
360 ELF::SHF_ALLOC
| ELF::SHF_MERGE
, 4, "");
362 MergeableConst8Section
=
363 Ctx
->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS
,
364 ELF::SHF_ALLOC
| ELF::SHF_MERGE
, 8, "");
366 MergeableConst16Section
=
367 Ctx
->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS
,
368 ELF::SHF_ALLOC
| ELF::SHF_MERGE
, 16, "");
370 MergeableConst32Section
=
371 Ctx
->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS
,
372 ELF::SHF_ALLOC
| ELF::SHF_MERGE
, 32, "");
374 // Exception Handling Sections.
376 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
377 // it contains relocatable pointers. In PIC mode, this is probably a big
378 // runtime hit for C++ apps. Either the contents of the LSDA need to be
379 // adjusted or this should be a data section.
380 LSDASection
= Ctx
->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS
,
383 COFFDebugSymbolsSection
= nullptr;
384 COFFDebugTypesSection
= nullptr;
386 unsigned DebugSecType
= ELF::SHT_PROGBITS
;
388 // MIPS .debug_* sections should have SHT_MIPS_DWARF section type
389 // to distinguish among sections contain DWARF and ECOFF debug formats.
390 // Sections with ECOFF debug format are obsoleted and marked by SHT_PROGBITS.
392 DebugSecType
= ELF::SHT_MIPS_DWARF
;
394 // Debug Info Sections.
396 Ctx
->getELFSection(".debug_abbrev", DebugSecType
, 0);
397 DwarfInfoSection
= Ctx
->getELFSection(".debug_info", DebugSecType
, 0);
398 DwarfLineSection
= Ctx
->getELFSection(".debug_line", DebugSecType
, 0);
399 DwarfLineStrSection
=
400 Ctx
->getELFSection(".debug_line_str", DebugSecType
,
401 ELF::SHF_MERGE
| ELF::SHF_STRINGS
, 1, "");
402 DwarfFrameSection
= Ctx
->getELFSection(".debug_frame", DebugSecType
, 0);
403 DwarfPubNamesSection
=
404 Ctx
->getELFSection(".debug_pubnames", DebugSecType
, 0);
405 DwarfPubTypesSection
=
406 Ctx
->getELFSection(".debug_pubtypes", DebugSecType
, 0);
407 DwarfGnuPubNamesSection
=
408 Ctx
->getELFSection(".debug_gnu_pubnames", DebugSecType
, 0);
409 DwarfGnuPubTypesSection
=
410 Ctx
->getELFSection(".debug_gnu_pubtypes", DebugSecType
, 0);
412 Ctx
->getELFSection(".debug_str", DebugSecType
,
413 ELF::SHF_MERGE
| ELF::SHF_STRINGS
, 1, "");
414 DwarfLocSection
= Ctx
->getELFSection(".debug_loc", DebugSecType
, 0);
415 DwarfARangesSection
=
416 Ctx
->getELFSection(".debug_aranges", DebugSecType
, 0);
418 Ctx
->getELFSection(".debug_ranges", DebugSecType
, 0);
419 DwarfMacinfoSection
=
420 Ctx
->getELFSection(".debug_macinfo", DebugSecType
, 0);
422 // DWARF5 Experimental Debug Info
424 // Accelerator Tables
425 DwarfDebugNamesSection
=
426 Ctx
->getELFSection(".debug_names", ELF::SHT_PROGBITS
, 0);
427 DwarfAccelNamesSection
=
428 Ctx
->getELFSection(".apple_names", ELF::SHT_PROGBITS
, 0);
429 DwarfAccelObjCSection
=
430 Ctx
->getELFSection(".apple_objc", ELF::SHT_PROGBITS
, 0);
431 DwarfAccelNamespaceSection
=
432 Ctx
->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS
, 0);
433 DwarfAccelTypesSection
=
434 Ctx
->getELFSection(".apple_types", ELF::SHT_PROGBITS
, 0);
436 // String Offset and Address Sections
438 Ctx
->getELFSection(".debug_str_offsets", DebugSecType
, 0);
439 DwarfAddrSection
= Ctx
->getELFSection(".debug_addr", DebugSecType
, 0);
440 DwarfRnglistsSection
= Ctx
->getELFSection(".debug_rnglists", DebugSecType
, 0);
441 DwarfLoclistsSection
= Ctx
->getELFSection(".debug_loclists", DebugSecType
, 0);
444 DwarfInfoDWOSection
=
445 Ctx
->getELFSection(".debug_info.dwo", DebugSecType
, ELF::SHF_EXCLUDE
);
446 DwarfTypesDWOSection
=
447 Ctx
->getELFSection(".debug_types.dwo", DebugSecType
, ELF::SHF_EXCLUDE
);
448 DwarfAbbrevDWOSection
=
449 Ctx
->getELFSection(".debug_abbrev.dwo", DebugSecType
, ELF::SHF_EXCLUDE
);
450 DwarfStrDWOSection
= Ctx
->getELFSection(
451 ".debug_str.dwo", DebugSecType
,
452 ELF::SHF_MERGE
| ELF::SHF_STRINGS
| ELF::SHF_EXCLUDE
, 1, "");
453 DwarfLineDWOSection
=
454 Ctx
->getELFSection(".debug_line.dwo", DebugSecType
, ELF::SHF_EXCLUDE
);
456 Ctx
->getELFSection(".debug_loc.dwo", DebugSecType
, ELF::SHF_EXCLUDE
);
457 DwarfStrOffDWOSection
= Ctx
->getELFSection(".debug_str_offsets.dwo",
458 DebugSecType
, ELF::SHF_EXCLUDE
);
459 DwarfRnglistsDWOSection
=
460 Ctx
->getELFSection(".debug_rnglists.dwo", DebugSecType
, ELF::SHF_EXCLUDE
);
463 DwarfCUIndexSection
=
464 Ctx
->getELFSection(".debug_cu_index", DebugSecType
, 0);
465 DwarfTUIndexSection
=
466 Ctx
->getELFSection(".debug_tu_index", DebugSecType
, 0);
469 Ctx
->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS
, ELF::SHF_ALLOC
);
472 Ctx
->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS
, ELF::SHF_ALLOC
);
475 Ctx
->getELFSection(".eh_frame", EHSectionType
, EHSectionFlags
);
477 StackSizesSection
= Ctx
->getELFSection(".stack_sizes", ELF::SHT_PROGBITS
, 0);
480 void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple
&T
) {
482 Ctx
->getCOFFSection(".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
483 COFF::IMAGE_SCN_MEM_READ
,
484 SectionKind::getData());
486 // Set the `IMAGE_SCN_MEM_16BIT` flag when compiling for thumb mode. This is
487 // used to indicate to the linker that the text segment contains thumb instructions
488 // and to set the ISA selection bit for calls accordingly.
489 const bool IsThumb
= T
.getArch() == Triple::thumb
;
491 CommDirectiveSupportsAlignment
= true;
494 BSSSection
= Ctx
->getCOFFSection(
495 ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
|
496 COFF::IMAGE_SCN_MEM_READ
| COFF::IMAGE_SCN_MEM_WRITE
,
497 SectionKind::getBSS());
498 TextSection
= Ctx
->getCOFFSection(
500 (IsThumb
? COFF::IMAGE_SCN_MEM_16BIT
: (COFF::SectionCharacteristics
)0) |
501 COFF::IMAGE_SCN_CNT_CODE
| COFF::IMAGE_SCN_MEM_EXECUTE
|
502 COFF::IMAGE_SCN_MEM_READ
,
503 SectionKind::getText());
504 DataSection
= Ctx
->getCOFFSection(
505 ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
| COFF::IMAGE_SCN_MEM_READ
|
506 COFF::IMAGE_SCN_MEM_WRITE
,
507 SectionKind::getData());
508 ReadOnlySection
= Ctx
->getCOFFSection(
509 ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
| COFF::IMAGE_SCN_MEM_READ
,
510 SectionKind::getReadOnly());
512 if (T
.getArch() == Triple::x86_64
|| T
.getArch() == Triple::aarch64
) {
513 // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
514 LSDASection
= nullptr;
516 LSDASection
= Ctx
->getCOFFSection(".gcc_except_table",
517 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
518 COFF::IMAGE_SCN_MEM_READ
,
519 SectionKind::getReadOnly());
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(
535 (COFF::IMAGE_SCN_MEM_DISCARDABLE
| COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
536 COFF::IMAGE_SCN_MEM_READ
),
537 SectionKind::getMetadata());
539 DwarfAbbrevSection
= Ctx
->getCOFFSection(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
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(
686 COFF::IMAGE_SCN_MEM_DISCARDABLE
| COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
687 COFF::IMAGE_SCN_MEM_READ
,
688 SectionKind::getMetadata(), "objc_begin");
690 DrectveSection
= Ctx
->getCOFFSection(
691 ".drectve", COFF::IMAGE_SCN_LNK_INFO
| COFF::IMAGE_SCN_LNK_REMOVE
,
692 SectionKind::getMetadata());
694 PDataSection
= Ctx
->getCOFFSection(
695 ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
| COFF::IMAGE_SCN_MEM_READ
,
696 SectionKind::getData());
698 XDataSection
= Ctx
->getCOFFSection(
699 ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
| COFF::IMAGE_SCN_MEM_READ
,
700 SectionKind::getData());
702 SXDataSection
= Ctx
->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO
,
703 SectionKind::getMetadata());
705 GFIDsSection
= Ctx
->getCOFFSection(".gfids$y",
706 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
707 COFF::IMAGE_SCN_MEM_READ
,
708 SectionKind::getMetadata());
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());
715 StackMapSection
= Ctx
->getCOFFSection(".llvm_stackmaps",
716 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
717 COFF::IMAGE_SCN_MEM_READ
,
718 SectionKind::getReadOnly());
721 void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple
&T
) {
722 TextSection
= Ctx
->getWasmSection(".text", SectionKind::getText());
723 DataSection
= Ctx
->getWasmSection(".data", SectionKind::getData());
726 Ctx
->getWasmSection(".debug_line", SectionKind::getMetadata());
727 DwarfLineStrSection
=
728 Ctx
->getWasmSection(".debug_line_str", SectionKind::getMetadata());
730 Ctx
->getWasmSection(".debug_str", SectionKind::getMetadata());
732 Ctx
->getWasmSection(".debug_loc", SectionKind::getMetadata());
734 Ctx
->getWasmSection(".debug_abbrev", SectionKind::getMetadata());
735 DwarfARangesSection
= Ctx
->getWasmSection(".debug_aranges", SectionKind::getMetadata());
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());
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());
749 // Wasm use data section for LSDA.
750 // TODO Consider putting each function's exception table in a separate
751 // section, as in -function-sections, to facilitate lld's --gc-section.
752 LSDASection
= Ctx
->getWasmSection(".rodata.gcc_except_table",
753 SectionKind::getReadOnlyWithRel());
755 // TODO: Define more sections.
758 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple
&TheTriple
, bool PIC
,
760 bool LargeCodeModel
) {
761 PositionIndependent
= PIC
;
765 CommDirectiveSupportsAlignment
= true;
766 SupportsWeakOmittedEHFrame
= true;
767 SupportsCompactUnwindWithoutEHFrame
= false;
768 OmitDwarfIfHaveCompactUnwind
= false;
770 FDECFIEncoding
= dwarf::DW_EH_PE_absptr
;
772 CompactUnwindDwarfEHFrameOnly
= 0;
774 EHFrameSection
= nullptr; // Created on demand.
775 CompactUnwindSection
= nullptr; // Used only by selected targets.
776 DwarfAccelNamesSection
= nullptr; // Used only by selected targets.
777 DwarfAccelObjCSection
= nullptr; // Used only by selected targets.
778 DwarfAccelNamespaceSection
= nullptr; // Used only by selected targets.
779 DwarfAccelTypesSection
= nullptr; // Used only by selected targets.
783 switch (TT
.getObjectFormat()) {
786 initMachOMCObjectFileInfo(TT
);
789 if (!TT
.isOSWindows())
791 "Cannot initialize MC for non-Windows COFF object files.");
794 initCOFFMCObjectFileInfo(TT
);
798 initELFMCObjectFileInfo(TT
, LargeCodeModel
);
802 initWasmMCObjectFileInfo(TT
);
804 case Triple::UnknownObjectFormat
:
805 report_fatal_error("Cannot initialize MC for unknown object file format.");
810 MCSection
*MCObjectFileInfo::getDwarfComdatSection(const char *Name
,
811 uint64_t Hash
) const {
812 switch (TT
.getObjectFormat()) {
814 return Ctx
->getELFSection(Name
, ELF::SHT_PROGBITS
, ELF::SHF_GROUP
, 0,
819 case Triple::UnknownObjectFormat
:
820 report_fatal_error("Cannot get DWARF comdat section for this object file "
821 "format: not implemented.");
824 llvm_unreachable("Unknown ObjectFormatType");
828 MCObjectFileInfo::getStackSizesSection(const MCSection
&TextSec
) const {
830 return StackSizesSection
;
832 const MCSectionELF
&ElfSec
= static_cast<const MCSectionELF
&>(TextSec
);
833 unsigned Flags
= ELF::SHF_LINK_ORDER
;
835 if (const MCSymbol
*Group
= ElfSec
.getGroup()) {
836 GroupName
= Group
->getName();
837 Flags
|= ELF::SHF_GROUP
;
840 const MCSymbol
*Link
= TextSec
.getBeginSymbol();
841 auto It
= StackSizesUniquing
.insert({Link
, StackSizesUniquing
.size()});
842 unsigned UniqueID
= It
.first
->second
;
844 return Ctx
->getELFSection(".stack_sizes", ELF::SHT_PROGBITS
, Flags
, 0,
845 GroupName
, UniqueID
, cast
<MCSymbolELF
>(Link
));