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