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 22 using namespace llvm; 23 24 static bool useCompactUnwind(const Triple &T) { 25 // Only on darwin. 26 if (!T.isOSDarwin()) 27 return false; 28 29 // aarch64 always has it. 30 if (T.getArch() == Triple::aarch64) 31 return true; 32 33 // armv7k always has it. 34 if (T.isWatchABI()) 35 return true; 36 37 // Use it on newer version of OS X. 38 if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6)) 39 return true; 40 41 // And the iOS simulator. 42 if (T.isiOS() && 43 (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86)) 44 return true; 45 46 return false; 47 } 48 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() && T.getArch() == Triple::aarch64) 60 SupportsCompactUnwindWithoutEHFrame = true; 61 62 if (T.isWatchABI()) 63 OmitDwarfIfHaveCompactUnwind = true; 64 65 FDECFIEncoding = dwarf::DW_EH_PE_pcrel; 66 67 // .comm doesn't support alignment before Leopard. 68 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5)) 69 CommDirectiveSupportsAlignment = false; 70 71 TextSection // .text 72 = Ctx->getMachOSection("__TEXT", "__text", 73 MachO::S_ATTR_PURE_INSTRUCTIONS, 74 SectionKind::getText()); 75 DataSection // .data 76 = Ctx->getMachOSection("__DATA", "__data", 0, SectionKind::getData()); 77 78 // BSSSection might not be expected initialized on msvc. 79 BSSSection = nullptr; 80 81 TLSDataSection // .tdata 82 = Ctx->getMachOSection("__DATA", "__thread_data", 83 MachO::S_THREAD_LOCAL_REGULAR, 84 SectionKind::getData()); 85 TLSBSSSection // .tbss 86 = Ctx->getMachOSection("__DATA", "__thread_bss", 87 MachO::S_THREAD_LOCAL_ZEROFILL, 88 SectionKind::getThreadBSS()); 89 90 // TODO: Verify datarel below. 91 TLSTLVSection // .tlv 92 = Ctx->getMachOSection("__DATA", "__thread_vars", 93 MachO::S_THREAD_LOCAL_VARIABLES, 94 SectionKind::getData()); 95 96 TLSThreadInitSection = Ctx->getMachOSection( 97 "__DATA", "__thread_init", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS, 98 SectionKind::getData()); 99 100 CStringSection // .cstring 101 = Ctx->getMachOSection("__TEXT", "__cstring", 102 MachO::S_CSTRING_LITERALS, 103 SectionKind::getMergeable1ByteCString()); 104 UStringSection 105 = Ctx->getMachOSection("__TEXT","__ustring", 0, 106 SectionKind::getMergeable2ByteCString()); 107 FourByteConstantSection // .literal4 108 = Ctx->getMachOSection("__TEXT", "__literal4", 109 MachO::S_4BYTE_LITERALS, 110 SectionKind::getMergeableConst4()); 111 EightByteConstantSection // .literal8 112 = Ctx->getMachOSection("__TEXT", "__literal8", 113 MachO::S_8BYTE_LITERALS, 114 SectionKind::getMergeableConst8()); 115 116 SixteenByteConstantSection // .literal16 117 = Ctx->getMachOSection("__TEXT", "__literal16", 118 MachO::S_16BYTE_LITERALS, 119 SectionKind::getMergeableConst16()); 120 121 ReadOnlySection // .const 122 = Ctx->getMachOSection("__TEXT", "__const", 0, 123 SectionKind::getReadOnly()); 124 125 // If the target is not powerpc, map the coal sections to the non-coal 126 // sections. 127 // 128 // "__TEXT/__textcoal_nt" => section "__TEXT/__text" 129 // "__TEXT/__const_coal" => section "__TEXT/__const" 130 // "__DATA/__datacoal_nt" => section "__DATA/__data" 131 Triple::ArchType ArchTy = T.getArch(); 132 133 ConstDataSection // .const_data 134 = Ctx->getMachOSection("__DATA", "__const", 0, 135 SectionKind::getReadOnlyWithRel()); 136 137 if (ArchTy == Triple::ppc || ArchTy == Triple::ppc64) { 138 TextCoalSection 139 = Ctx->getMachOSection("__TEXT", "__textcoal_nt", 140 MachO::S_COALESCED | 141 MachO::S_ATTR_PURE_INSTRUCTIONS, 142 SectionKind::getText()); 143 ConstTextCoalSection 144 = Ctx->getMachOSection("__TEXT", "__const_coal", 145 MachO::S_COALESCED, 146 SectionKind::getReadOnly()); 147 DataCoalSection = Ctx->getMachOSection( 148 "__DATA", "__datacoal_nt", MachO::S_COALESCED, SectionKind::getData()); 149 ConstDataCoalSection = DataCoalSection; 150 } else { 151 TextCoalSection = TextSection; 152 ConstTextCoalSection = ReadOnlySection; 153 DataCoalSection = DataSection; 154 ConstDataCoalSection = ConstDataSection; 155 } 156 157 DataCommonSection 158 = Ctx->getMachOSection("__DATA","__common", 159 MachO::S_ZEROFILL, 160 SectionKind::getBSS()); 161 DataBSSSection 162 = Ctx->getMachOSection("__DATA","__bss", MachO::S_ZEROFILL, 163 SectionKind::getBSS()); 164 165 166 LazySymbolPointerSection 167 = Ctx->getMachOSection("__DATA", "__la_symbol_ptr", 168 MachO::S_LAZY_SYMBOL_POINTERS, 169 SectionKind::getMetadata()); 170 NonLazySymbolPointerSection 171 = Ctx->getMachOSection("__DATA", "__nl_symbol_ptr", 172 MachO::S_NON_LAZY_SYMBOL_POINTERS, 173 SectionKind::getMetadata()); 174 175 ThreadLocalPointerSection 176 = Ctx->getMachOSection("__DATA", "__thread_ptr", 177 MachO::S_THREAD_LOCAL_VARIABLE_POINTERS, 178 SectionKind::getMetadata()); 179 180 // Exception Handling. 181 LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0, 182 SectionKind::getReadOnlyWithRel()); 183 184 COFFDebugSymbolsSection = nullptr; 185 COFFDebugTypesSection = nullptr; 186 COFFGlobalTypeHashesSection = nullptr; 187 188 if (useCompactUnwind(T)) { 189 CompactUnwindSection = 190 Ctx->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG, 191 SectionKind::getReadOnly()); 192 193 if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86) 194 CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_X86_64_MODE_DWARF 195 else if (T.getArch() == Triple::aarch64) 196 CompactUnwindDwarfEHFrameOnly = 0x03000000; // UNWIND_ARM64_MODE_DWARF 197 else if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb) 198 CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_ARM_MODE_DWARF 199 } 200 201 // Debug Information. 202 DwarfDebugNamesSection = 203 Ctx->getMachOSection("__DWARF", "__debug_names", MachO::S_ATTR_DEBUG, 204 SectionKind::getMetadata(), "debug_names_begin"); 205 DwarfAccelNamesSection = 206 Ctx->getMachOSection("__DWARF", "__apple_names", MachO::S_ATTR_DEBUG, 207 SectionKind::getMetadata(), "names_begin"); 208 DwarfAccelObjCSection = 209 Ctx->getMachOSection("__DWARF", "__apple_objc", MachO::S_ATTR_DEBUG, 210 SectionKind::getMetadata(), "objc_begin"); 211 // 16 character section limit... 212 DwarfAccelNamespaceSection = 213 Ctx->getMachOSection("__DWARF", "__apple_namespac", MachO::S_ATTR_DEBUG, 214 SectionKind::getMetadata(), "namespac_begin"); 215 DwarfAccelTypesSection = 216 Ctx->getMachOSection("__DWARF", "__apple_types", MachO::S_ATTR_DEBUG, 217 SectionKind::getMetadata(), "types_begin"); 218 219 DwarfSwiftASTSection = 220 Ctx->getMachOSection("__DWARF", "__swift_ast", MachO::S_ATTR_DEBUG, 221 SectionKind::getMetadata()); 222 223 DwarfAbbrevSection = 224 Ctx->getMachOSection("__DWARF", "__debug_abbrev", MachO::S_ATTR_DEBUG, 225 SectionKind::getMetadata(), "section_abbrev"); 226 DwarfInfoSection = 227 Ctx->getMachOSection("__DWARF", "__debug_info", MachO::S_ATTR_DEBUG, 228 SectionKind::getMetadata(), "section_info"); 229 DwarfLineSection = 230 Ctx->getMachOSection("__DWARF", "__debug_line", MachO::S_ATTR_DEBUG, 231 SectionKind::getMetadata(), "section_line"); 232 DwarfLineStrSection = 233 Ctx->getMachOSection("__DWARF", "__debug_line_str", MachO::S_ATTR_DEBUG, 234 SectionKind::getMetadata(), "section_line_str"); 235 DwarfFrameSection = 236 Ctx->getMachOSection("__DWARF", "__debug_frame", MachO::S_ATTR_DEBUG, 237 SectionKind::getMetadata()); 238 DwarfPubNamesSection = 239 Ctx->getMachOSection("__DWARF", "__debug_pubnames", MachO::S_ATTR_DEBUG, 240 SectionKind::getMetadata()); 241 DwarfPubTypesSection = 242 Ctx->getMachOSection("__DWARF", "__debug_pubtypes", MachO::S_ATTR_DEBUG, 243 SectionKind::getMetadata()); 244 DwarfGnuPubNamesSection = 245 Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn", MachO::S_ATTR_DEBUG, 246 SectionKind::getMetadata()); 247 DwarfGnuPubTypesSection = 248 Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt", MachO::S_ATTR_DEBUG, 249 SectionKind::getMetadata()); 250 DwarfStrSection = 251 Ctx->getMachOSection("__DWARF", "__debug_str", MachO::S_ATTR_DEBUG, 252 SectionKind::getMetadata(), "info_string"); 253 DwarfStrOffSection = 254 Ctx->getMachOSection("__DWARF", "__debug_str_offs", MachO::S_ATTR_DEBUG, 255 SectionKind::getMetadata(), "section_str_off"); 256 DwarfAddrSection = 257 Ctx->getMachOSection("__DWARF", "__debug_addr", MachO::S_ATTR_DEBUG, 258 SectionKind::getMetadata(), "section_info"); 259 DwarfLocSection = 260 Ctx->getMachOSection("__DWARF", "__debug_loc", MachO::S_ATTR_DEBUG, 261 SectionKind::getMetadata(), "section_debug_loc"); 262 DwarfLoclistsSection = 263 Ctx->getMachOSection("__DWARF", "__debug_loclists", MachO::S_ATTR_DEBUG, 264 SectionKind::getMetadata(), "section_debug_loc"); 265 266 DwarfARangesSection = 267 Ctx->getMachOSection("__DWARF", "__debug_aranges", MachO::S_ATTR_DEBUG, 268 SectionKind::getMetadata()); 269 DwarfRangesSection = 270 Ctx->getMachOSection("__DWARF", "__debug_ranges", MachO::S_ATTR_DEBUG, 271 SectionKind::getMetadata(), "debug_range"); 272 DwarfRnglistsSection = 273 Ctx->getMachOSection("__DWARF", "__debug_rnglists", MachO::S_ATTR_DEBUG, 274 SectionKind::getMetadata(), "debug_range"); 275 DwarfMacinfoSection = 276 Ctx->getMachOSection("__DWARF", "__debug_macinfo", MachO::S_ATTR_DEBUG, 277 SectionKind::getMetadata(), "debug_macinfo"); 278 DwarfDebugInlineSection = 279 Ctx->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG, 280 SectionKind::getMetadata()); 281 DwarfCUIndexSection = 282 Ctx->getMachOSection("__DWARF", "__debug_cu_index", MachO::S_ATTR_DEBUG, 283 SectionKind::getMetadata()); 284 DwarfTUIndexSection = 285 Ctx->getMachOSection("__DWARF", "__debug_tu_index", MachO::S_ATTR_DEBUG, 286 SectionKind::getMetadata()); 287 StackMapSection = Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps", 288 0, SectionKind::getMetadata()); 289 290 FaultMapSection = Ctx->getMachOSection("__LLVM_FAULTMAPS", "__llvm_faultmaps", 291 0, SectionKind::getMetadata()); 292 293 TLSExtraDataSection = TLSTLVSection; 294 } 295 296 void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) { 297 switch (T.getArch()) { 298 case Triple::mips: 299 case Triple::mipsel: 300 case Triple::mips64: 301 case Triple::mips64el: 302 FDECFIEncoding = Ctx->getAsmInfo()->getCodePointerSize() == 4 303 ? dwarf::DW_EH_PE_sdata4 304 : dwarf::DW_EH_PE_sdata8; 305 break; 306 case Triple::ppc64: 307 case Triple::ppc64le: 308 case Triple::x86_64: 309 FDECFIEncoding = dwarf::DW_EH_PE_pcrel | 310 (Large ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4); 311 break; 312 case Triple::bpfel: 313 case Triple::bpfeb: 314 FDECFIEncoding = dwarf::DW_EH_PE_sdata8; 315 break; 316 case Triple::hexagon: 317 FDECFIEncoding = 318 PositionIndependent ? dwarf::DW_EH_PE_pcrel : dwarf::DW_EH_PE_absptr; 319 break; 320 default: 321 FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 322 break; 323 } 324 325 unsigned EHSectionType = T.getArch() == Triple::x86_64 326 ? ELF::SHT_X86_64_UNWIND 327 : ELF::SHT_PROGBITS; 328 329 // Solaris requires different flags for .eh_frame to seemingly every other 330 // platform. 331 unsigned EHSectionFlags = ELF::SHF_ALLOC; 332 if (T.isOSSolaris() && T.getArch() != Triple::x86_64) 333 EHSectionFlags |= ELF::SHF_WRITE; 334 335 // ELF 336 BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS, 337 ELF::SHF_WRITE | ELF::SHF_ALLOC); 338 339 TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS, 340 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC); 341 342 DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS, 343 ELF::SHF_WRITE | ELF::SHF_ALLOC); 344 345 ReadOnlySection = 346 Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 347 348 TLSDataSection = 349 Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS, 350 ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE); 351 352 TLSBSSSection = Ctx->getELFSection( 353 ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE); 354 355 DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS, 356 ELF::SHF_ALLOC | ELF::SHF_WRITE); 357 358 MergeableConst4Section = 359 Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS, 360 ELF::SHF_ALLOC | ELF::SHF_MERGE, 4, ""); 361 362 MergeableConst8Section = 363 Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS, 364 ELF::SHF_ALLOC | ELF::SHF_MERGE, 8, ""); 365 366 MergeableConst16Section = 367 Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS, 368 ELF::SHF_ALLOC | ELF::SHF_MERGE, 16, ""); 369 370 MergeableConst32Section = 371 Ctx->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS, 372 ELF::SHF_ALLOC | ELF::SHF_MERGE, 32, ""); 373 374 // Exception Handling Sections. 375 376 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though 377 // it contains relocatable pointers. In PIC mode, this is probably a big 378 // runtime hit for C++ apps. Either the contents of the LSDA need to be 379 // adjusted or this should be a data section. 380 LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS, 381 ELF::SHF_ALLOC); 382 383 COFFDebugSymbolsSection = nullptr; 384 COFFDebugTypesSection = nullptr; 385 386 unsigned DebugSecType = ELF::SHT_PROGBITS; 387 388 // MIPS .debug_* sections should have SHT_MIPS_DWARF section type 389 // to distinguish among sections contain DWARF and ECOFF debug formats. 390 // Sections with ECOFF debug format are obsoleted and marked by SHT_PROGBITS. 391 if (T.isMIPS()) 392 DebugSecType = ELF::SHT_MIPS_DWARF; 393 394 // Debug Info Sections. 395 DwarfAbbrevSection = 396 Ctx->getELFSection(".debug_abbrev", DebugSecType, 0); 397 DwarfInfoSection = Ctx->getELFSection(".debug_info", DebugSecType, 0); 398 DwarfLineSection = Ctx->getELFSection(".debug_line", DebugSecType, 0); 399 DwarfLineStrSection = 400 Ctx->getELFSection(".debug_line_str", DebugSecType, 401 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); 402 DwarfFrameSection = Ctx->getELFSection(".debug_frame", DebugSecType, 0); 403 DwarfPubNamesSection = 404 Ctx->getELFSection(".debug_pubnames", DebugSecType, 0); 405 DwarfPubTypesSection = 406 Ctx->getELFSection(".debug_pubtypes", DebugSecType, 0); 407 DwarfGnuPubNamesSection = 408 Ctx->getELFSection(".debug_gnu_pubnames", DebugSecType, 0); 409 DwarfGnuPubTypesSection = 410 Ctx->getELFSection(".debug_gnu_pubtypes", DebugSecType, 0); 411 DwarfStrSection = 412 Ctx->getELFSection(".debug_str", DebugSecType, 413 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); 414 DwarfLocSection = Ctx->getELFSection(".debug_loc", DebugSecType, 0); 415 DwarfARangesSection = 416 Ctx->getELFSection(".debug_aranges", DebugSecType, 0); 417 DwarfRangesSection = 418 Ctx->getELFSection(".debug_ranges", DebugSecType, 0); 419 DwarfMacinfoSection = 420 Ctx->getELFSection(".debug_macinfo", DebugSecType, 0); 421 422 // DWARF5 Experimental Debug Info 423 424 // Accelerator Tables 425 DwarfDebugNamesSection = 426 Ctx->getELFSection(".debug_names", ELF::SHT_PROGBITS, 0); 427 DwarfAccelNamesSection = 428 Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0); 429 DwarfAccelObjCSection = 430 Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0); 431 DwarfAccelNamespaceSection = 432 Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0); 433 DwarfAccelTypesSection = 434 Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0); 435 436 // String Offset and Address Sections 437 DwarfStrOffSection = 438 Ctx->getELFSection(".debug_str_offsets", DebugSecType, 0); 439 DwarfAddrSection = Ctx->getELFSection(".debug_addr", DebugSecType, 0); 440 DwarfRnglistsSection = Ctx->getELFSection(".debug_rnglists", DebugSecType, 0); 441 DwarfLoclistsSection = Ctx->getELFSection(".debug_loclists", DebugSecType, 0); 442 443 // Fission Sections 444 DwarfInfoDWOSection = 445 Ctx->getELFSection(".debug_info.dwo", DebugSecType, ELF::SHF_EXCLUDE); 446 DwarfTypesDWOSection = 447 Ctx->getELFSection(".debug_types.dwo", DebugSecType, ELF::SHF_EXCLUDE); 448 DwarfAbbrevDWOSection = 449 Ctx->getELFSection(".debug_abbrev.dwo", DebugSecType, ELF::SHF_EXCLUDE); 450 DwarfStrDWOSection = Ctx->getELFSection( 451 ".debug_str.dwo", DebugSecType, 452 ELF::SHF_MERGE | ELF::SHF_STRINGS | ELF::SHF_EXCLUDE, 1, ""); 453 DwarfLineDWOSection = 454 Ctx->getELFSection(".debug_line.dwo", DebugSecType, ELF::SHF_EXCLUDE); 455 DwarfLocDWOSection = 456 Ctx->getELFSection(".debug_loc.dwo", DebugSecType, ELF::SHF_EXCLUDE); 457 DwarfStrOffDWOSection = Ctx->getELFSection(".debug_str_offsets.dwo", 458 DebugSecType, ELF::SHF_EXCLUDE); 459 DwarfRnglistsDWOSection = 460 Ctx->getELFSection(".debug_rnglists.dwo", DebugSecType, ELF::SHF_EXCLUDE); 461 462 // DWP Sections 463 DwarfCUIndexSection = 464 Ctx->getELFSection(".debug_cu_index", DebugSecType, 0); 465 DwarfTUIndexSection = 466 Ctx->getELFSection(".debug_tu_index", DebugSecType, 0); 467 468 StackMapSection = 469 Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 470 471 FaultMapSection = 472 Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 473 474 EHFrameSection = 475 Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags); 476 477 StackSizesSection = Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, 0); 478 } 479 480 void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) { 481 EHFrameSection = 482 Ctx->getCOFFSection(".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 483 COFF::IMAGE_SCN_MEM_READ, 484 SectionKind::getData()); 485 486 // Set the `IMAGE_SCN_MEM_16BIT` flag when compiling for thumb mode. This is 487 // used to indicate to the linker that the text segment contains thumb instructions 488 // and to set the ISA selection bit for calls accordingly. 489 const bool IsThumb = T.getArch() == Triple::thumb; 490 491 CommDirectiveSupportsAlignment = true; 492 493 // COFF 494 BSSSection = Ctx->getCOFFSection( 495 ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 496 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 497 SectionKind::getBSS()); 498 TextSection = Ctx->getCOFFSection( 499 ".text", 500 (IsThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) | 501 COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE | 502 COFF::IMAGE_SCN_MEM_READ, 503 SectionKind::getText()); 504 DataSection = Ctx->getCOFFSection( 505 ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | 506 COFF::IMAGE_SCN_MEM_WRITE, 507 SectionKind::getData()); 508 ReadOnlySection = Ctx->getCOFFSection( 509 ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 510 SectionKind::getReadOnly()); 511 512 if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::aarch64) { 513 // On Windows 64 with SEH, the LSDA is emitted into the .xdata section 514 LSDASection = nullptr; 515 } else { 516 LSDASection = Ctx->getCOFFSection(".gcc_except_table", 517 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 518 COFF::IMAGE_SCN_MEM_READ, 519 SectionKind::getReadOnly()); 520 } 521 522 // Debug info. 523 COFFDebugSymbolsSection = 524 Ctx->getCOFFSection(".debug$S", (COFF::IMAGE_SCN_MEM_DISCARDABLE | 525 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 526 COFF::IMAGE_SCN_MEM_READ), 527 SectionKind::getMetadata()); 528 COFFDebugTypesSection = 529 Ctx->getCOFFSection(".debug$T", (COFF::IMAGE_SCN_MEM_DISCARDABLE | 530 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 531 COFF::IMAGE_SCN_MEM_READ), 532 SectionKind::getMetadata()); 533 COFFGlobalTypeHashesSection = Ctx->getCOFFSection( 534 ".debug$H", 535 (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 536 COFF::IMAGE_SCN_MEM_READ), 537 SectionKind::getMetadata()); 538 539 DwarfAbbrevSection = Ctx->getCOFFSection( 540 ".debug_abbrev", 541 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 542 COFF::IMAGE_SCN_MEM_READ, 543 SectionKind::getMetadata(), "section_abbrev"); 544 DwarfInfoSection = Ctx->getCOFFSection( 545 ".debug_info", 546 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 547 COFF::IMAGE_SCN_MEM_READ, 548 SectionKind::getMetadata(), "section_info"); 549 DwarfLineSection = Ctx->getCOFFSection( 550 ".debug_line", 551 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 552 COFF::IMAGE_SCN_MEM_READ, 553 SectionKind::getMetadata(), "section_line"); 554 DwarfLineStrSection = Ctx->getCOFFSection( 555 ".debug_line_str", 556 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 557 COFF::IMAGE_SCN_MEM_READ, 558 SectionKind::getMetadata(), "section_line_str"); 559 DwarfFrameSection = Ctx->getCOFFSection( 560 ".debug_frame", 561 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 562 COFF::IMAGE_SCN_MEM_READ, 563 SectionKind::getMetadata()); 564 DwarfPubNamesSection = Ctx->getCOFFSection( 565 ".debug_pubnames", 566 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 567 COFF::IMAGE_SCN_MEM_READ, 568 SectionKind::getMetadata()); 569 DwarfPubTypesSection = Ctx->getCOFFSection( 570 ".debug_pubtypes", 571 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 572 COFF::IMAGE_SCN_MEM_READ, 573 SectionKind::getMetadata()); 574 DwarfGnuPubNamesSection = Ctx->getCOFFSection( 575 ".debug_gnu_pubnames", 576 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 577 COFF::IMAGE_SCN_MEM_READ, 578 SectionKind::getMetadata()); 579 DwarfGnuPubTypesSection = Ctx->getCOFFSection( 580 ".debug_gnu_pubtypes", 581 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 582 COFF::IMAGE_SCN_MEM_READ, 583 SectionKind::getMetadata()); 584 DwarfStrSection = Ctx->getCOFFSection( 585 ".debug_str", 586 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 587 COFF::IMAGE_SCN_MEM_READ, 588 SectionKind::getMetadata(), "info_string"); 589 DwarfStrOffSection = Ctx->getCOFFSection( 590 ".debug_str_offsets", 591 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 592 COFF::IMAGE_SCN_MEM_READ, 593 SectionKind::getMetadata(), "section_str_off"); 594 DwarfLocSection = Ctx->getCOFFSection( 595 ".debug_loc", 596 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 597 COFF::IMAGE_SCN_MEM_READ, 598 SectionKind::getMetadata(), "section_debug_loc"); 599 DwarfARangesSection = Ctx->getCOFFSection( 600 ".debug_aranges", 601 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 602 COFF::IMAGE_SCN_MEM_READ, 603 SectionKind::getMetadata()); 604 DwarfRangesSection = Ctx->getCOFFSection( 605 ".debug_ranges", 606 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 607 COFF::IMAGE_SCN_MEM_READ, 608 SectionKind::getMetadata(), "debug_range"); 609 DwarfMacinfoSection = Ctx->getCOFFSection( 610 ".debug_macinfo", 611 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 612 COFF::IMAGE_SCN_MEM_READ, 613 SectionKind::getMetadata(), "debug_macinfo"); 614 DwarfInfoDWOSection = Ctx->getCOFFSection( 615 ".debug_info.dwo", 616 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 617 COFF::IMAGE_SCN_MEM_READ, 618 SectionKind::getMetadata(), "section_info_dwo"); 619 DwarfTypesDWOSection = Ctx->getCOFFSection( 620 ".debug_types.dwo", 621 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 622 COFF::IMAGE_SCN_MEM_READ, 623 SectionKind::getMetadata(), "section_types_dwo"); 624 DwarfAbbrevDWOSection = Ctx->getCOFFSection( 625 ".debug_abbrev.dwo", 626 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 627 COFF::IMAGE_SCN_MEM_READ, 628 SectionKind::getMetadata(), "section_abbrev_dwo"); 629 DwarfStrDWOSection = Ctx->getCOFFSection( 630 ".debug_str.dwo", 631 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 632 COFF::IMAGE_SCN_MEM_READ, 633 SectionKind::getMetadata(), "skel_string"); 634 DwarfLineDWOSection = Ctx->getCOFFSection( 635 ".debug_line.dwo", 636 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 637 COFF::IMAGE_SCN_MEM_READ, 638 SectionKind::getMetadata()); 639 DwarfLocDWOSection = Ctx->getCOFFSection( 640 ".debug_loc.dwo", 641 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 642 COFF::IMAGE_SCN_MEM_READ, 643 SectionKind::getMetadata(), "skel_loc"); 644 DwarfStrOffDWOSection = Ctx->getCOFFSection( 645 ".debug_str_offsets.dwo", 646 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 647 COFF::IMAGE_SCN_MEM_READ, 648 SectionKind::getMetadata(), "section_str_off_dwo"); 649 DwarfAddrSection = Ctx->getCOFFSection( 650 ".debug_addr", 651 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 652 COFF::IMAGE_SCN_MEM_READ, 653 SectionKind::getMetadata(), "addr_sec"); 654 DwarfCUIndexSection = Ctx->getCOFFSection( 655 ".debug_cu_index", 656 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 657 COFF::IMAGE_SCN_MEM_READ, 658 SectionKind::getMetadata()); 659 DwarfTUIndexSection = Ctx->getCOFFSection( 660 ".debug_tu_index", 661 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 662 COFF::IMAGE_SCN_MEM_READ, 663 SectionKind::getMetadata()); 664 DwarfDebugNamesSection = Ctx->getCOFFSection( 665 ".debug_names", 666 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 667 COFF::IMAGE_SCN_MEM_READ, 668 SectionKind::getMetadata(), "debug_names_begin"); 669 DwarfAccelNamesSection = Ctx->getCOFFSection( 670 ".apple_names", 671 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 672 COFF::IMAGE_SCN_MEM_READ, 673 SectionKind::getMetadata(), "names_begin"); 674 DwarfAccelNamespaceSection = Ctx->getCOFFSection( 675 ".apple_namespaces", 676 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 677 COFF::IMAGE_SCN_MEM_READ, 678 SectionKind::getMetadata(), "namespac_begin"); 679 DwarfAccelTypesSection = Ctx->getCOFFSection( 680 ".apple_types", 681 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 682 COFF::IMAGE_SCN_MEM_READ, 683 SectionKind::getMetadata(), "types_begin"); 684 DwarfAccelObjCSection = Ctx->getCOFFSection( 685 ".apple_objc", 686 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 687 COFF::IMAGE_SCN_MEM_READ, 688 SectionKind::getMetadata(), "objc_begin"); 689 690 DrectveSection = Ctx->getCOFFSection( 691 ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE, 692 SectionKind::getMetadata()); 693 694 PDataSection = Ctx->getCOFFSection( 695 ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 696 SectionKind::getData()); 697 698 XDataSection = Ctx->getCOFFSection( 699 ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 700 SectionKind::getData()); 701 702 SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO, 703 SectionKind::getMetadata()); 704 705 GFIDsSection = Ctx->getCOFFSection(".gfids$y", 706 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 707 COFF::IMAGE_SCN_MEM_READ, 708 SectionKind::getMetadata()); 709 710 TLSDataSection = Ctx->getCOFFSection( 711 ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | 712 COFF::IMAGE_SCN_MEM_WRITE, 713 SectionKind::getData()); 714 715 StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps", 716 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 717 COFF::IMAGE_SCN_MEM_READ, 718 SectionKind::getReadOnly()); 719 } 720 721 void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) { 722 TextSection = Ctx->getWasmSection(".text", SectionKind::getText()); 723 DataSection = Ctx->getWasmSection(".data", SectionKind::getData()); 724 725 DwarfLineSection = 726 Ctx->getWasmSection(".debug_line", SectionKind::getMetadata()); 727 DwarfLineStrSection = 728 Ctx->getWasmSection(".debug_line_str", SectionKind::getMetadata()); 729 DwarfStrSection = 730 Ctx->getWasmSection(".debug_str", SectionKind::getMetadata()); 731 DwarfLocSection = 732 Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata()); 733 DwarfAbbrevSection = 734 Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata()); 735 DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata()); 736 DwarfRangesSection = 737 Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata()); 738 DwarfMacinfoSection = 739 Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata()); 740 DwarfAddrSection = Ctx->getWasmSection(".debug_addr", SectionKind::getMetadata()); 741 DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata()); 742 DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata()); 743 DwarfInfoSection = 744 Ctx->getWasmSection(".debug_info", SectionKind::getMetadata()); 745 DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata()); 746 DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata()); 747 DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata()); 748 749 // Wasm use data section for LSDA. 750 // TODO Consider putting each function's exception table in a separate 751 // section, as in -function-sections, to facilitate lld's --gc-section. 752 LSDASection = Ctx->getWasmSection(".rodata.gcc_except_table", 753 SectionKind::getReadOnlyWithRel()); 754 755 // TODO: Define more sections. 756 } 757 758 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, bool PIC, 759 MCContext &ctx, 760 bool LargeCodeModel) { 761 PositionIndependent = PIC; 762 Ctx = &ctx; 763 764 // Common. 765 CommDirectiveSupportsAlignment = true; 766 SupportsWeakOmittedEHFrame = true; 767 SupportsCompactUnwindWithoutEHFrame = false; 768 OmitDwarfIfHaveCompactUnwind = false; 769 770 FDECFIEncoding = dwarf::DW_EH_PE_absptr; 771 772 CompactUnwindDwarfEHFrameOnly = 0; 773 774 EHFrameSection = nullptr; // Created on demand. 775 CompactUnwindSection = nullptr; // Used only by selected targets. 776 DwarfAccelNamesSection = nullptr; // Used only by selected targets. 777 DwarfAccelObjCSection = nullptr; // Used only by selected targets. 778 DwarfAccelNamespaceSection = nullptr; // Used only by selected targets. 779 DwarfAccelTypesSection = nullptr; // Used only by selected targets. 780 781 TT = TheTriple; 782 783 switch (TT.getObjectFormat()) { 784 case Triple::MachO: 785 Env = IsMachO; 786 initMachOMCObjectFileInfo(TT); 787 break; 788 case Triple::COFF: 789 if (!TT.isOSWindows()) 790 report_fatal_error( 791 "Cannot initialize MC for non-Windows COFF object files."); 792 793 Env = IsCOFF; 794 initCOFFMCObjectFileInfo(TT); 795 break; 796 case Triple::ELF: 797 Env = IsELF; 798 initELFMCObjectFileInfo(TT, LargeCodeModel); 799 break; 800 case Triple::Wasm: 801 Env = IsWasm; 802 initWasmMCObjectFileInfo(TT); 803 break; 804 case Triple::UnknownObjectFormat: 805 report_fatal_error("Cannot initialize MC for unknown object file format."); 806 break; 807 } 808 } 809 810 MCSection *MCObjectFileInfo::getDwarfComdatSection(const char *Name, 811 uint64_t Hash) const { 812 switch (TT.getObjectFormat()) { 813 case Triple::ELF: 814 return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, ELF::SHF_GROUP, 0, 815 utostr(Hash)); 816 case Triple::MachO: 817 case Triple::COFF: 818 case Triple::Wasm: 819 case Triple::UnknownObjectFormat: 820 report_fatal_error("Cannot get DWARF comdat section for this object file " 821 "format: not implemented."); 822 break; 823 } 824 llvm_unreachable("Unknown ObjectFormatType"); 825 } 826 827 MCSection * 828 MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const { 829 if (Env != IsELF) 830 return StackSizesSection; 831 832 const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec); 833 unsigned Flags = ELF::SHF_LINK_ORDER; 834 StringRef GroupName; 835 if (const MCSymbol *Group = ElfSec.getGroup()) { 836 GroupName = Group->getName(); 837 Flags |= ELF::SHF_GROUP; 838 } 839 840 const MCSymbol *Link = TextSec.getBeginSymbol(); 841 auto It = StackSizesUniquing.insert({Link, StackSizesUniquing.size()}); 842 unsigned UniqueID = It.first->second; 843 844 return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0, 845 GroupName, UniqueID, cast<MCSymbolELF>(Link)); 846 } 847