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