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/Error.h" 29 #include "lldb/Utility/Log.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 lldb_private::Timer scoped_timer( 731 LLVM_PRETTY_FUNCTION, 732 "Calculating module crc32 %s with size %" PRIu64 " KiB", 733 file.GetLastPathComponent().AsCString(), 734 (file.GetByteSize() - file_offset) / 1024); 735 736 // For core files - which usually don't happen to have a 737 // gnu_debuglink, and are pretty bulky - calculating whole 738 // contents crc32 would be too much of luxury. Thus we will need 739 // to fallback to something simpler. 740 if (header.e_type == llvm::ELF::ET_CORE) { 741 size_t program_headers_end = 742 header.e_phoff + header.e_phnum * header.e_phentsize; 743 if (program_headers_end > data_sp->GetByteSize()) { 744 data_sp = DataBufferLLVM::CreateSliceFromPath( 745 file.GetPath(), program_headers_end, file_offset); 746 if (data_sp) 747 data.SetData(data_sp); 748 } 749 ProgramHeaderColl program_headers; 750 GetProgramHeaderInfo(program_headers, set_data, header); 751 752 size_t segment_data_end = 0; 753 for (ProgramHeaderCollConstIter I = program_headers.begin(); 754 I != program_headers.end(); ++I) { 755 segment_data_end = std::max<unsigned long long>( 756 I->p_offset + I->p_filesz, segment_data_end); 757 } 758 759 if (segment_data_end > data_sp->GetByteSize()) { 760 data_sp = DataBufferLLVM::CreateSliceFromPath( 761 file.GetPath(), segment_data_end, file_offset); 762 if (data_sp) 763 data.SetData(data_sp); 764 } 765 766 core_notes_crc = 767 CalculateELFNotesSegmentsCRC32(program_headers, data); 768 } else { 769 // Need to map entire file into memory to calculate the crc. 770 data_sp = DataBufferLLVM::CreateSliceFromPath(file.GetPath(), -1, 771 file_offset); 772 if (data_sp) { 773 data.SetData(data_sp); 774 gnu_debuglink_crc = calc_gnu_debuglink_crc32( 775 data.GetDataStart(), data.GetByteSize()); 776 } 777 } 778 } 779 if (gnu_debuglink_crc) { 780 // Use 4 bytes of crc from the .gnu_debuglink section. 781 uint32_t uuidt[4] = {gnu_debuglink_crc, 0, 0, 0}; 782 uuid.SetBytes(uuidt, sizeof(uuidt)); 783 } else if (core_notes_crc) { 784 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make 785 // it look different form 786 // .gnu_debuglink crc followed by 4 bytes of note segments crc. 787 uint32_t uuidt[4] = {g_core_uuid_magic, core_notes_crc, 0, 0}; 788 uuid.SetBytes(uuidt, sizeof(uuidt)); 789 } 790 } 791 792 specs.Append(spec); 793 } 794 } 795 } 796 } 797 798 return specs.GetSize() - initial_count; 799 } 800 801 //------------------------------------------------------------------ 802 // PluginInterface protocol 803 //------------------------------------------------------------------ 804 lldb_private::ConstString ObjectFileELF::GetPluginName() { 805 return GetPluginNameStatic(); 806 } 807 808 uint32_t ObjectFileELF::GetPluginVersion() { return m_plugin_version; } 809 //------------------------------------------------------------------ 810 // ObjectFile protocol 811 //------------------------------------------------------------------ 812 813 ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp, 814 DataBufferSP &data_sp, lldb::offset_t data_offset, 815 const FileSpec *file, lldb::offset_t file_offset, 816 lldb::offset_t length) 817 : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 818 m_header(), m_uuid(), m_gnu_debuglink_file(), m_gnu_debuglink_crc(0), 819 m_program_headers(), m_section_headers(), m_dynamic_symbols(), 820 m_filespec_ap(), m_entry_point_address(), m_arch_spec() { 821 if (file) 822 m_file = *file; 823 ::memset(&m_header, 0, sizeof(m_header)); 824 } 825 826 ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp, 827 DataBufferSP &header_data_sp, 828 const lldb::ProcessSP &process_sp, 829 addr_t header_addr) 830 : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 831 m_header(), m_uuid(), m_gnu_debuglink_file(), m_gnu_debuglink_crc(0), 832 m_program_headers(), m_section_headers(), m_dynamic_symbols(), 833 m_filespec_ap(), m_entry_point_address(), m_arch_spec() { 834 ::memset(&m_header, 0, sizeof(m_header)); 835 } 836 837 ObjectFileELF::~ObjectFileELF() {} 838 839 bool ObjectFileELF::IsExecutable() const { 840 return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0); 841 } 842 843 bool ObjectFileELF::SetLoadAddress(Target &target, lldb::addr_t value, 844 bool value_is_offset) { 845 ModuleSP module_sp = GetModule(); 846 if (module_sp) { 847 size_t num_loaded_sections = 0; 848 SectionList *section_list = GetSectionList(); 849 if (section_list) { 850 if (!value_is_offset) { 851 bool found_offset = false; 852 for (size_t i = 1, count = GetProgramHeaderCount(); i <= count; ++i) { 853 const elf::ELFProgramHeader *header = GetProgramHeaderByIndex(i); 854 if (header == nullptr) 855 continue; 856 857 if (header->p_type != PT_LOAD || header->p_offset != 0) 858 continue; 859 860 value = value - header->p_vaddr; 861 found_offset = true; 862 break; 863 } 864 if (!found_offset) 865 return false; 866 } 867 868 const size_t num_sections = section_list->GetSize(); 869 size_t sect_idx = 0; 870 871 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 872 // Iterate through the object file sections to find all 873 // of the sections that have SHF_ALLOC in their flag bits. 874 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 875 if (section_sp && section_sp->Test(SHF_ALLOC)) { 876 lldb::addr_t load_addr = section_sp->GetFileAddress(); 877 // We don't want to update the load address of a section with type 878 // eSectionTypeAbsoluteAddress as they already have the absolute load 879 // address 880 // already specified 881 if (section_sp->GetType() != eSectionTypeAbsoluteAddress) 882 load_addr += value; 883 884 // On 32-bit systems the load address have to fit into 4 bytes. The 885 // rest of 886 // the bytes are the overflow from the addition. 887 if (GetAddressByteSize() == 4) 888 load_addr &= 0xFFFFFFFF; 889 890 if (target.GetSectionLoadList().SetSectionLoadAddress(section_sp, 891 load_addr)) 892 ++num_loaded_sections; 893 } 894 } 895 return num_loaded_sections > 0; 896 } 897 } 898 return false; 899 } 900 901 ByteOrder ObjectFileELF::GetByteOrder() const { 902 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) 903 return eByteOrderBig; 904 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) 905 return eByteOrderLittle; 906 return eByteOrderInvalid; 907 } 908 909 uint32_t ObjectFileELF::GetAddressByteSize() const { 910 return m_data.GetAddressByteSize(); 911 } 912 913 AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) { 914 Symtab *symtab = GetSymtab(); 915 if (!symtab) 916 return eAddressClassUnknown; 917 918 // The address class is determined based on the symtab. Ask it from the object 919 // file what 920 // contains the symtab information. 921 ObjectFile *symtab_objfile = symtab->GetObjectFile(); 922 if (symtab_objfile != nullptr && symtab_objfile != this) 923 return symtab_objfile->GetAddressClass(file_addr); 924 925 auto res = ObjectFile::GetAddressClass(file_addr); 926 if (res != eAddressClassCode) 927 return res; 928 929 auto ub = m_address_class_map.upper_bound(file_addr); 930 if (ub == m_address_class_map.begin()) { 931 // No entry in the address class map before the address. Return 932 // default address class for an address in a code section. 933 return eAddressClassCode; 934 } 935 936 // Move iterator to the address class entry preceding address 937 --ub; 938 939 return ub->second; 940 } 941 942 size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) { 943 return std::distance(m_section_headers.begin(), I) + 1u; 944 } 945 946 size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const { 947 return std::distance(m_section_headers.begin(), I) + 1u; 948 } 949 950 bool ObjectFileELF::ParseHeader() { 951 lldb::offset_t offset = 0; 952 if (!m_header.Parse(m_data, &offset)) 953 return false; 954 955 if (!IsInMemory()) 956 return true; 957 958 // For in memory object files m_data might not contain the full object file. 959 // Try to load it 960 // until the end of the "Section header table" what is at the end of the ELF 961 // file. 962 addr_t file_size = m_header.e_shoff + m_header.e_shnum * m_header.e_shentsize; 963 if (m_data.GetByteSize() < file_size) { 964 ProcessSP process_sp(m_process_wp.lock()); 965 if (!process_sp) 966 return false; 967 968 DataBufferSP data_sp = ReadMemory(process_sp, m_memory_addr, file_size); 969 if (!data_sp) 970 return false; 971 m_data.SetData(data_sp, 0, file_size); 972 } 973 974 return true; 975 } 976 977 bool ObjectFileELF::GetUUID(lldb_private::UUID *uuid) { 978 // Need to parse the section list to get the UUIDs, so make sure that's been 979 // done. 980 if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile) 981 return false; 982 983 if (m_uuid.IsValid()) { 984 // We have the full build id uuid. 985 *uuid = m_uuid; 986 return true; 987 } else if (GetType() == ObjectFile::eTypeCoreFile) { 988 uint32_t core_notes_crc = 0; 989 990 if (!ParseProgramHeaders()) 991 return false; 992 993 core_notes_crc = CalculateELFNotesSegmentsCRC32(m_program_headers, m_data); 994 995 if (core_notes_crc) { 996 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it 997 // look different form .gnu_debuglink crc - followed by 4 bytes of note 998 // segments crc. 999 uint32_t uuidt[4] = {g_core_uuid_magic, core_notes_crc, 0, 0}; 1000 m_uuid.SetBytes(uuidt, sizeof(uuidt)); 1001 } 1002 } else { 1003 if (!m_gnu_debuglink_crc) 1004 m_gnu_debuglink_crc = 1005 calc_gnu_debuglink_crc32(m_data.GetDataStart(), m_data.GetByteSize()); 1006 if (m_gnu_debuglink_crc) { 1007 // Use 4 bytes of crc from the .gnu_debuglink section. 1008 uint32_t uuidt[4] = {m_gnu_debuglink_crc, 0, 0, 0}; 1009 m_uuid.SetBytes(uuidt, sizeof(uuidt)); 1010 } 1011 } 1012 1013 if (m_uuid.IsValid()) { 1014 *uuid = m_uuid; 1015 return true; 1016 } 1017 1018 return false; 1019 } 1020 1021 lldb_private::FileSpecList ObjectFileELF::GetDebugSymbolFilePaths() { 1022 FileSpecList file_spec_list; 1023 1024 if (!m_gnu_debuglink_file.empty()) { 1025 FileSpec file_spec(m_gnu_debuglink_file, false); 1026 file_spec_list.Append(file_spec); 1027 } 1028 return file_spec_list; 1029 } 1030 1031 uint32_t ObjectFileELF::GetDependentModules(FileSpecList &files) { 1032 size_t num_modules = ParseDependentModules(); 1033 uint32_t num_specs = 0; 1034 1035 for (unsigned i = 0; i < num_modules; ++i) { 1036 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) 1037 num_specs++; 1038 } 1039 1040 return num_specs; 1041 } 1042 1043 Address ObjectFileELF::GetImageInfoAddress(Target *target) { 1044 if (!ParseDynamicSymbols()) 1045 return Address(); 1046 1047 SectionList *section_list = GetSectionList(); 1048 if (!section_list) 1049 return Address(); 1050 1051 // Find the SHT_DYNAMIC (.dynamic) section. 1052 SectionSP dynsym_section_sp( 1053 section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)); 1054 if (!dynsym_section_sp) 1055 return Address(); 1056 assert(dynsym_section_sp->GetObjectFile() == this); 1057 1058 user_id_t dynsym_id = dynsym_section_sp->GetID(); 1059 const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id); 1060 if (!dynsym_hdr) 1061 return Address(); 1062 1063 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) { 1064 ELFDynamic &symbol = m_dynamic_symbols[i]; 1065 1066 if (symbol.d_tag == DT_DEBUG) { 1067 // Compute the offset as the number of previous entries plus the 1068 // size of d_tag. 1069 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); 1070 return Address(dynsym_section_sp, offset); 1071 } 1072 // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP 1073 // exists in non-PIE. 1074 else if ((symbol.d_tag == DT_MIPS_RLD_MAP || 1075 symbol.d_tag == DT_MIPS_RLD_MAP_REL) && 1076 target) { 1077 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); 1078 addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target); 1079 if (dyn_base == LLDB_INVALID_ADDRESS) 1080 return Address(); 1081 1082 Error error; 1083 if (symbol.d_tag == DT_MIPS_RLD_MAP) { 1084 // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer. 1085 Address addr; 1086 if (target->ReadPointerFromMemory(dyn_base + offset, false, error, 1087 addr)) 1088 return addr; 1089 } 1090 if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) { 1091 // DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer, 1092 // relative to the address of the tag. 1093 uint64_t rel_offset; 1094 rel_offset = target->ReadUnsignedIntegerFromMemory( 1095 dyn_base + offset, false, GetAddressByteSize(), UINT64_MAX, error); 1096 if (error.Success() && rel_offset != UINT64_MAX) { 1097 Address addr; 1098 addr_t debug_ptr_address = 1099 dyn_base + (offset - GetAddressByteSize()) + rel_offset; 1100 addr.SetOffset(debug_ptr_address); 1101 return addr; 1102 } 1103 } 1104 } 1105 } 1106 1107 return Address(); 1108 } 1109 1110 lldb_private::Address ObjectFileELF::GetEntryPointAddress() { 1111 if (m_entry_point_address.IsValid()) 1112 return m_entry_point_address; 1113 1114 if (!ParseHeader() || !IsExecutable()) 1115 return m_entry_point_address; 1116 1117 SectionList *section_list = GetSectionList(); 1118 addr_t offset = m_header.e_entry; 1119 1120 if (!section_list) 1121 m_entry_point_address.SetOffset(offset); 1122 else 1123 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list); 1124 return m_entry_point_address; 1125 } 1126 1127 //---------------------------------------------------------------------- 1128 // ParseDependentModules 1129 //---------------------------------------------------------------------- 1130 size_t ObjectFileELF::ParseDependentModules() { 1131 if (m_filespec_ap.get()) 1132 return m_filespec_ap->GetSize(); 1133 1134 m_filespec_ap.reset(new FileSpecList()); 1135 1136 if (!ParseSectionHeaders()) 1137 return 0; 1138 1139 SectionList *section_list = GetSectionList(); 1140 if (!section_list) 1141 return 0; 1142 1143 // Find the SHT_DYNAMIC section. 1144 Section *dynsym = 1145 section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) 1146 .get(); 1147 if (!dynsym) 1148 return 0; 1149 assert(dynsym->GetObjectFile() == this); 1150 1151 const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex(dynsym->GetID()); 1152 if (!header) 1153 return 0; 1154 // sh_link: section header index of string table used by entries in the 1155 // section. 1156 Section *dynstr = section_list->FindSectionByID(header->sh_link + 1).get(); 1157 if (!dynstr) 1158 return 0; 1159 1160 DataExtractor dynsym_data; 1161 DataExtractor dynstr_data; 1162 if (ReadSectionData(dynsym, dynsym_data) && 1163 ReadSectionData(dynstr, dynstr_data)) { 1164 ELFDynamic symbol; 1165 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 1166 lldb::offset_t offset = 0; 1167 1168 // The only type of entries we are concerned with are tagged DT_NEEDED, 1169 // yielding the name of a required library. 1170 while (offset < section_size) { 1171 if (!symbol.Parse(dynsym_data, &offset)) 1172 break; 1173 1174 if (symbol.d_tag != DT_NEEDED) 1175 continue; 1176 1177 uint32_t str_index = static_cast<uint32_t>(symbol.d_val); 1178 const char *lib_name = dynstr_data.PeekCStr(str_index); 1179 m_filespec_ap->Append(FileSpec(lib_name, true)); 1180 } 1181 } 1182 1183 return m_filespec_ap->GetSize(); 1184 } 1185 1186 //---------------------------------------------------------------------- 1187 // GetProgramHeaderInfo 1188 //---------------------------------------------------------------------- 1189 size_t ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers, 1190 const SetDataFunction &set_data, 1191 const ELFHeader &header) { 1192 // We have already parsed the program headers 1193 if (!program_headers.empty()) 1194 return program_headers.size(); 1195 1196 // If there are no program headers to read we are done. 1197 if (header.e_phnum == 0) 1198 return 0; 1199 1200 program_headers.resize(header.e_phnum); 1201 if (program_headers.size() != header.e_phnum) 1202 return 0; 1203 1204 const size_t ph_size = header.e_phnum * header.e_phentsize; 1205 const elf_off ph_offset = header.e_phoff; 1206 DataExtractor data; 1207 if (set_data(data, ph_offset, ph_size) != ph_size) 1208 return 0; 1209 1210 uint32_t idx; 1211 lldb::offset_t offset; 1212 for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) { 1213 if (program_headers[idx].Parse(data, &offset) == false) 1214 break; 1215 } 1216 1217 if (idx < program_headers.size()) 1218 program_headers.resize(idx); 1219 1220 return program_headers.size(); 1221 } 1222 1223 //---------------------------------------------------------------------- 1224 // ParseProgramHeaders 1225 //---------------------------------------------------------------------- 1226 size_t ObjectFileELF::ParseProgramHeaders() { 1227 using namespace std::placeholders; 1228 return GetProgramHeaderInfo( 1229 m_program_headers, 1230 std::bind(&ObjectFileELF::SetDataWithReadMemoryFallback, this, _1, _2, 1231 _3), 1232 m_header); 1233 } 1234 1235 lldb_private::Error 1236 ObjectFileELF::RefineModuleDetailsFromNote(lldb_private::DataExtractor &data, 1237 lldb_private::ArchSpec &arch_spec, 1238 lldb_private::UUID &uuid) { 1239 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES)); 1240 Error error; 1241 1242 lldb::offset_t offset = 0; 1243 1244 while (true) { 1245 // Parse the note header. If this fails, bail out. 1246 const lldb::offset_t note_offset = offset; 1247 ELFNote note = ELFNote(); 1248 if (!note.Parse(data, &offset)) { 1249 // We're done. 1250 return error; 1251 } 1252 1253 if (log) 1254 log->Printf("ObjectFileELF::%s parsing note name='%s', type=%" PRIu32, 1255 __FUNCTION__, note.n_name.c_str(), note.n_type); 1256 1257 // Process FreeBSD ELF notes. 1258 if ((note.n_name == LLDB_NT_OWNER_FREEBSD) && 1259 (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) && 1260 (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) { 1261 // Pull out the min version info. 1262 uint32_t version_info; 1263 if (data.GetU32(&offset, &version_info, 1) == nullptr) { 1264 error.SetErrorString("failed to read FreeBSD ABI note payload"); 1265 return error; 1266 } 1267 1268 // Convert the version info into a major/minor number. 1269 const uint32_t version_major = version_info / 100000; 1270 const uint32_t version_minor = (version_info / 1000) % 100; 1271 1272 char os_name[32]; 1273 snprintf(os_name, sizeof(os_name), "freebsd%" PRIu32 ".%" PRIu32, 1274 version_major, version_minor); 1275 1276 // Set the elf OS version to FreeBSD. Also clear the vendor. 1277 arch_spec.GetTriple().setOSName(os_name); 1278 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1279 1280 if (log) 1281 log->Printf("ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32 1282 ".%" PRIu32, 1283 __FUNCTION__, version_major, version_minor, 1284 static_cast<uint32_t>(version_info % 1000)); 1285 } 1286 // Process GNU ELF notes. 1287 else if (note.n_name == LLDB_NT_OWNER_GNU) { 1288 switch (note.n_type) { 1289 case LLDB_NT_GNU_ABI_TAG: 1290 if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) { 1291 // Pull out the min OS version supporting the ABI. 1292 uint32_t version_info[4]; 1293 if (data.GetU32(&offset, &version_info[0], note.n_descsz / 4) == 1294 nullptr) { 1295 error.SetErrorString("failed to read GNU ABI note payload"); 1296 return error; 1297 } 1298 1299 // Set the OS per the OS field. 1300 switch (version_info[0]) { 1301 case LLDB_NT_GNU_ABI_OS_LINUX: 1302 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1303 arch_spec.GetTriple().setVendor( 1304 llvm::Triple::VendorType::UnknownVendor); 1305 if (log) 1306 log->Printf( 1307 "ObjectFileELF::%s detected Linux, min version %" PRIu32 1308 ".%" PRIu32 ".%" PRIu32, 1309 __FUNCTION__, version_info[1], version_info[2], 1310 version_info[3]); 1311 // FIXME we have the minimal version number, we could be propagating 1312 // that. version_info[1] = OS Major, version_info[2] = OS Minor, 1313 // version_info[3] = Revision. 1314 break; 1315 case LLDB_NT_GNU_ABI_OS_HURD: 1316 arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS); 1317 arch_spec.GetTriple().setVendor( 1318 llvm::Triple::VendorType::UnknownVendor); 1319 if (log) 1320 log->Printf("ObjectFileELF::%s detected Hurd (unsupported), min " 1321 "version %" PRIu32 ".%" PRIu32 ".%" PRIu32, 1322 __FUNCTION__, version_info[1], version_info[2], 1323 version_info[3]); 1324 break; 1325 case LLDB_NT_GNU_ABI_OS_SOLARIS: 1326 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Solaris); 1327 arch_spec.GetTriple().setVendor( 1328 llvm::Triple::VendorType::UnknownVendor); 1329 if (log) 1330 log->Printf( 1331 "ObjectFileELF::%s detected Solaris, min version %" PRIu32 1332 ".%" PRIu32 ".%" PRIu32, 1333 __FUNCTION__, version_info[1], version_info[2], 1334 version_info[3]); 1335 break; 1336 default: 1337 if (log) 1338 log->Printf( 1339 "ObjectFileELF::%s unrecognized OS in note, id %" PRIu32 1340 ", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, 1341 __FUNCTION__, version_info[0], version_info[1], 1342 version_info[2], version_info[3]); 1343 break; 1344 } 1345 } 1346 break; 1347 1348 case LLDB_NT_GNU_BUILD_ID_TAG: 1349 // Only bother processing this if we don't already have the uuid set. 1350 if (!uuid.IsValid()) { 1351 // 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a 1352 // build-id of a different 1353 // length. Accept it as long as it's at least 4 bytes as it will be 1354 // better than our own crc32. 1355 if (note.n_descsz >= 4 && note.n_descsz <= 20) { 1356 uint8_t uuidbuf[20]; 1357 if (data.GetU8(&offset, &uuidbuf, note.n_descsz) == nullptr) { 1358 error.SetErrorString("failed to read GNU_BUILD_ID note payload"); 1359 return error; 1360 } 1361 1362 // Save the build id as the UUID for the module. 1363 uuid.SetBytes(uuidbuf, note.n_descsz); 1364 } 1365 } 1366 break; 1367 } 1368 if (arch_spec.IsMIPS() && 1369 arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) 1370 // The note.n_name == LLDB_NT_OWNER_GNU is valid for Linux platform 1371 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1372 } 1373 // Process NetBSD ELF notes. 1374 else if ((note.n_name == LLDB_NT_OWNER_NETBSD) && 1375 (note.n_type == LLDB_NT_NETBSD_ABI_TAG) && 1376 (note.n_descsz == LLDB_NT_NETBSD_ABI_SIZE)) { 1377 // Pull out the min version info. 1378 uint32_t version_info; 1379 if (data.GetU32(&offset, &version_info, 1) == nullptr) { 1380 error.SetErrorString("failed to read NetBSD ABI note payload"); 1381 return error; 1382 } 1383 1384 // Set the elf OS version to NetBSD. Also clear the vendor. 1385 arch_spec.GetTriple().setOS(llvm::Triple::OSType::NetBSD); 1386 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1387 1388 if (log) 1389 log->Printf( 1390 "ObjectFileELF::%s detected NetBSD, min version constant %" PRIu32, 1391 __FUNCTION__, version_info); 1392 } 1393 // Process OpenBSD ELF notes. 1394 else if (note.n_name == LLDB_NT_OWNER_OPENBSD) { 1395 // Set the elf OS version to OpenBSD. Also clear the vendor. 1396 arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD); 1397 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1398 } 1399 // Process CSR kalimba notes 1400 else if ((note.n_type == LLDB_NT_GNU_ABI_TAG) && 1401 (note.n_name == LLDB_NT_OWNER_CSR)) { 1402 arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS); 1403 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::CSR); 1404 1405 // TODO At some point the description string could be processed. 1406 // It could provide a steer towards the kalimba variant which 1407 // this ELF targets. 1408 if (note.n_descsz) { 1409 const char *cstr = 1410 data.GetCStr(&offset, llvm::alignTo(note.n_descsz, 4)); 1411 (void)cstr; 1412 } 1413 } else if (note.n_name == LLDB_NT_OWNER_ANDROID) { 1414 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1415 arch_spec.GetTriple().setEnvironment( 1416 llvm::Triple::EnvironmentType::Android); 1417 } else if (note.n_name == LLDB_NT_OWNER_LINUX) { 1418 // This is sometimes found in core files and usually contains extended 1419 // register info 1420 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1421 } else if (note.n_name == LLDB_NT_OWNER_CORE) { 1422 // Parse the NT_FILE to look for stuff in paths to shared libraries 1423 // As the contents look like this in a 64 bit ELF core file: 1424 // count = 0x000000000000000a (10) 1425 // page_size = 0x0000000000001000 (4096) 1426 // Index start end file_ofs path 1427 // ===== ------------------ ------------------ ------------------ 1428 // ------------------------------------- 1429 // [ 0] 0x0000000000400000 0x0000000000401000 0x0000000000000000 1430 // /tmp/a.out 1431 // [ 1] 0x0000000000600000 0x0000000000601000 0x0000000000000000 1432 // /tmp/a.out 1433 // [ 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 1434 // /tmp/a.out 1435 // [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 1436 // /lib/x86_64-linux-gnu/libc-2.19.so 1437 // [ 4] 0x00007fa79cba8000 0x00007fa79cda7000 0x00000000000001bb 1438 // /lib/x86_64-linux-gnu/libc-2.19.so 1439 // [ 5] 0x00007fa79cda7000 0x00007fa79cdab000 0x00000000000001ba 1440 // /lib/x86_64-linux-gnu/libc-2.19.so 1441 // [ 6] 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be 1442 // /lib/x86_64-linux-gnu/libc-2.19.so 1443 // [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000 0x0000000000000000 1444 // /lib/x86_64-linux-gnu/ld-2.19.so 1445 // [ 8] 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 1446 // /lib/x86_64-linux-gnu/ld-2.19.so 1447 // [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000 0x0000000000000023 1448 // /lib/x86_64-linux-gnu/ld-2.19.so 1449 // In the 32 bit ELFs the count, page_size, start, end, file_ofs are 1450 // uint32_t 1451 // For reference: see readelf source code (in binutils). 1452 if (note.n_type == NT_FILE) { 1453 uint64_t count = data.GetAddress(&offset); 1454 const char *cstr; 1455 data.GetAddress(&offset); // Skip page size 1456 offset += count * 3 * 1457 data.GetAddressByteSize(); // Skip all start/end/file_ofs 1458 for (size_t i = 0; i < count; ++i) { 1459 cstr = data.GetCStr(&offset); 1460 if (cstr == nullptr) { 1461 error.SetErrorStringWithFormat("ObjectFileELF::%s trying to read " 1462 "at an offset after the end " 1463 "(GetCStr returned nullptr)", 1464 __FUNCTION__); 1465 return error; 1466 } 1467 llvm::StringRef path(cstr); 1468 if (path.contains("/lib/x86_64-linux-gnu") || path.contains("/lib/i386-linux-gnu")) { 1469 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1470 break; 1471 } 1472 } 1473 if (arch_spec.IsMIPS() && 1474 arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) 1475 // In case of MIPSR6, the LLDB_NT_OWNER_GNU note is missing 1476 // for some cases (e.g. compile with -nostdlib) 1477 // Hence set OS to Linux 1478 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1479 } 1480 } 1481 1482 // Calculate the offset of the next note just in case "offset" has been used 1483 // to poke at the contents of the note data 1484 offset = note_offset + note.GetByteSize(); 1485 } 1486 1487 return error; 1488 } 1489 1490 void ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length, 1491 ArchSpec &arch_spec) { 1492 lldb::offset_t Offset = 0; 1493 1494 uint8_t FormatVersion = data.GetU8(&Offset); 1495 if (FormatVersion != llvm::ARMBuildAttrs::Format_Version) 1496 return; 1497 1498 Offset = Offset + sizeof(uint32_t); // Section Length 1499 llvm::StringRef VendorName = data.GetCStr(&Offset); 1500 1501 if (VendorName != "aeabi") 1502 return; 1503 1504 if (arch_spec.GetTriple().getEnvironment() == 1505 llvm::Triple::UnknownEnvironment) 1506 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI); 1507 1508 while (Offset < length) { 1509 uint8_t Tag = data.GetU8(&Offset); 1510 uint32_t Size = data.GetU32(&Offset); 1511 1512 if (Tag != llvm::ARMBuildAttrs::File || Size == 0) 1513 continue; 1514 1515 while (Offset < length) { 1516 uint64_t Tag = data.GetULEB128(&Offset); 1517 switch (Tag) { 1518 default: 1519 if (Tag < 32) 1520 data.GetULEB128(&Offset); 1521 else if (Tag % 2 == 0) 1522 data.GetULEB128(&Offset); 1523 else 1524 data.GetCStr(&Offset); 1525 1526 break; 1527 1528 case llvm::ARMBuildAttrs::CPU_raw_name: 1529 case llvm::ARMBuildAttrs::CPU_name: 1530 data.GetCStr(&Offset); 1531 1532 break; 1533 1534 case llvm::ARMBuildAttrs::ABI_VFP_args: { 1535 uint64_t VFPArgs = data.GetULEB128(&Offset); 1536 1537 if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS) { 1538 if (arch_spec.GetTriple().getEnvironment() == 1539 llvm::Triple::UnknownEnvironment || 1540 arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF) 1541 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI); 1542 1543 arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float); 1544 } else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS) { 1545 if (arch_spec.GetTriple().getEnvironment() == 1546 llvm::Triple::UnknownEnvironment || 1547 arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI) 1548 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF); 1549 1550 arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float); 1551 } 1552 1553 break; 1554 } 1555 } 1556 } 1557 } 1558 } 1559 1560 //---------------------------------------------------------------------- 1561 // GetSectionHeaderInfo 1562 //---------------------------------------------------------------------- 1563 size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers, 1564 const SetDataFunction &set_data, 1565 const elf::ELFHeader &header, 1566 lldb_private::UUID &uuid, 1567 std::string &gnu_debuglink_file, 1568 uint32_t &gnu_debuglink_crc, 1569 ArchSpec &arch_spec) { 1570 // Don't reparse the section headers if we already did that. 1571 if (!section_headers.empty()) 1572 return section_headers.size(); 1573 1574 // Only initialize the arch_spec to okay defaults if they're not already set. 1575 // We'll refine this with note data as we parse the notes. 1576 if (arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) { 1577 llvm::Triple::OSType ostype; 1578 llvm::Triple::OSType spec_ostype; 1579 const uint32_t sub_type = subTypeFromElfHeader(header); 1580 arch_spec.SetArchitecture(eArchTypeELF, header.e_machine, sub_type, 1581 header.e_ident[EI_OSABI]); 1582 1583 // Validate if it is ok to remove GetOsFromOSABI. 1584 // Note, that now the OS is determined based on EI_OSABI flag and 1585 // the info extracted from ELF notes (see RefineModuleDetailsFromNote). 1586 // However in some cases that still might be not enough: for example 1587 // a shared library might not have any notes at all 1588 // and have EI_OSABI flag set to System V, 1589 // as result the OS will be set to UnknownOS. 1590 GetOsFromOSABI(header.e_ident[EI_OSABI], ostype); 1591 spec_ostype = arch_spec.GetTriple().getOS(); 1592 assert(spec_ostype == ostype); 1593 UNUSED_IF_ASSERT_DISABLED(spec_ostype); 1594 } 1595 1596 if (arch_spec.GetMachine() == llvm::Triple::mips || 1597 arch_spec.GetMachine() == llvm::Triple::mipsel || 1598 arch_spec.GetMachine() == llvm::Triple::mips64 || 1599 arch_spec.GetMachine() == llvm::Triple::mips64el) { 1600 switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) { 1601 case llvm::ELF::EF_MIPS_MICROMIPS: 1602 arch_spec.SetFlags(ArchSpec::eMIPSAse_micromips); 1603 break; 1604 case llvm::ELF::EF_MIPS_ARCH_ASE_M16: 1605 arch_spec.SetFlags(ArchSpec::eMIPSAse_mips16); 1606 break; 1607 case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX: 1608 arch_spec.SetFlags(ArchSpec::eMIPSAse_mdmx); 1609 break; 1610 default: 1611 break; 1612 } 1613 } 1614 1615 if (arch_spec.GetMachine() == llvm::Triple::arm || 1616 arch_spec.GetMachine() == llvm::Triple::thumb) { 1617 if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT) 1618 arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float); 1619 else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT) 1620 arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float); 1621 } 1622 1623 // If there are no section headers we are done. 1624 if (header.e_shnum == 0) 1625 return 0; 1626 1627 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES)); 1628 1629 section_headers.resize(header.e_shnum); 1630 if (section_headers.size() != header.e_shnum) 1631 return 0; 1632 1633 const size_t sh_size = header.e_shnum * header.e_shentsize; 1634 const elf_off sh_offset = header.e_shoff; 1635 DataExtractor sh_data; 1636 if (set_data(sh_data, sh_offset, sh_size) != sh_size) 1637 return 0; 1638 1639 uint32_t idx; 1640 lldb::offset_t offset; 1641 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) { 1642 if (section_headers[idx].Parse(sh_data, &offset) == false) 1643 break; 1644 } 1645 if (idx < section_headers.size()) 1646 section_headers.resize(idx); 1647 1648 const unsigned strtab_idx = header.e_shstrndx; 1649 if (strtab_idx && strtab_idx < section_headers.size()) { 1650 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx]; 1651 const size_t byte_size = sheader.sh_size; 1652 const Elf64_Off offset = sheader.sh_offset; 1653 lldb_private::DataExtractor shstr_data; 1654 1655 if (set_data(shstr_data, offset, byte_size) == byte_size) { 1656 for (SectionHeaderCollIter I = section_headers.begin(); 1657 I != section_headers.end(); ++I) { 1658 static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink"); 1659 const ELFSectionHeaderInfo &sheader = *I; 1660 const uint64_t section_size = 1661 sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size; 1662 ConstString name(shstr_data.PeekCStr(I->sh_name)); 1663 1664 I->section_name = name; 1665 1666 if (arch_spec.IsMIPS()) { 1667 uint32_t arch_flags = arch_spec.GetFlags(); 1668 DataExtractor data; 1669 if (sheader.sh_type == SHT_MIPS_ABIFLAGS) { 1670 1671 if (section_size && (set_data(data, sheader.sh_offset, 1672 section_size) == section_size)) { 1673 // MIPS ASE Mask is at offset 12 in MIPS.abiflags section 1674 lldb::offset_t offset = 12; // MIPS ABI Flags Version: 0 1675 arch_flags |= data.GetU32(&offset); 1676 1677 // The floating point ABI is at offset 7 1678 offset = 7; 1679 switch (data.GetU8(&offset)) { 1680 case llvm::Mips::Val_GNU_MIPS_ABI_FP_ANY: 1681 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_ANY; 1682 break; 1683 case llvm::Mips::Val_GNU_MIPS_ABI_FP_DOUBLE: 1684 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_DOUBLE; 1685 break; 1686 case llvm::Mips::Val_GNU_MIPS_ABI_FP_SINGLE: 1687 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SINGLE; 1688 break; 1689 case llvm::Mips::Val_GNU_MIPS_ABI_FP_SOFT: 1690 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT; 1691 break; 1692 case llvm::Mips::Val_GNU_MIPS_ABI_FP_OLD_64: 1693 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_OLD_64; 1694 break; 1695 case llvm::Mips::Val_GNU_MIPS_ABI_FP_XX: 1696 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_XX; 1697 break; 1698 case llvm::Mips::Val_GNU_MIPS_ABI_FP_64: 1699 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64; 1700 break; 1701 case llvm::Mips::Val_GNU_MIPS_ABI_FP_64A: 1702 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64A; 1703 break; 1704 } 1705 } 1706 } 1707 // Settings appropriate ArchSpec ABI Flags 1708 switch (header.e_flags & llvm::ELF::EF_MIPS_ABI) { 1709 case llvm::ELF::EF_MIPS_ABI_O32: 1710 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32; 1711 break; 1712 case EF_MIPS_ABI_O64: 1713 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O64; 1714 break; 1715 case EF_MIPS_ABI_EABI32: 1716 arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI32; 1717 break; 1718 case EF_MIPS_ABI_EABI64: 1719 arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI64; 1720 break; 1721 default: 1722 // ABI Mask doesn't cover N32 and N64 ABI. 1723 if (header.e_ident[EI_CLASS] == llvm::ELF::ELFCLASS64) 1724 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N64; 1725 else if (header.e_flags && llvm::ELF::EF_MIPS_ABI2) 1726 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32; 1727 break; 1728 } 1729 arch_spec.SetFlags(arch_flags); 1730 } 1731 1732 if (arch_spec.GetMachine() == llvm::Triple::arm || 1733 arch_spec.GetMachine() == llvm::Triple::thumb) { 1734 DataExtractor data; 1735 1736 if (sheader.sh_type == SHT_ARM_ATTRIBUTES && section_size != 0 && 1737 set_data(data, sheader.sh_offset, section_size) == section_size) 1738 ParseARMAttributes(data, section_size, arch_spec); 1739 } 1740 1741 if (name == g_sect_name_gnu_debuglink) { 1742 DataExtractor data; 1743 if (section_size && (set_data(data, sheader.sh_offset, 1744 section_size) == section_size)) { 1745 lldb::offset_t gnu_debuglink_offset = 0; 1746 gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset); 1747 gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4); 1748 data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1); 1749 } 1750 } 1751 1752 // Process ELF note section entries. 1753 bool is_note_header = (sheader.sh_type == SHT_NOTE); 1754 1755 // The section header ".note.android.ident" is stored as a 1756 // PROGBITS type header but it is actually a note header. 1757 static ConstString g_sect_name_android_ident(".note.android.ident"); 1758 if (!is_note_header && name == g_sect_name_android_ident) 1759 is_note_header = true; 1760 1761 if (is_note_header) { 1762 // Allow notes to refine module info. 1763 DataExtractor data; 1764 if (section_size && (set_data(data, sheader.sh_offset, 1765 section_size) == section_size)) { 1766 Error error = RefineModuleDetailsFromNote(data, arch_spec, uuid); 1767 if (error.Fail()) { 1768 if (log) 1769 log->Printf("ObjectFileELF::%s ELF note processing failed: %s", 1770 __FUNCTION__, error.AsCString()); 1771 } 1772 } 1773 } 1774 } 1775 1776 // Make any unknown triple components to be unspecified unknowns. 1777 if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor) 1778 arch_spec.GetTriple().setVendorName(llvm::StringRef()); 1779 if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS) 1780 arch_spec.GetTriple().setOSName(llvm::StringRef()); 1781 1782 return section_headers.size(); 1783 } 1784 } 1785 1786 section_headers.clear(); 1787 return 0; 1788 } 1789 1790 size_t ObjectFileELF::GetProgramHeaderCount() { return ParseProgramHeaders(); } 1791 1792 const elf::ELFProgramHeader * 1793 ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id) { 1794 if (!id || !ParseProgramHeaders()) 1795 return NULL; 1796 1797 if (--id < m_program_headers.size()) 1798 return &m_program_headers[id]; 1799 1800 return NULL; 1801 } 1802 1803 DataExtractor ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id) { 1804 const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id); 1805 if (segment_header == NULL) 1806 return DataExtractor(); 1807 return DataExtractor(m_data, segment_header->p_offset, 1808 segment_header->p_filesz); 1809 } 1810 1811 std::string 1812 ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const { 1813 size_t pos = symbol_name.find('@'); 1814 return symbol_name.substr(0, pos).str(); 1815 } 1816 1817 //---------------------------------------------------------------------- 1818 // ParseSectionHeaders 1819 //---------------------------------------------------------------------- 1820 size_t ObjectFileELF::ParseSectionHeaders() { 1821 using namespace std::placeholders; 1822 1823 return GetSectionHeaderInfo( 1824 m_section_headers, 1825 std::bind(&ObjectFileELF::SetDataWithReadMemoryFallback, this, _1, _2, 1826 _3), 1827 m_header, m_uuid, m_gnu_debuglink_file, m_gnu_debuglink_crc, m_arch_spec); 1828 } 1829 1830 lldb::offset_t ObjectFileELF::SetData(const lldb_private::DataExtractor &src, 1831 lldb_private::DataExtractor &dst, 1832 lldb::offset_t offset, 1833 lldb::offset_t length) { 1834 return dst.SetData(src, offset, length); 1835 } 1836 1837 lldb::offset_t 1838 ObjectFileELF::SetDataWithReadMemoryFallback(lldb_private::DataExtractor &dst, 1839 lldb::offset_t offset, 1840 lldb::offset_t length) { 1841 if (offset + length <= m_data.GetByteSize()) 1842 return dst.SetData(m_data, offset, length); 1843 1844 const auto process_sp = m_process_wp.lock(); 1845 if (process_sp != nullptr) { 1846 addr_t file_size = offset + length; 1847 1848 DataBufferSP data_sp = ReadMemory(process_sp, m_memory_addr, file_size); 1849 if (!data_sp) 1850 return false; 1851 m_data.SetData(data_sp, 0, file_size); 1852 } 1853 1854 return dst.SetData(m_data, offset, length); 1855 } 1856 1857 const ObjectFileELF::ELFSectionHeaderInfo * 1858 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) { 1859 if (!id || !ParseSectionHeaders()) 1860 return NULL; 1861 1862 if (--id < m_section_headers.size()) 1863 return &m_section_headers[id]; 1864 1865 return NULL; 1866 } 1867 1868 lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) { 1869 if (!name || !name[0] || !ParseSectionHeaders()) 1870 return 0; 1871 for (size_t i = 1; i < m_section_headers.size(); ++i) 1872 if (m_section_headers[i].section_name == ConstString(name)) 1873 return i; 1874 return 0; 1875 } 1876 1877 void ObjectFileELF::CreateSections(SectionList &unified_section_list) { 1878 if (!m_sections_ap.get() && ParseSectionHeaders()) { 1879 m_sections_ap.reset(new SectionList()); 1880 1881 for (SectionHeaderCollIter I = m_section_headers.begin(); 1882 I != m_section_headers.end(); ++I) { 1883 const ELFSectionHeaderInfo &header = *I; 1884 1885 ConstString &name = I->section_name; 1886 const uint64_t file_size = 1887 header.sh_type == SHT_NOBITS ? 0 : header.sh_size; 1888 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0; 1889 1890 static ConstString g_sect_name_text(".text"); 1891 static ConstString g_sect_name_data(".data"); 1892 static ConstString g_sect_name_bss(".bss"); 1893 static ConstString g_sect_name_tdata(".tdata"); 1894 static ConstString g_sect_name_tbss(".tbss"); 1895 static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev"); 1896 static ConstString g_sect_name_dwarf_debug_addr(".debug_addr"); 1897 static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges"); 1898 static ConstString g_sect_name_dwarf_debug_frame(".debug_frame"); 1899 static ConstString g_sect_name_dwarf_debug_info(".debug_info"); 1900 static ConstString g_sect_name_dwarf_debug_line(".debug_line"); 1901 static ConstString g_sect_name_dwarf_debug_loc(".debug_loc"); 1902 static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo"); 1903 static ConstString g_sect_name_dwarf_debug_macro(".debug_macro"); 1904 static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames"); 1905 static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes"); 1906 static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges"); 1907 static ConstString g_sect_name_dwarf_debug_str(".debug_str"); 1908 static ConstString g_sect_name_dwarf_debug_str_offsets( 1909 ".debug_str_offsets"); 1910 static ConstString g_sect_name_dwarf_debug_abbrev_dwo( 1911 ".debug_abbrev.dwo"); 1912 static ConstString g_sect_name_dwarf_debug_info_dwo(".debug_info.dwo"); 1913 static ConstString g_sect_name_dwarf_debug_line_dwo(".debug_line.dwo"); 1914 static ConstString g_sect_name_dwarf_debug_macro_dwo(".debug_macro.dwo"); 1915 static ConstString g_sect_name_dwarf_debug_loc_dwo(".debug_loc.dwo"); 1916 static ConstString g_sect_name_dwarf_debug_str_dwo(".debug_str.dwo"); 1917 static ConstString g_sect_name_dwarf_debug_str_offsets_dwo( 1918 ".debug_str_offsets.dwo"); 1919 static ConstString g_sect_name_eh_frame(".eh_frame"); 1920 static ConstString g_sect_name_arm_exidx(".ARM.exidx"); 1921 static ConstString g_sect_name_arm_extab(".ARM.extab"); 1922 static ConstString g_sect_name_go_symtab(".gosymtab"); 1923 1924 SectionType sect_type = eSectionTypeOther; 1925 1926 bool is_thread_specific = false; 1927 1928 if (name == g_sect_name_text) 1929 sect_type = eSectionTypeCode; 1930 else if (name == g_sect_name_data) 1931 sect_type = eSectionTypeData; 1932 else if (name == g_sect_name_bss) 1933 sect_type = eSectionTypeZeroFill; 1934 else if (name == g_sect_name_tdata) { 1935 sect_type = eSectionTypeData; 1936 is_thread_specific = true; 1937 } else if (name == g_sect_name_tbss) { 1938 sect_type = eSectionTypeZeroFill; 1939 is_thread_specific = true; 1940 } 1941 // .debug_abbrev – Abbreviations used in the .debug_info section 1942 // .debug_aranges – Lookup table for mapping addresses to compilation 1943 // units 1944 // .debug_frame – Call frame information 1945 // .debug_info – The core DWARF information section 1946 // .debug_line – Line number information 1947 // .debug_loc – Location lists used in DW_AT_location attributes 1948 // .debug_macinfo – Macro information 1949 // .debug_pubnames – Lookup table for mapping object and function names to 1950 // compilation units 1951 // .debug_pubtypes – Lookup table for mapping type names to compilation 1952 // units 1953 // .debug_ranges – Address ranges used in DW_AT_ranges attributes 1954 // .debug_str – String table used in .debug_info 1955 // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, 1956 // http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html 1957 // MISSING? .debug-index - 1958 // http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644 1959 // MISSING? .debug_types - Type descriptions from DWARF 4? See 1960 // http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo 1961 else if (name == g_sect_name_dwarf_debug_abbrev) 1962 sect_type = eSectionTypeDWARFDebugAbbrev; 1963 else if (name == g_sect_name_dwarf_debug_addr) 1964 sect_type = eSectionTypeDWARFDebugAddr; 1965 else if (name == g_sect_name_dwarf_debug_aranges) 1966 sect_type = eSectionTypeDWARFDebugAranges; 1967 else if (name == g_sect_name_dwarf_debug_frame) 1968 sect_type = eSectionTypeDWARFDebugFrame; 1969 else if (name == g_sect_name_dwarf_debug_info) 1970 sect_type = eSectionTypeDWARFDebugInfo; 1971 else if (name == g_sect_name_dwarf_debug_line) 1972 sect_type = eSectionTypeDWARFDebugLine; 1973 else if (name == g_sect_name_dwarf_debug_loc) 1974 sect_type = eSectionTypeDWARFDebugLoc; 1975 else if (name == g_sect_name_dwarf_debug_macinfo) 1976 sect_type = eSectionTypeDWARFDebugMacInfo; 1977 else if (name == g_sect_name_dwarf_debug_macro) 1978 sect_type = eSectionTypeDWARFDebugMacro; 1979 else if (name == g_sect_name_dwarf_debug_pubnames) 1980 sect_type = eSectionTypeDWARFDebugPubNames; 1981 else if (name == g_sect_name_dwarf_debug_pubtypes) 1982 sect_type = eSectionTypeDWARFDebugPubTypes; 1983 else if (name == g_sect_name_dwarf_debug_ranges) 1984 sect_type = eSectionTypeDWARFDebugRanges; 1985 else if (name == g_sect_name_dwarf_debug_str) 1986 sect_type = eSectionTypeDWARFDebugStr; 1987 else if (name == g_sect_name_dwarf_debug_str_offsets) 1988 sect_type = eSectionTypeDWARFDebugStrOffsets; 1989 else if (name == g_sect_name_dwarf_debug_abbrev_dwo) 1990 sect_type = eSectionTypeDWARFDebugAbbrev; 1991 else if (name == g_sect_name_dwarf_debug_info_dwo) 1992 sect_type = eSectionTypeDWARFDebugInfo; 1993 else if (name == g_sect_name_dwarf_debug_line_dwo) 1994 sect_type = eSectionTypeDWARFDebugLine; 1995 else if (name == g_sect_name_dwarf_debug_macro_dwo) 1996 sect_type = eSectionTypeDWARFDebugMacro; 1997 else if (name == g_sect_name_dwarf_debug_loc_dwo) 1998 sect_type = eSectionTypeDWARFDebugLoc; 1999 else if (name == g_sect_name_dwarf_debug_str_dwo) 2000 sect_type = eSectionTypeDWARFDebugStr; 2001 else if (name == g_sect_name_dwarf_debug_str_offsets_dwo) 2002 sect_type = eSectionTypeDWARFDebugStrOffsets; 2003 else if (name == g_sect_name_eh_frame) 2004 sect_type = eSectionTypeEHFrame; 2005 else if (name == g_sect_name_arm_exidx) 2006 sect_type = eSectionTypeARMexidx; 2007 else if (name == g_sect_name_arm_extab) 2008 sect_type = eSectionTypeARMextab; 2009 else if (name == g_sect_name_go_symtab) 2010 sect_type = eSectionTypeGoSymtab; 2011 2012 const uint32_t permissions = 2013 ((header.sh_flags & SHF_ALLOC) ? ePermissionsReadable : 0u) | 2014 ((header.sh_flags & SHF_WRITE) ? ePermissionsWritable : 0u) | 2015 ((header.sh_flags & SHF_EXECINSTR) ? ePermissionsExecutable : 0u); 2016 switch (header.sh_type) { 2017 case SHT_SYMTAB: 2018 assert(sect_type == eSectionTypeOther); 2019 sect_type = eSectionTypeELFSymbolTable; 2020 break; 2021 case SHT_DYNSYM: 2022 assert(sect_type == eSectionTypeOther); 2023 sect_type = eSectionTypeELFDynamicSymbols; 2024 break; 2025 case SHT_RELA: 2026 case SHT_REL: 2027 assert(sect_type == eSectionTypeOther); 2028 sect_type = eSectionTypeELFRelocationEntries; 2029 break; 2030 case SHT_DYNAMIC: 2031 assert(sect_type == eSectionTypeOther); 2032 sect_type = eSectionTypeELFDynamicLinkInfo; 2033 break; 2034 } 2035 2036 if (eSectionTypeOther == sect_type) { 2037 // the kalimba toolchain assumes that ELF section names are free-form. 2038 // It does 2039 // support linkscripts which (can) give rise to various arbitrarily 2040 // named 2041 // sections being "Code" or "Data". 2042 sect_type = kalimbaSectionType(m_header, header); 2043 } 2044 2045 const uint32_t target_bytes_size = 2046 (eSectionTypeData == sect_type || eSectionTypeZeroFill == sect_type) 2047 ? m_arch_spec.GetDataByteSize() 2048 : eSectionTypeCode == sect_type ? m_arch_spec.GetCodeByteSize() 2049 : 1; 2050 2051 elf::elf_xword log2align = 2052 (header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign); 2053 SectionSP section_sp(new Section( 2054 GetModule(), // Module to which this section belongs. 2055 this, // ObjectFile to which this section belongs and should read 2056 // section data from. 2057 SectionIndex(I), // Section ID. 2058 name, // Section name. 2059 sect_type, // Section type. 2060 header.sh_addr, // VM address. 2061 vm_size, // VM size in bytes of this section. 2062 header.sh_offset, // Offset of this section in the file. 2063 file_size, // Size of the section as found in the file. 2064 log2align, // Alignment of the section 2065 header.sh_flags, // Flags for this section. 2066 target_bytes_size)); // Number of host bytes per target byte 2067 2068 section_sp->SetPermissions(permissions); 2069 if (is_thread_specific) 2070 section_sp->SetIsThreadSpecific(is_thread_specific); 2071 m_sections_ap->AddSection(section_sp); 2072 } 2073 } 2074 2075 if (m_sections_ap.get()) { 2076 if (GetType() == eTypeDebugInfo) { 2077 static const SectionType g_sections[] = { 2078 eSectionTypeDWARFDebugAbbrev, eSectionTypeDWARFDebugAddr, 2079 eSectionTypeDWARFDebugAranges, eSectionTypeDWARFDebugFrame, 2080 eSectionTypeDWARFDebugInfo, eSectionTypeDWARFDebugLine, 2081 eSectionTypeDWARFDebugLoc, eSectionTypeDWARFDebugMacInfo, 2082 eSectionTypeDWARFDebugPubNames, eSectionTypeDWARFDebugPubTypes, 2083 eSectionTypeDWARFDebugRanges, eSectionTypeDWARFDebugStr, 2084 eSectionTypeDWARFDebugStrOffsets, eSectionTypeELFSymbolTable, 2085 }; 2086 SectionList *elf_section_list = m_sections_ap.get(); 2087 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); 2088 ++idx) { 2089 SectionType section_type = g_sections[idx]; 2090 SectionSP section_sp( 2091 elf_section_list->FindSectionByType(section_type, true)); 2092 if (section_sp) { 2093 SectionSP module_section_sp( 2094 unified_section_list.FindSectionByType(section_type, true)); 2095 if (module_section_sp) 2096 unified_section_list.ReplaceSection(module_section_sp->GetID(), 2097 section_sp); 2098 else 2099 unified_section_list.AddSection(section_sp); 2100 } 2101 } 2102 } else { 2103 unified_section_list = *m_sections_ap; 2104 } 2105 } 2106 } 2107 2108 // Find the arm/aarch64 mapping symbol character in the given symbol name. 2109 // Mapping symbols have the 2110 // form of "$<char>[.<any>]*". Additionally we recognize cases when the mapping 2111 // symbol prefixed by 2112 // an arbitrary string because if a symbol prefix added to each symbol in the 2113 // object file with 2114 // objcopy then the mapping symbols are also prefixed. 2115 static char FindArmAarch64MappingSymbol(const char *symbol_name) { 2116 if (!symbol_name) 2117 return '\0'; 2118 2119 const char *dollar_pos = ::strchr(symbol_name, '$'); 2120 if (!dollar_pos || dollar_pos[1] == '\0') 2121 return '\0'; 2122 2123 if (dollar_pos[2] == '\0' || dollar_pos[2] == '.') 2124 return dollar_pos[1]; 2125 return '\0'; 2126 } 2127 2128 #define STO_MIPS_ISA (3 << 6) 2129 #define STO_MICROMIPS (2 << 6) 2130 #define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS) 2131 2132 // private 2133 unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id, 2134 SectionList *section_list, 2135 const size_t num_symbols, 2136 const DataExtractor &symtab_data, 2137 const DataExtractor &strtab_data) { 2138 ELFSymbol symbol; 2139 lldb::offset_t offset = 0; 2140 2141 static ConstString text_section_name(".text"); 2142 static ConstString init_section_name(".init"); 2143 static ConstString fini_section_name(".fini"); 2144 static ConstString ctors_section_name(".ctors"); 2145 static ConstString dtors_section_name(".dtors"); 2146 2147 static ConstString data_section_name(".data"); 2148 static ConstString rodata_section_name(".rodata"); 2149 static ConstString rodata1_section_name(".rodata1"); 2150 static ConstString data2_section_name(".data1"); 2151 static ConstString bss_section_name(".bss"); 2152 static ConstString opd_section_name(".opd"); // For ppc64 2153 2154 // On Android the oatdata and the oatexec symbols in the oat and odex files 2155 // covers the full 2156 // .text section what causes issues with displaying unusable symbol name to 2157 // the user and very 2158 // slow unwinding speed because the instruction emulation based unwind plans 2159 // try to emulate all 2160 // instructions in these symbols. Don't add these symbols to the symbol list 2161 // as they have no 2162 // use for the debugger and they are causing a lot of trouble. 2163 // Filtering can't be restricted to Android because this special object file 2164 // don't contain the 2165 // note section specifying the environment to Android but the custom extension 2166 // and file name 2167 // makes it highly unlikely that this will collide with anything else. 2168 ConstString file_extension = m_file.GetFileNameExtension(); 2169 bool skip_oatdata_oatexec = file_extension == ConstString("oat") || 2170 file_extension == ConstString("odex"); 2171 2172 ArchSpec arch; 2173 GetArchitecture(arch); 2174 ModuleSP module_sp(GetModule()); 2175 SectionList *module_section_list = 2176 module_sp ? module_sp->GetSectionList() : nullptr; 2177 2178 // Local cache to avoid doing a FindSectionByName for each symbol. The "const 2179 // char*" key must 2180 // came from a ConstString object so they can be compared by pointer 2181 std::unordered_map<const char *, lldb::SectionSP> section_name_to_section; 2182 2183 unsigned i; 2184 for (i = 0; i < num_symbols; ++i) { 2185 if (symbol.Parse(symtab_data, &offset) == false) 2186 break; 2187 2188 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 2189 if (!symbol_name) 2190 symbol_name = ""; 2191 2192 // No need to add non-section symbols that have no names 2193 if (symbol.getType() != STT_SECTION && 2194 (symbol_name == nullptr || symbol_name[0] == '\0')) 2195 continue; 2196 2197 // Skipping oatdata and oatexec sections if it is requested. See details 2198 // above the 2199 // definition of skip_oatdata_oatexec for the reasons. 2200 if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 || 2201 ::strcmp(symbol_name, "oatexec") == 0)) 2202 continue; 2203 2204 SectionSP symbol_section_sp; 2205 SymbolType symbol_type = eSymbolTypeInvalid; 2206 Elf64_Half section_idx = symbol.st_shndx; 2207 2208 switch (section_idx) { 2209 case SHN_ABS: 2210 symbol_type = eSymbolTypeAbsolute; 2211 break; 2212 case SHN_UNDEF: 2213 symbol_type = eSymbolTypeUndefined; 2214 break; 2215 default: 2216 symbol_section_sp = section_list->GetSectionAtIndex(section_idx); 2217 break; 2218 } 2219 2220 // If a symbol is undefined do not process it further even if it has a STT 2221 // type 2222 if (symbol_type != eSymbolTypeUndefined) { 2223 switch (symbol.getType()) { 2224 default: 2225 case STT_NOTYPE: 2226 // The symbol's type is not specified. 2227 break; 2228 2229 case STT_OBJECT: 2230 // The symbol is associated with a data object, such as a variable, 2231 // an array, etc. 2232 symbol_type = eSymbolTypeData; 2233 break; 2234 2235 case STT_FUNC: 2236 // The symbol is associated with a function or other executable code. 2237 symbol_type = eSymbolTypeCode; 2238 break; 2239 2240 case STT_SECTION: 2241 // The symbol is associated with a section. Symbol table entries of 2242 // this type exist primarily for relocation and normally have 2243 // STB_LOCAL binding. 2244 break; 2245 2246 case STT_FILE: 2247 // Conventionally, the symbol's name gives the name of the source 2248 // file associated with the object file. A file symbol has STB_LOCAL 2249 // binding, its section index is SHN_ABS, and it precedes the other 2250 // STB_LOCAL symbols for the file, if it is present. 2251 symbol_type = eSymbolTypeSourceFile; 2252 break; 2253 2254 case STT_GNU_IFUNC: 2255 // The symbol is associated with an indirect function. The actual 2256 // function will be resolved if it is referenced. 2257 symbol_type = eSymbolTypeResolver; 2258 break; 2259 } 2260 } 2261 2262 if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) { 2263 if (symbol_section_sp) { 2264 const ConstString §_name = symbol_section_sp->GetName(); 2265 if (sect_name == text_section_name || sect_name == init_section_name || 2266 sect_name == fini_section_name || sect_name == ctors_section_name || 2267 sect_name == dtors_section_name) { 2268 symbol_type = eSymbolTypeCode; 2269 } else if (sect_name == data_section_name || 2270 sect_name == data2_section_name || 2271 sect_name == rodata_section_name || 2272 sect_name == rodata1_section_name || 2273 sect_name == bss_section_name) { 2274 symbol_type = eSymbolTypeData; 2275 } 2276 } 2277 } 2278 2279 int64_t symbol_value_offset = 0; 2280 uint32_t additional_flags = 0; 2281 2282 if (arch.IsValid()) { 2283 if (arch.GetMachine() == llvm::Triple::arm) { 2284 if (symbol.getBinding() == STB_LOCAL) { 2285 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); 2286 if (symbol_type == eSymbolTypeCode) { 2287 switch (mapping_symbol) { 2288 case 'a': 2289 // $a[.<any>]* - marks an ARM instruction sequence 2290 m_address_class_map[symbol.st_value] = eAddressClassCode; 2291 break; 2292 case 'b': 2293 case 't': 2294 // $b[.<any>]* - marks a THUMB BL instruction sequence 2295 // $t[.<any>]* - marks a THUMB instruction sequence 2296 m_address_class_map[symbol.st_value] = 2297 eAddressClassCodeAlternateISA; 2298 break; 2299 case 'd': 2300 // $d[.<any>]* - marks a data item sequence (e.g. lit pool) 2301 m_address_class_map[symbol.st_value] = eAddressClassData; 2302 break; 2303 } 2304 } 2305 if (mapping_symbol) 2306 continue; 2307 } 2308 } else if (arch.GetMachine() == llvm::Triple::aarch64) { 2309 if (symbol.getBinding() == STB_LOCAL) { 2310 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); 2311 if (symbol_type == eSymbolTypeCode) { 2312 switch (mapping_symbol) { 2313 case 'x': 2314 // $x[.<any>]* - marks an A64 instruction sequence 2315 m_address_class_map[symbol.st_value] = eAddressClassCode; 2316 break; 2317 case 'd': 2318 // $d[.<any>]* - marks a data item sequence (e.g. lit pool) 2319 m_address_class_map[symbol.st_value] = eAddressClassData; 2320 break; 2321 } 2322 } 2323 if (mapping_symbol) 2324 continue; 2325 } 2326 } 2327 2328 if (arch.GetMachine() == llvm::Triple::arm) { 2329 if (symbol_type == eSymbolTypeCode) { 2330 if (symbol.st_value & 1) { 2331 // Subtracting 1 from the address effectively unsets 2332 // the low order bit, which results in the address 2333 // actually pointing to the beginning of the symbol. 2334 // This delta will be used below in conjunction with 2335 // symbol.st_value to produce the final symbol_value 2336 // that we store in the symtab. 2337 symbol_value_offset = -1; 2338 m_address_class_map[symbol.st_value ^ 1] = 2339 eAddressClassCodeAlternateISA; 2340 } else { 2341 // This address is ARM 2342 m_address_class_map[symbol.st_value] = eAddressClassCode; 2343 } 2344 } 2345 } 2346 2347 /* 2348 * MIPS: 2349 * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for 2350 * MIPS). 2351 * This allows processor to switch between microMIPS and MIPS without any 2352 * need 2353 * for special mode-control register. However, apart from .debug_line, 2354 * none of 2355 * the ELF/DWARF sections set the ISA bit (for symbol or section). Use 2356 * st_other 2357 * flag to check whether the symbol is microMIPS and then set the address 2358 * class 2359 * accordingly. 2360 */ 2361 const llvm::Triple::ArchType llvm_arch = arch.GetMachine(); 2362 if (llvm_arch == llvm::Triple::mips || 2363 llvm_arch == llvm::Triple::mipsel || 2364 llvm_arch == llvm::Triple::mips64 || 2365 llvm_arch == llvm::Triple::mips64el) { 2366 if (IS_MICROMIPS(symbol.st_other)) 2367 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA; 2368 else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) { 2369 symbol.st_value = symbol.st_value & (~1ull); 2370 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA; 2371 } else { 2372 if (symbol_type == eSymbolTypeCode) 2373 m_address_class_map[symbol.st_value] = eAddressClassCode; 2374 else if (symbol_type == eSymbolTypeData) 2375 m_address_class_map[symbol.st_value] = eAddressClassData; 2376 else 2377 m_address_class_map[symbol.st_value] = eAddressClassUnknown; 2378 } 2379 } 2380 } 2381 2382 // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB 2383 // symbols. See above for 2384 // more details. 2385 uint64_t symbol_value = symbol.st_value + symbol_value_offset; 2386 2387 if (symbol_section_sp == nullptr && section_idx == SHN_ABS && 2388 symbol.st_size != 0) { 2389 // We don't have a section for a symbol with non-zero size. Create a new 2390 // section for it 2391 // so the address range covered by the symbol is also covered by the 2392 // module (represented 2393 // through the section list). It is needed so module lookup for the 2394 // addresses covered 2395 // by this symbol will be successfull. This case happens for absolute 2396 // symbols. 2397 ConstString fake_section_name(std::string(".absolute.") + symbol_name); 2398 symbol_section_sp = 2399 std::make_shared<Section>(module_sp, this, SHN_ABS, fake_section_name, 2400 eSectionTypeAbsoluteAddress, symbol_value, 2401 symbol.st_size, 0, 0, 0, SHF_ALLOC); 2402 2403 module_section_list->AddSection(symbol_section_sp); 2404 section_list->AddSection(symbol_section_sp); 2405 } 2406 2407 if (symbol_section_sp && 2408 CalculateType() != ObjectFile::Type::eTypeObjectFile) 2409 symbol_value -= symbol_section_sp->GetFileAddress(); 2410 2411 if (symbol_section_sp && module_section_list && 2412 module_section_list != section_list) { 2413 const ConstString §_name = symbol_section_sp->GetName(); 2414 auto section_it = section_name_to_section.find(sect_name.GetCString()); 2415 if (section_it == section_name_to_section.end()) 2416 section_it = 2417 section_name_to_section 2418 .emplace(sect_name.GetCString(), 2419 module_section_list->FindSectionByName(sect_name)) 2420 .first; 2421 if (section_it->second && section_it->second->GetFileSize()) 2422 symbol_section_sp = section_it->second; 2423 } 2424 2425 bool is_global = symbol.getBinding() == STB_GLOBAL; 2426 uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags; 2427 bool is_mangled = (symbol_name[0] == '_' && symbol_name[1] == 'Z'); 2428 2429 llvm::StringRef symbol_ref(symbol_name); 2430 2431 // Symbol names may contain @VERSION suffixes. Find those and strip them 2432 // temporarily. 2433 size_t version_pos = symbol_ref.find('@'); 2434 bool has_suffix = version_pos != llvm::StringRef::npos; 2435 llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos); 2436 Mangled mangled(ConstString(symbol_bare), is_mangled); 2437 2438 // Now append the suffix back to mangled and unmangled names. Only do it if 2439 // the 2440 // demangling was successful (string is not empty). 2441 if (has_suffix) { 2442 llvm::StringRef suffix = symbol_ref.substr(version_pos); 2443 2444 llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef(); 2445 if (!mangled_name.empty()) 2446 mangled.SetMangledName(ConstString((mangled_name + suffix).str())); 2447 2448 ConstString demangled = 2449 mangled.GetDemangledName(lldb::eLanguageTypeUnknown); 2450 llvm::StringRef demangled_name = demangled.GetStringRef(); 2451 if (!demangled_name.empty()) 2452 mangled.SetDemangledName(ConstString((demangled_name + suffix).str())); 2453 } 2454 2455 // In ELF all symbol should have a valid size but it is not true for some 2456 // function symbols 2457 // coming from hand written assembly. As none of the function symbol should 2458 // have 0 size we 2459 // try to calculate the size for these symbols in the symtab with saying 2460 // that their original 2461 // size is not valid. 2462 bool symbol_size_valid = 2463 symbol.st_size != 0 || symbol.getType() != STT_FUNC; 2464 2465 Symbol dc_symbol( 2466 i + start_id, // ID is the original symbol table index. 2467 mangled, 2468 symbol_type, // Type of this symbol 2469 is_global, // Is this globally visible? 2470 false, // Is this symbol debug info? 2471 false, // Is this symbol a trampoline? 2472 false, // Is this symbol artificial? 2473 AddressRange(symbol_section_sp, // Section in which this symbol is 2474 // defined or null. 2475 symbol_value, // Offset in section or symbol value. 2476 symbol.st_size), // Size in bytes of this symbol. 2477 symbol_size_valid, // Symbol size is valid 2478 has_suffix, // Contains linker annotations? 2479 flags); // Symbol flags. 2480 symtab->AddSymbol(dc_symbol); 2481 } 2482 return i; 2483 } 2484 2485 unsigned ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, 2486 user_id_t start_id, 2487 lldb_private::Section *symtab) { 2488 if (symtab->GetObjectFile() != this) { 2489 // If the symbol table section is owned by a different object file, have it 2490 // do the 2491 // parsing. 2492 ObjectFileELF *obj_file_elf = 2493 static_cast<ObjectFileELF *>(symtab->GetObjectFile()); 2494 return obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab); 2495 } 2496 2497 // Get section list for this object file. 2498 SectionList *section_list = m_sections_ap.get(); 2499 if (!section_list) 2500 return 0; 2501 2502 user_id_t symtab_id = symtab->GetID(); 2503 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 2504 assert(symtab_hdr->sh_type == SHT_SYMTAB || 2505 symtab_hdr->sh_type == SHT_DYNSYM); 2506 2507 // sh_link: section header index of associated string table. 2508 // Section ID's are ones based. 2509 user_id_t strtab_id = symtab_hdr->sh_link + 1; 2510 Section *strtab = section_list->FindSectionByID(strtab_id).get(); 2511 2512 if (symtab && strtab) { 2513 assert(symtab->GetObjectFile() == this); 2514 assert(strtab->GetObjectFile() == this); 2515 2516 DataExtractor symtab_data; 2517 DataExtractor strtab_data; 2518 if (ReadSectionData(symtab, symtab_data) && 2519 ReadSectionData(strtab, strtab_data)) { 2520 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; 2521 2522 return ParseSymbols(symbol_table, start_id, section_list, num_symbols, 2523 symtab_data, strtab_data); 2524 } 2525 } 2526 2527 return 0; 2528 } 2529 2530 size_t ObjectFileELF::ParseDynamicSymbols() { 2531 if (m_dynamic_symbols.size()) 2532 return m_dynamic_symbols.size(); 2533 2534 SectionList *section_list = GetSectionList(); 2535 if (!section_list) 2536 return 0; 2537 2538 // Find the SHT_DYNAMIC section. 2539 Section *dynsym = 2540 section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) 2541 .get(); 2542 if (!dynsym) 2543 return 0; 2544 assert(dynsym->GetObjectFile() == this); 2545 2546 ELFDynamic symbol; 2547 DataExtractor dynsym_data; 2548 if (ReadSectionData(dynsym, dynsym_data)) { 2549 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 2550 lldb::offset_t cursor = 0; 2551 2552 while (cursor < section_size) { 2553 if (!symbol.Parse(dynsym_data, &cursor)) 2554 break; 2555 2556 m_dynamic_symbols.push_back(symbol); 2557 } 2558 } 2559 2560 return m_dynamic_symbols.size(); 2561 } 2562 2563 const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) { 2564 if (!ParseDynamicSymbols()) 2565 return NULL; 2566 2567 DynamicSymbolCollIter I = m_dynamic_symbols.begin(); 2568 DynamicSymbolCollIter E = m_dynamic_symbols.end(); 2569 for (; I != E; ++I) { 2570 ELFDynamic *symbol = &*I; 2571 2572 if (symbol->d_tag == tag) 2573 return symbol; 2574 } 2575 2576 return NULL; 2577 } 2578 2579 unsigned ObjectFileELF::PLTRelocationType() { 2580 // DT_PLTREL 2581 // This member specifies the type of relocation entry to which the 2582 // procedure linkage table refers. The d_val member holds DT_REL or 2583 // DT_RELA, as appropriate. All relocations in a procedure linkage table 2584 // must use the same relocation. 2585 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); 2586 2587 if (symbol) 2588 return symbol->d_val; 2589 2590 return 0; 2591 } 2592 2593 // Returns the size of the normal plt entries and the offset of the first normal 2594 // plt entry. The 2595 // 0th entry in the plt table is usually a resolution entry which have different 2596 // size in some 2597 // architectures then the rest of the plt entries. 2598 static std::pair<uint64_t, uint64_t> 2599 GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr, 2600 const ELFSectionHeader *plt_hdr) { 2601 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2602 2603 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 2604 // bytes. 2605 // So round the entsize up by the alignment if addralign is set. 2606 elf_xword plt_entsize = 2607 plt_hdr->sh_addralign 2608 ? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign) 2609 : plt_hdr->sh_entsize; 2610 2611 // Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly. 2612 // PLT entries relocation code in general requires multiple instruction and 2613 // should be greater than 4 bytes in most cases. Try to guess correct size 2614 // just in case. 2615 if (plt_entsize <= 4) { 2616 // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the 2617 // size of the plt 2618 // entries based on the number of entries and the size of the plt section 2619 // with the 2620 // assumption that the size of the 0th entry is at least as big as the size 2621 // of the normal 2622 // entries and it isn't much bigger then that. 2623 if (plt_hdr->sh_addralign) 2624 plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign / 2625 (num_relocations + 1) * plt_hdr->sh_addralign; 2626 else 2627 plt_entsize = plt_hdr->sh_size / (num_relocations + 1); 2628 } 2629 2630 elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize; 2631 2632 return std::make_pair(plt_entsize, plt_offset); 2633 } 2634 2635 static unsigned ParsePLTRelocations( 2636 Symtab *symbol_table, user_id_t start_id, unsigned rel_type, 2637 const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, 2638 const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr, 2639 const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data, 2640 DataExtractor &symtab_data, DataExtractor &strtab_data) { 2641 ELFRelocation rel(rel_type); 2642 ELFSymbol symbol; 2643 lldb::offset_t offset = 0; 2644 2645 uint64_t plt_offset, plt_entsize; 2646 std::tie(plt_entsize, plt_offset) = 2647 GetPltEntrySizeAndOffset(rel_hdr, plt_hdr); 2648 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2649 2650 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 2651 reloc_info_fn reloc_type; 2652 reloc_info_fn reloc_symbol; 2653 2654 if (hdr->Is32Bit()) { 2655 reloc_type = ELFRelocation::RelocType32; 2656 reloc_symbol = ELFRelocation::RelocSymbol32; 2657 } else { 2658 reloc_type = ELFRelocation::RelocType64; 2659 reloc_symbol = ELFRelocation::RelocSymbol64; 2660 } 2661 2662 unsigned slot_type = hdr->GetRelocationJumpSlotType(); 2663 unsigned i; 2664 for (i = 0; i < num_relocations; ++i) { 2665 if (rel.Parse(rel_data, &offset) == false) 2666 break; 2667 2668 if (reloc_type(rel) != slot_type) 2669 continue; 2670 2671 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; 2672 if (!symbol.Parse(symtab_data, &symbol_offset)) 2673 break; 2674 2675 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 2676 bool is_mangled = 2677 symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; 2678 uint64_t plt_index = plt_offset + i * plt_entsize; 2679 2680 Symbol jump_symbol( 2681 i + start_id, // Symbol table index 2682 symbol_name, // symbol name. 2683 is_mangled, // is the symbol name mangled? 2684 eSymbolTypeTrampoline, // Type of this symbol 2685 false, // Is this globally visible? 2686 false, // Is this symbol debug info? 2687 true, // Is this symbol a trampoline? 2688 true, // Is this symbol artificial? 2689 plt_section_sp, // Section in which this symbol is defined or null. 2690 plt_index, // Offset in section or symbol value. 2691 plt_entsize, // Size in bytes of this symbol. 2692 true, // Size is valid 2693 false, // Contains linker annotations? 2694 0); // Symbol flags. 2695 2696 symbol_table->AddSymbol(jump_symbol); 2697 } 2698 2699 return i; 2700 } 2701 2702 unsigned 2703 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id, 2704 const ELFSectionHeaderInfo *rel_hdr, 2705 user_id_t rel_id) { 2706 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 2707 2708 // The link field points to the associated symbol table. 2709 user_id_t symtab_id = rel_hdr->sh_link; 2710 2711 // If the link field doesn't point to the appropriate symbol name table then 2712 // try to find it by name as some compiler don't fill in the link fields. 2713 if (!symtab_id) 2714 symtab_id = GetSectionIndexByName(".dynsym"); 2715 2716 // Get PLT section. We cannot use rel_hdr->sh_info, since current linkers 2717 // point that to the .got.plt or .got section instead of .plt. 2718 user_id_t plt_id = GetSectionIndexByName(".plt"); 2719 2720 if (!symtab_id || !plt_id) 2721 return 0; 2722 2723 // Section ID's are ones based; 2724 symtab_id++; 2725 plt_id++; 2726 2727 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); 2728 if (!plt_hdr) 2729 return 0; 2730 2731 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); 2732 if (!sym_hdr) 2733 return 0; 2734 2735 SectionList *section_list = m_sections_ap.get(); 2736 if (!section_list) 2737 return 0; 2738 2739 Section *rel_section = section_list->FindSectionByID(rel_id).get(); 2740 if (!rel_section) 2741 return 0; 2742 2743 SectionSP plt_section_sp(section_list->FindSectionByID(plt_id)); 2744 if (!plt_section_sp) 2745 return 0; 2746 2747 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 2748 if (!symtab) 2749 return 0; 2750 2751 // sh_link points to associated string table. 2752 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get(); 2753 if (!strtab) 2754 return 0; 2755 2756 DataExtractor rel_data; 2757 if (!ReadSectionData(rel_section, rel_data)) 2758 return 0; 2759 2760 DataExtractor symtab_data; 2761 if (!ReadSectionData(symtab, symtab_data)) 2762 return 0; 2763 2764 DataExtractor strtab_data; 2765 if (!ReadSectionData(strtab, strtab_data)) 2766 return 0; 2767 2768 unsigned rel_type = PLTRelocationType(); 2769 if (!rel_type) 2770 return 0; 2771 2772 return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header, 2773 rel_hdr, plt_hdr, sym_hdr, plt_section_sp, 2774 rel_data, symtab_data, strtab_data); 2775 } 2776 2777 unsigned ObjectFileELF::RelocateSection( 2778 Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, 2779 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr, 2780 DataExtractor &rel_data, DataExtractor &symtab_data, 2781 DataExtractor &debug_data, Section *rel_section) { 2782 ELFRelocation rel(rel_hdr->sh_type); 2783 lldb::addr_t offset = 0; 2784 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2785 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 2786 reloc_info_fn reloc_type; 2787 reloc_info_fn reloc_symbol; 2788 2789 if (hdr->Is32Bit()) { 2790 reloc_type = ELFRelocation::RelocType32; 2791 reloc_symbol = ELFRelocation::RelocSymbol32; 2792 } else { 2793 reloc_type = ELFRelocation::RelocType64; 2794 reloc_symbol = ELFRelocation::RelocSymbol64; 2795 } 2796 2797 for (unsigned i = 0; i < num_relocations; ++i) { 2798 if (rel.Parse(rel_data, &offset) == false) 2799 break; 2800 2801 Symbol *symbol = NULL; 2802 2803 if (hdr->Is32Bit()) { 2804 switch (reloc_type(rel)) { 2805 case R_386_32: 2806 case R_386_PC32: 2807 default: 2808 assert(false && "unexpected relocation type"); 2809 } 2810 } else { 2811 switch (reloc_type(rel)) { 2812 case R_X86_64_64: { 2813 symbol = symtab->FindSymbolByID(reloc_symbol(rel)); 2814 if (symbol) { 2815 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2816 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2817 uint64_t *dst = reinterpret_cast<uint64_t *>( 2818 data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + 2819 ELFRelocation::RelocOffset64(rel)); 2820 *dst = value + ELFRelocation::RelocAddend64(rel); 2821 } 2822 break; 2823 } 2824 case R_X86_64_32: 2825 case R_X86_64_32S: { 2826 symbol = symtab->FindSymbolByID(reloc_symbol(rel)); 2827 if (symbol) { 2828 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2829 value += ELFRelocation::RelocAddend32(rel); 2830 assert( 2831 (reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) || 2832 (reloc_type(rel) == R_X86_64_32S && 2833 ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN))); 2834 uint32_t truncated_addr = (value & 0xFFFFFFFF); 2835 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2836 uint32_t *dst = reinterpret_cast<uint32_t *>( 2837 data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + 2838 ELFRelocation::RelocOffset32(rel)); 2839 *dst = truncated_addr; 2840 } 2841 break; 2842 } 2843 case R_X86_64_PC32: 2844 default: 2845 assert(false && "unexpected relocation type"); 2846 } 2847 } 2848 } 2849 2850 return 0; 2851 } 2852 2853 unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, 2854 user_id_t rel_id) { 2855 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 2856 2857 // Parse in the section list if needed. 2858 SectionList *section_list = GetSectionList(); 2859 if (!section_list) 2860 return 0; 2861 2862 // Section ID's are ones based. 2863 user_id_t symtab_id = rel_hdr->sh_link + 1; 2864 user_id_t debug_id = rel_hdr->sh_info + 1; 2865 2866 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 2867 if (!symtab_hdr) 2868 return 0; 2869 2870 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id); 2871 if (!debug_hdr) 2872 return 0; 2873 2874 Section *rel = section_list->FindSectionByID(rel_id).get(); 2875 if (!rel) 2876 return 0; 2877 2878 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 2879 if (!symtab) 2880 return 0; 2881 2882 Section *debug = section_list->FindSectionByID(debug_id).get(); 2883 if (!debug) 2884 return 0; 2885 2886 DataExtractor rel_data; 2887 DataExtractor symtab_data; 2888 DataExtractor debug_data; 2889 2890 if (ReadSectionData(rel, rel_data) && ReadSectionData(symtab, symtab_data) && 2891 ReadSectionData(debug, debug_data)) { 2892 RelocateSection(m_symtab_ap.get(), &m_header, rel_hdr, symtab_hdr, 2893 debug_hdr, rel_data, symtab_data, debug_data, debug); 2894 } 2895 2896 return 0; 2897 } 2898 2899 Symtab *ObjectFileELF::GetSymtab() { 2900 ModuleSP module_sp(GetModule()); 2901 if (!module_sp) 2902 return NULL; 2903 2904 // We always want to use the main object file so we (hopefully) only have one 2905 // cached copy 2906 // of our symtab, dynamic sections, etc. 2907 ObjectFile *module_obj_file = module_sp->GetObjectFile(); 2908 if (module_obj_file && module_obj_file != this) 2909 return module_obj_file->GetSymtab(); 2910 2911 if (m_symtab_ap.get() == NULL) { 2912 SectionList *section_list = module_sp->GetSectionList(); 2913 if (!section_list) 2914 return NULL; 2915 2916 uint64_t symbol_id = 0; 2917 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 2918 2919 // Sharable objects and dynamic executables usually have 2 distinct symbol 2920 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a 2921 // smaller 2922 // version of the symtab that only contains global symbols. The information 2923 // found 2924 // in the dynsym is therefore also found in the symtab, while the reverse is 2925 // not 2926 // necessarily true. 2927 Section *symtab = 2928 section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get(); 2929 if (!symtab) { 2930 // The symtab section is non-allocable and can be stripped, so if it 2931 // doesn't exist 2932 // then use the dynsym section which should always be there. 2933 symtab = 2934 section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true) 2935 .get(); 2936 } 2937 if (symtab) { 2938 m_symtab_ap.reset(new Symtab(symtab->GetObjectFile())); 2939 symbol_id += ParseSymbolTable(m_symtab_ap.get(), symbol_id, symtab); 2940 } 2941 2942 // DT_JMPREL 2943 // If present, this entry's d_ptr member holds the address of 2944 // relocation 2945 // entries associated solely with the procedure linkage table. 2946 // Separating 2947 // these relocation entries lets the dynamic linker ignore them during 2948 // process initialization, if lazy binding is enabled. If this entry is 2949 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must 2950 // also be present. 2951 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); 2952 if (symbol) { 2953 // Synthesize trampoline symbols to help navigate the PLT. 2954 addr_t addr = symbol->d_ptr; 2955 Section *reloc_section = 2956 section_list->FindSectionContainingFileAddress(addr).get(); 2957 if (reloc_section) { 2958 user_id_t reloc_id = reloc_section->GetID(); 2959 const ELFSectionHeaderInfo *reloc_header = 2960 GetSectionHeaderByIndex(reloc_id); 2961 assert(reloc_header); 2962 2963 if (m_symtab_ap == nullptr) 2964 m_symtab_ap.reset(new Symtab(reloc_section->GetObjectFile())); 2965 2966 ParseTrampolineSymbols(m_symtab_ap.get(), symbol_id, reloc_header, 2967 reloc_id); 2968 } 2969 } 2970 2971 DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo(); 2972 if (eh_frame) { 2973 if (m_symtab_ap == nullptr) 2974 m_symtab_ap.reset(new Symtab(this)); 2975 ParseUnwindSymbols(m_symtab_ap.get(), eh_frame); 2976 } 2977 2978 // If we still don't have any symtab then create an empty instance to avoid 2979 // do the section 2980 // lookup next time. 2981 if (m_symtab_ap == nullptr) 2982 m_symtab_ap.reset(new Symtab(this)); 2983 2984 m_symtab_ap->CalculateSymbolSizes(); 2985 } 2986 2987 for (SectionHeaderCollIter I = m_section_headers.begin(); 2988 I != m_section_headers.end(); ++I) { 2989 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) { 2990 if (CalculateType() == eTypeObjectFile) { 2991 const char *section_name = I->section_name.AsCString(""); 2992 if (strstr(section_name, ".rela.debug") || 2993 strstr(section_name, ".rel.debug")) { 2994 const ELFSectionHeader &reloc_header = *I; 2995 user_id_t reloc_id = SectionIndex(I); 2996 RelocateDebugSections(&reloc_header, reloc_id); 2997 } 2998 } 2999 } 3000 } 3001 return m_symtab_ap.get(); 3002 } 3003 3004 void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table, 3005 DWARFCallFrameInfo *eh_frame) { 3006 SectionList *section_list = GetSectionList(); 3007 if (!section_list) 3008 return; 3009 3010 // First we save the new symbols into a separate list and add them to the 3011 // symbol table after 3012 // we colleced all symbols we want to add. This is neccessary because adding a 3013 // new symbol 3014 // invalidates the internal index of the symtab what causing the next lookup 3015 // to be slow because 3016 // it have to recalculate the index first. 3017 std::vector<Symbol> new_symbols; 3018 3019 eh_frame->ForEachFDEEntries([this, symbol_table, section_list, &new_symbols]( 3020 lldb::addr_t file_addr, uint32_t size, dw_offset_t) { 3021 Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr); 3022 if (symbol) { 3023 if (!symbol->GetByteSizeIsValid()) { 3024 symbol->SetByteSize(size); 3025 symbol->SetSizeIsSynthesized(true); 3026 } 3027 } else { 3028 SectionSP section_sp = 3029 section_list->FindSectionContainingFileAddress(file_addr); 3030 if (section_sp) { 3031 addr_t offset = file_addr - section_sp->GetFileAddress(); 3032 const char *symbol_name = GetNextSyntheticSymbolName().GetCString(); 3033 uint64_t symbol_id = symbol_table->GetNumSymbols(); 3034 Symbol eh_symbol( 3035 symbol_id, // Symbol table index. 3036 symbol_name, // Symbol name. 3037 false, // Is the symbol name mangled? 3038 eSymbolTypeCode, // Type of this symbol. 3039 true, // Is this globally visible? 3040 false, // Is this symbol debug info? 3041 false, // Is this symbol a trampoline? 3042 true, // Is this symbol artificial? 3043 section_sp, // Section in which this symbol is defined or null. 3044 offset, // Offset in section or symbol value. 3045 0, // Size: Don't specify the size as an FDE can 3046 false, // Size is valid: cover multiple symbols. 3047 false, // Contains linker annotations? 3048 0); // Symbol flags. 3049 new_symbols.push_back(eh_symbol); 3050 } 3051 } 3052 return true; 3053 }); 3054 3055 for (const Symbol &s : new_symbols) 3056 symbol_table->AddSymbol(s); 3057 } 3058 3059 bool ObjectFileELF::IsStripped() { 3060 // TODO: determine this for ELF 3061 return false; 3062 } 3063 3064 //===----------------------------------------------------------------------===// 3065 // Dump 3066 // 3067 // Dump the specifics of the runtime file container (such as any headers 3068 // segments, sections, etc). 3069 //---------------------------------------------------------------------- 3070 void ObjectFileELF::Dump(Stream *s) { 3071 ModuleSP module_sp(GetModule()); 3072 if (!module_sp) { 3073 return; 3074 } 3075 3076 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 3077 s->Printf("%p: ", static_cast<void *>(this)); 3078 s->Indent(); 3079 s->PutCString("ObjectFileELF"); 3080 3081 ArchSpec header_arch; 3082 GetArchitecture(header_arch); 3083 3084 *s << ", file = '" << m_file 3085 << "', arch = " << header_arch.GetArchitectureName() << "\n"; 3086 3087 DumpELFHeader(s, m_header); 3088 s->EOL(); 3089 DumpELFProgramHeaders(s); 3090 s->EOL(); 3091 DumpELFSectionHeaders(s); 3092 s->EOL(); 3093 SectionList *section_list = GetSectionList(); 3094 if (section_list) 3095 section_list->Dump(s, NULL, true, UINT32_MAX); 3096 Symtab *symtab = GetSymtab(); 3097 if (symtab) 3098 symtab->Dump(s, NULL, eSortOrderNone); 3099 s->EOL(); 3100 DumpDependentModules(s); 3101 s->EOL(); 3102 } 3103 3104 //---------------------------------------------------------------------- 3105 // DumpELFHeader 3106 // 3107 // Dump the ELF header to the specified output stream 3108 //---------------------------------------------------------------------- 3109 void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) { 3110 s->PutCString("ELF Header\n"); 3111 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); 3112 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1], 3113 header.e_ident[EI_MAG1]); 3114 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2], 3115 header.e_ident[EI_MAG2]); 3116 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3], 3117 header.e_ident[EI_MAG3]); 3118 3119 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); 3120 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); 3121 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); 3122 s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); 3123 s->Printf("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); 3124 3125 s->Printf("e_type = 0x%4.4x ", header.e_type); 3126 DumpELFHeader_e_type(s, header.e_type); 3127 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); 3128 s->Printf("e_version = 0x%8.8x\n", header.e_version); 3129 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); 3130 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); 3131 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); 3132 s->Printf("e_flags = 0x%8.8x\n", header.e_flags); 3133 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); 3134 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); 3135 s->Printf("e_phnum = 0x%8.8x\n", header.e_phnum); 3136 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); 3137 s->Printf("e_shnum = 0x%8.8x\n", header.e_shnum); 3138 s->Printf("e_shstrndx = 0x%8.8x\n", header.e_shstrndx); 3139 } 3140 3141 //---------------------------------------------------------------------- 3142 // DumpELFHeader_e_type 3143 // 3144 // Dump an token value for the ELF header member e_type 3145 //---------------------------------------------------------------------- 3146 void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) { 3147 switch (e_type) { 3148 case ET_NONE: 3149 *s << "ET_NONE"; 3150 break; 3151 case ET_REL: 3152 *s << "ET_REL"; 3153 break; 3154 case ET_EXEC: 3155 *s << "ET_EXEC"; 3156 break; 3157 case ET_DYN: 3158 *s << "ET_DYN"; 3159 break; 3160 case ET_CORE: 3161 *s << "ET_CORE"; 3162 break; 3163 default: 3164 break; 3165 } 3166 } 3167 3168 //---------------------------------------------------------------------- 3169 // DumpELFHeader_e_ident_EI_DATA 3170 // 3171 // Dump an token value for the ELF header member e_ident[EI_DATA] 3172 //---------------------------------------------------------------------- 3173 void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, 3174 unsigned char ei_data) { 3175 switch (ei_data) { 3176 case ELFDATANONE: 3177 *s << "ELFDATANONE"; 3178 break; 3179 case ELFDATA2LSB: 3180 *s << "ELFDATA2LSB - Little Endian"; 3181 break; 3182 case ELFDATA2MSB: 3183 *s << "ELFDATA2MSB - Big Endian"; 3184 break; 3185 default: 3186 break; 3187 } 3188 } 3189 3190 //---------------------------------------------------------------------- 3191 // DumpELFProgramHeader 3192 // 3193 // Dump a single ELF program header to the specified output stream 3194 //---------------------------------------------------------------------- 3195 void ObjectFileELF::DumpELFProgramHeader(Stream *s, 3196 const ELFProgramHeader &ph) { 3197 DumpELFProgramHeader_p_type(s, ph.p_type); 3198 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, 3199 ph.p_vaddr, ph.p_paddr); 3200 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, 3201 ph.p_flags); 3202 3203 DumpELFProgramHeader_p_flags(s, ph.p_flags); 3204 s->Printf(") %8.8" PRIx64, ph.p_align); 3205 } 3206 3207 //---------------------------------------------------------------------- 3208 // DumpELFProgramHeader_p_type 3209 // 3210 // Dump an token value for the ELF program header member p_type which 3211 // describes the type of the program header 3212 // ---------------------------------------------------------------------- 3213 void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) { 3214 const int kStrWidth = 15; 3215 switch (p_type) { 3216 CASE_AND_STREAM(s, PT_NULL, kStrWidth); 3217 CASE_AND_STREAM(s, PT_LOAD, kStrWidth); 3218 CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth); 3219 CASE_AND_STREAM(s, PT_INTERP, kStrWidth); 3220 CASE_AND_STREAM(s, PT_NOTE, kStrWidth); 3221 CASE_AND_STREAM(s, PT_SHLIB, kStrWidth); 3222 CASE_AND_STREAM(s, PT_PHDR, kStrWidth); 3223 CASE_AND_STREAM(s, PT_TLS, kStrWidth); 3224 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); 3225 default: 3226 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); 3227 break; 3228 } 3229 } 3230 3231 //---------------------------------------------------------------------- 3232 // DumpELFProgramHeader_p_flags 3233 // 3234 // Dump an token value for the ELF program header member p_flags 3235 //---------------------------------------------------------------------- 3236 void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) { 3237 *s << ((p_flags & PF_X) ? "PF_X" : " ") 3238 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') 3239 << ((p_flags & PF_W) ? "PF_W" : " ") 3240 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') 3241 << ((p_flags & PF_R) ? "PF_R" : " "); 3242 } 3243 3244 //---------------------------------------------------------------------- 3245 // DumpELFProgramHeaders 3246 // 3247 // Dump all of the ELF program header to the specified output stream 3248 //---------------------------------------------------------------------- 3249 void ObjectFileELF::DumpELFProgramHeaders(Stream *s) { 3250 if (!ParseProgramHeaders()) 3251 return; 3252 3253 s->PutCString("Program Headers\n"); 3254 s->PutCString("IDX p_type p_offset p_vaddr p_paddr " 3255 "p_filesz p_memsz p_flags p_align\n"); 3256 s->PutCString("==== --------------- -------- -------- -------- " 3257 "-------- -------- ------------------------- --------\n"); 3258 3259 uint32_t idx = 0; 3260 for (ProgramHeaderCollConstIter I = m_program_headers.begin(); 3261 I != m_program_headers.end(); ++I, ++idx) { 3262 s->Printf("[%2u] ", idx); 3263 ObjectFileELF::DumpELFProgramHeader(s, *I); 3264 s->EOL(); 3265 } 3266 } 3267 3268 //---------------------------------------------------------------------- 3269 // DumpELFSectionHeader 3270 // 3271 // Dump a single ELF section header to the specified output stream 3272 //---------------------------------------------------------------------- 3273 void ObjectFileELF::DumpELFSectionHeader(Stream *s, 3274 const ELFSectionHeaderInfo &sh) { 3275 s->Printf("%8.8x ", sh.sh_name); 3276 DumpELFSectionHeader_sh_type(s, sh.sh_type); 3277 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); 3278 DumpELFSectionHeader_sh_flags(s, sh.sh_flags); 3279 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, 3280 sh.sh_offset, sh.sh_size); 3281 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); 3282 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); 3283 } 3284 3285 //---------------------------------------------------------------------- 3286 // DumpELFSectionHeader_sh_type 3287 // 3288 // Dump an token value for the ELF section header member sh_type which 3289 // describes the type of the section 3290 //---------------------------------------------------------------------- 3291 void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) { 3292 const int kStrWidth = 12; 3293 switch (sh_type) { 3294 CASE_AND_STREAM(s, SHT_NULL, kStrWidth); 3295 CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth); 3296 CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth); 3297 CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth); 3298 CASE_AND_STREAM(s, SHT_RELA, kStrWidth); 3299 CASE_AND_STREAM(s, SHT_HASH, kStrWidth); 3300 CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth); 3301 CASE_AND_STREAM(s, SHT_NOTE, kStrWidth); 3302 CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth); 3303 CASE_AND_STREAM(s, SHT_REL, kStrWidth); 3304 CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth); 3305 CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth); 3306 CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth); 3307 CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth); 3308 CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth); 3309 CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth); 3310 default: 3311 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); 3312 break; 3313 } 3314 } 3315 3316 //---------------------------------------------------------------------- 3317 // DumpELFSectionHeader_sh_flags 3318 // 3319 // Dump an token value for the ELF section header member sh_flags 3320 //---------------------------------------------------------------------- 3321 void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, 3322 elf_xword sh_flags) { 3323 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") 3324 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') 3325 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") 3326 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') 3327 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); 3328 } 3329 3330 //---------------------------------------------------------------------- 3331 // DumpELFSectionHeaders 3332 // 3333 // Dump all of the ELF section header to the specified output stream 3334 //---------------------------------------------------------------------- 3335 void ObjectFileELF::DumpELFSectionHeaders(Stream *s) { 3336 if (!ParseSectionHeaders()) 3337 return; 3338 3339 s->PutCString("Section Headers\n"); 3340 s->PutCString("IDX name type flags " 3341 "addr offset size link info addralgn " 3342 "entsize Name\n"); 3343 s->PutCString("==== -------- ------------ -------------------------------- " 3344 "-------- -------- -------- -------- -------- -------- " 3345 "-------- ====================\n"); 3346 3347 uint32_t idx = 0; 3348 for (SectionHeaderCollConstIter I = m_section_headers.begin(); 3349 I != m_section_headers.end(); ++I, ++idx) { 3350 s->Printf("[%2u] ", idx); 3351 ObjectFileELF::DumpELFSectionHeader(s, *I); 3352 const char *section_name = I->section_name.AsCString(""); 3353 if (section_name) 3354 *s << ' ' << section_name << "\n"; 3355 } 3356 } 3357 3358 void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) { 3359 size_t num_modules = ParseDependentModules(); 3360 3361 if (num_modules > 0) { 3362 s->PutCString("Dependent Modules:\n"); 3363 for (unsigned i = 0; i < num_modules; ++i) { 3364 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); 3365 s->Printf(" %s\n", spec.GetFilename().GetCString()); 3366 } 3367 } 3368 } 3369 3370 bool ObjectFileELF::GetArchitecture(ArchSpec &arch) { 3371 if (!ParseHeader()) 3372 return false; 3373 3374 if (m_section_headers.empty()) { 3375 // Allow elf notes to be parsed which may affect the detected architecture. 3376 ParseSectionHeaders(); 3377 } 3378 3379 if (CalculateType() == eTypeCoreFile && 3380 m_arch_spec.TripleOSIsUnspecifiedUnknown()) { 3381 // Core files don't have section headers yet they have PT_NOTE program 3382 // headers 3383 // that might shed more light on the architecture 3384 if (ParseProgramHeaders()) { 3385 for (size_t i = 1, count = GetProgramHeaderCount(); i <= count; ++i) { 3386 const elf::ELFProgramHeader *header = GetProgramHeaderByIndex(i); 3387 if (header && header->p_type == PT_NOTE && header->p_offset != 0 && 3388 header->p_filesz > 0) { 3389 DataExtractor data; 3390 if (data.SetData(m_data, header->p_offset, header->p_filesz) == 3391 header->p_filesz) { 3392 lldb_private::UUID uuid; 3393 RefineModuleDetailsFromNote(data, m_arch_spec, uuid); 3394 } 3395 } 3396 } 3397 } 3398 } 3399 arch = m_arch_spec; 3400 return true; 3401 } 3402 3403 ObjectFile::Type ObjectFileELF::CalculateType() { 3404 switch (m_header.e_type) { 3405 case llvm::ELF::ET_NONE: 3406 // 0 - No file type 3407 return eTypeUnknown; 3408 3409 case llvm::ELF::ET_REL: 3410 // 1 - Relocatable file 3411 return eTypeObjectFile; 3412 3413 case llvm::ELF::ET_EXEC: 3414 // 2 - Executable file 3415 return eTypeExecutable; 3416 3417 case llvm::ELF::ET_DYN: 3418 // 3 - Shared object file 3419 return eTypeSharedLibrary; 3420 3421 case ET_CORE: 3422 // 4 - Core file 3423 return eTypeCoreFile; 3424 3425 default: 3426 break; 3427 } 3428 return eTypeUnknown; 3429 } 3430 3431 ObjectFile::Strata ObjectFileELF::CalculateStrata() { 3432 switch (m_header.e_type) { 3433 case llvm::ELF::ET_NONE: 3434 // 0 - No file type 3435 return eStrataUnknown; 3436 3437 case llvm::ELF::ET_REL: 3438 // 1 - Relocatable file 3439 return eStrataUnknown; 3440 3441 case llvm::ELF::ET_EXEC: 3442 // 2 - Executable file 3443 // TODO: is there any way to detect that an executable is a kernel 3444 // related executable by inspecting the program headers, section 3445 // headers, symbols, or any other flag bits??? 3446 return eStrataUser; 3447 3448 case llvm::ELF::ET_DYN: 3449 // 3 - Shared object file 3450 // TODO: is there any way to detect that an shared library is a kernel 3451 // related executable by inspecting the program headers, section 3452 // headers, symbols, or any other flag bits??? 3453 return eStrataUnknown; 3454 3455 case ET_CORE: 3456 // 4 - Core file 3457 // TODO: is there any way to detect that an core file is a kernel 3458 // related executable by inspecting the program headers, section 3459 // headers, symbols, or any other flag bits??? 3460 return eStrataUnknown; 3461 3462 default: 3463 break; 3464 } 3465 return eStrataUnknown; 3466 } 3467