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