1 //===-- ELFHeader.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 <cstring> 11 12 #include "lldb/Core/Section.h" 13 #include "lldb/Utility/DataExtractor.h" 14 #include "lldb/Utility/Stream.h" 15 16 #include "ELFHeader.h" 17 18 using namespace elf; 19 using namespace lldb; 20 using namespace llvm::ELF; 21 22 //------------------------------------------------------------------------------ 23 // Static utility functions. 24 // 25 // GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor 26 // with error handling code and provide for parsing a sequence of values. 27 static bool GetMaxU64(const lldb_private::DataExtractor &data, 28 lldb::offset_t *offset, uint64_t *value, 29 uint32_t byte_size) { 30 const lldb::offset_t saved_offset = *offset; 31 *value = data.GetMaxU64(offset, byte_size); 32 return *offset != saved_offset; 33 } 34 35 static bool GetMaxU64(const lldb_private::DataExtractor &data, 36 lldb::offset_t *offset, uint64_t *value, 37 uint32_t byte_size, uint32_t count) { 38 lldb::offset_t saved_offset = *offset; 39 40 for (uint32_t i = 0; i < count; ++i, ++value) { 41 if (GetMaxU64(data, offset, value, byte_size) == false) { 42 *offset = saved_offset; 43 return false; 44 } 45 } 46 return true; 47 } 48 49 static bool GetMaxS64(const lldb_private::DataExtractor &data, 50 lldb::offset_t *offset, int64_t *value, 51 uint32_t byte_size) { 52 const lldb::offset_t saved_offset = *offset; 53 *value = data.GetMaxS64(offset, byte_size); 54 return *offset != saved_offset; 55 } 56 57 static bool GetMaxS64(const lldb_private::DataExtractor &data, 58 lldb::offset_t *offset, int64_t *value, 59 uint32_t byte_size, uint32_t count) { 60 lldb::offset_t saved_offset = *offset; 61 62 for (uint32_t i = 0; i < count; ++i, ++value) { 63 if (GetMaxS64(data, offset, value, byte_size) == false) { 64 *offset = saved_offset; 65 return false; 66 } 67 } 68 return true; 69 } 70 71 //------------------------------------------------------------------------------ 72 // ELFHeader 73 74 ELFHeader::ELFHeader() { memset(this, 0, sizeof(ELFHeader)); } 75 76 ByteOrder ELFHeader::GetByteOrder() const { 77 if (e_ident[EI_DATA] == ELFDATA2MSB) 78 return eByteOrderBig; 79 if (e_ident[EI_DATA] == ELFDATA2LSB) 80 return eByteOrderLittle; 81 return eByteOrderInvalid; 82 } 83 84 bool ELFHeader::HasHeaderExtension() const { 85 bool result = false; 86 87 // Check if any of these values looks like sentinel. 88 result |= e_phnum_hdr == 0xFFFF; // PN_XNUM 89 result |= e_shnum_hdr == SHN_UNDEF; 90 result |= e_shstrndx_hdr == SHN_XINDEX; 91 92 // If header extension is present, the section offset cannot be null. 93 result &= e_shoff != 0; 94 95 // Done. 96 return result; 97 } 98 99 void ELFHeader::ParseHeaderExtension(lldb_private::DataExtractor &data) { 100 // Extract section #0 header. 101 ELFSectionHeader section_zero; 102 lldb::offset_t offset = 0; 103 lldb_private::DataExtractor sh_data(data, e_shoff, e_shentsize); 104 bool ok = section_zero.Parse(sh_data, &offset); 105 106 // If we succeeded, fix the header. 107 if (ok) { 108 if (e_phnum_hdr == 0xFFFF) // PN_XNUM 109 e_phnum = section_zero.sh_info; 110 if (e_shnum_hdr == SHN_UNDEF) 111 e_shnum = section_zero.sh_size; 112 if (e_shstrndx_hdr == SHN_XINDEX) 113 e_shstrndx = section_zero.sh_link; 114 } 115 } 116 117 bool ELFHeader::Parse(lldb_private::DataExtractor &data, 118 lldb::offset_t *offset) { 119 // Read e_ident. This provides byte order and address size info. 120 if (data.GetU8(offset, &e_ident, EI_NIDENT) == NULL) 121 return false; 122 123 const unsigned byte_size = Is32Bit() ? 4 : 8; 124 data.SetByteOrder(GetByteOrder()); 125 data.SetAddressByteSize(byte_size); 126 127 // Read e_type and e_machine. 128 if (data.GetU16(offset, &e_type, 2) == NULL) 129 return false; 130 131 // Read e_version. 132 if (data.GetU32(offset, &e_version, 1) == NULL) 133 return false; 134 135 // Read e_entry, e_phoff and e_shoff. 136 if (GetMaxU64(data, offset, &e_entry, byte_size, 3) == false) 137 return false; 138 139 // Read e_flags. 140 if (data.GetU32(offset, &e_flags, 1) == NULL) 141 return false; 142 143 // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx. 144 if (data.GetU16(offset, &e_ehsize, 6) == NULL) 145 return false; 146 147 // Initialize e_phnum, e_shnum, and e_shstrndx with the values read from the 148 // header. 149 e_phnum = e_phnum_hdr; 150 e_shnum = e_shnum_hdr; 151 e_shstrndx = e_shstrndx_hdr; 152 153 // See if we have extended header in section #0. 154 if (HasHeaderExtension()) 155 ParseHeaderExtension(data); 156 157 return true; 158 } 159 160 bool ELFHeader::MagicBytesMatch(const uint8_t *magic) { 161 return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0; 162 } 163 164 unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) { 165 unsigned address_size = 0; 166 167 switch (magic[EI_CLASS]) { 168 case ELFCLASS32: 169 address_size = 4; 170 break; 171 172 case ELFCLASS64: 173 address_size = 8; 174 break; 175 } 176 return address_size; 177 } 178 179 unsigned ELFHeader::GetRelocationJumpSlotType() const { 180 unsigned slot = 0; 181 182 switch (e_machine) { 183 default: 184 assert(false && "architecture not supported"); 185 break; 186 case EM_PPC: 187 slot = R_PPC_JMP_SLOT; 188 break; 189 case EM_PPC64: 190 slot = R_PPC64_JMP_SLOT; 191 break; 192 case EM_386: 193 case EM_IAMCU: // FIXME: is this correct? 194 slot = R_386_JUMP_SLOT; 195 break; 196 case EM_X86_64: 197 slot = R_X86_64_JUMP_SLOT; 198 break; 199 case EM_ARM: 200 slot = R_ARM_JUMP_SLOT; 201 break; 202 case EM_HEXAGON: 203 slot = R_HEX_JMP_SLOT; 204 break; 205 case EM_AARCH64: 206 slot = R_AARCH64_JUMP_SLOT; 207 break; 208 case EM_MIPS: 209 slot = R_MIPS_JUMP_SLOT; 210 break; 211 case EM_S390: 212 slot = R_390_JMP_SLOT; 213 break; 214 } 215 216 return slot; 217 } 218 219 //------------------------------------------------------------------------------ 220 // ELFSectionHeader 221 222 ELFSectionHeader::ELFSectionHeader() { 223 memset(this, 0, sizeof(ELFSectionHeader)); 224 } 225 226 bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data, 227 lldb::offset_t *offset) { 228 const unsigned byte_size = data.GetAddressByteSize(); 229 230 // Read sh_name and sh_type. 231 if (data.GetU32(offset, &sh_name, 2) == NULL) 232 return false; 233 234 // Read sh_flags. 235 if (GetMaxU64(data, offset, &sh_flags, byte_size) == false) 236 return false; 237 238 // Read sh_addr, sh_off and sh_size. 239 if (GetMaxU64(data, offset, &sh_addr, byte_size, 3) == false) 240 return false; 241 242 // Read sh_link and sh_info. 243 if (data.GetU32(offset, &sh_link, 2) == NULL) 244 return false; 245 246 // Read sh_addralign and sh_entsize. 247 if (GetMaxU64(data, offset, &sh_addralign, byte_size, 2) == false) 248 return false; 249 250 return true; 251 } 252 253 //------------------------------------------------------------------------------ 254 // ELFSymbol 255 256 ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); } 257 258 #define ENUM_TO_CSTR(e) \ 259 case e: \ 260 return #e 261 262 const char *ELFSymbol::bindingToCString(unsigned char binding) { 263 switch (binding) { 264 ENUM_TO_CSTR(STB_LOCAL); 265 ENUM_TO_CSTR(STB_GLOBAL); 266 ENUM_TO_CSTR(STB_WEAK); 267 ENUM_TO_CSTR(STB_LOOS); 268 ENUM_TO_CSTR(STB_HIOS); 269 ENUM_TO_CSTR(STB_LOPROC); 270 ENUM_TO_CSTR(STB_HIPROC); 271 } 272 return ""; 273 } 274 275 const char *ELFSymbol::typeToCString(unsigned char type) { 276 switch (type) { 277 ENUM_TO_CSTR(STT_NOTYPE); 278 ENUM_TO_CSTR(STT_OBJECT); 279 ENUM_TO_CSTR(STT_FUNC); 280 ENUM_TO_CSTR(STT_SECTION); 281 ENUM_TO_CSTR(STT_FILE); 282 ENUM_TO_CSTR(STT_COMMON); 283 ENUM_TO_CSTR(STT_TLS); 284 ENUM_TO_CSTR(STT_GNU_IFUNC); 285 ENUM_TO_CSTR(STT_HIOS); 286 ENUM_TO_CSTR(STT_LOPROC); 287 ENUM_TO_CSTR(STT_HIPROC); 288 } 289 return ""; 290 } 291 292 const char *ELFSymbol::sectionIndexToCString( 293 elf_half shndx, const lldb_private::SectionList *section_list) { 294 switch (shndx) { 295 ENUM_TO_CSTR(SHN_UNDEF); 296 ENUM_TO_CSTR(SHN_LOPROC); 297 ENUM_TO_CSTR(SHN_HIPROC); 298 ENUM_TO_CSTR(SHN_LOOS); 299 ENUM_TO_CSTR(SHN_HIOS); 300 ENUM_TO_CSTR(SHN_ABS); 301 ENUM_TO_CSTR(SHN_COMMON); 302 ENUM_TO_CSTR(SHN_XINDEX); 303 default: { 304 const lldb_private::Section *section = 305 section_list->GetSectionAtIndex(shndx).get(); 306 if (section) 307 return section->GetName().AsCString(""); 308 } break; 309 } 310 return ""; 311 } 312 313 void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx, 314 const lldb_private::DataExtractor *strtab_data, 315 const lldb_private::SectionList *section_list) { 316 s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64 317 " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n", 318 idx, st_value, st_size, st_name, st_info, 319 bindingToCString(getBinding()), typeToCString(getType()), st_other, 320 st_shndx, sectionIndexToCString(st_shndx, section_list), 321 strtab_data ? strtab_data->PeekCStr(st_name) : ""); 322 } 323 324 bool ELFSymbol::Parse(const lldb_private::DataExtractor &data, 325 lldb::offset_t *offset) { 326 const unsigned byte_size = data.GetAddressByteSize(); 327 const bool parsing_32 = byte_size == 4; 328 329 // Read st_name. 330 if (data.GetU32(offset, &st_name, 1) == NULL) 331 return false; 332 333 if (parsing_32) { 334 // Read st_value and st_size. 335 if (GetMaxU64(data, offset, &st_value, byte_size, 2) == false) 336 return false; 337 338 // Read st_info and st_other. 339 if (data.GetU8(offset, &st_info, 2) == NULL) 340 return false; 341 342 // Read st_shndx. 343 if (data.GetU16(offset, &st_shndx, 1) == NULL) 344 return false; 345 } else { 346 // Read st_info and st_other. 347 if (data.GetU8(offset, &st_info, 2) == NULL) 348 return false; 349 350 // Read st_shndx. 351 if (data.GetU16(offset, &st_shndx, 1) == NULL) 352 return false; 353 354 // Read st_value and st_size. 355 if (data.GetU64(offset, &st_value, 2) == NULL) 356 return false; 357 } 358 return true; 359 } 360 361 //------------------------------------------------------------------------------ 362 // ELFProgramHeader 363 364 ELFProgramHeader::ELFProgramHeader() { 365 memset(this, 0, sizeof(ELFProgramHeader)); 366 } 367 368 bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data, 369 lldb::offset_t *offset) { 370 const uint32_t byte_size = data.GetAddressByteSize(); 371 const bool parsing_32 = byte_size == 4; 372 373 // Read p_type; 374 if (data.GetU32(offset, &p_type, 1) == NULL) 375 return false; 376 377 if (parsing_32) { 378 // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz. 379 if (GetMaxU64(data, offset, &p_offset, byte_size, 5) == false) 380 return false; 381 382 // Read p_flags. 383 if (data.GetU32(offset, &p_flags, 1) == NULL) 384 return false; 385 386 // Read p_align. 387 if (GetMaxU64(data, offset, &p_align, byte_size) == false) 388 return false; 389 } else { 390 // Read p_flags. 391 if (data.GetU32(offset, &p_flags, 1) == NULL) 392 return false; 393 394 // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align. 395 if (GetMaxU64(data, offset, &p_offset, byte_size, 6) == false) 396 return false; 397 } 398 399 return true; 400 } 401 402 //------------------------------------------------------------------------------ 403 // ELFDynamic 404 405 ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); } 406 407 bool ELFDynamic::Parse(const lldb_private::DataExtractor &data, 408 lldb::offset_t *offset) { 409 const unsigned byte_size = data.GetAddressByteSize(); 410 return GetMaxS64(data, offset, &d_tag, byte_size, 2); 411 } 412 413 //------------------------------------------------------------------------------ 414 // ELFRel 415 416 ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); } 417 418 bool ELFRel::Parse(const lldb_private::DataExtractor &data, 419 lldb::offset_t *offset) { 420 const unsigned byte_size = data.GetAddressByteSize(); 421 422 // Read r_offset and r_info. 423 if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false) 424 return false; 425 426 return true; 427 } 428 429 //------------------------------------------------------------------------------ 430 // ELFRela 431 432 ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); } 433 434 bool ELFRela::Parse(const lldb_private::DataExtractor &data, 435 lldb::offset_t *offset) { 436 const unsigned byte_size = data.GetAddressByteSize(); 437 438 // Read r_offset and r_info. 439 if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false) 440 return false; 441 442 // Read r_addend; 443 if (GetMaxS64(data, offset, &r_addend, byte_size) == false) 444 return false; 445 446 return true; 447 } 448