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