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