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