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