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