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