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