Bug Summary

File:tools/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
Warning:line 163, column 1
Potential memory leak

Annotated Source Code

Press '?' to see keyboard shortcuts

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