xref: /llvm-project/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp (revision b0b9605a544dbf26940a48777af401a419f4e4f1)
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       .Cases(".data", ".tdata", eSectionTypeData)
1677       .Case(".eh_frame", eSectionTypeEHFrame)
1678       .Case(".gnu_debugaltlink", eSectionTypeDWARFGNUDebugAltLink)
1679       .Case(".gosymtab", eSectionTypeGoSymtab)
1680       .Case(".text", eSectionTypeCode)
1681       .Default(eSectionTypeOther);
1682 }
1683 
1684 SectionType ObjectFileELF::GetSectionType(const ELFSectionHeaderInfo &H) const {
1685   switch (H.sh_type) {
1686   case SHT_PROGBITS:
1687     if (H.sh_flags & SHF_EXECINSTR)
1688       return eSectionTypeCode;
1689     break;
1690   case SHT_SYMTAB:
1691     return eSectionTypeELFSymbolTable;
1692   case SHT_DYNSYM:
1693     return eSectionTypeELFDynamicSymbols;
1694   case SHT_RELA:
1695   case SHT_REL:
1696     return eSectionTypeELFRelocationEntries;
1697   case SHT_DYNAMIC:
1698     return eSectionTypeELFDynamicLinkInfo;
1699   }
1700   return GetSectionTypeFromName(H.section_name.GetStringRef());
1701 }
1702 
1703 static uint32_t GetTargetByteSize(SectionType Type, const ArchSpec &arch) {
1704   switch (Type) {
1705   case eSectionTypeData:
1706   case eSectionTypeZeroFill:
1707     return arch.GetDataByteSize();
1708   case eSectionTypeCode:
1709     return arch.GetCodeByteSize();
1710   default:
1711     return 1;
1712   }
1713 }
1714 
1715 static Permissions GetPermissions(const ELFSectionHeader &H) {
1716   Permissions Perm = Permissions(0);
1717   if (H.sh_flags & SHF_ALLOC)
1718     Perm |= ePermissionsReadable;
1719   if (H.sh_flags & SHF_WRITE)
1720     Perm |= ePermissionsWritable;
1721   if (H.sh_flags & SHF_EXECINSTR)
1722     Perm |= ePermissionsExecutable;
1723   return Perm;
1724 }
1725 
1726 static Permissions GetPermissions(const ELFProgramHeader &H) {
1727   Permissions Perm = Permissions(0);
1728   if (H.p_flags & PF_R)
1729     Perm |= ePermissionsReadable;
1730   if (H.p_flags & PF_W)
1731     Perm |= ePermissionsWritable;
1732   if (H.p_flags & PF_X)
1733     Perm |= ePermissionsExecutable;
1734   return Perm;
1735 }
1736 
1737 namespace {
1738 
1739 using VMRange = lldb_private::Range<addr_t, addr_t>;
1740 
1741 struct SectionAddressInfo {
1742   SectionSP Segment;
1743   VMRange Range;
1744 };
1745 
1746 // (Unlinked) ELF object files usually have 0 for every section address, meaning
1747 // we need to compute synthetic addresses in order for "file addresses" from
1748 // different sections to not overlap. This class handles that logic.
1749 class VMAddressProvider {
1750   using VMMap = llvm::IntervalMap<addr_t, SectionSP, 4,
1751                                        llvm::IntervalMapHalfOpenInfo<addr_t>>;
1752 
1753   ObjectFile::Type ObjectType;
1754   addr_t NextVMAddress = 0;
1755   VMMap::Allocator Alloc;
1756   VMMap Segments{Alloc};
1757   VMMap Sections{Alloc};
1758   lldb_private::Log *Log = GetLog(LLDBLog::Modules);
1759   size_t SegmentCount = 0;
1760   std::string SegmentName;
1761 
1762   VMRange GetVMRange(const ELFSectionHeader &H) {
1763     addr_t Address = H.sh_addr;
1764     addr_t Size = H.sh_flags & SHF_ALLOC ? H.sh_size : 0;
1765     if (ObjectType == ObjectFile::Type::eTypeObjectFile && Segments.empty() && (H.sh_flags & SHF_ALLOC)) {
1766       NextVMAddress =
1767           llvm::alignTo(NextVMAddress, std::max<addr_t>(H.sh_addralign, 1));
1768       Address = NextVMAddress;
1769       NextVMAddress += Size;
1770     }
1771     return VMRange(Address, Size);
1772   }
1773 
1774 public:
1775   VMAddressProvider(ObjectFile::Type Type, llvm::StringRef SegmentName)
1776       : ObjectType(Type), SegmentName(std::string(SegmentName)) {}
1777 
1778   std::string GetNextSegmentName() const {
1779     return llvm::formatv("{0}[{1}]", SegmentName, SegmentCount).str();
1780   }
1781 
1782   std::optional<VMRange> GetAddressInfo(const ELFProgramHeader &H) {
1783     if (H.p_memsz == 0) {
1784       LLDB_LOG(Log, "Ignoring zero-sized {0} segment. Corrupt object file?",
1785                SegmentName);
1786       return std::nullopt;
1787     }
1788 
1789     if (Segments.overlaps(H.p_vaddr, H.p_vaddr + H.p_memsz)) {
1790       LLDB_LOG(Log, "Ignoring overlapping {0} segment. Corrupt object file?",
1791                SegmentName);
1792       return std::nullopt;
1793     }
1794     return VMRange(H.p_vaddr, H.p_memsz);
1795   }
1796 
1797   std::optional<SectionAddressInfo> GetAddressInfo(const ELFSectionHeader &H) {
1798     VMRange Range = GetVMRange(H);
1799     SectionSP Segment;
1800     auto It = Segments.find(Range.GetRangeBase());
1801     if ((H.sh_flags & SHF_ALLOC) && It.valid()) {
1802       addr_t MaxSize;
1803       if (It.start() <= Range.GetRangeBase()) {
1804         MaxSize = It.stop() - Range.GetRangeBase();
1805         Segment = *It;
1806       } else
1807         MaxSize = It.start() - Range.GetRangeBase();
1808       if (Range.GetByteSize() > MaxSize) {
1809         LLDB_LOG(Log, "Shortening section crossing segment boundaries. "
1810                       "Corrupt object file?");
1811         Range.SetByteSize(MaxSize);
1812       }
1813     }
1814     if (Range.GetByteSize() > 0 &&
1815         Sections.overlaps(Range.GetRangeBase(), Range.GetRangeEnd())) {
1816       LLDB_LOG(Log, "Ignoring overlapping section. Corrupt object file?");
1817       return std::nullopt;
1818     }
1819     if (Segment)
1820       Range.Slide(-Segment->GetFileAddress());
1821     return SectionAddressInfo{Segment, Range};
1822   }
1823 
1824   void AddSegment(const VMRange &Range, SectionSP Seg) {
1825     Segments.insert(Range.GetRangeBase(), Range.GetRangeEnd(), std::move(Seg));
1826     ++SegmentCount;
1827   }
1828 
1829   void AddSection(SectionAddressInfo Info, SectionSP Sect) {
1830     if (Info.Range.GetByteSize() == 0)
1831       return;
1832     if (Info.Segment)
1833       Info.Range.Slide(Info.Segment->GetFileAddress());
1834     Sections.insert(Info.Range.GetRangeBase(), Info.Range.GetRangeEnd(),
1835                     std::move(Sect));
1836   }
1837 };
1838 }
1839 
1840 void ObjectFileELF::CreateSections(SectionList &unified_section_list) {
1841   if (m_sections_up)
1842     return;
1843 
1844   m_sections_up = std::make_unique<SectionList>();
1845   VMAddressProvider regular_provider(GetType(), "PT_LOAD");
1846   VMAddressProvider tls_provider(GetType(), "PT_TLS");
1847 
1848   for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {
1849     const ELFProgramHeader &PHdr = EnumPHdr.value();
1850     if (PHdr.p_type != PT_LOAD && PHdr.p_type != PT_TLS)
1851       continue;
1852 
1853     VMAddressProvider &provider =
1854         PHdr.p_type == PT_TLS ? tls_provider : regular_provider;
1855     auto InfoOr = provider.GetAddressInfo(PHdr);
1856     if (!InfoOr)
1857       continue;
1858 
1859     uint32_t Log2Align = llvm::Log2_64(std::max<elf_xword>(PHdr.p_align, 1));
1860     SectionSP Segment = std::make_shared<Section>(
1861         GetModule(), this, SegmentID(EnumPHdr.index()),
1862         ConstString(provider.GetNextSegmentName()), eSectionTypeContainer,
1863         InfoOr->GetRangeBase(), InfoOr->GetByteSize(), PHdr.p_offset,
1864         PHdr.p_filesz, Log2Align, /*flags*/ 0);
1865     Segment->SetPermissions(GetPermissions(PHdr));
1866     Segment->SetIsThreadSpecific(PHdr.p_type == PT_TLS);
1867     m_sections_up->AddSection(Segment);
1868 
1869     provider.AddSegment(*InfoOr, std::move(Segment));
1870   }
1871 
1872   ParseSectionHeaders();
1873   if (m_section_headers.empty())
1874     return;
1875 
1876   for (SectionHeaderCollIter I = std::next(m_section_headers.begin());
1877        I != m_section_headers.end(); ++I) {
1878     const ELFSectionHeaderInfo &header = *I;
1879 
1880     ConstString &name = I->section_name;
1881     const uint64_t file_size =
1882         header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
1883 
1884     VMAddressProvider &provider =
1885         header.sh_flags & SHF_TLS ? tls_provider : regular_provider;
1886     auto InfoOr = provider.GetAddressInfo(header);
1887     if (!InfoOr)
1888       continue;
1889 
1890     SectionType sect_type = GetSectionType(header);
1891 
1892     const uint32_t target_bytes_size =
1893         GetTargetByteSize(sect_type, m_arch_spec);
1894 
1895     elf::elf_xword log2align =
1896         (header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign);
1897 
1898     SectionSP section_sp(new Section(
1899         InfoOr->Segment, GetModule(), // Module to which this section belongs.
1900         this,            // ObjectFile to which this section belongs and should
1901                          // read section data from.
1902         SectionIndex(I), // Section ID.
1903         name,            // Section name.
1904         sect_type,       // Section type.
1905         InfoOr->Range.GetRangeBase(), // VM address.
1906         InfoOr->Range.GetByteSize(),  // VM size in bytes of this section.
1907         header.sh_offset,             // Offset of this section in the file.
1908         file_size,           // Size of the section as found in the file.
1909         log2align,           // Alignment of the section
1910         header.sh_flags,     // Flags for this section.
1911         target_bytes_size)); // Number of host bytes per target byte
1912 
1913     section_sp->SetPermissions(GetPermissions(header));
1914     section_sp->SetIsThreadSpecific(header.sh_flags & SHF_TLS);
1915     (InfoOr->Segment ? InfoOr->Segment->GetChildren() : *m_sections_up)
1916         .AddSection(section_sp);
1917     provider.AddSection(std::move(*InfoOr), std::move(section_sp));
1918   }
1919 
1920   // For eTypeDebugInfo files, the Symbol Vendor will take care of updating the
1921   // unified section list.
1922   if (GetType() != eTypeDebugInfo)
1923     unified_section_list = *m_sections_up;
1924 
1925   // If there's a .gnu_debugdata section, we'll try to read the .symtab that's
1926   // embedded in there and replace the one in the original object file (if any).
1927   // If there's none in the orignal object file, we add it to it.
1928   if (auto gdd_obj_file = GetGnuDebugDataObjectFile()) {
1929     if (auto gdd_objfile_section_list = gdd_obj_file->GetSectionList()) {
1930       if (SectionSP symtab_section_sp =
1931               gdd_objfile_section_list->FindSectionByType(
1932                   eSectionTypeELFSymbolTable, true)) {
1933         SectionSP module_section_sp = unified_section_list.FindSectionByType(
1934             eSectionTypeELFSymbolTable, true);
1935         if (module_section_sp)
1936           unified_section_list.ReplaceSection(module_section_sp->GetID(),
1937                                               symtab_section_sp);
1938         else
1939           unified_section_list.AddSection(symtab_section_sp);
1940       }
1941     }
1942   }
1943 }
1944 
1945 std::shared_ptr<ObjectFileELF> ObjectFileELF::GetGnuDebugDataObjectFile() {
1946   if (m_gnu_debug_data_object_file != nullptr)
1947     return m_gnu_debug_data_object_file;
1948 
1949   SectionSP section =
1950       GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"));
1951   if (!section)
1952     return nullptr;
1953 
1954   if (!lldb_private::lzma::isAvailable()) {
1955     GetModule()->ReportWarning(
1956         "No LZMA support found for reading .gnu_debugdata section");
1957     return nullptr;
1958   }
1959 
1960   // Uncompress the data
1961   DataExtractor data;
1962   section->GetSectionData(data);
1963   llvm::SmallVector<uint8_t, 0> uncompressedData;
1964   auto err = lldb_private::lzma::uncompress(data.GetData(), uncompressedData);
1965   if (err) {
1966     GetModule()->ReportWarning(
1967         "An error occurred while decompression the section {0}: {1}",
1968         section->GetName().AsCString(), llvm::toString(std::move(err)).c_str());
1969     return nullptr;
1970   }
1971 
1972   // Construct ObjectFileELF object from decompressed buffer
1973   DataBufferSP gdd_data_buf(
1974       new DataBufferHeap(uncompressedData.data(), uncompressedData.size()));
1975   auto fspec = GetFileSpec().CopyByAppendingPathComponent(
1976       llvm::StringRef("gnu_debugdata"));
1977   m_gnu_debug_data_object_file.reset(new ObjectFileELF(
1978       GetModule(), gdd_data_buf, 0, &fspec, 0, gdd_data_buf->GetByteSize()));
1979 
1980   // This line is essential; otherwise a breakpoint can be set but not hit.
1981   m_gnu_debug_data_object_file->SetType(ObjectFile::eTypeDebugInfo);
1982 
1983   ArchSpec spec = m_gnu_debug_data_object_file->GetArchitecture();
1984   if (spec && m_gnu_debug_data_object_file->SetModulesArchitecture(spec))
1985     return m_gnu_debug_data_object_file;
1986 
1987   return nullptr;
1988 }
1989 
1990 // Find the arm/aarch64 mapping symbol character in the given symbol name.
1991 // Mapping symbols have the form of "$<char>[.<any>]*". Additionally we
1992 // recognize cases when the mapping symbol prefixed by an arbitrary string
1993 // because if a symbol prefix added to each symbol in the object file with
1994 // objcopy then the mapping symbols are also prefixed.
1995 static char FindArmAarch64MappingSymbol(const char *symbol_name) {
1996   if (!symbol_name)
1997     return '\0';
1998 
1999   const char *dollar_pos = ::strchr(symbol_name, '$');
2000   if (!dollar_pos || dollar_pos[1] == '\0')
2001     return '\0';
2002 
2003   if (dollar_pos[2] == '\0' || dollar_pos[2] == '.')
2004     return dollar_pos[1];
2005   return '\0';
2006 }
2007 
2008 #define STO_MIPS_ISA (3 << 6)
2009 #define STO_MICROMIPS (2 << 6)
2010 #define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS)
2011 
2012 // private
2013 unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
2014                                      SectionList *section_list,
2015                                      const size_t num_symbols,
2016                                      const DataExtractor &symtab_data,
2017                                      const DataExtractor &strtab_data) {
2018   ELFSymbol symbol;
2019   lldb::offset_t offset = 0;
2020 
2021   static ConstString text_section_name(".text");
2022   static ConstString init_section_name(".init");
2023   static ConstString fini_section_name(".fini");
2024   static ConstString ctors_section_name(".ctors");
2025   static ConstString dtors_section_name(".dtors");
2026 
2027   static ConstString data_section_name(".data");
2028   static ConstString rodata_section_name(".rodata");
2029   static ConstString rodata1_section_name(".rodata1");
2030   static ConstString data2_section_name(".data1");
2031   static ConstString bss_section_name(".bss");
2032   static ConstString opd_section_name(".opd"); // For ppc64
2033 
2034   // On Android the oatdata and the oatexec symbols in the oat and odex files
2035   // covers the full .text section what causes issues with displaying unusable
2036   // symbol name to the user and very slow unwinding speed because the
2037   // instruction emulation based unwind plans try to emulate all instructions
2038   // in these symbols. Don't add these symbols to the symbol list as they have
2039   // no use for the debugger and they are causing a lot of trouble. Filtering
2040   // can't be restricted to Android because this special object file don't
2041   // contain the note section specifying the environment to Android but the
2042   // custom extension and file name makes it highly unlikely that this will
2043   // collide with anything else.
2044   llvm::StringRef file_extension = m_file.GetFileNameExtension();
2045   bool skip_oatdata_oatexec =
2046       file_extension == ".oat" || file_extension == ".odex";
2047 
2048   ArchSpec arch = GetArchitecture();
2049   ModuleSP module_sp(GetModule());
2050   SectionList *module_section_list =
2051       module_sp ? module_sp->GetSectionList() : nullptr;
2052 
2053   // Local cache to avoid doing a FindSectionByName for each symbol. The "const
2054   // char*" key must came from a ConstString object so they can be compared by
2055   // pointer
2056   std::unordered_map<const char *, lldb::SectionSP> section_name_to_section;
2057 
2058   unsigned i;
2059   for (i = 0; i < num_symbols; ++i) {
2060     if (!symbol.Parse(symtab_data, &offset))
2061       break;
2062 
2063     const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
2064     if (!symbol_name)
2065       symbol_name = "";
2066 
2067     // No need to add non-section symbols that have no names
2068     if (symbol.getType() != STT_SECTION &&
2069         (symbol_name == nullptr || symbol_name[0] == '\0'))
2070       continue;
2071 
2072     // Skipping oatdata and oatexec sections if it is requested. See details
2073     // above the definition of skip_oatdata_oatexec for the reasons.
2074     if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 ||
2075                                  ::strcmp(symbol_name, "oatexec") == 0))
2076       continue;
2077 
2078     SectionSP symbol_section_sp;
2079     SymbolType symbol_type = eSymbolTypeInvalid;
2080     Elf64_Half shndx = symbol.st_shndx;
2081 
2082     switch (shndx) {
2083     case SHN_ABS:
2084       symbol_type = eSymbolTypeAbsolute;
2085       break;
2086     case SHN_UNDEF:
2087       symbol_type = eSymbolTypeUndefined;
2088       break;
2089     default:
2090       symbol_section_sp = section_list->FindSectionByID(shndx);
2091       break;
2092     }
2093 
2094     // If a symbol is undefined do not process it further even if it has a STT
2095     // type
2096     if (symbol_type != eSymbolTypeUndefined) {
2097       switch (symbol.getType()) {
2098       default:
2099       case STT_NOTYPE:
2100         // The symbol's type is not specified.
2101         break;
2102 
2103       case STT_OBJECT:
2104         // The symbol is associated with a data object, such as a variable, an
2105         // array, etc.
2106         symbol_type = eSymbolTypeData;
2107         break;
2108 
2109       case STT_FUNC:
2110         // The symbol is associated with a function or other executable code.
2111         symbol_type = eSymbolTypeCode;
2112         break;
2113 
2114       case STT_SECTION:
2115         // The symbol is associated with a section. Symbol table entries of
2116         // this type exist primarily for relocation and normally have STB_LOCAL
2117         // binding.
2118         break;
2119 
2120       case STT_FILE:
2121         // Conventionally, the symbol's name gives the name of the source file
2122         // associated with the object file. A file symbol has STB_LOCAL
2123         // binding, its section index is SHN_ABS, and it precedes the other
2124         // STB_LOCAL symbols for the file, if it is present.
2125         symbol_type = eSymbolTypeSourceFile;
2126         break;
2127 
2128       case STT_GNU_IFUNC:
2129         // The symbol is associated with an indirect function. The actual
2130         // function will be resolved if it is referenced.
2131         symbol_type = eSymbolTypeResolver;
2132         break;
2133       }
2134     }
2135 
2136     if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) {
2137       if (symbol_section_sp) {
2138         ConstString sect_name = symbol_section_sp->GetName();
2139         if (sect_name == text_section_name || sect_name == init_section_name ||
2140             sect_name == fini_section_name || sect_name == ctors_section_name ||
2141             sect_name == dtors_section_name) {
2142           symbol_type = eSymbolTypeCode;
2143         } else if (sect_name == data_section_name ||
2144                    sect_name == data2_section_name ||
2145                    sect_name == rodata_section_name ||
2146                    sect_name == rodata1_section_name ||
2147                    sect_name == bss_section_name) {
2148           symbol_type = eSymbolTypeData;
2149         }
2150       }
2151     }
2152 
2153     int64_t symbol_value_offset = 0;
2154     uint32_t additional_flags = 0;
2155 
2156     if (arch.IsValid()) {
2157       if (arch.GetMachine() == llvm::Triple::arm) {
2158         if (symbol.getBinding() == STB_LOCAL) {
2159           char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
2160           if (symbol_type == eSymbolTypeCode) {
2161             switch (mapping_symbol) {
2162             case 'a':
2163               // $a[.<any>]* - marks an ARM instruction sequence
2164               m_address_class_map[symbol.st_value] = AddressClass::eCode;
2165               break;
2166             case 'b':
2167             case 't':
2168               // $b[.<any>]* - marks a THUMB BL instruction sequence
2169               // $t[.<any>]* - marks a THUMB instruction sequence
2170               m_address_class_map[symbol.st_value] =
2171                   AddressClass::eCodeAlternateISA;
2172               break;
2173             case 'd':
2174               // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2175               m_address_class_map[symbol.st_value] = AddressClass::eData;
2176               break;
2177             }
2178           }
2179           if (mapping_symbol)
2180             continue;
2181         }
2182       } else if (arch.GetMachine() == llvm::Triple::aarch64) {
2183         if (symbol.getBinding() == STB_LOCAL) {
2184           char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
2185           if (symbol_type == eSymbolTypeCode) {
2186             switch (mapping_symbol) {
2187             case 'x':
2188               // $x[.<any>]* - marks an A64 instruction sequence
2189               m_address_class_map[symbol.st_value] = AddressClass::eCode;
2190               break;
2191             case 'd':
2192               // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2193               m_address_class_map[symbol.st_value] = AddressClass::eData;
2194               break;
2195             }
2196           }
2197           if (mapping_symbol)
2198             continue;
2199         }
2200       }
2201 
2202       if (arch.GetMachine() == llvm::Triple::arm) {
2203         if (symbol_type == eSymbolTypeCode) {
2204           if (symbol.st_value & 1) {
2205             // Subtracting 1 from the address effectively unsets the low order
2206             // bit, which results in the address actually pointing to the
2207             // beginning of the symbol. This delta will be used below in
2208             // conjunction with symbol.st_value to produce the final
2209             // symbol_value that we store in the symtab.
2210             symbol_value_offset = -1;
2211             m_address_class_map[symbol.st_value ^ 1] =
2212                 AddressClass::eCodeAlternateISA;
2213           } else {
2214             // This address is ARM
2215             m_address_class_map[symbol.st_value] = AddressClass::eCode;
2216           }
2217         }
2218       }
2219 
2220       /*
2221        * MIPS:
2222        * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for
2223        * MIPS).
2224        * This allows processor to switch between microMIPS and MIPS without any
2225        * need
2226        * for special mode-control register. However, apart from .debug_line,
2227        * none of
2228        * the ELF/DWARF sections set the ISA bit (for symbol or section). Use
2229        * st_other
2230        * flag to check whether the symbol is microMIPS and then set the address
2231        * class
2232        * accordingly.
2233       */
2234       if (arch.IsMIPS()) {
2235         if (IS_MICROMIPS(symbol.st_other))
2236           m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
2237         else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) {
2238           symbol.st_value = symbol.st_value & (~1ull);
2239           m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
2240         } else {
2241           if (symbol_type == eSymbolTypeCode)
2242             m_address_class_map[symbol.st_value] = AddressClass::eCode;
2243           else if (symbol_type == eSymbolTypeData)
2244             m_address_class_map[symbol.st_value] = AddressClass::eData;
2245           else
2246             m_address_class_map[symbol.st_value] = AddressClass::eUnknown;
2247         }
2248       }
2249     }
2250 
2251     // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB
2252     // symbols. See above for more details.
2253     uint64_t symbol_value = symbol.st_value + symbol_value_offset;
2254 
2255     if (symbol_section_sp &&
2256         CalculateType() != ObjectFile::Type::eTypeObjectFile)
2257       symbol_value -= symbol_section_sp->GetFileAddress();
2258 
2259     if (symbol_section_sp && module_section_list &&
2260         module_section_list != section_list) {
2261       ConstString sect_name = symbol_section_sp->GetName();
2262       auto section_it = section_name_to_section.find(sect_name.GetCString());
2263       if (section_it == section_name_to_section.end())
2264         section_it =
2265             section_name_to_section
2266                 .emplace(sect_name.GetCString(),
2267                          module_section_list->FindSectionByName(sect_name))
2268                 .first;
2269       if (section_it->second)
2270         symbol_section_sp = section_it->second;
2271     }
2272 
2273     bool is_global = symbol.getBinding() == STB_GLOBAL;
2274     uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags;
2275     llvm::StringRef symbol_ref(symbol_name);
2276 
2277     // Symbol names may contain @VERSION suffixes. Find those and strip them
2278     // temporarily.
2279     size_t version_pos = symbol_ref.find('@');
2280     bool has_suffix = version_pos != llvm::StringRef::npos;
2281     llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos);
2282     Mangled mangled(symbol_bare);
2283 
2284     // Now append the suffix back to mangled and unmangled names. Only do it if
2285     // the demangling was successful (string is not empty).
2286     if (has_suffix) {
2287       llvm::StringRef suffix = symbol_ref.substr(version_pos);
2288 
2289       llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef();
2290       if (!mangled_name.empty())
2291         mangled.SetMangledName(ConstString((mangled_name + suffix).str()));
2292 
2293       ConstString demangled = mangled.GetDemangledName();
2294       llvm::StringRef demangled_name = demangled.GetStringRef();
2295       if (!demangled_name.empty())
2296         mangled.SetDemangledName(ConstString((demangled_name + suffix).str()));
2297     }
2298 
2299     // In ELF all symbol should have a valid size but it is not true for some
2300     // function symbols coming from hand written assembly. As none of the
2301     // function symbol should have 0 size we try to calculate the size for
2302     // these symbols in the symtab with saying that their original size is not
2303     // valid.
2304     bool symbol_size_valid =
2305         symbol.st_size != 0 || symbol.getType() != STT_FUNC;
2306 
2307     Symbol dc_symbol(
2308         i + start_id, // ID is the original symbol table index.
2309         mangled,
2310         symbol_type,                    // Type of this symbol
2311         is_global,                      // Is this globally visible?
2312         false,                          // Is this symbol debug info?
2313         false,                          // Is this symbol a trampoline?
2314         false,                          // Is this symbol artificial?
2315         AddressRange(symbol_section_sp, // Section in which this symbol is
2316                                         // defined or null.
2317                      symbol_value,      // Offset in section or symbol value.
2318                      symbol.st_size),   // Size in bytes of this symbol.
2319         symbol_size_valid,              // Symbol size is valid
2320         has_suffix,                     // Contains linker annotations?
2321         flags);                         // Symbol flags.
2322     if (symbol.getBinding() == STB_WEAK)
2323       dc_symbol.SetIsWeak(true);
2324     symtab->AddSymbol(dc_symbol);
2325   }
2326   return i;
2327 }
2328 
2329 unsigned ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
2330                                          user_id_t start_id,
2331                                          lldb_private::Section *symtab) {
2332   if (symtab->GetObjectFile() != this) {
2333     // If the symbol table section is owned by a different object file, have it
2334     // do the parsing.
2335     ObjectFileELF *obj_file_elf =
2336         static_cast<ObjectFileELF *>(symtab->GetObjectFile());
2337     return obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab);
2338   }
2339 
2340   // Get section list for this object file.
2341   SectionList *section_list = m_sections_up.get();
2342   if (!section_list)
2343     return 0;
2344 
2345   user_id_t symtab_id = symtab->GetID();
2346   const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
2347   assert(symtab_hdr->sh_type == SHT_SYMTAB ||
2348          symtab_hdr->sh_type == SHT_DYNSYM);
2349 
2350   // sh_link: section header index of associated string table.
2351   user_id_t strtab_id = symtab_hdr->sh_link;
2352   Section *strtab = section_list->FindSectionByID(strtab_id).get();
2353 
2354   if (symtab && strtab) {
2355     assert(symtab->GetObjectFile() == this);
2356     assert(strtab->GetObjectFile() == this);
2357 
2358     DataExtractor symtab_data;
2359     DataExtractor strtab_data;
2360     if (ReadSectionData(symtab, symtab_data) &&
2361         ReadSectionData(strtab, strtab_data)) {
2362       size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
2363 
2364       return ParseSymbols(symbol_table, start_id, section_list, num_symbols,
2365                           symtab_data, strtab_data);
2366     }
2367   }
2368 
2369   return 0;
2370 }
2371 
2372 size_t ObjectFileELF::ParseDynamicSymbols() {
2373   if (m_dynamic_symbols.size())
2374     return m_dynamic_symbols.size();
2375 
2376   SectionList *section_list = GetSectionList();
2377   if (!section_list)
2378     return 0;
2379 
2380   // Find the SHT_DYNAMIC section.
2381   Section *dynsym =
2382       section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)
2383           .get();
2384   if (!dynsym)
2385     return 0;
2386   assert(dynsym->GetObjectFile() == this);
2387 
2388   ELFDynamic symbol;
2389   DataExtractor dynsym_data;
2390   if (ReadSectionData(dynsym, dynsym_data)) {
2391     const lldb::offset_t section_size = dynsym_data.GetByteSize();
2392     lldb::offset_t cursor = 0;
2393 
2394     while (cursor < section_size) {
2395       if (!symbol.Parse(dynsym_data, &cursor))
2396         break;
2397 
2398       m_dynamic_symbols.push_back(symbol);
2399     }
2400   }
2401 
2402   return m_dynamic_symbols.size();
2403 }
2404 
2405 const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) {
2406   if (!ParseDynamicSymbols())
2407     return nullptr;
2408 
2409   DynamicSymbolCollIter I = m_dynamic_symbols.begin();
2410   DynamicSymbolCollIter E = m_dynamic_symbols.end();
2411   for (; I != E; ++I) {
2412     ELFDynamic *symbol = &*I;
2413 
2414     if (symbol->d_tag == tag)
2415       return symbol;
2416   }
2417 
2418   return nullptr;
2419 }
2420 
2421 unsigned ObjectFileELF::PLTRelocationType() {
2422   // DT_PLTREL
2423   //  This member specifies the type of relocation entry to which the
2424   //  procedure linkage table refers. The d_val member holds DT_REL or
2425   //  DT_RELA, as appropriate. All relocations in a procedure linkage table
2426   //  must use the same relocation.
2427   const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
2428 
2429   if (symbol)
2430     return symbol->d_val;
2431 
2432   return 0;
2433 }
2434 
2435 // Returns the size of the normal plt entries and the offset of the first
2436 // normal plt entry. The 0th entry in the plt table is usually a resolution
2437 // entry which have different size in some architectures then the rest of the
2438 // plt entries.
2439 static std::pair<uint64_t, uint64_t>
2440 GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr,
2441                          const ELFSectionHeader *plt_hdr) {
2442   const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2443 
2444   // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are
2445   // 16 bytes. So round the entsize up by the alignment if addralign is set.
2446   elf_xword plt_entsize =
2447       plt_hdr->sh_addralign
2448           ? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign)
2449           : plt_hdr->sh_entsize;
2450 
2451   // Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly.
2452   // PLT entries relocation code in general requires multiple instruction and
2453   // should be greater than 4 bytes in most cases. Try to guess correct size
2454   // just in case.
2455   if (plt_entsize <= 4) {
2456     // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the
2457     // size of the plt entries based on the number of entries and the size of
2458     // the plt section with the assumption that the size of the 0th entry is at
2459     // least as big as the size of the normal entries and it isn't much bigger
2460     // then that.
2461     if (plt_hdr->sh_addralign)
2462       plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign /
2463                     (num_relocations + 1) * plt_hdr->sh_addralign;
2464     else
2465       plt_entsize = plt_hdr->sh_size / (num_relocations + 1);
2466   }
2467 
2468   elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize;
2469 
2470   return std::make_pair(plt_entsize, plt_offset);
2471 }
2472 
2473 static unsigned ParsePLTRelocations(
2474     Symtab *symbol_table, user_id_t start_id, unsigned rel_type,
2475     const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
2476     const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr,
2477     const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data,
2478     DataExtractor &symtab_data, DataExtractor &strtab_data) {
2479   ELFRelocation rel(rel_type);
2480   ELFSymbol symbol;
2481   lldb::offset_t offset = 0;
2482 
2483   uint64_t plt_offset, plt_entsize;
2484   std::tie(plt_entsize, plt_offset) =
2485       GetPltEntrySizeAndOffset(rel_hdr, plt_hdr);
2486   const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2487 
2488   typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2489   reloc_info_fn reloc_type;
2490   reloc_info_fn reloc_symbol;
2491 
2492   if (hdr->Is32Bit()) {
2493     reloc_type = ELFRelocation::RelocType32;
2494     reloc_symbol = ELFRelocation::RelocSymbol32;
2495   } else {
2496     reloc_type = ELFRelocation::RelocType64;
2497     reloc_symbol = ELFRelocation::RelocSymbol64;
2498   }
2499 
2500   unsigned slot_type = hdr->GetRelocationJumpSlotType();
2501   unsigned i;
2502   for (i = 0; i < num_relocations; ++i) {
2503     if (!rel.Parse(rel_data, &offset))
2504       break;
2505 
2506     if (reloc_type(rel) != slot_type)
2507       continue;
2508 
2509     lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
2510     if (!symbol.Parse(symtab_data, &symbol_offset))
2511       break;
2512 
2513     const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
2514     uint64_t plt_index = plt_offset + i * plt_entsize;
2515 
2516     Symbol jump_symbol(
2517         i + start_id,          // Symbol table index
2518         symbol_name,           // symbol name.
2519         eSymbolTypeTrampoline, // Type of this symbol
2520         false,                 // Is this globally visible?
2521         false,                 // Is this symbol debug info?
2522         true,                  // Is this symbol a trampoline?
2523         true,                  // Is this symbol artificial?
2524         plt_section_sp, // Section in which this symbol is defined or null.
2525         plt_index,      // Offset in section or symbol value.
2526         plt_entsize,    // Size in bytes of this symbol.
2527         true,           // Size is valid
2528         false,          // Contains linker annotations?
2529         0);             // Symbol flags.
2530 
2531     symbol_table->AddSymbol(jump_symbol);
2532   }
2533 
2534   return i;
2535 }
2536 
2537 unsigned
2538 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id,
2539                                       const ELFSectionHeaderInfo *rel_hdr,
2540                                       user_id_t rel_id) {
2541   assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2542 
2543   // The link field points to the associated symbol table.
2544   user_id_t symtab_id = rel_hdr->sh_link;
2545 
2546   // If the link field doesn't point to the appropriate symbol name table then
2547   // try to find it by name as some compiler don't fill in the link fields.
2548   if (!symtab_id)
2549     symtab_id = GetSectionIndexByName(".dynsym");
2550 
2551   // Get PLT section.  We cannot use rel_hdr->sh_info, since current linkers
2552   // point that to the .got.plt or .got section instead of .plt.
2553   user_id_t plt_id = GetSectionIndexByName(".plt");
2554 
2555   if (!symtab_id || !plt_id)
2556     return 0;
2557 
2558   const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
2559   if (!plt_hdr)
2560     return 0;
2561 
2562   const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
2563   if (!sym_hdr)
2564     return 0;
2565 
2566   SectionList *section_list = m_sections_up.get();
2567   if (!section_list)
2568     return 0;
2569 
2570   Section *rel_section = section_list->FindSectionByID(rel_id).get();
2571   if (!rel_section)
2572     return 0;
2573 
2574   SectionSP plt_section_sp(section_list->FindSectionByID(plt_id));
2575   if (!plt_section_sp)
2576     return 0;
2577 
2578   Section *symtab = section_list->FindSectionByID(symtab_id).get();
2579   if (!symtab)
2580     return 0;
2581 
2582   // sh_link points to associated string table.
2583   Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link).get();
2584   if (!strtab)
2585     return 0;
2586 
2587   DataExtractor rel_data;
2588   if (!ReadSectionData(rel_section, rel_data))
2589     return 0;
2590 
2591   DataExtractor symtab_data;
2592   if (!ReadSectionData(symtab, symtab_data))
2593     return 0;
2594 
2595   DataExtractor strtab_data;
2596   if (!ReadSectionData(strtab, strtab_data))
2597     return 0;
2598 
2599   unsigned rel_type = PLTRelocationType();
2600   if (!rel_type)
2601     return 0;
2602 
2603   return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header,
2604                              rel_hdr, plt_hdr, sym_hdr, plt_section_sp,
2605                              rel_data, symtab_data, strtab_data);
2606 }
2607 
2608 static void ApplyELF64ABS64Relocation(Symtab *symtab, ELFRelocation &rel,
2609                                       DataExtractor &debug_data,
2610                                       Section *rel_section) {
2611   Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel));
2612   if (symbol) {
2613     addr_t value = symbol->GetAddressRef().GetFileAddress();
2614     DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2615     // ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2616     WritableDataBuffer *data_buffer =
2617         llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2618     uint64_t *dst = reinterpret_cast<uint64_t *>(
2619         data_buffer->GetBytes() + rel_section->GetFileOffset() +
2620         ELFRelocation::RelocOffset64(rel));
2621     uint64_t val_offset = value + ELFRelocation::RelocAddend64(rel);
2622     memcpy(dst, &val_offset, sizeof(uint64_t));
2623   }
2624 }
2625 
2626 static void ApplyELF64ABS32Relocation(Symtab *symtab, ELFRelocation &rel,
2627                                       DataExtractor &debug_data,
2628                                       Section *rel_section, bool is_signed) {
2629   Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel));
2630   if (symbol) {
2631     addr_t value = symbol->GetAddressRef().GetFileAddress();
2632     value += ELFRelocation::RelocAddend32(rel);
2633     if ((!is_signed && (value > UINT32_MAX)) ||
2634         (is_signed &&
2635          ((int64_t)value > INT32_MAX || (int64_t)value < INT32_MIN))) {
2636       Log *log = GetLog(LLDBLog::Modules);
2637       LLDB_LOGF(log, "Failed to apply debug info relocations");
2638       return;
2639     }
2640     uint32_t truncated_addr = (value & 0xFFFFFFFF);
2641     DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2642     // ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2643     WritableDataBuffer *data_buffer =
2644         llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2645     uint32_t *dst = reinterpret_cast<uint32_t *>(
2646         data_buffer->GetBytes() + rel_section->GetFileOffset() +
2647         ELFRelocation::RelocOffset32(rel));
2648     memcpy(dst, &truncated_addr, sizeof(uint32_t));
2649   }
2650 }
2651 
2652 static void ApplyELF32ABS32RelRelocation(Symtab *symtab, ELFRelocation &rel,
2653                                          DataExtractor &debug_data,
2654                                          Section *rel_section) {
2655   Log *log = GetLog(LLDBLog::Modules);
2656   Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol32(rel));
2657   if (symbol) {
2658     addr_t value = symbol->GetAddressRef().GetFileAddress();
2659     if (value == LLDB_INVALID_ADDRESS) {
2660       const char *name = symbol->GetName().GetCString();
2661       LLDB_LOGF(log, "Debug info symbol invalid: %s", name);
2662       return;
2663     }
2664     assert(llvm::isUInt<32>(value) && "Valid addresses are 32-bit");
2665     DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2666     // ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2667     WritableDataBuffer *data_buffer =
2668         llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2669     uint8_t *dst = data_buffer->GetBytes() + rel_section->GetFileOffset() +
2670                    ELFRelocation::RelocOffset32(rel);
2671     // Implicit addend is stored inline as a signed value.
2672     int32_t addend;
2673     memcpy(&addend, dst, sizeof(int32_t));
2674     // The sum must be positive. This extra check prevents UB from overflow in
2675     // the actual range check below.
2676     if (addend < 0 && static_cast<uint32_t>(-addend) > value) {
2677       LLDB_LOGF(log, "Debug info relocation overflow: 0x%" PRIx64,
2678                 static_cast<int64_t>(value) + addend);
2679       return;
2680     }
2681     if (!llvm::isUInt<32>(value + addend)) {
2682       LLDB_LOGF(log, "Debug info relocation out of range: 0x%" PRIx64, value);
2683       return;
2684     }
2685     uint32_t addr = value + addend;
2686     memcpy(dst, &addr, sizeof(uint32_t));
2687   }
2688 }
2689 
2690 unsigned ObjectFileELF::ApplyRelocations(
2691     Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
2692     const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,
2693     DataExtractor &rel_data, DataExtractor &symtab_data,
2694     DataExtractor &debug_data, Section *rel_section) {
2695   ELFRelocation rel(rel_hdr->sh_type);
2696   lldb::addr_t offset = 0;
2697   const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2698   typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2699   reloc_info_fn reloc_type;
2700   reloc_info_fn reloc_symbol;
2701 
2702   if (hdr->Is32Bit()) {
2703     reloc_type = ELFRelocation::RelocType32;
2704     reloc_symbol = ELFRelocation::RelocSymbol32;
2705   } else {
2706     reloc_type = ELFRelocation::RelocType64;
2707     reloc_symbol = ELFRelocation::RelocSymbol64;
2708   }
2709 
2710   for (unsigned i = 0; i < num_relocations; ++i) {
2711     if (!rel.Parse(rel_data, &offset)) {
2712       GetModule()->ReportError(".rel{0}[{1:d}] failed to parse relocation",
2713                                rel_section->GetName().AsCString(), i);
2714       break;
2715     }
2716     Symbol *symbol = nullptr;
2717 
2718     if (hdr->Is32Bit()) {
2719       switch (hdr->e_machine) {
2720       case llvm::ELF::EM_ARM:
2721         switch (reloc_type(rel)) {
2722         case R_ARM_ABS32:
2723           ApplyELF32ABS32RelRelocation(symtab, rel, debug_data, rel_section);
2724           break;
2725         case R_ARM_REL32:
2726           GetModule()->ReportError("unsupported AArch32 relocation:"
2727                                    " .rel{0}[{1}], type {2}",
2728                                    rel_section->GetName().AsCString(), i,
2729                                    reloc_type(rel));
2730           break;
2731         default:
2732           assert(false && "unexpected relocation type");
2733         }
2734         break;
2735       case llvm::ELF::EM_386:
2736         switch (reloc_type(rel)) {
2737         case R_386_32:
2738           symbol = symtab->FindSymbolByID(reloc_symbol(rel));
2739           if (symbol) {
2740             addr_t f_offset =
2741                 rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel);
2742             DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2743             // ObjectFileELF creates a WritableDataBuffer in CreateInstance.
2744             WritableDataBuffer *data_buffer =
2745                 llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
2746             uint32_t *dst = reinterpret_cast<uint32_t *>(
2747                 data_buffer->GetBytes() + f_offset);
2748 
2749             addr_t value = symbol->GetAddressRef().GetFileAddress();
2750             if (rel.IsRela()) {
2751               value += ELFRelocation::RelocAddend32(rel);
2752             } else {
2753               value += *dst;
2754             }
2755             *dst = value;
2756           } else {
2757             GetModule()->ReportError(".rel{0}[{1}] unknown symbol id: {2:d}",
2758                                     rel_section->GetName().AsCString(), i,
2759                                     reloc_symbol(rel));
2760           }
2761           break;
2762         case R_386_NONE:
2763         case R_386_PC32:
2764           GetModule()->ReportError("unsupported i386 relocation:"
2765                                    " .rel{0}[{1}], type {2}",
2766                                    rel_section->GetName().AsCString(), i,
2767                                    reloc_type(rel));
2768           break;
2769         default:
2770           assert(false && "unexpected relocation type");
2771           break;
2772         }
2773         break;
2774       default:
2775         GetModule()->ReportError("unsupported 32-bit ELF machine arch: {0}", hdr->e_machine);
2776         break;
2777       }
2778     } else {
2779       switch (hdr->e_machine) {
2780       case llvm::ELF::EM_AARCH64:
2781         switch (reloc_type(rel)) {
2782         case R_AARCH64_ABS64:
2783           ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);
2784           break;
2785         case R_AARCH64_ABS32:
2786           ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);
2787           break;
2788         default:
2789           assert(false && "unexpected relocation type");
2790         }
2791         break;
2792       case llvm::ELF::EM_LOONGARCH:
2793         switch (reloc_type(rel)) {
2794         case R_LARCH_64:
2795           ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);
2796           break;
2797         case R_LARCH_32:
2798           ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);
2799           break;
2800         default:
2801           assert(false && "unexpected relocation type");
2802         }
2803         break;
2804       case llvm::ELF::EM_X86_64:
2805         switch (reloc_type(rel)) {
2806         case R_X86_64_64:
2807           ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);
2808           break;
2809         case R_X86_64_32:
2810           ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section,
2811                                     false);
2812           break;
2813         case R_X86_64_32S:
2814           ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);
2815           break;
2816         case R_X86_64_PC32:
2817         default:
2818           assert(false && "unexpected relocation type");
2819         }
2820         break;
2821       default:
2822         GetModule()->ReportError("unsupported 64-bit ELF machine arch: {0}", hdr->e_machine);
2823         break;
2824       }
2825     }
2826   }
2827 
2828   return 0;
2829 }
2830 
2831 unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr,
2832                                               user_id_t rel_id,
2833                                               lldb_private::Symtab *thetab) {
2834   assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2835 
2836   // Parse in the section list if needed.
2837   SectionList *section_list = GetSectionList();
2838   if (!section_list)
2839     return 0;
2840 
2841   user_id_t symtab_id = rel_hdr->sh_link;
2842   user_id_t debug_id = rel_hdr->sh_info;
2843 
2844   const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
2845   if (!symtab_hdr)
2846     return 0;
2847 
2848   const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);
2849   if (!debug_hdr)
2850     return 0;
2851 
2852   Section *rel = section_list->FindSectionByID(rel_id).get();
2853   if (!rel)
2854     return 0;
2855 
2856   Section *symtab = section_list->FindSectionByID(symtab_id).get();
2857   if (!symtab)
2858     return 0;
2859 
2860   Section *debug = section_list->FindSectionByID(debug_id).get();
2861   if (!debug)
2862     return 0;
2863 
2864   DataExtractor rel_data;
2865   DataExtractor symtab_data;
2866   DataExtractor debug_data;
2867 
2868   if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) &&
2869       GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) &&
2870       GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) {
2871     ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr,
2872                      rel_data, symtab_data, debug_data, debug);
2873   }
2874 
2875   return 0;
2876 }
2877 
2878 void ObjectFileELF::ParseSymtab(Symtab &lldb_symtab) {
2879   ModuleSP module_sp(GetModule());
2880   if (!module_sp)
2881     return;
2882 
2883   Progress progress(
2884       llvm::formatv("Parsing symbol table for {0}",
2885                     m_file.GetFilename().AsCString("<Unknown>")));
2886   ElapsedTime elapsed(module_sp->GetSymtabParseTime());
2887 
2888   // We always want to use the main object file so we (hopefully) only have one
2889   // cached copy of our symtab, dynamic sections, etc.
2890   ObjectFile *module_obj_file = module_sp->GetObjectFile();
2891   if (module_obj_file && module_obj_file != this)
2892     return module_obj_file->ParseSymtab(lldb_symtab);
2893 
2894   SectionList *section_list = module_sp->GetSectionList();
2895   if (!section_list)
2896     return;
2897 
2898   uint64_t symbol_id = 0;
2899 
2900   // Sharable objects and dynamic executables usually have 2 distinct symbol
2901   // tables, one named ".symtab", and the other ".dynsym". The dynsym is a
2902   // smaller version of the symtab that only contains global symbols. The
2903   // information found in the dynsym is therefore also found in the symtab,
2904   // while the reverse is not necessarily true.
2905   Section *symtab =
2906       section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get();
2907   if (symtab)
2908     symbol_id += ParseSymbolTable(&lldb_symtab, symbol_id, symtab);
2909 
2910   // The symtab section is non-allocable and can be stripped, while the
2911   // .dynsym section which should always be always be there. To support the
2912   // minidebuginfo case we parse .dynsym when there's a .gnu_debuginfo
2913   // section, nomatter if .symtab was already parsed or not. This is because
2914   // minidebuginfo normally removes the .symtab symbols which have their
2915   // matching .dynsym counterparts.
2916   if (!symtab ||
2917       GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"))) {
2918     Section *dynsym =
2919         section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true)
2920             .get();
2921     if (dynsym)
2922       symbol_id += ParseSymbolTable(&lldb_symtab, symbol_id, dynsym);
2923   }
2924 
2925   // DT_JMPREL
2926   //      If present, this entry's d_ptr member holds the address of
2927   //      relocation
2928   //      entries associated solely with the procedure linkage table.
2929   //      Separating
2930   //      these relocation entries lets the dynamic linker ignore them during
2931   //      process initialization, if lazy binding is enabled. If this entry is
2932   //      present, the related entries of types DT_PLTRELSZ and DT_PLTREL must
2933   //      also be present.
2934   const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
2935   if (symbol) {
2936     // Synthesize trampoline symbols to help navigate the PLT.
2937     addr_t addr = symbol->d_ptr;
2938     Section *reloc_section =
2939         section_list->FindSectionContainingFileAddress(addr).get();
2940     if (reloc_section) {
2941       user_id_t reloc_id = reloc_section->GetID();
2942       const ELFSectionHeaderInfo *reloc_header =
2943           GetSectionHeaderByIndex(reloc_id);
2944       if (reloc_header)
2945         ParseTrampolineSymbols(&lldb_symtab, symbol_id, reloc_header, reloc_id);
2946     }
2947   }
2948 
2949   if (DWARFCallFrameInfo *eh_frame =
2950           GetModule()->GetUnwindTable().GetEHFrameInfo()) {
2951     ParseUnwindSymbols(&lldb_symtab, eh_frame);
2952   }
2953 
2954   // In the event that there's no symbol entry for the entry point we'll
2955   // artificially create one. We delegate to the symtab object the figuring
2956   // out of the proper size, this will usually make it span til the next
2957   // symbol it finds in the section. This means that if there are missing
2958   // symbols the entry point might span beyond its function definition.
2959   // We're fine with this as it doesn't make it worse than not having a
2960   // symbol entry at all.
2961   if (CalculateType() == eTypeExecutable) {
2962     ArchSpec arch = GetArchitecture();
2963     auto entry_point_addr = GetEntryPointAddress();
2964     bool is_valid_entry_point =
2965         entry_point_addr.IsValid() && entry_point_addr.IsSectionOffset();
2966     addr_t entry_point_file_addr = entry_point_addr.GetFileAddress();
2967     if (is_valid_entry_point && !lldb_symtab.FindSymbolContainingFileAddress(
2968                                     entry_point_file_addr)) {
2969       uint64_t symbol_id = lldb_symtab.GetNumSymbols();
2970       // Don't set the name for any synthetic symbols, the Symbol
2971       // object will generate one if needed when the name is accessed
2972       // via accessors.
2973       SectionSP section_sp = entry_point_addr.GetSection();
2974       Symbol symbol(
2975           /*symID=*/symbol_id,
2976           /*name=*/llvm::StringRef(), // Name will be auto generated.
2977           /*type=*/eSymbolTypeCode,
2978           /*external=*/true,
2979           /*is_debug=*/false,
2980           /*is_trampoline=*/false,
2981           /*is_artificial=*/true,
2982           /*section_sp=*/section_sp,
2983           /*offset=*/0,
2984           /*size=*/0, // FDE can span multiple symbols so don't use its size.
2985           /*size_is_valid=*/false,
2986           /*contains_linker_annotations=*/false,
2987           /*flags=*/0);
2988       // When the entry point is arm thumb we need to explicitly set its
2989       // class address to reflect that. This is important because expression
2990       // evaluation relies on correctly setting a breakpoint at this
2991       // address.
2992       if (arch.GetMachine() == llvm::Triple::arm &&
2993           (entry_point_file_addr & 1)) {
2994         symbol.GetAddressRef().SetOffset(entry_point_addr.GetOffset() ^ 1);
2995         m_address_class_map[entry_point_file_addr ^ 1] =
2996             AddressClass::eCodeAlternateISA;
2997       } else {
2998         m_address_class_map[entry_point_file_addr] = AddressClass::eCode;
2999       }
3000       lldb_symtab.AddSymbol(symbol);
3001     }
3002   }
3003 }
3004 
3005 void ObjectFileELF::RelocateSection(lldb_private::Section *section)
3006 {
3007   static const char *debug_prefix = ".debug";
3008 
3009   // Set relocated bit so we stop getting called, regardless of whether we
3010   // actually relocate.
3011   section->SetIsRelocated(true);
3012 
3013   // We only relocate in ELF relocatable files
3014   if (CalculateType() != eTypeObjectFile)
3015     return;
3016 
3017   const char *section_name = section->GetName().GetCString();
3018   // Can't relocate that which can't be named
3019   if (section_name == nullptr)
3020     return;
3021 
3022   // We don't relocate non-debug sections at the moment
3023   if (strncmp(section_name, debug_prefix, strlen(debug_prefix)))
3024     return;
3025 
3026   // Relocation section names to look for
3027   std::string needle = std::string(".rel") + section_name;
3028   std::string needlea = std::string(".rela") + section_name;
3029 
3030   for (SectionHeaderCollIter I = m_section_headers.begin();
3031        I != m_section_headers.end(); ++I) {
3032     if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) {
3033       const char *hay_name = I->section_name.GetCString();
3034       if (hay_name == nullptr)
3035         continue;
3036       if (needle == hay_name || needlea == hay_name) {
3037         const ELFSectionHeader &reloc_header = *I;
3038         user_id_t reloc_id = SectionIndex(I);
3039         RelocateDebugSections(&reloc_header, reloc_id, GetSymtab());
3040         break;
3041       }
3042     }
3043   }
3044 }
3045 
3046 void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table,
3047                                        DWARFCallFrameInfo *eh_frame) {
3048   SectionList *section_list = GetSectionList();
3049   if (!section_list)
3050     return;
3051 
3052   // First we save the new symbols into a separate list and add them to the
3053   // symbol table after we collected all symbols we want to add. This is
3054   // neccessary because adding a new symbol invalidates the internal index of
3055   // the symtab what causing the next lookup to be slow because it have to
3056   // recalculate the index first.
3057   std::vector<Symbol> new_symbols;
3058 
3059   size_t num_symbols = symbol_table->GetNumSymbols();
3060   uint64_t last_symbol_id =
3061       num_symbols ? symbol_table->SymbolAtIndex(num_symbols - 1)->GetID() : 0;
3062   eh_frame->ForEachFDEEntries([&](lldb::addr_t file_addr, uint32_t size,
3063                                   dw_offset_t) {
3064     Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr);
3065     if (symbol) {
3066       if (!symbol->GetByteSizeIsValid()) {
3067         symbol->SetByteSize(size);
3068         symbol->SetSizeIsSynthesized(true);
3069       }
3070     } else {
3071       SectionSP section_sp =
3072           section_list->FindSectionContainingFileAddress(file_addr);
3073       if (section_sp) {
3074         addr_t offset = file_addr - section_sp->GetFileAddress();
3075         uint64_t symbol_id = ++last_symbol_id;
3076         // Don't set the name for any synthetic symbols, the Symbol
3077         // object will generate one if needed when the name is accessed
3078         // via accessors.
3079         Symbol eh_symbol(
3080             /*symID=*/symbol_id,
3081             /*name=*/llvm::StringRef(), // Name will be auto generated.
3082             /*type=*/eSymbolTypeCode,
3083             /*external=*/true,
3084             /*is_debug=*/false,
3085             /*is_trampoline=*/false,
3086             /*is_artificial=*/true,
3087             /*section_sp=*/section_sp,
3088             /*offset=*/offset,
3089             /*size=*/0, // FDE can span multiple symbols so don't use its size.
3090             /*size_is_valid=*/false,
3091             /*contains_linker_annotations=*/false,
3092             /*flags=*/0);
3093         new_symbols.push_back(eh_symbol);
3094       }
3095     }
3096     return true;
3097   });
3098 
3099   for (const Symbol &s : new_symbols)
3100     symbol_table->AddSymbol(s);
3101 }
3102 
3103 bool ObjectFileELF::IsStripped() {
3104   // TODO: determine this for ELF
3105   return false;
3106 }
3107 
3108 //===----------------------------------------------------------------------===//
3109 // Dump
3110 //
3111 // Dump the specifics of the runtime file container (such as any headers
3112 // segments, sections, etc).
3113 void ObjectFileELF::Dump(Stream *s) {
3114   ModuleSP module_sp(GetModule());
3115   if (!module_sp) {
3116     return;
3117   }
3118 
3119   std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
3120   s->Printf("%p: ", static_cast<void *>(this));
3121   s->Indent();
3122   s->PutCString("ObjectFileELF");
3123 
3124   ArchSpec header_arch = GetArchitecture();
3125 
3126   *s << ", file = '" << m_file
3127      << "', arch = " << header_arch.GetArchitectureName() << "\n";
3128 
3129   DumpELFHeader(s, m_header);
3130   s->EOL();
3131   DumpELFProgramHeaders(s);
3132   s->EOL();
3133   DumpELFSectionHeaders(s);
3134   s->EOL();
3135   SectionList *section_list = GetSectionList();
3136   if (section_list)
3137     section_list->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
3138                        UINT32_MAX);
3139   Symtab *symtab = GetSymtab();
3140   if (symtab)
3141     symtab->Dump(s, nullptr, eSortOrderNone);
3142   s->EOL();
3143   DumpDependentModules(s);
3144   s->EOL();
3145 }
3146 
3147 // DumpELFHeader
3148 //
3149 // Dump the ELF header to the specified output stream
3150 void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) {
3151   s->PutCString("ELF Header\n");
3152   s->Printf("e_ident[EI_MAG0   ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
3153   s->Printf("e_ident[EI_MAG1   ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1],
3154             header.e_ident[EI_MAG1]);
3155   s->Printf("e_ident[EI_MAG2   ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2],
3156             header.e_ident[EI_MAG2]);
3157   s->Printf("e_ident[EI_MAG3   ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3],
3158             header.e_ident[EI_MAG3]);
3159 
3160   s->Printf("e_ident[EI_CLASS  ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
3161   s->Printf("e_ident[EI_DATA   ] = 0x%2.2x ", header.e_ident[EI_DATA]);
3162   DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
3163   s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
3164   s->Printf("e_ident[EI_PAD    ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
3165 
3166   s->Printf("e_type      = 0x%4.4x ", header.e_type);
3167   DumpELFHeader_e_type(s, header.e_type);
3168   s->Printf("\ne_machine   = 0x%4.4x\n", header.e_machine);
3169   s->Printf("e_version   = 0x%8.8x\n", header.e_version);
3170   s->Printf("e_entry     = 0x%8.8" PRIx64 "\n", header.e_entry);
3171   s->Printf("e_phoff     = 0x%8.8" PRIx64 "\n", header.e_phoff);
3172   s->Printf("e_shoff     = 0x%8.8" PRIx64 "\n", header.e_shoff);
3173   s->Printf("e_flags     = 0x%8.8x\n", header.e_flags);
3174   s->Printf("e_ehsize    = 0x%4.4x\n", header.e_ehsize);
3175   s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
3176   s->Printf("e_phnum     = 0x%8.8x\n", header.e_phnum);
3177   s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
3178   s->Printf("e_shnum     = 0x%8.8x\n", header.e_shnum);
3179   s->Printf("e_shstrndx  = 0x%8.8x\n", header.e_shstrndx);
3180 }
3181 
3182 // DumpELFHeader_e_type
3183 //
3184 // Dump an token value for the ELF header member e_type
3185 void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) {
3186   switch (e_type) {
3187   case ET_NONE:
3188     *s << "ET_NONE";
3189     break;
3190   case ET_REL:
3191     *s << "ET_REL";
3192     break;
3193   case ET_EXEC:
3194     *s << "ET_EXEC";
3195     break;
3196   case ET_DYN:
3197     *s << "ET_DYN";
3198     break;
3199   case ET_CORE:
3200     *s << "ET_CORE";
3201     break;
3202   default:
3203     break;
3204   }
3205 }
3206 
3207 // DumpELFHeader_e_ident_EI_DATA
3208 //
3209 // Dump an token value for the ELF header member e_ident[EI_DATA]
3210 void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s,
3211                                                   unsigned char ei_data) {
3212   switch (ei_data) {
3213   case ELFDATANONE:
3214     *s << "ELFDATANONE";
3215     break;
3216   case ELFDATA2LSB:
3217     *s << "ELFDATA2LSB - Little Endian";
3218     break;
3219   case ELFDATA2MSB:
3220     *s << "ELFDATA2MSB - Big Endian";
3221     break;
3222   default:
3223     break;
3224   }
3225 }
3226 
3227 // DumpELFProgramHeader
3228 //
3229 // Dump a single ELF program header to the specified output stream
3230 void ObjectFileELF::DumpELFProgramHeader(Stream *s,
3231                                          const ELFProgramHeader &ph) {
3232   DumpELFProgramHeader_p_type(s, ph.p_type);
3233   s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset,
3234             ph.p_vaddr, ph.p_paddr);
3235   s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz,
3236             ph.p_flags);
3237 
3238   DumpELFProgramHeader_p_flags(s, ph.p_flags);
3239   s->Printf(") %8.8" PRIx64, ph.p_align);
3240 }
3241 
3242 // DumpELFProgramHeader_p_type
3243 //
3244 // Dump an token value for the ELF program header member p_type which describes
3245 // the type of the program header
3246 void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) {
3247   const int kStrWidth = 15;
3248   switch (p_type) {
3249     CASE_AND_STREAM(s, PT_NULL, kStrWidth);
3250     CASE_AND_STREAM(s, PT_LOAD, kStrWidth);
3251     CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth);
3252     CASE_AND_STREAM(s, PT_INTERP, kStrWidth);
3253     CASE_AND_STREAM(s, PT_NOTE, kStrWidth);
3254     CASE_AND_STREAM(s, PT_SHLIB, kStrWidth);
3255     CASE_AND_STREAM(s, PT_PHDR, kStrWidth);
3256     CASE_AND_STREAM(s, PT_TLS, kStrWidth);
3257     CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
3258   default:
3259     s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
3260     break;
3261   }
3262 }
3263 
3264 // DumpELFProgramHeader_p_flags
3265 //
3266 // Dump an token value for the ELF program header member p_flags
3267 void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) {
3268   *s << ((p_flags & PF_X) ? "PF_X" : "    ")
3269      << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
3270      << ((p_flags & PF_W) ? "PF_W" : "    ")
3271      << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
3272      << ((p_flags & PF_R) ? "PF_R" : "    ");
3273 }
3274 
3275 // DumpELFProgramHeaders
3276 //
3277 // Dump all of the ELF program header to the specified output stream
3278 void ObjectFileELF::DumpELFProgramHeaders(Stream *s) {
3279   if (!ParseProgramHeaders())
3280     return;
3281 
3282   s->PutCString("Program Headers\n");
3283   s->PutCString("IDX  p_type          p_offset p_vaddr  p_paddr  "
3284                 "p_filesz p_memsz  p_flags                   p_align\n");
3285   s->PutCString("==== --------------- -------- -------- -------- "
3286                 "-------- -------- ------------------------- --------\n");
3287 
3288   for (const auto &H : llvm::enumerate(m_program_headers)) {
3289     s->Format("[{0,2}] ", H.index());
3290     ObjectFileELF::DumpELFProgramHeader(s, H.value());
3291     s->EOL();
3292   }
3293 }
3294 
3295 // DumpELFSectionHeader
3296 //
3297 // Dump a single ELF section header to the specified output stream
3298 void ObjectFileELF::DumpELFSectionHeader(Stream *s,
3299                                          const ELFSectionHeaderInfo &sh) {
3300   s->Printf("%8.8x ", sh.sh_name);
3301   DumpELFSectionHeader_sh_type(s, sh.sh_type);
3302   s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
3303   DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
3304   s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr,
3305             sh.sh_offset, sh.sh_size);
3306   s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
3307   s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
3308 }
3309 
3310 // DumpELFSectionHeader_sh_type
3311 //
3312 // Dump an token value for the ELF section header member sh_type which
3313 // describes the type of the section
3314 void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) {
3315   const int kStrWidth = 12;
3316   switch (sh_type) {
3317     CASE_AND_STREAM(s, SHT_NULL, kStrWidth);
3318     CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth);
3319     CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth);
3320     CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth);
3321     CASE_AND_STREAM(s, SHT_RELA, kStrWidth);
3322     CASE_AND_STREAM(s, SHT_HASH, kStrWidth);
3323     CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth);
3324     CASE_AND_STREAM(s, SHT_NOTE, kStrWidth);
3325     CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth);
3326     CASE_AND_STREAM(s, SHT_REL, kStrWidth);
3327     CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth);
3328     CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth);
3329     CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth);
3330     CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth);
3331     CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth);
3332     CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth);
3333   default:
3334     s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
3335     break;
3336   }
3337 }
3338 
3339 // DumpELFSectionHeader_sh_flags
3340 //
3341 // Dump an token value for the ELF section header member sh_flags
3342 void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s,
3343                                                   elf_xword sh_flags) {
3344   *s << ((sh_flags & SHF_WRITE) ? "WRITE" : "     ")
3345      << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
3346      << ((sh_flags & SHF_ALLOC) ? "ALLOC" : "     ")
3347      << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
3348      << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : "         ");
3349 }
3350 
3351 // DumpELFSectionHeaders
3352 //
3353 // Dump all of the ELF section header to the specified output stream
3354 void ObjectFileELF::DumpELFSectionHeaders(Stream *s) {
3355   if (!ParseSectionHeaders())
3356     return;
3357 
3358   s->PutCString("Section Headers\n");
3359   s->PutCString("IDX  name     type         flags                            "
3360                 "addr     offset   size     link     info     addralgn "
3361                 "entsize  Name\n");
3362   s->PutCString("==== -------- ------------ -------------------------------- "
3363                 "-------- -------- -------- -------- -------- -------- "
3364                 "-------- ====================\n");
3365 
3366   uint32_t idx = 0;
3367   for (SectionHeaderCollConstIter I = m_section_headers.begin();
3368        I != m_section_headers.end(); ++I, ++idx) {
3369     s->Printf("[%2u] ", idx);
3370     ObjectFileELF::DumpELFSectionHeader(s, *I);
3371     const char *section_name = I->section_name.AsCString("");
3372     if (section_name)
3373       *s << ' ' << section_name << "\n";
3374   }
3375 }
3376 
3377 void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) {
3378   size_t num_modules = ParseDependentModules();
3379 
3380   if (num_modules > 0) {
3381     s->PutCString("Dependent Modules:\n");
3382     for (unsigned i = 0; i < num_modules; ++i) {
3383       const FileSpec &spec = m_filespec_up->GetFileSpecAtIndex(i);
3384       s->Printf("   %s\n", spec.GetFilename().GetCString());
3385     }
3386   }
3387 }
3388 
3389 ArchSpec ObjectFileELF::GetArchitecture() {
3390   if (!ParseHeader())
3391     return ArchSpec();
3392 
3393   if (m_section_headers.empty()) {
3394     // Allow elf notes to be parsed which may affect the detected architecture.
3395     ParseSectionHeaders();
3396   }
3397 
3398   if (CalculateType() == eTypeCoreFile &&
3399       !m_arch_spec.TripleOSWasSpecified()) {
3400     // Core files don't have section headers yet they have PT_NOTE program
3401     // headers that might shed more light on the architecture
3402     for (const elf::ELFProgramHeader &H : ProgramHeaders()) {
3403       if (H.p_type != PT_NOTE || H.p_offset == 0 || H.p_filesz == 0)
3404         continue;
3405       DataExtractor data;
3406       if (data.SetData(m_data, H.p_offset, H.p_filesz) == H.p_filesz) {
3407         UUID uuid;
3408         RefineModuleDetailsFromNote(data, m_arch_spec, uuid);
3409       }
3410     }
3411   }
3412   return m_arch_spec;
3413 }
3414 
3415 ObjectFile::Type ObjectFileELF::CalculateType() {
3416   switch (m_header.e_type) {
3417   case llvm::ELF::ET_NONE:
3418     // 0 - No file type
3419     return eTypeUnknown;
3420 
3421   case llvm::ELF::ET_REL:
3422     // 1 - Relocatable file
3423     return eTypeObjectFile;
3424 
3425   case llvm::ELF::ET_EXEC:
3426     // 2 - Executable file
3427     return eTypeExecutable;
3428 
3429   case llvm::ELF::ET_DYN:
3430     // 3 - Shared object file
3431     return eTypeSharedLibrary;
3432 
3433   case ET_CORE:
3434     // 4 - Core file
3435     return eTypeCoreFile;
3436 
3437   default:
3438     break;
3439   }
3440   return eTypeUnknown;
3441 }
3442 
3443 ObjectFile::Strata ObjectFileELF::CalculateStrata() {
3444   switch (m_header.e_type) {
3445   case llvm::ELF::ET_NONE:
3446     // 0 - No file type
3447     return eStrataUnknown;
3448 
3449   case llvm::ELF::ET_REL:
3450     // 1 - Relocatable file
3451     return eStrataUnknown;
3452 
3453   case llvm::ELF::ET_EXEC:
3454     // 2 - Executable file
3455     // TODO: is there any way to detect that an executable is a kernel
3456     // related executable by inspecting the program headers, section headers,
3457     // symbols, or any other flag bits???
3458     return eStrataUser;
3459 
3460   case llvm::ELF::ET_DYN:
3461     // 3 - Shared object file
3462     // TODO: is there any way to detect that an shared library is a kernel
3463     // related executable by inspecting the program headers, section headers,
3464     // symbols, or any other flag bits???
3465     return eStrataUnknown;
3466 
3467   case ET_CORE:
3468     // 4 - Core file
3469     // TODO: is there any way to detect that an core file is a kernel
3470     // related executable by inspecting the program headers, section headers,
3471     // symbols, or any other flag bits???
3472     return eStrataUnknown;
3473 
3474   default:
3475     break;
3476   }
3477   return eStrataUnknown;
3478 }
3479 
3480 size_t ObjectFileELF::ReadSectionData(Section *section,
3481                        lldb::offset_t section_offset, void *dst,
3482                        size_t dst_len) {
3483   // If some other objectfile owns this data, pass this to them.
3484   if (section->GetObjectFile() != this)
3485     return section->GetObjectFile()->ReadSectionData(section, section_offset,
3486                                                      dst, dst_len);
3487 
3488   if (!section->Test(SHF_COMPRESSED))
3489     return ObjectFile::ReadSectionData(section, section_offset, dst, dst_len);
3490 
3491   // For compressed sections we need to read to full data to be able to
3492   // decompress.
3493   DataExtractor data;
3494   ReadSectionData(section, data);
3495   return data.CopyData(section_offset, dst_len, dst);
3496 }
3497 
3498 size_t ObjectFileELF::ReadSectionData(Section *section,
3499                                       DataExtractor &section_data) {
3500   // If some other objectfile owns this data, pass this to them.
3501   if (section->GetObjectFile() != this)
3502     return section->GetObjectFile()->ReadSectionData(section, section_data);
3503 
3504   size_t result = ObjectFile::ReadSectionData(section, section_data);
3505   if (result == 0 || !(section->Get() & llvm::ELF::SHF_COMPRESSED))
3506     return result;
3507 
3508   auto Decompressor = llvm::object::Decompressor::create(
3509       section->GetName().GetStringRef(),
3510       {reinterpret_cast<const char *>(section_data.GetDataStart()),
3511        size_t(section_data.GetByteSize())},
3512       GetByteOrder() == eByteOrderLittle, GetAddressByteSize() == 8);
3513   if (!Decompressor) {
3514     GetModule()->ReportWarning(
3515         "Unable to initialize decompressor for section '{0}': {1}",
3516         section->GetName().GetCString(),
3517         llvm::toString(Decompressor.takeError()).c_str());
3518     section_data.Clear();
3519     return 0;
3520   }
3521 
3522   auto buffer_sp =
3523       std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0);
3524   if (auto error = Decompressor->decompress(
3525           {buffer_sp->GetBytes(), size_t(buffer_sp->GetByteSize())})) {
3526     GetModule()->ReportWarning("Decompression of section '{0}' failed: {1}",
3527                                section->GetName().GetCString(),
3528                                llvm::toString(std::move(error)).c_str());
3529     section_data.Clear();
3530     return 0;
3531   }
3532 
3533   section_data.SetData(buffer_sp);
3534   return buffer_sp->GetByteSize();
3535 }
3536 
3537 llvm::ArrayRef<ELFProgramHeader> ObjectFileELF::ProgramHeaders() {
3538   ParseProgramHeaders();
3539   return m_program_headers;
3540 }
3541 
3542 DataExtractor ObjectFileELF::GetSegmentData(const ELFProgramHeader &H) {
3543   return DataExtractor(m_data, H.p_offset, H.p_filesz);
3544 }
3545 
3546 bool ObjectFileELF::AnySegmentHasPhysicalAddress() {
3547   for (const ELFProgramHeader &H : ProgramHeaders()) {
3548     if (H.p_paddr != 0)
3549       return true;
3550   }
3551   return false;
3552 }
3553 
3554 std::vector<ObjectFile::LoadableData>
3555 ObjectFileELF::GetLoadableData(Target &target) {
3556   // Create a list of loadable data from loadable segments, using physical
3557   // addresses if they aren't all null
3558   std::vector<LoadableData> loadables;
3559   bool should_use_paddr = AnySegmentHasPhysicalAddress();
3560   for (const ELFProgramHeader &H : ProgramHeaders()) {
3561     LoadableData loadable;
3562     if (H.p_type != llvm::ELF::PT_LOAD)
3563       continue;
3564     loadable.Dest = should_use_paddr ? H.p_paddr : H.p_vaddr;
3565     if (loadable.Dest == LLDB_INVALID_ADDRESS)
3566       continue;
3567     if (H.p_filesz == 0)
3568       continue;
3569     auto segment_data = GetSegmentData(H);
3570     loadable.Contents = llvm::ArrayRef<uint8_t>(segment_data.GetDataStart(),
3571                                                 segment_data.GetByteSize());
3572     loadables.push_back(loadable);
3573   }
3574   return loadables;
3575 }
3576 
3577 lldb::WritableDataBufferSP
3578 ObjectFileELF::MapFileDataWritable(const FileSpec &file, uint64_t Size,
3579                                    uint64_t Offset) {
3580   return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size,
3581                                                          Offset);
3582 }
3583