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