1f325ba9dSStephen Wilson //===-- ELFHeader.cpp ----------------------------------------- -*- C++ -*-===// 2f325ba9dSStephen Wilson // 3f325ba9dSStephen Wilson // The LLVM Compiler Infrastructure 4f325ba9dSStephen Wilson // 5f325ba9dSStephen Wilson // This file is distributed under the University of Illinois Open Source 6f325ba9dSStephen Wilson // License. See LICENSE.TXT for details. 7f325ba9dSStephen Wilson // 8f325ba9dSStephen Wilson //===----------------------------------------------------------------------===// 9f325ba9dSStephen Wilson 10f325ba9dSStephen Wilson #include <cstring> 11f325ba9dSStephen Wilson 129594f4c8SGreg Clayton #include "lldb/Core/Section.h" 13*666cc0b2SZachary Turner #include "lldb/Utility/DataExtractor.h" 14bf9a7730SZachary Turner #include "lldb/Utility/Stream.h" 15f325ba9dSStephen Wilson 16f325ba9dSStephen Wilson #include "ELFHeader.h" 17f325ba9dSStephen Wilson 18f325ba9dSStephen Wilson using namespace elf; 19f325ba9dSStephen Wilson using namespace lldb; 20f325ba9dSStephen Wilson using namespace llvm::ELF; 21f325ba9dSStephen Wilson 22f325ba9dSStephen Wilson //------------------------------------------------------------------------------ 23f325ba9dSStephen Wilson // Static utility functions. 24f325ba9dSStephen Wilson // 25f325ba9dSStephen Wilson // GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor 26f325ba9dSStephen Wilson // with error handling code and provide for parsing a sequence of values. 27b9c1b51eSKate Stone static bool GetMaxU64(const lldb_private::DataExtractor &data, 28b9c1b51eSKate Stone lldb::offset_t *offset, uint64_t *value, 29b9c1b51eSKate Stone uint32_t byte_size) { 30c7bece56SGreg Clayton const lldb::offset_t saved_offset = *offset; 31f325ba9dSStephen Wilson *value = data.GetMaxU64(offset, byte_size); 32f325ba9dSStephen Wilson return *offset != saved_offset; 33f325ba9dSStephen Wilson } 34f325ba9dSStephen Wilson 35b9c1b51eSKate Stone static bool GetMaxU64(const lldb_private::DataExtractor &data, 36b9c1b51eSKate Stone lldb::offset_t *offset, uint64_t *value, 37b9c1b51eSKate Stone uint32_t byte_size, uint32_t count) { 38c7bece56SGreg Clayton lldb::offset_t saved_offset = *offset; 39f325ba9dSStephen Wilson 40b9c1b51eSKate Stone for (uint32_t i = 0; i < count; ++i, ++value) { 41b9c1b51eSKate Stone if (GetMaxU64(data, offset, value, byte_size) == false) { 42f325ba9dSStephen Wilson *offset = saved_offset; 43f325ba9dSStephen Wilson return false; 44f325ba9dSStephen Wilson } 45f325ba9dSStephen Wilson } 46f325ba9dSStephen Wilson return true; 47f325ba9dSStephen Wilson } 48f325ba9dSStephen Wilson 49b9c1b51eSKate Stone static bool GetMaxS64(const lldb_private::DataExtractor &data, 50b9c1b51eSKate Stone lldb::offset_t *offset, int64_t *value, 51b9c1b51eSKate Stone uint32_t byte_size) { 52c7bece56SGreg Clayton const lldb::offset_t saved_offset = *offset; 53f325ba9dSStephen Wilson *value = data.GetMaxS64(offset, byte_size); 54f325ba9dSStephen Wilson return *offset != saved_offset; 55f325ba9dSStephen Wilson } 56f325ba9dSStephen Wilson 57b9c1b51eSKate Stone static bool GetMaxS64(const lldb_private::DataExtractor &data, 58b9c1b51eSKate Stone lldb::offset_t *offset, int64_t *value, 59b9c1b51eSKate Stone uint32_t byte_size, uint32_t count) { 60c7bece56SGreg Clayton lldb::offset_t saved_offset = *offset; 61f325ba9dSStephen Wilson 62b9c1b51eSKate Stone for (uint32_t i = 0; i < count; ++i, ++value) { 63b9c1b51eSKate Stone if (GetMaxS64(data, offset, value, byte_size) == false) { 64f325ba9dSStephen Wilson *offset = saved_offset; 65f325ba9dSStephen Wilson return false; 66f325ba9dSStephen Wilson } 67f325ba9dSStephen Wilson } 68f325ba9dSStephen Wilson return true; 69f325ba9dSStephen Wilson } 70f325ba9dSStephen Wilson 71f325ba9dSStephen Wilson //------------------------------------------------------------------------------ 72f325ba9dSStephen Wilson // ELFHeader 73f325ba9dSStephen Wilson 74b9c1b51eSKate Stone ELFHeader::ELFHeader() { memset(this, 0, sizeof(ELFHeader)); } 75f325ba9dSStephen Wilson 76b9c1b51eSKate Stone ByteOrder ELFHeader::GetByteOrder() const { 77f325ba9dSStephen Wilson if (e_ident[EI_DATA] == ELFDATA2MSB) 78f325ba9dSStephen Wilson return eByteOrderBig; 79f325ba9dSStephen Wilson if (e_ident[EI_DATA] == ELFDATA2LSB) 80f325ba9dSStephen Wilson return eByteOrderLittle; 81f325ba9dSStephen Wilson return eByteOrderInvalid; 82f325ba9dSStephen Wilson } 83f325ba9dSStephen Wilson 8423ccc291SPavel Labath bool ELFHeader::HasHeaderExtension() const { 8523ccc291SPavel Labath bool result = false; 8623ccc291SPavel Labath 8723ccc291SPavel Labath // Check if any of these values looks like sentinel. 8823ccc291SPavel Labath result |= e_phnum_hdr == 0xFFFF; // PN_XNUM 8923ccc291SPavel Labath result |= e_shnum_hdr == SHN_UNDEF; 9023ccc291SPavel Labath result |= e_shstrndx_hdr == SHN_XINDEX; 9123ccc291SPavel Labath 9223ccc291SPavel Labath // If header extension is present, the section offset cannot be null. 9323ccc291SPavel Labath result &= e_shoff != 0; 9423ccc291SPavel Labath 9523ccc291SPavel Labath // Done. 9623ccc291SPavel Labath return result; 9723ccc291SPavel Labath } 9823ccc291SPavel Labath 9923ccc291SPavel Labath void ELFHeader::ParseHeaderExtension(lldb_private::DataExtractor &data) { 10023ccc291SPavel Labath // Extract section #0 header. 10123ccc291SPavel Labath ELFSectionHeader section_zero; 10223ccc291SPavel Labath lldb::offset_t offset = 0; 10323ccc291SPavel Labath lldb_private::DataExtractor sh_data(data, e_shoff, e_shentsize); 10423ccc291SPavel Labath bool ok = section_zero.Parse(sh_data, &offset); 10523ccc291SPavel Labath 10623ccc291SPavel Labath // If we succeeded, fix the header. 10723ccc291SPavel Labath if (ok) { 10823ccc291SPavel Labath if (e_phnum_hdr == 0xFFFF) // PN_XNUM 10923ccc291SPavel Labath e_phnum = section_zero.sh_info; 11023ccc291SPavel Labath if (e_shnum_hdr == SHN_UNDEF) 11123ccc291SPavel Labath e_shnum = section_zero.sh_size; 11223ccc291SPavel Labath if (e_shstrndx_hdr == SHN_XINDEX) 11323ccc291SPavel Labath e_shstrndx = section_zero.sh_link; 11423ccc291SPavel Labath } 11523ccc291SPavel Labath } 11623ccc291SPavel Labath 117b9c1b51eSKate Stone bool ELFHeader::Parse(lldb_private::DataExtractor &data, 118b9c1b51eSKate Stone lldb::offset_t *offset) { 119f325ba9dSStephen Wilson // Read e_ident. This provides byte order and address size info. 120f325ba9dSStephen Wilson if (data.GetU8(offset, &e_ident, EI_NIDENT) == NULL) 121f325ba9dSStephen Wilson return false; 122f325ba9dSStephen Wilson 123f325ba9dSStephen Wilson const unsigned byte_size = Is32Bit() ? 4 : 8; 124f325ba9dSStephen Wilson data.SetByteOrder(GetByteOrder()); 125f325ba9dSStephen Wilson data.SetAddressByteSize(byte_size); 126f325ba9dSStephen Wilson 127f325ba9dSStephen Wilson // Read e_type and e_machine. 128f325ba9dSStephen Wilson if (data.GetU16(offset, &e_type, 2) == NULL) 129f325ba9dSStephen Wilson return false; 130f325ba9dSStephen Wilson 131f325ba9dSStephen Wilson // Read e_version. 132f325ba9dSStephen Wilson if (data.GetU32(offset, &e_version, 1) == NULL) 133f325ba9dSStephen Wilson return false; 134f325ba9dSStephen Wilson 135f325ba9dSStephen Wilson // Read e_entry, e_phoff and e_shoff. 136f325ba9dSStephen Wilson if (GetMaxU64(data, offset, &e_entry, byte_size, 3) == false) 137f325ba9dSStephen Wilson return false; 138f325ba9dSStephen Wilson 139f325ba9dSStephen Wilson // Read e_flags. 140f325ba9dSStephen Wilson if (data.GetU32(offset, &e_flags, 1) == NULL) 141f325ba9dSStephen Wilson return false; 142f325ba9dSStephen Wilson 143f325ba9dSStephen Wilson // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and 144f325ba9dSStephen Wilson // e_shstrndx. 145f325ba9dSStephen Wilson if (data.GetU16(offset, &e_ehsize, 6) == NULL) 146f325ba9dSStephen Wilson return false; 147f325ba9dSStephen Wilson 14823ccc291SPavel Labath // Initialize e_phnum, e_shnum, and e_shstrndx with the values 14923ccc291SPavel Labath // read from the header. 15023ccc291SPavel Labath e_phnum = e_phnum_hdr; 15123ccc291SPavel Labath e_shnum = e_shnum_hdr; 15223ccc291SPavel Labath e_shstrndx = e_shstrndx_hdr; 15323ccc291SPavel Labath 15423ccc291SPavel Labath // See if we have extended header in section #0. 15523ccc291SPavel Labath if (HasHeaderExtension()) 15623ccc291SPavel Labath ParseHeaderExtension(data); 15723ccc291SPavel Labath 158f325ba9dSStephen Wilson return true; 159f325ba9dSStephen Wilson } 160f325ba9dSStephen Wilson 161b9c1b51eSKate Stone bool ELFHeader::MagicBytesMatch(const uint8_t *magic) { 162f325ba9dSStephen Wilson return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0; 163f325ba9dSStephen Wilson } 164f325ba9dSStephen Wilson 165b9c1b51eSKate Stone unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) { 166f325ba9dSStephen Wilson unsigned address_size = 0; 167f325ba9dSStephen Wilson 168b9c1b51eSKate Stone switch (magic[EI_CLASS]) { 169f325ba9dSStephen Wilson case ELFCLASS32: 170f325ba9dSStephen Wilson address_size = 4; 171f325ba9dSStephen Wilson break; 172f325ba9dSStephen Wilson 173f325ba9dSStephen Wilson case ELFCLASS64: 174f325ba9dSStephen Wilson address_size = 8; 175f325ba9dSStephen Wilson break; 176f325ba9dSStephen Wilson } 177f325ba9dSStephen Wilson return address_size; 178f325ba9dSStephen Wilson } 179f325ba9dSStephen Wilson 180b9c1b51eSKate Stone unsigned ELFHeader::GetRelocationJumpSlotType() const { 18143fe645bSStephen Wilson unsigned slot = 0; 18243fe645bSStephen Wilson 183b9c1b51eSKate Stone switch (e_machine) { 18443fe645bSStephen Wilson default: 18543fe645bSStephen Wilson assert(false && "architecture not supported"); 18643fe645bSStephen Wilson break; 1876256a0eaSJustin Hibbits case EM_PPC: 1886256a0eaSJustin Hibbits slot = R_PPC_JMP_SLOT; 1896256a0eaSJustin Hibbits break; 1906256a0eaSJustin Hibbits case EM_PPC64: 1916256a0eaSJustin Hibbits slot = R_PPC64_JMP_SLOT; 1926256a0eaSJustin Hibbits break; 19343fe645bSStephen Wilson case EM_386: 19486f422e1SRafael Espindola case EM_IAMCU: // FIXME: is this correct? 19543fe645bSStephen Wilson slot = R_386_JUMP_SLOT; 19643fe645bSStephen Wilson break; 19743fe645bSStephen Wilson case EM_X86_64: 19843fe645bSStephen Wilson slot = R_X86_64_JUMP_SLOT; 19943fe645bSStephen Wilson break; 20043fe645bSStephen Wilson case EM_ARM: 20143fe645bSStephen Wilson slot = R_ARM_JUMP_SLOT; 20243fe645bSStephen Wilson break; 203ca238a7bSDeepak Panickal case EM_HEXAGON: 204ca238a7bSDeepak Panickal slot = R_HEX_JMP_SLOT; 205ca238a7bSDeepak Panickal break; 20633bba9f4STodd Fiala case EM_AARCH64: 20733bba9f4STodd Fiala slot = R_AARCH64_JUMP_SLOT; 20833bba9f4STodd Fiala break; 209c9335a3fSMohit K. Bhakkad case EM_MIPS: 210c9335a3fSMohit K. Bhakkad slot = R_MIPS_JUMP_SLOT; 211c9335a3fSMohit K. Bhakkad break; 212bb00d0b6SUlrich Weigand case EM_S390: 213bb00d0b6SUlrich Weigand slot = R_390_JMP_SLOT; 214bb00d0b6SUlrich Weigand break; 21543fe645bSStephen Wilson } 21643fe645bSStephen Wilson 21743fe645bSStephen Wilson return slot; 21843fe645bSStephen Wilson } 21943fe645bSStephen Wilson 220f325ba9dSStephen Wilson //------------------------------------------------------------------------------ 221f325ba9dSStephen Wilson // ELFSectionHeader 222f325ba9dSStephen Wilson 223b9c1b51eSKate Stone ELFSectionHeader::ELFSectionHeader() { 224f325ba9dSStephen Wilson memset(this, 0, sizeof(ELFSectionHeader)); 225f325ba9dSStephen Wilson } 226f325ba9dSStephen Wilson 227b9c1b51eSKate Stone bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data, 228b9c1b51eSKate Stone lldb::offset_t *offset) { 229f325ba9dSStephen Wilson const unsigned byte_size = data.GetAddressByteSize(); 230f325ba9dSStephen Wilson 231f325ba9dSStephen Wilson // Read sh_name and sh_type. 232f325ba9dSStephen Wilson if (data.GetU32(offset, &sh_name, 2) == NULL) 233f325ba9dSStephen Wilson return false; 234f325ba9dSStephen Wilson 235f325ba9dSStephen Wilson // Read sh_flags. 236f325ba9dSStephen Wilson if (GetMaxU64(data, offset, &sh_flags, byte_size) == false) 237f325ba9dSStephen Wilson return false; 238f325ba9dSStephen Wilson 239f325ba9dSStephen Wilson // Read sh_addr, sh_off and sh_size. 240f325ba9dSStephen Wilson if (GetMaxU64(data, offset, &sh_addr, byte_size, 3) == false) 241f325ba9dSStephen Wilson return false; 242f325ba9dSStephen Wilson 243f325ba9dSStephen Wilson // Read sh_link and sh_info. 244f325ba9dSStephen Wilson if (data.GetU32(offset, &sh_link, 2) == NULL) 245f325ba9dSStephen Wilson return false; 246f325ba9dSStephen Wilson 247f325ba9dSStephen Wilson // Read sh_addralign and sh_entsize. 248f325ba9dSStephen Wilson if (GetMaxU64(data, offset, &sh_addralign, byte_size, 2) == false) 249f325ba9dSStephen Wilson return false; 250f325ba9dSStephen Wilson 251f325ba9dSStephen Wilson return true; 252f325ba9dSStephen Wilson } 253f325ba9dSStephen Wilson 254f325ba9dSStephen Wilson //------------------------------------------------------------------------------ 255f325ba9dSStephen Wilson // ELFSymbol 256f325ba9dSStephen Wilson 257b9c1b51eSKate Stone ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); } 258f325ba9dSStephen Wilson 259b9c1b51eSKate Stone #define ENUM_TO_CSTR(e) \ 260b9c1b51eSKate Stone case e: \ 261b9c1b51eSKate Stone return #e 2629594f4c8SGreg Clayton 263b9c1b51eSKate Stone const char *ELFSymbol::bindingToCString(unsigned char binding) { 264b9c1b51eSKate Stone switch (binding) { 2659594f4c8SGreg Clayton ENUM_TO_CSTR(STB_LOCAL); 2669594f4c8SGreg Clayton ENUM_TO_CSTR(STB_GLOBAL); 2679594f4c8SGreg Clayton ENUM_TO_CSTR(STB_WEAK); 2689594f4c8SGreg Clayton ENUM_TO_CSTR(STB_LOOS); 2699594f4c8SGreg Clayton ENUM_TO_CSTR(STB_HIOS); 2709594f4c8SGreg Clayton ENUM_TO_CSTR(STB_LOPROC); 2719594f4c8SGreg Clayton ENUM_TO_CSTR(STB_HIPROC); 2729594f4c8SGreg Clayton } 2739594f4c8SGreg Clayton return ""; 2749594f4c8SGreg Clayton } 2759594f4c8SGreg Clayton 276b9c1b51eSKate Stone const char *ELFSymbol::typeToCString(unsigned char type) { 277b9c1b51eSKate Stone switch (type) { 2789594f4c8SGreg Clayton ENUM_TO_CSTR(STT_NOTYPE); 2799594f4c8SGreg Clayton ENUM_TO_CSTR(STT_OBJECT); 2809594f4c8SGreg Clayton ENUM_TO_CSTR(STT_FUNC); 2819594f4c8SGreg Clayton ENUM_TO_CSTR(STT_SECTION); 2829594f4c8SGreg Clayton ENUM_TO_CSTR(STT_FILE); 2839594f4c8SGreg Clayton ENUM_TO_CSTR(STT_COMMON); 2849594f4c8SGreg Clayton ENUM_TO_CSTR(STT_TLS); 2859594f4c8SGreg Clayton ENUM_TO_CSTR(STT_GNU_IFUNC); 286a6d0dd74SPavel Labath ENUM_TO_CSTR(STT_HIOS); 2879594f4c8SGreg Clayton ENUM_TO_CSTR(STT_LOPROC); 2889594f4c8SGreg Clayton ENUM_TO_CSTR(STT_HIPROC); 2899594f4c8SGreg Clayton } 2909594f4c8SGreg Clayton return ""; 2919594f4c8SGreg Clayton } 2929594f4c8SGreg Clayton 293b9c1b51eSKate Stone const char *ELFSymbol::sectionIndexToCString( 294b9c1b51eSKate Stone elf_half shndx, const lldb_private::SectionList *section_list) { 295b9c1b51eSKate Stone switch (shndx) { 2969594f4c8SGreg Clayton ENUM_TO_CSTR(SHN_UNDEF); 2979594f4c8SGreg Clayton ENUM_TO_CSTR(SHN_LOPROC); 2989594f4c8SGreg Clayton ENUM_TO_CSTR(SHN_HIPROC); 2999594f4c8SGreg Clayton ENUM_TO_CSTR(SHN_LOOS); 3009594f4c8SGreg Clayton ENUM_TO_CSTR(SHN_HIOS); 3019594f4c8SGreg Clayton ENUM_TO_CSTR(SHN_ABS); 3029594f4c8SGreg Clayton ENUM_TO_CSTR(SHN_COMMON); 3039594f4c8SGreg Clayton ENUM_TO_CSTR(SHN_XINDEX); 304b9c1b51eSKate Stone default: { 305b9c1b51eSKate Stone const lldb_private::Section *section = 306b9c1b51eSKate Stone section_list->GetSectionAtIndex(shndx).get(); 3079594f4c8SGreg Clayton if (section) 3089594f4c8SGreg Clayton return section->GetName().AsCString(""); 309b9c1b51eSKate Stone } break; 3109594f4c8SGreg Clayton } 3119594f4c8SGreg Clayton return ""; 3129594f4c8SGreg Clayton } 3139594f4c8SGreg Clayton 314b9c1b51eSKate Stone void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx, 3159594f4c8SGreg Clayton const lldb_private::DataExtractor *strtab_data, 316b9c1b51eSKate Stone const lldb_private::SectionList *section_list) { 317b9c1b51eSKate Stone s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64 318b9c1b51eSKate Stone " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n", 319b9c1b51eSKate Stone idx, st_value, st_size, st_name, st_info, 320b9c1b51eSKate Stone bindingToCString(getBinding()), typeToCString(getType()), st_other, 321b9c1b51eSKate Stone st_shndx, sectionIndexToCString(st_shndx, section_list), 3229594f4c8SGreg Clayton strtab_data ? strtab_data->PeekCStr(st_name) : ""); 3239594f4c8SGreg Clayton } 3249594f4c8SGreg Clayton 325b9c1b51eSKate Stone bool ELFSymbol::Parse(const lldb_private::DataExtractor &data, 326b9c1b51eSKate Stone lldb::offset_t *offset) { 327f325ba9dSStephen Wilson const unsigned byte_size = data.GetAddressByteSize(); 328f325ba9dSStephen Wilson const bool parsing_32 = byte_size == 4; 329f325ba9dSStephen Wilson 330f325ba9dSStephen Wilson // Read st_name. 331f325ba9dSStephen Wilson if (data.GetU32(offset, &st_name, 1) == NULL) 332f325ba9dSStephen Wilson return false; 333f325ba9dSStephen Wilson 334b9c1b51eSKate Stone if (parsing_32) { 335f325ba9dSStephen Wilson // Read st_value and st_size. 336f325ba9dSStephen Wilson if (GetMaxU64(data, offset, &st_value, byte_size, 2) == false) 337f325ba9dSStephen Wilson return false; 338f325ba9dSStephen Wilson 339f325ba9dSStephen Wilson // Read st_info and st_other. 340f325ba9dSStephen Wilson if (data.GetU8(offset, &st_info, 2) == NULL) 341f325ba9dSStephen Wilson return false; 342f325ba9dSStephen Wilson 343f325ba9dSStephen Wilson // Read st_shndx. 344f325ba9dSStephen Wilson if (data.GetU16(offset, &st_shndx, 1) == NULL) 345f325ba9dSStephen Wilson return false; 346b9c1b51eSKate Stone } else { 347f325ba9dSStephen Wilson // Read st_info and st_other. 348f325ba9dSStephen Wilson if (data.GetU8(offset, &st_info, 2) == NULL) 349f325ba9dSStephen Wilson return false; 350f325ba9dSStephen Wilson 351f325ba9dSStephen Wilson // Read st_shndx. 352f325ba9dSStephen Wilson if (data.GetU16(offset, &st_shndx, 1) == NULL) 353f325ba9dSStephen Wilson return false; 354f325ba9dSStephen Wilson 355f325ba9dSStephen Wilson // Read st_value and st_size. 356f325ba9dSStephen Wilson if (data.GetU64(offset, &st_value, 2) == NULL) 357f325ba9dSStephen Wilson return false; 358f325ba9dSStephen Wilson } 359f325ba9dSStephen Wilson return true; 360f325ba9dSStephen Wilson } 361f325ba9dSStephen Wilson 362f325ba9dSStephen Wilson //------------------------------------------------------------------------------ 363f325ba9dSStephen Wilson // ELFProgramHeader 364f325ba9dSStephen Wilson 365b9c1b51eSKate Stone ELFProgramHeader::ELFProgramHeader() { 366f325ba9dSStephen Wilson memset(this, 0, sizeof(ELFProgramHeader)); 367f325ba9dSStephen Wilson } 368f325ba9dSStephen Wilson 369b9c1b51eSKate Stone bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data, 370b9c1b51eSKate Stone lldb::offset_t *offset) { 371f325ba9dSStephen Wilson const uint32_t byte_size = data.GetAddressByteSize(); 372f325ba9dSStephen Wilson const bool parsing_32 = byte_size == 4; 373f325ba9dSStephen Wilson 374f325ba9dSStephen Wilson // Read p_type; 375f325ba9dSStephen Wilson if (data.GetU32(offset, &p_type, 1) == NULL) 376f325ba9dSStephen Wilson return false; 377f325ba9dSStephen Wilson 378f325ba9dSStephen Wilson if (parsing_32) { 379f325ba9dSStephen Wilson // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz. 380f325ba9dSStephen Wilson if (GetMaxU64(data, offset, &p_offset, byte_size, 5) == false) 381f325ba9dSStephen Wilson return false; 382f325ba9dSStephen Wilson 383f325ba9dSStephen Wilson // Read p_flags. 384f325ba9dSStephen Wilson if (data.GetU32(offset, &p_flags, 1) == NULL) 385f325ba9dSStephen Wilson return false; 386f325ba9dSStephen Wilson 387f325ba9dSStephen Wilson // Read p_align. 388f325ba9dSStephen Wilson if (GetMaxU64(data, offset, &p_align, byte_size) == false) 389f325ba9dSStephen Wilson return false; 390b9c1b51eSKate Stone } else { 391f325ba9dSStephen Wilson // Read p_flags. 392f325ba9dSStephen Wilson if (data.GetU32(offset, &p_flags, 1) == NULL) 393f325ba9dSStephen Wilson return false; 394f325ba9dSStephen Wilson 395f325ba9dSStephen Wilson // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align. 396f325ba9dSStephen Wilson if (GetMaxU64(data, offset, &p_offset, byte_size, 6) == false) 397f325ba9dSStephen Wilson return false; 398f325ba9dSStephen Wilson } 399f325ba9dSStephen Wilson 400f325ba9dSStephen Wilson return true; 401f325ba9dSStephen Wilson } 402f325ba9dSStephen Wilson 403f325ba9dSStephen Wilson //------------------------------------------------------------------------------ 404f325ba9dSStephen Wilson // ELFDynamic 405f325ba9dSStephen Wilson 406b9c1b51eSKate Stone ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); } 407f325ba9dSStephen Wilson 408b9c1b51eSKate Stone bool ELFDynamic::Parse(const lldb_private::DataExtractor &data, 409b9c1b51eSKate Stone lldb::offset_t *offset) { 410f325ba9dSStephen Wilson const unsigned byte_size = data.GetAddressByteSize(); 411f325ba9dSStephen Wilson return GetMaxS64(data, offset, &d_tag, byte_size, 2); 412f325ba9dSStephen Wilson } 413f325ba9dSStephen Wilson 41443fe645bSStephen Wilson //------------------------------------------------------------------------------ 41543fe645bSStephen Wilson // ELFRel 41643fe645bSStephen Wilson 417b9c1b51eSKate Stone ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); } 41843fe645bSStephen Wilson 419b9c1b51eSKate Stone bool ELFRel::Parse(const lldb_private::DataExtractor &data, 420b9c1b51eSKate Stone lldb::offset_t *offset) { 42143fe645bSStephen Wilson const unsigned byte_size = data.GetAddressByteSize(); 42243fe645bSStephen Wilson 42343fe645bSStephen Wilson // Read r_offset and r_info. 42443fe645bSStephen Wilson if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false) 42543fe645bSStephen Wilson return false; 42643fe645bSStephen Wilson 42743fe645bSStephen Wilson return true; 42843fe645bSStephen Wilson } 42943fe645bSStephen Wilson 43043fe645bSStephen Wilson //------------------------------------------------------------------------------ 43143fe645bSStephen Wilson // ELFRela 43243fe645bSStephen Wilson 433b9c1b51eSKate Stone ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); } 43443fe645bSStephen Wilson 435b9c1b51eSKate Stone bool ELFRela::Parse(const lldb_private::DataExtractor &data, 436b9c1b51eSKate Stone lldb::offset_t *offset) { 43743fe645bSStephen Wilson const unsigned byte_size = data.GetAddressByteSize(); 43843fe645bSStephen Wilson 43943fe645bSStephen Wilson // Read r_offset and r_info. 44043fe645bSStephen Wilson if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false) 44143fe645bSStephen Wilson return false; 44243fe645bSStephen Wilson 44343fe645bSStephen Wilson // Read r_addend; 44443fe645bSStephen Wilson if (GetMaxS64(data, offset, &r_addend, byte_size) == false) 44543fe645bSStephen Wilson return false; 44643fe645bSStephen Wilson 44743fe645bSStephen Wilson return true; 44843fe645bSStephen Wilson } 449