1ac7ddfbfSEd Maste //===-- ELFHeader.cpp ----------------------------------------- -*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
10ac7ddfbfSEd Maste #include <cstring>
11ac7ddfbfSEd Maste
12ac7ddfbfSEd Maste #include "lldb/Core/Section.h"
13f678e45dSDimitry Andric #include "lldb/Utility/DataExtractor.h"
14f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
15ac7ddfbfSEd Maste
16ac7ddfbfSEd Maste #include "ELFHeader.h"
17ac7ddfbfSEd Maste
18ac7ddfbfSEd Maste using namespace elf;
19ac7ddfbfSEd Maste using namespace lldb;
20ac7ddfbfSEd Maste using namespace llvm::ELF;
21ac7ddfbfSEd Maste
22ac7ddfbfSEd Maste //------------------------------------------------------------------------------
23ac7ddfbfSEd Maste // Static utility functions.
24ac7ddfbfSEd Maste //
25ac7ddfbfSEd Maste // GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor
26ac7ddfbfSEd Maste // 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)27435933ddSDimitry Andric static bool GetMaxU64(const lldb_private::DataExtractor &data,
28435933ddSDimitry Andric lldb::offset_t *offset, uint64_t *value,
29435933ddSDimitry Andric uint32_t byte_size) {
30ac7ddfbfSEd Maste const lldb::offset_t saved_offset = *offset;
31ac7ddfbfSEd Maste *value = data.GetMaxU64(offset, byte_size);
32ac7ddfbfSEd Maste return *offset != saved_offset;
33ac7ddfbfSEd Maste }
34ac7ddfbfSEd Maste
GetMaxU64(const lldb_private::DataExtractor & data,lldb::offset_t * offset,uint64_t * value,uint32_t byte_size,uint32_t count)35435933ddSDimitry Andric static bool GetMaxU64(const lldb_private::DataExtractor &data,
36435933ddSDimitry Andric lldb::offset_t *offset, uint64_t *value,
37435933ddSDimitry Andric uint32_t byte_size, uint32_t count) {
38ac7ddfbfSEd Maste lldb::offset_t saved_offset = *offset;
39ac7ddfbfSEd Maste
40435933ddSDimitry Andric for (uint32_t i = 0; i < count; ++i, ++value) {
41*b5893f02SDimitry Andric if (!GetMaxU64(data, offset, value, byte_size)) {
42ac7ddfbfSEd Maste *offset = saved_offset;
43ac7ddfbfSEd Maste return false;
44ac7ddfbfSEd Maste }
45ac7ddfbfSEd Maste }
46ac7ddfbfSEd Maste return true;
47ac7ddfbfSEd Maste }
48ac7ddfbfSEd Maste
GetMaxS64(const lldb_private::DataExtractor & data,lldb::offset_t * offset,int64_t * value,uint32_t byte_size)49435933ddSDimitry Andric static bool GetMaxS64(const lldb_private::DataExtractor &data,
50435933ddSDimitry Andric lldb::offset_t *offset, int64_t *value,
51435933ddSDimitry Andric uint32_t byte_size) {
52ac7ddfbfSEd Maste const lldb::offset_t saved_offset = *offset;
53ac7ddfbfSEd Maste *value = data.GetMaxS64(offset, byte_size);
54ac7ddfbfSEd Maste return *offset != saved_offset;
55ac7ddfbfSEd Maste }
56ac7ddfbfSEd Maste
GetMaxS64(const lldb_private::DataExtractor & data,lldb::offset_t * offset,int64_t * value,uint32_t byte_size,uint32_t count)57435933ddSDimitry Andric static bool GetMaxS64(const lldb_private::DataExtractor &data,
58435933ddSDimitry Andric lldb::offset_t *offset, int64_t *value,
59435933ddSDimitry Andric uint32_t byte_size, uint32_t count) {
60ac7ddfbfSEd Maste lldb::offset_t saved_offset = *offset;
61ac7ddfbfSEd Maste
62435933ddSDimitry Andric for (uint32_t i = 0; i < count; ++i, ++value) {
63*b5893f02SDimitry Andric if (!GetMaxS64(data, offset, value, byte_size)) {
64ac7ddfbfSEd Maste *offset = saved_offset;
65ac7ddfbfSEd Maste return false;
66ac7ddfbfSEd Maste }
67ac7ddfbfSEd Maste }
68ac7ddfbfSEd Maste return true;
69ac7ddfbfSEd Maste }
70ac7ddfbfSEd Maste
71ac7ddfbfSEd Maste //------------------------------------------------------------------------------
72ac7ddfbfSEd Maste // ELFHeader
73ac7ddfbfSEd Maste
ELFHeader()74435933ddSDimitry Andric ELFHeader::ELFHeader() { memset(this, 0, sizeof(ELFHeader)); }
75ac7ddfbfSEd Maste
GetByteOrder() const76435933ddSDimitry Andric ByteOrder ELFHeader::GetByteOrder() const {
77ac7ddfbfSEd Maste if (e_ident[EI_DATA] == ELFDATA2MSB)
78ac7ddfbfSEd Maste return eByteOrderBig;
79ac7ddfbfSEd Maste if (e_ident[EI_DATA] == ELFDATA2LSB)
80ac7ddfbfSEd Maste return eByteOrderLittle;
81ac7ddfbfSEd Maste return eByteOrderInvalid;
82ac7ddfbfSEd Maste }
83ac7ddfbfSEd Maste
HasHeaderExtension() const84f678e45dSDimitry Andric bool ELFHeader::HasHeaderExtension() const {
85f678e45dSDimitry Andric bool result = false;
86f678e45dSDimitry Andric
87f678e45dSDimitry Andric // Check if any of these values looks like sentinel.
88f678e45dSDimitry Andric result |= e_phnum_hdr == 0xFFFF; // PN_XNUM
89f678e45dSDimitry Andric result |= e_shnum_hdr == SHN_UNDEF;
90f678e45dSDimitry Andric result |= e_shstrndx_hdr == SHN_XINDEX;
91f678e45dSDimitry Andric
92f678e45dSDimitry Andric // If header extension is present, the section offset cannot be null.
93f678e45dSDimitry Andric result &= e_shoff != 0;
94f678e45dSDimitry Andric
95f678e45dSDimitry Andric // Done.
96f678e45dSDimitry Andric return result;
97f678e45dSDimitry Andric }
98f678e45dSDimitry Andric
ParseHeaderExtension(lldb_private::DataExtractor & data)99f678e45dSDimitry Andric void ELFHeader::ParseHeaderExtension(lldb_private::DataExtractor &data) {
100f678e45dSDimitry Andric // Extract section #0 header.
101f678e45dSDimitry Andric ELFSectionHeader section_zero;
102f678e45dSDimitry Andric lldb::offset_t offset = 0;
103f678e45dSDimitry Andric lldb_private::DataExtractor sh_data(data, e_shoff, e_shentsize);
104f678e45dSDimitry Andric bool ok = section_zero.Parse(sh_data, &offset);
105f678e45dSDimitry Andric
106f678e45dSDimitry Andric // If we succeeded, fix the header.
107f678e45dSDimitry Andric if (ok) {
108f678e45dSDimitry Andric if (e_phnum_hdr == 0xFFFF) // PN_XNUM
109f678e45dSDimitry Andric e_phnum = section_zero.sh_info;
110f678e45dSDimitry Andric if (e_shnum_hdr == SHN_UNDEF)
111f678e45dSDimitry Andric e_shnum = section_zero.sh_size;
112f678e45dSDimitry Andric if (e_shstrndx_hdr == SHN_XINDEX)
113f678e45dSDimitry Andric e_shstrndx = section_zero.sh_link;
114f678e45dSDimitry Andric }
115f678e45dSDimitry Andric }
116f678e45dSDimitry Andric
Parse(lldb_private::DataExtractor & data,lldb::offset_t * offset)117435933ddSDimitry Andric bool ELFHeader::Parse(lldb_private::DataExtractor &data,
118435933ddSDimitry Andric lldb::offset_t *offset) {
119ac7ddfbfSEd Maste // Read e_ident. This provides byte order and address size info.
120ac7ddfbfSEd Maste if (data.GetU8(offset, &e_ident, EI_NIDENT) == NULL)
121ac7ddfbfSEd Maste return false;
122ac7ddfbfSEd Maste
123ac7ddfbfSEd Maste const unsigned byte_size = Is32Bit() ? 4 : 8;
124ac7ddfbfSEd Maste data.SetByteOrder(GetByteOrder());
125ac7ddfbfSEd Maste data.SetAddressByteSize(byte_size);
126ac7ddfbfSEd Maste
127ac7ddfbfSEd Maste // Read e_type and e_machine.
128ac7ddfbfSEd Maste if (data.GetU16(offset, &e_type, 2) == NULL)
129ac7ddfbfSEd Maste return false;
130ac7ddfbfSEd Maste
131ac7ddfbfSEd Maste // Read e_version.
132ac7ddfbfSEd Maste if (data.GetU32(offset, &e_version, 1) == NULL)
133ac7ddfbfSEd Maste return false;
134ac7ddfbfSEd Maste
135ac7ddfbfSEd Maste // Read e_entry, e_phoff and e_shoff.
136*b5893f02SDimitry Andric if (!GetMaxU64(data, offset, &e_entry, byte_size, 3))
137ac7ddfbfSEd Maste return false;
138ac7ddfbfSEd Maste
139ac7ddfbfSEd Maste // Read e_flags.
140ac7ddfbfSEd Maste if (data.GetU32(offset, &e_flags, 1) == NULL)
141ac7ddfbfSEd Maste return false;
142ac7ddfbfSEd Maste
1434ba319b5SDimitry Andric // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx.
144ac7ddfbfSEd Maste if (data.GetU16(offset, &e_ehsize, 6) == NULL)
145ac7ddfbfSEd Maste return false;
146ac7ddfbfSEd Maste
1474ba319b5SDimitry Andric // Initialize e_phnum, e_shnum, and e_shstrndx with the values read from the
1484ba319b5SDimitry Andric // header.
149f678e45dSDimitry Andric e_phnum = e_phnum_hdr;
150f678e45dSDimitry Andric e_shnum = e_shnum_hdr;
151f678e45dSDimitry Andric e_shstrndx = e_shstrndx_hdr;
152f678e45dSDimitry Andric
153f678e45dSDimitry Andric // See if we have extended header in section #0.
154f678e45dSDimitry Andric if (HasHeaderExtension())
155f678e45dSDimitry Andric ParseHeaderExtension(data);
156f678e45dSDimitry Andric
157ac7ddfbfSEd Maste return true;
158ac7ddfbfSEd Maste }
159ac7ddfbfSEd Maste
MagicBytesMatch(const uint8_t * magic)160435933ddSDimitry Andric bool ELFHeader::MagicBytesMatch(const uint8_t *magic) {
161ac7ddfbfSEd Maste return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
162ac7ddfbfSEd Maste }
163ac7ddfbfSEd Maste
AddressSizeInBytes(const uint8_t * magic)164435933ddSDimitry Andric unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) {
165ac7ddfbfSEd Maste unsigned address_size = 0;
166ac7ddfbfSEd Maste
167435933ddSDimitry Andric switch (magic[EI_CLASS]) {
168ac7ddfbfSEd Maste case ELFCLASS32:
169ac7ddfbfSEd Maste address_size = 4;
170ac7ddfbfSEd Maste break;
171ac7ddfbfSEd Maste
172ac7ddfbfSEd Maste case ELFCLASS64:
173ac7ddfbfSEd Maste address_size = 8;
174ac7ddfbfSEd Maste break;
175ac7ddfbfSEd Maste }
176ac7ddfbfSEd Maste return address_size;
177ac7ddfbfSEd Maste }
178ac7ddfbfSEd Maste
GetRelocationJumpSlotType() const179435933ddSDimitry Andric unsigned ELFHeader::GetRelocationJumpSlotType() const {
180ac7ddfbfSEd Maste unsigned slot = 0;
181ac7ddfbfSEd Maste
182435933ddSDimitry Andric switch (e_machine) {
183ac7ddfbfSEd Maste default:
184ac7ddfbfSEd Maste assert(false && "architecture not supported");
185ac7ddfbfSEd Maste break;
1867aa51b79SEd Maste case EM_PPC:
1877aa51b79SEd Maste slot = R_PPC_JMP_SLOT;
1887aa51b79SEd Maste break;
1897aa51b79SEd Maste case EM_PPC64:
1907aa51b79SEd Maste slot = R_PPC64_JMP_SLOT;
1917aa51b79SEd Maste break;
192ac7ddfbfSEd Maste case EM_386:
1931c3bbb01SEd Maste case EM_IAMCU: // FIXME: is this correct?
194ac7ddfbfSEd Maste slot = R_386_JUMP_SLOT;
195ac7ddfbfSEd Maste break;
196ac7ddfbfSEd Maste case EM_X86_64:
197ac7ddfbfSEd Maste slot = R_X86_64_JUMP_SLOT;
198ac7ddfbfSEd Maste break;
199ac7ddfbfSEd Maste case EM_ARM:
200ac7ddfbfSEd Maste slot = R_ARM_JUMP_SLOT;
201ac7ddfbfSEd Maste break;
2020127ef0fSEd Maste case EM_HEXAGON:
2030127ef0fSEd Maste slot = R_HEX_JMP_SLOT;
2040127ef0fSEd Maste break;
2050127ef0fSEd Maste case EM_AARCH64:
2060127ef0fSEd Maste slot = R_AARCH64_JUMP_SLOT;
2070127ef0fSEd Maste break;
2087aa51b79SEd Maste case EM_MIPS:
2097aa51b79SEd Maste slot = R_MIPS_JUMP_SLOT;
2107aa51b79SEd Maste break;
2114bb0738eSEd Maste case EM_S390:
2124bb0738eSEd Maste slot = R_390_JMP_SLOT;
2134bb0738eSEd Maste break;
214ac7ddfbfSEd Maste }
215ac7ddfbfSEd Maste
216ac7ddfbfSEd Maste return slot;
217ac7ddfbfSEd Maste }
218ac7ddfbfSEd Maste
219ac7ddfbfSEd Maste //------------------------------------------------------------------------------
220ac7ddfbfSEd Maste // ELFSectionHeader
221ac7ddfbfSEd Maste
ELFSectionHeader()222435933ddSDimitry Andric ELFSectionHeader::ELFSectionHeader() {
223ac7ddfbfSEd Maste memset(this, 0, sizeof(ELFSectionHeader));
224ac7ddfbfSEd Maste }
225ac7ddfbfSEd Maste
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)226435933ddSDimitry Andric bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
227435933ddSDimitry Andric lldb::offset_t *offset) {
228ac7ddfbfSEd Maste const unsigned byte_size = data.GetAddressByteSize();
229ac7ddfbfSEd Maste
230ac7ddfbfSEd Maste // Read sh_name and sh_type.
231ac7ddfbfSEd Maste if (data.GetU32(offset, &sh_name, 2) == NULL)
232ac7ddfbfSEd Maste return false;
233ac7ddfbfSEd Maste
234ac7ddfbfSEd Maste // Read sh_flags.
235*b5893f02SDimitry Andric if (!GetMaxU64(data, offset, &sh_flags, byte_size))
236ac7ddfbfSEd Maste return false;
237ac7ddfbfSEd Maste
238ac7ddfbfSEd Maste // Read sh_addr, sh_off and sh_size.
239*b5893f02SDimitry Andric if (!GetMaxU64(data, offset, &sh_addr, byte_size, 3))
240ac7ddfbfSEd Maste return false;
241ac7ddfbfSEd Maste
242ac7ddfbfSEd Maste // Read sh_link and sh_info.
243ac7ddfbfSEd Maste if (data.GetU32(offset, &sh_link, 2) == NULL)
244ac7ddfbfSEd Maste return false;
245ac7ddfbfSEd Maste
246ac7ddfbfSEd Maste // Read sh_addralign and sh_entsize.
247*b5893f02SDimitry Andric if (!GetMaxU64(data, offset, &sh_addralign, byte_size, 2))
248ac7ddfbfSEd Maste return false;
249ac7ddfbfSEd Maste
250ac7ddfbfSEd Maste return true;
251ac7ddfbfSEd Maste }
252ac7ddfbfSEd Maste
253ac7ddfbfSEd Maste //------------------------------------------------------------------------------
254ac7ddfbfSEd Maste // ELFSymbol
255ac7ddfbfSEd Maste
ELFSymbol()256435933ddSDimitry Andric ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); }
257ac7ddfbfSEd Maste
258435933ddSDimitry Andric #define ENUM_TO_CSTR(e) \
259435933ddSDimitry Andric case e: \
260435933ddSDimitry Andric return #e
261ac7ddfbfSEd Maste
bindingToCString(unsigned char binding)262435933ddSDimitry Andric const char *ELFSymbol::bindingToCString(unsigned char binding) {
263435933ddSDimitry Andric switch (binding) {
264ac7ddfbfSEd Maste ENUM_TO_CSTR(STB_LOCAL);
265ac7ddfbfSEd Maste ENUM_TO_CSTR(STB_GLOBAL);
266ac7ddfbfSEd Maste ENUM_TO_CSTR(STB_WEAK);
267ac7ddfbfSEd Maste ENUM_TO_CSTR(STB_LOOS);
268ac7ddfbfSEd Maste ENUM_TO_CSTR(STB_HIOS);
269ac7ddfbfSEd Maste ENUM_TO_CSTR(STB_LOPROC);
270ac7ddfbfSEd Maste ENUM_TO_CSTR(STB_HIPROC);
271ac7ddfbfSEd Maste }
272ac7ddfbfSEd Maste return "";
273ac7ddfbfSEd Maste }
274ac7ddfbfSEd Maste
typeToCString(unsigned char type)275435933ddSDimitry Andric const char *ELFSymbol::typeToCString(unsigned char type) {
276435933ddSDimitry Andric switch (type) {
277ac7ddfbfSEd Maste ENUM_TO_CSTR(STT_NOTYPE);
278ac7ddfbfSEd Maste ENUM_TO_CSTR(STT_OBJECT);
279ac7ddfbfSEd Maste ENUM_TO_CSTR(STT_FUNC);
280ac7ddfbfSEd Maste ENUM_TO_CSTR(STT_SECTION);
281ac7ddfbfSEd Maste ENUM_TO_CSTR(STT_FILE);
282ac7ddfbfSEd Maste ENUM_TO_CSTR(STT_COMMON);
283ac7ddfbfSEd Maste ENUM_TO_CSTR(STT_TLS);
284ac7ddfbfSEd Maste ENUM_TO_CSTR(STT_GNU_IFUNC);
2851c3bbb01SEd Maste ENUM_TO_CSTR(STT_HIOS);
286ac7ddfbfSEd Maste ENUM_TO_CSTR(STT_LOPROC);
287ac7ddfbfSEd Maste ENUM_TO_CSTR(STT_HIPROC);
288ac7ddfbfSEd Maste }
289ac7ddfbfSEd Maste return "";
290ac7ddfbfSEd Maste }
291ac7ddfbfSEd Maste
sectionIndexToCString(elf_half shndx,const lldb_private::SectionList * section_list)292435933ddSDimitry Andric const char *ELFSymbol::sectionIndexToCString(
293435933ddSDimitry Andric elf_half shndx, const lldb_private::SectionList *section_list) {
294435933ddSDimitry Andric switch (shndx) {
295ac7ddfbfSEd Maste ENUM_TO_CSTR(SHN_UNDEF);
296ac7ddfbfSEd Maste ENUM_TO_CSTR(SHN_LOPROC);
297ac7ddfbfSEd Maste ENUM_TO_CSTR(SHN_HIPROC);
298ac7ddfbfSEd Maste ENUM_TO_CSTR(SHN_LOOS);
299ac7ddfbfSEd Maste ENUM_TO_CSTR(SHN_HIOS);
300ac7ddfbfSEd Maste ENUM_TO_CSTR(SHN_ABS);
301ac7ddfbfSEd Maste ENUM_TO_CSTR(SHN_COMMON);
302ac7ddfbfSEd Maste ENUM_TO_CSTR(SHN_XINDEX);
303435933ddSDimitry Andric default: {
304435933ddSDimitry Andric const lldb_private::Section *section =
305435933ddSDimitry Andric section_list->GetSectionAtIndex(shndx).get();
306ac7ddfbfSEd Maste if (section)
307ac7ddfbfSEd Maste return section->GetName().AsCString("");
308435933ddSDimitry Andric } break;
309ac7ddfbfSEd Maste }
310ac7ddfbfSEd Maste return "";
311ac7ddfbfSEd Maste }
312ac7ddfbfSEd Maste
Dump(lldb_private::Stream * s,uint32_t idx,const lldb_private::DataExtractor * strtab_data,const lldb_private::SectionList * section_list)313435933ddSDimitry Andric void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx,
314ac7ddfbfSEd Maste const lldb_private::DataExtractor *strtab_data,
315435933ddSDimitry Andric const lldb_private::SectionList *section_list) {
316435933ddSDimitry Andric s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64
317435933ddSDimitry Andric " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
318435933ddSDimitry Andric idx, st_value, st_size, st_name, st_info,
319435933ddSDimitry Andric bindingToCString(getBinding()), typeToCString(getType()), st_other,
320435933ddSDimitry Andric st_shndx, sectionIndexToCString(st_shndx, section_list),
321ac7ddfbfSEd Maste strtab_data ? strtab_data->PeekCStr(st_name) : "");
322ac7ddfbfSEd Maste }
323ac7ddfbfSEd Maste
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)324435933ddSDimitry Andric bool ELFSymbol::Parse(const lldb_private::DataExtractor &data,
325435933ddSDimitry Andric lldb::offset_t *offset) {
326ac7ddfbfSEd Maste const unsigned byte_size = data.GetAddressByteSize();
327ac7ddfbfSEd Maste const bool parsing_32 = byte_size == 4;
328ac7ddfbfSEd Maste
329ac7ddfbfSEd Maste // Read st_name.
330ac7ddfbfSEd Maste if (data.GetU32(offset, &st_name, 1) == NULL)
331ac7ddfbfSEd Maste return false;
332ac7ddfbfSEd Maste
333435933ddSDimitry Andric if (parsing_32) {
334ac7ddfbfSEd Maste // Read st_value and st_size.
335*b5893f02SDimitry Andric if (!GetMaxU64(data, offset, &st_value, byte_size, 2))
336ac7ddfbfSEd Maste return false;
337ac7ddfbfSEd Maste
338ac7ddfbfSEd Maste // Read st_info and st_other.
339ac7ddfbfSEd Maste if (data.GetU8(offset, &st_info, 2) == NULL)
340ac7ddfbfSEd Maste return false;
341ac7ddfbfSEd Maste
342ac7ddfbfSEd Maste // Read st_shndx.
343ac7ddfbfSEd Maste if (data.GetU16(offset, &st_shndx, 1) == NULL)
344ac7ddfbfSEd Maste return false;
345435933ddSDimitry Andric } else {
346ac7ddfbfSEd Maste // Read st_info and st_other.
347ac7ddfbfSEd Maste if (data.GetU8(offset, &st_info, 2) == NULL)
348ac7ddfbfSEd Maste return false;
349ac7ddfbfSEd Maste
350ac7ddfbfSEd Maste // Read st_shndx.
351ac7ddfbfSEd Maste if (data.GetU16(offset, &st_shndx, 1) == NULL)
352ac7ddfbfSEd Maste return false;
353ac7ddfbfSEd Maste
354ac7ddfbfSEd Maste // Read st_value and st_size.
355ac7ddfbfSEd Maste if (data.GetU64(offset, &st_value, 2) == NULL)
356ac7ddfbfSEd Maste return false;
357ac7ddfbfSEd Maste }
358ac7ddfbfSEd Maste return true;
359ac7ddfbfSEd Maste }
360ac7ddfbfSEd Maste
361ac7ddfbfSEd Maste //------------------------------------------------------------------------------
362ac7ddfbfSEd Maste // ELFProgramHeader
363ac7ddfbfSEd Maste
ELFProgramHeader()364435933ddSDimitry Andric ELFProgramHeader::ELFProgramHeader() {
365ac7ddfbfSEd Maste memset(this, 0, sizeof(ELFProgramHeader));
366ac7ddfbfSEd Maste }
367ac7ddfbfSEd Maste
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)368435933ddSDimitry Andric bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
369435933ddSDimitry Andric lldb::offset_t *offset) {
370ac7ddfbfSEd Maste const uint32_t byte_size = data.GetAddressByteSize();
371ac7ddfbfSEd Maste const bool parsing_32 = byte_size == 4;
372ac7ddfbfSEd Maste
373ac7ddfbfSEd Maste // Read p_type;
374ac7ddfbfSEd Maste if (data.GetU32(offset, &p_type, 1) == NULL)
375ac7ddfbfSEd Maste return false;
376ac7ddfbfSEd Maste
377ac7ddfbfSEd Maste if (parsing_32) {
378ac7ddfbfSEd Maste // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
379*b5893f02SDimitry Andric if (!GetMaxU64(data, offset, &p_offset, byte_size, 5))
380ac7ddfbfSEd Maste return false;
381ac7ddfbfSEd Maste
382ac7ddfbfSEd Maste // Read p_flags.
383ac7ddfbfSEd Maste if (data.GetU32(offset, &p_flags, 1) == NULL)
384ac7ddfbfSEd Maste return false;
385ac7ddfbfSEd Maste
386ac7ddfbfSEd Maste // Read p_align.
387*b5893f02SDimitry Andric if (!GetMaxU64(data, offset, &p_align, byte_size))
388ac7ddfbfSEd Maste return false;
389435933ddSDimitry Andric } else {
390ac7ddfbfSEd Maste // Read p_flags.
391ac7ddfbfSEd Maste if (data.GetU32(offset, &p_flags, 1) == NULL)
392ac7ddfbfSEd Maste return false;
393ac7ddfbfSEd Maste
394ac7ddfbfSEd Maste // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
395*b5893f02SDimitry Andric if (!GetMaxU64(data, offset, &p_offset, byte_size, 6))
396ac7ddfbfSEd Maste return false;
397ac7ddfbfSEd Maste }
398ac7ddfbfSEd Maste
399ac7ddfbfSEd Maste return true;
400ac7ddfbfSEd Maste }
401ac7ddfbfSEd Maste
402ac7ddfbfSEd Maste //------------------------------------------------------------------------------
403ac7ddfbfSEd Maste // ELFDynamic
404ac7ddfbfSEd Maste
ELFDynamic()405435933ddSDimitry Andric ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); }
406ac7ddfbfSEd Maste
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)407435933ddSDimitry Andric bool ELFDynamic::Parse(const lldb_private::DataExtractor &data,
408435933ddSDimitry Andric lldb::offset_t *offset) {
409ac7ddfbfSEd Maste const unsigned byte_size = data.GetAddressByteSize();
410ac7ddfbfSEd Maste return GetMaxS64(data, offset, &d_tag, byte_size, 2);
411ac7ddfbfSEd Maste }
412ac7ddfbfSEd Maste
413ac7ddfbfSEd Maste //------------------------------------------------------------------------------
414ac7ddfbfSEd Maste // ELFRel
415ac7ddfbfSEd Maste
ELFRel()416435933ddSDimitry Andric ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
417ac7ddfbfSEd Maste
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)418435933ddSDimitry Andric bool ELFRel::Parse(const lldb_private::DataExtractor &data,
419435933ddSDimitry Andric lldb::offset_t *offset) {
420ac7ddfbfSEd Maste const unsigned byte_size = data.GetAddressByteSize();
421ac7ddfbfSEd Maste
422ac7ddfbfSEd Maste // Read r_offset and r_info.
423*b5893f02SDimitry Andric return GetMaxU64(data, offset, &r_offset, byte_size, 2) != false;
424ac7ddfbfSEd Maste }
425ac7ddfbfSEd Maste
426ac7ddfbfSEd Maste //------------------------------------------------------------------------------
427ac7ddfbfSEd Maste // ELFRela
428ac7ddfbfSEd Maste
ELFRela()429435933ddSDimitry Andric ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
430ac7ddfbfSEd Maste
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)431435933ddSDimitry Andric bool ELFRela::Parse(const lldb_private::DataExtractor &data,
432435933ddSDimitry Andric lldb::offset_t *offset) {
433ac7ddfbfSEd Maste const unsigned byte_size = data.GetAddressByteSize();
434ac7ddfbfSEd Maste
435ac7ddfbfSEd Maste // Read r_offset and r_info.
436*b5893f02SDimitry Andric if (!GetMaxU64(data, offset, &r_offset, byte_size, 2))
437ac7ddfbfSEd Maste return false;
438ac7ddfbfSEd Maste
439ac7ddfbfSEd Maste // Read r_addend;
440*b5893f02SDimitry Andric if (!GetMaxS64(data, offset, &r_addend, byte_size))
441ac7ddfbfSEd Maste return false;
442ac7ddfbfSEd Maste
443ac7ddfbfSEd Maste return true;
444ac7ddfbfSEd Maste }
445