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