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