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