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