1*0b57cec5SDimitry Andric //===-- ELFHeader.cpp -----------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include <cstring>
10*0b57cec5SDimitry Andric
11*0b57cec5SDimitry Andric #include "lldb/Core/Section.h"
12*0b57cec5SDimitry Andric #include "lldb/Utility/DataExtractor.h"
13*0b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
14*0b57cec5SDimitry Andric
15*0b57cec5SDimitry Andric #include "ELFHeader.h"
16*0b57cec5SDimitry Andric
17*0b57cec5SDimitry Andric using namespace elf;
18*0b57cec5SDimitry Andric using namespace lldb;
19*0b57cec5SDimitry Andric using namespace llvm::ELF;
20*0b57cec5SDimitry Andric
21*0b57cec5SDimitry Andric // Static utility functions.
22*0b57cec5SDimitry Andric //
23*0b57cec5SDimitry Andric // GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor
24*0b57cec5SDimitry Andric // 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)25*0b57cec5SDimitry Andric static bool GetMaxU64(const lldb_private::DataExtractor &data,
26*0b57cec5SDimitry Andric lldb::offset_t *offset, uint64_t *value,
27*0b57cec5SDimitry Andric uint32_t byte_size) {
28*0b57cec5SDimitry Andric const lldb::offset_t saved_offset = *offset;
29*0b57cec5SDimitry Andric *value = data.GetMaxU64(offset, byte_size);
30*0b57cec5SDimitry Andric return *offset != saved_offset;
31*0b57cec5SDimitry Andric }
32*0b57cec5SDimitry Andric
GetMaxU64(const lldb_private::DataExtractor & data,lldb::offset_t * offset,uint64_t * value,uint32_t byte_size,uint32_t count)33*0b57cec5SDimitry Andric static bool GetMaxU64(const lldb_private::DataExtractor &data,
34*0b57cec5SDimitry Andric lldb::offset_t *offset, uint64_t *value,
35*0b57cec5SDimitry Andric uint32_t byte_size, uint32_t count) {
36*0b57cec5SDimitry Andric lldb::offset_t saved_offset = *offset;
37*0b57cec5SDimitry Andric
38*0b57cec5SDimitry Andric for (uint32_t i = 0; i < count; ++i, ++value) {
39*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, value, byte_size)) {
40*0b57cec5SDimitry Andric *offset = saved_offset;
41*0b57cec5SDimitry Andric return false;
42*0b57cec5SDimitry Andric }
43*0b57cec5SDimitry Andric }
44*0b57cec5SDimitry Andric return true;
45*0b57cec5SDimitry Andric }
46*0b57cec5SDimitry Andric
GetMaxS64(const lldb_private::DataExtractor & data,lldb::offset_t * offset,int64_t * value,uint32_t byte_size)47*0b57cec5SDimitry Andric static bool GetMaxS64(const lldb_private::DataExtractor &data,
48*0b57cec5SDimitry Andric lldb::offset_t *offset, int64_t *value,
49*0b57cec5SDimitry Andric uint32_t byte_size) {
50*0b57cec5SDimitry Andric const lldb::offset_t saved_offset = *offset;
51*0b57cec5SDimitry Andric *value = data.GetMaxS64(offset, byte_size);
52*0b57cec5SDimitry Andric return *offset != saved_offset;
53*0b57cec5SDimitry Andric }
54*0b57cec5SDimitry Andric
GetMaxS64(const lldb_private::DataExtractor & data,lldb::offset_t * offset,int64_t * value,uint32_t byte_size,uint32_t count)55*0b57cec5SDimitry Andric static bool GetMaxS64(const lldb_private::DataExtractor &data,
56*0b57cec5SDimitry Andric lldb::offset_t *offset, int64_t *value,
57*0b57cec5SDimitry Andric uint32_t byte_size, uint32_t count) {
58*0b57cec5SDimitry Andric lldb::offset_t saved_offset = *offset;
59*0b57cec5SDimitry Andric
60*0b57cec5SDimitry Andric for (uint32_t i = 0; i < count; ++i, ++value) {
61*0b57cec5SDimitry Andric if (!GetMaxS64(data, offset, value, byte_size)) {
62*0b57cec5SDimitry Andric *offset = saved_offset;
63*0b57cec5SDimitry Andric return false;
64*0b57cec5SDimitry Andric }
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric return true;
67*0b57cec5SDimitry Andric }
68*0b57cec5SDimitry Andric
69*0b57cec5SDimitry Andric // ELFHeader
70*0b57cec5SDimitry Andric
ELFHeader()71*0b57cec5SDimitry Andric ELFHeader::ELFHeader() { memset(this, 0, sizeof(ELFHeader)); }
72*0b57cec5SDimitry Andric
GetByteOrder() const73*0b57cec5SDimitry Andric ByteOrder ELFHeader::GetByteOrder() const {
74*0b57cec5SDimitry Andric if (e_ident[EI_DATA] == ELFDATA2MSB)
75*0b57cec5SDimitry Andric return eByteOrderBig;
76*0b57cec5SDimitry Andric if (e_ident[EI_DATA] == ELFDATA2LSB)
77*0b57cec5SDimitry Andric return eByteOrderLittle;
78*0b57cec5SDimitry Andric return eByteOrderInvalid;
79*0b57cec5SDimitry Andric }
80*0b57cec5SDimitry Andric
HasHeaderExtension() const81*0b57cec5SDimitry Andric bool ELFHeader::HasHeaderExtension() const {
82*0b57cec5SDimitry Andric bool result = false;
83*0b57cec5SDimitry Andric
84*0b57cec5SDimitry Andric // Check if any of these values looks like sentinel.
85*0b57cec5SDimitry Andric result |= e_phnum_hdr == 0xFFFF; // PN_XNUM
86*0b57cec5SDimitry Andric result |= e_shnum_hdr == SHN_UNDEF;
87*0b57cec5SDimitry Andric result |= e_shstrndx_hdr == SHN_XINDEX;
88*0b57cec5SDimitry Andric
89*0b57cec5SDimitry Andric // If header extension is present, the section offset cannot be null.
90*0b57cec5SDimitry Andric result &= e_shoff != 0;
91*0b57cec5SDimitry Andric
92*0b57cec5SDimitry Andric // Done.
93*0b57cec5SDimitry Andric return result;
94*0b57cec5SDimitry Andric }
95*0b57cec5SDimitry Andric
ParseHeaderExtension(lldb_private::DataExtractor & data)96*0b57cec5SDimitry Andric void ELFHeader::ParseHeaderExtension(lldb_private::DataExtractor &data) {
97*0b57cec5SDimitry Andric // Extract section #0 header.
98*0b57cec5SDimitry Andric ELFSectionHeader section_zero;
99*0b57cec5SDimitry Andric lldb::offset_t offset = 0;
100*0b57cec5SDimitry Andric lldb_private::DataExtractor sh_data(data, e_shoff, e_shentsize);
101*0b57cec5SDimitry Andric bool ok = section_zero.Parse(sh_data, &offset);
102*0b57cec5SDimitry Andric
103*0b57cec5SDimitry Andric // If we succeeded, fix the header.
104*0b57cec5SDimitry Andric if (ok) {
105*0b57cec5SDimitry Andric if (e_phnum_hdr == 0xFFFF) // PN_XNUM
106*0b57cec5SDimitry Andric e_phnum = section_zero.sh_info;
107*0b57cec5SDimitry Andric if (e_shnum_hdr == SHN_UNDEF)
108*0b57cec5SDimitry Andric e_shnum = section_zero.sh_size;
109*0b57cec5SDimitry Andric if (e_shstrndx_hdr == SHN_XINDEX)
110*0b57cec5SDimitry Andric e_shstrndx = section_zero.sh_link;
111*0b57cec5SDimitry Andric }
112*0b57cec5SDimitry Andric }
113*0b57cec5SDimitry Andric
Parse(lldb_private::DataExtractor & data,lldb::offset_t * offset)114*0b57cec5SDimitry Andric bool ELFHeader::Parse(lldb_private::DataExtractor &data,
115*0b57cec5SDimitry Andric lldb::offset_t *offset) {
116*0b57cec5SDimitry Andric // Read e_ident. This provides byte order and address size info.
117*0b57cec5SDimitry Andric if (data.GetU8(offset, &e_ident, EI_NIDENT) == nullptr)
118*0b57cec5SDimitry Andric return false;
119*0b57cec5SDimitry Andric
120*0b57cec5SDimitry Andric const unsigned byte_size = Is32Bit() ? 4 : 8;
121*0b57cec5SDimitry Andric data.SetByteOrder(GetByteOrder());
122*0b57cec5SDimitry Andric data.SetAddressByteSize(byte_size);
123*0b57cec5SDimitry Andric
124*0b57cec5SDimitry Andric // Read e_type and e_machine.
125*0b57cec5SDimitry Andric if (data.GetU16(offset, &e_type, 2) == nullptr)
126*0b57cec5SDimitry Andric return false;
127*0b57cec5SDimitry Andric
128*0b57cec5SDimitry Andric // Read e_version.
129*0b57cec5SDimitry Andric if (data.GetU32(offset, &e_version, 1) == nullptr)
130*0b57cec5SDimitry Andric return false;
131*0b57cec5SDimitry Andric
132*0b57cec5SDimitry Andric // Read e_entry, e_phoff and e_shoff.
133*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &e_entry, byte_size, 3))
134*0b57cec5SDimitry Andric return false;
135*0b57cec5SDimitry Andric
136*0b57cec5SDimitry Andric // Read e_flags.
137*0b57cec5SDimitry Andric if (data.GetU32(offset, &e_flags, 1) == nullptr)
138*0b57cec5SDimitry Andric return false;
139*0b57cec5SDimitry Andric
140*0b57cec5SDimitry Andric // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx.
141*0b57cec5SDimitry Andric if (data.GetU16(offset, &e_ehsize, 6) == nullptr)
142*0b57cec5SDimitry Andric return false;
143*0b57cec5SDimitry Andric
144*0b57cec5SDimitry Andric // Initialize e_phnum, e_shnum, and e_shstrndx with the values read from the
145*0b57cec5SDimitry Andric // header.
146*0b57cec5SDimitry Andric e_phnum = e_phnum_hdr;
147*0b57cec5SDimitry Andric e_shnum = e_shnum_hdr;
148*0b57cec5SDimitry Andric e_shstrndx = e_shstrndx_hdr;
149*0b57cec5SDimitry Andric
150*0b57cec5SDimitry Andric // See if we have extended header in section #0.
151*0b57cec5SDimitry Andric if (HasHeaderExtension())
152*0b57cec5SDimitry Andric ParseHeaderExtension(data);
153*0b57cec5SDimitry Andric
154*0b57cec5SDimitry Andric return true;
155*0b57cec5SDimitry Andric }
156*0b57cec5SDimitry Andric
MagicBytesMatch(const uint8_t * magic)157*0b57cec5SDimitry Andric bool ELFHeader::MagicBytesMatch(const uint8_t *magic) {
158*0b57cec5SDimitry Andric return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
159*0b57cec5SDimitry Andric }
160*0b57cec5SDimitry Andric
AddressSizeInBytes(const uint8_t * magic)161*0b57cec5SDimitry Andric unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) {
162*0b57cec5SDimitry Andric unsigned address_size = 0;
163*0b57cec5SDimitry Andric
164*0b57cec5SDimitry Andric switch (magic[EI_CLASS]) {
165*0b57cec5SDimitry Andric case ELFCLASS32:
166*0b57cec5SDimitry Andric address_size = 4;
167*0b57cec5SDimitry Andric break;
168*0b57cec5SDimitry Andric
169*0b57cec5SDimitry Andric case ELFCLASS64:
170*0b57cec5SDimitry Andric address_size = 8;
171*0b57cec5SDimitry Andric break;
172*0b57cec5SDimitry Andric }
173*0b57cec5SDimitry Andric return address_size;
174*0b57cec5SDimitry Andric }
175*0b57cec5SDimitry Andric
GetRelocationJumpSlotType() const176*0b57cec5SDimitry Andric unsigned ELFHeader::GetRelocationJumpSlotType() const {
177*0b57cec5SDimitry Andric unsigned slot = 0;
178*0b57cec5SDimitry Andric
179*0b57cec5SDimitry Andric switch (e_machine) {
180*0b57cec5SDimitry Andric default:
181*0b57cec5SDimitry Andric assert(false && "architecture not supported");
182*0b57cec5SDimitry Andric break;
183*0b57cec5SDimitry Andric case EM_PPC:
184*0b57cec5SDimitry Andric slot = R_PPC_JMP_SLOT;
185*0b57cec5SDimitry Andric break;
186*0b57cec5SDimitry Andric case EM_PPC64:
187*0b57cec5SDimitry Andric slot = R_PPC64_JMP_SLOT;
188*0b57cec5SDimitry Andric break;
189*0b57cec5SDimitry Andric case EM_386:
190*0b57cec5SDimitry Andric case EM_IAMCU: // FIXME: is this correct?
191*0b57cec5SDimitry Andric slot = R_386_JUMP_SLOT;
192*0b57cec5SDimitry Andric break;
193*0b57cec5SDimitry Andric case EM_X86_64:
194*0b57cec5SDimitry Andric slot = R_X86_64_JUMP_SLOT;
195*0b57cec5SDimitry Andric break;
196*0b57cec5SDimitry Andric case EM_ARM:
197*0b57cec5SDimitry Andric slot = R_ARM_JUMP_SLOT;
198*0b57cec5SDimitry Andric break;
199*0b57cec5SDimitry Andric case EM_HEXAGON:
200*0b57cec5SDimitry Andric slot = R_HEX_JMP_SLOT;
201*0b57cec5SDimitry Andric break;
202*0b57cec5SDimitry Andric case EM_AARCH64:
203*0b57cec5SDimitry Andric slot = R_AARCH64_JUMP_SLOT;
204*0b57cec5SDimitry Andric break;
205*0b57cec5SDimitry Andric case EM_MIPS:
206*0b57cec5SDimitry Andric slot = R_MIPS_JUMP_SLOT;
207*0b57cec5SDimitry Andric break;
208*0b57cec5SDimitry Andric case EM_S390:
209*0b57cec5SDimitry Andric slot = R_390_JMP_SLOT;
210*0b57cec5SDimitry Andric break;
211*0b57cec5SDimitry Andric }
212*0b57cec5SDimitry Andric
213*0b57cec5SDimitry Andric return slot;
214*0b57cec5SDimitry Andric }
215*0b57cec5SDimitry Andric
216*0b57cec5SDimitry Andric // ELFSectionHeader
217*0b57cec5SDimitry Andric
ELFSectionHeader()218*0b57cec5SDimitry Andric ELFSectionHeader::ELFSectionHeader() {
219*0b57cec5SDimitry Andric memset(this, 0, sizeof(ELFSectionHeader));
220*0b57cec5SDimitry Andric }
221*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)222*0b57cec5SDimitry Andric bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
223*0b57cec5SDimitry Andric lldb::offset_t *offset) {
224*0b57cec5SDimitry Andric const unsigned byte_size = data.GetAddressByteSize();
225*0b57cec5SDimitry Andric
226*0b57cec5SDimitry Andric // Read sh_name and sh_type.
227*0b57cec5SDimitry Andric if (data.GetU32(offset, &sh_name, 2) == nullptr)
228*0b57cec5SDimitry Andric return false;
229*0b57cec5SDimitry Andric
230*0b57cec5SDimitry Andric // Read sh_flags.
231*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &sh_flags, byte_size))
232*0b57cec5SDimitry Andric return false;
233*0b57cec5SDimitry Andric
234*0b57cec5SDimitry Andric // Read sh_addr, sh_off and sh_size.
235*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &sh_addr, byte_size, 3))
236*0b57cec5SDimitry Andric return false;
237*0b57cec5SDimitry Andric
238*0b57cec5SDimitry Andric // Read sh_link and sh_info.
239*0b57cec5SDimitry Andric if (data.GetU32(offset, &sh_link, 2) == nullptr)
240*0b57cec5SDimitry Andric return false;
241*0b57cec5SDimitry Andric
242*0b57cec5SDimitry Andric // Read sh_addralign and sh_entsize.
243*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &sh_addralign, byte_size, 2))
244*0b57cec5SDimitry Andric return false;
245*0b57cec5SDimitry Andric
246*0b57cec5SDimitry Andric return true;
247*0b57cec5SDimitry Andric }
248*0b57cec5SDimitry Andric
249*0b57cec5SDimitry Andric // ELFSymbol
250*0b57cec5SDimitry Andric
ELFSymbol()251*0b57cec5SDimitry Andric ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); }
252*0b57cec5SDimitry Andric
253*0b57cec5SDimitry Andric #define ENUM_TO_CSTR(e) \
254*0b57cec5SDimitry Andric case e: \
255*0b57cec5SDimitry Andric return #e
256*0b57cec5SDimitry Andric
bindingToCString(unsigned char binding)257*0b57cec5SDimitry Andric const char *ELFSymbol::bindingToCString(unsigned char binding) {
258*0b57cec5SDimitry Andric switch (binding) {
259*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_LOCAL);
260*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_GLOBAL);
261*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_WEAK);
262*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_LOOS);
263*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_HIOS);
264*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_LOPROC);
265*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_HIPROC);
266*0b57cec5SDimitry Andric }
267*0b57cec5SDimitry Andric return "";
268*0b57cec5SDimitry Andric }
269*0b57cec5SDimitry Andric
typeToCString(unsigned char type)270*0b57cec5SDimitry Andric const char *ELFSymbol::typeToCString(unsigned char type) {
271*0b57cec5SDimitry Andric switch (type) {
272*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_NOTYPE);
273*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_OBJECT);
274*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_FUNC);
275*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_SECTION);
276*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_FILE);
277*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_COMMON);
278*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_TLS);
279*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_GNU_IFUNC);
280*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_HIOS);
281*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_LOPROC);
282*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_HIPROC);
283*0b57cec5SDimitry Andric }
284*0b57cec5SDimitry Andric return "";
285*0b57cec5SDimitry Andric }
286*0b57cec5SDimitry Andric
sectionIndexToCString(elf_half shndx,const lldb_private::SectionList * section_list)287*0b57cec5SDimitry Andric const char *ELFSymbol::sectionIndexToCString(
288*0b57cec5SDimitry Andric elf_half shndx, const lldb_private::SectionList *section_list) {
289*0b57cec5SDimitry Andric switch (shndx) {
290*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_UNDEF);
291*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_LOPROC);
292*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_HIPROC);
293*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_LOOS);
294*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_HIOS);
295*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_ABS);
296*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_COMMON);
297*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_XINDEX);
298*0b57cec5SDimitry Andric default: {
299*0b57cec5SDimitry Andric const lldb_private::Section *section =
300*0b57cec5SDimitry Andric section_list->GetSectionAtIndex(shndx).get();
301*0b57cec5SDimitry Andric if (section)
302*0b57cec5SDimitry Andric return section->GetName().AsCString("");
303*0b57cec5SDimitry Andric } break;
304*0b57cec5SDimitry Andric }
305*0b57cec5SDimitry Andric return "";
306*0b57cec5SDimitry Andric }
307*0b57cec5SDimitry Andric
Dump(lldb_private::Stream * s,uint32_t idx,const lldb_private::DataExtractor * strtab_data,const lldb_private::SectionList * section_list)308*0b57cec5SDimitry Andric void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx,
309*0b57cec5SDimitry Andric const lldb_private::DataExtractor *strtab_data,
310*0b57cec5SDimitry Andric const lldb_private::SectionList *section_list) {
311*0b57cec5SDimitry Andric s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64
312*0b57cec5SDimitry Andric " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
313*0b57cec5SDimitry Andric idx, st_value, st_size, st_name, st_info,
314*0b57cec5SDimitry Andric bindingToCString(getBinding()), typeToCString(getType()), st_other,
315*0b57cec5SDimitry Andric st_shndx, sectionIndexToCString(st_shndx, section_list),
316*0b57cec5SDimitry Andric strtab_data ? strtab_data->PeekCStr(st_name) : "");
317*0b57cec5SDimitry Andric }
318*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)319*0b57cec5SDimitry Andric bool ELFSymbol::Parse(const lldb_private::DataExtractor &data,
320*0b57cec5SDimitry Andric lldb::offset_t *offset) {
321*0b57cec5SDimitry Andric const unsigned byte_size = data.GetAddressByteSize();
322*0b57cec5SDimitry Andric const bool parsing_32 = byte_size == 4;
323*0b57cec5SDimitry Andric
324*0b57cec5SDimitry Andric // Read st_name.
325*0b57cec5SDimitry Andric if (data.GetU32(offset, &st_name, 1) == nullptr)
326*0b57cec5SDimitry Andric return false;
327*0b57cec5SDimitry Andric
328*0b57cec5SDimitry Andric if (parsing_32) {
329*0b57cec5SDimitry Andric // Read st_value and st_size.
330*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &st_value, byte_size, 2))
331*0b57cec5SDimitry Andric return false;
332*0b57cec5SDimitry Andric
333*0b57cec5SDimitry Andric // Read st_info and st_other.
334*0b57cec5SDimitry Andric if (data.GetU8(offset, &st_info, 2) == nullptr)
335*0b57cec5SDimitry Andric return false;
336*0b57cec5SDimitry Andric
337*0b57cec5SDimitry Andric // Read st_shndx.
338*0b57cec5SDimitry Andric if (data.GetU16(offset, &st_shndx, 1) == nullptr)
339*0b57cec5SDimitry Andric return false;
340*0b57cec5SDimitry Andric } else {
341*0b57cec5SDimitry Andric // Read st_info and st_other.
342*0b57cec5SDimitry Andric if (data.GetU8(offset, &st_info, 2) == nullptr)
343*0b57cec5SDimitry Andric return false;
344*0b57cec5SDimitry Andric
345*0b57cec5SDimitry Andric // Read st_shndx.
346*0b57cec5SDimitry Andric if (data.GetU16(offset, &st_shndx, 1) == nullptr)
347*0b57cec5SDimitry Andric return false;
348*0b57cec5SDimitry Andric
349*0b57cec5SDimitry Andric // Read st_value and st_size.
350*0b57cec5SDimitry Andric if (data.GetU64(offset, &st_value, 2) == nullptr)
351*0b57cec5SDimitry Andric return false;
352*0b57cec5SDimitry Andric }
353*0b57cec5SDimitry Andric return true;
354*0b57cec5SDimitry Andric }
355*0b57cec5SDimitry Andric
356*0b57cec5SDimitry Andric // ELFProgramHeader
357*0b57cec5SDimitry Andric
ELFProgramHeader()358*0b57cec5SDimitry Andric ELFProgramHeader::ELFProgramHeader() {
359*0b57cec5SDimitry Andric memset(this, 0, sizeof(ELFProgramHeader));
360*0b57cec5SDimitry Andric }
361*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)362*0b57cec5SDimitry Andric bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
363*0b57cec5SDimitry Andric lldb::offset_t *offset) {
364*0b57cec5SDimitry Andric const uint32_t byte_size = data.GetAddressByteSize();
365*0b57cec5SDimitry Andric const bool parsing_32 = byte_size == 4;
366*0b57cec5SDimitry Andric
367*0b57cec5SDimitry Andric // Read p_type;
368*0b57cec5SDimitry Andric if (data.GetU32(offset, &p_type, 1) == nullptr)
369*0b57cec5SDimitry Andric return false;
370*0b57cec5SDimitry Andric
371*0b57cec5SDimitry Andric if (parsing_32) {
372*0b57cec5SDimitry Andric // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
373*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &p_offset, byte_size, 5))
374*0b57cec5SDimitry Andric return false;
375*0b57cec5SDimitry Andric
376*0b57cec5SDimitry Andric // Read p_flags.
377*0b57cec5SDimitry Andric if (data.GetU32(offset, &p_flags, 1) == nullptr)
378*0b57cec5SDimitry Andric return false;
379*0b57cec5SDimitry Andric
380*0b57cec5SDimitry Andric // Read p_align.
381*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &p_align, byte_size))
382*0b57cec5SDimitry Andric return false;
383*0b57cec5SDimitry Andric } else {
384*0b57cec5SDimitry Andric // Read p_flags.
385*0b57cec5SDimitry Andric if (data.GetU32(offset, &p_flags, 1) == nullptr)
386*0b57cec5SDimitry Andric return false;
387*0b57cec5SDimitry Andric
388*0b57cec5SDimitry Andric // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
389*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &p_offset, byte_size, 6))
390*0b57cec5SDimitry Andric return false;
391*0b57cec5SDimitry Andric }
392*0b57cec5SDimitry Andric
393*0b57cec5SDimitry Andric return true;
394*0b57cec5SDimitry Andric }
395*0b57cec5SDimitry Andric
396*0b57cec5SDimitry Andric // ELFDynamic
397*0b57cec5SDimitry Andric
ELFDynamic()398*0b57cec5SDimitry Andric ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); }
399*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)400*0b57cec5SDimitry Andric bool ELFDynamic::Parse(const lldb_private::DataExtractor &data,
401*0b57cec5SDimitry Andric lldb::offset_t *offset) {
402*0b57cec5SDimitry Andric const unsigned byte_size = data.GetAddressByteSize();
403*0b57cec5SDimitry Andric return GetMaxS64(data, offset, &d_tag, byte_size, 2);
404*0b57cec5SDimitry Andric }
405*0b57cec5SDimitry Andric
406*0b57cec5SDimitry Andric // ELFRel
407*0b57cec5SDimitry Andric
ELFRel()408*0b57cec5SDimitry Andric ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
409*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)410*0b57cec5SDimitry Andric bool ELFRel::Parse(const lldb_private::DataExtractor &data,
411*0b57cec5SDimitry Andric lldb::offset_t *offset) {
412*0b57cec5SDimitry Andric const unsigned byte_size = data.GetAddressByteSize();
413*0b57cec5SDimitry Andric
414*0b57cec5SDimitry Andric // Read r_offset and r_info.
415*0b57cec5SDimitry Andric return GetMaxU64(data, offset, &r_offset, byte_size, 2) != false;
416*0b57cec5SDimitry Andric }
417*0b57cec5SDimitry Andric
418*0b57cec5SDimitry Andric // ELFRela
419*0b57cec5SDimitry Andric
ELFRela()420*0b57cec5SDimitry Andric ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
421*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)422*0b57cec5SDimitry Andric bool ELFRela::Parse(const lldb_private::DataExtractor &data,
423*0b57cec5SDimitry Andric lldb::offset_t *offset) {
424*0b57cec5SDimitry Andric const unsigned byte_size = data.GetAddressByteSize();
425*0b57cec5SDimitry Andric
426*0b57cec5SDimitry Andric // Read r_offset and r_info.
427*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &r_offset, byte_size, 2))
428*0b57cec5SDimitry Andric return false;
429*0b57cec5SDimitry Andric
430*0b57cec5SDimitry Andric // Read r_addend;
431*0b57cec5SDimitry Andric if (!GetMaxS64(data, offset, &r_addend, byte_size))
432*0b57cec5SDimitry Andric return false;
433*0b57cec5SDimitry Andric
434*0b57cec5SDimitry Andric return true;
435*0b57cec5SDimitry Andric }
436