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/DataExtractor.h"
13 #include "lldb/Core/Section.h"
14 #include "lldb/Core/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.
28 GetMaxU64(const lldb_private::DataExtractor &data,
29 lldb::offset_t *offset,
33 const lldb::offset_t saved_offset = *offset;
34 *value = data.GetMaxU64(offset, byte_size);
35 return *offset != saved_offset;
39 GetMaxU64(const lldb_private::DataExtractor &data,
40 lldb::offset_t *offset,
45 lldb::offset_t saved_offset = *offset;
47 for (uint32_t i = 0; i < count; ++i, ++value)
49 if (GetMaxU64(data, offset, value, byte_size) == false)
51 *offset = saved_offset;
59 GetMaxS64(const lldb_private::DataExtractor &data,
60 lldb::offset_t *offset,
64 const lldb::offset_t saved_offset = *offset;
65 *value = data.GetMaxS64(offset, byte_size);
66 return *offset != saved_offset;
70 GetMaxS64(const lldb_private::DataExtractor &data,
71 lldb::offset_t *offset,
76 lldb::offset_t saved_offset = *offset;
78 for (uint32_t i = 0; i < count; ++i, ++value)
80 if (GetMaxS64(data, offset, value, byte_size) == false)
82 *offset = saved_offset;
89 //------------------------------------------------------------------------------
92 ELFHeader::ELFHeader()
94 memset(this, 0, sizeof(ELFHeader));
98 ELFHeader::GetByteOrder() const
100 if (e_ident[EI_DATA] == ELFDATA2MSB)
101 return eByteOrderBig;
102 if (e_ident[EI_DATA] == ELFDATA2LSB)
103 return eByteOrderLittle;
104 return eByteOrderInvalid;
108 ELFHeader::Parse(lldb_private::DataExtractor &data, lldb::offset_t *offset)
110 // Read e_ident. This provides byte order and address size info.
111 if (data.GetU8(offset, &e_ident, EI_NIDENT) == NULL)
114 const unsigned byte_size = Is32Bit() ? 4 : 8;
115 data.SetByteOrder(GetByteOrder());
116 data.SetAddressByteSize(byte_size);
118 // Read e_type and e_machine.
119 if (data.GetU16(offset, &e_type, 2) == NULL)
123 if (data.GetU32(offset, &e_version, 1) == NULL)
126 // Read e_entry, e_phoff and e_shoff.
127 if (GetMaxU64(data, offset, &e_entry, byte_size, 3) == false)
131 if (data.GetU32(offset, &e_flags, 1) == NULL)
134 // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and
136 if (data.GetU16(offset, &e_ehsize, 6) == NULL)
143 ELFHeader::MagicBytesMatch(const uint8_t *magic)
145 return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
149 ELFHeader::AddressSizeInBytes(const uint8_t *magic)
151 unsigned address_size = 0;
153 switch (magic[EI_CLASS])
167 ELFHeader::GetRelocationJumpSlotType() const
174 assert(false && "architecture not supported");
178 slot = R_386_JUMP_SLOT;
181 slot = R_X86_64_JUMP_SLOT;
184 slot = R_ARM_JUMP_SLOT;
191 //------------------------------------------------------------------------------
194 ELFSectionHeader::ELFSectionHeader()
196 memset(this, 0, sizeof(ELFSectionHeader));
200 ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
201 lldb::offset_t *offset)
203 const unsigned byte_size = data.GetAddressByteSize();
205 // Read sh_name and sh_type.
206 if (data.GetU32(offset, &sh_name, 2) == NULL)
210 if (GetMaxU64(data, offset, &sh_flags, byte_size) == false)
213 // Read sh_addr, sh_off and sh_size.
214 if (GetMaxU64(data, offset, &sh_addr, byte_size, 3) == false)
217 // Read sh_link and sh_info.
218 if (data.GetU32(offset, &sh_link, 2) == NULL)
221 // Read sh_addralign and sh_entsize.
222 if (GetMaxU64(data, offset, &sh_addralign, byte_size, 2) == false)
228 //------------------------------------------------------------------------------
231 ELFSymbol::ELFSymbol()
233 memset(this, 0, sizeof(ELFSymbol));
236 #define ENUM_TO_CSTR(e) case e: return #e
239 ELFSymbol::bindingToCString(unsigned char binding)
243 ENUM_TO_CSTR(STB_LOCAL);
244 ENUM_TO_CSTR(STB_GLOBAL);
245 ENUM_TO_CSTR(STB_WEAK);
246 ENUM_TO_CSTR(STB_LOOS);
247 ENUM_TO_CSTR(STB_HIOS);
248 ENUM_TO_CSTR(STB_LOPROC);
249 ENUM_TO_CSTR(STB_HIPROC);
255 ELFSymbol::typeToCString(unsigned char type)
259 ENUM_TO_CSTR(STT_NOTYPE);
260 ENUM_TO_CSTR(STT_OBJECT);
261 ENUM_TO_CSTR(STT_FUNC);
262 ENUM_TO_CSTR(STT_SECTION);
263 ENUM_TO_CSTR(STT_FILE);
264 ENUM_TO_CSTR(STT_COMMON);
265 ENUM_TO_CSTR(STT_TLS);
266 ENUM_TO_CSTR(STT_LOOS);
267 ENUM_TO_CSTR(STT_HIOS);
268 ENUM_TO_CSTR(STT_GNU_IFUNC);
269 ENUM_TO_CSTR(STT_LOPROC);
270 ENUM_TO_CSTR(STT_HIPROC);
276 ELFSymbol::sectionIndexToCString (elf_half shndx,
277 const lldb_private::SectionList *section_list)
281 ENUM_TO_CSTR(SHN_UNDEF);
282 ENUM_TO_CSTR(SHN_LOPROC);
283 ENUM_TO_CSTR(SHN_HIPROC);
284 ENUM_TO_CSTR(SHN_LOOS);
285 ENUM_TO_CSTR(SHN_HIOS);
286 ENUM_TO_CSTR(SHN_ABS);
287 ENUM_TO_CSTR(SHN_COMMON);
288 ENUM_TO_CSTR(SHN_XINDEX);
291 const lldb_private::Section *section = section_list->GetSectionAtIndex(shndx).get();
293 return section->GetName().AsCString("");
301 ELFSymbol::Dump (lldb_private::Stream *s,
303 const lldb_private::DataExtractor *strtab_data,
304 const lldb_private::SectionList *section_list)
306 s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64 " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
312 bindingToCString (getBinding()),
313 typeToCString (getType()),
316 sectionIndexToCString (st_shndx, section_list),
317 strtab_data ? strtab_data->PeekCStr(st_name) : "");
321 ELFSymbol::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
323 const unsigned byte_size = data.GetAddressByteSize();
324 const bool parsing_32 = byte_size == 4;
327 if (data.GetU32(offset, &st_name, 1) == NULL)
332 // Read st_value and st_size.
333 if (GetMaxU64(data, offset, &st_value, byte_size, 2) == false)
336 // Read st_info and st_other.
337 if (data.GetU8(offset, &st_info, 2) == NULL)
341 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()
366 memset(this, 0, sizeof(ELFProgramHeader));
370 ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
371 lldb::offset_t *offset)
373 const uint32_t byte_size = data.GetAddressByteSize();
374 const bool parsing_32 = byte_size == 4;
377 if (data.GetU32(offset, &p_type, 1) == NULL)
381 // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
382 if (GetMaxU64(data, offset, &p_offset, byte_size, 5) == false)
386 if (data.GetU32(offset, &p_flags, 1) == NULL)
390 if (GetMaxU64(data, offset, &p_align, byte_size) == false)
395 if (data.GetU32(offset, &p_flags, 1) == NULL)
398 // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
399 if (GetMaxU64(data, offset, &p_offset, byte_size, 6) == false)
406 //------------------------------------------------------------------------------
409 ELFDynamic::ELFDynamic()
411 memset(this, 0, sizeof(ELFDynamic));
415 ELFDynamic::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
417 const unsigned byte_size = data.GetAddressByteSize();
418 return GetMaxS64(data, offset, &d_tag, byte_size, 2);
421 //------------------------------------------------------------------------------
426 memset(this, 0, sizeof(ELFRel));
430 ELFRel::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
432 const unsigned byte_size = data.GetAddressByteSize();
434 // Read r_offset and r_info.
435 if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
441 //------------------------------------------------------------------------------
446 memset(this, 0, sizeof(ELFRela));
450 ELFRela::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
452 const unsigned byte_size = data.GetAddressByteSize();
454 // Read r_offset and r_info.
455 if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
459 if (GetMaxS64(data, offset, &r_addend, byte_size) == false)