1 //===-- MCObjectFileInfo.cpp - Object File Information --------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/MC/MCObjectFileInfo.h" 11 #include "llvm/ADT/StringExtras.h" 12 #include "llvm/ADT/Triple.h" 13 #include "llvm/MC/MCAsmInfo.h" 14 #include "llvm/MC/MCContext.h" 15 #include "llvm/MC/MCSection.h" 16 #include "llvm/MC/MCSectionCOFF.h" 17 #include "llvm/MC/MCSectionELF.h" 18 #include "llvm/MC/MCSectionMachO.h" 19 #include "llvm/Support/COFF.h" 20 21 using namespace llvm; 22 23 static bool useCompactUnwind(const Triple &T) { 24 // Only on darwin. 25 if (!T.isOSDarwin()) 26 return false; 27 28 // aarch64 always has it. 29 if (T.getArch() == Triple::aarch64) 30 return true; 31 32 // armv7k always has it. 33 if (T.isWatchABI()) 34 return true; 35 36 // Use it on newer version of OS X. 37 if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6)) 38 return true; 39 40 // And the iOS simulator. 41 if (T.isiOS() && 42 (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86)) 43 return true; 44 45 return false; 46 } 47 48 void MCObjectFileInfo::initMachOMCObjectFileInfo(Triple T) { 49 // MachO 50 SupportsWeakOmittedEHFrame = false; 51 52 EHFrameSection = Ctx->getMachOSection( 53 "__TEXT", "__eh_frame", 54 MachO::S_COALESCED | MachO::S_ATTR_NO_TOC | 55 MachO::S_ATTR_STRIP_STATIC_SYMS | MachO::S_ATTR_LIVE_SUPPORT, 56 SectionKind::getReadOnly()); 57 58 if (T.isOSDarwin() && T.getArch() == Triple::aarch64) 59 SupportsCompactUnwindWithoutEHFrame = true; 60 61 if (T.isWatchABI()) 62 OmitDwarfIfHaveCompactUnwind = true; 63 64 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel 65 | dwarf::DW_EH_PE_sdata4; 66 LSDAEncoding = FDECFIEncoding = dwarf::DW_EH_PE_pcrel; 67 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 68 dwarf::DW_EH_PE_sdata4; 69 70 // .comm doesn't support alignment before Leopard. 71 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5)) 72 CommDirectiveSupportsAlignment = false; 73 74 TextSection // .text 75 = Ctx->getMachOSection("__TEXT", "__text", 76 MachO::S_ATTR_PURE_INSTRUCTIONS, 77 SectionKind::getText()); 78 DataSection // .data 79 = Ctx->getMachOSection("__DATA", "__data", 0, SectionKind::getData()); 80 81 // BSSSection might not be expected initialized on msvc. 82 BSSSection = nullptr; 83 84 TLSDataSection // .tdata 85 = Ctx->getMachOSection("__DATA", "__thread_data", 86 MachO::S_THREAD_LOCAL_REGULAR, 87 SectionKind::getData()); 88 TLSBSSSection // .tbss 89 = Ctx->getMachOSection("__DATA", "__thread_bss", 90 MachO::S_THREAD_LOCAL_ZEROFILL, 91 SectionKind::getThreadBSS()); 92 93 // TODO: Verify datarel below. 94 TLSTLVSection // .tlv 95 = Ctx->getMachOSection("__DATA", "__thread_vars", 96 MachO::S_THREAD_LOCAL_VARIABLES, 97 SectionKind::getData()); 98 99 TLSThreadInitSection = Ctx->getMachOSection( 100 "__DATA", "__thread_init", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS, 101 SectionKind::getData()); 102 103 CStringSection // .cstring 104 = Ctx->getMachOSection("__TEXT", "__cstring", 105 MachO::S_CSTRING_LITERALS, 106 SectionKind::getMergeable1ByteCString()); 107 UStringSection 108 = Ctx->getMachOSection("__TEXT","__ustring", 0, 109 SectionKind::getMergeable2ByteCString()); 110 FourByteConstantSection // .literal4 111 = Ctx->getMachOSection("__TEXT", "__literal4", 112 MachO::S_4BYTE_LITERALS, 113 SectionKind::getMergeableConst4()); 114 EightByteConstantSection // .literal8 115 = Ctx->getMachOSection("__TEXT", "__literal8", 116 MachO::S_8BYTE_LITERALS, 117 SectionKind::getMergeableConst8()); 118 119 SixteenByteConstantSection // .literal16 120 = Ctx->getMachOSection("__TEXT", "__literal16", 121 MachO::S_16BYTE_LITERALS, 122 SectionKind::getMergeableConst16()); 123 124 ReadOnlySection // .const 125 = Ctx->getMachOSection("__TEXT", "__const", 0, 126 SectionKind::getReadOnly()); 127 128 // If the target is not powerpc, map the coal sections to the non-coal 129 // sections. 130 // 131 // "__TEXT/__textcoal_nt" => section "__TEXT/__text" 132 // "__TEXT/__const_coal" => section "__TEXT/__const" 133 // "__DATA/__datacoal_nt" => section "__DATA/__data" 134 Triple::ArchType ArchTy = T.getArch(); 135 136 if (ArchTy == Triple::ppc || ArchTy == Triple::ppc64) { 137 TextCoalSection 138 = Ctx->getMachOSection("__TEXT", "__textcoal_nt", 139 MachO::S_COALESCED | 140 MachO::S_ATTR_PURE_INSTRUCTIONS, 141 SectionKind::getText()); 142 ConstTextCoalSection 143 = Ctx->getMachOSection("__TEXT", "__const_coal", 144 MachO::S_COALESCED, 145 SectionKind::getReadOnly()); 146 DataCoalSection = Ctx->getMachOSection( 147 "__DATA", "__datacoal_nt", MachO::S_COALESCED, SectionKind::getData()); 148 } else { 149 TextCoalSection = TextSection; 150 ConstTextCoalSection = ReadOnlySection; 151 DataCoalSection = DataSection; 152 } 153 154 ConstDataSection // .const_data 155 = Ctx->getMachOSection("__DATA", "__const", 0, 156 SectionKind::getReadOnlyWithRel()); 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 if (RelocM == Reloc::Static) { 176 StaticCtorSection = Ctx->getMachOSection("__TEXT", "__constructor", 0, 177 SectionKind::getData()); 178 StaticDtorSection = Ctx->getMachOSection("__TEXT", "__destructor", 0, 179 SectionKind::getData()); 180 } else { 181 StaticCtorSection = Ctx->getMachOSection("__DATA", "__mod_init_func", 182 MachO::S_MOD_INIT_FUNC_POINTERS, 183 SectionKind::getData()); 184 StaticDtorSection = Ctx->getMachOSection("__DATA", "__mod_term_func", 185 MachO::S_MOD_TERM_FUNC_POINTERS, 186 SectionKind::getData()); 187 } 188 189 // Exception Handling. 190 LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0, 191 SectionKind::getReadOnlyWithRel()); 192 193 COFFDebugSymbolsSection = nullptr; 194 COFFDebugTypesSection = nullptr; 195 196 if (useCompactUnwind(T)) { 197 CompactUnwindSection = 198 Ctx->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG, 199 SectionKind::getReadOnly()); 200 201 if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86) 202 CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_X86_64_MODE_DWARF 203 else if (T.getArch() == Triple::aarch64) 204 CompactUnwindDwarfEHFrameOnly = 0x03000000; // UNWIND_ARM64_MODE_DWARF 205 else if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb) 206 CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_ARM_MODE_DWARF 207 } 208 209 // Debug Information. 210 DwarfAccelNamesSection = 211 Ctx->getMachOSection("__DWARF", "__apple_names", MachO::S_ATTR_DEBUG, 212 SectionKind::getMetadata(), "names_begin"); 213 DwarfAccelObjCSection = 214 Ctx->getMachOSection("__DWARF", "__apple_objc", MachO::S_ATTR_DEBUG, 215 SectionKind::getMetadata(), "objc_begin"); 216 // 16 character section limit... 217 DwarfAccelNamespaceSection = 218 Ctx->getMachOSection("__DWARF", "__apple_namespac", MachO::S_ATTR_DEBUG, 219 SectionKind::getMetadata(), "namespac_begin"); 220 DwarfAccelTypesSection = 221 Ctx->getMachOSection("__DWARF", "__apple_types", MachO::S_ATTR_DEBUG, 222 SectionKind::getMetadata(), "types_begin"); 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 DwarfFrameSection = 234 Ctx->getMachOSection("__DWARF", "__debug_frame", MachO::S_ATTR_DEBUG, 235 SectionKind::getMetadata()); 236 DwarfPubNamesSection = 237 Ctx->getMachOSection("__DWARF", "__debug_pubnames", MachO::S_ATTR_DEBUG, 238 SectionKind::getMetadata()); 239 DwarfPubTypesSection = 240 Ctx->getMachOSection("__DWARF", "__debug_pubtypes", MachO::S_ATTR_DEBUG, 241 SectionKind::getMetadata()); 242 DwarfGnuPubNamesSection = 243 Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn", MachO::S_ATTR_DEBUG, 244 SectionKind::getMetadata()); 245 DwarfGnuPubTypesSection = 246 Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt", MachO::S_ATTR_DEBUG, 247 SectionKind::getMetadata()); 248 DwarfStrSection = 249 Ctx->getMachOSection("__DWARF", "__debug_str", MachO::S_ATTR_DEBUG, 250 SectionKind::getMetadata(), "info_string"); 251 DwarfLocSection = 252 Ctx->getMachOSection("__DWARF", "__debug_loc", MachO::S_ATTR_DEBUG, 253 SectionKind::getMetadata(), "section_debug_loc"); 254 DwarfARangesSection = 255 Ctx->getMachOSection("__DWARF", "__debug_aranges", MachO::S_ATTR_DEBUG, 256 SectionKind::getMetadata()); 257 DwarfRangesSection = 258 Ctx->getMachOSection("__DWARF", "__debug_ranges", MachO::S_ATTR_DEBUG, 259 SectionKind::getMetadata(), "debug_range"); 260 DwarfMacinfoSection = 261 Ctx->getMachOSection("__DWARF", "__debug_macinfo", MachO::S_ATTR_DEBUG, 262 SectionKind::getMetadata(), "debug_macinfo"); 263 DwarfDebugInlineSection = 264 Ctx->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG, 265 SectionKind::getMetadata()); 266 DwarfCUIndexSection = 267 Ctx->getMachOSection("__DWARF", "__debug_cu_index", MachO::S_ATTR_DEBUG, 268 SectionKind::getMetadata()); 269 DwarfTUIndexSection = 270 Ctx->getMachOSection("__DWARF", "__debug_tu_index", MachO::S_ATTR_DEBUG, 271 SectionKind::getMetadata()); 272 StackMapSection = Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps", 273 0, SectionKind::getMetadata()); 274 275 FaultMapSection = Ctx->getMachOSection("__LLVM_FAULTMAPS", "__llvm_faultmaps", 276 0, SectionKind::getMetadata()); 277 278 TLSExtraDataSection = TLSTLVSection; 279 } 280 281 void MCObjectFileInfo::initELFMCObjectFileInfo(Triple T) { 282 switch (T.getArch()) { 283 case Triple::mips: 284 case Triple::mipsel: 285 FDECFIEncoding = dwarf::DW_EH_PE_sdata4; 286 break; 287 case Triple::mips64: 288 case Triple::mips64el: 289 FDECFIEncoding = dwarf::DW_EH_PE_sdata8; 290 break; 291 case Triple::x86_64: 292 FDECFIEncoding = dwarf::DW_EH_PE_pcrel | 293 ((CMModel == CodeModel::Large) ? dwarf::DW_EH_PE_sdata8 294 : dwarf::DW_EH_PE_sdata4); 295 break; 296 default: 297 FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 298 break; 299 } 300 301 switch (T.getArch()) { 302 case Triple::arm: 303 case Triple::armeb: 304 case Triple::thumb: 305 case Triple::thumbeb: 306 if (Ctx->getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM) 307 break; 308 // Fallthrough if not using EHABI 309 case Triple::ppc: 310 case Triple::x86: 311 PersonalityEncoding = (RelocM == Reloc::PIC_) 312 ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 313 : dwarf::DW_EH_PE_absptr; 314 LSDAEncoding = (RelocM == Reloc::PIC_) 315 ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 316 : dwarf::DW_EH_PE_absptr; 317 TTypeEncoding = (RelocM == Reloc::PIC_) 318 ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 319 : dwarf::DW_EH_PE_absptr; 320 break; 321 case Triple::x86_64: 322 if (RelocM == Reloc::PIC_) { 323 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 324 ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium) 325 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); 326 LSDAEncoding = dwarf::DW_EH_PE_pcrel | 327 (CMModel == CodeModel::Small 328 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); 329 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 330 ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium) 331 ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); 332 } else { 333 PersonalityEncoding = 334 (CMModel == CodeModel::Small || CMModel == CodeModel::Medium) 335 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 336 LSDAEncoding = (CMModel == CodeModel::Small) 337 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 338 TTypeEncoding = (CMModel == CodeModel::Small) 339 ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; 340 } 341 break; 342 case Triple::aarch64: 343 case Triple::aarch64_be: 344 // The small model guarantees static code/data size < 4GB, but not where it 345 // will be in memory. Most of these could end up >2GB away so even a signed 346 // pc-relative 32-bit address is insufficient, theoretically. 347 if (RelocM == Reloc::PIC_) { 348 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 349 dwarf::DW_EH_PE_sdata8; 350 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8; 351 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 352 dwarf::DW_EH_PE_sdata8; 353 } else { 354 PersonalityEncoding = dwarf::DW_EH_PE_absptr; 355 LSDAEncoding = dwarf::DW_EH_PE_absptr; 356 TTypeEncoding = dwarf::DW_EH_PE_absptr; 357 } 358 break; 359 case Triple::mips: 360 case Triple::mipsel: 361 case Triple::mips64: 362 case Triple::mips64el: 363 // MIPS uses indirect pointer to refer personality functions and types, so 364 // that the eh_frame section can be read-only. DW.ref.personality will be 365 // generated for relocation. 366 PersonalityEncoding = dwarf::DW_EH_PE_indirect; 367 // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't 368 // identify N64 from just a triple. 369 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 370 dwarf::DW_EH_PE_sdata4; 371 // We don't support PC-relative LSDA references in GAS so we use the default 372 // DW_EH_PE_absptr for those. 373 break; 374 case Triple::ppc64: 375 case Triple::ppc64le: 376 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 377 dwarf::DW_EH_PE_udata8; 378 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8; 379 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 380 dwarf::DW_EH_PE_udata8; 381 break; 382 case Triple::sparcel: 383 case Triple::sparc: 384 if (RelocM == Reloc::PIC_) { 385 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 386 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 387 dwarf::DW_EH_PE_sdata4; 388 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 389 dwarf::DW_EH_PE_sdata4; 390 } else { 391 LSDAEncoding = dwarf::DW_EH_PE_absptr; 392 PersonalityEncoding = dwarf::DW_EH_PE_absptr; 393 TTypeEncoding = dwarf::DW_EH_PE_absptr; 394 } 395 break; 396 case Triple::sparcv9: 397 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 398 if (RelocM == Reloc::PIC_) { 399 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 400 dwarf::DW_EH_PE_sdata4; 401 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 402 dwarf::DW_EH_PE_sdata4; 403 } else { 404 PersonalityEncoding = dwarf::DW_EH_PE_absptr; 405 TTypeEncoding = dwarf::DW_EH_PE_absptr; 406 } 407 break; 408 case Triple::systemz: 409 // All currently-defined code models guarantee that 4-byte PC-relative 410 // values will be in range. 411 if (RelocM == Reloc::PIC_) { 412 PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 413 dwarf::DW_EH_PE_sdata4; 414 LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; 415 TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | 416 dwarf::DW_EH_PE_sdata4; 417 } else { 418 PersonalityEncoding = dwarf::DW_EH_PE_absptr; 419 LSDAEncoding = dwarf::DW_EH_PE_absptr; 420 TTypeEncoding = dwarf::DW_EH_PE_absptr; 421 } 422 break; 423 default: 424 break; 425 } 426 427 unsigned EHSectionType = T.getArch() == Triple::x86_64 428 ? ELF::SHT_X86_64_UNWIND 429 : ELF::SHT_PROGBITS; 430 431 // Solaris requires different flags for .eh_frame to seemingly every other 432 // platform. 433 unsigned EHSectionFlags = ELF::SHF_ALLOC; 434 if (T.isOSSolaris() && T.getArch() != Triple::x86_64) 435 EHSectionFlags |= ELF::SHF_WRITE; 436 437 // ELF 438 BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS, 439 ELF::SHF_WRITE | ELF::SHF_ALLOC); 440 441 TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS, 442 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC); 443 444 DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS, 445 ELF::SHF_WRITE | ELF::SHF_ALLOC); 446 447 ReadOnlySection = 448 Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 449 450 TLSDataSection = 451 Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS, 452 ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE); 453 454 TLSBSSSection = Ctx->getELFSection( 455 ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE); 456 457 DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS, 458 ELF::SHF_ALLOC | ELF::SHF_WRITE); 459 460 MergeableConst4Section = 461 Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS, 462 ELF::SHF_ALLOC | ELF::SHF_MERGE, 4, ""); 463 464 MergeableConst8Section = 465 Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS, 466 ELF::SHF_ALLOC | ELF::SHF_MERGE, 8, ""); 467 468 MergeableConst16Section = 469 Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS, 470 ELF::SHF_ALLOC | ELF::SHF_MERGE, 16, ""); 471 472 MergeableConst32Section = 473 Ctx->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS, 474 ELF::SHF_ALLOC | ELF::SHF_MERGE, 32, ""); 475 476 StaticCtorSection = Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS, 477 ELF::SHF_ALLOC | ELF::SHF_WRITE); 478 479 StaticDtorSection = Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS, 480 ELF::SHF_ALLOC | ELF::SHF_WRITE); 481 482 // Exception Handling Sections. 483 484 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though 485 // it contains relocatable pointers. In PIC mode, this is probably a big 486 // runtime hit for C++ apps. Either the contents of the LSDA need to be 487 // adjusted or this should be a data section. 488 LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS, 489 ELF::SHF_ALLOC); 490 491 COFFDebugSymbolsSection = nullptr; 492 COFFDebugTypesSection = nullptr; 493 494 // Debug Info Sections. 495 DwarfAbbrevSection = Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0, 496 "section_abbrev"); 497 DwarfInfoSection = 498 Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0, "section_info"); 499 DwarfLineSection = Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0); 500 DwarfFrameSection = Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0); 501 DwarfPubNamesSection = 502 Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0); 503 DwarfPubTypesSection = 504 Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0); 505 DwarfGnuPubNamesSection = 506 Ctx->getELFSection(".debug_gnu_pubnames", ELF::SHT_PROGBITS, 0); 507 DwarfGnuPubTypesSection = 508 Ctx->getELFSection(".debug_gnu_pubtypes", ELF::SHT_PROGBITS, 0); 509 DwarfStrSection = 510 Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS, 511 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); 512 DwarfLocSection = Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0); 513 DwarfARangesSection = 514 Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0); 515 DwarfRangesSection = 516 Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0, "debug_range"); 517 DwarfMacinfoSection = Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 518 0, "debug_macinfo"); 519 520 // DWARF5 Experimental Debug Info 521 522 // Accelerator Tables 523 DwarfAccelNamesSection = 524 Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0, "names_begin"); 525 DwarfAccelObjCSection = 526 Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0, "objc_begin"); 527 DwarfAccelNamespaceSection = Ctx->getELFSection( 528 ".apple_namespaces", ELF::SHT_PROGBITS, 0, "namespac_begin"); 529 DwarfAccelTypesSection = 530 Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0, "types_begin"); 531 532 // Fission Sections 533 DwarfInfoDWOSection = 534 Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0); 535 DwarfTypesDWOSection = 536 Ctx->getELFSection(".debug_types.dwo", ELF::SHT_PROGBITS, 0); 537 DwarfAbbrevDWOSection = 538 Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0); 539 DwarfStrDWOSection = 540 Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS, 541 ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); 542 DwarfLineDWOSection = 543 Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0); 544 DwarfLocDWOSection = 545 Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0, "skel_loc"); 546 DwarfStrOffDWOSection = 547 Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0); 548 DwarfAddrSection = 549 Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0, "addr_sec"); 550 551 // DWP Sections 552 DwarfCUIndexSection = 553 Ctx->getELFSection(".debug_cu_index", ELF::SHT_PROGBITS, 0); 554 DwarfTUIndexSection = 555 Ctx->getELFSection(".debug_tu_index", ELF::SHT_PROGBITS, 0); 556 557 StackMapSection = 558 Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 559 560 FaultMapSection = 561 Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 562 563 EHFrameSection = 564 Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags); 565 } 566 567 void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) { 568 EHFrameSection = Ctx->getCOFFSection( 569 ".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 570 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 571 SectionKind::getData()); 572 573 bool IsWoA = T.getArch() == Triple::arm || T.getArch() == Triple::thumb; 574 575 CommDirectiveSupportsAlignment = true; 576 577 // COFF 578 BSSSection = Ctx->getCOFFSection( 579 ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 580 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 581 SectionKind::getBSS()); 582 TextSection = Ctx->getCOFFSection( 583 ".text", 584 (IsWoA ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) | 585 COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE | 586 COFF::IMAGE_SCN_MEM_READ, 587 SectionKind::getText()); 588 DataSection = Ctx->getCOFFSection( 589 ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | 590 COFF::IMAGE_SCN_MEM_WRITE, 591 SectionKind::getData()); 592 ReadOnlySection = Ctx->getCOFFSection( 593 ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 594 SectionKind::getReadOnly()); 595 596 if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { 597 StaticCtorSection = 598 Ctx->getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 599 COFF::IMAGE_SCN_MEM_READ, 600 SectionKind::getReadOnly()); 601 StaticDtorSection = 602 Ctx->getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 603 COFF::IMAGE_SCN_MEM_READ, 604 SectionKind::getReadOnly()); 605 } else { 606 StaticCtorSection = Ctx->getCOFFSection( 607 ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 608 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 609 SectionKind::getData()); 610 StaticDtorSection = Ctx->getCOFFSection( 611 ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 612 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, 613 SectionKind::getData()); 614 } 615 616 // FIXME: We're emitting LSDA info into a readonly section on COFF, even 617 // though it contains relocatable pointers. In PIC mode, this is probably a 618 // big runtime hit for C++ apps. Either the contents of the LSDA need to be 619 // adjusted or this should be a data section. 620 if (T.getArch() == Triple::x86_64) { 621 // On Windows 64 with SEH, the LSDA is emitted into the .xdata section 622 LSDASection = nullptr; 623 } else { 624 LSDASection = Ctx->getCOFFSection(".gcc_except_table", 625 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 626 COFF::IMAGE_SCN_MEM_READ, 627 SectionKind::getReadOnly()); 628 } 629 630 // Debug info. 631 COFFDebugSymbolsSection = 632 Ctx->getCOFFSection(".debug$S", (COFF::IMAGE_SCN_MEM_DISCARDABLE | 633 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 634 COFF::IMAGE_SCN_MEM_READ), 635 SectionKind::getMetadata()); 636 COFFDebugTypesSection = 637 Ctx->getCOFFSection(".debug$T", (COFF::IMAGE_SCN_MEM_DISCARDABLE | 638 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 639 COFF::IMAGE_SCN_MEM_READ), 640 SectionKind::getMetadata()); 641 642 DwarfAbbrevSection = Ctx->getCOFFSection( 643 ".debug_abbrev", 644 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 645 COFF::IMAGE_SCN_MEM_READ, 646 SectionKind::getMetadata(), "section_abbrev"); 647 DwarfInfoSection = Ctx->getCOFFSection( 648 ".debug_info", 649 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 650 COFF::IMAGE_SCN_MEM_READ, 651 SectionKind::getMetadata(), "section_info"); 652 DwarfLineSection = Ctx->getCOFFSection( 653 ".debug_line", 654 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 655 COFF::IMAGE_SCN_MEM_READ, 656 SectionKind::getMetadata(), "section_line"); 657 658 DwarfFrameSection = Ctx->getCOFFSection( 659 ".debug_frame", 660 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 661 COFF::IMAGE_SCN_MEM_READ, 662 SectionKind::getMetadata()); 663 DwarfPubNamesSection = Ctx->getCOFFSection( 664 ".debug_pubnames", 665 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 666 COFF::IMAGE_SCN_MEM_READ, 667 SectionKind::getMetadata()); 668 DwarfPubTypesSection = Ctx->getCOFFSection( 669 ".debug_pubtypes", 670 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 671 COFF::IMAGE_SCN_MEM_READ, 672 SectionKind::getMetadata()); 673 DwarfGnuPubNamesSection = Ctx->getCOFFSection( 674 ".debug_gnu_pubnames", 675 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 676 COFF::IMAGE_SCN_MEM_READ, 677 SectionKind::getMetadata()); 678 DwarfGnuPubTypesSection = Ctx->getCOFFSection( 679 ".debug_gnu_pubtypes", 680 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 681 COFF::IMAGE_SCN_MEM_READ, 682 SectionKind::getMetadata()); 683 DwarfStrSection = Ctx->getCOFFSection( 684 ".debug_str", 685 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 686 COFF::IMAGE_SCN_MEM_READ, 687 SectionKind::getMetadata(), "info_string"); 688 DwarfLocSection = Ctx->getCOFFSection( 689 ".debug_loc", 690 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 691 COFF::IMAGE_SCN_MEM_READ, 692 SectionKind::getMetadata(), "section_debug_loc"); 693 DwarfARangesSection = Ctx->getCOFFSection( 694 ".debug_aranges", 695 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 696 COFF::IMAGE_SCN_MEM_READ, 697 SectionKind::getMetadata()); 698 DwarfRangesSection = Ctx->getCOFFSection( 699 ".debug_ranges", 700 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 701 COFF::IMAGE_SCN_MEM_READ, 702 SectionKind::getMetadata(), "debug_range"); 703 DwarfMacinfoSection = Ctx->getCOFFSection( 704 ".debug_macinfo", 705 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 706 COFF::IMAGE_SCN_MEM_READ, 707 SectionKind::getMetadata(), "debug_macinfo"); 708 DwarfInfoDWOSection = Ctx->getCOFFSection( 709 ".debug_info.dwo", 710 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 711 COFF::IMAGE_SCN_MEM_READ, 712 SectionKind::getMetadata(), "section_info_dwo"); 713 DwarfTypesDWOSection = Ctx->getCOFFSection( 714 ".debug_types.dwo", 715 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 716 COFF::IMAGE_SCN_MEM_READ, 717 SectionKind::getMetadata(), "section_types_dwo"); 718 DwarfAbbrevDWOSection = Ctx->getCOFFSection( 719 ".debug_abbrev.dwo", 720 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 721 COFF::IMAGE_SCN_MEM_READ, 722 SectionKind::getMetadata(), "section_abbrev_dwo"); 723 DwarfStrDWOSection = Ctx->getCOFFSection( 724 ".debug_str.dwo", 725 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 726 COFF::IMAGE_SCN_MEM_READ, 727 SectionKind::getMetadata(), "skel_string"); 728 DwarfLineDWOSection = Ctx->getCOFFSection( 729 ".debug_line.dwo", 730 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 731 COFF::IMAGE_SCN_MEM_READ, 732 SectionKind::getMetadata()); 733 DwarfLocDWOSection = Ctx->getCOFFSection( 734 ".debug_loc.dwo", 735 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 736 COFF::IMAGE_SCN_MEM_READ, 737 SectionKind::getMetadata(), "skel_loc"); 738 DwarfStrOffDWOSection = Ctx->getCOFFSection( 739 ".debug_str_offsets.dwo", 740 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 741 COFF::IMAGE_SCN_MEM_READ, 742 SectionKind::getMetadata()); 743 DwarfAddrSection = Ctx->getCOFFSection( 744 ".debug_addr", 745 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 746 COFF::IMAGE_SCN_MEM_READ, 747 SectionKind::getMetadata(), "addr_sec"); 748 DwarfCUIndexSection = Ctx->getCOFFSection( 749 ".debug_cu_index", 750 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 751 COFF::IMAGE_SCN_MEM_READ, 752 SectionKind::getMetadata()); 753 DwarfTUIndexSection = Ctx->getCOFFSection( 754 ".debug_tu_index", 755 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 756 COFF::IMAGE_SCN_MEM_READ, 757 SectionKind::getMetadata()); 758 DwarfAccelNamesSection = Ctx->getCOFFSection( 759 ".apple_names", 760 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 761 COFF::IMAGE_SCN_MEM_READ, 762 SectionKind::getMetadata(), "names_begin"); 763 DwarfAccelNamespaceSection = Ctx->getCOFFSection( 764 ".apple_namespaces", 765 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 766 COFF::IMAGE_SCN_MEM_READ, 767 SectionKind::getMetadata(), "namespac_begin"); 768 DwarfAccelTypesSection = Ctx->getCOFFSection( 769 ".apple_types", 770 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 771 COFF::IMAGE_SCN_MEM_READ, 772 SectionKind::getMetadata(), "types_begin"); 773 DwarfAccelObjCSection = Ctx->getCOFFSection( 774 ".apple_objc", 775 COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 776 COFF::IMAGE_SCN_MEM_READ, 777 SectionKind::getMetadata(), "objc_begin"); 778 779 DrectveSection = Ctx->getCOFFSection( 780 ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE, 781 SectionKind::getMetadata()); 782 783 PDataSection = Ctx->getCOFFSection( 784 ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 785 SectionKind::getData()); 786 787 XDataSection = Ctx->getCOFFSection( 788 ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, 789 SectionKind::getData()); 790 791 SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO, 792 SectionKind::getMetadata()); 793 794 TLSDataSection = Ctx->getCOFFSection( 795 ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | 796 COFF::IMAGE_SCN_MEM_WRITE, 797 SectionKind::getData()); 798 799 StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps", 800 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 801 COFF::IMAGE_SCN_MEM_READ, 802 SectionKind::getReadOnly()); 803 } 804 805 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, 806 Reloc::Model relocm, 807 CodeModel::Model cm, 808 MCContext &ctx) { 809 RelocM = relocm; 810 CMModel = cm; 811 Ctx = &ctx; 812 813 // Common. 814 CommDirectiveSupportsAlignment = true; 815 SupportsWeakOmittedEHFrame = true; 816 SupportsCompactUnwindWithoutEHFrame = false; 817 OmitDwarfIfHaveCompactUnwind = false; 818 819 PersonalityEncoding = LSDAEncoding = FDECFIEncoding = TTypeEncoding = 820 dwarf::DW_EH_PE_absptr; 821 822 CompactUnwindDwarfEHFrameOnly = 0; 823 824 EHFrameSection = nullptr; // Created on demand. 825 CompactUnwindSection = nullptr; // Used only by selected targets. 826 DwarfAccelNamesSection = nullptr; // Used only by selected targets. 827 DwarfAccelObjCSection = nullptr; // Used only by selected targets. 828 DwarfAccelNamespaceSection = nullptr; // Used only by selected targets. 829 DwarfAccelTypesSection = nullptr; // Used only by selected targets. 830 831 TT = TheTriple; 832 833 switch (TT.getObjectFormat()) { 834 case Triple::MachO: 835 Env = IsMachO; 836 initMachOMCObjectFileInfo(TT); 837 break; 838 case Triple::COFF: 839 if (!TT.isOSWindows()) 840 report_fatal_error( 841 "Cannot initialize MC for non-Windows COFF object files."); 842 843 Env = IsCOFF; 844 initCOFFMCObjectFileInfo(TT); 845 break; 846 case Triple::ELF: 847 Env = IsELF; 848 initELFMCObjectFileInfo(TT); 849 break; 850 case Triple::UnknownObjectFormat: 851 report_fatal_error("Cannot initialize MC for unknown object file format."); 852 break; 853 } 854 } 855 856 void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model RM, 857 CodeModel::Model CM, 858 MCContext &ctx) { 859 InitMCObjectFileInfo(Triple(TT), RM, CM, ctx); 860 } 861 862 MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const { 863 return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP, 864 0, utostr(Hash)); 865 } 866