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