1 //===-- ObjectFileELF.cpp -------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ObjectFileELF.h"
10 
11 #include <algorithm>
12 #include <cassert>
13 #include <optional>
14 #include <unordered_map>
15 
16 #include "lldb/Core/FileSpecList.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/ModuleSpec.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/Progress.h"
21 #include "lldb/Core/Section.h"
22 #include "lldb/Host/FileSystem.h"
23 #include "lldb/Host/LZMA.h"
24 #include "lldb/Symbol/DWARFCallFrameInfo.h"
25 #include "lldb/Symbol/SymbolContext.h"
26 #include "lldb/Target/SectionLoadList.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Utility/ArchSpec.h"
29 #include "lldb/Utility/DataBufferHeap.h"
30 #include "lldb/Utility/LLDBLog.h"
31 #include "lldb/Utility/Log.h"
32 #include "lldb/Utility/RangeMap.h"
33 #include "lldb/Utility/Status.h"
34 #include "lldb/Utility/Stream.h"
35 #include "lldb/Utility/Timer.h"
36 #include "llvm/ADT/IntervalMap.h"
37 #include "llvm/ADT/PointerUnion.h"
38 #include "llvm/ADT/StringRef.h"
39 #include "llvm/BinaryFormat/ELF.h"
40 #include "llvm/Object/Decompressor.h"
41 #include "llvm/Support/ARMBuildAttributes.h"
42 #include "llvm/Support/CRC.h"
43 #include "llvm/Support/FormatVariadic.h"
44 #include "llvm/Support/MathExtras.h"
45 #include "llvm/Support/MemoryBuffer.h"
46 #include "llvm/Support/MipsABIFlags.h"
47 
48 #define CASE_AND_STREAM(s, def, width)                                         \
49   case def:                                                                    \
50     s->Printf("%-*s", width, #def);                                            \
51     break;
52 
53 using namespace lldb;
54 using namespace lldb_private;
55 using namespace elf;
56 using namespace llvm::ELF;
57 
58 LLDB_PLUGIN_DEFINE(ObjectFileELF)
59 
60 // ELF note owner definitions
61 static const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD";
62 static const char *const LLDB_NT_OWNER_GNU = "GNU";
63 static const char *const LLDB_NT_OWNER_NETBSD = "NetBSD";
64 static const char *const LLDB_NT_OWNER_NETBSDCORE = "NetBSD-CORE";
65 static const char *const LLDB_NT_OWNER_OPENBSD = "OpenBSD";
66 static const char *const LLDB_NT_OWNER_ANDROID = "Android";
67 static const char *const LLDB_NT_OWNER_CORE = "CORE";
68 static const char *const LLDB_NT_OWNER_LINUX = "LINUX";
69 
70 // ELF note type definitions
71 static const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01;
72 static const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4;
73 
74 static const elf_word LLDB_NT_GNU_ABI_TAG = 0x01;
75 static const elf_word LLDB_NT_GNU_ABI_SIZE = 16;
76 
77 static const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03;
78 
79 static const elf_word LLDB_NT_NETBSD_IDENT_TAG = 1;
80 static const elf_word LLDB_NT_NETBSD_IDENT_DESCSZ = 4;
81 static const elf_word LLDB_NT_NETBSD_IDENT_NAMESZ = 7;
82 static const elf_word LLDB_NT_NETBSD_PROCINFO = 1;
83 
84 // GNU ABI note OS constants
85 static const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00;
86 static const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01;
87 static const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02;
88 
89 namespace {
90 
91 //===----------------------------------------------------------------------===//
92 /// \class ELFRelocation
93 /// Generic wrapper for ELFRel and ELFRela.
94 ///
95 /// This helper class allows us to parse both ELFRel and ELFRela relocation
96 /// entries in a generic manner.
97 class ELFRelocation {
98 public:
99   /// Constructs an ELFRelocation entry with a personality as given by @p
100   /// type.
101   ///
102   /// \param type Either DT_REL or DT_RELA.  Any other value is invalid.
103   ELFRelocation(unsigned type);
104 
105   ~ELFRelocation();
106 
107   bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
108 
109   static unsigned RelocType32(const ELFRelocation &rel);
110 
111   static unsigned RelocType64(const ELFRelocation &rel);
112 
113   static unsigned RelocSymbol32(const ELFRelocation &rel);
114 
115   static unsigned RelocSymbol64(const ELFRelocation &rel);
116 
117   static elf_addr RelocOffset32(const ELFRelocation &rel);
118 
119   static elf_addr RelocOffset64(const ELFRelocation &rel);
120 
121   static elf_sxword RelocAddend32(const ELFRelocation &rel);
122 
123   static elf_sxword RelocAddend64(const ELFRelocation &rel);
124 
IsRela()125   bool IsRela() { return (reloc.is<ELFRela *>()); }
126 
127 private:
128   typedef llvm::PointerUnion<ELFRel *, ELFRela *> RelocUnion;
129 
130   RelocUnion reloc;
131 };
132 } // end anonymous namespace
133 
ELFRelocation(unsigned type)134 ELFRelocation::ELFRelocation(unsigned type) {
135   if (type == DT_REL || type == SHT_REL)
136     reloc = new ELFRel();
137   else if (type == DT_RELA || type == SHT_RELA)
138     reloc = new ELFRela();
139   else {
140     assert(false && "unexpected relocation type");
141     reloc = static_cast<ELFRel *>(nullptr);
142   }
143 }
144 
~ELFRelocation()145 ELFRelocation::~ELFRelocation() {
146   if (reloc.is<ELFRel *>())
147     delete reloc.get<ELFRel *>();
148   else
149     delete reloc.get<ELFRela *>();
150 }
151 
Parse(const lldb_private::DataExtractor & data,lldb::offset_t * offset)152 bool ELFRelocation::Parse(const lldb_private::DataExtractor &data,
153                           lldb::offset_t *offset) {
154   if (reloc.is<ELFRel *>())
155     return reloc.get<ELFRel *>()->Parse(data, offset);
156   else
157     return reloc.get<ELFRela *>()->Parse(data, offset);
158 }
159 
RelocType32(const ELFRelocation & rel)160 unsigned ELFRelocation::RelocType32(const ELFRelocation &rel) {
161   if (rel.reloc.is<ELFRel *>())
162     return ELFRel::RelocType32(*rel.reloc.get<ELFRel *>());
163   else
164     return ELFRela::RelocType32(*rel.reloc.get<ELFRela *>());
165 }
166 
RelocType64(const ELFRelocation & rel)167 unsigned ELFRelocation::RelocType64(const ELFRelocation &rel) {
168   if (rel.reloc.is<ELFRel *>())
169     return ELFRel::RelocType64(*rel.reloc.get<ELFRel *>());
170   else
171     return ELFRela::RelocType64(*rel.reloc.get<ELFRela *>());
172 }
173 
RelocSymbol32(const ELFRelocation & rel)174 unsigned ELFRelocation::RelocSymbol32(const ELFRelocation &rel) {
175   if (rel.reloc.is<ELFRel *>())
176     return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel *>());
177   else
178     return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela *>());
179 }
180 
RelocSymbol64(const ELFRelocation & rel)181 unsigned ELFRelocation::RelocSymbol64(const ELFRelocation &rel) {
182   if (rel.reloc.is<ELFRel *>())
183     return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel *>());
184   else
185     return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela *>());
186 }
187 
RelocOffset32(const ELFRelocation & rel)188 elf_addr ELFRelocation::RelocOffset32(const ELFRelocation &rel) {
189   if (rel.reloc.is<ELFRel *>())
190     return rel.reloc.get<ELFRel *>()->r_offset;
191   else
192     return rel.reloc.get<ELFRela *>()->r_offset;
193 }
194 
RelocOffset64(const ELFRelocation & rel)195 elf_addr ELFRelocation::RelocOffset64(const ELFRelocation &rel) {
196   if (rel.reloc.is<ELFRel *>())
197     return rel.reloc.get<ELFRel *>()->r_offset;
198   else
199     return rel.reloc.get<ELFRela *>()->r_offset;
200 }
201 
RelocAddend32(const ELFRelocation & rel)202 elf_sxword ELFRelocation::RelocAddend32(const ELFRelocation &rel) {
203   if (rel.reloc.is<ELFRel *>())
204     return 0;
205   else
206     return rel.reloc.get<ELFRela *>()->r_addend;
207 }
208 
RelocAddend64(const ELFRelocation & rel)209 elf_sxword  ELFRelocation::RelocAddend64(const ELFRelocation &rel) {
210   if (rel.reloc.is<ELFRel *>())
211     return 0;
212   else
213     return rel.reloc.get<ELFRela *>()->r_addend;
214 }
215 
SegmentID(size_t PHdrIndex)216 static user_id_t SegmentID(size_t PHdrIndex) {
217   return ~user_id_t(PHdrIndex);
218 }
219 
Parse(const DataExtractor & data,lldb::offset_t * offset)220 bool ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) {
221   // Read all fields.
222   if (data.GetU32(offset, &n_namesz, 3) == nullptr)
223     return false;
224 
225   // The name field is required to be nul-terminated, and n_namesz includes the
226   // terminating nul in observed implementations (contrary to the ELF-64 spec).
227   // A special case is needed for cores generated by some older Linux versions,
228   // which write a note named "CORE" without a nul terminator and n_namesz = 4.
229   if (n_namesz == 4) {
230     char buf[4];
231     if (data.ExtractBytes(*offset, 4, data.GetByteOrder(), buf) != 4)
232       return false;
233     if (strncmp(buf, "CORE", 4) == 0) {
234       n_name = "CORE";
235       *offset += 4;
236       return true;
237     }
238   }
239 
240   const char *cstr = data.GetCStr(offset, llvm::alignTo(n_namesz, 4));
241   if (cstr == nullptr) {
242     Log *log = GetLog(LLDBLog::Symbols);
243     LLDB_LOGF(log, "Failed to parse note name lacking nul terminator");
244 
245     return false;
246   }
247   n_name = cstr;
248   return true;
249 }
250 
mipsVariantFromElfFlags(const elf::ELFHeader & header)251 static uint32_t mipsVariantFromElfFlags (const elf::ELFHeader &header) {
252   const uint32_t mips_arch = header.e_flags & llvm::ELF::EF_MIPS_ARCH;
253   uint32_t endian = header.e_ident[EI_DATA];
254   uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown;
255   uint32_t fileclass = header.e_ident[EI_CLASS];
256 
257   // If there aren't any elf flags available (e.g core elf file) then return
258   // default
259   // 32 or 64 bit arch (without any architecture revision) based on object file's class.
260   if (header.e_type == ET_CORE) {
261     switch (fileclass) {
262     case llvm::ELF::ELFCLASS32:
263       return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el
264                                      : ArchSpec::eMIPSSubType_mips32;
265     case llvm::ELF::ELFCLASS64:
266       return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el
267                                      : ArchSpec::eMIPSSubType_mips64;
268     default:
269       return arch_variant;
270     }
271   }
272 
273   switch (mips_arch) {
274   case llvm::ELF::EF_MIPS_ARCH_1:
275   case llvm::ELF::EF_MIPS_ARCH_2:
276   case llvm::ELF::EF_MIPS_ARCH_32:
277     return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el
278                                    : ArchSpec::eMIPSSubType_mips32;
279   case llvm::ELF::EF_MIPS_ARCH_32R2:
280     return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el
281                                    : ArchSpec::eMIPSSubType_mips32r2;
282   case llvm::ELF::EF_MIPS_ARCH_32R6:
283     return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el
284                                    : ArchSpec::eMIPSSubType_mips32r6;
285   case llvm::ELF::EF_MIPS_ARCH_3:
286   case llvm::ELF::EF_MIPS_ARCH_4:
287   case llvm::ELF::EF_MIPS_ARCH_5:
288   case llvm::ELF::EF_MIPS_ARCH_64:
289     return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el
290                                    : ArchSpec::eMIPSSubType_mips64;
291   case llvm::ELF::EF_MIPS_ARCH_64R2:
292     return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el
293                                    : ArchSpec::eMIPSSubType_mips64r2;
294   case llvm::ELF::EF_MIPS_ARCH_64R6:
295     return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el
296                                    : ArchSpec::eMIPSSubType_mips64r6;
297   default:
298     break;
299   }
300 
301   return arch_variant;
302 }
303 
riscvVariantFromElfFlags(const elf::ELFHeader & header)304 static uint32_t riscvVariantFromElfFlags(const elf::ELFHeader &header) {
305   uint32_t fileclass = header.e_ident[EI_CLASS];
306   switch (fileclass) {
307   case llvm::ELF::ELFCLASS32:
308     return ArchSpec::eRISCVSubType_riscv32;
309   case llvm::ELF::ELFCLASS64:
310     return ArchSpec::eRISCVSubType_riscv64;
311   default:
312     return ArchSpec::eRISCVSubType_unknown;
313   }
314 }
315 
ppc64VariantFromElfFlags(const elf::ELFHeader & header)316 static uint32_t ppc64VariantFromElfFlags(const elf::ELFHeader &header) {
317   uint32_t endian = header.e_ident[EI_DATA];
318   if (endian == ELFDATA2LSB)
319     return ArchSpec::eCore_ppc64le_generic;
320   else
321     return ArchSpec::eCore_ppc64_generic;
322 }
323 
loongarchVariantFromElfFlags(const elf::ELFHeader & header)324 static uint32_t loongarchVariantFromElfFlags(const elf::ELFHeader &header) {
325   uint32_t fileclass = header.e_ident[EI_CLASS];
326   switch (fileclass) {
327   case llvm::ELF::ELFCLASS32:
328     return ArchSpec::eLoongArchSubType_loongarch32;
329   case llvm::ELF::ELFCLASS64:
330     return ArchSpec::eLoongArchSubType_loongarch64;
331   default:
332     return ArchSpec::eLoongArchSubType_unknown;
333   }
334 }
335 
subTypeFromElfHeader(const elf::ELFHeader & header)336 static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) {
337   if (header.e_machine == llvm::ELF::EM_MIPS)
338     return mipsVariantFromElfFlags(header);
339   else if (header.e_machine == llvm::ELF::EM_PPC64)
340     return ppc64VariantFromElfFlags(header);
341   else if (header.e_machine == llvm::ELF::EM_RISCV)
342     return riscvVariantFromElfFlags(header);
343   else if (header.e_machine == llvm::ELF::EM_LOONGARCH)
344     return loongarchVariantFromElfFlags(header);
345 
346   return LLDB_INVALID_CPUTYPE;
347 }
348 
349 char ObjectFileELF::ID;
350 
351 // Arbitrary constant used as UUID prefix for core files.
352 const uint32_t ObjectFileELF::g_core_uuid_magic(0xE210C);
353 
354 // Static methods.
Initialize()355 void ObjectFileELF::Initialize() {
356   PluginManager::RegisterPlugin(GetPluginNameStatic(),
357                                 GetPluginDescriptionStatic(), CreateInstance,
358                                 CreateMemoryInstance, GetModuleSpecifications);
359 }
360 
Terminate()361 void ObjectFileELF::Terminate() {
362   PluginManager::UnregisterPlugin(CreateInstance);
363 }
364 
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)365 ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp,
366                                           DataBufferSP data_sp,
367                                           lldb::offset_t data_offset,
368                                           const lldb_private::FileSpec *file,
369                                           lldb::offset_t file_offset,
370                                           lldb::offset_t length) {
371   bool mapped_writable = false;
372   if (!data_sp) {
373     data_sp = MapFileDataWritable(*file, length, file_offset);
374     if (!data_sp)
375       return nullptr;
376     data_offset = 0;
377     mapped_writable = true;
378   }
379 
380   assert(data_sp);
381 
382   if (data_sp->GetByteSize() <= (llvm::ELF::EI_NIDENT + data_offset))
383     return nullptr;
384 
385   const uint8_t *magic = data_sp->GetBytes() + data_offset;
386   if (!ELFHeader::MagicBytesMatch(magic))
387     return nullptr;
388 
389   // Update the data to contain the entire file if it doesn't already
390   if (data_sp->GetByteSize() < length) {
391     data_sp = MapFileDataWritable(*file, length, file_offset);
392     if (!data_sp)
393       return nullptr;
394     data_offset = 0;
395     mapped_writable = true;
396     magic = data_sp->GetBytes();
397   }
398 
399   // If we didn't map the data as writable take ownership of the buffer.
400   if (!mapped_writable) {
401     data_sp = std::make_shared<DataBufferHeap>(data_sp->GetBytes(),
402                                                data_sp->GetByteSize());
403     data_offset = 0;
404     magic = data_sp->GetBytes();
405   }
406 
407   unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
408   if (address_size == 4 || address_size == 8) {
409     std::unique_ptr<ObjectFileELF> objfile_up(new ObjectFileELF(
410         module_sp, data_sp, data_offset, file, file_offset, length));
411     ArchSpec spec = objfile_up->GetArchitecture();
412     if (spec && objfile_up->SetModulesArchitecture(spec))
413       return objfile_up.release();
414   }
415 
416   return nullptr;
417 }
418 
CreateMemoryInstance(const lldb::ModuleSP & module_sp,WritableDataBufferSP data_sp,const lldb::ProcessSP & process_sp,lldb::addr_t header_addr)419 ObjectFile *ObjectFileELF::CreateMemoryInstance(
420     const lldb::ModuleSP &module_sp, WritableDataBufferSP data_sp,
421     const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
422   if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT)) {
423     const uint8_t *magic = data_sp->GetBytes();
424     if (ELFHeader::MagicBytesMatch(magic)) {
425       unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
426       if (address_size == 4 || address_size == 8) {
427         std::unique_ptr<ObjectFileELF> objfile_up(
428             new ObjectFileELF(module_sp, data_sp, process_sp, header_addr));
429         ArchSpec spec = objfile_up->GetArchitecture();
430         if (spec && objfile_up->SetModulesArchitecture(spec))
431           return objfile_up.release();
432       }
433     }
434   }
435   return nullptr;
436 }
437 
MagicBytesMatch(DataBufferSP & data_sp,lldb::addr_t data_offset,lldb::addr_t data_length)438 bool ObjectFileELF::MagicBytesMatch(DataBufferSP &data_sp,
439                                     lldb::addr_t data_offset,
440                                     lldb::addr_t data_length) {
441   if (data_sp &&
442       data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) {
443     const uint8_t *magic = data_sp->GetBytes() + data_offset;
444     return ELFHeader::MagicBytesMatch(magic);
445   }
446   return false;
447 }
448 
calc_crc32(uint32_t init,const DataExtractor & data)449 static uint32_t calc_crc32(uint32_t init, const DataExtractor &data) {
450   return llvm::crc32(init,
451                      llvm::ArrayRef(data.GetDataStart(), data.GetByteSize()));
452 }
453 
CalculateELFNotesSegmentsCRC32(const ProgramHeaderColl & program_headers,DataExtractor & object_data)454 uint32_t ObjectFileELF::CalculateELFNotesSegmentsCRC32(
455     const ProgramHeaderColl &program_headers, DataExtractor &object_data) {
456 
457   uint32_t core_notes_crc = 0;
458 
459   for (const ELFProgramHeader &H : program_headers) {
460     if (H.p_type == llvm::ELF::PT_NOTE) {
461       const elf_off ph_offset = H.p_offset;
462       const size_t ph_size = H.p_filesz;
463 
464       DataExtractor segment_data;
465       if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size) {
466         // The ELF program header contained incorrect data, probably corefile
467         // is incomplete or corrupted.
468         break;
469       }
470 
471       core_notes_crc = calc_crc32(core_notes_crc, segment_data);
472     }
473   }
474 
475   return core_notes_crc;
476 }
477 
OSABIAsCString(unsigned char osabi_byte)478 static const char *OSABIAsCString(unsigned char osabi_byte) {
479 #define _MAKE_OSABI_CASE(x)                                                    \
480   case x:                                                                      \
481     return #x
482   switch (osabi_byte) {
483     _MAKE_OSABI_CASE(ELFOSABI_NONE);
484     _MAKE_OSABI_CASE(ELFOSABI_HPUX);
485     _MAKE_OSABI_CASE(ELFOSABI_NETBSD);
486     _MAKE_OSABI_CASE(ELFOSABI_GNU);
487     _MAKE_OSABI_CASE(ELFOSABI_HURD);
488     _MAKE_OSABI_CASE(ELFOSABI_SOLARIS);
489     _MAKE_OSABI_CASE(ELFOSABI_AIX);
490     _MAKE_OSABI_CASE(ELFOSABI_IRIX);
491     _MAKE_OSABI_CASE(ELFOSABI_FREEBSD);
492     _MAKE_OSABI_CASE(ELFOSABI_TRU64);
493     _MAKE_OSABI_CASE(ELFOSABI_MODESTO);
494     _MAKE_OSABI_CASE(ELFOSABI_OPENBSD);
495     _MAKE_OSABI_CASE(ELFOSABI_OPENVMS);
496     _MAKE_OSABI_CASE(ELFOSABI_NSK);
497     _MAKE_OSABI_CASE(ELFOSABI_AROS);
498     _MAKE_OSABI_CASE(ELFOSABI_FENIXOS);
499     _MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI);
500     _MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX);
501     _MAKE_OSABI_CASE(ELFOSABI_ARM);
502     _MAKE_OSABI_CASE(ELFOSABI_STANDALONE);
503   default:
504     return "<unknown-osabi>";
505   }
506 #undef _MAKE_OSABI_CASE
507 }
508 
509 //
510 // WARNING : This function is being deprecated
511 // It's functionality has moved to ArchSpec::SetArchitecture This function is
512 // only being kept to validate the move.
513 //
514 // TODO : Remove this function
GetOsFromOSABI(unsigned char osabi_byte,llvm::Triple::OSType & ostype)515 static bool GetOsFromOSABI(unsigned char osabi_byte,
516                            llvm::Triple::OSType &ostype) {
517   switch (osabi_byte) {
518   case ELFOSABI_AIX:
519     ostype = llvm::Triple::OSType::AIX;
520     break;
521   case ELFOSABI_FREEBSD:
522     ostype = llvm::Triple::OSType::FreeBSD;
523     break;
524   case ELFOSABI_GNU:
525     ostype = llvm::Triple::OSType::Linux;
526     break;
527   case ELFOSABI_NETBSD:
528     ostype = llvm::Triple::OSType::NetBSD;
529     break;
530   case ELFOSABI_OPENBSD:
531     ostype = llvm::Triple::OSType::OpenBSD;
532     break;
533   case ELFOSABI_SOLARIS:
534     ostype = llvm::Triple::OSType::Solaris;
535     break;
536   default:
537     ostype = llvm::Triple::OSType::UnknownOS;
538   }
539   return ostype != llvm::Triple::OSType::UnknownOS;
540 }
541 
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)542 size_t ObjectFileELF::GetModuleSpecifications(
543     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
544     lldb::offset_t data_offset, lldb::offset_t file_offset,
545     lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
546   Log *log = GetLog(LLDBLog::Modules);
547 
548   const size_t initial_count = specs.GetSize();
549 
550   if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) {
551     DataExtractor data;
552     data.SetData(data_sp);
553     elf::ELFHeader header;
554     lldb::offset_t header_offset = data_offset;
555     if (header.Parse(data, &header_offset)) {
556       if (data_sp) {
557         ModuleSpec spec(file);
558 
559         const uint32_t sub_type = subTypeFromElfHeader(header);
560         spec.GetArchitecture().SetArchitecture(
561             eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]);
562 
563         if (spec.GetArchitecture().IsValid()) {
564           llvm::Triple::OSType ostype;
565           llvm::Triple::VendorType vendor;
566           llvm::Triple::OSType spec_ostype =
567               spec.GetArchitecture().GetTriple().getOS();
568 
569           LLDB_LOGF(log, "ObjectFileELF::%s file '%s' module OSABI: %s",
570                     __FUNCTION__, file.GetPath().c_str(),
571                     OSABIAsCString(header.e_ident[EI_OSABI]));
572 
573           // SetArchitecture should have set the vendor to unknown
574           vendor = spec.GetArchitecture().GetTriple().getVendor();
575           assert(vendor == llvm::Triple::UnknownVendor);
576           UNUSED_IF_ASSERT_DISABLED(vendor);
577 
578           //
579           // Validate it is ok to remove GetOsFromOSABI
580           GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);
581           assert(spec_ostype == ostype);
582           if (spec_ostype != llvm::Triple::OSType::UnknownOS) {
583             LLDB_LOGF(log,
584                       "ObjectFileELF::%s file '%s' set ELF module OS type "
585                       "from ELF header OSABI.",
586                       __FUNCTION__, file.GetPath().c_str());
587           }
588 
589           if (data_sp->GetByteSize() < length)
590             data_sp = MapFileData(file, -1, file_offset);
591           if (data_sp)
592             data.SetData(data_sp);
593           // In case there is header extension in the section #0, the header we
594           // parsed above could have sentinel values for e_phnum, e_shnum, and
595           // e_shstrndx.  In this case we need to reparse the header with a
596           // bigger data source to get the actual values.
597           if (header.HasHeaderExtension()) {
598             lldb::offset_t header_offset = data_offset;
599             header.Parse(data, &header_offset);
600           }
601 
602           uint32_t gnu_debuglink_crc = 0;
603           std::string gnu_debuglink_file;
604           SectionHeaderColl section_headers;
605           lldb_private::UUID &uuid = spec.GetUUID();
606 
607           GetSectionHeaderInfo(section_headers, data, header, uuid,
608                                gnu_debuglink_file, gnu_debuglink_crc,
609                                spec.GetArchitecture());
610 
611           llvm::Triple &spec_triple = spec.GetArchitecture().GetTriple();
612 
613           LLDB_LOGF(log,
614                     "ObjectFileELF::%s file '%s' module set to triple: %s "
615                     "(architecture %s)",
616                     __FUNCTION__, file.GetPath().c_str(),
617                     spec_triple.getTriple().c_str(),
618                     spec.GetArchitecture().GetArchitectureName());
619 
620           if (!uuid.IsValid()) {
621             uint32_t core_notes_crc = 0;
622 
623             if (!gnu_debuglink_crc) {
624               LLDB_SCOPED_TIMERF(
625                   "Calculating module crc32 %s with size %" PRIu64 " KiB",
626                   file.GetLastPathComponent().AsCString(),
627                   (length - file_offset) / 1024);
628 
629               // For core files - which usually don't happen to have a
630               // gnu_debuglink, and are pretty bulky - calculating whole
631               // contents crc32 would be too much of luxury.  Thus we will need
632               // to fallback to something simpler.
633               if (header.e_type == llvm::ELF::ET_CORE) {
634                 ProgramHeaderColl program_headers;
635                 GetProgramHeaderInfo(program_headers, data, header);
636 
637                 core_notes_crc =
638                     CalculateELFNotesSegmentsCRC32(program_headers, data);
639               } else {
640                 gnu_debuglink_crc = calc_crc32(0, data);
641               }
642             }
643             using u32le = llvm::support::ulittle32_t;
644             if (gnu_debuglink_crc) {
645               // Use 4 bytes of crc from the .gnu_debuglink section.
646               u32le data(gnu_debuglink_crc);
647               uuid = UUID(&data, sizeof(data));
648             } else if (core_notes_crc) {
649               // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make
650               // it look different form .gnu_debuglink crc followed by 4 bytes
651               // of note segments crc.
652               u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};
653               uuid = UUID(data, sizeof(data));
654             }
655           }
656 
657           specs.Append(spec);
658         }
659       }
660     }
661   }
662 
663   return specs.GetSize() - initial_count;
664 }
665 
666 // ObjectFile protocol
667 
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)668 ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,
669                              DataBufferSP data_sp, lldb::offset_t data_offset,
670                              const FileSpec *file, lldb::offset_t file_offset,
671                              lldb::offset_t length)
672     : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset) {
673   if (file)
674     m_file = *file;
675 }
676 
ObjectFileELF(const lldb::ModuleSP & module_sp,DataBufferSP header_data_sp,const lldb::ProcessSP & process_sp,addr_t header_addr)677 ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,
678                              DataBufferSP header_data_sp,
679                              const lldb::ProcessSP &process_sp,
680                              addr_t header_addr)
681     : ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {}
682 
IsExecutable() const683 bool ObjectFileELF::IsExecutable() const {
684   return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0);
685 }
686 
SetLoadAddress(Target & target,lldb::addr_t value,bool value_is_offset)687 bool ObjectFileELF::SetLoadAddress(Target &target, lldb::addr_t value,
688                                    bool value_is_offset) {
689   ModuleSP module_sp = GetModule();
690   if (module_sp) {
691     size_t num_loaded_sections = 0;
692     SectionList *section_list = GetSectionList();
693     if (section_list) {
694       if (!value_is_offset) {
695         addr_t base = GetBaseAddress().GetFileAddress();
696         if (base == LLDB_INVALID_ADDRESS)
697           return false;
698         value -= base;
699       }
700 
701       const size_t num_sections = section_list->GetSize();
702       size_t sect_idx = 0;
703 
704       for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
705         // Iterate through the object file sections to find all of the sections
706         // that have SHF_ALLOC in their flag bits.
707         SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
708         if (section_sp->Test(SHF_ALLOC) ||
709             section_sp->GetType() == eSectionTypeContainer) {
710           lldb::addr_t load_addr = section_sp->GetFileAddress();
711           // We don't want to update the load address of a section with type
712           // eSectionTypeAbsoluteAddress as they already have the absolute load
713           // address already specified
714           if (section_sp->GetType() != eSectionTypeAbsoluteAddress)
715             load_addr += value;
716 
717           // On 32-bit systems the load address have to fit into 4 bytes. The
718           // rest of the bytes are the overflow from the addition.
719           if (GetAddressByteSize() == 4)
720             load_addr &= 0xFFFFFFFF;
721 
722           if (target.GetSectionLoadList().SetSectionLoadAddress(section_sp,
723                                                                 load_addr))
724             ++num_loaded_sections;
725         }
726       }
727       return num_loaded_sections > 0;
728     }
729   }
730   return false;
731 }
732 
GetByteOrder() const733 ByteOrder ObjectFileELF::GetByteOrder() const {
734   if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
735     return eByteOrderBig;
736   if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
737     return eByteOrderLittle;
738   return eByteOrderInvalid;
739 }
740 
GetAddressByteSize() const741 uint32_t ObjectFileELF::GetAddressByteSize() const {
742   return m_data.GetAddressByteSize();
743 }
744 
GetAddressClass(addr_t file_addr)745 AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) {
746   Symtab *symtab = GetSymtab();
747   if (!symtab)
748     return AddressClass::eUnknown;
749 
750   // The address class is determined based on the symtab. Ask it from the
751   // object file what contains the symtab information.
752   ObjectFile *symtab_objfile = symtab->GetObjectFile();
753   if (symtab_objfile != nullptr && symtab_objfile != this)
754     return symtab_objfile->GetAddressClass(file_addr);
755 
756   auto res = ObjectFile::GetAddressClass(file_addr);
757   if (res != AddressClass::eCode)
758     return res;
759 
760   auto ub = m_address_class_map.upper_bound(file_addr);
761   if (ub == m_address_class_map.begin()) {
762     // No entry in the address class map before the address. Return default
763     // address class for an address in a code section.
764     return AddressClass::eCode;
765   }
766 
767   // Move iterator to the address class entry preceding address
768   --ub;
769 
770   return ub->second;
771 }
772 
SectionIndex(const SectionHeaderCollIter & I)773 size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) {
774   return std::distance(m_section_headers.begin(), I);
775 }
776 
SectionIndex(const SectionHeaderCollConstIter & I) const777 size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const {
778   return std::distance(m_section_headers.begin(), I);
779 }
780 
ParseHeader()781 bool ObjectFileELF::ParseHeader() {
782   lldb::offset_t offset = 0;
783   return m_header.Parse(m_data, &offset);
784 }
785 
GetUUID()786 UUID ObjectFileELF::GetUUID() {
787   // Need to parse the section list to get the UUIDs, so make sure that's been
788   // done.
789   if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile)
790     return UUID();
791 
792   if (!m_uuid) {
793     using u32le = llvm::support::ulittle32_t;
794     if (GetType() == ObjectFile::eTypeCoreFile) {
795       uint32_t core_notes_crc = 0;
796 
797       if (!ParseProgramHeaders())
798         return UUID();
799 
800       core_notes_crc =
801           CalculateELFNotesSegmentsCRC32(m_program_headers, m_data);
802 
803       if (core_notes_crc) {
804         // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it
805         // look different form .gnu_debuglink crc - followed by 4 bytes of note
806         // segments crc.
807         u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};
808         m_uuid = UUID(data, sizeof(data));
809       }
810     } else {
811       if (!m_gnu_debuglink_crc)
812         m_gnu_debuglink_crc = calc_crc32(0, m_data);
813       if (m_gnu_debuglink_crc) {
814         // Use 4 bytes of crc from the .gnu_debuglink section.
815         u32le data(m_gnu_debuglink_crc);
816         m_uuid = UUID(&data, sizeof(data));
817       }
818     }
819   }
820 
821   return m_uuid;
822 }
823 
GetDebugLink()824 std::optional<FileSpec> ObjectFileELF::GetDebugLink() {
825   if (m_gnu_debuglink_file.empty())
826     return std::nullopt;
827   return FileSpec(m_gnu_debuglink_file);
828 }
829 
GetDependentModules(FileSpecList & files)830 uint32_t ObjectFileELF::GetDependentModules(FileSpecList &files) {
831   size_t num_modules = ParseDependentModules();
832   uint32_t num_specs = 0;
833 
834   for (unsigned i = 0; i < num_modules; ++i) {
835     if (files.AppendIfUnique(m_filespec_up->GetFileSpecAtIndex(i)))
836       num_specs++;
837   }
838 
839   return num_specs;
840 }
841 
GetImageInfoAddress(Target * target)842 Address ObjectFileELF::GetImageInfoAddress(Target *target) {
843   if (!ParseDynamicSymbols())
844     return Address();
845 
846   SectionList *section_list = GetSectionList();
847   if (!section_list)
848     return Address();
849 
850   // Find the SHT_DYNAMIC (.dynamic) section.
851   SectionSP dynsym_section_sp(
852       section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true));
853   if (!dynsym_section_sp)
854     return Address();
855   assert(dynsym_section_sp->GetObjectFile() == this);
856 
857   user_id_t dynsym_id = dynsym_section_sp->GetID();
858   const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
859   if (!dynsym_hdr)
860     return Address();
861 
862   for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) {
863     ELFDynamic &symbol = m_dynamic_symbols[i];
864 
865     if (symbol.d_tag == DT_DEBUG) {
866       // Compute the offset as the number of previous entries plus the size of
867       // d_tag.
868       addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
869       return Address(dynsym_section_sp, offset);
870     }
871     // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP
872     // exists in non-PIE.
873     else if ((symbol.d_tag == DT_MIPS_RLD_MAP ||
874               symbol.d_tag == DT_MIPS_RLD_MAP_REL) &&
875              target) {
876       addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
877       addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);
878       if (dyn_base == LLDB_INVALID_ADDRESS)
879         return Address();
880 
881       Status error;
882       if (symbol.d_tag == DT_MIPS_RLD_MAP) {
883         // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer.
884         Address addr;
885         if (target->ReadPointerFromMemory(dyn_base + offset, error, addr, true))
886           return addr;
887       }
888       if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) {
889         // DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
890         // relative to the address of the tag.
891         uint64_t rel_offset;
892         rel_offset = target->ReadUnsignedIntegerFromMemory(
893             dyn_base + offset, GetAddressByteSize(), UINT64_MAX, error, true);
894         if (error.Success() && rel_offset != UINT64_MAX) {
895           Address addr;
896           addr_t debug_ptr_address =
897               dyn_base + (offset - GetAddressByteSize()) + rel_offset;
898           addr.SetOffset(debug_ptr_address);
899           return addr;
900         }
901       }
902     }
903   }
904 
905   return Address();
906 }
907 
GetEntryPointAddress()908 lldb_private::Address ObjectFileELF::GetEntryPointAddress() {
909   if (m_entry_point_address.IsValid())
910     return m_entry_point_address;
911 
912   if (!ParseHeader() || !IsExecutable())
913     return m_entry_point_address;
914 
915   SectionList *section_list = GetSectionList();
916   addr_t offset = m_header.e_entry;
917 
918   if (!section_list)
919     m_entry_point_address.SetOffset(offset);
920   else
921     m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
922   return m_entry_point_address;
923 }
924 
GetBaseAddress()925 Address ObjectFileELF::GetBaseAddress() {
926   for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {
927     const ELFProgramHeader &H = EnumPHdr.value();
928     if (H.p_type != PT_LOAD)
929       continue;
930 
931     return Address(
932         GetSectionList()->FindSectionByID(SegmentID(EnumPHdr.index())), 0);
933   }
934   return LLDB_INVALID_ADDRESS;
935 }
936 
937 // ParseDependentModules
ParseDependentModules()938 size_t ObjectFileELF::ParseDependentModules() {
939   if (m_filespec_up)
940     return m_filespec_up->GetSize();
941 
942   m_filespec_up = std::make_unique<FileSpecList>();
943 
944   if (!ParseSectionHeaders())
945     return 0;
946 
947   SectionList *section_list = GetSectionList();
948   if (!section_list)
949     return 0;
950 
951   // Find the SHT_DYNAMIC section.
952   Section *dynsym =
953       section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)
954           .get();
955   if (!dynsym)
956     return 0;
957   assert(dynsym->GetObjectFile() == this);
958 
959   const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex(dynsym->GetID());
960   if (!header)
961     return 0;
962   // sh_link: section header index of string table used by entries in the
963   // section.
964   Section *dynstr = section_list->FindSectionByID(header->sh_link).get();
965   if (!dynstr)
966     return 0;
967 
968   DataExtractor dynsym_data;
969   DataExtractor dynstr_data;
970   if (ReadSectionData(dynsym, dynsym_data) &&
971       ReadSectionData(dynstr, dynstr_data)) {
972     ELFDynamic symbol;
973     const lldb::offset_t section_size = dynsym_data.GetByteSize();
974     lldb::offset_t offset = 0;
975 
976     // The only type of entries we are concerned with are tagged DT_NEEDED,
977     // yielding the name of a required library.
978     while (offset < section_size) {
979       if (!symbol.Parse(dynsym_data, &offset))
980         break;
981 
982       if (symbol.d_tag != DT_NEEDED)
983         continue;
984 
985       uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
986       const char *lib_name = dynstr_data.PeekCStr(str_index);
987       FileSpec file_spec(lib_name);
988       FileSystem::Instance().Resolve(file_spec);
989       m_filespec_up->Append(file_spec);
990     }
991   }
992 
993   return m_filespec_up->GetSize();
994 }
995 
996 // GetProgramHeaderInfo
GetProgramHeaderInfo(ProgramHeaderColl & program_headers,DataExtractor & object_data,const ELFHeader & header)997 size_t ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
998                                            DataExtractor &object_data,
999                                            const ELFHeader &header) {
1000   // We have already parsed the program headers
1001   if (!program_headers.empty())
1002     return program_headers.size();
1003 
1004   // If there are no program headers to read we are done.
1005   if (header.e_phnum == 0)
1006     return 0;
1007 
1008   program_headers.resize(header.e_phnum);
1009   if (program_headers.size() != header.e_phnum)
1010     return 0;
1011 
1012   const size_t ph_size = header.e_phnum * header.e_phentsize;
1013   const elf_off ph_offset = header.e_phoff;
1014   DataExtractor data;
1015   if (data.SetData(object_data, ph_offset, ph_size) != ph_size)
1016     return 0;
1017 
1018   uint32_t idx;
1019   lldb::offset_t offset;
1020   for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) {
1021     if (!program_headers[idx].Parse(data, &offset))
1022       break;
1023   }
1024 
1025   if (idx < program_headers.size())
1026     program_headers.resize(idx);
1027 
1028   return program_headers.size();
1029 }
1030 
1031 // ParseProgramHeaders
ParseProgramHeaders()1032 bool ObjectFileELF::ParseProgramHeaders() {
1033   return GetProgramHeaderInfo(m_program_headers, m_data, m_header) != 0;
1034 }
1035 
1036 lldb_private::Status
RefineModuleDetailsFromNote(lldb_private::DataExtractor & data,lldb_private::ArchSpec & arch_spec,lldb_private::UUID & uuid)1037 ObjectFileELF::RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,
1038                                            lldb_private::ArchSpec &arch_spec,
1039                                            lldb_private::UUID &uuid) {
1040   Log *log = GetLog(LLDBLog::Modules);
1041   Status error;
1042 
1043   lldb::offset_t offset = 0;
1044 
1045   while (true) {
1046     // Parse the note header.  If this fails, bail out.
1047     const lldb::offset_t note_offset = offset;
1048     ELFNote note = ELFNote();
1049     if (!note.Parse(data, &offset)) {
1050       // We're done.
1051       return error;
1052     }
1053 
1054     LLDB_LOGF(log, "ObjectFileELF::%s parsing note name='%s', type=%" PRIu32,
1055               __FUNCTION__, note.n_name.c_str(), note.n_type);
1056 
1057     // Process FreeBSD ELF notes.
1058     if ((note.n_name == LLDB_NT_OWNER_FREEBSD) &&
1059         (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) &&
1060         (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) {
1061       // Pull out the min version info.
1062       uint32_t version_info;
1063       if (data.GetU32(&offset, &version_info, 1) == nullptr) {
1064         error.SetErrorString("failed to read FreeBSD ABI note payload");
1065         return error;
1066       }
1067 
1068       // Convert the version info into a major/minor number.
1069       const uint32_t version_major = version_info / 100000;
1070       const uint32_t version_minor = (version_info / 1000) % 100;
1071 
1072       char os_name[32];
1073       snprintf(os_name, sizeof(os_name), "freebsd%" PRIu32 ".%" PRIu32,
1074                version_major, version_minor);
1075 
1076       // Set the elf OS version to FreeBSD.  Also clear the vendor.
1077       arch_spec.GetTriple().setOSName(os_name);
1078       arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1079 
1080       LLDB_LOGF(log,
1081                 "ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32
1082                 ".%" PRIu32,
1083                 __FUNCTION__, version_major, version_minor,
1084                 static_cast<uint32_t>(version_info % 1000));
1085     }
1086     // Process GNU ELF notes.
1087     else if (note.n_name == LLDB_NT_OWNER_GNU) {
1088       switch (note.n_type) {
1089       case LLDB_NT_GNU_ABI_TAG:
1090         if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) {
1091           // Pull out the min OS version supporting the ABI.
1092           uint32_t version_info[4];
1093           if (data.GetU32(&offset, &version_info[0], note.n_descsz / 4) ==
1094               nullptr) {
1095             error.SetErrorString("failed to read GNU ABI note payload");
1096             return error;
1097           }
1098 
1099           // Set the OS per the OS field.
1100           switch (version_info[0]) {
1101           case LLDB_NT_GNU_ABI_OS_LINUX:
1102             arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1103             arch_spec.GetTriple().setVendor(
1104                 llvm::Triple::VendorType::UnknownVendor);
1105             LLDB_LOGF(log,
1106                       "ObjectFileELF::%s detected Linux, min version %" PRIu32
1107                       ".%" PRIu32 ".%" PRIu32,
1108                       __FUNCTION__, version_info[1], version_info[2],
1109                       version_info[3]);
1110             // FIXME we have the minimal version number, we could be propagating
1111             // that.  version_info[1] = OS Major, version_info[2] = OS Minor,
1112             // version_info[3] = Revision.
1113             break;
1114           case LLDB_NT_GNU_ABI_OS_HURD:
1115             arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS);
1116             arch_spec.GetTriple().setVendor(
1117                 llvm::Triple::VendorType::UnknownVendor);
1118             LLDB_LOGF(log,
1119                       "ObjectFileELF::%s detected Hurd (unsupported), min "
1120                       "version %" PRIu32 ".%" PRIu32 ".%" PRIu32,
1121                       __FUNCTION__, version_info[1], version_info[2],
1122                       version_info[3]);
1123             break;
1124           case LLDB_NT_GNU_ABI_OS_SOLARIS:
1125             arch_spec.GetTriple().setOS(llvm::Triple::OSType::Solaris);
1126             arch_spec.GetTriple().setVendor(
1127                 llvm::Triple::VendorType::UnknownVendor);
1128             LLDB_LOGF(log,
1129                       "ObjectFileELF::%s detected Solaris, min version %" PRIu32
1130                       ".%" PRIu32 ".%" PRIu32,
1131                       __FUNCTION__, version_info[1], version_info[2],
1132                       version_info[3]);
1133             break;
1134           default:
1135             LLDB_LOGF(log,
1136                       "ObjectFileELF::%s unrecognized OS in note, id %" PRIu32
1137                       ", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32,
1138                       __FUNCTION__, version_info[0], version_info[1],
1139                       version_info[2], version_info[3]);
1140             break;
1141           }
1142         }
1143         break;
1144 
1145       case LLDB_NT_GNU_BUILD_ID_TAG:
1146         // Only bother processing this if we don't already have the uuid set.
1147         if (!uuid.IsValid()) {
1148           // 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a
1149           // build-id of a different length. Accept it as long as it's at least
1150           // 4 bytes as it will be better than our own crc32.
1151           if (note.n_descsz >= 4) {
1152             if (const uint8_t *buf = data.PeekData(offset, note.n_descsz)) {
1153               // Save the build id as the UUID for the module.
1154               uuid = UUID(buf, note.n_descsz);
1155             } else {
1156               error.SetErrorString("failed to read GNU_BUILD_ID note payload");
1157               return error;
1158             }
1159           }
1160         }
1161         break;
1162       }
1163       if (arch_spec.IsMIPS() &&
1164           arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS)
1165         // The note.n_name == LLDB_NT_OWNER_GNU is valid for Linux platform
1166         arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1167     }
1168     // Process NetBSD ELF executables and shared libraries
1169     else if ((note.n_name == LLDB_NT_OWNER_NETBSD) &&
1170              (note.n_type == LLDB_NT_NETBSD_IDENT_TAG) &&
1171              (note.n_descsz == LLDB_NT_NETBSD_IDENT_DESCSZ) &&
1172              (note.n_namesz == LLDB_NT_NETBSD_IDENT_NAMESZ)) {
1173       // Pull out the version info.
1174       uint32_t version_info;
1175       if (data.GetU32(&offset, &version_info, 1) == nullptr) {
1176         error.SetErrorString("failed to read NetBSD ABI note payload");
1177         return error;
1178       }
1179       // Convert the version info into a major/minor/patch number.
1180       //     #define __NetBSD_Version__ MMmmrrpp00
1181       //
1182       //     M = major version
1183       //     m = minor version; a minor number of 99 indicates current.
1184       //     r = 0 (since NetBSD 3.0 not used)
1185       //     p = patchlevel
1186       const uint32_t version_major = version_info / 100000000;
1187       const uint32_t version_minor = (version_info % 100000000) / 1000000;
1188       const uint32_t version_patch = (version_info % 10000) / 100;
1189       // Set the elf OS version to NetBSD.  Also clear the vendor.
1190       arch_spec.GetTriple().setOSName(
1191           llvm::formatv("netbsd{0}.{1}.{2}", version_major, version_minor,
1192                         version_patch).str());
1193       arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1194     }
1195     // Process NetBSD ELF core(5) notes
1196     else if ((note.n_name == LLDB_NT_OWNER_NETBSDCORE) &&
1197              (note.n_type == LLDB_NT_NETBSD_PROCINFO)) {
1198       // Set the elf OS version to NetBSD.  Also clear the vendor.
1199       arch_spec.GetTriple().setOS(llvm::Triple::OSType::NetBSD);
1200       arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1201     }
1202     // Process OpenBSD ELF notes.
1203     else if (note.n_name == LLDB_NT_OWNER_OPENBSD) {
1204       // Set the elf OS version to OpenBSD.  Also clear the vendor.
1205       arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD);
1206       arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1207     } else if (note.n_name == LLDB_NT_OWNER_ANDROID) {
1208       arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1209       arch_spec.GetTriple().setEnvironment(
1210           llvm::Triple::EnvironmentType::Android);
1211     } else if (note.n_name == LLDB_NT_OWNER_LINUX) {
1212       // This is sometimes found in core files and usually contains extended
1213       // register info
1214       arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1215     } else if (note.n_name == LLDB_NT_OWNER_CORE) {
1216       // Parse the NT_FILE to look for stuff in paths to shared libraries
1217       // The contents look like this in a 64 bit ELF core file:
1218       //
1219       // count     = 0x000000000000000a (10)
1220       // page_size = 0x0000000000001000 (4096)
1221       // Index start              end                file_ofs           path
1222       // ===== ------------------ ------------------ ------------------ -------------------------------------
1223       // [  0] 0x0000000000401000 0x0000000000000000                    /tmp/a.out
1224       // [  1] 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out
1225       // [  2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out
1226       // [  3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 /lib/x86_64-linux-gnu/libc-2.19.so
1227       // [  4] 0x00007fa79cba8000 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux-gnu/libc-2.19.so
1228       // [  5] 0x00007fa79cda7000 0x00007fa79cdab000 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so
1229       // [  6] 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64-linux-gnu/libc-2.19.so
1230       // [  7] 0x00007fa79cdb2000 0x00007fa79cdd5000 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so
1231       // [  8] 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64-linux-gnu/ld-2.19.so
1232       // [  9] 0x00007fa79cfd5000 0x00007fa79cfd6000 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so
1233       //
1234       // In the 32 bit ELFs the count, page_size, start, end, file_ofs are
1235       // uint32_t.
1236       //
1237       // For reference: see readelf source code (in binutils).
1238       if (note.n_type == NT_FILE) {
1239         uint64_t count = data.GetAddress(&offset);
1240         const char *cstr;
1241         data.GetAddress(&offset); // Skip page size
1242         offset += count * 3 *
1243                   data.GetAddressByteSize(); // Skip all start/end/file_ofs
1244         for (size_t i = 0; i < count; ++i) {
1245           cstr = data.GetCStr(&offset);
1246           if (cstr == nullptr) {
1247             error.SetErrorStringWithFormat("ObjectFileELF::%s trying to read "
1248                                            "at an offset after the end "
1249                                            "(GetCStr returned nullptr)",
1250                                            __FUNCTION__);
1251             return error;
1252           }
1253           llvm::StringRef path(cstr);
1254           if (path.contains("/lib/x86_64-linux-gnu") || path.contains("/lib/i386-linux-gnu")) {
1255             arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1256             break;
1257           }
1258         }
1259         if (arch_spec.IsMIPS() &&
1260             arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS)
1261           // In case of MIPSR6, the LLDB_NT_OWNER_GNU note is missing for some
1262           // cases (e.g. compile with -nostdlib) Hence set OS to Linux
1263           arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1264       }
1265     }
1266 
1267     // Calculate the offset of the next note just in case "offset" has been
1268     // used to poke at the contents of the note data
1269     offset = note_offset + note.GetByteSize();
1270   }
1271 
1272   return error;
1273 }
1274 
ParseARMAttributes(DataExtractor & data,uint64_t length,ArchSpec & arch_spec)1275 void ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length,
1276                                        ArchSpec &arch_spec) {
1277   lldb::offset_t Offset = 0;
1278 
1279   uint8_t FormatVersion = data.GetU8(&Offset);
1280   if (FormatVersion != llvm::ELFAttrs::Format_Version)
1281     return;
1282 
1283   Offset = Offset + sizeof(uint32_t); // Section Length
1284   llvm::StringRef VendorName = data.GetCStr(&Offset);
1285 
1286   if (VendorName != "aeabi")
1287     return;
1288 
1289   if (arch_spec.GetTriple().getEnvironment() ==
1290       llvm::Triple::UnknownEnvironment)
1291     arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);
1292 
1293   while (Offset < length) {
1294     uint8_t Tag = data.GetU8(&Offset);
1295     uint32_t Size = data.GetU32(&Offset);
1296 
1297     if (Tag != llvm::ARMBuildAttrs::File || Size == 0)
1298       continue;
1299 
1300     while (Offset < length) {
1301       uint64_t Tag = data.GetULEB128(&Offset);
1302       switch (Tag) {
1303       default:
1304         if (Tag < 32)
1305           data.GetULEB128(&Offset);
1306         else if (Tag % 2 == 0)
1307           data.GetULEB128(&Offset);
1308         else
1309           data.GetCStr(&Offset);
1310 
1311         break;
1312 
1313       case llvm::ARMBuildAttrs::CPU_raw_name:
1314       case llvm::ARMBuildAttrs::CPU_name:
1315         data.GetCStr(&Offset);
1316 
1317         break;
1318 
1319       case llvm::ARMBuildAttrs::ABI_VFP_args: {
1320         uint64_t VFPArgs = data.GetULEB128(&Offset);
1321 
1322         if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS) {
1323           if (arch_spec.GetTriple().getEnvironment() ==
1324                   llvm::Triple::UnknownEnvironment ||
1325               arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF)
1326             arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);
1327 
1328           arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);
1329         } else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS) {
1330           if (arch_spec.GetTriple().getEnvironment() ==
1331                   llvm::Triple::UnknownEnvironment ||
1332               arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI)
1333             arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF);
1334 
1335           arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);
1336         }
1337 
1338         break;
1339       }
1340       }
1341     }
1342   }
1343 }
1344 
1345 // 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)1346 size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl &section_headers,
1347                                            DataExtractor &object_data,
1348                                            const elf::ELFHeader &header,
1349                                            lldb_private::UUID &uuid,
1350                                            std::string &gnu_debuglink_file,
1351                                            uint32_t &gnu_debuglink_crc,
1352                                            ArchSpec &arch_spec) {
1353   // Don't reparse the section headers if we already did that.
1354   if (!section_headers.empty())
1355     return section_headers.size();
1356 
1357   // Only initialize the arch_spec to okay defaults if they're not already set.
1358   // We'll refine this with note data as we parse the notes.
1359   if (arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) {
1360     llvm::Triple::OSType ostype;
1361     llvm::Triple::OSType spec_ostype;
1362     const uint32_t sub_type = subTypeFromElfHeader(header);
1363     arch_spec.SetArchitecture(eArchTypeELF, header.e_machine, sub_type,
1364                               header.e_ident[EI_OSABI]);
1365 
1366     // Validate if it is ok to remove GetOsFromOSABI. Note, that now the OS is
1367     // determined based on EI_OSABI flag and the info extracted from ELF notes
1368     // (see RefineModuleDetailsFromNote). However in some cases that still
1369     // might be not enough: for example a shared library might not have any
1370     // notes at all and have EI_OSABI flag set to System V, as result the OS
1371     // will be set to UnknownOS.
1372     GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);
1373     spec_ostype = arch_spec.GetTriple().getOS();
1374     assert(spec_ostype == ostype);
1375     UNUSED_IF_ASSERT_DISABLED(spec_ostype);
1376   }
1377 
1378   if (arch_spec.GetMachine() == llvm::Triple::mips ||
1379       arch_spec.GetMachine() == llvm::Triple::mipsel ||
1380       arch_spec.GetMachine() == llvm::Triple::mips64 ||
1381       arch_spec.GetMachine() == llvm::Triple::mips64el) {
1382     switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) {
1383     case llvm::ELF::EF_MIPS_MICROMIPS:
1384       arch_spec.SetFlags(ArchSpec::eMIPSAse_micromips);
1385       break;
1386     case llvm::ELF::EF_MIPS_ARCH_ASE_M16:
1387       arch_spec.SetFlags(ArchSpec::eMIPSAse_mips16);
1388       break;
1389     case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX:
1390       arch_spec.SetFlags(ArchSpec::eMIPSAse_mdmx);
1391       break;
1392     default:
1393       break;
1394     }
1395   }
1396 
1397   if (arch_spec.GetMachine() == llvm::Triple::arm ||
1398       arch_spec.GetMachine() == llvm::Triple::thumb) {
1399     if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT)
1400       arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);
1401     else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT)
1402       arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);
1403   }
1404 
1405   if (arch_spec.GetMachine() == llvm::Triple::riscv32 ||
1406       arch_spec.GetMachine() == llvm::Triple::riscv64) {
1407     uint32_t flags = arch_spec.GetFlags();
1408 
1409     if (header.e_flags & llvm::ELF::EF_RISCV_RVC)
1410       flags |= ArchSpec::eRISCV_rvc;
1411     if (header.e_flags & llvm::ELF::EF_RISCV_RVE)
1412       flags |= ArchSpec::eRISCV_rve;
1413 
1414     if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE) ==
1415         llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE)
1416       flags |= ArchSpec::eRISCV_float_abi_single;
1417     else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE) ==
1418              llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE)
1419       flags |= ArchSpec::eRISCV_float_abi_double;
1420     else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD) ==
1421              llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD)
1422       flags |= ArchSpec::eRISCV_float_abi_quad;
1423 
1424     arch_spec.SetFlags(flags);
1425   }
1426 
1427   // If there are no section headers we are done.
1428   if (header.e_shnum == 0)
1429     return 0;
1430 
1431   Log *log = GetLog(LLDBLog::Modules);
1432 
1433   section_headers.resize(header.e_shnum);
1434   if (section_headers.size() != header.e_shnum)
1435     return 0;
1436 
1437   const size_t sh_size = header.e_shnum * header.e_shentsize;
1438   const elf_off sh_offset = header.e_shoff;
1439   DataExtractor sh_data;
1440   if (sh_data.SetData(object_data, sh_offset, sh_size) != sh_size)
1441     return 0;
1442 
1443   uint32_t idx;
1444   lldb::offset_t offset;
1445   for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) {
1446     if (!section_headers[idx].Parse(sh_data, &offset))
1447       break;
1448   }
1449   if (idx < section_headers.size())
1450     section_headers.resize(idx);
1451 
1452   const unsigned strtab_idx = header.e_shstrndx;
1453   if (strtab_idx && strtab_idx < section_headers.size()) {
1454     const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx];
1455     const size_t byte_size = sheader.sh_size;
1456     const Elf64_Off offset = sheader.sh_offset;
1457     lldb_private::DataExtractor shstr_data;
1458 
1459     if (shstr_data.SetData(object_data, offset, byte_size) == byte_size) {
1460       for (SectionHeaderCollIter I = section_headers.begin();
1461            I != section_headers.end(); ++I) {
1462         static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink");
1463         const ELFSectionHeaderInfo &sheader = *I;
1464         const uint64_t section_size =
1465             sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size;
1466         ConstString name(shstr_data.PeekCStr(I->sh_name));
1467 
1468         I->section_name = name;
1469 
1470         if (arch_spec.IsMIPS()) {
1471           uint32_t arch_flags = arch_spec.GetFlags();
1472           DataExtractor data;
1473           if (sheader.sh_type == SHT_MIPS_ABIFLAGS) {
1474 
1475             if (section_size && (data.SetData(object_data, sheader.sh_offset,
1476                                               section_size) == section_size)) {
1477               // MIPS ASE Mask is at offset 12 in MIPS.abiflags section
1478               lldb::offset_t offset = 12; // MIPS ABI Flags Version: 0
1479               arch_flags |= data.GetU32(&offset);
1480 
1481               // The floating point ABI is at offset 7
1482               offset = 7;
1483               switch (data.GetU8(&offset)) {
1484               case llvm::Mips::Val_GNU_MIPS_ABI_FP_ANY:
1485                 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_ANY;
1486                 break;
1487               case llvm::Mips::Val_GNU_MIPS_ABI_FP_DOUBLE:
1488                 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_DOUBLE;
1489                 break;
1490               case llvm::Mips::Val_GNU_MIPS_ABI_FP_SINGLE:
1491                 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SINGLE;
1492                 break;
1493               case llvm::Mips::Val_GNU_MIPS_ABI_FP_SOFT:
1494                 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT;
1495                 break;
1496               case llvm::Mips::Val_GNU_MIPS_ABI_FP_OLD_64:
1497                 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_OLD_64;
1498                 break;
1499               case llvm::Mips::Val_GNU_MIPS_ABI_FP_XX:
1500                 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_XX;
1501                 break;
1502               case llvm::Mips::Val_GNU_MIPS_ABI_FP_64:
1503                 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64;
1504                 break;
1505               case llvm::Mips::Val_GNU_MIPS_ABI_FP_64A:
1506                 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64A;
1507                 break;
1508               }
1509             }
1510           }
1511           // Settings appropriate ArchSpec ABI Flags
1512           switch (header.e_flags & llvm::ELF::EF_MIPS_ABI) {
1513           case llvm::ELF::EF_MIPS_ABI_O32:
1514             arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32;
1515             break;
1516           case EF_MIPS_ABI_O64:
1517             arch_flags |= lldb_private::ArchSpec::eMIPSABI_O64;
1518             break;
1519           case EF_MIPS_ABI_EABI32:
1520             arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI32;
1521             break;
1522           case EF_MIPS_ABI_EABI64:
1523             arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI64;
1524             break;
1525           default:
1526             // ABI Mask doesn't cover N32 and N64 ABI.
1527             if (header.e_ident[EI_CLASS] == llvm::ELF::ELFCLASS64)
1528               arch_flags |= lldb_private::ArchSpec::eMIPSABI_N64;
1529             else if (header.e_flags & llvm::ELF::EF_MIPS_ABI2)
1530               arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32;
1531             break;
1532           }
1533           arch_spec.SetFlags(arch_flags);
1534         }
1535 
1536         if (arch_spec.GetMachine() == llvm::Triple::arm ||
1537             arch_spec.GetMachine() == llvm::Triple::thumb) {
1538           DataExtractor data;
1539 
1540           if (sheader.sh_type == SHT_ARM_ATTRIBUTES && section_size != 0 &&
1541               data.SetData(object_data, sheader.sh_offset, section_size) == section_size)
1542             ParseARMAttributes(data, section_size, arch_spec);
1543         }
1544 
1545         if (name == g_sect_name_gnu_debuglink) {
1546           DataExtractor data;
1547           if (section_size && (data.SetData(object_data, sheader.sh_offset,
1548                                             section_size) == section_size)) {
1549             lldb::offset_t gnu_debuglink_offset = 0;
1550             gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset);
1551             gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4);
1552             data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
1553           }
1554         }
1555 
1556         // Process ELF note section entries.
1557         bool is_note_header = (sheader.sh_type == SHT_NOTE);
1558 
1559         // The section header ".note.android.ident" is stored as a
1560         // PROGBITS type header but it is actually a note header.
1561         static ConstString g_sect_name_android_ident(".note.android.ident");
1562         if (!is_note_header && name == g_sect_name_android_ident)
1563           is_note_header = true;
1564 
1565         if (is_note_header) {
1566           // Allow notes to refine module info.
1567           DataExtractor data;
1568           if (section_size && (data.SetData(object_data, sheader.sh_offset,
1569                                             section_size) == section_size)) {
1570             Status error = RefineModuleDetailsFromNote(data, arch_spec, uuid);
1571             if (error.Fail()) {
1572               LLDB_LOGF(log, "ObjectFileELF::%s ELF note processing failed: %s",
1573                         __FUNCTION__, error.AsCString());
1574             }
1575           }
1576         }
1577       }
1578 
1579       // Make any unknown triple components to be unspecified unknowns.
1580       if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
1581         arch_spec.GetTriple().setVendorName(llvm::StringRef());
1582       if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS)
1583         arch_spec.GetTriple().setOSName(llvm::StringRef());
1584 
1585       return section_headers.size();
1586     }
1587   }
1588 
1589   section_headers.clear();
1590   return 0;
1591 }
1592 
1593 llvm::StringRef
StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const1594 ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const {
1595   size_t pos = symbol_name.find('@');
1596   return symbol_name.substr(0, pos);
1597 }
1598 
1599 // ParseSectionHeaders
ParseSectionHeaders()1600 size_t ObjectFileELF::ParseSectionHeaders() {
1601   return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid,
1602                               m_gnu_debuglink_file, m_gnu_debuglink_crc,
1603                               m_arch_spec);
1604 }
1605 
1606 const ObjectFileELF::ELFSectionHeaderInfo *
GetSectionHeaderByIndex(lldb::user_id_t id)1607 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) {
1608   if (!ParseSectionHeaders())
1609     return nullptr;
1610 
1611   if (id < m_section_headers.size())
1612     return &m_section_headers[id];
1613 
1614   return nullptr;
1615 }
1616 
GetSectionIndexByName(const char * name)1617 lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) {
1618   if (!name || !name[0] || !ParseSectionHeaders())
1619     return 0;
1620   for (size_t i = 1; i < m_section_headers.size(); ++i)
1621     if (m_section_headers[i].section_name == ConstString(name))
1622       return i;
1623   return 0;
1624 }
1625 
GetSectionTypeFromName(llvm::StringRef Name)1626 static SectionType GetSectionTypeFromName(llvm::StringRef Name) {
1627   if (Name.consume_front(".debug_")) {
1628     return llvm::StringSwitch<SectionType>(Name)
1629         .Case("abbrev", eSectionTypeDWARFDebugAbbrev)
1630         .Case("abbrev.dwo", eSectionTypeDWARFDebugAbbrevDwo)
1631         .Case("addr", eSectionTypeDWARFDebugAddr)
1632         .Case("aranges", eSectionTypeDWARFDebugAranges)
1633         .Case("cu_index", eSectionTypeDWARFDebugCuIndex)
1634         .Case("frame", eSectionTypeDWARFDebugFrame)
1635         .Case("info", eSectionTypeDWARFDebugInfo)
1636         .Case("info.dwo", eSectionTypeDWARFDebugInfoDwo)
1637         .Cases("line", "line.dwo", eSectionTypeDWARFDebugLine)
1638         .Cases("line_str", "line_str.dwo", eSectionTypeDWARFDebugLineStr)
1639         .Case("loc", eSectionTypeDWARFDebugLoc)
1640         .Case("loc.dwo", eSectionTypeDWARFDebugLocDwo)
1641         .Case("loclists", eSectionTypeDWARFDebugLocLists)
1642         .Case("loclists.dwo", eSectionTypeDWARFDebugLocListsDwo)
1643         .Case("macinfo", eSectionTypeDWARFDebugMacInfo)
1644         .Cases("macro", "macro.dwo", eSectionTypeDWARFDebugMacro)
1645         .Case("names", eSectionTypeDWARFDebugNames)
1646         .Case("pubnames", eSectionTypeDWARFDebugPubNames)
1647         .Case("pubtypes", eSectionTypeDWARFDebugPubTypes)
1648         .Case("ranges", eSectionTypeDWARFDebugRanges)
1649         .Case("rnglists", eSectionTypeDWARFDebugRngLists)
1650         .Case("rnglists.dwo", eSectionTypeDWARFDebugRngListsDwo)
1651         .Case("str", eSectionTypeDWARFDebugStr)
1652         .Case("str.dwo", eSectionTypeDWARFDebugStrDwo)
1653         .Case("str_offsets", eSectionTypeDWARFDebugStrOffsets)
1654         .Case("str_offsets.dwo", eSectionTypeDWARFDebugStrOffsetsDwo)
1655         .Case("tu_index", eSectionTypeDWARFDebugTuIndex)
1656         .Case("types", eSectionTypeDWARFDebugTypes)
1657         .Case("types.dwo", eSectionTypeDWARFDebugTypesDwo)
1658         .Default(eSectionTypeOther);
1659   }
1660   return llvm::StringSwitch<SectionType>(Name)
1661       .Case(".ARM.exidx", eSectionTypeARMexidx)
1662       .Case(".ARM.extab", eSectionTypeARMextab)
1663       .Cases(".bss", ".tbss", eSectionTypeZeroFill)
1664       .Cases(".data", ".tdata", eSectionTypeData)
1665       .Case(".eh_frame", eSectionTypeEHFrame)
1666       .Case(".gnu_debugaltlink", eSectionTypeDWARFGNUDebugAltLink)
1667       .Case(".gosymtab", eSectionTypeGoSymtab)
1668       .Case(".text", eSectionTypeCode)
1669       .Default(eSectionTypeOther);
1670 }
1671 
GetSectionType(const ELFSectionHeaderInfo & H) const1672 SectionType ObjectFileELF::GetSectionType(const ELFSectionHeaderInfo &H) const {
1673   switch (H.sh_type) {
1674   case SHT_PROGBITS:
1675     if (H.sh_flags & SHF_EXECINSTR)
1676       return eSectionTypeCode;
1677     break;
1678   case SHT_SYMTAB:
1679     return eSectionTypeELFSymbolTable;
1680   case SHT_DYNSYM:
1681     return eSectionTypeELFDynamicSymbols;
1682   case SHT_RELA:
1683   case SHT_REL:
1684     return eSectionTypeELFRelocationEntries;
1685   case SHT_DYNAMIC:
1686     return eSectionTypeELFDynamicLinkInfo;
1687   }
1688   return GetSectionTypeFromName(H.section_name.GetStringRef());
1689 }
1690 
GetTargetByteSize(SectionType Type,const ArchSpec & arch)1691 static uint32_t GetTargetByteSize(SectionType Type, const ArchSpec &arch) {
1692   switch (Type) {
1693   case eSectionTypeData:
1694   case eSectionTypeZeroFill:
1695     return arch.GetDataByteSize();
1696   case eSectionTypeCode:
1697     return arch.GetCodeByteSize();
1698   default:
1699     return 1;
1700   }
1701 }
1702 
GetPermissions(const ELFSectionHeader & H)1703 static Permissions GetPermissions(const ELFSectionHeader &H) {
1704   Permissions Perm = Permissions(0);
1705   if (H.sh_flags & SHF_ALLOC)
1706     Perm |= ePermissionsReadable;
1707   if (H.sh_flags & SHF_WRITE)
1708     Perm |= ePermissionsWritable;
1709   if (H.sh_flags & SHF_EXECINSTR)
1710     Perm |= ePermissionsExecutable;
1711   return Perm;
1712 }
1713 
GetPermissions(const ELFProgramHeader & H)1714 static Permissions GetPermissions(const ELFProgramHeader &H) {
1715   Permissions Perm = Permissions(0);
1716   if (H.p_flags & PF_R)
1717     Perm |= ePermissionsReadable;
1718   if (H.p_flags & PF_W)
1719     Perm |= ePermissionsWritable;
1720   if (H.p_flags & PF_X)
1721     Perm |= ePermissionsExecutable;
1722   return Perm;
1723 }
1724 
1725 namespace {
1726 
1727 using VMRange = lldb_private::Range<addr_t, addr_t>;
1728 
1729 struct SectionAddressInfo {
1730   SectionSP Segment;
1731   VMRange Range;
1732 };
1733 
1734 // (Unlinked) ELF object files usually have 0 for every section address, meaning
1735 // we need to compute synthetic addresses in order for "file addresses" from
1736 // different sections to not overlap. This class handles that logic.
1737 class VMAddressProvider {
1738   using VMMap = llvm::IntervalMap<addr_t, SectionSP, 4,
1739                                        llvm::IntervalMapHalfOpenInfo<addr_t>>;
1740 
1741   ObjectFile::Type ObjectType;
1742   addr_t NextVMAddress = 0;
1743   VMMap::Allocator Alloc;
1744   VMMap Segments{Alloc};
1745   VMMap Sections{Alloc};
1746   lldb_private::Log *Log = GetLog(LLDBLog::Modules);
1747   size_t SegmentCount = 0;
1748   std::string SegmentName;
1749 
GetVMRange(const ELFSectionHeader & H)1750   VMRange GetVMRange(const ELFSectionHeader &H) {
1751     addr_t Address = H.sh_addr;
1752     addr_t Size = H.sh_flags & SHF_ALLOC ? H.sh_size : 0;
1753     if (ObjectType == ObjectFile::Type::eTypeObjectFile && Segments.empty() && (H.sh_flags & SHF_ALLOC)) {
1754       NextVMAddress =
1755           llvm::alignTo(NextVMAddress, std::max<addr_t>(H.sh_addralign, 1));
1756       Address = NextVMAddress;
1757       NextVMAddress += Size;
1758     }
1759     return VMRange(Address, Size);
1760   }
1761 
1762 public:
VMAddressProvider(ObjectFile::Type Type,llvm::StringRef SegmentName)1763   VMAddressProvider(ObjectFile::Type Type, llvm::StringRef SegmentName)
1764       : ObjectType(Type), SegmentName(std::string(SegmentName)) {}
1765 
GetNextSegmentName() const1766   std::string GetNextSegmentName() const {
1767     return llvm::formatv("{0}[{1}]", SegmentName, SegmentCount).str();
1768   }
1769 
GetAddressInfo(const ELFProgramHeader & H)1770   std::optional<VMRange> GetAddressInfo(const ELFProgramHeader &H) {
1771     if (H.p_memsz == 0) {
1772       LLDB_LOG(Log, "Ignoring zero-sized {0} segment. Corrupt object file?",
1773                SegmentName);
1774       return std::nullopt;
1775     }
1776 
1777     if (Segments.overlaps(H.p_vaddr, H.p_vaddr + H.p_memsz)) {
1778       LLDB_LOG(Log, "Ignoring overlapping {0} segment. Corrupt object file?",
1779                SegmentName);
1780       return std::nullopt;
1781     }
1782     return VMRange(H.p_vaddr, H.p_memsz);
1783   }
1784 
GetAddressInfo(const ELFSectionHeader & H)1785   std::optional<SectionAddressInfo> GetAddressInfo(const ELFSectionHeader &H) {
1786     VMRange Range = GetVMRange(H);
1787     SectionSP Segment;
1788     auto It = Segments.find(Range.GetRangeBase());
1789     if ((H.sh_flags & SHF_ALLOC) && It.valid()) {
1790       addr_t MaxSize;
1791       if (It.start() <= Range.GetRangeBase()) {
1792         MaxSize = It.stop() - Range.GetRangeBase();
1793         Segment = *It;
1794       } else
1795         MaxSize = It.start() - Range.GetRangeBase();
1796       if (Range.GetByteSize() > MaxSize) {
1797         LLDB_LOG(Log, "Shortening section crossing segment boundaries. "
1798                       "Corrupt object file?");
1799         Range.SetByteSize(MaxSize);
1800       }
1801     }
1802     if (Range.GetByteSize() > 0 &&
1803         Sections.overlaps(Range.GetRangeBase(), Range.GetRangeEnd())) {
1804       LLDB_LOG(Log, "Ignoring overlapping section. Corrupt object file?");
1805       return std::nullopt;
1806     }
1807     if (Segment)
1808       Range.Slide(-Segment->GetFileAddress());
1809     return SectionAddressInfo{Segment, Range};
1810   }
1811 
AddSegment(const VMRange & Range,SectionSP Seg)1812   void AddSegment(const VMRange &Range, SectionSP Seg) {
1813     Segments.insert(Range.GetRangeBase(), Range.GetRangeEnd(), std::move(Seg));
1814     ++SegmentCount;
1815   }
1816 
AddSection(SectionAddressInfo Info,SectionSP Sect)1817   void AddSection(SectionAddressInfo Info, SectionSP Sect) {
1818     if (Info.Range.GetByteSize() == 0)
1819       return;
1820     if (Info.Segment)
1821       Info.Range.Slide(Info.Segment->GetFileAddress());
1822     Sections.insert(Info.Range.GetRangeBase(), Info.Range.GetRangeEnd(),
1823                     std::move(Sect));
1824   }
1825 };
1826 }
1827 
CreateSections(SectionList & unified_section_list)1828 void ObjectFileELF::CreateSections(SectionList &unified_section_list) {
1829   if (m_sections_up)
1830     return;
1831 
1832   m_sections_up = std::make_unique<SectionList>();
1833   VMAddressProvider regular_provider(GetType(), "PT_LOAD");
1834   VMAddressProvider tls_provider(GetType(), "PT_TLS");
1835 
1836   for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {
1837     const ELFProgramHeader &PHdr = EnumPHdr.value();
1838     if (PHdr.p_type != PT_LOAD && PHdr.p_type != PT_TLS)
1839       continue;
1840 
1841     VMAddressProvider &provider =
1842         PHdr.p_type == PT_TLS ? tls_provider : regular_provider;
1843     auto InfoOr = provider.GetAddressInfo(PHdr);
1844     if (!InfoOr)
1845       continue;
1846 
1847     uint32_t Log2Align = llvm::Log2_64(std::max<elf_xword>(PHdr.p_align, 1));
1848     SectionSP Segment = std::make_shared<Section>(
1849         GetModule(), this, SegmentID(EnumPHdr.index()),
1850         ConstString(provider.GetNextSegmentName()), eSectionTypeContainer,
1851         InfoOr->GetRangeBase(), InfoOr->GetByteSize(), PHdr.p_offset,
1852         PHdr.p_filesz, Log2Align, /*flags*/ 0);
1853     Segment->SetPermissions(GetPermissions(PHdr));
1854     Segment->SetIsThreadSpecific(PHdr.p_type == PT_TLS);
1855     m_sections_up->AddSection(Segment);
1856 
1857     provider.AddSegment(*InfoOr, std::move(Segment));
1858   }
1859 
1860   ParseSectionHeaders();
1861   if (m_section_headers.empty())
1862     return;
1863 
1864   for (SectionHeaderCollIter I = std::next(m_section_headers.begin());
1865        I != m_section_headers.end(); ++I) {
1866     const ELFSectionHeaderInfo &header = *I;
1867 
1868     ConstString &name = I->section_name;
1869     const uint64_t file_size =
1870         header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
1871 
1872     VMAddressProvider &provider =
1873         header.sh_flags & SHF_TLS ? tls_provider : regular_provider;
1874     auto InfoOr = provider.GetAddressInfo(header);
1875     if (!InfoOr)
1876       continue;
1877 
1878     SectionType sect_type = GetSectionType(header);
1879 
1880     const uint32_t target_bytes_size =
1881         GetTargetByteSize(sect_type, m_arch_spec);
1882 
1883     elf::elf_xword log2align =
1884         (header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign);
1885 
1886     SectionSP section_sp(new Section(
1887         InfoOr->Segment, GetModule(), // Module to which this section belongs.
1888         this,            // ObjectFile to which this section belongs and should
1889                          // read section data from.
1890         SectionIndex(I), // Section ID.
1891         name,            // Section name.
1892         sect_type,       // Section type.
1893         InfoOr->Range.GetRangeBase(), // VM address.
1894         InfoOr->Range.GetByteSize(),  // VM size in bytes of this section.
1895         header.sh_offset,             // Offset of this section in the file.
1896         file_size,           // Size of the section as found in the file.
1897         log2align,           // Alignment of the section
1898         header.sh_flags,     // Flags for this section.
1899         target_bytes_size)); // Number of host bytes per target byte
1900 
1901     section_sp->SetPermissions(GetPermissions(header));
1902     section_sp->SetIsThreadSpecific(header.sh_flags & SHF_TLS);
1903     (InfoOr->Segment ? InfoOr->Segment->GetChildren() : *m_sections_up)
1904         .AddSection(section_sp);
1905     provider.AddSection(std::move(*InfoOr), std::move(section_sp));
1906   }
1907 
1908   // For eTypeDebugInfo files, the Symbol Vendor will take care of updating the
1909   // unified section list.
1910   if (GetType() != eTypeDebugInfo)
1911     unified_section_list = *m_sections_up;
1912 
1913   // If there's a .gnu_debugdata section, we'll try to read the .symtab that's
1914   // embedded in there and replace the one in the original object file (if any).
1915   // If there's none in the orignal object file, we add it to it.
1916   if (auto gdd_obj_file = GetGnuDebugDataObjectFile()) {
1917     if (auto gdd_objfile_section_list = gdd_obj_file->GetSectionList()) {
1918       if (SectionSP symtab_section_sp =
1919               gdd_objfile_section_list->FindSectionByType(
1920                   eSectionTypeELFSymbolTable, true)) {
1921         SectionSP module_section_sp = unified_section_list.FindSectionByType(
1922             eSectionTypeELFSymbolTable, true);
1923         if (module_section_sp)
1924           unified_section_list.ReplaceSection(module_section_sp->GetID(),
1925                                               symtab_section_sp);
1926         else
1927           unified_section_list.AddSection(symtab_section_sp);
1928       }
1929     }
1930   }
1931 }
1932 
GetGnuDebugDataObjectFile()1933 std::shared_ptr<ObjectFileELF> ObjectFileELF::GetGnuDebugDataObjectFile() {
1934   if (m_gnu_debug_data_object_file != nullptr)
1935     return m_gnu_debug_data_object_file;
1936 
1937   SectionSP section =
1938       GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"));
1939   if (!section)
1940     return nullptr;
1941 
1942   if (!lldb_private::lzma::isAvailable()) {
1943     GetModule()->ReportWarning(
1944         "No LZMA support found for reading .gnu_debugdata section");
1945     return nullptr;
1946   }
1947 
1948   // Uncompress the data
1949   DataExtractor data;
1950   section->GetSectionData(data);
1951   llvm::SmallVector<uint8_t, 0> uncompressedData;
1952   auto err = lldb_private::lzma::uncompress(data.GetData(), uncompressedData);
1953   if (err) {
1954     GetModule()->ReportWarning(
1955         "An error occurred while decompression the section {0}: {1}",
1956         section->GetName().AsCString(), llvm::toString(std::move(err)).c_str());
1957     return nullptr;
1958   }
1959 
1960   // Construct ObjectFileELF object from decompressed buffer
1961   DataBufferSP gdd_data_buf(
1962       new DataBufferHeap(uncompressedData.data(), uncompressedData.size()));
1963   auto fspec = GetFileSpec().CopyByAppendingPathComponent(
1964       llvm::StringRef("gnu_debugdata"));
1965   m_gnu_debug_data_object_file.reset(new ObjectFileELF(
1966       GetModule(), gdd_data_buf, 0, &fspec, 0, gdd_data_buf->GetByteSize()));
1967 
1968   // This line is essential; otherwise a breakpoint can be set but not hit.
1969   m_gnu_debug_data_object_file->SetType(ObjectFile::eTypeDebugInfo);
1970 
1971   ArchSpec spec = m_gnu_debug_data_object_file->GetArchitecture();
1972   if (spec && m_gnu_debug_data_object_file->SetModulesArchitecture(spec))
1973     return m_gnu_debug_data_object_file;
1974 
1975   return nullptr;
1976 }
1977 
1978 // Find the arm/aarch64 mapping symbol character in the given symbol name.
1979 // Mapping symbols have the form of "$<char>[.<any>]*". Additionally we
1980 // recognize cases when the mapping symbol prefixed by an arbitrary string
1981 // because if a symbol prefix added to each symbol in the object file with
1982 // objcopy then the mapping symbols are also prefixed.
FindArmAarch64MappingSymbol(const char * symbol_name)1983 static char FindArmAarch64MappingSymbol(const char *symbol_name) {
1984   if (!symbol_name)
1985     return '\0';
1986 
1987   const char *dollar_pos = ::strchr(symbol_name, '$');
1988   if (!dollar_pos || dollar_pos[1] == '\0')
1989     return '\0';
1990 
1991   if (dollar_pos[2] == '\0' || dollar_pos[2] == '.')
1992     return dollar_pos[1];
1993   return '\0';
1994 }
1995 
1996 #define STO_MIPS_ISA (3 << 6)
1997 #define STO_MICROMIPS (2 << 6)
1998 #define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS)
1999 
2000 // 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)2001 unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
2002                                      SectionList *section_list,
2003                                      const size_t num_symbols,
2004                                      const DataExtractor &symtab_data,
2005                                      const DataExtractor &strtab_data) {
2006   ELFSymbol symbol;
2007   lldb::offset_t offset = 0;
2008 
2009   static ConstString text_section_name(".text");
2010   static ConstString init_section_name(".init");
2011   static ConstString fini_section_name(".fini");
2012   static ConstString ctors_section_name(".ctors");
2013   static ConstString dtors_section_name(".dtors");
2014 
2015   static ConstString data_section_name(".data");
2016   static ConstString rodata_section_name(".rodata");
2017   static ConstString rodata1_section_name(".rodata1");
2018   static ConstString data2_section_name(".data1");
2019   static ConstString bss_section_name(".bss");
2020   static ConstString opd_section_name(".opd"); // For ppc64
2021 
2022   // On Android the oatdata and the oatexec symbols in the oat and odex files
2023   // covers the full .text section what causes issues with displaying unusable
2024   // symbol name to the user and very slow unwinding speed because the
2025   // instruction emulation based unwind plans try to emulate all instructions
2026   // in these symbols. Don't add these symbols to the symbol list as they have
2027   // no use for the debugger and they are causing a lot of trouble. Filtering
2028   // can't be restricted to Android because this special object file don't
2029   // contain the note section specifying the environment to Android but the
2030   // custom extension and file name makes it highly unlikely that this will
2031   // collide with anything else.
2032   ConstString file_extension = m_file.GetFileNameExtension();
2033   bool skip_oatdata_oatexec =
2034       file_extension == ".oat" || file_extension == ".odex";
2035 
2036   ArchSpec arch = GetArchitecture();
2037   ModuleSP module_sp(GetModule());
2038   SectionList *module_section_list =
2039       module_sp ? module_sp->GetSectionList() : nullptr;
2040 
2041   // Local cache to avoid doing a FindSectionByName for each symbol. The "const
2042   // char*" key must came from a ConstString object so they can be compared by
2043   // pointer
2044   std::unordered_map<const char *, lldb::SectionSP> section_name_to_section;
2045 
2046   unsigned i;
2047   for (i = 0; i < num_symbols; ++i) {
2048     if (!symbol.Parse(symtab_data, &offset))
2049       break;
2050 
2051     const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
2052     if (!symbol_name)
2053       symbol_name = "";
2054 
2055     // No need to add non-section symbols that have no names
2056     if (symbol.getType() != STT_SECTION &&
2057         (symbol_name == nullptr || symbol_name[0] == '\0'))
2058       continue;
2059 
2060     // Skipping oatdata and oatexec sections if it is requested. See details
2061     // above the definition of skip_oatdata_oatexec for the reasons.
2062     if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 ||
2063                                  ::strcmp(symbol_name, "oatexec") == 0))
2064       continue;
2065 
2066     SectionSP symbol_section_sp;
2067     SymbolType symbol_type = eSymbolTypeInvalid;
2068     Elf64_Half shndx = symbol.st_shndx;
2069 
2070     switch (shndx) {
2071     case SHN_ABS:
2072       symbol_type = eSymbolTypeAbsolute;
2073       break;
2074     case SHN_UNDEF:
2075       symbol_type = eSymbolTypeUndefined;
2076       break;
2077     default:
2078       symbol_section_sp = section_list->FindSectionByID(shndx);
2079       break;
2080     }
2081 
2082     // If a symbol is undefined do not process it further even if it has a STT
2083     // type
2084     if (symbol_type != eSymbolTypeUndefined) {
2085       switch (symbol.getType()) {
2086       default:
2087       case STT_NOTYPE:
2088         // The symbol's type is not specified.
2089         break;
2090 
2091       case STT_OBJECT:
2092         // The symbol is associated with a data object, such as a variable, an
2093         // array, etc.
2094         symbol_type = eSymbolTypeData;
2095         break;
2096 
2097       case STT_FUNC:
2098         // The symbol is associated with a function or other executable code.
2099         symbol_type = eSymbolTypeCode;
2100         break;
2101 
2102       case STT_SECTION:
2103         // The symbol is associated with a section. Symbol table entries of
2104         // this type exist primarily for relocation and normally have STB_LOCAL
2105         // binding.
2106         break;
2107 
2108       case STT_FILE:
2109         // Conventionally, the symbol's name gives the name of the source file
2110         // associated with the object file. A file symbol has STB_LOCAL
2111         // binding, its section index is SHN_ABS, and it precedes the other
2112         // STB_LOCAL symbols for the file, if it is present.
2113         symbol_type = eSymbolTypeSourceFile;
2114         break;
2115 
2116       case STT_GNU_IFUNC:
2117         // The symbol is associated with an indirect function. The actual
2118         // function will be resolved if it is referenced.
2119         symbol_type = eSymbolTypeResolver;
2120         break;
2121       }
2122     }
2123 
2124     if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) {
2125       if (symbol_section_sp) {
2126         ConstString sect_name = symbol_section_sp->GetName();
2127         if (sect_name == text_section_name || sect_name == init_section_name ||
2128             sect_name == fini_section_name || sect_name == ctors_section_name ||
2129             sect_name == dtors_section_name) {
2130           symbol_type = eSymbolTypeCode;
2131         } else if (sect_name == data_section_name ||
2132                    sect_name == data2_section_name ||
2133                    sect_name == rodata_section_name ||
2134                    sect_name == rodata1_section_name ||
2135                    sect_name == bss_section_name) {
2136           symbol_type = eSymbolTypeData;
2137         }
2138       }
2139     }
2140 
2141     int64_t symbol_value_offset = 0;
2142     uint32_t additional_flags = 0;
2143 
2144     if (arch.IsValid()) {
2145       if (arch.GetMachine() == llvm::Triple::arm) {
2146         if (symbol.getBinding() == STB_LOCAL) {
2147           char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
2148           if (symbol_type == eSymbolTypeCode) {
2149             switch (mapping_symbol) {
2150             case 'a':
2151               // $a[.<any>]* - marks an ARM instruction sequence
2152               m_address_class_map[symbol.st_value] = AddressClass::eCode;
2153               break;
2154             case 'b':
2155             case 't':
2156               // $b[.<any>]* - marks a THUMB BL instruction sequence
2157               // $t[.<any>]* - marks a THUMB instruction sequence
2158               m_address_class_map[symbol.st_value] =
2159                   AddressClass::eCodeAlternateISA;
2160               break;
2161             case 'd':
2162               // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2163               m_address_class_map[symbol.st_value] = AddressClass::eData;
2164               break;
2165             }
2166           }
2167           if (mapping_symbol)
2168             continue;
2169         }
2170       } else if (arch.GetMachine() == llvm::Triple::aarch64) {
2171         if (symbol.getBinding() == STB_LOCAL) {
2172           char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
2173           if (symbol_type == eSymbolTypeCode) {
2174             switch (mapping_symbol) {
2175             case 'x':
2176               // $x[.<any>]* - marks an A64 instruction sequence
2177               m_address_class_map[symbol.st_value] = AddressClass::eCode;
2178               break;
2179             case 'd':
2180               // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2181               m_address_class_map[symbol.st_value] = AddressClass::eData;
2182               break;
2183             }
2184           }
2185           if (mapping_symbol)
2186             continue;
2187         }
2188       }
2189 
2190       if (arch.GetMachine() == llvm::Triple::arm) {
2191         if (symbol_type == eSymbolTypeCode) {
2192           if (symbol.st_value & 1) {
2193             // Subtracting 1 from the address effectively unsets the low order
2194             // bit, which results in the address actually pointing to the
2195             // beginning of the symbol. This delta will be used below in
2196             // conjunction with symbol.st_value to produce the final
2197             // symbol_value that we store in the symtab.
2198             symbol_value_offset = -1;
2199             m_address_class_map[symbol.st_value ^ 1] =
2200                 AddressClass::eCodeAlternateISA;
2201           } else {
2202             // This address is ARM
2203             m_address_class_map[symbol.st_value] = AddressClass::eCode;
2204           }
2205         }
2206       }
2207 
2208       /*
2209        * MIPS:
2210        * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for
2211        * MIPS).
2212        * This allows processor to switch between microMIPS and MIPS without any
2213        * need
2214        * for special mode-control register. However, apart from .debug_line,
2215        * none of
2216        * the ELF/DWARF sections set the ISA bit (for symbol or section). Use
2217        * st_other
2218        * flag to check whether the symbol is microMIPS and then set the address
2219        * class
2220        * accordingly.
2221       */
2222       if (arch.IsMIPS()) {
2223         if (IS_MICROMIPS(symbol.st_other))
2224           m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
2225         else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) {
2226           symbol.st_value = symbol.st_value & (~1ull);
2227           m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
2228         } else {
2229           if (symbol_type == eSymbolTypeCode)
2230             m_address_class_map[symbol.st_value] = AddressClass::eCode;
2231           else if (symbol_type == eSymbolTypeData)
2232             m_address_class_map[symbol.st_value] = AddressClass::eData;
2233           else
2234             m_address_class_map[symbol.st_value] = AddressClass::eUnknown;
2235         }
2236       }
2237     }
2238 
2239     // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB
2240     // symbols. See above for more details.
2241     uint64_t symbol_value = symbol.st_value + symbol_value_offset;
2242 
2243     if (symbol_section_sp &&
2244         CalculateType() != ObjectFile::Type::eTypeObjectFile)
2245       symbol_value -= symbol_section_sp->GetFileAddress();
2246 
2247     if (symbol_section_sp && module_section_list &&
2248         module_section_list != section_list) {
2249       ConstString sect_name = symbol_section_sp->GetName();
2250       auto section_it = section_name_to_section.find(sect_name.GetCString());
2251       if (section_it == section_name_to_section.end())
2252         section_it =
2253             section_name_to_section
2254                 .emplace(sect_name.GetCString(),
2255                          module_section_list->FindSectionByName(sect_name))
2256                 .first;
2257       if (section_it->second)
2258         symbol_section_sp = section_it->second;
2259     }
2260 
2261     bool is_global = symbol.getBinding() == STB_GLOBAL;
2262     uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags;
2263     llvm::StringRef symbol_ref(symbol_name);
2264 
2265     // Symbol names may contain @VERSION suffixes. Find those and strip them
2266     // temporarily.
2267     size_t version_pos = symbol_ref.find('@');
2268     bool has_suffix = version_pos != llvm::StringRef::npos;
2269     llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos);
2270     Mangled mangled(symbol_bare);
2271 
2272     // Now append the suffix back to mangled and unmangled names. Only do it if
2273     // the demangling was successful (string is not empty).
2274     if (has_suffix) {
2275       llvm::StringRef suffix = symbol_ref.substr(version_pos);
2276 
2277       llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef();
2278       if (!mangled_name.empty())
2279         mangled.SetMangledName(ConstString((mangled_name + suffix).str()));
2280 
2281       ConstString demangled = mangled.GetDemangledName();
2282       llvm::StringRef demangled_name = demangled.GetStringRef();
2283       if (!demangled_name.empty())
2284         mangled.SetDemangledName(ConstString((demangled_name + suffix).str()));
2285     }
2286 
2287     // In ELF all symbol should have a valid size but it is not true for some
2288     // function symbols coming from hand written assembly. As none of the
2289     // function symbol should have 0 size we try to calculate the size for
2290     // these symbols in the symtab with saying that their original size is not
2291     // valid.
2292     bool symbol_size_valid =
2293         symbol.st_size != 0 || symbol.getType() != STT_FUNC;
2294 
2295     Symbol dc_symbol(
2296         i + start_id, // ID is the original symbol table index.
2297         mangled,
2298         symbol_type,                    // Type of this symbol
2299         is_global,                      // Is this globally visible?
2300         false,                          // Is this symbol debug info?
2301         false,                          // Is this symbol a trampoline?
2302         false,                          // Is this symbol artificial?
2303         AddressRange(symbol_section_sp, // Section in which this symbol is
2304                                         // defined or null.
2305                      symbol_value,      // Offset in section or symbol value.
2306                      symbol.st_size),   // Size in bytes of this symbol.
2307         symbol_size_valid,              // Symbol size is valid
2308         has_suffix,                     // Contains linker annotations?
2309         flags);                         // Symbol flags.
2310     if (symbol.getBinding() == STB_WEAK)
2311       dc_symbol.SetIsWeak(true);
2312     symtab->AddSymbol(dc_symbol);
2313   }
2314   return i;
2315 }
2316 
ParseSymbolTable(Symtab * symbol_table,user_id_t start_id,lldb_private::Section * symtab)2317 unsigned ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
2318                                          user_id_t start_id,
2319                                          lldb_private::Section *symtab) {
2320   if (symtab->GetObjectFile() != this) {
2321     // If the symbol table section is owned by a different object file, have it
2322     // do the parsing.
2323     ObjectFileELF *obj_file_elf =
2324         static_cast<ObjectFileELF *>(symtab->GetObjectFile());
2325     return obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab);
2326   }
2327 
2328   // Get section list for this object file.
2329   SectionList *section_list = m_sections_up.get();
2330   if (!section_list)
2331     return 0;
2332 
2333   user_id_t symtab_id = symtab->GetID();
2334   const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
2335   assert(symtab_hdr->sh_type == SHT_SYMTAB ||
2336          symtab_hdr->sh_type == SHT_DYNSYM);
2337 
2338   // sh_link: section header index of associated string table.
2339   user_id_t strtab_id = symtab_hdr->sh_link;
2340   Section *strtab = section_list->FindSectionByID(strtab_id).get();
2341 
2342   if (symtab && strtab) {
2343     assert(symtab->GetObjectFile() == this);
2344     assert(strtab->GetObjectFile() == this);
2345 
2346     DataExtractor symtab_data;
2347     DataExtractor strtab_data;
2348     if (ReadSectionData(symtab, symtab_data) &&
2349         ReadSectionData(strtab, strtab_data)) {
2350       size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
2351 
2352       return ParseSymbols(symbol_table, start_id, section_list, num_symbols,
2353                           symtab_data, strtab_data);
2354     }
2355   }
2356 
2357   return 0;
2358 }
2359 
ParseDynamicSymbols()2360 size_t ObjectFileELF::ParseDynamicSymbols() {
2361   if (m_dynamic_symbols.size())
2362     return m_dynamic_symbols.size();
2363 
2364   SectionList *section_list = GetSectionList();
2365   if (!section_list)
2366     return 0;
2367 
2368   // Find the SHT_DYNAMIC section.
2369   Section *dynsym =
2370       section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)
2371           .get();
2372   if (!dynsym)
2373     return 0;
2374   assert(dynsym->GetObjectFile() == this);
2375 
2376   ELFDynamic symbol;
2377   DataExtractor dynsym_data;
2378   if (ReadSectionData(dynsym, dynsym_data)) {
2379     const lldb::offset_t section_size = dynsym_data.GetByteSize();
2380     lldb::offset_t cursor = 0;
2381 
2382     while (cursor < section_size) {
2383       if (!symbol.Parse(dynsym_data, &cursor))
2384         break;
2385 
2386       m_dynamic_symbols.push_back(symbol);
2387     }
2388   }
2389 
2390   return m_dynamic_symbols.size();
2391 }
2392 
FindDynamicSymbol(unsigned tag)2393 const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) {
2394   if (!ParseDynamicSymbols())
2395     return nullptr;
2396 
2397   DynamicSymbolCollIter I = m_dynamic_symbols.begin();
2398   DynamicSymbolCollIter E = m_dynamic_symbols.end();
2399   for (; I != E; ++I) {
2400     ELFDynamic *symbol = &*I;
2401 
2402     if (symbol->d_tag == tag)
2403       return symbol;
2404   }
2405 
2406   return nullptr;
2407 }
2408 
PLTRelocationType()2409 unsigned ObjectFileELF::PLTRelocationType() {
2410   // DT_PLTREL
2411   //  This member specifies the type of relocation entry to which the
2412   //  procedure linkage table refers. The d_val member holds DT_REL or
2413   //  DT_RELA, as appropriate. All relocations in a procedure linkage table
2414   //  must use the same relocation.
2415   const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
2416 
2417   if (symbol)
2418     return symbol->d_val;
2419 
2420   return 0;
2421 }
2422 
2423 // Returns the size of the normal plt entries and the offset of the first
2424 // normal plt entry. The 0th entry in the plt table is usually a resolution
2425 // entry which have different size in some architectures then the rest of the
2426 // plt entries.
2427 static std::pair<uint64_t, uint64_t>
GetPltEntrySizeAndOffset(const ELFSectionHeader * rel_hdr,const ELFSectionHeader * plt_hdr)2428 GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr,
2429                          const ELFSectionHeader *plt_hdr) {
2430   const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2431 
2432   // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are
2433   // 16 bytes. So round the entsize up by the alignment if addralign is set.
2434   elf_xword plt_entsize =
2435       plt_hdr->sh_addralign
2436           ? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign)
2437           : plt_hdr->sh_entsize;
2438 
2439   // Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly.
2440   // PLT entries relocation code in general requires multiple instruction and
2441   // should be greater than 4 bytes in most cases. Try to guess correct size
2442   // just in case.
2443   if (plt_entsize <= 4) {
2444     // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the
2445     // size of the plt entries based on the number of entries and the size of
2446     // the plt section with the assumption that the size of the 0th entry is at
2447     // least as big as the size of the normal entries and it isn't much bigger
2448     // then that.
2449     if (plt_hdr->sh_addralign)
2450       plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign /
2451                     (num_relocations + 1) * plt_hdr->sh_addralign;
2452     else
2453       plt_entsize = plt_hdr->sh_size / (num_relocations + 1);
2454   }
2455 
2456   elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize;
2457 
2458   return std::make_pair(plt_entsize, plt_offset);
2459 }
2460 
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)2461 static unsigned ParsePLTRelocations(
2462     Symtab *symbol_table, user_id_t start_id, unsigned rel_type,
2463     const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
2464     const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr,
2465     const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data,
2466     DataExtractor &symtab_data, DataExtractor &strtab_data) {
2467   ELFRelocation rel(rel_type);
2468   ELFSymbol symbol;
2469   lldb::offset_t offset = 0;
2470 
2471   uint64_t plt_offset, plt_entsize;
2472   std::tie(plt_entsize, plt_offset) =
2473       GetPltEntrySizeAndOffset(rel_hdr, plt_hdr);
2474   const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2475 
2476   typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2477   reloc_info_fn reloc_type;
2478   reloc_info_fn reloc_symbol;
2479 
2480   if (hdr->Is32Bit()) {
2481     reloc_type = ELFRelocation::RelocType32;
2482     reloc_symbol = ELFRelocation::RelocSymbol32;
2483   } else {
2484     reloc_type = ELFRelocation::RelocType64;
2485     reloc_symbol = ELFRelocation::RelocSymbol64;
2486   }
2487 
2488   unsigned slot_type = hdr->GetRelocationJumpSlotType();
2489   unsigned i;
2490   for (i = 0; i < num_relocations; ++i) {
2491     if (!rel.Parse(rel_data, &offset))
2492       break;
2493 
2494     if (reloc_type(rel) != slot_type)
2495       continue;
2496 
2497     lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
2498     if (!symbol.Parse(symtab_data, &symbol_offset))
2499       break;
2500 
2501     const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
2502     uint64_t plt_index = plt_offset + i * plt_entsize;
2503 
2504     Symbol jump_symbol(
2505         i + start_id,          // Symbol table index
2506         symbol_name,           // symbol name.
2507         eSymbolTypeTrampoline, // Type of this symbol
2508         false,                 // Is this globally visible?
2509         false,                 // Is this symbol debug info?
2510         true,                  // Is this symbol a trampoline?
2511         true,                  // Is this symbol artificial?
2512         plt_section_sp, // Section in which this symbol is defined or null.
2513         plt_index,      // Offset in section or symbol value.
2514         plt_entsize,    // Size in bytes of this symbol.
2515         true,           // Size is valid
2516         false,          // Contains linker annotations?
2517         0);             // Symbol flags.
2518 
2519     symbol_table->AddSymbol(jump_symbol);
2520   }
2521 
2522   return i;
2523 }
2524 
2525 unsigned
ParseTrampolineSymbols(Symtab * symbol_table,user_id_t start_id,const ELFSectionHeaderInfo * rel_hdr,user_id_t rel_id)2526 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id,
2527                                       const ELFSectionHeaderInfo *rel_hdr,
2528                                       user_id_t rel_id) {
2529   assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2530 
2531   // The link field points to the associated symbol table.
2532   user_id_t symtab_id = rel_hdr->sh_link;
2533 
2534   // If the link field doesn't point to the appropriate symbol name table then
2535   // try to find it by name as some compiler don't fill in the link fields.
2536   if (!symtab_id)
2537     symtab_id = GetSectionIndexByName(".dynsym");
2538 
2539   // Get PLT section.  We cannot use rel_hdr->sh_info, since current linkers
2540   // point that to the .got.plt or .got section instead of .plt.
2541   user_id_t plt_id = GetSectionIndexByName(".plt");
2542 
2543   if (!symtab_id || !plt_id)
2544     return 0;
2545 
2546   const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
2547   if (!plt_hdr)
2548     return 0;
2549 
2550   const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
2551   if (!sym_hdr)
2552     return 0;
2553 
2554   SectionList *section_list = m_sections_up.get();
2555   if (!section_list)
2556     return 0;
2557 
2558   Section *rel_section = section_list->FindSectionByID(rel_id).get();
2559   if (!rel_section)
2560     return 0;
2561 
2562   SectionSP plt_section_sp(section_list->FindSectionByID(plt_id));
2563   if (!plt_section_sp)
2564     return 0;
2565 
2566   Section *symtab = section_list->FindSectionByID(symtab_id).get();
2567   if (!symtab)
2568     return 0;
2569 
2570   // sh_link points to associated string table.
2571   Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link).get();
2572   if (!strtab)
2573     return 0;
2574 
2575   DataExtractor rel_data;
2576   if (!ReadSectionData(rel_section, rel_data))
2577     return 0;
2578 
2579   DataExtractor symtab_data;
2580   if (!ReadSectionData(symtab, symtab_data))
2581     return 0;
2582 
2583   DataExtractor strtab_data;
2584   if (!ReadSectionData(strtab, strtab_data))
2585     return 0;
2586 
2587   unsigned rel_type = PLTRelocationType();
2588   if (!rel_type)
2589     return 0;
2590 
2591   return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header,
2592                              rel_hdr, plt_hdr, sym_hdr, plt_section_sp,
2593                              rel_data, symtab_data, strtab_data);
2594 }
2595 
ApplyELF64ABS64Relocation(Symtab * symtab,ELFRelocation & rel,DataExtractor & debug_data,Section * rel_section)2596 static void ApplyELF64ABS64Relocation(Symtab *symtab, ELFRelocation &rel,
2597                                       DataExtractor &debug_data,
2598                                       Section *rel_section) {
2599   Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel));
2600   if (symbol) {
2601     addr_t value = symbol->GetAddressRef().GetFileAddress();
2602     DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2603     // ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2604     WritableDataBuffer *data_buffer =
2605         llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2606     uint64_t *dst = reinterpret_cast<uint64_t *>(
2607         data_buffer->GetBytes() + rel_section->GetFileOffset() +
2608         ELFRelocation::RelocOffset64(rel));
2609     uint64_t val_offset = value + ELFRelocation::RelocAddend64(rel);
2610     memcpy(dst, &val_offset, sizeof(uint64_t));
2611   }
2612 }
2613 
ApplyELF64ABS32Relocation(Symtab * symtab,ELFRelocation & rel,DataExtractor & debug_data,Section * rel_section,bool is_signed)2614 static void ApplyELF64ABS32Relocation(Symtab *symtab, ELFRelocation &rel,
2615                                       DataExtractor &debug_data,
2616                                       Section *rel_section, bool is_signed) {
2617   Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel));
2618   if (symbol) {
2619     addr_t value = symbol->GetAddressRef().GetFileAddress();
2620     value += ELFRelocation::RelocAddend32(rel);
2621     if ((!is_signed && (value > UINT32_MAX)) ||
2622         (is_signed &&
2623          ((int64_t)value > INT32_MAX || (int64_t)value < INT32_MIN))) {
2624       Log *log = GetLog(LLDBLog::Modules);
2625       LLDB_LOGF(log, "Failed to apply debug info relocations");
2626       return;
2627     }
2628     uint32_t truncated_addr = (value & 0xFFFFFFFF);
2629     DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2630     // ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2631     WritableDataBuffer *data_buffer =
2632         llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2633     uint32_t *dst = reinterpret_cast<uint32_t *>(
2634         data_buffer->GetBytes() + rel_section->GetFileOffset() +
2635         ELFRelocation::RelocOffset32(rel));
2636     memcpy(dst, &truncated_addr, sizeof(uint32_t));
2637   }
2638 }
2639 
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)2640 unsigned ObjectFileELF::ApplyRelocations(
2641     Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
2642     const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,
2643     DataExtractor &rel_data, DataExtractor &symtab_data,
2644     DataExtractor &debug_data, Section *rel_section) {
2645   ELFRelocation rel(rel_hdr->sh_type);
2646   lldb::addr_t offset = 0;
2647   const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2648   typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2649   reloc_info_fn reloc_type;
2650   reloc_info_fn reloc_symbol;
2651 
2652   if (hdr->Is32Bit()) {
2653     reloc_type = ELFRelocation::RelocType32;
2654     reloc_symbol = ELFRelocation::RelocSymbol32;
2655   } else {
2656     reloc_type = ELFRelocation::RelocType64;
2657     reloc_symbol = ELFRelocation::RelocSymbol64;
2658   }
2659 
2660   for (unsigned i = 0; i < num_relocations; ++i) {
2661     if (!rel.Parse(rel_data, &offset)) {
2662       GetModule()->ReportError(".rel{0}[{1:d}] failed to parse relocation",
2663                                rel_section->GetName().AsCString(), i);
2664       break;
2665     }
2666     Symbol *symbol = nullptr;
2667 
2668     if (hdr->Is32Bit()) {
2669       switch (reloc_type(rel)) {
2670       case R_386_32:
2671         symbol = symtab->FindSymbolByID(reloc_symbol(rel));
2672         if (symbol) {
2673           addr_t f_offset =
2674               rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel);
2675           DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2676           // ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2677           WritableDataBuffer *data_buffer =
2678               llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2679           uint32_t *dst = reinterpret_cast<uint32_t *>(
2680               data_buffer->GetBytes() + f_offset);
2681 
2682           addr_t value = symbol->GetAddressRef().GetFileAddress();
2683           if (rel.IsRela()) {
2684             value += ELFRelocation::RelocAddend32(rel);
2685           } else {
2686             value += *dst;
2687           }
2688           *dst = value;
2689         } else {
2690           GetModule()->ReportError(".rel{0}[{1}] unknown symbol id: {2:d}",
2691                                    rel_section->GetName().AsCString(), i,
2692                                    reloc_symbol(rel));
2693         }
2694         break;
2695       case R_386_PC32:
2696       default:
2697         GetModule()->ReportError("unsupported 32-bit relocation:"
2698                                  " .rel{0}[{1}], type {2}",
2699                                  rel_section->GetName().AsCString(), i,
2700                                  reloc_type(rel));
2701       }
2702     } else {
2703       switch (hdr->e_machine) {
2704       case llvm::ELF::EM_AARCH64:
2705         switch (reloc_type(rel)) {
2706         case R_AARCH64_ABS64:
2707           ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);
2708           break;
2709         case R_AARCH64_ABS32:
2710           ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);
2711           break;
2712         default:
2713           assert(false && "unexpected relocation type");
2714         }
2715         break;
2716       case llvm::ELF::EM_LOONGARCH:
2717         switch (reloc_type(rel)) {
2718         case R_LARCH_64:
2719           ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);
2720           break;
2721         case R_LARCH_32:
2722           ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);
2723           break;
2724         default:
2725           assert(false && "unexpected relocation type");
2726         }
2727         break;
2728       case llvm::ELF::EM_X86_64:
2729         switch (reloc_type(rel)) {
2730         case R_X86_64_64:
2731           ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);
2732           break;
2733         case R_X86_64_32:
2734           ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section,
2735                                     false);
2736           break;
2737         case R_X86_64_32S:
2738           ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);
2739           break;
2740         case R_X86_64_PC32:
2741         default:
2742           assert(false && "unexpected relocation type");
2743         }
2744         break;
2745       default:
2746         assert(false && "unsupported machine");
2747       }
2748     }
2749   }
2750 
2751   return 0;
2752 }
2753 
RelocateDebugSections(const ELFSectionHeader * rel_hdr,user_id_t rel_id,lldb_private::Symtab * thetab)2754 unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr,
2755                                               user_id_t rel_id,
2756                                               lldb_private::Symtab *thetab) {
2757   assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2758 
2759   // Parse in the section list if needed.
2760   SectionList *section_list = GetSectionList();
2761   if (!section_list)
2762     return 0;
2763 
2764   user_id_t symtab_id = rel_hdr->sh_link;
2765   user_id_t debug_id = rel_hdr->sh_info;
2766 
2767   const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
2768   if (!symtab_hdr)
2769     return 0;
2770 
2771   const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);
2772   if (!debug_hdr)
2773     return 0;
2774 
2775   Section *rel = section_list->FindSectionByID(rel_id).get();
2776   if (!rel)
2777     return 0;
2778 
2779   Section *symtab = section_list->FindSectionByID(symtab_id).get();
2780   if (!symtab)
2781     return 0;
2782 
2783   Section *debug = section_list->FindSectionByID(debug_id).get();
2784   if (!debug)
2785     return 0;
2786 
2787   DataExtractor rel_data;
2788   DataExtractor symtab_data;
2789   DataExtractor debug_data;
2790 
2791   if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) &&
2792       GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) &&
2793       GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) {
2794     ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr,
2795                      rel_data, symtab_data, debug_data, debug);
2796   }
2797 
2798   return 0;
2799 }
2800 
ParseSymtab(Symtab & lldb_symtab)2801 void ObjectFileELF::ParseSymtab(Symtab &lldb_symtab) {
2802   ModuleSP module_sp(GetModule());
2803   if (!module_sp)
2804     return;
2805 
2806   Progress progress(
2807       llvm::formatv("Parsing symbol table for {0}",
2808                     m_file.GetFilename().AsCString("<Unknown>")));
2809   ElapsedTime elapsed(module_sp->GetSymtabParseTime());
2810 
2811   // We always want to use the main object file so we (hopefully) only have one
2812   // cached copy of our symtab, dynamic sections, etc.
2813   ObjectFile *module_obj_file = module_sp->GetObjectFile();
2814   if (module_obj_file && module_obj_file != this)
2815     return module_obj_file->ParseSymtab(lldb_symtab);
2816 
2817   SectionList *section_list = module_sp->GetSectionList();
2818   if (!section_list)
2819     return;
2820 
2821   uint64_t symbol_id = 0;
2822 
2823   // Sharable objects and dynamic executables usually have 2 distinct symbol
2824   // tables, one named ".symtab", and the other ".dynsym". The dynsym is a
2825   // smaller version of the symtab that only contains global symbols. The
2826   // information found in the dynsym is therefore also found in the symtab,
2827   // while the reverse is not necessarily true.
2828   Section *symtab =
2829       section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get();
2830   if (symtab)
2831     symbol_id += ParseSymbolTable(&lldb_symtab, symbol_id, symtab);
2832 
2833   // The symtab section is non-allocable and can be stripped, while the
2834   // .dynsym section which should always be always be there. To support the
2835   // minidebuginfo case we parse .dynsym when there's a .gnu_debuginfo
2836   // section, nomatter if .symtab was already parsed or not. This is because
2837   // minidebuginfo normally removes the .symtab symbols which have their
2838   // matching .dynsym counterparts.
2839   if (!symtab ||
2840       GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"))) {
2841     Section *dynsym =
2842         section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true)
2843             .get();
2844     if (dynsym)
2845       symbol_id += ParseSymbolTable(&lldb_symtab, symbol_id, dynsym);
2846   }
2847 
2848   // DT_JMPREL
2849   //      If present, this entry's d_ptr member holds the address of
2850   //      relocation
2851   //      entries associated solely with the procedure linkage table.
2852   //      Separating
2853   //      these relocation entries lets the dynamic linker ignore them during
2854   //      process initialization, if lazy binding is enabled. If this entry is
2855   //      present, the related entries of types DT_PLTRELSZ and DT_PLTREL must
2856   //      also be present.
2857   const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
2858   if (symbol) {
2859     const ELFDynamic *pltrelsz = FindDynamicSymbol(DT_PLTRELSZ);
2860     assert(pltrelsz != NULL);
2861     // Synthesize trampoline symbols to help navigate the PLT.
2862     addr_t addr = symbol->d_ptr;
2863     Section *reloc_section =
2864         section_list->FindSectionContainingFileAddress(addr).get();
2865     if (reloc_section && pltrelsz->d_val > 0) {
2866       user_id_t reloc_id = reloc_section->GetID();
2867       const ELFSectionHeaderInfo *reloc_header =
2868           GetSectionHeaderByIndex(reloc_id);
2869       if (reloc_header)
2870         ParseTrampolineSymbols(&lldb_symtab, symbol_id, reloc_header, reloc_id);
2871     }
2872   }
2873 
2874   if (DWARFCallFrameInfo *eh_frame =
2875           GetModule()->GetUnwindTable().GetEHFrameInfo()) {
2876     ParseUnwindSymbols(&lldb_symtab, eh_frame);
2877   }
2878 
2879   // In the event that there's no symbol entry for the entry point we'll
2880   // artificially create one. We delegate to the symtab object the figuring
2881   // out of the proper size, this will usually make it span til the next
2882   // symbol it finds in the section. This means that if there are missing
2883   // symbols the entry point might span beyond its function definition.
2884   // We're fine with this as it doesn't make it worse than not having a
2885   // symbol entry at all.
2886   if (CalculateType() == eTypeExecutable) {
2887     ArchSpec arch = GetArchitecture();
2888     auto entry_point_addr = GetEntryPointAddress();
2889     bool is_valid_entry_point =
2890         entry_point_addr.IsValid() && entry_point_addr.IsSectionOffset();
2891     addr_t entry_point_file_addr = entry_point_addr.GetFileAddress();
2892     if (is_valid_entry_point && !lldb_symtab.FindSymbolContainingFileAddress(
2893                                     entry_point_file_addr)) {
2894       uint64_t symbol_id = lldb_symtab.GetNumSymbols();
2895       // Don't set the name for any synthetic symbols, the Symbol
2896       // object will generate one if needed when the name is accessed
2897       // via accessors.
2898       SectionSP section_sp = entry_point_addr.GetSection();
2899       Symbol symbol(
2900           /*symID=*/symbol_id,
2901           /*name=*/llvm::StringRef(), // Name will be auto generated.
2902           /*type=*/eSymbolTypeCode,
2903           /*external=*/true,
2904           /*is_debug=*/false,
2905           /*is_trampoline=*/false,
2906           /*is_artificial=*/true,
2907           /*section_sp=*/section_sp,
2908           /*offset=*/0,
2909           /*size=*/0, // FDE can span multiple symbols so don't use its size.
2910           /*size_is_valid=*/false,
2911           /*contains_linker_annotations=*/false,
2912           /*flags=*/0);
2913       // When the entry point is arm thumb we need to explicitly set its
2914       // class address to reflect that. This is important because expression
2915       // evaluation relies on correctly setting a breakpoint at this
2916       // address.
2917       if (arch.GetMachine() == llvm::Triple::arm &&
2918           (entry_point_file_addr & 1)) {
2919         symbol.GetAddressRef().SetOffset(entry_point_addr.GetOffset() ^ 1);
2920         m_address_class_map[entry_point_file_addr ^ 1] =
2921             AddressClass::eCodeAlternateISA;
2922       } else {
2923         m_address_class_map[entry_point_file_addr] = AddressClass::eCode;
2924       }
2925       lldb_symtab.AddSymbol(symbol);
2926     }
2927   }
2928 }
2929 
RelocateSection(lldb_private::Section * section)2930 void ObjectFileELF::RelocateSection(lldb_private::Section *section)
2931 {
2932   static const char *debug_prefix = ".debug";
2933 
2934   // Set relocated bit so we stop getting called, regardless of whether we
2935   // actually relocate.
2936   section->SetIsRelocated(true);
2937 
2938   // We only relocate in ELF relocatable files
2939   if (CalculateType() != eTypeObjectFile)
2940     return;
2941 
2942   const char *section_name = section->GetName().GetCString();
2943   // Can't relocate that which can't be named
2944   if (section_name == nullptr)
2945     return;
2946 
2947   // We don't relocate non-debug sections at the moment
2948   if (strncmp(section_name, debug_prefix, strlen(debug_prefix)))
2949     return;
2950 
2951   // Relocation section names to look for
2952   std::string needle = std::string(".rel") + section_name;
2953   std::string needlea = std::string(".rela") + section_name;
2954 
2955   for (SectionHeaderCollIter I = m_section_headers.begin();
2956        I != m_section_headers.end(); ++I) {
2957     if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) {
2958       const char *hay_name = I->section_name.GetCString();
2959       if (hay_name == nullptr)
2960         continue;
2961       if (needle == hay_name || needlea == hay_name) {
2962         const ELFSectionHeader &reloc_header = *I;
2963         user_id_t reloc_id = SectionIndex(I);
2964         RelocateDebugSections(&reloc_header, reloc_id, GetSymtab());
2965         break;
2966       }
2967     }
2968   }
2969 }
2970 
ParseUnwindSymbols(Symtab * symbol_table,DWARFCallFrameInfo * eh_frame)2971 void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table,
2972                                        DWARFCallFrameInfo *eh_frame) {
2973   SectionList *section_list = GetSectionList();
2974   if (!section_list)
2975     return;
2976 
2977   // First we save the new symbols into a separate list and add them to the
2978   // symbol table after we collected all symbols we want to add. This is
2979   // neccessary because adding a new symbol invalidates the internal index of
2980   // the symtab what causing the next lookup to be slow because it have to
2981   // recalculate the index first.
2982   std::vector<Symbol> new_symbols;
2983 
2984   size_t num_symbols = symbol_table->GetNumSymbols();
2985   uint64_t last_symbol_id =
2986       num_symbols ? symbol_table->SymbolAtIndex(num_symbols - 1)->GetID() : 0;
2987   eh_frame->ForEachFDEEntries([&](lldb::addr_t file_addr, uint32_t size,
2988                                   dw_offset_t) {
2989     Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr);
2990     if (symbol) {
2991       if (!symbol->GetByteSizeIsValid()) {
2992         symbol->SetByteSize(size);
2993         symbol->SetSizeIsSynthesized(true);
2994       }
2995     } else {
2996       SectionSP section_sp =
2997           section_list->FindSectionContainingFileAddress(file_addr);
2998       if (section_sp) {
2999         addr_t offset = file_addr - section_sp->GetFileAddress();
3000         uint64_t symbol_id = ++last_symbol_id;
3001         // Don't set the name for any synthetic symbols, the Symbol
3002         // object will generate one if needed when the name is accessed
3003         // via accessors.
3004         Symbol eh_symbol(
3005             /*symID=*/symbol_id,
3006             /*name=*/llvm::StringRef(), // Name will be auto generated.
3007             /*type=*/eSymbolTypeCode,
3008             /*external=*/true,
3009             /*is_debug=*/false,
3010             /*is_trampoline=*/false,
3011             /*is_artificial=*/true,
3012             /*section_sp=*/section_sp,
3013             /*offset=*/offset,
3014             /*size=*/0, // FDE can span multiple symbols so don't use its size.
3015             /*size_is_valid=*/false,
3016             /*contains_linker_annotations=*/false,
3017             /*flags=*/0);
3018         new_symbols.push_back(eh_symbol);
3019       }
3020     }
3021     return true;
3022   });
3023 
3024   for (const Symbol &s : new_symbols)
3025     symbol_table->AddSymbol(s);
3026 }
3027 
IsStripped()3028 bool ObjectFileELF::IsStripped() {
3029   // TODO: determine this for ELF
3030   return false;
3031 }
3032 
3033 //===----------------------------------------------------------------------===//
3034 // Dump
3035 //
3036 // Dump the specifics of the runtime file container (such as any headers
3037 // segments, sections, etc).
Dump(Stream * s)3038 void ObjectFileELF::Dump(Stream *s) {
3039   ModuleSP module_sp(GetModule());
3040   if (!module_sp) {
3041     return;
3042   }
3043 
3044   std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
3045   s->Printf("%p: ", static_cast<void *>(this));
3046   s->Indent();
3047   s->PutCString("ObjectFileELF");
3048 
3049   ArchSpec header_arch = GetArchitecture();
3050 
3051   *s << ", file = '" << m_file
3052      << "', arch = " << header_arch.GetArchitectureName() << "\n";
3053 
3054   DumpELFHeader(s, m_header);
3055   s->EOL();
3056   DumpELFProgramHeaders(s);
3057   s->EOL();
3058   DumpELFSectionHeaders(s);
3059   s->EOL();
3060   SectionList *section_list = GetSectionList();
3061   if (section_list)
3062     section_list->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
3063                        UINT32_MAX);
3064   Symtab *symtab = GetSymtab();
3065   if (symtab)
3066     symtab->Dump(s, nullptr, eSortOrderNone);
3067   s->EOL();
3068   DumpDependentModules(s);
3069   s->EOL();
3070 }
3071 
3072 // DumpELFHeader
3073 //
3074 // Dump the ELF header to the specified output stream
DumpELFHeader(Stream * s,const ELFHeader & header)3075 void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) {
3076   s->PutCString("ELF Header\n");
3077   s->Printf("e_ident[EI_MAG0   ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
3078   s->Printf("e_ident[EI_MAG1   ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1],
3079             header.e_ident[EI_MAG1]);
3080   s->Printf("e_ident[EI_MAG2   ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2],
3081             header.e_ident[EI_MAG2]);
3082   s->Printf("e_ident[EI_MAG3   ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3],
3083             header.e_ident[EI_MAG3]);
3084 
3085   s->Printf("e_ident[EI_CLASS  ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
3086   s->Printf("e_ident[EI_DATA   ] = 0x%2.2x ", header.e_ident[EI_DATA]);
3087   DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
3088   s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
3089   s->Printf("e_ident[EI_PAD    ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
3090 
3091   s->Printf("e_type      = 0x%4.4x ", header.e_type);
3092   DumpELFHeader_e_type(s, header.e_type);
3093   s->Printf("\ne_machine   = 0x%4.4x\n", header.e_machine);
3094   s->Printf("e_version   = 0x%8.8x\n", header.e_version);
3095   s->Printf("e_entry     = 0x%8.8" PRIx64 "\n", header.e_entry);
3096   s->Printf("e_phoff     = 0x%8.8" PRIx64 "\n", header.e_phoff);
3097   s->Printf("e_shoff     = 0x%8.8" PRIx64 "\n", header.e_shoff);
3098   s->Printf("e_flags     = 0x%8.8x\n", header.e_flags);
3099   s->Printf("e_ehsize    = 0x%4.4x\n", header.e_ehsize);
3100   s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
3101   s->Printf("e_phnum     = 0x%8.8x\n", header.e_phnum);
3102   s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
3103   s->Printf("e_shnum     = 0x%8.8x\n", header.e_shnum);
3104   s->Printf("e_shstrndx  = 0x%8.8x\n", header.e_shstrndx);
3105 }
3106 
3107 // DumpELFHeader_e_type
3108 //
3109 // Dump an token value for the ELF header member e_type
DumpELFHeader_e_type(Stream * s,elf_half e_type)3110 void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) {
3111   switch (e_type) {
3112   case ET_NONE:
3113     *s << "ET_NONE";
3114     break;
3115   case ET_REL:
3116     *s << "ET_REL";
3117     break;
3118   case ET_EXEC:
3119     *s << "ET_EXEC";
3120     break;
3121   case ET_DYN:
3122     *s << "ET_DYN";
3123     break;
3124   case ET_CORE:
3125     *s << "ET_CORE";
3126     break;
3127   default:
3128     break;
3129   }
3130 }
3131 
3132 // DumpELFHeader_e_ident_EI_DATA
3133 //
3134 // Dump an token value for the ELF header member e_ident[EI_DATA]
DumpELFHeader_e_ident_EI_DATA(Stream * s,unsigned char ei_data)3135 void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s,
3136                                                   unsigned char ei_data) {
3137   switch (ei_data) {
3138   case ELFDATANONE:
3139     *s << "ELFDATANONE";
3140     break;
3141   case ELFDATA2LSB:
3142     *s << "ELFDATA2LSB - Little Endian";
3143     break;
3144   case ELFDATA2MSB:
3145     *s << "ELFDATA2MSB - Big Endian";
3146     break;
3147   default:
3148     break;
3149   }
3150 }
3151 
3152 // DumpELFProgramHeader
3153 //
3154 // Dump a single ELF program header to the specified output stream
DumpELFProgramHeader(Stream * s,const ELFProgramHeader & ph)3155 void ObjectFileELF::DumpELFProgramHeader(Stream *s,
3156                                          const ELFProgramHeader &ph) {
3157   DumpELFProgramHeader_p_type(s, ph.p_type);
3158   s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset,
3159             ph.p_vaddr, ph.p_paddr);
3160   s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz,
3161             ph.p_flags);
3162 
3163   DumpELFProgramHeader_p_flags(s, ph.p_flags);
3164   s->Printf(") %8.8" PRIx64, ph.p_align);
3165 }
3166 
3167 // DumpELFProgramHeader_p_type
3168 //
3169 // Dump an token value for the ELF program header member p_type which describes
3170 // the type of the program header
DumpELFProgramHeader_p_type(Stream * s,elf_word p_type)3171 void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) {
3172   const int kStrWidth = 15;
3173   switch (p_type) {
3174     CASE_AND_STREAM(s, PT_NULL, kStrWidth);
3175     CASE_AND_STREAM(s, PT_LOAD, kStrWidth);
3176     CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth);
3177     CASE_AND_STREAM(s, PT_INTERP, kStrWidth);
3178     CASE_AND_STREAM(s, PT_NOTE, kStrWidth);
3179     CASE_AND_STREAM(s, PT_SHLIB, kStrWidth);
3180     CASE_AND_STREAM(s, PT_PHDR, kStrWidth);
3181     CASE_AND_STREAM(s, PT_TLS, kStrWidth);
3182     CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
3183   default:
3184     s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
3185     break;
3186   }
3187 }
3188 
3189 // DumpELFProgramHeader_p_flags
3190 //
3191 // Dump an token value for the ELF program header member p_flags
DumpELFProgramHeader_p_flags(Stream * s,elf_word p_flags)3192 void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) {
3193   *s << ((p_flags & PF_X) ? "PF_X" : "    ")
3194      << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
3195      << ((p_flags & PF_W) ? "PF_W" : "    ")
3196      << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
3197      << ((p_flags & PF_R) ? "PF_R" : "    ");
3198 }
3199 
3200 // DumpELFProgramHeaders
3201 //
3202 // Dump all of the ELF program header to the specified output stream
DumpELFProgramHeaders(Stream * s)3203 void ObjectFileELF::DumpELFProgramHeaders(Stream *s) {
3204   if (!ParseProgramHeaders())
3205     return;
3206 
3207   s->PutCString("Program Headers\n");
3208   s->PutCString("IDX  p_type          p_offset p_vaddr  p_paddr  "
3209                 "p_filesz p_memsz  p_flags                   p_align\n");
3210   s->PutCString("==== --------------- -------- -------- -------- "
3211                 "-------- -------- ------------------------- --------\n");
3212 
3213   for (const auto &H : llvm::enumerate(m_program_headers)) {
3214     s->Format("[{0,2}] ", H.index());
3215     ObjectFileELF::DumpELFProgramHeader(s, H.value());
3216     s->EOL();
3217   }
3218 }
3219 
3220 // DumpELFSectionHeader
3221 //
3222 // Dump a single ELF section header to the specified output stream
DumpELFSectionHeader(Stream * s,const ELFSectionHeaderInfo & sh)3223 void ObjectFileELF::DumpELFSectionHeader(Stream *s,
3224                                          const ELFSectionHeaderInfo &sh) {
3225   s->Printf("%8.8x ", sh.sh_name);
3226   DumpELFSectionHeader_sh_type(s, sh.sh_type);
3227   s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
3228   DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
3229   s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr,
3230             sh.sh_offset, sh.sh_size);
3231   s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
3232   s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
3233 }
3234 
3235 // DumpELFSectionHeader_sh_type
3236 //
3237 // Dump an token value for the ELF section header member sh_type which
3238 // describes the type of the section
DumpELFSectionHeader_sh_type(Stream * s,elf_word sh_type)3239 void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) {
3240   const int kStrWidth = 12;
3241   switch (sh_type) {
3242     CASE_AND_STREAM(s, SHT_NULL, kStrWidth);
3243     CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth);
3244     CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth);
3245     CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth);
3246     CASE_AND_STREAM(s, SHT_RELA, kStrWidth);
3247     CASE_AND_STREAM(s, SHT_HASH, kStrWidth);
3248     CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth);
3249     CASE_AND_STREAM(s, SHT_NOTE, kStrWidth);
3250     CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth);
3251     CASE_AND_STREAM(s, SHT_REL, kStrWidth);
3252     CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth);
3253     CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth);
3254     CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth);
3255     CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth);
3256     CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth);
3257     CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth);
3258   default:
3259     s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
3260     break;
3261   }
3262 }
3263 
3264 // DumpELFSectionHeader_sh_flags
3265 //
3266 // Dump an token value for the ELF section header member sh_flags
DumpELFSectionHeader_sh_flags(Stream * s,elf_xword sh_flags)3267 void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s,
3268                                                   elf_xword sh_flags) {
3269   *s << ((sh_flags & SHF_WRITE) ? "WRITE" : "     ")
3270      << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
3271      << ((sh_flags & SHF_ALLOC) ? "ALLOC" : "     ")
3272      << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
3273      << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : "         ");
3274 }
3275 
3276 // DumpELFSectionHeaders
3277 //
3278 // Dump all of the ELF section header to the specified output stream
DumpELFSectionHeaders(Stream * s)3279 void ObjectFileELF::DumpELFSectionHeaders(Stream *s) {
3280   if (!ParseSectionHeaders())
3281     return;
3282 
3283   s->PutCString("Section Headers\n");
3284   s->PutCString("IDX  name     type         flags                            "
3285                 "addr     offset   size     link     info     addralgn "
3286                 "entsize  Name\n");
3287   s->PutCString("==== -------- ------------ -------------------------------- "
3288                 "-------- -------- -------- -------- -------- -------- "
3289                 "-------- ====================\n");
3290 
3291   uint32_t idx = 0;
3292   for (SectionHeaderCollConstIter I = m_section_headers.begin();
3293        I != m_section_headers.end(); ++I, ++idx) {
3294     s->Printf("[%2u] ", idx);
3295     ObjectFileELF::DumpELFSectionHeader(s, *I);
3296     const char *section_name = I->section_name.AsCString("");
3297     if (section_name)
3298       *s << ' ' << section_name << "\n";
3299   }
3300 }
3301 
DumpDependentModules(lldb_private::Stream * s)3302 void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) {
3303   size_t num_modules = ParseDependentModules();
3304 
3305   if (num_modules > 0) {
3306     s->PutCString("Dependent Modules:\n");
3307     for (unsigned i = 0; i < num_modules; ++i) {
3308       const FileSpec &spec = m_filespec_up->GetFileSpecAtIndex(i);
3309       s->Printf("   %s\n", spec.GetFilename().GetCString());
3310     }
3311   }
3312 }
3313 
GetArchitecture()3314 ArchSpec ObjectFileELF::GetArchitecture() {
3315   if (!ParseHeader())
3316     return ArchSpec();
3317 
3318   if (m_section_headers.empty()) {
3319     // Allow elf notes to be parsed which may affect the detected architecture.
3320     ParseSectionHeaders();
3321   }
3322 
3323   if (CalculateType() == eTypeCoreFile &&
3324       !m_arch_spec.TripleOSWasSpecified()) {
3325     // Core files don't have section headers yet they have PT_NOTE program
3326     // headers that might shed more light on the architecture
3327     for (const elf::ELFProgramHeader &H : ProgramHeaders()) {
3328       if (H.p_type != PT_NOTE || H.p_offset == 0 || H.p_filesz == 0)
3329         continue;
3330       DataExtractor data;
3331       if (data.SetData(m_data, H.p_offset, H.p_filesz) == H.p_filesz) {
3332         UUID uuid;
3333         RefineModuleDetailsFromNote(data, m_arch_spec, uuid);
3334       }
3335     }
3336   }
3337   return m_arch_spec;
3338 }
3339 
CalculateType()3340 ObjectFile::Type ObjectFileELF::CalculateType() {
3341   switch (m_header.e_type) {
3342   case llvm::ELF::ET_NONE:
3343     // 0 - No file type
3344     return eTypeUnknown;
3345 
3346   case llvm::ELF::ET_REL:
3347     // 1 - Relocatable file
3348     return eTypeObjectFile;
3349 
3350   case llvm::ELF::ET_EXEC:
3351     // 2 - Executable file
3352     return eTypeExecutable;
3353 
3354   case llvm::ELF::ET_DYN:
3355     // 3 - Shared object file
3356     return eTypeSharedLibrary;
3357 
3358   case ET_CORE:
3359     // 4 - Core file
3360     return eTypeCoreFile;
3361 
3362   default:
3363     break;
3364   }
3365   return eTypeUnknown;
3366 }
3367 
CalculateStrata()3368 ObjectFile::Strata ObjectFileELF::CalculateStrata() {
3369   switch (m_header.e_type) {
3370   case llvm::ELF::ET_NONE:
3371     // 0 - No file type
3372     return eStrataUnknown;
3373 
3374   case llvm::ELF::ET_REL:
3375     // 1 - Relocatable file
3376     return eStrataUnknown;
3377 
3378   case llvm::ELF::ET_EXEC:
3379     // 2 - Executable file
3380     // TODO: is there any way to detect that an executable is a kernel
3381     // related executable by inspecting the program headers, section headers,
3382     // symbols, or any other flag bits???
3383     return eStrataUser;
3384 
3385   case llvm::ELF::ET_DYN:
3386     // 3 - Shared object file
3387     // TODO: is there any way to detect that an shared library is a kernel
3388     // related executable by inspecting the program headers, section headers,
3389     // symbols, or any other flag bits???
3390     return eStrataUnknown;
3391 
3392   case ET_CORE:
3393     // 4 - Core file
3394     // TODO: is there any way to detect that an core file is a kernel
3395     // related executable by inspecting the program headers, section headers,
3396     // symbols, or any other flag bits???
3397     return eStrataUnknown;
3398 
3399   default:
3400     break;
3401   }
3402   return eStrataUnknown;
3403 }
3404 
ReadSectionData(Section * section,lldb::offset_t section_offset,void * dst,size_t dst_len)3405 size_t ObjectFileELF::ReadSectionData(Section *section,
3406                        lldb::offset_t section_offset, void *dst,
3407                        size_t dst_len) {
3408   // If some other objectfile owns this data, pass this to them.
3409   if (section->GetObjectFile() != this)
3410     return section->GetObjectFile()->ReadSectionData(section, section_offset,
3411                                                      dst, dst_len);
3412 
3413   if (!section->Test(SHF_COMPRESSED))
3414     return ObjectFile::ReadSectionData(section, section_offset, dst, dst_len);
3415 
3416   // For compressed sections we need to read to full data to be able to
3417   // decompress.
3418   DataExtractor data;
3419   ReadSectionData(section, data);
3420   return data.CopyData(section_offset, dst_len, dst);
3421 }
3422 
ReadSectionData(Section * section,DataExtractor & section_data)3423 size_t ObjectFileELF::ReadSectionData(Section *section,
3424                                       DataExtractor &section_data) {
3425   // If some other objectfile owns this data, pass this to them.
3426   if (section->GetObjectFile() != this)
3427     return section->GetObjectFile()->ReadSectionData(section, section_data);
3428 
3429   size_t result = ObjectFile::ReadSectionData(section, section_data);
3430   if (result == 0 || !(section->Get() & llvm::ELF::SHF_COMPRESSED))
3431     return result;
3432 
3433   auto Decompressor = llvm::object::Decompressor::create(
3434       section->GetName().GetStringRef(),
3435       {reinterpret_cast<const char *>(section_data.GetDataStart()),
3436        size_t(section_data.GetByteSize())},
3437       GetByteOrder() == eByteOrderLittle, GetAddressByteSize() == 8);
3438   if (!Decompressor) {
3439     GetModule()->ReportWarning(
3440         "Unable to initialize decompressor for section '{0}': {1}",
3441         section->GetName().GetCString(),
3442         llvm::toString(Decompressor.takeError()).c_str());
3443     section_data.Clear();
3444     return 0;
3445   }
3446 
3447   auto buffer_sp =
3448       std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0);
3449   if (auto error = Decompressor->decompress(
3450           {buffer_sp->GetBytes(), size_t(buffer_sp->GetByteSize())})) {
3451     GetModule()->ReportWarning("Decompression of section '{0}' failed: {1}",
3452                                section->GetName().GetCString(),
3453                                llvm::toString(std::move(error)).c_str());
3454     section_data.Clear();
3455     return 0;
3456   }
3457 
3458   section_data.SetData(buffer_sp);
3459   return buffer_sp->GetByteSize();
3460 }
3461 
ProgramHeaders()3462 llvm::ArrayRef<ELFProgramHeader> ObjectFileELF::ProgramHeaders() {
3463   ParseProgramHeaders();
3464   return m_program_headers;
3465 }
3466 
GetSegmentData(const ELFProgramHeader & H)3467 DataExtractor ObjectFileELF::GetSegmentData(const ELFProgramHeader &H) {
3468   return DataExtractor(m_data, H.p_offset, H.p_filesz);
3469 }
3470 
AnySegmentHasPhysicalAddress()3471 bool ObjectFileELF::AnySegmentHasPhysicalAddress() {
3472   for (const ELFProgramHeader &H : ProgramHeaders()) {
3473     if (H.p_paddr != 0)
3474       return true;
3475   }
3476   return false;
3477 }
3478 
3479 std::vector<ObjectFile::LoadableData>
GetLoadableData(Target & target)3480 ObjectFileELF::GetLoadableData(Target &target) {
3481   // Create a list of loadable data from loadable segments, using physical
3482   // addresses if they aren't all null
3483   std::vector<LoadableData> loadables;
3484   bool should_use_paddr = AnySegmentHasPhysicalAddress();
3485   for (const ELFProgramHeader &H : ProgramHeaders()) {
3486     LoadableData loadable;
3487     if (H.p_type != llvm::ELF::PT_LOAD)
3488       continue;
3489     loadable.Dest = should_use_paddr ? H.p_paddr : H.p_vaddr;
3490     if (loadable.Dest == LLDB_INVALID_ADDRESS)
3491       continue;
3492     if (H.p_filesz == 0)
3493       continue;
3494     auto segment_data = GetSegmentData(H);
3495     loadable.Contents = llvm::ArrayRef<uint8_t>(segment_data.GetDataStart(),
3496                                                 segment_data.GetByteSize());
3497     loadables.push_back(loadable);
3498   }
3499   return loadables;
3500 }
3501 
3502 lldb::WritableDataBufferSP
MapFileDataWritable(const FileSpec & file,uint64_t Size,uint64_t Offset)3503 ObjectFileELF::MapFileDataWritable(const FileSpec &file, uint64_t Size,
3504                                    uint64_t Offset) {
3505   return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size,
3506                                                          Offset);
3507 }
3508