1 //===-- ELFHeader.cpp ----------------------------------------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "lldb/Core/Section.h"
13 #include "lldb/Utility/DataExtractor.h"
14 #include "lldb/Utility/Stream.h"
16 #include "ELFHeader.h"
20 using namespace llvm::ELF;
22 //------------------------------------------------------------------------------
23 // Static utility functions.
25 // GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor
26 // with error handling code and provide for parsing a sequence of values.
27 static bool GetMaxU64(const lldb_private::DataExtractor &data,
28 lldb::offset_t *offset, uint64_t *value,
30 const lldb::offset_t saved_offset = *offset;
31 *value = data.GetMaxU64(offset, byte_size);
32 return *offset != saved_offset;
35 static bool GetMaxU64(const lldb_private::DataExtractor &data,
36 lldb::offset_t *offset, uint64_t *value,
37 uint32_t byte_size, uint32_t count) {
38 lldb::offset_t saved_offset = *offset;
40 for (uint32_t i = 0; i < count; ++i, ++value) {
41 if (GetMaxU64(data, offset, value, byte_size) == false) {
42 *offset = saved_offset;
49 static bool GetMaxS64(const lldb_private::DataExtractor &data,
50 lldb::offset_t *offset, int64_t *value,
52 const lldb::offset_t saved_offset = *offset;
53 *value = data.GetMaxS64(offset, byte_size);
54 return *offset != saved_offset;
57 static bool GetMaxS64(const lldb_private::DataExtractor &data,
58 lldb::offset_t *offset, int64_t *value,
59 uint32_t byte_size, uint32_t count) {
60 lldb::offset_t saved_offset = *offset;
62 for (uint32_t i = 0; i < count; ++i, ++value) {
63 if (GetMaxS64(data, offset, value, byte_size) == false) {
64 *offset = saved_offset;
71 //------------------------------------------------------------------------------
74 ELFHeader::ELFHeader() { memset(this, 0, sizeof(ELFHeader)); }
76 ByteOrder ELFHeader::GetByteOrder() const {
77 if (e_ident[EI_DATA] == ELFDATA2MSB)
79 if (e_ident[EI_DATA] == ELFDATA2LSB)
80 return eByteOrderLittle;
81 return eByteOrderInvalid;
84 bool ELFHeader::HasHeaderExtension() const {
87 // Check if any of these values looks like sentinel.
88 result |= e_phnum_hdr == 0xFFFF; // PN_XNUM
89 result |= e_shnum_hdr == SHN_UNDEF;
90 result |= e_shstrndx_hdr == SHN_XINDEX;
92 // If header extension is present, the section offset cannot be null.
93 result &= e_shoff != 0;
99 void ELFHeader::ParseHeaderExtension(lldb_private::DataExtractor &data) {
100 // Extract section #0 header.
101 ELFSectionHeader section_zero;
102 lldb::offset_t offset = 0;
103 lldb_private::DataExtractor sh_data(data, e_shoff, e_shentsize);
104 bool ok = section_zero.Parse(sh_data, &offset);
106 // If we succeeded, fix the header.
108 if (e_phnum_hdr == 0xFFFF) // PN_XNUM
109 e_phnum = section_zero.sh_info;
110 if (e_shnum_hdr == SHN_UNDEF)
111 e_shnum = section_zero.sh_size;
112 if (e_shstrndx_hdr == SHN_XINDEX)
113 e_shstrndx = section_zero.sh_link;
117 bool ELFHeader::Parse(lldb_private::DataExtractor &data,
118 lldb::offset_t *offset) {
119 // Read e_ident. This provides byte order and address size info.
120 if (data.GetU8(offset, &e_ident, EI_NIDENT) == NULL)
123 const unsigned byte_size = Is32Bit() ? 4 : 8;
124 data.SetByteOrder(GetByteOrder());
125 data.SetAddressByteSize(byte_size);
127 // Read e_type and e_machine.
128 if (data.GetU16(offset, &e_type, 2) == NULL)
132 if (data.GetU32(offset, &e_version, 1) == NULL)
135 // Read e_entry, e_phoff and e_shoff.
136 if (GetMaxU64(data, offset, &e_entry, byte_size, 3) == false)
140 if (data.GetU32(offset, &e_flags, 1) == NULL)
143 // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx.
144 if (data.GetU16(offset, &e_ehsize, 6) == NULL)
147 // Initialize e_phnum, e_shnum, and e_shstrndx with the values read from the
149 e_phnum = e_phnum_hdr;
150 e_shnum = e_shnum_hdr;
151 e_shstrndx = e_shstrndx_hdr;
153 // See if we have extended header in section #0.
154 if (HasHeaderExtension())
155 ParseHeaderExtension(data);
160 bool ELFHeader::MagicBytesMatch(const uint8_t *magic) {
161 return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
164 unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) {
165 unsigned address_size = 0;
167 switch (magic[EI_CLASS]) {
179 unsigned ELFHeader::GetRelocationJumpSlotType() const {
184 assert(false && "architecture not supported");
187 slot = R_PPC_JMP_SLOT;
190 slot = R_PPC64_JMP_SLOT;
193 case EM_IAMCU: // FIXME: is this correct?
194 slot = R_386_JUMP_SLOT;
197 slot = R_X86_64_JUMP_SLOT;
200 slot = R_ARM_JUMP_SLOT;
203 slot = R_HEX_JMP_SLOT;
206 slot = R_AARCH64_JUMP_SLOT;
209 slot = R_MIPS_JUMP_SLOT;
212 slot = R_390_JMP_SLOT;
219 //------------------------------------------------------------------------------
222 ELFSectionHeader::ELFSectionHeader() {
223 memset(this, 0, sizeof(ELFSectionHeader));
226 bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
227 lldb::offset_t *offset) {
228 const unsigned byte_size = data.GetAddressByteSize();
230 // Read sh_name and sh_type.
231 if (data.GetU32(offset, &sh_name, 2) == NULL)
235 if (GetMaxU64(data, offset, &sh_flags, byte_size) == false)
238 // Read sh_addr, sh_off and sh_size.
239 if (GetMaxU64(data, offset, &sh_addr, byte_size, 3) == false)
242 // Read sh_link and sh_info.
243 if (data.GetU32(offset, &sh_link, 2) == NULL)
246 // Read sh_addralign and sh_entsize.
247 if (GetMaxU64(data, offset, &sh_addralign, byte_size, 2) == false)
253 //------------------------------------------------------------------------------
256 ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); }
258 #define ENUM_TO_CSTR(e) \
262 const char *ELFSymbol::bindingToCString(unsigned char binding) {
264 ENUM_TO_CSTR(STB_LOCAL);
265 ENUM_TO_CSTR(STB_GLOBAL);
266 ENUM_TO_CSTR(STB_WEAK);
267 ENUM_TO_CSTR(STB_LOOS);
268 ENUM_TO_CSTR(STB_HIOS);
269 ENUM_TO_CSTR(STB_LOPROC);
270 ENUM_TO_CSTR(STB_HIPROC);
275 const char *ELFSymbol::typeToCString(unsigned char type) {
277 ENUM_TO_CSTR(STT_NOTYPE);
278 ENUM_TO_CSTR(STT_OBJECT);
279 ENUM_TO_CSTR(STT_FUNC);
280 ENUM_TO_CSTR(STT_SECTION);
281 ENUM_TO_CSTR(STT_FILE);
282 ENUM_TO_CSTR(STT_COMMON);
283 ENUM_TO_CSTR(STT_TLS);
284 ENUM_TO_CSTR(STT_GNU_IFUNC);
285 ENUM_TO_CSTR(STT_HIOS);
286 ENUM_TO_CSTR(STT_LOPROC);
287 ENUM_TO_CSTR(STT_HIPROC);
292 const char *ELFSymbol::sectionIndexToCString(
293 elf_half shndx, const lldb_private::SectionList *section_list) {
295 ENUM_TO_CSTR(SHN_UNDEF);
296 ENUM_TO_CSTR(SHN_LOPROC);
297 ENUM_TO_CSTR(SHN_HIPROC);
298 ENUM_TO_CSTR(SHN_LOOS);
299 ENUM_TO_CSTR(SHN_HIOS);
300 ENUM_TO_CSTR(SHN_ABS);
301 ENUM_TO_CSTR(SHN_COMMON);
302 ENUM_TO_CSTR(SHN_XINDEX);
304 const lldb_private::Section *section =
305 section_list->GetSectionAtIndex(shndx).get();
307 return section->GetName().AsCString("");
313 void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx,
314 const lldb_private::DataExtractor *strtab_data,
315 const lldb_private::SectionList *section_list) {
316 s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64
317 " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
318 idx, st_value, st_size, st_name, st_info,
319 bindingToCString(getBinding()), typeToCString(getType()), st_other,
320 st_shndx, sectionIndexToCString(st_shndx, section_list),
321 strtab_data ? strtab_data->PeekCStr(st_name) : "");
324 bool ELFSymbol::Parse(const lldb_private::DataExtractor &data,
325 lldb::offset_t *offset) {
326 const unsigned byte_size = data.GetAddressByteSize();
327 const bool parsing_32 = byte_size == 4;
330 if (data.GetU32(offset, &st_name, 1) == NULL)
334 // Read st_value and st_size.
335 if (GetMaxU64(data, offset, &st_value, byte_size, 2) == false)
338 // Read st_info and st_other.
339 if (data.GetU8(offset, &st_info, 2) == NULL)
343 if (data.GetU16(offset, &st_shndx, 1) == NULL)
346 // Read st_info and st_other.
347 if (data.GetU8(offset, &st_info, 2) == NULL)
351 if (data.GetU16(offset, &st_shndx, 1) == NULL)
354 // Read st_value and st_size.
355 if (data.GetU64(offset, &st_value, 2) == NULL)
361 //------------------------------------------------------------------------------
364 ELFProgramHeader::ELFProgramHeader() {
365 memset(this, 0, sizeof(ELFProgramHeader));
368 bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
369 lldb::offset_t *offset) {
370 const uint32_t byte_size = data.GetAddressByteSize();
371 const bool parsing_32 = byte_size == 4;
374 if (data.GetU32(offset, &p_type, 1) == NULL)
378 // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
379 if (GetMaxU64(data, offset, &p_offset, byte_size, 5) == false)
383 if (data.GetU32(offset, &p_flags, 1) == NULL)
387 if (GetMaxU64(data, offset, &p_align, byte_size) == false)
391 if (data.GetU32(offset, &p_flags, 1) == NULL)
394 // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
395 if (GetMaxU64(data, offset, &p_offset, byte_size, 6) == false)
402 //------------------------------------------------------------------------------
405 ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); }
407 bool ELFDynamic::Parse(const lldb_private::DataExtractor &data,
408 lldb::offset_t *offset) {
409 const unsigned byte_size = data.GetAddressByteSize();
410 return GetMaxS64(data, offset, &d_tag, byte_size, 2);
413 //------------------------------------------------------------------------------
416 ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
418 bool ELFRel::Parse(const lldb_private::DataExtractor &data,
419 lldb::offset_t *offset) {
420 const unsigned byte_size = data.GetAddressByteSize();
422 // Read r_offset and r_info.
423 if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
429 //------------------------------------------------------------------------------
432 ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
434 bool ELFRela::Parse(const lldb_private::DataExtractor &data,
435 lldb::offset_t *offset) {
436 const unsigned byte_size = data.GetAddressByteSize();
438 // Read r_offset and r_info.
439 if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
443 if (GetMaxS64(data, offset, &r_addend, byte_size) == false)