Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
39642 views
1
//===-- ObjectFileELF.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 "ObjectFileELF.h"
10
11
#include <algorithm>
12
#include <cassert>
13
#include <optional>
14
#include <unordered_map>
15
16
#include "lldb/Core/Module.h"
17
#include "lldb/Core/ModuleSpec.h"
18
#include "lldb/Core/PluginManager.h"
19
#include "lldb/Core/Progress.h"
20
#include "lldb/Core/Section.h"
21
#include "lldb/Host/FileSystem.h"
22
#include "lldb/Host/LZMA.h"
23
#include "lldb/Symbol/DWARFCallFrameInfo.h"
24
#include "lldb/Symbol/SymbolContext.h"
25
#include "lldb/Target/SectionLoadList.h"
26
#include "lldb/Target/Target.h"
27
#include "lldb/Utility/ArchSpec.h"
28
#include "lldb/Utility/DataBufferHeap.h"
29
#include "lldb/Utility/FileSpecList.h"
30
#include "lldb/Utility/LLDBLog.h"
31
#include "lldb/Utility/Log.h"
32
#include "lldb/Utility/RangeMap.h"
33
#include "lldb/Utility/Status.h"
34
#include "lldb/Utility/Stream.h"
35
#include "lldb/Utility/Timer.h"
36
#include "llvm/ADT/IntervalMap.h"
37
#include "llvm/ADT/PointerUnion.h"
38
#include "llvm/ADT/StringRef.h"
39
#include "llvm/BinaryFormat/ELF.h"
40
#include "llvm/Object/Decompressor.h"
41
#include "llvm/Support/ARMBuildAttributes.h"
42
#include "llvm/Support/CRC.h"
43
#include "llvm/Support/FormatVariadic.h"
44
#include "llvm/Support/MathExtras.h"
45
#include "llvm/Support/MemoryBuffer.h"
46
#include "llvm/Support/MipsABIFlags.h"
47
48
#define CASE_AND_STREAM(s, def, width) \
49
case def: \
50
s->Printf("%-*s", width, #def); \
51
break;
52
53
using namespace lldb;
54
using namespace lldb_private;
55
using namespace elf;
56
using namespace llvm::ELF;
57
58
LLDB_PLUGIN_DEFINE(ObjectFileELF)
59
60
// ELF note owner definitions
61
static const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD";
62
static const char *const LLDB_NT_OWNER_GNU = "GNU";
63
static const char *const LLDB_NT_OWNER_NETBSD = "NetBSD";
64
static const char *const LLDB_NT_OWNER_NETBSDCORE = "NetBSD-CORE";
65
static const char *const LLDB_NT_OWNER_OPENBSD = "OpenBSD";
66
static const char *const LLDB_NT_OWNER_ANDROID = "Android";
67
static const char *const LLDB_NT_OWNER_CORE = "CORE";
68
static const char *const LLDB_NT_OWNER_LINUX = "LINUX";
69
70
// ELF note type definitions
71
static const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01;
72
static const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4;
73
74
static const elf_word LLDB_NT_GNU_ABI_TAG = 0x01;
75
static const elf_word LLDB_NT_GNU_ABI_SIZE = 16;
76
77
static const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03;
78
79
static const elf_word LLDB_NT_NETBSD_IDENT_TAG = 1;
80
static const elf_word LLDB_NT_NETBSD_IDENT_DESCSZ = 4;
81
static const elf_word LLDB_NT_NETBSD_IDENT_NAMESZ = 7;
82
static const elf_word LLDB_NT_NETBSD_PROCINFO = 1;
83
84
// GNU ABI note OS constants
85
static const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00;
86
static const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01;
87
static const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02;
88
89
namespace {
90
91
//===----------------------------------------------------------------------===//
92
/// \class ELFRelocation
93
/// Generic wrapper for ELFRel and ELFRela.
94
///
95
/// This helper class allows us to parse both ELFRel and ELFRela relocation
96
/// entries in a generic manner.
97
class ELFRelocation {
98
public:
99
/// Constructs an ELFRelocation entry with a personality as given by @p
100
/// type.
101
///
102
/// \param type Either DT_REL or DT_RELA. Any other value is invalid.
103
ELFRelocation(unsigned type);
104
105
~ELFRelocation();
106
107
bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
108
109
static unsigned RelocType32(const ELFRelocation &rel);
110
111
static unsigned RelocType64(const ELFRelocation &rel);
112
113
static unsigned RelocSymbol32(const ELFRelocation &rel);
114
115
static unsigned RelocSymbol64(const ELFRelocation &rel);
116
117
static elf_addr RelocOffset32(const ELFRelocation &rel);
118
119
static elf_addr RelocOffset64(const ELFRelocation &rel);
120
121
static elf_sxword RelocAddend32(const ELFRelocation &rel);
122
123
static elf_sxword RelocAddend64(const ELFRelocation &rel);
124
125
bool IsRela() { return (reloc.is<ELFRela *>()); }
126
127
private:
128
typedef llvm::PointerUnion<ELFRel *, ELFRela *> RelocUnion;
129
130
RelocUnion reloc;
131
};
132
} // end anonymous namespace
133
134
ELFRelocation::ELFRelocation(unsigned type) {
135
if (type == DT_REL || type == SHT_REL)
136
reloc = new ELFRel();
137
else if (type == DT_RELA || type == SHT_RELA)
138
reloc = new ELFRela();
139
else {
140
assert(false && "unexpected relocation type");
141
reloc = static_cast<ELFRel *>(nullptr);
142
}
143
}
144
145
ELFRelocation::~ELFRelocation() {
146
if (reloc.is<ELFRel *>())
147
delete reloc.get<ELFRel *>();
148
else
149
delete reloc.get<ELFRela *>();
150
}
151
152
bool ELFRelocation::Parse(const lldb_private::DataExtractor &data,
153
lldb::offset_t *offset) {
154
if (reloc.is<ELFRel *>())
155
return reloc.get<ELFRel *>()->Parse(data, offset);
156
else
157
return reloc.get<ELFRela *>()->Parse(data, offset);
158
}
159
160
unsigned ELFRelocation::RelocType32(const ELFRelocation &rel) {
161
if (rel.reloc.is<ELFRel *>())
162
return ELFRel::RelocType32(*rel.reloc.get<ELFRel *>());
163
else
164
return ELFRela::RelocType32(*rel.reloc.get<ELFRela *>());
165
}
166
167
unsigned ELFRelocation::RelocType64(const ELFRelocation &rel) {
168
if (rel.reloc.is<ELFRel *>())
169
return ELFRel::RelocType64(*rel.reloc.get<ELFRel *>());
170
else
171
return ELFRela::RelocType64(*rel.reloc.get<ELFRela *>());
172
}
173
174
unsigned ELFRelocation::RelocSymbol32(const ELFRelocation &rel) {
175
if (rel.reloc.is<ELFRel *>())
176
return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel *>());
177
else
178
return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela *>());
179
}
180
181
unsigned ELFRelocation::RelocSymbol64(const ELFRelocation &rel) {
182
if (rel.reloc.is<ELFRel *>())
183
return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel *>());
184
else
185
return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela *>());
186
}
187
188
elf_addr ELFRelocation::RelocOffset32(const ELFRelocation &rel) {
189
if (rel.reloc.is<ELFRel *>())
190
return rel.reloc.get<ELFRel *>()->r_offset;
191
else
192
return rel.reloc.get<ELFRela *>()->r_offset;
193
}
194
195
elf_addr ELFRelocation::RelocOffset64(const ELFRelocation &rel) {
196
if (rel.reloc.is<ELFRel *>())
197
return rel.reloc.get<ELFRel *>()->r_offset;
198
else
199
return rel.reloc.get<ELFRela *>()->r_offset;
200
}
201
202
elf_sxword ELFRelocation::RelocAddend32(const ELFRelocation &rel) {
203
if (rel.reloc.is<ELFRel *>())
204
return 0;
205
else
206
return rel.reloc.get<ELFRela *>()->r_addend;
207
}
208
209
elf_sxword ELFRelocation::RelocAddend64(const ELFRelocation &rel) {
210
if (rel.reloc.is<ELFRel *>())
211
return 0;
212
else
213
return rel.reloc.get<ELFRela *>()->r_addend;
214
}
215
216
static user_id_t SegmentID(size_t PHdrIndex) {
217
return ~user_id_t(PHdrIndex);
218
}
219
220
bool ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) {
221
// Read all fields.
222
if (data.GetU32(offset, &n_namesz, 3) == nullptr)
223
return false;
224
225
// The name field is required to be nul-terminated, and n_namesz includes the
226
// terminating nul in observed implementations (contrary to the ELF-64 spec).
227
// A special case is needed for cores generated by some older Linux versions,
228
// which write a note named "CORE" without a nul terminator and n_namesz = 4.
229
if (n_namesz == 4) {
230
char buf[4];
231
if (data.ExtractBytes(*offset, 4, data.GetByteOrder(), buf) != 4)
232
return false;
233
if (strncmp(buf, "CORE", 4) == 0) {
234
n_name = "CORE";
235
*offset += 4;
236
return true;
237
}
238
}
239
240
const char *cstr = data.GetCStr(offset, llvm::alignTo(n_namesz, 4));
241
if (cstr == nullptr) {
242
Log *log = GetLog(LLDBLog::Symbols);
243
LLDB_LOGF(log, "Failed to parse note name lacking nul terminator");
244
245
return false;
246
}
247
n_name = cstr;
248
return true;
249
}
250
251
static uint32_t mipsVariantFromElfFlags (const elf::ELFHeader &header) {
252
const uint32_t mips_arch = header.e_flags & llvm::ELF::EF_MIPS_ARCH;
253
uint32_t endian = header.e_ident[EI_DATA];
254
uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown;
255
uint32_t fileclass = header.e_ident[EI_CLASS];
256
257
// If there aren't any elf flags available (e.g core elf file) then return
258
// default
259
// 32 or 64 bit arch (without any architecture revision) based on object file's class.
260
if (header.e_type == ET_CORE) {
261
switch (fileclass) {
262
case llvm::ELF::ELFCLASS32:
263
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el
264
: ArchSpec::eMIPSSubType_mips32;
265
case llvm::ELF::ELFCLASS64:
266
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el
267
: ArchSpec::eMIPSSubType_mips64;
268
default:
269
return arch_variant;
270
}
271
}
272
273
switch (mips_arch) {
274
case llvm::ELF::EF_MIPS_ARCH_1:
275
case llvm::ELF::EF_MIPS_ARCH_2:
276
case llvm::ELF::EF_MIPS_ARCH_32:
277
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el
278
: ArchSpec::eMIPSSubType_mips32;
279
case llvm::ELF::EF_MIPS_ARCH_32R2:
280
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el
281
: ArchSpec::eMIPSSubType_mips32r2;
282
case llvm::ELF::EF_MIPS_ARCH_32R6:
283
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el
284
: ArchSpec::eMIPSSubType_mips32r6;
285
case llvm::ELF::EF_MIPS_ARCH_3:
286
case llvm::ELF::EF_MIPS_ARCH_4:
287
case llvm::ELF::EF_MIPS_ARCH_5:
288
case llvm::ELF::EF_MIPS_ARCH_64:
289
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el
290
: ArchSpec::eMIPSSubType_mips64;
291
case llvm::ELF::EF_MIPS_ARCH_64R2:
292
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el
293
: ArchSpec::eMIPSSubType_mips64r2;
294
case llvm::ELF::EF_MIPS_ARCH_64R6:
295
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el
296
: ArchSpec::eMIPSSubType_mips64r6;
297
default:
298
break;
299
}
300
301
return arch_variant;
302
}
303
304
static uint32_t riscvVariantFromElfFlags(const elf::ELFHeader &header) {
305
uint32_t fileclass = header.e_ident[EI_CLASS];
306
switch (fileclass) {
307
case llvm::ELF::ELFCLASS32:
308
return ArchSpec::eRISCVSubType_riscv32;
309
case llvm::ELF::ELFCLASS64:
310
return ArchSpec::eRISCVSubType_riscv64;
311
default:
312
return ArchSpec::eRISCVSubType_unknown;
313
}
314
}
315
316
static uint32_t ppc64VariantFromElfFlags(const elf::ELFHeader &header) {
317
uint32_t endian = header.e_ident[EI_DATA];
318
if (endian == ELFDATA2LSB)
319
return ArchSpec::eCore_ppc64le_generic;
320
else
321
return ArchSpec::eCore_ppc64_generic;
322
}
323
324
static uint32_t loongarchVariantFromElfFlags(const elf::ELFHeader &header) {
325
uint32_t fileclass = header.e_ident[EI_CLASS];
326
switch (fileclass) {
327
case llvm::ELF::ELFCLASS32:
328
return ArchSpec::eLoongArchSubType_loongarch32;
329
case llvm::ELF::ELFCLASS64:
330
return ArchSpec::eLoongArchSubType_loongarch64;
331
default:
332
return ArchSpec::eLoongArchSubType_unknown;
333
}
334
}
335
336
static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) {
337
if (header.e_machine == llvm::ELF::EM_MIPS)
338
return mipsVariantFromElfFlags(header);
339
else if (header.e_machine == llvm::ELF::EM_PPC64)
340
return ppc64VariantFromElfFlags(header);
341
else if (header.e_machine == llvm::ELF::EM_RISCV)
342
return riscvVariantFromElfFlags(header);
343
else if (header.e_machine == llvm::ELF::EM_LOONGARCH)
344
return loongarchVariantFromElfFlags(header);
345
346
return LLDB_INVALID_CPUTYPE;
347
}
348
349
char ObjectFileELF::ID;
350
351
// Arbitrary constant used as UUID prefix for core files.
352
const uint32_t ObjectFileELF::g_core_uuid_magic(0xE210C);
353
354
// Static methods.
355
void ObjectFileELF::Initialize() {
356
PluginManager::RegisterPlugin(GetPluginNameStatic(),
357
GetPluginDescriptionStatic(), CreateInstance,
358
CreateMemoryInstance, GetModuleSpecifications);
359
}
360
361
void ObjectFileELF::Terminate() {
362
PluginManager::UnregisterPlugin(CreateInstance);
363
}
364
365
ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp,
366
DataBufferSP data_sp,
367
lldb::offset_t data_offset,
368
const lldb_private::FileSpec *file,
369
lldb::offset_t file_offset,
370
lldb::offset_t length) {
371
bool mapped_writable = false;
372
if (!data_sp) {
373
data_sp = MapFileDataWritable(*file, length, file_offset);
374
if (!data_sp)
375
return nullptr;
376
data_offset = 0;
377
mapped_writable = true;
378
}
379
380
assert(data_sp);
381
382
if (data_sp->GetByteSize() <= (llvm::ELF::EI_NIDENT + data_offset))
383
return nullptr;
384
385
const uint8_t *magic = data_sp->GetBytes() + data_offset;
386
if (!ELFHeader::MagicBytesMatch(magic))
387
return nullptr;
388
389
// Update the data to contain the entire file if it doesn't already
390
if (data_sp->GetByteSize() < length) {
391
data_sp = MapFileDataWritable(*file, length, file_offset);
392
if (!data_sp)
393
return nullptr;
394
data_offset = 0;
395
mapped_writable = true;
396
magic = data_sp->GetBytes();
397
}
398
399
// If we didn't map the data as writable take ownership of the buffer.
400
if (!mapped_writable) {
401
data_sp = std::make_shared<DataBufferHeap>(data_sp->GetBytes(),
402
data_sp->GetByteSize());
403
data_offset = 0;
404
magic = data_sp->GetBytes();
405
}
406
407
unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
408
if (address_size == 4 || address_size == 8) {
409
std::unique_ptr<ObjectFileELF> objfile_up(new ObjectFileELF(
410
module_sp, data_sp, data_offset, file, file_offset, length));
411
ArchSpec spec = objfile_up->GetArchitecture();
412
if (spec && objfile_up->SetModulesArchitecture(spec))
413
return objfile_up.release();
414
}
415
416
return nullptr;
417
}
418
419
ObjectFile *ObjectFileELF::CreateMemoryInstance(
420
const lldb::ModuleSP &module_sp, WritableDataBufferSP data_sp,
421
const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
422
if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT)) {
423
const uint8_t *magic = data_sp->GetBytes();
424
if (ELFHeader::MagicBytesMatch(magic)) {
425
unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
426
if (address_size == 4 || address_size == 8) {
427
std::unique_ptr<ObjectFileELF> objfile_up(
428
new ObjectFileELF(module_sp, data_sp, process_sp, header_addr));
429
ArchSpec spec = objfile_up->GetArchitecture();
430
if (spec && objfile_up->SetModulesArchitecture(spec))
431
return objfile_up.release();
432
}
433
}
434
}
435
return nullptr;
436
}
437
438
bool ObjectFileELF::MagicBytesMatch(DataBufferSP &data_sp,
439
lldb::addr_t data_offset,
440
lldb::addr_t data_length) {
441
if (data_sp &&
442
data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) {
443
const uint8_t *magic = data_sp->GetBytes() + data_offset;
444
return ELFHeader::MagicBytesMatch(magic);
445
}
446
return false;
447
}
448
449
static uint32_t calc_crc32(uint32_t init, const DataExtractor &data) {
450
return llvm::crc32(init,
451
llvm::ArrayRef(data.GetDataStart(), data.GetByteSize()));
452
}
453
454
uint32_t ObjectFileELF::CalculateELFNotesSegmentsCRC32(
455
const ProgramHeaderColl &program_headers, DataExtractor &object_data) {
456
457
uint32_t core_notes_crc = 0;
458
459
for (const ELFProgramHeader &H : program_headers) {
460
if (H.p_type == llvm::ELF::PT_NOTE) {
461
const elf_off ph_offset = H.p_offset;
462
const size_t ph_size = H.p_filesz;
463
464
DataExtractor segment_data;
465
if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size) {
466
// The ELF program header contained incorrect data, probably corefile
467
// is incomplete or corrupted.
468
break;
469
}
470
471
core_notes_crc = calc_crc32(core_notes_crc, segment_data);
472
}
473
}
474
475
return core_notes_crc;
476
}
477
478
static const char *OSABIAsCString(unsigned char osabi_byte) {
479
#define _MAKE_OSABI_CASE(x) \
480
case x: \
481
return #x
482
switch (osabi_byte) {
483
_MAKE_OSABI_CASE(ELFOSABI_NONE);
484
_MAKE_OSABI_CASE(ELFOSABI_HPUX);
485
_MAKE_OSABI_CASE(ELFOSABI_NETBSD);
486
_MAKE_OSABI_CASE(ELFOSABI_GNU);
487
_MAKE_OSABI_CASE(ELFOSABI_HURD);
488
_MAKE_OSABI_CASE(ELFOSABI_SOLARIS);
489
_MAKE_OSABI_CASE(ELFOSABI_AIX);
490
_MAKE_OSABI_CASE(ELFOSABI_IRIX);
491
_MAKE_OSABI_CASE(ELFOSABI_FREEBSD);
492
_MAKE_OSABI_CASE(ELFOSABI_TRU64);
493
_MAKE_OSABI_CASE(ELFOSABI_MODESTO);
494
_MAKE_OSABI_CASE(ELFOSABI_OPENBSD);
495
_MAKE_OSABI_CASE(ELFOSABI_OPENVMS);
496
_MAKE_OSABI_CASE(ELFOSABI_NSK);
497
_MAKE_OSABI_CASE(ELFOSABI_AROS);
498
_MAKE_OSABI_CASE(ELFOSABI_FENIXOS);
499
_MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI);
500
_MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX);
501
_MAKE_OSABI_CASE(ELFOSABI_ARM);
502
_MAKE_OSABI_CASE(ELFOSABI_STANDALONE);
503
default:
504
return "<unknown-osabi>";
505
}
506
#undef _MAKE_OSABI_CASE
507
}
508
509
//
510
// WARNING : This function is being deprecated
511
// It's functionality has moved to ArchSpec::SetArchitecture This function is
512
// only being kept to validate the move.
513
//
514
// TODO : Remove this function
515
static bool GetOsFromOSABI(unsigned char osabi_byte,
516
llvm::Triple::OSType &ostype) {
517
switch (osabi_byte) {
518
case ELFOSABI_AIX:
519
ostype = llvm::Triple::OSType::AIX;
520
break;
521
case ELFOSABI_FREEBSD:
522
ostype = llvm::Triple::OSType::FreeBSD;
523
break;
524
case ELFOSABI_GNU:
525
ostype = llvm::Triple::OSType::Linux;
526
break;
527
case ELFOSABI_NETBSD:
528
ostype = llvm::Triple::OSType::NetBSD;
529
break;
530
case ELFOSABI_OPENBSD:
531
ostype = llvm::Triple::OSType::OpenBSD;
532
break;
533
case ELFOSABI_SOLARIS:
534
ostype = llvm::Triple::OSType::Solaris;
535
break;
536
default:
537
ostype = llvm::Triple::OSType::UnknownOS;
538
}
539
return ostype != llvm::Triple::OSType::UnknownOS;
540
}
541
542
size_t ObjectFileELF::GetModuleSpecifications(
543
const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
544
lldb::offset_t data_offset, lldb::offset_t file_offset,
545
lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
546
Log *log = GetLog(LLDBLog::Modules);
547
548
const size_t initial_count = specs.GetSize();
549
550
if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) {
551
DataExtractor data;
552
data.SetData(data_sp);
553
elf::ELFHeader header;
554
lldb::offset_t header_offset = data_offset;
555
if (header.Parse(data, &header_offset)) {
556
if (data_sp) {
557
ModuleSpec spec(file);
558
// In Android API level 23 and above, bionic dynamic linker is able to
559
// load .so file directly from zip file. In that case, .so file is
560
// page aligned and uncompressed, and this module spec should retain the
561
// .so file offset and file size to pass through the information from
562
// lldb-server to LLDB. For normal file, file_offset should be 0,
563
// length should be the size of the file.
564
spec.SetObjectOffset(file_offset);
565
spec.SetObjectSize(length);
566
567
const uint32_t sub_type = subTypeFromElfHeader(header);
568
spec.GetArchitecture().SetArchitecture(
569
eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]);
570
571
if (spec.GetArchitecture().IsValid()) {
572
llvm::Triple::OSType ostype;
573
llvm::Triple::VendorType vendor;
574
llvm::Triple::OSType spec_ostype =
575
spec.GetArchitecture().GetTriple().getOS();
576
577
LLDB_LOGF(log, "ObjectFileELF::%s file '%s' module OSABI: %s",
578
__FUNCTION__, file.GetPath().c_str(),
579
OSABIAsCString(header.e_ident[EI_OSABI]));
580
581
// SetArchitecture should have set the vendor to unknown
582
vendor = spec.GetArchitecture().GetTriple().getVendor();
583
assert(vendor == llvm::Triple::UnknownVendor);
584
UNUSED_IF_ASSERT_DISABLED(vendor);
585
586
//
587
// Validate it is ok to remove GetOsFromOSABI
588
GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);
589
assert(spec_ostype == ostype);
590
if (spec_ostype != llvm::Triple::OSType::UnknownOS) {
591
LLDB_LOGF(log,
592
"ObjectFileELF::%s file '%s' set ELF module OS type "
593
"from ELF header OSABI.",
594
__FUNCTION__, file.GetPath().c_str());
595
}
596
597
// When ELF file does not contain GNU build ID, the later code will
598
// calculate CRC32 with this data_sp file_offset and length. It is
599
// important for Android zip .so file, which is a slice of a file,
600
// to not access the outside of the file slice range.
601
if (data_sp->GetByteSize() < length)
602
data_sp = MapFileData(file, length, file_offset);
603
if (data_sp)
604
data.SetData(data_sp);
605
// In case there is header extension in the section #0, the header we
606
// parsed above could have sentinel values for e_phnum, e_shnum, and
607
// e_shstrndx. In this case we need to reparse the header with a
608
// bigger data source to get the actual values.
609
if (header.HasHeaderExtension()) {
610
lldb::offset_t header_offset = data_offset;
611
header.Parse(data, &header_offset);
612
}
613
614
uint32_t gnu_debuglink_crc = 0;
615
std::string gnu_debuglink_file;
616
SectionHeaderColl section_headers;
617
lldb_private::UUID &uuid = spec.GetUUID();
618
619
GetSectionHeaderInfo(section_headers, data, header, uuid,
620
gnu_debuglink_file, gnu_debuglink_crc,
621
spec.GetArchitecture());
622
623
llvm::Triple &spec_triple = spec.GetArchitecture().GetTriple();
624
625
LLDB_LOGF(log,
626
"ObjectFileELF::%s file '%s' module set to triple: %s "
627
"(architecture %s)",
628
__FUNCTION__, file.GetPath().c_str(),
629
spec_triple.getTriple().c_str(),
630
spec.GetArchitecture().GetArchitectureName());
631
632
if (!uuid.IsValid()) {
633
uint32_t core_notes_crc = 0;
634
635
if (!gnu_debuglink_crc) {
636
LLDB_SCOPED_TIMERF(
637
"Calculating module crc32 %s with size %" PRIu64 " KiB",
638
file.GetFilename().AsCString(),
639
(length - file_offset) / 1024);
640
641
// For core files - which usually don't happen to have a
642
// gnu_debuglink, and are pretty bulky - calculating whole
643
// contents crc32 would be too much of luxury. Thus we will need
644
// to fallback to something simpler.
645
if (header.e_type == llvm::ELF::ET_CORE) {
646
ProgramHeaderColl program_headers;
647
GetProgramHeaderInfo(program_headers, data, header);
648
649
core_notes_crc =
650
CalculateELFNotesSegmentsCRC32(program_headers, data);
651
} else {
652
gnu_debuglink_crc = calc_crc32(0, data);
653
}
654
}
655
using u32le = llvm::support::ulittle32_t;
656
if (gnu_debuglink_crc) {
657
// Use 4 bytes of crc from the .gnu_debuglink section.
658
u32le data(gnu_debuglink_crc);
659
uuid = UUID(&data, sizeof(data));
660
} else if (core_notes_crc) {
661
// Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make
662
// it look different form .gnu_debuglink crc followed by 4 bytes
663
// of note segments crc.
664
u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};
665
uuid = UUID(data, sizeof(data));
666
}
667
}
668
669
specs.Append(spec);
670
}
671
}
672
}
673
}
674
675
return specs.GetSize() - initial_count;
676
}
677
678
// ObjectFile protocol
679
680
ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,
681
DataBufferSP data_sp, lldb::offset_t data_offset,
682
const FileSpec *file, lldb::offset_t file_offset,
683
lldb::offset_t length)
684
: ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset) {
685
if (file)
686
m_file = *file;
687
}
688
689
ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,
690
DataBufferSP header_data_sp,
691
const lldb::ProcessSP &process_sp,
692
addr_t header_addr)
693
: ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {}
694
695
bool ObjectFileELF::IsExecutable() const {
696
return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0);
697
}
698
699
bool ObjectFileELF::SetLoadAddress(Target &target, lldb::addr_t value,
700
bool value_is_offset) {
701
ModuleSP module_sp = GetModule();
702
if (module_sp) {
703
size_t num_loaded_sections = 0;
704
SectionList *section_list = GetSectionList();
705
if (section_list) {
706
if (!value_is_offset) {
707
addr_t base = GetBaseAddress().GetFileAddress();
708
if (base == LLDB_INVALID_ADDRESS)
709
return false;
710
value -= base;
711
}
712
713
const size_t num_sections = section_list->GetSize();
714
size_t sect_idx = 0;
715
716
for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
717
// Iterate through the object file sections to find all of the sections
718
// that have SHF_ALLOC in their flag bits.
719
SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
720
721
// PT_TLS segments can have the same p_vaddr and p_paddr as other
722
// PT_LOAD segments so we shouldn't load them. If we do load them, then
723
// the SectionLoadList will incorrectly fill in the instance variable
724
// SectionLoadList::m_addr_to_sect with the same address as a PT_LOAD
725
// segment and we won't be able to resolve addresses in the PT_LOAD
726
// segment whose p_vaddr entry matches that of the PT_TLS. Any variables
727
// that appear in the PT_TLS segments get resolved by the DWARF
728
// expressions. If this ever changes we will need to fix all object
729
// file plug-ins, but until then, we don't want PT_TLS segments to
730
// remove the entry from SectionLoadList::m_addr_to_sect when we call
731
// SetSectionLoadAddress() below.
732
if (section_sp->IsThreadSpecific())
733
continue;
734
if (section_sp->Test(SHF_ALLOC) ||
735
section_sp->GetType() == eSectionTypeContainer) {
736
lldb::addr_t load_addr = section_sp->GetFileAddress();
737
// We don't want to update the load address of a section with type
738
// eSectionTypeAbsoluteAddress as they already have the absolute load
739
// address already specified
740
if (section_sp->GetType() != eSectionTypeAbsoluteAddress)
741
load_addr += value;
742
743
// On 32-bit systems the load address have to fit into 4 bytes. The
744
// rest of the bytes are the overflow from the addition.
745
if (GetAddressByteSize() == 4)
746
load_addr &= 0xFFFFFFFF;
747
748
if (target.GetSectionLoadList().SetSectionLoadAddress(section_sp,
749
load_addr))
750
++num_loaded_sections;
751
}
752
}
753
return num_loaded_sections > 0;
754
}
755
}
756
return false;
757
}
758
759
ByteOrder ObjectFileELF::GetByteOrder() const {
760
if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
761
return eByteOrderBig;
762
if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
763
return eByteOrderLittle;
764
return eByteOrderInvalid;
765
}
766
767
uint32_t ObjectFileELF::GetAddressByteSize() const {
768
return m_data.GetAddressByteSize();
769
}
770
771
AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) {
772
Symtab *symtab = GetSymtab();
773
if (!symtab)
774
return AddressClass::eUnknown;
775
776
// The address class is determined based on the symtab. Ask it from the
777
// object file what contains the symtab information.
778
ObjectFile *symtab_objfile = symtab->GetObjectFile();
779
if (symtab_objfile != nullptr && symtab_objfile != this)
780
return symtab_objfile->GetAddressClass(file_addr);
781
782
auto res = ObjectFile::GetAddressClass(file_addr);
783
if (res != AddressClass::eCode)
784
return res;
785
786
auto ub = m_address_class_map.upper_bound(file_addr);
787
if (ub == m_address_class_map.begin()) {
788
// No entry in the address class map before the address. Return default
789
// address class for an address in a code section.
790
return AddressClass::eCode;
791
}
792
793
// Move iterator to the address class entry preceding address
794
--ub;
795
796
return ub->second;
797
}
798
799
size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) {
800
return std::distance(m_section_headers.begin(), I);
801
}
802
803
size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const {
804
return std::distance(m_section_headers.begin(), I);
805
}
806
807
bool ObjectFileELF::ParseHeader() {
808
lldb::offset_t offset = 0;
809
return m_header.Parse(m_data, &offset);
810
}
811
812
UUID ObjectFileELF::GetUUID() {
813
// Need to parse the section list to get the UUIDs, so make sure that's been
814
// done.
815
if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile)
816
return UUID();
817
818
if (!m_uuid) {
819
using u32le = llvm::support::ulittle32_t;
820
if (GetType() == ObjectFile::eTypeCoreFile) {
821
uint32_t core_notes_crc = 0;
822
823
if (!ParseProgramHeaders())
824
return UUID();
825
826
core_notes_crc =
827
CalculateELFNotesSegmentsCRC32(m_program_headers, m_data);
828
829
if (core_notes_crc) {
830
// Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it
831
// look different form .gnu_debuglink crc - followed by 4 bytes of note
832
// segments crc.
833
u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};
834
m_uuid = UUID(data, sizeof(data));
835
}
836
} else {
837
if (!m_gnu_debuglink_crc)
838
m_gnu_debuglink_crc = calc_crc32(0, m_data);
839
if (m_gnu_debuglink_crc) {
840
// Use 4 bytes of crc from the .gnu_debuglink section.
841
u32le data(m_gnu_debuglink_crc);
842
m_uuid = UUID(&data, sizeof(data));
843
}
844
}
845
}
846
847
return m_uuid;
848
}
849
850
std::optional<FileSpec> ObjectFileELF::GetDebugLink() {
851
if (m_gnu_debuglink_file.empty())
852
return std::nullopt;
853
return FileSpec(m_gnu_debuglink_file);
854
}
855
856
uint32_t ObjectFileELF::GetDependentModules(FileSpecList &files) {
857
size_t num_modules = ParseDependentModules();
858
uint32_t num_specs = 0;
859
860
for (unsigned i = 0; i < num_modules; ++i) {
861
if (files.AppendIfUnique(m_filespec_up->GetFileSpecAtIndex(i)))
862
num_specs++;
863
}
864
865
return num_specs;
866
}
867
868
Address ObjectFileELF::GetImageInfoAddress(Target *target) {
869
if (!ParseDynamicSymbols())
870
return Address();
871
872
SectionList *section_list = GetSectionList();
873
if (!section_list)
874
return Address();
875
876
// Find the SHT_DYNAMIC (.dynamic) section.
877
SectionSP dynsym_section_sp(
878
section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true));
879
if (!dynsym_section_sp)
880
return Address();
881
assert(dynsym_section_sp->GetObjectFile() == this);
882
883
user_id_t dynsym_id = dynsym_section_sp->GetID();
884
const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
885
if (!dynsym_hdr)
886
return Address();
887
888
for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) {
889
ELFDynamic &symbol = m_dynamic_symbols[i];
890
891
if (symbol.d_tag == DT_DEBUG) {
892
// Compute the offset as the number of previous entries plus the size of
893
// d_tag.
894
addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
895
return Address(dynsym_section_sp, offset);
896
}
897
// MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP
898
// exists in non-PIE.
899
else if ((symbol.d_tag == DT_MIPS_RLD_MAP ||
900
symbol.d_tag == DT_MIPS_RLD_MAP_REL) &&
901
target) {
902
addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
903
addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);
904
if (dyn_base == LLDB_INVALID_ADDRESS)
905
return Address();
906
907
Status error;
908
if (symbol.d_tag == DT_MIPS_RLD_MAP) {
909
// DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer.
910
Address addr;
911
if (target->ReadPointerFromMemory(dyn_base + offset, error, addr, true))
912
return addr;
913
}
914
if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) {
915
// DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
916
// relative to the address of the tag.
917
uint64_t rel_offset;
918
rel_offset = target->ReadUnsignedIntegerFromMemory(
919
dyn_base + offset, GetAddressByteSize(), UINT64_MAX, error, true);
920
if (error.Success() && rel_offset != UINT64_MAX) {
921
Address addr;
922
addr_t debug_ptr_address =
923
dyn_base + (offset - GetAddressByteSize()) + rel_offset;
924
addr.SetOffset(debug_ptr_address);
925
return addr;
926
}
927
}
928
}
929
}
930
931
return Address();
932
}
933
934
lldb_private::Address ObjectFileELF::GetEntryPointAddress() {
935
if (m_entry_point_address.IsValid())
936
return m_entry_point_address;
937
938
if (!ParseHeader() || !IsExecutable())
939
return m_entry_point_address;
940
941
SectionList *section_list = GetSectionList();
942
addr_t offset = m_header.e_entry;
943
944
if (!section_list)
945
m_entry_point_address.SetOffset(offset);
946
else
947
m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
948
return m_entry_point_address;
949
}
950
951
Address ObjectFileELF::GetBaseAddress() {
952
if (GetType() == ObjectFile::eTypeObjectFile) {
953
for (SectionHeaderCollIter I = std::next(m_section_headers.begin());
954
I != m_section_headers.end(); ++I) {
955
const ELFSectionHeaderInfo &header = *I;
956
if (header.sh_flags & SHF_ALLOC)
957
return Address(GetSectionList()->FindSectionByID(SectionIndex(I)), 0);
958
}
959
return LLDB_INVALID_ADDRESS;
960
}
961
962
for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {
963
const ELFProgramHeader &H = EnumPHdr.value();
964
if (H.p_type != PT_LOAD)
965
continue;
966
967
return Address(
968
GetSectionList()->FindSectionByID(SegmentID(EnumPHdr.index())), 0);
969
}
970
return LLDB_INVALID_ADDRESS;
971
}
972
973
// ParseDependentModules
974
size_t ObjectFileELF::ParseDependentModules() {
975
if (m_filespec_up)
976
return m_filespec_up->GetSize();
977
978
m_filespec_up = std::make_unique<FileSpecList>();
979
980
if (!ParseSectionHeaders())
981
return 0;
982
983
SectionList *section_list = GetSectionList();
984
if (!section_list)
985
return 0;
986
987
// Find the SHT_DYNAMIC section.
988
Section *dynsym =
989
section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)
990
.get();
991
if (!dynsym)
992
return 0;
993
assert(dynsym->GetObjectFile() == this);
994
995
const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex(dynsym->GetID());
996
if (!header)
997
return 0;
998
// sh_link: section header index of string table used by entries in the
999
// section.
1000
Section *dynstr = section_list->FindSectionByID(header->sh_link).get();
1001
if (!dynstr)
1002
return 0;
1003
1004
DataExtractor dynsym_data;
1005
DataExtractor dynstr_data;
1006
if (ReadSectionData(dynsym, dynsym_data) &&
1007
ReadSectionData(dynstr, dynstr_data)) {
1008
ELFDynamic symbol;
1009
const lldb::offset_t section_size = dynsym_data.GetByteSize();
1010
lldb::offset_t offset = 0;
1011
1012
// The only type of entries we are concerned with are tagged DT_NEEDED,
1013
// yielding the name of a required library.
1014
while (offset < section_size) {
1015
if (!symbol.Parse(dynsym_data, &offset))
1016
break;
1017
1018
if (symbol.d_tag != DT_NEEDED)
1019
continue;
1020
1021
uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
1022
const char *lib_name = dynstr_data.PeekCStr(str_index);
1023
FileSpec file_spec(lib_name);
1024
FileSystem::Instance().Resolve(file_spec);
1025
m_filespec_up->Append(file_spec);
1026
}
1027
}
1028
1029
return m_filespec_up->GetSize();
1030
}
1031
1032
// GetProgramHeaderInfo
1033
size_t ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
1034
DataExtractor &object_data,
1035
const ELFHeader &header) {
1036
// We have already parsed the program headers
1037
if (!program_headers.empty())
1038
return program_headers.size();
1039
1040
// If there are no program headers to read we are done.
1041
if (header.e_phnum == 0)
1042
return 0;
1043
1044
program_headers.resize(header.e_phnum);
1045
if (program_headers.size() != header.e_phnum)
1046
return 0;
1047
1048
const size_t ph_size = header.e_phnum * header.e_phentsize;
1049
const elf_off ph_offset = header.e_phoff;
1050
DataExtractor data;
1051
if (data.SetData(object_data, ph_offset, ph_size) != ph_size)
1052
return 0;
1053
1054
uint32_t idx;
1055
lldb::offset_t offset;
1056
for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) {
1057
if (!program_headers[idx].Parse(data, &offset))
1058
break;
1059
}
1060
1061
if (idx < program_headers.size())
1062
program_headers.resize(idx);
1063
1064
return program_headers.size();
1065
}
1066
1067
// ParseProgramHeaders
1068
bool ObjectFileELF::ParseProgramHeaders() {
1069
return GetProgramHeaderInfo(m_program_headers, m_data, m_header) != 0;
1070
}
1071
1072
lldb_private::Status
1073
ObjectFileELF::RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,
1074
lldb_private::ArchSpec &arch_spec,
1075
lldb_private::UUID &uuid) {
1076
Log *log = GetLog(LLDBLog::Modules);
1077
Status error;
1078
1079
lldb::offset_t offset = 0;
1080
1081
while (true) {
1082
// Parse the note header. If this fails, bail out.
1083
const lldb::offset_t note_offset = offset;
1084
ELFNote note = ELFNote();
1085
if (!note.Parse(data, &offset)) {
1086
// We're done.
1087
return error;
1088
}
1089
1090
LLDB_LOGF(log, "ObjectFileELF::%s parsing note name='%s', type=%" PRIu32,
1091
__FUNCTION__, note.n_name.c_str(), note.n_type);
1092
1093
// Process FreeBSD ELF notes.
1094
if ((note.n_name == LLDB_NT_OWNER_FREEBSD) &&
1095
(note.n_type == LLDB_NT_FREEBSD_ABI_TAG) &&
1096
(note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) {
1097
// Pull out the min version info.
1098
uint32_t version_info;
1099
if (data.GetU32(&offset, &version_info, 1) == nullptr) {
1100
error.SetErrorString("failed to read FreeBSD ABI note payload");
1101
return error;
1102
}
1103
1104
// Convert the version info into a major/minor number.
1105
const uint32_t version_major = version_info / 100000;
1106
const uint32_t version_minor = (version_info / 1000) % 100;
1107
1108
char os_name[32];
1109
snprintf(os_name, sizeof(os_name), "freebsd%" PRIu32 ".%" PRIu32,
1110
version_major, version_minor);
1111
1112
// Set the elf OS version to FreeBSD. Also clear the vendor.
1113
arch_spec.GetTriple().setOSName(os_name);
1114
arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1115
1116
LLDB_LOGF(log,
1117
"ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32
1118
".%" PRIu32,
1119
__FUNCTION__, version_major, version_minor,
1120
static_cast<uint32_t>(version_info % 1000));
1121
}
1122
// Process GNU ELF notes.
1123
else if (note.n_name == LLDB_NT_OWNER_GNU) {
1124
switch (note.n_type) {
1125
case LLDB_NT_GNU_ABI_TAG:
1126
if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) {
1127
// Pull out the min OS version supporting the ABI.
1128
uint32_t version_info[4];
1129
if (data.GetU32(&offset, &version_info[0], note.n_descsz / 4) ==
1130
nullptr) {
1131
error.SetErrorString("failed to read GNU ABI note payload");
1132
return error;
1133
}
1134
1135
// Set the OS per the OS field.
1136
switch (version_info[0]) {
1137
case LLDB_NT_GNU_ABI_OS_LINUX:
1138
arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1139
arch_spec.GetTriple().setVendor(
1140
llvm::Triple::VendorType::UnknownVendor);
1141
LLDB_LOGF(log,
1142
"ObjectFileELF::%s detected Linux, min version %" PRIu32
1143
".%" PRIu32 ".%" PRIu32,
1144
__FUNCTION__, version_info[1], version_info[2],
1145
version_info[3]);
1146
// FIXME we have the minimal version number, we could be propagating
1147
// that. version_info[1] = OS Major, version_info[2] = OS Minor,
1148
// version_info[3] = Revision.
1149
break;
1150
case LLDB_NT_GNU_ABI_OS_HURD:
1151
arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS);
1152
arch_spec.GetTriple().setVendor(
1153
llvm::Triple::VendorType::UnknownVendor);
1154
LLDB_LOGF(log,
1155
"ObjectFileELF::%s detected Hurd (unsupported), min "
1156
"version %" PRIu32 ".%" PRIu32 ".%" PRIu32,
1157
__FUNCTION__, version_info[1], version_info[2],
1158
version_info[3]);
1159
break;
1160
case LLDB_NT_GNU_ABI_OS_SOLARIS:
1161
arch_spec.GetTriple().setOS(llvm::Triple::OSType::Solaris);
1162
arch_spec.GetTriple().setVendor(
1163
llvm::Triple::VendorType::UnknownVendor);
1164
LLDB_LOGF(log,
1165
"ObjectFileELF::%s detected Solaris, min version %" PRIu32
1166
".%" PRIu32 ".%" PRIu32,
1167
__FUNCTION__, version_info[1], version_info[2],
1168
version_info[3]);
1169
break;
1170
default:
1171
LLDB_LOGF(log,
1172
"ObjectFileELF::%s unrecognized OS in note, id %" PRIu32
1173
", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32,
1174
__FUNCTION__, version_info[0], version_info[1],
1175
version_info[2], version_info[3]);
1176
break;
1177
}
1178
}
1179
break;
1180
1181
case LLDB_NT_GNU_BUILD_ID_TAG:
1182
// Only bother processing this if we don't already have the uuid set.
1183
if (!uuid.IsValid()) {
1184
// 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a
1185
// build-id of a different length. Accept it as long as it's at least
1186
// 4 bytes as it will be better than our own crc32.
1187
if (note.n_descsz >= 4) {
1188
if (const uint8_t *buf = data.PeekData(offset, note.n_descsz)) {
1189
// Save the build id as the UUID for the module.
1190
uuid = UUID(buf, note.n_descsz);
1191
} else {
1192
error.SetErrorString("failed to read GNU_BUILD_ID note payload");
1193
return error;
1194
}
1195
}
1196
}
1197
break;
1198
}
1199
if (arch_spec.IsMIPS() &&
1200
arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS)
1201
// The note.n_name == LLDB_NT_OWNER_GNU is valid for Linux platform
1202
arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1203
}
1204
// Process NetBSD ELF executables and shared libraries
1205
else if ((note.n_name == LLDB_NT_OWNER_NETBSD) &&
1206
(note.n_type == LLDB_NT_NETBSD_IDENT_TAG) &&
1207
(note.n_descsz == LLDB_NT_NETBSD_IDENT_DESCSZ) &&
1208
(note.n_namesz == LLDB_NT_NETBSD_IDENT_NAMESZ)) {
1209
// Pull out the version info.
1210
uint32_t version_info;
1211
if (data.GetU32(&offset, &version_info, 1) == nullptr) {
1212
error.SetErrorString("failed to read NetBSD ABI note payload");
1213
return error;
1214
}
1215
// Convert the version info into a major/minor/patch number.
1216
// #define __NetBSD_Version__ MMmmrrpp00
1217
//
1218
// M = major version
1219
// m = minor version; a minor number of 99 indicates current.
1220
// r = 0 (since NetBSD 3.0 not used)
1221
// p = patchlevel
1222
const uint32_t version_major = version_info / 100000000;
1223
const uint32_t version_minor = (version_info % 100000000) / 1000000;
1224
const uint32_t version_patch = (version_info % 10000) / 100;
1225
// Set the elf OS version to NetBSD. Also clear the vendor.
1226
arch_spec.GetTriple().setOSName(
1227
llvm::formatv("netbsd{0}.{1}.{2}", version_major, version_minor,
1228
version_patch).str());
1229
arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1230
}
1231
// Process NetBSD ELF core(5) notes
1232
else if ((note.n_name == LLDB_NT_OWNER_NETBSDCORE) &&
1233
(note.n_type == LLDB_NT_NETBSD_PROCINFO)) {
1234
// Set the elf OS version to NetBSD. Also clear the vendor.
1235
arch_spec.GetTriple().setOS(llvm::Triple::OSType::NetBSD);
1236
arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1237
}
1238
// Process OpenBSD ELF notes.
1239
else if (note.n_name == LLDB_NT_OWNER_OPENBSD) {
1240
// Set the elf OS version to OpenBSD. Also clear the vendor.
1241
arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD);
1242
arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1243
} else if (note.n_name == LLDB_NT_OWNER_ANDROID) {
1244
arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1245
arch_spec.GetTriple().setEnvironment(
1246
llvm::Triple::EnvironmentType::Android);
1247
} else if (note.n_name == LLDB_NT_OWNER_LINUX) {
1248
// This is sometimes found in core files and usually contains extended
1249
// register info
1250
arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1251
} else if (note.n_name == LLDB_NT_OWNER_CORE) {
1252
// Parse the NT_FILE to look for stuff in paths to shared libraries
1253
// The contents look like this in a 64 bit ELF core file:
1254
//
1255
// count = 0x000000000000000a (10)
1256
// page_size = 0x0000000000001000 (4096)
1257
// Index start end file_ofs path
1258
// ===== ------------------ ------------------ ------------------ -------------------------------------
1259
// [ 0] 0x0000000000401000 0x0000000000000000 /tmp/a.out
1260
// [ 1] 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out
1261
// [ 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out
1262
// [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 /lib/x86_64-linux-gnu/libc-2.19.so
1263
// [ 4] 0x00007fa79cba8000 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux-gnu/libc-2.19.so
1264
// [ 5] 0x00007fa79cda7000 0x00007fa79cdab000 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so
1265
// [ 6] 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64-linux-gnu/libc-2.19.so
1266
// [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so
1267
// [ 8] 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64-linux-gnu/ld-2.19.so
1268
// [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so
1269
//
1270
// In the 32 bit ELFs the count, page_size, start, end, file_ofs are
1271
// uint32_t.
1272
//
1273
// For reference: see readelf source code (in binutils).
1274
if (note.n_type == NT_FILE) {
1275
uint64_t count = data.GetAddress(&offset);
1276
const char *cstr;
1277
data.GetAddress(&offset); // Skip page size
1278
offset += count * 3 *
1279
data.GetAddressByteSize(); // Skip all start/end/file_ofs
1280
for (size_t i = 0; i < count; ++i) {
1281
cstr = data.GetCStr(&offset);
1282
if (cstr == nullptr) {
1283
error.SetErrorStringWithFormat("ObjectFileELF::%s trying to read "
1284
"at an offset after the end "
1285
"(GetCStr returned nullptr)",
1286
__FUNCTION__);
1287
return error;
1288
}
1289
llvm::StringRef path(cstr);
1290
if (path.contains("/lib/x86_64-linux-gnu") || path.contains("/lib/i386-linux-gnu")) {
1291
arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1292
break;
1293
}
1294
}
1295
if (arch_spec.IsMIPS() &&
1296
arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS)
1297
// In case of MIPSR6, the LLDB_NT_OWNER_GNU note is missing for some
1298
// cases (e.g. compile with -nostdlib) Hence set OS to Linux
1299
arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1300
}
1301
}
1302
1303
// Calculate the offset of the next note just in case "offset" has been
1304
// used to poke at the contents of the note data
1305
offset = note_offset + note.GetByteSize();
1306
}
1307
1308
return error;
1309
}
1310
1311
void ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length,
1312
ArchSpec &arch_spec) {
1313
lldb::offset_t Offset = 0;
1314
1315
uint8_t FormatVersion = data.GetU8(&Offset);
1316
if (FormatVersion != llvm::ELFAttrs::Format_Version)
1317
return;
1318
1319
Offset = Offset + sizeof(uint32_t); // Section Length
1320
llvm::StringRef VendorName = data.GetCStr(&Offset);
1321
1322
if (VendorName != "aeabi")
1323
return;
1324
1325
if (arch_spec.GetTriple().getEnvironment() ==
1326
llvm::Triple::UnknownEnvironment)
1327
arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);
1328
1329
while (Offset < length) {
1330
uint8_t Tag = data.GetU8(&Offset);
1331
uint32_t Size = data.GetU32(&Offset);
1332
1333
if (Tag != llvm::ARMBuildAttrs::File || Size == 0)
1334
continue;
1335
1336
while (Offset < length) {
1337
uint64_t Tag = data.GetULEB128(&Offset);
1338
switch (Tag) {
1339
default:
1340
if (Tag < 32)
1341
data.GetULEB128(&Offset);
1342
else if (Tag % 2 == 0)
1343
data.GetULEB128(&Offset);
1344
else
1345
data.GetCStr(&Offset);
1346
1347
break;
1348
1349
case llvm::ARMBuildAttrs::CPU_raw_name:
1350
case llvm::ARMBuildAttrs::CPU_name:
1351
data.GetCStr(&Offset);
1352
1353
break;
1354
1355
case llvm::ARMBuildAttrs::ABI_VFP_args: {
1356
uint64_t VFPArgs = data.GetULEB128(&Offset);
1357
1358
if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS) {
1359
if (arch_spec.GetTriple().getEnvironment() ==
1360
llvm::Triple::UnknownEnvironment ||
1361
arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF)
1362
arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);
1363
1364
arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);
1365
} else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS) {
1366
if (arch_spec.GetTriple().getEnvironment() ==
1367
llvm::Triple::UnknownEnvironment ||
1368
arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI)
1369
arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF);
1370
1371
arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);
1372
}
1373
1374
break;
1375
}
1376
}
1377
}
1378
}
1379
}
1380
1381
// GetSectionHeaderInfo
1382
size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl &section_headers,
1383
DataExtractor &object_data,
1384
const elf::ELFHeader &header,
1385
lldb_private::UUID &uuid,
1386
std::string &gnu_debuglink_file,
1387
uint32_t &gnu_debuglink_crc,
1388
ArchSpec &arch_spec) {
1389
// Don't reparse the section headers if we already did that.
1390
if (!section_headers.empty())
1391
return section_headers.size();
1392
1393
// Only initialize the arch_spec to okay defaults if they're not already set.
1394
// We'll refine this with note data as we parse the notes.
1395
if (arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) {
1396
llvm::Triple::OSType ostype;
1397
llvm::Triple::OSType spec_ostype;
1398
const uint32_t sub_type = subTypeFromElfHeader(header);
1399
arch_spec.SetArchitecture(eArchTypeELF, header.e_machine, sub_type,
1400
header.e_ident[EI_OSABI]);
1401
1402
// Validate if it is ok to remove GetOsFromOSABI. Note, that now the OS is
1403
// determined based on EI_OSABI flag and the info extracted from ELF notes
1404
// (see RefineModuleDetailsFromNote). However in some cases that still
1405
// might be not enough: for example a shared library might not have any
1406
// notes at all and have EI_OSABI flag set to System V, as result the OS
1407
// will be set to UnknownOS.
1408
GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);
1409
spec_ostype = arch_spec.GetTriple().getOS();
1410
assert(spec_ostype == ostype);
1411
UNUSED_IF_ASSERT_DISABLED(spec_ostype);
1412
}
1413
1414
if (arch_spec.GetMachine() == llvm::Triple::mips ||
1415
arch_spec.GetMachine() == llvm::Triple::mipsel ||
1416
arch_spec.GetMachine() == llvm::Triple::mips64 ||
1417
arch_spec.GetMachine() == llvm::Triple::mips64el) {
1418
switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) {
1419
case llvm::ELF::EF_MIPS_MICROMIPS:
1420
arch_spec.SetFlags(ArchSpec::eMIPSAse_micromips);
1421
break;
1422
case llvm::ELF::EF_MIPS_ARCH_ASE_M16:
1423
arch_spec.SetFlags(ArchSpec::eMIPSAse_mips16);
1424
break;
1425
case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX:
1426
arch_spec.SetFlags(ArchSpec::eMIPSAse_mdmx);
1427
break;
1428
default:
1429
break;
1430
}
1431
}
1432
1433
if (arch_spec.GetMachine() == llvm::Triple::arm ||
1434
arch_spec.GetMachine() == llvm::Triple::thumb) {
1435
if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT)
1436
arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);
1437
else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT)
1438
arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);
1439
}
1440
1441
if (arch_spec.GetMachine() == llvm::Triple::riscv32 ||
1442
arch_spec.GetMachine() == llvm::Triple::riscv64) {
1443
uint32_t flags = arch_spec.GetFlags();
1444
1445
if (header.e_flags & llvm::ELF::EF_RISCV_RVC)
1446
flags |= ArchSpec::eRISCV_rvc;
1447
if (header.e_flags & llvm::ELF::EF_RISCV_RVE)
1448
flags |= ArchSpec::eRISCV_rve;
1449
1450
if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE) ==
1451
llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE)
1452
flags |= ArchSpec::eRISCV_float_abi_single;
1453
else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE) ==
1454
llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE)
1455
flags |= ArchSpec::eRISCV_float_abi_double;
1456
else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD) ==
1457
llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD)
1458
flags |= ArchSpec::eRISCV_float_abi_quad;
1459
1460
arch_spec.SetFlags(flags);
1461
}
1462
1463
// If there are no section headers we are done.
1464
if (header.e_shnum == 0)
1465
return 0;
1466
1467
Log *log = GetLog(LLDBLog::Modules);
1468
1469
section_headers.resize(header.e_shnum);
1470
if (section_headers.size() != header.e_shnum)
1471
return 0;
1472
1473
const size_t sh_size = header.e_shnum * header.e_shentsize;
1474
const elf_off sh_offset = header.e_shoff;
1475
DataExtractor sh_data;
1476
if (sh_data.SetData(object_data, sh_offset, sh_size) != sh_size)
1477
return 0;
1478
1479
uint32_t idx;
1480
lldb::offset_t offset;
1481
for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) {
1482
if (!section_headers[idx].Parse(sh_data, &offset))
1483
break;
1484
}
1485
if (idx < section_headers.size())
1486
section_headers.resize(idx);
1487
1488
const unsigned strtab_idx = header.e_shstrndx;
1489
if (strtab_idx && strtab_idx < section_headers.size()) {
1490
const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx];
1491
const size_t byte_size = sheader.sh_size;
1492
const Elf64_Off offset = sheader.sh_offset;
1493
lldb_private::DataExtractor shstr_data;
1494
1495
if (shstr_data.SetData(object_data, offset, byte_size) == byte_size) {
1496
for (SectionHeaderCollIter I = section_headers.begin();
1497
I != section_headers.end(); ++I) {
1498
static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink");
1499
const ELFSectionHeaderInfo &sheader = *I;
1500
const uint64_t section_size =
1501
sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size;
1502
ConstString name(shstr_data.PeekCStr(I->sh_name));
1503
1504
I->section_name = name;
1505
1506
if (arch_spec.IsMIPS()) {
1507
uint32_t arch_flags = arch_spec.GetFlags();
1508
DataExtractor data;
1509
if (sheader.sh_type == SHT_MIPS_ABIFLAGS) {
1510
1511
if (section_size && (data.SetData(object_data, sheader.sh_offset,
1512
section_size) == section_size)) {
1513
// MIPS ASE Mask is at offset 12 in MIPS.abiflags section
1514
lldb::offset_t offset = 12; // MIPS ABI Flags Version: 0
1515
arch_flags |= data.GetU32(&offset);
1516
1517
// The floating point ABI is at offset 7
1518
offset = 7;
1519
switch (data.GetU8(&offset)) {
1520
case llvm::Mips::Val_GNU_MIPS_ABI_FP_ANY:
1521
arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_ANY;
1522
break;
1523
case llvm::Mips::Val_GNU_MIPS_ABI_FP_DOUBLE:
1524
arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_DOUBLE;
1525
break;
1526
case llvm::Mips::Val_GNU_MIPS_ABI_FP_SINGLE:
1527
arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SINGLE;
1528
break;
1529
case llvm::Mips::Val_GNU_MIPS_ABI_FP_SOFT:
1530
arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT;
1531
break;
1532
case llvm::Mips::Val_GNU_MIPS_ABI_FP_OLD_64:
1533
arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_OLD_64;
1534
break;
1535
case llvm::Mips::Val_GNU_MIPS_ABI_FP_XX:
1536
arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_XX;
1537
break;
1538
case llvm::Mips::Val_GNU_MIPS_ABI_FP_64:
1539
arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64;
1540
break;
1541
case llvm::Mips::Val_GNU_MIPS_ABI_FP_64A:
1542
arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64A;
1543
break;
1544
}
1545
}
1546
}
1547
// Settings appropriate ArchSpec ABI Flags
1548
switch (header.e_flags & llvm::ELF::EF_MIPS_ABI) {
1549
case llvm::ELF::EF_MIPS_ABI_O32:
1550
arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32;
1551
break;
1552
case EF_MIPS_ABI_O64:
1553
arch_flags |= lldb_private::ArchSpec::eMIPSABI_O64;
1554
break;
1555
case EF_MIPS_ABI_EABI32:
1556
arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI32;
1557
break;
1558
case EF_MIPS_ABI_EABI64:
1559
arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI64;
1560
break;
1561
default:
1562
// ABI Mask doesn't cover N32 and N64 ABI.
1563
if (header.e_ident[EI_CLASS] == llvm::ELF::ELFCLASS64)
1564
arch_flags |= lldb_private::ArchSpec::eMIPSABI_N64;
1565
else if (header.e_flags & llvm::ELF::EF_MIPS_ABI2)
1566
arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32;
1567
break;
1568
}
1569
arch_spec.SetFlags(arch_flags);
1570
}
1571
1572
if (arch_spec.GetMachine() == llvm::Triple::arm ||
1573
arch_spec.GetMachine() == llvm::Triple::thumb) {
1574
DataExtractor data;
1575
1576
if (sheader.sh_type == SHT_ARM_ATTRIBUTES && section_size != 0 &&
1577
data.SetData(object_data, sheader.sh_offset, section_size) == section_size)
1578
ParseARMAttributes(data, section_size, arch_spec);
1579
}
1580
1581
if (name == g_sect_name_gnu_debuglink) {
1582
DataExtractor data;
1583
if (section_size && (data.SetData(object_data, sheader.sh_offset,
1584
section_size) == section_size)) {
1585
lldb::offset_t gnu_debuglink_offset = 0;
1586
gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset);
1587
gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4);
1588
data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
1589
}
1590
}
1591
1592
// Process ELF note section entries.
1593
bool is_note_header = (sheader.sh_type == SHT_NOTE);
1594
1595
// The section header ".note.android.ident" is stored as a
1596
// PROGBITS type header but it is actually a note header.
1597
static ConstString g_sect_name_android_ident(".note.android.ident");
1598
if (!is_note_header && name == g_sect_name_android_ident)
1599
is_note_header = true;
1600
1601
if (is_note_header) {
1602
// Allow notes to refine module info.
1603
DataExtractor data;
1604
if (section_size && (data.SetData(object_data, sheader.sh_offset,
1605
section_size) == section_size)) {
1606
Status error = RefineModuleDetailsFromNote(data, arch_spec, uuid);
1607
if (error.Fail()) {
1608
LLDB_LOGF(log, "ObjectFileELF::%s ELF note processing failed: %s",
1609
__FUNCTION__, error.AsCString());
1610
}
1611
}
1612
}
1613
}
1614
1615
// Make any unknown triple components to be unspecified unknowns.
1616
if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
1617
arch_spec.GetTriple().setVendorName(llvm::StringRef());
1618
if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS)
1619
arch_spec.GetTriple().setOSName(llvm::StringRef());
1620
1621
return section_headers.size();
1622
}
1623
}
1624
1625
section_headers.clear();
1626
return 0;
1627
}
1628
1629
llvm::StringRef
1630
ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const {
1631
size_t pos = symbol_name.find('@');
1632
return symbol_name.substr(0, pos);
1633
}
1634
1635
// ParseSectionHeaders
1636
size_t ObjectFileELF::ParseSectionHeaders() {
1637
return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid,
1638
m_gnu_debuglink_file, m_gnu_debuglink_crc,
1639
m_arch_spec);
1640
}
1641
1642
const ObjectFileELF::ELFSectionHeaderInfo *
1643
ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) {
1644
if (!ParseSectionHeaders())
1645
return nullptr;
1646
1647
if (id < m_section_headers.size())
1648
return &m_section_headers[id];
1649
1650
return nullptr;
1651
}
1652
1653
lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) {
1654
if (!name || !name[0] || !ParseSectionHeaders())
1655
return 0;
1656
for (size_t i = 1; i < m_section_headers.size(); ++i)
1657
if (m_section_headers[i].section_name == ConstString(name))
1658
return i;
1659
return 0;
1660
}
1661
1662
static SectionType GetSectionTypeFromName(llvm::StringRef Name) {
1663
if (Name.consume_front(".debug_")) {
1664
return llvm::StringSwitch<SectionType>(Name)
1665
.Case("abbrev", eSectionTypeDWARFDebugAbbrev)
1666
.Case("abbrev.dwo", eSectionTypeDWARFDebugAbbrevDwo)
1667
.Case("addr", eSectionTypeDWARFDebugAddr)
1668
.Case("aranges", eSectionTypeDWARFDebugAranges)
1669
.Case("cu_index", eSectionTypeDWARFDebugCuIndex)
1670
.Case("frame", eSectionTypeDWARFDebugFrame)
1671
.Case("info", eSectionTypeDWARFDebugInfo)
1672
.Case("info.dwo", eSectionTypeDWARFDebugInfoDwo)
1673
.Cases("line", "line.dwo", eSectionTypeDWARFDebugLine)
1674
.Cases("line_str", "line_str.dwo", eSectionTypeDWARFDebugLineStr)
1675
.Case("loc", eSectionTypeDWARFDebugLoc)
1676
.Case("loc.dwo", eSectionTypeDWARFDebugLocDwo)
1677
.Case("loclists", eSectionTypeDWARFDebugLocLists)
1678
.Case("loclists.dwo", eSectionTypeDWARFDebugLocListsDwo)
1679
.Case("macinfo", eSectionTypeDWARFDebugMacInfo)
1680
.Cases("macro", "macro.dwo", eSectionTypeDWARFDebugMacro)
1681
.Case("names", eSectionTypeDWARFDebugNames)
1682
.Case("pubnames", eSectionTypeDWARFDebugPubNames)
1683
.Case("pubtypes", eSectionTypeDWARFDebugPubTypes)
1684
.Case("ranges", eSectionTypeDWARFDebugRanges)
1685
.Case("rnglists", eSectionTypeDWARFDebugRngLists)
1686
.Case("rnglists.dwo", eSectionTypeDWARFDebugRngListsDwo)
1687
.Case("str", eSectionTypeDWARFDebugStr)
1688
.Case("str.dwo", eSectionTypeDWARFDebugStrDwo)
1689
.Case("str_offsets", eSectionTypeDWARFDebugStrOffsets)
1690
.Case("str_offsets.dwo", eSectionTypeDWARFDebugStrOffsetsDwo)
1691
.Case("tu_index", eSectionTypeDWARFDebugTuIndex)
1692
.Case("types", eSectionTypeDWARFDebugTypes)
1693
.Case("types.dwo", eSectionTypeDWARFDebugTypesDwo)
1694
.Default(eSectionTypeOther);
1695
}
1696
return llvm::StringSwitch<SectionType>(Name)
1697
.Case(".ARM.exidx", eSectionTypeARMexidx)
1698
.Case(".ARM.extab", eSectionTypeARMextab)
1699
.Case(".ctf", eSectionTypeDebug)
1700
.Cases(".data", ".tdata", eSectionTypeData)
1701
.Case(".eh_frame", eSectionTypeEHFrame)
1702
.Case(".gnu_debugaltlink", eSectionTypeDWARFGNUDebugAltLink)
1703
.Case(".gosymtab", eSectionTypeGoSymtab)
1704
.Case(".text", eSectionTypeCode)
1705
.Case(".swift_ast", eSectionTypeSwiftModules)
1706
.Default(eSectionTypeOther);
1707
}
1708
1709
SectionType ObjectFileELF::GetSectionType(const ELFSectionHeaderInfo &H) const {
1710
switch (H.sh_type) {
1711
case SHT_PROGBITS:
1712
if (H.sh_flags & SHF_EXECINSTR)
1713
return eSectionTypeCode;
1714
break;
1715
case SHT_NOBITS:
1716
if (H.sh_flags & SHF_ALLOC)
1717
return eSectionTypeZeroFill;
1718
break;
1719
case SHT_SYMTAB:
1720
return eSectionTypeELFSymbolTable;
1721
case SHT_DYNSYM:
1722
return eSectionTypeELFDynamicSymbols;
1723
case SHT_RELA:
1724
case SHT_REL:
1725
return eSectionTypeELFRelocationEntries;
1726
case SHT_DYNAMIC:
1727
return eSectionTypeELFDynamicLinkInfo;
1728
}
1729
return GetSectionTypeFromName(H.section_name.GetStringRef());
1730
}
1731
1732
static uint32_t GetTargetByteSize(SectionType Type, const ArchSpec &arch) {
1733
switch (Type) {
1734
case eSectionTypeData:
1735
case eSectionTypeZeroFill:
1736
return arch.GetDataByteSize();
1737
case eSectionTypeCode:
1738
return arch.GetCodeByteSize();
1739
default:
1740
return 1;
1741
}
1742
}
1743
1744
static Permissions GetPermissions(const ELFSectionHeader &H) {
1745
Permissions Perm = Permissions(0);
1746
if (H.sh_flags & SHF_ALLOC)
1747
Perm |= ePermissionsReadable;
1748
if (H.sh_flags & SHF_WRITE)
1749
Perm |= ePermissionsWritable;
1750
if (H.sh_flags & SHF_EXECINSTR)
1751
Perm |= ePermissionsExecutable;
1752
return Perm;
1753
}
1754
1755
static Permissions GetPermissions(const ELFProgramHeader &H) {
1756
Permissions Perm = Permissions(0);
1757
if (H.p_flags & PF_R)
1758
Perm |= ePermissionsReadable;
1759
if (H.p_flags & PF_W)
1760
Perm |= ePermissionsWritable;
1761
if (H.p_flags & PF_X)
1762
Perm |= ePermissionsExecutable;
1763
return Perm;
1764
}
1765
1766
namespace {
1767
1768
using VMRange = lldb_private::Range<addr_t, addr_t>;
1769
1770
struct SectionAddressInfo {
1771
SectionSP Segment;
1772
VMRange Range;
1773
};
1774
1775
// (Unlinked) ELF object files usually have 0 for every section address, meaning
1776
// we need to compute synthetic addresses in order for "file addresses" from
1777
// different sections to not overlap. This class handles that logic.
1778
class VMAddressProvider {
1779
using VMMap = llvm::IntervalMap<addr_t, SectionSP, 4,
1780
llvm::IntervalMapHalfOpenInfo<addr_t>>;
1781
1782
ObjectFile::Type ObjectType;
1783
addr_t NextVMAddress = 0;
1784
VMMap::Allocator Alloc;
1785
VMMap Segments{Alloc};
1786
VMMap Sections{Alloc};
1787
lldb_private::Log *Log = GetLog(LLDBLog::Modules);
1788
size_t SegmentCount = 0;
1789
std::string SegmentName;
1790
1791
VMRange GetVMRange(const ELFSectionHeader &H) {
1792
addr_t Address = H.sh_addr;
1793
addr_t Size = H.sh_flags & SHF_ALLOC ? H.sh_size : 0;
1794
1795
// When this is a debug file for relocatable file, the address is all zero
1796
// and thus needs to use accumulate method
1797
if ((ObjectType == ObjectFile::Type::eTypeObjectFile ||
1798
(ObjectType == ObjectFile::Type::eTypeDebugInfo && H.sh_addr == 0)) &&
1799
Segments.empty() && (H.sh_flags & SHF_ALLOC)) {
1800
NextVMAddress =
1801
llvm::alignTo(NextVMAddress, std::max<addr_t>(H.sh_addralign, 1));
1802
Address = NextVMAddress;
1803
NextVMAddress += Size;
1804
}
1805
return VMRange(Address, Size);
1806
}
1807
1808
public:
1809
VMAddressProvider(ObjectFile::Type Type, llvm::StringRef SegmentName)
1810
: ObjectType(Type), SegmentName(std::string(SegmentName)) {}
1811
1812
std::string GetNextSegmentName() const {
1813
return llvm::formatv("{0}[{1}]", SegmentName, SegmentCount).str();
1814
}
1815
1816
std::optional<VMRange> GetAddressInfo(const ELFProgramHeader &H) {
1817
if (H.p_memsz == 0) {
1818
LLDB_LOG(Log, "Ignoring zero-sized {0} segment. Corrupt object file?",
1819
SegmentName);
1820
return std::nullopt;
1821
}
1822
1823
if (Segments.overlaps(H.p_vaddr, H.p_vaddr + H.p_memsz)) {
1824
LLDB_LOG(Log, "Ignoring overlapping {0} segment. Corrupt object file?",
1825
SegmentName);
1826
return std::nullopt;
1827
}
1828
return VMRange(H.p_vaddr, H.p_memsz);
1829
}
1830
1831
std::optional<SectionAddressInfo> GetAddressInfo(const ELFSectionHeader &H) {
1832
VMRange Range = GetVMRange(H);
1833
SectionSP Segment;
1834
auto It = Segments.find(Range.GetRangeBase());
1835
if ((H.sh_flags & SHF_ALLOC) && It.valid()) {
1836
addr_t MaxSize;
1837
if (It.start() <= Range.GetRangeBase()) {
1838
MaxSize = It.stop() - Range.GetRangeBase();
1839
Segment = *It;
1840
} else
1841
MaxSize = It.start() - Range.GetRangeBase();
1842
if (Range.GetByteSize() > MaxSize) {
1843
LLDB_LOG(Log, "Shortening section crossing segment boundaries. "
1844
"Corrupt object file?");
1845
Range.SetByteSize(MaxSize);
1846
}
1847
}
1848
if (Range.GetByteSize() > 0 &&
1849
Sections.overlaps(Range.GetRangeBase(), Range.GetRangeEnd())) {
1850
LLDB_LOG(Log, "Ignoring overlapping section. Corrupt object file?");
1851
return std::nullopt;
1852
}
1853
if (Segment)
1854
Range.Slide(-Segment->GetFileAddress());
1855
return SectionAddressInfo{Segment, Range};
1856
}
1857
1858
void AddSegment(const VMRange &Range, SectionSP Seg) {
1859
Segments.insert(Range.GetRangeBase(), Range.GetRangeEnd(), std::move(Seg));
1860
++SegmentCount;
1861
}
1862
1863
void AddSection(SectionAddressInfo Info, SectionSP Sect) {
1864
if (Info.Range.GetByteSize() == 0)
1865
return;
1866
if (Info.Segment)
1867
Info.Range.Slide(Info.Segment->GetFileAddress());
1868
Sections.insert(Info.Range.GetRangeBase(), Info.Range.GetRangeEnd(),
1869
std::move(Sect));
1870
}
1871
};
1872
}
1873
1874
// We have to do this because ELF doesn't have section IDs, and also
1875
// doesn't require section names to be unique. (We use the section index
1876
// for section IDs, but that isn't guaranteed to be the same in separate
1877
// debug images.)
1878
static SectionSP FindMatchingSection(const SectionList &section_list,
1879
SectionSP section) {
1880
SectionSP sect_sp;
1881
1882
addr_t vm_addr = section->GetFileAddress();
1883
ConstString name = section->GetName();
1884
offset_t byte_size = section->GetByteSize();
1885
bool thread_specific = section->IsThreadSpecific();
1886
uint32_t permissions = section->GetPermissions();
1887
uint32_t alignment = section->GetLog2Align();
1888
1889
for (auto sect : section_list) {
1890
if (sect->GetName() == name &&
1891
sect->IsThreadSpecific() == thread_specific &&
1892
sect->GetPermissions() == permissions &&
1893
sect->GetByteSize() == byte_size && sect->GetFileAddress() == vm_addr &&
1894
sect->GetLog2Align() == alignment) {
1895
sect_sp = sect;
1896
break;
1897
} else {
1898
sect_sp = FindMatchingSection(sect->GetChildren(), section);
1899
if (sect_sp)
1900
break;
1901
}
1902
}
1903
1904
return sect_sp;
1905
}
1906
1907
void ObjectFileELF::CreateSections(SectionList &unified_section_list) {
1908
if (m_sections_up)
1909
return;
1910
1911
m_sections_up = std::make_unique<SectionList>();
1912
VMAddressProvider regular_provider(GetType(), "PT_LOAD");
1913
VMAddressProvider tls_provider(GetType(), "PT_TLS");
1914
1915
for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {
1916
const ELFProgramHeader &PHdr = EnumPHdr.value();
1917
if (PHdr.p_type != PT_LOAD && PHdr.p_type != PT_TLS)
1918
continue;
1919
1920
VMAddressProvider &provider =
1921
PHdr.p_type == PT_TLS ? tls_provider : regular_provider;
1922
auto InfoOr = provider.GetAddressInfo(PHdr);
1923
if (!InfoOr)
1924
continue;
1925
1926
uint32_t Log2Align = llvm::Log2_64(std::max<elf_xword>(PHdr.p_align, 1));
1927
SectionSP Segment = std::make_shared<Section>(
1928
GetModule(), this, SegmentID(EnumPHdr.index()),
1929
ConstString(provider.GetNextSegmentName()), eSectionTypeContainer,
1930
InfoOr->GetRangeBase(), InfoOr->GetByteSize(), PHdr.p_offset,
1931
PHdr.p_filesz, Log2Align, /*flags*/ 0);
1932
Segment->SetPermissions(GetPermissions(PHdr));
1933
Segment->SetIsThreadSpecific(PHdr.p_type == PT_TLS);
1934
m_sections_up->AddSection(Segment);
1935
1936
provider.AddSegment(*InfoOr, std::move(Segment));
1937
}
1938
1939
ParseSectionHeaders();
1940
if (m_section_headers.empty())
1941
return;
1942
1943
for (SectionHeaderCollIter I = std::next(m_section_headers.begin());
1944
I != m_section_headers.end(); ++I) {
1945
const ELFSectionHeaderInfo &header = *I;
1946
1947
ConstString &name = I->section_name;
1948
const uint64_t file_size =
1949
header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
1950
1951
VMAddressProvider &provider =
1952
header.sh_flags & SHF_TLS ? tls_provider : regular_provider;
1953
auto InfoOr = provider.GetAddressInfo(header);
1954
if (!InfoOr)
1955
continue;
1956
1957
SectionType sect_type = GetSectionType(header);
1958
1959
const uint32_t target_bytes_size =
1960
GetTargetByteSize(sect_type, m_arch_spec);
1961
1962
elf::elf_xword log2align =
1963
(header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign);
1964
1965
SectionSP section_sp(new Section(
1966
InfoOr->Segment, GetModule(), // Module to which this section belongs.
1967
this, // ObjectFile to which this section belongs and should
1968
// read section data from.
1969
SectionIndex(I), // Section ID.
1970
name, // Section name.
1971
sect_type, // Section type.
1972
InfoOr->Range.GetRangeBase(), // VM address.
1973
InfoOr->Range.GetByteSize(), // VM size in bytes of this section.
1974
header.sh_offset, // Offset of this section in the file.
1975
file_size, // Size of the section as found in the file.
1976
log2align, // Alignment of the section
1977
header.sh_flags, // Flags for this section.
1978
target_bytes_size)); // Number of host bytes per target byte
1979
1980
section_sp->SetPermissions(GetPermissions(header));
1981
section_sp->SetIsThreadSpecific(header.sh_flags & SHF_TLS);
1982
(InfoOr->Segment ? InfoOr->Segment->GetChildren() : *m_sections_up)
1983
.AddSection(section_sp);
1984
provider.AddSection(std::move(*InfoOr), std::move(section_sp));
1985
}
1986
1987
// For eTypeDebugInfo files, the Symbol Vendor will take care of updating the
1988
// unified section list.
1989
if (GetType() != eTypeDebugInfo)
1990
unified_section_list = *m_sections_up;
1991
1992
// If there's a .gnu_debugdata section, we'll try to read the .symtab that's
1993
// embedded in there and replace the one in the original object file (if any).
1994
// If there's none in the orignal object file, we add it to it.
1995
if (auto gdd_obj_file = GetGnuDebugDataObjectFile()) {
1996
if (auto gdd_objfile_section_list = gdd_obj_file->GetSectionList()) {
1997
if (SectionSP symtab_section_sp =
1998
gdd_objfile_section_list->FindSectionByType(
1999
eSectionTypeELFSymbolTable, true)) {
2000
SectionSP module_section_sp = unified_section_list.FindSectionByType(
2001
eSectionTypeELFSymbolTable, true);
2002
if (module_section_sp)
2003
unified_section_list.ReplaceSection(module_section_sp->GetID(),
2004
symtab_section_sp);
2005
else
2006
unified_section_list.AddSection(symtab_section_sp);
2007
}
2008
}
2009
}
2010
}
2011
2012
std::shared_ptr<ObjectFileELF> ObjectFileELF::GetGnuDebugDataObjectFile() {
2013
if (m_gnu_debug_data_object_file != nullptr)
2014
return m_gnu_debug_data_object_file;
2015
2016
SectionSP section =
2017
GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"));
2018
if (!section)
2019
return nullptr;
2020
2021
if (!lldb_private::lzma::isAvailable()) {
2022
GetModule()->ReportWarning(
2023
"No LZMA support found for reading .gnu_debugdata section");
2024
return nullptr;
2025
}
2026
2027
// Uncompress the data
2028
DataExtractor data;
2029
section->GetSectionData(data);
2030
llvm::SmallVector<uint8_t, 0> uncompressedData;
2031
auto err = lldb_private::lzma::uncompress(data.GetData(), uncompressedData);
2032
if (err) {
2033
GetModule()->ReportWarning(
2034
"An error occurred while decompression the section {0}: {1}",
2035
section->GetName().AsCString(), llvm::toString(std::move(err)).c_str());
2036
return nullptr;
2037
}
2038
2039
// Construct ObjectFileELF object from decompressed buffer
2040
DataBufferSP gdd_data_buf(
2041
new DataBufferHeap(uncompressedData.data(), uncompressedData.size()));
2042
auto fspec = GetFileSpec().CopyByAppendingPathComponent(
2043
llvm::StringRef("gnu_debugdata"));
2044
m_gnu_debug_data_object_file.reset(new ObjectFileELF(
2045
GetModule(), gdd_data_buf, 0, &fspec, 0, gdd_data_buf->GetByteSize()));
2046
2047
// This line is essential; otherwise a breakpoint can be set but not hit.
2048
m_gnu_debug_data_object_file->SetType(ObjectFile::eTypeDebugInfo);
2049
2050
ArchSpec spec = m_gnu_debug_data_object_file->GetArchitecture();
2051
if (spec && m_gnu_debug_data_object_file->SetModulesArchitecture(spec))
2052
return m_gnu_debug_data_object_file;
2053
2054
return nullptr;
2055
}
2056
2057
// Find the arm/aarch64 mapping symbol character in the given symbol name.
2058
// Mapping symbols have the form of "$<char>[.<any>]*". Additionally we
2059
// recognize cases when the mapping symbol prefixed by an arbitrary string
2060
// because if a symbol prefix added to each symbol in the object file with
2061
// objcopy then the mapping symbols are also prefixed.
2062
static char FindArmAarch64MappingSymbol(const char *symbol_name) {
2063
if (!symbol_name)
2064
return '\0';
2065
2066
const char *dollar_pos = ::strchr(symbol_name, '$');
2067
if (!dollar_pos || dollar_pos[1] == '\0')
2068
return '\0';
2069
2070
if (dollar_pos[2] == '\0' || dollar_pos[2] == '.')
2071
return dollar_pos[1];
2072
return '\0';
2073
}
2074
2075
#define STO_MIPS_ISA (3 << 6)
2076
#define STO_MICROMIPS (2 << 6)
2077
#define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS)
2078
2079
// private
2080
std::pair<unsigned, ObjectFileELF::FileAddressToAddressClassMap>
2081
ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
2082
SectionList *section_list, const size_t num_symbols,
2083
const DataExtractor &symtab_data,
2084
const DataExtractor &strtab_data) {
2085
ELFSymbol symbol;
2086
lldb::offset_t offset = 0;
2087
// The changes these symbols would make to the class map. We will also update
2088
// m_address_class_map but need to tell the caller what changed because the
2089
// caller may be another object file.
2090
FileAddressToAddressClassMap address_class_map;
2091
2092
static ConstString text_section_name(".text");
2093
static ConstString init_section_name(".init");
2094
static ConstString fini_section_name(".fini");
2095
static ConstString ctors_section_name(".ctors");
2096
static ConstString dtors_section_name(".dtors");
2097
2098
static ConstString data_section_name(".data");
2099
static ConstString rodata_section_name(".rodata");
2100
static ConstString rodata1_section_name(".rodata1");
2101
static ConstString data2_section_name(".data1");
2102
static ConstString bss_section_name(".bss");
2103
static ConstString opd_section_name(".opd"); // For ppc64
2104
2105
// On Android the oatdata and the oatexec symbols in the oat and odex files
2106
// covers the full .text section what causes issues with displaying unusable
2107
// symbol name to the user and very slow unwinding speed because the
2108
// instruction emulation based unwind plans try to emulate all instructions
2109
// in these symbols. Don't add these symbols to the symbol list as they have
2110
// no use for the debugger and they are causing a lot of trouble. Filtering
2111
// can't be restricted to Android because this special object file don't
2112
// contain the note section specifying the environment to Android but the
2113
// custom extension and file name makes it highly unlikely that this will
2114
// collide with anything else.
2115
llvm::StringRef file_extension = m_file.GetFileNameExtension();
2116
bool skip_oatdata_oatexec =
2117
file_extension == ".oat" || file_extension == ".odex";
2118
2119
ArchSpec arch = GetArchitecture();
2120
ModuleSP module_sp(GetModule());
2121
SectionList *module_section_list =
2122
module_sp ? module_sp->GetSectionList() : nullptr;
2123
2124
// We might have debug information in a separate object, in which case
2125
// we need to map the sections from that object to the sections in the
2126
// main object during symbol lookup. If we had to compare the sections
2127
// for every single symbol, that would be expensive, so this map is
2128
// used to accelerate the process.
2129
std::unordered_map<lldb::SectionSP, lldb::SectionSP> section_map;
2130
2131
unsigned i;
2132
for (i = 0; i < num_symbols; ++i) {
2133
if (!symbol.Parse(symtab_data, &offset))
2134
break;
2135
2136
const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
2137
if (!symbol_name)
2138
symbol_name = "";
2139
2140
// No need to add non-section symbols that have no names
2141
if (symbol.getType() != STT_SECTION &&
2142
(symbol_name == nullptr || symbol_name[0] == '\0'))
2143
continue;
2144
2145
// Skipping oatdata and oatexec sections if it is requested. See details
2146
// above the definition of skip_oatdata_oatexec for the reasons.
2147
if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 ||
2148
::strcmp(symbol_name, "oatexec") == 0))
2149
continue;
2150
2151
SectionSP symbol_section_sp;
2152
SymbolType symbol_type = eSymbolTypeInvalid;
2153
Elf64_Half shndx = symbol.st_shndx;
2154
2155
switch (shndx) {
2156
case SHN_ABS:
2157
symbol_type = eSymbolTypeAbsolute;
2158
break;
2159
case SHN_UNDEF:
2160
symbol_type = eSymbolTypeUndefined;
2161
break;
2162
default:
2163
symbol_section_sp = section_list->FindSectionByID(shndx);
2164
break;
2165
}
2166
2167
// If a symbol is undefined do not process it further even if it has a STT
2168
// type
2169
if (symbol_type != eSymbolTypeUndefined) {
2170
switch (symbol.getType()) {
2171
default:
2172
case STT_NOTYPE:
2173
// The symbol's type is not specified.
2174
break;
2175
2176
case STT_OBJECT:
2177
// The symbol is associated with a data object, such as a variable, an
2178
// array, etc.
2179
symbol_type = eSymbolTypeData;
2180
break;
2181
2182
case STT_FUNC:
2183
// The symbol is associated with a function or other executable code.
2184
symbol_type = eSymbolTypeCode;
2185
break;
2186
2187
case STT_SECTION:
2188
// The symbol is associated with a section. Symbol table entries of
2189
// this type exist primarily for relocation and normally have STB_LOCAL
2190
// binding.
2191
break;
2192
2193
case STT_FILE:
2194
// Conventionally, the symbol's name gives the name of the source file
2195
// associated with the object file. A file symbol has STB_LOCAL
2196
// binding, its section index is SHN_ABS, and it precedes the other
2197
// STB_LOCAL symbols for the file, if it is present.
2198
symbol_type = eSymbolTypeSourceFile;
2199
break;
2200
2201
case STT_GNU_IFUNC:
2202
// The symbol is associated with an indirect function. The actual
2203
// function will be resolved if it is referenced.
2204
symbol_type = eSymbolTypeResolver;
2205
break;
2206
}
2207
}
2208
2209
if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) {
2210
if (symbol_section_sp) {
2211
ConstString sect_name = symbol_section_sp->GetName();
2212
if (sect_name == text_section_name || sect_name == init_section_name ||
2213
sect_name == fini_section_name || sect_name == ctors_section_name ||
2214
sect_name == dtors_section_name) {
2215
symbol_type = eSymbolTypeCode;
2216
} else if (sect_name == data_section_name ||
2217
sect_name == data2_section_name ||
2218
sect_name == rodata_section_name ||
2219
sect_name == rodata1_section_name ||
2220
sect_name == bss_section_name) {
2221
symbol_type = eSymbolTypeData;
2222
}
2223
}
2224
}
2225
2226
int64_t symbol_value_offset = 0;
2227
uint32_t additional_flags = 0;
2228
2229
if (arch.IsValid()) {
2230
if (arch.GetMachine() == llvm::Triple::arm) {
2231
if (symbol.getBinding() == STB_LOCAL) {
2232
char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
2233
if (symbol_type == eSymbolTypeCode) {
2234
switch (mapping_symbol) {
2235
case 'a':
2236
// $a[.<any>]* - marks an ARM instruction sequence
2237
address_class_map[symbol.st_value] = AddressClass::eCode;
2238
break;
2239
case 'b':
2240
case 't':
2241
// $b[.<any>]* - marks a THUMB BL instruction sequence
2242
// $t[.<any>]* - marks a THUMB instruction sequence
2243
address_class_map[symbol.st_value] =
2244
AddressClass::eCodeAlternateISA;
2245
break;
2246
case 'd':
2247
// $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2248
address_class_map[symbol.st_value] = AddressClass::eData;
2249
break;
2250
}
2251
}
2252
if (mapping_symbol)
2253
continue;
2254
}
2255
} else if (arch.GetMachine() == llvm::Triple::aarch64) {
2256
if (symbol.getBinding() == STB_LOCAL) {
2257
char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
2258
if (symbol_type == eSymbolTypeCode) {
2259
switch (mapping_symbol) {
2260
case 'x':
2261
// $x[.<any>]* - marks an A64 instruction sequence
2262
address_class_map[symbol.st_value] = AddressClass::eCode;
2263
break;
2264
case 'd':
2265
// $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2266
address_class_map[symbol.st_value] = AddressClass::eData;
2267
break;
2268
}
2269
}
2270
if (mapping_symbol)
2271
continue;
2272
}
2273
}
2274
2275
if (arch.GetMachine() == llvm::Triple::arm) {
2276
if (symbol_type == eSymbolTypeCode) {
2277
if (symbol.st_value & 1) {
2278
// Subtracting 1 from the address effectively unsets the low order
2279
// bit, which results in the address actually pointing to the
2280
// beginning of the symbol. This delta will be used below in
2281
// conjunction with symbol.st_value to produce the final
2282
// symbol_value that we store in the symtab.
2283
symbol_value_offset = -1;
2284
address_class_map[symbol.st_value ^ 1] =
2285
AddressClass::eCodeAlternateISA;
2286
} else {
2287
// This address is ARM
2288
address_class_map[symbol.st_value] = AddressClass::eCode;
2289
}
2290
}
2291
}
2292
2293
/*
2294
* MIPS:
2295
* The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for
2296
* MIPS).
2297
* This allows processor to switch between microMIPS and MIPS without any
2298
* need
2299
* for special mode-control register. However, apart from .debug_line,
2300
* none of
2301
* the ELF/DWARF sections set the ISA bit (for symbol or section). Use
2302
* st_other
2303
* flag to check whether the symbol is microMIPS and then set the address
2304
* class
2305
* accordingly.
2306
*/
2307
if (arch.IsMIPS()) {
2308
if (IS_MICROMIPS(symbol.st_other))
2309
address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
2310
else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) {
2311
symbol.st_value = symbol.st_value & (~1ull);
2312
address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
2313
} else {
2314
if (symbol_type == eSymbolTypeCode)
2315
address_class_map[symbol.st_value] = AddressClass::eCode;
2316
else if (symbol_type == eSymbolTypeData)
2317
address_class_map[symbol.st_value] = AddressClass::eData;
2318
else
2319
address_class_map[symbol.st_value] = AddressClass::eUnknown;
2320
}
2321
}
2322
}
2323
2324
// symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB
2325
// symbols. See above for more details.
2326
uint64_t symbol_value = symbol.st_value + symbol_value_offset;
2327
2328
if (symbol_section_sp &&
2329
CalculateType() != ObjectFile::Type::eTypeObjectFile)
2330
symbol_value -= symbol_section_sp->GetFileAddress();
2331
2332
if (symbol_section_sp && module_section_list &&
2333
module_section_list != section_list) {
2334
auto section_it = section_map.find(symbol_section_sp);
2335
if (section_it == section_map.end()) {
2336
section_it = section_map
2337
.emplace(symbol_section_sp,
2338
FindMatchingSection(*module_section_list,
2339
symbol_section_sp))
2340
.first;
2341
}
2342
if (section_it->second)
2343
symbol_section_sp = section_it->second;
2344
}
2345
2346
bool is_global = symbol.getBinding() == STB_GLOBAL;
2347
uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags;
2348
llvm::StringRef symbol_ref(symbol_name);
2349
2350
// Symbol names may contain @VERSION suffixes. Find those and strip them
2351
// temporarily.
2352
size_t version_pos = symbol_ref.find('@');
2353
bool has_suffix = version_pos != llvm::StringRef::npos;
2354
llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos);
2355
Mangled mangled(symbol_bare);
2356
2357
// Now append the suffix back to mangled and unmangled names. Only do it if
2358
// the demangling was successful (string is not empty).
2359
if (has_suffix) {
2360
llvm::StringRef suffix = symbol_ref.substr(version_pos);
2361
2362
llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef();
2363
if (!mangled_name.empty())
2364
mangled.SetMangledName(ConstString((mangled_name + suffix).str()));
2365
2366
ConstString demangled = mangled.GetDemangledName();
2367
llvm::StringRef demangled_name = demangled.GetStringRef();
2368
if (!demangled_name.empty())
2369
mangled.SetDemangledName(ConstString((demangled_name + suffix).str()));
2370
}
2371
2372
// In ELF all symbol should have a valid size but it is not true for some
2373
// function symbols coming from hand written assembly. As none of the
2374
// function symbol should have 0 size we try to calculate the size for
2375
// these symbols in the symtab with saying that their original size is not
2376
// valid.
2377
bool symbol_size_valid =
2378
symbol.st_size != 0 || symbol.getType() != STT_FUNC;
2379
2380
bool is_trampoline = false;
2381
if (arch.IsValid() && (arch.GetMachine() == llvm::Triple::aarch64)) {
2382
// On AArch64, trampolines are registered as code.
2383
// If we detect a trampoline (which starts with __AArch64ADRPThunk_ or
2384
// __AArch64AbsLongThunk_) we register the symbol as a trampoline. This
2385
// way we will be able to detect the trampoline when we step in a function
2386
// and step through the trampoline.
2387
if (symbol_type == eSymbolTypeCode) {
2388
llvm::StringRef trampoline_name = mangled.GetName().GetStringRef();
2389
if (trampoline_name.starts_with("__AArch64ADRPThunk_") ||
2390
trampoline_name.starts_with("__AArch64AbsLongThunk_")) {
2391
symbol_type = eSymbolTypeTrampoline;
2392
is_trampoline = true;
2393
}
2394
}
2395
}
2396
2397
Symbol dc_symbol(
2398
i + start_id, // ID is the original symbol table index.
2399
mangled,
2400
symbol_type, // Type of this symbol
2401
is_global, // Is this globally visible?
2402
false, // Is this symbol debug info?
2403
is_trampoline, // Is this symbol a trampoline?
2404
false, // Is this symbol artificial?
2405
AddressRange(symbol_section_sp, // Section in which this symbol is
2406
// defined or null.
2407
symbol_value, // Offset in section or symbol value.
2408
symbol.st_size), // Size in bytes of this symbol.
2409
symbol_size_valid, // Symbol size is valid
2410
has_suffix, // Contains linker annotations?
2411
flags); // Symbol flags.
2412
if (symbol.getBinding() == STB_WEAK)
2413
dc_symbol.SetIsWeak(true);
2414
symtab->AddSymbol(dc_symbol);
2415
}
2416
2417
m_address_class_map.merge(address_class_map);
2418
return {i, address_class_map};
2419
}
2420
2421
std::pair<unsigned, ObjectFileELF::FileAddressToAddressClassMap>
2422
ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id,
2423
lldb_private::Section *symtab) {
2424
if (symtab->GetObjectFile() != this) {
2425
// If the symbol table section is owned by a different object file, have it
2426
// do the parsing.
2427
ObjectFileELF *obj_file_elf =
2428
static_cast<ObjectFileELF *>(symtab->GetObjectFile());
2429
auto [num_symbols, address_class_map] =
2430
obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab);
2431
2432
// The other object file returned the changes it made to its address
2433
// class map, make the same changes to ours.
2434
m_address_class_map.merge(address_class_map);
2435
2436
return {num_symbols, address_class_map};
2437
}
2438
2439
// Get section list for this object file.
2440
SectionList *section_list = m_sections_up.get();
2441
if (!section_list)
2442
return {};
2443
2444
user_id_t symtab_id = symtab->GetID();
2445
const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
2446
assert(symtab_hdr->sh_type == SHT_SYMTAB ||
2447
symtab_hdr->sh_type == SHT_DYNSYM);
2448
2449
// sh_link: section header index of associated string table.
2450
user_id_t strtab_id = symtab_hdr->sh_link;
2451
Section *strtab = section_list->FindSectionByID(strtab_id).get();
2452
2453
if (symtab && strtab) {
2454
assert(symtab->GetObjectFile() == this);
2455
assert(strtab->GetObjectFile() == this);
2456
2457
DataExtractor symtab_data;
2458
DataExtractor strtab_data;
2459
if (ReadSectionData(symtab, symtab_data) &&
2460
ReadSectionData(strtab, strtab_data)) {
2461
size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
2462
2463
return ParseSymbols(symbol_table, start_id, section_list, num_symbols,
2464
symtab_data, strtab_data);
2465
}
2466
}
2467
2468
return {0, {}};
2469
}
2470
2471
size_t ObjectFileELF::ParseDynamicSymbols() {
2472
if (m_dynamic_symbols.size())
2473
return m_dynamic_symbols.size();
2474
2475
SectionList *section_list = GetSectionList();
2476
if (!section_list)
2477
return 0;
2478
2479
// Find the SHT_DYNAMIC section.
2480
Section *dynsym =
2481
section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)
2482
.get();
2483
if (!dynsym)
2484
return 0;
2485
assert(dynsym->GetObjectFile() == this);
2486
2487
ELFDynamic symbol;
2488
DataExtractor dynsym_data;
2489
if (ReadSectionData(dynsym, dynsym_data)) {
2490
const lldb::offset_t section_size = dynsym_data.GetByteSize();
2491
lldb::offset_t cursor = 0;
2492
2493
while (cursor < section_size) {
2494
if (!symbol.Parse(dynsym_data, &cursor))
2495
break;
2496
2497
m_dynamic_symbols.push_back(symbol);
2498
}
2499
}
2500
2501
return m_dynamic_symbols.size();
2502
}
2503
2504
const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) {
2505
if (!ParseDynamicSymbols())
2506
return nullptr;
2507
2508
DynamicSymbolCollIter I = m_dynamic_symbols.begin();
2509
DynamicSymbolCollIter E = m_dynamic_symbols.end();
2510
for (; I != E; ++I) {
2511
ELFDynamic *symbol = &*I;
2512
2513
if (symbol->d_tag == tag)
2514
return symbol;
2515
}
2516
2517
return nullptr;
2518
}
2519
2520
unsigned ObjectFileELF::PLTRelocationType() {
2521
// DT_PLTREL
2522
// This member specifies the type of relocation entry to which the
2523
// procedure linkage table refers. The d_val member holds DT_REL or
2524
// DT_RELA, as appropriate. All relocations in a procedure linkage table
2525
// must use the same relocation.
2526
const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
2527
2528
if (symbol)
2529
return symbol->d_val;
2530
2531
return 0;
2532
}
2533
2534
// Returns the size of the normal plt entries and the offset of the first
2535
// normal plt entry. The 0th entry in the plt table is usually a resolution
2536
// entry which have different size in some architectures then the rest of the
2537
// plt entries.
2538
static std::pair<uint64_t, uint64_t>
2539
GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr,
2540
const ELFSectionHeader *plt_hdr) {
2541
const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2542
2543
// Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are
2544
// 16 bytes. So round the entsize up by the alignment if addralign is set.
2545
elf_xword plt_entsize =
2546
plt_hdr->sh_addralign
2547
? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign)
2548
: plt_hdr->sh_entsize;
2549
2550
// Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly.
2551
// PLT entries relocation code in general requires multiple instruction and
2552
// should be greater than 4 bytes in most cases. Try to guess correct size
2553
// just in case.
2554
if (plt_entsize <= 4) {
2555
// The linker haven't set the plt_hdr->sh_entsize field. Try to guess the
2556
// size of the plt entries based on the number of entries and the size of
2557
// the plt section with the assumption that the size of the 0th entry is at
2558
// least as big as the size of the normal entries and it isn't much bigger
2559
// then that.
2560
if (plt_hdr->sh_addralign)
2561
plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign /
2562
(num_relocations + 1) * plt_hdr->sh_addralign;
2563
else
2564
plt_entsize = plt_hdr->sh_size / (num_relocations + 1);
2565
}
2566
2567
elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize;
2568
2569
return std::make_pair(plt_entsize, plt_offset);
2570
}
2571
2572
static unsigned ParsePLTRelocations(
2573
Symtab *symbol_table, user_id_t start_id, unsigned rel_type,
2574
const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
2575
const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr,
2576
const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data,
2577
DataExtractor &symtab_data, DataExtractor &strtab_data) {
2578
ELFRelocation rel(rel_type);
2579
ELFSymbol symbol;
2580
lldb::offset_t offset = 0;
2581
2582
uint64_t plt_offset, plt_entsize;
2583
std::tie(plt_entsize, plt_offset) =
2584
GetPltEntrySizeAndOffset(rel_hdr, plt_hdr);
2585
const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2586
2587
typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2588
reloc_info_fn reloc_type;
2589
reloc_info_fn reloc_symbol;
2590
2591
if (hdr->Is32Bit()) {
2592
reloc_type = ELFRelocation::RelocType32;
2593
reloc_symbol = ELFRelocation::RelocSymbol32;
2594
} else {
2595
reloc_type = ELFRelocation::RelocType64;
2596
reloc_symbol = ELFRelocation::RelocSymbol64;
2597
}
2598
2599
unsigned slot_type = hdr->GetRelocationJumpSlotType();
2600
unsigned i;
2601
for (i = 0; i < num_relocations; ++i) {
2602
if (!rel.Parse(rel_data, &offset))
2603
break;
2604
2605
if (reloc_type(rel) != slot_type)
2606
continue;
2607
2608
lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
2609
if (!symbol.Parse(symtab_data, &symbol_offset))
2610
break;
2611
2612
const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
2613
uint64_t plt_index = plt_offset + i * plt_entsize;
2614
2615
Symbol jump_symbol(
2616
i + start_id, // Symbol table index
2617
symbol_name, // symbol name.
2618
eSymbolTypeTrampoline, // Type of this symbol
2619
false, // Is this globally visible?
2620
false, // Is this symbol debug info?
2621
true, // Is this symbol a trampoline?
2622
true, // Is this symbol artificial?
2623
plt_section_sp, // Section in which this symbol is defined or null.
2624
plt_index, // Offset in section or symbol value.
2625
plt_entsize, // Size in bytes of this symbol.
2626
true, // Size is valid
2627
false, // Contains linker annotations?
2628
0); // Symbol flags.
2629
2630
symbol_table->AddSymbol(jump_symbol);
2631
}
2632
2633
return i;
2634
}
2635
2636
unsigned
2637
ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id,
2638
const ELFSectionHeaderInfo *rel_hdr,
2639
user_id_t rel_id) {
2640
assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2641
2642
// The link field points to the associated symbol table.
2643
user_id_t symtab_id = rel_hdr->sh_link;
2644
2645
// If the link field doesn't point to the appropriate symbol name table then
2646
// try to find it by name as some compiler don't fill in the link fields.
2647
if (!symtab_id)
2648
symtab_id = GetSectionIndexByName(".dynsym");
2649
2650
// Get PLT section. We cannot use rel_hdr->sh_info, since current linkers
2651
// point that to the .got.plt or .got section instead of .plt.
2652
user_id_t plt_id = GetSectionIndexByName(".plt");
2653
2654
if (!symtab_id || !plt_id)
2655
return 0;
2656
2657
const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
2658
if (!plt_hdr)
2659
return 0;
2660
2661
const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
2662
if (!sym_hdr)
2663
return 0;
2664
2665
SectionList *section_list = m_sections_up.get();
2666
if (!section_list)
2667
return 0;
2668
2669
Section *rel_section = section_list->FindSectionByID(rel_id).get();
2670
if (!rel_section)
2671
return 0;
2672
2673
SectionSP plt_section_sp(section_list->FindSectionByID(plt_id));
2674
if (!plt_section_sp)
2675
return 0;
2676
2677
Section *symtab = section_list->FindSectionByID(symtab_id).get();
2678
if (!symtab)
2679
return 0;
2680
2681
// sh_link points to associated string table.
2682
Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link).get();
2683
if (!strtab)
2684
return 0;
2685
2686
DataExtractor rel_data;
2687
if (!ReadSectionData(rel_section, rel_data))
2688
return 0;
2689
2690
DataExtractor symtab_data;
2691
if (!ReadSectionData(symtab, symtab_data))
2692
return 0;
2693
2694
DataExtractor strtab_data;
2695
if (!ReadSectionData(strtab, strtab_data))
2696
return 0;
2697
2698
unsigned rel_type = PLTRelocationType();
2699
if (!rel_type)
2700
return 0;
2701
2702
return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header,
2703
rel_hdr, plt_hdr, sym_hdr, plt_section_sp,
2704
rel_data, symtab_data, strtab_data);
2705
}
2706
2707
static void ApplyELF64ABS64Relocation(Symtab *symtab, ELFRelocation &rel,
2708
DataExtractor &debug_data,
2709
Section *rel_section) {
2710
Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel));
2711
if (symbol) {
2712
addr_t value = symbol->GetAddressRef().GetFileAddress();
2713
DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2714
// ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2715
WritableDataBuffer *data_buffer =
2716
llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2717
uint64_t *dst = reinterpret_cast<uint64_t *>(
2718
data_buffer->GetBytes() + rel_section->GetFileOffset() +
2719
ELFRelocation::RelocOffset64(rel));
2720
uint64_t val_offset = value + ELFRelocation::RelocAddend64(rel);
2721
memcpy(dst, &val_offset, sizeof(uint64_t));
2722
}
2723
}
2724
2725
static void ApplyELF64ABS32Relocation(Symtab *symtab, ELFRelocation &rel,
2726
DataExtractor &debug_data,
2727
Section *rel_section, bool is_signed) {
2728
Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel));
2729
if (symbol) {
2730
addr_t value = symbol->GetAddressRef().GetFileAddress();
2731
value += ELFRelocation::RelocAddend32(rel);
2732
if ((!is_signed && (value > UINT32_MAX)) ||
2733
(is_signed &&
2734
((int64_t)value > INT32_MAX || (int64_t)value < INT32_MIN))) {
2735
Log *log = GetLog(LLDBLog::Modules);
2736
LLDB_LOGF(log, "Failed to apply debug info relocations");
2737
return;
2738
}
2739
uint32_t truncated_addr = (value & 0xFFFFFFFF);
2740
DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2741
// ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2742
WritableDataBuffer *data_buffer =
2743
llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2744
uint32_t *dst = reinterpret_cast<uint32_t *>(
2745
data_buffer->GetBytes() + rel_section->GetFileOffset() +
2746
ELFRelocation::RelocOffset32(rel));
2747
memcpy(dst, &truncated_addr, sizeof(uint32_t));
2748
}
2749
}
2750
2751
static void ApplyELF32ABS32RelRelocation(Symtab *symtab, ELFRelocation &rel,
2752
DataExtractor &debug_data,
2753
Section *rel_section) {
2754
Log *log = GetLog(LLDBLog::Modules);
2755
Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol32(rel));
2756
if (symbol) {
2757
addr_t value = symbol->GetAddressRef().GetFileAddress();
2758
if (value == LLDB_INVALID_ADDRESS) {
2759
const char *name = symbol->GetName().GetCString();
2760
LLDB_LOGF(log, "Debug info symbol invalid: %s", name);
2761
return;
2762
}
2763
assert(llvm::isUInt<32>(value) && "Valid addresses are 32-bit");
2764
DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2765
// ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2766
WritableDataBuffer *data_buffer =
2767
llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2768
uint8_t *dst = data_buffer->GetBytes() + rel_section->GetFileOffset() +
2769
ELFRelocation::RelocOffset32(rel);
2770
// Implicit addend is stored inline as a signed value.
2771
int32_t addend;
2772
memcpy(&addend, dst, sizeof(int32_t));
2773
// The sum must be positive. This extra check prevents UB from overflow in
2774
// the actual range check below.
2775
if (addend < 0 && static_cast<uint32_t>(-addend) > value) {
2776
LLDB_LOGF(log, "Debug info relocation overflow: 0x%" PRIx64,
2777
static_cast<int64_t>(value) + addend);
2778
return;
2779
}
2780
if (!llvm::isUInt<32>(value + addend)) {
2781
LLDB_LOGF(log, "Debug info relocation out of range: 0x%" PRIx64, value);
2782
return;
2783
}
2784
uint32_t addr = value + addend;
2785
memcpy(dst, &addr, sizeof(uint32_t));
2786
}
2787
}
2788
2789
unsigned ObjectFileELF::ApplyRelocations(
2790
Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
2791
const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,
2792
DataExtractor &rel_data, DataExtractor &symtab_data,
2793
DataExtractor &debug_data, Section *rel_section) {
2794
ELFRelocation rel(rel_hdr->sh_type);
2795
lldb::addr_t offset = 0;
2796
const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2797
typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2798
reloc_info_fn reloc_type;
2799
reloc_info_fn reloc_symbol;
2800
2801
if (hdr->Is32Bit()) {
2802
reloc_type = ELFRelocation::RelocType32;
2803
reloc_symbol = ELFRelocation::RelocSymbol32;
2804
} else {
2805
reloc_type = ELFRelocation::RelocType64;
2806
reloc_symbol = ELFRelocation::RelocSymbol64;
2807
}
2808
2809
for (unsigned i = 0; i < num_relocations; ++i) {
2810
if (!rel.Parse(rel_data, &offset)) {
2811
GetModule()->ReportError(".rel{0}[{1:d}] failed to parse relocation",
2812
rel_section->GetName().AsCString(), i);
2813
break;
2814
}
2815
Symbol *symbol = nullptr;
2816
2817
if (hdr->Is32Bit()) {
2818
switch (hdr->e_machine) {
2819
case llvm::ELF::EM_ARM:
2820
switch (reloc_type(rel)) {
2821
case R_ARM_ABS32:
2822
ApplyELF32ABS32RelRelocation(symtab, rel, debug_data, rel_section);
2823
break;
2824
case R_ARM_REL32:
2825
GetModule()->ReportError("unsupported AArch32 relocation:"
2826
" .rel{0}[{1}], type {2}",
2827
rel_section->GetName().AsCString(), i,
2828
reloc_type(rel));
2829
break;
2830
default:
2831
assert(false && "unexpected relocation type");
2832
}
2833
break;
2834
case llvm::ELF::EM_386:
2835
switch (reloc_type(rel)) {
2836
case R_386_32:
2837
symbol = symtab->FindSymbolByID(reloc_symbol(rel));
2838
if (symbol) {
2839
addr_t f_offset =
2840
rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel);
2841
DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2842
// ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2843
WritableDataBuffer *data_buffer =
2844
llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2845
uint32_t *dst = reinterpret_cast<uint32_t *>(
2846
data_buffer->GetBytes() + f_offset);
2847
2848
addr_t value = symbol->GetAddressRef().GetFileAddress();
2849
if (rel.IsRela()) {
2850
value += ELFRelocation::RelocAddend32(rel);
2851
} else {
2852
value += *dst;
2853
}
2854
*dst = value;
2855
} else {
2856
GetModule()->ReportError(".rel{0}[{1}] unknown symbol id: {2:d}",
2857
rel_section->GetName().AsCString(), i,
2858
reloc_symbol(rel));
2859
}
2860
break;
2861
case R_386_NONE:
2862
case R_386_PC32:
2863
GetModule()->ReportError("unsupported i386 relocation:"
2864
" .rel{0}[{1}], type {2}",
2865
rel_section->GetName().AsCString(), i,
2866
reloc_type(rel));
2867
break;
2868
default:
2869
assert(false && "unexpected relocation type");
2870
break;
2871
}
2872
break;
2873
default:
2874
GetModule()->ReportError("unsupported 32-bit ELF machine arch: {0}", hdr->e_machine);
2875
break;
2876
}
2877
} else {
2878
switch (hdr->e_machine) {
2879
case llvm::ELF::EM_AARCH64:
2880
switch (reloc_type(rel)) {
2881
case R_AARCH64_ABS64:
2882
ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);
2883
break;
2884
case R_AARCH64_ABS32:
2885
ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);
2886
break;
2887
default:
2888
assert(false && "unexpected relocation type");
2889
}
2890
break;
2891
case llvm::ELF::EM_LOONGARCH:
2892
switch (reloc_type(rel)) {
2893
case R_LARCH_64:
2894
ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);
2895
break;
2896
case R_LARCH_32:
2897
ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);
2898
break;
2899
default:
2900
assert(false && "unexpected relocation type");
2901
}
2902
break;
2903
case llvm::ELF::EM_X86_64:
2904
switch (reloc_type(rel)) {
2905
case R_X86_64_64:
2906
ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);
2907
break;
2908
case R_X86_64_32:
2909
ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section,
2910
false);
2911
break;
2912
case R_X86_64_32S:
2913
ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);
2914
break;
2915
case R_X86_64_PC32:
2916
default:
2917
assert(false && "unexpected relocation type");
2918
}
2919
break;
2920
default:
2921
GetModule()->ReportError("unsupported 64-bit ELF machine arch: {0}", hdr->e_machine);
2922
break;
2923
}
2924
}
2925
}
2926
2927
return 0;
2928
}
2929
2930
unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr,
2931
user_id_t rel_id,
2932
lldb_private::Symtab *thetab) {
2933
assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2934
2935
// Parse in the section list if needed.
2936
SectionList *section_list = GetSectionList();
2937
if (!section_list)
2938
return 0;
2939
2940
user_id_t symtab_id = rel_hdr->sh_link;
2941
user_id_t debug_id = rel_hdr->sh_info;
2942
2943
const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
2944
if (!symtab_hdr)
2945
return 0;
2946
2947
const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);
2948
if (!debug_hdr)
2949
return 0;
2950
2951
Section *rel = section_list->FindSectionByID(rel_id).get();
2952
if (!rel)
2953
return 0;
2954
2955
Section *symtab = section_list->FindSectionByID(symtab_id).get();
2956
if (!symtab)
2957
return 0;
2958
2959
Section *debug = section_list->FindSectionByID(debug_id).get();
2960
if (!debug)
2961
return 0;
2962
2963
DataExtractor rel_data;
2964
DataExtractor symtab_data;
2965
DataExtractor debug_data;
2966
2967
if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) &&
2968
GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) &&
2969
GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) {
2970
ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr,
2971
rel_data, symtab_data, debug_data, debug);
2972
}
2973
2974
return 0;
2975
}
2976
2977
void ObjectFileELF::ParseSymtab(Symtab &lldb_symtab) {
2978
ModuleSP module_sp(GetModule());
2979
if (!module_sp)
2980
return;
2981
2982
Progress progress("Parsing symbol table",
2983
m_file.GetFilename().AsCString("<Unknown>"));
2984
ElapsedTime elapsed(module_sp->GetSymtabParseTime());
2985
2986
// We always want to use the main object file so we (hopefully) only have one
2987
// cached copy of our symtab, dynamic sections, etc.
2988
ObjectFile *module_obj_file = module_sp->GetObjectFile();
2989
if (module_obj_file && module_obj_file != this)
2990
return module_obj_file->ParseSymtab(lldb_symtab);
2991
2992
SectionList *section_list = module_sp->GetSectionList();
2993
if (!section_list)
2994
return;
2995
2996
uint64_t symbol_id = 0;
2997
2998
// Sharable objects and dynamic executables usually have 2 distinct symbol
2999
// tables, one named ".symtab", and the other ".dynsym". The dynsym is a
3000
// smaller version of the symtab that only contains global symbols. The
3001
// information found in the dynsym is therefore also found in the symtab,
3002
// while the reverse is not necessarily true.
3003
Section *symtab =
3004
section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get();
3005
if (symtab) {
3006
auto [num_symbols, address_class_map] =
3007
ParseSymbolTable(&lldb_symtab, symbol_id, symtab);
3008
m_address_class_map.merge(address_class_map);
3009
symbol_id += num_symbols;
3010
}
3011
3012
// The symtab section is non-allocable and can be stripped, while the
3013
// .dynsym section which should always be always be there. To support the
3014
// minidebuginfo case we parse .dynsym when there's a .gnu_debuginfo
3015
// section, nomatter if .symtab was already parsed or not. This is because
3016
// minidebuginfo normally removes the .symtab symbols which have their
3017
// matching .dynsym counterparts.
3018
if (!symtab ||
3019
GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"))) {
3020
Section *dynsym =
3021
section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true)
3022
.get();
3023
if (dynsym) {
3024
auto [num_symbols, address_class_map] =
3025
ParseSymbolTable(&lldb_symtab, symbol_id, dynsym);
3026
symbol_id += num_symbols;
3027
m_address_class_map.merge(address_class_map);
3028
}
3029
}
3030
3031
// DT_JMPREL
3032
// If present, this entry's d_ptr member holds the address of
3033
// relocation
3034
// entries associated solely with the procedure linkage table.
3035
// Separating
3036
// these relocation entries lets the dynamic linker ignore them during
3037
// process initialization, if lazy binding is enabled. If this entry is
3038
// present, the related entries of types DT_PLTRELSZ and DT_PLTREL must
3039
// also be present.
3040
const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
3041
if (symbol) {
3042
// Synthesize trampoline symbols to help navigate the PLT.
3043
addr_t addr = symbol->d_ptr;
3044
Section *reloc_section =
3045
section_list->FindSectionContainingFileAddress(addr).get();
3046
if (reloc_section) {
3047
user_id_t reloc_id = reloc_section->GetID();
3048
const ELFSectionHeaderInfo *reloc_header =
3049
GetSectionHeaderByIndex(reloc_id);
3050
if (reloc_header)
3051
ParseTrampolineSymbols(&lldb_symtab, symbol_id, reloc_header, reloc_id);
3052
}
3053
}
3054
3055
if (DWARFCallFrameInfo *eh_frame =
3056
GetModule()->GetUnwindTable().GetEHFrameInfo()) {
3057
ParseUnwindSymbols(&lldb_symtab, eh_frame);
3058
}
3059
3060
// In the event that there's no symbol entry for the entry point we'll
3061
// artificially create one. We delegate to the symtab object the figuring
3062
// out of the proper size, this will usually make it span til the next
3063
// symbol it finds in the section. This means that if there are missing
3064
// symbols the entry point might span beyond its function definition.
3065
// We're fine with this as it doesn't make it worse than not having a
3066
// symbol entry at all.
3067
if (CalculateType() == eTypeExecutable) {
3068
ArchSpec arch = GetArchitecture();
3069
auto entry_point_addr = GetEntryPointAddress();
3070
bool is_valid_entry_point =
3071
entry_point_addr.IsValid() && entry_point_addr.IsSectionOffset();
3072
addr_t entry_point_file_addr = entry_point_addr.GetFileAddress();
3073
if (is_valid_entry_point && !lldb_symtab.FindSymbolContainingFileAddress(
3074
entry_point_file_addr)) {
3075
uint64_t symbol_id = lldb_symtab.GetNumSymbols();
3076
// Don't set the name for any synthetic symbols, the Symbol
3077
// object will generate one if needed when the name is accessed
3078
// via accessors.
3079
SectionSP section_sp = entry_point_addr.GetSection();
3080
Symbol symbol(
3081
/*symID=*/symbol_id,
3082
/*name=*/llvm::StringRef(), // Name will be auto generated.
3083
/*type=*/eSymbolTypeCode,
3084
/*external=*/true,
3085
/*is_debug=*/false,
3086
/*is_trampoline=*/false,
3087
/*is_artificial=*/true,
3088
/*section_sp=*/section_sp,
3089
/*offset=*/0,
3090
/*size=*/0, // FDE can span multiple symbols so don't use its size.
3091
/*size_is_valid=*/false,
3092
/*contains_linker_annotations=*/false,
3093
/*flags=*/0);
3094
// When the entry point is arm thumb we need to explicitly set its
3095
// class address to reflect that. This is important because expression
3096
// evaluation relies on correctly setting a breakpoint at this
3097
// address.
3098
if (arch.GetMachine() == llvm::Triple::arm &&
3099
(entry_point_file_addr & 1)) {
3100
symbol.GetAddressRef().SetOffset(entry_point_addr.GetOffset() ^ 1);
3101
m_address_class_map[entry_point_file_addr ^ 1] =
3102
AddressClass::eCodeAlternateISA;
3103
} else {
3104
m_address_class_map[entry_point_file_addr] = AddressClass::eCode;
3105
}
3106
lldb_symtab.AddSymbol(symbol);
3107
}
3108
}
3109
}
3110
3111
void ObjectFileELF::RelocateSection(lldb_private::Section *section)
3112
{
3113
static const char *debug_prefix = ".debug";
3114
3115
// Set relocated bit so we stop getting called, regardless of whether we
3116
// actually relocate.
3117
section->SetIsRelocated(true);
3118
3119
// We only relocate in ELF relocatable files
3120
if (CalculateType() != eTypeObjectFile)
3121
return;
3122
3123
const char *section_name = section->GetName().GetCString();
3124
// Can't relocate that which can't be named
3125
if (section_name == nullptr)
3126
return;
3127
3128
// We don't relocate non-debug sections at the moment
3129
if (strncmp(section_name, debug_prefix, strlen(debug_prefix)))
3130
return;
3131
3132
// Relocation section names to look for
3133
std::string needle = std::string(".rel") + section_name;
3134
std::string needlea = std::string(".rela") + section_name;
3135
3136
for (SectionHeaderCollIter I = m_section_headers.begin();
3137
I != m_section_headers.end(); ++I) {
3138
if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) {
3139
const char *hay_name = I->section_name.GetCString();
3140
if (hay_name == nullptr)
3141
continue;
3142
if (needle == hay_name || needlea == hay_name) {
3143
const ELFSectionHeader &reloc_header = *I;
3144
user_id_t reloc_id = SectionIndex(I);
3145
RelocateDebugSections(&reloc_header, reloc_id, GetSymtab());
3146
break;
3147
}
3148
}
3149
}
3150
}
3151
3152
void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table,
3153
DWARFCallFrameInfo *eh_frame) {
3154
SectionList *section_list = GetSectionList();
3155
if (!section_list)
3156
return;
3157
3158
// First we save the new symbols into a separate list and add them to the
3159
// symbol table after we collected all symbols we want to add. This is
3160
// neccessary because adding a new symbol invalidates the internal index of
3161
// the symtab what causing the next lookup to be slow because it have to
3162
// recalculate the index first.
3163
std::vector<Symbol> new_symbols;
3164
3165
size_t num_symbols = symbol_table->GetNumSymbols();
3166
uint64_t last_symbol_id =
3167
num_symbols ? symbol_table->SymbolAtIndex(num_symbols - 1)->GetID() : 0;
3168
eh_frame->ForEachFDEEntries([&](lldb::addr_t file_addr, uint32_t size,
3169
dw_offset_t) {
3170
Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr);
3171
if (symbol) {
3172
if (!symbol->GetByteSizeIsValid()) {
3173
symbol->SetByteSize(size);
3174
symbol->SetSizeIsSynthesized(true);
3175
}
3176
} else {
3177
SectionSP section_sp =
3178
section_list->FindSectionContainingFileAddress(file_addr);
3179
if (section_sp) {
3180
addr_t offset = file_addr - section_sp->GetFileAddress();
3181
uint64_t symbol_id = ++last_symbol_id;
3182
// Don't set the name for any synthetic symbols, the Symbol
3183
// object will generate one if needed when the name is accessed
3184
// via accessors.
3185
Symbol eh_symbol(
3186
/*symID=*/symbol_id,
3187
/*name=*/llvm::StringRef(), // Name will be auto generated.
3188
/*type=*/eSymbolTypeCode,
3189
/*external=*/true,
3190
/*is_debug=*/false,
3191
/*is_trampoline=*/false,
3192
/*is_artificial=*/true,
3193
/*section_sp=*/section_sp,
3194
/*offset=*/offset,
3195
/*size=*/0, // FDE can span multiple symbols so don't use its size.
3196
/*size_is_valid=*/false,
3197
/*contains_linker_annotations=*/false,
3198
/*flags=*/0);
3199
new_symbols.push_back(eh_symbol);
3200
}
3201
}
3202
return true;
3203
});
3204
3205
for (const Symbol &s : new_symbols)
3206
symbol_table->AddSymbol(s);
3207
}
3208
3209
bool ObjectFileELF::IsStripped() {
3210
// TODO: determine this for ELF
3211
return false;
3212
}
3213
3214
//===----------------------------------------------------------------------===//
3215
// Dump
3216
//
3217
// Dump the specifics of the runtime file container (such as any headers
3218
// segments, sections, etc).
3219
void ObjectFileELF::Dump(Stream *s) {
3220
ModuleSP module_sp(GetModule());
3221
if (!module_sp) {
3222
return;
3223
}
3224
3225
std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
3226
s->Printf("%p: ", static_cast<void *>(this));
3227
s->Indent();
3228
s->PutCString("ObjectFileELF");
3229
3230
ArchSpec header_arch = GetArchitecture();
3231
3232
*s << ", file = '" << m_file
3233
<< "', arch = " << header_arch.GetArchitectureName() << "\n";
3234
3235
DumpELFHeader(s, m_header);
3236
s->EOL();
3237
DumpELFProgramHeaders(s);
3238
s->EOL();
3239
DumpELFSectionHeaders(s);
3240
s->EOL();
3241
SectionList *section_list = GetSectionList();
3242
if (section_list)
3243
section_list->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
3244
UINT32_MAX);
3245
Symtab *symtab = GetSymtab();
3246
if (symtab)
3247
symtab->Dump(s, nullptr, eSortOrderNone);
3248
s->EOL();
3249
DumpDependentModules(s);
3250
s->EOL();
3251
}
3252
3253
// DumpELFHeader
3254
//
3255
// Dump the ELF header to the specified output stream
3256
void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) {
3257
s->PutCString("ELF Header\n");
3258
s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
3259
s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1],
3260
header.e_ident[EI_MAG1]);
3261
s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2],
3262
header.e_ident[EI_MAG2]);
3263
s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3],
3264
header.e_ident[EI_MAG3]);
3265
3266
s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
3267
s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
3268
DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
3269
s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
3270
s->Printf("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
3271
3272
s->Printf("e_type = 0x%4.4x ", header.e_type);
3273
DumpELFHeader_e_type(s, header.e_type);
3274
s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
3275
s->Printf("e_version = 0x%8.8x\n", header.e_version);
3276
s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
3277
s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
3278
s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
3279
s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
3280
s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
3281
s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
3282
s->Printf("e_phnum = 0x%8.8x\n", header.e_phnum);
3283
s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
3284
s->Printf("e_shnum = 0x%8.8x\n", header.e_shnum);
3285
s->Printf("e_shstrndx = 0x%8.8x\n", header.e_shstrndx);
3286
}
3287
3288
// DumpELFHeader_e_type
3289
//
3290
// Dump an token value for the ELF header member e_type
3291
void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) {
3292
switch (e_type) {
3293
case ET_NONE:
3294
*s << "ET_NONE";
3295
break;
3296
case ET_REL:
3297
*s << "ET_REL";
3298
break;
3299
case ET_EXEC:
3300
*s << "ET_EXEC";
3301
break;
3302
case ET_DYN:
3303
*s << "ET_DYN";
3304
break;
3305
case ET_CORE:
3306
*s << "ET_CORE";
3307
break;
3308
default:
3309
break;
3310
}
3311
}
3312
3313
// DumpELFHeader_e_ident_EI_DATA
3314
//
3315
// Dump an token value for the ELF header member e_ident[EI_DATA]
3316
void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s,
3317
unsigned char ei_data) {
3318
switch (ei_data) {
3319
case ELFDATANONE:
3320
*s << "ELFDATANONE";
3321
break;
3322
case ELFDATA2LSB:
3323
*s << "ELFDATA2LSB - Little Endian";
3324
break;
3325
case ELFDATA2MSB:
3326
*s << "ELFDATA2MSB - Big Endian";
3327
break;
3328
default:
3329
break;
3330
}
3331
}
3332
3333
// DumpELFProgramHeader
3334
//
3335
// Dump a single ELF program header to the specified output stream
3336
void ObjectFileELF::DumpELFProgramHeader(Stream *s,
3337
const ELFProgramHeader &ph) {
3338
DumpELFProgramHeader_p_type(s, ph.p_type);
3339
s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset,
3340
ph.p_vaddr, ph.p_paddr);
3341
s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz,
3342
ph.p_flags);
3343
3344
DumpELFProgramHeader_p_flags(s, ph.p_flags);
3345
s->Printf(") %8.8" PRIx64, ph.p_align);
3346
}
3347
3348
// DumpELFProgramHeader_p_type
3349
//
3350
// Dump an token value for the ELF program header member p_type which describes
3351
// the type of the program header
3352
void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) {
3353
const int kStrWidth = 15;
3354
switch (p_type) {
3355
CASE_AND_STREAM(s, PT_NULL, kStrWidth);
3356
CASE_AND_STREAM(s, PT_LOAD, kStrWidth);
3357
CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth);
3358
CASE_AND_STREAM(s, PT_INTERP, kStrWidth);
3359
CASE_AND_STREAM(s, PT_NOTE, kStrWidth);
3360
CASE_AND_STREAM(s, PT_SHLIB, kStrWidth);
3361
CASE_AND_STREAM(s, PT_PHDR, kStrWidth);
3362
CASE_AND_STREAM(s, PT_TLS, kStrWidth);
3363
CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
3364
default:
3365
s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
3366
break;
3367
}
3368
}
3369
3370
// DumpELFProgramHeader_p_flags
3371
//
3372
// Dump an token value for the ELF program header member p_flags
3373
void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) {
3374
*s << ((p_flags & PF_X) ? "PF_X" : " ")
3375
<< (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
3376
<< ((p_flags & PF_W) ? "PF_W" : " ")
3377
<< (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
3378
<< ((p_flags & PF_R) ? "PF_R" : " ");
3379
}
3380
3381
// DumpELFProgramHeaders
3382
//
3383
// Dump all of the ELF program header to the specified output stream
3384
void ObjectFileELF::DumpELFProgramHeaders(Stream *s) {
3385
if (!ParseProgramHeaders())
3386
return;
3387
3388
s->PutCString("Program Headers\n");
3389
s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
3390
"p_filesz p_memsz p_flags p_align\n");
3391
s->PutCString("==== --------------- -------- -------- -------- "
3392
"-------- -------- ------------------------- --------\n");
3393
3394
for (const auto &H : llvm::enumerate(m_program_headers)) {
3395
s->Format("[{0,2}] ", H.index());
3396
ObjectFileELF::DumpELFProgramHeader(s, H.value());
3397
s->EOL();
3398
}
3399
}
3400
3401
// DumpELFSectionHeader
3402
//
3403
// Dump a single ELF section header to the specified output stream
3404
void ObjectFileELF::DumpELFSectionHeader(Stream *s,
3405
const ELFSectionHeaderInfo &sh) {
3406
s->Printf("%8.8x ", sh.sh_name);
3407
DumpELFSectionHeader_sh_type(s, sh.sh_type);
3408
s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
3409
DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
3410
s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr,
3411
sh.sh_offset, sh.sh_size);
3412
s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
3413
s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
3414
}
3415
3416
// DumpELFSectionHeader_sh_type
3417
//
3418
// Dump an token value for the ELF section header member sh_type which
3419
// describes the type of the section
3420
void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) {
3421
const int kStrWidth = 12;
3422
switch (sh_type) {
3423
CASE_AND_STREAM(s, SHT_NULL, kStrWidth);
3424
CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth);
3425
CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth);
3426
CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth);
3427
CASE_AND_STREAM(s, SHT_RELA, kStrWidth);
3428
CASE_AND_STREAM(s, SHT_HASH, kStrWidth);
3429
CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth);
3430
CASE_AND_STREAM(s, SHT_NOTE, kStrWidth);
3431
CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth);
3432
CASE_AND_STREAM(s, SHT_REL, kStrWidth);
3433
CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth);
3434
CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth);
3435
CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth);
3436
CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth);
3437
CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth);
3438
CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth);
3439
default:
3440
s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
3441
break;
3442
}
3443
}
3444
3445
// DumpELFSectionHeader_sh_flags
3446
//
3447
// Dump an token value for the ELF section header member sh_flags
3448
void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s,
3449
elf_xword sh_flags) {
3450
*s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
3451
<< (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
3452
<< ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
3453
<< (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
3454
<< ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
3455
}
3456
3457
// DumpELFSectionHeaders
3458
//
3459
// Dump all of the ELF section header to the specified output stream
3460
void ObjectFileELF::DumpELFSectionHeaders(Stream *s) {
3461
if (!ParseSectionHeaders())
3462
return;
3463
3464
s->PutCString("Section Headers\n");
3465
s->PutCString("IDX name type flags "
3466
"addr offset size link info addralgn "
3467
"entsize Name\n");
3468
s->PutCString("==== -------- ------------ -------------------------------- "
3469
"-------- -------- -------- -------- -------- -------- "
3470
"-------- ====================\n");
3471
3472
uint32_t idx = 0;
3473
for (SectionHeaderCollConstIter I = m_section_headers.begin();
3474
I != m_section_headers.end(); ++I, ++idx) {
3475
s->Printf("[%2u] ", idx);
3476
ObjectFileELF::DumpELFSectionHeader(s, *I);
3477
const char *section_name = I->section_name.AsCString("");
3478
if (section_name)
3479
*s << ' ' << section_name << "\n";
3480
}
3481
}
3482
3483
void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) {
3484
size_t num_modules = ParseDependentModules();
3485
3486
if (num_modules > 0) {
3487
s->PutCString("Dependent Modules:\n");
3488
for (unsigned i = 0; i < num_modules; ++i) {
3489
const FileSpec &spec = m_filespec_up->GetFileSpecAtIndex(i);
3490
s->Printf(" %s\n", spec.GetFilename().GetCString());
3491
}
3492
}
3493
}
3494
3495
ArchSpec ObjectFileELF::GetArchitecture() {
3496
if (!ParseHeader())
3497
return ArchSpec();
3498
3499
if (m_section_headers.empty()) {
3500
// Allow elf notes to be parsed which may affect the detected architecture.
3501
ParseSectionHeaders();
3502
}
3503
3504
if (CalculateType() == eTypeCoreFile &&
3505
!m_arch_spec.TripleOSWasSpecified()) {
3506
// Core files don't have section headers yet they have PT_NOTE program
3507
// headers that might shed more light on the architecture
3508
for (const elf::ELFProgramHeader &H : ProgramHeaders()) {
3509
if (H.p_type != PT_NOTE || H.p_offset == 0 || H.p_filesz == 0)
3510
continue;
3511
DataExtractor data;
3512
if (data.SetData(m_data, H.p_offset, H.p_filesz) == H.p_filesz) {
3513
UUID uuid;
3514
RefineModuleDetailsFromNote(data, m_arch_spec, uuid);
3515
}
3516
}
3517
}
3518
return m_arch_spec;
3519
}
3520
3521
ObjectFile::Type ObjectFileELF::CalculateType() {
3522
switch (m_header.e_type) {
3523
case llvm::ELF::ET_NONE:
3524
// 0 - No file type
3525
return eTypeUnknown;
3526
3527
case llvm::ELF::ET_REL:
3528
// 1 - Relocatable file
3529
return eTypeObjectFile;
3530
3531
case llvm::ELF::ET_EXEC:
3532
// 2 - Executable file
3533
return eTypeExecutable;
3534
3535
case llvm::ELF::ET_DYN:
3536
// 3 - Shared object file
3537
return eTypeSharedLibrary;
3538
3539
case ET_CORE:
3540
// 4 - Core file
3541
return eTypeCoreFile;
3542
3543
default:
3544
break;
3545
}
3546
return eTypeUnknown;
3547
}
3548
3549
ObjectFile::Strata ObjectFileELF::CalculateStrata() {
3550
switch (m_header.e_type) {
3551
case llvm::ELF::ET_NONE:
3552
// 0 - No file type
3553
return eStrataUnknown;
3554
3555
case llvm::ELF::ET_REL:
3556
// 1 - Relocatable file
3557
return eStrataUnknown;
3558
3559
case llvm::ELF::ET_EXEC:
3560
// 2 - Executable file
3561
{
3562
SectionList *section_list = GetSectionList();
3563
if (section_list) {
3564
static ConstString loader_section_name(".interp");
3565
SectionSP loader_section =
3566
section_list->FindSectionByName(loader_section_name);
3567
if (loader_section) {
3568
char buffer[256];
3569
size_t read_size =
3570
ReadSectionData(loader_section.get(), 0, buffer, sizeof(buffer));
3571
3572
// We compare the content of .interp section
3573
// It will contains \0 when counting read_size, so the size needs to
3574
// decrease by one
3575
llvm::StringRef loader_name(buffer, read_size - 1);
3576
llvm::StringRef freebsd_kernel_loader_name("/red/herring");
3577
if (loader_name == freebsd_kernel_loader_name)
3578
return eStrataKernel;
3579
}
3580
}
3581
return eStrataUser;
3582
}
3583
3584
case llvm::ELF::ET_DYN:
3585
// 3 - Shared object file
3586
// TODO: is there any way to detect that an shared library is a kernel
3587
// related executable by inspecting the program headers, section headers,
3588
// symbols, or any other flag bits???
3589
return eStrataUnknown;
3590
3591
case ET_CORE:
3592
// 4 - Core file
3593
// TODO: is there any way to detect that an core file is a kernel
3594
// related executable by inspecting the program headers, section headers,
3595
// symbols, or any other flag bits???
3596
return eStrataUnknown;
3597
3598
default:
3599
break;
3600
}
3601
return eStrataUnknown;
3602
}
3603
3604
size_t ObjectFileELF::ReadSectionData(Section *section,
3605
lldb::offset_t section_offset, void *dst,
3606
size_t dst_len) {
3607
// If some other objectfile owns this data, pass this to them.
3608
if (section->GetObjectFile() != this)
3609
return section->GetObjectFile()->ReadSectionData(section, section_offset,
3610
dst, dst_len);
3611
3612
if (!section->Test(SHF_COMPRESSED))
3613
return ObjectFile::ReadSectionData(section, section_offset, dst, dst_len);
3614
3615
// For compressed sections we need to read to full data to be able to
3616
// decompress.
3617
DataExtractor data;
3618
ReadSectionData(section, data);
3619
return data.CopyData(section_offset, dst_len, dst);
3620
}
3621
3622
size_t ObjectFileELF::ReadSectionData(Section *section,
3623
DataExtractor &section_data) {
3624
// If some other objectfile owns this data, pass this to them.
3625
if (section->GetObjectFile() != this)
3626
return section->GetObjectFile()->ReadSectionData(section, section_data);
3627
3628
size_t result = ObjectFile::ReadSectionData(section, section_data);
3629
if (result == 0 || !(section->Get() & llvm::ELF::SHF_COMPRESSED))
3630
return result;
3631
3632
auto Decompressor = llvm::object::Decompressor::create(
3633
section->GetName().GetStringRef(),
3634
{reinterpret_cast<const char *>(section_data.GetDataStart()),
3635
size_t(section_data.GetByteSize())},
3636
GetByteOrder() == eByteOrderLittle, GetAddressByteSize() == 8);
3637
if (!Decompressor) {
3638
GetModule()->ReportWarning(
3639
"Unable to initialize decompressor for section '{0}': {1}",
3640
section->GetName().GetCString(),
3641
llvm::toString(Decompressor.takeError()).c_str());
3642
section_data.Clear();
3643
return 0;
3644
}
3645
3646
auto buffer_sp =
3647
std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0);
3648
if (auto error = Decompressor->decompress(
3649
{buffer_sp->GetBytes(), size_t(buffer_sp->GetByteSize())})) {
3650
GetModule()->ReportWarning("Decompression of section '{0}' failed: {1}",
3651
section->GetName().GetCString(),
3652
llvm::toString(std::move(error)).c_str());
3653
section_data.Clear();
3654
return 0;
3655
}
3656
3657
section_data.SetData(buffer_sp);
3658
return buffer_sp->GetByteSize();
3659
}
3660
3661
llvm::ArrayRef<ELFProgramHeader> ObjectFileELF::ProgramHeaders() {
3662
ParseProgramHeaders();
3663
return m_program_headers;
3664
}
3665
3666
DataExtractor ObjectFileELF::GetSegmentData(const ELFProgramHeader &H) {
3667
return DataExtractor(m_data, H.p_offset, H.p_filesz);
3668
}
3669
3670
bool ObjectFileELF::AnySegmentHasPhysicalAddress() {
3671
for (const ELFProgramHeader &H : ProgramHeaders()) {
3672
if (H.p_paddr != 0)
3673
return true;
3674
}
3675
return false;
3676
}
3677
3678
std::vector<ObjectFile::LoadableData>
3679
ObjectFileELF::GetLoadableData(Target &target) {
3680
// Create a list of loadable data from loadable segments, using physical
3681
// addresses if they aren't all null
3682
std::vector<LoadableData> loadables;
3683
bool should_use_paddr = AnySegmentHasPhysicalAddress();
3684
for (const ELFProgramHeader &H : ProgramHeaders()) {
3685
LoadableData loadable;
3686
if (H.p_type != llvm::ELF::PT_LOAD)
3687
continue;
3688
loadable.Dest = should_use_paddr ? H.p_paddr : H.p_vaddr;
3689
if (loadable.Dest == LLDB_INVALID_ADDRESS)
3690
continue;
3691
if (H.p_filesz == 0)
3692
continue;
3693
auto segment_data = GetSegmentData(H);
3694
loadable.Contents = llvm::ArrayRef<uint8_t>(segment_data.GetDataStart(),
3695
segment_data.GetByteSize());
3696
loadables.push_back(loadable);
3697
}
3698
return loadables;
3699
}
3700
3701
lldb::WritableDataBufferSP
3702
ObjectFileELF::MapFileDataWritable(const FileSpec &file, uint64_t Size,
3703
uint64_t Offset) {
3704
return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size,
3705
Offset);
3706
}
3707
3708