1 //===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===// 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 "ObjectFileELF.h" 11 12 #include <cassert> 13 #include <algorithm> 14 #include <unordered_map> 15 16 #include "lldb/Core/ArchSpec.h" 17 #include "lldb/Core/DataBuffer.h" 18 #include "lldb/Core/Error.h" 19 #include "lldb/Core/FileSpecList.h" 20 #include "lldb/Core/Log.h" 21 #include "lldb/Core/Module.h" 22 #include "lldb/Core/ModuleSpec.h" 23 #include "lldb/Core/PluginManager.h" 24 #include "lldb/Core/Section.h" 25 #include "lldb/Core/Stream.h" 26 #include "lldb/Core/Timer.h" 27 #include "lldb/Symbol/DWARFCallFrameInfo.h" 28 #include "lldb/Symbol/SymbolContext.h" 29 #include "lldb/Target/SectionLoadList.h" 30 #include "lldb/Target/Target.h" 31 32 #include "llvm/ADT/PointerUnion.h" 33 #include "llvm/ADT/StringRef.h" 34 #include "llvm/Support/MathExtras.h" 35 36 #define CASE_AND_STREAM(s, def, width) \ 37 case def: s->Printf("%-*s", width, #def); break; 38 39 using namespace lldb; 40 using namespace lldb_private; 41 using namespace elf; 42 using namespace llvm::ELF; 43 44 namespace { 45 46 // ELF note owner definitions 47 const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD"; 48 const char *const LLDB_NT_OWNER_GNU = "GNU"; 49 const char *const LLDB_NT_OWNER_NETBSD = "NetBSD"; 50 const char *const LLDB_NT_OWNER_CSR = "csr"; 51 const char *const LLDB_NT_OWNER_ANDROID = "Android"; 52 const char *const LLDB_NT_OWNER_CORE = "CORE"; 53 const char *const LLDB_NT_OWNER_LINUX = "LINUX"; 54 55 // ELF note type definitions 56 const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01; 57 const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4; 58 59 const elf_word LLDB_NT_GNU_ABI_TAG = 0x01; 60 const elf_word LLDB_NT_GNU_ABI_SIZE = 16; 61 62 const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03; 63 64 const elf_word LLDB_NT_NETBSD_ABI_TAG = 0x01; 65 const elf_word LLDB_NT_NETBSD_ABI_SIZE = 4; 66 67 // GNU ABI note OS constants 68 const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00; 69 const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01; 70 const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02; 71 72 // LLDB_NT_OWNER_CORE and LLDB_NT_OWNER_LINUX note contants 73 #define NT_PRSTATUS 1 74 #define NT_PRFPREG 2 75 #define NT_PRPSINFO 3 76 #define NT_TASKSTRUCT 4 77 #define NT_AUXV 6 78 #define NT_SIGINFO 0x53494749 79 #define NT_FILE 0x46494c45 80 #define NT_PRXFPREG 0x46e62b7f 81 #define NT_PPC_VMX 0x100 82 #define NT_PPC_SPE 0x101 83 #define NT_PPC_VSX 0x102 84 #define NT_386_TLS 0x200 85 #define NT_386_IOPERM 0x201 86 #define NT_X86_XSTATE 0x202 87 #define NT_S390_HIGH_GPRS 0x300 88 #define NT_S390_TIMER 0x301 89 #define NT_S390_TODCMP 0x302 90 #define NT_S390_TODPREG 0x303 91 #define NT_S390_CTRS 0x304 92 #define NT_S390_PREFIX 0x305 93 #define NT_S390_LAST_BREAK 0x306 94 #define NT_S390_SYSTEM_CALL 0x307 95 #define NT_S390_TDB 0x308 96 #define NT_S390_VXRS_LOW 0x309 97 #define NT_S390_VXRS_HIGH 0x30a 98 #define NT_ARM_VFP 0x400 99 #define NT_ARM_TLS 0x401 100 #define NT_ARM_HW_BREAK 0x402 101 #define NT_ARM_HW_WATCH 0x403 102 #define NT_ARM_SYSTEM_CALL 0x404 103 #define NT_METAG_CBUF 0x500 104 #define NT_METAG_RPIPE 0x501 105 #define NT_METAG_TLS 0x502 106 107 //===----------------------------------------------------------------------===// 108 /// @class ELFRelocation 109 /// @brief Generic wrapper for ELFRel and ELFRela. 110 /// 111 /// This helper class allows us to parse both ELFRel and ELFRela relocation 112 /// entries in a generic manner. 113 class ELFRelocation 114 { 115 public: 116 117 /// Constructs an ELFRelocation entry with a personality as given by @p 118 /// type. 119 /// 120 /// @param type Either DT_REL or DT_RELA. Any other value is invalid. 121 ELFRelocation(unsigned type); 122 123 ~ELFRelocation(); 124 125 bool 126 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 127 128 static unsigned 129 RelocType32(const ELFRelocation &rel); 130 131 static unsigned 132 RelocType64(const ELFRelocation &rel); 133 134 static unsigned 135 RelocSymbol32(const ELFRelocation &rel); 136 137 static unsigned 138 RelocSymbol64(const ELFRelocation &rel); 139 140 static unsigned 141 RelocOffset32(const ELFRelocation &rel); 142 143 static unsigned 144 RelocOffset64(const ELFRelocation &rel); 145 146 static unsigned 147 RelocAddend32(const ELFRelocation &rel); 148 149 static unsigned 150 RelocAddend64(const ELFRelocation &rel); 151 152 private: 153 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion; 154 155 RelocUnion reloc; 156 }; 157 158 ELFRelocation::ELFRelocation(unsigned type) 159 { 160 if (type == DT_REL || type == SHT_REL) 161 reloc = new ELFRel(); 162 else if (type == DT_RELA || type == SHT_RELA) 163 reloc = new ELFRela(); 164 else { 165 assert(false && "unexpected relocation type"); 166 reloc = static_cast<ELFRel*>(NULL); 167 } 168 } 169 170 ELFRelocation::~ELFRelocation() 171 { 172 if (reloc.is<ELFRel*>()) 173 delete reloc.get<ELFRel*>(); 174 else 175 delete reloc.get<ELFRela*>(); 176 } 177 178 bool 179 ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset) 180 { 181 if (reloc.is<ELFRel*>()) 182 return reloc.get<ELFRel*>()->Parse(data, offset); 183 else 184 return reloc.get<ELFRela*>()->Parse(data, offset); 185 } 186 187 unsigned 188 ELFRelocation::RelocType32(const ELFRelocation &rel) 189 { 190 if (rel.reloc.is<ELFRel*>()) 191 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>()); 192 else 193 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>()); 194 } 195 196 unsigned 197 ELFRelocation::RelocType64(const ELFRelocation &rel) 198 { 199 if (rel.reloc.is<ELFRel*>()) 200 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>()); 201 else 202 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>()); 203 } 204 205 unsigned 206 ELFRelocation::RelocSymbol32(const ELFRelocation &rel) 207 { 208 if (rel.reloc.is<ELFRel*>()) 209 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>()); 210 else 211 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>()); 212 } 213 214 unsigned 215 ELFRelocation::RelocSymbol64(const ELFRelocation &rel) 216 { 217 if (rel.reloc.is<ELFRel*>()) 218 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>()); 219 else 220 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>()); 221 } 222 223 unsigned 224 ELFRelocation::RelocOffset32(const ELFRelocation &rel) 225 { 226 if (rel.reloc.is<ELFRel*>()) 227 return rel.reloc.get<ELFRel*>()->r_offset; 228 else 229 return rel.reloc.get<ELFRela*>()->r_offset; 230 } 231 232 unsigned 233 ELFRelocation::RelocOffset64(const ELFRelocation &rel) 234 { 235 if (rel.reloc.is<ELFRel*>()) 236 return rel.reloc.get<ELFRel*>()->r_offset; 237 else 238 return rel.reloc.get<ELFRela*>()->r_offset; 239 } 240 241 unsigned 242 ELFRelocation::RelocAddend32(const ELFRelocation &rel) 243 { 244 if (rel.reloc.is<ELFRel*>()) 245 return 0; 246 else 247 return rel.reloc.get<ELFRela*>()->r_addend; 248 } 249 250 unsigned 251 ELFRelocation::RelocAddend64(const ELFRelocation &rel) 252 { 253 if (rel.reloc.is<ELFRel*>()) 254 return 0; 255 else 256 return rel.reloc.get<ELFRela*>()->r_addend; 257 } 258 259 } // end anonymous namespace 260 261 bool 262 ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) 263 { 264 // Read all fields. 265 if (data.GetU32(offset, &n_namesz, 3) == NULL) 266 return false; 267 268 // The name field is required to be nul-terminated, and n_namesz 269 // includes the terminating nul in observed implementations (contrary 270 // to the ELF-64 spec). A special case is needed for cores generated 271 // by some older Linux versions, which write a note named "CORE" 272 // without a nul terminator and n_namesz = 4. 273 if (n_namesz == 4) 274 { 275 char buf[4]; 276 if (data.ExtractBytes (*offset, 4, data.GetByteOrder(), buf) != 4) 277 return false; 278 if (strncmp (buf, "CORE", 4) == 0) 279 { 280 n_name = "CORE"; 281 *offset += 4; 282 return true; 283 } 284 } 285 286 const char *cstr = data.GetCStr(offset, llvm::alignTo (n_namesz, 4)); 287 if (cstr == NULL) 288 { 289 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS)); 290 if (log) 291 log->Printf("Failed to parse note name lacking nul terminator"); 292 293 return false; 294 } 295 n_name = cstr; 296 return true; 297 } 298 299 static uint32_t 300 kalimbaVariantFromElfFlags(const elf::elf_word e_flags) 301 { 302 const uint32_t dsp_rev = e_flags & 0xFF; 303 uint32_t kal_arch_variant = LLDB_INVALID_CPUTYPE; 304 switch(dsp_rev) 305 { 306 // TODO(mg11) Support more variants 307 case 10: 308 kal_arch_variant = llvm::Triple::KalimbaSubArch_v3; 309 break; 310 case 14: 311 kal_arch_variant = llvm::Triple::KalimbaSubArch_v4; 312 break; 313 case 17: 314 case 20: 315 kal_arch_variant = llvm::Triple::KalimbaSubArch_v5; 316 break; 317 default: 318 break; 319 } 320 return kal_arch_variant; 321 } 322 323 static uint32_t 324 mipsVariantFromElfFlags(const elf::elf_word e_flags, uint32_t endian) 325 { 326 const uint32_t mips_arch = e_flags & llvm::ELF::EF_MIPS_ARCH; 327 uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown; 328 329 switch (mips_arch) 330 { 331 case llvm::ELF::EF_MIPS_ARCH_1: 332 case llvm::ELF::EF_MIPS_ARCH_2: 333 case llvm::ELF::EF_MIPS_ARCH_32: 334 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el : ArchSpec::eMIPSSubType_mips32; 335 case llvm::ELF::EF_MIPS_ARCH_32R2: 336 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el : ArchSpec::eMIPSSubType_mips32r2; 337 case llvm::ELF::EF_MIPS_ARCH_32R6: 338 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el : ArchSpec::eMIPSSubType_mips32r6; 339 case llvm::ELF::EF_MIPS_ARCH_3: 340 case llvm::ELF::EF_MIPS_ARCH_4: 341 case llvm::ELF::EF_MIPS_ARCH_5: 342 case llvm::ELF::EF_MIPS_ARCH_64: 343 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el : ArchSpec::eMIPSSubType_mips64; 344 case llvm::ELF::EF_MIPS_ARCH_64R2: 345 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el : ArchSpec::eMIPSSubType_mips64r2; 346 case llvm::ELF::EF_MIPS_ARCH_64R6: 347 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el : ArchSpec::eMIPSSubType_mips64r6; 348 default: 349 break; 350 } 351 352 return arch_variant; 353 } 354 355 static uint32_t 356 subTypeFromElfHeader(const elf::ELFHeader& header) 357 { 358 if (header.e_machine == llvm::ELF::EM_MIPS) 359 return mipsVariantFromElfFlags (header.e_flags, 360 header.e_ident[EI_DATA]); 361 362 return 363 llvm::ELF::EM_CSR_KALIMBA == header.e_machine ? 364 kalimbaVariantFromElfFlags(header.e_flags) : 365 LLDB_INVALID_CPUTYPE; 366 } 367 368 //! The kalimba toolchain identifies a code section as being 369 //! one with the SHT_PROGBITS set in the section sh_type and the top 370 //! bit in the 32-bit address field set. 371 static lldb::SectionType 372 kalimbaSectionType( 373 const elf::ELFHeader& header, 374 const elf::ELFSectionHeader& sect_hdr) 375 { 376 if (llvm::ELF::EM_CSR_KALIMBA != header.e_machine) 377 { 378 return eSectionTypeOther; 379 } 380 381 if (llvm::ELF::SHT_NOBITS == sect_hdr.sh_type) 382 { 383 return eSectionTypeZeroFill; 384 } 385 386 if (llvm::ELF::SHT_PROGBITS == sect_hdr.sh_type) 387 { 388 const lldb::addr_t KAL_CODE_BIT = 1 << 31; 389 return KAL_CODE_BIT & sect_hdr.sh_addr ? 390 eSectionTypeCode : eSectionTypeData; 391 } 392 393 return eSectionTypeOther; 394 } 395 396 // Arbitrary constant used as UUID prefix for core files. 397 const uint32_t 398 ObjectFileELF::g_core_uuid_magic(0xE210C); 399 400 //------------------------------------------------------------------ 401 // Static methods. 402 //------------------------------------------------------------------ 403 void 404 ObjectFileELF::Initialize() 405 { 406 PluginManager::RegisterPlugin(GetPluginNameStatic(), 407 GetPluginDescriptionStatic(), 408 CreateInstance, 409 CreateMemoryInstance, 410 GetModuleSpecifications); 411 } 412 413 void 414 ObjectFileELF::Terminate() 415 { 416 PluginManager::UnregisterPlugin(CreateInstance); 417 } 418 419 lldb_private::ConstString 420 ObjectFileELF::GetPluginNameStatic() 421 { 422 static ConstString g_name("elf"); 423 return g_name; 424 } 425 426 const char * 427 ObjectFileELF::GetPluginDescriptionStatic() 428 { 429 return "ELF object file reader."; 430 } 431 432 ObjectFile * 433 ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp, 434 DataBufferSP &data_sp, 435 lldb::offset_t data_offset, 436 const lldb_private::FileSpec* file, 437 lldb::offset_t file_offset, 438 lldb::offset_t length) 439 { 440 if (!data_sp) 441 { 442 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length); 443 data_offset = 0; 444 } 445 446 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) 447 { 448 const uint8_t *magic = data_sp->GetBytes() + data_offset; 449 if (ELFHeader::MagicBytesMatch(magic)) 450 { 451 // Update the data to contain the entire file if it doesn't already 452 if (data_sp->GetByteSize() < length) { 453 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length); 454 data_offset = 0; 455 magic = data_sp->GetBytes(); 456 } 457 unsigned address_size = ELFHeader::AddressSizeInBytes(magic); 458 if (address_size == 4 || address_size == 8) 459 { 460 std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, data_offset, file, file_offset, length)); 461 ArchSpec spec; 462 if (objfile_ap->GetArchitecture(spec) && 463 objfile_ap->SetModulesArchitecture(spec)) 464 return objfile_ap.release(); 465 } 466 } 467 } 468 return NULL; 469 } 470 471 472 ObjectFile* 473 ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp, 474 DataBufferSP& data_sp, 475 const lldb::ProcessSP &process_sp, 476 lldb::addr_t header_addr) 477 { 478 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT)) 479 { 480 const uint8_t *magic = data_sp->GetBytes(); 481 if (ELFHeader::MagicBytesMatch(magic)) 482 { 483 unsigned address_size = ELFHeader::AddressSizeInBytes(magic); 484 if (address_size == 4 || address_size == 8) 485 { 486 std::auto_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, process_sp, header_addr)); 487 ArchSpec spec; 488 if (objfile_ap->GetArchitecture(spec) && 489 objfile_ap->SetModulesArchitecture(spec)) 490 return objfile_ap.release(); 491 } 492 } 493 } 494 return NULL; 495 } 496 497 bool 498 ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp, 499 lldb::addr_t data_offset, 500 lldb::addr_t data_length) 501 { 502 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) 503 { 504 const uint8_t *magic = data_sp->GetBytes() + data_offset; 505 return ELFHeader::MagicBytesMatch(magic); 506 } 507 return false; 508 } 509 510 /* 511 * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c 512 * 513 * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or 514 * code or tables extracted from it, as desired without restriction. 515 */ 516 static uint32_t 517 calc_crc32(uint32_t crc, const void *buf, size_t size) 518 { 519 static const uint32_t g_crc32_tab[] = 520 { 521 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 522 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 523 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 524 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 525 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 526 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 527 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 528 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 529 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 530 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 531 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 532 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 533 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 534 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 535 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 536 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 537 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 538 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 539 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 540 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 541 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 542 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 543 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 544 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 545 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 546 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 547 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 548 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 549 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 550 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 551 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 552 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 553 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 554 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 555 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 556 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 557 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 558 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 559 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 560 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 561 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 562 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 563 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d 564 }; 565 const uint8_t *p = (const uint8_t *)buf; 566 567 crc = crc ^ ~0U; 568 while (size--) 569 crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); 570 return crc ^ ~0U; 571 } 572 573 static uint32_t 574 calc_gnu_debuglink_crc32(const void *buf, size_t size) 575 { 576 return calc_crc32(0U, buf, size); 577 } 578 579 uint32_t 580 ObjectFileELF::CalculateELFNotesSegmentsCRC32 (const ProgramHeaderColl& program_headers, 581 DataExtractor& object_data) 582 { 583 typedef ProgramHeaderCollConstIter Iter; 584 585 uint32_t core_notes_crc = 0; 586 587 for (Iter I = program_headers.begin(); I != program_headers.end(); ++I) 588 { 589 if (I->p_type == llvm::ELF::PT_NOTE) 590 { 591 const elf_off ph_offset = I->p_offset; 592 const size_t ph_size = I->p_filesz; 593 594 DataExtractor segment_data; 595 if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size) 596 { 597 // The ELF program header contained incorrect data, 598 // probably corefile is incomplete or corrupted. 599 break; 600 } 601 602 core_notes_crc = calc_crc32(core_notes_crc, 603 segment_data.GetDataStart(), 604 segment_data.GetByteSize()); 605 } 606 } 607 608 return core_notes_crc; 609 } 610 611 static const char* 612 OSABIAsCString (unsigned char osabi_byte) 613 { 614 #define _MAKE_OSABI_CASE(x) case x: return #x 615 switch (osabi_byte) 616 { 617 _MAKE_OSABI_CASE(ELFOSABI_NONE); 618 _MAKE_OSABI_CASE(ELFOSABI_HPUX); 619 _MAKE_OSABI_CASE(ELFOSABI_NETBSD); 620 _MAKE_OSABI_CASE(ELFOSABI_GNU); 621 _MAKE_OSABI_CASE(ELFOSABI_HURD); 622 _MAKE_OSABI_CASE(ELFOSABI_SOLARIS); 623 _MAKE_OSABI_CASE(ELFOSABI_AIX); 624 _MAKE_OSABI_CASE(ELFOSABI_IRIX); 625 _MAKE_OSABI_CASE(ELFOSABI_FREEBSD); 626 _MAKE_OSABI_CASE(ELFOSABI_TRU64); 627 _MAKE_OSABI_CASE(ELFOSABI_MODESTO); 628 _MAKE_OSABI_CASE(ELFOSABI_OPENBSD); 629 _MAKE_OSABI_CASE(ELFOSABI_OPENVMS); 630 _MAKE_OSABI_CASE(ELFOSABI_NSK); 631 _MAKE_OSABI_CASE(ELFOSABI_AROS); 632 _MAKE_OSABI_CASE(ELFOSABI_FENIXOS); 633 _MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI); 634 _MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX); 635 _MAKE_OSABI_CASE(ELFOSABI_ARM); 636 _MAKE_OSABI_CASE(ELFOSABI_STANDALONE); 637 default: 638 return "<unknown-osabi>"; 639 } 640 #undef _MAKE_OSABI_CASE 641 } 642 643 // 644 // WARNING : This function is being deprecated 645 // It's functionality has moved to ArchSpec::SetArchitecture 646 // This function is only being kept to validate the move. 647 // 648 // TODO : Remove this function 649 static bool 650 GetOsFromOSABI (unsigned char osabi_byte, llvm::Triple::OSType &ostype) 651 { 652 switch (osabi_byte) 653 { 654 case ELFOSABI_AIX: ostype = llvm::Triple::OSType::AIX; break; 655 case ELFOSABI_FREEBSD: ostype = llvm::Triple::OSType::FreeBSD; break; 656 case ELFOSABI_GNU: ostype = llvm::Triple::OSType::Linux; break; 657 case ELFOSABI_NETBSD: ostype = llvm::Triple::OSType::NetBSD; break; 658 case ELFOSABI_OPENBSD: ostype = llvm::Triple::OSType::OpenBSD; break; 659 case ELFOSABI_SOLARIS: ostype = llvm::Triple::OSType::Solaris; break; 660 default: 661 ostype = llvm::Triple::OSType::UnknownOS; 662 } 663 return ostype != llvm::Triple::OSType::UnknownOS; 664 } 665 666 size_t 667 ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file, 668 lldb::DataBufferSP& data_sp, 669 lldb::offset_t data_offset, 670 lldb::offset_t file_offset, 671 lldb::offset_t length, 672 lldb_private::ModuleSpecList &specs) 673 { 674 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MODULES)); 675 676 const size_t initial_count = specs.GetSize(); 677 678 if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) 679 { 680 DataExtractor data; 681 data.SetData(data_sp); 682 elf::ELFHeader header; 683 if (header.Parse(data, &data_offset)) 684 { 685 if (data_sp) 686 { 687 ModuleSpec spec (file); 688 689 const uint32_t sub_type = subTypeFromElfHeader(header); 690 spec.GetArchitecture().SetArchitecture(eArchTypeELF, 691 header.e_machine, 692 sub_type, 693 header.e_ident[EI_OSABI]); 694 695 if (spec.GetArchitecture().IsValid()) 696 { 697 llvm::Triple::OSType ostype; 698 llvm::Triple::VendorType vendor; 699 llvm::Triple::OSType spec_ostype = spec.GetArchitecture ().GetTriple ().getOS (); 700 701 if (log) 702 log->Printf ("ObjectFileELF::%s file '%s' module OSABI: %s", __FUNCTION__, file.GetPath ().c_str (), OSABIAsCString (header.e_ident[EI_OSABI])); 703 704 // SetArchitecture should have set the vendor to unknown 705 vendor = spec.GetArchitecture ().GetTriple ().getVendor (); 706 assert(vendor == llvm::Triple::UnknownVendor); 707 708 // 709 // Validate it is ok to remove GetOsFromOSABI 710 GetOsFromOSABI (header.e_ident[EI_OSABI], ostype); 711 assert(spec_ostype == ostype); 712 if (spec_ostype != llvm::Triple::OSType::UnknownOS) 713 { 714 if (log) 715 log->Printf ("ObjectFileELF::%s file '%s' set ELF module OS type from ELF header OSABI.", __FUNCTION__, file.GetPath ().c_str ()); 716 } 717 718 // Try to get the UUID from the section list. Usually that's at the end, so 719 // map the file in if we don't have it already. 720 size_t section_header_end = header.e_shoff + header.e_shnum * header.e_shentsize; 721 if (section_header_end > data_sp->GetByteSize()) 722 { 723 data_sp = file.MemoryMapFileContentsIfLocal (file_offset, section_header_end); 724 data.SetData(data_sp); 725 } 726 727 uint32_t gnu_debuglink_crc = 0; 728 std::string gnu_debuglink_file; 729 SectionHeaderColl section_headers; 730 lldb_private::UUID &uuid = spec.GetUUID(); 731 732 GetSectionHeaderInfo(section_headers, data, header, uuid, gnu_debuglink_file, gnu_debuglink_crc, spec.GetArchitecture ()); 733 734 llvm::Triple &spec_triple = spec.GetArchitecture ().GetTriple (); 735 736 if (log) 737 log->Printf ("ObjectFileELF::%s file '%s' module set to triple: %s (architecture %s)", __FUNCTION__, file.GetPath ().c_str (), spec_triple.getTriple ().c_str (), spec.GetArchitecture ().GetArchitectureName ()); 738 739 if (!uuid.IsValid()) 740 { 741 uint32_t core_notes_crc = 0; 742 743 if (!gnu_debuglink_crc) 744 { 745 lldb_private::Timer scoped_timer (__PRETTY_FUNCTION__, 746 "Calculating module crc32 %s with size %" PRIu64 " KiB", 747 file.GetLastPathComponent().AsCString(), 748 (file.GetByteSize()-file_offset)/1024); 749 750 // For core files - which usually don't happen to have a gnu_debuglink, 751 // and are pretty bulky - calculating whole contents crc32 would be too much of luxury. 752 // Thus we will need to fallback to something simpler. 753 if (header.e_type == llvm::ELF::ET_CORE) 754 { 755 size_t program_headers_end = header.e_phoff + header.e_phnum * header.e_phentsize; 756 if (program_headers_end > data_sp->GetByteSize()) 757 { 758 data_sp = file.MemoryMapFileContentsIfLocal(file_offset, program_headers_end); 759 data.SetData(data_sp); 760 } 761 ProgramHeaderColl program_headers; 762 GetProgramHeaderInfo(program_headers, data, header); 763 764 size_t segment_data_end = 0; 765 for (ProgramHeaderCollConstIter I = program_headers.begin(); 766 I != program_headers.end(); ++I) 767 { 768 segment_data_end = std::max<unsigned long long> (I->p_offset + I->p_filesz, segment_data_end); 769 } 770 771 if (segment_data_end > data_sp->GetByteSize()) 772 { 773 data_sp = file.MemoryMapFileContentsIfLocal(file_offset, segment_data_end); 774 data.SetData(data_sp); 775 } 776 777 core_notes_crc = CalculateELFNotesSegmentsCRC32 (program_headers, data); 778 } 779 else 780 { 781 // Need to map entire file into memory to calculate the crc. 782 data_sp = file.MemoryMapFileContentsIfLocal (file_offset, SIZE_MAX); 783 data.SetData(data_sp); 784 gnu_debuglink_crc = calc_gnu_debuglink_crc32 (data.GetDataStart(), data.GetByteSize()); 785 } 786 } 787 if (gnu_debuglink_crc) 788 { 789 // Use 4 bytes of crc from the .gnu_debuglink section. 790 uint32_t uuidt[4] = { gnu_debuglink_crc, 0, 0, 0 }; 791 uuid.SetBytes (uuidt, sizeof(uuidt)); 792 } 793 else if (core_notes_crc) 794 { 795 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it look different form 796 // .gnu_debuglink crc followed by 4 bytes of note segments crc. 797 uint32_t uuidt[4] = { g_core_uuid_magic, core_notes_crc, 0, 0 }; 798 uuid.SetBytes (uuidt, sizeof(uuidt)); 799 } 800 } 801 802 specs.Append(spec); 803 } 804 } 805 } 806 } 807 808 return specs.GetSize() - initial_count; 809 } 810 811 //------------------------------------------------------------------ 812 // PluginInterface protocol 813 //------------------------------------------------------------------ 814 lldb_private::ConstString 815 ObjectFileELF::GetPluginName() 816 { 817 return GetPluginNameStatic(); 818 } 819 820 uint32_t 821 ObjectFileELF::GetPluginVersion() 822 { 823 return m_plugin_version; 824 } 825 //------------------------------------------------------------------ 826 // ObjectFile protocol 827 //------------------------------------------------------------------ 828 829 ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp, 830 DataBufferSP& data_sp, 831 lldb::offset_t data_offset, 832 const FileSpec* file, 833 lldb::offset_t file_offset, 834 lldb::offset_t length) : 835 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 836 m_header(), 837 m_uuid(), 838 m_gnu_debuglink_file(), 839 m_gnu_debuglink_crc(0), 840 m_program_headers(), 841 m_section_headers(), 842 m_dynamic_symbols(), 843 m_filespec_ap(), 844 m_entry_point_address(), 845 m_arch_spec() 846 { 847 if (file) 848 m_file = *file; 849 ::memset(&m_header, 0, sizeof(m_header)); 850 } 851 852 ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp, 853 DataBufferSP& header_data_sp, 854 const lldb::ProcessSP &process_sp, 855 addr_t header_addr) : 856 ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 857 m_header(), 858 m_uuid(), 859 m_gnu_debuglink_file(), 860 m_gnu_debuglink_crc(0), 861 m_program_headers(), 862 m_section_headers(), 863 m_dynamic_symbols(), 864 m_filespec_ap(), 865 m_entry_point_address(), 866 m_arch_spec() 867 { 868 ::memset(&m_header, 0, sizeof(m_header)); 869 } 870 871 ObjectFileELF::~ObjectFileELF() 872 { 873 } 874 875 bool 876 ObjectFileELF::IsExecutable() const 877 { 878 return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0); 879 } 880 881 bool 882 ObjectFileELF::SetLoadAddress (Target &target, 883 lldb::addr_t value, 884 bool value_is_offset) 885 { 886 ModuleSP module_sp = GetModule(); 887 if (module_sp) 888 { 889 size_t num_loaded_sections = 0; 890 SectionList *section_list = GetSectionList (); 891 if (section_list) 892 { 893 if (!value_is_offset) 894 { 895 bool found_offset = false; 896 for (size_t i = 0, count = GetProgramHeaderCount(); i < count; ++i) 897 { 898 const elf::ELFProgramHeader* header = GetProgramHeaderByIndex(i); 899 if (header == nullptr) 900 continue; 901 902 if (header->p_type != PT_LOAD || header->p_offset != 0) 903 continue; 904 905 value = value - header->p_vaddr; 906 found_offset = true; 907 break; 908 } 909 if (!found_offset) 910 return false; 911 } 912 913 const size_t num_sections = section_list->GetSize(); 914 size_t sect_idx = 0; 915 916 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) 917 { 918 // Iterate through the object file sections to find all 919 // of the sections that have SHF_ALLOC in their flag bits. 920 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx)); 921 // if (section_sp && !section_sp->IsThreadSpecific()) 922 if (section_sp && section_sp->Test(SHF_ALLOC)) 923 { 924 lldb::addr_t load_addr = section_sp->GetFileAddress() + value; 925 926 // On 32-bit systems the load address have to fit into 4 bytes. The rest of 927 // the bytes are the overflow from the addition. 928 if (GetAddressByteSize() == 4) 929 load_addr &= 0xFFFFFFFF; 930 931 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr)) 932 ++num_loaded_sections; 933 } 934 } 935 return num_loaded_sections > 0; 936 } 937 } 938 return false; 939 } 940 941 ByteOrder 942 ObjectFileELF::GetByteOrder() const 943 { 944 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) 945 return eByteOrderBig; 946 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) 947 return eByteOrderLittle; 948 return eByteOrderInvalid; 949 } 950 951 uint32_t 952 ObjectFileELF::GetAddressByteSize() const 953 { 954 return m_data.GetAddressByteSize(); 955 } 956 957 AddressClass 958 ObjectFileELF::GetAddressClass (addr_t file_addr) 959 { 960 Symtab* symtab = GetSymtab(); 961 if (!symtab) 962 return eAddressClassUnknown; 963 964 // The address class is determined based on the symtab. Ask it from the object file what 965 // contains the symtab information. 966 ObjectFile* symtab_objfile = symtab->GetObjectFile(); 967 if (symtab_objfile != nullptr && symtab_objfile != this) 968 return symtab_objfile->GetAddressClass(file_addr); 969 970 auto res = ObjectFile::GetAddressClass (file_addr); 971 if (res != eAddressClassCode) 972 return res; 973 974 auto ub = m_address_class_map.upper_bound(file_addr); 975 if (ub == m_address_class_map.begin()) 976 { 977 // No entry in the address class map before the address. Return 978 // default address class for an address in a code section. 979 return eAddressClassCode; 980 } 981 982 // Move iterator to the address class entry preceding address 983 --ub; 984 985 return ub->second; 986 } 987 988 size_t 989 ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) 990 { 991 return std::distance(m_section_headers.begin(), I) + 1u; 992 } 993 994 size_t 995 ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const 996 { 997 return std::distance(m_section_headers.begin(), I) + 1u; 998 } 999 1000 bool 1001 ObjectFileELF::ParseHeader() 1002 { 1003 lldb::offset_t offset = 0; 1004 if (!m_header.Parse(m_data, &offset)) 1005 return false; 1006 1007 if (!IsInMemory()) 1008 return true; 1009 1010 // For in memory object files m_data might not contain the full object file. Try to load it 1011 // until the end of the "Section header table" what is at the end of the ELF file. 1012 addr_t file_size = m_header.e_shoff + m_header.e_shnum * m_header.e_shentsize; 1013 if (m_data.GetByteSize() < file_size) 1014 { 1015 ProcessSP process_sp (m_process_wp.lock()); 1016 if (!process_sp) 1017 return false; 1018 1019 DataBufferSP data_sp = ReadMemory(process_sp, m_memory_addr, file_size); 1020 if (!data_sp) 1021 return false; 1022 m_data.SetData(data_sp, 0, file_size); 1023 } 1024 1025 return true; 1026 } 1027 1028 bool 1029 ObjectFileELF::GetUUID(lldb_private::UUID* uuid) 1030 { 1031 // Need to parse the section list to get the UUIDs, so make sure that's been done. 1032 if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile) 1033 return false; 1034 1035 if (m_uuid.IsValid()) 1036 { 1037 // We have the full build id uuid. 1038 *uuid = m_uuid; 1039 return true; 1040 } 1041 else if (GetType() == ObjectFile::eTypeCoreFile) 1042 { 1043 uint32_t core_notes_crc = 0; 1044 1045 if (!ParseProgramHeaders()) 1046 return false; 1047 1048 core_notes_crc = CalculateELFNotesSegmentsCRC32(m_program_headers, m_data); 1049 1050 if (core_notes_crc) 1051 { 1052 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it 1053 // look different form .gnu_debuglink crc - followed by 4 bytes of note 1054 // segments crc. 1055 uint32_t uuidt[4] = { g_core_uuid_magic, core_notes_crc, 0, 0 }; 1056 m_uuid.SetBytes (uuidt, sizeof(uuidt)); 1057 } 1058 } 1059 else 1060 { 1061 if (!m_gnu_debuglink_crc) 1062 m_gnu_debuglink_crc = calc_gnu_debuglink_crc32 (m_data.GetDataStart(), m_data.GetByteSize()); 1063 if (m_gnu_debuglink_crc) 1064 { 1065 // Use 4 bytes of crc from the .gnu_debuglink section. 1066 uint32_t uuidt[4] = { m_gnu_debuglink_crc, 0, 0, 0 }; 1067 m_uuid.SetBytes (uuidt, sizeof(uuidt)); 1068 } 1069 } 1070 1071 if (m_uuid.IsValid()) 1072 { 1073 *uuid = m_uuid; 1074 return true; 1075 } 1076 1077 return false; 1078 } 1079 1080 lldb_private::FileSpecList 1081 ObjectFileELF::GetDebugSymbolFilePaths() 1082 { 1083 FileSpecList file_spec_list; 1084 1085 if (!m_gnu_debuglink_file.empty()) 1086 { 1087 FileSpec file_spec (m_gnu_debuglink_file.c_str(), false); 1088 file_spec_list.Append (file_spec); 1089 } 1090 return file_spec_list; 1091 } 1092 1093 uint32_t 1094 ObjectFileELF::GetDependentModules(FileSpecList &files) 1095 { 1096 size_t num_modules = ParseDependentModules(); 1097 uint32_t num_specs = 0; 1098 1099 for (unsigned i = 0; i < num_modules; ++i) 1100 { 1101 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) 1102 num_specs++; 1103 } 1104 1105 return num_specs; 1106 } 1107 1108 Address 1109 ObjectFileELF::GetImageInfoAddress(Target *target) 1110 { 1111 if (!ParseDynamicSymbols()) 1112 return Address(); 1113 1114 SectionList *section_list = GetSectionList(); 1115 if (!section_list) 1116 return Address(); 1117 1118 // Find the SHT_DYNAMIC (.dynamic) section. 1119 SectionSP dynsym_section_sp (section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true)); 1120 if (!dynsym_section_sp) 1121 return Address(); 1122 assert (dynsym_section_sp->GetObjectFile() == this); 1123 1124 user_id_t dynsym_id = dynsym_section_sp->GetID(); 1125 const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id); 1126 if (!dynsym_hdr) 1127 return Address(); 1128 1129 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) 1130 { 1131 ELFDynamic &symbol = m_dynamic_symbols[i]; 1132 1133 if (symbol.d_tag == DT_DEBUG) 1134 { 1135 // Compute the offset as the number of previous entries plus the 1136 // size of d_tag. 1137 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); 1138 return Address(dynsym_section_sp, offset); 1139 } 1140 // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP exists in non-PIE. 1141 else if ((symbol.d_tag == DT_MIPS_RLD_MAP || symbol.d_tag == DT_MIPS_RLD_MAP_REL) && target) 1142 { 1143 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); 1144 addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target); 1145 if (dyn_base == LLDB_INVALID_ADDRESS) 1146 return Address(); 1147 1148 Error error; 1149 if (symbol.d_tag == DT_MIPS_RLD_MAP) 1150 { 1151 // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer. 1152 Address addr; 1153 if (target->ReadPointerFromMemory(dyn_base + offset, false, error, addr)) 1154 return addr; 1155 } 1156 if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) 1157 { 1158 // DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer, relative to the address of the tag. 1159 uint64_t rel_offset; 1160 rel_offset = target->ReadUnsignedIntegerFromMemory(dyn_base + offset, false, GetAddressByteSize(), UINT64_MAX, error); 1161 if (error.Success() && rel_offset != UINT64_MAX) 1162 { 1163 Address addr; 1164 addr_t debug_ptr_address = dyn_base + (offset - GetAddressByteSize()) + rel_offset; 1165 addr.SetOffset (debug_ptr_address); 1166 return addr; 1167 } 1168 } 1169 } 1170 } 1171 1172 return Address(); 1173 } 1174 1175 lldb_private::Address 1176 ObjectFileELF::GetEntryPointAddress () 1177 { 1178 if (m_entry_point_address.IsValid()) 1179 return m_entry_point_address; 1180 1181 if (!ParseHeader() || !IsExecutable()) 1182 return m_entry_point_address; 1183 1184 SectionList *section_list = GetSectionList(); 1185 addr_t offset = m_header.e_entry; 1186 1187 if (!section_list) 1188 m_entry_point_address.SetOffset(offset); 1189 else 1190 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list); 1191 return m_entry_point_address; 1192 } 1193 1194 //---------------------------------------------------------------------- 1195 // ParseDependentModules 1196 //---------------------------------------------------------------------- 1197 size_t 1198 ObjectFileELF::ParseDependentModules() 1199 { 1200 if (m_filespec_ap.get()) 1201 return m_filespec_ap->GetSize(); 1202 1203 m_filespec_ap.reset(new FileSpecList()); 1204 1205 if (!ParseSectionHeaders()) 1206 return 0; 1207 1208 SectionList *section_list = GetSectionList(); 1209 if (!section_list) 1210 return 0; 1211 1212 // Find the SHT_DYNAMIC section. 1213 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get(); 1214 if (!dynsym) 1215 return 0; 1216 assert (dynsym->GetObjectFile() == this); 1217 1218 const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex (dynsym->GetID()); 1219 if (!header) 1220 return 0; 1221 // sh_link: section header index of string table used by entries in the section. 1222 Section *dynstr = section_list->FindSectionByID (header->sh_link + 1).get(); 1223 if (!dynstr) 1224 return 0; 1225 1226 DataExtractor dynsym_data; 1227 DataExtractor dynstr_data; 1228 if (ReadSectionData(dynsym, dynsym_data) && 1229 ReadSectionData(dynstr, dynstr_data)) 1230 { 1231 ELFDynamic symbol; 1232 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 1233 lldb::offset_t offset = 0; 1234 1235 // The only type of entries we are concerned with are tagged DT_NEEDED, 1236 // yielding the name of a required library. 1237 while (offset < section_size) 1238 { 1239 if (!symbol.Parse(dynsym_data, &offset)) 1240 break; 1241 1242 if (symbol.d_tag != DT_NEEDED) 1243 continue; 1244 1245 uint32_t str_index = static_cast<uint32_t>(symbol.d_val); 1246 const char *lib_name = dynstr_data.PeekCStr(str_index); 1247 m_filespec_ap->Append(FileSpec(lib_name, true)); 1248 } 1249 } 1250 1251 return m_filespec_ap->GetSize(); 1252 } 1253 1254 //---------------------------------------------------------------------- 1255 // GetProgramHeaderInfo 1256 //---------------------------------------------------------------------- 1257 size_t 1258 ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers, 1259 DataExtractor &object_data, 1260 const ELFHeader &header) 1261 { 1262 // We have already parsed the program headers 1263 if (!program_headers.empty()) 1264 return program_headers.size(); 1265 1266 // If there are no program headers to read we are done. 1267 if (header.e_phnum == 0) 1268 return 0; 1269 1270 program_headers.resize(header.e_phnum); 1271 if (program_headers.size() != header.e_phnum) 1272 return 0; 1273 1274 const size_t ph_size = header.e_phnum * header.e_phentsize; 1275 const elf_off ph_offset = header.e_phoff; 1276 DataExtractor data; 1277 if (data.SetData(object_data, ph_offset, ph_size) != ph_size) 1278 return 0; 1279 1280 uint32_t idx; 1281 lldb::offset_t offset; 1282 for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) 1283 { 1284 if (program_headers[idx].Parse(data, &offset) == false) 1285 break; 1286 } 1287 1288 if (idx < program_headers.size()) 1289 program_headers.resize(idx); 1290 1291 return program_headers.size(); 1292 1293 } 1294 1295 //---------------------------------------------------------------------- 1296 // ParseProgramHeaders 1297 //---------------------------------------------------------------------- 1298 size_t 1299 ObjectFileELF::ParseProgramHeaders() 1300 { 1301 return GetProgramHeaderInfo(m_program_headers, m_data, m_header); 1302 } 1303 1304 lldb_private::Error 1305 ObjectFileELF::RefineModuleDetailsFromNote (lldb_private::DataExtractor &data, lldb_private::ArchSpec &arch_spec, lldb_private::UUID &uuid) 1306 { 1307 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MODULES)); 1308 Error error; 1309 1310 lldb::offset_t offset = 0; 1311 1312 while (true) 1313 { 1314 // Parse the note header. If this fails, bail out. 1315 const lldb::offset_t note_offset = offset; 1316 ELFNote note = ELFNote(); 1317 if (!note.Parse(data, &offset)) 1318 { 1319 // We're done. 1320 return error; 1321 } 1322 1323 if (log) 1324 log->Printf ("ObjectFileELF::%s parsing note name='%s', type=%" PRIu32, __FUNCTION__, note.n_name.c_str (), note.n_type); 1325 1326 // Process FreeBSD ELF notes. 1327 if ((note.n_name == LLDB_NT_OWNER_FREEBSD) && 1328 (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) && 1329 (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) 1330 { 1331 // Pull out the min version info. 1332 uint32_t version_info; 1333 if (data.GetU32 (&offset, &version_info, 1) == nullptr) 1334 { 1335 error.SetErrorString ("failed to read FreeBSD ABI note payload"); 1336 return error; 1337 } 1338 1339 // Convert the version info into a major/minor number. 1340 const uint32_t version_major = version_info / 100000; 1341 const uint32_t version_minor = (version_info / 1000) % 100; 1342 1343 char os_name[32]; 1344 snprintf (os_name, sizeof (os_name), "freebsd%" PRIu32 ".%" PRIu32, version_major, version_minor); 1345 1346 // Set the elf OS version to FreeBSD. Also clear the vendor. 1347 arch_spec.GetTriple ().setOSName (os_name); 1348 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor); 1349 1350 if (log) 1351 log->Printf ("ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_major, version_minor, static_cast<uint32_t> (version_info % 1000)); 1352 } 1353 // Process GNU ELF notes. 1354 else if (note.n_name == LLDB_NT_OWNER_GNU) 1355 { 1356 switch (note.n_type) 1357 { 1358 case LLDB_NT_GNU_ABI_TAG: 1359 if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) 1360 { 1361 // Pull out the min OS version supporting the ABI. 1362 uint32_t version_info[4]; 1363 if (data.GetU32 (&offset, &version_info[0], note.n_descsz / 4) == nullptr) 1364 { 1365 error.SetErrorString ("failed to read GNU ABI note payload"); 1366 return error; 1367 } 1368 1369 // Set the OS per the OS field. 1370 switch (version_info[0]) 1371 { 1372 case LLDB_NT_GNU_ABI_OS_LINUX: 1373 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::Linux); 1374 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor); 1375 if (log) 1376 log->Printf ("ObjectFileELF::%s detected Linux, min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[1], version_info[2], version_info[3]); 1377 // FIXME we have the minimal version number, we could be propagating that. version_info[1] = OS Major, version_info[2] = OS Minor, version_info[3] = Revision. 1378 break; 1379 case LLDB_NT_GNU_ABI_OS_HURD: 1380 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::UnknownOS); 1381 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor); 1382 if (log) 1383 log->Printf ("ObjectFileELF::%s detected Hurd (unsupported), min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[1], version_info[2], version_info[3]); 1384 break; 1385 case LLDB_NT_GNU_ABI_OS_SOLARIS: 1386 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::Solaris); 1387 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor); 1388 if (log) 1389 log->Printf ("ObjectFileELF::%s detected Solaris, min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[1], version_info[2], version_info[3]); 1390 break; 1391 default: 1392 if (log) 1393 log->Printf ("ObjectFileELF::%s unrecognized OS in note, id %" PRIu32 ", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[0], version_info[1], version_info[2], version_info[3]); 1394 break; 1395 } 1396 } 1397 break; 1398 1399 case LLDB_NT_GNU_BUILD_ID_TAG: 1400 // Only bother processing this if we don't already have the uuid set. 1401 if (!uuid.IsValid()) 1402 { 1403 // 16 bytes is UUID|MD5, 20 bytes is SHA1 1404 if ((note.n_descsz == 16 || note.n_descsz == 20)) 1405 { 1406 uint8_t uuidbuf[20]; 1407 if (data.GetU8 (&offset, &uuidbuf, note.n_descsz) == nullptr) 1408 { 1409 error.SetErrorString ("failed to read GNU_BUILD_ID note payload"); 1410 return error; 1411 } 1412 1413 // Save the build id as the UUID for the module. 1414 uuid.SetBytes (uuidbuf, note.n_descsz); 1415 } 1416 } 1417 break; 1418 } 1419 } 1420 // Process NetBSD ELF notes. 1421 else if ((note.n_name == LLDB_NT_OWNER_NETBSD) && 1422 (note.n_type == LLDB_NT_NETBSD_ABI_TAG) && 1423 (note.n_descsz == LLDB_NT_NETBSD_ABI_SIZE)) 1424 { 1425 // Pull out the min version info. 1426 uint32_t version_info; 1427 if (data.GetU32 (&offset, &version_info, 1) == nullptr) 1428 { 1429 error.SetErrorString ("failed to read NetBSD ABI note payload"); 1430 return error; 1431 } 1432 1433 // Set the elf OS version to NetBSD. Also clear the vendor. 1434 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::NetBSD); 1435 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor); 1436 1437 if (log) 1438 log->Printf ("ObjectFileELF::%s detected NetBSD, min version constant %" PRIu32, __FUNCTION__, version_info); 1439 } 1440 // Process CSR kalimba notes 1441 else if ((note.n_type == LLDB_NT_GNU_ABI_TAG) && 1442 (note.n_name == LLDB_NT_OWNER_CSR)) 1443 { 1444 arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS); 1445 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::CSR); 1446 1447 // TODO At some point the description string could be processed. 1448 // It could provide a steer towards the kalimba variant which 1449 // this ELF targets. 1450 if(note.n_descsz) 1451 { 1452 const char *cstr = data.GetCStr(&offset, llvm::alignTo (note.n_descsz, 4)); 1453 (void)cstr; 1454 } 1455 } 1456 else if (note.n_name == LLDB_NT_OWNER_ANDROID) 1457 { 1458 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1459 arch_spec.GetTriple().setEnvironment(llvm::Triple::EnvironmentType::Android); 1460 } 1461 else if (note.n_name == LLDB_NT_OWNER_LINUX) 1462 { 1463 // This is sometimes found in core files and usually contains extended register info 1464 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1465 } 1466 else if (note.n_name == LLDB_NT_OWNER_CORE) 1467 { 1468 // Parse the NT_FILE to look for stuff in paths to shared libraries 1469 // As the contents look like: 1470 // count = 0x000000000000000a (10) 1471 // page_size = 0x0000000000001000 (4096) 1472 // Index start end file_ofs path 1473 // ===== ------------------ ------------------ ------------------ ------------------------------------- 1474 // [ 0] 0x0000000000400000 0x0000000000401000 0x0000000000000000 /tmp/a.out 1475 // [ 1] 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out 1476 // [ 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out 1477 // [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 /lib/x86_64-linux-gnu/libc-2.19.so 1478 // [ 4] 0x00007fa79cba8000 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux-gnu/libc-2.19.so 1479 // [ 5] 0x00007fa79cda7000 0x00007fa79cdab000 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so 1480 // [ 6] 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64-linux-gnu/libc-2.19.so 1481 // [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so 1482 // [ 8] 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64-linux-gnu/ld-2.19.so 1483 // [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so 1484 if (note.n_type == NT_FILE) 1485 { 1486 uint64_t count = data.GetU64(&offset); 1487 offset += 8 + 3*8*count; // Skip page size and all start/end/file_ofs 1488 for (size_t i=0; i<count; ++i) 1489 { 1490 llvm::StringRef path(data.GetCStr(&offset)); 1491 if (path.startswith("/lib/x86_64-linux-gnu")) 1492 { 1493 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1494 break; 1495 } 1496 } 1497 } 1498 } 1499 1500 // Calculate the offset of the next note just in case "offset" has been used 1501 // to poke at the contents of the note data 1502 offset = note_offset + note.GetByteSize(); 1503 } 1504 1505 return error; 1506 } 1507 1508 1509 //---------------------------------------------------------------------- 1510 // GetSectionHeaderInfo 1511 //---------------------------------------------------------------------- 1512 size_t 1513 ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers, 1514 lldb_private::DataExtractor &object_data, 1515 const elf::ELFHeader &header, 1516 lldb_private::UUID &uuid, 1517 std::string &gnu_debuglink_file, 1518 uint32_t &gnu_debuglink_crc, 1519 ArchSpec &arch_spec) 1520 { 1521 // Don't reparse the section headers if we already did that. 1522 if (!section_headers.empty()) 1523 return section_headers.size(); 1524 1525 // Only initialize the arch_spec to okay defaults if they're not already set. 1526 // We'll refine this with note data as we parse the notes. 1527 if (arch_spec.GetTriple ().getOS () == llvm::Triple::OSType::UnknownOS) 1528 { 1529 llvm::Triple::OSType ostype; 1530 llvm::Triple::OSType spec_ostype; 1531 const uint32_t sub_type = subTypeFromElfHeader(header); 1532 arch_spec.SetArchitecture (eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]); 1533 // 1534 // Validate if it is ok to remove GetOsFromOSABI 1535 GetOsFromOSABI (header.e_ident[EI_OSABI], ostype); 1536 spec_ostype = arch_spec.GetTriple ().getOS (); 1537 assert(spec_ostype == ostype); 1538 } 1539 1540 if (arch_spec.GetMachine() == llvm::Triple::mips || arch_spec.GetMachine() == llvm::Triple::mipsel 1541 || arch_spec.GetMachine() == llvm::Triple::mips64 || arch_spec.GetMachine() == llvm::Triple::mips64el) 1542 { 1543 switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) 1544 { 1545 case llvm::ELF::EF_MIPS_MICROMIPS: 1546 arch_spec.SetFlags (ArchSpec::eMIPSAse_micromips); 1547 break; 1548 case llvm::ELF::EF_MIPS_ARCH_ASE_M16: 1549 arch_spec.SetFlags (ArchSpec::eMIPSAse_mips16); 1550 break; 1551 case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX: 1552 arch_spec.SetFlags (ArchSpec::eMIPSAse_mdmx); 1553 break; 1554 default: 1555 break; 1556 } 1557 } 1558 1559 // If there are no section headers we are done. 1560 if (header.e_shnum == 0) 1561 return 0; 1562 1563 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MODULES)); 1564 1565 section_headers.resize(header.e_shnum); 1566 if (section_headers.size() != header.e_shnum) 1567 return 0; 1568 1569 const size_t sh_size = header.e_shnum * header.e_shentsize; 1570 const elf_off sh_offset = header.e_shoff; 1571 DataExtractor sh_data; 1572 if (sh_data.SetData (object_data, sh_offset, sh_size) != sh_size) 1573 return 0; 1574 1575 uint32_t idx; 1576 lldb::offset_t offset; 1577 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) 1578 { 1579 if (section_headers[idx].Parse(sh_data, &offset) == false) 1580 break; 1581 } 1582 if (idx < section_headers.size()) 1583 section_headers.resize(idx); 1584 1585 const unsigned strtab_idx = header.e_shstrndx; 1586 if (strtab_idx && strtab_idx < section_headers.size()) 1587 { 1588 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx]; 1589 const size_t byte_size = sheader.sh_size; 1590 const Elf64_Off offset = sheader.sh_offset; 1591 lldb_private::DataExtractor shstr_data; 1592 1593 if (shstr_data.SetData (object_data, offset, byte_size) == byte_size) 1594 { 1595 for (SectionHeaderCollIter I = section_headers.begin(); 1596 I != section_headers.end(); ++I) 1597 { 1598 static ConstString g_sect_name_gnu_debuglink (".gnu_debuglink"); 1599 const ELFSectionHeaderInfo &sheader = *I; 1600 const uint64_t section_size = sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size; 1601 ConstString name(shstr_data.PeekCStr(I->sh_name)); 1602 1603 I->section_name = name; 1604 1605 if (arch_spec.GetMachine() == llvm::Triple::mips || arch_spec.GetMachine() == llvm::Triple::mipsel 1606 || arch_spec.GetMachine() == llvm::Triple::mips64 || arch_spec.GetMachine() == llvm::Triple::mips64el) 1607 { 1608 uint32_t arch_flags = arch_spec.GetFlags (); 1609 DataExtractor data; 1610 if (sheader.sh_type == SHT_MIPS_ABIFLAGS) 1611 { 1612 1613 if (section_size && (data.SetData (object_data, sheader.sh_offset, section_size) == section_size)) 1614 { 1615 lldb::offset_t ase_offset = 12; // MIPS ABI Flags Version: 0 1616 arch_flags |= data.GetU32 (&ase_offset); 1617 } 1618 } 1619 // Settings appropriate ArchSpec ABI Flags 1620 if (header.e_flags & llvm::ELF::EF_MIPS_ABI2) 1621 { 1622 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32; 1623 } 1624 else if (header.e_flags & llvm::ELF::EF_MIPS_ABI_O32) 1625 { 1626 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32; 1627 } 1628 arch_spec.SetFlags (arch_flags); 1629 } 1630 1631 if (name == g_sect_name_gnu_debuglink) 1632 { 1633 DataExtractor data; 1634 if (section_size && (data.SetData (object_data, sheader.sh_offset, section_size) == section_size)) 1635 { 1636 lldb::offset_t gnu_debuglink_offset = 0; 1637 gnu_debuglink_file = data.GetCStr (&gnu_debuglink_offset); 1638 gnu_debuglink_offset = llvm::alignTo (gnu_debuglink_offset, 4); 1639 data.GetU32 (&gnu_debuglink_offset, &gnu_debuglink_crc, 1); 1640 } 1641 } 1642 1643 // Process ELF note section entries. 1644 bool is_note_header = (sheader.sh_type == SHT_NOTE); 1645 1646 // The section header ".note.android.ident" is stored as a 1647 // PROGBITS type header but it is actually a note header. 1648 static ConstString g_sect_name_android_ident (".note.android.ident"); 1649 if (!is_note_header && name == g_sect_name_android_ident) 1650 is_note_header = true; 1651 1652 if (is_note_header) 1653 { 1654 // Allow notes to refine module info. 1655 DataExtractor data; 1656 if (section_size && (data.SetData (object_data, sheader.sh_offset, section_size) == section_size)) 1657 { 1658 Error error = RefineModuleDetailsFromNote (data, arch_spec, uuid); 1659 if (error.Fail ()) 1660 { 1661 if (log) 1662 log->Printf ("ObjectFileELF::%s ELF note processing failed: %s", __FUNCTION__, error.AsCString ()); 1663 } 1664 } 1665 } 1666 } 1667 1668 // Make any unknown triple components to be unspecified unknowns. 1669 if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor) 1670 arch_spec.GetTriple().setVendorName (llvm::StringRef()); 1671 if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS) 1672 arch_spec.GetTriple().setOSName (llvm::StringRef()); 1673 1674 return section_headers.size(); 1675 } 1676 } 1677 1678 section_headers.clear(); 1679 return 0; 1680 } 1681 1682 size_t 1683 ObjectFileELF::GetProgramHeaderCount() 1684 { 1685 return ParseProgramHeaders(); 1686 } 1687 1688 const elf::ELFProgramHeader * 1689 ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id) 1690 { 1691 if (!id || !ParseProgramHeaders()) 1692 return NULL; 1693 1694 if (--id < m_program_headers.size()) 1695 return &m_program_headers[id]; 1696 1697 return NULL; 1698 } 1699 1700 DataExtractor 1701 ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id) 1702 { 1703 const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id); 1704 if (segment_header == NULL) 1705 return DataExtractor(); 1706 return DataExtractor(m_data, segment_header->p_offset, segment_header->p_filesz); 1707 } 1708 1709 std::string 1710 ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const 1711 { 1712 size_t pos = symbol_name.find('@'); 1713 return symbol_name.substr(0, pos).str(); 1714 } 1715 1716 //---------------------------------------------------------------------- 1717 // ParseSectionHeaders 1718 //---------------------------------------------------------------------- 1719 size_t 1720 ObjectFileELF::ParseSectionHeaders() 1721 { 1722 return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, m_gnu_debuglink_file, m_gnu_debuglink_crc, m_arch_spec); 1723 } 1724 1725 const ObjectFileELF::ELFSectionHeaderInfo * 1726 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) 1727 { 1728 if (!id || !ParseSectionHeaders()) 1729 return NULL; 1730 1731 if (--id < m_section_headers.size()) 1732 return &m_section_headers[id]; 1733 1734 return NULL; 1735 } 1736 1737 lldb::user_id_t 1738 ObjectFileELF::GetSectionIndexByName(const char* name) 1739 { 1740 if (!name || !name[0] || !ParseSectionHeaders()) 1741 return 0; 1742 for (size_t i = 1; i < m_section_headers.size(); ++i) 1743 if (m_section_headers[i].section_name == ConstString(name)) 1744 return i; 1745 return 0; 1746 } 1747 1748 void 1749 ObjectFileELF::CreateSections(SectionList &unified_section_list) 1750 { 1751 if (!m_sections_ap.get() && ParseSectionHeaders()) 1752 { 1753 m_sections_ap.reset(new SectionList()); 1754 1755 for (SectionHeaderCollIter I = m_section_headers.begin(); 1756 I != m_section_headers.end(); ++I) 1757 { 1758 const ELFSectionHeaderInfo &header = *I; 1759 1760 ConstString& name = I->section_name; 1761 const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; 1762 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0; 1763 1764 static ConstString g_sect_name_text (".text"); 1765 static ConstString g_sect_name_data (".data"); 1766 static ConstString g_sect_name_bss (".bss"); 1767 static ConstString g_sect_name_tdata (".tdata"); 1768 static ConstString g_sect_name_tbss (".tbss"); 1769 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev"); 1770 static ConstString g_sect_name_dwarf_debug_addr (".debug_addr"); 1771 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges"); 1772 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame"); 1773 static ConstString g_sect_name_dwarf_debug_info (".debug_info"); 1774 static ConstString g_sect_name_dwarf_debug_line (".debug_line"); 1775 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc"); 1776 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo"); 1777 static ConstString g_sect_name_dwarf_debug_macro (".debug_macro"); 1778 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames"); 1779 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes"); 1780 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges"); 1781 static ConstString g_sect_name_dwarf_debug_str (".debug_str"); 1782 static ConstString g_sect_name_dwarf_debug_str_offsets (".debug_str_offsets"); 1783 static ConstString g_sect_name_dwarf_debug_abbrev_dwo (".debug_abbrev.dwo"); 1784 static ConstString g_sect_name_dwarf_debug_info_dwo (".debug_info.dwo"); 1785 static ConstString g_sect_name_dwarf_debug_line_dwo (".debug_line.dwo"); 1786 static ConstString g_sect_name_dwarf_debug_macro_dwo (".debug_macro.dwo"); 1787 static ConstString g_sect_name_dwarf_debug_loc_dwo (".debug_loc.dwo"); 1788 static ConstString g_sect_name_dwarf_debug_str_dwo (".debug_str.dwo"); 1789 static ConstString g_sect_name_dwarf_debug_str_offsets_dwo (".debug_str_offsets.dwo"); 1790 static ConstString g_sect_name_eh_frame (".eh_frame"); 1791 static ConstString g_sect_name_arm_exidx (".ARM.exidx"); 1792 static ConstString g_sect_name_arm_extab (".ARM.extab"); 1793 static ConstString g_sect_name_go_symtab (".gosymtab"); 1794 1795 SectionType sect_type = eSectionTypeOther; 1796 1797 bool is_thread_specific = false; 1798 1799 if (name == g_sect_name_text) sect_type = eSectionTypeCode; 1800 else if (name == g_sect_name_data) sect_type = eSectionTypeData; 1801 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill; 1802 else if (name == g_sect_name_tdata) 1803 { 1804 sect_type = eSectionTypeData; 1805 is_thread_specific = true; 1806 } 1807 else if (name == g_sect_name_tbss) 1808 { 1809 sect_type = eSectionTypeZeroFill; 1810 is_thread_specific = true; 1811 } 1812 // .debug_abbrev – Abbreviations used in the .debug_info section 1813 // .debug_aranges – Lookup table for mapping addresses to compilation units 1814 // .debug_frame – Call frame information 1815 // .debug_info – The core DWARF information section 1816 // .debug_line – Line number information 1817 // .debug_loc – Location lists used in DW_AT_location attributes 1818 // .debug_macinfo – Macro information 1819 // .debug_pubnames – Lookup table for mapping object and function names to compilation units 1820 // .debug_pubtypes – Lookup table for mapping type names to compilation units 1821 // .debug_ranges – Address ranges used in DW_AT_ranges attributes 1822 // .debug_str – String table used in .debug_info 1823 // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html 1824 // MISSING? .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644 1825 // MISSING? .debug_types - Type descriptions from DWARF 4? See http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo 1826 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev; 1827 else if (name == g_sect_name_dwarf_debug_addr) sect_type = eSectionTypeDWARFDebugAddr; 1828 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges; 1829 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame; 1830 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo; 1831 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine; 1832 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc; 1833 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo; 1834 else if (name == g_sect_name_dwarf_debug_macro) sect_type = eSectionTypeDWARFDebugMacro; 1835 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames; 1836 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes; 1837 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges; 1838 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr; 1839 else if (name == g_sect_name_dwarf_debug_str_offsets) sect_type = eSectionTypeDWARFDebugStrOffsets; 1840 else if (name == g_sect_name_dwarf_debug_abbrev_dwo) sect_type = eSectionTypeDWARFDebugAbbrev; 1841 else if (name == g_sect_name_dwarf_debug_info_dwo) sect_type = eSectionTypeDWARFDebugInfo; 1842 else if (name == g_sect_name_dwarf_debug_line_dwo) sect_type = eSectionTypeDWARFDebugLine; 1843 else if (name == g_sect_name_dwarf_debug_macro_dwo) sect_type = eSectionTypeDWARFDebugMacro; 1844 else if (name == g_sect_name_dwarf_debug_loc_dwo) sect_type = eSectionTypeDWARFDebugLoc; 1845 else if (name == g_sect_name_dwarf_debug_str_dwo) sect_type = eSectionTypeDWARFDebugStr; 1846 else if (name == g_sect_name_dwarf_debug_str_offsets_dwo) sect_type = eSectionTypeDWARFDebugStrOffsets; 1847 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame; 1848 else if (name == g_sect_name_arm_exidx) sect_type = eSectionTypeARMexidx; 1849 else if (name == g_sect_name_arm_extab) sect_type = eSectionTypeARMextab; 1850 else if (name == g_sect_name_go_symtab) sect_type = eSectionTypeGoSymtab; 1851 1852 switch (header.sh_type) 1853 { 1854 case SHT_SYMTAB: 1855 assert (sect_type == eSectionTypeOther); 1856 sect_type = eSectionTypeELFSymbolTable; 1857 break; 1858 case SHT_DYNSYM: 1859 assert (sect_type == eSectionTypeOther); 1860 sect_type = eSectionTypeELFDynamicSymbols; 1861 break; 1862 case SHT_RELA: 1863 case SHT_REL: 1864 assert (sect_type == eSectionTypeOther); 1865 sect_type = eSectionTypeELFRelocationEntries; 1866 break; 1867 case SHT_DYNAMIC: 1868 assert (sect_type == eSectionTypeOther); 1869 sect_type = eSectionTypeELFDynamicLinkInfo; 1870 break; 1871 } 1872 1873 if (eSectionTypeOther == sect_type) 1874 { 1875 // the kalimba toolchain assumes that ELF section names are free-form. It does 1876 // support linkscripts which (can) give rise to various arbitrarily named 1877 // sections being "Code" or "Data". 1878 sect_type = kalimbaSectionType(m_header, header); 1879 } 1880 1881 const uint32_t target_bytes_size = 1882 (eSectionTypeData == sect_type || eSectionTypeZeroFill == sect_type) ? 1883 m_arch_spec.GetDataByteSize() : 1884 eSectionTypeCode == sect_type ? 1885 m_arch_spec.GetCodeByteSize() : 1; 1886 1887 elf::elf_xword log2align = (header.sh_addralign==0) 1888 ? 0 1889 : llvm::Log2_64(header.sh_addralign); 1890 SectionSP section_sp (new Section(GetModule(), // Module to which this section belongs. 1891 this, // ObjectFile to which this section belongs and should read section data from. 1892 SectionIndex(I), // Section ID. 1893 name, // Section name. 1894 sect_type, // Section type. 1895 header.sh_addr, // VM address. 1896 vm_size, // VM size in bytes of this section. 1897 header.sh_offset, // Offset of this section in the file. 1898 file_size, // Size of the section as found in the file. 1899 log2align, // Alignment of the section 1900 header.sh_flags, // Flags for this section. 1901 target_bytes_size));// Number of host bytes per target byte 1902 1903 if (is_thread_specific) 1904 section_sp->SetIsThreadSpecific (is_thread_specific); 1905 m_sections_ap->AddSection(section_sp); 1906 } 1907 } 1908 1909 if (m_sections_ap.get()) 1910 { 1911 if (GetType() == eTypeDebugInfo) 1912 { 1913 static const SectionType g_sections[] = 1914 { 1915 eSectionTypeDWARFDebugAbbrev, 1916 eSectionTypeDWARFDebugAddr, 1917 eSectionTypeDWARFDebugAranges, 1918 eSectionTypeDWARFDebugFrame, 1919 eSectionTypeDWARFDebugInfo, 1920 eSectionTypeDWARFDebugLine, 1921 eSectionTypeDWARFDebugLoc, 1922 eSectionTypeDWARFDebugMacInfo, 1923 eSectionTypeDWARFDebugPubNames, 1924 eSectionTypeDWARFDebugPubTypes, 1925 eSectionTypeDWARFDebugRanges, 1926 eSectionTypeDWARFDebugStr, 1927 eSectionTypeDWARFDebugStrOffsets, 1928 eSectionTypeELFSymbolTable, 1929 }; 1930 SectionList *elf_section_list = m_sections_ap.get(); 1931 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx) 1932 { 1933 SectionType section_type = g_sections[idx]; 1934 SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true)); 1935 if (section_sp) 1936 { 1937 SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true)); 1938 if (module_section_sp) 1939 unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp); 1940 else 1941 unified_section_list.AddSection (section_sp); 1942 } 1943 } 1944 } 1945 else 1946 { 1947 unified_section_list = *m_sections_ap; 1948 } 1949 } 1950 } 1951 1952 // Find the arm/aarch64 mapping symbol character in the given symbol name. Mapping symbols have the 1953 // form of "$<char>[.<any>]*". Additionally we recognize cases when the mapping symbol prefixed by 1954 // an arbitrary string because if a symbol prefix added to each symbol in the object file with 1955 // objcopy then the mapping symbols are also prefixed. 1956 static char 1957 FindArmAarch64MappingSymbol(const char* symbol_name) 1958 { 1959 if (!symbol_name) 1960 return '\0'; 1961 1962 const char* dollar_pos = ::strchr(symbol_name, '$'); 1963 if (!dollar_pos || dollar_pos[1] == '\0') 1964 return '\0'; 1965 1966 if (dollar_pos[2] == '\0' || dollar_pos[2] == '.') 1967 return dollar_pos[1]; 1968 return '\0'; 1969 } 1970 1971 #define STO_MIPS_ISA (3 << 6) 1972 #define STO_MICROMIPS (2 << 6) 1973 #define IS_MICROMIPS(ST_OTHER) (((ST_OTHER) & STO_MIPS_ISA) == STO_MICROMIPS) 1974 1975 // private 1976 unsigned 1977 ObjectFileELF::ParseSymbols (Symtab *symtab, 1978 user_id_t start_id, 1979 SectionList *section_list, 1980 const size_t num_symbols, 1981 const DataExtractor &symtab_data, 1982 const DataExtractor &strtab_data) 1983 { 1984 ELFSymbol symbol; 1985 lldb::offset_t offset = 0; 1986 1987 static ConstString text_section_name(".text"); 1988 static ConstString init_section_name(".init"); 1989 static ConstString fini_section_name(".fini"); 1990 static ConstString ctors_section_name(".ctors"); 1991 static ConstString dtors_section_name(".dtors"); 1992 1993 static ConstString data_section_name(".data"); 1994 static ConstString rodata_section_name(".rodata"); 1995 static ConstString rodata1_section_name(".rodata1"); 1996 static ConstString data2_section_name(".data1"); 1997 static ConstString bss_section_name(".bss"); 1998 static ConstString opd_section_name(".opd"); // For ppc64 1999 2000 // On Android the oatdata and the oatexec symbols in system@framework@boot.oat covers the full 2001 // .text section what causes issues with displaying unusable symbol name to the user and very 2002 // slow unwinding speed because the instruction emulation based unwind plans try to emulate all 2003 // instructions in these symbols. Don't add these symbols to the symbol list as they have no 2004 // use for the debugger and they are causing a lot of trouble. 2005 // Filtering can't be restricted to Android because this special object file don't contain the 2006 // note section specifying the environment to Android but the custom extension and file name 2007 // makes it highly unlikely that this will collide with anything else. 2008 bool skip_oatdata_oatexec = m_file.GetFilename() == ConstString("system@framework@boot.oat"); 2009 2010 ArchSpec arch; 2011 GetArchitecture(arch); 2012 2013 // Local cache to avoid doing a FindSectionByName for each symbol. The "const char*" key must 2014 // came from a ConstString object so they can be compared by pointer 2015 std::unordered_map<const char*, lldb::SectionSP> section_name_to_section; 2016 2017 unsigned i; 2018 for (i = 0; i < num_symbols; ++i) 2019 { 2020 if (symbol.Parse(symtab_data, &offset) == false) 2021 break; 2022 2023 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 2024 2025 // No need to add non-section symbols that have no names 2026 if (symbol.getType() != STT_SECTION && 2027 (symbol_name == NULL || symbol_name[0] == '\0')) 2028 continue; 2029 2030 // Skipping oatdata and oatexec sections if it is requested. See details above the 2031 // definition of skip_oatdata_oatexec for the reasons. 2032 if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 || ::strcmp(symbol_name, "oatexec") == 0)) 2033 continue; 2034 2035 SectionSP symbol_section_sp; 2036 SymbolType symbol_type = eSymbolTypeInvalid; 2037 Elf64_Half symbol_idx = symbol.st_shndx; 2038 2039 switch (symbol_idx) 2040 { 2041 case SHN_ABS: 2042 symbol_type = eSymbolTypeAbsolute; 2043 break; 2044 case SHN_UNDEF: 2045 symbol_type = eSymbolTypeUndefined; 2046 break; 2047 default: 2048 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx); 2049 break; 2050 } 2051 2052 // If a symbol is undefined do not process it further even if it has a STT type 2053 if (symbol_type != eSymbolTypeUndefined) 2054 { 2055 switch (symbol.getType()) 2056 { 2057 default: 2058 case STT_NOTYPE: 2059 // The symbol's type is not specified. 2060 break; 2061 2062 case STT_OBJECT: 2063 // The symbol is associated with a data object, such as a variable, 2064 // an array, etc. 2065 symbol_type = eSymbolTypeData; 2066 break; 2067 2068 case STT_FUNC: 2069 // The symbol is associated with a function or other executable code. 2070 symbol_type = eSymbolTypeCode; 2071 break; 2072 2073 case STT_SECTION: 2074 // The symbol is associated with a section. Symbol table entries of 2075 // this type exist primarily for relocation and normally have 2076 // STB_LOCAL binding. 2077 break; 2078 2079 case STT_FILE: 2080 // Conventionally, the symbol's name gives the name of the source 2081 // file associated with the object file. A file symbol has STB_LOCAL 2082 // binding, its section index is SHN_ABS, and it precedes the other 2083 // STB_LOCAL symbols for the file, if it is present. 2084 symbol_type = eSymbolTypeSourceFile; 2085 break; 2086 2087 case STT_GNU_IFUNC: 2088 // The symbol is associated with an indirect function. The actual 2089 // function will be resolved if it is referenced. 2090 symbol_type = eSymbolTypeResolver; 2091 break; 2092 } 2093 } 2094 2095 if (symbol_type == eSymbolTypeInvalid) 2096 { 2097 if (symbol_section_sp) 2098 { 2099 const ConstString §_name = symbol_section_sp->GetName(); 2100 if (sect_name == text_section_name || 2101 sect_name == init_section_name || 2102 sect_name == fini_section_name || 2103 sect_name == ctors_section_name || 2104 sect_name == dtors_section_name) 2105 { 2106 symbol_type = eSymbolTypeCode; 2107 } 2108 else if (sect_name == data_section_name || 2109 sect_name == data2_section_name || 2110 sect_name == rodata_section_name || 2111 sect_name == rodata1_section_name || 2112 sect_name == bss_section_name) 2113 { 2114 symbol_type = eSymbolTypeData; 2115 } 2116 } 2117 } 2118 2119 int64_t symbol_value_offset = 0; 2120 uint32_t additional_flags = 0; 2121 2122 if (arch.IsValid()) 2123 { 2124 if (arch.GetMachine() == llvm::Triple::arm) 2125 { 2126 if (symbol.getBinding() == STB_LOCAL) 2127 { 2128 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); 2129 if (symbol_type == eSymbolTypeCode) 2130 { 2131 switch (mapping_symbol) 2132 { 2133 case 'a': 2134 // $a[.<any>]* - marks an ARM instruction sequence 2135 m_address_class_map[symbol.st_value] = eAddressClassCode; 2136 break; 2137 case 'b': 2138 case 't': 2139 // $b[.<any>]* - marks a THUMB BL instruction sequence 2140 // $t[.<any>]* - marks a THUMB instruction sequence 2141 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA; 2142 break; 2143 case 'd': 2144 // $d[.<any>]* - marks a data item sequence (e.g. lit pool) 2145 m_address_class_map[symbol.st_value] = eAddressClassData; 2146 break; 2147 } 2148 } 2149 if (mapping_symbol) 2150 continue; 2151 } 2152 } 2153 else if (arch.GetMachine() == llvm::Triple::aarch64) 2154 { 2155 if (symbol.getBinding() == STB_LOCAL) 2156 { 2157 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); 2158 if (symbol_type == eSymbolTypeCode) 2159 { 2160 switch (mapping_symbol) 2161 { 2162 case 'x': 2163 // $x[.<any>]* - marks an A64 instruction sequence 2164 m_address_class_map[symbol.st_value] = eAddressClassCode; 2165 break; 2166 case 'd': 2167 // $d[.<any>]* - marks a data item sequence (e.g. lit pool) 2168 m_address_class_map[symbol.st_value] = eAddressClassData; 2169 break; 2170 } 2171 } 2172 if (mapping_symbol) 2173 continue; 2174 } 2175 } 2176 2177 if (arch.GetMachine() == llvm::Triple::arm) 2178 { 2179 if (symbol_type == eSymbolTypeCode) 2180 { 2181 if (symbol.st_value & 1) 2182 { 2183 // Subtracting 1 from the address effectively unsets 2184 // the low order bit, which results in the address 2185 // actually pointing to the beginning of the symbol. 2186 // This delta will be used below in conjunction with 2187 // symbol.st_value to produce the final symbol_value 2188 // that we store in the symtab. 2189 symbol_value_offset = -1; 2190 m_address_class_map[symbol.st_value^1] = eAddressClassCodeAlternateISA; 2191 } 2192 else 2193 { 2194 // This address is ARM 2195 m_address_class_map[symbol.st_value] = eAddressClassCode; 2196 } 2197 } 2198 } 2199 2200 /* 2201 * MIPS: 2202 * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for MIPS). 2203 * This allows processer to switch between microMIPS and MIPS without any need 2204 * for special mode-control register. However, apart from .debug_line, none of 2205 * the ELF/DWARF sections set the ISA bit (for symbol or section). Use st_other 2206 * flag to check whether the symbol is microMIPS and then set the address class 2207 * accordingly. 2208 */ 2209 const llvm::Triple::ArchType llvm_arch = arch.GetMachine(); 2210 if (llvm_arch == llvm::Triple::mips || llvm_arch == llvm::Triple::mipsel 2211 || llvm_arch == llvm::Triple::mips64 || llvm_arch == llvm::Triple::mips64el) 2212 { 2213 if (IS_MICROMIPS(symbol.st_other)) 2214 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA; 2215 else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) 2216 { 2217 symbol.st_value = symbol.st_value & (~1ull); 2218 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA; 2219 } 2220 else 2221 { 2222 if (symbol_type == eSymbolTypeCode) 2223 m_address_class_map[symbol.st_value] = eAddressClassCode; 2224 else if (symbol_type == eSymbolTypeData) 2225 m_address_class_map[symbol.st_value] = eAddressClassData; 2226 else 2227 m_address_class_map[symbol.st_value] = eAddressClassUnknown; 2228 } 2229 } 2230 } 2231 2232 // symbol_value_offset may contain 0 for ARM symbols or -1 for 2233 // THUMB symbols. See above for more details. 2234 uint64_t symbol_value = symbol.st_value + symbol_value_offset; 2235 if (symbol_section_sp && CalculateType() != ObjectFile::Type::eTypeObjectFile) 2236 symbol_value -= symbol_section_sp->GetFileAddress(); 2237 2238 if (symbol_section_sp) 2239 { 2240 ModuleSP module_sp(GetModule()); 2241 if (module_sp) 2242 { 2243 SectionList *module_section_list = module_sp->GetSectionList(); 2244 if (module_section_list && module_section_list != section_list) 2245 { 2246 const ConstString §_name = symbol_section_sp->GetName(); 2247 auto section_it = section_name_to_section.find(sect_name.GetCString()); 2248 if (section_it == section_name_to_section.end()) 2249 section_it = section_name_to_section.emplace( 2250 sect_name.GetCString(), 2251 module_section_list->FindSectionByName (sect_name)).first; 2252 if (section_it->second && section_it->second->GetFileSize()) 2253 symbol_section_sp = section_it->second; 2254 } 2255 } 2256 } 2257 2258 bool is_global = symbol.getBinding() == STB_GLOBAL; 2259 uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags; 2260 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; 2261 2262 llvm::StringRef symbol_ref(symbol_name); 2263 2264 // Symbol names may contain @VERSION suffixes. Find those and strip them temporarily. 2265 size_t version_pos = symbol_ref.find('@'); 2266 bool has_suffix = version_pos != llvm::StringRef::npos; 2267 llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos); 2268 Mangled mangled(ConstString(symbol_bare), is_mangled); 2269 2270 // Now append the suffix back to mangled and unmangled names. Only do it if the 2271 // demangling was successful (string is not empty). 2272 if (has_suffix) 2273 { 2274 llvm::StringRef suffix = symbol_ref.substr(version_pos); 2275 2276 llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef(); 2277 if (! mangled_name.empty()) 2278 mangled.SetMangledName( ConstString((mangled_name + suffix).str()) ); 2279 2280 ConstString demangled = mangled.GetDemangledName(lldb::eLanguageTypeUnknown); 2281 llvm::StringRef demangled_name = demangled.GetStringRef(); 2282 if (!demangled_name.empty()) 2283 mangled.SetDemangledName( ConstString((demangled_name + suffix).str()) ); 2284 } 2285 2286 // In ELF all symbol should have a valid size but it is not true for some code symbols 2287 // coming from hand written assembly. As none of the code symbol should have 0 size we try 2288 // to calculate the size for these symbols in the symtab with saying that their original 2289 // size is not valid. 2290 bool symbol_size_valid = symbol.st_size != 0 || symbol_type != eSymbolTypeCode; 2291 2292 Symbol dc_symbol( 2293 i + start_id, // ID is the original symbol table index. 2294 mangled, 2295 symbol_type, // Type of this symbol 2296 is_global, // Is this globally visible? 2297 false, // Is this symbol debug info? 2298 false, // Is this symbol a trampoline? 2299 false, // Is this symbol artificial? 2300 AddressRange( 2301 symbol_section_sp, // Section in which this symbol is defined or null. 2302 symbol_value, // Offset in section or symbol value. 2303 symbol.st_size), // Size in bytes of this symbol. 2304 symbol_size_valid, // Symbol size is valid 2305 has_suffix, // Contains linker annotations? 2306 flags); // Symbol flags. 2307 symtab->AddSymbol(dc_symbol); 2308 } 2309 return i; 2310 } 2311 2312 unsigned 2313 ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, 2314 user_id_t start_id, 2315 lldb_private::Section *symtab) 2316 { 2317 if (symtab->GetObjectFile() != this) 2318 { 2319 // If the symbol table section is owned by a different object file, have it do the 2320 // parsing. 2321 ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile()); 2322 return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab); 2323 } 2324 2325 // Get section list for this object file. 2326 SectionList *section_list = m_sections_ap.get(); 2327 if (!section_list) 2328 return 0; 2329 2330 user_id_t symtab_id = symtab->GetID(); 2331 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 2332 assert(symtab_hdr->sh_type == SHT_SYMTAB || 2333 symtab_hdr->sh_type == SHT_DYNSYM); 2334 2335 // sh_link: section header index of associated string table. 2336 // Section ID's are ones based. 2337 user_id_t strtab_id = symtab_hdr->sh_link + 1; 2338 Section *strtab = section_list->FindSectionByID(strtab_id).get(); 2339 2340 if (symtab && strtab) 2341 { 2342 assert (symtab->GetObjectFile() == this); 2343 assert (strtab->GetObjectFile() == this); 2344 2345 DataExtractor symtab_data; 2346 DataExtractor strtab_data; 2347 if (ReadSectionData(symtab, symtab_data) && 2348 ReadSectionData(strtab, strtab_data)) 2349 { 2350 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; 2351 2352 return ParseSymbols(symbol_table, start_id, section_list, 2353 num_symbols, symtab_data, strtab_data); 2354 } 2355 } 2356 2357 return 0; 2358 } 2359 2360 size_t 2361 ObjectFileELF::ParseDynamicSymbols() 2362 { 2363 if (m_dynamic_symbols.size()) 2364 return m_dynamic_symbols.size(); 2365 2366 SectionList *section_list = GetSectionList(); 2367 if (!section_list) 2368 return 0; 2369 2370 // Find the SHT_DYNAMIC section. 2371 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get(); 2372 if (!dynsym) 2373 return 0; 2374 assert (dynsym->GetObjectFile() == this); 2375 2376 ELFDynamic symbol; 2377 DataExtractor dynsym_data; 2378 if (ReadSectionData(dynsym, dynsym_data)) 2379 { 2380 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 2381 lldb::offset_t cursor = 0; 2382 2383 while (cursor < section_size) 2384 { 2385 if (!symbol.Parse(dynsym_data, &cursor)) 2386 break; 2387 2388 m_dynamic_symbols.push_back(symbol); 2389 } 2390 } 2391 2392 return m_dynamic_symbols.size(); 2393 } 2394 2395 const ELFDynamic * 2396 ObjectFileELF::FindDynamicSymbol(unsigned tag) 2397 { 2398 if (!ParseDynamicSymbols()) 2399 return NULL; 2400 2401 DynamicSymbolCollIter I = m_dynamic_symbols.begin(); 2402 DynamicSymbolCollIter E = m_dynamic_symbols.end(); 2403 for ( ; I != E; ++I) 2404 { 2405 ELFDynamic *symbol = &*I; 2406 2407 if (symbol->d_tag == tag) 2408 return symbol; 2409 } 2410 2411 return NULL; 2412 } 2413 2414 unsigned 2415 ObjectFileELF::PLTRelocationType() 2416 { 2417 // DT_PLTREL 2418 // This member specifies the type of relocation entry to which the 2419 // procedure linkage table refers. The d_val member holds DT_REL or 2420 // DT_RELA, as appropriate. All relocations in a procedure linkage table 2421 // must use the same relocation. 2422 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); 2423 2424 if (symbol) 2425 return symbol->d_val; 2426 2427 return 0; 2428 } 2429 2430 // Returns the size of the normal plt entries and the offset of the first normal plt entry. The 2431 // 0th entry in the plt table is usually a resolution entry which have different size in some 2432 // architectures then the rest of the plt entries. 2433 static std::pair<uint64_t, uint64_t> 2434 GetPltEntrySizeAndOffset(const ELFSectionHeader* rel_hdr, const ELFSectionHeader* plt_hdr) 2435 { 2436 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2437 2438 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 bytes. 2439 // So round the entsize up by the alignment if addralign is set. 2440 elf_xword plt_entsize = plt_hdr->sh_addralign ? 2441 llvm::alignTo (plt_hdr->sh_entsize, plt_hdr->sh_addralign) : plt_hdr->sh_entsize; 2442 2443 if (plt_entsize == 0) 2444 { 2445 // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the size of the plt 2446 // entries based on the number of entries and the size of the plt section with the 2447 // assumption that the size of the 0th entry is at least as big as the size of the normal 2448 // entries and it isn't much bigger then that. 2449 if (plt_hdr->sh_addralign) 2450 plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign / (num_relocations + 1) * plt_hdr->sh_addralign; 2451 else 2452 plt_entsize = plt_hdr->sh_size / (num_relocations + 1); 2453 } 2454 2455 elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize; 2456 2457 return std::make_pair(plt_entsize, plt_offset); 2458 } 2459 2460 static unsigned 2461 ParsePLTRelocations(Symtab *symbol_table, 2462 user_id_t start_id, 2463 unsigned rel_type, 2464 const ELFHeader *hdr, 2465 const ELFSectionHeader *rel_hdr, 2466 const ELFSectionHeader *plt_hdr, 2467 const ELFSectionHeader *sym_hdr, 2468 const lldb::SectionSP &plt_section_sp, 2469 DataExtractor &rel_data, 2470 DataExtractor &symtab_data, 2471 DataExtractor &strtab_data) 2472 { 2473 ELFRelocation rel(rel_type); 2474 ELFSymbol symbol; 2475 lldb::offset_t offset = 0; 2476 2477 uint64_t plt_offset, plt_entsize; 2478 std::tie(plt_entsize, plt_offset) = GetPltEntrySizeAndOffset(rel_hdr, plt_hdr); 2479 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2480 2481 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 2482 reloc_info_fn reloc_type; 2483 reloc_info_fn reloc_symbol; 2484 2485 if (hdr->Is32Bit()) 2486 { 2487 reloc_type = ELFRelocation::RelocType32; 2488 reloc_symbol = ELFRelocation::RelocSymbol32; 2489 } 2490 else 2491 { 2492 reloc_type = ELFRelocation::RelocType64; 2493 reloc_symbol = ELFRelocation::RelocSymbol64; 2494 } 2495 2496 unsigned slot_type = hdr->GetRelocationJumpSlotType(); 2497 unsigned i; 2498 for (i = 0; i < num_relocations; ++i) 2499 { 2500 if (rel.Parse(rel_data, &offset) == false) 2501 break; 2502 2503 if (reloc_type(rel) != slot_type) 2504 continue; 2505 2506 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; 2507 if (!symbol.Parse(symtab_data, &symbol_offset)) 2508 break; 2509 2510 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 2511 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; 2512 uint64_t plt_index = plt_offset + i * plt_entsize; 2513 2514 Symbol jump_symbol( 2515 i + start_id, // Symbol table index 2516 symbol_name, // symbol name. 2517 is_mangled, // is the symbol name mangled? 2518 eSymbolTypeTrampoline, // Type of this symbol 2519 false, // Is this globally visible? 2520 false, // Is this symbol debug info? 2521 true, // Is this symbol a trampoline? 2522 true, // Is this symbol artificial? 2523 plt_section_sp, // Section in which this symbol is defined or null. 2524 plt_index, // Offset in section or symbol value. 2525 plt_entsize, // Size in bytes of this symbol. 2526 true, // Size is valid 2527 false, // Contains linker annotations? 2528 0); // Symbol flags. 2529 2530 symbol_table->AddSymbol(jump_symbol); 2531 } 2532 2533 return i; 2534 } 2535 2536 unsigned 2537 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, 2538 user_id_t start_id, 2539 const ELFSectionHeaderInfo *rel_hdr, 2540 user_id_t rel_id) 2541 { 2542 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 2543 2544 // The link field points to the associated symbol table. The info field 2545 // points to the section holding the plt. 2546 user_id_t symtab_id = rel_hdr->sh_link; 2547 user_id_t plt_id = rel_hdr->sh_info; 2548 2549 // If the link field doesn't point to the appropriate symbol name table then 2550 // try to find it by name as some compiler don't fill in the link fields. 2551 if (!symtab_id) 2552 symtab_id = GetSectionIndexByName(".dynsym"); 2553 if (!plt_id) 2554 plt_id = GetSectionIndexByName(".plt"); 2555 2556 if (!symtab_id || !plt_id) 2557 return 0; 2558 2559 // Section ID's are ones based; 2560 symtab_id++; 2561 plt_id++; 2562 2563 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); 2564 if (!plt_hdr) 2565 return 0; 2566 2567 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); 2568 if (!sym_hdr) 2569 return 0; 2570 2571 SectionList *section_list = m_sections_ap.get(); 2572 if (!section_list) 2573 return 0; 2574 2575 Section *rel_section = section_list->FindSectionByID(rel_id).get(); 2576 if (!rel_section) 2577 return 0; 2578 2579 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id)); 2580 if (!plt_section_sp) 2581 return 0; 2582 2583 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 2584 if (!symtab) 2585 return 0; 2586 2587 // sh_link points to associated string table. 2588 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get(); 2589 if (!strtab) 2590 return 0; 2591 2592 DataExtractor rel_data; 2593 if (!ReadSectionData(rel_section, rel_data)) 2594 return 0; 2595 2596 DataExtractor symtab_data; 2597 if (!ReadSectionData(symtab, symtab_data)) 2598 return 0; 2599 2600 DataExtractor strtab_data; 2601 if (!ReadSectionData(strtab, strtab_data)) 2602 return 0; 2603 2604 unsigned rel_type = PLTRelocationType(); 2605 if (!rel_type) 2606 return 0; 2607 2608 return ParsePLTRelocations (symbol_table, 2609 start_id, 2610 rel_type, 2611 &m_header, 2612 rel_hdr, 2613 plt_hdr, 2614 sym_hdr, 2615 plt_section_sp, 2616 rel_data, 2617 symtab_data, 2618 strtab_data); 2619 } 2620 2621 unsigned 2622 ObjectFileELF::RelocateSection(Symtab* symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, 2623 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr, 2624 DataExtractor &rel_data, DataExtractor &symtab_data, 2625 DataExtractor &debug_data, Section* rel_section) 2626 { 2627 ELFRelocation rel(rel_hdr->sh_type); 2628 lldb::addr_t offset = 0; 2629 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2630 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 2631 reloc_info_fn reloc_type; 2632 reloc_info_fn reloc_symbol; 2633 2634 if (hdr->Is32Bit()) 2635 { 2636 reloc_type = ELFRelocation::RelocType32; 2637 reloc_symbol = ELFRelocation::RelocSymbol32; 2638 } 2639 else 2640 { 2641 reloc_type = ELFRelocation::RelocType64; 2642 reloc_symbol = ELFRelocation::RelocSymbol64; 2643 } 2644 2645 for (unsigned i = 0; i < num_relocations; ++i) 2646 { 2647 if (rel.Parse(rel_data, &offset) == false) 2648 break; 2649 2650 Symbol* symbol = NULL; 2651 2652 if (hdr->Is32Bit()) 2653 { 2654 switch (reloc_type(rel)) { 2655 case R_386_32: 2656 case R_386_PC32: 2657 default: 2658 assert(false && "unexpected relocation type"); 2659 } 2660 } else { 2661 switch (reloc_type(rel)) { 2662 case R_X86_64_64: 2663 { 2664 symbol = symtab->FindSymbolByID(reloc_symbol(rel)); 2665 if (symbol) 2666 { 2667 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2668 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer(); 2669 uint64_t* dst = reinterpret_cast<uint64_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset64(rel)); 2670 *dst = value + ELFRelocation::RelocAddend64(rel); 2671 } 2672 break; 2673 } 2674 case R_X86_64_32: 2675 case R_X86_64_32S: 2676 { 2677 symbol = symtab->FindSymbolByID(reloc_symbol(rel)); 2678 if (symbol) 2679 { 2680 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2681 value += ELFRelocation::RelocAddend32(rel); 2682 assert((reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) || 2683 (reloc_type(rel) == R_X86_64_32S && 2684 ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN))); 2685 uint32_t truncated_addr = (value & 0xFFFFFFFF); 2686 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer(); 2687 uint32_t* dst = reinterpret_cast<uint32_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel)); 2688 *dst = truncated_addr; 2689 } 2690 break; 2691 } 2692 case R_X86_64_PC32: 2693 default: 2694 assert(false && "unexpected relocation type"); 2695 } 2696 } 2697 } 2698 2699 return 0; 2700 } 2701 2702 unsigned 2703 ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, user_id_t rel_id) 2704 { 2705 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 2706 2707 // Parse in the section list if needed. 2708 SectionList *section_list = GetSectionList(); 2709 if (!section_list) 2710 return 0; 2711 2712 // Section ID's are ones based. 2713 user_id_t symtab_id = rel_hdr->sh_link + 1; 2714 user_id_t debug_id = rel_hdr->sh_info + 1; 2715 2716 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 2717 if (!symtab_hdr) 2718 return 0; 2719 2720 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id); 2721 if (!debug_hdr) 2722 return 0; 2723 2724 Section *rel = section_list->FindSectionByID(rel_id).get(); 2725 if (!rel) 2726 return 0; 2727 2728 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 2729 if (!symtab) 2730 return 0; 2731 2732 Section *debug = section_list->FindSectionByID(debug_id).get(); 2733 if (!debug) 2734 return 0; 2735 2736 DataExtractor rel_data; 2737 DataExtractor symtab_data; 2738 DataExtractor debug_data; 2739 2740 if (ReadSectionData(rel, rel_data) && 2741 ReadSectionData(symtab, symtab_data) && 2742 ReadSectionData(debug, debug_data)) 2743 { 2744 RelocateSection(m_symtab_ap.get(), &m_header, rel_hdr, symtab_hdr, debug_hdr, 2745 rel_data, symtab_data, debug_data, debug); 2746 } 2747 2748 return 0; 2749 } 2750 2751 Symtab * 2752 ObjectFileELF::GetSymtab() 2753 { 2754 ModuleSP module_sp(GetModule()); 2755 if (!module_sp) 2756 return NULL; 2757 2758 // We always want to use the main object file so we (hopefully) only have one cached copy 2759 // of our symtab, dynamic sections, etc. 2760 ObjectFile *module_obj_file = module_sp->GetObjectFile(); 2761 if (module_obj_file && module_obj_file != this) 2762 return module_obj_file->GetSymtab(); 2763 2764 if (m_symtab_ap.get() == NULL) 2765 { 2766 SectionList *section_list = module_sp->GetSectionList(); 2767 if (!section_list) 2768 return NULL; 2769 2770 uint64_t symbol_id = 0; 2771 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 2772 2773 // Sharable objects and dynamic executables usually have 2 distinct symbol 2774 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller 2775 // version of the symtab that only contains global symbols. The information found 2776 // in the dynsym is therefore also found in the symtab, while the reverse is not 2777 // necessarily true. 2778 Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get(); 2779 if (!symtab) 2780 { 2781 // The symtab section is non-allocable and can be stripped, so if it doesn't exist 2782 // then use the dynsym section which should always be there. 2783 symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get(); 2784 } 2785 if (symtab) 2786 { 2787 m_symtab_ap.reset(new Symtab(symtab->GetObjectFile())); 2788 symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab); 2789 } 2790 2791 // DT_JMPREL 2792 // If present, this entry's d_ptr member holds the address of relocation 2793 // entries associated solely with the procedure linkage table. Separating 2794 // these relocation entries lets the dynamic linker ignore them during 2795 // process initialization, if lazy binding is enabled. If this entry is 2796 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must 2797 // also be present. 2798 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); 2799 if (symbol) 2800 { 2801 // Synthesize trampoline symbols to help navigate the PLT. 2802 addr_t addr = symbol->d_ptr; 2803 Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get(); 2804 if (reloc_section) 2805 { 2806 user_id_t reloc_id = reloc_section->GetID(); 2807 const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id); 2808 assert(reloc_header); 2809 2810 if (m_symtab_ap == nullptr) 2811 m_symtab_ap.reset(new Symtab(reloc_section->GetObjectFile())); 2812 2813 ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id); 2814 } 2815 } 2816 2817 // If we still don't have any symtab then create an empty instance to avoid do the section 2818 // lookup next time. 2819 if (m_symtab_ap == nullptr) 2820 m_symtab_ap.reset(new Symtab(this)); 2821 2822 m_symtab_ap->CalculateSymbolSizes(); 2823 } 2824 2825 for (SectionHeaderCollIter I = m_section_headers.begin(); 2826 I != m_section_headers.end(); ++I) 2827 { 2828 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) 2829 { 2830 if (CalculateType() == eTypeObjectFile) 2831 { 2832 const char *section_name = I->section_name.AsCString(""); 2833 if (strstr(section_name, ".rela.debug") || 2834 strstr(section_name, ".rel.debug")) 2835 { 2836 const ELFSectionHeader &reloc_header = *I; 2837 user_id_t reloc_id = SectionIndex(I); 2838 RelocateDebugSections(&reloc_header, reloc_id); 2839 } 2840 } 2841 } 2842 } 2843 return m_symtab_ap.get(); 2844 } 2845 2846 Symbol * 2847 ObjectFileELF::ResolveSymbolForAddress(const Address& so_addr, bool verify_unique) 2848 { 2849 if (!m_symtab_ap.get()) 2850 return nullptr; // GetSymtab() should be called first. 2851 2852 const SectionList *section_list = GetSectionList(); 2853 if (!section_list) 2854 return nullptr; 2855 2856 if (DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo()) 2857 { 2858 AddressRange range; 2859 if (eh_frame->GetAddressRange (so_addr, range)) 2860 { 2861 const addr_t file_addr = range.GetBaseAddress().GetFileAddress(); 2862 Symbol * symbol = verify_unique ? m_symtab_ap->FindSymbolContainingFileAddress(file_addr) : nullptr; 2863 if (symbol) 2864 return symbol; 2865 2866 // Note that a (stripped) symbol won't be found by GetSymtab()... 2867 lldb::SectionSP eh_sym_section_sp = section_list->FindSectionContainingFileAddress(file_addr); 2868 if (eh_sym_section_sp.get()) 2869 { 2870 addr_t section_base = eh_sym_section_sp->GetFileAddress(); 2871 addr_t offset = file_addr - section_base; 2872 uint64_t symbol_id = m_symtab_ap->GetNumSymbols(); 2873 2874 Symbol eh_symbol( 2875 symbol_id, // Symbol table index. 2876 "???", // Symbol name. 2877 false, // Is the symbol name mangled? 2878 eSymbolTypeCode, // Type of this symbol. 2879 true, // Is this globally visible? 2880 false, // Is this symbol debug info? 2881 false, // Is this symbol a trampoline? 2882 true, // Is this symbol artificial? 2883 eh_sym_section_sp, // Section in which this symbol is defined or null. 2884 offset, // Offset in section or symbol value. 2885 range.GetByteSize(), // Size in bytes of this symbol. 2886 true, // Size is valid. 2887 false, // Contains linker annotations? 2888 0); // Symbol flags. 2889 if (symbol_id == m_symtab_ap->AddSymbol(eh_symbol)) 2890 return m_symtab_ap->SymbolAtIndex(symbol_id); 2891 } 2892 } 2893 } 2894 return nullptr; 2895 } 2896 2897 2898 bool 2899 ObjectFileELF::IsStripped () 2900 { 2901 // TODO: determine this for ELF 2902 return false; 2903 } 2904 2905 //===----------------------------------------------------------------------===// 2906 // Dump 2907 // 2908 // Dump the specifics of the runtime file container (such as any headers 2909 // segments, sections, etc). 2910 //---------------------------------------------------------------------- 2911 void 2912 ObjectFileELF::Dump(Stream *s) 2913 { 2914 DumpELFHeader(s, m_header); 2915 s->EOL(); 2916 DumpELFProgramHeaders(s); 2917 s->EOL(); 2918 DumpELFSectionHeaders(s); 2919 s->EOL(); 2920 SectionList *section_list = GetSectionList(); 2921 if (section_list) 2922 section_list->Dump(s, NULL, true, UINT32_MAX); 2923 Symtab *symtab = GetSymtab(); 2924 if (symtab) 2925 symtab->Dump(s, NULL, eSortOrderNone); 2926 s->EOL(); 2927 DumpDependentModules(s); 2928 s->EOL(); 2929 } 2930 2931 //---------------------------------------------------------------------- 2932 // DumpELFHeader 2933 // 2934 // Dump the ELF header to the specified output stream 2935 //---------------------------------------------------------------------- 2936 void 2937 ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) 2938 { 2939 s->PutCString("ELF Header\n"); 2940 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); 2941 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", 2942 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]); 2943 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", 2944 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]); 2945 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", 2946 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]); 2947 2948 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); 2949 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); 2950 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); 2951 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); 2952 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); 2953 2954 s->Printf("e_type = 0x%4.4x ", header.e_type); 2955 DumpELFHeader_e_type(s, header.e_type); 2956 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); 2957 s->Printf("e_version = 0x%8.8x\n", header.e_version); 2958 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); 2959 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); 2960 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); 2961 s->Printf("e_flags = 0x%8.8x\n", header.e_flags); 2962 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); 2963 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); 2964 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum); 2965 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); 2966 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum); 2967 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx); 2968 } 2969 2970 //---------------------------------------------------------------------- 2971 // DumpELFHeader_e_type 2972 // 2973 // Dump an token value for the ELF header member e_type 2974 //---------------------------------------------------------------------- 2975 void 2976 ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) 2977 { 2978 switch (e_type) 2979 { 2980 case ET_NONE: *s << "ET_NONE"; break; 2981 case ET_REL: *s << "ET_REL"; break; 2982 case ET_EXEC: *s << "ET_EXEC"; break; 2983 case ET_DYN: *s << "ET_DYN"; break; 2984 case ET_CORE: *s << "ET_CORE"; break; 2985 default: 2986 break; 2987 } 2988 } 2989 2990 //---------------------------------------------------------------------- 2991 // DumpELFHeader_e_ident_EI_DATA 2992 // 2993 // Dump an token value for the ELF header member e_ident[EI_DATA] 2994 //---------------------------------------------------------------------- 2995 void 2996 ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) 2997 { 2998 switch (ei_data) 2999 { 3000 case ELFDATANONE: *s << "ELFDATANONE"; break; 3001 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break; 3002 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break; 3003 default: 3004 break; 3005 } 3006 } 3007 3008 3009 //---------------------------------------------------------------------- 3010 // DumpELFProgramHeader 3011 // 3012 // Dump a single ELF program header to the specified output stream 3013 //---------------------------------------------------------------------- 3014 void 3015 ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph) 3016 { 3017 DumpELFProgramHeader_p_type(s, ph.p_type); 3018 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr); 3019 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags); 3020 3021 DumpELFProgramHeader_p_flags(s, ph.p_flags); 3022 s->Printf(") %8.8" PRIx64, ph.p_align); 3023 } 3024 3025 //---------------------------------------------------------------------- 3026 // DumpELFProgramHeader_p_type 3027 // 3028 // Dump an token value for the ELF program header member p_type which 3029 // describes the type of the program header 3030 // ---------------------------------------------------------------------- 3031 void 3032 ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) 3033 { 3034 const int kStrWidth = 15; 3035 switch (p_type) 3036 { 3037 CASE_AND_STREAM(s, PT_NULL , kStrWidth); 3038 CASE_AND_STREAM(s, PT_LOAD , kStrWidth); 3039 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth); 3040 CASE_AND_STREAM(s, PT_INTERP , kStrWidth); 3041 CASE_AND_STREAM(s, PT_NOTE , kStrWidth); 3042 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth); 3043 CASE_AND_STREAM(s, PT_PHDR , kStrWidth); 3044 CASE_AND_STREAM(s, PT_TLS , kStrWidth); 3045 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); 3046 default: 3047 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); 3048 break; 3049 } 3050 } 3051 3052 3053 //---------------------------------------------------------------------- 3054 // DumpELFProgramHeader_p_flags 3055 // 3056 // Dump an token value for the ELF program header member p_flags 3057 //---------------------------------------------------------------------- 3058 void 3059 ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) 3060 { 3061 *s << ((p_flags & PF_X) ? "PF_X" : " ") 3062 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') 3063 << ((p_flags & PF_W) ? "PF_W" : " ") 3064 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') 3065 << ((p_flags & PF_R) ? "PF_R" : " "); 3066 } 3067 3068 //---------------------------------------------------------------------- 3069 // DumpELFProgramHeaders 3070 // 3071 // Dump all of the ELF program header to the specified output stream 3072 //---------------------------------------------------------------------- 3073 void 3074 ObjectFileELF::DumpELFProgramHeaders(Stream *s) 3075 { 3076 if (!ParseProgramHeaders()) 3077 return; 3078 3079 s->PutCString("Program Headers\n"); 3080 s->PutCString("IDX p_type p_offset p_vaddr p_paddr " 3081 "p_filesz p_memsz p_flags p_align\n"); 3082 s->PutCString("==== --------------- -------- -------- -------- " 3083 "-------- -------- ------------------------- --------\n"); 3084 3085 uint32_t idx = 0; 3086 for (ProgramHeaderCollConstIter I = m_program_headers.begin(); 3087 I != m_program_headers.end(); ++I, ++idx) 3088 { 3089 s->Printf("[%2u] ", idx); 3090 ObjectFileELF::DumpELFProgramHeader(s, *I); 3091 s->EOL(); 3092 } 3093 } 3094 3095 //---------------------------------------------------------------------- 3096 // DumpELFSectionHeader 3097 // 3098 // Dump a single ELF section header to the specified output stream 3099 //---------------------------------------------------------------------- 3100 void 3101 ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh) 3102 { 3103 s->Printf("%8.8x ", sh.sh_name); 3104 DumpELFSectionHeader_sh_type(s, sh.sh_type); 3105 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); 3106 DumpELFSectionHeader_sh_flags(s, sh.sh_flags); 3107 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, sh.sh_offset, sh.sh_size); 3108 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); 3109 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); 3110 } 3111 3112 //---------------------------------------------------------------------- 3113 // DumpELFSectionHeader_sh_type 3114 // 3115 // Dump an token value for the ELF section header member sh_type which 3116 // describes the type of the section 3117 //---------------------------------------------------------------------- 3118 void 3119 ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) 3120 { 3121 const int kStrWidth = 12; 3122 switch (sh_type) 3123 { 3124 CASE_AND_STREAM(s, SHT_NULL , kStrWidth); 3125 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth); 3126 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth); 3127 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth); 3128 CASE_AND_STREAM(s, SHT_RELA , kStrWidth); 3129 CASE_AND_STREAM(s, SHT_HASH , kStrWidth); 3130 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth); 3131 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth); 3132 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth); 3133 CASE_AND_STREAM(s, SHT_REL , kStrWidth); 3134 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth); 3135 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth); 3136 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth); 3137 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth); 3138 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth); 3139 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth); 3140 default: 3141 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); 3142 break; 3143 } 3144 } 3145 3146 //---------------------------------------------------------------------- 3147 // DumpELFSectionHeader_sh_flags 3148 // 3149 // Dump an token value for the ELF section header member sh_flags 3150 //---------------------------------------------------------------------- 3151 void 3152 ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags) 3153 { 3154 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") 3155 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') 3156 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") 3157 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') 3158 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); 3159 } 3160 3161 //---------------------------------------------------------------------- 3162 // DumpELFSectionHeaders 3163 // 3164 // Dump all of the ELF section header to the specified output stream 3165 //---------------------------------------------------------------------- 3166 void 3167 ObjectFileELF::DumpELFSectionHeaders(Stream *s) 3168 { 3169 if (!ParseSectionHeaders()) 3170 return; 3171 3172 s->PutCString("Section Headers\n"); 3173 s->PutCString("IDX name type flags " 3174 "addr offset size link info addralgn " 3175 "entsize Name\n"); 3176 s->PutCString("==== -------- ------------ -------------------------------- " 3177 "-------- -------- -------- -------- -------- -------- " 3178 "-------- ====================\n"); 3179 3180 uint32_t idx = 0; 3181 for (SectionHeaderCollConstIter I = m_section_headers.begin(); 3182 I != m_section_headers.end(); ++I, ++idx) 3183 { 3184 s->Printf("[%2u] ", idx); 3185 ObjectFileELF::DumpELFSectionHeader(s, *I); 3186 const char* section_name = I->section_name.AsCString(""); 3187 if (section_name) 3188 *s << ' ' << section_name << "\n"; 3189 } 3190 } 3191 3192 void 3193 ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) 3194 { 3195 size_t num_modules = ParseDependentModules(); 3196 3197 if (num_modules > 0) 3198 { 3199 s->PutCString("Dependent Modules:\n"); 3200 for (unsigned i = 0; i < num_modules; ++i) 3201 { 3202 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); 3203 s->Printf(" %s\n", spec.GetFilename().GetCString()); 3204 } 3205 } 3206 } 3207 3208 bool 3209 ObjectFileELF::GetArchitecture (ArchSpec &arch) 3210 { 3211 if (!ParseHeader()) 3212 return false; 3213 3214 if (m_section_headers.empty()) 3215 { 3216 // Allow elf notes to be parsed which may affect the detected architecture. 3217 ParseSectionHeaders(); 3218 } 3219 3220 if (CalculateType() == eTypeCoreFile && m_arch_spec.TripleOSIsUnspecifiedUnknown()) 3221 { 3222 // Core files don't have section headers yet they have PT_NOTE program headers 3223 // that might shed more light on the architecture 3224 if (ParseProgramHeaders()) 3225 { 3226 for (size_t i = 0, count = GetProgramHeaderCount(); i < count; ++i) 3227 { 3228 const elf::ELFProgramHeader* header = GetProgramHeaderByIndex(i); 3229 if (header && header->p_type == PT_NOTE && header->p_offset != 0 && header->p_filesz > 0) 3230 { 3231 DataExtractor data; 3232 if (data.SetData (m_data, header->p_offset, header->p_filesz) == header->p_filesz) 3233 { 3234 lldb_private::UUID uuid; 3235 RefineModuleDetailsFromNote (data, m_arch_spec, uuid); 3236 } 3237 } 3238 } 3239 } 3240 } 3241 arch = m_arch_spec; 3242 return true; 3243 } 3244 3245 ObjectFile::Type 3246 ObjectFileELF::CalculateType() 3247 { 3248 switch (m_header.e_type) 3249 { 3250 case llvm::ELF::ET_NONE: 3251 // 0 - No file type 3252 return eTypeUnknown; 3253 3254 case llvm::ELF::ET_REL: 3255 // 1 - Relocatable file 3256 return eTypeObjectFile; 3257 3258 case llvm::ELF::ET_EXEC: 3259 // 2 - Executable file 3260 return eTypeExecutable; 3261 3262 case llvm::ELF::ET_DYN: 3263 // 3 - Shared object file 3264 return eTypeSharedLibrary; 3265 3266 case ET_CORE: 3267 // 4 - Core file 3268 return eTypeCoreFile; 3269 3270 default: 3271 break; 3272 } 3273 return eTypeUnknown; 3274 } 3275 3276 ObjectFile::Strata 3277 ObjectFileELF::CalculateStrata() 3278 { 3279 switch (m_header.e_type) 3280 { 3281 case llvm::ELF::ET_NONE: 3282 // 0 - No file type 3283 return eStrataUnknown; 3284 3285 case llvm::ELF::ET_REL: 3286 // 1 - Relocatable file 3287 return eStrataUnknown; 3288 3289 case llvm::ELF::ET_EXEC: 3290 // 2 - Executable file 3291 // TODO: is there any way to detect that an executable is a kernel 3292 // related executable by inspecting the program headers, section 3293 // headers, symbols, or any other flag bits??? 3294 return eStrataUser; 3295 3296 case llvm::ELF::ET_DYN: 3297 // 3 - Shared object file 3298 // TODO: is there any way to detect that an shared library is a kernel 3299 // related executable by inspecting the program headers, section 3300 // headers, symbols, or any other flag bits??? 3301 return eStrataUnknown; 3302 3303 case ET_CORE: 3304 // 4 - Core file 3305 // TODO: is there any way to detect that an core file is a kernel 3306 // related executable by inspecting the program headers, section 3307 // headers, symbols, or any other flag bits??? 3308 return eStrataUnknown; 3309 3310 default: 3311 break; 3312 } 3313 return eStrataUnknown; 3314 } 3315 3316