blob: 16cbb6e5753be924cdb0046aab5c97b0b73d2ef0 [file] [log] [blame]
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001//===-- ELFHeader.cpp ----------------------------------------- -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <cstring>
11
Greg Clayton9594f4c2013-04-13 23:17:23 +000012#include "lldb/Core/Section.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000013#include "lldb/Utility/DataExtractor.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000014#include "lldb/Utility/Stream.h"
Stephen Wilsonf325ba92010-07-13 23:07:23 +000015
16#include "ELFHeader.h"
17
18using namespace elf;
19using namespace lldb;
20using namespace llvm::ELF;
21
22//------------------------------------------------------------------------------
23// Static utility functions.
24//
25// GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor
26// with error handling code and provide for parsing a sequence of values.
Kate Stoneb9c1b512016-09-06 20:57:50 +000027static bool GetMaxU64(const lldb_private::DataExtractor &data,
28 lldb::offset_t *offset, uint64_t *value,
29 uint32_t byte_size) {
30 const lldb::offset_t saved_offset = *offset;
31 *value = data.GetMaxU64(offset, byte_size);
32 return *offset != saved_offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000033}
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035static 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;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000039
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 for (uint32_t i = 0; i < count; ++i, ++value) {
41 if (GetMaxU64(data, offset, value, byte_size) == false) {
42 *offset = saved_offset;
43 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000044 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 }
46 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000047}
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049static bool GetMaxS64(const lldb_private::DataExtractor &data,
50 lldb::offset_t *offset, int64_t *value,
51 uint32_t byte_size) {
52 const lldb::offset_t saved_offset = *offset;
53 *value = data.GetMaxS64(offset, byte_size);
54 return *offset != saved_offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000055}
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057static 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;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 for (uint32_t i = 0; i < count; ++i, ++value) {
63 if (GetMaxS64(data, offset, value, byte_size) == false) {
64 *offset = saved_offset;
65 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000066 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 }
68 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000069}
70
71//------------------------------------------------------------------------------
72// ELFHeader
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074ELFHeader::ELFHeader() { memset(this, 0, sizeof(ELFHeader)); }
75
76ByteOrder ELFHeader::GetByteOrder() const {
77 if (e_ident[EI_DATA] == ELFDATA2MSB)
78 return eByteOrderBig;
79 if (e_ident[EI_DATA] == ELFDATA2LSB)
80 return eByteOrderLittle;
81 return eByteOrderInvalid;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000082}
83
Pavel Labath23ccc292017-01-31 23:09:46 +000084bool ELFHeader::HasHeaderExtension() const {
85 bool result = false;
86
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;
91
92 // If header extension is present, the section offset cannot be null.
93 result &= e_shoff != 0;
94
95 // Done.
96 return result;
97}
98
99void 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);
105
106 // If we succeeded, fix the header.
107 if (ok) {
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;
114 }
115}
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117bool 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)
121 return false;
122
123 const unsigned byte_size = Is32Bit() ? 4 : 8;
124 data.SetByteOrder(GetByteOrder());
125 data.SetAddressByteSize(byte_size);
126
127 // Read e_type and e_machine.
128 if (data.GetU16(offset, &e_type, 2) == NULL)
129 return false;
130
131 // Read e_version.
132 if (data.GetU32(offset, &e_version, 1) == NULL)
133 return false;
134
135 // Read e_entry, e_phoff and e_shoff.
136 if (GetMaxU64(data, offset, &e_entry, byte_size, 3) == false)
137 return false;
138
139 // Read e_flags.
140 if (data.GetU32(offset, &e_flags, 1) == NULL)
141 return false;
142
Adrian Prantl05097242018-04-30 16:49:04 +0000143 // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 if (data.GetU16(offset, &e_ehsize, 6) == NULL)
145 return false;
146
Adrian Prantl05097242018-04-30 16:49:04 +0000147 // Initialize e_phnum, e_shnum, and e_shstrndx with the values read from the
148 // header.
Pavel Labath23ccc292017-01-31 23:09:46 +0000149 e_phnum = e_phnum_hdr;
150 e_shnum = e_shnum_hdr;
151 e_shstrndx = e_shstrndx_hdr;
152
153 // See if we have extended header in section #0.
154 if (HasHeaderExtension())
155 ParseHeaderExtension(data);
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160bool ELFHeader::MagicBytesMatch(const uint8_t *magic) {
161 return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) {
165 unsigned address_size = 0;
166
167 switch (magic[EI_CLASS]) {
168 case ELFCLASS32:
169 address_size = 4;
170 break;
171
172 case ELFCLASS64:
173 address_size = 8;
174 break;
175 }
176 return address_size;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000177}
178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179unsigned ELFHeader::GetRelocationJumpSlotType() const {
180 unsigned slot = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 switch (e_machine) {
183 default:
184 assert(false && "architecture not supported");
185 break;
186 case EM_PPC:
187 slot = R_PPC_JMP_SLOT;
188 break;
189 case EM_PPC64:
190 slot = R_PPC64_JMP_SLOT;
191 break;
192 case EM_386:
193 case EM_IAMCU: // FIXME: is this correct?
194 slot = R_386_JUMP_SLOT;
195 break;
196 case EM_X86_64:
197 slot = R_X86_64_JUMP_SLOT;
198 break;
199 case EM_ARM:
200 slot = R_ARM_JUMP_SLOT;
201 break;
202 case EM_HEXAGON:
203 slot = R_HEX_JMP_SLOT;
204 break;
205 case EM_AARCH64:
206 slot = R_AARCH64_JUMP_SLOT;
207 break;
208 case EM_MIPS:
209 slot = R_MIPS_JUMP_SLOT;
210 break;
211 case EM_S390:
212 slot = R_390_JMP_SLOT;
213 break;
214 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 return slot;
Stephen Wilson43fe6452011-03-30 15:59:12 +0000217}
218
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000219//------------------------------------------------------------------------------
220// ELFSectionHeader
221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222ELFSectionHeader::ELFSectionHeader() {
223 memset(this, 0, sizeof(ELFSectionHeader));
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000224}
225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
227 lldb::offset_t *offset) {
228 const unsigned byte_size = data.GetAddressByteSize();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 // Read sh_name and sh_type.
231 if (data.GetU32(offset, &sh_name, 2) == NULL)
232 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 // Read sh_flags.
235 if (GetMaxU64(data, offset, &sh_flags, byte_size) == false)
236 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 // Read sh_addr, sh_off and sh_size.
239 if (GetMaxU64(data, offset, &sh_addr, byte_size, 3) == false)
240 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 // Read sh_link and sh_info.
243 if (data.GetU32(offset, &sh_link, 2) == NULL)
244 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 // Read sh_addralign and sh_entsize.
247 if (GetMaxU64(data, offset, &sh_addralign, byte_size, 2) == false)
248 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000251}
252
253//------------------------------------------------------------------------------
254// ELFSymbol
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258#define ENUM_TO_CSTR(e) \
259 case e: \
260 return #e
Greg Clayton9594f4c2013-04-13 23:17:23 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262const char *ELFSymbol::bindingToCString(unsigned char binding) {
263 switch (binding) {
Greg Clayton9594f4c2013-04-13 23:17:23 +0000264 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);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 }
272 return "";
Greg Clayton9594f4c2013-04-13 23:17:23 +0000273}
274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275const char *ELFSymbol::typeToCString(unsigned char type) {
276 switch (type) {
Greg Clayton9594f4c2013-04-13 23:17:23 +0000277 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);
Greg Clayton9594f4c2013-04-13 23:17:23 +0000284 ENUM_TO_CSTR(STT_GNU_IFUNC);
Pavel Labatha6d0dd72015-06-23 17:15:14 +0000285 ENUM_TO_CSTR(STT_HIOS);
Greg Clayton9594f4c2013-04-13 23:17:23 +0000286 ENUM_TO_CSTR(STT_LOPROC);
287 ENUM_TO_CSTR(STT_HIPROC);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 }
289 return "";
Greg Clayton9594f4c2013-04-13 23:17:23 +0000290}
291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292const char *ELFSymbol::sectionIndexToCString(
293 elf_half shndx, const lldb_private::SectionList *section_list) {
294 switch (shndx) {
Greg Clayton9594f4c2013-04-13 23:17:23 +0000295 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);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 default: {
304 const lldb_private::Section *section =
305 section_list->GetSectionAtIndex(shndx).get();
306 if (section)
307 return section->GetName().AsCString("");
308 } break;
309 }
310 return "";
Greg Clayton9594f4c2013-04-13 23:17:23 +0000311}
312
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313void 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) : "");
Greg Clayton9594f4c2013-04-13 23:17:23 +0000322}
323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324bool 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;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 // Read st_name.
330 if (data.GetU32(offset, &st_name, 1) == NULL)
331 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 if (parsing_32) {
334 // Read st_value and st_size.
335 if (GetMaxU64(data, offset, &st_value, byte_size, 2) == false)
336 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000337
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 // Read st_info and st_other.
339 if (data.GetU8(offset, &st_info, 2) == NULL)
340 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 // Read st_shndx.
343 if (data.GetU16(offset, &st_shndx, 1) == NULL)
344 return false;
345 } else {
346 // Read st_info and st_other.
347 if (data.GetU8(offset, &st_info, 2) == NULL)
348 return false;
349
350 // Read st_shndx.
351 if (data.GetU16(offset, &st_shndx, 1) == NULL)
352 return false;
353
354 // Read st_value and st_size.
355 if (data.GetU64(offset, &st_value, 2) == NULL)
356 return false;
357 }
358 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000359}
360
361//------------------------------------------------------------------------------
362// ELFProgramHeader
363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364ELFProgramHeader::ELFProgramHeader() {
365 memset(this, 0, sizeof(ELFProgramHeader));
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000366}
367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368bool 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;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000372
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373 // Read p_type;
374 if (data.GetU32(offset, &p_type, 1) == NULL)
375 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 if (parsing_32) {
378 // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
379 if (GetMaxU64(data, offset, &p_offset, byte_size, 5) == false)
380 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000381
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 // Read p_flags.
383 if (data.GetU32(offset, &p_flags, 1) == NULL)
384 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 // Read p_align.
387 if (GetMaxU64(data, offset, &p_align, byte_size) == false)
388 return false;
389 } else {
390 // Read p_flags.
391 if (data.GetU32(offset, &p_flags, 1) == NULL)
392 return false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 // 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)
396 return false;
397 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000398
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000400}
401
402//------------------------------------------------------------------------------
403// ELFDynamic
404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407bool 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);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000411}
412
Stephen Wilson43fe6452011-03-30 15:59:12 +0000413//------------------------------------------------------------------------------
414// ELFRel
415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418bool ELFRel::Parse(const lldb_private::DataExtractor &data,
419 lldb::offset_t *offset) {
420 const unsigned byte_size = data.GetAddressByteSize();
Stephen Wilson43fe6452011-03-30 15:59:12 +0000421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 // Read r_offset and r_info.
423 if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
424 return false;
Stephen Wilson43fe6452011-03-30 15:59:12 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 return true;
Stephen Wilson43fe6452011-03-30 15:59:12 +0000427}
428
429//------------------------------------------------------------------------------
430// ELFRela
431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
433
434bool ELFRela::Parse(const lldb_private::DataExtractor &data,
435 lldb::offset_t *offset) {
436 const unsigned byte_size = data.GetAddressByteSize();
437
438 // Read r_offset and r_info.
439 if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
440 return false;
441
442 // Read r_addend;
443 if (GetMaxS64(data, offset, &r_addend, byte_size) == false)
444 return false;
445
446 return true;
Stephen Wilson43fe6452011-03-30 15:59:12 +0000447}