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 case EM_RISCV:
212*0b57cec5SDimitry Andric slot = R_RISCV_JUMP_SLOT;
213*0b57cec5SDimitry Andric break;
214*0b57cec5SDimitry Andric case EM_LOONGARCH:
215*0b57cec5SDimitry Andric slot = R_LARCH_JUMP_SLOT;
216*0b57cec5SDimitry Andric break;
217*0b57cec5SDimitry Andric }
218*0b57cec5SDimitry Andric
219*0b57cec5SDimitry Andric return slot;
220*0b57cec5SDimitry Andric }
221*0b57cec5SDimitry Andric
222*0b57cec5SDimitry Andric // ELFSectionHeader
223*0b57cec5SDimitry Andric
ELFSectionHeader()224*0b57cec5SDimitry Andric ELFSectionHeader::ELFSectionHeader() {
225*0b57cec5SDimitry Andric memset(this, 0, sizeof(ELFSectionHeader));
226*0b57cec5SDimitry Andric }
227*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)228*0b57cec5SDimitry Andric bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
229*0b57cec5SDimitry Andric lldb::offset_t *offset) {
230*0b57cec5SDimitry Andric const unsigned byte_size = data.GetAddressByteSize();
231*0b57cec5SDimitry Andric
232*0b57cec5SDimitry Andric // Read sh_name and sh_type.
233*0b57cec5SDimitry Andric if (data.GetU32(offset, &sh_name, 2) == nullptr)
234*0b57cec5SDimitry Andric return false;
235*0b57cec5SDimitry Andric
236*0b57cec5SDimitry Andric // Read sh_flags.
237*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &sh_flags, byte_size))
238*0b57cec5SDimitry Andric return false;
239*0b57cec5SDimitry Andric
240*0b57cec5SDimitry Andric // Read sh_addr, sh_off and sh_size.
241*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &sh_addr, byte_size, 3))
242*0b57cec5SDimitry Andric return false;
243*0b57cec5SDimitry Andric
244*0b57cec5SDimitry Andric // Read sh_link and sh_info.
245*0b57cec5SDimitry Andric if (data.GetU32(offset, &sh_link, 2) == nullptr)
246*0b57cec5SDimitry Andric return false;
247*0b57cec5SDimitry Andric
248*0b57cec5SDimitry Andric // Read sh_addralign and sh_entsize.
249*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &sh_addralign, byte_size, 2))
250*0b57cec5SDimitry Andric return false;
251*0b57cec5SDimitry Andric
252*0b57cec5SDimitry Andric return true;
253*0b57cec5SDimitry Andric }
254*0b57cec5SDimitry Andric
255*0b57cec5SDimitry Andric // ELFSymbol
256*0b57cec5SDimitry Andric
ELFSymbol()257*0b57cec5SDimitry Andric ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); }
258*0b57cec5SDimitry Andric
259*0b57cec5SDimitry Andric #define ENUM_TO_CSTR(e) \
260*0b57cec5SDimitry Andric case e: \
261*0b57cec5SDimitry Andric return #e
262*0b57cec5SDimitry Andric
bindingToCString(unsigned char binding)263*0b57cec5SDimitry Andric const char *ELFSymbol::bindingToCString(unsigned char binding) {
264*0b57cec5SDimitry Andric switch (binding) {
265*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_LOCAL);
266*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_GLOBAL);
267*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_WEAK);
268*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_LOOS);
269*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_HIOS);
270*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_LOPROC);
271*0b57cec5SDimitry Andric ENUM_TO_CSTR(STB_HIPROC);
272*0b57cec5SDimitry Andric }
273*0b57cec5SDimitry Andric return "";
274*0b57cec5SDimitry Andric }
275*0b57cec5SDimitry Andric
typeToCString(unsigned char type)276*0b57cec5SDimitry Andric const char *ELFSymbol::typeToCString(unsigned char type) {
277*0b57cec5SDimitry Andric switch (type) {
278*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_NOTYPE);
279*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_OBJECT);
280*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_FUNC);
281*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_SECTION);
282*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_FILE);
283*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_COMMON);
284*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_TLS);
285*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_GNU_IFUNC);
286*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_HIOS);
287*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_LOPROC);
288*0b57cec5SDimitry Andric ENUM_TO_CSTR(STT_HIPROC);
289*0b57cec5SDimitry Andric }
290*0b57cec5SDimitry Andric return "";
291*0b57cec5SDimitry Andric }
292*0b57cec5SDimitry Andric
sectionIndexToCString(elf_half shndx,const lldb_private::SectionList * section_list)293*0b57cec5SDimitry Andric const char *ELFSymbol::sectionIndexToCString(
294*0b57cec5SDimitry Andric elf_half shndx, const lldb_private::SectionList *section_list) {
295*0b57cec5SDimitry Andric switch (shndx) {
296*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_UNDEF);
297*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_LOPROC);
298*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_HIPROC);
299*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_LOOS);
300*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_HIOS);
301*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_ABS);
302*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_COMMON);
303*0b57cec5SDimitry Andric ENUM_TO_CSTR(SHN_XINDEX);
304*0b57cec5SDimitry Andric default: {
305*0b57cec5SDimitry Andric const lldb_private::Section *section =
306*0b57cec5SDimitry Andric section_list->GetSectionAtIndex(shndx).get();
307*0b57cec5SDimitry Andric if (section)
308*0b57cec5SDimitry Andric return section->GetName().AsCString("");
309*0b57cec5SDimitry Andric } break;
310*0b57cec5SDimitry Andric }
311*0b57cec5SDimitry Andric return "";
312*0b57cec5SDimitry Andric }
313*0b57cec5SDimitry Andric
Dump(lldb_private::Stream * s,uint32_t idx,const lldb_private::DataExtractor * strtab_data,const lldb_private::SectionList * section_list)314*0b57cec5SDimitry Andric void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx,
315*0b57cec5SDimitry Andric const lldb_private::DataExtractor *strtab_data,
316*0b57cec5SDimitry Andric const lldb_private::SectionList *section_list) {
317*0b57cec5SDimitry Andric s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64
318*0b57cec5SDimitry Andric " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
319*0b57cec5SDimitry Andric idx, st_value, st_size, st_name, st_info,
320*0b57cec5SDimitry Andric bindingToCString(getBinding()), typeToCString(getType()), st_other,
321*0b57cec5SDimitry Andric st_shndx, sectionIndexToCString(st_shndx, section_list),
322*0b57cec5SDimitry Andric strtab_data ? strtab_data->PeekCStr(st_name) : "");
323*0b57cec5SDimitry Andric }
324*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)325*0b57cec5SDimitry Andric bool ELFSymbol::Parse(const lldb_private::DataExtractor &data,
326*0b57cec5SDimitry Andric lldb::offset_t *offset) {
327*0b57cec5SDimitry Andric const unsigned byte_size = data.GetAddressByteSize();
328*0b57cec5SDimitry Andric const bool parsing_32 = byte_size == 4;
329*0b57cec5SDimitry Andric
330*0b57cec5SDimitry Andric // Read st_name.
331*0b57cec5SDimitry Andric if (data.GetU32(offset, &st_name, 1) == nullptr)
332*0b57cec5SDimitry Andric return false;
333*0b57cec5SDimitry Andric
334*0b57cec5SDimitry Andric if (parsing_32) {
335*0b57cec5SDimitry Andric // Read st_value and st_size.
336*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &st_value, byte_size, 2))
337*0b57cec5SDimitry Andric return false;
338*0b57cec5SDimitry Andric
339*0b57cec5SDimitry Andric // Read st_info and st_other.
340*0b57cec5SDimitry Andric if (data.GetU8(offset, &st_info, 2) == nullptr)
341*0b57cec5SDimitry Andric return false;
342*0b57cec5SDimitry Andric
343*0b57cec5SDimitry Andric // Read st_shndx.
344*0b57cec5SDimitry Andric if (data.GetU16(offset, &st_shndx, 1) == nullptr)
345*0b57cec5SDimitry Andric return false;
346*0b57cec5SDimitry Andric } else {
347*0b57cec5SDimitry Andric // Read st_info and st_other.
348*0b57cec5SDimitry Andric if (data.GetU8(offset, &st_info, 2) == nullptr)
349*0b57cec5SDimitry Andric return false;
350*0b57cec5SDimitry Andric
351*0b57cec5SDimitry Andric // Read st_shndx.
352*0b57cec5SDimitry Andric if (data.GetU16(offset, &st_shndx, 1) == nullptr)
353*0b57cec5SDimitry Andric return false;
354*0b57cec5SDimitry Andric
355*0b57cec5SDimitry Andric // Read st_value and st_size.
356*0b57cec5SDimitry Andric if (data.GetU64(offset, &st_value, 2) == nullptr)
357*0b57cec5SDimitry Andric return false;
358*0b57cec5SDimitry Andric }
359*0b57cec5SDimitry Andric return true;
360*0b57cec5SDimitry Andric }
361*0b57cec5SDimitry Andric
362*0b57cec5SDimitry Andric // ELFProgramHeader
363*0b57cec5SDimitry Andric
ELFProgramHeader()364*0b57cec5SDimitry Andric ELFProgramHeader::ELFProgramHeader() {
365*0b57cec5SDimitry Andric memset(this, 0, sizeof(ELFProgramHeader));
366*0b57cec5SDimitry Andric }
367*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)368*0b57cec5SDimitry Andric bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
369*0b57cec5SDimitry Andric lldb::offset_t *offset) {
370*0b57cec5SDimitry Andric const uint32_t byte_size = data.GetAddressByteSize();
371*0b57cec5SDimitry Andric const bool parsing_32 = byte_size == 4;
372*0b57cec5SDimitry Andric
373*0b57cec5SDimitry Andric // Read p_type;
374*0b57cec5SDimitry Andric if (data.GetU32(offset, &p_type, 1) == nullptr)
375*0b57cec5SDimitry Andric return false;
376*0b57cec5SDimitry Andric
377*0b57cec5SDimitry Andric if (parsing_32) {
378*0b57cec5SDimitry Andric // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
379*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &p_offset, byte_size, 5))
380*0b57cec5SDimitry Andric return false;
381*0b57cec5SDimitry Andric
382*0b57cec5SDimitry Andric // Read p_flags.
383*0b57cec5SDimitry Andric if (data.GetU32(offset, &p_flags, 1) == nullptr)
384*0b57cec5SDimitry Andric return false;
385*0b57cec5SDimitry Andric
386*0b57cec5SDimitry Andric // Read p_align.
387*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &p_align, byte_size))
388*0b57cec5SDimitry Andric return false;
389*0b57cec5SDimitry Andric } else {
390*0b57cec5SDimitry Andric // Read p_flags.
391*0b57cec5SDimitry Andric if (data.GetU32(offset, &p_flags, 1) == nullptr)
392*0b57cec5SDimitry Andric return false;
393*0b57cec5SDimitry Andric
394*0b57cec5SDimitry Andric // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
395*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &p_offset, byte_size, 6))
396*0b57cec5SDimitry Andric return false;
397*0b57cec5SDimitry Andric }
398*0b57cec5SDimitry Andric
399*0b57cec5SDimitry Andric return true;
400*0b57cec5SDimitry Andric }
401*0b57cec5SDimitry Andric
402*0b57cec5SDimitry Andric // ELFDynamic
403*0b57cec5SDimitry Andric
ELFDynamic()404*0b57cec5SDimitry Andric ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); }
405*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)406*0b57cec5SDimitry Andric bool ELFDynamic::Parse(const lldb_private::DataExtractor &data,
407*0b57cec5SDimitry Andric lldb::offset_t *offset) {
408*0b57cec5SDimitry Andric const unsigned byte_size = data.GetAddressByteSize();
409*0b57cec5SDimitry Andric return GetMaxS64(data, offset, &d_tag, byte_size, 2);
410*0b57cec5SDimitry Andric }
411*0b57cec5SDimitry Andric
412*0b57cec5SDimitry Andric // ELFRel
413*0b57cec5SDimitry Andric
ELFRel()414*0b57cec5SDimitry Andric ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
415*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)416*0b57cec5SDimitry Andric bool ELFRel::Parse(const lldb_private::DataExtractor &data,
417*0b57cec5SDimitry Andric lldb::offset_t *offset) {
418*0b57cec5SDimitry Andric const unsigned byte_size = data.GetAddressByteSize();
419*0b57cec5SDimitry Andric
420*0b57cec5SDimitry Andric // Read r_offset and r_info.
421*0b57cec5SDimitry Andric return GetMaxU64(data, offset, &r_offset, byte_size, 2) != false;
422*0b57cec5SDimitry Andric }
423*0b57cec5SDimitry Andric
424*0b57cec5SDimitry Andric // ELFRela
425*0b57cec5SDimitry Andric
ELFRela()426*0b57cec5SDimitry Andric ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
427*0b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)428*0b57cec5SDimitry Andric bool ELFRela::Parse(const lldb_private::DataExtractor &data,
429*0b57cec5SDimitry Andric lldb::offset_t *offset) {
430*0b57cec5SDimitry Andric const unsigned byte_size = data.GetAddressByteSize();
431*0b57cec5SDimitry Andric
432*0b57cec5SDimitry Andric // Read r_offset and r_info.
433*0b57cec5SDimitry Andric if (!GetMaxU64(data, offset, &r_offset, byte_size, 2))
434*0b57cec5SDimitry Andric return false;
435*0b57cec5SDimitry Andric
436 // Read r_addend;
437 if (!GetMaxS64(data, offset, &r_addend, byte_size))
438 return false;
439
440 return true;
441 }
442