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