1 //===-- MCObjectFileInfo.cpp - Object File Information --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
25 static bool useCompactUnwind(const Triple &T) {
30 // aarch64 always has it.
31 if (T.getArch() == Triple::aarch64)
34 // armv7k always has it.
38 // Use it on newer version of OS X.
39 if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6))
42 // And the iOS simulator.
44 (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86))
50 void MCObjectFileInfo::initMachOMCObjectFileInfo(const Triple &T) {
52 SupportsWeakOmittedEHFrame = false;
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());
60 if (T.isOSDarwin() && T.getArch() == Triple::aarch64)
61 SupportsCompactUnwindWithoutEHFrame = true;
64 OmitDwarfIfHaveCompactUnwind = true;
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;
72 // .comm doesn't support alignment before Leopard.
73 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
74 CommDirectiveSupportsAlignment = false;
77 = Ctx->getMachOSection("__TEXT", "__text",
78 MachO::S_ATTR_PURE_INSTRUCTIONS,
79 SectionKind::getText());
81 = Ctx->getMachOSection("__DATA", "__data", 0, SectionKind::getData());
83 // BSSSection might not be expected initialized on msvc.
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());
95 // TODO: Verify datarel below.
97 = Ctx->getMachOSection("__DATA", "__thread_vars",
98 MachO::S_THREAD_LOCAL_VARIABLES,
99 SectionKind::getData());
101 TLSThreadInitSection = Ctx->getMachOSection(
102 "__DATA", "__thread_init", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
103 SectionKind::getData());
105 CStringSection // .cstring
106 = Ctx->getMachOSection("__TEXT", "__cstring",
107 MachO::S_CSTRING_LITERALS,
108 SectionKind::getMergeable1ByteCString());
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());
121 SixteenByteConstantSection // .literal16
122 = Ctx->getMachOSection("__TEXT", "__literal16",
123 MachO::S_16BYTE_LITERALS,
124 SectionKind::getMergeableConst16());
126 ReadOnlySection // .const
127 = Ctx->getMachOSection("__TEXT", "__const", 0,
128 SectionKind::getReadOnly());
130 // If the target is not powerpc, map the coal sections to the non-coal
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();
138 if (ArchTy == Triple::ppc || ArchTy == Triple::ppc64) {
140 = Ctx->getMachOSection("__TEXT", "__textcoal_nt",
142 MachO::S_ATTR_PURE_INSTRUCTIONS,
143 SectionKind::getText());
145 = Ctx->getMachOSection("__TEXT", "__const_coal",
147 SectionKind::getReadOnly());
148 DataCoalSection = Ctx->getMachOSection(
149 "__DATA", "__datacoal_nt", MachO::S_COALESCED, SectionKind::getData());
151 TextCoalSection = TextSection;
152 ConstTextCoalSection = ReadOnlySection;
153 DataCoalSection = DataSection;
156 ConstDataSection // .const_data
157 = Ctx->getMachOSection("__DATA", "__const", 0,
158 SectionKind::getReadOnlyWithRel());
160 = Ctx->getMachOSection("__DATA","__common",
162 SectionKind::getBSS());
164 = Ctx->getMachOSection("__DATA","__bss", MachO::S_ZEROFILL,
165 SectionKind::getBSS());
168 LazySymbolPointerSection
169 = Ctx->getMachOSection("__DATA", "__la_symbol_ptr",
170 MachO::S_LAZY_SYMBOL_POINTERS,
171 SectionKind::getMetadata());
172 NonLazySymbolPointerSection
173 = Ctx->getMachOSection("__DATA", "__nl_symbol_ptr",
174 MachO::S_NON_LAZY_SYMBOL_POINTERS,
175 SectionKind::getMetadata());
177 ThreadLocalPointerSection
178 = Ctx->getMachOSection("__DATA", "__thread_ptr",
179 MachO::S_THREAD_LOCAL_VARIABLE_POINTERS,
180 SectionKind::getMetadata());
182 // Exception Handling.
183 LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0,
184 SectionKind::getReadOnlyWithRel());
186 COFFDebugSymbolsSection = nullptr;
187 COFFDebugTypesSection = nullptr;
189 if (useCompactUnwind(T)) {
190 CompactUnwindSection =
191 Ctx->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG,
192 SectionKind::getReadOnly());
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
202 // Debug Information.
203 DwarfAccelNamesSection =
204 Ctx->getMachOSection("__DWARF", "__apple_names", MachO::S_ATTR_DEBUG,
205 SectionKind::getMetadata(), "names_begin");
206 DwarfAccelObjCSection =
207 Ctx->getMachOSection("__DWARF", "__apple_objc", MachO::S_ATTR_DEBUG,
208 SectionKind::getMetadata(), "objc_begin");
209 // 16 character section limit...
210 DwarfAccelNamespaceSection =
211 Ctx->getMachOSection("__DWARF", "__apple_namespac", MachO::S_ATTR_DEBUG,
212 SectionKind::getMetadata(), "namespac_begin");
213 DwarfAccelTypesSection =
214 Ctx->getMachOSection("__DWARF", "__apple_types", MachO::S_ATTR_DEBUG,
215 SectionKind::getMetadata(), "types_begin");
218 Ctx->getMachOSection("__DWARF", "__debug_abbrev", MachO::S_ATTR_DEBUG,
219 SectionKind::getMetadata(), "section_abbrev");
221 Ctx->getMachOSection("__DWARF", "__debug_info", MachO::S_ATTR_DEBUG,
222 SectionKind::getMetadata(), "section_info");
224 Ctx->getMachOSection("__DWARF", "__debug_line", MachO::S_ATTR_DEBUG,
225 SectionKind::getMetadata(), "section_line");
227 Ctx->getMachOSection("__DWARF", "__debug_frame", MachO::S_ATTR_DEBUG,
228 SectionKind::getMetadata());
229 DwarfPubNamesSection =
230 Ctx->getMachOSection("__DWARF", "__debug_pubnames", MachO::S_ATTR_DEBUG,
231 SectionKind::getMetadata());
232 DwarfPubTypesSection =
233 Ctx->getMachOSection("__DWARF", "__debug_pubtypes", MachO::S_ATTR_DEBUG,
234 SectionKind::getMetadata());
235 DwarfGnuPubNamesSection =
236 Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn", MachO::S_ATTR_DEBUG,
237 SectionKind::getMetadata());
238 DwarfGnuPubTypesSection =
239 Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt", MachO::S_ATTR_DEBUG,
240 SectionKind::getMetadata());
242 Ctx->getMachOSection("__DWARF", "__debug_str", MachO::S_ATTR_DEBUG,
243 SectionKind::getMetadata(), "info_string");
245 Ctx->getMachOSection("__DWARF", "__debug_str_offs", MachO::S_ATTR_DEBUG,
246 SectionKind::getMetadata(), "section_str_off");
248 Ctx->getMachOSection("__DWARF", "__debug_loc", MachO::S_ATTR_DEBUG,
249 SectionKind::getMetadata(), "section_debug_loc");
250 DwarfARangesSection =
251 Ctx->getMachOSection("__DWARF", "__debug_aranges", MachO::S_ATTR_DEBUG,
252 SectionKind::getMetadata());
254 Ctx->getMachOSection("__DWARF", "__debug_ranges", MachO::S_ATTR_DEBUG,
255 SectionKind::getMetadata(), "debug_range");
256 DwarfMacinfoSection =
257 Ctx->getMachOSection("__DWARF", "__debug_macinfo", MachO::S_ATTR_DEBUG,
258 SectionKind::getMetadata(), "debug_macinfo");
259 DwarfDebugInlineSection =
260 Ctx->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG,
261 SectionKind::getMetadata());
262 DwarfCUIndexSection =
263 Ctx->getMachOSection("__DWARF", "__debug_cu_index", MachO::S_ATTR_DEBUG,
264 SectionKind::getMetadata());
265 DwarfTUIndexSection =
266 Ctx->getMachOSection("__DWARF", "__debug_tu_index", MachO::S_ATTR_DEBUG,
267 SectionKind::getMetadata());
268 StackMapSection = Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps",
269 0, SectionKind::getMetadata());
271 FaultMapSection = Ctx->getMachOSection("__LLVM_FAULTMAPS", "__llvm_faultmaps",
272 0, SectionKind::getMetadata());
274 TLSExtraDataSection = TLSTLVSection;
277 void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T) {
278 switch (T.getArch()) {
281 FDECFIEncoding = dwarf::DW_EH_PE_sdata4;
284 case Triple::mips64el:
285 FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
288 FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
289 ((CMModel == CodeModel::Large) ? dwarf::DW_EH_PE_sdata8
290 : dwarf::DW_EH_PE_sdata4);
294 FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
297 FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
301 switch (T.getArch()) {
305 case Triple::thumbeb:
306 if (Ctx->getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
308 // Fallthrough if not using EHABI
312 PersonalityEncoding = PositionIndependent
313 ? dwarf::DW_EH_PE_indirect |
314 dwarf::DW_EH_PE_pcrel |
315 dwarf::DW_EH_PE_sdata4
316 : dwarf::DW_EH_PE_absptr;
317 LSDAEncoding = PositionIndependent
318 ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
319 : dwarf::DW_EH_PE_absptr;
320 TTypeEncoding = PositionIndependent
321 ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
322 dwarf::DW_EH_PE_sdata4
323 : dwarf::DW_EH_PE_absptr;
326 if (PositionIndependent) {
327 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
328 ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
329 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
330 LSDAEncoding = dwarf::DW_EH_PE_pcrel |
331 (CMModel == CodeModel::Small
332 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
333 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
334 ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
335 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
337 PersonalityEncoding =
338 (CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
339 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
340 LSDAEncoding = (CMModel == CodeModel::Small)
341 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
342 TTypeEncoding = (CMModel == CodeModel::Small)
343 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
346 case Triple::hexagon:
347 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
348 LSDAEncoding = dwarf::DW_EH_PE_absptr;
349 FDECFIEncoding = dwarf::DW_EH_PE_absptr;
350 TTypeEncoding = dwarf::DW_EH_PE_absptr;
351 if (PositionIndependent) {
352 PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
353 LSDAEncoding |= dwarf::DW_EH_PE_pcrel;
354 FDECFIEncoding |= dwarf::DW_EH_PE_pcrel;
355 TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
358 case Triple::aarch64:
359 case Triple::aarch64_be:
360 // The small model guarantees static code/data size < 4GB, but not where it
361 // will be in memory. Most of these could end up >2GB away so even a signed
362 // pc-relative 32-bit address is insufficient, theoretically.
363 if (PositionIndependent) {
364 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
365 dwarf::DW_EH_PE_sdata8;
366 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
367 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
368 dwarf::DW_EH_PE_sdata8;
370 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
371 LSDAEncoding = dwarf::DW_EH_PE_absptr;
372 TTypeEncoding = dwarf::DW_EH_PE_absptr;
376 LSDAEncoding = dwarf::DW_EH_PE_absptr;
377 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
378 TTypeEncoding = dwarf::DW_EH_PE_absptr;
383 case Triple::mips64el:
384 // MIPS uses indirect pointer to refer personality functions and types, so
385 // that the eh_frame section can be read-only. DW.ref.personality will be
386 // generated for relocation.
387 PersonalityEncoding = dwarf::DW_EH_PE_indirect;
388 // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
389 // identify N64 from just a triple.
390 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
391 dwarf::DW_EH_PE_sdata4;
392 // We don't support PC-relative LSDA references in GAS so we use the default
393 // DW_EH_PE_absptr for those.
395 // FreeBSD must be explicit about the data size and using pcrel since it's
396 // assembler/linker won't do the automatic conversion that the Linux tools
398 if (T.isOSFreeBSD()) {
399 PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
400 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
404 case Triple::ppc64le:
405 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
406 dwarf::DW_EH_PE_udata8;
407 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
408 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
409 dwarf::DW_EH_PE_udata8;
411 case Triple::sparcel:
413 if (PositionIndependent) {
414 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
415 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
416 dwarf::DW_EH_PE_sdata4;
417 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
418 dwarf::DW_EH_PE_sdata4;
420 LSDAEncoding = dwarf::DW_EH_PE_absptr;
421 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
422 TTypeEncoding = dwarf::DW_EH_PE_absptr;
425 case Triple::sparcv9:
426 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
427 if (PositionIndependent) {
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;
433 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
434 TTypeEncoding = dwarf::DW_EH_PE_absptr;
437 case Triple::systemz:
438 // All currently-defined code models guarantee that 4-byte PC-relative
439 // values will be in range.
440 if (PositionIndependent) {
441 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
442 dwarf::DW_EH_PE_sdata4;
443 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
444 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
445 dwarf::DW_EH_PE_sdata4;
447 PersonalityEncoding = dwarf::DW_EH_PE_absptr;
448 LSDAEncoding = dwarf::DW_EH_PE_absptr;
449 TTypeEncoding = dwarf::DW_EH_PE_absptr;
456 unsigned EHSectionType = T.getArch() == Triple::x86_64
457 ? ELF::SHT_X86_64_UNWIND
460 // Solaris requires different flags for .eh_frame to seemingly every other
462 unsigned EHSectionFlags = ELF::SHF_ALLOC;
463 if (T.isOSSolaris() && T.getArch() != Triple::x86_64)
464 EHSectionFlags |= ELF::SHF_WRITE;
467 BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
468 ELF::SHF_WRITE | ELF::SHF_ALLOC);
470 TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
471 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
473 DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
474 ELF::SHF_WRITE | ELF::SHF_ALLOC);
477 Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
480 Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
481 ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
483 TLSBSSSection = Ctx->getELFSection(
484 ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
486 DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
487 ELF::SHF_ALLOC | ELF::SHF_WRITE);
489 MergeableConst4Section =
490 Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
491 ELF::SHF_ALLOC | ELF::SHF_MERGE, 4, "");
493 MergeableConst8Section =
494 Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
495 ELF::SHF_ALLOC | ELF::SHF_MERGE, 8, "");
497 MergeableConst16Section =
498 Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
499 ELF::SHF_ALLOC | ELF::SHF_MERGE, 16, "");
501 MergeableConst32Section =
502 Ctx->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS,
503 ELF::SHF_ALLOC | ELF::SHF_MERGE, 32, "");
505 // Exception Handling Sections.
507 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
508 // it contains relocatable pointers. In PIC mode, this is probably a big
509 // runtime hit for C++ apps. Either the contents of the LSDA need to be
510 // adjusted or this should be a data section.
511 LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
514 COFFDebugSymbolsSection = nullptr;
515 COFFDebugTypesSection = nullptr;
517 unsigned DebugSecType = ELF::SHT_PROGBITS;
519 // MIPS .debug_* sections should have SHT_MIPS_DWARF section type
520 // to distinguish among sections contain DWARF and ECOFF debug formats.
521 // Sections with ECOFF debug format are obsoleted and marked by SHT_PROGBITS.
522 if (T.getArch() == Triple::mips || T.getArch() == Triple::mipsel ||
523 T.getArch() == Triple::mips64 || T.getArch() == Triple::mips64el)
524 DebugSecType = ELF::SHT_MIPS_DWARF;
526 // Debug Info Sections.
528 Ctx->getELFSection(".debug_abbrev", DebugSecType, 0);
529 DwarfInfoSection = Ctx->getELFSection(".debug_info", DebugSecType, 0);
530 DwarfLineSection = Ctx->getELFSection(".debug_line", DebugSecType, 0);
531 DwarfFrameSection = Ctx->getELFSection(".debug_frame", DebugSecType, 0);
532 DwarfPubNamesSection =
533 Ctx->getELFSection(".debug_pubnames", DebugSecType, 0);
534 DwarfPubTypesSection =
535 Ctx->getELFSection(".debug_pubtypes", DebugSecType, 0);
536 DwarfGnuPubNamesSection =
537 Ctx->getELFSection(".debug_gnu_pubnames", DebugSecType, 0);
538 DwarfGnuPubTypesSection =
539 Ctx->getELFSection(".debug_gnu_pubtypes", DebugSecType, 0);
541 Ctx->getELFSection(".debug_str", DebugSecType,
542 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
543 DwarfLocSection = Ctx->getELFSection(".debug_loc", DebugSecType, 0);
544 DwarfARangesSection =
545 Ctx->getELFSection(".debug_aranges", DebugSecType, 0);
547 Ctx->getELFSection(".debug_ranges", DebugSecType, 0);
548 DwarfMacinfoSection =
549 Ctx->getELFSection(".debug_macinfo", DebugSecType, 0);
551 // DWARF5 Experimental Debug Info
553 // Accelerator Tables
554 DwarfAccelNamesSection =
555 Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0);
556 DwarfAccelObjCSection =
557 Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0);
558 DwarfAccelNamespaceSection =
559 Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0);
560 DwarfAccelTypesSection =
561 Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0);
563 // String Offset and Address Sections
565 Ctx->getELFSection(".debug_str_offsets", DebugSecType, 0);
566 DwarfAddrSection = Ctx->getELFSection(".debug_addr", DebugSecType, 0);
569 DwarfInfoDWOSection =
570 Ctx->getELFSection(".debug_info.dwo", DebugSecType, 0);
571 DwarfTypesDWOSection =
572 Ctx->getELFSection(".debug_types.dwo", DebugSecType, 0);
573 DwarfAbbrevDWOSection =
574 Ctx->getELFSection(".debug_abbrev.dwo", DebugSecType, 0);
576 Ctx->getELFSection(".debug_str.dwo", DebugSecType,
577 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
578 DwarfLineDWOSection =
579 Ctx->getELFSection(".debug_line.dwo", DebugSecType, 0);
581 Ctx->getELFSection(".debug_loc.dwo", DebugSecType, 0);
582 DwarfStrOffDWOSection =
583 Ctx->getELFSection(".debug_str_offsets.dwo", DebugSecType, 0);
586 DwarfCUIndexSection =
587 Ctx->getELFSection(".debug_cu_index", DebugSecType, 0);
588 DwarfTUIndexSection =
589 Ctx->getELFSection(".debug_tu_index", DebugSecType, 0);
592 Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
595 Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
598 Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);
601 void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
602 EHFrameSection = Ctx->getCOFFSection(
603 ".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
604 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
605 SectionKind::getData());
607 // Set the `IMAGE_SCN_MEM_16BIT` flag when compiling for thumb mode. This is
608 // used to indicate to the linker that the text segment contains thumb instructions
609 // and to set the ISA selection bit for calls accordingly.
610 const bool IsThumb = T.getArch() == Triple::thumb;
612 CommDirectiveSupportsAlignment = true;
615 BSSSection = Ctx->getCOFFSection(
616 ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
617 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
618 SectionKind::getBSS());
619 TextSection = Ctx->getCOFFSection(
621 (IsThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) |
622 COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
623 COFF::IMAGE_SCN_MEM_READ,
624 SectionKind::getText());
625 DataSection = Ctx->getCOFFSection(
626 ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
627 COFF::IMAGE_SCN_MEM_WRITE,
628 SectionKind::getData());
629 ReadOnlySection = Ctx->getCOFFSection(
630 ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
631 SectionKind::getReadOnly());
633 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
634 // though it contains relocatable pointers. In PIC mode, this is probably a
635 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
636 // adjusted or this should be a data section.
637 if (T.getArch() == Triple::x86_64) {
638 // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
639 LSDASection = nullptr;
641 LSDASection = Ctx->getCOFFSection(".gcc_except_table",
642 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
643 COFF::IMAGE_SCN_MEM_READ,
644 SectionKind::getReadOnly());
648 COFFDebugSymbolsSection =
649 Ctx->getCOFFSection(".debug$S", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
650 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
651 COFF::IMAGE_SCN_MEM_READ),
652 SectionKind::getMetadata());
653 COFFDebugTypesSection =
654 Ctx->getCOFFSection(".debug$T", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
655 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
656 COFF::IMAGE_SCN_MEM_READ),
657 SectionKind::getMetadata());
659 DwarfAbbrevSection = Ctx->getCOFFSection(
661 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
662 COFF::IMAGE_SCN_MEM_READ,
663 SectionKind::getMetadata(), "section_abbrev");
664 DwarfInfoSection = Ctx->getCOFFSection(
666 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
667 COFF::IMAGE_SCN_MEM_READ,
668 SectionKind::getMetadata(), "section_info");
669 DwarfLineSection = Ctx->getCOFFSection(
671 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
672 COFF::IMAGE_SCN_MEM_READ,
673 SectionKind::getMetadata(), "section_line");
675 DwarfFrameSection = Ctx->getCOFFSection(
677 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
678 COFF::IMAGE_SCN_MEM_READ,
679 SectionKind::getMetadata());
680 DwarfPubNamesSection = Ctx->getCOFFSection(
682 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
683 COFF::IMAGE_SCN_MEM_READ,
684 SectionKind::getMetadata());
685 DwarfPubTypesSection = Ctx->getCOFFSection(
687 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
688 COFF::IMAGE_SCN_MEM_READ,
689 SectionKind::getMetadata());
690 DwarfGnuPubNamesSection = Ctx->getCOFFSection(
691 ".debug_gnu_pubnames",
692 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
693 COFF::IMAGE_SCN_MEM_READ,
694 SectionKind::getMetadata());
695 DwarfGnuPubTypesSection = Ctx->getCOFFSection(
696 ".debug_gnu_pubtypes",
697 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
698 COFF::IMAGE_SCN_MEM_READ,
699 SectionKind::getMetadata());
700 DwarfStrSection = Ctx->getCOFFSection(
702 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
703 COFF::IMAGE_SCN_MEM_READ,
704 SectionKind::getMetadata(), "info_string");
705 DwarfStrOffSection = Ctx->getCOFFSection(
706 ".debug_str_offsets",
707 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
708 COFF::IMAGE_SCN_MEM_READ,
709 SectionKind::getMetadata(), "section_str_off");
710 DwarfLocSection = Ctx->getCOFFSection(
712 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
713 COFF::IMAGE_SCN_MEM_READ,
714 SectionKind::getMetadata(), "section_debug_loc");
715 DwarfARangesSection = Ctx->getCOFFSection(
717 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
718 COFF::IMAGE_SCN_MEM_READ,
719 SectionKind::getMetadata());
720 DwarfRangesSection = Ctx->getCOFFSection(
722 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
723 COFF::IMAGE_SCN_MEM_READ,
724 SectionKind::getMetadata(), "debug_range");
725 DwarfMacinfoSection = Ctx->getCOFFSection(
727 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
728 COFF::IMAGE_SCN_MEM_READ,
729 SectionKind::getMetadata(), "debug_macinfo");
730 DwarfInfoDWOSection = Ctx->getCOFFSection(
732 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
733 COFF::IMAGE_SCN_MEM_READ,
734 SectionKind::getMetadata(), "section_info_dwo");
735 DwarfTypesDWOSection = Ctx->getCOFFSection(
737 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
738 COFF::IMAGE_SCN_MEM_READ,
739 SectionKind::getMetadata(), "section_types_dwo");
740 DwarfAbbrevDWOSection = Ctx->getCOFFSection(
742 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
743 COFF::IMAGE_SCN_MEM_READ,
744 SectionKind::getMetadata(), "section_abbrev_dwo");
745 DwarfStrDWOSection = Ctx->getCOFFSection(
747 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
748 COFF::IMAGE_SCN_MEM_READ,
749 SectionKind::getMetadata(), "skel_string");
750 DwarfLineDWOSection = Ctx->getCOFFSection(
752 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
753 COFF::IMAGE_SCN_MEM_READ,
754 SectionKind::getMetadata());
755 DwarfLocDWOSection = Ctx->getCOFFSection(
757 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
758 COFF::IMAGE_SCN_MEM_READ,
759 SectionKind::getMetadata(), "skel_loc");
760 DwarfStrOffDWOSection = Ctx->getCOFFSection(
761 ".debug_str_offsets.dwo",
762 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
763 COFF::IMAGE_SCN_MEM_READ,
764 SectionKind::getMetadata(), "section_str_off_dwo");
765 DwarfAddrSection = Ctx->getCOFFSection(
767 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
768 COFF::IMAGE_SCN_MEM_READ,
769 SectionKind::getMetadata(), "addr_sec");
770 DwarfCUIndexSection = Ctx->getCOFFSection(
772 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
773 COFF::IMAGE_SCN_MEM_READ,
774 SectionKind::getMetadata());
775 DwarfTUIndexSection = Ctx->getCOFFSection(
777 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
778 COFF::IMAGE_SCN_MEM_READ,
779 SectionKind::getMetadata());
780 DwarfAccelNamesSection = Ctx->getCOFFSection(
782 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
783 COFF::IMAGE_SCN_MEM_READ,
784 SectionKind::getMetadata(), "names_begin");
785 DwarfAccelNamespaceSection = Ctx->getCOFFSection(
787 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
788 COFF::IMAGE_SCN_MEM_READ,
789 SectionKind::getMetadata(), "namespac_begin");
790 DwarfAccelTypesSection = Ctx->getCOFFSection(
792 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
793 COFF::IMAGE_SCN_MEM_READ,
794 SectionKind::getMetadata(), "types_begin");
795 DwarfAccelObjCSection = Ctx->getCOFFSection(
797 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
798 COFF::IMAGE_SCN_MEM_READ,
799 SectionKind::getMetadata(), "objc_begin");
801 DrectveSection = Ctx->getCOFFSection(
802 ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE,
803 SectionKind::getMetadata());
805 PDataSection = Ctx->getCOFFSection(
806 ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
807 SectionKind::getData());
809 XDataSection = Ctx->getCOFFSection(
810 ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
811 SectionKind::getData());
813 SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO,
814 SectionKind::getMetadata());
816 TLSDataSection = Ctx->getCOFFSection(
817 ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
818 COFF::IMAGE_SCN_MEM_WRITE,
819 SectionKind::getData());
821 StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps",
822 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
823 COFF::IMAGE_SCN_MEM_READ,
824 SectionKind::getReadOnly());
827 void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
828 // TODO: Set the section types and flags.
829 TextSection = Ctx->getWasmSection(".text", 0, 0);
830 DataSection = Ctx->getWasmSection(".data", 0, 0);
832 // TODO: Set the section types and flags.
833 DwarfLineSection = Ctx->getWasmSection(".debug_line", 0, 0);
834 DwarfStrSection = Ctx->getWasmSection(".debug_str", 0, 0);
835 DwarfLocSection = Ctx->getWasmSection(".debug_loc", 0, 0);
836 DwarfAbbrevSection = Ctx->getWasmSection(".debug_abbrev", 0, 0, "section_abbrev");
837 DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", 0, 0);
838 DwarfRangesSection = Ctx->getWasmSection(".debug_ranges", 0, 0, "debug_range");
839 DwarfMacinfoSection = Ctx->getWasmSection(".debug_macinfo", 0, 0, "debug_macinfo");
840 DwarfAddrSection = Ctx->getWasmSection(".debug_addr", 0, 0);
841 DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", 0, 0);
842 DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", 0, 0);
843 DwarfInfoSection = Ctx->getWasmSection(".debug_info", 0, 0, "section_info");
844 DwarfFrameSection = Ctx->getWasmSection(".debug_frame", 0, 0);
845 DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", 0, 0);
846 DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", 0, 0);
848 // TODO: Define more sections.
851 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, bool PIC,
854 PositionIndependent = PIC;
859 CommDirectiveSupportsAlignment = true;
860 SupportsWeakOmittedEHFrame = true;
861 SupportsCompactUnwindWithoutEHFrame = false;
862 OmitDwarfIfHaveCompactUnwind = false;
864 PersonalityEncoding = LSDAEncoding = FDECFIEncoding = TTypeEncoding =
865 dwarf::DW_EH_PE_absptr;
867 CompactUnwindDwarfEHFrameOnly = 0;
869 EHFrameSection = nullptr; // Created on demand.
870 CompactUnwindSection = nullptr; // Used only by selected targets.
871 DwarfAccelNamesSection = nullptr; // Used only by selected targets.
872 DwarfAccelObjCSection = nullptr; // Used only by selected targets.
873 DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
874 DwarfAccelTypesSection = nullptr; // Used only by selected targets.
878 switch (TT.getObjectFormat()) {
881 initMachOMCObjectFileInfo(TT);
884 if (!TT.isOSWindows())
886 "Cannot initialize MC for non-Windows COFF object files.");
889 initCOFFMCObjectFileInfo(TT);
893 initELFMCObjectFileInfo(TT);
897 initWasmMCObjectFileInfo(TT);
899 case Triple::UnknownObjectFormat:
900 report_fatal_error("Cannot initialize MC for unknown object file format.");
905 MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const {
906 return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP,