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