Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ObjectFile/ELF/ELFHeader.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ELFHeader.cpp -----------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include <cstring>
10
11
#include "lldb/Core/Section.h"
12
#include "lldb/Utility/DataExtractor.h"
13
#include "lldb/Utility/Stream.h"
14
15
#include "ELFHeader.h"
16
17
using namespace elf;
18
using namespace lldb;
19
using namespace llvm::ELF;
20
21
// Static utility functions.
22
//
23
// GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor
24
// with error handling code and provide for parsing a sequence of values.
25
static bool GetMaxU64(const lldb_private::DataExtractor &data,
26
                      lldb::offset_t *offset, uint64_t *value,
27
860k
                      uint32_t byte_size) {
28
860k
  const lldb::offset_t saved_offset = *offset;
29
860k
  *value = data.GetMaxU64(offset, byte_size);
30
860k
  return *offset != saved_offset;
31
860k
}
32
33
static bool GetMaxU64(const lldb_private::DataExtractor &data,
34
                      lldb::offset_t *offset, uint64_t *value,
35
284k
                      uint32_t byte_size, uint32_t count) {
36
284k
  lldb::offset_t saved_offset = *offset;
37
38
1.00M
  for (uint32_t i = 0; i < count; 
++i, ++value720k
) {
39
720k
    if (!GetMaxU64(data, offset, value, byte_size)) {
40
0
      *offset = saved_offset;
41
0
      return false;
42
0
    }
43
720k
  }
44
284k
  return true;
45
284k
}
46
47
static bool GetMaxS64(const lldb_private::DataExtractor &data,
48
                      lldb::offset_t *offset, int64_t *value,
49
4.06k
                      uint32_t byte_size) {
50
4.06k
  const lldb::offset_t saved_offset = *offset;
51
4.06k
  *value = data.GetMaxS64(offset, byte_size);
52
4.06k
  return *offset != saved_offset;
53
4.06k
}
54
55
static bool GetMaxS64(const lldb_private::DataExtractor &data,
56
                      lldb::offset_t *offset, int64_t *value,
57
1.09k
                      uint32_t byte_size, uint32_t count) {
58
1.09k
  lldb::offset_t saved_offset = *offset;
59
60
3.27k
  for (uint32_t i = 0; i < count; 
++i, ++value2.18k
) {
61
2.18k
    if (!GetMaxS64(data, offset, value, byte_size)) {
62
0
      *offset = saved_offset;
63
0
      return false;
64
0
    }
65
2.18k
  }
66
1.09k
  return true;
67
1.09k
}
68
69
// ELFHeader
70
71
835
ELFHeader::ELFHeader() { memset(this, 0, sizeof(ELFHeader)); }
72
73
1.94k
ByteOrder ELFHeader::GetByteOrder() const {
74
1.94k
  if (e_ident[EI_DATA] == ELFDATA2MSB)
75
0
    return eByteOrderBig;
76
1.94k
  if (e_ident[EI_DATA] == ELFDATA2LSB)
77
1.94k
    return eByteOrderLittle;
78
0
  return eByteOrderInvalid;
79
1.94k
}
80
81
2.40k
bool ELFHeader::HasHeaderExtension() const {
82
2.40k
  bool result = false;
83
84
  // Check if any of these values looks like sentinel.
85
2.40k
  result |= e_phnum_hdr == 0xFFFF; // PN_XNUM
86
2.40k
  result |= e_shnum_hdr == SHN_UNDEF;
87
2.40k
  result |= e_shstrndx_hdr == SHN_XINDEX;
88
89
  // If header extension is present, the section offset cannot be null.
90
2.40k
  result &= e_shoff != 0;
91
92
  // Done.
93
2.40k
  return result;
94
2.40k
}
95
96
5
void ELFHeader::ParseHeaderExtension(lldb_private::DataExtractor &data) {
97
  // Extract section #0 header.
98
5
  ELFSectionHeader section_zero;
99
5
  lldb::offset_t offset = 0;
100
5
  lldb_private::DataExtractor sh_data(data, e_shoff, e_shentsize);
101
5
  bool ok = section_zero.Parse(sh_data, &offset);
102
103
  // If we succeeded, fix the header.
104
5
  if (ok) {
105
4
    if (e_phnum_hdr == 0xFFFF) // PN_XNUM
106
0
      e_phnum = section_zero.sh_info;
107
4
    if (e_shnum_hdr == SHN_UNDEF)
108
4
      e_shnum = section_zero.sh_size;
109
4
    if (e_shstrndx_hdr == SHN_XINDEX)
110
0
      e_shstrndx = section_zero.sh_link;
111
4
  }
112
5
}
113
114
bool ELFHeader::Parse(lldb_private::DataExtractor &data,
115
1.94k
                      lldb::offset_t *offset) {
116
  // Read e_ident.  This provides byte order and address size info.
117
1.94k
  if (data.GetU8(offset, &e_ident, EI_NIDENT) == nullptr)
118
0
    return false;
119
120
1.94k
  const unsigned byte_size = Is32Bit() ? 
4386
:
81.55k
;
121
1.94k
  data.SetByteOrder(GetByteOrder());
122
1.94k
  data.SetAddressByteSize(byte_size);
123
124
  // Read e_type and e_machine.
125
1.94k
  if (data.GetU16(offset, &e_type, 2) == nullptr)
126
0
    return false;
127
128
  // Read e_version.
129
1.94k
  if (data.GetU32(offset, &e_version, 1) == nullptr)
130
0
    return false;
131
132
  // Read e_entry, e_phoff and e_shoff.
133
1.94k
  if (!GetMaxU64(data, offset, &e_entry, byte_size, 3))
134
0
    return false;
135
136
  // Read e_flags.
137
1.94k
  if (data.GetU32(offset, &e_flags, 1) == nullptr)
138
0
    return false;
139
140
  // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx.
141
1.94k
  if (data.GetU16(offset, &e_ehsize, 6) == nullptr)
142
0
    return false;
143
144
  // Initialize e_phnum, e_shnum, and e_shstrndx with the values read from the
145
  // header.
146
1.94k
  e_phnum = e_phnum_hdr;
147
1.94k
  e_shnum = e_shnum_hdr;
148
1.94k
  e_shstrndx = e_shstrndx_hdr;
149
150
  // See if we have extended header in section #0.
151
1.94k
  if (HasHeaderExtension())
152
5
    ParseHeaderExtension(data);
153
154
1.94k
  return true;
155
1.94k
}
156
157
286k
bool ELFHeader::MagicBytesMatch(const uint8_t *magic) {
158
286k
  return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0;
159
286k
}
160
161
324
unsigned ELFHeader::AddressSizeInBytes(const uint8_t *magic) {
162
324
  unsigned address_size = 0;
163
164
324
  switch (magic[EI_CLASS]) {
165
57
  case ELFCLASS32:
166
57
    address_size = 4;
167
57
    break;
168
169
267
  case ELFCLASS64:
170
267
    address_size = 8;
171
267
    break;
172
324
  }
173
324
  return address_size;
174
324
}
175
176
22
unsigned ELFHeader::GetRelocationJumpSlotType() const {
177
22
  unsigned slot = 0;
178
179
22
  switch (e_machine) {
180
0
  default:
181
0
    assert(false && "architecture not supported");
182
0
    break;
183
0
  case EM_PPC:
184
0
    slot = R_PPC_JMP_SLOT;
185
0
    break;
186
0
  case EM_PPC64:
187
0
    slot = R_PPC64_JMP_SLOT;
188
0
    break;
189
0
  case EM_386:
190
0
  case EM_IAMCU: // FIXME: is this correct?
191
0
    slot = R_386_JUMP_SLOT;
192
0
    break;
193
19
  case EM_X86_64:
194
19
    slot = R_X86_64_JUMP_SLOT;
195
19
    break;
196
0
  case EM_ARM:
197
0
    slot = R_ARM_JUMP_SLOT;
198
0
    break;
199
0
  case EM_HEXAGON:
200
0
    slot = R_HEX_JMP_SLOT;
201
0
    break;
202
3
  case EM_AARCH64:
203
3
    slot = R_AARCH64_JUMP_SLOT;
204
3
    break;
205
0
  case EM_MIPS:
206
0
    slot = R_MIPS_JUMP_SLOT;
207
0
    break;
208
0
  case EM_S390:
209
0
    slot = R_390_JMP_SLOT;
210
0
    break;
211
22
  }
212
213
22
  return slot;
214
22
}
215
216
// ELFSectionHeader
217
218
138k
ELFSectionHeader::ELFSectionHeader() {
219
138k
  memset(this, 0, sizeof(ELFSectionHeader));
220
138k
}
221
222
bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
223
138k
                             lldb::offset_t *offset) {
224
138k
  const unsigned byte_size = data.GetAddressByteSize();
225
226
  // Read sh_name and sh_type.
227
138k
  if (data.GetU32(offset, &sh_name, 2) == nullptr)
228
1
    return false;
229
230
  // Read sh_flags.
231
138k
  if (!GetMaxU64(data, offset, &sh_flags, byte_size))
232
0
    return false;
233
234
  // Read sh_addr, sh_off and sh_size.
235
138k
  if (!GetMaxU64(data, offset, &sh_addr, byte_size, 3))
236
0
    return false;
237
238
  // Read sh_link and sh_info.
239
138k
  if (data.GetU32(offset, &sh_link, 2) == nullptr)
240
0
    return false;
241
242
  // Read sh_addralign and sh_entsize.
243
138k
  if (!GetMaxU64(data, offset, &sh_addralign, byte_size, 2))
244
0
    return false;
245
246
138k
  return true;
247
138k
}
248
249
// ELFSymbol
250
251
165
ELFSymbol::ELFSymbol() { memset(this, 0, sizeof(ELFSymbol)); }
252
253
#define ENUM_TO_CSTR(e)                                                        \
254
0
  case e:                                                                      \
255
0
    return #e
256
257
0
const char *ELFSymbol::bindingToCString(unsigned char binding) {
258
0
  switch (binding) {
259
0
    ENUM_TO_CSTR(STB_LOCAL);
260
0
    ENUM_TO_CSTR(STB_GLOBAL);
261
0
    ENUM_TO_CSTR(STB_WEAK);
262
0
    ENUM_TO_CSTR(STB_LOOS);
263
0
    ENUM_TO_CSTR(STB_HIOS);
264
0
    ENUM_TO_CSTR(STB_LOPROC);
265
0
    ENUM_TO_CSTR(STB_HIPROC);
266
0
  }
267
0
  return "";
268
0
}
269
270
0
const char *ELFSymbol::typeToCString(unsigned char type) {
271
0
  switch (type) {
272
0
    ENUM_TO_CSTR(STT_NOTYPE);
273
0
    ENUM_TO_CSTR(STT_OBJECT);
274
0
    ENUM_TO_CSTR(STT_FUNC);
275
0
    ENUM_TO_CSTR(STT_SECTION);
276
0
    ENUM_TO_CSTR(STT_FILE);
277
0
    ENUM_TO_CSTR(STT_COMMON);
278
0
    ENUM_TO_CSTR(STT_TLS);
279
0
    ENUM_TO_CSTR(STT_GNU_IFUNC);
280
0
    ENUM_TO_CSTR(STT_HIOS);
281
0
    ENUM_TO_CSTR(STT_LOPROC);
282
0
    ENUM_TO_CSTR(STT_HIPROC);
283
0
  }
284
0
  return "";
285
0
}
286
287
const char *ELFSymbol::sectionIndexToCString(
288
0
    elf_half shndx, const lldb_private::SectionList *section_list) {
289
0
  switch (shndx) {
290
0
    ENUM_TO_CSTR(SHN_UNDEF);
291
0
    ENUM_TO_CSTR(SHN_LOPROC);
292
0
    ENUM_TO_CSTR(SHN_HIPROC);
293
0
    ENUM_TO_CSTR(SHN_LOOS);
294
0
    ENUM_TO_CSTR(SHN_HIOS);
295
0
    ENUM_TO_CSTR(SHN_ABS);
296
0
    ENUM_TO_CSTR(SHN_COMMON);
297
0
    ENUM_TO_CSTR(SHN_XINDEX);
298
0
  default: {
299
0
    const lldb_private::Section *section =
300
0
        section_list->GetSectionAtIndex(shndx).get();
301
0
    if (section)
302
0
      return section->GetName().AsCString("");
303
0
  } break;
304
0
  }
305
0
  return "";
306
0
}
307
308
void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx,
309
                     const lldb_private::DataExtractor *strtab_data,
310
0
                     const lldb_private::SectionList *section_list) {
311
0
  s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64
312
0
            " 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
313
0
            idx, st_value, st_size, st_name, st_info,
314
0
            bindingToCString(getBinding()), typeToCString(getType()), st_other,
315
0
            st_shndx, sectionIndexToCString(st_shndx, section_list),
316
0
            strtab_data ? strtab_data->PeekCStr(st_name) : "");
317
0
}
318
319
bool ELFSymbol::Parse(const lldb_private::DataExtractor &data,
320
2.34k
                      lldb::offset_t *offset) {
321
2.34k
  const unsigned byte_size = data.GetAddressByteSize();
322
2.34k
  const bool parsing_32 = byte_size == 4;
323
324
  // Read st_name.
325
2.34k
  if (data.GetU32(offset, &st_name, 1) == nullptr)
326
0
    return false;
327
328
2.34k
  if (parsing_32) {
329
    // Read st_value and st_size.
330
96
    if (!GetMaxU64(data, offset, &st_value, byte_size, 2))
331
0
      return false;
332
333
    // Read st_info and st_other.
334
96
    if (data.GetU8(offset, &st_info, 2) == nullptr)
335
0
      return false;
336
337
    // Read st_shndx.
338
96
    if (data.GetU16(offset, &st_shndx, 1) == nullptr)
339
0
      return false;
340
2.24k
  } else {
341
    // Read st_info and st_other.
342
2.24k
    if (data.GetU8(offset, &st_info, 2) == nullptr)
343
0
      return false;
344
345
    // Read st_shndx.
346
2.24k
    if (data.GetU16(offset, &st_shndx, 1) == nullptr)
347
0
      return false;
348
349
    // Read st_value and st_size.
350
2.24k
    if (data.GetU64(offset, &st_value, 2) == nullptr)
351
0
      return false;
352
2.24k
  }
353
2.34k
  return true;
354
2.34k
}
355
356
// ELFProgramHeader
357
358
3.01k
ELFProgramHeader::ELFProgramHeader() {
359
3.01k
  memset(this, 0, sizeof(ELFProgramHeader));
360
3.01k
}
361
362
bool ELFProgramHeader::Parse(const lldb_private::DataExtractor &data,
363
3.01k
                             lldb::offset_t *offset) {
364
3.01k
  const uint32_t byte_size = data.GetAddressByteSize();
365
3.01k
  const bool parsing_32 = byte_size == 4;
366
367
  // Read p_type;
368
3.01k
  if (data.GetU32(offset, &p_type, 1) == nullptr)
369
0
    return false;
370
371
3.01k
  if (parsing_32) {
372
    // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz.
373
965
    if (!GetMaxU64(data, offset, &p_offset, byte_size, 5))
374
0
      return false;
375
376
    // Read p_flags.
377
965
    if (data.GetU32(offset, &p_flags, 1) == nullptr)
378
0
      return false;
379
380
    // Read p_align.
381
965
    if (!GetMaxU64(data, offset, &p_align, byte_size))
382
0
      return false;
383
2.04k
  } else {
384
    // Read p_flags.
385
2.04k
    if (data.GetU32(offset, &p_flags, 1) == nullptr)
386
0
      return false;
387
388
    // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align.
389
2.04k
    if (!GetMaxU64(data, offset, &p_offset, byte_size, 6))
390
0
      return false;
391
2.04k
  }
392
393
3.01k
  return true;
394
3.01k
}
395
396
// ELFDynamic
397
398
55
ELFDynamic::ELFDynamic() { memset(this, 0, sizeof(ELFDynamic)); }
399
400
bool ELFDynamic::Parse(const lldb_private::DataExtractor &data,
401
1.09k
                       lldb::offset_t *offset) {
402
1.09k
  const unsigned byte_size = data.GetAddressByteSize();
403
1.09k
  return GetMaxS64(data, offset, &d_tag, byte_size, 2);
404
1.09k
}
405
406
// ELFRel
407
408
0
ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
409
410
bool ELFRel::Parse(const lldb_private::DataExtractor &data,
411
0
                   lldb::offset_t *offset) {
412
0
  const unsigned byte_size = data.GetAddressByteSize();
413
414
  // Read r_offset and r_info.
415
0
  return GetMaxU64(data, offset, &r_offset, byte_size, 2) != false;
416
0
}
417
418
// ELFRela
419
420
116
ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
421
422
bool ELFRela::Parse(const lldb_private::DataExtractor &data,
423
1.88k
                    lldb::offset_t *offset) {
424
1.88k
  const unsigned byte_size = data.GetAddressByteSize();
425
426
  // Read r_offset and r_info.
427
1.88k
  if (!GetMaxU64(data, offset, &r_offset, byte_size, 2))
428
0
    return false;
429
430
  // Read r_addend;
431
1.88k
  if (!GetMaxS64(data, offset, &r_addend, byte_size))
432
0
    return false;
433
434
1.88k
  return true;
435
1.88k
}