15ffd83dbSDimitry Andric //===-- ObjectFileELF.cpp -------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "ObjectFileELF.h"
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #include <algorithm>
120b57cec5SDimitry Andric #include <cassert>
130b57cec5SDimitry Andric #include <unordered_map>
140b57cec5SDimitry Andric
150b57cec5SDimitry Andric #include "lldb/Core/FileSpecList.h"
160b57cec5SDimitry Andric #include "lldb/Core/Module.h"
170b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h"
180b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
19*5f7ddb14SDimitry Andric #include "lldb/Core/Progress.h"
200b57cec5SDimitry Andric #include "lldb/Core/Section.h"
210b57cec5SDimitry Andric #include "lldb/Host/FileSystem.h"
229dba64beSDimitry Andric #include "lldb/Host/LZMA.h"
230b57cec5SDimitry Andric #include "lldb/Symbol/DWARFCallFrameInfo.h"
240b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContext.h"
250b57cec5SDimitry Andric #include "lldb/Target/SectionLoadList.h"
260b57cec5SDimitry Andric #include "lldb/Target/Target.h"
270b57cec5SDimitry Andric #include "lldb/Utility/ArchSpec.h"
280b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
290b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
300b57cec5SDimitry Andric #include "lldb/Utility/RangeMap.h"
310b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
320b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
330b57cec5SDimitry Andric #include "lldb/Utility/Timer.h"
340b57cec5SDimitry Andric #include "llvm/ADT/IntervalMap.h"
350b57cec5SDimitry Andric #include "llvm/ADT/PointerUnion.h"
360b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
379dba64beSDimitry Andric #include "llvm/BinaryFormat/ELF.h"
380b57cec5SDimitry Andric #include "llvm/Object/Decompressor.h"
390b57cec5SDimitry Andric #include "llvm/Support/ARMBuildAttributes.h"
409dba64beSDimitry Andric #include "llvm/Support/CRC.h"
41*5f7ddb14SDimitry Andric #include "llvm/Support/FormatVariadic.h"
420b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
430b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
440b57cec5SDimitry Andric #include "llvm/Support/MipsABIFlags.h"
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric #define CASE_AND_STREAM(s, def, width) \
470b57cec5SDimitry Andric case def: \
480b57cec5SDimitry Andric s->Printf("%-*s", width, #def); \
490b57cec5SDimitry Andric break;
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric using namespace lldb;
520b57cec5SDimitry Andric using namespace lldb_private;
530b57cec5SDimitry Andric using namespace elf;
540b57cec5SDimitry Andric using namespace llvm::ELF;
550b57cec5SDimitry Andric
565ffd83dbSDimitry Andric LLDB_PLUGIN_DEFINE(ObjectFileELF)
575ffd83dbSDimitry Andric
580b57cec5SDimitry Andric namespace {
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric // ELF note owner definitions
610b57cec5SDimitry Andric const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD";
620b57cec5SDimitry Andric const char *const LLDB_NT_OWNER_GNU = "GNU";
630b57cec5SDimitry Andric const char *const LLDB_NT_OWNER_NETBSD = "NetBSD";
640b57cec5SDimitry Andric const char *const LLDB_NT_OWNER_NETBSDCORE = "NetBSD-CORE";
650b57cec5SDimitry Andric const char *const LLDB_NT_OWNER_OPENBSD = "OpenBSD";
660b57cec5SDimitry Andric const char *const LLDB_NT_OWNER_ANDROID = "Android";
670b57cec5SDimitry Andric const char *const LLDB_NT_OWNER_CORE = "CORE";
680b57cec5SDimitry Andric const char *const LLDB_NT_OWNER_LINUX = "LINUX";
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric // ELF note type definitions
710b57cec5SDimitry Andric const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01;
720b57cec5SDimitry Andric const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4;
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric const elf_word LLDB_NT_GNU_ABI_TAG = 0x01;
750b57cec5SDimitry Andric const elf_word LLDB_NT_GNU_ABI_SIZE = 16;
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03;
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric const elf_word LLDB_NT_NETBSD_IDENT_TAG = 1;
800b57cec5SDimitry Andric const elf_word LLDB_NT_NETBSD_IDENT_DESCSZ = 4;
810b57cec5SDimitry Andric const elf_word LLDB_NT_NETBSD_IDENT_NAMESZ = 7;
820b57cec5SDimitry Andric const elf_word LLDB_NT_NETBSD_PROCINFO = 1;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric // GNU ABI note OS constants
850b57cec5SDimitry Andric const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00;
860b57cec5SDimitry Andric const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01;
870b57cec5SDimitry Andric const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02;
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
900b57cec5SDimitry Andric /// \class ELFRelocation
910b57cec5SDimitry Andric /// Generic wrapper for ELFRel and ELFRela.
920b57cec5SDimitry Andric ///
930b57cec5SDimitry Andric /// This helper class allows us to parse both ELFRel and ELFRela relocation
940b57cec5SDimitry Andric /// entries in a generic manner.
950b57cec5SDimitry Andric class ELFRelocation {
960b57cec5SDimitry Andric public:
970b57cec5SDimitry Andric /// Constructs an ELFRelocation entry with a personality as given by @p
980b57cec5SDimitry Andric /// type.
990b57cec5SDimitry Andric ///
1000b57cec5SDimitry Andric /// \param type Either DT_REL or DT_RELA. Any other value is invalid.
1010b57cec5SDimitry Andric ELFRelocation(unsigned type);
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric ~ELFRelocation();
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
1060b57cec5SDimitry Andric
1070b57cec5SDimitry Andric static unsigned RelocType32(const ELFRelocation &rel);
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric static unsigned RelocType64(const ELFRelocation &rel);
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric static unsigned RelocSymbol32(const ELFRelocation &rel);
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric static unsigned RelocSymbol64(const ELFRelocation &rel);
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric static unsigned RelocOffset32(const ELFRelocation &rel);
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric static unsigned RelocOffset64(const ELFRelocation &rel);
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric static unsigned RelocAddend32(const ELFRelocation &rel);
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric static unsigned RelocAddend64(const ELFRelocation &rel);
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric private:
1240b57cec5SDimitry Andric typedef llvm::PointerUnion<ELFRel *, ELFRela *> RelocUnion;
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric RelocUnion reloc;
1270b57cec5SDimitry Andric };
1280b57cec5SDimitry Andric
ELFRelocation(unsigned type)1290b57cec5SDimitry Andric ELFRelocation::ELFRelocation(unsigned type) {
1300b57cec5SDimitry Andric if (type == DT_REL || type == SHT_REL)
1310b57cec5SDimitry Andric reloc = new ELFRel();
1320b57cec5SDimitry Andric else if (type == DT_RELA || type == SHT_RELA)
1330b57cec5SDimitry Andric reloc = new ELFRela();
1340b57cec5SDimitry Andric else {
1350b57cec5SDimitry Andric assert(false && "unexpected relocation type");
1360b57cec5SDimitry Andric reloc = static_cast<ELFRel *>(nullptr);
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric
~ELFRelocation()1400b57cec5SDimitry Andric ELFRelocation::~ELFRelocation() {
1410b57cec5SDimitry Andric if (reloc.is<ELFRel *>())
1420b57cec5SDimitry Andric delete reloc.get<ELFRel *>();
1430b57cec5SDimitry Andric else
1440b57cec5SDimitry Andric delete reloc.get<ELFRela *>();
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)1470b57cec5SDimitry Andric bool ELFRelocation::Parse(const lldb_private::DataExtractor &data,
1480b57cec5SDimitry Andric lldb::offset_t *offset) {
1490b57cec5SDimitry Andric if (reloc.is<ELFRel *>())
1500b57cec5SDimitry Andric return reloc.get<ELFRel *>()->Parse(data, offset);
1510b57cec5SDimitry Andric else
1520b57cec5SDimitry Andric return reloc.get<ELFRela *>()->Parse(data, offset);
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric
RelocType32(const ELFRelocation & rel)1550b57cec5SDimitry Andric unsigned ELFRelocation::RelocType32(const ELFRelocation &rel) {
1560b57cec5SDimitry Andric if (rel.reloc.is<ELFRel *>())
1570b57cec5SDimitry Andric return ELFRel::RelocType32(*rel.reloc.get<ELFRel *>());
1580b57cec5SDimitry Andric else
1590b57cec5SDimitry Andric return ELFRela::RelocType32(*rel.reloc.get<ELFRela *>());
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric
RelocType64(const ELFRelocation & rel)1620b57cec5SDimitry Andric unsigned ELFRelocation::RelocType64(const ELFRelocation &rel) {
1630b57cec5SDimitry Andric if (rel.reloc.is<ELFRel *>())
1640b57cec5SDimitry Andric return ELFRel::RelocType64(*rel.reloc.get<ELFRel *>());
1650b57cec5SDimitry Andric else
1660b57cec5SDimitry Andric return ELFRela::RelocType64(*rel.reloc.get<ELFRela *>());
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric
RelocSymbol32(const ELFRelocation & rel)1690b57cec5SDimitry Andric unsigned ELFRelocation::RelocSymbol32(const ELFRelocation &rel) {
1700b57cec5SDimitry Andric if (rel.reloc.is<ELFRel *>())
1710b57cec5SDimitry Andric return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel *>());
1720b57cec5SDimitry Andric else
1730b57cec5SDimitry Andric return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela *>());
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric
RelocSymbol64(const ELFRelocation & rel)1760b57cec5SDimitry Andric unsigned ELFRelocation::RelocSymbol64(const ELFRelocation &rel) {
1770b57cec5SDimitry Andric if (rel.reloc.is<ELFRel *>())
1780b57cec5SDimitry Andric return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel *>());
1790b57cec5SDimitry Andric else
1800b57cec5SDimitry Andric return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela *>());
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric
RelocOffset32(const ELFRelocation & rel)1830b57cec5SDimitry Andric unsigned ELFRelocation::RelocOffset32(const ELFRelocation &rel) {
1840b57cec5SDimitry Andric if (rel.reloc.is<ELFRel *>())
1850b57cec5SDimitry Andric return rel.reloc.get<ELFRel *>()->r_offset;
1860b57cec5SDimitry Andric else
1870b57cec5SDimitry Andric return rel.reloc.get<ELFRela *>()->r_offset;
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric
RelocOffset64(const ELFRelocation & rel)1900b57cec5SDimitry Andric unsigned ELFRelocation::RelocOffset64(const ELFRelocation &rel) {
1910b57cec5SDimitry Andric if (rel.reloc.is<ELFRel *>())
1920b57cec5SDimitry Andric return rel.reloc.get<ELFRel *>()->r_offset;
1930b57cec5SDimitry Andric else
1940b57cec5SDimitry Andric return rel.reloc.get<ELFRela *>()->r_offset;
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric
RelocAddend32(const ELFRelocation & rel)1970b57cec5SDimitry Andric unsigned ELFRelocation::RelocAddend32(const ELFRelocation &rel) {
1980b57cec5SDimitry Andric if (rel.reloc.is<ELFRel *>())
1990b57cec5SDimitry Andric return 0;
2000b57cec5SDimitry Andric else
2010b57cec5SDimitry Andric return rel.reloc.get<ELFRela *>()->r_addend;
2020b57cec5SDimitry Andric }
2030b57cec5SDimitry Andric
RelocAddend64(const ELFRelocation & rel)2040b57cec5SDimitry Andric unsigned ELFRelocation::RelocAddend64(const ELFRelocation &rel) {
2050b57cec5SDimitry Andric if (rel.reloc.is<ELFRel *>())
2060b57cec5SDimitry Andric return 0;
2070b57cec5SDimitry Andric else
2080b57cec5SDimitry Andric return rel.reloc.get<ELFRela *>()->r_addend;
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric } // end anonymous namespace
2120b57cec5SDimitry Andric
SegmentID(size_t PHdrIndex)2135ffd83dbSDimitry Andric static user_id_t SegmentID(size_t PHdrIndex) {
2145ffd83dbSDimitry Andric return ~user_id_t(PHdrIndex);
2155ffd83dbSDimitry Andric }
2160b57cec5SDimitry Andric
Parse(const DataExtractor & data,lldb::offset_t * offset)2170b57cec5SDimitry Andric bool ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) {
2180b57cec5SDimitry Andric // Read all fields.
2190b57cec5SDimitry Andric if (data.GetU32(offset, &n_namesz, 3) == nullptr)
2200b57cec5SDimitry Andric return false;
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric // The name field is required to be nul-terminated, and n_namesz includes the
2230b57cec5SDimitry Andric // terminating nul in observed implementations (contrary to the ELF-64 spec).
2240b57cec5SDimitry Andric // A special case is needed for cores generated by some older Linux versions,
2250b57cec5SDimitry Andric // which write a note named "CORE" without a nul terminator and n_namesz = 4.
2260b57cec5SDimitry Andric if (n_namesz == 4) {
2270b57cec5SDimitry Andric char buf[4];
2280b57cec5SDimitry Andric if (data.ExtractBytes(*offset, 4, data.GetByteOrder(), buf) != 4)
2290b57cec5SDimitry Andric return false;
2300b57cec5SDimitry Andric if (strncmp(buf, "CORE", 4) == 0) {
2310b57cec5SDimitry Andric n_name = "CORE";
2320b57cec5SDimitry Andric *offset += 4;
2330b57cec5SDimitry Andric return true;
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric const char *cstr = data.GetCStr(offset, llvm::alignTo(n_namesz, 4));
2380b57cec5SDimitry Andric if (cstr == nullptr) {
2390b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS));
2409dba64beSDimitry Andric LLDB_LOGF(log, "Failed to parse note name lacking nul terminator");
2410b57cec5SDimitry Andric
2420b57cec5SDimitry Andric return false;
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric n_name = cstr;
2450b57cec5SDimitry Andric return true;
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric
mipsVariantFromElfFlags(const elf::ELFHeader & header)2480b57cec5SDimitry Andric static uint32_t mipsVariantFromElfFlags (const elf::ELFHeader &header) {
2490b57cec5SDimitry Andric const uint32_t mips_arch = header.e_flags & llvm::ELF::EF_MIPS_ARCH;
2500b57cec5SDimitry Andric uint32_t endian = header.e_ident[EI_DATA];
2510b57cec5SDimitry Andric uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown;
2520b57cec5SDimitry Andric uint32_t fileclass = header.e_ident[EI_CLASS];
2530b57cec5SDimitry Andric
2540b57cec5SDimitry Andric // If there aren't any elf flags available (e.g core elf file) then return
2550b57cec5SDimitry Andric // default
2560b57cec5SDimitry Andric // 32 or 64 bit arch (without any architecture revision) based on object file's class.
2570b57cec5SDimitry Andric if (header.e_type == ET_CORE) {
2580b57cec5SDimitry Andric switch (fileclass) {
2590b57cec5SDimitry Andric case llvm::ELF::ELFCLASS32:
2600b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el
2610b57cec5SDimitry Andric : ArchSpec::eMIPSSubType_mips32;
2620b57cec5SDimitry Andric case llvm::ELF::ELFCLASS64:
2630b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el
2640b57cec5SDimitry Andric : ArchSpec::eMIPSSubType_mips64;
2650b57cec5SDimitry Andric default:
2660b57cec5SDimitry Andric return arch_variant;
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andric switch (mips_arch) {
2710b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_1:
2720b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_2:
2730b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_32:
2740b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el
2750b57cec5SDimitry Andric : ArchSpec::eMIPSSubType_mips32;
2760b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_32R2:
2770b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el
2780b57cec5SDimitry Andric : ArchSpec::eMIPSSubType_mips32r2;
2790b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_32R6:
2800b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el
2810b57cec5SDimitry Andric : ArchSpec::eMIPSSubType_mips32r6;
2820b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_3:
2830b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_4:
2840b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_5:
2850b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_64:
2860b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el
2870b57cec5SDimitry Andric : ArchSpec::eMIPSSubType_mips64;
2880b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_64R2:
2890b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el
2900b57cec5SDimitry Andric : ArchSpec::eMIPSSubType_mips64r2;
2910b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_64R6:
2920b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el
2930b57cec5SDimitry Andric : ArchSpec::eMIPSSubType_mips64r6;
2940b57cec5SDimitry Andric default:
2950b57cec5SDimitry Andric break;
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andric return arch_variant;
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric
riscvVariantFromElfFlags(const elf::ELFHeader & header)301af732203SDimitry Andric static uint32_t riscvVariantFromElfFlags(const elf::ELFHeader &header) {
302af732203SDimitry Andric uint32_t fileclass = header.e_ident[EI_CLASS];
303af732203SDimitry Andric switch (fileclass) {
304af732203SDimitry Andric case llvm::ELF::ELFCLASS32:
305af732203SDimitry Andric return ArchSpec::eRISCVSubType_riscv32;
306af732203SDimitry Andric case llvm::ELF::ELFCLASS64:
307af732203SDimitry Andric return ArchSpec::eRISCVSubType_riscv64;
308af732203SDimitry Andric default:
309af732203SDimitry Andric return ArchSpec::eRISCVSubType_unknown;
310af732203SDimitry Andric }
311af732203SDimitry Andric }
312af732203SDimitry Andric
subTypeFromElfHeader(const elf::ELFHeader & header)3130b57cec5SDimitry Andric static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) {
3140b57cec5SDimitry Andric if (header.e_machine == llvm::ELF::EM_MIPS)
3150b57cec5SDimitry Andric return mipsVariantFromElfFlags(header);
316af732203SDimitry Andric else if (header.e_machine == llvm::ELF::EM_RISCV)
317af732203SDimitry Andric return riscvVariantFromElfFlags(header);
3180b57cec5SDimitry Andric
3190b57cec5SDimitry Andric return LLDB_INVALID_CPUTYPE;
3200b57cec5SDimitry Andric }
3210b57cec5SDimitry Andric
3229dba64beSDimitry Andric char ObjectFileELF::ID;
3239dba64beSDimitry Andric
3240b57cec5SDimitry Andric // Arbitrary constant used as UUID prefix for core files.
3250b57cec5SDimitry Andric const uint32_t ObjectFileELF::g_core_uuid_magic(0xE210C);
3260b57cec5SDimitry Andric
3270b57cec5SDimitry Andric // Static methods.
Initialize()3280b57cec5SDimitry Andric void ObjectFileELF::Initialize() {
3290b57cec5SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(),
3300b57cec5SDimitry Andric GetPluginDescriptionStatic(), CreateInstance,
3310b57cec5SDimitry Andric CreateMemoryInstance, GetModuleSpecifications);
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric
Terminate()3340b57cec5SDimitry Andric void ObjectFileELF::Terminate() {
3350b57cec5SDimitry Andric PluginManager::UnregisterPlugin(CreateInstance);
3360b57cec5SDimitry Andric }
3370b57cec5SDimitry Andric
GetPluginNameStatic()3380b57cec5SDimitry Andric lldb_private::ConstString ObjectFileELF::GetPluginNameStatic() {
3390b57cec5SDimitry Andric static ConstString g_name("elf");
3400b57cec5SDimitry Andric return g_name;
3410b57cec5SDimitry Andric }
3420b57cec5SDimitry Andric
GetPluginDescriptionStatic()3430b57cec5SDimitry Andric const char *ObjectFileELF::GetPluginDescriptionStatic() {
3440b57cec5SDimitry Andric return "ELF object file reader.";
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric
CreateInstance(const lldb::ModuleSP & module_sp,DataBufferSP & data_sp,lldb::offset_t data_offset,const lldb_private::FileSpec * file,lldb::offset_t file_offset,lldb::offset_t length)3470b57cec5SDimitry Andric ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp,
3480b57cec5SDimitry Andric DataBufferSP &data_sp,
3490b57cec5SDimitry Andric lldb::offset_t data_offset,
3500b57cec5SDimitry Andric const lldb_private::FileSpec *file,
3510b57cec5SDimitry Andric lldb::offset_t file_offset,
3520b57cec5SDimitry Andric lldb::offset_t length) {
3530b57cec5SDimitry Andric if (!data_sp) {
3540b57cec5SDimitry Andric data_sp = MapFileData(*file, length, file_offset);
3550b57cec5SDimitry Andric if (!data_sp)
3560b57cec5SDimitry Andric return nullptr;
3570b57cec5SDimitry Andric data_offset = 0;
3580b57cec5SDimitry Andric }
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andric assert(data_sp);
3610b57cec5SDimitry Andric
3620b57cec5SDimitry Andric if (data_sp->GetByteSize() <= (llvm::ELF::EI_NIDENT + data_offset))
3630b57cec5SDimitry Andric return nullptr;
3640b57cec5SDimitry Andric
3650b57cec5SDimitry Andric const uint8_t *magic = data_sp->GetBytes() + data_offset;
3660b57cec5SDimitry Andric if (!ELFHeader::MagicBytesMatch(magic))
3670b57cec5SDimitry Andric return nullptr;
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andric // Update the data to contain the entire file if it doesn't already
3700b57cec5SDimitry Andric if (data_sp->GetByteSize() < length) {
3710b57cec5SDimitry Andric data_sp = MapFileData(*file, length, file_offset);
3720b57cec5SDimitry Andric if (!data_sp)
3730b57cec5SDimitry Andric return nullptr;
3740b57cec5SDimitry Andric data_offset = 0;
3750b57cec5SDimitry Andric magic = data_sp->GetBytes();
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric
3780b57cec5SDimitry Andric unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
3790b57cec5SDimitry Andric if (address_size == 4 || address_size == 8) {
3800b57cec5SDimitry Andric std::unique_ptr<ObjectFileELF> objfile_up(new ObjectFileELF(
3810b57cec5SDimitry Andric module_sp, data_sp, data_offset, file, file_offset, length));
3820b57cec5SDimitry Andric ArchSpec spec = objfile_up->GetArchitecture();
3830b57cec5SDimitry Andric if (spec && objfile_up->SetModulesArchitecture(spec))
3840b57cec5SDimitry Andric return objfile_up.release();
3850b57cec5SDimitry Andric }
3860b57cec5SDimitry Andric
3870b57cec5SDimitry Andric return nullptr;
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric
CreateMemoryInstance(const lldb::ModuleSP & module_sp,DataBufferSP & data_sp,const lldb::ProcessSP & process_sp,lldb::addr_t header_addr)3900b57cec5SDimitry Andric ObjectFile *ObjectFileELF::CreateMemoryInstance(
3910b57cec5SDimitry Andric const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
3920b57cec5SDimitry Andric const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
3930b57cec5SDimitry Andric if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT)) {
3940b57cec5SDimitry Andric const uint8_t *magic = data_sp->GetBytes();
3950b57cec5SDimitry Andric if (ELFHeader::MagicBytesMatch(magic)) {
3960b57cec5SDimitry Andric unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
3970b57cec5SDimitry Andric if (address_size == 4 || address_size == 8) {
3980b57cec5SDimitry Andric std::unique_ptr<ObjectFileELF> objfile_up(
3990b57cec5SDimitry Andric new ObjectFileELF(module_sp, data_sp, process_sp, header_addr));
4000b57cec5SDimitry Andric ArchSpec spec = objfile_up->GetArchitecture();
4010b57cec5SDimitry Andric if (spec && objfile_up->SetModulesArchitecture(spec))
4020b57cec5SDimitry Andric return objfile_up.release();
4030b57cec5SDimitry Andric }
4040b57cec5SDimitry Andric }
4050b57cec5SDimitry Andric }
4060b57cec5SDimitry Andric return nullptr;
4070b57cec5SDimitry Andric }
4080b57cec5SDimitry Andric
MagicBytesMatch(DataBufferSP & data_sp,lldb::addr_t data_offset,lldb::addr_t data_length)4090b57cec5SDimitry Andric bool ObjectFileELF::MagicBytesMatch(DataBufferSP &data_sp,
4100b57cec5SDimitry Andric lldb::addr_t data_offset,
4110b57cec5SDimitry Andric lldb::addr_t data_length) {
4120b57cec5SDimitry Andric if (data_sp &&
4130b57cec5SDimitry Andric data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) {
4140b57cec5SDimitry Andric const uint8_t *magic = data_sp->GetBytes() + data_offset;
4150b57cec5SDimitry Andric return ELFHeader::MagicBytesMatch(magic);
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric return false;
4180b57cec5SDimitry Andric }
4190b57cec5SDimitry Andric
calc_crc32(uint32_t init,const DataExtractor & data)4209dba64beSDimitry Andric static uint32_t calc_crc32(uint32_t init, const DataExtractor &data) {
4219dba64beSDimitry Andric return llvm::crc32(
4229dba64beSDimitry Andric init, llvm::makeArrayRef(data.GetDataStart(), data.GetByteSize()));
4230b57cec5SDimitry Andric }
4240b57cec5SDimitry Andric
CalculateELFNotesSegmentsCRC32(const ProgramHeaderColl & program_headers,DataExtractor & object_data)4250b57cec5SDimitry Andric uint32_t ObjectFileELF::CalculateELFNotesSegmentsCRC32(
4260b57cec5SDimitry Andric const ProgramHeaderColl &program_headers, DataExtractor &object_data) {
4270b57cec5SDimitry Andric
4280b57cec5SDimitry Andric uint32_t core_notes_crc = 0;
4290b57cec5SDimitry Andric
4300b57cec5SDimitry Andric for (const ELFProgramHeader &H : program_headers) {
4310b57cec5SDimitry Andric if (H.p_type == llvm::ELF::PT_NOTE) {
4320b57cec5SDimitry Andric const elf_off ph_offset = H.p_offset;
4330b57cec5SDimitry Andric const size_t ph_size = H.p_filesz;
4340b57cec5SDimitry Andric
4350b57cec5SDimitry Andric DataExtractor segment_data;
4360b57cec5SDimitry Andric if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size) {
4370b57cec5SDimitry Andric // The ELF program header contained incorrect data, probably corefile
4380b57cec5SDimitry Andric // is incomplete or corrupted.
4390b57cec5SDimitry Andric break;
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric
4429dba64beSDimitry Andric core_notes_crc = calc_crc32(core_notes_crc, segment_data);
4430b57cec5SDimitry Andric }
4440b57cec5SDimitry Andric }
4450b57cec5SDimitry Andric
4460b57cec5SDimitry Andric return core_notes_crc;
4470b57cec5SDimitry Andric }
4480b57cec5SDimitry Andric
OSABIAsCString(unsigned char osabi_byte)4490b57cec5SDimitry Andric static const char *OSABIAsCString(unsigned char osabi_byte) {
4500b57cec5SDimitry Andric #define _MAKE_OSABI_CASE(x) \
4510b57cec5SDimitry Andric case x: \
4520b57cec5SDimitry Andric return #x
4530b57cec5SDimitry Andric switch (osabi_byte) {
4540b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_NONE);
4550b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_HPUX);
4560b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_NETBSD);
4570b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_GNU);
4580b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_HURD);
4590b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_SOLARIS);
4600b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_AIX);
4610b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_IRIX);
4620b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_FREEBSD);
4630b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_TRU64);
4640b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_MODESTO);
4650b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_OPENBSD);
4660b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_OPENVMS);
4670b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_NSK);
4680b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_AROS);
4690b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_FENIXOS);
4700b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI);
4710b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX);
4720b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_ARM);
4730b57cec5SDimitry Andric _MAKE_OSABI_CASE(ELFOSABI_STANDALONE);
4740b57cec5SDimitry Andric default:
4750b57cec5SDimitry Andric return "<unknown-osabi>";
4760b57cec5SDimitry Andric }
4770b57cec5SDimitry Andric #undef _MAKE_OSABI_CASE
4780b57cec5SDimitry Andric }
4790b57cec5SDimitry Andric
4800b57cec5SDimitry Andric //
4810b57cec5SDimitry Andric // WARNING : This function is being deprecated
4820b57cec5SDimitry Andric // It's functionality has moved to ArchSpec::SetArchitecture This function is
4830b57cec5SDimitry Andric // only being kept to validate the move.
4840b57cec5SDimitry Andric //
4850b57cec5SDimitry Andric // TODO : Remove this function
GetOsFromOSABI(unsigned char osabi_byte,llvm::Triple::OSType & ostype)4860b57cec5SDimitry Andric static bool GetOsFromOSABI(unsigned char osabi_byte,
4870b57cec5SDimitry Andric llvm::Triple::OSType &ostype) {
4880b57cec5SDimitry Andric switch (osabi_byte) {
4890b57cec5SDimitry Andric case ELFOSABI_AIX:
4900b57cec5SDimitry Andric ostype = llvm::Triple::OSType::AIX;
4910b57cec5SDimitry Andric break;
4920b57cec5SDimitry Andric case ELFOSABI_FREEBSD:
4930b57cec5SDimitry Andric ostype = llvm::Triple::OSType::FreeBSD;
4940b57cec5SDimitry Andric break;
4950b57cec5SDimitry Andric case ELFOSABI_GNU:
4960b57cec5SDimitry Andric ostype = llvm::Triple::OSType::Linux;
4970b57cec5SDimitry Andric break;
4980b57cec5SDimitry Andric case ELFOSABI_NETBSD:
4990b57cec5SDimitry Andric ostype = llvm::Triple::OSType::NetBSD;
5000b57cec5SDimitry Andric break;
5010b57cec5SDimitry Andric case ELFOSABI_OPENBSD:
5020b57cec5SDimitry Andric ostype = llvm::Triple::OSType::OpenBSD;
5030b57cec5SDimitry Andric break;
5040b57cec5SDimitry Andric case ELFOSABI_SOLARIS:
5050b57cec5SDimitry Andric ostype = llvm::Triple::OSType::Solaris;
5060b57cec5SDimitry Andric break;
5070b57cec5SDimitry Andric default:
5080b57cec5SDimitry Andric ostype = llvm::Triple::OSType::UnknownOS;
5090b57cec5SDimitry Andric }
5100b57cec5SDimitry Andric return ostype != llvm::Triple::OSType::UnknownOS;
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric
GetModuleSpecifications(const lldb_private::FileSpec & file,lldb::DataBufferSP & data_sp,lldb::offset_t data_offset,lldb::offset_t file_offset,lldb::offset_t length,lldb_private::ModuleSpecList & specs)5130b57cec5SDimitry Andric size_t ObjectFileELF::GetModuleSpecifications(
5140b57cec5SDimitry Andric const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
5150b57cec5SDimitry Andric lldb::offset_t data_offset, lldb::offset_t file_offset,
5160b57cec5SDimitry Andric lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
5170b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
5180b57cec5SDimitry Andric
5190b57cec5SDimitry Andric const size_t initial_count = specs.GetSize();
5200b57cec5SDimitry Andric
5210b57cec5SDimitry Andric if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) {
5220b57cec5SDimitry Andric DataExtractor data;
5230b57cec5SDimitry Andric data.SetData(data_sp);
5240b57cec5SDimitry Andric elf::ELFHeader header;
5250b57cec5SDimitry Andric lldb::offset_t header_offset = data_offset;
5260b57cec5SDimitry Andric if (header.Parse(data, &header_offset)) {
5270b57cec5SDimitry Andric if (data_sp) {
5280b57cec5SDimitry Andric ModuleSpec spec(file);
5290b57cec5SDimitry Andric
5300b57cec5SDimitry Andric const uint32_t sub_type = subTypeFromElfHeader(header);
5310b57cec5SDimitry Andric spec.GetArchitecture().SetArchitecture(
5320b57cec5SDimitry Andric eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]);
5330b57cec5SDimitry Andric
5340b57cec5SDimitry Andric if (spec.GetArchitecture().IsValid()) {
5350b57cec5SDimitry Andric llvm::Triple::OSType ostype;
5360b57cec5SDimitry Andric llvm::Triple::VendorType vendor;
5370b57cec5SDimitry Andric llvm::Triple::OSType spec_ostype =
5380b57cec5SDimitry Andric spec.GetArchitecture().GetTriple().getOS();
5390b57cec5SDimitry Andric
5409dba64beSDimitry Andric LLDB_LOGF(log, "ObjectFileELF::%s file '%s' module OSABI: %s",
5410b57cec5SDimitry Andric __FUNCTION__, file.GetPath().c_str(),
5420b57cec5SDimitry Andric OSABIAsCString(header.e_ident[EI_OSABI]));
5430b57cec5SDimitry Andric
5440b57cec5SDimitry Andric // SetArchitecture should have set the vendor to unknown
5450b57cec5SDimitry Andric vendor = spec.GetArchitecture().GetTriple().getVendor();
5460b57cec5SDimitry Andric assert(vendor == llvm::Triple::UnknownVendor);
5470b57cec5SDimitry Andric UNUSED_IF_ASSERT_DISABLED(vendor);
5480b57cec5SDimitry Andric
5490b57cec5SDimitry Andric //
5500b57cec5SDimitry Andric // Validate it is ok to remove GetOsFromOSABI
5510b57cec5SDimitry Andric GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);
5520b57cec5SDimitry Andric assert(spec_ostype == ostype);
5530b57cec5SDimitry Andric if (spec_ostype != llvm::Triple::OSType::UnknownOS) {
5549dba64beSDimitry Andric LLDB_LOGF(log,
5559dba64beSDimitry Andric "ObjectFileELF::%s file '%s' set ELF module OS type "
5560b57cec5SDimitry Andric "from ELF header OSABI.",
5570b57cec5SDimitry Andric __FUNCTION__, file.GetPath().c_str());
5580b57cec5SDimitry Andric }
5590b57cec5SDimitry Andric
5605ffd83dbSDimitry Andric if (data_sp->GetByteSize() < length)
5610b57cec5SDimitry Andric data_sp = MapFileData(file, -1, file_offset);
5620b57cec5SDimitry Andric if (data_sp)
5630b57cec5SDimitry Andric data.SetData(data_sp);
5640b57cec5SDimitry Andric // In case there is header extension in the section #0, the header we
5650b57cec5SDimitry Andric // parsed above could have sentinel values for e_phnum, e_shnum, and
5660b57cec5SDimitry Andric // e_shstrndx. In this case we need to reparse the header with a
5670b57cec5SDimitry Andric // bigger data source to get the actual values.
5680b57cec5SDimitry Andric if (header.HasHeaderExtension()) {
5690b57cec5SDimitry Andric lldb::offset_t header_offset = data_offset;
5700b57cec5SDimitry Andric header.Parse(data, &header_offset);
5710b57cec5SDimitry Andric }
5720b57cec5SDimitry Andric
5730b57cec5SDimitry Andric uint32_t gnu_debuglink_crc = 0;
5740b57cec5SDimitry Andric std::string gnu_debuglink_file;
5750b57cec5SDimitry Andric SectionHeaderColl section_headers;
5760b57cec5SDimitry Andric lldb_private::UUID &uuid = spec.GetUUID();
5770b57cec5SDimitry Andric
5780b57cec5SDimitry Andric GetSectionHeaderInfo(section_headers, data, header, uuid,
5790b57cec5SDimitry Andric gnu_debuglink_file, gnu_debuglink_crc,
5800b57cec5SDimitry Andric spec.GetArchitecture());
5810b57cec5SDimitry Andric
5820b57cec5SDimitry Andric llvm::Triple &spec_triple = spec.GetArchitecture().GetTriple();
5830b57cec5SDimitry Andric
5849dba64beSDimitry Andric LLDB_LOGF(log,
5859dba64beSDimitry Andric "ObjectFileELF::%s file '%s' module set to triple: %s "
5860b57cec5SDimitry Andric "(architecture %s)",
5870b57cec5SDimitry Andric __FUNCTION__, file.GetPath().c_str(),
5880b57cec5SDimitry Andric spec_triple.getTriple().c_str(),
5890b57cec5SDimitry Andric spec.GetArchitecture().GetArchitectureName());
5900b57cec5SDimitry Andric
5910b57cec5SDimitry Andric if (!uuid.IsValid()) {
5920b57cec5SDimitry Andric uint32_t core_notes_crc = 0;
5930b57cec5SDimitry Andric
5940b57cec5SDimitry Andric if (!gnu_debuglink_crc) {
595af732203SDimitry Andric LLDB_SCOPED_TIMERF(
5960b57cec5SDimitry Andric "Calculating module crc32 %s with size %" PRIu64 " KiB",
5970b57cec5SDimitry Andric file.GetLastPathComponent().AsCString(),
5985ffd83dbSDimitry Andric (length - file_offset) / 1024);
5990b57cec5SDimitry Andric
6000b57cec5SDimitry Andric // For core files - which usually don't happen to have a
6010b57cec5SDimitry Andric // gnu_debuglink, and are pretty bulky - calculating whole
6020b57cec5SDimitry Andric // contents crc32 would be too much of luxury. Thus we will need
6030b57cec5SDimitry Andric // to fallback to something simpler.
6040b57cec5SDimitry Andric if (header.e_type == llvm::ELF::ET_CORE) {
6050b57cec5SDimitry Andric ProgramHeaderColl program_headers;
6060b57cec5SDimitry Andric GetProgramHeaderInfo(program_headers, data, header);
6070b57cec5SDimitry Andric
6080b57cec5SDimitry Andric core_notes_crc =
6090b57cec5SDimitry Andric CalculateELFNotesSegmentsCRC32(program_headers, data);
6100b57cec5SDimitry Andric } else {
6119dba64beSDimitry Andric gnu_debuglink_crc = calc_crc32(0, data);
6120b57cec5SDimitry Andric }
6130b57cec5SDimitry Andric }
6140b57cec5SDimitry Andric using u32le = llvm::support::ulittle32_t;
6150b57cec5SDimitry Andric if (gnu_debuglink_crc) {
6160b57cec5SDimitry Andric // Use 4 bytes of crc from the .gnu_debuglink section.
6170b57cec5SDimitry Andric u32le data(gnu_debuglink_crc);
6180b57cec5SDimitry Andric uuid = UUID::fromData(&data, sizeof(data));
6190b57cec5SDimitry Andric } else if (core_notes_crc) {
6200b57cec5SDimitry Andric // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make
6210b57cec5SDimitry Andric // it look different form .gnu_debuglink crc followed by 4 bytes
6220b57cec5SDimitry Andric // of note segments crc.
6230b57cec5SDimitry Andric u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};
6240b57cec5SDimitry Andric uuid = UUID::fromData(data, sizeof(data));
6250b57cec5SDimitry Andric }
6260b57cec5SDimitry Andric }
6270b57cec5SDimitry Andric
6280b57cec5SDimitry Andric specs.Append(spec);
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric }
6310b57cec5SDimitry Andric }
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric
6340b57cec5SDimitry Andric return specs.GetSize() - initial_count;
6350b57cec5SDimitry Andric }
6360b57cec5SDimitry Andric
6370b57cec5SDimitry Andric // PluginInterface protocol
GetPluginName()6380b57cec5SDimitry Andric lldb_private::ConstString ObjectFileELF::GetPluginName() {
6390b57cec5SDimitry Andric return GetPluginNameStatic();
6400b57cec5SDimitry Andric }
6410b57cec5SDimitry Andric
GetPluginVersion()6420b57cec5SDimitry Andric uint32_t ObjectFileELF::GetPluginVersion() { return m_plugin_version; }
6430b57cec5SDimitry Andric // ObjectFile protocol
6440b57cec5SDimitry Andric
ObjectFileELF(const lldb::ModuleSP & module_sp,DataBufferSP & data_sp,lldb::offset_t data_offset,const FileSpec * file,lldb::offset_t file_offset,lldb::offset_t length)6450b57cec5SDimitry Andric ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,
6460b57cec5SDimitry Andric DataBufferSP &data_sp, lldb::offset_t data_offset,
6470b57cec5SDimitry Andric const FileSpec *file, lldb::offset_t file_offset,
6480b57cec5SDimitry Andric lldb::offset_t length)
6499dba64beSDimitry Andric : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset) {
6500b57cec5SDimitry Andric if (file)
6510b57cec5SDimitry Andric m_file = *file;
6520b57cec5SDimitry Andric }
6530b57cec5SDimitry Andric
ObjectFileELF(const lldb::ModuleSP & module_sp,DataBufferSP & header_data_sp,const lldb::ProcessSP & process_sp,addr_t header_addr)6540b57cec5SDimitry Andric ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,
6550b57cec5SDimitry Andric DataBufferSP &header_data_sp,
6560b57cec5SDimitry Andric const lldb::ProcessSP &process_sp,
6570b57cec5SDimitry Andric addr_t header_addr)
6589dba64beSDimitry Andric : ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {}
6590b57cec5SDimitry Andric
IsExecutable() const6600b57cec5SDimitry Andric bool ObjectFileELF::IsExecutable() const {
6610b57cec5SDimitry Andric return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0);
6620b57cec5SDimitry Andric }
6630b57cec5SDimitry Andric
SetLoadAddress(Target & target,lldb::addr_t value,bool value_is_offset)6640b57cec5SDimitry Andric bool ObjectFileELF::SetLoadAddress(Target &target, lldb::addr_t value,
6650b57cec5SDimitry Andric bool value_is_offset) {
6660b57cec5SDimitry Andric ModuleSP module_sp = GetModule();
6670b57cec5SDimitry Andric if (module_sp) {
6680b57cec5SDimitry Andric size_t num_loaded_sections = 0;
6690b57cec5SDimitry Andric SectionList *section_list = GetSectionList();
6700b57cec5SDimitry Andric if (section_list) {
6710b57cec5SDimitry Andric if (!value_is_offset) {
6720b57cec5SDimitry Andric addr_t base = GetBaseAddress().GetFileAddress();
6730b57cec5SDimitry Andric if (base == LLDB_INVALID_ADDRESS)
6740b57cec5SDimitry Andric return false;
6750b57cec5SDimitry Andric value -= base;
6760b57cec5SDimitry Andric }
6770b57cec5SDimitry Andric
6780b57cec5SDimitry Andric const size_t num_sections = section_list->GetSize();
6790b57cec5SDimitry Andric size_t sect_idx = 0;
6800b57cec5SDimitry Andric
6810b57cec5SDimitry Andric for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
6820b57cec5SDimitry Andric // Iterate through the object file sections to find all of the sections
6830b57cec5SDimitry Andric // that have SHF_ALLOC in their flag bits.
6840b57cec5SDimitry Andric SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
6850b57cec5SDimitry Andric if (section_sp->Test(SHF_ALLOC) ||
6860b57cec5SDimitry Andric section_sp->GetType() == eSectionTypeContainer) {
6870b57cec5SDimitry Andric lldb::addr_t load_addr = section_sp->GetFileAddress();
6880b57cec5SDimitry Andric // We don't want to update the load address of a section with type
6890b57cec5SDimitry Andric // eSectionTypeAbsoluteAddress as they already have the absolute load
6900b57cec5SDimitry Andric // address already specified
6910b57cec5SDimitry Andric if (section_sp->GetType() != eSectionTypeAbsoluteAddress)
6920b57cec5SDimitry Andric load_addr += value;
6930b57cec5SDimitry Andric
6940b57cec5SDimitry Andric // On 32-bit systems the load address have to fit into 4 bytes. The
6950b57cec5SDimitry Andric // rest of the bytes are the overflow from the addition.
6960b57cec5SDimitry Andric if (GetAddressByteSize() == 4)
6970b57cec5SDimitry Andric load_addr &= 0xFFFFFFFF;
6980b57cec5SDimitry Andric
6990b57cec5SDimitry Andric if (target.GetSectionLoadList().SetSectionLoadAddress(section_sp,
7000b57cec5SDimitry Andric load_addr))
7010b57cec5SDimitry Andric ++num_loaded_sections;
7020b57cec5SDimitry Andric }
7030b57cec5SDimitry Andric }
7040b57cec5SDimitry Andric return num_loaded_sections > 0;
7050b57cec5SDimitry Andric }
7060b57cec5SDimitry Andric }
7070b57cec5SDimitry Andric return false;
7080b57cec5SDimitry Andric }
7090b57cec5SDimitry Andric
GetByteOrder() const7100b57cec5SDimitry Andric ByteOrder ObjectFileELF::GetByteOrder() const {
7110b57cec5SDimitry Andric if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
7120b57cec5SDimitry Andric return eByteOrderBig;
7130b57cec5SDimitry Andric if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
7140b57cec5SDimitry Andric return eByteOrderLittle;
7150b57cec5SDimitry Andric return eByteOrderInvalid;
7160b57cec5SDimitry Andric }
7170b57cec5SDimitry Andric
GetAddressByteSize() const7180b57cec5SDimitry Andric uint32_t ObjectFileELF::GetAddressByteSize() const {
7190b57cec5SDimitry Andric return m_data.GetAddressByteSize();
7200b57cec5SDimitry Andric }
7210b57cec5SDimitry Andric
GetAddressClass(addr_t file_addr)7220b57cec5SDimitry Andric AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) {
7230b57cec5SDimitry Andric Symtab *symtab = GetSymtab();
7240b57cec5SDimitry Andric if (!symtab)
7250b57cec5SDimitry Andric return AddressClass::eUnknown;
7260b57cec5SDimitry Andric
7270b57cec5SDimitry Andric // The address class is determined based on the symtab. Ask it from the
7280b57cec5SDimitry Andric // object file what contains the symtab information.
7290b57cec5SDimitry Andric ObjectFile *symtab_objfile = symtab->GetObjectFile();
7300b57cec5SDimitry Andric if (symtab_objfile != nullptr && symtab_objfile != this)
7310b57cec5SDimitry Andric return symtab_objfile->GetAddressClass(file_addr);
7320b57cec5SDimitry Andric
7330b57cec5SDimitry Andric auto res = ObjectFile::GetAddressClass(file_addr);
7340b57cec5SDimitry Andric if (res != AddressClass::eCode)
7350b57cec5SDimitry Andric return res;
7360b57cec5SDimitry Andric
7370b57cec5SDimitry Andric auto ub = m_address_class_map.upper_bound(file_addr);
7380b57cec5SDimitry Andric if (ub == m_address_class_map.begin()) {
7390b57cec5SDimitry Andric // No entry in the address class map before the address. Return default
7400b57cec5SDimitry Andric // address class for an address in a code section.
7410b57cec5SDimitry Andric return AddressClass::eCode;
7420b57cec5SDimitry Andric }
7430b57cec5SDimitry Andric
7440b57cec5SDimitry Andric // Move iterator to the address class entry preceding address
7450b57cec5SDimitry Andric --ub;
7460b57cec5SDimitry Andric
7470b57cec5SDimitry Andric return ub->second;
7480b57cec5SDimitry Andric }
7490b57cec5SDimitry Andric
SectionIndex(const SectionHeaderCollIter & I)7500b57cec5SDimitry Andric size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) {
7510b57cec5SDimitry Andric return std::distance(m_section_headers.begin(), I);
7520b57cec5SDimitry Andric }
7530b57cec5SDimitry Andric
SectionIndex(const SectionHeaderCollConstIter & I) const7540b57cec5SDimitry Andric size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const {
7550b57cec5SDimitry Andric return std::distance(m_section_headers.begin(), I);
7560b57cec5SDimitry Andric }
7570b57cec5SDimitry Andric
ParseHeader()7580b57cec5SDimitry Andric bool ObjectFileELF::ParseHeader() {
7590b57cec5SDimitry Andric lldb::offset_t offset = 0;
7600b57cec5SDimitry Andric return m_header.Parse(m_data, &offset);
7610b57cec5SDimitry Andric }
7620b57cec5SDimitry Andric
GetUUID()7630b57cec5SDimitry Andric UUID ObjectFileELF::GetUUID() {
7640b57cec5SDimitry Andric // Need to parse the section list to get the UUIDs, so make sure that's been
7650b57cec5SDimitry Andric // done.
7660b57cec5SDimitry Andric if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile)
7670b57cec5SDimitry Andric return UUID();
7680b57cec5SDimitry Andric
7690b57cec5SDimitry Andric if (!m_uuid) {
7700b57cec5SDimitry Andric using u32le = llvm::support::ulittle32_t;
7710b57cec5SDimitry Andric if (GetType() == ObjectFile::eTypeCoreFile) {
7720b57cec5SDimitry Andric uint32_t core_notes_crc = 0;
7730b57cec5SDimitry Andric
7740b57cec5SDimitry Andric if (!ParseProgramHeaders())
7750b57cec5SDimitry Andric return UUID();
7760b57cec5SDimitry Andric
7770b57cec5SDimitry Andric core_notes_crc =
7780b57cec5SDimitry Andric CalculateELFNotesSegmentsCRC32(m_program_headers, m_data);
7790b57cec5SDimitry Andric
7800b57cec5SDimitry Andric if (core_notes_crc) {
7810b57cec5SDimitry Andric // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it
7820b57cec5SDimitry Andric // look different form .gnu_debuglink crc - followed by 4 bytes of note
7830b57cec5SDimitry Andric // segments crc.
7840b57cec5SDimitry Andric u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};
7850b57cec5SDimitry Andric m_uuid = UUID::fromData(data, sizeof(data));
7860b57cec5SDimitry Andric }
7870b57cec5SDimitry Andric } else {
7880b57cec5SDimitry Andric if (!m_gnu_debuglink_crc)
7899dba64beSDimitry Andric m_gnu_debuglink_crc = calc_crc32(0, m_data);
7900b57cec5SDimitry Andric if (m_gnu_debuglink_crc) {
7910b57cec5SDimitry Andric // Use 4 bytes of crc from the .gnu_debuglink section.
7920b57cec5SDimitry Andric u32le data(m_gnu_debuglink_crc);
7930b57cec5SDimitry Andric m_uuid = UUID::fromData(&data, sizeof(data));
7940b57cec5SDimitry Andric }
7950b57cec5SDimitry Andric }
7960b57cec5SDimitry Andric }
7970b57cec5SDimitry Andric
7980b57cec5SDimitry Andric return m_uuid;
7990b57cec5SDimitry Andric }
8000b57cec5SDimitry Andric
GetDebugLink()8019dba64beSDimitry Andric llvm::Optional<FileSpec> ObjectFileELF::GetDebugLink() {
8029dba64beSDimitry Andric if (m_gnu_debuglink_file.empty())
8039dba64beSDimitry Andric return llvm::None;
8049dba64beSDimitry Andric return FileSpec(m_gnu_debuglink_file);
8050b57cec5SDimitry Andric }
8060b57cec5SDimitry Andric
GetDependentModules(FileSpecList & files)8070b57cec5SDimitry Andric uint32_t ObjectFileELF::GetDependentModules(FileSpecList &files) {
8080b57cec5SDimitry Andric size_t num_modules = ParseDependentModules();
8090b57cec5SDimitry Andric uint32_t num_specs = 0;
8100b57cec5SDimitry Andric
8110b57cec5SDimitry Andric for (unsigned i = 0; i < num_modules; ++i) {
8120b57cec5SDimitry Andric if (files.AppendIfUnique(m_filespec_up->GetFileSpecAtIndex(i)))
8130b57cec5SDimitry Andric num_specs++;
8140b57cec5SDimitry Andric }
8150b57cec5SDimitry Andric
8160b57cec5SDimitry Andric return num_specs;
8170b57cec5SDimitry Andric }
8180b57cec5SDimitry Andric
GetImageInfoAddress(Target * target)8190b57cec5SDimitry Andric Address ObjectFileELF::GetImageInfoAddress(Target *target) {
8200b57cec5SDimitry Andric if (!ParseDynamicSymbols())
8210b57cec5SDimitry Andric return Address();
8220b57cec5SDimitry Andric
8230b57cec5SDimitry Andric SectionList *section_list = GetSectionList();
8240b57cec5SDimitry Andric if (!section_list)
8250b57cec5SDimitry Andric return Address();
8260b57cec5SDimitry Andric
8270b57cec5SDimitry Andric // Find the SHT_DYNAMIC (.dynamic) section.
8280b57cec5SDimitry Andric SectionSP dynsym_section_sp(
8290b57cec5SDimitry Andric section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true));
8300b57cec5SDimitry Andric if (!dynsym_section_sp)
8310b57cec5SDimitry Andric return Address();
8320b57cec5SDimitry Andric assert(dynsym_section_sp->GetObjectFile() == this);
8330b57cec5SDimitry Andric
8340b57cec5SDimitry Andric user_id_t dynsym_id = dynsym_section_sp->GetID();
8350b57cec5SDimitry Andric const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
8360b57cec5SDimitry Andric if (!dynsym_hdr)
8370b57cec5SDimitry Andric return Address();
8380b57cec5SDimitry Andric
8390b57cec5SDimitry Andric for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) {
8400b57cec5SDimitry Andric ELFDynamic &symbol = m_dynamic_symbols[i];
8410b57cec5SDimitry Andric
8420b57cec5SDimitry Andric if (symbol.d_tag == DT_DEBUG) {
8430b57cec5SDimitry Andric // Compute the offset as the number of previous entries plus the size of
8440b57cec5SDimitry Andric // d_tag.
8450b57cec5SDimitry Andric addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
8460b57cec5SDimitry Andric return Address(dynsym_section_sp, offset);
8470b57cec5SDimitry Andric }
8480b57cec5SDimitry Andric // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP
8490b57cec5SDimitry Andric // exists in non-PIE.
8500b57cec5SDimitry Andric else if ((symbol.d_tag == DT_MIPS_RLD_MAP ||
8510b57cec5SDimitry Andric symbol.d_tag == DT_MIPS_RLD_MAP_REL) &&
8520b57cec5SDimitry Andric target) {
8530b57cec5SDimitry Andric addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
8540b57cec5SDimitry Andric addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);
8550b57cec5SDimitry Andric if (dyn_base == LLDB_INVALID_ADDRESS)
8560b57cec5SDimitry Andric return Address();
8570b57cec5SDimitry Andric
8580b57cec5SDimitry Andric Status error;
8590b57cec5SDimitry Andric if (symbol.d_tag == DT_MIPS_RLD_MAP) {
8600b57cec5SDimitry Andric // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer.
8610b57cec5SDimitry Andric Address addr;
862*5f7ddb14SDimitry Andric if (target->ReadPointerFromMemory(dyn_base + offset, error, addr, true))
8630b57cec5SDimitry Andric return addr;
8640b57cec5SDimitry Andric }
8650b57cec5SDimitry Andric if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) {
8660b57cec5SDimitry Andric // DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
8670b57cec5SDimitry Andric // relative to the address of the tag.
8680b57cec5SDimitry Andric uint64_t rel_offset;
8690b57cec5SDimitry Andric rel_offset = target->ReadUnsignedIntegerFromMemory(
870*5f7ddb14SDimitry Andric dyn_base + offset, GetAddressByteSize(), UINT64_MAX, error, true);
8710b57cec5SDimitry Andric if (error.Success() && rel_offset != UINT64_MAX) {
8720b57cec5SDimitry Andric Address addr;
8730b57cec5SDimitry Andric addr_t debug_ptr_address =
8740b57cec5SDimitry Andric dyn_base + (offset - GetAddressByteSize()) + rel_offset;
8750b57cec5SDimitry Andric addr.SetOffset(debug_ptr_address);
8760b57cec5SDimitry Andric return addr;
8770b57cec5SDimitry Andric }
8780b57cec5SDimitry Andric }
8790b57cec5SDimitry Andric }
8800b57cec5SDimitry Andric }
8810b57cec5SDimitry Andric
8820b57cec5SDimitry Andric return Address();
8830b57cec5SDimitry Andric }
8840b57cec5SDimitry Andric
GetEntryPointAddress()8850b57cec5SDimitry Andric lldb_private::Address ObjectFileELF::GetEntryPointAddress() {
8860b57cec5SDimitry Andric if (m_entry_point_address.IsValid())
8870b57cec5SDimitry Andric return m_entry_point_address;
8880b57cec5SDimitry Andric
8890b57cec5SDimitry Andric if (!ParseHeader() || !IsExecutable())
8900b57cec5SDimitry Andric return m_entry_point_address;
8910b57cec5SDimitry Andric
8920b57cec5SDimitry Andric SectionList *section_list = GetSectionList();
8930b57cec5SDimitry Andric addr_t offset = m_header.e_entry;
8940b57cec5SDimitry Andric
8950b57cec5SDimitry Andric if (!section_list)
8960b57cec5SDimitry Andric m_entry_point_address.SetOffset(offset);
8970b57cec5SDimitry Andric else
8980b57cec5SDimitry Andric m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
8990b57cec5SDimitry Andric return m_entry_point_address;
9000b57cec5SDimitry Andric }
9010b57cec5SDimitry Andric
GetBaseAddress()9020b57cec5SDimitry Andric Address ObjectFileELF::GetBaseAddress() {
9030b57cec5SDimitry Andric for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {
9040b57cec5SDimitry Andric const ELFProgramHeader &H = EnumPHdr.value();
9050b57cec5SDimitry Andric if (H.p_type != PT_LOAD)
9060b57cec5SDimitry Andric continue;
9070b57cec5SDimitry Andric
9080b57cec5SDimitry Andric return Address(
9090b57cec5SDimitry Andric GetSectionList()->FindSectionByID(SegmentID(EnumPHdr.index())), 0);
9100b57cec5SDimitry Andric }
9110b57cec5SDimitry Andric return LLDB_INVALID_ADDRESS;
9120b57cec5SDimitry Andric }
9130b57cec5SDimitry Andric
9140b57cec5SDimitry Andric // ParseDependentModules
ParseDependentModules()9150b57cec5SDimitry Andric size_t ObjectFileELF::ParseDependentModules() {
9160b57cec5SDimitry Andric if (m_filespec_up)
9170b57cec5SDimitry Andric return m_filespec_up->GetSize();
9180b57cec5SDimitry Andric
9195ffd83dbSDimitry Andric m_filespec_up = std::make_unique<FileSpecList>();
9200b57cec5SDimitry Andric
9210b57cec5SDimitry Andric if (!ParseSectionHeaders())
9220b57cec5SDimitry Andric return 0;
9230b57cec5SDimitry Andric
9240b57cec5SDimitry Andric SectionList *section_list = GetSectionList();
9250b57cec5SDimitry Andric if (!section_list)
9260b57cec5SDimitry Andric return 0;
9270b57cec5SDimitry Andric
9280b57cec5SDimitry Andric // Find the SHT_DYNAMIC section.
9290b57cec5SDimitry Andric Section *dynsym =
9300b57cec5SDimitry Andric section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)
9310b57cec5SDimitry Andric .get();
9320b57cec5SDimitry Andric if (!dynsym)
9330b57cec5SDimitry Andric return 0;
9340b57cec5SDimitry Andric assert(dynsym->GetObjectFile() == this);
9350b57cec5SDimitry Andric
9360b57cec5SDimitry Andric const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex(dynsym->GetID());
9370b57cec5SDimitry Andric if (!header)
9380b57cec5SDimitry Andric return 0;
9390b57cec5SDimitry Andric // sh_link: section header index of string table used by entries in the
9400b57cec5SDimitry Andric // section.
9410b57cec5SDimitry Andric Section *dynstr = section_list->FindSectionByID(header->sh_link).get();
9420b57cec5SDimitry Andric if (!dynstr)
9430b57cec5SDimitry Andric return 0;
9440b57cec5SDimitry Andric
9450b57cec5SDimitry Andric DataExtractor dynsym_data;
9460b57cec5SDimitry Andric DataExtractor dynstr_data;
9470b57cec5SDimitry Andric if (ReadSectionData(dynsym, dynsym_data) &&
9480b57cec5SDimitry Andric ReadSectionData(dynstr, dynstr_data)) {
9490b57cec5SDimitry Andric ELFDynamic symbol;
9500b57cec5SDimitry Andric const lldb::offset_t section_size = dynsym_data.GetByteSize();
9510b57cec5SDimitry Andric lldb::offset_t offset = 0;
9520b57cec5SDimitry Andric
9530b57cec5SDimitry Andric // The only type of entries we are concerned with are tagged DT_NEEDED,
9540b57cec5SDimitry Andric // yielding the name of a required library.
9550b57cec5SDimitry Andric while (offset < section_size) {
9560b57cec5SDimitry Andric if (!symbol.Parse(dynsym_data, &offset))
9570b57cec5SDimitry Andric break;
9580b57cec5SDimitry Andric
9590b57cec5SDimitry Andric if (symbol.d_tag != DT_NEEDED)
9600b57cec5SDimitry Andric continue;
9610b57cec5SDimitry Andric
9620b57cec5SDimitry Andric uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
9630b57cec5SDimitry Andric const char *lib_name = dynstr_data.PeekCStr(str_index);
9640b57cec5SDimitry Andric FileSpec file_spec(lib_name);
9650b57cec5SDimitry Andric FileSystem::Instance().Resolve(file_spec);
9660b57cec5SDimitry Andric m_filespec_up->Append(file_spec);
9670b57cec5SDimitry Andric }
9680b57cec5SDimitry Andric }
9690b57cec5SDimitry Andric
9700b57cec5SDimitry Andric return m_filespec_up->GetSize();
9710b57cec5SDimitry Andric }
9720b57cec5SDimitry Andric
9730b57cec5SDimitry Andric // GetProgramHeaderInfo
GetProgramHeaderInfo(ProgramHeaderColl & program_headers,DataExtractor & object_data,const ELFHeader & header)9740b57cec5SDimitry Andric size_t ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
9750b57cec5SDimitry Andric DataExtractor &object_data,
9760b57cec5SDimitry Andric const ELFHeader &header) {
9770b57cec5SDimitry Andric // We have already parsed the program headers
9780b57cec5SDimitry Andric if (!program_headers.empty())
9790b57cec5SDimitry Andric return program_headers.size();
9800b57cec5SDimitry Andric
9810b57cec5SDimitry Andric // If there are no program headers to read we are done.
9820b57cec5SDimitry Andric if (header.e_phnum == 0)
9830b57cec5SDimitry Andric return 0;
9840b57cec5SDimitry Andric
9850b57cec5SDimitry Andric program_headers.resize(header.e_phnum);
9860b57cec5SDimitry Andric if (program_headers.size() != header.e_phnum)
9870b57cec5SDimitry Andric return 0;
9880b57cec5SDimitry Andric
9890b57cec5SDimitry Andric const size_t ph_size = header.e_phnum * header.e_phentsize;
9900b57cec5SDimitry Andric const elf_off ph_offset = header.e_phoff;
9910b57cec5SDimitry Andric DataExtractor data;
9920b57cec5SDimitry Andric if (data.SetData(object_data, ph_offset, ph_size) != ph_size)
9930b57cec5SDimitry Andric return 0;
9940b57cec5SDimitry Andric
9950b57cec5SDimitry Andric uint32_t idx;
9960b57cec5SDimitry Andric lldb::offset_t offset;
9970b57cec5SDimitry Andric for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) {
9980b57cec5SDimitry Andric if (!program_headers[idx].Parse(data, &offset))
9990b57cec5SDimitry Andric break;
10000b57cec5SDimitry Andric }
10010b57cec5SDimitry Andric
10020b57cec5SDimitry Andric if (idx < program_headers.size())
10030b57cec5SDimitry Andric program_headers.resize(idx);
10040b57cec5SDimitry Andric
10050b57cec5SDimitry Andric return program_headers.size();
10060b57cec5SDimitry Andric }
10070b57cec5SDimitry Andric
10080b57cec5SDimitry Andric // ParseProgramHeaders
ParseProgramHeaders()10090b57cec5SDimitry Andric bool ObjectFileELF::ParseProgramHeaders() {
10100b57cec5SDimitry Andric return GetProgramHeaderInfo(m_program_headers, m_data, m_header) != 0;
10110b57cec5SDimitry Andric }
10120b57cec5SDimitry Andric
10130b57cec5SDimitry Andric lldb_private::Status
RefineModuleDetailsFromNote(lldb_private::DataExtractor & data,lldb_private::ArchSpec & arch_spec,lldb_private::UUID & uuid)10140b57cec5SDimitry Andric ObjectFileELF::RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,
10150b57cec5SDimitry Andric lldb_private::ArchSpec &arch_spec,
10160b57cec5SDimitry Andric lldb_private::UUID &uuid) {
10170b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
10180b57cec5SDimitry Andric Status error;
10190b57cec5SDimitry Andric
10200b57cec5SDimitry Andric lldb::offset_t offset = 0;
10210b57cec5SDimitry Andric
10220b57cec5SDimitry Andric while (true) {
10230b57cec5SDimitry Andric // Parse the note header. If this fails, bail out.
10240b57cec5SDimitry Andric const lldb::offset_t note_offset = offset;
10250b57cec5SDimitry Andric ELFNote note = ELFNote();
10260b57cec5SDimitry Andric if (!note.Parse(data, &offset)) {
10270b57cec5SDimitry Andric // We're done.
10280b57cec5SDimitry Andric return error;
10290b57cec5SDimitry Andric }
10300b57cec5SDimitry Andric
10319dba64beSDimitry Andric LLDB_LOGF(log, "ObjectFileELF::%s parsing note name='%s', type=%" PRIu32,
10320b57cec5SDimitry Andric __FUNCTION__, note.n_name.c_str(), note.n_type);
10330b57cec5SDimitry Andric
10340b57cec5SDimitry Andric // Process FreeBSD ELF notes.
10350b57cec5SDimitry Andric if ((note.n_name == LLDB_NT_OWNER_FREEBSD) &&
10360b57cec5SDimitry Andric (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) &&
10370b57cec5SDimitry Andric (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) {
10380b57cec5SDimitry Andric // Pull out the min version info.
10390b57cec5SDimitry Andric uint32_t version_info;
10400b57cec5SDimitry Andric if (data.GetU32(&offset, &version_info, 1) == nullptr) {
10410b57cec5SDimitry Andric error.SetErrorString("failed to read FreeBSD ABI note payload");
10420b57cec5SDimitry Andric return error;
10430b57cec5SDimitry Andric }
10440b57cec5SDimitry Andric
10450b57cec5SDimitry Andric // Convert the version info into a major/minor number.
10460b57cec5SDimitry Andric const uint32_t version_major = version_info / 100000;
10470b57cec5SDimitry Andric const uint32_t version_minor = (version_info / 1000) % 100;
10480b57cec5SDimitry Andric
10490b57cec5SDimitry Andric char os_name[32];
10500b57cec5SDimitry Andric snprintf(os_name, sizeof(os_name), "freebsd%" PRIu32 ".%" PRIu32,
10510b57cec5SDimitry Andric version_major, version_minor);
10520b57cec5SDimitry Andric
10530b57cec5SDimitry Andric // Set the elf OS version to FreeBSD. Also clear the vendor.
10540b57cec5SDimitry Andric arch_spec.GetTriple().setOSName(os_name);
10550b57cec5SDimitry Andric arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
10560b57cec5SDimitry Andric
10579dba64beSDimitry Andric LLDB_LOGF(log,
10589dba64beSDimitry Andric "ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32
10590b57cec5SDimitry Andric ".%" PRIu32,
10600b57cec5SDimitry Andric __FUNCTION__, version_major, version_minor,
10610b57cec5SDimitry Andric static_cast<uint32_t>(version_info % 1000));
10620b57cec5SDimitry Andric }
10630b57cec5SDimitry Andric // Process GNU ELF notes.
10640b57cec5SDimitry Andric else if (note.n_name == LLDB_NT_OWNER_GNU) {
10650b57cec5SDimitry Andric switch (note.n_type) {
10660b57cec5SDimitry Andric case LLDB_NT_GNU_ABI_TAG:
10670b57cec5SDimitry Andric if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) {
10680b57cec5SDimitry Andric // Pull out the min OS version supporting the ABI.
10690b57cec5SDimitry Andric uint32_t version_info[4];
10700b57cec5SDimitry Andric if (data.GetU32(&offset, &version_info[0], note.n_descsz / 4) ==
10710b57cec5SDimitry Andric nullptr) {
10720b57cec5SDimitry Andric error.SetErrorString("failed to read GNU ABI note payload");
10730b57cec5SDimitry Andric return error;
10740b57cec5SDimitry Andric }
10750b57cec5SDimitry Andric
10760b57cec5SDimitry Andric // Set the OS per the OS field.
10770b57cec5SDimitry Andric switch (version_info[0]) {
10780b57cec5SDimitry Andric case LLDB_NT_GNU_ABI_OS_LINUX:
10790b57cec5SDimitry Andric arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
10800b57cec5SDimitry Andric arch_spec.GetTriple().setVendor(
10810b57cec5SDimitry Andric llvm::Triple::VendorType::UnknownVendor);
10829dba64beSDimitry Andric LLDB_LOGF(log,
10830b57cec5SDimitry Andric "ObjectFileELF::%s detected Linux, min version %" PRIu32
10840b57cec5SDimitry Andric ".%" PRIu32 ".%" PRIu32,
10850b57cec5SDimitry Andric __FUNCTION__, version_info[1], version_info[2],
10860b57cec5SDimitry Andric version_info[3]);
10870b57cec5SDimitry Andric // FIXME we have the minimal version number, we could be propagating
10880b57cec5SDimitry Andric // that. version_info[1] = OS Major, version_info[2] = OS Minor,
10890b57cec5SDimitry Andric // version_info[3] = Revision.
10900b57cec5SDimitry Andric break;
10910b57cec5SDimitry Andric case LLDB_NT_GNU_ABI_OS_HURD:
10920b57cec5SDimitry Andric arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS);
10930b57cec5SDimitry Andric arch_spec.GetTriple().setVendor(
10940b57cec5SDimitry Andric llvm::Triple::VendorType::UnknownVendor);
10959dba64beSDimitry Andric LLDB_LOGF(log,
10969dba64beSDimitry Andric "ObjectFileELF::%s detected Hurd (unsupported), min "
10970b57cec5SDimitry Andric "version %" PRIu32 ".%" PRIu32 ".%" PRIu32,
10980b57cec5SDimitry Andric __FUNCTION__, version_info[1], version_info[2],
10990b57cec5SDimitry Andric version_info[3]);
11000b57cec5SDimitry Andric break;
11010b57cec5SDimitry Andric case LLDB_NT_GNU_ABI_OS_SOLARIS:
11020b57cec5SDimitry Andric arch_spec.GetTriple().setOS(llvm::Triple::OSType::Solaris);
11030b57cec5SDimitry Andric arch_spec.GetTriple().setVendor(
11040b57cec5SDimitry Andric llvm::Triple::VendorType::UnknownVendor);
11059dba64beSDimitry Andric LLDB_LOGF(log,
11060b57cec5SDimitry Andric "ObjectFileELF::%s detected Solaris, min version %" PRIu32
11070b57cec5SDimitry Andric ".%" PRIu32 ".%" PRIu32,
11080b57cec5SDimitry Andric __FUNCTION__, version_info[1], version_info[2],
11090b57cec5SDimitry Andric version_info[3]);
11100b57cec5SDimitry Andric break;
11110b57cec5SDimitry Andric default:
11129dba64beSDimitry Andric LLDB_LOGF(log,
11130b57cec5SDimitry Andric "ObjectFileELF::%s unrecognized OS in note, id %" PRIu32
11140b57cec5SDimitry Andric ", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32,
11150b57cec5SDimitry Andric __FUNCTION__, version_info[0], version_info[1],
11160b57cec5SDimitry Andric version_info[2], version_info[3]);
11170b57cec5SDimitry Andric break;
11180b57cec5SDimitry Andric }
11190b57cec5SDimitry Andric }
11200b57cec5SDimitry Andric break;
11210b57cec5SDimitry Andric
11220b57cec5SDimitry Andric case LLDB_NT_GNU_BUILD_ID_TAG:
11230b57cec5SDimitry Andric // Only bother processing this if we don't already have the uuid set.
11240b57cec5SDimitry Andric if (!uuid.IsValid()) {
11250b57cec5SDimitry Andric // 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a
11260b57cec5SDimitry Andric // build-id of a different length. Accept it as long as it's at least
11270b57cec5SDimitry Andric // 4 bytes as it will be better than our own crc32.
11280b57cec5SDimitry Andric if (note.n_descsz >= 4) {
11290b57cec5SDimitry Andric if (const uint8_t *buf = data.PeekData(offset, note.n_descsz)) {
11300b57cec5SDimitry Andric // Save the build id as the UUID for the module.
11310b57cec5SDimitry Andric uuid = UUID::fromData(buf, note.n_descsz);
11320b57cec5SDimitry Andric } else {
11330b57cec5SDimitry Andric error.SetErrorString("failed to read GNU_BUILD_ID note payload");
11340b57cec5SDimitry Andric return error;
11350b57cec5SDimitry Andric }
11360b57cec5SDimitry Andric }
11370b57cec5SDimitry Andric }
11380b57cec5SDimitry Andric break;
11390b57cec5SDimitry Andric }
11400b57cec5SDimitry Andric if (arch_spec.IsMIPS() &&
11410b57cec5SDimitry Andric arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS)
11420b57cec5SDimitry Andric // The note.n_name == LLDB_NT_OWNER_GNU is valid for Linux platform
11430b57cec5SDimitry Andric arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
11440b57cec5SDimitry Andric }
11450b57cec5SDimitry Andric // Process NetBSD ELF executables and shared libraries
11460b57cec5SDimitry Andric else if ((note.n_name == LLDB_NT_OWNER_NETBSD) &&
11470b57cec5SDimitry Andric (note.n_type == LLDB_NT_NETBSD_IDENT_TAG) &&
11480b57cec5SDimitry Andric (note.n_descsz == LLDB_NT_NETBSD_IDENT_DESCSZ) &&
11490b57cec5SDimitry Andric (note.n_namesz == LLDB_NT_NETBSD_IDENT_NAMESZ)) {
11500b57cec5SDimitry Andric // Pull out the version info.
11510b57cec5SDimitry Andric uint32_t version_info;
11520b57cec5SDimitry Andric if (data.GetU32(&offset, &version_info, 1) == nullptr) {
11530b57cec5SDimitry Andric error.SetErrorString("failed to read NetBSD ABI note payload");
11540b57cec5SDimitry Andric return error;
11550b57cec5SDimitry Andric }
11560b57cec5SDimitry Andric // Convert the version info into a major/minor/patch number.
11570b57cec5SDimitry Andric // #define __NetBSD_Version__ MMmmrrpp00
11580b57cec5SDimitry Andric //
11590b57cec5SDimitry Andric // M = major version
11600b57cec5SDimitry Andric // m = minor version; a minor number of 99 indicates current.
11610b57cec5SDimitry Andric // r = 0 (since NetBSD 3.0 not used)
11620b57cec5SDimitry Andric // p = patchlevel
11630b57cec5SDimitry Andric const uint32_t version_major = version_info / 100000000;
11640b57cec5SDimitry Andric const uint32_t version_minor = (version_info % 100000000) / 1000000;
11650b57cec5SDimitry Andric const uint32_t version_patch = (version_info % 10000) / 100;
11660b57cec5SDimitry Andric // Set the elf OS version to NetBSD. Also clear the vendor.
11670b57cec5SDimitry Andric arch_spec.GetTriple().setOSName(
11680b57cec5SDimitry Andric llvm::formatv("netbsd{0}.{1}.{2}", version_major, version_minor,
11690b57cec5SDimitry Andric version_patch).str());
11700b57cec5SDimitry Andric arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
11710b57cec5SDimitry Andric }
11720b57cec5SDimitry Andric // Process NetBSD ELF core(5) notes
11730b57cec5SDimitry Andric else if ((note.n_name == LLDB_NT_OWNER_NETBSDCORE) &&
11740b57cec5SDimitry Andric (note.n_type == LLDB_NT_NETBSD_PROCINFO)) {
11750b57cec5SDimitry Andric // Set the elf OS version to NetBSD. Also clear the vendor.
11760b57cec5SDimitry Andric arch_spec.GetTriple().setOS(llvm::Triple::OSType::NetBSD);
11770b57cec5SDimitry Andric arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
11780b57cec5SDimitry Andric }
11790b57cec5SDimitry Andric // Process OpenBSD ELF notes.
11800b57cec5SDimitry Andric else if (note.n_name == LLDB_NT_OWNER_OPENBSD) {
11810b57cec5SDimitry Andric // Set the elf OS version to OpenBSD. Also clear the vendor.
11820b57cec5SDimitry Andric arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD);
11830b57cec5SDimitry Andric arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
11840b57cec5SDimitry Andric } else if (note.n_name == LLDB_NT_OWNER_ANDROID) {
11850b57cec5SDimitry Andric arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
11860b57cec5SDimitry Andric arch_spec.GetTriple().setEnvironment(
11870b57cec5SDimitry Andric llvm::Triple::EnvironmentType::Android);
11880b57cec5SDimitry Andric } else if (note.n_name == LLDB_NT_OWNER_LINUX) {
11890b57cec5SDimitry Andric // This is sometimes found in core files and usually contains extended
11900b57cec5SDimitry Andric // register info
11910b57cec5SDimitry Andric arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
11920b57cec5SDimitry Andric } else if (note.n_name == LLDB_NT_OWNER_CORE) {
11930b57cec5SDimitry Andric // Parse the NT_FILE to look for stuff in paths to shared libraries As
11940b57cec5SDimitry Andric // the contents look like this in a 64 bit ELF core file: count =
11950b57cec5SDimitry Andric // 0x000000000000000a (10) page_size = 0x0000000000001000 (4096) Index
11960b57cec5SDimitry Andric // start end file_ofs path =====
11970b57cec5SDimitry Andric // 0x0000000000401000 0x0000000000000000 /tmp/a.out [ 1]
11980b57cec5SDimitry Andric // 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out [
11990b57cec5SDimitry Andric // 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out
12000b57cec5SDimitry Andric // [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000
12010b57cec5SDimitry Andric // /lib/x86_64-linux-gnu/libc-2.19.so [ 4] 0x00007fa79cba8000
12020b57cec5SDimitry Andric // 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux-
12030b57cec5SDimitry Andric // gnu/libc-2.19.so [ 5] 0x00007fa79cda7000 0x00007fa79cdab000
12040b57cec5SDimitry Andric // 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so [ 6]
12050b57cec5SDimitry Andric // 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64
12060b57cec5SDimitry Andric // -linux-gnu/libc-2.19.so [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000
12070b57cec5SDimitry Andric // 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so [ 8]
12080b57cec5SDimitry Andric // 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64
12090b57cec5SDimitry Andric // -linux-gnu/ld-2.19.so [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000
12100b57cec5SDimitry Andric // 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so In the 32 bit ELFs
12110b57cec5SDimitry Andric // the count, page_size, start, end, file_ofs are uint32_t For reference:
12120b57cec5SDimitry Andric // see readelf source code (in binutils).
12130b57cec5SDimitry Andric if (note.n_type == NT_FILE) {
12140b57cec5SDimitry Andric uint64_t count = data.GetAddress(&offset);
12150b57cec5SDimitry Andric const char *cstr;
12160b57cec5SDimitry Andric data.GetAddress(&offset); // Skip page size
12170b57cec5SDimitry Andric offset += count * 3 *
12180b57cec5SDimitry Andric data.GetAddressByteSize(); // Skip all start/end/file_ofs
12190b57cec5SDimitry Andric for (size_t i = 0; i < count; ++i) {
12200b57cec5SDimitry Andric cstr = data.GetCStr(&offset);
12210b57cec5SDimitry Andric if (cstr == nullptr) {
12220b57cec5SDimitry Andric error.SetErrorStringWithFormat("ObjectFileELF::%s trying to read "
12230b57cec5SDimitry Andric "at an offset after the end "
12240b57cec5SDimitry Andric "(GetCStr returned nullptr)",
12250b57cec5SDimitry Andric __FUNCTION__);
12260b57cec5SDimitry Andric return error;
12270b57cec5SDimitry Andric }
12280b57cec5SDimitry Andric llvm::StringRef path(cstr);
12290b57cec5SDimitry Andric if (path.contains("/lib/x86_64-linux-gnu") || path.contains("/lib/i386-linux-gnu")) {
12300b57cec5SDimitry Andric arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
12310b57cec5SDimitry Andric break;
12320b57cec5SDimitry Andric }
12330b57cec5SDimitry Andric }
12340b57cec5SDimitry Andric if (arch_spec.IsMIPS() &&
12350b57cec5SDimitry Andric arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS)
12360b57cec5SDimitry Andric // In case of MIPSR6, the LLDB_NT_OWNER_GNU note is missing for some
12370b57cec5SDimitry Andric // cases (e.g. compile with -nostdlib) Hence set OS to Linux
12380b57cec5SDimitry Andric arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
12390b57cec5SDimitry Andric }
12400b57cec5SDimitry Andric }
12410b57cec5SDimitry Andric
12420b57cec5SDimitry Andric // Calculate the offset of the next note just in case "offset" has been
12430b57cec5SDimitry Andric // used to poke at the contents of the note data
12440b57cec5SDimitry Andric offset = note_offset + note.GetByteSize();
12450b57cec5SDimitry Andric }
12460b57cec5SDimitry Andric
12470b57cec5SDimitry Andric return error;
12480b57cec5SDimitry Andric }
12490b57cec5SDimitry Andric
ParseARMAttributes(DataExtractor & data,uint64_t length,ArchSpec & arch_spec)12500b57cec5SDimitry Andric void ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length,
12510b57cec5SDimitry Andric ArchSpec &arch_spec) {
12520b57cec5SDimitry Andric lldb::offset_t Offset = 0;
12530b57cec5SDimitry Andric
12540b57cec5SDimitry Andric uint8_t FormatVersion = data.GetU8(&Offset);
12555ffd83dbSDimitry Andric if (FormatVersion != llvm::ELFAttrs::Format_Version)
12560b57cec5SDimitry Andric return;
12570b57cec5SDimitry Andric
12580b57cec5SDimitry Andric Offset = Offset + sizeof(uint32_t); // Section Length
12590b57cec5SDimitry Andric llvm::StringRef VendorName = data.GetCStr(&Offset);
12600b57cec5SDimitry Andric
12610b57cec5SDimitry Andric if (VendorName != "aeabi")
12620b57cec5SDimitry Andric return;
12630b57cec5SDimitry Andric
12640b57cec5SDimitry Andric if (arch_spec.GetTriple().getEnvironment() ==
12650b57cec5SDimitry Andric llvm::Triple::UnknownEnvironment)
12660b57cec5SDimitry Andric arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);
12670b57cec5SDimitry Andric
12680b57cec5SDimitry Andric while (Offset < length) {
12690b57cec5SDimitry Andric uint8_t Tag = data.GetU8(&Offset);
12700b57cec5SDimitry Andric uint32_t Size = data.GetU32(&Offset);
12710b57cec5SDimitry Andric
12720b57cec5SDimitry Andric if (Tag != llvm::ARMBuildAttrs::File || Size == 0)
12730b57cec5SDimitry Andric continue;
12740b57cec5SDimitry Andric
12750b57cec5SDimitry Andric while (Offset < length) {
12760b57cec5SDimitry Andric uint64_t Tag = data.GetULEB128(&Offset);
12770b57cec5SDimitry Andric switch (Tag) {
12780b57cec5SDimitry Andric default:
12790b57cec5SDimitry Andric if (Tag < 32)
12800b57cec5SDimitry Andric data.GetULEB128(&Offset);
12810b57cec5SDimitry Andric else if (Tag % 2 == 0)
12820b57cec5SDimitry Andric data.GetULEB128(&Offset);
12830b57cec5SDimitry Andric else
12840b57cec5SDimitry Andric data.GetCStr(&Offset);
12850b57cec5SDimitry Andric
12860b57cec5SDimitry Andric break;
12870b57cec5SDimitry Andric
12880b57cec5SDimitry Andric case llvm::ARMBuildAttrs::CPU_raw_name:
12890b57cec5SDimitry Andric case llvm::ARMBuildAttrs::CPU_name:
12900b57cec5SDimitry Andric data.GetCStr(&Offset);
12910b57cec5SDimitry Andric
12920b57cec5SDimitry Andric break;
12930b57cec5SDimitry Andric
12940b57cec5SDimitry Andric case llvm::ARMBuildAttrs::ABI_VFP_args: {
12950b57cec5SDimitry Andric uint64_t VFPArgs = data.GetULEB128(&Offset);
12960b57cec5SDimitry Andric
12970b57cec5SDimitry Andric if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS) {
12980b57cec5SDimitry Andric if (arch_spec.GetTriple().getEnvironment() ==
12990b57cec5SDimitry Andric llvm::Triple::UnknownEnvironment ||
13000b57cec5SDimitry Andric arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF)
13010b57cec5SDimitry Andric arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);
13020b57cec5SDimitry Andric
13030b57cec5SDimitry Andric arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);
13040b57cec5SDimitry Andric } else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS) {
13050b57cec5SDimitry Andric if (arch_spec.GetTriple().getEnvironment() ==
13060b57cec5SDimitry Andric llvm::Triple::UnknownEnvironment ||
13070b57cec5SDimitry Andric arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI)
13080b57cec5SDimitry Andric arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF);
13090b57cec5SDimitry Andric
13100b57cec5SDimitry Andric arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);
13110b57cec5SDimitry Andric }
13120b57cec5SDimitry Andric
13130b57cec5SDimitry Andric break;
13140b57cec5SDimitry Andric }
13150b57cec5SDimitry Andric }
13160b57cec5SDimitry Andric }
13170b57cec5SDimitry Andric }
13180b57cec5SDimitry Andric }
13190b57cec5SDimitry Andric
13200b57cec5SDimitry Andric // GetSectionHeaderInfo
GetSectionHeaderInfo(SectionHeaderColl & section_headers,DataExtractor & object_data,const elf::ELFHeader & header,lldb_private::UUID & uuid,std::string & gnu_debuglink_file,uint32_t & gnu_debuglink_crc,ArchSpec & arch_spec)13210b57cec5SDimitry Andric size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers,
13220b57cec5SDimitry Andric DataExtractor &object_data,
13230b57cec5SDimitry Andric const elf::ELFHeader &header,
13240b57cec5SDimitry Andric lldb_private::UUID &uuid,
13250b57cec5SDimitry Andric std::string &gnu_debuglink_file,
13260b57cec5SDimitry Andric uint32_t &gnu_debuglink_crc,
13270b57cec5SDimitry Andric ArchSpec &arch_spec) {
13280b57cec5SDimitry Andric // Don't reparse the section headers if we already did that.
13290b57cec5SDimitry Andric if (!section_headers.empty())
13300b57cec5SDimitry Andric return section_headers.size();
13310b57cec5SDimitry Andric
13320b57cec5SDimitry Andric // Only initialize the arch_spec to okay defaults if they're not already set.
13330b57cec5SDimitry Andric // We'll refine this with note data as we parse the notes.
13340b57cec5SDimitry Andric if (arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) {
13350b57cec5SDimitry Andric llvm::Triple::OSType ostype;
13360b57cec5SDimitry Andric llvm::Triple::OSType spec_ostype;
13370b57cec5SDimitry Andric const uint32_t sub_type = subTypeFromElfHeader(header);
13380b57cec5SDimitry Andric arch_spec.SetArchitecture(eArchTypeELF, header.e_machine, sub_type,
13390b57cec5SDimitry Andric header.e_ident[EI_OSABI]);
13400b57cec5SDimitry Andric
13410b57cec5SDimitry Andric // Validate if it is ok to remove GetOsFromOSABI. Note, that now the OS is
13420b57cec5SDimitry Andric // determined based on EI_OSABI flag and the info extracted from ELF notes
13430b57cec5SDimitry Andric // (see RefineModuleDetailsFromNote). However in some cases that still
13440b57cec5SDimitry Andric // might be not enough: for example a shared library might not have any
13450b57cec5SDimitry Andric // notes at all and have EI_OSABI flag set to System V, as result the OS
13460b57cec5SDimitry Andric // will be set to UnknownOS.
13470b57cec5SDimitry Andric GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);
13480b57cec5SDimitry Andric spec_ostype = arch_spec.GetTriple().getOS();
13490b57cec5SDimitry Andric assert(spec_ostype == ostype);
13500b57cec5SDimitry Andric UNUSED_IF_ASSERT_DISABLED(spec_ostype);
13510b57cec5SDimitry Andric }
13520b57cec5SDimitry Andric
13530b57cec5SDimitry Andric if (arch_spec.GetMachine() == llvm::Triple::mips ||
13540b57cec5SDimitry Andric arch_spec.GetMachine() == llvm::Triple::mipsel ||
13550b57cec5SDimitry Andric arch_spec.GetMachine() == llvm::Triple::mips64 ||
13560b57cec5SDimitry Andric arch_spec.GetMachine() == llvm::Triple::mips64el) {
13570b57cec5SDimitry Andric switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) {
13580b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_MICROMIPS:
13590b57cec5SDimitry Andric arch_spec.SetFlags(ArchSpec::eMIPSAse_micromips);
13600b57cec5SDimitry Andric break;
13610b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_ASE_M16:
13620b57cec5SDimitry Andric arch_spec.SetFlags(ArchSpec::eMIPSAse_mips16);
13630b57cec5SDimitry Andric break;
13640b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX:
13650b57cec5SDimitry Andric arch_spec.SetFlags(ArchSpec::eMIPSAse_mdmx);
13660b57cec5SDimitry Andric break;
13670b57cec5SDimitry Andric default:
13680b57cec5SDimitry Andric break;
13690b57cec5SDimitry Andric }
13700b57cec5SDimitry Andric }
13710b57cec5SDimitry Andric
13720b57cec5SDimitry Andric if (arch_spec.GetMachine() == llvm::Triple::arm ||
13730b57cec5SDimitry Andric arch_spec.GetMachine() == llvm::Triple::thumb) {
13740b57cec5SDimitry Andric if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT)
13750b57cec5SDimitry Andric arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);
13760b57cec5SDimitry Andric else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT)
13770b57cec5SDimitry Andric arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);
13780b57cec5SDimitry Andric }
13790b57cec5SDimitry Andric
13800b57cec5SDimitry Andric // If there are no section headers we are done.
13810b57cec5SDimitry Andric if (header.e_shnum == 0)
13820b57cec5SDimitry Andric return 0;
13830b57cec5SDimitry Andric
13840b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
13850b57cec5SDimitry Andric
13860b57cec5SDimitry Andric section_headers.resize(header.e_shnum);
13870b57cec5SDimitry Andric if (section_headers.size() != header.e_shnum)
13880b57cec5SDimitry Andric return 0;
13890b57cec5SDimitry Andric
13900b57cec5SDimitry Andric const size_t sh_size = header.e_shnum * header.e_shentsize;
13910b57cec5SDimitry Andric const elf_off sh_offset = header.e_shoff;
13920b57cec5SDimitry Andric DataExtractor sh_data;
13930b57cec5SDimitry Andric if (sh_data.SetData(object_data, sh_offset, sh_size) != sh_size)
13940b57cec5SDimitry Andric return 0;
13950b57cec5SDimitry Andric
13960b57cec5SDimitry Andric uint32_t idx;
13970b57cec5SDimitry Andric lldb::offset_t offset;
13980b57cec5SDimitry Andric for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) {
13990b57cec5SDimitry Andric if (!section_headers[idx].Parse(sh_data, &offset))
14000b57cec5SDimitry Andric break;
14010b57cec5SDimitry Andric }
14020b57cec5SDimitry Andric if (idx < section_headers.size())
14030b57cec5SDimitry Andric section_headers.resize(idx);
14040b57cec5SDimitry Andric
14050b57cec5SDimitry Andric const unsigned strtab_idx = header.e_shstrndx;
14060b57cec5SDimitry Andric if (strtab_idx && strtab_idx < section_headers.size()) {
14070b57cec5SDimitry Andric const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx];
14080b57cec5SDimitry Andric const size_t byte_size = sheader.sh_size;
14090b57cec5SDimitry Andric const Elf64_Off offset = sheader.sh_offset;
14100b57cec5SDimitry Andric lldb_private::DataExtractor shstr_data;
14110b57cec5SDimitry Andric
14120b57cec5SDimitry Andric if (shstr_data.SetData(object_data, offset, byte_size) == byte_size) {
14130b57cec5SDimitry Andric for (SectionHeaderCollIter I = section_headers.begin();
14140b57cec5SDimitry Andric I != section_headers.end(); ++I) {
14150b57cec5SDimitry Andric static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink");
14160b57cec5SDimitry Andric const ELFSectionHeaderInfo &sheader = *I;
14170b57cec5SDimitry Andric const uint64_t section_size =
14180b57cec5SDimitry Andric sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size;
14190b57cec5SDimitry Andric ConstString name(shstr_data.PeekCStr(I->sh_name));
14200b57cec5SDimitry Andric
14210b57cec5SDimitry Andric I->section_name = name;
14220b57cec5SDimitry Andric
14230b57cec5SDimitry Andric if (arch_spec.IsMIPS()) {
14240b57cec5SDimitry Andric uint32_t arch_flags = arch_spec.GetFlags();
14250b57cec5SDimitry Andric DataExtractor data;
14260b57cec5SDimitry Andric if (sheader.sh_type == SHT_MIPS_ABIFLAGS) {
14270b57cec5SDimitry Andric
14280b57cec5SDimitry Andric if (section_size && (data.SetData(object_data, sheader.sh_offset,
14290b57cec5SDimitry Andric section_size) == section_size)) {
14300b57cec5SDimitry Andric // MIPS ASE Mask is at offset 12 in MIPS.abiflags section
14310b57cec5SDimitry Andric lldb::offset_t offset = 12; // MIPS ABI Flags Version: 0
14320b57cec5SDimitry Andric arch_flags |= data.GetU32(&offset);
14330b57cec5SDimitry Andric
14340b57cec5SDimitry Andric // The floating point ABI is at offset 7
14350b57cec5SDimitry Andric offset = 7;
14360b57cec5SDimitry Andric switch (data.GetU8(&offset)) {
14370b57cec5SDimitry Andric case llvm::Mips::Val_GNU_MIPS_ABI_FP_ANY:
14380b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_ANY;
14390b57cec5SDimitry Andric break;
14400b57cec5SDimitry Andric case llvm::Mips::Val_GNU_MIPS_ABI_FP_DOUBLE:
14410b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_DOUBLE;
14420b57cec5SDimitry Andric break;
14430b57cec5SDimitry Andric case llvm::Mips::Val_GNU_MIPS_ABI_FP_SINGLE:
14440b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SINGLE;
14450b57cec5SDimitry Andric break;
14460b57cec5SDimitry Andric case llvm::Mips::Val_GNU_MIPS_ABI_FP_SOFT:
14470b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT;
14480b57cec5SDimitry Andric break;
14490b57cec5SDimitry Andric case llvm::Mips::Val_GNU_MIPS_ABI_FP_OLD_64:
14500b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_OLD_64;
14510b57cec5SDimitry Andric break;
14520b57cec5SDimitry Andric case llvm::Mips::Val_GNU_MIPS_ABI_FP_XX:
14530b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_XX;
14540b57cec5SDimitry Andric break;
14550b57cec5SDimitry Andric case llvm::Mips::Val_GNU_MIPS_ABI_FP_64:
14560b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64;
14570b57cec5SDimitry Andric break;
14580b57cec5SDimitry Andric case llvm::Mips::Val_GNU_MIPS_ABI_FP_64A:
14590b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64A;
14600b57cec5SDimitry Andric break;
14610b57cec5SDimitry Andric }
14620b57cec5SDimitry Andric }
14630b57cec5SDimitry Andric }
14640b57cec5SDimitry Andric // Settings appropriate ArchSpec ABI Flags
14650b57cec5SDimitry Andric switch (header.e_flags & llvm::ELF::EF_MIPS_ABI) {
14660b57cec5SDimitry Andric case llvm::ELF::EF_MIPS_ABI_O32:
14670b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32;
14680b57cec5SDimitry Andric break;
14690b57cec5SDimitry Andric case EF_MIPS_ABI_O64:
14700b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPSABI_O64;
14710b57cec5SDimitry Andric break;
14720b57cec5SDimitry Andric case EF_MIPS_ABI_EABI32:
14730b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI32;
14740b57cec5SDimitry Andric break;
14750b57cec5SDimitry Andric case EF_MIPS_ABI_EABI64:
14760b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI64;
14770b57cec5SDimitry Andric break;
14780b57cec5SDimitry Andric default:
14790b57cec5SDimitry Andric // ABI Mask doesn't cover N32 and N64 ABI.
14800b57cec5SDimitry Andric if (header.e_ident[EI_CLASS] == llvm::ELF::ELFCLASS64)
14810b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPSABI_N64;
14820b57cec5SDimitry Andric else if (header.e_flags & llvm::ELF::EF_MIPS_ABI2)
14830b57cec5SDimitry Andric arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32;
14840b57cec5SDimitry Andric break;
14850b57cec5SDimitry Andric }
14860b57cec5SDimitry Andric arch_spec.SetFlags(arch_flags);
14870b57cec5SDimitry Andric }
14880b57cec5SDimitry Andric
14890b57cec5SDimitry Andric if (arch_spec.GetMachine() == llvm::Triple::arm ||
14900b57cec5SDimitry Andric arch_spec.GetMachine() == llvm::Triple::thumb) {
14910b57cec5SDimitry Andric DataExtractor data;
14920b57cec5SDimitry Andric
14930b57cec5SDimitry Andric if (sheader.sh_type == SHT_ARM_ATTRIBUTES && section_size != 0 &&
14940b57cec5SDimitry Andric data.SetData(object_data, sheader.sh_offset, section_size) == section_size)
14950b57cec5SDimitry Andric ParseARMAttributes(data, section_size, arch_spec);
14960b57cec5SDimitry Andric }
14970b57cec5SDimitry Andric
14980b57cec5SDimitry Andric if (name == g_sect_name_gnu_debuglink) {
14990b57cec5SDimitry Andric DataExtractor data;
15000b57cec5SDimitry Andric if (section_size && (data.SetData(object_data, sheader.sh_offset,
15010b57cec5SDimitry Andric section_size) == section_size)) {
15020b57cec5SDimitry Andric lldb::offset_t gnu_debuglink_offset = 0;
15030b57cec5SDimitry Andric gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset);
15040b57cec5SDimitry Andric gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4);
15050b57cec5SDimitry Andric data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
15060b57cec5SDimitry Andric }
15070b57cec5SDimitry Andric }
15080b57cec5SDimitry Andric
15090b57cec5SDimitry Andric // Process ELF note section entries.
15100b57cec5SDimitry Andric bool is_note_header = (sheader.sh_type == SHT_NOTE);
15110b57cec5SDimitry Andric
15120b57cec5SDimitry Andric // The section header ".note.android.ident" is stored as a
15130b57cec5SDimitry Andric // PROGBITS type header but it is actually a note header.
15140b57cec5SDimitry Andric static ConstString g_sect_name_android_ident(".note.android.ident");
15150b57cec5SDimitry Andric if (!is_note_header && name == g_sect_name_android_ident)
15160b57cec5SDimitry Andric is_note_header = true;
15170b57cec5SDimitry Andric
15180b57cec5SDimitry Andric if (is_note_header) {
15190b57cec5SDimitry Andric // Allow notes to refine module info.
15200b57cec5SDimitry Andric DataExtractor data;
15210b57cec5SDimitry Andric if (section_size && (data.SetData(object_data, sheader.sh_offset,
15220b57cec5SDimitry Andric section_size) == section_size)) {
15230b57cec5SDimitry Andric Status error = RefineModuleDetailsFromNote(data, arch_spec, uuid);
15240b57cec5SDimitry Andric if (error.Fail()) {
15259dba64beSDimitry Andric LLDB_LOGF(log, "ObjectFileELF::%s ELF note processing failed: %s",
15260b57cec5SDimitry Andric __FUNCTION__, error.AsCString());
15270b57cec5SDimitry Andric }
15280b57cec5SDimitry Andric }
15290b57cec5SDimitry Andric }
15300b57cec5SDimitry Andric }
15310b57cec5SDimitry Andric
15320b57cec5SDimitry Andric // Make any unknown triple components to be unspecified unknowns.
15330b57cec5SDimitry Andric if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
15340b57cec5SDimitry Andric arch_spec.GetTriple().setVendorName(llvm::StringRef());
15350b57cec5SDimitry Andric if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS)
15360b57cec5SDimitry Andric arch_spec.GetTriple().setOSName(llvm::StringRef());
15370b57cec5SDimitry Andric
15380b57cec5SDimitry Andric return section_headers.size();
15390b57cec5SDimitry Andric }
15400b57cec5SDimitry Andric }
15410b57cec5SDimitry Andric
15420b57cec5SDimitry Andric section_headers.clear();
15430b57cec5SDimitry Andric return 0;
15440b57cec5SDimitry Andric }
15450b57cec5SDimitry Andric
15460b57cec5SDimitry Andric llvm::StringRef
StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const15470b57cec5SDimitry Andric ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const {
15480b57cec5SDimitry Andric size_t pos = symbol_name.find('@');
15490b57cec5SDimitry Andric return symbol_name.substr(0, pos);
15500b57cec5SDimitry Andric }
15510b57cec5SDimitry Andric
15520b57cec5SDimitry Andric // ParseSectionHeaders
ParseSectionHeaders()15530b57cec5SDimitry Andric size_t ObjectFileELF::ParseSectionHeaders() {
15540b57cec5SDimitry Andric return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid,
15550b57cec5SDimitry Andric m_gnu_debuglink_file, m_gnu_debuglink_crc,
15560b57cec5SDimitry Andric m_arch_spec);
15570b57cec5SDimitry Andric }
15580b57cec5SDimitry Andric
15590b57cec5SDimitry Andric const ObjectFileELF::ELFSectionHeaderInfo *
GetSectionHeaderByIndex(lldb::user_id_t id)15600b57cec5SDimitry Andric ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) {
15610b57cec5SDimitry Andric if (!ParseSectionHeaders())
15620b57cec5SDimitry Andric return nullptr;
15630b57cec5SDimitry Andric
15640b57cec5SDimitry Andric if (id < m_section_headers.size())
15650b57cec5SDimitry Andric return &m_section_headers[id];
15660b57cec5SDimitry Andric
15670b57cec5SDimitry Andric return nullptr;
15680b57cec5SDimitry Andric }
15690b57cec5SDimitry Andric
GetSectionIndexByName(const char * name)15700b57cec5SDimitry Andric lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) {
15710b57cec5SDimitry Andric if (!name || !name[0] || !ParseSectionHeaders())
15720b57cec5SDimitry Andric return 0;
15730b57cec5SDimitry Andric for (size_t i = 1; i < m_section_headers.size(); ++i)
15740b57cec5SDimitry Andric if (m_section_headers[i].section_name == ConstString(name))
15750b57cec5SDimitry Andric return i;
15760b57cec5SDimitry Andric return 0;
15770b57cec5SDimitry Andric }
15780b57cec5SDimitry Andric
GetSectionTypeFromName(llvm::StringRef Name)15790b57cec5SDimitry Andric static SectionType GetSectionTypeFromName(llvm::StringRef Name) {
15800b57cec5SDimitry Andric if (Name.consume_front(".debug_") || Name.consume_front(".zdebug_")) {
15810b57cec5SDimitry Andric return llvm::StringSwitch<SectionType>(Name)
15820b57cec5SDimitry Andric .Case("abbrev", eSectionTypeDWARFDebugAbbrev)
15830b57cec5SDimitry Andric .Case("abbrev.dwo", eSectionTypeDWARFDebugAbbrevDwo)
15840b57cec5SDimitry Andric .Case("addr", eSectionTypeDWARFDebugAddr)
15850b57cec5SDimitry Andric .Case("aranges", eSectionTypeDWARFDebugAranges)
15860b57cec5SDimitry Andric .Case("cu_index", eSectionTypeDWARFDebugCuIndex)
15870b57cec5SDimitry Andric .Case("frame", eSectionTypeDWARFDebugFrame)
15880b57cec5SDimitry Andric .Case("info", eSectionTypeDWARFDebugInfo)
15890b57cec5SDimitry Andric .Case("info.dwo", eSectionTypeDWARFDebugInfoDwo)
15900b57cec5SDimitry Andric .Cases("line", "line.dwo", eSectionTypeDWARFDebugLine)
15910b57cec5SDimitry Andric .Cases("line_str", "line_str.dwo", eSectionTypeDWARFDebugLineStr)
1592480093f4SDimitry Andric .Case("loc", eSectionTypeDWARFDebugLoc)
1593480093f4SDimitry Andric .Case("loc.dwo", eSectionTypeDWARFDebugLocDwo)
1594480093f4SDimitry Andric .Case("loclists", eSectionTypeDWARFDebugLocLists)
1595480093f4SDimitry Andric .Case("loclists.dwo", eSectionTypeDWARFDebugLocListsDwo)
15960b57cec5SDimitry Andric .Case("macinfo", eSectionTypeDWARFDebugMacInfo)
15970b57cec5SDimitry Andric .Cases("macro", "macro.dwo", eSectionTypeDWARFDebugMacro)
15980b57cec5SDimitry Andric .Case("names", eSectionTypeDWARFDebugNames)
15990b57cec5SDimitry Andric .Case("pubnames", eSectionTypeDWARFDebugPubNames)
16000b57cec5SDimitry Andric .Case("pubtypes", eSectionTypeDWARFDebugPubTypes)
16010b57cec5SDimitry Andric .Case("ranges", eSectionTypeDWARFDebugRanges)
16020b57cec5SDimitry Andric .Case("rnglists", eSectionTypeDWARFDebugRngLists)
1603480093f4SDimitry Andric .Case("rnglists.dwo", eSectionTypeDWARFDebugRngListsDwo)
16040b57cec5SDimitry Andric .Case("str", eSectionTypeDWARFDebugStr)
16050b57cec5SDimitry Andric .Case("str.dwo", eSectionTypeDWARFDebugStrDwo)
16060b57cec5SDimitry Andric .Case("str_offsets", eSectionTypeDWARFDebugStrOffsets)
16070b57cec5SDimitry Andric .Case("str_offsets.dwo", eSectionTypeDWARFDebugStrOffsetsDwo)
16085ffd83dbSDimitry Andric .Case("tu_index", eSectionTypeDWARFDebugTuIndex)
16090b57cec5SDimitry Andric .Case("types", eSectionTypeDWARFDebugTypes)
16100b57cec5SDimitry Andric .Case("types.dwo", eSectionTypeDWARFDebugTypesDwo)
16110b57cec5SDimitry Andric .Default(eSectionTypeOther);
16120b57cec5SDimitry Andric }
16130b57cec5SDimitry Andric return llvm::StringSwitch<SectionType>(Name)
16140b57cec5SDimitry Andric .Case(".ARM.exidx", eSectionTypeARMexidx)
16150b57cec5SDimitry Andric .Case(".ARM.extab", eSectionTypeARMextab)
16160b57cec5SDimitry Andric .Cases(".bss", ".tbss", eSectionTypeZeroFill)
16170b57cec5SDimitry Andric .Cases(".data", ".tdata", eSectionTypeData)
16180b57cec5SDimitry Andric .Case(".eh_frame", eSectionTypeEHFrame)
16190b57cec5SDimitry Andric .Case(".gnu_debugaltlink", eSectionTypeDWARFGNUDebugAltLink)
16200b57cec5SDimitry Andric .Case(".gosymtab", eSectionTypeGoSymtab)
16210b57cec5SDimitry Andric .Case(".text", eSectionTypeCode)
16220b57cec5SDimitry Andric .Default(eSectionTypeOther);
16230b57cec5SDimitry Andric }
16240b57cec5SDimitry Andric
GetSectionType(const ELFSectionHeaderInfo & H) const16250b57cec5SDimitry Andric SectionType ObjectFileELF::GetSectionType(const ELFSectionHeaderInfo &H) const {
16260b57cec5SDimitry Andric switch (H.sh_type) {
16270b57cec5SDimitry Andric case SHT_PROGBITS:
16280b57cec5SDimitry Andric if (H.sh_flags & SHF_EXECINSTR)
16290b57cec5SDimitry Andric return eSectionTypeCode;
16300b57cec5SDimitry Andric break;
16310b57cec5SDimitry Andric case SHT_SYMTAB:
16320b57cec5SDimitry Andric return eSectionTypeELFSymbolTable;
16330b57cec5SDimitry Andric case SHT_DYNSYM:
16340b57cec5SDimitry Andric return eSectionTypeELFDynamicSymbols;
16350b57cec5SDimitry Andric case SHT_RELA:
16360b57cec5SDimitry Andric case SHT_REL:
16370b57cec5SDimitry Andric return eSectionTypeELFRelocationEntries;
16380b57cec5SDimitry Andric case SHT_DYNAMIC:
16390b57cec5SDimitry Andric return eSectionTypeELFDynamicLinkInfo;
16400b57cec5SDimitry Andric }
16410b57cec5SDimitry Andric return GetSectionTypeFromName(H.section_name.GetStringRef());
16420b57cec5SDimitry Andric }
16430b57cec5SDimitry Andric
GetTargetByteSize(SectionType Type,const ArchSpec & arch)16440b57cec5SDimitry Andric static uint32_t GetTargetByteSize(SectionType Type, const ArchSpec &arch) {
16450b57cec5SDimitry Andric switch (Type) {
16460b57cec5SDimitry Andric case eSectionTypeData:
16470b57cec5SDimitry Andric case eSectionTypeZeroFill:
16480b57cec5SDimitry Andric return arch.GetDataByteSize();
16490b57cec5SDimitry Andric case eSectionTypeCode:
16500b57cec5SDimitry Andric return arch.GetCodeByteSize();
16510b57cec5SDimitry Andric default:
16520b57cec5SDimitry Andric return 1;
16530b57cec5SDimitry Andric }
16540b57cec5SDimitry Andric }
16550b57cec5SDimitry Andric
GetPermissions(const ELFSectionHeader & H)16560b57cec5SDimitry Andric static Permissions GetPermissions(const ELFSectionHeader &H) {
16570b57cec5SDimitry Andric Permissions Perm = Permissions(0);
16580b57cec5SDimitry Andric if (H.sh_flags & SHF_ALLOC)
16590b57cec5SDimitry Andric Perm |= ePermissionsReadable;
16600b57cec5SDimitry Andric if (H.sh_flags & SHF_WRITE)
16610b57cec5SDimitry Andric Perm |= ePermissionsWritable;
16620b57cec5SDimitry Andric if (H.sh_flags & SHF_EXECINSTR)
16630b57cec5SDimitry Andric Perm |= ePermissionsExecutable;
16640b57cec5SDimitry Andric return Perm;
16650b57cec5SDimitry Andric }
16660b57cec5SDimitry Andric
GetPermissions(const ELFProgramHeader & H)16670b57cec5SDimitry Andric static Permissions GetPermissions(const ELFProgramHeader &H) {
16680b57cec5SDimitry Andric Permissions Perm = Permissions(0);
16690b57cec5SDimitry Andric if (H.p_flags & PF_R)
16700b57cec5SDimitry Andric Perm |= ePermissionsReadable;
16710b57cec5SDimitry Andric if (H.p_flags & PF_W)
16720b57cec5SDimitry Andric Perm |= ePermissionsWritable;
16730b57cec5SDimitry Andric if (H.p_flags & PF_X)
16740b57cec5SDimitry Andric Perm |= ePermissionsExecutable;
16750b57cec5SDimitry Andric return Perm;
16760b57cec5SDimitry Andric }
16770b57cec5SDimitry Andric
16780b57cec5SDimitry Andric namespace {
16790b57cec5SDimitry Andric
16800b57cec5SDimitry Andric using VMRange = lldb_private::Range<addr_t, addr_t>;
16810b57cec5SDimitry Andric
16820b57cec5SDimitry Andric struct SectionAddressInfo {
16830b57cec5SDimitry Andric SectionSP Segment;
16840b57cec5SDimitry Andric VMRange Range;
16850b57cec5SDimitry Andric };
16860b57cec5SDimitry Andric
16870b57cec5SDimitry Andric // (Unlinked) ELF object files usually have 0 for every section address, meaning
16880b57cec5SDimitry Andric // we need to compute synthetic addresses in order for "file addresses" from
16890b57cec5SDimitry Andric // different sections to not overlap. This class handles that logic.
16900b57cec5SDimitry Andric class VMAddressProvider {
16910b57cec5SDimitry Andric using VMMap = llvm::IntervalMap<addr_t, SectionSP, 4,
16920b57cec5SDimitry Andric llvm::IntervalMapHalfOpenInfo<addr_t>>;
16930b57cec5SDimitry Andric
16940b57cec5SDimitry Andric ObjectFile::Type ObjectType;
16950b57cec5SDimitry Andric addr_t NextVMAddress = 0;
16960b57cec5SDimitry Andric VMMap::Allocator Alloc;
16970b57cec5SDimitry Andric VMMap Segments = VMMap(Alloc);
16980b57cec5SDimitry Andric VMMap Sections = VMMap(Alloc);
16990b57cec5SDimitry Andric lldb_private::Log *Log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES);
17009dba64beSDimitry Andric size_t SegmentCount = 0;
17019dba64beSDimitry Andric std::string SegmentName;
17020b57cec5SDimitry Andric
GetVMRange(const ELFSectionHeader & H)17030b57cec5SDimitry Andric VMRange GetVMRange(const ELFSectionHeader &H) {
17040b57cec5SDimitry Andric addr_t Address = H.sh_addr;
17050b57cec5SDimitry Andric addr_t Size = H.sh_flags & SHF_ALLOC ? H.sh_size : 0;
17060b57cec5SDimitry Andric if (ObjectType == ObjectFile::Type::eTypeObjectFile && Segments.empty() && (H.sh_flags & SHF_ALLOC)) {
17070b57cec5SDimitry Andric NextVMAddress =
17080b57cec5SDimitry Andric llvm::alignTo(NextVMAddress, std::max<addr_t>(H.sh_addralign, 1));
17090b57cec5SDimitry Andric Address = NextVMAddress;
17100b57cec5SDimitry Andric NextVMAddress += Size;
17110b57cec5SDimitry Andric }
17120b57cec5SDimitry Andric return VMRange(Address, Size);
17130b57cec5SDimitry Andric }
17140b57cec5SDimitry Andric
17150b57cec5SDimitry Andric public:
VMAddressProvider(ObjectFile::Type Type,llvm::StringRef SegmentName)17169dba64beSDimitry Andric VMAddressProvider(ObjectFile::Type Type, llvm::StringRef SegmentName)
17175ffd83dbSDimitry Andric : ObjectType(Type), SegmentName(std::string(SegmentName)) {}
17189dba64beSDimitry Andric
GetNextSegmentName() const17199dba64beSDimitry Andric std::string GetNextSegmentName() const {
17209dba64beSDimitry Andric return llvm::formatv("{0}[{1}]", SegmentName, SegmentCount).str();
17219dba64beSDimitry Andric }
17220b57cec5SDimitry Andric
GetAddressInfo(const ELFProgramHeader & H)17230b57cec5SDimitry Andric llvm::Optional<VMRange> GetAddressInfo(const ELFProgramHeader &H) {
17240b57cec5SDimitry Andric if (H.p_memsz == 0) {
17259dba64beSDimitry Andric LLDB_LOG(Log, "Ignoring zero-sized {0} segment. Corrupt object file?",
17269dba64beSDimitry Andric SegmentName);
17270b57cec5SDimitry Andric return llvm::None;
17280b57cec5SDimitry Andric }
17290b57cec5SDimitry Andric
17300b57cec5SDimitry Andric if (Segments.overlaps(H.p_vaddr, H.p_vaddr + H.p_memsz)) {
17319dba64beSDimitry Andric LLDB_LOG(Log, "Ignoring overlapping {0} segment. Corrupt object file?",
17329dba64beSDimitry Andric SegmentName);
17330b57cec5SDimitry Andric return llvm::None;
17340b57cec5SDimitry Andric }
17350b57cec5SDimitry Andric return VMRange(H.p_vaddr, H.p_memsz);
17360b57cec5SDimitry Andric }
17370b57cec5SDimitry Andric
GetAddressInfo(const ELFSectionHeader & H)17380b57cec5SDimitry Andric llvm::Optional<SectionAddressInfo> GetAddressInfo(const ELFSectionHeader &H) {
17390b57cec5SDimitry Andric VMRange Range = GetVMRange(H);
17400b57cec5SDimitry Andric SectionSP Segment;
17410b57cec5SDimitry Andric auto It = Segments.find(Range.GetRangeBase());
17420b57cec5SDimitry Andric if ((H.sh_flags & SHF_ALLOC) && It.valid()) {
17430b57cec5SDimitry Andric addr_t MaxSize;
17440b57cec5SDimitry Andric if (It.start() <= Range.GetRangeBase()) {
17450b57cec5SDimitry Andric MaxSize = It.stop() - Range.GetRangeBase();
17460b57cec5SDimitry Andric Segment = *It;
17470b57cec5SDimitry Andric } else
17480b57cec5SDimitry Andric MaxSize = It.start() - Range.GetRangeBase();
17490b57cec5SDimitry Andric if (Range.GetByteSize() > MaxSize) {
17500b57cec5SDimitry Andric LLDB_LOG(Log, "Shortening section crossing segment boundaries. "
17510b57cec5SDimitry Andric "Corrupt object file?");
17520b57cec5SDimitry Andric Range.SetByteSize(MaxSize);
17530b57cec5SDimitry Andric }
17540b57cec5SDimitry Andric }
17550b57cec5SDimitry Andric if (Range.GetByteSize() > 0 &&
17560b57cec5SDimitry Andric Sections.overlaps(Range.GetRangeBase(), Range.GetRangeEnd())) {
17570b57cec5SDimitry Andric LLDB_LOG(Log, "Ignoring overlapping section. Corrupt object file?");
17580b57cec5SDimitry Andric return llvm::None;
17590b57cec5SDimitry Andric }
17600b57cec5SDimitry Andric if (Segment)
17610b57cec5SDimitry Andric Range.Slide(-Segment->GetFileAddress());
17620b57cec5SDimitry Andric return SectionAddressInfo{Segment, Range};
17630b57cec5SDimitry Andric }
17640b57cec5SDimitry Andric
AddSegment(const VMRange & Range,SectionSP Seg)17650b57cec5SDimitry Andric void AddSegment(const VMRange &Range, SectionSP Seg) {
17660b57cec5SDimitry Andric Segments.insert(Range.GetRangeBase(), Range.GetRangeEnd(), std::move(Seg));
17679dba64beSDimitry Andric ++SegmentCount;
17680b57cec5SDimitry Andric }
17690b57cec5SDimitry Andric
AddSection(SectionAddressInfo Info,SectionSP Sect)17700b57cec5SDimitry Andric void AddSection(SectionAddressInfo Info, SectionSP Sect) {
17710b57cec5SDimitry Andric if (Info.Range.GetByteSize() == 0)
17720b57cec5SDimitry Andric return;
17730b57cec5SDimitry Andric if (Info.Segment)
17740b57cec5SDimitry Andric Info.Range.Slide(Info.Segment->GetFileAddress());
17750b57cec5SDimitry Andric Sections.insert(Info.Range.GetRangeBase(), Info.Range.GetRangeEnd(),
17760b57cec5SDimitry Andric std::move(Sect));
17770b57cec5SDimitry Andric }
17780b57cec5SDimitry Andric };
17790b57cec5SDimitry Andric }
17800b57cec5SDimitry Andric
CreateSections(SectionList & unified_section_list)17810b57cec5SDimitry Andric void ObjectFileELF::CreateSections(SectionList &unified_section_list) {
17820b57cec5SDimitry Andric if (m_sections_up)
17830b57cec5SDimitry Andric return;
17840b57cec5SDimitry Andric
17859dba64beSDimitry Andric m_sections_up = std::make_unique<SectionList>();
17869dba64beSDimitry Andric VMAddressProvider regular_provider(GetType(), "PT_LOAD");
17879dba64beSDimitry Andric VMAddressProvider tls_provider(GetType(), "PT_TLS");
17880b57cec5SDimitry Andric
17890b57cec5SDimitry Andric for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {
17900b57cec5SDimitry Andric const ELFProgramHeader &PHdr = EnumPHdr.value();
17919dba64beSDimitry Andric if (PHdr.p_type != PT_LOAD && PHdr.p_type != PT_TLS)
17920b57cec5SDimitry Andric continue;
17930b57cec5SDimitry Andric
17949dba64beSDimitry Andric VMAddressProvider &provider =
17959dba64beSDimitry Andric PHdr.p_type == PT_TLS ? tls_provider : regular_provider;
17969dba64beSDimitry Andric auto InfoOr = provider.GetAddressInfo(PHdr);
17970b57cec5SDimitry Andric if (!InfoOr)
17980b57cec5SDimitry Andric continue;
17990b57cec5SDimitry Andric
18000b57cec5SDimitry Andric uint32_t Log2Align = llvm::Log2_64(std::max<elf_xword>(PHdr.p_align, 1));
18010b57cec5SDimitry Andric SectionSP Segment = std::make_shared<Section>(
18029dba64beSDimitry Andric GetModule(), this, SegmentID(EnumPHdr.index()),
18039dba64beSDimitry Andric ConstString(provider.GetNextSegmentName()), eSectionTypeContainer,
18049dba64beSDimitry Andric InfoOr->GetRangeBase(), InfoOr->GetByteSize(), PHdr.p_offset,
18059dba64beSDimitry Andric PHdr.p_filesz, Log2Align, /*flags*/ 0);
18060b57cec5SDimitry Andric Segment->SetPermissions(GetPermissions(PHdr));
18079dba64beSDimitry Andric Segment->SetIsThreadSpecific(PHdr.p_type == PT_TLS);
18080b57cec5SDimitry Andric m_sections_up->AddSection(Segment);
18090b57cec5SDimitry Andric
18109dba64beSDimitry Andric provider.AddSegment(*InfoOr, std::move(Segment));
18110b57cec5SDimitry Andric }
18120b57cec5SDimitry Andric
18130b57cec5SDimitry Andric ParseSectionHeaders();
18140b57cec5SDimitry Andric if (m_section_headers.empty())
18150b57cec5SDimitry Andric return;
18160b57cec5SDimitry Andric
18170b57cec5SDimitry Andric for (SectionHeaderCollIter I = std::next(m_section_headers.begin());
18180b57cec5SDimitry Andric I != m_section_headers.end(); ++I) {
18190b57cec5SDimitry Andric const ELFSectionHeaderInfo &header = *I;
18200b57cec5SDimitry Andric
18210b57cec5SDimitry Andric ConstString &name = I->section_name;
18220b57cec5SDimitry Andric const uint64_t file_size =
18230b57cec5SDimitry Andric header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
18240b57cec5SDimitry Andric
18259dba64beSDimitry Andric VMAddressProvider &provider =
18269dba64beSDimitry Andric header.sh_flags & SHF_TLS ? tls_provider : regular_provider;
18279dba64beSDimitry Andric auto InfoOr = provider.GetAddressInfo(header);
18280b57cec5SDimitry Andric if (!InfoOr)
18290b57cec5SDimitry Andric continue;
18300b57cec5SDimitry Andric
18310b57cec5SDimitry Andric SectionType sect_type = GetSectionType(header);
18320b57cec5SDimitry Andric
18330b57cec5SDimitry Andric const uint32_t target_bytes_size =
18340b57cec5SDimitry Andric GetTargetByteSize(sect_type, m_arch_spec);
18350b57cec5SDimitry Andric
18360b57cec5SDimitry Andric elf::elf_xword log2align =
18370b57cec5SDimitry Andric (header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign);
18380b57cec5SDimitry Andric
18390b57cec5SDimitry Andric SectionSP section_sp(new Section(
18400b57cec5SDimitry Andric InfoOr->Segment, GetModule(), // Module to which this section belongs.
18410b57cec5SDimitry Andric this, // ObjectFile to which this section belongs and should
18420b57cec5SDimitry Andric // read section data from.
18430b57cec5SDimitry Andric SectionIndex(I), // Section ID.
18440b57cec5SDimitry Andric name, // Section name.
18450b57cec5SDimitry Andric sect_type, // Section type.
18460b57cec5SDimitry Andric InfoOr->Range.GetRangeBase(), // VM address.
18470b57cec5SDimitry Andric InfoOr->Range.GetByteSize(), // VM size in bytes of this section.
18480b57cec5SDimitry Andric header.sh_offset, // Offset of this section in the file.
18490b57cec5SDimitry Andric file_size, // Size of the section as found in the file.
18500b57cec5SDimitry Andric log2align, // Alignment of the section
18510b57cec5SDimitry Andric header.sh_flags, // Flags for this section.
18520b57cec5SDimitry Andric target_bytes_size)); // Number of host bytes per target byte
18530b57cec5SDimitry Andric
18540b57cec5SDimitry Andric section_sp->SetPermissions(GetPermissions(header));
18550b57cec5SDimitry Andric section_sp->SetIsThreadSpecific(header.sh_flags & SHF_TLS);
18560b57cec5SDimitry Andric (InfoOr->Segment ? InfoOr->Segment->GetChildren() : *m_sections_up)
18570b57cec5SDimitry Andric .AddSection(section_sp);
18589dba64beSDimitry Andric provider.AddSection(std::move(*InfoOr), std::move(section_sp));
18590b57cec5SDimitry Andric }
18600b57cec5SDimitry Andric
18610b57cec5SDimitry Andric // For eTypeDebugInfo files, the Symbol Vendor will take care of updating the
18620b57cec5SDimitry Andric // unified section list.
18630b57cec5SDimitry Andric if (GetType() != eTypeDebugInfo)
18640b57cec5SDimitry Andric unified_section_list = *m_sections_up;
18659dba64beSDimitry Andric
18669dba64beSDimitry Andric // If there's a .gnu_debugdata section, we'll try to read the .symtab that's
18679dba64beSDimitry Andric // embedded in there and replace the one in the original object file (if any).
18689dba64beSDimitry Andric // If there's none in the orignal object file, we add it to it.
18699dba64beSDimitry Andric if (auto gdd_obj_file = GetGnuDebugDataObjectFile()) {
18709dba64beSDimitry Andric if (auto gdd_objfile_section_list = gdd_obj_file->GetSectionList()) {
18719dba64beSDimitry Andric if (SectionSP symtab_section_sp =
18729dba64beSDimitry Andric gdd_objfile_section_list->FindSectionByType(
18739dba64beSDimitry Andric eSectionTypeELFSymbolTable, true)) {
18749dba64beSDimitry Andric SectionSP module_section_sp = unified_section_list.FindSectionByType(
18759dba64beSDimitry Andric eSectionTypeELFSymbolTable, true);
18769dba64beSDimitry Andric if (module_section_sp)
18779dba64beSDimitry Andric unified_section_list.ReplaceSection(module_section_sp->GetID(),
18789dba64beSDimitry Andric symtab_section_sp);
18799dba64beSDimitry Andric else
18809dba64beSDimitry Andric unified_section_list.AddSection(symtab_section_sp);
18819dba64beSDimitry Andric }
18829dba64beSDimitry Andric }
18839dba64beSDimitry Andric }
18849dba64beSDimitry Andric }
18859dba64beSDimitry Andric
GetGnuDebugDataObjectFile()18869dba64beSDimitry Andric std::shared_ptr<ObjectFileELF> ObjectFileELF::GetGnuDebugDataObjectFile() {
18879dba64beSDimitry Andric if (m_gnu_debug_data_object_file != nullptr)
18889dba64beSDimitry Andric return m_gnu_debug_data_object_file;
18899dba64beSDimitry Andric
18909dba64beSDimitry Andric SectionSP section =
18919dba64beSDimitry Andric GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"));
18929dba64beSDimitry Andric if (!section)
18939dba64beSDimitry Andric return nullptr;
18949dba64beSDimitry Andric
18959dba64beSDimitry Andric if (!lldb_private::lzma::isAvailable()) {
18969dba64beSDimitry Andric GetModule()->ReportWarning(
18979dba64beSDimitry Andric "No LZMA support found for reading .gnu_debugdata section");
18989dba64beSDimitry Andric return nullptr;
18999dba64beSDimitry Andric }
19009dba64beSDimitry Andric
19019dba64beSDimitry Andric // Uncompress the data
19029dba64beSDimitry Andric DataExtractor data;
19039dba64beSDimitry Andric section->GetSectionData(data);
19049dba64beSDimitry Andric llvm::SmallVector<uint8_t, 0> uncompressedData;
19059dba64beSDimitry Andric auto err = lldb_private::lzma::uncompress(data.GetData(), uncompressedData);
19069dba64beSDimitry Andric if (err) {
19079dba64beSDimitry Andric GetModule()->ReportWarning(
19089dba64beSDimitry Andric "An error occurred while decompression the section %s: %s",
19099dba64beSDimitry Andric section->GetName().AsCString(), llvm::toString(std::move(err)).c_str());
19109dba64beSDimitry Andric return nullptr;
19119dba64beSDimitry Andric }
19129dba64beSDimitry Andric
19139dba64beSDimitry Andric // Construct ObjectFileELF object from decompressed buffer
19149dba64beSDimitry Andric DataBufferSP gdd_data_buf(
19159dba64beSDimitry Andric new DataBufferHeap(uncompressedData.data(), uncompressedData.size()));
19169dba64beSDimitry Andric auto fspec = GetFileSpec().CopyByAppendingPathComponent(
19179dba64beSDimitry Andric llvm::StringRef("gnu_debugdata"));
19189dba64beSDimitry Andric m_gnu_debug_data_object_file.reset(new ObjectFileELF(
19199dba64beSDimitry Andric GetModule(), gdd_data_buf, 0, &fspec, 0, gdd_data_buf->GetByteSize()));
19209dba64beSDimitry Andric
19219dba64beSDimitry Andric // This line is essential; otherwise a breakpoint can be set but not hit.
19229dba64beSDimitry Andric m_gnu_debug_data_object_file->SetType(ObjectFile::eTypeDebugInfo);
19239dba64beSDimitry Andric
19249dba64beSDimitry Andric ArchSpec spec = m_gnu_debug_data_object_file->GetArchitecture();
19259dba64beSDimitry Andric if (spec && m_gnu_debug_data_object_file->SetModulesArchitecture(spec))
19269dba64beSDimitry Andric return m_gnu_debug_data_object_file;
19279dba64beSDimitry Andric
19289dba64beSDimitry Andric return nullptr;
19290b57cec5SDimitry Andric }
19300b57cec5SDimitry Andric
19310b57cec5SDimitry Andric // Find the arm/aarch64 mapping symbol character in the given symbol name.
19320b57cec5SDimitry Andric // Mapping symbols have the form of "$<char>[.<any>]*". Additionally we
19330b57cec5SDimitry Andric // recognize cases when the mapping symbol prefixed by an arbitrary string
19340b57cec5SDimitry Andric // because if a symbol prefix added to each symbol in the object file with
19350b57cec5SDimitry Andric // objcopy then the mapping symbols are also prefixed.
FindArmAarch64MappingSymbol(const char * symbol_name)19360b57cec5SDimitry Andric static char FindArmAarch64MappingSymbol(const char *symbol_name) {
19370b57cec5SDimitry Andric if (!symbol_name)
19380b57cec5SDimitry Andric return '\0';
19390b57cec5SDimitry Andric
19400b57cec5SDimitry Andric const char *dollar_pos = ::strchr(symbol_name, '$');
19410b57cec5SDimitry Andric if (!dollar_pos || dollar_pos[1] == '\0')
19420b57cec5SDimitry Andric return '\0';
19430b57cec5SDimitry Andric
19440b57cec5SDimitry Andric if (dollar_pos[2] == '\0' || dollar_pos[2] == '.')
19450b57cec5SDimitry Andric return dollar_pos[1];
19460b57cec5SDimitry Andric return '\0';
19470b57cec5SDimitry Andric }
19480b57cec5SDimitry Andric
19490b57cec5SDimitry Andric #define STO_MIPS_ISA (3 << 6)
19500b57cec5SDimitry Andric #define STO_MICROMIPS (2 << 6)
19510b57cec5SDimitry Andric #define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS)
19520b57cec5SDimitry Andric
19530b57cec5SDimitry Andric // private
ParseSymbols(Symtab * symtab,user_id_t start_id,SectionList * section_list,const size_t num_symbols,const DataExtractor & symtab_data,const DataExtractor & strtab_data)19540b57cec5SDimitry Andric unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
19550b57cec5SDimitry Andric SectionList *section_list,
19560b57cec5SDimitry Andric const size_t num_symbols,
19570b57cec5SDimitry Andric const DataExtractor &symtab_data,
19580b57cec5SDimitry Andric const DataExtractor &strtab_data) {
19590b57cec5SDimitry Andric ELFSymbol symbol;
19600b57cec5SDimitry Andric lldb::offset_t offset = 0;
19610b57cec5SDimitry Andric
19620b57cec5SDimitry Andric static ConstString text_section_name(".text");
19630b57cec5SDimitry Andric static ConstString init_section_name(".init");
19640b57cec5SDimitry Andric static ConstString fini_section_name(".fini");
19650b57cec5SDimitry Andric static ConstString ctors_section_name(".ctors");
19660b57cec5SDimitry Andric static ConstString dtors_section_name(".dtors");
19670b57cec5SDimitry Andric
19680b57cec5SDimitry Andric static ConstString data_section_name(".data");
19690b57cec5SDimitry Andric static ConstString rodata_section_name(".rodata");
19700b57cec5SDimitry Andric static ConstString rodata1_section_name(".rodata1");
19710b57cec5SDimitry Andric static ConstString data2_section_name(".data1");
19720b57cec5SDimitry Andric static ConstString bss_section_name(".bss");
19730b57cec5SDimitry Andric static ConstString opd_section_name(".opd"); // For ppc64
19740b57cec5SDimitry Andric
19750b57cec5SDimitry Andric // On Android the oatdata and the oatexec symbols in the oat and odex files
19760b57cec5SDimitry Andric // covers the full .text section what causes issues with displaying unusable
19770b57cec5SDimitry Andric // symbol name to the user and very slow unwinding speed because the
19780b57cec5SDimitry Andric // instruction emulation based unwind plans try to emulate all instructions
19790b57cec5SDimitry Andric // in these symbols. Don't add these symbols to the symbol list as they have
19800b57cec5SDimitry Andric // no use for the debugger and they are causing a lot of trouble. Filtering
19810b57cec5SDimitry Andric // can't be restricted to Android because this special object file don't
19820b57cec5SDimitry Andric // contain the note section specifying the environment to Android but the
19830b57cec5SDimitry Andric // custom extension and file name makes it highly unlikely that this will
19840b57cec5SDimitry Andric // collide with anything else.
19850b57cec5SDimitry Andric ConstString file_extension = m_file.GetFileNameExtension();
19860b57cec5SDimitry Andric bool skip_oatdata_oatexec =
19870b57cec5SDimitry Andric file_extension == ".oat" || file_extension == ".odex";
19880b57cec5SDimitry Andric
19890b57cec5SDimitry Andric ArchSpec arch = GetArchitecture();
19900b57cec5SDimitry Andric ModuleSP module_sp(GetModule());
19910b57cec5SDimitry Andric SectionList *module_section_list =
19920b57cec5SDimitry Andric module_sp ? module_sp->GetSectionList() : nullptr;
19930b57cec5SDimitry Andric
19940b57cec5SDimitry Andric // Local cache to avoid doing a FindSectionByName for each symbol. The "const
19950b57cec5SDimitry Andric // char*" key must came from a ConstString object so they can be compared by
19960b57cec5SDimitry Andric // pointer
19970b57cec5SDimitry Andric std::unordered_map<const char *, lldb::SectionSP> section_name_to_section;
19980b57cec5SDimitry Andric
19990b57cec5SDimitry Andric unsigned i;
20000b57cec5SDimitry Andric for (i = 0; i < num_symbols; ++i) {
20010b57cec5SDimitry Andric if (!symbol.Parse(symtab_data, &offset))
20020b57cec5SDimitry Andric break;
20030b57cec5SDimitry Andric
20040b57cec5SDimitry Andric const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
20050b57cec5SDimitry Andric if (!symbol_name)
20060b57cec5SDimitry Andric symbol_name = "";
20070b57cec5SDimitry Andric
20080b57cec5SDimitry Andric // No need to add non-section symbols that have no names
20090b57cec5SDimitry Andric if (symbol.getType() != STT_SECTION &&
20100b57cec5SDimitry Andric (symbol_name == nullptr || symbol_name[0] == '\0'))
20110b57cec5SDimitry Andric continue;
20120b57cec5SDimitry Andric
20130b57cec5SDimitry Andric // Skipping oatdata and oatexec sections if it is requested. See details
20140b57cec5SDimitry Andric // above the definition of skip_oatdata_oatexec for the reasons.
20150b57cec5SDimitry Andric if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 ||
20160b57cec5SDimitry Andric ::strcmp(symbol_name, "oatexec") == 0))
20170b57cec5SDimitry Andric continue;
20180b57cec5SDimitry Andric
20190b57cec5SDimitry Andric SectionSP symbol_section_sp;
20200b57cec5SDimitry Andric SymbolType symbol_type = eSymbolTypeInvalid;
20210b57cec5SDimitry Andric Elf64_Half shndx = symbol.st_shndx;
20220b57cec5SDimitry Andric
20230b57cec5SDimitry Andric switch (shndx) {
20240b57cec5SDimitry Andric case SHN_ABS:
20250b57cec5SDimitry Andric symbol_type = eSymbolTypeAbsolute;
20260b57cec5SDimitry Andric break;
20270b57cec5SDimitry Andric case SHN_UNDEF:
20280b57cec5SDimitry Andric symbol_type = eSymbolTypeUndefined;
20290b57cec5SDimitry Andric break;
20300b57cec5SDimitry Andric default:
20310b57cec5SDimitry Andric symbol_section_sp = section_list->FindSectionByID(shndx);
20320b57cec5SDimitry Andric break;
20330b57cec5SDimitry Andric }
20340b57cec5SDimitry Andric
20350b57cec5SDimitry Andric // If a symbol is undefined do not process it further even if it has a STT
20360b57cec5SDimitry Andric // type
20370b57cec5SDimitry Andric if (symbol_type != eSymbolTypeUndefined) {
20380b57cec5SDimitry Andric switch (symbol.getType()) {
20390b57cec5SDimitry Andric default:
20400b57cec5SDimitry Andric case STT_NOTYPE:
20410b57cec5SDimitry Andric // The symbol's type is not specified.
20420b57cec5SDimitry Andric break;
20430b57cec5SDimitry Andric
20440b57cec5SDimitry Andric case STT_OBJECT:
20450b57cec5SDimitry Andric // The symbol is associated with a data object, such as a variable, an
20460b57cec5SDimitry Andric // array, etc.
20470b57cec5SDimitry Andric symbol_type = eSymbolTypeData;
20480b57cec5SDimitry Andric break;
20490b57cec5SDimitry Andric
20500b57cec5SDimitry Andric case STT_FUNC:
20510b57cec5SDimitry Andric // The symbol is associated with a function or other executable code.
20520b57cec5SDimitry Andric symbol_type = eSymbolTypeCode;
20530b57cec5SDimitry Andric break;
20540b57cec5SDimitry Andric
20550b57cec5SDimitry Andric case STT_SECTION:
20560b57cec5SDimitry Andric // The symbol is associated with a section. Symbol table entries of
20570b57cec5SDimitry Andric // this type exist primarily for relocation and normally have STB_LOCAL
20580b57cec5SDimitry Andric // binding.
20590b57cec5SDimitry Andric break;
20600b57cec5SDimitry Andric
20610b57cec5SDimitry Andric case STT_FILE:
20620b57cec5SDimitry Andric // Conventionally, the symbol's name gives the name of the source file
20630b57cec5SDimitry Andric // associated with the object file. A file symbol has STB_LOCAL
20640b57cec5SDimitry Andric // binding, its section index is SHN_ABS, and it precedes the other
20650b57cec5SDimitry Andric // STB_LOCAL symbols for the file, if it is present.
20660b57cec5SDimitry Andric symbol_type = eSymbolTypeSourceFile;
20670b57cec5SDimitry Andric break;
20680b57cec5SDimitry Andric
20690b57cec5SDimitry Andric case STT_GNU_IFUNC:
20700b57cec5SDimitry Andric // The symbol is associated with an indirect function. The actual
20710b57cec5SDimitry Andric // function will be resolved if it is referenced.
20720b57cec5SDimitry Andric symbol_type = eSymbolTypeResolver;
20730b57cec5SDimitry Andric break;
20740b57cec5SDimitry Andric }
20750b57cec5SDimitry Andric }
20760b57cec5SDimitry Andric
20770b57cec5SDimitry Andric if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) {
20780b57cec5SDimitry Andric if (symbol_section_sp) {
20790b57cec5SDimitry Andric ConstString sect_name = symbol_section_sp->GetName();
20800b57cec5SDimitry Andric if (sect_name == text_section_name || sect_name == init_section_name ||
20810b57cec5SDimitry Andric sect_name == fini_section_name || sect_name == ctors_section_name ||
20820b57cec5SDimitry Andric sect_name == dtors_section_name) {
20830b57cec5SDimitry Andric symbol_type = eSymbolTypeCode;
20840b57cec5SDimitry Andric } else if (sect_name == data_section_name ||
20850b57cec5SDimitry Andric sect_name == data2_section_name ||
20860b57cec5SDimitry Andric sect_name == rodata_section_name ||
20870b57cec5SDimitry Andric sect_name == rodata1_section_name ||
20880b57cec5SDimitry Andric sect_name == bss_section_name) {
20890b57cec5SDimitry Andric symbol_type = eSymbolTypeData;
20900b57cec5SDimitry Andric }
20910b57cec5SDimitry Andric }
20920b57cec5SDimitry Andric }
20930b57cec5SDimitry Andric
20940b57cec5SDimitry Andric int64_t symbol_value_offset = 0;
20950b57cec5SDimitry Andric uint32_t additional_flags = 0;
20960b57cec5SDimitry Andric
20970b57cec5SDimitry Andric if (arch.IsValid()) {
20980b57cec5SDimitry Andric if (arch.GetMachine() == llvm::Triple::arm) {
20990b57cec5SDimitry Andric if (symbol.getBinding() == STB_LOCAL) {
21000b57cec5SDimitry Andric char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
21010b57cec5SDimitry Andric if (symbol_type == eSymbolTypeCode) {
21020b57cec5SDimitry Andric switch (mapping_symbol) {
21030b57cec5SDimitry Andric case 'a':
21040b57cec5SDimitry Andric // $a[.<any>]* - marks an ARM instruction sequence
21050b57cec5SDimitry Andric m_address_class_map[symbol.st_value] = AddressClass::eCode;
21060b57cec5SDimitry Andric break;
21070b57cec5SDimitry Andric case 'b':
21080b57cec5SDimitry Andric case 't':
21090b57cec5SDimitry Andric // $b[.<any>]* - marks a THUMB BL instruction sequence
21100b57cec5SDimitry Andric // $t[.<any>]* - marks a THUMB instruction sequence
21110b57cec5SDimitry Andric m_address_class_map[symbol.st_value] =
21120b57cec5SDimitry Andric AddressClass::eCodeAlternateISA;
21130b57cec5SDimitry Andric break;
21140b57cec5SDimitry Andric case 'd':
21150b57cec5SDimitry Andric // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
21160b57cec5SDimitry Andric m_address_class_map[symbol.st_value] = AddressClass::eData;
21170b57cec5SDimitry Andric break;
21180b57cec5SDimitry Andric }
21190b57cec5SDimitry Andric }
21200b57cec5SDimitry Andric if (mapping_symbol)
21210b57cec5SDimitry Andric continue;
21220b57cec5SDimitry Andric }
21230b57cec5SDimitry Andric } else if (arch.GetMachine() == llvm::Triple::aarch64) {
21240b57cec5SDimitry Andric if (symbol.getBinding() == STB_LOCAL) {
21250b57cec5SDimitry Andric char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
21260b57cec5SDimitry Andric if (symbol_type == eSymbolTypeCode) {
21270b57cec5SDimitry Andric switch (mapping_symbol) {
21280b57cec5SDimitry Andric case 'x':
21290b57cec5SDimitry Andric // $x[.<any>]* - marks an A64 instruction sequence
21300b57cec5SDimitry Andric m_address_class_map[symbol.st_value] = AddressClass::eCode;
21310b57cec5SDimitry Andric break;
21320b57cec5SDimitry Andric case 'd':
21330b57cec5SDimitry Andric // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
21340b57cec5SDimitry Andric m_address_class_map[symbol.st_value] = AddressClass::eData;
21350b57cec5SDimitry Andric break;
21360b57cec5SDimitry Andric }
21370b57cec5SDimitry Andric }
21380b57cec5SDimitry Andric if (mapping_symbol)
21390b57cec5SDimitry Andric continue;
21400b57cec5SDimitry Andric }
21410b57cec5SDimitry Andric }
21420b57cec5SDimitry Andric
21430b57cec5SDimitry Andric if (arch.GetMachine() == llvm::Triple::arm) {
21440b57cec5SDimitry Andric if (symbol_type == eSymbolTypeCode) {
21450b57cec5SDimitry Andric if (symbol.st_value & 1) {
21460b57cec5SDimitry Andric // Subtracting 1 from the address effectively unsets the low order
21470b57cec5SDimitry Andric // bit, which results in the address actually pointing to the
21480b57cec5SDimitry Andric // beginning of the symbol. This delta will be used below in
21490b57cec5SDimitry Andric // conjunction with symbol.st_value to produce the final
21500b57cec5SDimitry Andric // symbol_value that we store in the symtab.
21510b57cec5SDimitry Andric symbol_value_offset = -1;
21520b57cec5SDimitry Andric m_address_class_map[symbol.st_value ^ 1] =
21530b57cec5SDimitry Andric AddressClass::eCodeAlternateISA;
21540b57cec5SDimitry Andric } else {
21550b57cec5SDimitry Andric // This address is ARM
21560b57cec5SDimitry Andric m_address_class_map[symbol.st_value] = AddressClass::eCode;
21570b57cec5SDimitry Andric }
21580b57cec5SDimitry Andric }
21590b57cec5SDimitry Andric }
21600b57cec5SDimitry Andric
21610b57cec5SDimitry Andric /*
21620b57cec5SDimitry Andric * MIPS:
21630b57cec5SDimitry Andric * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for
21640b57cec5SDimitry Andric * MIPS).
21650b57cec5SDimitry Andric * This allows processor to switch between microMIPS and MIPS without any
21660b57cec5SDimitry Andric * need
21670b57cec5SDimitry Andric * for special mode-control register. However, apart from .debug_line,
21680b57cec5SDimitry Andric * none of
21690b57cec5SDimitry Andric * the ELF/DWARF sections set the ISA bit (for symbol or section). Use
21700b57cec5SDimitry Andric * st_other
21710b57cec5SDimitry Andric * flag to check whether the symbol is microMIPS and then set the address
21720b57cec5SDimitry Andric * class
21730b57cec5SDimitry Andric * accordingly.
21740b57cec5SDimitry Andric */
21750b57cec5SDimitry Andric if (arch.IsMIPS()) {
21760b57cec5SDimitry Andric if (IS_MICROMIPS(symbol.st_other))
21770b57cec5SDimitry Andric m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
21780b57cec5SDimitry Andric else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) {
21790b57cec5SDimitry Andric symbol.st_value = symbol.st_value & (~1ull);
21800b57cec5SDimitry Andric m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
21810b57cec5SDimitry Andric } else {
21820b57cec5SDimitry Andric if (symbol_type == eSymbolTypeCode)
21830b57cec5SDimitry Andric m_address_class_map[symbol.st_value] = AddressClass::eCode;
21840b57cec5SDimitry Andric else if (symbol_type == eSymbolTypeData)
21850b57cec5SDimitry Andric m_address_class_map[symbol.st_value] = AddressClass::eData;
21860b57cec5SDimitry Andric else
21870b57cec5SDimitry Andric m_address_class_map[symbol.st_value] = AddressClass::eUnknown;
21880b57cec5SDimitry Andric }
21890b57cec5SDimitry Andric }
21900b57cec5SDimitry Andric }
21910b57cec5SDimitry Andric
21920b57cec5SDimitry Andric // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB
21930b57cec5SDimitry Andric // symbols. See above for more details.
21940b57cec5SDimitry Andric uint64_t symbol_value = symbol.st_value + symbol_value_offset;
21950b57cec5SDimitry Andric
21960b57cec5SDimitry Andric if (symbol_section_sp == nullptr && shndx == SHN_ABS &&
21970b57cec5SDimitry Andric symbol.st_size != 0) {
21980b57cec5SDimitry Andric // We don't have a section for a symbol with non-zero size. Create a new
21990b57cec5SDimitry Andric // section for it so the address range covered by the symbol is also
22000b57cec5SDimitry Andric // covered by the module (represented through the section list). It is
22010b57cec5SDimitry Andric // needed so module lookup for the addresses covered by this symbol will
22020b57cec5SDimitry Andric // be successfull. This case happens for absolute symbols.
22030b57cec5SDimitry Andric ConstString fake_section_name(std::string(".absolute.") + symbol_name);
22040b57cec5SDimitry Andric symbol_section_sp =
22050b57cec5SDimitry Andric std::make_shared<Section>(module_sp, this, SHN_ABS, fake_section_name,
22060b57cec5SDimitry Andric eSectionTypeAbsoluteAddress, symbol_value,
22070b57cec5SDimitry Andric symbol.st_size, 0, 0, 0, SHF_ALLOC);
22080b57cec5SDimitry Andric
22090b57cec5SDimitry Andric module_section_list->AddSection(symbol_section_sp);
22100b57cec5SDimitry Andric section_list->AddSection(symbol_section_sp);
22110b57cec5SDimitry Andric }
22120b57cec5SDimitry Andric
22130b57cec5SDimitry Andric if (symbol_section_sp &&
22140b57cec5SDimitry Andric CalculateType() != ObjectFile::Type::eTypeObjectFile)
22150b57cec5SDimitry Andric symbol_value -= symbol_section_sp->GetFileAddress();
22160b57cec5SDimitry Andric
22170b57cec5SDimitry Andric if (symbol_section_sp && module_section_list &&
22180b57cec5SDimitry Andric module_section_list != section_list) {
22190b57cec5SDimitry Andric ConstString sect_name = symbol_section_sp->GetName();
22200b57cec5SDimitry Andric auto section_it = section_name_to_section.find(sect_name.GetCString());
22210b57cec5SDimitry Andric if (section_it == section_name_to_section.end())
22220b57cec5SDimitry Andric section_it =
22230b57cec5SDimitry Andric section_name_to_section
22240b57cec5SDimitry Andric .emplace(sect_name.GetCString(),
22250b57cec5SDimitry Andric module_section_list->FindSectionByName(sect_name))
22260b57cec5SDimitry Andric .first;
22270b57cec5SDimitry Andric if (section_it->second)
22280b57cec5SDimitry Andric symbol_section_sp = section_it->second;
22290b57cec5SDimitry Andric }
22300b57cec5SDimitry Andric
22310b57cec5SDimitry Andric bool is_global = symbol.getBinding() == STB_GLOBAL;
22320b57cec5SDimitry Andric uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags;
22330b57cec5SDimitry Andric llvm::StringRef symbol_ref(symbol_name);
22340b57cec5SDimitry Andric
22350b57cec5SDimitry Andric // Symbol names may contain @VERSION suffixes. Find those and strip them
22360b57cec5SDimitry Andric // temporarily.
22370b57cec5SDimitry Andric size_t version_pos = symbol_ref.find('@');
22380b57cec5SDimitry Andric bool has_suffix = version_pos != llvm::StringRef::npos;
22390b57cec5SDimitry Andric llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos);
22409dba64beSDimitry Andric Mangled mangled(symbol_bare);
22410b57cec5SDimitry Andric
22420b57cec5SDimitry Andric // Now append the suffix back to mangled and unmangled names. Only do it if
22430b57cec5SDimitry Andric // the demangling was successful (string is not empty).
22440b57cec5SDimitry Andric if (has_suffix) {
22450b57cec5SDimitry Andric llvm::StringRef suffix = symbol_ref.substr(version_pos);
22460b57cec5SDimitry Andric
22470b57cec5SDimitry Andric llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef();
22480b57cec5SDimitry Andric if (!mangled_name.empty())
22490b57cec5SDimitry Andric mangled.SetMangledName(ConstString((mangled_name + suffix).str()));
22500b57cec5SDimitry Andric
22515ffd83dbSDimitry Andric ConstString demangled = mangled.GetDemangledName();
22520b57cec5SDimitry Andric llvm::StringRef demangled_name = demangled.GetStringRef();
22530b57cec5SDimitry Andric if (!demangled_name.empty())
22540b57cec5SDimitry Andric mangled.SetDemangledName(ConstString((demangled_name + suffix).str()));
22550b57cec5SDimitry Andric }
22560b57cec5SDimitry Andric
22570b57cec5SDimitry Andric // In ELF all symbol should have a valid size but it is not true for some
22580b57cec5SDimitry Andric // function symbols coming from hand written assembly. As none of the
22590b57cec5SDimitry Andric // function symbol should have 0 size we try to calculate the size for
22600b57cec5SDimitry Andric // these symbols in the symtab with saying that their original size is not
22610b57cec5SDimitry Andric // valid.
22620b57cec5SDimitry Andric bool symbol_size_valid =
22630b57cec5SDimitry Andric symbol.st_size != 0 || symbol.getType() != STT_FUNC;
22640b57cec5SDimitry Andric
22650b57cec5SDimitry Andric Symbol dc_symbol(
22660b57cec5SDimitry Andric i + start_id, // ID is the original symbol table index.
22670b57cec5SDimitry Andric mangled,
22680b57cec5SDimitry Andric symbol_type, // Type of this symbol
22690b57cec5SDimitry Andric is_global, // Is this globally visible?
22700b57cec5SDimitry Andric false, // Is this symbol debug info?
22710b57cec5SDimitry Andric false, // Is this symbol a trampoline?
22720b57cec5SDimitry Andric false, // Is this symbol artificial?
22730b57cec5SDimitry Andric AddressRange(symbol_section_sp, // Section in which this symbol is
22740b57cec5SDimitry Andric // defined or null.
22750b57cec5SDimitry Andric symbol_value, // Offset in section or symbol value.
22760b57cec5SDimitry Andric symbol.st_size), // Size in bytes of this symbol.
22770b57cec5SDimitry Andric symbol_size_valid, // Symbol size is valid
22780b57cec5SDimitry Andric has_suffix, // Contains linker annotations?
22790b57cec5SDimitry Andric flags); // Symbol flags.
2280480093f4SDimitry Andric if (symbol.getBinding() == STB_WEAK)
2281480093f4SDimitry Andric dc_symbol.SetIsWeak(true);
22820b57cec5SDimitry Andric symtab->AddSymbol(dc_symbol);
22830b57cec5SDimitry Andric }
22840b57cec5SDimitry Andric return i;
22850b57cec5SDimitry Andric }
22860b57cec5SDimitry Andric
ParseSymbolTable(Symtab * symbol_table,user_id_t start_id,lldb_private::Section * symtab)22870b57cec5SDimitry Andric unsigned ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
22880b57cec5SDimitry Andric user_id_t start_id,
22890b57cec5SDimitry Andric lldb_private::Section *symtab) {
22900b57cec5SDimitry Andric if (symtab->GetObjectFile() != this) {
22910b57cec5SDimitry Andric // If the symbol table section is owned by a different object file, have it
22920b57cec5SDimitry Andric // do the parsing.
22930b57cec5SDimitry Andric ObjectFileELF *obj_file_elf =
22940b57cec5SDimitry Andric static_cast<ObjectFileELF *>(symtab->GetObjectFile());
22950b57cec5SDimitry Andric return obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab);
22960b57cec5SDimitry Andric }
22970b57cec5SDimitry Andric
22980b57cec5SDimitry Andric // Get section list for this object file.
22990b57cec5SDimitry Andric SectionList *section_list = m_sections_up.get();
23000b57cec5SDimitry Andric if (!section_list)
23010b57cec5SDimitry Andric return 0;
23020b57cec5SDimitry Andric
23030b57cec5SDimitry Andric user_id_t symtab_id = symtab->GetID();
23040b57cec5SDimitry Andric const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
23050b57cec5SDimitry Andric assert(symtab_hdr->sh_type == SHT_SYMTAB ||
23060b57cec5SDimitry Andric symtab_hdr->sh_type == SHT_DYNSYM);
23070b57cec5SDimitry Andric
23080b57cec5SDimitry Andric // sh_link: section header index of associated string table.
23090b57cec5SDimitry Andric user_id_t strtab_id = symtab_hdr->sh_link;
23100b57cec5SDimitry Andric Section *strtab = section_list->FindSectionByID(strtab_id).get();
23110b57cec5SDimitry Andric
23120b57cec5SDimitry Andric if (symtab && strtab) {
23130b57cec5SDimitry Andric assert(symtab->GetObjectFile() == this);
23140b57cec5SDimitry Andric assert(strtab->GetObjectFile() == this);
23150b57cec5SDimitry Andric
23160b57cec5SDimitry Andric DataExtractor symtab_data;
23170b57cec5SDimitry Andric DataExtractor strtab_data;
23180b57cec5SDimitry Andric if (ReadSectionData(symtab, symtab_data) &&
23190b57cec5SDimitry Andric ReadSectionData(strtab, strtab_data)) {
23200b57cec5SDimitry Andric size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
23210b57cec5SDimitry Andric
23220b57cec5SDimitry Andric return ParseSymbols(symbol_table, start_id, section_list, num_symbols,
23230b57cec5SDimitry Andric symtab_data, strtab_data);
23240b57cec5SDimitry Andric }
23250b57cec5SDimitry Andric }
23260b57cec5SDimitry Andric
23270b57cec5SDimitry Andric return 0;
23280b57cec5SDimitry Andric }
23290b57cec5SDimitry Andric
ParseDynamicSymbols()23300b57cec5SDimitry Andric size_t ObjectFileELF::ParseDynamicSymbols() {
23310b57cec5SDimitry Andric if (m_dynamic_symbols.size())
23320b57cec5SDimitry Andric return m_dynamic_symbols.size();
23330b57cec5SDimitry Andric
23340b57cec5SDimitry Andric SectionList *section_list = GetSectionList();
23350b57cec5SDimitry Andric if (!section_list)
23360b57cec5SDimitry Andric return 0;
23370b57cec5SDimitry Andric
23380b57cec5SDimitry Andric // Find the SHT_DYNAMIC section.
23390b57cec5SDimitry Andric Section *dynsym =
23400b57cec5SDimitry Andric section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)
23410b57cec5SDimitry Andric .get();
23420b57cec5SDimitry Andric if (!dynsym)
23430b57cec5SDimitry Andric return 0;
23440b57cec5SDimitry Andric assert(dynsym->GetObjectFile() == this);
23450b57cec5SDimitry Andric
23460b57cec5SDimitry Andric ELFDynamic symbol;
23470b57cec5SDimitry Andric DataExtractor dynsym_data;
23480b57cec5SDimitry Andric if (ReadSectionData(dynsym, dynsym_data)) {
23490b57cec5SDimitry Andric const lldb::offset_t section_size = dynsym_data.GetByteSize();
23500b57cec5SDimitry Andric lldb::offset_t cursor = 0;
23510b57cec5SDimitry Andric
23520b57cec5SDimitry Andric while (cursor < section_size) {
23530b57cec5SDimitry Andric if (!symbol.Parse(dynsym_data, &cursor))
23540b57cec5SDimitry Andric break;
23550b57cec5SDimitry Andric
23560b57cec5SDimitry Andric m_dynamic_symbols.push_back(symbol);
23570b57cec5SDimitry Andric }
23580b57cec5SDimitry Andric }
23590b57cec5SDimitry Andric
23600b57cec5SDimitry Andric return m_dynamic_symbols.size();
23610b57cec5SDimitry Andric }
23620b57cec5SDimitry Andric
FindDynamicSymbol(unsigned tag)23630b57cec5SDimitry Andric const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) {
23640b57cec5SDimitry Andric if (!ParseDynamicSymbols())
23650b57cec5SDimitry Andric return nullptr;
23660b57cec5SDimitry Andric
23670b57cec5SDimitry Andric DynamicSymbolCollIter I = m_dynamic_symbols.begin();
23680b57cec5SDimitry Andric DynamicSymbolCollIter E = m_dynamic_symbols.end();
23690b57cec5SDimitry Andric for (; I != E; ++I) {
23700b57cec5SDimitry Andric ELFDynamic *symbol = &*I;
23710b57cec5SDimitry Andric
23720b57cec5SDimitry Andric if (symbol->d_tag == tag)
23730b57cec5SDimitry Andric return symbol;
23740b57cec5SDimitry Andric }
23750b57cec5SDimitry Andric
23760b57cec5SDimitry Andric return nullptr;
23770b57cec5SDimitry Andric }
23780b57cec5SDimitry Andric
PLTRelocationType()23790b57cec5SDimitry Andric unsigned ObjectFileELF::PLTRelocationType() {
23800b57cec5SDimitry Andric // DT_PLTREL
23810b57cec5SDimitry Andric // This member specifies the type of relocation entry to which the
23820b57cec5SDimitry Andric // procedure linkage table refers. The d_val member holds DT_REL or
23830b57cec5SDimitry Andric // DT_RELA, as appropriate. All relocations in a procedure linkage table
23840b57cec5SDimitry Andric // must use the same relocation.
23850b57cec5SDimitry Andric const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
23860b57cec5SDimitry Andric
23870b57cec5SDimitry Andric if (symbol)
23880b57cec5SDimitry Andric return symbol->d_val;
23890b57cec5SDimitry Andric
23900b57cec5SDimitry Andric return 0;
23910b57cec5SDimitry Andric }
23920b57cec5SDimitry Andric
23930b57cec5SDimitry Andric // Returns the size of the normal plt entries and the offset of the first
23940b57cec5SDimitry Andric // normal plt entry. The 0th entry in the plt table is usually a resolution
23950b57cec5SDimitry Andric // entry which have different size in some architectures then the rest of the
23960b57cec5SDimitry Andric // plt entries.
23970b57cec5SDimitry Andric static std::pair<uint64_t, uint64_t>
GetPltEntrySizeAndOffset(const ELFSectionHeader * rel_hdr,const ELFSectionHeader * plt_hdr)23980b57cec5SDimitry Andric GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr,
23990b57cec5SDimitry Andric const ELFSectionHeader *plt_hdr) {
24000b57cec5SDimitry Andric const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
24010b57cec5SDimitry Andric
24020b57cec5SDimitry Andric // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are
24030b57cec5SDimitry Andric // 16 bytes. So round the entsize up by the alignment if addralign is set.
24040b57cec5SDimitry Andric elf_xword plt_entsize =
24050b57cec5SDimitry Andric plt_hdr->sh_addralign
24060b57cec5SDimitry Andric ? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign)
24070b57cec5SDimitry Andric : plt_hdr->sh_entsize;
24080b57cec5SDimitry Andric
24090b57cec5SDimitry Andric // Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly.
24100b57cec5SDimitry Andric // PLT entries relocation code in general requires multiple instruction and
24110b57cec5SDimitry Andric // should be greater than 4 bytes in most cases. Try to guess correct size
24120b57cec5SDimitry Andric // just in case.
24130b57cec5SDimitry Andric if (plt_entsize <= 4) {
24140b57cec5SDimitry Andric // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the
24150b57cec5SDimitry Andric // size of the plt entries based on the number of entries and the size of
24160b57cec5SDimitry Andric // the plt section with the assumption that the size of the 0th entry is at
24170b57cec5SDimitry Andric // least as big as the size of the normal entries and it isn't much bigger
24180b57cec5SDimitry Andric // then that.
24190b57cec5SDimitry Andric if (plt_hdr->sh_addralign)
24200b57cec5SDimitry Andric plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign /
24210b57cec5SDimitry Andric (num_relocations + 1) * plt_hdr->sh_addralign;
24220b57cec5SDimitry Andric else
24230b57cec5SDimitry Andric plt_entsize = plt_hdr->sh_size / (num_relocations + 1);
24240b57cec5SDimitry Andric }
24250b57cec5SDimitry Andric
24260b57cec5SDimitry Andric elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize;
24270b57cec5SDimitry Andric
24280b57cec5SDimitry Andric return std::make_pair(plt_entsize, plt_offset);
24290b57cec5SDimitry Andric }
24300b57cec5SDimitry Andric
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)24310b57cec5SDimitry Andric static unsigned ParsePLTRelocations(
24320b57cec5SDimitry Andric Symtab *symbol_table, user_id_t start_id, unsigned rel_type,
24330b57cec5SDimitry Andric const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
24340b57cec5SDimitry Andric const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr,
24350b57cec5SDimitry Andric const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data,
24360b57cec5SDimitry Andric DataExtractor &symtab_data, DataExtractor &strtab_data) {
24370b57cec5SDimitry Andric ELFRelocation rel(rel_type);
24380b57cec5SDimitry Andric ELFSymbol symbol;
24390b57cec5SDimitry Andric lldb::offset_t offset = 0;
24400b57cec5SDimitry Andric
24410b57cec5SDimitry Andric uint64_t plt_offset, plt_entsize;
24420b57cec5SDimitry Andric std::tie(plt_entsize, plt_offset) =
24430b57cec5SDimitry Andric GetPltEntrySizeAndOffset(rel_hdr, plt_hdr);
24440b57cec5SDimitry Andric const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
24450b57cec5SDimitry Andric
24460b57cec5SDimitry Andric typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
24470b57cec5SDimitry Andric reloc_info_fn reloc_type;
24480b57cec5SDimitry Andric reloc_info_fn reloc_symbol;
24490b57cec5SDimitry Andric
24500b57cec5SDimitry Andric if (hdr->Is32Bit()) {
24510b57cec5SDimitry Andric reloc_type = ELFRelocation::RelocType32;
24520b57cec5SDimitry Andric reloc_symbol = ELFRelocation::RelocSymbol32;
24530b57cec5SDimitry Andric } else {
24540b57cec5SDimitry Andric reloc_type = ELFRelocation::RelocType64;
24550b57cec5SDimitry Andric reloc_symbol = ELFRelocation::RelocSymbol64;
24560b57cec5SDimitry Andric }
24570b57cec5SDimitry Andric
24580b57cec5SDimitry Andric unsigned slot_type = hdr->GetRelocationJumpSlotType();
24590b57cec5SDimitry Andric unsigned i;
24600b57cec5SDimitry Andric for (i = 0; i < num_relocations; ++i) {
24610b57cec5SDimitry Andric if (!rel.Parse(rel_data, &offset))
24620b57cec5SDimitry Andric break;
24630b57cec5SDimitry Andric
24640b57cec5SDimitry Andric if (reloc_type(rel) != slot_type)
24650b57cec5SDimitry Andric continue;
24660b57cec5SDimitry Andric
24670b57cec5SDimitry Andric lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
24680b57cec5SDimitry Andric if (!symbol.Parse(symtab_data, &symbol_offset))
24690b57cec5SDimitry Andric break;
24700b57cec5SDimitry Andric
24710b57cec5SDimitry Andric const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
24720b57cec5SDimitry Andric uint64_t plt_index = plt_offset + i * plt_entsize;
24730b57cec5SDimitry Andric
24740b57cec5SDimitry Andric Symbol jump_symbol(
24750b57cec5SDimitry Andric i + start_id, // Symbol table index
24760b57cec5SDimitry Andric symbol_name, // symbol name.
24770b57cec5SDimitry Andric eSymbolTypeTrampoline, // Type of this symbol
24780b57cec5SDimitry Andric false, // Is this globally visible?
24790b57cec5SDimitry Andric false, // Is this symbol debug info?
24800b57cec5SDimitry Andric true, // Is this symbol a trampoline?
24810b57cec5SDimitry Andric true, // Is this symbol artificial?
24820b57cec5SDimitry Andric plt_section_sp, // Section in which this symbol is defined or null.
24830b57cec5SDimitry Andric plt_index, // Offset in section or symbol value.
24840b57cec5SDimitry Andric plt_entsize, // Size in bytes of this symbol.
24850b57cec5SDimitry Andric true, // Size is valid
24860b57cec5SDimitry Andric false, // Contains linker annotations?
24870b57cec5SDimitry Andric 0); // Symbol flags.
24880b57cec5SDimitry Andric
24890b57cec5SDimitry Andric symbol_table->AddSymbol(jump_symbol);
24900b57cec5SDimitry Andric }
24910b57cec5SDimitry Andric
24920b57cec5SDimitry Andric return i;
24930b57cec5SDimitry Andric }
24940b57cec5SDimitry Andric
24950b57cec5SDimitry Andric unsigned
ParseTrampolineSymbols(Symtab * symbol_table,user_id_t start_id,const ELFSectionHeaderInfo * rel_hdr,user_id_t rel_id)24960b57cec5SDimitry Andric ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id,
24970b57cec5SDimitry Andric const ELFSectionHeaderInfo *rel_hdr,
24980b57cec5SDimitry Andric user_id_t rel_id) {
24990b57cec5SDimitry Andric assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
25000b57cec5SDimitry Andric
25010b57cec5SDimitry Andric // The link field points to the associated symbol table.
25020b57cec5SDimitry Andric user_id_t symtab_id = rel_hdr->sh_link;
25030b57cec5SDimitry Andric
25040b57cec5SDimitry Andric // If the link field doesn't point to the appropriate symbol name table then
25050b57cec5SDimitry Andric // try to find it by name as some compiler don't fill in the link fields.
25060b57cec5SDimitry Andric if (!symtab_id)
25070b57cec5SDimitry Andric symtab_id = GetSectionIndexByName(".dynsym");
25080b57cec5SDimitry Andric
25090b57cec5SDimitry Andric // Get PLT section. We cannot use rel_hdr->sh_info, since current linkers
25100b57cec5SDimitry Andric // point that to the .got.plt or .got section instead of .plt.
25110b57cec5SDimitry Andric user_id_t plt_id = GetSectionIndexByName(".plt");
25120b57cec5SDimitry Andric
25130b57cec5SDimitry Andric if (!symtab_id || !plt_id)
25140b57cec5SDimitry Andric return 0;
25150b57cec5SDimitry Andric
25160b57cec5SDimitry Andric const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
25170b57cec5SDimitry Andric if (!plt_hdr)
25180b57cec5SDimitry Andric return 0;
25190b57cec5SDimitry Andric
25200b57cec5SDimitry Andric const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
25210b57cec5SDimitry Andric if (!sym_hdr)
25220b57cec5SDimitry Andric return 0;
25230b57cec5SDimitry Andric
25240b57cec5SDimitry Andric SectionList *section_list = m_sections_up.get();
25250b57cec5SDimitry Andric if (!section_list)
25260b57cec5SDimitry Andric return 0;
25270b57cec5SDimitry Andric
25280b57cec5SDimitry Andric Section *rel_section = section_list->FindSectionByID(rel_id).get();
25290b57cec5SDimitry Andric if (!rel_section)
25300b57cec5SDimitry Andric return 0;
25310b57cec5SDimitry Andric
25320b57cec5SDimitry Andric SectionSP plt_section_sp(section_list->FindSectionByID(plt_id));
25330b57cec5SDimitry Andric if (!plt_section_sp)
25340b57cec5SDimitry Andric return 0;
25350b57cec5SDimitry Andric
25360b57cec5SDimitry Andric Section *symtab = section_list->FindSectionByID(symtab_id).get();
25370b57cec5SDimitry Andric if (!symtab)
25380b57cec5SDimitry Andric return 0;
25390b57cec5SDimitry Andric
25400b57cec5SDimitry Andric // sh_link points to associated string table.
25410b57cec5SDimitry Andric Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link).get();
25420b57cec5SDimitry Andric if (!strtab)
25430b57cec5SDimitry Andric return 0;
25440b57cec5SDimitry Andric
25450b57cec5SDimitry Andric DataExtractor rel_data;
25460b57cec5SDimitry Andric if (!ReadSectionData(rel_section, rel_data))
25470b57cec5SDimitry Andric return 0;
25480b57cec5SDimitry Andric
25490b57cec5SDimitry Andric DataExtractor symtab_data;
25500b57cec5SDimitry Andric if (!ReadSectionData(symtab, symtab_data))
25510b57cec5SDimitry Andric return 0;
25520b57cec5SDimitry Andric
25530b57cec5SDimitry Andric DataExtractor strtab_data;
25540b57cec5SDimitry Andric if (!ReadSectionData(strtab, strtab_data))
25550b57cec5SDimitry Andric return 0;
25560b57cec5SDimitry Andric
25570b57cec5SDimitry Andric unsigned rel_type = PLTRelocationType();
25580b57cec5SDimitry Andric if (!rel_type)
25590b57cec5SDimitry Andric return 0;
25600b57cec5SDimitry Andric
25610b57cec5SDimitry Andric return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header,
25620b57cec5SDimitry Andric rel_hdr, plt_hdr, sym_hdr, plt_section_sp,
25630b57cec5SDimitry Andric rel_data, symtab_data, strtab_data);
25640b57cec5SDimitry Andric }
25650b57cec5SDimitry Andric
ApplyRelocations(Symtab * symtab,const ELFHeader * hdr,const ELFSectionHeader * rel_hdr,const ELFSectionHeader * symtab_hdr,const ELFSectionHeader * debug_hdr,DataExtractor & rel_data,DataExtractor & symtab_data,DataExtractor & debug_data,Section * rel_section)25660b57cec5SDimitry Andric unsigned ObjectFileELF::ApplyRelocations(
25670b57cec5SDimitry Andric Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
25680b57cec5SDimitry Andric const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,
25690b57cec5SDimitry Andric DataExtractor &rel_data, DataExtractor &symtab_data,
25700b57cec5SDimitry Andric DataExtractor &debug_data, Section *rel_section) {
25710b57cec5SDimitry Andric ELFRelocation rel(rel_hdr->sh_type);
25720b57cec5SDimitry Andric lldb::addr_t offset = 0;
25730b57cec5SDimitry Andric const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
25740b57cec5SDimitry Andric typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
25750b57cec5SDimitry Andric reloc_info_fn reloc_type;
25760b57cec5SDimitry Andric reloc_info_fn reloc_symbol;
25770b57cec5SDimitry Andric
25780b57cec5SDimitry Andric if (hdr->Is32Bit()) {
25790b57cec5SDimitry Andric reloc_type = ELFRelocation::RelocType32;
25800b57cec5SDimitry Andric reloc_symbol = ELFRelocation::RelocSymbol32;
25810b57cec5SDimitry Andric } else {
25820b57cec5SDimitry Andric reloc_type = ELFRelocation::RelocType64;
25830b57cec5SDimitry Andric reloc_symbol = ELFRelocation::RelocSymbol64;
25840b57cec5SDimitry Andric }
25850b57cec5SDimitry Andric
25860b57cec5SDimitry Andric for (unsigned i = 0; i < num_relocations; ++i) {
25870b57cec5SDimitry Andric if (!rel.Parse(rel_data, &offset))
25880b57cec5SDimitry Andric break;
25890b57cec5SDimitry Andric
25900b57cec5SDimitry Andric Symbol *symbol = nullptr;
25910b57cec5SDimitry Andric
25920b57cec5SDimitry Andric if (hdr->Is32Bit()) {
25930b57cec5SDimitry Andric switch (reloc_type(rel)) {
25940b57cec5SDimitry Andric case R_386_32:
25950b57cec5SDimitry Andric case R_386_PC32:
25960b57cec5SDimitry Andric default:
25970b57cec5SDimitry Andric // FIXME: This asserts with this input:
25980b57cec5SDimitry Andric //
25990b57cec5SDimitry Andric // foo.cpp
26000b57cec5SDimitry Andric // int main(int argc, char **argv) { return 0; }
26010b57cec5SDimitry Andric //
26020b57cec5SDimitry Andric // clang++.exe --target=i686-unknown-linux-gnu -g -c foo.cpp -o foo.o
26030b57cec5SDimitry Andric //
26040b57cec5SDimitry Andric // and running this on the foo.o module.
26050b57cec5SDimitry Andric assert(false && "unexpected relocation type");
26060b57cec5SDimitry Andric }
26070b57cec5SDimitry Andric } else {
26080b57cec5SDimitry Andric switch (reloc_type(rel)) {
26090b57cec5SDimitry Andric case R_AARCH64_ABS64:
26100b57cec5SDimitry Andric case R_X86_64_64: {
26110b57cec5SDimitry Andric symbol = symtab->FindSymbolByID(reloc_symbol(rel));
26120b57cec5SDimitry Andric if (symbol) {
26130b57cec5SDimitry Andric addr_t value = symbol->GetAddressRef().GetFileAddress();
26140b57cec5SDimitry Andric DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
26150b57cec5SDimitry Andric uint64_t *dst = reinterpret_cast<uint64_t *>(
26160b57cec5SDimitry Andric data_buffer_sp->GetBytes() + rel_section->GetFileOffset() +
26170b57cec5SDimitry Andric ELFRelocation::RelocOffset64(rel));
26180b57cec5SDimitry Andric uint64_t val_offset = value + ELFRelocation::RelocAddend64(rel);
26190b57cec5SDimitry Andric memcpy(dst, &val_offset, sizeof(uint64_t));
26200b57cec5SDimitry Andric }
26210b57cec5SDimitry Andric break;
26220b57cec5SDimitry Andric }
26230b57cec5SDimitry Andric case R_X86_64_32:
26240b57cec5SDimitry Andric case R_X86_64_32S:
26250b57cec5SDimitry Andric case R_AARCH64_ABS32: {
26260b57cec5SDimitry Andric symbol = symtab->FindSymbolByID(reloc_symbol(rel));
26270b57cec5SDimitry Andric if (symbol) {
26280b57cec5SDimitry Andric addr_t value = symbol->GetAddressRef().GetFileAddress();
26290b57cec5SDimitry Andric value += ELFRelocation::RelocAddend32(rel);
26300b57cec5SDimitry Andric if ((reloc_type(rel) == R_X86_64_32 && (value > UINT32_MAX)) ||
26310b57cec5SDimitry Andric (reloc_type(rel) == R_X86_64_32S &&
26320b57cec5SDimitry Andric ((int64_t)value > INT32_MAX && (int64_t)value < INT32_MIN)) ||
26330b57cec5SDimitry Andric (reloc_type(rel) == R_AARCH64_ABS32 &&
26340b57cec5SDimitry Andric ((int64_t)value > INT32_MAX && (int64_t)value < INT32_MIN))) {
26350b57cec5SDimitry Andric Log *log =
26360b57cec5SDimitry Andric lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES);
26379dba64beSDimitry Andric LLDB_LOGF(log, "Failed to apply debug info relocations");
26380b57cec5SDimitry Andric break;
26390b57cec5SDimitry Andric }
26400b57cec5SDimitry Andric uint32_t truncated_addr = (value & 0xFFFFFFFF);
26410b57cec5SDimitry Andric DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
26420b57cec5SDimitry Andric uint32_t *dst = reinterpret_cast<uint32_t *>(
26430b57cec5SDimitry Andric data_buffer_sp->GetBytes() + rel_section->GetFileOffset() +
26440b57cec5SDimitry Andric ELFRelocation::RelocOffset32(rel));
26450b57cec5SDimitry Andric memcpy(dst, &truncated_addr, sizeof(uint32_t));
26460b57cec5SDimitry Andric }
26470b57cec5SDimitry Andric break;
26480b57cec5SDimitry Andric }
26490b57cec5SDimitry Andric case R_X86_64_PC32:
26500b57cec5SDimitry Andric default:
26510b57cec5SDimitry Andric assert(false && "unexpected relocation type");
26520b57cec5SDimitry Andric }
26530b57cec5SDimitry Andric }
26540b57cec5SDimitry Andric }
26550b57cec5SDimitry Andric
26560b57cec5SDimitry Andric return 0;
26570b57cec5SDimitry Andric }
26580b57cec5SDimitry Andric
RelocateDebugSections(const ELFSectionHeader * rel_hdr,user_id_t rel_id,lldb_private::Symtab * thetab)26590b57cec5SDimitry Andric unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr,
26600b57cec5SDimitry Andric user_id_t rel_id,
26610b57cec5SDimitry Andric lldb_private::Symtab *thetab) {
26620b57cec5SDimitry Andric assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
26630b57cec5SDimitry Andric
26640b57cec5SDimitry Andric // Parse in the section list if needed.
26650b57cec5SDimitry Andric SectionList *section_list = GetSectionList();
26660b57cec5SDimitry Andric if (!section_list)
26670b57cec5SDimitry Andric return 0;
26680b57cec5SDimitry Andric
26690b57cec5SDimitry Andric user_id_t symtab_id = rel_hdr->sh_link;
26700b57cec5SDimitry Andric user_id_t debug_id = rel_hdr->sh_info;
26710b57cec5SDimitry Andric
26720b57cec5SDimitry Andric const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
26730b57cec5SDimitry Andric if (!symtab_hdr)
26740b57cec5SDimitry Andric return 0;
26750b57cec5SDimitry Andric
26760b57cec5SDimitry Andric const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);
26770b57cec5SDimitry Andric if (!debug_hdr)
26780b57cec5SDimitry Andric return 0;
26790b57cec5SDimitry Andric
26800b57cec5SDimitry Andric Section *rel = section_list->FindSectionByID(rel_id).get();
26810b57cec5SDimitry Andric if (!rel)
26820b57cec5SDimitry Andric return 0;
26830b57cec5SDimitry Andric
26840b57cec5SDimitry Andric Section *symtab = section_list->FindSectionByID(symtab_id).get();
26850b57cec5SDimitry Andric if (!symtab)
26860b57cec5SDimitry Andric return 0;
26870b57cec5SDimitry Andric
26880b57cec5SDimitry Andric Section *debug = section_list->FindSectionByID(debug_id).get();
26890b57cec5SDimitry Andric if (!debug)
26900b57cec5SDimitry Andric return 0;
26910b57cec5SDimitry Andric
26920b57cec5SDimitry Andric DataExtractor rel_data;
26930b57cec5SDimitry Andric DataExtractor symtab_data;
26940b57cec5SDimitry Andric DataExtractor debug_data;
26950b57cec5SDimitry Andric
26960b57cec5SDimitry Andric if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) &&
26970b57cec5SDimitry Andric GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) &&
26980b57cec5SDimitry Andric GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) {
26990b57cec5SDimitry Andric ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr,
27000b57cec5SDimitry Andric rel_data, symtab_data, debug_data, debug);
27010b57cec5SDimitry Andric }
27020b57cec5SDimitry Andric
27030b57cec5SDimitry Andric return 0;
27040b57cec5SDimitry Andric }
27050b57cec5SDimitry Andric
GetSymtab()27060b57cec5SDimitry Andric Symtab *ObjectFileELF::GetSymtab() {
27070b57cec5SDimitry Andric ModuleSP module_sp(GetModule());
27080b57cec5SDimitry Andric if (!module_sp)
27090b57cec5SDimitry Andric return nullptr;
27100b57cec5SDimitry Andric
2711*5f7ddb14SDimitry Andric Progress progress(llvm::formatv("Parsing symbol table for {0}",
2712*5f7ddb14SDimitry Andric m_file.GetFilename().AsCString("<Unknown>")));
2713*5f7ddb14SDimitry Andric
27140b57cec5SDimitry Andric // We always want to use the main object file so we (hopefully) only have one
27150b57cec5SDimitry Andric // cached copy of our symtab, dynamic sections, etc.
27160b57cec5SDimitry Andric ObjectFile *module_obj_file = module_sp->GetObjectFile();
27170b57cec5SDimitry Andric if (module_obj_file && module_obj_file != this)
27180b57cec5SDimitry Andric return module_obj_file->GetSymtab();
27190b57cec5SDimitry Andric
27200b57cec5SDimitry Andric if (m_symtab_up == nullptr) {
27210b57cec5SDimitry Andric SectionList *section_list = module_sp->GetSectionList();
27220b57cec5SDimitry Andric if (!section_list)
27230b57cec5SDimitry Andric return nullptr;
27240b57cec5SDimitry Andric
27250b57cec5SDimitry Andric uint64_t symbol_id = 0;
27260b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
27270b57cec5SDimitry Andric
27280b57cec5SDimitry Andric // Sharable objects and dynamic executables usually have 2 distinct symbol
27290b57cec5SDimitry Andric // tables, one named ".symtab", and the other ".dynsym". The dynsym is a
27300b57cec5SDimitry Andric // smaller version of the symtab that only contains global symbols. The
27310b57cec5SDimitry Andric // information found in the dynsym is therefore also found in the symtab,
27320b57cec5SDimitry Andric // while the reverse is not necessarily true.
27330b57cec5SDimitry Andric Section *symtab =
27340b57cec5SDimitry Andric section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get();
27350b57cec5SDimitry Andric if (symtab) {
27365ffd83dbSDimitry Andric m_symtab_up = std::make_unique<Symtab>(symtab->GetObjectFile());
27370b57cec5SDimitry Andric symbol_id += ParseSymbolTable(m_symtab_up.get(), symbol_id, symtab);
27380b57cec5SDimitry Andric }
27390b57cec5SDimitry Andric
27409dba64beSDimitry Andric // The symtab section is non-allocable and can be stripped, while the
27419dba64beSDimitry Andric // .dynsym section which should always be always be there. To support the
27429dba64beSDimitry Andric // minidebuginfo case we parse .dynsym when there's a .gnu_debuginfo
27439dba64beSDimitry Andric // section, nomatter if .symtab was already parsed or not. This is because
27449dba64beSDimitry Andric // minidebuginfo normally removes the .symtab symbols which have their
27459dba64beSDimitry Andric // matching .dynsym counterparts.
27469dba64beSDimitry Andric if (!symtab ||
27479dba64beSDimitry Andric GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"))) {
27489dba64beSDimitry Andric Section *dynsym =
27499dba64beSDimitry Andric section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true)
27509dba64beSDimitry Andric .get();
27519dba64beSDimitry Andric if (dynsym) {
27529dba64beSDimitry Andric if (!m_symtab_up)
27535ffd83dbSDimitry Andric m_symtab_up = std::make_unique<Symtab>(dynsym->GetObjectFile());
27549dba64beSDimitry Andric symbol_id += ParseSymbolTable(m_symtab_up.get(), symbol_id, dynsym);
27559dba64beSDimitry Andric }
27569dba64beSDimitry Andric }
27579dba64beSDimitry Andric
27580b57cec5SDimitry Andric // DT_JMPREL
27590b57cec5SDimitry Andric // If present, this entry's d_ptr member holds the address of
27600b57cec5SDimitry Andric // relocation
27610b57cec5SDimitry Andric // entries associated solely with the procedure linkage table.
27620b57cec5SDimitry Andric // Separating
27630b57cec5SDimitry Andric // these relocation entries lets the dynamic linker ignore them during
27640b57cec5SDimitry Andric // process initialization, if lazy binding is enabled. If this entry is
27650b57cec5SDimitry Andric // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must
27660b57cec5SDimitry Andric // also be present.
27670b57cec5SDimitry Andric const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
27680b57cec5SDimitry Andric if (symbol) {
27690b57cec5SDimitry Andric // Synthesize trampoline symbols to help navigate the PLT.
27700b57cec5SDimitry Andric addr_t addr = symbol->d_ptr;
27710b57cec5SDimitry Andric Section *reloc_section =
27720b57cec5SDimitry Andric section_list->FindSectionContainingFileAddress(addr).get();
27730b57cec5SDimitry Andric if (reloc_section) {
27740b57cec5SDimitry Andric user_id_t reloc_id = reloc_section->GetID();
27750b57cec5SDimitry Andric const ELFSectionHeaderInfo *reloc_header =
27760b57cec5SDimitry Andric GetSectionHeaderByIndex(reloc_id);
2777*5f7ddb14SDimitry Andric if (reloc_header) {
27780b57cec5SDimitry Andric if (m_symtab_up == nullptr)
27795ffd83dbSDimitry Andric m_symtab_up =
27805ffd83dbSDimitry Andric std::make_unique<Symtab>(reloc_section->GetObjectFile());
27810b57cec5SDimitry Andric
27820b57cec5SDimitry Andric ParseTrampolineSymbols(m_symtab_up.get(), symbol_id, reloc_header,
27830b57cec5SDimitry Andric reloc_id);
27840b57cec5SDimitry Andric }
27850b57cec5SDimitry Andric }
2786*5f7ddb14SDimitry Andric }
27870b57cec5SDimitry Andric
27880b57cec5SDimitry Andric if (DWARFCallFrameInfo *eh_frame =
27890b57cec5SDimitry Andric GetModule()->GetUnwindTable().GetEHFrameInfo()) {
27900b57cec5SDimitry Andric if (m_symtab_up == nullptr)
27915ffd83dbSDimitry Andric m_symtab_up = std::make_unique<Symtab>(this);
27920b57cec5SDimitry Andric ParseUnwindSymbols(m_symtab_up.get(), eh_frame);
27930b57cec5SDimitry Andric }
27940b57cec5SDimitry Andric
27950b57cec5SDimitry Andric // If we still don't have any symtab then create an empty instance to avoid
27960b57cec5SDimitry Andric // do the section lookup next time.
27970b57cec5SDimitry Andric if (m_symtab_up == nullptr)
27985ffd83dbSDimitry Andric m_symtab_up = std::make_unique<Symtab>(this);
27990b57cec5SDimitry Andric
28009dba64beSDimitry Andric // In the event that there's no symbol entry for the entry point we'll
28015ffd83dbSDimitry Andric // artificially create one. We delegate to the symtab object the figuring
28029dba64beSDimitry Andric // out of the proper size, this will usually make it span til the next
28039dba64beSDimitry Andric // symbol it finds in the section. This means that if there are missing
28049dba64beSDimitry Andric // symbols the entry point might span beyond its function definition.
28059dba64beSDimitry Andric // We're fine with this as it doesn't make it worse than not having a
28069dba64beSDimitry Andric // symbol entry at all.
28079dba64beSDimitry Andric if (CalculateType() == eTypeExecutable) {
28089dba64beSDimitry Andric ArchSpec arch = GetArchitecture();
28099dba64beSDimitry Andric auto entry_point_addr = GetEntryPointAddress();
28109dba64beSDimitry Andric bool is_valid_entry_point =
28119dba64beSDimitry Andric entry_point_addr.IsValid() && entry_point_addr.IsSectionOffset();
28129dba64beSDimitry Andric addr_t entry_point_file_addr = entry_point_addr.GetFileAddress();
28139dba64beSDimitry Andric if (is_valid_entry_point && !m_symtab_up->FindSymbolContainingFileAddress(
28149dba64beSDimitry Andric entry_point_file_addr)) {
28159dba64beSDimitry Andric uint64_t symbol_id = m_symtab_up->GetNumSymbols();
2816*5f7ddb14SDimitry Andric // Don't set the name for any synthetic symbols, the Symbol
2817*5f7ddb14SDimitry Andric // object will generate one if needed when the name is accessed
2818*5f7ddb14SDimitry Andric // via accessors.
2819*5f7ddb14SDimitry Andric SectionSP section_sp = entry_point_addr.GetSection();
2820*5f7ddb14SDimitry Andric Symbol symbol(
2821*5f7ddb14SDimitry Andric /*symID=*/symbol_id,
2822*5f7ddb14SDimitry Andric /*name=*/llvm::StringRef(), // Name will be auto generated.
2823*5f7ddb14SDimitry Andric /*type=*/eSymbolTypeCode,
2824*5f7ddb14SDimitry Andric /*external=*/true,
2825*5f7ddb14SDimitry Andric /*is_debug=*/false,
2826*5f7ddb14SDimitry Andric /*is_trampoline=*/false,
2827*5f7ddb14SDimitry Andric /*is_artificial=*/true,
2828*5f7ddb14SDimitry Andric /*section_sp=*/section_sp,
2829*5f7ddb14SDimitry Andric /*offset=*/0,
2830*5f7ddb14SDimitry Andric /*size=*/0, // FDE can span multiple symbols so don't use its size.
2831*5f7ddb14SDimitry Andric /*size_is_valid=*/false,
2832*5f7ddb14SDimitry Andric /*contains_linker_annotations=*/false,
2833*5f7ddb14SDimitry Andric /*flags=*/0);
28349dba64beSDimitry Andric // When the entry point is arm thumb we need to explicitly set its
28359dba64beSDimitry Andric // class address to reflect that. This is important because expression
28369dba64beSDimitry Andric // evaluation relies on correctly setting a breakpoint at this
28379dba64beSDimitry Andric // address.
28389dba64beSDimitry Andric if (arch.GetMachine() == llvm::Triple::arm &&
2839*5f7ddb14SDimitry Andric (entry_point_file_addr & 1)) {
2840*5f7ddb14SDimitry Andric symbol.GetAddressRef().SetOffset(entry_point_addr.GetOffset() ^ 1);
28419dba64beSDimitry Andric m_address_class_map[entry_point_file_addr ^ 1] =
28429dba64beSDimitry Andric AddressClass::eCodeAlternateISA;
2843*5f7ddb14SDimitry Andric } else {
28449dba64beSDimitry Andric m_address_class_map[entry_point_file_addr] = AddressClass::eCode;
28459dba64beSDimitry Andric }
2846*5f7ddb14SDimitry Andric m_symtab_up->AddSymbol(symbol);
2847*5f7ddb14SDimitry Andric }
28489dba64beSDimitry Andric }
28499dba64beSDimitry Andric
28500b57cec5SDimitry Andric m_symtab_up->CalculateSymbolSizes();
28510b57cec5SDimitry Andric }
28520b57cec5SDimitry Andric
28530b57cec5SDimitry Andric return m_symtab_up.get();
28540b57cec5SDimitry Andric }
28550b57cec5SDimitry Andric
RelocateSection(lldb_private::Section * section)28560b57cec5SDimitry Andric void ObjectFileELF::RelocateSection(lldb_private::Section *section)
28570b57cec5SDimitry Andric {
28580b57cec5SDimitry Andric static const char *debug_prefix = ".debug";
28590b57cec5SDimitry Andric
28600b57cec5SDimitry Andric // Set relocated bit so we stop getting called, regardless of whether we
28610b57cec5SDimitry Andric // actually relocate.
28620b57cec5SDimitry Andric section->SetIsRelocated(true);
28630b57cec5SDimitry Andric
28640b57cec5SDimitry Andric // We only relocate in ELF relocatable files
28650b57cec5SDimitry Andric if (CalculateType() != eTypeObjectFile)
28660b57cec5SDimitry Andric return;
28670b57cec5SDimitry Andric
28680b57cec5SDimitry Andric const char *section_name = section->GetName().GetCString();
28690b57cec5SDimitry Andric // Can't relocate that which can't be named
28700b57cec5SDimitry Andric if (section_name == nullptr)
28710b57cec5SDimitry Andric return;
28720b57cec5SDimitry Andric
28730b57cec5SDimitry Andric // We don't relocate non-debug sections at the moment
28740b57cec5SDimitry Andric if (strncmp(section_name, debug_prefix, strlen(debug_prefix)))
28750b57cec5SDimitry Andric return;
28760b57cec5SDimitry Andric
28770b57cec5SDimitry Andric // Relocation section names to look for
28780b57cec5SDimitry Andric std::string needle = std::string(".rel") + section_name;
28790b57cec5SDimitry Andric std::string needlea = std::string(".rela") + section_name;
28800b57cec5SDimitry Andric
28810b57cec5SDimitry Andric for (SectionHeaderCollIter I = m_section_headers.begin();
28820b57cec5SDimitry Andric I != m_section_headers.end(); ++I) {
28830b57cec5SDimitry Andric if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) {
28840b57cec5SDimitry Andric const char *hay_name = I->section_name.GetCString();
28850b57cec5SDimitry Andric if (hay_name == nullptr)
28860b57cec5SDimitry Andric continue;
28870b57cec5SDimitry Andric if (needle == hay_name || needlea == hay_name) {
28880b57cec5SDimitry Andric const ELFSectionHeader &reloc_header = *I;
28890b57cec5SDimitry Andric user_id_t reloc_id = SectionIndex(I);
28900b57cec5SDimitry Andric RelocateDebugSections(&reloc_header, reloc_id, GetSymtab());
28910b57cec5SDimitry Andric break;
28920b57cec5SDimitry Andric }
28930b57cec5SDimitry Andric }
28940b57cec5SDimitry Andric }
28950b57cec5SDimitry Andric }
28960b57cec5SDimitry Andric
ParseUnwindSymbols(Symtab * symbol_table,DWARFCallFrameInfo * eh_frame)28970b57cec5SDimitry Andric void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table,
28980b57cec5SDimitry Andric DWARFCallFrameInfo *eh_frame) {
28990b57cec5SDimitry Andric SectionList *section_list = GetSectionList();
29000b57cec5SDimitry Andric if (!section_list)
29010b57cec5SDimitry Andric return;
29020b57cec5SDimitry Andric
29030b57cec5SDimitry Andric // First we save the new symbols into a separate list and add them to the
29045ffd83dbSDimitry Andric // symbol table after we collected all symbols we want to add. This is
29050b57cec5SDimitry Andric // neccessary because adding a new symbol invalidates the internal index of
29060b57cec5SDimitry Andric // the symtab what causing the next lookup to be slow because it have to
29070b57cec5SDimitry Andric // recalculate the index first.
29080b57cec5SDimitry Andric std::vector<Symbol> new_symbols;
29090b57cec5SDimitry Andric
2910*5f7ddb14SDimitry Andric size_t num_symbols = symbol_table->GetNumSymbols();
2911*5f7ddb14SDimitry Andric uint64_t last_symbol_id =
2912*5f7ddb14SDimitry Andric num_symbols ? symbol_table->SymbolAtIndex(num_symbols - 1)->GetID() : 0;
2913*5f7ddb14SDimitry Andric eh_frame->ForEachFDEEntries([&](lldb::addr_t file_addr, uint32_t size,
2914*5f7ddb14SDimitry Andric dw_offset_t) {
29150b57cec5SDimitry Andric Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr);
29160b57cec5SDimitry Andric if (symbol) {
29170b57cec5SDimitry Andric if (!symbol->GetByteSizeIsValid()) {
29180b57cec5SDimitry Andric symbol->SetByteSize(size);
29190b57cec5SDimitry Andric symbol->SetSizeIsSynthesized(true);
29200b57cec5SDimitry Andric }
29210b57cec5SDimitry Andric } else {
29220b57cec5SDimitry Andric SectionSP section_sp =
29230b57cec5SDimitry Andric section_list->FindSectionContainingFileAddress(file_addr);
29240b57cec5SDimitry Andric if (section_sp) {
29250b57cec5SDimitry Andric addr_t offset = file_addr - section_sp->GetFileAddress();
2926*5f7ddb14SDimitry Andric uint64_t symbol_id = ++last_symbol_id;
2927*5f7ddb14SDimitry Andric // Don't set the name for any synthetic symbols, the Symbol
2928*5f7ddb14SDimitry Andric // object will generate one if needed when the name is accessed
2929*5f7ddb14SDimitry Andric // via accessors.
29300b57cec5SDimitry Andric Symbol eh_symbol(
2931*5f7ddb14SDimitry Andric /*symID=*/symbol_id,
2932*5f7ddb14SDimitry Andric /*name=*/llvm::StringRef(), // Name will be auto generated.
2933*5f7ddb14SDimitry Andric /*type=*/eSymbolTypeCode,
2934*5f7ddb14SDimitry Andric /*external=*/true,
2935*5f7ddb14SDimitry Andric /*is_debug=*/false,
2936*5f7ddb14SDimitry Andric /*is_trampoline=*/false,
2937*5f7ddb14SDimitry Andric /*is_artificial=*/true,
2938*5f7ddb14SDimitry Andric /*section_sp=*/section_sp,
2939*5f7ddb14SDimitry Andric /*offset=*/offset,
2940*5f7ddb14SDimitry Andric /*size=*/0, // FDE can span multiple symbols so don't use its size.
2941*5f7ddb14SDimitry Andric /*size_is_valid=*/false,
2942*5f7ddb14SDimitry Andric /*contains_linker_annotations=*/false,
2943*5f7ddb14SDimitry Andric /*flags=*/0);
29440b57cec5SDimitry Andric new_symbols.push_back(eh_symbol);
29450b57cec5SDimitry Andric }
29460b57cec5SDimitry Andric }
29470b57cec5SDimitry Andric return true;
29480b57cec5SDimitry Andric });
29490b57cec5SDimitry Andric
29500b57cec5SDimitry Andric for (const Symbol &s : new_symbols)
29510b57cec5SDimitry Andric symbol_table->AddSymbol(s);
29520b57cec5SDimitry Andric }
29530b57cec5SDimitry Andric
IsStripped()29540b57cec5SDimitry Andric bool ObjectFileELF::IsStripped() {
29550b57cec5SDimitry Andric // TODO: determine this for ELF
29560b57cec5SDimitry Andric return false;
29570b57cec5SDimitry Andric }
29580b57cec5SDimitry Andric
29590b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
29600b57cec5SDimitry Andric // Dump
29610b57cec5SDimitry Andric //
29620b57cec5SDimitry Andric // Dump the specifics of the runtime file container (such as any headers
29630b57cec5SDimitry Andric // segments, sections, etc).
Dump(Stream * s)29640b57cec5SDimitry Andric void ObjectFileELF::Dump(Stream *s) {
29650b57cec5SDimitry Andric ModuleSP module_sp(GetModule());
29660b57cec5SDimitry Andric if (!module_sp) {
29670b57cec5SDimitry Andric return;
29680b57cec5SDimitry Andric }
29690b57cec5SDimitry Andric
29700b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
29710b57cec5SDimitry Andric s->Printf("%p: ", static_cast<void *>(this));
29720b57cec5SDimitry Andric s->Indent();
29730b57cec5SDimitry Andric s->PutCString("ObjectFileELF");
29740b57cec5SDimitry Andric
29750b57cec5SDimitry Andric ArchSpec header_arch = GetArchitecture();
29760b57cec5SDimitry Andric
29770b57cec5SDimitry Andric *s << ", file = '" << m_file
29780b57cec5SDimitry Andric << "', arch = " << header_arch.GetArchitectureName() << "\n";
29790b57cec5SDimitry Andric
29800b57cec5SDimitry Andric DumpELFHeader(s, m_header);
29810b57cec5SDimitry Andric s->EOL();
29820b57cec5SDimitry Andric DumpELFProgramHeaders(s);
29830b57cec5SDimitry Andric s->EOL();
29840b57cec5SDimitry Andric DumpELFSectionHeaders(s);
29850b57cec5SDimitry Andric s->EOL();
29860b57cec5SDimitry Andric SectionList *section_list = GetSectionList();
29870b57cec5SDimitry Andric if (section_list)
29885ffd83dbSDimitry Andric section_list->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
29895ffd83dbSDimitry Andric UINT32_MAX);
29900b57cec5SDimitry Andric Symtab *symtab = GetSymtab();
29910b57cec5SDimitry Andric if (symtab)
29920b57cec5SDimitry Andric symtab->Dump(s, nullptr, eSortOrderNone);
29930b57cec5SDimitry Andric s->EOL();
29940b57cec5SDimitry Andric DumpDependentModules(s);
29950b57cec5SDimitry Andric s->EOL();
29960b57cec5SDimitry Andric }
29970b57cec5SDimitry Andric
29980b57cec5SDimitry Andric // DumpELFHeader
29990b57cec5SDimitry Andric //
30000b57cec5SDimitry Andric // Dump the ELF header to the specified output stream
DumpELFHeader(Stream * s,const ELFHeader & header)30010b57cec5SDimitry Andric void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) {
30020b57cec5SDimitry Andric s->PutCString("ELF Header\n");
30030b57cec5SDimitry Andric s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
30040b57cec5SDimitry Andric s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1],
30050b57cec5SDimitry Andric header.e_ident[EI_MAG1]);
30060b57cec5SDimitry Andric s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2],
30070b57cec5SDimitry Andric header.e_ident[EI_MAG2]);
30080b57cec5SDimitry Andric s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3],
30090b57cec5SDimitry Andric header.e_ident[EI_MAG3]);
30100b57cec5SDimitry Andric
30110b57cec5SDimitry Andric s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
30120b57cec5SDimitry Andric s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
30130b57cec5SDimitry Andric DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
30140b57cec5SDimitry Andric s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
30150b57cec5SDimitry Andric s->Printf("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
30160b57cec5SDimitry Andric
30170b57cec5SDimitry Andric s->Printf("e_type = 0x%4.4x ", header.e_type);
30180b57cec5SDimitry Andric DumpELFHeader_e_type(s, header.e_type);
30190b57cec5SDimitry Andric s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
30200b57cec5SDimitry Andric s->Printf("e_version = 0x%8.8x\n", header.e_version);
30210b57cec5SDimitry Andric s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
30220b57cec5SDimitry Andric s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
30230b57cec5SDimitry Andric s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
30240b57cec5SDimitry Andric s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
30250b57cec5SDimitry Andric s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
30260b57cec5SDimitry Andric s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
30270b57cec5SDimitry Andric s->Printf("e_phnum = 0x%8.8x\n", header.e_phnum);
30280b57cec5SDimitry Andric s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
30290b57cec5SDimitry Andric s->Printf("e_shnum = 0x%8.8x\n", header.e_shnum);
30300b57cec5SDimitry Andric s->Printf("e_shstrndx = 0x%8.8x\n", header.e_shstrndx);
30310b57cec5SDimitry Andric }
30320b57cec5SDimitry Andric
30330b57cec5SDimitry Andric // DumpELFHeader_e_type
30340b57cec5SDimitry Andric //
30350b57cec5SDimitry Andric // Dump an token value for the ELF header member e_type
DumpELFHeader_e_type(Stream * s,elf_half e_type)30360b57cec5SDimitry Andric void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) {
30370b57cec5SDimitry Andric switch (e_type) {
30380b57cec5SDimitry Andric case ET_NONE:
30390b57cec5SDimitry Andric *s << "ET_NONE";
30400b57cec5SDimitry Andric break;
30410b57cec5SDimitry Andric case ET_REL:
30420b57cec5SDimitry Andric *s << "ET_REL";
30430b57cec5SDimitry Andric break;
30440b57cec5SDimitry Andric case ET_EXEC:
30450b57cec5SDimitry Andric *s << "ET_EXEC";
30460b57cec5SDimitry Andric break;
30470b57cec5SDimitry Andric case ET_DYN:
30480b57cec5SDimitry Andric *s << "ET_DYN";
30490b57cec5SDimitry Andric break;
30500b57cec5SDimitry Andric case ET_CORE:
30510b57cec5SDimitry Andric *s << "ET_CORE";
30520b57cec5SDimitry Andric break;
30530b57cec5SDimitry Andric default:
30540b57cec5SDimitry Andric break;
30550b57cec5SDimitry Andric }
30560b57cec5SDimitry Andric }
30570b57cec5SDimitry Andric
30580b57cec5SDimitry Andric // DumpELFHeader_e_ident_EI_DATA
30590b57cec5SDimitry Andric //
30600b57cec5SDimitry Andric // Dump an token value for the ELF header member e_ident[EI_DATA]
DumpELFHeader_e_ident_EI_DATA(Stream * s,unsigned char ei_data)30610b57cec5SDimitry Andric void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s,
30620b57cec5SDimitry Andric unsigned char ei_data) {
30630b57cec5SDimitry Andric switch (ei_data) {
30640b57cec5SDimitry Andric case ELFDATANONE:
30650b57cec5SDimitry Andric *s << "ELFDATANONE";
30660b57cec5SDimitry Andric break;
30670b57cec5SDimitry Andric case ELFDATA2LSB:
30680b57cec5SDimitry Andric *s << "ELFDATA2LSB - Little Endian";
30690b57cec5SDimitry Andric break;
30700b57cec5SDimitry Andric case ELFDATA2MSB:
30710b57cec5SDimitry Andric *s << "ELFDATA2MSB - Big Endian";
30720b57cec5SDimitry Andric break;
30730b57cec5SDimitry Andric default:
30740b57cec5SDimitry Andric break;
30750b57cec5SDimitry Andric }
30760b57cec5SDimitry Andric }
30770b57cec5SDimitry Andric
30780b57cec5SDimitry Andric // DumpELFProgramHeader
30790b57cec5SDimitry Andric //
30800b57cec5SDimitry Andric // Dump a single ELF program header to the specified output stream
DumpELFProgramHeader(Stream * s,const ELFProgramHeader & ph)30810b57cec5SDimitry Andric void ObjectFileELF::DumpELFProgramHeader(Stream *s,
30820b57cec5SDimitry Andric const ELFProgramHeader &ph) {
30830b57cec5SDimitry Andric DumpELFProgramHeader_p_type(s, ph.p_type);
30840b57cec5SDimitry Andric s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset,
30850b57cec5SDimitry Andric ph.p_vaddr, ph.p_paddr);
30860b57cec5SDimitry Andric s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz,
30870b57cec5SDimitry Andric ph.p_flags);
30880b57cec5SDimitry Andric
30890b57cec5SDimitry Andric DumpELFProgramHeader_p_flags(s, ph.p_flags);
30900b57cec5SDimitry Andric s->Printf(") %8.8" PRIx64, ph.p_align);
30910b57cec5SDimitry Andric }
30920b57cec5SDimitry Andric
30930b57cec5SDimitry Andric // DumpELFProgramHeader_p_type
30940b57cec5SDimitry Andric //
30950b57cec5SDimitry Andric // Dump an token value for the ELF program header member p_type which describes
30960b57cec5SDimitry Andric // the type of the program header
DumpELFProgramHeader_p_type(Stream * s,elf_word p_type)30970b57cec5SDimitry Andric void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) {
30980b57cec5SDimitry Andric const int kStrWidth = 15;
30990b57cec5SDimitry Andric switch (p_type) {
31000b57cec5SDimitry Andric CASE_AND_STREAM(s, PT_NULL, kStrWidth);
31010b57cec5SDimitry Andric CASE_AND_STREAM(s, PT_LOAD, kStrWidth);
31020b57cec5SDimitry Andric CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth);
31030b57cec5SDimitry Andric CASE_AND_STREAM(s, PT_INTERP, kStrWidth);
31040b57cec5SDimitry Andric CASE_AND_STREAM(s, PT_NOTE, kStrWidth);
31050b57cec5SDimitry Andric CASE_AND_STREAM(s, PT_SHLIB, kStrWidth);
31060b57cec5SDimitry Andric CASE_AND_STREAM(s, PT_PHDR, kStrWidth);
31070b57cec5SDimitry Andric CASE_AND_STREAM(s, PT_TLS, kStrWidth);
31080b57cec5SDimitry Andric CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
31090b57cec5SDimitry Andric default:
31100b57cec5SDimitry Andric s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
31110b57cec5SDimitry Andric break;
31120b57cec5SDimitry Andric }
31130b57cec5SDimitry Andric }
31140b57cec5SDimitry Andric
31150b57cec5SDimitry Andric // DumpELFProgramHeader_p_flags
31160b57cec5SDimitry Andric //
31170b57cec5SDimitry Andric // Dump an token value for the ELF program header member p_flags
DumpELFProgramHeader_p_flags(Stream * s,elf_word p_flags)31180b57cec5SDimitry Andric void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) {
31190b57cec5SDimitry Andric *s << ((p_flags & PF_X) ? "PF_X" : " ")
31200b57cec5SDimitry Andric << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
31210b57cec5SDimitry Andric << ((p_flags & PF_W) ? "PF_W" : " ")
31220b57cec5SDimitry Andric << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
31230b57cec5SDimitry Andric << ((p_flags & PF_R) ? "PF_R" : " ");
31240b57cec5SDimitry Andric }
31250b57cec5SDimitry Andric
31260b57cec5SDimitry Andric // DumpELFProgramHeaders
31270b57cec5SDimitry Andric //
31280b57cec5SDimitry Andric // Dump all of the ELF program header to the specified output stream
DumpELFProgramHeaders(Stream * s)31290b57cec5SDimitry Andric void ObjectFileELF::DumpELFProgramHeaders(Stream *s) {
31300b57cec5SDimitry Andric if (!ParseProgramHeaders())
31310b57cec5SDimitry Andric return;
31320b57cec5SDimitry Andric
31330b57cec5SDimitry Andric s->PutCString("Program Headers\n");
31340b57cec5SDimitry Andric s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
31350b57cec5SDimitry Andric "p_filesz p_memsz p_flags p_align\n");
31360b57cec5SDimitry Andric s->PutCString("==== --------------- -------- -------- -------- "
31370b57cec5SDimitry Andric "-------- -------- ------------------------- --------\n");
31380b57cec5SDimitry Andric
31390b57cec5SDimitry Andric for (const auto &H : llvm::enumerate(m_program_headers)) {
31400b57cec5SDimitry Andric s->Format("[{0,2}] ", H.index());
31410b57cec5SDimitry Andric ObjectFileELF::DumpELFProgramHeader(s, H.value());
31420b57cec5SDimitry Andric s->EOL();
31430b57cec5SDimitry Andric }
31440b57cec5SDimitry Andric }
31450b57cec5SDimitry Andric
31460b57cec5SDimitry Andric // DumpELFSectionHeader
31470b57cec5SDimitry Andric //
31480b57cec5SDimitry Andric // Dump a single ELF section header to the specified output stream
DumpELFSectionHeader(Stream * s,const ELFSectionHeaderInfo & sh)31490b57cec5SDimitry Andric void ObjectFileELF::DumpELFSectionHeader(Stream *s,
31500b57cec5SDimitry Andric const ELFSectionHeaderInfo &sh) {
31510b57cec5SDimitry Andric s->Printf("%8.8x ", sh.sh_name);
31520b57cec5SDimitry Andric DumpELFSectionHeader_sh_type(s, sh.sh_type);
31530b57cec5SDimitry Andric s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
31540b57cec5SDimitry Andric DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
31550b57cec5SDimitry Andric s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr,
31560b57cec5SDimitry Andric sh.sh_offset, sh.sh_size);
31570b57cec5SDimitry Andric s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
31580b57cec5SDimitry Andric s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
31590b57cec5SDimitry Andric }
31600b57cec5SDimitry Andric
31610b57cec5SDimitry Andric // DumpELFSectionHeader_sh_type
31620b57cec5SDimitry Andric //
31630b57cec5SDimitry Andric // Dump an token value for the ELF section header member sh_type which
31640b57cec5SDimitry Andric // describes the type of the section
DumpELFSectionHeader_sh_type(Stream * s,elf_word sh_type)31650b57cec5SDimitry Andric void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) {
31660b57cec5SDimitry Andric const int kStrWidth = 12;
31670b57cec5SDimitry Andric switch (sh_type) {
31680b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_NULL, kStrWidth);
31690b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth);
31700b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth);
31710b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth);
31720b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_RELA, kStrWidth);
31730b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_HASH, kStrWidth);
31740b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth);
31750b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_NOTE, kStrWidth);
31760b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth);
31770b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_REL, kStrWidth);
31780b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth);
31790b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth);
31800b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth);
31810b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth);
31820b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth);
31830b57cec5SDimitry Andric CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth);
31840b57cec5SDimitry Andric default:
31850b57cec5SDimitry Andric s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
31860b57cec5SDimitry Andric break;
31870b57cec5SDimitry Andric }
31880b57cec5SDimitry Andric }
31890b57cec5SDimitry Andric
31900b57cec5SDimitry Andric // DumpELFSectionHeader_sh_flags
31910b57cec5SDimitry Andric //
31920b57cec5SDimitry Andric // Dump an token value for the ELF section header member sh_flags
DumpELFSectionHeader_sh_flags(Stream * s,elf_xword sh_flags)31930b57cec5SDimitry Andric void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s,
31940b57cec5SDimitry Andric elf_xword sh_flags) {
31950b57cec5SDimitry Andric *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
31960b57cec5SDimitry Andric << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
31970b57cec5SDimitry Andric << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
31980b57cec5SDimitry Andric << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
31990b57cec5SDimitry Andric << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
32000b57cec5SDimitry Andric }
32010b57cec5SDimitry Andric
32020b57cec5SDimitry Andric // DumpELFSectionHeaders
32030b57cec5SDimitry Andric //
32040b57cec5SDimitry Andric // Dump all of the ELF section header to the specified output stream
DumpELFSectionHeaders(Stream * s)32050b57cec5SDimitry Andric void ObjectFileELF::DumpELFSectionHeaders(Stream *s) {
32060b57cec5SDimitry Andric if (!ParseSectionHeaders())
32070b57cec5SDimitry Andric return;
32080b57cec5SDimitry Andric
32090b57cec5SDimitry Andric s->PutCString("Section Headers\n");
32100b57cec5SDimitry Andric s->PutCString("IDX name type flags "
32110b57cec5SDimitry Andric "addr offset size link info addralgn "
32120b57cec5SDimitry Andric "entsize Name\n");
32130b57cec5SDimitry Andric s->PutCString("==== -------- ------------ -------------------------------- "
32140b57cec5SDimitry Andric "-------- -------- -------- -------- -------- -------- "
32150b57cec5SDimitry Andric "-------- ====================\n");
32160b57cec5SDimitry Andric
32170b57cec5SDimitry Andric uint32_t idx = 0;
32180b57cec5SDimitry Andric for (SectionHeaderCollConstIter I = m_section_headers.begin();
32190b57cec5SDimitry Andric I != m_section_headers.end(); ++I, ++idx) {
32200b57cec5SDimitry Andric s->Printf("[%2u] ", idx);
32210b57cec5SDimitry Andric ObjectFileELF::DumpELFSectionHeader(s, *I);
32220b57cec5SDimitry Andric const char *section_name = I->section_name.AsCString("");
32230b57cec5SDimitry Andric if (section_name)
32240b57cec5SDimitry Andric *s << ' ' << section_name << "\n";
32250b57cec5SDimitry Andric }
32260b57cec5SDimitry Andric }
32270b57cec5SDimitry Andric
DumpDependentModules(lldb_private::Stream * s)32280b57cec5SDimitry Andric void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) {
32290b57cec5SDimitry Andric size_t num_modules = ParseDependentModules();
32300b57cec5SDimitry Andric
32310b57cec5SDimitry Andric if (num_modules > 0) {
32320b57cec5SDimitry Andric s->PutCString("Dependent Modules:\n");
32330b57cec5SDimitry Andric for (unsigned i = 0; i < num_modules; ++i) {
32340b57cec5SDimitry Andric const FileSpec &spec = m_filespec_up->GetFileSpecAtIndex(i);
32350b57cec5SDimitry Andric s->Printf(" %s\n", spec.GetFilename().GetCString());
32360b57cec5SDimitry Andric }
32370b57cec5SDimitry Andric }
32380b57cec5SDimitry Andric }
32390b57cec5SDimitry Andric
GetArchitecture()32400b57cec5SDimitry Andric ArchSpec ObjectFileELF::GetArchitecture() {
32410b57cec5SDimitry Andric if (!ParseHeader())
32420b57cec5SDimitry Andric return ArchSpec();
32430b57cec5SDimitry Andric
32440b57cec5SDimitry Andric if (m_section_headers.empty()) {
32450b57cec5SDimitry Andric // Allow elf notes to be parsed which may affect the detected architecture.
32460b57cec5SDimitry Andric ParseSectionHeaders();
32470b57cec5SDimitry Andric }
32480b57cec5SDimitry Andric
32490b57cec5SDimitry Andric if (CalculateType() == eTypeCoreFile &&
32500b57cec5SDimitry Andric !m_arch_spec.TripleOSWasSpecified()) {
32510b57cec5SDimitry Andric // Core files don't have section headers yet they have PT_NOTE program
32520b57cec5SDimitry Andric // headers that might shed more light on the architecture
32530b57cec5SDimitry Andric for (const elf::ELFProgramHeader &H : ProgramHeaders()) {
32540b57cec5SDimitry Andric if (H.p_type != PT_NOTE || H.p_offset == 0 || H.p_filesz == 0)
32550b57cec5SDimitry Andric continue;
32560b57cec5SDimitry Andric DataExtractor data;
32570b57cec5SDimitry Andric if (data.SetData(m_data, H.p_offset, H.p_filesz) == H.p_filesz) {
32580b57cec5SDimitry Andric UUID uuid;
32590b57cec5SDimitry Andric RefineModuleDetailsFromNote(data, m_arch_spec, uuid);
32600b57cec5SDimitry Andric }
32610b57cec5SDimitry Andric }
32620b57cec5SDimitry Andric }
32630b57cec5SDimitry Andric return m_arch_spec;
32640b57cec5SDimitry Andric }
32650b57cec5SDimitry Andric
CalculateType()32660b57cec5SDimitry Andric ObjectFile::Type ObjectFileELF::CalculateType() {
32670b57cec5SDimitry Andric switch (m_header.e_type) {
32680b57cec5SDimitry Andric case llvm::ELF::ET_NONE:
32690b57cec5SDimitry Andric // 0 - No file type
32700b57cec5SDimitry Andric return eTypeUnknown;
32710b57cec5SDimitry Andric
32720b57cec5SDimitry Andric case llvm::ELF::ET_REL:
32730b57cec5SDimitry Andric // 1 - Relocatable file
32740b57cec5SDimitry Andric return eTypeObjectFile;
32750b57cec5SDimitry Andric
32760b57cec5SDimitry Andric case llvm::ELF::ET_EXEC:
32770b57cec5SDimitry Andric // 2 - Executable file
32780b57cec5SDimitry Andric return eTypeExecutable;
32790b57cec5SDimitry Andric
32800b57cec5SDimitry Andric case llvm::ELF::ET_DYN:
32810b57cec5SDimitry Andric // 3 - Shared object file
32820b57cec5SDimitry Andric return eTypeSharedLibrary;
32830b57cec5SDimitry Andric
32840b57cec5SDimitry Andric case ET_CORE:
32850b57cec5SDimitry Andric // 4 - Core file
32860b57cec5SDimitry Andric return eTypeCoreFile;
32870b57cec5SDimitry Andric
32880b57cec5SDimitry Andric default:
32890b57cec5SDimitry Andric break;
32900b57cec5SDimitry Andric }
32910b57cec5SDimitry Andric return eTypeUnknown;
32920b57cec5SDimitry Andric }
32930b57cec5SDimitry Andric
CalculateStrata()32940b57cec5SDimitry Andric ObjectFile::Strata ObjectFileELF::CalculateStrata() {
32950b57cec5SDimitry Andric switch (m_header.e_type) {
32960b57cec5SDimitry Andric case llvm::ELF::ET_NONE:
32970b57cec5SDimitry Andric // 0 - No file type
32980b57cec5SDimitry Andric return eStrataUnknown;
32990b57cec5SDimitry Andric
33000b57cec5SDimitry Andric case llvm::ELF::ET_REL:
33010b57cec5SDimitry Andric // 1 - Relocatable file
33020b57cec5SDimitry Andric return eStrataUnknown;
33030b57cec5SDimitry Andric
33040b57cec5SDimitry Andric case llvm::ELF::ET_EXEC:
33050b57cec5SDimitry Andric // 2 - Executable file
33060b57cec5SDimitry Andric // TODO: is there any way to detect that an executable is a kernel
33070b57cec5SDimitry Andric // related executable by inspecting the program headers, section headers,
33080b57cec5SDimitry Andric // symbols, or any other flag bits???
33090b57cec5SDimitry Andric return eStrataUser;
33100b57cec5SDimitry Andric
33110b57cec5SDimitry Andric case llvm::ELF::ET_DYN:
33120b57cec5SDimitry Andric // 3 - Shared object file
33130b57cec5SDimitry Andric // TODO: is there any way to detect that an shared library is a kernel
33140b57cec5SDimitry Andric // related executable by inspecting the program headers, section headers,
33150b57cec5SDimitry Andric // symbols, or any other flag bits???
33160b57cec5SDimitry Andric return eStrataUnknown;
33170b57cec5SDimitry Andric
33180b57cec5SDimitry Andric case ET_CORE:
33190b57cec5SDimitry Andric // 4 - Core file
33200b57cec5SDimitry Andric // TODO: is there any way to detect that an core file is a kernel
33210b57cec5SDimitry Andric // related executable by inspecting the program headers, section headers,
33220b57cec5SDimitry Andric // symbols, or any other flag bits???
33230b57cec5SDimitry Andric return eStrataUnknown;
33240b57cec5SDimitry Andric
33250b57cec5SDimitry Andric default:
33260b57cec5SDimitry Andric break;
33270b57cec5SDimitry Andric }
33280b57cec5SDimitry Andric return eStrataUnknown;
33290b57cec5SDimitry Andric }
33300b57cec5SDimitry Andric
ReadSectionData(Section * section,lldb::offset_t section_offset,void * dst,size_t dst_len)33310b57cec5SDimitry Andric size_t ObjectFileELF::ReadSectionData(Section *section,
33320b57cec5SDimitry Andric lldb::offset_t section_offset, void *dst,
33330b57cec5SDimitry Andric size_t dst_len) {
33340b57cec5SDimitry Andric // If some other objectfile owns this data, pass this to them.
33350b57cec5SDimitry Andric if (section->GetObjectFile() != this)
33360b57cec5SDimitry Andric return section->GetObjectFile()->ReadSectionData(section, section_offset,
33370b57cec5SDimitry Andric dst, dst_len);
33380b57cec5SDimitry Andric
33390b57cec5SDimitry Andric if (!section->Test(SHF_COMPRESSED))
33400b57cec5SDimitry Andric return ObjectFile::ReadSectionData(section, section_offset, dst, dst_len);
33410b57cec5SDimitry Andric
33420b57cec5SDimitry Andric // For compressed sections we need to read to full data to be able to
33430b57cec5SDimitry Andric // decompress.
33440b57cec5SDimitry Andric DataExtractor data;
33450b57cec5SDimitry Andric ReadSectionData(section, data);
33460b57cec5SDimitry Andric return data.CopyData(section_offset, dst_len, dst);
33470b57cec5SDimitry Andric }
33480b57cec5SDimitry Andric
ReadSectionData(Section * section,DataExtractor & section_data)33490b57cec5SDimitry Andric size_t ObjectFileELF::ReadSectionData(Section *section,
33500b57cec5SDimitry Andric DataExtractor §ion_data) {
33510b57cec5SDimitry Andric // If some other objectfile owns this data, pass this to them.
33520b57cec5SDimitry Andric if (section->GetObjectFile() != this)
33530b57cec5SDimitry Andric return section->GetObjectFile()->ReadSectionData(section, section_data);
33540b57cec5SDimitry Andric
33550b57cec5SDimitry Andric size_t result = ObjectFile::ReadSectionData(section, section_data);
33560b57cec5SDimitry Andric if (result == 0 || !llvm::object::Decompressor::isCompressedELFSection(
33570b57cec5SDimitry Andric section->Get(), section->GetName().GetStringRef()))
33580b57cec5SDimitry Andric return result;
33590b57cec5SDimitry Andric
33600b57cec5SDimitry Andric auto Decompressor = llvm::object::Decompressor::create(
33610b57cec5SDimitry Andric section->GetName().GetStringRef(),
33620b57cec5SDimitry Andric {reinterpret_cast<const char *>(section_data.GetDataStart()),
33630b57cec5SDimitry Andric size_t(section_data.GetByteSize())},
33640b57cec5SDimitry Andric GetByteOrder() == eByteOrderLittle, GetAddressByteSize() == 8);
33650b57cec5SDimitry Andric if (!Decompressor) {
33660b57cec5SDimitry Andric GetModule()->ReportWarning(
33670b57cec5SDimitry Andric "Unable to initialize decompressor for section '%s': %s",
33680b57cec5SDimitry Andric section->GetName().GetCString(),
33690b57cec5SDimitry Andric llvm::toString(Decompressor.takeError()).c_str());
33700b57cec5SDimitry Andric section_data.Clear();
33710b57cec5SDimitry Andric return 0;
33720b57cec5SDimitry Andric }
33730b57cec5SDimitry Andric
33740b57cec5SDimitry Andric auto buffer_sp =
33750b57cec5SDimitry Andric std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0);
33760b57cec5SDimitry Andric if (auto error = Decompressor->decompress(
33770b57cec5SDimitry Andric {reinterpret_cast<char *>(buffer_sp->GetBytes()),
33780b57cec5SDimitry Andric size_t(buffer_sp->GetByteSize())})) {
33790b57cec5SDimitry Andric GetModule()->ReportWarning(
33800b57cec5SDimitry Andric "Decompression of section '%s' failed: %s",
33810b57cec5SDimitry Andric section->GetName().GetCString(),
33820b57cec5SDimitry Andric llvm::toString(std::move(error)).c_str());
33830b57cec5SDimitry Andric section_data.Clear();
33840b57cec5SDimitry Andric return 0;
33850b57cec5SDimitry Andric }
33860b57cec5SDimitry Andric
33870b57cec5SDimitry Andric section_data.SetData(buffer_sp);
33880b57cec5SDimitry Andric return buffer_sp->GetByteSize();
33890b57cec5SDimitry Andric }
33900b57cec5SDimitry Andric
ProgramHeaders()33910b57cec5SDimitry Andric llvm::ArrayRef<ELFProgramHeader> ObjectFileELF::ProgramHeaders() {
33920b57cec5SDimitry Andric ParseProgramHeaders();
33930b57cec5SDimitry Andric return m_program_headers;
33940b57cec5SDimitry Andric }
33950b57cec5SDimitry Andric
GetSegmentData(const ELFProgramHeader & H)33960b57cec5SDimitry Andric DataExtractor ObjectFileELF::GetSegmentData(const ELFProgramHeader &H) {
33970b57cec5SDimitry Andric return DataExtractor(m_data, H.p_offset, H.p_filesz);
33980b57cec5SDimitry Andric }
33990b57cec5SDimitry Andric
AnySegmentHasPhysicalAddress()34000b57cec5SDimitry Andric bool ObjectFileELF::AnySegmentHasPhysicalAddress() {
34010b57cec5SDimitry Andric for (const ELFProgramHeader &H : ProgramHeaders()) {
34020b57cec5SDimitry Andric if (H.p_paddr != 0)
34030b57cec5SDimitry Andric return true;
34040b57cec5SDimitry Andric }
34050b57cec5SDimitry Andric return false;
34060b57cec5SDimitry Andric }
34070b57cec5SDimitry Andric
34080b57cec5SDimitry Andric std::vector<ObjectFile::LoadableData>
GetLoadableData(Target & target)34090b57cec5SDimitry Andric ObjectFileELF::GetLoadableData(Target &target) {
34100b57cec5SDimitry Andric // Create a list of loadable data from loadable segments, using physical
34110b57cec5SDimitry Andric // addresses if they aren't all null
34120b57cec5SDimitry Andric std::vector<LoadableData> loadables;
34130b57cec5SDimitry Andric bool should_use_paddr = AnySegmentHasPhysicalAddress();
34140b57cec5SDimitry Andric for (const ELFProgramHeader &H : ProgramHeaders()) {
34150b57cec5SDimitry Andric LoadableData loadable;
34160b57cec5SDimitry Andric if (H.p_type != llvm::ELF::PT_LOAD)
34170b57cec5SDimitry Andric continue;
34180b57cec5SDimitry Andric loadable.Dest = should_use_paddr ? H.p_paddr : H.p_vaddr;
34190b57cec5SDimitry Andric if (loadable.Dest == LLDB_INVALID_ADDRESS)
34200b57cec5SDimitry Andric continue;
34210b57cec5SDimitry Andric if (H.p_filesz == 0)
34220b57cec5SDimitry Andric continue;
34230b57cec5SDimitry Andric auto segment_data = GetSegmentData(H);
34240b57cec5SDimitry Andric loadable.Contents = llvm::ArrayRef<uint8_t>(segment_data.GetDataStart(),
34250b57cec5SDimitry Andric segment_data.GetByteSize());
34260b57cec5SDimitry Andric loadables.push_back(loadable);
34270b57cec5SDimitry Andric }
34280b57cec5SDimitry Andric return loadables;
34290b57cec5SDimitry Andric }
3430