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