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