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