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