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