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