1 //===-- 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 
12 #include "lldb/Core/DataExtractor.h"
13 #include "lldb/Core/Section.h"
14 #include "lldb/Core/Stream.h"
15 
16 #include "ELFHeader.h"
17 
18 using namespace elf;
19 using namespace lldb;
20 using 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.
27 static bool
28 GetMaxU64(const lldb_private::DataExtractor &data,
29           lldb::offset_t *offset,
30           uint64_t *value,
31           uint32_t byte_size)
32 {
33     const lldb::offset_t saved_offset = *offset;
34     *value = data.GetMaxU64(offset, byte_size);
35     return *offset != saved_offset;
36 }
37 
38 static bool
39 GetMaxU64(const lldb_private::DataExtractor &data,
40           lldb::offset_t *offset,
41           uint64_t *value,
42           uint32_t byte_size,
43           uint32_t count)
44 {
45     lldb::offset_t saved_offset = *offset;
46 
47     for (uint32_t i = 0; i < count; ++i, ++value)
48     {
49         if (GetMaxU64(data, offset, value, byte_size) == false)
50         {
51             *offset = saved_offset;
52             return false;
53         }
54     }
55     return true;
56 }
57 
58 static bool
59 GetMaxS64(const lldb_private::DataExtractor &data,
60           lldb::offset_t *offset,
61           int64_t *value,
62           uint32_t byte_size)
63 {
64     const lldb::offset_t saved_offset = *offset;
65     *value = data.GetMaxS64(offset, byte_size);
66     return *offset != saved_offset;
67 }
68 
69 static bool
70 GetMaxS64(const lldb_private::DataExtractor &data,
71           lldb::offset_t *offset,
72           int64_t *value,
73           uint32_t byte_size,
74           uint32_t count)
75 {
76     lldb::offset_t saved_offset = *offset;
77 
78     for (uint32_t i = 0; i < count; ++i, ++value)
79     {
80         if (GetMaxS64(data, offset, value, byte_size) == false)
81         {
82             *offset = saved_offset;
83             return false;
84         }
85     }
86     return true;
87 }
88 
89 //------------------------------------------------------------------------------
90 // ELFHeader
91 
92 ELFHeader::ELFHeader()
93 {
94     memset(this, 0, sizeof(ELFHeader));
95 }
96 
97 ByteOrder
98 ELFHeader::GetByteOrder() const
99 {
100     if (e_ident[EI_DATA] == ELFDATA2MSB)
101         return eByteOrderBig;
102     if (e_ident[EI_DATA] == ELFDATA2LSB)
103         return eByteOrderLittle;
104     return eByteOrderInvalid;
105 }
106 
107 bool
108 ELFHeader::Parse(lldb_private::DataExtractor &data, lldb::offset_t *offset)
109 {
110     // Read e_ident.  This provides byte order and address size info.
111     if (data.GetU8(offset, &e_ident, EI_NIDENT) == NULL)
112         return false;
113 
114     const unsigned byte_size = Is32Bit() ? 4 : 8;
115     data.SetByteOrder(GetByteOrder());
116     data.SetAddressByteSize(byte_size);
117 
118     // Read e_type and e_machine.
119     if (data.GetU16(offset, &e_type, 2) == NULL)
120         return false;
121 
122     // Read e_version.
123     if (data.GetU32(offset, &e_version, 1) == NULL)
124         return false;
125 
126     // Read e_entry, e_phoff and e_shoff.
127     if (GetMaxU64(data, offset, &e_entry, byte_size, 3) == false)
128         return false;
129 
130     // Read e_flags.
131     if (data.GetU32(offset, &e_flags, 1) == NULL)
132         return false;
133 
134     // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and
135     // e_shstrndx.
136     if (data.GetU16(offset, &e_ehsize, 6) == NULL)
137         return false;
138 
139     return true;
140 }
141 
142 bool
143 ELFHeader::MagicBytesMatch(const uint8_t *magic)
144 {
145     return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
146 }
147 
148 unsigned
149 ELFHeader::AddressSizeInBytes(const uint8_t *magic)
150 {
151     unsigned address_size = 0;
152 
153     switch (magic[EI_CLASS])
154     {
155     case ELFCLASS32:
156         address_size = 4;
157         break;
158 
159     case ELFCLASS64:
160         address_size = 8;
161         break;
162     }
163     return address_size;
164 }
165 
166 unsigned
167 ELFHeader::GetRelocationJumpSlotType() const
168 {
169     unsigned slot = 0;
170 
171     switch (e_machine)
172     {
173     default:
174         assert(false && "architecture not supported");
175         break;
176     case EM_386:
177     case EM_486:
178         slot = R_386_JUMP_SLOT;
179         break;
180     case EM_X86_64:
181         slot = R_X86_64_JUMP_SLOT;
182         break;
183     case EM_ARM:
184         slot = R_ARM_JUMP_SLOT;
185         break;
186     case EM_HEXAGON:
187         slot = R_HEX_JMP_SLOT;
188         break;
189     case EM_AARCH64:
190         slot = R_AARCH64_JUMP_SLOT;
191         break;
192     }
193 
194     return slot;
195 }
196 
197 //------------------------------------------------------------------------------
198 // ELFSectionHeader
199 
200 ELFSectionHeader::ELFSectionHeader()
201 {
202     memset(this, 0, sizeof(ELFSectionHeader));
203 }
204 
205 bool
206 ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
207                         lldb::offset_t *offset)
208 {
209     const unsigned byte_size = data.GetAddressByteSize();
210 
211     // Read sh_name and sh_type.
212     if (data.GetU32(offset, &sh_name, 2) == NULL)
213         return false;
214 
215     // Read sh_flags.
216     if (GetMaxU64(data, offset, &sh_flags, byte_size) == false)
217         return false;
218 
219     // Read sh_addr, sh_off and sh_size.
220     if (GetMaxU64(data, offset, &sh_addr, byte_size, 3) == false)
221         return false;
222 
223     // Read sh_link and sh_info.
224     if (data.GetU32(offset, &sh_link, 2) == NULL)
225         return false;
226 
227     // Read sh_addralign and sh_entsize.
228     if (GetMaxU64(data, offset, &sh_addralign, byte_size, 2) == false)
229         return false;
230 
231     return true;
232 }
233 
234 //------------------------------------------------------------------------------
235 // ELFSymbol
236 
237 ELFSymbol::ELFSymbol()
238 {
239     memset(this, 0, sizeof(ELFSymbol));
240 }
241 
242 #define ENUM_TO_CSTR(e) case e: return #e
243 
244 const char *
245 ELFSymbol::bindingToCString(unsigned char binding)
246 {
247     switch (binding)
248     {
249     ENUM_TO_CSTR(STB_LOCAL);
250     ENUM_TO_CSTR(STB_GLOBAL);
251     ENUM_TO_CSTR(STB_WEAK);
252     ENUM_TO_CSTR(STB_LOOS);
253     ENUM_TO_CSTR(STB_HIOS);
254     ENUM_TO_CSTR(STB_LOPROC);
255     ENUM_TO_CSTR(STB_HIPROC);
256     }
257     return "";
258 }
259 
260 const char *
261 ELFSymbol::typeToCString(unsigned char type)
262 {
263     switch (type)
264     {
265     ENUM_TO_CSTR(STT_NOTYPE);
266     ENUM_TO_CSTR(STT_OBJECT);
267     ENUM_TO_CSTR(STT_FUNC);
268     ENUM_TO_CSTR(STT_SECTION);
269     ENUM_TO_CSTR(STT_FILE);
270     ENUM_TO_CSTR(STT_COMMON);
271     ENUM_TO_CSTR(STT_TLS);
272     ENUM_TO_CSTR(STT_LOOS);
273     ENUM_TO_CSTR(STT_HIOS);
274     ENUM_TO_CSTR(STT_GNU_IFUNC);
275     ENUM_TO_CSTR(STT_LOPROC);
276     ENUM_TO_CSTR(STT_HIPROC);
277     }
278     return "";
279 }
280 
281 const char *
282 ELFSymbol::sectionIndexToCString (elf_half shndx,
283                                   const lldb_private::SectionList *section_list)
284 {
285     switch (shndx)
286     {
287     ENUM_TO_CSTR(SHN_UNDEF);
288     ENUM_TO_CSTR(SHN_LOPROC);
289     ENUM_TO_CSTR(SHN_HIPROC);
290     ENUM_TO_CSTR(SHN_LOOS);
291     ENUM_TO_CSTR(SHN_HIOS);
292     ENUM_TO_CSTR(SHN_ABS);
293     ENUM_TO_CSTR(SHN_COMMON);
294     ENUM_TO_CSTR(SHN_XINDEX);
295     default:
296         {
297             const lldb_private::Section *section = section_list->GetSectionAtIndex(shndx).get();
298             if (section)
299                 return section->GetName().AsCString("");
300         }
301         break;
302     }
303     return "";
304 }
305 
306 void
307 ELFSymbol::Dump (lldb_private::Stream *s,
308                  uint32_t idx,
309                  const lldb_private::DataExtractor *strtab_data,
310                  const lldb_private::SectionList *section_list)
311 {
312     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",
313               idx,
314               st_value,
315               st_size,
316               st_name,
317               st_info,
318               bindingToCString (getBinding()),
319               typeToCString (getType()),
320               st_other,
321               st_shndx,
322               sectionIndexToCString (st_shndx, section_list),
323               strtab_data ? strtab_data->PeekCStr(st_name) : "");
324 }
325 
326 bool
327 ELFSymbol::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
328 {
329     const unsigned byte_size = data.GetAddressByteSize();
330     const bool parsing_32 = byte_size == 4;
331 
332     // Read st_name.
333     if (data.GetU32(offset, &st_name, 1) == NULL)
334         return false;
335 
336     if (parsing_32)
337     {
338         // Read st_value and st_size.
339         if (GetMaxU64(data, offset, &st_value, byte_size, 2) == false)
340             return false;
341 
342         // Read st_info and st_other.
343         if (data.GetU8(offset, &st_info, 2) == NULL)
344             return false;
345 
346         // Read st_shndx.
347         if (data.GetU16(offset, &st_shndx, 1) == NULL)
348             return false;
349     }
350     else
351     {
352         // Read st_info and st_other.
353         if (data.GetU8(offset, &st_info, 2) == NULL)
354             return false;
355 
356         // Read st_shndx.
357         if (data.GetU16(offset, &st_shndx, 1) == NULL)
358             return false;
359 
360         // Read st_value and st_size.
361         if (data.GetU64(offset, &st_value, 2) == NULL)
362             return false;
363     }
364     return true;
365 }
366 
367 //------------------------------------------------------------------------------
368 // ELFProgramHeader
369 
370 ELFProgramHeader::ELFProgramHeader()
371 {
372     memset(this, 0, sizeof(ELFProgramHeader));
373 }
374 
375 bool
376 ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
377                         lldb::offset_t *offset)
378 {
379     const uint32_t byte_size = data.GetAddressByteSize();
380     const bool parsing_32 = byte_size == 4;
381 
382     // Read p_type;
383     if (data.GetU32(offset, &p_type, 1) == NULL)
384         return false;
385 
386     if (parsing_32) {
387         // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
388         if (GetMaxU64(data, offset, &p_offset, byte_size, 5) == false)
389             return false;
390 
391         // Read p_flags.
392         if (data.GetU32(offset, &p_flags, 1) == NULL)
393             return false;
394 
395         // Read p_align.
396         if (GetMaxU64(data, offset, &p_align, byte_size) == false)
397             return false;
398     }
399     else {
400         // Read p_flags.
401         if (data.GetU32(offset, &p_flags, 1) == NULL)
402             return false;
403 
404         // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
405         if (GetMaxU64(data, offset, &p_offset, byte_size, 6) == false)
406             return false;
407     }
408 
409     return true;
410 }
411 
412 //------------------------------------------------------------------------------
413 // ELFDynamic
414 
415 ELFDynamic::ELFDynamic()
416 {
417     memset(this, 0, sizeof(ELFDynamic));
418 }
419 
420 bool
421 ELFDynamic::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
422 {
423     const unsigned byte_size = data.GetAddressByteSize();
424     return GetMaxS64(data, offset, &d_tag, byte_size, 2);
425 }
426 
427 //------------------------------------------------------------------------------
428 // ELFRel
429 
430 ELFRel::ELFRel()
431 {
432     memset(this, 0, sizeof(ELFRel));
433 }
434 
435 bool
436 ELFRel::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
437 {
438     const unsigned byte_size = data.GetAddressByteSize();
439 
440     // Read r_offset and r_info.
441     if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
442         return false;
443 
444     return true;
445 }
446 
447 //------------------------------------------------------------------------------
448 // ELFRela
449 
450 ELFRela::ELFRela()
451 {
452     memset(this, 0, sizeof(ELFRela));
453 }
454 
455 bool
456 ELFRela::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
457 {
458     const unsigned byte_size = data.GetAddressByteSize();
459 
460     // Read r_offset and r_info.
461     if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
462         return false;
463 
464     // Read r_addend;
465     if (GetMaxS64(data, offset, &r_addend, byte_size) == false)
466         return false;
467 
468     return true;
469 }
470 
471 
472