xref: /llvm-project/lldb/source/Plugins/ObjectFile/ELF/ELFHeader.cpp (revision 86f422e1aa85ed38ab6e9bfbce031cf6da02cb0b)
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_PPC:
177         slot = R_PPC_JMP_SLOT;
178         break;
179     case EM_PPC64:
180         slot = R_PPC64_JMP_SLOT;
181         break;
182     case EM_386:
183     case EM_IAMCU: // FIXME: is this correct?
184         slot = R_386_JUMP_SLOT;
185         break;
186     case EM_X86_64:
187         slot = R_X86_64_JUMP_SLOT;
188         break;
189     case EM_ARM:
190         slot = R_ARM_JUMP_SLOT;
191         break;
192     case EM_HEXAGON:
193         slot = R_HEX_JMP_SLOT;
194         break;
195     case EM_AARCH64:
196         slot = R_AARCH64_JUMP_SLOT;
197         break;
198     case EM_MIPS:
199         slot = R_MIPS_JUMP_SLOT;
200         break;
201     }
202 
203     return slot;
204 }
205 
206 //------------------------------------------------------------------------------
207 // ELFSectionHeader
208 
209 ELFSectionHeader::ELFSectionHeader()
210 {
211     memset(this, 0, sizeof(ELFSectionHeader));
212 }
213 
214 bool
215 ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
216                         lldb::offset_t *offset)
217 {
218     const unsigned byte_size = data.GetAddressByteSize();
219 
220     // Read sh_name and sh_type.
221     if (data.GetU32(offset, &sh_name, 2) == NULL)
222         return false;
223 
224     // Read sh_flags.
225     if (GetMaxU64(data, offset, &sh_flags, byte_size) == false)
226         return false;
227 
228     // Read sh_addr, sh_off and sh_size.
229     if (GetMaxU64(data, offset, &sh_addr, byte_size, 3) == false)
230         return false;
231 
232     // Read sh_link and sh_info.
233     if (data.GetU32(offset, &sh_link, 2) == NULL)
234         return false;
235 
236     // Read sh_addralign and sh_entsize.
237     if (GetMaxU64(data, offset, &sh_addralign, byte_size, 2) == false)
238         return false;
239 
240     return true;
241 }
242 
243 //------------------------------------------------------------------------------
244 // ELFSymbol
245 
246 ELFSymbol::ELFSymbol()
247 {
248     memset(this, 0, sizeof(ELFSymbol));
249 }
250 
251 #define ENUM_TO_CSTR(e) case e: return #e
252 
253 const char *
254 ELFSymbol::bindingToCString(unsigned char binding)
255 {
256     switch (binding)
257     {
258     ENUM_TO_CSTR(STB_LOCAL);
259     ENUM_TO_CSTR(STB_GLOBAL);
260     ENUM_TO_CSTR(STB_WEAK);
261     ENUM_TO_CSTR(STB_LOOS);
262     ENUM_TO_CSTR(STB_HIOS);
263     ENUM_TO_CSTR(STB_LOPROC);
264     ENUM_TO_CSTR(STB_HIPROC);
265     }
266     return "";
267 }
268 
269 const char *
270 ELFSymbol::typeToCString(unsigned char type)
271 {
272     switch (type)
273     {
274     ENUM_TO_CSTR(STT_NOTYPE);
275     ENUM_TO_CSTR(STT_OBJECT);
276     ENUM_TO_CSTR(STT_FUNC);
277     ENUM_TO_CSTR(STT_SECTION);
278     ENUM_TO_CSTR(STT_FILE);
279     ENUM_TO_CSTR(STT_COMMON);
280     ENUM_TO_CSTR(STT_TLS);
281     ENUM_TO_CSTR(STT_LOOS);
282     ENUM_TO_CSTR(STT_HIOS);
283     ENUM_TO_CSTR(STT_GNU_IFUNC);
284     ENUM_TO_CSTR(STT_LOPROC);
285     ENUM_TO_CSTR(STT_HIPROC);
286     }
287     return "";
288 }
289 
290 const char *
291 ELFSymbol::sectionIndexToCString (elf_half shndx,
292                                   const lldb_private::SectionList *section_list)
293 {
294     switch (shndx)
295     {
296     ENUM_TO_CSTR(SHN_UNDEF);
297     ENUM_TO_CSTR(SHN_LOPROC);
298     ENUM_TO_CSTR(SHN_HIPROC);
299     ENUM_TO_CSTR(SHN_LOOS);
300     ENUM_TO_CSTR(SHN_HIOS);
301     ENUM_TO_CSTR(SHN_ABS);
302     ENUM_TO_CSTR(SHN_COMMON);
303     ENUM_TO_CSTR(SHN_XINDEX);
304     default:
305         {
306             const lldb_private::Section *section = section_list->GetSectionAtIndex(shndx).get();
307             if (section)
308                 return section->GetName().AsCString("");
309         }
310         break;
311     }
312     return "";
313 }
314 
315 void
316 ELFSymbol::Dump (lldb_private::Stream *s,
317                  uint32_t idx,
318                  const lldb_private::DataExtractor *strtab_data,
319                  const lldb_private::SectionList *section_list)
320 {
321     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",
322               idx,
323               st_value,
324               st_size,
325               st_name,
326               st_info,
327               bindingToCString (getBinding()),
328               typeToCString (getType()),
329               st_other,
330               st_shndx,
331               sectionIndexToCString (st_shndx, section_list),
332               strtab_data ? strtab_data->PeekCStr(st_name) : "");
333 }
334 
335 bool
336 ELFSymbol::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
337 {
338     const unsigned byte_size = data.GetAddressByteSize();
339     const bool parsing_32 = byte_size == 4;
340 
341     // Read st_name.
342     if (data.GetU32(offset, &st_name, 1) == NULL)
343         return false;
344 
345     if (parsing_32)
346     {
347         // Read st_value and st_size.
348         if (GetMaxU64(data, offset, &st_value, byte_size, 2) == false)
349             return false;
350 
351         // Read st_info and st_other.
352         if (data.GetU8(offset, &st_info, 2) == NULL)
353             return false;
354 
355         // Read st_shndx.
356         if (data.GetU16(offset, &st_shndx, 1) == NULL)
357             return false;
358     }
359     else
360     {
361         // Read st_info and st_other.
362         if (data.GetU8(offset, &st_info, 2) == NULL)
363             return false;
364 
365         // Read st_shndx.
366         if (data.GetU16(offset, &st_shndx, 1) == NULL)
367             return false;
368 
369         // Read st_value and st_size.
370         if (data.GetU64(offset, &st_value, 2) == NULL)
371             return false;
372     }
373     return true;
374 }
375 
376 //------------------------------------------------------------------------------
377 // ELFProgramHeader
378 
379 ELFProgramHeader::ELFProgramHeader()
380 {
381     memset(this, 0, sizeof(ELFProgramHeader));
382 }
383 
384 bool
385 ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
386                         lldb::offset_t *offset)
387 {
388     const uint32_t byte_size = data.GetAddressByteSize();
389     const bool parsing_32 = byte_size == 4;
390 
391     // Read p_type;
392     if (data.GetU32(offset, &p_type, 1) == NULL)
393         return false;
394 
395     if (parsing_32) {
396         // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
397         if (GetMaxU64(data, offset, &p_offset, byte_size, 5) == false)
398             return false;
399 
400         // Read p_flags.
401         if (data.GetU32(offset, &p_flags, 1) == NULL)
402             return false;
403 
404         // Read p_align.
405         if (GetMaxU64(data, offset, &p_align, byte_size) == false)
406             return false;
407     }
408     else {
409         // Read p_flags.
410         if (data.GetU32(offset, &p_flags, 1) == NULL)
411             return false;
412 
413         // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
414         if (GetMaxU64(data, offset, &p_offset, byte_size, 6) == false)
415             return false;
416     }
417 
418     return true;
419 }
420 
421 //------------------------------------------------------------------------------
422 // ELFDynamic
423 
424 ELFDynamic::ELFDynamic()
425 {
426     memset(this, 0, sizeof(ELFDynamic));
427 }
428 
429 bool
430 ELFDynamic::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
431 {
432     const unsigned byte_size = data.GetAddressByteSize();
433     return GetMaxS64(data, offset, &d_tag, byte_size, 2);
434 }
435 
436 //------------------------------------------------------------------------------
437 // ELFRel
438 
439 ELFRel::ELFRel()
440 {
441     memset(this, 0, sizeof(ELFRel));
442 }
443 
444 bool
445 ELFRel::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
446 {
447     const unsigned byte_size = data.GetAddressByteSize();
448 
449     // Read r_offset and r_info.
450     if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
451         return false;
452 
453     return true;
454 }
455 
456 //------------------------------------------------------------------------------
457 // ELFRela
458 
459 ELFRela::ELFRela()
460 {
461     memset(this, 0, sizeof(ELFRela));
462 }
463 
464 bool
465 ELFRela::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
466 {
467     const unsigned byte_size = data.GetAddressByteSize();
468 
469     // Read r_offset and r_info.
470     if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
471         return false;
472 
473     // Read r_addend;
474     if (GetMaxS64(data, offset, &r_addend, byte_size) == false)
475         return false;
476 
477     return true;
478 }
479 
480 
481