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