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"
13666cc0b2SZachary 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) {
41*a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, value, byte_size)) {
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) {
63*a6682a41SJonas Devlieghere     if (!GetMaxS64(data, offset, value, byte_size)) {
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.
136*a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &e_entry, byte_size, 3))
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 
14305097246SAdrian Prantl   // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx.
144f325ba9dSStephen Wilson   if (data.GetU16(offset, &e_ehsize, 6) == NULL)
145f325ba9dSStephen Wilson     return false;
146f325ba9dSStephen Wilson 
14705097246SAdrian Prantl   // Initialize e_phnum, e_shnum, and e_shstrndx with the values read from the
14805097246SAdrian Prantl   // header.
14923ccc291SPavel Labath   e_phnum = e_phnum_hdr;
15023ccc291SPavel Labath   e_shnum = e_shnum_hdr;
15123ccc291SPavel Labath   e_shstrndx = e_shstrndx_hdr;
15223ccc291SPavel Labath 
15323ccc291SPavel Labath   // See if we have extended header in section #0.
15423ccc291SPavel Labath   if (HasHeaderExtension())
15523ccc291SPavel Labath     ParseHeaderExtension(data);
15623ccc291SPavel Labath 
157f325ba9dSStephen Wilson   return true;
158f325ba9dSStephen Wilson }
159f325ba9dSStephen Wilson 
160b9c1b51eSKate Stone bool ELFHeader::MagicBytesMatch(const uint8_t *magic) {
161f325ba9dSStephen Wilson   return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
162f325ba9dSStephen Wilson }
163f325ba9dSStephen Wilson 
164b9c1b51eSKate Stone unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) {
165f325ba9dSStephen Wilson   unsigned address_size = 0;
166f325ba9dSStephen Wilson 
167b9c1b51eSKate Stone   switch (magic[EI_CLASS]) {
168f325ba9dSStephen Wilson   case ELFCLASS32:
169f325ba9dSStephen Wilson     address_size = 4;
170f325ba9dSStephen Wilson     break;
171f325ba9dSStephen Wilson 
172f325ba9dSStephen Wilson   case ELFCLASS64:
173f325ba9dSStephen Wilson     address_size = 8;
174f325ba9dSStephen Wilson     break;
175f325ba9dSStephen Wilson   }
176f325ba9dSStephen Wilson   return address_size;
177f325ba9dSStephen Wilson }
178f325ba9dSStephen Wilson 
179b9c1b51eSKate Stone unsigned ELFHeader::GetRelocationJumpSlotType() const {
18043fe645bSStephen Wilson   unsigned slot = 0;
18143fe645bSStephen Wilson 
182b9c1b51eSKate Stone   switch (e_machine) {
18343fe645bSStephen Wilson   default:
18443fe645bSStephen Wilson     assert(false && "architecture not supported");
18543fe645bSStephen Wilson     break;
1866256a0eaSJustin Hibbits   case EM_PPC:
1876256a0eaSJustin Hibbits     slot = R_PPC_JMP_SLOT;
1886256a0eaSJustin Hibbits     break;
1896256a0eaSJustin Hibbits   case EM_PPC64:
1906256a0eaSJustin Hibbits     slot = R_PPC64_JMP_SLOT;
1916256a0eaSJustin Hibbits     break;
19243fe645bSStephen Wilson   case EM_386:
19386f422e1SRafael Espindola   case EM_IAMCU: // FIXME: is this correct?
19443fe645bSStephen Wilson     slot = R_386_JUMP_SLOT;
19543fe645bSStephen Wilson     break;
19643fe645bSStephen Wilson   case EM_X86_64:
19743fe645bSStephen Wilson     slot = R_X86_64_JUMP_SLOT;
19843fe645bSStephen Wilson     break;
19943fe645bSStephen Wilson   case EM_ARM:
20043fe645bSStephen Wilson     slot = R_ARM_JUMP_SLOT;
20143fe645bSStephen Wilson     break;
202ca238a7bSDeepak Panickal   case EM_HEXAGON:
203ca238a7bSDeepak Panickal     slot = R_HEX_JMP_SLOT;
204ca238a7bSDeepak Panickal     break;
20533bba9f4STodd Fiala   case EM_AARCH64:
20633bba9f4STodd Fiala     slot = R_AARCH64_JUMP_SLOT;
20733bba9f4STodd Fiala     break;
208c9335a3fSMohit K. Bhakkad   case EM_MIPS:
209c9335a3fSMohit K. Bhakkad     slot = R_MIPS_JUMP_SLOT;
210c9335a3fSMohit K. Bhakkad     break;
211bb00d0b6SUlrich Weigand   case EM_S390:
212bb00d0b6SUlrich Weigand     slot = R_390_JMP_SLOT;
213bb00d0b6SUlrich Weigand     break;
21443fe645bSStephen Wilson   }
21543fe645bSStephen Wilson 
21643fe645bSStephen Wilson   return slot;
21743fe645bSStephen Wilson }
21843fe645bSStephen Wilson 
219f325ba9dSStephen Wilson //------------------------------------------------------------------------------
220f325ba9dSStephen Wilson // ELFSectionHeader
221f325ba9dSStephen Wilson 
222b9c1b51eSKate Stone ELFSectionHeader::ELFSectionHeader() {
223f325ba9dSStephen Wilson   memset(this, 0, sizeof(ELFSectionHeader));
224f325ba9dSStephen Wilson }
225f325ba9dSStephen Wilson 
226b9c1b51eSKate Stone bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
227b9c1b51eSKate Stone                              lldb::offset_t *offset) {
228f325ba9dSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
229f325ba9dSStephen Wilson 
230f325ba9dSStephen Wilson   // Read sh_name and sh_type.
231f325ba9dSStephen Wilson   if (data.GetU32(offset, &sh_name, 2) == NULL)
232f325ba9dSStephen Wilson     return false;
233f325ba9dSStephen Wilson 
234f325ba9dSStephen Wilson   // Read sh_flags.
235*a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &sh_flags, byte_size))
236f325ba9dSStephen Wilson     return false;
237f325ba9dSStephen Wilson 
238f325ba9dSStephen Wilson   // Read sh_addr, sh_off and sh_size.
239*a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &sh_addr, byte_size, 3))
240f325ba9dSStephen Wilson     return false;
241f325ba9dSStephen Wilson 
242f325ba9dSStephen Wilson   // Read sh_link and sh_info.
243f325ba9dSStephen Wilson   if (data.GetU32(offset, &sh_link, 2) == NULL)
244f325ba9dSStephen Wilson     return false;
245f325ba9dSStephen Wilson 
246f325ba9dSStephen Wilson   // Read sh_addralign and sh_entsize.
247*a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &sh_addralign, byte_size, 2))
248f325ba9dSStephen Wilson     return false;
249f325ba9dSStephen Wilson 
250f325ba9dSStephen Wilson   return true;
251f325ba9dSStephen Wilson }
252f325ba9dSStephen Wilson 
253f325ba9dSStephen Wilson //------------------------------------------------------------------------------
254f325ba9dSStephen Wilson // ELFSymbol
255f325ba9dSStephen Wilson 
256b9c1b51eSKate Stone ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); }
257f325ba9dSStephen Wilson 
258b9c1b51eSKate Stone #define ENUM_TO_CSTR(e)                                                        \
259b9c1b51eSKate Stone   case e:                                                                      \
260b9c1b51eSKate Stone     return #e
2619594f4c8SGreg Clayton 
262b9c1b51eSKate Stone const char *ELFSymbol::bindingToCString(unsigned char binding) {
263b9c1b51eSKate Stone   switch (binding) {
2649594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_LOCAL);
2659594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_GLOBAL);
2669594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_WEAK);
2679594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_LOOS);
2689594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_HIOS);
2699594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_LOPROC);
2709594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_HIPROC);
2719594f4c8SGreg Clayton   }
2729594f4c8SGreg Clayton   return "";
2739594f4c8SGreg Clayton }
2749594f4c8SGreg Clayton 
275b9c1b51eSKate Stone const char *ELFSymbol::typeToCString(unsigned char type) {
276b9c1b51eSKate Stone   switch (type) {
2779594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_NOTYPE);
2789594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_OBJECT);
2799594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_FUNC);
2809594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_SECTION);
2819594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_FILE);
2829594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_COMMON);
2839594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_TLS);
2849594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_GNU_IFUNC);
285a6d0dd74SPavel Labath     ENUM_TO_CSTR(STT_HIOS);
2869594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_LOPROC);
2879594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_HIPROC);
2889594f4c8SGreg Clayton   }
2899594f4c8SGreg Clayton   return "";
2909594f4c8SGreg Clayton }
2919594f4c8SGreg Clayton 
292b9c1b51eSKate Stone const char *ELFSymbol::sectionIndexToCString(
293b9c1b51eSKate Stone     elf_half shndx, const lldb_private::SectionList *section_list) {
294b9c1b51eSKate Stone   switch (shndx) {
2959594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_UNDEF);
2969594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_LOPROC);
2979594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_HIPROC);
2989594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_LOOS);
2999594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_HIOS);
3009594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_ABS);
3019594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_COMMON);
3029594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_XINDEX);
303b9c1b51eSKate Stone   default: {
304b9c1b51eSKate Stone     const lldb_private::Section *section =
305b9c1b51eSKate Stone         section_list->GetSectionAtIndex(shndx).get();
3069594f4c8SGreg Clayton     if (section)
3079594f4c8SGreg Clayton       return section->GetName().AsCString("");
308b9c1b51eSKate Stone   } break;
3099594f4c8SGreg Clayton   }
3109594f4c8SGreg Clayton   return "";
3119594f4c8SGreg Clayton }
3129594f4c8SGreg Clayton 
313b9c1b51eSKate Stone void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx,
3149594f4c8SGreg Clayton                      const lldb_private::DataExtractor *strtab_data,
315b9c1b51eSKate Stone                      const lldb_private::SectionList *section_list) {
316b9c1b51eSKate Stone   s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64
317b9c1b51eSKate Stone             " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
318b9c1b51eSKate Stone             idx, st_value, st_size, st_name, st_info,
319b9c1b51eSKate Stone             bindingToCString(getBinding()), typeToCString(getType()), st_other,
320b9c1b51eSKate Stone             st_shndx, sectionIndexToCString(st_shndx, section_list),
3219594f4c8SGreg Clayton             strtab_data ? strtab_data->PeekCStr(st_name) : "");
3229594f4c8SGreg Clayton }
3239594f4c8SGreg Clayton 
324b9c1b51eSKate Stone bool ELFSymbol::Parse(const lldb_private::DataExtractor &data,
325b9c1b51eSKate Stone                       lldb::offset_t *offset) {
326f325ba9dSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
327f325ba9dSStephen Wilson   const bool parsing_32 = byte_size == 4;
328f325ba9dSStephen Wilson 
329f325ba9dSStephen Wilson   // Read st_name.
330f325ba9dSStephen Wilson   if (data.GetU32(offset, &st_name, 1) == NULL)
331f325ba9dSStephen Wilson     return false;
332f325ba9dSStephen Wilson 
333b9c1b51eSKate Stone   if (parsing_32) {
334f325ba9dSStephen Wilson     // Read st_value and st_size.
335*a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &st_value, byte_size, 2))
336f325ba9dSStephen Wilson       return false;
337f325ba9dSStephen Wilson 
338f325ba9dSStephen Wilson     // Read st_info and st_other.
339f325ba9dSStephen Wilson     if (data.GetU8(offset, &st_info, 2) == NULL)
340f325ba9dSStephen Wilson       return false;
341f325ba9dSStephen Wilson 
342f325ba9dSStephen Wilson     // Read st_shndx.
343f325ba9dSStephen Wilson     if (data.GetU16(offset, &st_shndx, 1) == NULL)
344f325ba9dSStephen Wilson       return false;
345b9c1b51eSKate Stone   } else {
346f325ba9dSStephen Wilson     // Read st_info and st_other.
347f325ba9dSStephen Wilson     if (data.GetU8(offset, &st_info, 2) == NULL)
348f325ba9dSStephen Wilson       return false;
349f325ba9dSStephen Wilson 
350f325ba9dSStephen Wilson     // Read st_shndx.
351f325ba9dSStephen Wilson     if (data.GetU16(offset, &st_shndx, 1) == NULL)
352f325ba9dSStephen Wilson       return false;
353f325ba9dSStephen Wilson 
354f325ba9dSStephen Wilson     // Read st_value and st_size.
355f325ba9dSStephen Wilson     if (data.GetU64(offset, &st_value, 2) == NULL)
356f325ba9dSStephen Wilson       return false;
357f325ba9dSStephen Wilson   }
358f325ba9dSStephen Wilson   return true;
359f325ba9dSStephen Wilson }
360f325ba9dSStephen Wilson 
361f325ba9dSStephen Wilson //------------------------------------------------------------------------------
362f325ba9dSStephen Wilson // ELFProgramHeader
363f325ba9dSStephen Wilson 
364b9c1b51eSKate Stone ELFProgramHeader::ELFProgramHeader() {
365f325ba9dSStephen Wilson   memset(this, 0, sizeof(ELFProgramHeader));
366f325ba9dSStephen Wilson }
367f325ba9dSStephen Wilson 
368b9c1b51eSKate Stone bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
369b9c1b51eSKate Stone                              lldb::offset_t *offset) {
370f325ba9dSStephen Wilson   const uint32_t byte_size = data.GetAddressByteSize();
371f325ba9dSStephen Wilson   const bool parsing_32 = byte_size == 4;
372f325ba9dSStephen Wilson 
373f325ba9dSStephen Wilson   // Read p_type;
374f325ba9dSStephen Wilson   if (data.GetU32(offset, &p_type, 1) == NULL)
375f325ba9dSStephen Wilson     return false;
376f325ba9dSStephen Wilson 
377f325ba9dSStephen Wilson   if (parsing_32) {
378f325ba9dSStephen Wilson     // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
379*a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &p_offset, byte_size, 5))
380f325ba9dSStephen Wilson       return false;
381f325ba9dSStephen Wilson 
382f325ba9dSStephen Wilson     // Read p_flags.
383f325ba9dSStephen Wilson     if (data.GetU32(offset, &p_flags, 1) == NULL)
384f325ba9dSStephen Wilson       return false;
385f325ba9dSStephen Wilson 
386f325ba9dSStephen Wilson     // Read p_align.
387*a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &p_align, byte_size))
388f325ba9dSStephen Wilson       return false;
389b9c1b51eSKate Stone   } else {
390f325ba9dSStephen Wilson     // Read p_flags.
391f325ba9dSStephen Wilson     if (data.GetU32(offset, &p_flags, 1) == NULL)
392f325ba9dSStephen Wilson       return false;
393f325ba9dSStephen Wilson 
394f325ba9dSStephen Wilson     // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
395*a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &p_offset, byte_size, 6))
396f325ba9dSStephen Wilson       return false;
397f325ba9dSStephen Wilson   }
398f325ba9dSStephen Wilson 
399f325ba9dSStephen Wilson   return true;
400f325ba9dSStephen Wilson }
401f325ba9dSStephen Wilson 
402f325ba9dSStephen Wilson //------------------------------------------------------------------------------
403f325ba9dSStephen Wilson // ELFDynamic
404f325ba9dSStephen Wilson 
405b9c1b51eSKate Stone ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); }
406f325ba9dSStephen Wilson 
407b9c1b51eSKate Stone bool ELFDynamic::Parse(const lldb_private::DataExtractor &data,
408b9c1b51eSKate Stone                        lldb::offset_t *offset) {
409f325ba9dSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
410f325ba9dSStephen Wilson   return GetMaxS64(data, offset, &d_tag, byte_size, 2);
411f325ba9dSStephen Wilson }
412f325ba9dSStephen Wilson 
41343fe645bSStephen Wilson //------------------------------------------------------------------------------
41443fe645bSStephen Wilson // ELFRel
41543fe645bSStephen Wilson 
416b9c1b51eSKate Stone ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
41743fe645bSStephen Wilson 
418b9c1b51eSKate Stone bool ELFRel::Parse(const lldb_private::DataExtractor &data,
419b9c1b51eSKate Stone                    lldb::offset_t *offset) {
42043fe645bSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
42143fe645bSStephen Wilson 
42243fe645bSStephen Wilson   // Read r_offset and r_info.
423*a6682a41SJonas Devlieghere   return GetMaxU64(data, offset, &r_offset, byte_size, 2) != false;
42443fe645bSStephen Wilson }
42543fe645bSStephen Wilson 
42643fe645bSStephen Wilson //------------------------------------------------------------------------------
42743fe645bSStephen Wilson // ELFRela
42843fe645bSStephen Wilson 
429b9c1b51eSKate Stone ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
43043fe645bSStephen Wilson 
431b9c1b51eSKate Stone bool ELFRela::Parse(const lldb_private::DataExtractor &data,
432b9c1b51eSKate Stone                     lldb::offset_t *offset) {
43343fe645bSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
43443fe645bSStephen Wilson 
43543fe645bSStephen Wilson   // Read r_offset and r_info.
436*a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &r_offset, byte_size, 2))
43743fe645bSStephen Wilson     return false;
43843fe645bSStephen Wilson 
43943fe645bSStephen Wilson   // Read r_addend;
440*a6682a41SJonas Devlieghere   if (!GetMaxS64(data, offset, &r_addend, byte_size))
44143fe645bSStephen Wilson     return false;
44243fe645bSStephen Wilson 
44343fe645bSStephen Wilson   return true;
44443fe645bSStephen Wilson }
445