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/ADT/Triple.h"
12 #include "llvm/BinaryFormat/COFF.h"
13 #include "llvm/BinaryFormat/ELF.h"
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCSection.h"
17 #include "llvm/MC/MCSectionCOFF.h"
18 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/MC/MCSectionMachO.h"
20 #include "llvm/MC/MCSectionWasm.h"
21 #include "llvm/MC/MCSectionXCOFF.h"
22
23 using namespace llvm;
24
useCompactUnwind(const Triple & T)25 static bool useCompactUnwind(const Triple &T) {
26 // Only on darwin.
27 if (!T.isOSDarwin())
28 return false;
29
30 // aarch64 always has it.
31 if (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32)
32 return true;
33
34 // armv7k always has it.
35 if (T.isWatchABI())
36 return true;
37
38 // Use it on newer version of OS X.
39 if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6))
40 return true;
41
42 // And the iOS simulator.
43 if (T.isiOS() && T.isX86())
44 return true;
45
46 return false;
47 }
48
initMachOMCObjectFileInfo(const Triple & T)49 void MCObjectFileInfo::initMachOMCObjectFileInfo(const Triple &T) {
50 // MachO
51 SupportsWeakOmittedEHFrame = false;
52
53 EHFrameSection = Ctx->getMachOSection(
54 "__TEXT", "__eh_frame",
55 MachO::S_COALESCED | MachO::S_ATTR_NO_TOC |
56 MachO::S_ATTR_STRIP_STATIC_SYMS | MachO::S_ATTR_LIVE_SUPPORT,
57 SectionKind::getReadOnly());
58
59 if (T.isOSDarwin() &&
60 (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32))
61 SupportsCompactUnwindWithoutEHFrame = true;
62
63 if (T.isWatchABI())
64 OmitDwarfIfHaveCompactUnwind = true;
65
66 FDECFIEncoding = dwarf::DW_EH_PE_pcrel;
67
68 // .comm doesn't support alignment before Leopard.
69 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
70 CommDirectiveSupportsAlignment = false;
71
72 TextSection // .text
73 = Ctx->getMachOSection("__TEXT", "__text",
74 MachO::S_ATTR_PURE_INSTRUCTIONS,
75 SectionKind::getText());
76 DataSection // .data
77 = Ctx->getMachOSection("__DATA", "__data", 0, SectionKind::getData());
78
79 // BSSSection might not be expected initialized on msvc.
80 BSSSection = nullptr;
81
82 TLSDataSection // .tdata
83 = Ctx->getMachOSection("__DATA", "__thread_data",
84 MachO::S_THREAD_LOCAL_REGULAR,
85 SectionKind::getData());
86 TLSBSSSection // .tbss
87 = Ctx->getMachOSection("__DATA", "__thread_bss",
88 MachO::S_THREAD_LOCAL_ZEROFILL,
89 SectionKind::getThreadBSS());
90
91 // TODO: Verify datarel below.
92 TLSTLVSection // .tlv
93 = Ctx->getMachOSection("__DATA", "__thread_vars",
94 MachO::S_THREAD_LOCAL_VARIABLES,
95 SectionKind::getData());
96
97 TLSThreadInitSection = Ctx->getMachOSection(
98 "__DATA", "__thread_init", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
99 SectionKind::getData());
100
101 CStringSection // .cstring
102 = Ctx->getMachOSection("__TEXT", "__cstring",
103 MachO::S_CSTRING_LITERALS,
104 SectionKind::getMergeable1ByteCString());
105 UStringSection
106 = Ctx->getMachOSection("__TEXT","__ustring", 0,
107 SectionKind::getMergeable2ByteCString());
108 FourByteConstantSection // .literal4
109 = Ctx->getMachOSection("__TEXT", "__literal4",
110 MachO::S_4BYTE_LITERALS,
111 SectionKind::getMergeableConst4());
112 EightByteConstantSection // .literal8
113 = Ctx->getMachOSection("__TEXT", "__literal8",
114 MachO::S_8BYTE_LITERALS,
115 SectionKind::getMergeableConst8());
116
117 SixteenByteConstantSection // .literal16
118 = Ctx->getMachOSection("__TEXT", "__literal16",
119 MachO::S_16BYTE_LITERALS,
120 SectionKind::getMergeableConst16());
121
122 ReadOnlySection // .const
123 = Ctx->getMachOSection("__TEXT", "__const", 0,
124 SectionKind::getReadOnly());
125
126 // If the target is not powerpc, map the coal sections to the non-coal
127 // sections.
128 //
129 // "__TEXT/__textcoal_nt" => section "__TEXT/__text"
130 // "__TEXT/__const_coal" => section "__TEXT/__const"
131 // "__DATA/__datacoal_nt" => section "__DATA/__data"
132 Triple::ArchType ArchTy = T.getArch();
133
134 ConstDataSection // .const_data
135 = Ctx->getMachOSection("__DATA", "__const", 0,
136 SectionKind::getReadOnlyWithRel());
137
138 if (ArchTy == Triple::ppc || ArchTy == Triple::ppc64) {
139 TextCoalSection
140 = Ctx->getMachOSection("__TEXT", "__textcoal_nt",
141 MachO::S_COALESCED |
142 MachO::S_ATTR_PURE_INSTRUCTIONS,
143 SectionKind::getText());
144 ConstTextCoalSection
145 = Ctx->getMachOSection("__TEXT", "__const_coal",
146 MachO::S_COALESCED,
147 SectionKind::getReadOnly());
148 DataCoalSection = Ctx->getMachOSection(
149 "__DATA", "__datacoal_nt", MachO::S_COALESCED, SectionKind::getData());
150 ConstDataCoalSection = DataCoalSection;
151 } else {
152 TextCoalSection = TextSection;
153 ConstTextCoalSection = ReadOnlySection;
154 DataCoalSection = DataSection;
155 ConstDataCoalSection = ConstDataSection;
156 }
157
158 DataCommonSection
159 = Ctx->getMachOSection("__DATA","__common",
160 MachO::S_ZEROFILL,
161 SectionKind::getBSS());
162 DataBSSSection
163 = Ctx->getMachOSection("__DATA","__bss", MachO::S_ZEROFILL,
164 SectionKind::getBSS());
165
166
167 LazySymbolPointerSection
168 = Ctx->getMachOSection("__DATA", "__la_symbol_ptr",
169 MachO::S_LAZY_SYMBOL_POINTERS,
170 SectionKind::getMetadata());
171 NonLazySymbolPointerSection
172 = Ctx->getMachOSection("__DATA", "__nl_symbol_ptr",
173 MachO::S_NON_LAZY_SYMBOL_POINTERS,
174 SectionKind::getMetadata());
175
176 ThreadLocalPointerSection
177 = Ctx->getMachOSection("__DATA", "__thread_ptr",
178 MachO::S_THREAD_LOCAL_VARIABLE_POINTERS,
179 SectionKind::getMetadata());
180
181 // Exception Handling.
182 LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0,
183 SectionKind::getReadOnlyWithRel());
184
185 COFFDebugSymbolsSection = nullptr;
186 COFFDebugTypesSection = nullptr;
187 COFFGlobalTypeHashesSection = nullptr;
188
189 if (useCompactUnwind(T)) {
190 CompactUnwindSection =
191 Ctx->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG,
192 SectionKind::getReadOnly());
193
194 if (T.isX86())
195 CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_X86_64_MODE_DWARF
196 else if (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32)
197 CompactUnwindDwarfEHFrameOnly = 0x03000000; // UNWIND_ARM64_MODE_DWARF
198 else if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb)
199 CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_ARM_MODE_DWARF
200 }
201
202 // Debug Information.
203 DwarfDebugNamesSection =
204 Ctx->getMachOSection("__DWARF", "__debug_names", MachO::S_ATTR_DEBUG,
205 SectionKind::getMetadata(), "debug_names_begin");
206 DwarfAccelNamesSection =
207 Ctx->getMachOSection("__DWARF", "__apple_names", MachO::S_ATTR_DEBUG,
208 SectionKind::getMetadata(), "names_begin");
209 DwarfAccelObjCSection =
210 Ctx->getMachOSection("__DWARF", "__apple_objc", MachO::S_ATTR_DEBUG,
211 SectionKind::getMetadata(), "objc_begin");
212 // 16 character section limit...
213 DwarfAccelNamespaceSection =
214 Ctx->getMachOSection("__DWARF", "__apple_namespac", MachO::S_ATTR_DEBUG,
215 SectionKind::getMetadata(), "namespac_begin");
216 DwarfAccelTypesSection =
217 Ctx->getMachOSection("__DWARF", "__apple_types", MachO::S_ATTR_DEBUG,
218 SectionKind::getMetadata(), "types_begin");
219
220 DwarfSwiftASTSection =
221 Ctx->getMachOSection("__DWARF", "__swift_ast", MachO::S_ATTR_DEBUG,
222 SectionKind::getMetadata());
223
224 DwarfAbbrevSection =
225 Ctx->getMachOSection("__DWARF", "__debug_abbrev", MachO::S_ATTR_DEBUG,
226 SectionKind::getMetadata(), "section_abbrev");
227 DwarfInfoSection =
228 Ctx->getMachOSection("__DWARF", "__debug_info", MachO::S_ATTR_DEBUG,
229 SectionKind::getMetadata(), "section_info");
230 DwarfLineSection =
231 Ctx->getMachOSection("__DWARF", "__debug_line", MachO::S_ATTR_DEBUG,
232 SectionKind::getMetadata(), "section_line");
233 DwarfLineStrSection =
234 Ctx->getMachOSection("__DWARF", "__debug_line_str", MachO::S_ATTR_DEBUG,
235 SectionKind::getMetadata(), "section_line_str");
236 DwarfFrameSection =
237 Ctx->getMachOSection("__DWARF", "__debug_frame", MachO::S_ATTR_DEBUG,
238 SectionKind::getMetadata());
239 DwarfPubNamesSection =
240 Ctx->getMachOSection("__DWARF", "__debug_pubnames", MachO::S_ATTR_DEBUG,
241 SectionKind::getMetadata());
242 DwarfPubTypesSection =
243 Ctx->getMachOSection("__DWARF", "__debug_pubtypes", MachO::S_ATTR_DEBUG,
244 SectionKind::getMetadata());
245 DwarfGnuPubNamesSection =
246 Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn", MachO::S_ATTR_DEBUG,
247 SectionKind::getMetadata());
248 DwarfGnuPubTypesSection =
249 Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt", MachO::S_ATTR_DEBUG,
250 SectionKind::getMetadata());
251 DwarfStrSection =
252 Ctx->getMachOSection("__DWARF", "__debug_str", MachO::S_ATTR_DEBUG,
253 SectionKind::getMetadata(), "info_string");
254 DwarfStrOffSection =
255 Ctx->getMachOSection("__DWARF", "__debug_str_offs", MachO::S_ATTR_DEBUG,
256 SectionKind::getMetadata(), "section_str_off");
257 DwarfAddrSection =
258 Ctx->getMachOSection("__DWARF", "__debug_addr", MachO::S_ATTR_DEBUG,
259 SectionKind::getMetadata(), "section_info");
260 DwarfLocSection =
261 Ctx->getMachOSection("__DWARF", "__debug_loc", MachO::S_ATTR_DEBUG,
262 SectionKind::getMetadata(), "section_debug_loc");
263 DwarfLoclistsSection =
264 Ctx->getMachOSection("__DWARF", "__debug_loclists", MachO::S_ATTR_DEBUG,
265 SectionKind::getMetadata(), "section_debug_loc");
266
267 DwarfARangesSection =
268 Ctx->getMachOSection("__DWARF", "__debug_aranges", MachO::S_ATTR_DEBUG,
269 SectionKind::getMetadata());
270 DwarfRangesSection =
271 Ctx->getMachOSection("__DWARF", "__debug_ranges", MachO::S_ATTR_DEBUG,
272 SectionKind::getMetadata(), "debug_range");
273 DwarfRnglistsSection =
274 Ctx->getMachOSection("__DWARF", "__debug_rnglists", MachO::S_ATTR_DEBUG,
275 SectionKind::getMetadata(), "debug_range");
276 DwarfMacinfoSection =
277 Ctx->getMachOSection("__DWARF", "__debug_macinfo", MachO::S_ATTR_DEBUG,
278 SectionKind::getMetadata(), "debug_macinfo");
279 DwarfMacroSection =
280 Ctx->getMachOSection("__DWARF", "__debug_macro", MachO::S_ATTR_DEBUG,
281 SectionKind::getMetadata(), "debug_macro");
282 DwarfDebugInlineSection =
283 Ctx->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG,
284 SectionKind::getMetadata());
285 DwarfCUIndexSection =
286 Ctx->getMachOSection("__DWARF", "__debug_cu_index", MachO::S_ATTR_DEBUG,
287 SectionKind::getMetadata());
288 DwarfTUIndexSection =
289 Ctx->getMachOSection("__DWARF", "__debug_tu_index", MachO::S_ATTR_DEBUG,
290 SectionKind::getMetadata());
291 StackMapSection = Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps",
292 0, SectionKind::getMetadata());
293
294 FaultMapSection = Ctx->getMachOSection("__LLVM_FAULTMAPS", "__llvm_faultmaps",
295 0, SectionKind::getMetadata());
296
297 RemarksSection = Ctx->getMachOSection(
298 "__LLVM", "__remarks", MachO::S_ATTR_DEBUG, SectionKind::getMetadata());
299
300 TLSExtraDataSection = TLSTLVSection;
301 }
302
initELFMCObjectFileInfo(const Triple & T,bool Large)303 void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
304 switch (T.getArch()) {
305 case Triple::mips:
306 case Triple::mipsel:
307 case Triple::mips64:
308 case Triple::mips64el:
309 // We cannot use DW_EH_PE_sdata8 for the large PositionIndependent case
310 // since there is no R_MIPS_PC64 relocation (only a 32-bit version).
311 if (PositionIndependent && !Large)
312 FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
313 else
314 FDECFIEncoding = Ctx->getAsmInfo()->getCodePointerSize() == 4
315 ? dwarf::DW_EH_PE_sdata4
316 : dwarf::DW_EH_PE_sdata8;
317 break;
318 case Triple::ppc64:
319 case Triple::ppc64le:
320 case Triple::aarch64:
321 case Triple::aarch64_be:
322 case Triple::x86_64:
323 FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
324 (Large ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
325 break;
326 case Triple::bpfel:
327 case Triple::bpfeb:
328 FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
329 break;
330 case Triple::hexagon:
331 FDECFIEncoding =
332 PositionIndependent ? dwarf::DW_EH_PE_pcrel : dwarf::DW_EH_PE_absptr;
333 break;
334 default:
335 FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
336 break;
337 }
338
339 unsigned EHSectionType = T.getArch() == Triple::x86_64
340 ? ELF::SHT_X86_64_UNWIND
341 : ELF::SHT_PROGBITS;
342
343 // Solaris requires different flags for .eh_frame to seemingly every other
344 // platform.
345 unsigned EHSectionFlags = ELF::SHF_ALLOC;
346 if (T.isOSSolaris() && T.getArch() != Triple::x86_64)
347 EHSectionFlags |= ELF::SHF_WRITE;
348
349 // ELF
350 BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
351 ELF::SHF_WRITE | ELF::SHF_ALLOC);
352
353 TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
354 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
355
356 DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
357 ELF::SHF_WRITE | ELF::SHF_ALLOC);
358
359 ReadOnlySection =
360 Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
361
362 TLSDataSection =
363 Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
364 ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
365
366 TLSBSSSection = Ctx->getELFSection(
367 ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
368
369 DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
370 ELF::SHF_ALLOC | ELF::SHF_WRITE);
371
372 MergeableConst4Section =
373 Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
374 ELF::SHF_ALLOC | ELF::SHF_MERGE, 4, "");
375
376 MergeableConst8Section =
377 Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
378 ELF::SHF_ALLOC | ELF::SHF_MERGE, 8, "");
379
380 MergeableConst16Section =
381 Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
382 ELF::SHF_ALLOC | ELF::SHF_MERGE, 16, "");
383
384 MergeableConst32Section =
385 Ctx->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS,
386 ELF::SHF_ALLOC | ELF::SHF_MERGE, 32, "");
387
388 // Exception Handling Sections.
389
390 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
391 // it contains relocatable pointers. In PIC mode, this is probably a big
392 // runtime hit for C++ apps. Either the contents of the LSDA need to be
393 // adjusted or this should be a data section.
394 LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
395 ELF::SHF_ALLOC);
396
397 COFFDebugSymbolsSection = nullptr;
398 COFFDebugTypesSection = nullptr;
399
400 unsigned DebugSecType = ELF::SHT_PROGBITS;
401
402 // MIPS .debug_* sections should have SHT_MIPS_DWARF section type
403 // to distinguish among sections contain DWARF and ECOFF debug formats.
404 // Sections with ECOFF debug format are obsoleted and marked by SHT_PROGBITS.
405 if (T.isMIPS())
406 DebugSecType = ELF::SHT_MIPS_DWARF;
407
408 // Debug Info Sections.
409 DwarfAbbrevSection =
410 Ctx->getELFSection(".debug_abbrev", DebugSecType, 0);
411 DwarfInfoSection = Ctx->getELFSection(".debug_info", DebugSecType, 0);
412 DwarfLineSection = Ctx->getELFSection(".debug_line", DebugSecType, 0);
413 DwarfLineStrSection =
414 Ctx->getELFSection(".debug_line_str", DebugSecType,
415 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
416 DwarfFrameSection = Ctx->getELFSection(".debug_frame", DebugSecType, 0);
417 DwarfPubNamesSection =
418 Ctx->getELFSection(".debug_pubnames", DebugSecType, 0);
419 DwarfPubTypesSection =
420 Ctx->getELFSection(".debug_pubtypes", DebugSecType, 0);
421 DwarfGnuPubNamesSection =
422 Ctx->getELFSection(".debug_gnu_pubnames", DebugSecType, 0);
423 DwarfGnuPubTypesSection =
424 Ctx->getELFSection(".debug_gnu_pubtypes", DebugSecType, 0);
425 DwarfStrSection =
426 Ctx->getELFSection(".debug_str", DebugSecType,
427 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
428 DwarfLocSection = Ctx->getELFSection(".debug_loc", DebugSecType, 0);
429 DwarfARangesSection =
430 Ctx->getELFSection(".debug_aranges", DebugSecType, 0);
431 DwarfRangesSection =
432 Ctx->getELFSection(".debug_ranges", DebugSecType, 0);
433 DwarfMacinfoSection =
434 Ctx->getELFSection(".debug_macinfo", DebugSecType, 0);
435 DwarfMacroSection = Ctx->getELFSection(".debug_macro", DebugSecType, 0);
436
437 // DWARF5 Experimental Debug Info
438
439 // Accelerator Tables
440 DwarfDebugNamesSection =
441 Ctx->getELFSection(".debug_names", ELF::SHT_PROGBITS, 0);
442 DwarfAccelNamesSection =
443 Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0);
444 DwarfAccelObjCSection =
445 Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0);
446 DwarfAccelNamespaceSection =
447 Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0);
448 DwarfAccelTypesSection =
449 Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0);
450
451 // String Offset and Address Sections
452 DwarfStrOffSection =
453 Ctx->getELFSection(".debug_str_offsets", DebugSecType, 0);
454 DwarfAddrSection = Ctx->getELFSection(".debug_addr", DebugSecType, 0);
455 DwarfRnglistsSection = Ctx->getELFSection(".debug_rnglists", DebugSecType, 0);
456 DwarfLoclistsSection = Ctx->getELFSection(".debug_loclists", DebugSecType, 0);
457
458 // Fission Sections
459 DwarfInfoDWOSection =
460 Ctx->getELFSection(".debug_info.dwo", DebugSecType, ELF::SHF_EXCLUDE);
461 DwarfTypesDWOSection =
462 Ctx->getELFSection(".debug_types.dwo", DebugSecType, ELF::SHF_EXCLUDE);
463 DwarfAbbrevDWOSection =
464 Ctx->getELFSection(".debug_abbrev.dwo", DebugSecType, ELF::SHF_EXCLUDE);
465 DwarfStrDWOSection = Ctx->getELFSection(
466 ".debug_str.dwo", DebugSecType,
467 ELF::SHF_MERGE | ELF::SHF_STRINGS | ELF::SHF_EXCLUDE, 1, "");
468 DwarfLineDWOSection =
469 Ctx->getELFSection(".debug_line.dwo", DebugSecType, ELF::SHF_EXCLUDE);
470 DwarfLocDWOSection =
471 Ctx->getELFSection(".debug_loc.dwo", DebugSecType, ELF::SHF_EXCLUDE);
472 DwarfStrOffDWOSection = Ctx->getELFSection(".debug_str_offsets.dwo",
473 DebugSecType, ELF::SHF_EXCLUDE);
474 DwarfRnglistsDWOSection =
475 Ctx->getELFSection(".debug_rnglists.dwo", DebugSecType, ELF::SHF_EXCLUDE);
476 DwarfMacinfoDWOSection =
477 Ctx->getELFSection(".debug_macinfo.dwo", DebugSecType, ELF::SHF_EXCLUDE);
478 DwarfMacroDWOSection =
479 Ctx->getELFSection(".debug_macro.dwo", DebugSecType, ELF::SHF_EXCLUDE);
480
481 DwarfLoclistsDWOSection =
482 Ctx->getELFSection(".debug_loclists.dwo", DebugSecType, ELF::SHF_EXCLUDE);
483
484 // DWP Sections
485 DwarfCUIndexSection =
486 Ctx->getELFSection(".debug_cu_index", DebugSecType, 0);
487 DwarfTUIndexSection =
488 Ctx->getELFSection(".debug_tu_index", DebugSecType, 0);
489
490 StackMapSection =
491 Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
492
493 FaultMapSection =
494 Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
495
496 EHFrameSection =
497 Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);
498
499 StackSizesSection = Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, 0);
500
501 PseudoProbeSection = Ctx->getELFSection(".pseudo_probe", DebugSecType, 0);
502 PseudoProbeDescSection =
503 Ctx->getELFSection(".pseudo_probe_desc", DebugSecType, 0);
504 }
505
initCOFFMCObjectFileInfo(const Triple & T)506 void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
507 EHFrameSection =
508 Ctx->getCOFFSection(".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
509 COFF::IMAGE_SCN_MEM_READ,
510 SectionKind::getData());
511
512 // Set the `IMAGE_SCN_MEM_16BIT` flag when compiling for thumb mode. This is
513 // used to indicate to the linker that the text segment contains thumb instructions
514 // and to set the ISA selection bit for calls accordingly.
515 const bool IsThumb = T.getArch() == Triple::thumb;
516
517 CommDirectiveSupportsAlignment = true;
518
519 // COFF
520 BSSSection = Ctx->getCOFFSection(
521 ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
522 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
523 SectionKind::getBSS());
524 TextSection = Ctx->getCOFFSection(
525 ".text",
526 (IsThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) |
527 COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
528 COFF::IMAGE_SCN_MEM_READ,
529 SectionKind::getText());
530 DataSection = Ctx->getCOFFSection(
531 ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
532 COFF::IMAGE_SCN_MEM_WRITE,
533 SectionKind::getData());
534 ReadOnlySection = Ctx->getCOFFSection(
535 ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
536 SectionKind::getReadOnly());
537
538 if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::aarch64) {
539 // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
540 LSDASection = nullptr;
541 } else {
542 LSDASection = Ctx->getCOFFSection(".gcc_except_table",
543 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
544 COFF::IMAGE_SCN_MEM_READ,
545 SectionKind::getReadOnly());
546 }
547
548 // Debug info.
549 COFFDebugSymbolsSection =
550 Ctx->getCOFFSection(".debug$S", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
551 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
552 COFF::IMAGE_SCN_MEM_READ),
553 SectionKind::getMetadata());
554 COFFDebugTypesSection =
555 Ctx->getCOFFSection(".debug$T", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
556 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
557 COFF::IMAGE_SCN_MEM_READ),
558 SectionKind::getMetadata());
559 COFFGlobalTypeHashesSection = Ctx->getCOFFSection(
560 ".debug$H",
561 (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
562 COFF::IMAGE_SCN_MEM_READ),
563 SectionKind::getMetadata());
564
565 DwarfAbbrevSection = Ctx->getCOFFSection(
566 ".debug_abbrev",
567 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
568 COFF::IMAGE_SCN_MEM_READ,
569 SectionKind::getMetadata(), "section_abbrev");
570 DwarfInfoSection = Ctx->getCOFFSection(
571 ".debug_info",
572 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
573 COFF::IMAGE_SCN_MEM_READ,
574 SectionKind::getMetadata(), "section_info");
575 DwarfLineSection = Ctx->getCOFFSection(
576 ".debug_line",
577 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
578 COFF::IMAGE_SCN_MEM_READ,
579 SectionKind::getMetadata(), "section_line");
580 DwarfLineStrSection = Ctx->getCOFFSection(
581 ".debug_line_str",
582 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
583 COFF::IMAGE_SCN_MEM_READ,
584 SectionKind::getMetadata(), "section_line_str");
585 DwarfFrameSection = Ctx->getCOFFSection(
586 ".debug_frame",
587 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
588 COFF::IMAGE_SCN_MEM_READ,
589 SectionKind::getMetadata());
590 DwarfPubNamesSection = Ctx->getCOFFSection(
591 ".debug_pubnames",
592 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
593 COFF::IMAGE_SCN_MEM_READ,
594 SectionKind::getMetadata());
595 DwarfPubTypesSection = Ctx->getCOFFSection(
596 ".debug_pubtypes",
597 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
598 COFF::IMAGE_SCN_MEM_READ,
599 SectionKind::getMetadata());
600 DwarfGnuPubNamesSection = Ctx->getCOFFSection(
601 ".debug_gnu_pubnames",
602 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
603 COFF::IMAGE_SCN_MEM_READ,
604 SectionKind::getMetadata());
605 DwarfGnuPubTypesSection = Ctx->getCOFFSection(
606 ".debug_gnu_pubtypes",
607 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
608 COFF::IMAGE_SCN_MEM_READ,
609 SectionKind::getMetadata());
610 DwarfStrSection = Ctx->getCOFFSection(
611 ".debug_str",
612 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
613 COFF::IMAGE_SCN_MEM_READ,
614 SectionKind::getMetadata(), "info_string");
615 DwarfStrOffSection = Ctx->getCOFFSection(
616 ".debug_str_offsets",
617 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
618 COFF::IMAGE_SCN_MEM_READ,
619 SectionKind::getMetadata(), "section_str_off");
620 DwarfLocSection = Ctx->getCOFFSection(
621 ".debug_loc",
622 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
623 COFF::IMAGE_SCN_MEM_READ,
624 SectionKind::getMetadata(), "section_debug_loc");
625 DwarfLoclistsSection = Ctx->getCOFFSection(
626 ".debug_loclists",
627 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
628 COFF::IMAGE_SCN_MEM_READ,
629 SectionKind::getMetadata(), "section_debug_loclists");
630 DwarfARangesSection = Ctx->getCOFFSection(
631 ".debug_aranges",
632 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
633 COFF::IMAGE_SCN_MEM_READ,
634 SectionKind::getMetadata());
635 DwarfRangesSection = Ctx->getCOFFSection(
636 ".debug_ranges",
637 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
638 COFF::IMAGE_SCN_MEM_READ,
639 SectionKind::getMetadata(), "debug_range");
640 DwarfRnglistsSection = Ctx->getCOFFSection(
641 ".debug_rnglists",
642 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
643 COFF::IMAGE_SCN_MEM_READ,
644 SectionKind::getMetadata(), "debug_rnglists");
645 DwarfMacinfoSection = Ctx->getCOFFSection(
646 ".debug_macinfo",
647 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
648 COFF::IMAGE_SCN_MEM_READ,
649 SectionKind::getMetadata(), "debug_macinfo");
650 DwarfMacroSection = Ctx->getCOFFSection(
651 ".debug_macro",
652 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
653 COFF::IMAGE_SCN_MEM_READ,
654 SectionKind::getMetadata(), "debug_macro");
655 DwarfMacinfoDWOSection = Ctx->getCOFFSection(
656 ".debug_macinfo.dwo",
657 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
658 COFF::IMAGE_SCN_MEM_READ,
659 SectionKind::getMetadata(), "debug_macinfo.dwo");
660 DwarfMacroDWOSection = Ctx->getCOFFSection(
661 ".debug_macro.dwo",
662 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
663 COFF::IMAGE_SCN_MEM_READ,
664 SectionKind::getMetadata(), "debug_macro.dwo");
665 DwarfInfoDWOSection = Ctx->getCOFFSection(
666 ".debug_info.dwo",
667 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
668 COFF::IMAGE_SCN_MEM_READ,
669 SectionKind::getMetadata(), "section_info_dwo");
670 DwarfTypesDWOSection = Ctx->getCOFFSection(
671 ".debug_types.dwo",
672 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
673 COFF::IMAGE_SCN_MEM_READ,
674 SectionKind::getMetadata(), "section_types_dwo");
675 DwarfAbbrevDWOSection = Ctx->getCOFFSection(
676 ".debug_abbrev.dwo",
677 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
678 COFF::IMAGE_SCN_MEM_READ,
679 SectionKind::getMetadata(), "section_abbrev_dwo");
680 DwarfStrDWOSection = Ctx->getCOFFSection(
681 ".debug_str.dwo",
682 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
683 COFF::IMAGE_SCN_MEM_READ,
684 SectionKind::getMetadata(), "skel_string");
685 DwarfLineDWOSection = Ctx->getCOFFSection(
686 ".debug_line.dwo",
687 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
688 COFF::IMAGE_SCN_MEM_READ,
689 SectionKind::getMetadata());
690 DwarfLocDWOSection = Ctx->getCOFFSection(
691 ".debug_loc.dwo",
692 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
693 COFF::IMAGE_SCN_MEM_READ,
694 SectionKind::getMetadata(), "skel_loc");
695 DwarfStrOffDWOSection = Ctx->getCOFFSection(
696 ".debug_str_offsets.dwo",
697 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
698 COFF::IMAGE_SCN_MEM_READ,
699 SectionKind::getMetadata(), "section_str_off_dwo");
700 DwarfAddrSection = Ctx->getCOFFSection(
701 ".debug_addr",
702 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
703 COFF::IMAGE_SCN_MEM_READ,
704 SectionKind::getMetadata(), "addr_sec");
705 DwarfCUIndexSection = Ctx->getCOFFSection(
706 ".debug_cu_index",
707 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
708 COFF::IMAGE_SCN_MEM_READ,
709 SectionKind::getMetadata());
710 DwarfTUIndexSection = Ctx->getCOFFSection(
711 ".debug_tu_index",
712 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
713 COFF::IMAGE_SCN_MEM_READ,
714 SectionKind::getMetadata());
715 DwarfDebugNamesSection = Ctx->getCOFFSection(
716 ".debug_names",
717 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
718 COFF::IMAGE_SCN_MEM_READ,
719 SectionKind::getMetadata(), "debug_names_begin");
720 DwarfAccelNamesSection = Ctx->getCOFFSection(
721 ".apple_names",
722 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
723 COFF::IMAGE_SCN_MEM_READ,
724 SectionKind::getMetadata(), "names_begin");
725 DwarfAccelNamespaceSection = Ctx->getCOFFSection(
726 ".apple_namespaces",
727 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
728 COFF::IMAGE_SCN_MEM_READ,
729 SectionKind::getMetadata(), "namespac_begin");
730 DwarfAccelTypesSection = Ctx->getCOFFSection(
731 ".apple_types",
732 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
733 COFF::IMAGE_SCN_MEM_READ,
734 SectionKind::getMetadata(), "types_begin");
735 DwarfAccelObjCSection = Ctx->getCOFFSection(
736 ".apple_objc",
737 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
738 COFF::IMAGE_SCN_MEM_READ,
739 SectionKind::getMetadata(), "objc_begin");
740
741 DrectveSection = Ctx->getCOFFSection(
742 ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE,
743 SectionKind::getMetadata());
744
745 PDataSection = Ctx->getCOFFSection(
746 ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
747 SectionKind::getData());
748
749 XDataSection = Ctx->getCOFFSection(
750 ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
751 SectionKind::getData());
752
753 SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO,
754 SectionKind::getMetadata());
755
756 GFIDsSection = Ctx->getCOFFSection(".gfids$y",
757 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
758 COFF::IMAGE_SCN_MEM_READ,
759 SectionKind::getMetadata());
760
761 GIATsSection = Ctx->getCOFFSection(".giats$y",
762 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
763 COFF::IMAGE_SCN_MEM_READ,
764 SectionKind::getMetadata());
765
766 GLJMPSection = Ctx->getCOFFSection(".gljmp$y",
767 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
768 COFF::IMAGE_SCN_MEM_READ,
769 SectionKind::getMetadata());
770
771 TLSDataSection = Ctx->getCOFFSection(
772 ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
773 COFF::IMAGE_SCN_MEM_WRITE,
774 SectionKind::getData());
775
776 StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps",
777 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
778 COFF::IMAGE_SCN_MEM_READ,
779 SectionKind::getReadOnly());
780 }
781
initWasmMCObjectFileInfo(const Triple & T)782 void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
783 TextSection = Ctx->getWasmSection(".text", SectionKind::getText());
784 DataSection = Ctx->getWasmSection(".data", SectionKind::getData());
785
786 DwarfLineSection =
787 Ctx->getWasmSection(".debug_line", SectionKind::getMetadata());
788 DwarfLineStrSection =
789 Ctx->getWasmSection(".debug_line_str", SectionKind::getMetadata());
790 DwarfStrSection =
791 Ctx->getWasmSection(".debug_str", SectionKind::getMetadata());
792 DwarfLocSection =
793 Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata());
794 DwarfAbbrevSection =
795 Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata());
796 DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata());
797 DwarfRangesSection =
798 Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata());
799 DwarfMacinfoSection =
800 Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata());
801 DwarfMacroSection =
802 Ctx->getWasmSection(".debug_macro", SectionKind::getMetadata());
803 DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
804 DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
805 DwarfInfoSection =
806 Ctx->getWasmSection(".debug_info", SectionKind::getMetadata());
807 DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata());
808 DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata());
809 DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata());
810 DwarfGnuPubNamesSection =
811 Ctx->getWasmSection(".debug_gnu_pubnames", SectionKind::getMetadata());
812 DwarfGnuPubTypesSection =
813 Ctx->getWasmSection(".debug_gnu_pubtypes", SectionKind::getMetadata());
814
815 DwarfDebugNamesSection =
816 Ctx->getWasmSection(".debug_names", SectionKind::getMetadata());
817 DwarfStrOffSection =
818 Ctx->getWasmSection(".debug_str_offsets", SectionKind::getMetadata());
819 DwarfAddrSection =
820 Ctx->getWasmSection(".debug_addr", SectionKind::getMetadata());
821 DwarfRnglistsSection =
822 Ctx->getWasmSection(".debug_rnglists", SectionKind::getMetadata());
823 DwarfLoclistsSection =
824 Ctx->getWasmSection(".debug_loclists", SectionKind::getMetadata());
825
826 // Fission Sections
827 DwarfInfoDWOSection =
828 Ctx->getWasmSection(".debug_info.dwo", SectionKind::getMetadata());
829 DwarfTypesDWOSection =
830 Ctx->getWasmSection(".debug_types.dwo", SectionKind::getMetadata());
831 DwarfAbbrevDWOSection =
832 Ctx->getWasmSection(".debug_abbrev.dwo", SectionKind::getMetadata());
833 DwarfStrDWOSection =
834 Ctx->getWasmSection(".debug_str.dwo", SectionKind::getMetadata());
835 DwarfLineDWOSection =
836 Ctx->getWasmSection(".debug_line.dwo", SectionKind::getMetadata());
837 DwarfLocDWOSection =
838 Ctx->getWasmSection(".debug_loc.dwo", SectionKind::getMetadata());
839 DwarfStrOffDWOSection =
840 Ctx->getWasmSection(".debug_str_offsets.dwo", SectionKind::getMetadata());
841 DwarfRnglistsDWOSection =
842 Ctx->getWasmSection(".debug_rnglists.dwo", SectionKind::getMetadata());
843 DwarfMacinfoDWOSection =
844 Ctx->getWasmSection(".debug_macinfo.dwo", SectionKind::getMetadata());
845 DwarfMacroDWOSection =
846 Ctx->getWasmSection(".debug_macro.dwo", SectionKind::getMetadata());
847
848 DwarfLoclistsDWOSection =
849 Ctx->getWasmSection(".debug_loclists.dwo", SectionKind::getMetadata());
850
851 // DWP Sections
852 DwarfCUIndexSection =
853 Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata(), 0);
854 DwarfTUIndexSection =
855 Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata(), 0);
856
857 // Wasm use data section for LSDA.
858 // TODO Consider putting each function's exception table in a separate
859 // section, as in -function-sections, to facilitate lld's --gc-section.
860 LSDASection = Ctx->getWasmSection(".rodata.gcc_except_table",
861 SectionKind::getReadOnlyWithRel());
862
863 // TODO: Define more sections.
864 }
865
initXCOFFMCObjectFileInfo(const Triple & T)866 void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) {
867 // The default csect for program code. Functions without a specified section
868 // get placed into this csect. The choice of csect name is not a property of
869 // the ABI or object file format. For example, the XL compiler uses an unnamed
870 // csect for program code.
871 TextSection = Ctx->getXCOFFSection(
872 ".text", XCOFF::StorageMappingClass::XMC_PR, XCOFF::XTY_SD,
873 SectionKind::getText(), /* MultiSymbolsAllowed*/ true);
874
875 DataSection = Ctx->getXCOFFSection(
876 ".data", XCOFF::StorageMappingClass::XMC_RW, XCOFF::XTY_SD,
877 SectionKind::getData(), /* MultiSymbolsAllowed*/ true);
878
879 ReadOnlySection = Ctx->getXCOFFSection(
880 ".rodata", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
881 SectionKind::getReadOnly(), /* MultiSymbolsAllowed*/ true);
882
883 TOCBaseSection =
884 Ctx->getXCOFFSection("TOC", XCOFF::StorageMappingClass::XMC_TC0,
885 XCOFF::XTY_SD, SectionKind::getData());
886
887 // The TOC-base always has 0 size, but 4 byte alignment.
888 TOCBaseSection->setAlignment(Align(4));
889
890 LSDASection = Ctx->getXCOFFSection(".gcc_except_table",
891 XCOFF::StorageMappingClass::XMC_RO,
892 XCOFF::XTY_SD, SectionKind::getReadOnly());
893
894 CompactUnwindSection =
895 Ctx->getXCOFFSection(".eh_info_table", XCOFF::StorageMappingClass::XMC_RW,
896 XCOFF::XTY_SD, SectionKind::getData());
897
898 // DWARF sections for XCOFF are not csects. They are special STYP_DWARF
899 // sections, and the individual DWARF sections are distinguished by their
900 // section subtype.
901 // TODO: Populate the DWARF sections appropriately.
902 DwarfAbbrevSection = nullptr; // SSUBTYP_DWABREV
903 DwarfInfoSection = nullptr; // SSUBTYP_DWINFO
904 DwarfLineSection = nullptr; // SSUBTYP_DWLINE
905 DwarfFrameSection = nullptr; // SSUBTYP_DWFRAME
906 DwarfPubNamesSection = nullptr; // SSUBTYP_DWPBNMS
907 DwarfPubTypesSection = nullptr; // SSUBTYP_DWPBTYP
908 DwarfStrSection = nullptr; // SSUBTYP_DWSTR
909 DwarfLocSection = nullptr; // SSUBTYP_DWLOC
910 DwarfARangesSection = nullptr; // SSUBTYP_DWARNGE
911 DwarfRangesSection = nullptr; // SSUBTYP_DWRNGES
912 DwarfMacinfoSection = nullptr; // SSUBTYP_DWMAC
913 }
914
InitMCObjectFileInfo(const Triple & TheTriple,bool PIC,MCContext & ctx,bool LargeCodeModel)915 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, bool PIC,
916 MCContext &ctx,
917 bool LargeCodeModel) {
918 PositionIndependent = PIC;
919 Ctx = &ctx;
920
921 // Common.
922 CommDirectiveSupportsAlignment = true;
923 SupportsWeakOmittedEHFrame = true;
924 SupportsCompactUnwindWithoutEHFrame = false;
925 OmitDwarfIfHaveCompactUnwind = false;
926
927 FDECFIEncoding = dwarf::DW_EH_PE_absptr;
928
929 CompactUnwindDwarfEHFrameOnly = 0;
930
931 EHFrameSection = nullptr; // Created on demand.
932 CompactUnwindSection = nullptr; // Used only by selected targets.
933 DwarfAccelNamesSection = nullptr; // Used only by selected targets.
934 DwarfAccelObjCSection = nullptr; // Used only by selected targets.
935 DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
936 DwarfAccelTypesSection = nullptr; // Used only by selected targets.
937
938 TT = TheTriple;
939
940 switch (TT.getObjectFormat()) {
941 case Triple::MachO:
942 Env = IsMachO;
943 initMachOMCObjectFileInfo(TT);
944 break;
945 case Triple::COFF:
946 if (!TT.isOSWindows())
947 report_fatal_error(
948 "Cannot initialize MC for non-Windows COFF object files.");
949
950 Env = IsCOFF;
951 initCOFFMCObjectFileInfo(TT);
952 break;
953 case Triple::ELF:
954 Env = IsELF;
955 initELFMCObjectFileInfo(TT, LargeCodeModel);
956 break;
957 case Triple::Wasm:
958 Env = IsWasm;
959 initWasmMCObjectFileInfo(TT);
960 break;
961 case Triple::GOFF:
962 report_fatal_error("Cannot initialize MC for GOFF object file format");
963 break;
964 case Triple::XCOFF:
965 Env = IsXCOFF;
966 initXCOFFMCObjectFileInfo(TT);
967 break;
968 case Triple::UnknownObjectFormat:
969 report_fatal_error("Cannot initialize MC for unknown object file format.");
970 break;
971 }
972 }
973
getDwarfComdatSection(const char * Name,uint64_t Hash) const974 MCSection *MCObjectFileInfo::getDwarfComdatSection(const char *Name,
975 uint64_t Hash) const {
976 switch (TT.getObjectFormat()) {
977 case Triple::ELF:
978 return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, ELF::SHF_GROUP, 0,
979 utostr(Hash));
980 case Triple::Wasm:
981 return Ctx->getWasmSection(Name, SectionKind::getMetadata(), utostr(Hash),
982 MCContext::GenericSectionID);
983 case Triple::MachO:
984 case Triple::COFF:
985 case Triple::GOFF:
986 case Triple::XCOFF:
987 case Triple::UnknownObjectFormat:
988 report_fatal_error("Cannot get DWARF comdat section for this object file "
989 "format: not implemented.");
990 break;
991 }
992 llvm_unreachable("Unknown ObjectFormatType");
993 }
994
995 MCSection *
getStackSizesSection(const MCSection & TextSec) const996 MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
997 if (Env != IsELF)
998 return StackSizesSection;
999
1000 const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1001 unsigned Flags = ELF::SHF_LINK_ORDER;
1002 StringRef GroupName;
1003 if (const MCSymbol *Group = ElfSec.getGroup()) {
1004 GroupName = Group->getName();
1005 Flags |= ELF::SHF_GROUP;
1006 }
1007
1008 return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0,
1009 GroupName, ElfSec.getUniqueID(),
1010 cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1011 }
1012
1013 MCSection *
getBBAddrMapSection(const MCSection & TextSec) const1014 MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
1015 if (Env != IsELF)
1016 return nullptr;
1017
1018 const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
1019 unsigned Flags = ELF::SHF_LINK_ORDER;
1020 StringRef GroupName;
1021 if (const MCSymbol *Group = ElfSec.getGroup()) {
1022 GroupName = Group->getName();
1023 Flags |= ELF::SHF_GROUP;
1024 }
1025
1026 // Use the text section's begin symbol and unique ID to create a separate
1027 // .llvm_bb_addr_map section associated with every unique text section.
1028 return Ctx->getELFSection(".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP,
1029 Flags, 0, GroupName, ElfSec.getUniqueID(),
1030 cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1031 }
1032
1033 MCSection *
getPseudoProbeSection(const MCSection * TextSec) const1034 MCObjectFileInfo::getPseudoProbeSection(const MCSection *TextSec) const {
1035 if (Env == IsELF) {
1036 const auto *ElfSec = static_cast<const MCSectionELF *>(TextSec);
1037 // Create a separate section for probes that comes with a comdat function.
1038 if (const MCSymbol *Group = ElfSec->getGroup()) {
1039 auto *S = static_cast<MCSectionELF *>(PseudoProbeSection);
1040 auto Flags = S->getFlags() | ELF::SHF_GROUP;
1041 return Ctx->getELFSection(S->getName(), S->getType(), Flags,
1042 S->getEntrySize(), Group->getName());
1043 }
1044 }
1045 return PseudoProbeSection;
1046 }
1047
1048 MCSection *
getPseudoProbeDescSection(StringRef FuncName) const1049 MCObjectFileInfo::getPseudoProbeDescSection(StringRef FuncName) const {
1050 if (Env == IsELF) {
1051 // Create a separate comdat group for each function's descriptor in order
1052 // for the linker to deduplicate. The duplication, must be from different
1053 // tranlation unit, can come from:
1054 // 1. Inline functions defined in header files;
1055 // 2. ThinLTO imported funcions;
1056 // 3. Weak-linkage definitions.
1057 // Use a concatenation of the section name and the function name as the
1058 // group name so that descriptor-only groups won't be folded with groups of
1059 // code.
1060 if (TT.supportsCOMDAT() && !FuncName.empty()) {
1061 auto *S = static_cast<MCSectionELF *>(PseudoProbeDescSection);
1062 auto Flags = S->getFlags() | ELF::SHF_GROUP;
1063 return Ctx->getELFSection(S->getName(), S->getType(), Flags,
1064 S->getEntrySize(),
1065 S->getName() + "_" + FuncName);
1066 }
1067 }
1068 return PseudoProbeDescSection;
1069 }
1070