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