180814287SRaphael Isemann //===-- ELFHeader.cpp -----------------------------------------------------===//
2f325ba9dSStephen Wilson //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f325ba9dSStephen Wilson //
7f325ba9dSStephen Wilson //===----------------------------------------------------------------------===//
8f325ba9dSStephen Wilson 
9f325ba9dSStephen Wilson #include <cstring>
10f325ba9dSStephen Wilson 
119594f4c8SGreg Clayton #include "lldb/Core/Section.h"
12666cc0b2SZachary Turner #include "lldb/Utility/DataExtractor.h"
13bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
14f325ba9dSStephen Wilson 
15f325ba9dSStephen Wilson #include "ELFHeader.h"
16f325ba9dSStephen Wilson 
17f325ba9dSStephen Wilson using namespace elf;
18f325ba9dSStephen Wilson using namespace lldb;
19f325ba9dSStephen Wilson using namespace llvm::ELF;
20f325ba9dSStephen Wilson 
21f325ba9dSStephen Wilson // Static utility functions.
22f325ba9dSStephen Wilson //
23f325ba9dSStephen Wilson // GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor
24f325ba9dSStephen Wilson // with error handling code and provide for parsing a sequence of values.
GetMaxU64(const lldb_private::DataExtractor & data,lldb::offset_t * offset,uint64_t * value,uint32_t byte_size)25b9c1b51eSKate Stone static bool GetMaxU64(const lldb_private::DataExtractor &data,
26b9c1b51eSKate Stone                       lldb::offset_t *offset, uint64_t *value,
27b9c1b51eSKate Stone                       uint32_t byte_size) {
28c7bece56SGreg Clayton   const lldb::offset_t saved_offset = *offset;
29f325ba9dSStephen Wilson   *value = data.GetMaxU64(offset, byte_size);
30f325ba9dSStephen Wilson   return *offset != saved_offset;
31f325ba9dSStephen Wilson }
32f325ba9dSStephen Wilson 
GetMaxU64(const lldb_private::DataExtractor & data,lldb::offset_t * offset,uint64_t * value,uint32_t byte_size,uint32_t count)33b9c1b51eSKate Stone static bool GetMaxU64(const lldb_private::DataExtractor &data,
34b9c1b51eSKate Stone                       lldb::offset_t *offset, uint64_t *value,
35b9c1b51eSKate Stone                       uint32_t byte_size, uint32_t count) {
36c7bece56SGreg Clayton   lldb::offset_t saved_offset = *offset;
37f325ba9dSStephen Wilson 
38b9c1b51eSKate Stone   for (uint32_t i = 0; i < count; ++i, ++value) {
39a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, value, byte_size)) {
40f325ba9dSStephen Wilson       *offset = saved_offset;
41f325ba9dSStephen Wilson       return false;
42f325ba9dSStephen Wilson     }
43f325ba9dSStephen Wilson   }
44f325ba9dSStephen Wilson   return true;
45f325ba9dSStephen Wilson }
46f325ba9dSStephen Wilson 
GetMaxS64(const lldb_private::DataExtractor & data,lldb::offset_t * offset,int64_t * value,uint32_t byte_size)47b9c1b51eSKate Stone static bool GetMaxS64(const lldb_private::DataExtractor &data,
48b9c1b51eSKate Stone                       lldb::offset_t *offset, int64_t *value,
49b9c1b51eSKate Stone                       uint32_t byte_size) {
50c7bece56SGreg Clayton   const lldb::offset_t saved_offset = *offset;
51f325ba9dSStephen Wilson   *value = data.GetMaxS64(offset, byte_size);
52f325ba9dSStephen Wilson   return *offset != saved_offset;
53f325ba9dSStephen Wilson }
54f325ba9dSStephen Wilson 
GetMaxS64(const lldb_private::DataExtractor & data,lldb::offset_t * offset,int64_t * value,uint32_t byte_size,uint32_t count)55b9c1b51eSKate Stone static bool GetMaxS64(const lldb_private::DataExtractor &data,
56b9c1b51eSKate Stone                       lldb::offset_t *offset, int64_t *value,
57b9c1b51eSKate Stone                       uint32_t byte_size, uint32_t count) {
58c7bece56SGreg Clayton   lldb::offset_t saved_offset = *offset;
59f325ba9dSStephen Wilson 
60b9c1b51eSKate Stone   for (uint32_t i = 0; i < count; ++i, ++value) {
61a6682a41SJonas Devlieghere     if (!GetMaxS64(data, offset, value, byte_size)) {
62f325ba9dSStephen Wilson       *offset = saved_offset;
63f325ba9dSStephen Wilson       return false;
64f325ba9dSStephen Wilson     }
65f325ba9dSStephen Wilson   }
66f325ba9dSStephen Wilson   return true;
67f325ba9dSStephen Wilson }
68f325ba9dSStephen Wilson 
69f325ba9dSStephen Wilson // ELFHeader
70f325ba9dSStephen Wilson 
ELFHeader()71b9c1b51eSKate Stone ELFHeader::ELFHeader() { memset(this, 0, sizeof(ELFHeader)); }
72f325ba9dSStephen Wilson 
GetByteOrder() const73b9c1b51eSKate Stone ByteOrder ELFHeader::GetByteOrder() const {
74f325ba9dSStephen Wilson   if (e_ident[EI_DATA] == ELFDATA2MSB)
75f325ba9dSStephen Wilson     return eByteOrderBig;
76f325ba9dSStephen Wilson   if (e_ident[EI_DATA] == ELFDATA2LSB)
77f325ba9dSStephen Wilson     return eByteOrderLittle;
78f325ba9dSStephen Wilson   return eByteOrderInvalid;
79f325ba9dSStephen Wilson }
80f325ba9dSStephen Wilson 
HasHeaderExtension() const8123ccc291SPavel Labath bool ELFHeader::HasHeaderExtension() const {
8223ccc291SPavel Labath   bool result = false;
8323ccc291SPavel Labath 
8423ccc291SPavel Labath   // Check if any of these values looks like sentinel.
8523ccc291SPavel Labath   result |= e_phnum_hdr == 0xFFFF; // PN_XNUM
8623ccc291SPavel Labath   result |= e_shnum_hdr == SHN_UNDEF;
8723ccc291SPavel Labath   result |= e_shstrndx_hdr == SHN_XINDEX;
8823ccc291SPavel Labath 
8923ccc291SPavel Labath   // If header extension is present, the section offset cannot be null.
9023ccc291SPavel Labath   result &= e_shoff != 0;
9123ccc291SPavel Labath 
9223ccc291SPavel Labath   // Done.
9323ccc291SPavel Labath   return result;
9423ccc291SPavel Labath }
9523ccc291SPavel Labath 
ParseHeaderExtension(lldb_private::DataExtractor & data)9623ccc291SPavel Labath void ELFHeader::ParseHeaderExtension(lldb_private::DataExtractor &data) {
9723ccc291SPavel Labath   // Extract section #0 header.
9823ccc291SPavel Labath   ELFSectionHeader section_zero;
9923ccc291SPavel Labath   lldb::offset_t offset = 0;
10023ccc291SPavel Labath   lldb_private::DataExtractor sh_data(data, e_shoff, e_shentsize);
10123ccc291SPavel Labath   bool ok = section_zero.Parse(sh_data, &offset);
10223ccc291SPavel Labath 
10323ccc291SPavel Labath   // If we succeeded, fix the header.
10423ccc291SPavel Labath   if (ok) {
10523ccc291SPavel Labath     if (e_phnum_hdr == 0xFFFF) // PN_XNUM
10623ccc291SPavel Labath       e_phnum = section_zero.sh_info;
10723ccc291SPavel Labath     if (e_shnum_hdr == SHN_UNDEF)
10823ccc291SPavel Labath       e_shnum = section_zero.sh_size;
10923ccc291SPavel Labath     if (e_shstrndx_hdr == SHN_XINDEX)
11023ccc291SPavel Labath       e_shstrndx = section_zero.sh_link;
11123ccc291SPavel Labath   }
11223ccc291SPavel Labath }
11323ccc291SPavel Labath 
Parse(lldb_private::DataExtractor & data,lldb::offset_t * offset)114b9c1b51eSKate Stone bool ELFHeader::Parse(lldb_private::DataExtractor &data,
115b9c1b51eSKate Stone                       lldb::offset_t *offset) {
116f325ba9dSStephen Wilson   // Read e_ident.  This provides byte order and address size info.
117248a1305SKonrad Kleine   if (data.GetU8(offset, &e_ident, EI_NIDENT) == nullptr)
118f325ba9dSStephen Wilson     return false;
119f325ba9dSStephen Wilson 
120f325ba9dSStephen Wilson   const unsigned byte_size = Is32Bit() ? 4 : 8;
121f325ba9dSStephen Wilson   data.SetByteOrder(GetByteOrder());
122f325ba9dSStephen Wilson   data.SetAddressByteSize(byte_size);
123f325ba9dSStephen Wilson 
124f325ba9dSStephen Wilson   // Read e_type and e_machine.
125248a1305SKonrad Kleine   if (data.GetU16(offset, &e_type, 2) == nullptr)
126f325ba9dSStephen Wilson     return false;
127f325ba9dSStephen Wilson 
128f325ba9dSStephen Wilson   // Read e_version.
129248a1305SKonrad Kleine   if (data.GetU32(offset, &e_version, 1) == nullptr)
130f325ba9dSStephen Wilson     return false;
131f325ba9dSStephen Wilson 
132f325ba9dSStephen Wilson   // Read e_entry, e_phoff and e_shoff.
133a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &e_entry, byte_size, 3))
134f325ba9dSStephen Wilson     return false;
135f325ba9dSStephen Wilson 
136f325ba9dSStephen Wilson   // Read e_flags.
137248a1305SKonrad Kleine   if (data.GetU32(offset, &e_flags, 1) == nullptr)
138f325ba9dSStephen Wilson     return false;
139f325ba9dSStephen Wilson 
14005097246SAdrian Prantl   // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx.
141248a1305SKonrad Kleine   if (data.GetU16(offset, &e_ehsize, 6) == nullptr)
142f325ba9dSStephen Wilson     return false;
143f325ba9dSStephen Wilson 
14405097246SAdrian Prantl   // Initialize e_phnum, e_shnum, and e_shstrndx with the values read from the
14505097246SAdrian Prantl   // header.
14623ccc291SPavel Labath   e_phnum = e_phnum_hdr;
14723ccc291SPavel Labath   e_shnum = e_shnum_hdr;
14823ccc291SPavel Labath   e_shstrndx = e_shstrndx_hdr;
14923ccc291SPavel Labath 
15023ccc291SPavel Labath   // See if we have extended header in section #0.
15123ccc291SPavel Labath   if (HasHeaderExtension())
15223ccc291SPavel Labath     ParseHeaderExtension(data);
15323ccc291SPavel Labath 
154f325ba9dSStephen Wilson   return true;
155f325ba9dSStephen Wilson }
156f325ba9dSStephen Wilson 
MagicBytesMatch(const uint8_t * magic)157b9c1b51eSKate Stone bool ELFHeader::MagicBytesMatch(const uint8_t *magic) {
158f325ba9dSStephen Wilson   return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
159f325ba9dSStephen Wilson }
160f325ba9dSStephen Wilson 
AddressSizeInBytes(const uint8_t * magic)161b9c1b51eSKate Stone unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) {
162f325ba9dSStephen Wilson   unsigned address_size = 0;
163f325ba9dSStephen Wilson 
164b9c1b51eSKate Stone   switch (magic[EI_CLASS]) {
165f325ba9dSStephen Wilson   case ELFCLASS32:
166f325ba9dSStephen Wilson     address_size = 4;
167f325ba9dSStephen Wilson     break;
168f325ba9dSStephen Wilson 
169f325ba9dSStephen Wilson   case ELFCLASS64:
170f325ba9dSStephen Wilson     address_size = 8;
171f325ba9dSStephen Wilson     break;
172f325ba9dSStephen Wilson   }
173f325ba9dSStephen Wilson   return address_size;
174f325ba9dSStephen Wilson }
175f325ba9dSStephen Wilson 
GetRelocationJumpSlotType() const176b9c1b51eSKate Stone unsigned ELFHeader::GetRelocationJumpSlotType() const {
17743fe645bSStephen Wilson   unsigned slot = 0;
17843fe645bSStephen Wilson 
179b9c1b51eSKate Stone   switch (e_machine) {
18043fe645bSStephen Wilson   default:
18143fe645bSStephen Wilson     assert(false && "architecture not supported");
18243fe645bSStephen Wilson     break;
1836256a0eaSJustin Hibbits   case EM_PPC:
1846256a0eaSJustin Hibbits     slot = R_PPC_JMP_SLOT;
1856256a0eaSJustin Hibbits     break;
1866256a0eaSJustin Hibbits   case EM_PPC64:
1876256a0eaSJustin Hibbits     slot = R_PPC64_JMP_SLOT;
1886256a0eaSJustin Hibbits     break;
18943fe645bSStephen Wilson   case EM_386:
19086f422e1SRafael Espindola   case EM_IAMCU: // FIXME: is this correct?
19143fe645bSStephen Wilson     slot = R_386_JUMP_SLOT;
19243fe645bSStephen Wilson     break;
19343fe645bSStephen Wilson   case EM_X86_64:
19443fe645bSStephen Wilson     slot = R_X86_64_JUMP_SLOT;
19543fe645bSStephen Wilson     break;
19643fe645bSStephen Wilson   case EM_ARM:
19743fe645bSStephen Wilson     slot = R_ARM_JUMP_SLOT;
19843fe645bSStephen Wilson     break;
199ca238a7bSDeepak Panickal   case EM_HEXAGON:
200ca238a7bSDeepak Panickal     slot = R_HEX_JMP_SLOT;
201ca238a7bSDeepak Panickal     break;
20233bba9f4STodd Fiala   case EM_AARCH64:
20333bba9f4STodd Fiala     slot = R_AARCH64_JUMP_SLOT;
20433bba9f4STodd Fiala     break;
205c9335a3fSMohit K. Bhakkad   case EM_MIPS:
206c9335a3fSMohit K. Bhakkad     slot = R_MIPS_JUMP_SLOT;
207c9335a3fSMohit K. Bhakkad     break;
208bb00d0b6SUlrich Weigand   case EM_S390:
209bb00d0b6SUlrich Weigand     slot = R_390_JMP_SLOT;
210bb00d0b6SUlrich Weigand     break;
211*b5491dd3SSaleem Abdulrasool   case EM_RISCV:
212*b5491dd3SSaleem Abdulrasool     slot = R_RISCV_JUMP_SLOT;
213*b5491dd3SSaleem Abdulrasool     break;
21443fe645bSStephen Wilson   }
21543fe645bSStephen Wilson 
21643fe645bSStephen Wilson   return slot;
21743fe645bSStephen Wilson }
21843fe645bSStephen Wilson 
219f325ba9dSStephen Wilson // ELFSectionHeader
220f325ba9dSStephen Wilson 
ELFSectionHeader()221b9c1b51eSKate Stone ELFSectionHeader::ELFSectionHeader() {
222f325ba9dSStephen Wilson   memset(this, 0, sizeof(ELFSectionHeader));
223f325ba9dSStephen Wilson }
224f325ba9dSStephen Wilson 
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)225b9c1b51eSKate Stone bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
226b9c1b51eSKate Stone                              lldb::offset_t *offset) {
227f325ba9dSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
228f325ba9dSStephen Wilson 
229f325ba9dSStephen Wilson   // Read sh_name and sh_type.
230248a1305SKonrad Kleine   if (data.GetU32(offset, &sh_name, 2) == nullptr)
231f325ba9dSStephen Wilson     return false;
232f325ba9dSStephen Wilson 
233f325ba9dSStephen Wilson   // Read sh_flags.
234a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &sh_flags, byte_size))
235f325ba9dSStephen Wilson     return false;
236f325ba9dSStephen Wilson 
237f325ba9dSStephen Wilson   // Read sh_addr, sh_off and sh_size.
238a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &sh_addr, byte_size, 3))
239f325ba9dSStephen Wilson     return false;
240f325ba9dSStephen Wilson 
241f325ba9dSStephen Wilson   // Read sh_link and sh_info.
242248a1305SKonrad Kleine   if (data.GetU32(offset, &sh_link, 2) == nullptr)
243f325ba9dSStephen Wilson     return false;
244f325ba9dSStephen Wilson 
245f325ba9dSStephen Wilson   // Read sh_addralign and sh_entsize.
246a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &sh_addralign, byte_size, 2))
247f325ba9dSStephen Wilson     return false;
248f325ba9dSStephen Wilson 
249f325ba9dSStephen Wilson   return true;
250f325ba9dSStephen Wilson }
251f325ba9dSStephen Wilson 
252f325ba9dSStephen Wilson // ELFSymbol
253f325ba9dSStephen Wilson 
ELFSymbol()254b9c1b51eSKate Stone ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); }
255f325ba9dSStephen Wilson 
256b9c1b51eSKate Stone #define ENUM_TO_CSTR(e)                                                        \
257b9c1b51eSKate Stone   case e:                                                                      \
258b9c1b51eSKate Stone     return #e
2599594f4c8SGreg Clayton 
bindingToCString(unsigned char binding)260b9c1b51eSKate Stone const char *ELFSymbol::bindingToCString(unsigned char binding) {
261b9c1b51eSKate Stone   switch (binding) {
2629594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_LOCAL);
2639594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_GLOBAL);
2649594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_WEAK);
2659594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_LOOS);
2669594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_HIOS);
2679594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_LOPROC);
2689594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_HIPROC);
2699594f4c8SGreg Clayton   }
2709594f4c8SGreg Clayton   return "";
2719594f4c8SGreg Clayton }
2729594f4c8SGreg Clayton 
typeToCString(unsigned char type)273b9c1b51eSKate Stone const char *ELFSymbol::typeToCString(unsigned char type) {
274b9c1b51eSKate Stone   switch (type) {
2759594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_NOTYPE);
2769594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_OBJECT);
2779594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_FUNC);
2789594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_SECTION);
2799594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_FILE);
2809594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_COMMON);
2819594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_TLS);
2829594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_GNU_IFUNC);
283a6d0dd74SPavel Labath     ENUM_TO_CSTR(STT_HIOS);
2849594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_LOPROC);
2859594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_HIPROC);
2869594f4c8SGreg Clayton   }
2879594f4c8SGreg Clayton   return "";
2889594f4c8SGreg Clayton }
2899594f4c8SGreg Clayton 
sectionIndexToCString(elf_half shndx,const lldb_private::SectionList * section_list)290b9c1b51eSKate Stone const char *ELFSymbol::sectionIndexToCString(
291b9c1b51eSKate Stone     elf_half shndx, const lldb_private::SectionList *section_list) {
292b9c1b51eSKate Stone   switch (shndx) {
2939594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_UNDEF);
2949594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_LOPROC);
2959594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_HIPROC);
2969594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_LOOS);
2979594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_HIOS);
2989594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_ABS);
2999594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_COMMON);
3009594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_XINDEX);
301b9c1b51eSKate Stone   default: {
302b9c1b51eSKate Stone     const lldb_private::Section *section =
303b9c1b51eSKate Stone         section_list->GetSectionAtIndex(shndx).get();
3049594f4c8SGreg Clayton     if (section)
3059594f4c8SGreg Clayton       return section->GetName().AsCString("");
306b9c1b51eSKate Stone   } break;
3079594f4c8SGreg Clayton   }
3089594f4c8SGreg Clayton   return "";
3099594f4c8SGreg Clayton }
3109594f4c8SGreg Clayton 
Dump(lldb_private::Stream * s,uint32_t idx,const lldb_private::DataExtractor * strtab_data,const lldb_private::SectionList * section_list)311b9c1b51eSKate Stone void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx,
3129594f4c8SGreg Clayton                      const lldb_private::DataExtractor *strtab_data,
313b9c1b51eSKate Stone                      const lldb_private::SectionList *section_list) {
314b9c1b51eSKate Stone   s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64
315b9c1b51eSKate Stone             " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
316b9c1b51eSKate Stone             idx, st_value, st_size, st_name, st_info,
317b9c1b51eSKate Stone             bindingToCString(getBinding()), typeToCString(getType()), st_other,
318b9c1b51eSKate Stone             st_shndx, sectionIndexToCString(st_shndx, section_list),
3199594f4c8SGreg Clayton             strtab_data ? strtab_data->PeekCStr(st_name) : "");
3209594f4c8SGreg Clayton }
3219594f4c8SGreg Clayton 
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)322b9c1b51eSKate Stone bool ELFSymbol::Parse(const lldb_private::DataExtractor &data,
323b9c1b51eSKate Stone                       lldb::offset_t *offset) {
324f325ba9dSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
325f325ba9dSStephen Wilson   const bool parsing_32 = byte_size == 4;
326f325ba9dSStephen Wilson 
327f325ba9dSStephen Wilson   // Read st_name.
328248a1305SKonrad Kleine   if (data.GetU32(offset, &st_name, 1) == nullptr)
329f325ba9dSStephen Wilson     return false;
330f325ba9dSStephen Wilson 
331b9c1b51eSKate Stone   if (parsing_32) {
332f325ba9dSStephen Wilson     // Read st_value and st_size.
333a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &st_value, byte_size, 2))
334f325ba9dSStephen Wilson       return false;
335f325ba9dSStephen Wilson 
336f325ba9dSStephen Wilson     // Read st_info and st_other.
337248a1305SKonrad Kleine     if (data.GetU8(offset, &st_info, 2) == nullptr)
338f325ba9dSStephen Wilson       return false;
339f325ba9dSStephen Wilson 
340f325ba9dSStephen Wilson     // Read st_shndx.
341248a1305SKonrad Kleine     if (data.GetU16(offset, &st_shndx, 1) == nullptr)
342f325ba9dSStephen Wilson       return false;
343b9c1b51eSKate Stone   } else {
344f325ba9dSStephen Wilson     // Read st_info and st_other.
345248a1305SKonrad Kleine     if (data.GetU8(offset, &st_info, 2) == nullptr)
346f325ba9dSStephen Wilson       return false;
347f325ba9dSStephen Wilson 
348f325ba9dSStephen Wilson     // Read st_shndx.
349248a1305SKonrad Kleine     if (data.GetU16(offset, &st_shndx, 1) == nullptr)
350f325ba9dSStephen Wilson       return false;
351f325ba9dSStephen Wilson 
352f325ba9dSStephen Wilson     // Read st_value and st_size.
353248a1305SKonrad Kleine     if (data.GetU64(offset, &st_value, 2) == nullptr)
354f325ba9dSStephen Wilson       return false;
355f325ba9dSStephen Wilson   }
356f325ba9dSStephen Wilson   return true;
357f325ba9dSStephen Wilson }
358f325ba9dSStephen Wilson 
359f325ba9dSStephen Wilson // ELFProgramHeader
360f325ba9dSStephen Wilson 
ELFProgramHeader()361b9c1b51eSKate Stone ELFProgramHeader::ELFProgramHeader() {
362f325ba9dSStephen Wilson   memset(this, 0, sizeof(ELFProgramHeader));
363f325ba9dSStephen Wilson }
364f325ba9dSStephen Wilson 
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)365b9c1b51eSKate Stone bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
366b9c1b51eSKate Stone                              lldb::offset_t *offset) {
367f325ba9dSStephen Wilson   const uint32_t byte_size = data.GetAddressByteSize();
368f325ba9dSStephen Wilson   const bool parsing_32 = byte_size == 4;
369f325ba9dSStephen Wilson 
370f325ba9dSStephen Wilson   // Read p_type;
371248a1305SKonrad Kleine   if (data.GetU32(offset, &p_type, 1) == nullptr)
372f325ba9dSStephen Wilson     return false;
373f325ba9dSStephen Wilson 
374f325ba9dSStephen Wilson   if (parsing_32) {
375f325ba9dSStephen Wilson     // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
376a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &p_offset, byte_size, 5))
377f325ba9dSStephen Wilson       return false;
378f325ba9dSStephen Wilson 
379f325ba9dSStephen Wilson     // Read p_flags.
380248a1305SKonrad Kleine     if (data.GetU32(offset, &p_flags, 1) == nullptr)
381f325ba9dSStephen Wilson       return false;
382f325ba9dSStephen Wilson 
383f325ba9dSStephen Wilson     // Read p_align.
384a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &p_align, byte_size))
385f325ba9dSStephen Wilson       return false;
386b9c1b51eSKate Stone   } else {
387f325ba9dSStephen Wilson     // Read p_flags.
388248a1305SKonrad Kleine     if (data.GetU32(offset, &p_flags, 1) == nullptr)
389f325ba9dSStephen Wilson       return false;
390f325ba9dSStephen Wilson 
391f325ba9dSStephen Wilson     // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
392a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &p_offset, byte_size, 6))
393f325ba9dSStephen Wilson       return false;
394f325ba9dSStephen Wilson   }
395f325ba9dSStephen Wilson 
396f325ba9dSStephen Wilson   return true;
397f325ba9dSStephen Wilson }
398f325ba9dSStephen Wilson 
399f325ba9dSStephen Wilson // ELFDynamic
400f325ba9dSStephen Wilson 
ELFDynamic()401b9c1b51eSKate Stone ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); }
402f325ba9dSStephen Wilson 
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)403b9c1b51eSKate Stone bool ELFDynamic::Parse(const lldb_private::DataExtractor &data,
404b9c1b51eSKate Stone                        lldb::offset_t *offset) {
405f325ba9dSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
406f325ba9dSStephen Wilson   return GetMaxS64(data, offset, &d_tag, byte_size, 2);
407f325ba9dSStephen Wilson }
408f325ba9dSStephen Wilson 
40943fe645bSStephen Wilson // ELFRel
41043fe645bSStephen Wilson 
ELFRel()411b9c1b51eSKate Stone ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
41243fe645bSStephen Wilson 
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)413b9c1b51eSKate Stone bool ELFRel::Parse(const lldb_private::DataExtractor &data,
414b9c1b51eSKate Stone                    lldb::offset_t *offset) {
41543fe645bSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
41643fe645bSStephen Wilson 
41743fe645bSStephen Wilson   // Read r_offset and r_info.
418a6682a41SJonas Devlieghere   return GetMaxU64(data, offset, &r_offset, byte_size, 2) != false;
41943fe645bSStephen Wilson }
42043fe645bSStephen Wilson 
42143fe645bSStephen Wilson // ELFRela
42243fe645bSStephen Wilson 
ELFRela()423b9c1b51eSKate Stone ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
42443fe645bSStephen Wilson 
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)425b9c1b51eSKate Stone bool ELFRela::Parse(const lldb_private::DataExtractor &data,
426b9c1b51eSKate Stone                     lldb::offset_t *offset) {
42743fe645bSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
42843fe645bSStephen Wilson 
42943fe645bSStephen Wilson   // Read r_offset and r_info.
430a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &r_offset, byte_size, 2))
43143fe645bSStephen Wilson     return false;
43243fe645bSStephen Wilson 
43343fe645bSStephen Wilson   // Read r_addend;
434a6682a41SJonas Devlieghere   if (!GetMaxS64(data, offset, &r_addend, byte_size))
43543fe645bSStephen Wilson     return false;
43643fe645bSStephen Wilson 
43743fe645bSStephen Wilson   return true;
43843fe645bSStephen Wilson }
439