1f325ba9dSStephen Wilson //===-- ELFHeader.cpp ----------------------------------------- -*- C++ -*-===//
2f325ba9dSStephen Wilson //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler 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 //------------------------------------------------------------------------------
22f325ba9dSStephen Wilson // Static utility functions.
23f325ba9dSStephen Wilson //
24f325ba9dSStephen Wilson // GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor
25f325ba9dSStephen Wilson // with error handling code and provide for parsing a sequence of values.
26b9c1b51eSKate Stone static bool GetMaxU64(const lldb_private::DataExtractor &data,
27b9c1b51eSKate Stone                       lldb::offset_t *offset, uint64_t *value,
28b9c1b51eSKate Stone                       uint32_t byte_size) {
29c7bece56SGreg Clayton   const lldb::offset_t saved_offset = *offset;
30f325ba9dSStephen Wilson   *value = data.GetMaxU64(offset, byte_size);
31f325ba9dSStephen Wilson   return *offset != saved_offset;
32f325ba9dSStephen Wilson }
33f325ba9dSStephen Wilson 
34b9c1b51eSKate Stone static bool GetMaxU64(const lldb_private::DataExtractor &data,
35b9c1b51eSKate Stone                       lldb::offset_t *offset, uint64_t *value,
36b9c1b51eSKate Stone                       uint32_t byte_size, uint32_t count) {
37c7bece56SGreg Clayton   lldb::offset_t saved_offset = *offset;
38f325ba9dSStephen Wilson 
39b9c1b51eSKate Stone   for (uint32_t i = 0; i < count; ++i, ++value) {
40a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, value, byte_size)) {
41f325ba9dSStephen Wilson       *offset = saved_offset;
42f325ba9dSStephen Wilson       return false;
43f325ba9dSStephen Wilson     }
44f325ba9dSStephen Wilson   }
45f325ba9dSStephen Wilson   return true;
46f325ba9dSStephen Wilson }
47f325ba9dSStephen Wilson 
48b9c1b51eSKate Stone static bool GetMaxS64(const lldb_private::DataExtractor &data,
49b9c1b51eSKate Stone                       lldb::offset_t *offset, int64_t *value,
50b9c1b51eSKate Stone                       uint32_t byte_size) {
51c7bece56SGreg Clayton   const lldb::offset_t saved_offset = *offset;
52f325ba9dSStephen Wilson   *value = data.GetMaxS64(offset, byte_size);
53f325ba9dSStephen Wilson   return *offset != saved_offset;
54f325ba9dSStephen Wilson }
55f325ba9dSStephen Wilson 
56b9c1b51eSKate Stone static bool GetMaxS64(const lldb_private::DataExtractor &data,
57b9c1b51eSKate Stone                       lldb::offset_t *offset, int64_t *value,
58b9c1b51eSKate Stone                       uint32_t byte_size, uint32_t count) {
59c7bece56SGreg Clayton   lldb::offset_t saved_offset = *offset;
60f325ba9dSStephen Wilson 
61b9c1b51eSKate Stone   for (uint32_t i = 0; i < count; ++i, ++value) {
62a6682a41SJonas Devlieghere     if (!GetMaxS64(data, offset, value, byte_size)) {
63f325ba9dSStephen Wilson       *offset = saved_offset;
64f325ba9dSStephen Wilson       return false;
65f325ba9dSStephen Wilson     }
66f325ba9dSStephen Wilson   }
67f325ba9dSStephen Wilson   return true;
68f325ba9dSStephen Wilson }
69f325ba9dSStephen Wilson 
70f325ba9dSStephen Wilson //------------------------------------------------------------------------------
71f325ba9dSStephen Wilson // ELFHeader
72f325ba9dSStephen Wilson 
73b9c1b51eSKate Stone ELFHeader::ELFHeader() { memset(this, 0, sizeof(ELFHeader)); }
74f325ba9dSStephen Wilson 
75b9c1b51eSKate Stone ByteOrder ELFHeader::GetByteOrder() const {
76f325ba9dSStephen Wilson   if (e_ident[EI_DATA] == ELFDATA2MSB)
77f325ba9dSStephen Wilson     return eByteOrderBig;
78f325ba9dSStephen Wilson   if (e_ident[EI_DATA] == ELFDATA2LSB)
79f325ba9dSStephen Wilson     return eByteOrderLittle;
80f325ba9dSStephen Wilson   return eByteOrderInvalid;
81f325ba9dSStephen Wilson }
82f325ba9dSStephen Wilson 
8323ccc291SPavel Labath bool ELFHeader::HasHeaderExtension() const {
8423ccc291SPavel Labath   bool result = false;
8523ccc291SPavel Labath 
8623ccc291SPavel Labath   // Check if any of these values looks like sentinel.
8723ccc291SPavel Labath   result |= e_phnum_hdr == 0xFFFF; // PN_XNUM
8823ccc291SPavel Labath   result |= e_shnum_hdr == SHN_UNDEF;
8923ccc291SPavel Labath   result |= e_shstrndx_hdr == SHN_XINDEX;
9023ccc291SPavel Labath 
9123ccc291SPavel Labath   // If header extension is present, the section offset cannot be null.
9223ccc291SPavel Labath   result &= e_shoff != 0;
9323ccc291SPavel Labath 
9423ccc291SPavel Labath   // Done.
9523ccc291SPavel Labath   return result;
9623ccc291SPavel Labath }
9723ccc291SPavel Labath 
9823ccc291SPavel Labath void ELFHeader::ParseHeaderExtension(lldb_private::DataExtractor &data) {
9923ccc291SPavel Labath   // Extract section #0 header.
10023ccc291SPavel Labath   ELFSectionHeader section_zero;
10123ccc291SPavel Labath   lldb::offset_t offset = 0;
10223ccc291SPavel Labath   lldb_private::DataExtractor sh_data(data, e_shoff, e_shentsize);
10323ccc291SPavel Labath   bool ok = section_zero.Parse(sh_data, &offset);
10423ccc291SPavel Labath 
10523ccc291SPavel Labath   // If we succeeded, fix the header.
10623ccc291SPavel Labath   if (ok) {
10723ccc291SPavel Labath     if (e_phnum_hdr == 0xFFFF) // PN_XNUM
10823ccc291SPavel Labath       e_phnum = section_zero.sh_info;
10923ccc291SPavel Labath     if (e_shnum_hdr == SHN_UNDEF)
11023ccc291SPavel Labath       e_shnum = section_zero.sh_size;
11123ccc291SPavel Labath     if (e_shstrndx_hdr == SHN_XINDEX)
11223ccc291SPavel Labath       e_shstrndx = section_zero.sh_link;
11323ccc291SPavel Labath   }
11423ccc291SPavel Labath }
11523ccc291SPavel Labath 
116b9c1b51eSKate Stone bool ELFHeader::Parse(lldb_private::DataExtractor &data,
117b9c1b51eSKate Stone                       lldb::offset_t *offset) {
118f325ba9dSStephen Wilson   // Read e_ident.  This provides byte order and address size info.
119f325ba9dSStephen Wilson   if (data.GetU8(offset, &e_ident, EI_NIDENT) == NULL)
120f325ba9dSStephen Wilson     return false;
121f325ba9dSStephen Wilson 
122f325ba9dSStephen Wilson   const unsigned byte_size = Is32Bit() ? 4 : 8;
123f325ba9dSStephen Wilson   data.SetByteOrder(GetByteOrder());
124f325ba9dSStephen Wilson   data.SetAddressByteSize(byte_size);
125f325ba9dSStephen Wilson 
126f325ba9dSStephen Wilson   // Read e_type and e_machine.
127f325ba9dSStephen Wilson   if (data.GetU16(offset, &e_type, 2) == NULL)
128f325ba9dSStephen Wilson     return false;
129f325ba9dSStephen Wilson 
130f325ba9dSStephen Wilson   // Read e_version.
131f325ba9dSStephen Wilson   if (data.GetU32(offset, &e_version, 1) == NULL)
132f325ba9dSStephen Wilson     return false;
133f325ba9dSStephen Wilson 
134f325ba9dSStephen Wilson   // Read e_entry, e_phoff and e_shoff.
135a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &e_entry, byte_size, 3))
136f325ba9dSStephen Wilson     return false;
137f325ba9dSStephen Wilson 
138f325ba9dSStephen Wilson   // Read e_flags.
139f325ba9dSStephen Wilson   if (data.GetU32(offset, &e_flags, 1) == NULL)
140f325ba9dSStephen Wilson     return false;
141f325ba9dSStephen Wilson 
14205097246SAdrian Prantl   // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx.
143f325ba9dSStephen Wilson   if (data.GetU16(offset, &e_ehsize, 6) == NULL)
144f325ba9dSStephen Wilson     return false;
145f325ba9dSStephen Wilson 
14605097246SAdrian Prantl   // Initialize e_phnum, e_shnum, and e_shstrndx with the values read from the
14705097246SAdrian Prantl   // header.
14823ccc291SPavel Labath   e_phnum = e_phnum_hdr;
14923ccc291SPavel Labath   e_shnum = e_shnum_hdr;
15023ccc291SPavel Labath   e_shstrndx = e_shstrndx_hdr;
15123ccc291SPavel Labath 
15223ccc291SPavel Labath   // See if we have extended header in section #0.
15323ccc291SPavel Labath   if (HasHeaderExtension())
15423ccc291SPavel Labath     ParseHeaderExtension(data);
15523ccc291SPavel Labath 
156f325ba9dSStephen Wilson   return true;
157f325ba9dSStephen Wilson }
158f325ba9dSStephen Wilson 
159b9c1b51eSKate Stone bool ELFHeader::MagicBytesMatch(const uint8_t *magic) {
160f325ba9dSStephen Wilson   return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
161f325ba9dSStephen Wilson }
162f325ba9dSStephen Wilson 
163b9c1b51eSKate Stone unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) {
164f325ba9dSStephen Wilson   unsigned address_size = 0;
165f325ba9dSStephen Wilson 
166b9c1b51eSKate Stone   switch (magic[EI_CLASS]) {
167f325ba9dSStephen Wilson   case ELFCLASS32:
168f325ba9dSStephen Wilson     address_size = 4;
169f325ba9dSStephen Wilson     break;
170f325ba9dSStephen Wilson 
171f325ba9dSStephen Wilson   case ELFCLASS64:
172f325ba9dSStephen Wilson     address_size = 8;
173f325ba9dSStephen Wilson     break;
174f325ba9dSStephen Wilson   }
175f325ba9dSStephen Wilson   return address_size;
176f325ba9dSStephen Wilson }
177f325ba9dSStephen Wilson 
178b9c1b51eSKate Stone unsigned ELFHeader::GetRelocationJumpSlotType() const {
17943fe645bSStephen Wilson   unsigned slot = 0;
18043fe645bSStephen Wilson 
181b9c1b51eSKate Stone   switch (e_machine) {
18243fe645bSStephen Wilson   default:
18343fe645bSStephen Wilson     assert(false && "architecture not supported");
18443fe645bSStephen Wilson     break;
1856256a0eaSJustin Hibbits   case EM_PPC:
1866256a0eaSJustin Hibbits     slot = R_PPC_JMP_SLOT;
1876256a0eaSJustin Hibbits     break;
1886256a0eaSJustin Hibbits   case EM_PPC64:
1896256a0eaSJustin Hibbits     slot = R_PPC64_JMP_SLOT;
1906256a0eaSJustin Hibbits     break;
19143fe645bSStephen Wilson   case EM_386:
19286f422e1SRafael Espindola   case EM_IAMCU: // FIXME: is this correct?
19343fe645bSStephen Wilson     slot = R_386_JUMP_SLOT;
19443fe645bSStephen Wilson     break;
19543fe645bSStephen Wilson   case EM_X86_64:
19643fe645bSStephen Wilson     slot = R_X86_64_JUMP_SLOT;
19743fe645bSStephen Wilson     break;
19843fe645bSStephen Wilson   case EM_ARM:
19943fe645bSStephen Wilson     slot = R_ARM_JUMP_SLOT;
20043fe645bSStephen Wilson     break;
201ca238a7bSDeepak Panickal   case EM_HEXAGON:
202ca238a7bSDeepak Panickal     slot = R_HEX_JMP_SLOT;
203ca238a7bSDeepak Panickal     break;
20433bba9f4STodd Fiala   case EM_AARCH64:
20533bba9f4STodd Fiala     slot = R_AARCH64_JUMP_SLOT;
20633bba9f4STodd Fiala     break;
207c9335a3fSMohit K. Bhakkad   case EM_MIPS:
208c9335a3fSMohit K. Bhakkad     slot = R_MIPS_JUMP_SLOT;
209c9335a3fSMohit K. Bhakkad     break;
210bb00d0b6SUlrich Weigand   case EM_S390:
211bb00d0b6SUlrich Weigand     slot = R_390_JMP_SLOT;
212bb00d0b6SUlrich Weigand     break;
21343fe645bSStephen Wilson   }
21443fe645bSStephen Wilson 
21543fe645bSStephen Wilson   return slot;
21643fe645bSStephen Wilson }
21743fe645bSStephen Wilson 
218f325ba9dSStephen Wilson //------------------------------------------------------------------------------
219f325ba9dSStephen Wilson // ELFSectionHeader
220f325ba9dSStephen Wilson 
221b9c1b51eSKate Stone ELFSectionHeader::ELFSectionHeader() {
222f325ba9dSStephen Wilson   memset(this, 0, sizeof(ELFSectionHeader));
223f325ba9dSStephen Wilson }
224f325ba9dSStephen Wilson 
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.
230f325ba9dSStephen Wilson   if (data.GetU32(offset, &sh_name, 2) == NULL)
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.
242f325ba9dSStephen Wilson   if (data.GetU32(offset, &sh_link, 2) == NULL)
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 //------------------------------------------------------------------------------
253f325ba9dSStephen Wilson // ELFSymbol
254f325ba9dSStephen Wilson 
255b9c1b51eSKate Stone ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); }
256f325ba9dSStephen Wilson 
257b9c1b51eSKate Stone #define ENUM_TO_CSTR(e)                                                        \
258b9c1b51eSKate Stone   case e:                                                                      \
259b9c1b51eSKate Stone     return #e
2609594f4c8SGreg Clayton 
261b9c1b51eSKate Stone const char *ELFSymbol::bindingToCString(unsigned char binding) {
262b9c1b51eSKate Stone   switch (binding) {
2639594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_LOCAL);
2649594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_GLOBAL);
2659594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_WEAK);
2669594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_LOOS);
2679594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_HIOS);
2689594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_LOPROC);
2699594f4c8SGreg Clayton     ENUM_TO_CSTR(STB_HIPROC);
2709594f4c8SGreg Clayton   }
2719594f4c8SGreg Clayton   return "";
2729594f4c8SGreg Clayton }
2739594f4c8SGreg Clayton 
274b9c1b51eSKate Stone const char *ELFSymbol::typeToCString(unsigned char type) {
275b9c1b51eSKate Stone   switch (type) {
2769594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_NOTYPE);
2779594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_OBJECT);
2789594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_FUNC);
2799594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_SECTION);
2809594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_FILE);
2819594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_COMMON);
2829594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_TLS);
2839594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_GNU_IFUNC);
284a6d0dd74SPavel Labath     ENUM_TO_CSTR(STT_HIOS);
2859594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_LOPROC);
2869594f4c8SGreg Clayton     ENUM_TO_CSTR(STT_HIPROC);
2879594f4c8SGreg Clayton   }
2889594f4c8SGreg Clayton   return "";
2899594f4c8SGreg Clayton }
2909594f4c8SGreg Clayton 
291b9c1b51eSKate Stone const char *ELFSymbol::sectionIndexToCString(
292b9c1b51eSKate Stone     elf_half shndx, const lldb_private::SectionList *section_list) {
293b9c1b51eSKate Stone   switch (shndx) {
2949594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_UNDEF);
2959594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_LOPROC);
2969594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_HIPROC);
2979594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_LOOS);
2989594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_HIOS);
2999594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_ABS);
3009594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_COMMON);
3019594f4c8SGreg Clayton     ENUM_TO_CSTR(SHN_XINDEX);
302b9c1b51eSKate Stone   default: {
303b9c1b51eSKate Stone     const lldb_private::Section *section =
304b9c1b51eSKate Stone         section_list->GetSectionAtIndex(shndx).get();
3059594f4c8SGreg Clayton     if (section)
3069594f4c8SGreg Clayton       return section->GetName().AsCString("");
307b9c1b51eSKate Stone   } break;
3089594f4c8SGreg Clayton   }
3099594f4c8SGreg Clayton   return "";
3109594f4c8SGreg Clayton }
3119594f4c8SGreg Clayton 
312b9c1b51eSKate Stone void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx,
3139594f4c8SGreg Clayton                      const lldb_private::DataExtractor *strtab_data,
314b9c1b51eSKate Stone                      const lldb_private::SectionList *section_list) {
315b9c1b51eSKate Stone   s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64
316b9c1b51eSKate Stone             " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
317b9c1b51eSKate Stone             idx, st_value, st_size, st_name, st_info,
318b9c1b51eSKate Stone             bindingToCString(getBinding()), typeToCString(getType()), st_other,
319b9c1b51eSKate Stone             st_shndx, sectionIndexToCString(st_shndx, section_list),
3209594f4c8SGreg Clayton             strtab_data ? strtab_data->PeekCStr(st_name) : "");
3219594f4c8SGreg Clayton }
3229594f4c8SGreg Clayton 
323b9c1b51eSKate Stone bool ELFSymbol::Parse(const lldb_private::DataExtractor &data,
324b9c1b51eSKate Stone                       lldb::offset_t *offset) {
325f325ba9dSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
326f325ba9dSStephen Wilson   const bool parsing_32 = byte_size == 4;
327f325ba9dSStephen Wilson 
328f325ba9dSStephen Wilson   // Read st_name.
329f325ba9dSStephen Wilson   if (data.GetU32(offset, &st_name, 1) == NULL)
330f325ba9dSStephen Wilson     return false;
331f325ba9dSStephen Wilson 
332b9c1b51eSKate Stone   if (parsing_32) {
333f325ba9dSStephen Wilson     // Read st_value and st_size.
334a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &st_value, byte_size, 2))
335f325ba9dSStephen Wilson       return false;
336f325ba9dSStephen Wilson 
337f325ba9dSStephen Wilson     // Read st_info and st_other.
338f325ba9dSStephen Wilson     if (data.GetU8(offset, &st_info, 2) == NULL)
339f325ba9dSStephen Wilson       return false;
340f325ba9dSStephen Wilson 
341f325ba9dSStephen Wilson     // Read st_shndx.
342f325ba9dSStephen Wilson     if (data.GetU16(offset, &st_shndx, 1) == NULL)
343f325ba9dSStephen Wilson       return false;
344b9c1b51eSKate Stone   } else {
345f325ba9dSStephen Wilson     // Read st_info and st_other.
346f325ba9dSStephen Wilson     if (data.GetU8(offset, &st_info, 2) == NULL)
347f325ba9dSStephen Wilson       return false;
348f325ba9dSStephen Wilson 
349f325ba9dSStephen Wilson     // Read st_shndx.
350f325ba9dSStephen Wilson     if (data.GetU16(offset, &st_shndx, 1) == NULL)
351f325ba9dSStephen Wilson       return false;
352f325ba9dSStephen Wilson 
353f325ba9dSStephen Wilson     // Read st_value and st_size.
354f325ba9dSStephen Wilson     if (data.GetU64(offset, &st_value, 2) == NULL)
355f325ba9dSStephen Wilson       return false;
356f325ba9dSStephen Wilson   }
357f325ba9dSStephen Wilson   return true;
358f325ba9dSStephen Wilson }
359f325ba9dSStephen Wilson 
360f325ba9dSStephen Wilson //------------------------------------------------------------------------------
361f325ba9dSStephen Wilson // ELFProgramHeader
362f325ba9dSStephen Wilson 
363b9c1b51eSKate Stone ELFProgramHeader::ELFProgramHeader() {
364f325ba9dSStephen Wilson   memset(this, 0, sizeof(ELFProgramHeader));
365f325ba9dSStephen Wilson }
366f325ba9dSStephen Wilson 
367b9c1b51eSKate Stone bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
368b9c1b51eSKate Stone                              lldb::offset_t *offset) {
369f325ba9dSStephen Wilson   const uint32_t byte_size = data.GetAddressByteSize();
370f325ba9dSStephen Wilson   const bool parsing_32 = byte_size == 4;
371f325ba9dSStephen Wilson 
372f325ba9dSStephen Wilson   // Read p_type;
373f325ba9dSStephen Wilson   if (data.GetU32(offset, &p_type, 1) == NULL)
374f325ba9dSStephen Wilson     return false;
375f325ba9dSStephen Wilson 
376f325ba9dSStephen Wilson   if (parsing_32) {
377f325ba9dSStephen Wilson     // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
378a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &p_offset, byte_size, 5))
379f325ba9dSStephen Wilson       return false;
380f325ba9dSStephen Wilson 
381f325ba9dSStephen Wilson     // Read p_flags.
382f325ba9dSStephen Wilson     if (data.GetU32(offset, &p_flags, 1) == NULL)
383f325ba9dSStephen Wilson       return false;
384f325ba9dSStephen Wilson 
385f325ba9dSStephen Wilson     // Read p_align.
386a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &p_align, byte_size))
387f325ba9dSStephen Wilson       return false;
388b9c1b51eSKate Stone   } else {
389f325ba9dSStephen Wilson     // Read p_flags.
390f325ba9dSStephen Wilson     if (data.GetU32(offset, &p_flags, 1) == NULL)
391f325ba9dSStephen Wilson       return false;
392f325ba9dSStephen Wilson 
393f325ba9dSStephen Wilson     // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
394a6682a41SJonas Devlieghere     if (!GetMaxU64(data, offset, &p_offset, byte_size, 6))
395f325ba9dSStephen Wilson       return false;
396f325ba9dSStephen Wilson   }
397f325ba9dSStephen Wilson 
398f325ba9dSStephen Wilson   return true;
399f325ba9dSStephen Wilson }
400f325ba9dSStephen Wilson 
401f325ba9dSStephen Wilson //------------------------------------------------------------------------------
402f325ba9dSStephen Wilson // ELFDynamic
403f325ba9dSStephen Wilson 
404b9c1b51eSKate Stone ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); }
405f325ba9dSStephen Wilson 
406b9c1b51eSKate Stone bool ELFDynamic::Parse(const lldb_private::DataExtractor &data,
407b9c1b51eSKate Stone                        lldb::offset_t *offset) {
408f325ba9dSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
409f325ba9dSStephen Wilson   return GetMaxS64(data, offset, &d_tag, byte_size, 2);
410f325ba9dSStephen Wilson }
411f325ba9dSStephen Wilson 
41243fe645bSStephen Wilson //------------------------------------------------------------------------------
41343fe645bSStephen Wilson // ELFRel
41443fe645bSStephen Wilson 
415b9c1b51eSKate Stone ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
41643fe645bSStephen Wilson 
417b9c1b51eSKate Stone bool ELFRel::Parse(const lldb_private::DataExtractor &data,
418b9c1b51eSKate Stone                    lldb::offset_t *offset) {
41943fe645bSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
42043fe645bSStephen Wilson 
42143fe645bSStephen Wilson   // Read r_offset and r_info.
422a6682a41SJonas Devlieghere   return GetMaxU64(data, offset, &r_offset, byte_size, 2) != false;
42343fe645bSStephen Wilson }
42443fe645bSStephen Wilson 
42543fe645bSStephen Wilson //------------------------------------------------------------------------------
42643fe645bSStephen Wilson // ELFRela
42743fe645bSStephen Wilson 
428b9c1b51eSKate Stone ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
42943fe645bSStephen Wilson 
430b9c1b51eSKate Stone bool ELFRela::Parse(const lldb_private::DataExtractor &data,
431b9c1b51eSKate Stone                     lldb::offset_t *offset) {
43243fe645bSStephen Wilson   const unsigned byte_size = data.GetAddressByteSize();
43343fe645bSStephen Wilson 
43443fe645bSStephen Wilson   // Read r_offset and r_info.
435a6682a41SJonas Devlieghere   if (!GetMaxU64(data, offset, &r_offset, byte_size, 2))
43643fe645bSStephen Wilson     return false;
43743fe645bSStephen Wilson 
43843fe645bSStephen Wilson   // Read r_addend;
439a6682a41SJonas Devlieghere   if (!GetMaxS64(data, offset, &r_addend, byte_size))
44043fe645bSStephen Wilson     return false;
44143fe645bSStephen Wilson 
44243fe645bSStephen Wilson   return true;
44343fe645bSStephen Wilson }
444