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