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