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