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