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