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