1//===-- MCObjectFileInfo.cpp - Object File Information --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/MC/MCObjectFileInfo.h"
10#include "llvm/ADT/StringExtras.h"
11#include "llvm/BinaryFormat/COFF.h"
12#include "llvm/BinaryFormat/ELF.h"
13#include "llvm/BinaryFormat/GOFF.h"
14#include "llvm/BinaryFormat/SFrame.h"
15#include "llvm/BinaryFormat/Wasm.h"
16#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCGOFFAttributes.h"
19#include "llvm/MC/MCSection.h"
20#include "llvm/MC/MCSectionCOFF.h"
21#include "llvm/MC/MCSectionDXContainer.h"
22#include "llvm/MC/MCSectionELF.h"
23#include "llvm/MC/MCSectionGOFF.h"
24#include "llvm/MC/MCSectionMachO.h"
25#include "llvm/MC/MCSectionSPIRV.h"
26#include "llvm/MC/MCSectionWasm.h"
27#include "llvm/MC/MCSectionXCOFF.h"
28#include "llvm/MC/MCSymbolGOFF.h"
29#include "llvm/MC/SectionKind.h"
30#include "llvm/TargetParser/Triple.h"
31
32using namespace llvm;
33
34static bool useCompactUnwind(const Triple &T) {
35 // Only on darwin.
36 if (!T.isOSDarwin())
37 return false;
38
39 // aarch64 always has it.
40 if (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32)
41 return true;
42
43 // armv7k always has it.
44 if (T.isWatchABI())
45 return true;
46
47 // Use it on newer version of OS X.
48 if (T.isMacOSX() && !T.isMacOSXVersionLT(Major: 10, Minor: 6))
49 return true;
50
51 // And the iOS simulator.
52 if (T.isiOS() && T.isX86())
53 return true;
54
55 // The rest of the simulators always have it.
56 if (T.isSimulatorEnvironment())
57 return true;
58
59 // XROS always has it.
60 if (T.isXROS())
61 return true;
62
63 return false;
64}
65
66void MCObjectFileInfo::initMachOMCObjectFileInfo(const Triple &T) {
67 EHFrameSection = Ctx->getMachOSection(
68 Segment: "__TEXT", Section: "__eh_frame",
69 TypeAndAttributes: MachO::S_COALESCED | MachO::S_ATTR_NO_TOC |
70 MachO::S_ATTR_STRIP_STATIC_SYMS | MachO::S_ATTR_LIVE_SUPPORT,
71 K: SectionKind::getReadOnly());
72
73 if (T.isOSDarwin() &&
74 (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32 ||
75 T.isSimulatorEnvironment()))
76 SupportsCompactUnwindWithoutEHFrame = true;
77
78 switch (Ctx->emitDwarfUnwindInfo()) {
79 case EmitDwarfUnwindType::Always:
80 case EmitDwarfUnwindType::DwarfOnly:
81 OmitDwarfIfHaveCompactUnwind = false;
82 break;
83 case EmitDwarfUnwindType::NoCompactUnwind:
84 OmitDwarfIfHaveCompactUnwind = true;
85 break;
86 case EmitDwarfUnwindType::Default:
87 OmitDwarfIfHaveCompactUnwind =
88 T.isWatchABI() || SupportsCompactUnwindWithoutEHFrame;
89 break;
90 }
91
92 FDECFIEncoding = dwarf::DW_EH_PE_pcrel;
93
94 TextSection // .text
95 = Ctx->getMachOSection(Segment: "__TEXT", Section: "__text",
96 TypeAndAttributes: MachO::S_ATTR_PURE_INSTRUCTIONS,
97 K: SectionKind::getText());
98 DataSection // .data
99 = Ctx->getMachOSection(Segment: "__DATA", Section: "__data", TypeAndAttributes: 0, K: SectionKind::getData());
100
101 // BSSSection might not be expected initialized on msvc.
102 BSSSection = nullptr;
103
104 TLSDataSection // .tdata
105 = Ctx->getMachOSection(Segment: "__DATA", Section: "__thread_data",
106 TypeAndAttributes: MachO::S_THREAD_LOCAL_REGULAR,
107 K: SectionKind::getData());
108 TLSBSSSection // .tbss
109 = Ctx->getMachOSection(Segment: "__DATA", Section: "__thread_bss",
110 TypeAndAttributes: MachO::S_THREAD_LOCAL_ZEROFILL,
111 K: SectionKind::getThreadBSS());
112
113 // TODO: Verify datarel below.
114 TLSTLVSection // .tlv
115 = Ctx->getMachOSection(Segment: "__DATA", Section: "__thread_vars",
116 TypeAndAttributes: MachO::S_THREAD_LOCAL_VARIABLES,
117 K: SectionKind::getData());
118
119 TLSThreadInitSection = Ctx->getMachOSection(
120 Segment: "__DATA", Section: "__thread_init", TypeAndAttributes: MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
121 K: SectionKind::getData());
122
123 CStringSection // .cstring
124 = Ctx->getMachOSection(Segment: "__TEXT", Section: "__cstring",
125 TypeAndAttributes: MachO::S_CSTRING_LITERALS,
126 K: SectionKind::getMergeable1ByteCString());
127 UStringSection
128 = Ctx->getMachOSection(Segment: "__TEXT",Section: "__ustring", TypeAndAttributes: 0,
129 K: SectionKind::getMergeable2ByteCString());
130 FourByteConstantSection // .literal4
131 = Ctx->getMachOSection(Segment: "__TEXT", Section: "__literal4",
132 TypeAndAttributes: MachO::S_4BYTE_LITERALS,
133 K: SectionKind::getMergeableConst4());
134 EightByteConstantSection // .literal8
135 = Ctx->getMachOSection(Segment: "__TEXT", Section: "__literal8",
136 TypeAndAttributes: MachO::S_8BYTE_LITERALS,
137 K: SectionKind::getMergeableConst8());
138
139 SixteenByteConstantSection // .literal16
140 = Ctx->getMachOSection(Segment: "__TEXT", Section: "__literal16",
141 TypeAndAttributes: MachO::S_16BYTE_LITERALS,
142 K: SectionKind::getMergeableConst16());
143
144 ReadOnlySection // .const
145 = Ctx->getMachOSection(Segment: "__TEXT", Section: "__const", TypeAndAttributes: 0,
146 K: SectionKind::getReadOnly());
147
148 // If the target is not powerpc, map the coal sections to the non-coal
149 // sections.
150 //
151 // "__TEXT/__textcoal_nt" => section "__TEXT/__text"
152 // "__TEXT/__const_coal" => section "__TEXT/__const"
153 // "__DATA/__datacoal_nt" => section "__DATA/__data"
154 Triple::ArchType ArchTy = T.getArch();
155
156 ConstDataSection // .const_data
157 = Ctx->getMachOSection(Segment: "__DATA", Section: "__const", TypeAndAttributes: 0,
158 K: SectionKind::getReadOnlyWithRel());
159
160 if (ArchTy == Triple::ppc || ArchTy == Triple::ppc64) {
161 TextCoalSection
162 = Ctx->getMachOSection(Segment: "__TEXT", Section: "__textcoal_nt",
163 TypeAndAttributes: MachO::S_COALESCED |
164 MachO::S_ATTR_PURE_INSTRUCTIONS,
165 K: SectionKind::getText());
166 ConstTextCoalSection
167 = Ctx->getMachOSection(Segment: "__TEXT", Section: "__const_coal",
168 TypeAndAttributes: MachO::S_COALESCED,
169 K: SectionKind::getReadOnly());
170 DataCoalSection = Ctx->getMachOSection(
171 Segment: "__DATA", Section: "__datacoal_nt", TypeAndAttributes: MachO::S_COALESCED, K: SectionKind::getData());
172 ConstDataCoalSection = DataCoalSection;
173 } else {
174 TextCoalSection = TextSection;
175 ConstTextCoalSection = ReadOnlySection;
176 DataCoalSection = DataSection;
177 ConstDataCoalSection = ConstDataSection;
178 }
179
180 DataCommonSection
181 = Ctx->getMachOSection(Segment: "__DATA",Section: "__common",
182 TypeAndAttributes: MachO::S_ZEROFILL,
183 K: SectionKind::getBSS());
184 DataBSSSection
185 = Ctx->getMachOSection(Segment: "__DATA",Section: "__bss", TypeAndAttributes: MachO::S_ZEROFILL,
186 K: SectionKind::getBSS());
187
188
189 LazySymbolPointerSection
190 = Ctx->getMachOSection(Segment: "__DATA", Section: "__la_symbol_ptr",
191 TypeAndAttributes: MachO::S_LAZY_SYMBOL_POINTERS,
192 K: SectionKind::getMetadata());
193 NonLazySymbolPointerSection
194 = Ctx->getMachOSection(Segment: "__DATA", Section: "__nl_symbol_ptr",
195 TypeAndAttributes: MachO::S_NON_LAZY_SYMBOL_POINTERS,
196 K: SectionKind::getMetadata());
197
198 ThreadLocalPointerSection
199 = Ctx->getMachOSection(Segment: "__DATA", Section: "__thread_ptr",
200 TypeAndAttributes: MachO::S_THREAD_LOCAL_VARIABLE_POINTERS,
201 K: SectionKind::getMetadata());
202
203 AddrSigSection = Ctx->getMachOSection(Segment: "__DATA", Section: "__llvm_addrsig", TypeAndAttributes: 0,
204 K: SectionKind::getData());
205
206 // Exception Handling.
207 LSDASection = Ctx->getMachOSection(Segment: "__TEXT", Section: "__gcc_except_tab", TypeAndAttributes: 0,
208 K: SectionKind::getReadOnlyWithRel());
209
210 COFFDebugSymbolsSection = nullptr;
211 COFFDebugTypesSection = nullptr;
212 COFFGlobalTypeHashesSection = nullptr;
213
214 if (useCompactUnwind(T)) {
215 CompactUnwindSection =
216 Ctx->getMachOSection(Segment: "__LD", Section: "__compact_unwind", TypeAndAttributes: MachO::S_ATTR_DEBUG,
217 K: SectionKind::getReadOnly());
218
219 if (T.isX86())
220 CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_X86_64_MODE_DWARF
221 else if (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32)
222 CompactUnwindDwarfEHFrameOnly = 0x03000000; // UNWIND_ARM64_MODE_DWARF
223 else if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb)
224 CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_ARM_MODE_DWARF
225 }
226
227 // Debug Information.
228 DwarfDebugNamesSection =
229 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_names", TypeAndAttributes: MachO::S_ATTR_DEBUG,
230 K: SectionKind::getMetadata(), BeginSymName: "debug_names_begin");
231 DwarfAccelNamesSection =
232 Ctx->getMachOSection(Segment: "__DWARF", Section: "__apple_names", TypeAndAttributes: MachO::S_ATTR_DEBUG,
233 K: SectionKind::getMetadata(), BeginSymName: "names_begin");
234 DwarfAccelObjCSection =
235 Ctx->getMachOSection(Segment: "__DWARF", Section: "__apple_objc", TypeAndAttributes: MachO::S_ATTR_DEBUG,
236 K: SectionKind::getMetadata(), BeginSymName: "objc_begin");
237 // 16 character section limit...
238 DwarfAccelNamespaceSection =
239 Ctx->getMachOSection(Segment: "__DWARF", Section: "__apple_namespac", TypeAndAttributes: MachO::S_ATTR_DEBUG,
240 K: SectionKind::getMetadata(), BeginSymName: "namespac_begin");
241 DwarfAccelTypesSection =
242 Ctx->getMachOSection(Segment: "__DWARF", Section: "__apple_types", TypeAndAttributes: MachO::S_ATTR_DEBUG,
243 K: SectionKind::getMetadata(), BeginSymName: "types_begin");
244
245 DwarfSwiftASTSection =
246 Ctx->getMachOSection(Segment: "__DWARF", Section: "__swift_ast", TypeAndAttributes: MachO::S_ATTR_DEBUG,
247 K: SectionKind::getMetadata());
248
249 DwarfAbbrevSection =
250 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_abbrev", TypeAndAttributes: MachO::S_ATTR_DEBUG,
251 K: SectionKind::getMetadata(), BeginSymName: "section_abbrev");
252 DwarfInfoSection =
253 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_info", TypeAndAttributes: MachO::S_ATTR_DEBUG,
254 K: SectionKind::getMetadata(), BeginSymName: "section_info");
255 DwarfLineSection =
256 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_line", TypeAndAttributes: MachO::S_ATTR_DEBUG,
257 K: SectionKind::getMetadata(), BeginSymName: "section_line");
258 DwarfLineStrSection =
259 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_line_str", TypeAndAttributes: MachO::S_ATTR_DEBUG,
260 K: SectionKind::getMetadata(), BeginSymName: "section_line_str");
261 DwarfFrameSection =
262 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_frame", TypeAndAttributes: MachO::S_ATTR_DEBUG,
263 K: SectionKind::getMetadata(), BeginSymName: "section_frame");
264 DwarfPubNamesSection =
265 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_pubnames", TypeAndAttributes: MachO::S_ATTR_DEBUG,
266 K: SectionKind::getMetadata());
267 DwarfPubTypesSection =
268 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_pubtypes", TypeAndAttributes: MachO::S_ATTR_DEBUG,
269 K: SectionKind::getMetadata());
270 DwarfGnuPubNamesSection =
271 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_gnu_pubn", TypeAndAttributes: MachO::S_ATTR_DEBUG,
272 K: SectionKind::getMetadata());
273 DwarfGnuPubTypesSection =
274 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_gnu_pubt", TypeAndAttributes: MachO::S_ATTR_DEBUG,
275 K: SectionKind::getMetadata());
276 DwarfStrSection =
277 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_str", TypeAndAttributes: MachO::S_ATTR_DEBUG,
278 K: SectionKind::getMetadata(), BeginSymName: "info_string");
279 DwarfStrOffSection =
280 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_str_offs", TypeAndAttributes: MachO::S_ATTR_DEBUG,
281 K: SectionKind::getMetadata(), BeginSymName: "section_str_off");
282 DwarfAddrSection =
283 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_addr", TypeAndAttributes: MachO::S_ATTR_DEBUG,
284 K: SectionKind::getMetadata(), BeginSymName: "section_info");
285 DwarfLocSection =
286 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_loc", TypeAndAttributes: MachO::S_ATTR_DEBUG,
287 K: SectionKind::getMetadata(), BeginSymName: "section_debug_loc");
288 DwarfLoclistsSection =
289 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_loclists", TypeAndAttributes: MachO::S_ATTR_DEBUG,
290 K: SectionKind::getMetadata(), BeginSymName: "section_debug_loc");
291
292 DwarfARangesSection =
293 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_aranges", TypeAndAttributes: MachO::S_ATTR_DEBUG,
294 K: SectionKind::getMetadata());
295 DwarfRangesSection =
296 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_ranges", TypeAndAttributes: MachO::S_ATTR_DEBUG,
297 K: SectionKind::getMetadata(), BeginSymName: "debug_range");
298 DwarfRnglistsSection =
299 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_rnglists", TypeAndAttributes: MachO::S_ATTR_DEBUG,
300 K: SectionKind::getMetadata(), BeginSymName: "debug_range");
301 DwarfMacinfoSection =
302 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_macinfo", TypeAndAttributes: MachO::S_ATTR_DEBUG,
303 K: SectionKind::getMetadata(), BeginSymName: "debug_macinfo");
304 DwarfMacroSection =
305 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_macro", TypeAndAttributes: MachO::S_ATTR_DEBUG,
306 K: SectionKind::getMetadata(), BeginSymName: "debug_macro");
307 DwarfDebugInlineSection =
308 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_inlined", TypeAndAttributes: MachO::S_ATTR_DEBUG,
309 K: SectionKind::getMetadata());
310 DwarfCUIndexSection =
311 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_cu_index", TypeAndAttributes: MachO::S_ATTR_DEBUG,
312 K: SectionKind::getMetadata());
313 DwarfTUIndexSection =
314 Ctx->getMachOSection(Segment: "__DWARF", Section: "__debug_tu_index", TypeAndAttributes: MachO::S_ATTR_DEBUG,
315 K: SectionKind::getMetadata());
316 StackMapSection = Ctx->getMachOSection(Segment: "__LLVM_STACKMAPS", Section: "__llvm_stackmaps",
317 TypeAndAttributes: 0, K: SectionKind::getMetadata());
318
319 FaultMapSection = Ctx->getMachOSection(Segment: "__LLVM_FAULTMAPS", Section: "__llvm_faultmaps",
320 TypeAndAttributes: 0, K: SectionKind::getMetadata());
321
322 RemarksSection = Ctx->getMachOSection(
323 Segment: "__LLVM", Section: "__remarks", TypeAndAttributes: MachO::S_ATTR_DEBUG, K: SectionKind::getMetadata());
324
325 PseudoProbeSection =
326 Ctx->getMachOSection(Segment: "__PSEUDO_PROBE", Section: "__probes",
327 TypeAndAttributes: MachO::S_ATTR_DEBUG | MachO::S_ATTR_NO_DEAD_STRIP,
328 K: SectionKind::getMetadata());
329 PseudoProbeDescSection =
330 Ctx->getMachOSection(Segment: "__PSEUDO_PROBE", Section: "__probe_descs",
331 TypeAndAttributes: MachO::S_ATTR_DEBUG | MachO::S_ATTR_NO_DEAD_STRIP,
332 K: SectionKind::getMetadata());
333
334 // The architecture of dsymutil makes it very difficult to copy the Swift
335 // reflection metadata sections into the __TEXT segment, so dsymutil creates
336 // these sections in the __DWARF segment instead.
337 if (!Ctx->getSwift5ReflectionSegmentName().empty()) {
338#define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF) \
339 Swift5ReflectionSections \
340 [llvm::binaryformat::Swift5ReflectionSectionKind::KIND] = \
341 Ctx->getMachOSection(Ctx->getSwift5ReflectionSegmentName().data(), \
342 MACHO, 0, SectionKind::getMetadata());
343#include "llvm/BinaryFormat/Swift.def"
344 }
345
346 TLSExtraDataSection = TLSTLVSection;
347}
348
349void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
350 switch (T.getArch()) {
351 case Triple::mips:
352 case Triple::mipsel:
353 case Triple::mips64:
354 case Triple::mips64el:
355 // We cannot use DW_EH_PE_sdata8 for the large PositionIndependent case
356 // since there is no R_MIPS_PC64 relocation (only a 32-bit version).
357 // In fact DW_EH_PE_sdata4 is enough for us now, and GNU ld doesn't
358 // support pcrel|sdata8 well. Let's use sdata4 for now.
359 if (PositionIndependent)
360 FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
361 else
362 FDECFIEncoding = Ctx->getAsmInfo().getCodePointerSize() == 4
363 ? dwarf::DW_EH_PE_sdata4
364 : dwarf::DW_EH_PE_sdata8;
365 break;
366 case Triple::ppc64:
367 case Triple::ppc64le:
368 case Triple::aarch64:
369 case Triple::aarch64_be:
370 case Triple::x86_64:
371 FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
372 ((Large || Ctx->getTargetOptions().LargeEHEncoding)
373 ? dwarf::DW_EH_PE_sdata8
374 : dwarf::DW_EH_PE_sdata4);
375 break;
376 case Triple::bpfel:
377 case Triple::bpfeb:
378 FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
379 break;
380 case Triple::hexagon:
381 FDECFIEncoding =
382 PositionIndependent ? dwarf::DW_EH_PE_pcrel : dwarf::DW_EH_PE_absptr;
383 break;
384 case Triple::xtensa:
385 FDECFIEncoding = dwarf::DW_EH_PE_sdata4;
386 break;
387 default:
388 FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
389 break;
390 }
391
392 unsigned EHSectionType = T.getArch() == Triple::x86_64
393 ? ELF::SHT_X86_64_UNWIND
394 : ELF::SHT_PROGBITS;
395 switch (T.getArch()) {
396 case Triple::x86_64:
397 SFrameABIArch = sframe::ABI::AMD64EndianLittle;
398 break;
399 case Triple::aarch64:
400 SFrameABIArch = sframe::ABI::AArch64EndianLittle;
401 break;
402 case Triple::aarch64_be:
403 SFrameABIArch = sframe::ABI::AArch64EndianBig;
404 break;
405 default:
406 break;
407 }
408
409 // Solaris requires different flags for .eh_frame to seemingly every other
410 // platform.
411 unsigned EHSectionFlags = ELF::SHF_ALLOC;
412 if (T.isOSSolaris() && T.getArch() != Triple::x86_64)
413 EHSectionFlags |= ELF::SHF_WRITE;
414
415 // ELF
416 BSSSection = Ctx->getELFSection(Section: ".bss", Type: ELF::SHT_NOBITS,
417 Flags: ELF::SHF_WRITE | ELF::SHF_ALLOC);
418
419 TextSection = Ctx->getELFSection(Section: ".text", Type: ELF::SHT_PROGBITS,
420 Flags: ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
421
422 DataSection = Ctx->getELFSection(Section: ".data", Type: ELF::SHT_PROGBITS,
423 Flags: ELF::SHF_WRITE | ELF::SHF_ALLOC);
424
425 ReadOnlySection =
426 Ctx->getELFSection(Section: ".rodata", Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_ALLOC);
427
428 TLSDataSection =
429 Ctx->getELFSection(Section: ".tdata", Type: ELF::SHT_PROGBITS,
430 Flags: ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
431
432 TLSBSSSection = Ctx->getELFSection(
433 Section: ".tbss", Type: ELF::SHT_NOBITS, Flags: ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
434
435 DataRelROSection = Ctx->getELFSection(Section: ".data.rel.ro", Type: ELF::SHT_PROGBITS,
436 Flags: ELF::SHF_ALLOC | ELF::SHF_WRITE);
437
438 unsigned MergeableCstFlags = ELF::SHF_ALLOC | ELF::SHF_MERGE;
439 StringRef CstPrefix = ".rodata";
440 if (Large && T.getArch() == Triple::x86_64) {
441 MergeableCstFlags |= ELF::SHF_X86_64_LARGE;
442 CstPrefix = ".lrodata";
443 }
444
445 MergeableConst4Section = Ctx->getELFSection(
446 Section: CstPrefix + ".cst4", Type: ELF::SHT_PROGBITS, Flags: MergeableCstFlags, EntrySize: 4);
447
448 MergeableConst8Section = Ctx->getELFSection(
449 Section: CstPrefix + ".cst8", Type: ELF::SHT_PROGBITS, Flags: MergeableCstFlags, EntrySize: 8);
450
451 MergeableConst16Section = Ctx->getELFSection(
452 Section: CstPrefix + ".cst16", Type: ELF::SHT_PROGBITS, Flags: MergeableCstFlags, EntrySize: 16);
453
454 MergeableConst32Section = Ctx->getELFSection(
455 Section: CstPrefix + ".cst32", Type: ELF::SHT_PROGBITS, Flags: MergeableCstFlags, EntrySize: 32);
456
457 // Exception Handling Sections.
458
459 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
460 // it contains relocatable pointers. In PIC mode, this is probably a big
461 // runtime hit for C++ apps. Either the contents of the LSDA need to be
462 // adjusted or this should be a data section.
463 LSDASection = Ctx->getELFSection(Section: ".gcc_except_table", Type: ELF::SHT_PROGBITS,
464 Flags: ELF::SHF_ALLOC);
465
466 COFFDebugSymbolsSection = nullptr;
467 COFFDebugTypesSection = nullptr;
468
469 unsigned DebugSecType = ELF::SHT_PROGBITS;
470
471 // MIPS .debug_* sections should have SHT_MIPS_DWARF section type
472 // to distinguish among sections contain DWARF and ECOFF debug formats.
473 // Sections with ECOFF debug format are obsoleted and marked by SHT_PROGBITS.
474 if (T.isMIPS())
475 DebugSecType = ELF::SHT_MIPS_DWARF;
476
477 // Debug Info Sections.
478 DwarfAbbrevSection =
479 Ctx->getELFSection(Section: ".debug_abbrev", Type: DebugSecType, Flags: 0);
480 DwarfInfoSection = Ctx->getELFSection(Section: ".debug_info", Type: DebugSecType, Flags: 0);
481 DwarfLineSection = Ctx->getELFSection(Section: ".debug_line", Type: DebugSecType, Flags: 0);
482 DwarfLineStrSection =
483 Ctx->getELFSection(Section: ".debug_line_str", Type: DebugSecType,
484 Flags: ELF::SHF_MERGE | ELF::SHF_STRINGS, EntrySize: 1);
485 DwarfFrameSection = Ctx->getELFSection(Section: ".debug_frame", Type: DebugSecType, Flags: 0);
486 DwarfPubNamesSection =
487 Ctx->getELFSection(Section: ".debug_pubnames", Type: DebugSecType, Flags: 0);
488 DwarfPubTypesSection =
489 Ctx->getELFSection(Section: ".debug_pubtypes", Type: DebugSecType, Flags: 0);
490 DwarfGnuPubNamesSection =
491 Ctx->getELFSection(Section: ".debug_gnu_pubnames", Type: DebugSecType, Flags: 0);
492 DwarfGnuPubTypesSection =
493 Ctx->getELFSection(Section: ".debug_gnu_pubtypes", Type: DebugSecType, Flags: 0);
494 DwarfStrSection =
495 Ctx->getELFSection(Section: ".debug_str", Type: DebugSecType,
496 Flags: ELF::SHF_MERGE | ELF::SHF_STRINGS, EntrySize: 1);
497 DwarfLocSection = Ctx->getELFSection(Section: ".debug_loc", Type: DebugSecType, Flags: 0);
498 DwarfARangesSection =
499 Ctx->getELFSection(Section: ".debug_aranges", Type: DebugSecType, Flags: 0);
500 DwarfRangesSection =
501 Ctx->getELFSection(Section: ".debug_ranges", Type: DebugSecType, Flags: 0);
502 DwarfMacinfoSection =
503 Ctx->getELFSection(Section: ".debug_macinfo", Type: DebugSecType, Flags: 0);
504 DwarfMacroSection = Ctx->getELFSection(Section: ".debug_macro", Type: DebugSecType, Flags: 0);
505
506 // DWARF5 Experimental Debug Info
507
508 // Accelerator Tables
509 DwarfDebugNamesSection =
510 Ctx->getELFSection(Section: ".debug_names", Type: ELF::SHT_PROGBITS, Flags: 0);
511 DwarfAccelNamesSection =
512 Ctx->getELFSection(Section: ".apple_names", Type: ELF::SHT_PROGBITS, Flags: 0);
513 DwarfAccelObjCSection =
514 Ctx->getELFSection(Section: ".apple_objc", Type: ELF::SHT_PROGBITS, Flags: 0);
515 DwarfAccelNamespaceSection =
516 Ctx->getELFSection(Section: ".apple_namespaces", Type: ELF::SHT_PROGBITS, Flags: 0);
517 DwarfAccelTypesSection =
518 Ctx->getELFSection(Section: ".apple_types", Type: ELF::SHT_PROGBITS, Flags: 0);
519
520 // String Offset and Address Sections
521 DwarfStrOffSection =
522 Ctx->getELFSection(Section: ".debug_str_offsets", Type: DebugSecType, Flags: 0);
523 DwarfAddrSection = Ctx->getELFSection(Section: ".debug_addr", Type: DebugSecType, Flags: 0);
524 DwarfRnglistsSection = Ctx->getELFSection(Section: ".debug_rnglists", Type: DebugSecType, Flags: 0);
525 DwarfLoclistsSection = Ctx->getELFSection(Section: ".debug_loclists", Type: DebugSecType, Flags: 0);
526
527 // Fission Sections
528 DwarfInfoDWOSection =
529 Ctx->getELFSection(Section: ".debug_info.dwo", Type: DebugSecType, Flags: ELF::SHF_EXCLUDE);
530 DwarfTypesDWOSection =
531 Ctx->getELFSection(Section: ".debug_types.dwo", Type: DebugSecType, Flags: ELF::SHF_EXCLUDE);
532 DwarfAbbrevDWOSection =
533 Ctx->getELFSection(Section: ".debug_abbrev.dwo", Type: DebugSecType, Flags: ELF::SHF_EXCLUDE);
534 DwarfStrDWOSection = Ctx->getELFSection(
535 Section: ".debug_str.dwo", Type: DebugSecType,
536 Flags: ELF::SHF_MERGE | ELF::SHF_STRINGS | ELF::SHF_EXCLUDE, EntrySize: 1);
537 DwarfLineDWOSection =
538 Ctx->getELFSection(Section: ".debug_line.dwo", Type: DebugSecType, Flags: ELF::SHF_EXCLUDE);
539 DwarfLocDWOSection =
540 Ctx->getELFSection(Section: ".debug_loc.dwo", Type: DebugSecType, Flags: ELF::SHF_EXCLUDE);
541 DwarfStrOffDWOSection = Ctx->getELFSection(Section: ".debug_str_offsets.dwo",
542 Type: DebugSecType, Flags: ELF::SHF_EXCLUDE);
543 DwarfRnglistsDWOSection =
544 Ctx->getELFSection(Section: ".debug_rnglists.dwo", Type: DebugSecType, Flags: ELF::SHF_EXCLUDE);
545 DwarfMacinfoDWOSection =
546 Ctx->getELFSection(Section: ".debug_macinfo.dwo", Type: DebugSecType, Flags: ELF::SHF_EXCLUDE);
547 DwarfMacroDWOSection =
548 Ctx->getELFSection(Section: ".debug_macro.dwo", Type: DebugSecType, Flags: ELF::SHF_EXCLUDE);
549
550 DwarfLoclistsDWOSection =
551 Ctx->getELFSection(Section: ".debug_loclists.dwo", Type: DebugSecType, Flags: ELF::SHF_EXCLUDE);
552
553 // DWP Sections
554 DwarfCUIndexSection =
555 Ctx->getELFSection(Section: ".debug_cu_index", Type: DebugSecType, Flags: 0);
556 DwarfTUIndexSection =
557 Ctx->getELFSection(Section: ".debug_tu_index", Type: DebugSecType, Flags: 0);
558
559 StackMapSection =
560 Ctx->getELFSection(Section: ".llvm_stackmaps", Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_ALLOC);
561
562 FaultMapSection =
563 Ctx->getELFSection(Section: ".llvm_faultmaps", Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_ALLOC);
564
565 EHFrameSection =
566 Ctx->getELFSection(Section: ".eh_frame", Type: EHSectionType, Flags: EHSectionFlags);
567
568 SFrameSection =
569 Ctx->getELFSection(Section: ".sframe", Type: ELF::SHT_GNU_SFRAME, Flags: ELF::SHF_ALLOC);
570
571 CallGraphSection =
572 Ctx->getELFSection(Section: ".llvm.callgraph", Type: ELF::SHT_LLVM_CALL_GRAPH, Flags: 0);
573
574 StackSizesSection = Ctx->getELFSection(Section: ".stack_sizes", Type: ELF::SHT_PROGBITS, Flags: 0);
575
576 PseudoProbeSection = Ctx->getELFSection(Section: ".pseudo_probe", Type: DebugSecType, Flags: 0);
577 PseudoProbeDescSection =
578 Ctx->getELFSection(Section: ".pseudo_probe_desc", Type: DebugSecType, Flags: 0);
579
580 LLVMStatsSection = Ctx->getELFSection(Section: ".llvm_stats", Type: ELF::SHT_PROGBITS, Flags: 0);
581}
582
583void MCObjectFileInfo::initGOFFMCObjectFileInfo(const Triple &T) {
584 MCSectionGOFF *RootSDSection = Ctx->getGOFFSection(
585 Kind: SectionKind::getMetadata(), Name: "#C",
586 SDAttributes: GOFF::SDAttr{.TaskingBehavior: GOFF::ESD_TA_Rent, .BindingScope: GOFF::ESD_BSC_Section});
587
588 MCSectionGOFF *ADAEDSection = Ctx->getGOFFSection(
589 Kind: SectionKind::getMetadata(), Name: GOFF::CLASS_WSA,
590 EDAttributes: GOFF::EDAttr{.IsReadOnly: false, .Rmode: GOFF::ESD_RMODE_64, .NameSpace: GOFF::ESD_NS_Parts,
591 .TextStyle: GOFF::ESD_TS_ByteOriented, .BindAlgorithm: GOFF::ESD_BA_Merge,
592 .LoadBehavior: GOFF::ESD_LB_Deferred, .ReservedQwords: GOFF::ESD_RQ_1, .FillByteValue: 0},
593 Parent: RootSDSection);
594 ADAEDSection->setAlignment(Align(16)); // Quadword
595 ADASection = Ctx->getGOFFSection(Kind: SectionKind::getData(), Name: "#S",
596 PRAttributes: GOFF::PRAttr{.IsRenamable: false, .Executable: GOFF::ESD_EXE_DATA,
597 .Linkage: GOFF::ESD_LT_XPLink,
598 .BindingScope: GOFF::ESD_BSC_Section, .SortKey: 0},
599 Parent: ADAEDSection);
600
601 TextSection = Ctx->getGOFFSection(
602 Kind: SectionKind::getText(), Name: GOFF::CLASS_CODE,
603 EDAttributes: GOFF::EDAttr{.IsReadOnly: true, .Rmode: GOFF::ESD_RMODE_64, .NameSpace: GOFF::ESD_NS_NormalName,
604 .TextStyle: GOFF::ESD_TS_ByteOriented, .BindAlgorithm: GOFF::ESD_BA_Concatenate,
605 .LoadBehavior: GOFF::ESD_LB_Initial, .ReservedQwords: GOFF::ESD_RQ_0, .FillByteValue: 0},
606 Parent: RootSDSection);
607 TextSection->setAlignment(Align(8)); // Doubleword
608
609 MCSectionGOFF *PPA2ListEDSection = Ctx->getGOFFSection(
610 Kind: SectionKind::getMetadata(), Name: GOFF::CLASS_PPA2,
611 EDAttributes: GOFF::EDAttr{.IsReadOnly: true, .Rmode: GOFF::ESD_RMODE_64, .NameSpace: GOFF::ESD_NS_Parts,
612 .TextStyle: GOFF::ESD_TS_ByteOriented, .BindAlgorithm: GOFF::ESD_BA_Merge,
613 .LoadBehavior: GOFF::ESD_LB_Initial, .ReservedQwords: GOFF::ESD_RQ_0, .FillByteValue: 0},
614 Parent: RootSDSection);
615 PPA2ListEDSection->setAlignment(Align(8)); // Doubleword
616 PPA2ListSection = Ctx->getGOFFSection(Kind: SectionKind::getData(), Name: ".&ppa2",
617 PRAttributes: GOFF::PRAttr{.IsRenamable: true, .Executable: GOFF::ESD_EXE_DATA,
618 .Linkage: GOFF::ESD_LT_OS,
619 .BindingScope: GOFF::ESD_BSC_Section, .SortKey: 0},
620 Parent: PPA2ListEDSection);
621
622 IDRLSection = Ctx->getGOFFSection(
623 Kind: SectionKind::getData(), Name: "B_IDRL",
624 EDAttributes: GOFF::EDAttr{.IsReadOnly: true, .Rmode: GOFF::ESD_RMODE_64, .NameSpace: GOFF::ESD_NS_NormalName,
625 .TextStyle: GOFF::ESD_TS_Structured, .BindAlgorithm: GOFF::ESD_BA_Concatenate,
626 .LoadBehavior: GOFF::ESD_LB_NoLoad, .ReservedQwords: GOFF::ESD_RQ_0, .FillByteValue: 0},
627 Parent: RootSDSection);
628 IDRLSection->setAlignment(Align(8)); // Doubleword
629
630 // Debug Info Sections. The ED name is the same used by the XL compiler.
631 auto InitDebugSection = [this,
632 RootSDSection](StringRef EDName,
633 StringRef LDName) -> MCSectionGOFF * {
634 MCSectionGOFF *ED = Ctx->getGOFFSection(
635 Kind: SectionKind::getMetadata(), Name: EDName,
636 EDAttributes: GOFF::EDAttr{.IsReadOnly: false, .Rmode: GOFF::ESD_RMODE_64, .NameSpace: GOFF::ESD_NS_Parts,
637 .TextStyle: GOFF::ESD_TS_ByteOriented, .BindAlgorithm: GOFF::ESD_BA_Concatenate,
638 .LoadBehavior: GOFF::ESD_LB_NoLoad, .ReservedQwords: GOFF::ESD_RQ_0, .FillByteValue: 0},
639 Parent: RootSDSection);
640 ED->setAlignment(Align(8)); // Doubleword
641 // At least for llc, this function is called twice! (See function
642 // compileModule() in llc.cpp). Since the context is not cleared, the
643 // already allocated section is returned above. We only add the begin symbol
644 // if it is not yet set to avoid an assertion.
645 MCSymbolGOFF *LD = static_cast<MCSymbolGOFF *>(ED->getBeginSymbol());
646 if (!LD) {
647 LD = static_cast<MCSymbolGOFF *>(getContext().getOrCreateSymbol(Name: LDName));
648 LD->setCodeData(GOFF::ESD_EXE_DATA);
649 LD->setWeak(false);
650 LD->setLinkage(GOFF::ESD_LT_XPLink);
651 LD->setExternal(false);
652 ED->setBeginSymbol(LD);
653 } else
654 assert(LD->getName() == LDName && "Wrong label name");
655 return ED;
656 };
657 DwarfAbbrevSection = InitDebugSection("D_ABREV", ".debug_abbrev");
658 DwarfInfoSection = InitDebugSection("D_INFO", ".debug_info");
659 DwarfLineSection = InitDebugSection("D_LINE", ".debug_line");
660 DwarfFrameSection = InitDebugSection("D_FRAME", ".debug_frame");
661 DwarfPubNamesSection = InitDebugSection("D_PBNMS", ".debug_pubnames");
662 DwarfPubTypesSection = InitDebugSection("D_PTYPES", ".debug_pubtypes");
663 DwarfStrSection = InitDebugSection("D_STR", ".debug_str");
664 DwarfLocSection = InitDebugSection("D_LOC", ".debug_loc");
665 DwarfARangesSection = InitDebugSection("D_ARNGE", ".debug_aranges");
666 DwarfRangesSection = InitDebugSection("D_RNGES", ".debug_ranges");
667 DwarfMacinfoSection = InitDebugSection("D_MACIN", ".debug_macinfo");
668
669 // DWARF 5 sections.
670 DwarfDebugNamesSection = InitDebugSection("D_NAMES", ".debug_names");
671 DwarfStrOffSection = InitDebugSection("D_STROFFS", ".debug_str_offsets");
672 DwarfAddrSection = InitDebugSection("D_ADDR", ".debug_addr");
673 DwarfRnglistsSection = InitDebugSection("D_RNGLISTS", ".debug_rnglists");
674 DwarfLoclistsSection = InitDebugSection("D_LOCLISTS", ".debug_loclists");
675 DwarfLineStrSection = InitDebugSection("D_LINESTR", ".debug_line_str");
676
677 // Special GNU sections.
678 DwarfGnuPubNamesSection = InitDebugSection("D_GPBNMS", ".debug_gnu_pubnames");
679 DwarfGnuPubTypesSection =
680 InitDebugSection("D_GPTYPES", ".debug_gnu_pubtypes");
681
682 // Accelerator Tables.
683 DwarfAccelNamesSection = InitDebugSection("D_APPLNMS", ".apple_names");
684 DwarfAccelNamespaceSection =
685 InitDebugSection("D_APPLNMSP", ".apple_namespaces");
686 DwarfAccelTypesSection = InitDebugSection("D_APPLTYPS", ".apple_types");
687 DwarfAccelObjCSection = InitDebugSection("D_APPLOBJC", ".apple_objc");
688}
689
690void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
691 EHFrameSection =
692 Ctx->getCOFFSection(Section: ".eh_frame", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
693 COFF::IMAGE_SCN_MEM_READ);
694
695 // Set the `IMAGE_SCN_MEM_16BIT` flag when compiling for thumb mode. This is
696 // used to indicate to the linker that the text segment contains thumb instructions
697 // and to set the ISA selection bit for calls accordingly.
698 const bool IsThumb = T.getArch() == Triple::thumb;
699
700 // COFF
701 BSSSection = Ctx->getCOFFSection(
702 Section: ".bss", Characteristics: COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
703 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE);
704 TextSection = Ctx->getCOFFSection(
705 Section: ".text",
706 Characteristics: (IsThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) |
707 COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
708 COFF::IMAGE_SCN_MEM_READ);
709 DataSection = Ctx->getCOFFSection(
710 Section: ".data", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
711 COFF::IMAGE_SCN_MEM_WRITE);
712 ReadOnlySection =
713 Ctx->getCOFFSection(Section: ".rdata", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
714 COFF::IMAGE_SCN_MEM_READ);
715
716 if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::aarch64 ||
717 T.getArch() == Triple::arm || T.getArch() == Triple::thumb) {
718 // On Windows with SEH, the LSDA is emitted into the .xdata section
719 LSDASection = nullptr;
720 } else {
721 LSDASection = Ctx->getCOFFSection(Section: ".gcc_except_table",
722 Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
723 COFF::IMAGE_SCN_MEM_READ);
724 }
725
726 if (T.getArch() == Triple::aarch64) {
727 ImportCallSection =
728 Ctx->getCOFFSection(Section: ".impcall", Characteristics: COFF::IMAGE_SCN_LNK_INFO);
729 } else if (T.getArch() == Triple::x86_64) {
730 // Import Call Optimization on x64 leverages the same metadata as the
731 // retpoline mitigation, hence the unusual section name.
732 ImportCallSection =
733 Ctx->getCOFFSection(Section: ".retplne", Characteristics: COFF::IMAGE_SCN_LNK_INFO);
734 }
735
736 // Debug info.
737 COFFDebugSymbolsSection =
738 Ctx->getCOFFSection(Section: ".debug$S", Characteristics: (COFF::IMAGE_SCN_MEM_DISCARDABLE |
739 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
740 COFF::IMAGE_SCN_MEM_READ));
741 COFFDebugTypesSection =
742 Ctx->getCOFFSection(Section: ".debug$T", Characteristics: (COFF::IMAGE_SCN_MEM_DISCARDABLE |
743 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
744 COFF::IMAGE_SCN_MEM_READ));
745 COFFGlobalTypeHashesSection =
746 Ctx->getCOFFSection(Section: ".debug$H", Characteristics: (COFF::IMAGE_SCN_MEM_DISCARDABLE |
747 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
748 COFF::IMAGE_SCN_MEM_READ));
749
750 DwarfAbbrevSection = Ctx->getCOFFSection(
751 Section: ".debug_abbrev", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
752 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
753 COFF::IMAGE_SCN_MEM_READ);
754 DwarfInfoSection = Ctx->getCOFFSection(
755 Section: ".debug_info", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
756 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
757 COFF::IMAGE_SCN_MEM_READ);
758 DwarfLineSection = Ctx->getCOFFSection(
759 Section: ".debug_line", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
760 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
761 COFF::IMAGE_SCN_MEM_READ);
762 DwarfLineStrSection = Ctx->getCOFFSection(
763 Section: ".debug_line_str", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
764 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
765 COFF::IMAGE_SCN_MEM_READ);
766 DwarfFrameSection = Ctx->getCOFFSection(
767 Section: ".debug_frame", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
768 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
769 COFF::IMAGE_SCN_MEM_READ);
770 DwarfPubNamesSection = Ctx->getCOFFSection(
771 Section: ".debug_pubnames", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
772 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
773 COFF::IMAGE_SCN_MEM_READ);
774 DwarfPubTypesSection = Ctx->getCOFFSection(
775 Section: ".debug_pubtypes", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
776 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
777 COFF::IMAGE_SCN_MEM_READ);
778 DwarfGnuPubNamesSection = Ctx->getCOFFSection(
779 Section: ".debug_gnu_pubnames", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
780 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
781 COFF::IMAGE_SCN_MEM_READ);
782 DwarfGnuPubTypesSection = Ctx->getCOFFSection(
783 Section: ".debug_gnu_pubtypes", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
784 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
785 COFF::IMAGE_SCN_MEM_READ);
786 DwarfStrSection = Ctx->getCOFFSection(
787 Section: ".debug_str", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
788 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
789 COFF::IMAGE_SCN_MEM_READ);
790 DwarfStrOffSection = Ctx->getCOFFSection(
791 Section: ".debug_str_offsets", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
792 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
793 COFF::IMAGE_SCN_MEM_READ);
794 DwarfLocSection = Ctx->getCOFFSection(
795 Section: ".debug_loc", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
796 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
797 COFF::IMAGE_SCN_MEM_READ);
798 DwarfLoclistsSection = Ctx->getCOFFSection(
799 Section: ".debug_loclists", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
800 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
801 COFF::IMAGE_SCN_MEM_READ);
802 DwarfARangesSection = Ctx->getCOFFSection(
803 Section: ".debug_aranges", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
804 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
805 COFF::IMAGE_SCN_MEM_READ);
806 DwarfRangesSection = Ctx->getCOFFSection(
807 Section: ".debug_ranges", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
808 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
809 COFF::IMAGE_SCN_MEM_READ);
810 DwarfRnglistsSection = Ctx->getCOFFSection(
811 Section: ".debug_rnglists", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
812 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
813 COFF::IMAGE_SCN_MEM_READ);
814 DwarfMacinfoSection = Ctx->getCOFFSection(
815 Section: ".debug_macinfo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
816 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
817 COFF::IMAGE_SCN_MEM_READ);
818 DwarfMacroSection = Ctx->getCOFFSection(
819 Section: ".debug_macro", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
820 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
821 COFF::IMAGE_SCN_MEM_READ);
822 DwarfMacinfoDWOSection = Ctx->getCOFFSection(
823 Section: ".debug_macinfo.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
824 COFF::IMAGE_SCN_LNK_REMOVE |
825 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
826 COFF::IMAGE_SCN_MEM_READ);
827 DwarfMacroDWOSection = Ctx->getCOFFSection(
828 Section: ".debug_macro.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
829 COFF::IMAGE_SCN_LNK_REMOVE |
830 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
831 COFF::IMAGE_SCN_MEM_READ);
832 DwarfInfoDWOSection = Ctx->getCOFFSection(
833 Section: ".debug_info.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
834 COFF::IMAGE_SCN_LNK_REMOVE |
835 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
836 COFF::IMAGE_SCN_MEM_READ);
837 DwarfTypesDWOSection = Ctx->getCOFFSection(
838 Section: ".debug_types.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
839 COFF::IMAGE_SCN_LNK_REMOVE |
840 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
841 COFF::IMAGE_SCN_MEM_READ);
842 DwarfAbbrevDWOSection = Ctx->getCOFFSection(
843 Section: ".debug_abbrev.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
844 COFF::IMAGE_SCN_LNK_REMOVE |
845 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
846 COFF::IMAGE_SCN_MEM_READ);
847 DwarfStrDWOSection = Ctx->getCOFFSection(
848 Section: ".debug_str.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
849 COFF::IMAGE_SCN_LNK_REMOVE |
850 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
851 COFF::IMAGE_SCN_MEM_READ);
852 DwarfLineDWOSection = Ctx->getCOFFSection(
853 Section: ".debug_line.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
854 COFF::IMAGE_SCN_LNK_REMOVE |
855 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
856 COFF::IMAGE_SCN_MEM_READ);
857 DwarfLocDWOSection = Ctx->getCOFFSection(
858 Section: ".debug_loc.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
859 COFF::IMAGE_SCN_LNK_REMOVE |
860 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
861 COFF::IMAGE_SCN_MEM_READ);
862 DwarfLoclistsDWOSection = Ctx->getCOFFSection(
863 Section: ".debug_loclists.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
864 COFF::IMAGE_SCN_LNK_REMOVE |
865 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
866 COFF::IMAGE_SCN_MEM_READ);
867 DwarfStrOffDWOSection = Ctx->getCOFFSection(
868 Section: ".debug_str_offsets.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
869 COFF::IMAGE_SCN_LNK_REMOVE |
870 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
871 COFF::IMAGE_SCN_MEM_READ);
872 DwarfRnglistsDWOSection = Ctx->getCOFFSection(
873 Section: ".debug_rnglists.dwo", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
874 COFF::IMAGE_SCN_LNK_REMOVE |
875 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
876 COFF::IMAGE_SCN_MEM_READ);
877 DwarfAddrSection = Ctx->getCOFFSection(
878 Section: ".debug_addr", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
879 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
880 COFF::IMAGE_SCN_MEM_READ);
881 DwarfCUIndexSection = Ctx->getCOFFSection(
882 Section: ".debug_cu_index", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
883 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
884 COFF::IMAGE_SCN_MEM_READ);
885 DwarfTUIndexSection = Ctx->getCOFFSection(
886 Section: ".debug_tu_index", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
887 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
888 COFF::IMAGE_SCN_MEM_READ);
889 DwarfDebugNamesSection = Ctx->getCOFFSection(
890 Section: ".debug_names", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
891 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
892 COFF::IMAGE_SCN_MEM_READ);
893 DwarfAccelNamesSection = Ctx->getCOFFSection(
894 Section: ".apple_names", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
895 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
896 COFF::IMAGE_SCN_MEM_READ);
897 DwarfAccelNamespaceSection = Ctx->getCOFFSection(
898 Section: ".apple_namespaces", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
899 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
900 COFF::IMAGE_SCN_MEM_READ);
901 DwarfAccelTypesSection = Ctx->getCOFFSection(
902 Section: ".apple_types", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
903 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
904 COFF::IMAGE_SCN_MEM_READ);
905 DwarfAccelObjCSection = Ctx->getCOFFSection(
906 Section: ".apple_objc", Characteristics: COFF::IMAGE_SCN_MEM_DISCARDABLE |
907 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
908 COFF::IMAGE_SCN_MEM_READ);
909
910 DrectveSection = Ctx->getCOFFSection(
911 Section: ".drectve", Characteristics: COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE);
912
913 PDataSection =
914 Ctx->getCOFFSection(Section: ".pdata", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
915 COFF::IMAGE_SCN_MEM_READ);
916
917 XDataSection =
918 Ctx->getCOFFSection(Section: ".xdata", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
919 COFF::IMAGE_SCN_MEM_READ);
920
921 SXDataSection = Ctx->getCOFFSection(Section: ".sxdata", Characteristics: COFF::IMAGE_SCN_LNK_INFO);
922
923 GEHContSection =
924 Ctx->getCOFFSection(Section: ".gehcont$y", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
925 COFF::IMAGE_SCN_MEM_READ);
926
927 GFIDsSection =
928 Ctx->getCOFFSection(Section: ".gfids$y", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
929 COFF::IMAGE_SCN_MEM_READ);
930
931 GIATsSection =
932 Ctx->getCOFFSection(Section: ".giats$y", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
933 COFF::IMAGE_SCN_MEM_READ);
934
935 GLJMPSection =
936 Ctx->getCOFFSection(Section: ".gljmp$y", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
937 COFF::IMAGE_SCN_MEM_READ);
938
939 TLSDataSection = Ctx->getCOFFSection(
940 Section: ".tls$", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
941 COFF::IMAGE_SCN_MEM_WRITE);
942
943 StackMapSection = Ctx->getCOFFSection(Section: ".llvm_stackmaps",
944 Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
945 COFF::IMAGE_SCN_MEM_READ);
946
947 // Set IMAGE_SCN_MEM_DISCARDABLE so that lld will not truncate section name.
948 PseudoProbeSection = Ctx->getCOFFSection(
949 Section: ".pseudo_probe", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
950 COFF::IMAGE_SCN_MEM_DISCARDABLE |
951 COFF::IMAGE_SCN_MEM_READ);
952 PseudoProbeDescSection = Ctx->getCOFFSection(
953 Section: ".pseudo_probe_desc", Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
954 COFF::IMAGE_SCN_MEM_DISCARDABLE |
955 COFF::IMAGE_SCN_MEM_READ);
956}
957
958void MCObjectFileInfo::initSPIRVMCObjectFileInfo(const Triple &T) {
959 // Put everything in a single binary section.
960 TextSection = Ctx->getSPIRVSection();
961}
962
963void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
964 TextSection = Ctx->getWasmSection(Section: ".text", K: SectionKind::getText());
965 DataSection = Ctx->getWasmSection(Section: ".data", K: SectionKind::getData());
966
967 DwarfLineSection =
968 Ctx->getWasmSection(Section: ".debug_line", K: SectionKind::getMetadata());
969 DwarfLineStrSection =
970 Ctx->getWasmSection(Section: ".debug_line_str", K: SectionKind::getMetadata(),
971 Flags: wasm::WASM_SEG_FLAG_STRINGS);
972 DwarfStrSection = Ctx->getWasmSection(
973 Section: ".debug_str", K: SectionKind::getMetadata(), Flags: wasm::WASM_SEG_FLAG_STRINGS);
974 DwarfLocSection =
975 Ctx->getWasmSection(Section: ".debug_loc", K: SectionKind::getMetadata());
976 DwarfAbbrevSection =
977 Ctx->getWasmSection(Section: ".debug_abbrev", K: SectionKind::getMetadata());
978 DwarfARangesSection = Ctx->getWasmSection(Section: ".debug_aranges", K: SectionKind::getMetadata());
979 DwarfRangesSection =
980 Ctx->getWasmSection(Section: ".debug_ranges", K: SectionKind::getMetadata());
981 DwarfMacinfoSection =
982 Ctx->getWasmSection(Section: ".debug_macinfo", K: SectionKind::getMetadata());
983 DwarfMacroSection =
984 Ctx->getWasmSection(Section: ".debug_macro", K: SectionKind::getMetadata());
985 DwarfCUIndexSection = Ctx->getWasmSection(Section: ".debug_cu_index", K: SectionKind::getMetadata());
986 DwarfTUIndexSection = Ctx->getWasmSection(Section: ".debug_tu_index", K: SectionKind::getMetadata());
987 DwarfInfoSection =
988 Ctx->getWasmSection(Section: ".debug_info", K: SectionKind::getMetadata());
989 DwarfFrameSection = Ctx->getWasmSection(Section: ".debug_frame", K: SectionKind::getMetadata());
990 DwarfPubNamesSection = Ctx->getWasmSection(Section: ".debug_pubnames", K: SectionKind::getMetadata());
991 DwarfPubTypesSection = Ctx->getWasmSection(Section: ".debug_pubtypes", K: SectionKind::getMetadata());
992 DwarfGnuPubNamesSection =
993 Ctx->getWasmSection(Section: ".debug_gnu_pubnames", K: SectionKind::getMetadata());
994 DwarfGnuPubTypesSection =
995 Ctx->getWasmSection(Section: ".debug_gnu_pubtypes", K: SectionKind::getMetadata());
996
997 DwarfDebugNamesSection =
998 Ctx->getWasmSection(Section: ".debug_names", K: SectionKind::getMetadata());
999 DwarfStrOffSection =
1000 Ctx->getWasmSection(Section: ".debug_str_offsets", K: SectionKind::getMetadata());
1001 DwarfAddrSection =
1002 Ctx->getWasmSection(Section: ".debug_addr", K: SectionKind::getMetadata());
1003 DwarfRnglistsSection =
1004 Ctx->getWasmSection(Section: ".debug_rnglists", K: SectionKind::getMetadata());
1005 DwarfLoclistsSection =
1006 Ctx->getWasmSection(Section: ".debug_loclists", K: SectionKind::getMetadata());
1007
1008 // Fission Sections
1009 DwarfInfoDWOSection =
1010 Ctx->getWasmSection(Section: ".debug_info.dwo", K: SectionKind::getMetadata());
1011 DwarfTypesDWOSection =
1012 Ctx->getWasmSection(Section: ".debug_types.dwo", K: SectionKind::getMetadata());
1013 DwarfAbbrevDWOSection =
1014 Ctx->getWasmSection(Section: ".debug_abbrev.dwo", K: SectionKind::getMetadata());
1015 DwarfStrDWOSection =
1016 Ctx->getWasmSection(Section: ".debug_str.dwo", K: SectionKind::getMetadata(),
1017 Flags: wasm::WASM_SEG_FLAG_STRINGS);
1018 DwarfLineDWOSection =
1019 Ctx->getWasmSection(Section: ".debug_line.dwo", K: SectionKind::getMetadata());
1020 DwarfLocDWOSection =
1021 Ctx->getWasmSection(Section: ".debug_loc.dwo", K: SectionKind::getMetadata());
1022 DwarfStrOffDWOSection =
1023 Ctx->getWasmSection(Section: ".debug_str_offsets.dwo", K: SectionKind::getMetadata());
1024 DwarfRnglistsDWOSection =
1025 Ctx->getWasmSection(Section: ".debug_rnglists.dwo", K: SectionKind::getMetadata());
1026 DwarfMacinfoDWOSection =
1027 Ctx->getWasmSection(Section: ".debug_macinfo.dwo", K: SectionKind::getMetadata());
1028 DwarfMacroDWOSection =
1029 Ctx->getWasmSection(Section: ".debug_macro.dwo", K: SectionKind::getMetadata());
1030
1031 DwarfLoclistsDWOSection =
1032 Ctx->getWasmSection(Section: ".debug_loclists.dwo", K: SectionKind::getMetadata());
1033
1034 // DWP Sections
1035 DwarfCUIndexSection =
1036 Ctx->getWasmSection(Section: ".debug_cu_index", K: SectionKind::getMetadata());
1037 DwarfTUIndexSection =
1038 Ctx->getWasmSection(Section: ".debug_tu_index", K: SectionKind::getMetadata());
1039
1040 // Wasm use data section for LSDA.
1041 // TODO Consider putting each function's exception table in a separate
1042 // section, as in -function-sections, to facilitate lld's --gc-section.
1043 LSDASection = Ctx->getWasmSection(Section: ".rodata.gcc_except_table",
1044 K: SectionKind::getReadOnlyWithRel());
1045
1046 // TODO: Define more sections.
1047}
1048
1049void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) {
1050 // The default csect for program code. Functions without a specified section
1051 // get placed into this csect. The choice of csect name is not a property of
1052 // the ABI or object file format, but various tools rely on the section
1053 // name being empty (considering named symbols to be "user symbol names").
1054 TextSection = Ctx->getXCOFFSection(
1055 Section: "..text..", // Use a non-null name to work around an AIX assembler bug...
1056 K: SectionKind::getText(),
1057 CsectProp: XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_PR, XCOFF::XTY_SD),
1058 /* MultiSymbolsAllowed*/ true);
1059
1060 // ... but use a null name when generating the symbol table.
1061 MCSectionXCOFF *TS = static_cast<MCSectionXCOFF *>(TextSection);
1062 TS->getQualNameSymbol()->setSymbolTableName("");
1063 TS->setSymbolTableName("");
1064
1065 DataSection = Ctx->getXCOFFSection(
1066 Section: ".data", K: SectionKind::getData(),
1067 CsectProp: XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW, XCOFF::XTY_SD),
1068 /* MultiSymbolsAllowed*/ true);
1069
1070 ReadOnlySection = Ctx->getXCOFFSection(
1071 Section: ".rodata", K: SectionKind::getReadOnly(),
1072 CsectProp: XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
1073 /* MultiSymbolsAllowed*/ true);
1074 ReadOnlySection->setAlignment(Align(4));
1075
1076 ReadOnly8Section = Ctx->getXCOFFSection(
1077 Section: ".rodata.8", K: SectionKind::getReadOnly(),
1078 CsectProp: XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
1079 /* MultiSymbolsAllowed*/ true);
1080 ReadOnly8Section->setAlignment(Align(8));
1081
1082 ReadOnly16Section = Ctx->getXCOFFSection(
1083 Section: ".rodata.16", K: SectionKind::getReadOnly(),
1084 CsectProp: XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
1085 /* MultiSymbolsAllowed*/ true);
1086 ReadOnly16Section->setAlignment(Align(16));
1087
1088 TLSDataSection = Ctx->getXCOFFSection(
1089 Section: ".tdata", K: SectionKind::getThreadData(),
1090 CsectProp: XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_TL, XCOFF::XTY_SD),
1091 /* MultiSymbolsAllowed*/ true);
1092
1093 TOCBaseSection = Ctx->getXCOFFSection(
1094 Section: "TOC", K: SectionKind::getData(),
1095 CsectProp: XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_TC0,
1096 XCOFF::XTY_SD));
1097
1098 // The TOC-base always has 0 size, but 4 byte alignment.
1099 TOCBaseSection->setAlignment(Align(4));
1100
1101 LSDASection = Ctx->getXCOFFSection(
1102 Section: ".gcc_except_table", K: SectionKind::getReadOnly(),
1103 CsectProp: XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO,
1104 XCOFF::XTY_SD));
1105
1106 CompactUnwindSection = Ctx->getXCOFFSection(
1107 Section: ".eh_info_table", K: SectionKind::getData(),
1108 CsectProp: XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW,
1109 XCOFF::XTY_SD));
1110
1111 // DWARF sections for XCOFF are not csects. They are special STYP_DWARF
1112 // sections, and the individual DWARF sections are distinguished by their
1113 // section subtype.
1114 DwarfAbbrevSection = Ctx->getXCOFFSection(
1115 Section: ".dwabrev", K: SectionKind::getMetadata(),
1116 /* CsectProperties */ CsectProp: std::nullopt,
1117 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWABREV);
1118
1119 DwarfInfoSection = Ctx->getXCOFFSection(
1120 Section: ".dwinfo", K: SectionKind::getMetadata(), /* CsectProperties */ CsectProp: std::nullopt,
1121 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWINFO);
1122
1123 DwarfLineSection = Ctx->getXCOFFSection(
1124 Section: ".dwline", K: SectionKind::getMetadata(), /* CsectProperties */ CsectProp: std::nullopt,
1125 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWLINE);
1126
1127 DwarfFrameSection = Ctx->getXCOFFSection(
1128 Section: ".dwframe", K: SectionKind::getMetadata(),
1129 /* CsectProperties */ CsectProp: std::nullopt,
1130 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWFRAME);
1131
1132 DwarfPubNamesSection = Ctx->getXCOFFSection(
1133 Section: ".dwpbnms", K: SectionKind::getMetadata(),
1134 /* CsectProperties */ CsectProp: std::nullopt,
1135 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWPBNMS);
1136
1137 DwarfPubTypesSection = Ctx->getXCOFFSection(
1138 Section: ".dwpbtyp", K: SectionKind::getMetadata(),
1139 /* CsectProperties */ CsectProp: std::nullopt,
1140 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWPBTYP);
1141
1142 DwarfStrSection = Ctx->getXCOFFSection(
1143 Section: ".dwstr", K: SectionKind::getMetadata(), /* CsectProperties */ CsectProp: std::nullopt,
1144 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWSTR);
1145
1146 DwarfLocSection = Ctx->getXCOFFSection(
1147 Section: ".dwloc", K: SectionKind::getMetadata(), /* CsectProperties */ CsectProp: std::nullopt,
1148 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWLOC);
1149
1150 DwarfARangesSection = Ctx->getXCOFFSection(
1151 Section: ".dwarnge", K: SectionKind::getMetadata(),
1152 /* CsectProperties */ CsectProp: std::nullopt,
1153 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWARNGE);
1154
1155 DwarfRangesSection = Ctx->getXCOFFSection(
1156 Section: ".dwrnges", K: SectionKind::getMetadata(),
1157 /* CsectProperties */ CsectProp: std::nullopt,
1158 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWRNGES);
1159
1160 DwarfMacinfoSection = Ctx->getXCOFFSection(
1161 Section: ".dwmac", K: SectionKind::getMetadata(), /* CsectProperties */ CsectProp: std::nullopt,
1162 /* MultiSymbolsAllowed */ true, DwarfSubtypeFlags: XCOFF::SSUBTYP_DWMAC);
1163}
1164
1165void MCObjectFileInfo::initDXContainerObjectFileInfo(const Triple &T) {
1166 // At the moment the DXBC section should end up empty.
1167 TextSection = Ctx->getDXContainerSection(Section: "DXBC", K: SectionKind::getText());
1168}
1169
1170MCObjectFileInfo::~MCObjectFileInfo() = default;
1171
1172void MCObjectFileInfo::initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
1173 bool LargeCodeModel) {
1174 PositionIndependent = PIC;
1175 Ctx = &MCCtx;
1176
1177 // Common.
1178 SupportsCompactUnwindWithoutEHFrame = false;
1179 OmitDwarfIfHaveCompactUnwind = false;
1180
1181 FDECFIEncoding = dwarf::DW_EH_PE_absptr;
1182
1183 CompactUnwindDwarfEHFrameOnly = 0;
1184
1185 EHFrameSection = nullptr; // Created on demand.
1186 SFrameSection = nullptr; // Created on demand.
1187 CompactUnwindSection = nullptr; // Used only by selected targets.
1188 DwarfAccelNamesSection = nullptr; // Used only by selected targets.
1189 DwarfAccelObjCSection = nullptr; // Used only by selected targets.
1190 DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
1191 DwarfAccelTypesSection = nullptr; // Used only by selected targets.
1192
1193 const Triple &TheTriple = Ctx->getTargetTriple();
1194 switch (Ctx->getObjectFileType()) {
1195 case MCContext::IsMachO:
1196 initMachOMCObjectFileInfo(T: TheTriple);
1197 break;
1198 case MCContext::IsCOFF:
1199 initCOFFMCObjectFileInfo(T: TheTriple);
1200 break;
1201 case MCContext::IsELF:
1202 initELFMCObjectFileInfo(T: TheTriple, Large: LargeCodeModel);
1203 break;
1204 case MCContext::IsGOFF:
1205 initGOFFMCObjectFileInfo(T: TheTriple);
1206 break;
1207 case MCContext::IsSPIRV:
1208 initSPIRVMCObjectFileInfo(T: TheTriple);
1209 break;
1210 case MCContext::IsWasm:
1211 initWasmMCObjectFileInfo(T: TheTriple);
1212 break;
1213 case MCContext::IsXCOFF:
1214 initXCOFFMCObjectFileInfo(T: TheTriple);
1215 break;
1216 case MCContext::IsDXContainer:
1217 initDXContainerObjectFileInfo(T: TheTriple);
1218 break;
1219 }
1220}
1221
1222MCSection *MCObjectFileInfo::getDwarfComdatSection(const char *Name,
1223 uint64_t Hash) const {
1224 switch (Ctx->getTargetTriple().getObjectFormat()) {
1225 case Triple::ELF:
1226 return Ctx->getELFSection(Section: Name, Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_GROUP, EntrySize: 0,
1227 Group: utostr(X: Hash), /*IsComdat=*/true);
1228 case Triple::Wasm:
1229 return Ctx->getWasmSection(Section: Name, K: SectionKind::getMetadata(), Flags: 0,
1230 Group: utostr(X: Hash), UniqueID: MCSection::NonUniqueID);
1231 case Triple::MachO:
1232 case Triple::COFF:
1233 case Triple::GOFF:
1234 case Triple::SPIRV:
1235 case Triple::XCOFF:
1236 case Triple::DXContainer:
1237 case Triple::UnknownObjectFormat:
1238 report_fatal_error(reason: "Cannot get DWARF comdat section for this object file "
1239 "format: not implemented.");
1240 break;
1241 }
1242 llvm_unreachable("Unknown ObjectFormatType");
1243}
1244
1245MCSection *
1246MCObjectFileInfo::getCallGraphSection(const MCSection &TextSec) const {
1247 if (Ctx->getObjectFileType() != MCContext::IsELF)
1248 return CallGraphSection;
1249
1250 const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1251 unsigned Flags = ELF::SHF_LINK_ORDER;
1252 StringRef GroupName;
1253 if (const MCSymbol *Group = ElfSec.getGroup()) {
1254 GroupName = Group->getName();
1255 Flags |= ELF::SHF_GROUP;
1256 }
1257
1258 return Ctx->getELFSection(
1259 Section: ".llvm.callgraph", Type: ELF::SHT_LLVM_CALL_GRAPH, Flags, EntrySize: 0, Group: GroupName,
1260 /*IsComdat=*/true, UniqueID: ElfSec.getUniqueID(),
1261 LinkedToSym: static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
1262}
1263
1264MCSection *
1265MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
1266 if ((Ctx->getObjectFileType() != MCContext::IsELF) ||
1267 Ctx->getTargetTriple().isPS4())
1268 return StackSizesSection;
1269
1270 const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1271 unsigned Flags = ELF::SHF_LINK_ORDER;
1272 StringRef GroupName;
1273 if (const MCSymbol *Group = ElfSec.getGroup()) {
1274 GroupName = Group->getName();
1275 Flags |= ELF::SHF_GROUP;
1276 }
1277
1278 return Ctx->getELFSection(
1279 Section: ".stack_sizes", Type: ELF::SHT_PROGBITS, Flags, EntrySize: 0, Group: GroupName, IsComdat: true,
1280 UniqueID: ElfSec.getUniqueID(),
1281 LinkedToSym: static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
1282}
1283
1284MCSection *
1285MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
1286 constexpr StringLiteral Name = ".llvm_bb_addr_map";
1287 if (Ctx->getObjectFileType() == MCContext::IsELF) {
1288 const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1289 unsigned Flags = ELF::SHF_LINK_ORDER;
1290 StringRef GroupName;
1291 if (const MCSymbol *Group = ElfSec.getGroup()) {
1292 GroupName = Group->getName();
1293 Flags |= ELF::SHF_GROUP;
1294 }
1295
1296 // Use the text section's begin symbol and unique ID to create a separate
1297 // .llvm_bb_addr_map section associated with every unique text section.
1298 return Ctx->getELFSection(
1299 Section: Name, Type: ELF::SHT_LLVM_BB_ADDR_MAP, Flags, EntrySize: 0, Group: GroupName, IsComdat: true,
1300 UniqueID: ElfSec.getUniqueID(),
1301 LinkedToSym: static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
1302 } else if (Ctx->getObjectFileType() == MCContext::IsCOFF) {
1303 StringRef COMDATSymName;
1304 int Selection = 0;
1305 unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1306 COFF::IMAGE_SCN_MEM_DISCARDABLE |
1307 COFF::IMAGE_SCN_MEM_READ;
1308 const auto &COFFSec = static_cast<const MCSectionCOFF &>(TextSec);
1309 if (const MCSymbol *COMDATSym = COFFSec.getCOMDATSymbol()) {
1310 if (!Ctx->getAsmInfo().hasCOFFAssociativeComdats())
1311 report_fatal_error(reason: "BB address map requires associative COMDAT "
1312 "support for COMDAT functions");
1313 COMDATSymName = COMDATSym->getName();
1314 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1315 Selection = COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
1316 }
1317 return Ctx->getCOFFSection(Section: Name, Characteristics, COMDATSymName, Selection,
1318 UniqueID: COFFSec.getUniqueID());
1319 }
1320
1321 return nullptr;
1322}
1323
1324MCSection *
1325MCObjectFileInfo::getKCFITrapSection(const MCSection &TextSec) const {
1326 if (Ctx->getObjectFileType() != MCContext::IsELF)
1327 return nullptr;
1328
1329 const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1330 unsigned Flags = ELF::SHF_LINK_ORDER | ELF::SHF_ALLOC;
1331 StringRef GroupName;
1332 if (const MCSymbol *Group = ElfSec.getGroup()) {
1333 GroupName = Group->getName();
1334 Flags |= ELF::SHF_GROUP;
1335 }
1336
1337 return Ctx->getELFSection(
1338 Section: ".kcfi_traps", Type: ELF::SHT_PROGBITS, Flags, EntrySize: 0, Group: GroupName,
1339 /*IsComdat=*/true, UniqueID: ElfSec.getUniqueID(),
1340 LinkedToSym: static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
1341}
1342
1343MCSection *
1344MCObjectFileInfo::getPseudoProbeSection(const MCSection &TextSec) const {
1345 auto ObjFileType = Ctx->getObjectFileType();
1346 if (ObjFileType == MCContext::IsELF) {
1347 const auto &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1348 unsigned Flags = ELF::SHF_LINK_ORDER;
1349 StringRef GroupName;
1350 if (const MCSymbol *Group = ElfSec.getGroup()) {
1351 GroupName = Group->getName();
1352 Flags |= ELF::SHF_GROUP;
1353 }
1354 return Ctx->getELFSection(
1355 Section: PseudoProbeSection->getName(), Type: ELF::SHT_PROGBITS, Flags, EntrySize: 0, Group: GroupName,
1356 IsComdat: true, UniqueID: ElfSec.getUniqueID(),
1357 LinkedToSym: static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
1358 } else if (ObjFileType == MCContext::IsCOFF) {
1359 StringRef COMDATSymName = "";
1360 int Selection = 0;
1361 unsigned Characteristics =
1362 static_cast<MCSectionCOFF *>(PseudoProbeSection)->getCharacteristics();
1363 const auto &COFFSec = static_cast<const MCSectionCOFF &>(TextSec);
1364 if (const MCSymbol *COMDATSym = COFFSec.getCOMDATSymbol()) {
1365 // Associate .pseudo_probe to its function section.
1366 COMDATSymName = COMDATSym->getName();
1367 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1368 Selection = COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
1369 }
1370 return Ctx->getCOFFSection(Section: PseudoProbeSection->getName(), Characteristics,
1371 COMDATSymName, Selection, UniqueID: COFFSec.getUniqueID());
1372 }
1373
1374 return PseudoProbeSection;
1375}
1376
1377MCSection *
1378MCObjectFileInfo::getPseudoProbeDescSection(StringRef FuncName,
1379 uint64_t FuncHash) const {
1380 if (!Ctx->getTargetTriple().supportsCOMDAT() || FuncName.empty())
1381 return PseudoProbeDescSection;
1382
1383 // Create a separate comdat group for each function's descriptor in order
1384 // for the linker to deduplicate. The duplication, must be from different
1385 // translation unit, can come from:
1386 // 1. Inline functions defined in header files;
1387 // 2. ThinLTO imported functions;
1388 // 3. Weak-linkage definitions.
1389 // Use a concatenation of the section name, function name, and function hash
1390 // as the group name so that descriptors with different hashes (due to user
1391 // code not following ODR or compiler codegen inconsistencies) get separate
1392 // COMDAT sections instead of being silently dropped (ELF) or causing linker
1393 // errors (COFF). Duplicate GUIDs with mismatching hashes are detected
1394 // during descriptor decoding and reported by llvm-profgen.
1395 auto ObjFileType = Ctx->getObjectFileType();
1396 if (ObjFileType == MCContext::IsELF) {
1397 auto *S = static_cast<MCSectionELF *>(PseudoProbeDescSection);
1398 auto Flags = S->getFlags() | ELF::SHF_GROUP;
1399 return Ctx->getELFSection(
1400 Section: S->getName(), Type: S->getType(), Flags, EntrySize: S->getEntrySize(),
1401 Group: S->getName() + "_" + FuncName + "." + Twine::utohexstr(Val: FuncHash),
1402 /*IsComdat=*/true);
1403 } else if (ObjFileType == MCContext::IsCOFF) {
1404 auto *S = static_cast<MCSectionCOFF *>(PseudoProbeDescSection);
1405 unsigned Characteristics =
1406 S->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
1407 std::string COMDATSymName =
1408 (S->getName() + "_" + FuncName + "." + Twine::utohexstr(Val: FuncHash))
1409 .str();
1410 return Ctx->getCOFFSection(Section: S->getName(), Characteristics, COMDATSymName,
1411 Selection: COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH);
1412 }
1413
1414 return PseudoProbeDescSection;
1415}
1416
1417MCSection *MCObjectFileInfo::getLLVMStatsSection() const {
1418 return LLVMStatsSection;
1419}
1420
1421MCSection *MCObjectFileInfo::getPCSection(StringRef Name,
1422 const MCSection *TextSec) const {
1423 if (Ctx->getObjectFileType() != MCContext::IsELF)
1424 return nullptr;
1425
1426 // SHF_WRITE for relocations, and let user post-process data in-place.
1427 unsigned Flags = ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER;
1428
1429 if (!TextSec)
1430 TextSec = getTextSection();
1431
1432 StringRef GroupName;
1433 const auto &ElfSec = static_cast<const MCSectionELF &>(*TextSec);
1434 if (const MCSymbol *Group = ElfSec.getGroup()) {
1435 GroupName = Group->getName();
1436 Flags |= ELF::SHF_GROUP;
1437 }
1438 return Ctx->getELFSection(
1439 Section: Name, Type: ELF::SHT_PROGBITS, Flags, EntrySize: 0, Group: GroupName, IsComdat: true, UniqueID: ElfSec.getUniqueID(),
1440 LinkedToSym: static_cast<const MCSymbolELF *>(TextSec->getBeginSymbol()));
1441}
1442