blob: f0496beba2ef461e747a8002588f532759bc28e8 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- ELFHeader.cpp -----------------------------------------------------===//
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Stephen Wilsonf325ba92010-07-13 23:07:23 +00006//
7//===----------------------------------------------------------------------===//
8
9#include <cstring>
10
Greg Clayton9594f4c2013-04-13 23:17:23 +000011#include "lldb/Core/Section.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000012#include "lldb/Utility/DataExtractor.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000013#include "lldb/Utility/Stream.h"
Stephen Wilsonf325ba92010-07-13 23:07:23 +000014
15#include "ELFHeader.h"
16
17using namespace elf;
18using namespace lldb;
19using namespace llvm::ELF;
20
Stephen Wilsonf325ba92010-07-13 23:07:23 +000021// Static utility functions.
22//
23// GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor
24// with error handling code and provide for parsing a sequence of values.
Kate Stoneb9c1b512016-09-06 20:57:50 +000025static bool GetMaxU64(const lldb_private::DataExtractor &data,
26 lldb::offset_t *offset, uint64_t *value,
27 uint32_t byte_size) {
28 const lldb::offset_t saved_offset = *offset;
29 *value = data.GetMaxU64(offset, byte_size);
30 return *offset != saved_offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000031}
32
Kate Stoneb9c1b512016-09-06 20:57:50 +000033static bool GetMaxU64(const lldb_private::DataExtractor &data,
34 lldb::offset_t *offset, uint64_t *value,
35 uint32_t byte_size, uint32_t count) {
36 lldb::offset_t saved_offset = *offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000037
Kate Stoneb9c1b512016-09-06 20:57:50 +000038 for (uint32_t i = 0; i < count; ++i, ++value) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +000039 if (!GetMaxU64(data, offset, value, byte_size)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 *offset = saved_offset;
41 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000042 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 }
44 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000045}
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047static bool GetMaxS64(const lldb_private::DataExtractor &data,
48 lldb::offset_t *offset, int64_t *value,
49 uint32_t byte_size) {
50 const lldb::offset_t saved_offset = *offset;
51 *value = data.GetMaxS64(offset, byte_size);
52 return *offset != saved_offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000053}
54
Kate Stoneb9c1b512016-09-06 20:57:50 +000055static bool GetMaxS64(const lldb_private::DataExtractor &data,
56 lldb::offset_t *offset, int64_t *value,
57 uint32_t byte_size, uint32_t count) {
58 lldb::offset_t saved_offset = *offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 for (uint32_t i = 0; i < count; ++i, ++value) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +000061 if (!GetMaxS64(data, offset, value, byte_size)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 *offset = saved_offset;
63 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000064 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 }
66 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000067}
68
Stephen Wilsonf325ba92010-07-13 23:07:23 +000069// ELFHeader
70
Kate Stoneb9c1b512016-09-06 20:57:50 +000071ELFHeader::ELFHeader() { memset(this, 0, sizeof(ELFHeader)); }
72
73ByteOrder ELFHeader::GetByteOrder() const {
74 if (e_ident[EI_DATA] == ELFDATA2MSB)
75 return eByteOrderBig;
76 if (e_ident[EI_DATA] == ELFDATA2LSB)
77 return eByteOrderLittle;
78 return eByteOrderInvalid;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000079}
80
Pavel Labath23ccc292017-01-31 23:09:46 +000081bool ELFHeader::HasHeaderExtension() const {
82 bool result = false;
83
84 // Check if any of these values looks like sentinel.
85 result |= e_phnum_hdr == 0xFFFF; // PN_XNUM
86 result |= e_shnum_hdr == SHN_UNDEF;
87 result |= e_shstrndx_hdr == SHN_XINDEX;
88
89 // If header extension is present, the section offset cannot be null.
90 result &= e_shoff != 0;
91
92 // Done.
93 return result;
94}
95
96void ELFHeader::ParseHeaderExtension(lldb_private::DataExtractor &data) {
97 // Extract section #0 header.
98 ELFSectionHeader section_zero;
99 lldb::offset_t offset = 0;
100 lldb_private::DataExtractor sh_data(data, e_shoff, e_shentsize);
101 bool ok = section_zero.Parse(sh_data, &offset);
102
103 // If we succeeded, fix the header.
104 if (ok) {
105 if (e_phnum_hdr == 0xFFFF) // PN_XNUM
106 e_phnum = section_zero.sh_info;
107 if (e_shnum_hdr == SHN_UNDEF)
108 e_shnum = section_zero.sh_size;
109 if (e_shstrndx_hdr == SHN_XINDEX)
110 e_shstrndx = section_zero.sh_link;
111 }
112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114bool ELFHeader::Parse(lldb_private::DataExtractor &data,
115 lldb::offset_t *offset) {
116 // Read e_ident. This provides byte order and address size info.
Konrad Kleine248a1302019-05-23 11:14:47 +0000117 if (data.GetU8(offset, &e_ident, EI_NIDENT) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 return false;
119
120 const unsigned byte_size = Is32Bit() ? 4 : 8;
121 data.SetByteOrder(GetByteOrder());
122 data.SetAddressByteSize(byte_size);
123
124 // Read e_type and e_machine.
Konrad Kleine248a1302019-05-23 11:14:47 +0000125 if (data.GetU16(offset, &e_type, 2) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 return false;
127
128 // Read e_version.
Konrad Kleine248a1302019-05-23 11:14:47 +0000129 if (data.GetU32(offset, &e_version, 1) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 return false;
131
132 // Read e_entry, e_phoff and e_shoff.
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000133 if (!GetMaxU64(data, offset, &e_entry, byte_size, 3))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 return false;
135
136 // Read e_flags.
Konrad Kleine248a1302019-05-23 11:14:47 +0000137 if (data.GetU32(offset, &e_flags, 1) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 return false;
139
Adrian Prantl05097242018-04-30 16:49:04 +0000140 // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx.
Konrad Kleine248a1302019-05-23 11:14:47 +0000141 if (data.GetU16(offset, &e_ehsize, 6) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 return false;
143
Adrian Prantl05097242018-04-30 16:49:04 +0000144 // Initialize e_phnum, e_shnum, and e_shstrndx with the values read from the
145 // header.
Pavel Labath23ccc292017-01-31 23:09:46 +0000146 e_phnum = e_phnum_hdr;
147 e_shnum = e_shnum_hdr;
148 e_shstrndx = e_shstrndx_hdr;
149
150 // See if we have extended header in section #0.
151 if (HasHeaderExtension())
152 ParseHeaderExtension(data);
153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000155}
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157bool ELFHeader::MagicBytesMatch(const uint8_t *magic) {
158 return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000159}
160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) {
162 unsigned address_size = 0;
163
164 switch (magic[EI_CLASS]) {
165 case ELFCLASS32:
166 address_size = 4;
167 break;
168
169 case ELFCLASS64:
170 address_size = 8;
171 break;
172 }
173 return address_size;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000174}
175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176unsigned ELFHeader::GetRelocationJumpSlotType() const {
177 unsigned slot = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 switch (e_machine) {
180 default:
181 assert(false && "architecture not supported");
182 break;
183 case EM_PPC:
184 slot = R_PPC_JMP_SLOT;
185 break;
186 case EM_PPC64:
187 slot = R_PPC64_JMP_SLOT;
188 break;
189 case EM_386:
190 case EM_IAMCU: // FIXME: is this correct?
191 slot = R_386_JUMP_SLOT;
192 break;
193 case EM_X86_64:
194 slot = R_X86_64_JUMP_SLOT;
195 break;
196 case EM_ARM:
197 slot = R_ARM_JUMP_SLOT;
198 break;
199 case EM_HEXAGON:
200 slot = R_HEX_JMP_SLOT;
201 break;
202 case EM_AARCH64:
203 slot = R_AARCH64_JUMP_SLOT;
204 break;
205 case EM_MIPS:
206 slot = R_MIPS_JUMP_SLOT;
207 break;
208 case EM_S390:
209 slot = R_390_JMP_SLOT;
210 break;
211 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 return slot;
Stephen Wilson43fe6452011-03-30 15:59:12 +0000214}
215
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000216// ELFSectionHeader
217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218ELFSectionHeader::ELFSectionHeader() {
219 memset(this, 0, sizeof(ELFSectionHeader));
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000220}
221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
223 lldb::offset_t *offset) {
224 const unsigned byte_size = data.GetAddressByteSize();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 // Read sh_name and sh_type.
Konrad Kleine248a1302019-05-23 11:14:47 +0000227 if (data.GetU32(offset, &sh_name, 2) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 // Read sh_flags.
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000231 if (!GetMaxU64(data, offset, &sh_flags, byte_size))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 // Read sh_addr, sh_off and sh_size.
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000235 if (!GetMaxU64(data, offset, &sh_addr, byte_size, 3))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 // Read sh_link and sh_info.
Konrad Kleine248a1302019-05-23 11:14:47 +0000239 if (data.GetU32(offset, &sh_link, 2) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 // Read sh_addralign and sh_entsize.
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000243 if (!GetMaxU64(data, offset, &sh_addralign, byte_size, 2))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000247}
248
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000249// ELFSymbol
250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253#define ENUM_TO_CSTR(e) \
254 case e: \
255 return #e
Greg Clayton9594f4c2013-04-13 23:17:23 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257const char *ELFSymbol::bindingToCString(unsigned char binding) {
258 switch (binding) {
Greg Clayton9594f4c2013-04-13 23:17:23 +0000259 ENUM_TO_CSTR(STB_LOCAL);
260 ENUM_TO_CSTR(STB_GLOBAL);
261 ENUM_TO_CSTR(STB_WEAK);
262 ENUM_TO_CSTR(STB_LOOS);
263 ENUM_TO_CSTR(STB_HIOS);
264 ENUM_TO_CSTR(STB_LOPROC);
265 ENUM_TO_CSTR(STB_HIPROC);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 }
267 return "";
Greg Clayton9594f4c2013-04-13 23:17:23 +0000268}
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270const char *ELFSymbol::typeToCString(unsigned char type) {
271 switch (type) {
Greg Clayton9594f4c2013-04-13 23:17:23 +0000272 ENUM_TO_CSTR(STT_NOTYPE);
273 ENUM_TO_CSTR(STT_OBJECT);
274 ENUM_TO_CSTR(STT_FUNC);
275 ENUM_TO_CSTR(STT_SECTION);
276 ENUM_TO_CSTR(STT_FILE);
277 ENUM_TO_CSTR(STT_COMMON);
278 ENUM_TO_CSTR(STT_TLS);
Greg Clayton9594f4c2013-04-13 23:17:23 +0000279 ENUM_TO_CSTR(STT_GNU_IFUNC);
Pavel Labatha6d0dd72015-06-23 17:15:14 +0000280 ENUM_TO_CSTR(STT_HIOS);
Greg Clayton9594f4c2013-04-13 23:17:23 +0000281 ENUM_TO_CSTR(STT_LOPROC);
282 ENUM_TO_CSTR(STT_HIPROC);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 }
284 return "";
Greg Clayton9594f4c2013-04-13 23:17:23 +0000285}
286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287const char *ELFSymbol::sectionIndexToCString(
288 elf_half shndx, const lldb_private::SectionList *section_list) {
289 switch (shndx) {
Greg Clayton9594f4c2013-04-13 23:17:23 +0000290 ENUM_TO_CSTR(SHN_UNDEF);
291 ENUM_TO_CSTR(SHN_LOPROC);
292 ENUM_TO_CSTR(SHN_HIPROC);
293 ENUM_TO_CSTR(SHN_LOOS);
294 ENUM_TO_CSTR(SHN_HIOS);
295 ENUM_TO_CSTR(SHN_ABS);
296 ENUM_TO_CSTR(SHN_COMMON);
297 ENUM_TO_CSTR(SHN_XINDEX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 default: {
299 const lldb_private::Section *section =
300 section_list->GetSectionAtIndex(shndx).get();
301 if (section)
302 return section->GetName().AsCString("");
303 } break;
304 }
305 return "";
Greg Clayton9594f4c2013-04-13 23:17:23 +0000306}
307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx,
309 const lldb_private::DataExtractor *strtab_data,
310 const lldb_private::SectionList *section_list) {
311 s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64
312 " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
313 idx, st_value, st_size, st_name, st_info,
314 bindingToCString(getBinding()), typeToCString(getType()), st_other,
315 st_shndx, sectionIndexToCString(st_shndx, section_list),
316 strtab_data ? strtab_data->PeekCStr(st_name) : "");
Greg Clayton9594f4c2013-04-13 23:17:23 +0000317}
318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319bool ELFSymbol::Parse(const lldb_private::DataExtractor &data,
320 lldb::offset_t *offset) {
321 const unsigned byte_size = data.GetAddressByteSize();
322 const bool parsing_32 = byte_size == 4;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 // Read st_name.
Konrad Kleine248a1302019-05-23 11:14:47 +0000325 if (data.GetU32(offset, &st_name, 1) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 if (parsing_32) {
329 // Read st_value and st_size.
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000330 if (!GetMaxU64(data, offset, &st_value, byte_size, 2))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 // Read st_info and st_other.
Konrad Kleine248a1302019-05-23 11:14:47 +0000334 if (data.GetU8(offset, &st_info, 2) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000336
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337 // Read st_shndx.
Konrad Kleine248a1302019-05-23 11:14:47 +0000338 if (data.GetU16(offset, &st_shndx, 1) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 return false;
340 } else {
341 // Read st_info and st_other.
Konrad Kleine248a1302019-05-23 11:14:47 +0000342 if (data.GetU8(offset, &st_info, 2) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 return false;
344
345 // Read st_shndx.
Konrad Kleine248a1302019-05-23 11:14:47 +0000346 if (data.GetU16(offset, &st_shndx, 1) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 return false;
348
349 // Read st_value and st_size.
Konrad Kleine248a1302019-05-23 11:14:47 +0000350 if (data.GetU64(offset, &st_value, 2) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 return false;
352 }
353 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000354}
355
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000356// ELFProgramHeader
357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358ELFProgramHeader::ELFProgramHeader() {
359 memset(this, 0, sizeof(ELFProgramHeader));
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000360}
361
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
363 lldb::offset_t *offset) {
364 const uint32_t byte_size = data.GetAddressByteSize();
365 const bool parsing_32 = byte_size == 4;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 // Read p_type;
Konrad Kleine248a1302019-05-23 11:14:47 +0000368 if (data.GetU32(offset, &p_type, 1) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000370
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 if (parsing_32) {
372 // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000373 if (!GetMaxU64(data, offset, &p_offset, byte_size, 5))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000375
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 // Read p_flags.
Konrad Kleine248a1302019-05-23 11:14:47 +0000377 if (data.GetU32(offset, &p_flags, 1) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 // Read p_align.
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000381 if (!GetMaxU64(data, offset, &p_align, byte_size))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 return false;
383 } else {
384 // Read p_flags.
Konrad Kleine248a1302019-05-23 11:14:47 +0000385 if (data.GetU32(offset, &p_flags, 1) == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000389 if (!GetMaxU64(data, offset, &p_offset, byte_size, 6))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 return false;
391 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000394}
395
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000396// ELFDynamic
397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400bool ELFDynamic::Parse(const lldb_private::DataExtractor &data,
401 lldb::offset_t *offset) {
402 const unsigned byte_size = data.GetAddressByteSize();
403 return GetMaxS64(data, offset, &d_tag, byte_size, 2);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000404}
405
Stephen Wilson43fe6452011-03-30 15:59:12 +0000406// ELFRel
407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000409
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410bool ELFRel::Parse(const lldb_private::DataExtractor &data,
411 lldb::offset_t *offset) {
412 const unsigned byte_size = data.GetAddressByteSize();
Stephen Wilson43fe6452011-03-30 15:59:12 +0000413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 // Read r_offset and r_info.
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000415 return GetMaxU64(data, offset, &r_offset, byte_size, 2) != false;
Stephen Wilson43fe6452011-03-30 15:59:12 +0000416}
417
Stephen Wilson43fe6452011-03-30 15:59:12 +0000418// ELFRela
419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
421
422bool ELFRela::Parse(const lldb_private::DataExtractor &data,
423 lldb::offset_t *offset) {
424 const unsigned byte_size = data.GetAddressByteSize();
425
426 // Read r_offset and r_info.
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000427 if (!GetMaxU64(data, offset, &r_offset, byte_size, 2))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 return false;
429
430 // Read r_addend;
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000431 if (!GetMaxS64(data, offset, &r_addend, byte_size))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 return false;
433
434 return true;
Stephen Wilson43fe6452011-03-30 15:59:12 +0000435}