blob: e20f994576e3d481d594d059aabf53a4d9450de7 [file] [log] [blame]
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001//===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
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
Stephen Wilsonf325ba92010-07-13 23:07:23 +000012#include <cassert>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include <algorithm>
Tamas Berghammer9fa11472015-10-27 10:43:27 +000014#include <unordered_map>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Stephen Wilson2ab0a582011-01-15 00:08:44 +000016#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/DataBuffer.h"
18#include "lldb/Core/Error.h"
Stephen Wilsonf325ba92010-07-13 23:07:23 +000019#include "lldb/Core/FileSpecList.h"
Ed Mastec113ff82013-12-02 17:49:13 +000020#include "lldb/Core/Log.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000021#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000022#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/PluginManager.h"
24#include "lldb/Core/Section.h"
25#include "lldb/Core/Stream.h"
Todd Fiala4339f3a2014-03-25 19:29:09 +000026#include "lldb/Core/Timer.h"
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +000027#include "lldb/Symbol/DWARFCallFrameInfo.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000028#include "lldb/Symbol/SymbolContext.h"
Steve Pucci9e02dac2014-02-06 19:02:19 +000029#include "lldb/Target/SectionLoadList.h"
Ed Maste54803652013-10-11 17:39:07 +000030#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Stephen Wilson499b40e2011-03-30 16:07:05 +000032#include "llvm/ADT/PointerUnion.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000033#include "llvm/ADT/StringRef.h"
Zachary Turner736d4d82014-06-25 05:42:32 +000034#include "llvm/Support/MathExtras.h"
Stephen Wilson499b40e2011-03-30 16:07:05 +000035
Stephen Wilsonf325ba92010-07-13 23:07:23 +000036#define CASE_AND_STREAM(s, def, width) \
37 case def: s->Printf("%-*s", width, #def); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039using namespace lldb;
40using namespace lldb_private;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000041using namespace elf;
42using namespace llvm::ELF;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
Stephen Wilson499b40e2011-03-30 16:07:05 +000044namespace {
Todd Fialab91de782014-06-27 16:52:49 +000045
46// ELF note owner definitions
47const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD";
48const char *const LLDB_NT_OWNER_GNU = "GNU";
49const char *const LLDB_NT_OWNER_NETBSD = "NetBSD";
Todd Fialacfee9632014-07-16 15:03:10 +000050const char *const LLDB_NT_OWNER_CSR = "csr";
Tamas Berghammerdb037d92015-03-18 10:36:27 +000051const char *const LLDB_NT_OWNER_ANDROID = "Android";
Greg Claytonb704b692015-10-28 18:04:38 +000052const char *const LLDB_NT_OWNER_CORE = "CORE";
53const char *const LLDB_NT_OWNER_LINUX = "LINUX";
Todd Fialab91de782014-06-27 16:52:49 +000054
55// ELF note type definitions
56const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01;
57const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4;
58
59const elf_word LLDB_NT_GNU_ABI_TAG = 0x01;
60const elf_word LLDB_NT_GNU_ABI_SIZE = 16;
61
62const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03;
63
64const elf_word LLDB_NT_NETBSD_ABI_TAG = 0x01;
65const elf_word LLDB_NT_NETBSD_ABI_SIZE = 4;
66
67// GNU ABI note OS constants
68const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00;
69const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01;
70const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02;
71
Greg Claytonb704b692015-10-28 18:04:38 +000072// LLDB_NT_OWNER_CORE and LLDB_NT_OWNER_LINUX note contants
73#define NT_PRSTATUS 1
74#define NT_PRFPREG 2
75#define NT_PRPSINFO 3
76#define NT_TASKSTRUCT 4
77#define NT_AUXV 6
78#define NT_SIGINFO 0x53494749
79#define NT_FILE 0x46494c45
80#define NT_PRXFPREG 0x46e62b7f
81#define NT_PPC_VMX 0x100
82#define NT_PPC_SPE 0x101
83#define NT_PPC_VSX 0x102
84#define NT_386_TLS 0x200
85#define NT_386_IOPERM 0x201
86#define NT_X86_XSTATE 0x202
87#define NT_S390_HIGH_GPRS 0x300
88#define NT_S390_TIMER 0x301
89#define NT_S390_TODCMP 0x302
90#define NT_S390_TODPREG 0x303
91#define NT_S390_CTRS 0x304
92#define NT_S390_PREFIX 0x305
93#define NT_S390_LAST_BREAK 0x306
94#define NT_S390_SYSTEM_CALL 0x307
95#define NT_S390_TDB 0x308
96#define NT_S390_VXRS_LOW 0x309
97#define NT_S390_VXRS_HIGH 0x30a
98#define NT_ARM_VFP 0x400
99#define NT_ARM_TLS 0x401
100#define NT_ARM_HW_BREAK 0x402
101#define NT_ARM_HW_WATCH 0x403
102#define NT_ARM_SYSTEM_CALL 0x404
103#define NT_METAG_CBUF 0x500
104#define NT_METAG_RPIPE 0x501
105#define NT_METAG_TLS 0x502
106
Stephen Wilson499b40e2011-03-30 16:07:05 +0000107//===----------------------------------------------------------------------===//
108/// @class ELFRelocation
109/// @brief Generic wrapper for ELFRel and ELFRela.
110///
111/// This helper class allows us to parse both ELFRel and ELFRela relocation
112/// entries in a generic manner.
113class ELFRelocation
114{
115public:
116
117 /// Constructs an ELFRelocation entry with a personality as given by @p
118 /// type.
119 ///
120 /// @param type Either DT_REL or DT_RELA. Any other value is invalid.
121 ELFRelocation(unsigned type);
Ed Maste81b4c5f2016-01-04 01:43:47 +0000122
Stephen Wilson499b40e2011-03-30 16:07:05 +0000123 ~ELFRelocation();
124
125 bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000126 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000127
128 static unsigned
129 RelocType32(const ELFRelocation &rel);
130
131 static unsigned
132 RelocType64(const ELFRelocation &rel);
133
134 static unsigned
135 RelocSymbol32(const ELFRelocation &rel);
136
137 static unsigned
138 RelocSymbol64(const ELFRelocation &rel);
139
Andrew MacPherson17220c12014-03-05 10:12:43 +0000140 static unsigned
141 RelocOffset32(const ELFRelocation &rel);
142
143 static unsigned
144 RelocOffset64(const ELFRelocation &rel);
145
146 static unsigned
147 RelocAddend32(const ELFRelocation &rel);
148
149 static unsigned
150 RelocAddend64(const ELFRelocation &rel);
151
Stephen Wilson499b40e2011-03-30 16:07:05 +0000152private:
153 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion;
154
155 RelocUnion reloc;
156};
157
158ELFRelocation::ELFRelocation(unsigned type)
Ed Maste81b4c5f2016-01-04 01:43:47 +0000159{
Andrew MacPherson17220c12014-03-05 10:12:43 +0000160 if (type == DT_REL || type == SHT_REL)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000161 reloc = new ELFRel();
Andrew MacPherson17220c12014-03-05 10:12:43 +0000162 else if (type == DT_RELA || type == SHT_RELA)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000163 reloc = new ELFRela();
164 else {
165 assert(false && "unexpected relocation type");
166 reloc = static_cast<ELFRel*>(NULL);
167 }
168}
169
170ELFRelocation::~ELFRelocation()
171{
172 if (reloc.is<ELFRel*>())
173 delete reloc.get<ELFRel*>();
174 else
Ed Maste81b4c5f2016-01-04 01:43:47 +0000175 delete reloc.get<ELFRela*>();
Stephen Wilson499b40e2011-03-30 16:07:05 +0000176}
177
178bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000179ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000180{
181 if (reloc.is<ELFRel*>())
182 return reloc.get<ELFRel*>()->Parse(data, offset);
183 else
184 return reloc.get<ELFRela*>()->Parse(data, offset);
185}
186
187unsigned
188ELFRelocation::RelocType32(const ELFRelocation &rel)
189{
190 if (rel.reloc.is<ELFRel*>())
191 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>());
192 else
193 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>());
194}
195
196unsigned
197ELFRelocation::RelocType64(const ELFRelocation &rel)
198{
199 if (rel.reloc.is<ELFRel*>())
200 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>());
201 else
202 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>());
203}
204
205unsigned
206ELFRelocation::RelocSymbol32(const ELFRelocation &rel)
207{
208 if (rel.reloc.is<ELFRel*>())
209 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>());
210 else
211 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>());
212}
213
214unsigned
215ELFRelocation::RelocSymbol64(const ELFRelocation &rel)
216{
217 if (rel.reloc.is<ELFRel*>())
218 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>());
219 else
220 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>());
221}
222
Andrew MacPherson17220c12014-03-05 10:12:43 +0000223unsigned
224ELFRelocation::RelocOffset32(const ELFRelocation &rel)
225{
226 if (rel.reloc.is<ELFRel*>())
227 return rel.reloc.get<ELFRel*>()->r_offset;
228 else
229 return rel.reloc.get<ELFRela*>()->r_offset;
230}
231
232unsigned
233ELFRelocation::RelocOffset64(const ELFRelocation &rel)
234{
235 if (rel.reloc.is<ELFRel*>())
236 return rel.reloc.get<ELFRel*>()->r_offset;
237 else
238 return rel.reloc.get<ELFRela*>()->r_offset;
239}
240
241unsigned
242ELFRelocation::RelocAddend32(const ELFRelocation &rel)
243{
244 if (rel.reloc.is<ELFRel*>())
245 return 0;
246 else
247 return rel.reloc.get<ELFRela*>()->r_addend;
248}
249
250unsigned
251ELFRelocation::RelocAddend64(const ELFRelocation &rel)
252{
253 if (rel.reloc.is<ELFRel*>())
254 return 0;
255 else
256 return rel.reloc.get<ELFRela*>()->r_addend;
257}
258
Stephen Wilson499b40e2011-03-30 16:07:05 +0000259} // end anonymous namespace
260
Ed Mastec113ff82013-12-02 17:49:13 +0000261bool
262ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset)
263{
264 // Read all fields.
265 if (data.GetU32(offset, &n_namesz, 3) == NULL)
266 return false;
267
268 // The name field is required to be nul-terminated, and n_namesz
269 // includes the terminating nul in observed implementations (contrary
270 // to the ELF-64 spec). A special case is needed for cores generated
271 // by some older Linux versions, which write a note named "CORE"
272 // without a nul terminator and n_namesz = 4.
273 if (n_namesz == 4)
274 {
275 char buf[4];
276 if (data.ExtractBytes (*offset, 4, data.GetByteOrder(), buf) != 4)
277 return false;
278 if (strncmp (buf, "CORE", 4) == 0)
279 {
280 n_name = "CORE";
281 *offset += 4;
282 return true;
283 }
284 }
285
Rafael Espindolaa94ae1e2016-01-18 20:57:54 +0000286 const char *cstr = data.GetCStr(offset, llvm::alignTo (n_namesz, 4));
Ed Mastec113ff82013-12-02 17:49:13 +0000287 if (cstr == NULL)
288 {
289 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
290 if (log)
291 log->Printf("Failed to parse note name lacking nul terminator");
292
293 return false;
294 }
295 n_name = cstr;
296 return true;
297}
298
Matthew Gardiner5f675792014-08-27 12:09:39 +0000299static uint32_t
300kalimbaVariantFromElfFlags(const elf::elf_word e_flags)
301{
302 const uint32_t dsp_rev = e_flags & 0xFF;
303 uint32_t kal_arch_variant = LLDB_INVALID_CPUTYPE;
304 switch(dsp_rev)
305 {
306 // TODO(mg11) Support more variants
307 case 10:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000308 kal_arch_variant = llvm::Triple::KalimbaSubArch_v3;
Matthew Gardiner5f675792014-08-27 12:09:39 +0000309 break;
310 case 14:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000311 kal_arch_variant = llvm::Triple::KalimbaSubArch_v4;
312 break;
313 case 17:
314 case 20:
315 kal_arch_variant = llvm::Triple::KalimbaSubArch_v5;
Matthew Gardiner5f675792014-08-27 12:09:39 +0000316 break;
317 default:
Ed Maste81b4c5f2016-01-04 01:43:47 +0000318 break;
Matthew Gardiner5f675792014-08-27 12:09:39 +0000319 }
320 return kal_arch_variant;
321}
322
323static uint32_t
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000324mipsVariantFromElfFlags(const elf::elf_word e_flags, uint32_t endian)
325{
326 const uint32_t mips_arch = e_flags & llvm::ELF::EF_MIPS_ARCH;
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +0000327 uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown;
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000328
329 switch (mips_arch)
330 {
Sagar Thakur40fc2e32015-12-15 05:50:55 +0000331 case llvm::ELF::EF_MIPS_ARCH_1:
332 case llvm::ELF::EF_MIPS_ARCH_2:
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +0000333 case llvm::ELF::EF_MIPS_ARCH_32:
334 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el : ArchSpec::eMIPSSubType_mips32;
335 case llvm::ELF::EF_MIPS_ARCH_32R2:
336 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el : ArchSpec::eMIPSSubType_mips32r2;
337 case llvm::ELF::EF_MIPS_ARCH_32R6:
338 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el : ArchSpec::eMIPSSubType_mips32r6;
Mohit K. Bhakkad884fc3e2016-01-12 06:03:01 +0000339 case llvm::ELF::EF_MIPS_ARCH_3:
340 case llvm::ELF::EF_MIPS_ARCH_4:
341 case llvm::ELF::EF_MIPS_ARCH_5:
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000342 case llvm::ELF::EF_MIPS_ARCH_64:
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +0000343 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el : ArchSpec::eMIPSSubType_mips64;
344 case llvm::ELF::EF_MIPS_ARCH_64R2:
345 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el : ArchSpec::eMIPSSubType_mips64r2;
346 case llvm::ELF::EF_MIPS_ARCH_64R6:
347 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el : ArchSpec::eMIPSSubType_mips64r6;
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000348 default:
349 break;
350 }
351
352 return arch_variant;
353}
354
355static uint32_t
Matthew Gardiner5f675792014-08-27 12:09:39 +0000356subTypeFromElfHeader(const elf::ELFHeader& header)
357{
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000358 if (header.e_machine == llvm::ELF::EM_MIPS)
359 return mipsVariantFromElfFlags (header.e_flags,
360 header.e_ident[EI_DATA]);
361
Matthew Gardiner5f675792014-08-27 12:09:39 +0000362 return
363 llvm::ELF::EM_CSR_KALIMBA == header.e_machine ?
364 kalimbaVariantFromElfFlags(header.e_flags) :
365 LLDB_INVALID_CPUTYPE;
366}
367
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000368//! The kalimba toolchain identifies a code section as being
369//! one with the SHT_PROGBITS set in the section sh_type and the top
370//! bit in the 32-bit address field set.
371static lldb::SectionType
372kalimbaSectionType(
373 const elf::ELFHeader& header,
374 const elf::ELFSectionHeader& sect_hdr)
375{
Matthew Gardiner6e7b0a02014-10-15 08:21:54 +0000376 if (llvm::ELF::EM_CSR_KALIMBA != header.e_machine)
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000377 {
378 return eSectionTypeOther;
379 }
380
Matthew Gardiner6e7b0a02014-10-15 08:21:54 +0000381 if (llvm::ELF::SHT_NOBITS == sect_hdr.sh_type)
382 {
383 return eSectionTypeZeroFill;
384 }
385
386 if (llvm::ELF::SHT_PROGBITS == sect_hdr.sh_type)
387 {
388 const lldb::addr_t KAL_CODE_BIT = 1 << 31;
389 return KAL_CODE_BIT & sect_hdr.sh_addr ?
390 eSectionTypeCode : eSectionTypeData;
391 }
392
393 return eSectionTypeOther;
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000394}
395
Todd Fiala4339f3a2014-03-25 19:29:09 +0000396// Arbitrary constant used as UUID prefix for core files.
397const uint32_t
398ObjectFileELF::g_core_uuid_magic(0xE210C);
399
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000400//------------------------------------------------------------------
401// Static methods.
402//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403void
404ObjectFileELF::Initialize()
405{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000406 PluginManager::RegisterPlugin(GetPluginNameStatic(),
407 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000408 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000409 CreateMemoryInstance,
410 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411}
412
413void
414ObjectFileELF::Terminate()
415{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000416 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417}
418
Greg Clayton57abc5d2013-05-10 21:47:16 +0000419lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420ObjectFileELF::GetPluginNameStatic()
421{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000422 static ConstString g_name("elf");
423 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424}
425
426const char *
427ObjectFileELF::GetPluginDescriptionStatic()
428{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000429 return "ELF object file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430}
431
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +0000433ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp,
434 DataBufferSP &data_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000435 lldb::offset_t data_offset,
436 const lldb_private::FileSpec* file,
437 lldb::offset_t file_offset,
438 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000440 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 {
Greg Clayton736888c2015-02-23 23:47:09 +0000442 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
Greg Clayton5ce9c562013-02-06 17:22:03 +0000443 data_offset = 0;
444 }
445
446 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
447 {
448 const uint8_t *magic = data_sp->GetBytes() + data_offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000449 if (ELFHeader::MagicBytesMatch(magic))
450 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000451 // Update the data to contain the entire file if it doesn't already
Andrew Kaylor213f6722013-02-07 21:30:54 +0000452 if (data_sp->GetByteSize() < length) {
Greg Clayton736888c2015-02-23 23:47:09 +0000453 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
Greg Clayton64ff6c72013-02-07 21:49:54 +0000454 data_offset = 0;
455 magic = data_sp->GetBytes();
Andrew Kaylor213f6722013-02-07 21:30:54 +0000456 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000457 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
458 if (address_size == 4 || address_size == 8)
459 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000460 std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, data_offset, file, file_offset, length));
Stephen Wilson3f4200fd2011-02-24 19:16:15 +0000461 ArchSpec spec;
462 if (objfile_ap->GetArchitecture(spec) &&
463 objfile_ap->SetModulesArchitecture(spec))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000464 return objfile_ap.release();
465 }
466 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 }
468 return NULL;
469}
470
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000471
Greg Claytonc9660542012-02-05 02:38:54 +0000472ObjectFile*
Ed Maste81b4c5f2016-01-04 01:43:47 +0000473ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
474 DataBufferSP& data_sp,
475 const lldb::ProcessSP &process_sp,
Greg Claytone72dfb32012-02-24 01:59:29 +0000476 lldb::addr_t header_addr)
Greg Claytonc9660542012-02-05 02:38:54 +0000477{
Andrew MacPherson17220c12014-03-05 10:12:43 +0000478 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT))
479 {
480 const uint8_t *magic = data_sp->GetBytes();
481 if (ELFHeader::MagicBytesMatch(magic))
482 {
483 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
484 if (address_size == 4 || address_size == 8)
485 {
486 std::auto_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, process_sp, header_addr));
487 ArchSpec spec;
488 if (objfile_ap->GetArchitecture(spec) &&
489 objfile_ap->SetModulesArchitecture(spec))
490 return objfile_ap.release();
491 }
492 }
493 }
Greg Claytonc9660542012-02-05 02:38:54 +0000494 return NULL;
495}
496
Michael Sartain9f0013d2013-05-17 00:20:21 +0000497bool
498ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp,
499 lldb::addr_t data_offset,
500 lldb::addr_t data_length)
501{
502 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
503 {
504 const uint8_t *magic = data_sp->GetBytes() + data_offset;
505 return ELFHeader::MagicBytesMatch(magic);
506 }
507 return false;
508}
Greg Claytonc9660542012-02-05 02:38:54 +0000509
Michael Sartain9f4517a2013-07-03 01:52:14 +0000510/*
511 * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c
512 *
513 * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
514 * code or tables extracted from it, as desired without restriction.
515 */
516static uint32_t
Todd Fiala4339f3a2014-03-25 19:29:09 +0000517calc_crc32(uint32_t crc, const void *buf, size_t size)
Michael Sartain9f4517a2013-07-03 01:52:14 +0000518{
519 static const uint32_t g_crc32_tab[] =
520 {
521 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
522 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
523 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
524 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
525 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
526 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
527 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
528 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
529 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
530 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
531 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
532 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
533 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
534 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
535 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
536 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
537 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
538 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
539 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
540 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
541 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
542 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
543 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
544 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
545 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
546 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
547 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
548 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
549 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
550 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
551 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
552 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
553 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
554 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
555 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
556 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
557 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
558 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
559 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
560 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
561 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
562 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
563 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
Ed Maste81b4c5f2016-01-04 01:43:47 +0000564 };
Michael Sartain9f4517a2013-07-03 01:52:14 +0000565 const uint8_t *p = (const uint8_t *)buf;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000566
Todd Fiala4339f3a2014-03-25 19:29:09 +0000567 crc = crc ^ ~0U;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000568 while (size--)
569 crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
570 return crc ^ ~0U;
571}
572
Todd Fiala4339f3a2014-03-25 19:29:09 +0000573static uint32_t
574calc_gnu_debuglink_crc32(const void *buf, size_t size)
575{
576 return calc_crc32(0U, buf, size);
577}
578
579uint32_t
580ObjectFileELF::CalculateELFNotesSegmentsCRC32 (const ProgramHeaderColl& program_headers,
581 DataExtractor& object_data)
582{
583 typedef ProgramHeaderCollConstIter Iter;
584
585 uint32_t core_notes_crc = 0;
586
587 for (Iter I = program_headers.begin(); I != program_headers.end(); ++I)
588 {
589 if (I->p_type == llvm::ELF::PT_NOTE)
590 {
591 const elf_off ph_offset = I->p_offset;
592 const size_t ph_size = I->p_filesz;
593
594 DataExtractor segment_data;
595 if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size)
596 {
597 // The ELF program header contained incorrect data,
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000598 // probably corefile is incomplete or corrupted.
Todd Fiala4339f3a2014-03-25 19:29:09 +0000599 break;
600 }
601
602 core_notes_crc = calc_crc32(core_notes_crc,
603 segment_data.GetDataStart(),
604 segment_data.GetByteSize());
605 }
606 }
607
608 return core_notes_crc;
609}
610
Todd Fialab91de782014-06-27 16:52:49 +0000611static const char*
612OSABIAsCString (unsigned char osabi_byte)
613{
614#define _MAKE_OSABI_CASE(x) case x: return #x
615 switch (osabi_byte)
616 {
617 _MAKE_OSABI_CASE(ELFOSABI_NONE);
618 _MAKE_OSABI_CASE(ELFOSABI_HPUX);
619 _MAKE_OSABI_CASE(ELFOSABI_NETBSD);
620 _MAKE_OSABI_CASE(ELFOSABI_GNU);
621 _MAKE_OSABI_CASE(ELFOSABI_HURD);
622 _MAKE_OSABI_CASE(ELFOSABI_SOLARIS);
623 _MAKE_OSABI_CASE(ELFOSABI_AIX);
624 _MAKE_OSABI_CASE(ELFOSABI_IRIX);
625 _MAKE_OSABI_CASE(ELFOSABI_FREEBSD);
626 _MAKE_OSABI_CASE(ELFOSABI_TRU64);
627 _MAKE_OSABI_CASE(ELFOSABI_MODESTO);
628 _MAKE_OSABI_CASE(ELFOSABI_OPENBSD);
629 _MAKE_OSABI_CASE(ELFOSABI_OPENVMS);
630 _MAKE_OSABI_CASE(ELFOSABI_NSK);
631 _MAKE_OSABI_CASE(ELFOSABI_AROS);
632 _MAKE_OSABI_CASE(ELFOSABI_FENIXOS);
633 _MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI);
634 _MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX);
635 _MAKE_OSABI_CASE(ELFOSABI_ARM);
636 _MAKE_OSABI_CASE(ELFOSABI_STANDALONE);
637 default:
638 return "<unknown-osabi>";
639 }
640#undef _MAKE_OSABI_CASE
641}
642
Ed Mastef6a13122015-06-05 13:03:08 +0000643//
644// WARNING : This function is being deprecated
645// It's functionality has moved to ArchSpec::SetArchitecture
646// This function is only being kept to validate the move.
647//
648// TODO : Remove this function
Todd Fialab91de782014-06-27 16:52:49 +0000649static bool
650GetOsFromOSABI (unsigned char osabi_byte, llvm::Triple::OSType &ostype)
651{
652 switch (osabi_byte)
653 {
654 case ELFOSABI_AIX: ostype = llvm::Triple::OSType::AIX; break;
655 case ELFOSABI_FREEBSD: ostype = llvm::Triple::OSType::FreeBSD; break;
656 case ELFOSABI_GNU: ostype = llvm::Triple::OSType::Linux; break;
657 case ELFOSABI_NETBSD: ostype = llvm::Triple::OSType::NetBSD; break;
658 case ELFOSABI_OPENBSD: ostype = llvm::Triple::OSType::OpenBSD; break;
659 case ELFOSABI_SOLARIS: ostype = llvm::Triple::OSType::Solaris; break;
660 default:
661 ostype = llvm::Triple::OSType::UnknownOS;
662 }
663 return ostype != llvm::Triple::OSType::UnknownOS;
664}
665
Greg Claytonf4d6de62013-04-24 22:29:28 +0000666size_t
667ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file,
668 lldb::DataBufferSP& data_sp,
669 lldb::offset_t data_offset,
670 lldb::offset_t file_offset,
671 lldb::offset_t length,
672 lldb_private::ModuleSpecList &specs)
673{
Todd Fialab91de782014-06-27 16:52:49 +0000674 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MODULES));
675
Michael Sartain9f0013d2013-05-17 00:20:21 +0000676 const size_t initial_count = specs.GetSize();
Michael Sartainc836ae72013-05-23 20:57:03 +0000677
Michael Sartain9f0013d2013-05-17 00:20:21 +0000678 if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
679 {
680 DataExtractor data;
681 data.SetData(data_sp);
682 elf::ELFHeader header;
683 if (header.Parse(data, &data_offset))
684 {
685 if (data_sp)
686 {
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000687 ModuleSpec spec (file);
Matthew Gardiner5f675792014-08-27 12:09:39 +0000688
689 const uint32_t sub_type = subTypeFromElfHeader(header);
Michael Sartain9f0013d2013-05-17 00:20:21 +0000690 spec.GetArchitecture().SetArchitecture(eArchTypeELF,
691 header.e_machine,
Ed Mastef6a13122015-06-05 13:03:08 +0000692 sub_type,
693 header.e_ident[EI_OSABI]);
Matthew Gardiner5f675792014-08-27 12:09:39 +0000694
Michael Sartain9f0013d2013-05-17 00:20:21 +0000695 if (spec.GetArchitecture().IsValid())
696 {
Todd Fialab91de782014-06-27 16:52:49 +0000697 llvm::Triple::OSType ostype;
Ed Mastef6a13122015-06-05 13:03:08 +0000698 llvm::Triple::VendorType vendor;
699 llvm::Triple::OSType spec_ostype = spec.GetArchitecture ().GetTriple ().getOS ();
Todd Fialab91de782014-06-27 16:52:49 +0000700
701 if (log)
702 log->Printf ("ObjectFileELF::%s file '%s' module OSABI: %s", __FUNCTION__, file.GetPath ().c_str (), OSABIAsCString (header.e_ident[EI_OSABI]));
Ed Mastef6a13122015-06-05 13:03:08 +0000703
704 // SetArchitecture should have set the vendor to unknown
705 vendor = spec.GetArchitecture ().GetTriple ().getVendor ();
706 assert(vendor == llvm::Triple::UnknownVendor);
707
708 //
709 // Validate it is ok to remove GetOsFromOSABI
710 GetOsFromOSABI (header.e_ident[EI_OSABI], ostype);
711 assert(spec_ostype == ostype);
712 if (spec_ostype != llvm::Triple::OSType::UnknownOS)
Todd Fialab91de782014-06-27 16:52:49 +0000713 {
Todd Fialab91de782014-06-27 16:52:49 +0000714 if (log)
715 log->Printf ("ObjectFileELF::%s file '%s' set ELF module OS type from ELF header OSABI.", __FUNCTION__, file.GetPath ().c_str ());
716 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000717
718 // Try to get the UUID from the section list. Usually that's at the end, so
719 // map the file in if we don't have it already.
720 size_t section_header_end = header.e_shoff + header.e_shnum * header.e_shentsize;
721 if (section_header_end > data_sp->GetByteSize())
722 {
Greg Clayton736888c2015-02-23 23:47:09 +0000723 data_sp = file.MemoryMapFileContentsIfLocal (file_offset, section_header_end);
Michael Sartaina7499c92013-07-01 19:45:50 +0000724 data.SetData(data_sp);
725 }
726
Michael Sartain9f4517a2013-07-03 01:52:14 +0000727 uint32_t gnu_debuglink_crc = 0;
Michael Sartaina7499c92013-07-01 19:45:50 +0000728 std::string gnu_debuglink_file;
729 SectionHeaderColl section_headers;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000730 lldb_private::UUID &uuid = spec.GetUUID();
Michael Sartain9f4517a2013-07-03 01:52:14 +0000731
Ravitheja Addepally15f89c42016-01-19 12:55:21 +0000732 using namespace std::placeholders;
733 const SetDataFunction set_data = std::bind(&ObjectFileELF::SetData, std::cref(data), _1, _2, _3);
734 GetSectionHeaderInfo(section_headers, set_data, header, uuid, gnu_debuglink_file, gnu_debuglink_crc, spec.GetArchitecture ());
735
Todd Fialab91de782014-06-27 16:52:49 +0000736
Todd Fialab91de782014-06-27 16:52:49 +0000737 llvm::Triple &spec_triple = spec.GetArchitecture ().GetTriple ();
Todd Fialab91de782014-06-27 16:52:49 +0000738
739 if (log)
740 log->Printf ("ObjectFileELF::%s file '%s' module set to triple: %s (architecture %s)", __FUNCTION__, file.GetPath ().c_str (), spec_triple.getTriple ().c_str (), spec.GetArchitecture ().GetArchitectureName ());
Todd Fiala4339f3a2014-03-25 19:29:09 +0000741
Michael Sartain9f4517a2013-07-03 01:52:14 +0000742 if (!uuid.IsValid())
743 {
Todd Fiala4339f3a2014-03-25 19:29:09 +0000744 uint32_t core_notes_crc = 0;
745
Michael Sartain9f4517a2013-07-03 01:52:14 +0000746 if (!gnu_debuglink_crc)
747 {
Todd Fiala4339f3a2014-03-25 19:29:09 +0000748 lldb_private::Timer scoped_timer (__PRETTY_FUNCTION__,
749 "Calculating module crc32 %s with size %" PRIu64 " KiB",
750 file.GetLastPathComponent().AsCString(),
751 (file.GetByteSize()-file_offset)/1024);
752
753 // For core files - which usually don't happen to have a gnu_debuglink,
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000754 // and are pretty bulky - calculating whole contents crc32 would be too much of luxury.
Todd Fiala4339f3a2014-03-25 19:29:09 +0000755 // Thus we will need to fallback to something simpler.
756 if (header.e_type == llvm::ELF::ET_CORE)
757 {
758 size_t program_headers_end = header.e_phoff + header.e_phnum * header.e_phentsize;
759 if (program_headers_end > data_sp->GetByteSize())
760 {
Greg Clayton736888c2015-02-23 23:47:09 +0000761 data_sp = file.MemoryMapFileContentsIfLocal(file_offset, program_headers_end);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000762 data.SetData(data_sp);
763 }
764 ProgramHeaderColl program_headers;
Ravitheja Addepally15f89c42016-01-19 12:55:21 +0000765 GetProgramHeaderInfo(program_headers, set_data, header);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000766
767 size_t segment_data_end = 0;
768 for (ProgramHeaderCollConstIter I = program_headers.begin();
769 I != program_headers.end(); ++I)
770 {
Enrico Granataafcbdb12014-03-25 20:53:33 +0000771 segment_data_end = std::max<unsigned long long> (I->p_offset + I->p_filesz, segment_data_end);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000772 }
773
774 if (segment_data_end > data_sp->GetByteSize())
775 {
Greg Clayton736888c2015-02-23 23:47:09 +0000776 data_sp = file.MemoryMapFileContentsIfLocal(file_offset, segment_data_end);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000777 data.SetData(data_sp);
778 }
779
780 core_notes_crc = CalculateELFNotesSegmentsCRC32 (program_headers, data);
781 }
782 else
783 {
784 // Need to map entire file into memory to calculate the crc.
Greg Clayton736888c2015-02-23 23:47:09 +0000785 data_sp = file.MemoryMapFileContentsIfLocal (file_offset, SIZE_MAX);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000786 data.SetData(data_sp);
787 gnu_debuglink_crc = calc_gnu_debuglink_crc32 (data.GetDataStart(), data.GetByteSize());
788 }
Michael Sartain9f4517a2013-07-03 01:52:14 +0000789 }
790 if (gnu_debuglink_crc)
791 {
792 // Use 4 bytes of crc from the .gnu_debuglink section.
793 uint32_t uuidt[4] = { gnu_debuglink_crc, 0, 0, 0 };
794 uuid.SetBytes (uuidt, sizeof(uuidt));
795 }
Todd Fiala4339f3a2014-03-25 19:29:09 +0000796 else if (core_notes_crc)
797 {
798 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it look different form
799 // .gnu_debuglink crc followed by 4 bytes of note segments crc.
800 uint32_t uuidt[4] = { g_core_uuid_magic, core_notes_crc, 0, 0 };
801 uuid.SetBytes (uuidt, sizeof(uuidt));
802 }
Michael Sartain9f4517a2013-07-03 01:52:14 +0000803 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000804
Michael Sartain9f0013d2013-05-17 00:20:21 +0000805 specs.Append(spec);
806 }
807 }
808 }
809 }
Michael Sartainc836ae72013-05-23 20:57:03 +0000810
Michael Sartain9f0013d2013-05-17 00:20:21 +0000811 return specs.GetSize() - initial_count;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000812}
813
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000814//------------------------------------------------------------------
815// PluginInterface protocol
816//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000817lldb_private::ConstString
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000818ObjectFileELF::GetPluginName()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000820 return GetPluginNameStatic();
821}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000822
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000823uint32_t
824ObjectFileELF::GetPluginVersion()
825{
826 return m_plugin_version;
827}
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000828//------------------------------------------------------------------
829// ObjectFile protocol
830//------------------------------------------------------------------
831
Ed Maste81b4c5f2016-01-04 01:43:47 +0000832ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000833 DataBufferSP& data_sp,
834 lldb::offset_t data_offset,
Ed Maste81b4c5f2016-01-04 01:43:47 +0000835 const FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000836 lldb::offset_t file_offset,
Ed Maste81b4c5f2016-01-04 01:43:47 +0000837 lldb::offset_t length) :
Greg Clayton5ce9c562013-02-06 17:22:03 +0000838 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytone72dfb32012-02-24 01:59:29 +0000839 m_header(),
Todd Fialab91de782014-06-27 16:52:49 +0000840 m_uuid(),
841 m_gnu_debuglink_file(),
842 m_gnu_debuglink_crc(0),
Greg Claytone72dfb32012-02-24 01:59:29 +0000843 m_program_headers(),
844 m_section_headers(),
Todd Fialab91de782014-06-27 16:52:49 +0000845 m_dynamic_symbols(),
846 m_filespec_ap(),
847 m_entry_point_address(),
848 m_arch_spec()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849{
850 if (file)
851 m_file = *file;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000852 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000853}
854
Andrew MacPherson17220c12014-03-05 10:12:43 +0000855ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
Tamas Berghammerf2561842015-06-30 10:41:23 +0000856 DataBufferSP& header_data_sp,
Andrew MacPherson17220c12014-03-05 10:12:43 +0000857 const lldb::ProcessSP &process_sp,
858 addr_t header_addr) :
Tamas Berghammerf2561842015-06-30 10:41:23 +0000859 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Andrew MacPherson17220c12014-03-05 10:12:43 +0000860 m_header(),
Todd Fialab91de782014-06-27 16:52:49 +0000861 m_uuid(),
862 m_gnu_debuglink_file(),
863 m_gnu_debuglink_crc(0),
Andrew MacPherson17220c12014-03-05 10:12:43 +0000864 m_program_headers(),
865 m_section_headers(),
Todd Fialab91de782014-06-27 16:52:49 +0000866 m_dynamic_symbols(),
867 m_filespec_ap(),
868 m_entry_point_address(),
869 m_arch_spec()
Andrew MacPherson17220c12014-03-05 10:12:43 +0000870{
871 ::memset(&m_header, 0, sizeof(m_header));
872}
873
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874ObjectFileELF::~ObjectFileELF()
875{
876}
877
Jim Ingham5aee1622010-08-09 23:31:02 +0000878bool
879ObjectFileELF::IsExecutable() const
880{
Deepak Panickal71f5b502014-07-22 12:01:43 +0000881 return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0);
Jim Ingham5aee1622010-08-09 23:31:02 +0000882}
883
Steve Pucci9e02dac2014-02-06 19:02:19 +0000884bool
Greg Clayton751caf62014-02-07 22:54:47 +0000885ObjectFileELF::SetLoadAddress (Target &target,
886 lldb::addr_t value,
887 bool value_is_offset)
Steve Pucci9e02dac2014-02-06 19:02:19 +0000888{
Steve Pucci9e02dac2014-02-06 19:02:19 +0000889 ModuleSP module_sp = GetModule();
890 if (module_sp)
891 {
892 size_t num_loaded_sections = 0;
893 SectionList *section_list = GetSectionList ();
894 if (section_list)
895 {
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000896 if (!value_is_offset)
Steve Pucci9e02dac2014-02-06 19:02:19 +0000897 {
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000898 bool found_offset = false;
899 for (size_t i = 0, count = GetProgramHeaderCount(); i < count; ++i)
Steve Pucci9e02dac2014-02-06 19:02:19 +0000900 {
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000901 const elf::ELFProgramHeader* header = GetProgramHeaderByIndex(i);
902 if (header == nullptr)
903 continue;
Tamas Berghammerf2561842015-06-30 10:41:23 +0000904
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000905 if (header->p_type != PT_LOAD || header->p_offset != 0)
906 continue;
Ed Maste81b4c5f2016-01-04 01:43:47 +0000907
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000908 value = value - header->p_vaddr;
909 found_offset = true;
910 break;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000911 }
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000912 if (!found_offset)
913 return false;
Greg Clayton751caf62014-02-07 22:54:47 +0000914 }
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000915
916 const size_t num_sections = section_list->GetSize();
917 size_t sect_idx = 0;
918
919 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
Greg Clayton751caf62014-02-07 22:54:47 +0000920 {
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000921 // Iterate through the object file sections to find all
922 // of the sections that have SHF_ALLOC in their flag bits.
923 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000924 if (section_sp && section_sp->Test(SHF_ALLOC))
925 {
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +0000926 lldb::addr_t load_addr = section_sp->GetFileAddress();
927 // We don't want to update the load address of a section with type
928 // eSectionTypeAbsoluteAddress as they already have the absolute load address
929 // already specified
930 if (section_sp->GetType() != eSectionTypeAbsoluteAddress)
931 load_addr += value;
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000932
933 // On 32-bit systems the load address have to fit into 4 bytes. The rest of
934 // the bytes are the overflow from the addition.
935 if (GetAddressByteSize() == 4)
936 load_addr &= 0xFFFFFFFF;
937
938 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
939 ++num_loaded_sections;
940 }
Steve Pucci9e02dac2014-02-06 19:02:19 +0000941 }
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000942 return num_loaded_sections > 0;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000943 }
Steve Pucci9e02dac2014-02-06 19:02:19 +0000944 }
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000945 return false;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000946}
947
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948ByteOrder
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000949ObjectFileELF::GetByteOrder() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950{
951 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
952 return eByteOrderBig;
953 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
954 return eByteOrderLittle;
955 return eByteOrderInvalid;
956}
957
Greg Claytonc7bece562013-01-25 18:06:21 +0000958uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000959ObjectFileELF::GetAddressByteSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960{
961 return m_data.GetAddressByteSize();
962}
963
Todd Fialafbd703a2014-09-15 22:33:39 +0000964AddressClass
965ObjectFileELF::GetAddressClass (addr_t file_addr)
966{
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000967 Symtab* symtab = GetSymtab();
968 if (!symtab)
969 return eAddressClassUnknown;
Todd Fialafbd703a2014-09-15 22:33:39 +0000970
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000971 // The address class is determined based on the symtab. Ask it from the object file what
972 // contains the symtab information.
973 ObjectFile* symtab_objfile = symtab->GetObjectFile();
974 if (symtab_objfile != nullptr && symtab_objfile != this)
975 return symtab_objfile->GetAddressClass(file_addr);
976
977 auto res = ObjectFile::GetAddressClass (file_addr);
Todd Fialafbd703a2014-09-15 22:33:39 +0000978 if (res != eAddressClassCode)
979 return res;
980
Tamas Berghammer83544cf2015-04-07 10:43:50 +0000981 auto ub = m_address_class_map.upper_bound(file_addr);
982 if (ub == m_address_class_map.begin())
983 {
984 // No entry in the address class map before the address. Return
985 // default address class for an address in a code section.
986 return eAddressClassCode;
987 }
Todd Fialafbd703a2014-09-15 22:33:39 +0000988
Tamas Berghammer83544cf2015-04-07 10:43:50 +0000989 // Move iterator to the address class entry preceding address
990 --ub;
Todd Fialafbd703a2014-09-15 22:33:39 +0000991
Tamas Berghammer83544cf2015-04-07 10:43:50 +0000992 return ub->second;
Todd Fialafbd703a2014-09-15 22:33:39 +0000993}
994
Greg Claytonc7bece562013-01-25 18:06:21 +0000995size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000996ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997{
Greg Claytonc7bece562013-01-25 18:06:21 +0000998 return std::distance(m_section_headers.begin(), I) + 1u;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000999}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001000
Greg Claytonc7bece562013-01-25 18:06:21 +00001001size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001002ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
1003{
Greg Claytonc7bece562013-01-25 18:06:21 +00001004 return std::distance(m_section_headers.begin(), I) + 1u;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005}
1006
1007bool
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001008ObjectFileELF::ParseHeader()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009{
Filipe Cabecinhas22b40f72013-05-16 23:29:36 +00001010 lldb::offset_t offset = 0;
Tamas Berghammerf2561842015-06-30 10:41:23 +00001011 if (!m_header.Parse(m_data, &offset))
1012 return false;
1013
1014 if (!IsInMemory())
1015 return true;
1016
1017 // For in memory object files m_data might not contain the full object file. Try to load it
1018 // until the end of the "Section header table" what is at the end of the ELF file.
1019 addr_t file_size = m_header.e_shoff + m_header.e_shnum * m_header.e_shentsize;
1020 if (m_data.GetByteSize() < file_size)
1021 {
1022 ProcessSP process_sp (m_process_wp.lock());
1023 if (!process_sp)
1024 return false;
1025
1026 DataBufferSP data_sp = ReadMemory(process_sp, m_memory_addr, file_size);
1027 if (!data_sp)
1028 return false;
1029 m_data.SetData(data_sp, 0, file_size);
1030 }
1031
1032 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001033}
1034
1035bool
Greg Clayton60830262011-02-04 18:53:10 +00001036ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001037{
Michael Sartaina7499c92013-07-01 19:45:50 +00001038 // Need to parse the section list to get the UUIDs, so make sure that's been done.
Todd Fiala4339f3a2014-03-25 19:29:09 +00001039 if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile)
Michael Sartaina7499c92013-07-01 19:45:50 +00001040 return false;
1041
Michael Sartainc836ae72013-05-23 20:57:03 +00001042 if (m_uuid.IsValid())
1043 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001044 // We have the full build id uuid.
Michael Sartainc836ae72013-05-23 20:57:03 +00001045 *uuid = m_uuid;
1046 return true;
1047 }
Todd Fiala4339f3a2014-03-25 19:29:09 +00001048 else if (GetType() == ObjectFile::eTypeCoreFile)
1049 {
1050 uint32_t core_notes_crc = 0;
1051
1052 if (!ParseProgramHeaders())
1053 return false;
1054
1055 core_notes_crc = CalculateELFNotesSegmentsCRC32(m_program_headers, m_data);
1056
1057 if (core_notes_crc)
1058 {
1059 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it
1060 // look different form .gnu_debuglink crc - followed by 4 bytes of note
1061 // segments crc.
1062 uint32_t uuidt[4] = { g_core_uuid_magic, core_notes_crc, 0, 0 };
1063 m_uuid.SetBytes (uuidt, sizeof(uuidt));
1064 }
1065 }
1066 else
Michael Sartaina7499c92013-07-01 19:45:50 +00001067 {
Michael Sartain9f4517a2013-07-03 01:52:14 +00001068 if (!m_gnu_debuglink_crc)
1069 m_gnu_debuglink_crc = calc_gnu_debuglink_crc32 (m_data.GetDataStart(), m_data.GetByteSize());
Michael Sartaina7499c92013-07-01 19:45:50 +00001070 if (m_gnu_debuglink_crc)
1071 {
1072 // Use 4 bytes of crc from the .gnu_debuglink section.
1073 uint32_t uuidt[4] = { m_gnu_debuglink_crc, 0, 0, 0 };
Todd Fiala4339f3a2014-03-25 19:29:09 +00001074 m_uuid.SetBytes (uuidt, sizeof(uuidt));
Michael Sartaina7499c92013-07-01 19:45:50 +00001075 }
1076 }
1077
Todd Fiala4339f3a2014-03-25 19:29:09 +00001078 if (m_uuid.IsValid())
1079 {
1080 *uuid = m_uuid;
1081 return true;
1082 }
1083
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 return false;
1085}
1086
Michael Sartaina7499c92013-07-01 19:45:50 +00001087lldb_private::FileSpecList
1088ObjectFileELF::GetDebugSymbolFilePaths()
1089{
1090 FileSpecList file_spec_list;
1091
1092 if (!m_gnu_debuglink_file.empty())
1093 {
1094 FileSpec file_spec (m_gnu_debuglink_file.c_str(), false);
1095 file_spec_list.Append (file_spec);
1096 }
1097 return file_spec_list;
1098}
1099
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001101ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001102{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001103 size_t num_modules = ParseDependentModules();
1104 uint32_t num_specs = 0;
1105
1106 for (unsigned i = 0; i < num_modules; ++i)
1107 {
1108 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
1109 num_specs++;
1110 }
1111
1112 return num_specs;
1113}
1114
Stephen Wilson499b40e2011-03-30 16:07:05 +00001115Address
Ed Maste54803652013-10-11 17:39:07 +00001116ObjectFileELF::GetImageInfoAddress(Target *target)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001117{
1118 if (!ParseDynamicSymbols())
Stephen Wilson2ab0a582011-01-15 00:08:44 +00001119 return Address();
1120
1121 SectionList *section_list = GetSectionList();
1122 if (!section_list)
1123 return Address();
1124
Greg Clayton3046e662013-07-10 01:23:25 +00001125 // Find the SHT_DYNAMIC (.dynamic) section.
1126 SectionSP dynsym_section_sp (section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true));
1127 if (!dynsym_section_sp)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001128 return Address();
Greg Clayton3046e662013-07-10 01:23:25 +00001129 assert (dynsym_section_sp->GetObjectFile() == this);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001130
Greg Clayton3046e662013-07-10 01:23:25 +00001131 user_id_t dynsym_id = dynsym_section_sp->GetID();
Michael Sartaina7499c92013-07-01 19:45:50 +00001132 const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001133 if (!dynsym_hdr)
1134 return Address();
1135
Greg Clayton3046e662013-07-10 01:23:25 +00001136 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
Stephen Wilson2ab0a582011-01-15 00:08:44 +00001137 {
Greg Clayton3046e662013-07-10 01:23:25 +00001138 ELFDynamic &symbol = m_dynamic_symbols[i];
Greg Claytone72dfb32012-02-24 01:59:29 +00001139
Ed Maste54803652013-10-11 17:39:07 +00001140 if (symbol.d_tag == DT_DEBUG)
Greg Clayton3046e662013-07-10 01:23:25 +00001141 {
1142 // Compute the offset as the number of previous entries plus the
1143 // size of d_tag.
1144 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
1145 return Address(dynsym_section_sp, offset);
Stephen Wilson2ab0a582011-01-15 00:08:44 +00001146 }
Bhushan D. Attarde1bcc7ba2015-09-15 05:45:29 +00001147 // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP exists in non-PIE.
1148 else if ((symbol.d_tag == DT_MIPS_RLD_MAP || symbol.d_tag == DT_MIPS_RLD_MAP_REL) && target)
Ed Maste54803652013-10-11 17:39:07 +00001149 {
1150 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
1151 addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);
1152 if (dyn_base == LLDB_INVALID_ADDRESS)
1153 return Address();
Bhushan D. Attarde1bcc7ba2015-09-15 05:45:29 +00001154
Ed Maste54803652013-10-11 17:39:07 +00001155 Error error;
Bhushan D. Attarde1bcc7ba2015-09-15 05:45:29 +00001156 if (symbol.d_tag == DT_MIPS_RLD_MAP)
1157 {
1158 // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer.
1159 Address addr;
1160 if (target->ReadPointerFromMemory(dyn_base + offset, false, error, addr))
1161 return addr;
1162 }
1163 if (symbol.d_tag == DT_MIPS_RLD_MAP_REL)
1164 {
1165 // DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer, relative to the address of the tag.
1166 uint64_t rel_offset;
1167 rel_offset = target->ReadUnsignedIntegerFromMemory(dyn_base + offset, false, GetAddressByteSize(), UINT64_MAX, error);
1168 if (error.Success() && rel_offset != UINT64_MAX)
1169 {
1170 Address addr;
1171 addr_t debug_ptr_address = dyn_base + (offset - GetAddressByteSize()) + rel_offset;
1172 addr.SetOffset (debug_ptr_address);
1173 return addr;
1174 }
1175 }
Ed Maste54803652013-10-11 17:39:07 +00001176 }
Stephen Wilson2ab0a582011-01-15 00:08:44 +00001177 }
1178
1179 return Address();
1180}
1181
Jim Ingham672e6f52011-03-07 23:44:08 +00001182lldb_private::Address
Ed Maste81b4c5f2016-01-04 01:43:47 +00001183ObjectFileELF::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00001184{
Stephen Wilsond126c8c2011-03-08 04:12:15 +00001185 if (m_entry_point_address.IsValid())
1186 return m_entry_point_address;
1187
1188 if (!ParseHeader() || !IsExecutable())
1189 return m_entry_point_address;
1190
Greg Clayton3046e662013-07-10 01:23:25 +00001191 SectionList *section_list = GetSectionList();
1192 addr_t offset = m_header.e_entry;
Stephen Wilsond126c8c2011-03-08 04:12:15 +00001193
Ed Maste81b4c5f2016-01-04 01:43:47 +00001194 if (!section_list)
Stephen Wilsond126c8c2011-03-08 04:12:15 +00001195 m_entry_point_address.SetOffset(offset);
Greg Clayton3046e662013-07-10 01:23:25 +00001196 else
1197 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
Stephen Wilsond126c8c2011-03-08 04:12:15 +00001198 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00001199}
1200
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001201//----------------------------------------------------------------------
1202// ParseDependentModules
1203//----------------------------------------------------------------------
1204size_t
1205ObjectFileELF::ParseDependentModules()
1206{
1207 if (m_filespec_ap.get())
1208 return m_filespec_ap->GetSize();
1209
1210 m_filespec_ap.reset(new FileSpecList());
1211
Michael Sartaina7499c92013-07-01 19:45:50 +00001212 if (!ParseSectionHeaders())
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001213 return 0;
1214
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001215 SectionList *section_list = GetSectionList();
1216 if (!section_list)
1217 return 0;
1218
Greg Clayton3046e662013-07-10 01:23:25 +00001219 // Find the SHT_DYNAMIC section.
1220 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
1221 if (!dynsym)
1222 return 0;
1223 assert (dynsym->GetObjectFile() == this);
1224
1225 const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex (dynsym->GetID());
1226 if (!header)
1227 return 0;
1228 // sh_link: section header index of string table used by entries in the section.
1229 Section *dynstr = section_list->FindSectionByID (header->sh_link + 1).get();
1230 if (!dynstr)
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001231 return 0;
1232
1233 DataExtractor dynsym_data;
1234 DataExtractor dynstr_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001235 if (ReadSectionData(dynsym, dynsym_data) &&
1236 ReadSectionData(dynstr, dynstr_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001237 {
1238 ELFDynamic symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00001239 const lldb::offset_t section_size = dynsym_data.GetByteSize();
1240 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001241
1242 // The only type of entries we are concerned with are tagged DT_NEEDED,
1243 // yielding the name of a required library.
1244 while (offset < section_size)
1245 {
1246 if (!symbol.Parse(dynsym_data, &offset))
1247 break;
1248
1249 if (symbol.d_tag != DT_NEEDED)
1250 continue;
1251
1252 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
1253 const char *lib_name = dynstr_data.PeekCStr(str_index);
Greg Clayton274060b2010-10-20 20:54:39 +00001254 m_filespec_ap->Append(FileSpec(lib_name, true));
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001255 }
1256 }
1257
1258 return m_filespec_ap->GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259}
1260
1261//----------------------------------------------------------------------
Todd Fiala4339f3a2014-03-25 19:29:09 +00001262// GetProgramHeaderInfo
1263//----------------------------------------------------------------------
1264size_t
1265ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001266 const SetDataFunction &set_data,
Todd Fiala4339f3a2014-03-25 19:29:09 +00001267 const ELFHeader &header)
1268{
1269 // We have already parsed the program headers
1270 if (!program_headers.empty())
1271 return program_headers.size();
1272
1273 // If there are no program headers to read we are done.
1274 if (header.e_phnum == 0)
1275 return 0;
1276
1277 program_headers.resize(header.e_phnum);
1278 if (program_headers.size() != header.e_phnum)
1279 return 0;
1280
1281 const size_t ph_size = header.e_phnum * header.e_phentsize;
1282 const elf_off ph_offset = header.e_phoff;
1283 DataExtractor data;
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001284 if (set_data(data, ph_offset, ph_size) != ph_size)
Todd Fiala4339f3a2014-03-25 19:29:09 +00001285 return 0;
1286
1287 uint32_t idx;
1288 lldb::offset_t offset;
1289 for (idx = 0, offset = 0; idx < header.e_phnum; ++idx)
1290 {
1291 if (program_headers[idx].Parse(data, &offset) == false)
1292 break;
1293 }
1294
1295 if (idx < program_headers.size())
1296 program_headers.resize(idx);
1297
1298 return program_headers.size();
1299
1300}
1301
1302//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303// ParseProgramHeaders
1304//----------------------------------------------------------------------
1305size_t
1306ObjectFileELF::ParseProgramHeaders()
1307{
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001308 using namespace std::placeholders;
1309 return GetProgramHeaderInfo(m_program_headers,
1310 std::bind(&ObjectFileELF::SetDataWithReadMemoryFallback, this, _1, _2, _3),
1311 m_header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312}
1313
Todd Fialab91de782014-06-27 16:52:49 +00001314lldb_private::Error
1315ObjectFileELF::RefineModuleDetailsFromNote (lldb_private::DataExtractor &data, lldb_private::ArchSpec &arch_spec, lldb_private::UUID &uuid)
Michael Sartainc836ae72013-05-23 20:57:03 +00001316{
Todd Fialab91de782014-06-27 16:52:49 +00001317 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MODULES));
1318 Error error;
1319
Michael Sartainc836ae72013-05-23 20:57:03 +00001320 lldb::offset_t offset = 0;
Michael Sartainc836ae72013-05-23 20:57:03 +00001321
1322 while (true)
1323 {
Todd Fialab91de782014-06-27 16:52:49 +00001324 // Parse the note header. If this fails, bail out.
Greg Claytonb704b692015-10-28 18:04:38 +00001325 const lldb::offset_t note_offset = offset;
Ed Mastec113ff82013-12-02 17:49:13 +00001326 ELFNote note = ELFNote();
1327 if (!note.Parse(data, &offset))
Michael Sartainc836ae72013-05-23 20:57:03 +00001328 {
Todd Fialab91de782014-06-27 16:52:49 +00001329 // We're done.
1330 return error;
Michael Sartainc836ae72013-05-23 20:57:03 +00001331 }
Todd Fialab91de782014-06-27 16:52:49 +00001332
Todd Fialab91de782014-06-27 16:52:49 +00001333 if (log)
1334 log->Printf ("ObjectFileELF::%s parsing note name='%s', type=%" PRIu32, __FUNCTION__, note.n_name.c_str (), note.n_type);
1335
1336 // Process FreeBSD ELF notes.
1337 if ((note.n_name == LLDB_NT_OWNER_FREEBSD) &&
1338 (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) &&
1339 (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE))
1340 {
Todd Fialab91de782014-06-27 16:52:49 +00001341 // Pull out the min version info.
1342 uint32_t version_info;
1343 if (data.GetU32 (&offset, &version_info, 1) == nullptr)
1344 {
1345 error.SetErrorString ("failed to read FreeBSD ABI note payload");
1346 return error;
1347 }
1348
1349 // Convert the version info into a major/minor number.
1350 const uint32_t version_major = version_info / 100000;
1351 const uint32_t version_minor = (version_info / 1000) % 100;
1352
1353 char os_name[32];
1354 snprintf (os_name, sizeof (os_name), "freebsd%" PRIu32 ".%" PRIu32, version_major, version_minor);
1355
1356 // Set the elf OS version to FreeBSD. Also clear the vendor.
1357 arch_spec.GetTriple ().setOSName (os_name);
1358 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
1359
1360 if (log)
1361 log->Printf ("ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_major, version_minor, static_cast<uint32_t> (version_info % 1000));
1362 }
1363 // Process GNU ELF notes.
1364 else if (note.n_name == LLDB_NT_OWNER_GNU)
1365 {
1366 switch (note.n_type)
1367 {
1368 case LLDB_NT_GNU_ABI_TAG:
1369 if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE)
1370 {
Todd Fialab91de782014-06-27 16:52:49 +00001371 // Pull out the min OS version supporting the ABI.
1372 uint32_t version_info[4];
1373 if (data.GetU32 (&offset, &version_info[0], note.n_descsz / 4) == nullptr)
1374 {
1375 error.SetErrorString ("failed to read GNU ABI note payload");
1376 return error;
1377 }
1378
1379 // Set the OS per the OS field.
1380 switch (version_info[0])
1381 {
1382 case LLDB_NT_GNU_ABI_OS_LINUX:
1383 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::Linux);
1384 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
1385 if (log)
1386 log->Printf ("ObjectFileELF::%s detected Linux, min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[1], version_info[2], version_info[3]);
1387 // FIXME we have the minimal version number, we could be propagating that. version_info[1] = OS Major, version_info[2] = OS Minor, version_info[3] = Revision.
1388 break;
1389 case LLDB_NT_GNU_ABI_OS_HURD:
1390 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::UnknownOS);
1391 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
1392 if (log)
1393 log->Printf ("ObjectFileELF::%s detected Hurd (unsupported), min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[1], version_info[2], version_info[3]);
1394 break;
1395 case LLDB_NT_GNU_ABI_OS_SOLARIS:
1396 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::Solaris);
1397 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
1398 if (log)
1399 log->Printf ("ObjectFileELF::%s detected Solaris, min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[1], version_info[2], version_info[3]);
1400 break;
1401 default:
1402 if (log)
1403 log->Printf ("ObjectFileELF::%s unrecognized OS in note, id %" PRIu32 ", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[0], version_info[1], version_info[2], version_info[3]);
1404 break;
1405 }
1406 }
1407 break;
1408
1409 case LLDB_NT_GNU_BUILD_ID_TAG:
1410 // Only bother processing this if we don't already have the uuid set.
1411 if (!uuid.IsValid())
1412 {
Ed Maste81955f82016-03-14 14:06:00 +00001413 // 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a build-id of a different
1414 // length. Accept it as long as it's at least 4 bytes as it will be better than our own crc32.
1415 if (note.n_descsz >= 4 && note.n_descsz <= 20)
Todd Fialab91de782014-06-27 16:52:49 +00001416 {
1417 uint8_t uuidbuf[20];
1418 if (data.GetU8 (&offset, &uuidbuf, note.n_descsz) == nullptr)
1419 {
1420 error.SetErrorString ("failed to read GNU_BUILD_ID note payload");
1421 return error;
1422 }
1423
1424 // Save the build id as the UUID for the module.
1425 uuid.SetBytes (uuidbuf, note.n_descsz);
1426 }
1427 }
1428 break;
1429 }
1430 }
1431 // Process NetBSD ELF notes.
1432 else if ((note.n_name == LLDB_NT_OWNER_NETBSD) &&
1433 (note.n_type == LLDB_NT_NETBSD_ABI_TAG) &&
1434 (note.n_descsz == LLDB_NT_NETBSD_ABI_SIZE))
1435 {
Todd Fialab91de782014-06-27 16:52:49 +00001436 // Pull out the min version info.
1437 uint32_t version_info;
1438 if (data.GetU32 (&offset, &version_info, 1) == nullptr)
1439 {
1440 error.SetErrorString ("failed to read NetBSD ABI note payload");
1441 return error;
1442 }
1443
1444 // Set the elf OS version to NetBSD. Also clear the vendor.
1445 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::NetBSD);
1446 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
1447
1448 if (log)
1449 log->Printf ("ObjectFileELF::%s detected NetBSD, min version constant %" PRIu32, __FUNCTION__, version_info);
1450 }
Todd Fialacfee9632014-07-16 15:03:10 +00001451 // Process CSR kalimba notes
1452 else if ((note.n_type == LLDB_NT_GNU_ABI_TAG) &&
1453 (note.n_name == LLDB_NT_OWNER_CSR))
1454 {
Todd Fialacfee9632014-07-16 15:03:10 +00001455 arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS);
1456 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::CSR);
1457
1458 // TODO At some point the description string could be processed.
1459 // It could provide a steer towards the kalimba variant which
1460 // this ELF targets.
1461 if(note.n_descsz)
1462 {
Rafael Espindolaa94ae1e2016-01-18 20:57:54 +00001463 const char *cstr = data.GetCStr(&offset, llvm::alignTo (note.n_descsz, 4));
Todd Fialacfee9632014-07-16 15:03:10 +00001464 (void)cstr;
1465 }
1466 }
Tamas Berghammerdb037d92015-03-18 10:36:27 +00001467 else if (note.n_name == LLDB_NT_OWNER_ANDROID)
1468 {
1469 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1470 arch_spec.GetTriple().setEnvironment(llvm::Triple::EnvironmentType::Android);
1471 }
Greg Claytonb704b692015-10-28 18:04:38 +00001472 else if (note.n_name == LLDB_NT_OWNER_LINUX)
1473 {
1474 // This is sometimes found in core files and usually contains extended register info
1475 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1476 }
1477 else if (note.n_name == LLDB_NT_OWNER_CORE)
1478 {
1479 // Parse the NT_FILE to look for stuff in paths to shared libraries
1480 // As the contents look like:
1481 // count = 0x000000000000000a (10)
1482 // page_size = 0x0000000000001000 (4096)
1483 // Index start end file_ofs path
1484 // ===== ------------------ ------------------ ------------------ -------------------------------------
1485 // [ 0] 0x0000000000400000 0x0000000000401000 0x0000000000000000 /tmp/a.out
1486 // [ 1] 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out
1487 // [ 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out
1488 // [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 /lib/x86_64-linux-gnu/libc-2.19.so
1489 // [ 4] 0x00007fa79cba8000 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux-gnu/libc-2.19.so
1490 // [ 5] 0x00007fa79cda7000 0x00007fa79cdab000 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so
1491 // [ 6] 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64-linux-gnu/libc-2.19.so
1492 // [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so
1493 // [ 8] 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64-linux-gnu/ld-2.19.so
1494 // [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so
1495 if (note.n_type == NT_FILE)
1496 {
1497 uint64_t count = data.GetU64(&offset);
1498 offset += 8 + 3*8*count; // Skip page size and all start/end/file_ofs
1499 for (size_t i=0; i<count; ++i)
1500 {
1501 llvm::StringRef path(data.GetCStr(&offset));
1502 if (path.startswith("/lib/x86_64-linux-gnu"))
1503 {
1504 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1505 break;
1506 }
1507 }
1508 }
1509 }
Todd Fialab91de782014-06-27 16:52:49 +00001510
Greg Claytonb704b692015-10-28 18:04:38 +00001511 // Calculate the offset of the next note just in case "offset" has been used
1512 // to poke at the contents of the note data
1513 offset = note_offset + note.GetByteSize();
Michael Sartainc836ae72013-05-23 20:57:03 +00001514 }
Todd Fialab91de782014-06-27 16:52:49 +00001515
1516 return error;
Michael Sartainc836ae72013-05-23 20:57:03 +00001517}
Michael Sartaina7499c92013-07-01 19:45:50 +00001518
Todd Fialab91de782014-06-27 16:52:49 +00001519
Michael Sartaina7499c92013-07-01 19:45:50 +00001520//----------------------------------------------------------------------
1521// GetSectionHeaderInfo
1522//----------------------------------------------------------------------
1523size_t
1524ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl &section_headers,
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001525 const SetDataFunction &set_data,
Michael Sartaina7499c92013-07-01 19:45:50 +00001526 const elf::ELFHeader &header,
1527 lldb_private::UUID &uuid,
1528 std::string &gnu_debuglink_file,
Todd Fialab91de782014-06-27 16:52:49 +00001529 uint32_t &gnu_debuglink_crc,
1530 ArchSpec &arch_spec)
Michael Sartaina7499c92013-07-01 19:45:50 +00001531{
Todd Fiala6477ea82014-07-11 15:13:33 +00001532 // Don't reparse the section headers if we already did that.
1533 if (!section_headers.empty())
1534 return section_headers.size();
1535
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001536 // Only initialize the arch_spec to okay defaults if they're not already set.
Todd Fialab91de782014-06-27 16:52:49 +00001537 // We'll refine this with note data as we parse the notes.
1538 if (arch_spec.GetTriple ().getOS () == llvm::Triple::OSType::UnknownOS)
1539 {
Ed Mastef6a13122015-06-05 13:03:08 +00001540 llvm::Triple::OSType ostype;
1541 llvm::Triple::OSType spec_ostype;
Matthew Gardiner5f675792014-08-27 12:09:39 +00001542 const uint32_t sub_type = subTypeFromElfHeader(header);
Ed Mastef6a13122015-06-05 13:03:08 +00001543 arch_spec.SetArchitecture (eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]);
1544 //
1545 // Validate if it is ok to remove GetOsFromOSABI
1546 GetOsFromOSABI (header.e_ident[EI_OSABI], ostype);
1547 spec_ostype = arch_spec.GetTriple ().getOS ();
1548 assert(spec_ostype == ostype);
Todd Fialab91de782014-06-27 16:52:49 +00001549 }
1550
Jaydeep Patil501a7812015-07-16 03:51:55 +00001551 if (arch_spec.GetMachine() == llvm::Triple::mips || arch_spec.GetMachine() == llvm::Triple::mipsel
1552 || arch_spec.GetMachine() == llvm::Triple::mips64 || arch_spec.GetMachine() == llvm::Triple::mips64el)
1553 {
1554 switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE)
1555 {
Ed Maste81b4c5f2016-01-04 01:43:47 +00001556 case llvm::ELF::EF_MIPS_MICROMIPS:
1557 arch_spec.SetFlags (ArchSpec::eMIPSAse_micromips);
Jaydeep Patil501a7812015-07-16 03:51:55 +00001558 break;
Ed Maste81b4c5f2016-01-04 01:43:47 +00001559 case llvm::ELF::EF_MIPS_ARCH_ASE_M16:
1560 arch_spec.SetFlags (ArchSpec::eMIPSAse_mips16);
Jaydeep Patil501a7812015-07-16 03:51:55 +00001561 break;
Ed Maste81b4c5f2016-01-04 01:43:47 +00001562 case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX:
1563 arch_spec.SetFlags (ArchSpec::eMIPSAse_mdmx);
Jaydeep Patil501a7812015-07-16 03:51:55 +00001564 break;
Ed Maste81b4c5f2016-01-04 01:43:47 +00001565 default:
Jaydeep Patil501a7812015-07-16 03:51:55 +00001566 break;
1567 }
1568 }
1569
Omair Javaid0a767222016-02-05 14:37:53 +00001570 if (arch_spec.GetMachine() == llvm::Triple::arm ||
1571 arch_spec.GetMachine() == llvm::Triple::thumb)
1572 {
1573 if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT)
1574 arch_spec.SetFlags (ArchSpec::eARM_abi_soft_float);
1575 else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT)
1576 arch_spec.SetFlags (ArchSpec::eARM_abi_hard_float);
1577 }
1578
Michael Sartaina7499c92013-07-01 19:45:50 +00001579 // If there are no section headers we are done.
1580 if (header.e_shnum == 0)
1581 return 0;
1582
Todd Fialab91de782014-06-27 16:52:49 +00001583 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MODULES));
1584
Michael Sartaina7499c92013-07-01 19:45:50 +00001585 section_headers.resize(header.e_shnum);
1586 if (section_headers.size() != header.e_shnum)
1587 return 0;
1588
1589 const size_t sh_size = header.e_shnum * header.e_shentsize;
1590 const elf_off sh_offset = header.e_shoff;
1591 DataExtractor sh_data;
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001592 if (set_data (sh_data, sh_offset, sh_size) != sh_size)
Michael Sartaina7499c92013-07-01 19:45:50 +00001593 return 0;
1594
1595 uint32_t idx;
1596 lldb::offset_t offset;
1597 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx)
1598 {
1599 if (section_headers[idx].Parse(sh_data, &offset) == false)
1600 break;
1601 }
1602 if (idx < section_headers.size())
1603 section_headers.resize(idx);
1604
1605 const unsigned strtab_idx = header.e_shstrndx;
1606 if (strtab_idx && strtab_idx < section_headers.size())
1607 {
1608 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx];
1609 const size_t byte_size = sheader.sh_size;
1610 const Elf64_Off offset = sheader.sh_offset;
1611 lldb_private::DataExtractor shstr_data;
1612
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001613 if (set_data (shstr_data, offset, byte_size) == byte_size)
Michael Sartaina7499c92013-07-01 19:45:50 +00001614 {
1615 for (SectionHeaderCollIter I = section_headers.begin();
1616 I != section_headers.end(); ++I)
1617 {
1618 static ConstString g_sect_name_gnu_debuglink (".gnu_debuglink");
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001619 const ELFSectionHeaderInfo &sheader = *I;
1620 const uint64_t section_size = sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size;
Michael Sartaina7499c92013-07-01 19:45:50 +00001621 ConstString name(shstr_data.PeekCStr(I->sh_name));
1622
1623 I->section_name = name;
1624
Jaydeep Patil501a7812015-07-16 03:51:55 +00001625 if (arch_spec.GetMachine() == llvm::Triple::mips || arch_spec.GetMachine() == llvm::Triple::mipsel
1626 || arch_spec.GetMachine() == llvm::Triple::mips64 || arch_spec.GetMachine() == llvm::Triple::mips64el)
1627 {
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001628 uint32_t arch_flags = arch_spec.GetFlags ();
1629 DataExtractor data;
1630 if (sheader.sh_type == SHT_MIPS_ABIFLAGS)
Jaydeep Patil501a7812015-07-16 03:51:55 +00001631 {
Ed Maste81b4c5f2016-01-04 01:43:47 +00001632
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001633 if (section_size && (set_data (data, sheader.sh_offset, section_size) == section_size))
Jaydeep Patil501a7812015-07-16 03:51:55 +00001634 {
1635 lldb::offset_t ase_offset = 12; // MIPS ABI Flags Version: 0
Jaydeep Patil501a7812015-07-16 03:51:55 +00001636 arch_flags |= data.GetU32 (&ase_offset);
Jaydeep Patil501a7812015-07-16 03:51:55 +00001637 }
1638 }
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001639 // Settings appropriate ArchSpec ABI Flags
1640 if (header.e_flags & llvm::ELF::EF_MIPS_ABI2)
Ed Maste81b4c5f2016-01-04 01:43:47 +00001641 {
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001642 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32;
1643 }
1644 else if (header.e_flags & llvm::ELF::EF_MIPS_ABI_O32)
1645 {
Ed Maste81b4c5f2016-01-04 01:43:47 +00001646 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32;
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001647 }
1648 arch_spec.SetFlags (arch_flags);
Jaydeep Patil501a7812015-07-16 03:51:55 +00001649 }
1650
Michael Sartaina7499c92013-07-01 19:45:50 +00001651 if (name == g_sect_name_gnu_debuglink)
1652 {
1653 DataExtractor data;
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001654 if (section_size && (set_data (data, sheader.sh_offset, section_size) == section_size))
Michael Sartaina7499c92013-07-01 19:45:50 +00001655 {
1656 lldb::offset_t gnu_debuglink_offset = 0;
1657 gnu_debuglink_file = data.GetCStr (&gnu_debuglink_offset);
Rafael Espindolaa94ae1e2016-01-18 20:57:54 +00001658 gnu_debuglink_offset = llvm::alignTo (gnu_debuglink_offset, 4);
Michael Sartaina7499c92013-07-01 19:45:50 +00001659 data.GetU32 (&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
1660 }
1661 }
1662
Todd Fialab91de782014-06-27 16:52:49 +00001663 // Process ELF note section entries.
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001664 bool is_note_header = (sheader.sh_type == SHT_NOTE);
Tamas Berghammerdb037d92015-03-18 10:36:27 +00001665
1666 // The section header ".note.android.ident" is stored as a
1667 // PROGBITS type header but it is actually a note header.
1668 static ConstString g_sect_name_android_ident (".note.android.ident");
1669 if (!is_note_header && name == g_sect_name_android_ident)
1670 is_note_header = true;
1671
1672 if (is_note_header)
Michael Sartaina7499c92013-07-01 19:45:50 +00001673 {
Todd Fialab91de782014-06-27 16:52:49 +00001674 // Allow notes to refine module info.
Michael Sartaina7499c92013-07-01 19:45:50 +00001675 DataExtractor data;
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001676 if (section_size && (set_data (data, sheader.sh_offset, section_size) == section_size))
Michael Sartaina7499c92013-07-01 19:45:50 +00001677 {
Todd Fialab91de782014-06-27 16:52:49 +00001678 Error error = RefineModuleDetailsFromNote (data, arch_spec, uuid);
1679 if (error.Fail ())
1680 {
1681 if (log)
1682 log->Printf ("ObjectFileELF::%s ELF note processing failed: %s", __FUNCTION__, error.AsCString ());
1683 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001684 }
1685 }
1686 }
1687
Todd Fiala7df337f2015-10-13 23:41:19 +00001688 // Make any unknown triple components to be unspecified unknowns.
1689 if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
1690 arch_spec.GetTriple().setVendorName (llvm::StringRef());
1691 if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS)
1692 arch_spec.GetTriple().setOSName (llvm::StringRef());
1693
Michael Sartaina7499c92013-07-01 19:45:50 +00001694 return section_headers.size();
1695 }
1696 }
1697
1698 section_headers.clear();
1699 return 0;
1700}
1701
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001702size_t
1703ObjectFileELF::GetProgramHeaderCount()
1704{
1705 return ParseProgramHeaders();
1706}
1707
1708const elf::ELFProgramHeader *
1709ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id)
1710{
1711 if (!id || !ParseProgramHeaders())
1712 return NULL;
1713
1714 if (--id < m_program_headers.size())
1715 return &m_program_headers[id];
1716
1717 return NULL;
1718}
1719
Ed Maste81b4c5f2016-01-04 01:43:47 +00001720DataExtractor
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001721ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id)
1722{
1723 const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id);
1724 if (segment_header == NULL)
1725 return DataExtractor();
1726 return DataExtractor(m_data, segment_header->p_offset, segment_header->p_filesz);
1727}
1728
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00001729std::string
1730ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const
1731{
Bruce Mitchener0b6ba7c2015-07-04 05:16:58 +00001732 size_t pos = symbol_name.find('@');
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00001733 return symbol_name.substr(0, pos).str();
1734}
1735
Michael Sartaina7499c92013-07-01 19:45:50 +00001736//----------------------------------------------------------------------
1737// ParseSectionHeaders
1738//----------------------------------------------------------------------
1739size_t
1740ObjectFileELF::ParseSectionHeaders()
1741{
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001742 using namespace std::placeholders;
1743
1744 return GetSectionHeaderInfo(m_section_headers,
1745 std::bind(&ObjectFileELF::SetDataWithReadMemoryFallback, this, _1, _2, _3),
1746 m_header,
1747 m_uuid,
1748 m_gnu_debuglink_file,
1749 m_gnu_debuglink_crc,
1750 m_arch_spec);
1751}
1752
1753lldb::offset_t
1754ObjectFileELF::SetData(const lldb_private::DataExtractor &src, lldb_private::DataExtractor &dst, lldb::offset_t offset, lldb::offset_t length)
1755{
1756 return dst.SetData(src, offset, length);
1757}
1758
1759lldb::offset_t
1760ObjectFileELF::SetDataWithReadMemoryFallback(lldb_private::DataExtractor &dst, lldb::offset_t offset, lldb::offset_t length)
1761{
1762 if (offset + length <= m_data.GetByteSize())
1763 return dst.SetData(m_data, offset, length);
1764
1765 const auto process_sp = m_process_wp.lock();
1766 if (process_sp != nullptr)
1767 {
1768 addr_t file_size = offset + length;
1769
1770 DataBufferSP data_sp = ReadMemory(process_sp, m_memory_addr, file_size);
1771 if (!data_sp)
1772 return false;
1773 m_data.SetData(data_sp, 0, file_size);
1774 }
1775
1776 return dst.SetData(m_data, offset, length);
Michael Sartaina7499c92013-07-01 19:45:50 +00001777}
1778
Michael Sartaina7499c92013-07-01 19:45:50 +00001779const ObjectFileELF::ELFSectionHeaderInfo *
1780ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
1781{
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001782 if (!id || !ParseSectionHeaders())
Michael Sartaina7499c92013-07-01 19:45:50 +00001783 return NULL;
1784
1785 if (--id < m_section_headers.size())
1786 return &m_section_headers[id];
1787
1788 return NULL;
1789}
1790
Tamas Berghammer85fadd92015-05-08 09:40:05 +00001791lldb::user_id_t
1792ObjectFileELF::GetSectionIndexByName(const char* name)
1793{
1794 if (!name || !name[0] || !ParseSectionHeaders())
1795 return 0;
1796 for (size_t i = 1; i < m_section_headers.size(); ++i)
1797 if (m_section_headers[i].section_name == ConstString(name))
1798 return i;
1799 return 0;
1800}
1801
Greg Clayton3046e662013-07-10 01:23:25 +00001802void
1803ObjectFileELF::CreateSections(SectionList &unified_section_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804{
Greg Clayton3046e662013-07-10 01:23:25 +00001805 if (!m_sections_ap.get() && ParseSectionHeaders())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001806 {
1807 m_sections_ap.reset(new SectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001808
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001809 for (SectionHeaderCollIter I = m_section_headers.begin();
1810 I != m_section_headers.end(); ++I)
1811 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001812 const ELFSectionHeaderInfo &header = *I;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001813
Michael Sartaina7499c92013-07-01 19:45:50 +00001814 ConstString& name = I->section_name;
Greg Clayton47037bc2012-03-27 02:40:46 +00001815 const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
1816 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001817
Greg Clayton4ceb9982010-07-21 22:54:26 +00001818 static ConstString g_sect_name_text (".text");
1819 static ConstString g_sect_name_data (".data");
1820 static ConstString g_sect_name_bss (".bss");
Greg Clayton741f3f92012-03-27 21:10:07 +00001821 static ConstString g_sect_name_tdata (".tdata");
1822 static ConstString g_sect_name_tbss (".tbss");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001823 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001824 static ConstString g_sect_name_dwarf_debug_addr (".debug_addr");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001825 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
1826 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
1827 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
1828 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
1829 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
1830 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
Siva Chandrad8335e92015-12-16 00:22:08 +00001831 static ConstString g_sect_name_dwarf_debug_macro (".debug_macro");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001832 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
1833 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
1834 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
1835 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001836 static ConstString g_sect_name_dwarf_debug_str_offsets (".debug_str_offsets");
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001837 static ConstString g_sect_name_dwarf_debug_abbrev_dwo (".debug_abbrev.dwo");
1838 static ConstString g_sect_name_dwarf_debug_info_dwo (".debug_info.dwo");
1839 static ConstString g_sect_name_dwarf_debug_line_dwo (".debug_line.dwo");
Siva Chandrad8335e92015-12-16 00:22:08 +00001840 static ConstString g_sect_name_dwarf_debug_macro_dwo (".debug_macro.dwo");
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001841 static ConstString g_sect_name_dwarf_debug_loc_dwo (".debug_loc.dwo");
1842 static ConstString g_sect_name_dwarf_debug_str_dwo (".debug_str.dwo");
1843 static ConstString g_sect_name_dwarf_debug_str_offsets_dwo (".debug_str_offsets.dwo");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001844 static ConstString g_sect_name_eh_frame (".eh_frame");
Tamas Berghammer648f3c72015-09-30 13:50:14 +00001845 static ConstString g_sect_name_arm_exidx (".ARM.exidx");
1846 static ConstString g_sect_name_arm_extab (".ARM.extab");
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001847 static ConstString g_sect_name_go_symtab (".gosymtab");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001848
1849 SectionType sect_type = eSectionTypeOther;
1850
Greg Clayton741f3f92012-03-27 21:10:07 +00001851 bool is_thread_specific = false;
Michael Sartaina7499c92013-07-01 19:45:50 +00001852
Greg Clayton4ceb9982010-07-21 22:54:26 +00001853 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
1854 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
1855 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
Greg Clayton741f3f92012-03-27 21:10:07 +00001856 else if (name == g_sect_name_tdata)
1857 {
1858 sect_type = eSectionTypeData;
Ed Maste81b4c5f2016-01-04 01:43:47 +00001859 is_thread_specific = true;
Greg Clayton741f3f92012-03-27 21:10:07 +00001860 }
1861 else if (name == g_sect_name_tbss)
1862 {
Ed Maste81b4c5f2016-01-04 01:43:47 +00001863 sect_type = eSectionTypeZeroFill;
1864 is_thread_specific = true;
Greg Clayton741f3f92012-03-27 21:10:07 +00001865 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001866 // .debug_abbrev – Abbreviations used in the .debug_info section
1867 // .debug_aranges – Lookup table for mapping addresses to compilation units
1868 // .debug_frame – Call frame information
1869 // .debug_info – The core DWARF information section
1870 // .debug_line – Line number information
1871 // .debug_loc – Location lists used in DW_AT_location attributes
1872 // .debug_macinfo – Macro information
1873 // .debug_pubnames – Lookup table for mapping object and function names to compilation units
1874 // .debug_pubtypes – Lookup table for mapping type names to compilation units
1875 // .debug_ranges – Address ranges used in DW_AT_ranges attributes
1876 // .debug_str – String table used in .debug_info
Michael Sartain3cf443d2013-07-17 00:26:30 +00001877 // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html
1878 // MISSING? .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644
Michael Sartaina7499c92013-07-01 19:45:50 +00001879 // MISSING? .debug_types - Type descriptions from DWARF 4? See http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001880 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
1881 else if (name == g_sect_name_dwarf_debug_addr) sect_type = eSectionTypeDWARFDebugAddr;
1882 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
1883 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
1884 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
1885 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
1886 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
1887 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
Siva Chandrad8335e92015-12-16 00:22:08 +00001888 else if (name == g_sect_name_dwarf_debug_macro) sect_type = eSectionTypeDWARFDebugMacro;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001889 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
1890 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
1891 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
1892 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
1893 else if (name == g_sect_name_dwarf_debug_str_offsets) sect_type = eSectionTypeDWARFDebugStrOffsets;
1894 else if (name == g_sect_name_dwarf_debug_abbrev_dwo) sect_type = eSectionTypeDWARFDebugAbbrev;
1895 else if (name == g_sect_name_dwarf_debug_info_dwo) sect_type = eSectionTypeDWARFDebugInfo;
1896 else if (name == g_sect_name_dwarf_debug_line_dwo) sect_type = eSectionTypeDWARFDebugLine;
Siva Chandrad8335e92015-12-16 00:22:08 +00001897 else if (name == g_sect_name_dwarf_debug_macro_dwo) sect_type = eSectionTypeDWARFDebugMacro;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001898 else if (name == g_sect_name_dwarf_debug_loc_dwo) sect_type = eSectionTypeDWARFDebugLoc;
1899 else if (name == g_sect_name_dwarf_debug_str_dwo) sect_type = eSectionTypeDWARFDebugStr;
1900 else if (name == g_sect_name_dwarf_debug_str_offsets_dwo) sect_type = eSectionTypeDWARFDebugStrOffsets;
1901 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
Tamas Berghammer648f3c72015-09-30 13:50:14 +00001902 else if (name == g_sect_name_arm_exidx) sect_type = eSectionTypeARMexidx;
1903 else if (name == g_sect_name_arm_extab) sect_type = eSectionTypeARMextab;
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001904 else if (name == g_sect_name_go_symtab) sect_type = eSectionTypeGoSymtab;
Michael Sartaina7499c92013-07-01 19:45:50 +00001905
1906 switch (header.sh_type)
Michael Sartainc836ae72013-05-23 20:57:03 +00001907 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001908 case SHT_SYMTAB:
1909 assert (sect_type == eSectionTypeOther);
1910 sect_type = eSectionTypeELFSymbolTable;
1911 break;
1912 case SHT_DYNSYM:
1913 assert (sect_type == eSectionTypeOther);
1914 sect_type = eSectionTypeELFDynamicSymbols;
1915 break;
1916 case SHT_RELA:
1917 case SHT_REL:
1918 assert (sect_type == eSectionTypeOther);
1919 sect_type = eSectionTypeELFRelocationEntries;
1920 break;
1921 case SHT_DYNAMIC:
1922 assert (sect_type == eSectionTypeOther);
1923 sect_type = eSectionTypeELFDynamicLinkInfo;
1924 break;
Michael Sartainc836ae72013-05-23 20:57:03 +00001925 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001926
Matthew Gardinerf03e6d842014-09-29 08:02:24 +00001927 if (eSectionTypeOther == sect_type)
1928 {
1929 // the kalimba toolchain assumes that ELF section names are free-form. It does
Bruce Mitchenere171da52015-07-22 00:16:02 +00001930 // support linkscripts which (can) give rise to various arbitrarily named
Ed Maste81b4c5f2016-01-04 01:43:47 +00001931 // sections being "Code" or "Data".
Matthew Gardinerf03e6d842014-09-29 08:02:24 +00001932 sect_type = kalimbaSectionType(m_header, header);
1933 }
1934
1935 const uint32_t target_bytes_size =
Ed Maste81b4c5f2016-01-04 01:43:47 +00001936 (eSectionTypeData == sect_type || eSectionTypeZeroFill == sect_type) ?
Matthew Gardinerf03e6d842014-09-29 08:02:24 +00001937 m_arch_spec.GetDataByteSize() :
1938 eSectionTypeCode == sect_type ?
1939 m_arch_spec.GetCodeByteSize() : 1;
1940
Zachary Turner736d4d82014-06-25 05:42:32 +00001941 elf::elf_xword log2align = (header.sh_addralign==0)
1942 ? 0
1943 : llvm::Log2_64(header.sh_addralign);
Greg Clayton3046e662013-07-10 01:23:25 +00001944 SectionSP section_sp (new Section(GetModule(), // Module to which this section belongs.
1945 this, // ObjectFile to which this section belongs and should read section data from.
1946 SectionIndex(I), // Section ID.
1947 name, // Section name.
1948 sect_type, // Section type.
1949 header.sh_addr, // VM address.
1950 vm_size, // VM size in bytes of this section.
1951 header.sh_offset, // Offset of this section in the file.
1952 file_size, // Size of the section as found in the file.
Zachary Turner736d4d82014-06-25 05:42:32 +00001953 log2align, // Alignment of the section
Matthew Gardinerf03e6d842014-09-29 08:02:24 +00001954 header.sh_flags, // Flags for this section.
1955 target_bytes_size));// Number of host bytes per target byte
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001956
Greg Clayton741f3f92012-03-27 21:10:07 +00001957 if (is_thread_specific)
1958 section_sp->SetIsThreadSpecific (is_thread_specific);
1959 m_sections_ap->AddSection(section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001960 }
1961 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001962
Greg Clayton3046e662013-07-10 01:23:25 +00001963 if (m_sections_ap.get())
1964 {
1965 if (GetType() == eTypeDebugInfo)
1966 {
1967 static const SectionType g_sections[] =
1968 {
Greg Clayton3046e662013-07-10 01:23:25 +00001969 eSectionTypeDWARFDebugAbbrev,
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001970 eSectionTypeDWARFDebugAddr,
1971 eSectionTypeDWARFDebugAranges,
Greg Clayton3046e662013-07-10 01:23:25 +00001972 eSectionTypeDWARFDebugFrame,
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001973 eSectionTypeDWARFDebugInfo,
Greg Clayton3046e662013-07-10 01:23:25 +00001974 eSectionTypeDWARFDebugLine,
Greg Clayton3046e662013-07-10 01:23:25 +00001975 eSectionTypeDWARFDebugLoc,
1976 eSectionTypeDWARFDebugMacInfo,
1977 eSectionTypeDWARFDebugPubNames,
1978 eSectionTypeDWARFDebugPubTypes,
1979 eSectionTypeDWARFDebugRanges,
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001980 eSectionTypeDWARFDebugStr,
1981 eSectionTypeDWARFDebugStrOffsets,
Greg Clayton3046e662013-07-10 01:23:25 +00001982 eSectionTypeELFSymbolTable,
1983 };
1984 SectionList *elf_section_list = m_sections_ap.get();
1985 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx)
1986 {
1987 SectionType section_type = g_sections[idx];
1988 SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true));
1989 if (section_sp)
1990 {
1991 SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true));
1992 if (module_section_sp)
1993 unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp);
1994 else
1995 unified_section_list.AddSection (section_sp);
1996 }
1997 }
1998 }
1999 else
2000 {
2001 unified_section_list = *m_sections_ap;
2002 }
2003 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002004}
2005
Stephane Sezer9187e732015-09-11 18:56:59 +00002006// Find the arm/aarch64 mapping symbol character in the given symbol name. Mapping symbols have the
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002007// form of "$<char>[.<any>]*". Additionally we recognize cases when the mapping symbol prefixed by
2008// an arbitrary string because if a symbol prefix added to each symbol in the object file with
2009// objcopy then the mapping symbols are also prefixed.
2010static char
2011FindArmAarch64MappingSymbol(const char* symbol_name)
2012{
2013 if (!symbol_name)
2014 return '\0';
2015
2016 const char* dollar_pos = ::strchr(symbol_name, '$');
2017 if (!dollar_pos || dollar_pos[1] == '\0')
2018 return '\0';
2019
2020 if (dollar_pos[2] == '\0' || dollar_pos[2] == '.')
2021 return dollar_pos[1];
2022 return '\0';
2023}
2024
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002025#define STO_MIPS_ISA (3 << 6)
2026#define STO_MICROMIPS (2 << 6)
2027#define IS_MICROMIPS(ST_OTHER) (((ST_OTHER) & STO_MIPS_ISA) == STO_MICROMIPS)
2028
Greg Clayton3046e662013-07-10 01:23:25 +00002029// private
Michael Sartaina7499c92013-07-01 19:45:50 +00002030unsigned
Greg Clayton3046e662013-07-10 01:23:25 +00002031ObjectFileELF::ParseSymbols (Symtab *symtab,
2032 user_id_t start_id,
2033 SectionList *section_list,
2034 const size_t num_symbols,
2035 const DataExtractor &symtab_data,
2036 const DataExtractor &strtab_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002037{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002038 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00002039 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002040
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002041 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");
Justin Hibbits6256a0e2014-10-31 02:34:28 +00002052 static ConstString opd_section_name(".opd"); // For ppc64
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002053
Tamas Berghammerc9627ae2015-07-13 09:54:41 +00002054 // On Android the oatdata and the oatexec symbols in system@framework@boot.oat covers the full
2055 // .text section what causes issues with displaying unusable symbol name to the user and very
2056 // slow unwinding speed because the instruction emulation based unwind plans try to emulate all
2057 // instructions in these symbols. Don't add these symbols to the symbol list as they have no
2058 // use for the debugger and they are causing a lot of trouble.
2059 // Filtering can't be restricted to Android because this special object file don't contain the
2060 // note section specifying the environment to Android but the custom extension and file name
2061 // makes it highly unlikely that this will collide with anything else.
2062 bool skip_oatdata_oatexec = m_file.GetFilename() == ConstString("system@framework@boot.oat");
2063
Tamas Berghammer9fa11472015-10-27 10:43:27 +00002064 ArchSpec arch;
2065 GetArchitecture(arch);
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002066 ModuleSP module_sp(GetModule());
2067 SectionList* module_section_list = module_sp ? module_sp->GetSectionList() : nullptr;
Tamas Berghammer9fa11472015-10-27 10:43:27 +00002068
2069 // Local cache to avoid doing a FindSectionByName for each symbol. The "const char*" key must
2070 // came from a ConstString object so they can be compared by pointer
2071 std::unordered_map<const char*, lldb::SectionSP> section_name_to_section;
2072
Stephen Wilson499b40e2011-03-30 16:07:05 +00002073 unsigned i;
2074 for (i = 0; i < num_symbols; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002075 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002076 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002077 break;
Ed Maste81b4c5f2016-01-04 01:43:47 +00002078
Greg Clayton9594f4c2013-04-13 23:17:23 +00002079 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
2080
Andrew MacPherson17220c12014-03-05 10:12:43 +00002081 // No need to add non-section symbols that have no names
2082 if (symbol.getType() != STT_SECTION &&
2083 (symbol_name == NULL || symbol_name[0] == '\0'))
Greg Clayton9594f4c2013-04-13 23:17:23 +00002084 continue;
2085
Tamas Berghammerc9627ae2015-07-13 09:54:41 +00002086 // Skipping oatdata and oatexec sections if it is requested. See details above the
2087 // definition of skip_oatdata_oatexec for the reasons.
2088 if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 || ::strcmp(symbol_name, "oatexec") == 0))
2089 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002090
Greg Claytone72dfb32012-02-24 01:59:29 +00002091 SectionSP symbol_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002092 SymbolType symbol_type = eSymbolTypeInvalid;
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002093 Elf64_Half section_idx = symbol.st_shndx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002094
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002095 switch (section_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002096 {
2097 case SHN_ABS:
2098 symbol_type = eSymbolTypeAbsolute;
2099 break;
2100 case SHN_UNDEF:
2101 symbol_type = eSymbolTypeUndefined;
2102 break;
2103 default:
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002104 symbol_section_sp = section_list->GetSectionAtIndex(section_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002105 break;
2106 }
2107
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002108 // If a symbol is undefined do not process it further even if it has a STT type
2109 if (symbol_type != eSymbolTypeUndefined)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002110 {
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002111 switch (symbol.getType())
2112 {
2113 default:
2114 case STT_NOTYPE:
2115 // The symbol's type is not specified.
2116 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002117
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002118 case STT_OBJECT:
2119 // The symbol is associated with a data object, such as a variable,
2120 // an array, etc.
2121 symbol_type = eSymbolTypeData;
2122 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002123
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002124 case STT_FUNC:
2125 // The symbol is associated with a function or other executable code.
2126 symbol_type = eSymbolTypeCode;
2127 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002128
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002129 case STT_SECTION:
2130 // The symbol is associated with a section. Symbol table entries of
2131 // this type exist primarily for relocation and normally have
2132 // STB_LOCAL binding.
2133 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002134
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002135 case STT_FILE:
2136 // Conventionally, the symbol's name gives the name of the source
2137 // file associated with the object file. A file symbol has STB_LOCAL
2138 // binding, its section index is SHN_ABS, and it precedes the other
2139 // STB_LOCAL symbols for the file, if it is present.
Greg Clayton9594f4c2013-04-13 23:17:23 +00002140 symbol_type = eSymbolTypeSourceFile;
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002141 break;
Matt Kopec00049b82013-02-27 20:13:38 +00002142
2143 case STT_GNU_IFUNC:
2144 // The symbol is associated with an indirect function. The actual
2145 // function will be resolved if it is referenced.
2146 symbol_type = eSymbolTypeResolver;
2147 break;
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002148 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002149 }
2150
2151 if (symbol_type == eSymbolTypeInvalid)
2152 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002153 if (symbol_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002154 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002155 const ConstString &sect_name = symbol_section_sp->GetName();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002156 if (sect_name == text_section_name ||
2157 sect_name == init_section_name ||
2158 sect_name == fini_section_name ||
2159 sect_name == ctors_section_name ||
2160 sect_name == dtors_section_name)
2161 {
2162 symbol_type = eSymbolTypeCode;
2163 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002164 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)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002169 {
2170 symbol_type = eSymbolTypeData;
2171 }
2172 }
2173 }
2174
Todd Fialafbd703a2014-09-15 22:33:39 +00002175 int64_t symbol_value_offset = 0;
2176 uint32_t additional_flags = 0;
Todd Fiala1a088662014-09-15 16:27:44 +00002177
Tamas Berghammer9fa11472015-10-27 10:43:27 +00002178 if (arch.IsValid())
Todd Fiala1a088662014-09-15 16:27:44 +00002179 {
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002180 if (arch.GetMachine() == llvm::Triple::arm)
Todd Fialafbd703a2014-09-15 22:33:39 +00002181 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002182 if (symbol.getBinding() == STB_LOCAL)
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002183 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002184 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002185 if (symbol_type == eSymbolTypeCode)
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002186 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002187 switch (mapping_symbol)
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002188 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002189 case 'a':
2190 // $a[.<any>]* - marks an ARM instruction sequence
2191 m_address_class_map[symbol.st_value] = eAddressClassCode;
2192 break;
2193 case 'b':
2194 case 't':
2195 // $b[.<any>]* - marks a THUMB BL instruction sequence
2196 // $t[.<any>]* - marks a THUMB instruction sequence
2197 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA;
2198 break;
2199 case 'd':
2200 // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2201 m_address_class_map[symbol.st_value] = eAddressClassData;
2202 break;
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002203 }
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002204 }
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002205 if (mapping_symbol)
2206 continue;
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002207 }
2208 }
2209 else if (arch.GetMachine() == llvm::Triple::aarch64)
2210 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002211 if (symbol.getBinding() == STB_LOCAL)
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002212 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002213 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002214 if (symbol_type == eSymbolTypeCode)
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002215 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002216 switch (mapping_symbol)
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002217 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002218 case 'x':
2219 // $x[.<any>]* - marks an A64 instruction sequence
2220 m_address_class_map[symbol.st_value] = eAddressClassCode;
2221 break;
2222 case 'd':
2223 // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2224 m_address_class_map[symbol.st_value] = eAddressClassData;
2225 break;
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002226 }
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002227 }
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002228 if (mapping_symbol)
2229 continue;
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002230 }
2231 }
2232
2233 if (arch.GetMachine() == llvm::Triple::arm)
2234 {
Aidan Dodds5f2d0c32015-05-28 15:37:01 +00002235 if (symbol_type == eSymbolTypeCode)
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002236 {
Aidan Dodds5f2d0c32015-05-28 15:37:01 +00002237 if (symbol.st_value & 1)
2238 {
2239 // Subtracting 1 from the address effectively unsets
2240 // the low order bit, which results in the address
2241 // actually pointing to the beginning of the symbol.
2242 // This delta will be used below in conjunction with
2243 // symbol.st_value to produce the final symbol_value
2244 // that we store in the symtab.
2245 symbol_value_offset = -1;
Aidan Dodds5f2d0c32015-05-28 15:37:01 +00002246 m_address_class_map[symbol.st_value^1] = eAddressClassCodeAlternateISA;
2247 }
2248 else
2249 {
2250 // This address is ARM
2251 m_address_class_map[symbol.st_value] = eAddressClassCode;
2252 }
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002253 }
Todd Fialafbd703a2014-09-15 22:33:39 +00002254 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002255
2256 /*
2257 * MIPS:
2258 * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for MIPS).
2259 * This allows processer to switch between microMIPS and MIPS without any need
2260 * for special mode-control register. However, apart from .debug_line, none of
2261 * the ELF/DWARF sections set the ISA bit (for symbol or section). Use st_other
2262 * flag to check whether the symbol is microMIPS and then set the address class
2263 * accordingly.
2264 */
2265 const llvm::Triple::ArchType llvm_arch = arch.GetMachine();
2266 if (llvm_arch == llvm::Triple::mips || llvm_arch == llvm::Triple::mipsel
2267 || llvm_arch == llvm::Triple::mips64 || llvm_arch == llvm::Triple::mips64el)
2268 {
2269 if (IS_MICROMIPS(symbol.st_other))
2270 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA;
2271 else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode))
2272 {
2273 symbol.st_value = symbol.st_value & (~1ull);
2274 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA;
2275 }
2276 else
2277 {
2278 if (symbol_type == eSymbolTypeCode)
2279 m_address_class_map[symbol.st_value] = eAddressClassCode;
2280 else if (symbol_type == eSymbolTypeData)
2281 m_address_class_map[symbol.st_value] = eAddressClassData;
2282 else
2283 m_address_class_map[symbol.st_value] = eAddressClassUnknown;
2284 }
2285 }
Todd Fiala1a088662014-09-15 16:27:44 +00002286 }
2287
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002288 // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB symbols. See above for
2289 // more details.
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002290 uint64_t symbol_value = symbol.st_value + symbol_value_offset;
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002291
2292 if (symbol_section_sp == nullptr && section_idx == SHN_ABS && symbol.st_size != 0)
2293 {
2294 // We don't have a section for a symbol with non-zero size. Create a new section for it
2295 // so the address range covered by the symbol is also covered by the module (represented
2296 // through the section list). It is needed so module lookup for the addresses covered
2297 // by this symbol will be successfull. This case happens for absolute symbols.
2298 ConstString fake_section_name(std::string(".absolute.") + symbol_name);
2299 symbol_section_sp = std::make_shared<Section>(module_sp,
2300 this,
2301 SHN_ABS,
2302 fake_section_name,
2303 eSectionTypeAbsoluteAddress,
2304 symbol_value,
2305 symbol.st_size,
2306 0, 0, 0,
2307 SHF_ALLOC);
2308
2309 module_section_list->AddSection(symbol_section_sp);
2310 section_list->AddSection(symbol_section_sp);
2311 }
2312
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002313 if (symbol_section_sp && CalculateType() != ObjectFile::Type::eTypeObjectFile)
2314 symbol_value -= symbol_section_sp->GetFileAddress();
2315
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002316 if (symbol_section_sp && module_section_list && module_section_list != section_list)
Michael Sartaina7499c92013-07-01 19:45:50 +00002317 {
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002318 const ConstString &sect_name = symbol_section_sp->GetName();
2319 auto section_it = section_name_to_section.find(sect_name.GetCString());
2320 if (section_it == section_name_to_section.end())
2321 section_it = section_name_to_section.emplace(
2322 sect_name.GetCString(),
2323 module_section_list->FindSectionByName (sect_name)).first;
2324 if (section_it->second && section_it->second->GetFileSize())
2325 symbol_section_sp = section_it->second;
Michael Sartaina7499c92013-07-01 19:45:50 +00002326 }
2327
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002328 bool is_global = symbol.getBinding() == STB_GLOBAL;
Todd Fialafbd703a2014-09-15 22:33:39 +00002329 uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags;
Greg Clayton47037bc2012-03-27 02:40:46 +00002330 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Todd Fialafbd703a2014-09-15 22:33:39 +00002331
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002332 llvm::StringRef symbol_ref(symbol_name);
2333
2334 // Symbol names may contain @VERSION suffixes. Find those and strip them temporarily.
2335 size_t version_pos = symbol_ref.find('@');
2336 bool has_suffix = version_pos != llvm::StringRef::npos;
2337 llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos);
2338 Mangled mangled(ConstString(symbol_bare), is_mangled);
2339
2340 // Now append the suffix back to mangled and unmangled names. Only do it if the
Bruce Mitchenere171da52015-07-22 00:16:02 +00002341 // demangling was successful (string is not empty).
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002342 if (has_suffix)
2343 {
2344 llvm::StringRef suffix = symbol_ref.substr(version_pos);
2345
2346 llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef();
2347 if (! mangled_name.empty())
2348 mangled.SetMangledName( ConstString((mangled_name + suffix).str()) );
2349
Greg Claytonddaf6a72015-07-08 22:32:23 +00002350 ConstString demangled = mangled.GetDemangledName(lldb::eLanguageTypeUnknown);
2351 llvm::StringRef demangled_name = demangled.GetStringRef();
2352 if (!demangled_name.empty())
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002353 mangled.SetDemangledName( ConstString((demangled_name + suffix).str()) );
2354 }
2355
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002356 // In ELF all symbol should have a valid size but it is not true for some function symbols
2357 // coming from hand written assembly. As none of the function symbol should have 0 size we
2358 // try to calculate the size for these symbols in the symtab with saying that their original
Tamas Berghammer8c6996f72016-01-19 10:24:51 +00002359 // size is not valid.
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002360 bool symbol_size_valid = symbol.st_size != 0 || symbol.getType() != STT_FUNC;
Tamas Berghammer8c6996f72016-01-19 10:24:51 +00002361
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002362 Symbol dc_symbol(
Greg Claytone72dfb32012-02-24 01:59:29 +00002363 i + start_id, // ID is the original symbol table index.
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002364 mangled,
Greg Claytone72dfb32012-02-24 01:59:29 +00002365 symbol_type, // Type of this symbol
2366 is_global, // Is this globally visible?
2367 false, // Is this symbol debug info?
2368 false, // Is this symbol a trampoline?
2369 false, // Is this symbol artificial?
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002370 AddressRange(
2371 symbol_section_sp, // Section in which this symbol is defined or null.
2372 symbol_value, // Offset in section or symbol value.
2373 symbol.st_size), // Size in bytes of this symbol.
Tamas Berghammer8c6996f72016-01-19 10:24:51 +00002374 symbol_size_valid, // Symbol size is valid
Tamas Berghammer44ff9cce2015-06-24 11:27:32 +00002375 has_suffix, // Contains linker annotations?
2376 flags); // Symbol flags.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002377 symtab->AddSymbol(dc_symbol);
2378 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00002379 return i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002380}
2381
Stephen Wilson499b40e2011-03-30 16:07:05 +00002382unsigned
Tamas Berghammer8c6996f72016-01-19 10:24:51 +00002383ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
2384 user_id_t start_id,
2385 lldb_private::Section *symtab)
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002386{
Greg Clayton3046e662013-07-10 01:23:25 +00002387 if (symtab->GetObjectFile() != this)
2388 {
2389 // If the symbol table section is owned by a different object file, have it do the
2390 // parsing.
2391 ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile());
2392 return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab);
2393 }
2394
2395 // Get section list for this object file.
2396 SectionList *section_list = m_sections_ap.get();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002397 if (!section_list)
Stephen Wilson499b40e2011-03-30 16:07:05 +00002398 return 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002399
Greg Clayton3046e662013-07-10 01:23:25 +00002400 user_id_t symtab_id = symtab->GetID();
2401 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
Ed Maste81b4c5f2016-01-04 01:43:47 +00002402 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
Michael Sartaina7499c92013-07-01 19:45:50 +00002403 symtab_hdr->sh_type == SHT_DYNSYM);
2404
Greg Clayton3046e662013-07-10 01:23:25 +00002405 // sh_link: section header index of associated string table.
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002406 // Section ID's are ones based.
Stephen Wilson499b40e2011-03-30 16:07:05 +00002407 user_id_t strtab_id = symtab_hdr->sh_link + 1;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002408 Section *strtab = section_list->FindSectionByID(strtab_id).get();
Greg Clayton3046e662013-07-10 01:23:25 +00002409
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002410 if (symtab && strtab)
2411 {
Greg Clayton3046e662013-07-10 01:23:25 +00002412 assert (symtab->GetObjectFile() == this);
2413 assert (strtab->GetObjectFile() == this);
2414
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002415 DataExtractor symtab_data;
2416 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00002417 if (ReadSectionData(symtab, symtab_data) &&
2418 ReadSectionData(strtab, strtab_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002419 {
Greg Clayton3046e662013-07-10 01:23:25 +00002420 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
2421
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +00002422 return ParseSymbols(symbol_table, start_id, section_list,
2423 num_symbols, symtab_data, strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002424 }
2425 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00002426
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +00002427 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002428}
2429
2430size_t
2431ObjectFileELF::ParseDynamicSymbols()
2432{
2433 if (m_dynamic_symbols.size())
2434 return m_dynamic_symbols.size();
2435
Stephen Wilson499b40e2011-03-30 16:07:05 +00002436 SectionList *section_list = GetSectionList();
2437 if (!section_list)
Bill Wendlinged24dcc2012-04-03 07:50:11 +00002438 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002439
Greg Clayton3046e662013-07-10 01:23:25 +00002440 // Find the SHT_DYNAMIC section.
2441 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
Stephen Wilson499b40e2011-03-30 16:07:05 +00002442 if (!dynsym)
Bill Wendlinged24dcc2012-04-03 07:50:11 +00002443 return 0;
Greg Clayton3046e662013-07-10 01:23:25 +00002444 assert (dynsym->GetObjectFile() == this);
Stephen Wilson499b40e2011-03-30 16:07:05 +00002445
2446 ELFDynamic symbol;
2447 DataExtractor dynsym_data;
Greg Claytonc9660542012-02-05 02:38:54 +00002448 if (ReadSectionData(dynsym, dynsym_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00002449 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002450 const lldb::offset_t section_size = dynsym_data.GetByteSize();
2451 lldb::offset_t cursor = 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002452
2453 while (cursor < section_size)
2454 {
Stephen Wilson499b40e2011-03-30 16:07:05 +00002455 if (!symbol.Parse(dynsym_data, &cursor))
2456 break;
2457
2458 m_dynamic_symbols.push_back(symbol);
2459 }
2460 }
2461
2462 return m_dynamic_symbols.size();
2463}
2464
2465const ELFDynamic *
2466ObjectFileELF::FindDynamicSymbol(unsigned tag)
2467{
2468 if (!ParseDynamicSymbols())
2469 return NULL;
2470
Stephen Wilson499b40e2011-03-30 16:07:05 +00002471 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
2472 DynamicSymbolCollIter E = m_dynamic_symbols.end();
2473 for ( ; I != E; ++I)
2474 {
2475 ELFDynamic *symbol = &*I;
2476
2477 if (symbol->d_tag == tag)
2478 return symbol;
2479 }
2480
2481 return NULL;
2482}
2483
Stephen Wilson499b40e2011-03-30 16:07:05 +00002484unsigned
2485ObjectFileELF::PLTRelocationType()
2486{
Michael Sartainf7899542013-08-22 21:25:53 +00002487 // DT_PLTREL
2488 // This member specifies the type of relocation entry to which the
2489 // procedure linkage table refers. The d_val member holds DT_REL or
2490 // DT_RELA, as appropriate. All relocations in a procedure linkage table
2491 // must use the same relocation.
Stephen Wilson499b40e2011-03-30 16:07:05 +00002492 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
2493
2494 if (symbol)
2495 return symbol->d_val;
2496
2497 return 0;
2498}
2499
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002500// Returns the size of the normal plt entries and the offset of the first normal plt entry. The
Bruce Mitchenere171da52015-07-22 00:16:02 +00002501// 0th entry in the plt table is usually a resolution entry which have different size in some
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002502// architectures then the rest of the plt entries.
2503static std::pair<uint64_t, uint64_t>
2504GetPltEntrySizeAndOffset(const ELFSectionHeader* rel_hdr, const ELFSectionHeader* plt_hdr)
2505{
2506 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2507
2508 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 bytes.
2509 // So round the entsize up by the alignment if addralign is set.
2510 elf_xword plt_entsize = plt_hdr->sh_addralign ?
Rafael Espindolaa94ae1e2016-01-18 20:57:54 +00002511 llvm::alignTo (plt_hdr->sh_entsize, plt_hdr->sh_addralign) : plt_hdr->sh_entsize;
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002512
2513 if (plt_entsize == 0)
2514 {
2515 // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the size of the plt
2516 // entries based on the number of entries and the size of the plt section with the
Bruce Mitchenere171da52015-07-22 00:16:02 +00002517 // assumption that the size of the 0th entry is at least as big as the size of the normal
2518 // entries and it isn't much bigger then that.
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002519 if (plt_hdr->sh_addralign)
2520 plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign / (num_relocations + 1) * plt_hdr->sh_addralign;
2521 else
2522 plt_entsize = plt_hdr->sh_size / (num_relocations + 1);
2523 }
2524
2525 elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize;
2526
2527 return std::make_pair(plt_entsize, plt_offset);
2528}
2529
Stephen Wilson499b40e2011-03-30 16:07:05 +00002530static unsigned
2531ParsePLTRelocations(Symtab *symbol_table,
2532 user_id_t start_id,
2533 unsigned rel_type,
2534 const ELFHeader *hdr,
2535 const ELFSectionHeader *rel_hdr,
2536 const ELFSectionHeader *plt_hdr,
2537 const ELFSectionHeader *sym_hdr,
Greg Claytone72dfb32012-02-24 01:59:29 +00002538 const lldb::SectionSP &plt_section_sp,
Stephen Wilson499b40e2011-03-30 16:07:05 +00002539 DataExtractor &rel_data,
2540 DataExtractor &symtab_data,
2541 DataExtractor &strtab_data)
2542{
2543 ELFRelocation rel(rel_type);
2544 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00002545 lldb::offset_t offset = 0;
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002546
2547 uint64_t plt_offset, plt_entsize;
2548 std::tie(plt_entsize, plt_offset) = GetPltEntrySizeAndOffset(rel_hdr, plt_hdr);
Greg Claytonc7bece562013-01-25 18:06:21 +00002549 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002550
2551 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2552 reloc_info_fn reloc_type;
2553 reloc_info_fn reloc_symbol;
2554
Greg Claytond091afe2012-11-12 22:53:16 +00002555 if (hdr->Is32Bit())
Stephen Wilson499b40e2011-03-30 16:07:05 +00002556 {
2557 reloc_type = ELFRelocation::RelocType32;
2558 reloc_symbol = ELFRelocation::RelocSymbol32;
2559 }
2560 else
2561 {
2562 reloc_type = ELFRelocation::RelocType64;
2563 reloc_symbol = ELFRelocation::RelocSymbol64;
2564 }
2565
2566 unsigned slot_type = hdr->GetRelocationJumpSlotType();
2567 unsigned i;
2568 for (i = 0; i < num_relocations; ++i)
2569 {
2570 if (rel.Parse(rel_data, &offset) == false)
2571 break;
2572
2573 if (reloc_type(rel) != slot_type)
2574 continue;
2575
Greg Claytonc7bece562013-01-25 18:06:21 +00002576 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002577 if (!symbol.Parse(symtab_data, &symbol_offset))
2578 break;
2579
2580 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Greg Clayton47037bc2012-03-27 02:40:46 +00002581 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002582 uint64_t plt_index = plt_offset + i * plt_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002583
2584 Symbol jump_symbol(
2585 i + start_id, // Symbol table index
2586 symbol_name, // symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +00002587 is_mangled, // is the symbol name mangled?
Stephen Wilson499b40e2011-03-30 16:07:05 +00002588 eSymbolTypeTrampoline, // Type of this symbol
2589 false, // Is this globally visible?
2590 false, // Is this symbol debug info?
2591 true, // Is this symbol a trampoline?
2592 true, // Is this symbol artificial?
Greg Claytone72dfb32012-02-24 01:59:29 +00002593 plt_section_sp, // Section in which this symbol is defined or null.
Stephen Wilson499b40e2011-03-30 16:07:05 +00002594 plt_index, // Offset in section or symbol value.
2595 plt_entsize, // Size in bytes of this symbol.
Greg Clayton9594f4c2013-04-13 23:17:23 +00002596 true, // Size is valid
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002597 false, // Contains linker annotations?
Stephen Wilson499b40e2011-03-30 16:07:05 +00002598 0); // Symbol flags.
2599
2600 symbol_table->AddSymbol(jump_symbol);
2601 }
2602
2603 return i;
2604}
2605
2606unsigned
2607ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
2608 user_id_t start_id,
Michael Sartaina7499c92013-07-01 19:45:50 +00002609 const ELFSectionHeaderInfo *rel_hdr,
Stephen Wilson499b40e2011-03-30 16:07:05 +00002610 user_id_t rel_id)
2611{
2612 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2613
Greg Clayton3046e662013-07-10 01:23:25 +00002614 // The link field points to the associated symbol table. The info field
Stephen Wilson499b40e2011-03-30 16:07:05 +00002615 // points to the section holding the plt.
2616 user_id_t symtab_id = rel_hdr->sh_link;
2617 user_id_t plt_id = rel_hdr->sh_info;
2618
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002619 // If the link field doesn't point to the appropriate symbol name table then
2620 // try to find it by name as some compiler don't fill in the link fields.
2621 if (!symtab_id)
2622 symtab_id = GetSectionIndexByName(".dynsym");
2623 if (!plt_id)
2624 plt_id = GetSectionIndexByName(".plt");
2625
Stephen Wilson499b40e2011-03-30 16:07:05 +00002626 if (!symtab_id || !plt_id)
2627 return 0;
2628
2629 // Section ID's are ones based;
2630 symtab_id++;
2631 plt_id++;
2632
Michael Sartaina7499c92013-07-01 19:45:50 +00002633 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +00002634 if (!plt_hdr)
2635 return 0;
2636
Michael Sartaina7499c92013-07-01 19:45:50 +00002637 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +00002638 if (!sym_hdr)
2639 return 0;
2640
Greg Clayton3046e662013-07-10 01:23:25 +00002641 SectionList *section_list = m_sections_ap.get();
Stephen Wilson499b40e2011-03-30 16:07:05 +00002642 if (!section_list)
2643 return 0;
2644
2645 Section *rel_section = section_list->FindSectionByID(rel_id).get();
2646 if (!rel_section)
2647 return 0;
2648
Greg Claytone72dfb32012-02-24 01:59:29 +00002649 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
2650 if (!plt_section_sp)
Stephen Wilson499b40e2011-03-30 16:07:05 +00002651 return 0;
2652
2653 Section *symtab = section_list->FindSectionByID(symtab_id).get();
2654 if (!symtab)
2655 return 0;
2656
Greg Clayton3046e662013-07-10 01:23:25 +00002657 // sh_link points to associated string table.
Stephen Wilson499b40e2011-03-30 16:07:05 +00002658 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
2659 if (!strtab)
2660 return 0;
2661
2662 DataExtractor rel_data;
Greg Claytonc9660542012-02-05 02:38:54 +00002663 if (!ReadSectionData(rel_section, rel_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00002664 return 0;
2665
2666 DataExtractor symtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00002667 if (!ReadSectionData(symtab, symtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00002668 return 0;
2669
2670 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00002671 if (!ReadSectionData(strtab, strtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00002672 return 0;
2673
2674 unsigned rel_type = PLTRelocationType();
2675 if (!rel_type)
2676 return 0;
2677
Ed Maste81b4c5f2016-01-04 01:43:47 +00002678 return ParsePLTRelocations (symbol_table,
2679 start_id,
Greg Claytone72dfb32012-02-24 01:59:29 +00002680 rel_type,
Ed Maste81b4c5f2016-01-04 01:43:47 +00002681 &m_header,
2682 rel_hdr,
2683 plt_hdr,
Greg Claytone72dfb32012-02-24 01:59:29 +00002684 sym_hdr,
Ed Maste81b4c5f2016-01-04 01:43:47 +00002685 plt_section_sp,
2686 rel_data,
2687 symtab_data,
Greg Claytone72dfb32012-02-24 01:59:29 +00002688 strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002689}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002690
Andrew MacPherson17220c12014-03-05 10:12:43 +00002691unsigned
2692ObjectFileELF::RelocateSection(Symtab* symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
2693 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,
2694 DataExtractor &rel_data, DataExtractor &symtab_data,
2695 DataExtractor &debug_data, Section* rel_section)
2696{
2697 ELFRelocation rel(rel_hdr->sh_type);
2698 lldb::addr_t offset = 0;
2699 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2700 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2701 reloc_info_fn reloc_type;
2702 reloc_info_fn reloc_symbol;
2703
2704 if (hdr->Is32Bit())
2705 {
2706 reloc_type = ELFRelocation::RelocType32;
2707 reloc_symbol = ELFRelocation::RelocSymbol32;
2708 }
2709 else
2710 {
2711 reloc_type = ELFRelocation::RelocType64;
2712 reloc_symbol = ELFRelocation::RelocSymbol64;
2713 }
2714
2715 for (unsigned i = 0; i < num_relocations; ++i)
2716 {
2717 if (rel.Parse(rel_data, &offset) == false)
2718 break;
2719
2720 Symbol* symbol = NULL;
2721
2722 if (hdr->Is32Bit())
2723 {
2724 switch (reloc_type(rel)) {
2725 case R_386_32:
2726 case R_386_PC32:
2727 default:
2728 assert(false && "unexpected relocation type");
2729 }
2730 } else {
2731 switch (reloc_type(rel)) {
2732 case R_X86_64_64:
2733 {
2734 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
2735 if (symbol)
2736 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00002737 addr_t value = symbol->GetAddressRef().GetFileAddress();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002738 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer();
2739 uint64_t* dst = reinterpret_cast<uint64_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset64(rel));
2740 *dst = value + ELFRelocation::RelocAddend64(rel);
2741 }
2742 break;
2743 }
2744 case R_X86_64_32:
2745 case R_X86_64_32S:
2746 {
2747 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
2748 if (symbol)
2749 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00002750 addr_t value = symbol->GetAddressRef().GetFileAddress();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002751 value += ELFRelocation::RelocAddend32(rel);
2752 assert((reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) ||
2753 (reloc_type(rel) == R_X86_64_32S &&
2754 ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN)));
2755 uint32_t truncated_addr = (value & 0xFFFFFFFF);
2756 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer();
2757 uint32_t* dst = reinterpret_cast<uint32_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel));
2758 *dst = truncated_addr;
2759 }
2760 break;
2761 }
2762 case R_X86_64_PC32:
2763 default:
2764 assert(false && "unexpected relocation type");
2765 }
2766 }
2767 }
2768
2769 return 0;
2770}
2771
2772unsigned
2773ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, user_id_t rel_id)
2774{
2775 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2776
2777 // Parse in the section list if needed.
2778 SectionList *section_list = GetSectionList();
2779 if (!section_list)
2780 return 0;
2781
2782 // Section ID's are ones based.
2783 user_id_t symtab_id = rel_hdr->sh_link + 1;
2784 user_id_t debug_id = rel_hdr->sh_info + 1;
2785
2786 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
2787 if (!symtab_hdr)
2788 return 0;
2789
2790 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);
2791 if (!debug_hdr)
2792 return 0;
2793
2794 Section *rel = section_list->FindSectionByID(rel_id).get();
2795 if (!rel)
2796 return 0;
2797
2798 Section *symtab = section_list->FindSectionByID(symtab_id).get();
2799 if (!symtab)
2800 return 0;
2801
2802 Section *debug = section_list->FindSectionByID(debug_id).get();
2803 if (!debug)
2804 return 0;
2805
2806 DataExtractor rel_data;
2807 DataExtractor symtab_data;
2808 DataExtractor debug_data;
2809
2810 if (ReadSectionData(rel, rel_data) &&
2811 ReadSectionData(symtab, symtab_data) &&
2812 ReadSectionData(debug, debug_data))
2813 {
2814 RelocateSection(m_symtab_ap.get(), &m_header, rel_hdr, symtab_hdr, debug_hdr,
2815 rel_data, symtab_data, debug_data, debug);
2816 }
2817
2818 return 0;
2819}
2820
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002821Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +00002822ObjectFileELF::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002823{
Michael Sartaina7499c92013-07-01 19:45:50 +00002824 ModuleSP module_sp(GetModule());
Greg Clayton3046e662013-07-10 01:23:25 +00002825 if (!module_sp)
2826 return NULL;
Michael Sartaina7499c92013-07-01 19:45:50 +00002827
Greg Clayton3046e662013-07-10 01:23:25 +00002828 // We always want to use the main object file so we (hopefully) only have one cached copy
2829 // of our symtab, dynamic sections, etc.
2830 ObjectFile *module_obj_file = module_sp->GetObjectFile();
2831 if (module_obj_file && module_obj_file != this)
2832 return module_obj_file->GetSymtab();
2833
2834 if (m_symtab_ap.get() == NULL)
2835 {
Pavel Labath783cbdc2015-03-04 10:28:15 +00002836 SectionList *section_list = module_sp->GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00002837 if (!section_list)
2838 return NULL;
2839
Greg Clayton3046e662013-07-10 01:23:25 +00002840 uint64_t symbol_id = 0;
2841 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Stephen Wilson499b40e2011-03-30 16:07:05 +00002842
Michael Sartaina7499c92013-07-01 19:45:50 +00002843 // Sharable objects and dynamic executables usually have 2 distinct symbol
2844 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller
2845 // version of the symtab that only contains global symbols. The information found
2846 // in the dynsym is therefore also found in the symtab, while the reverse is not
2847 // necessarily true.
Greg Clayton3046e662013-07-10 01:23:25 +00002848 Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get();
2849 if (!symtab)
Michael Sartaina7499c92013-07-01 19:45:50 +00002850 {
2851 // The symtab section is non-allocable and can be stripped, so if it doesn't exist
2852 // then use the dynsym section which should always be there.
Greg Clayton3046e662013-07-10 01:23:25 +00002853 symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get();
Michael Sartaina7499c92013-07-01 19:45:50 +00002854 }
Greg Clayton3046e662013-07-10 01:23:25 +00002855 if (symtab)
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002856 {
2857 m_symtab_ap.reset(new Symtab(symtab->GetObjectFile()));
Greg Clayton3046e662013-07-10 01:23:25 +00002858 symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab);
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002859 }
Michael Sartaina7499c92013-07-01 19:45:50 +00002860
Michael Sartainf7899542013-08-22 21:25:53 +00002861 // DT_JMPREL
2862 // If present, this entry's d_ptr member holds the address of relocation
2863 // entries associated solely with the procedure linkage table. Separating
2864 // these relocation entries lets the dynamic linker ignore them during
2865 // process initialization, if lazy binding is enabled. If this entry is
2866 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must
2867 // also be present.
Greg Clayton3046e662013-07-10 01:23:25 +00002868 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
2869 if (symbol)
Michael Sartaina7499c92013-07-01 19:45:50 +00002870 {
Michael Sartainf7899542013-08-22 21:25:53 +00002871 // Synthesize trampoline symbols to help navigate the PLT.
Greg Clayton3046e662013-07-10 01:23:25 +00002872 addr_t addr = symbol->d_ptr;
2873 Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get();
Ed Maste81b4c5f2016-01-04 01:43:47 +00002874 if (reloc_section)
Michael Sartaina7499c92013-07-01 19:45:50 +00002875 {
Greg Clayton3046e662013-07-10 01:23:25 +00002876 user_id_t reloc_id = reloc_section->GetID();
2877 const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id);
2878 assert(reloc_header);
Ed Maste81b4c5f2016-01-04 01:43:47 +00002879
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002880 if (m_symtab_ap == nullptr)
2881 m_symtab_ap.reset(new Symtab(reloc_section->GetObjectFile()));
Michael Sartaina7499c92013-07-01 19:45:50 +00002882
Greg Clayton3046e662013-07-10 01:23:25 +00002883 ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id);
Michael Sartaina7499c92013-07-01 19:45:50 +00002884 }
2885 }
Ed Maste81b4c5f2016-01-04 01:43:47 +00002886
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002887 DWARFCallFrameInfo* eh_frame = GetUnwindTable().GetEHFrameInfo();
2888 if (eh_frame)
2889 {
2890 if (m_symtab_ap == nullptr)
2891 m_symtab_ap.reset(new Symtab(this));
2892 ParseUnwindSymbols (m_symtab_ap.get(), eh_frame);
2893 }
2894
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002895 // If we still don't have any symtab then create an empty instance to avoid do the section
2896 // lookup next time.
2897 if (m_symtab_ap == nullptr)
2898 m_symtab_ap.reset(new Symtab(this));
Ed Maste81b4c5f2016-01-04 01:43:47 +00002899
Tamas Berghammer2480fe02015-06-24 12:31:25 +00002900 m_symtab_ap->CalculateSymbolSizes();
Michael Sartaina7499c92013-07-01 19:45:50 +00002901 }
Andrew MacPherson17220c12014-03-05 10:12:43 +00002902
2903 for (SectionHeaderCollIter I = m_section_headers.begin();
2904 I != m_section_headers.end(); ++I)
2905 {
2906 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL)
2907 {
2908 if (CalculateType() == eTypeObjectFile)
2909 {
2910 const char *section_name = I->section_name.AsCString("");
2911 if (strstr(section_name, ".rela.debug") ||
2912 strstr(section_name, ".rel.debug"))
2913 {
2914 const ELFSectionHeader &reloc_header = *I;
2915 user_id_t reloc_id = SectionIndex(I);
2916 RelocateDebugSections(&reloc_header, reloc_id);
2917 }
2918 }
2919 }
2920 }
Greg Clayton3046e662013-07-10 01:23:25 +00002921 return m_symtab_ap.get();
2922}
2923
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002924void
2925ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table, DWARFCallFrameInfo* eh_frame)
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00002926{
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002927 SectionList* section_list = GetSectionList();
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00002928 if (!section_list)
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002929 return;
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00002930
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002931 // First we save the new symbols into a separate list and add them to the symbol table after
2932 // we colleced all symbols we want to add. This is neccessary because adding a new symbol
2933 // invalidates the internal index of the symtab what causing the next lookup to be slow because
2934 // it have to recalculate the index first.
2935 std::vector<Symbol> new_symbols;
2936
2937 eh_frame->ForEachFDEEntries(
2938 [this, symbol_table, section_list, &new_symbols](lldb::addr_t file_addr,
2939 uint32_t size,
2940 dw_offset_t) {
2941 Symbol* symbol = symbol_table->FindSymbolAtFileAddress(file_addr);
2942 if (symbol)
Tamas Berghammer5bfd4d02016-02-10 12:10:58 +00002943 {
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002944 if (!symbol->GetByteSizeIsValid())
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00002945 {
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002946 symbol->SetByteSize(size);
2947 symbol->SetSizeIsSynthesized(true);
Tamas Berghammered844cb2016-02-10 10:43:34 +00002948 }
2949 }
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002950 else
2951 {
2952 SectionSP section_sp = section_list->FindSectionContainingFileAddress(file_addr);
2953 if (section_sp)
2954 {
2955 addr_t offset = file_addr - section_sp->GetFileAddress();
2956 const char* symbol_name = GetNextSyntheticSymbolName().GetCString();
2957 uint64_t symbol_id = symbol_table->GetNumSymbols();
2958 Symbol eh_symbol(
2959 symbol_id, // Symbol table index.
2960 symbol_name, // Symbol name.
2961 false, // Is the symbol name mangled?
2962 eSymbolTypeCode, // Type of this symbol.
2963 true, // Is this globally visible?
2964 false, // Is this symbol debug info?
2965 false, // Is this symbol a trampoline?
2966 true, // Is this symbol artificial?
2967 section_sp, // Section in which this symbol is defined or null.
2968 offset, // Offset in section or symbol value.
2969 0, // Size: Don't specify the size as an FDE can
2970 false, // Size is valid: cover multiple symbols.
2971 false, // Contains linker annotations?
2972 0); // Symbol flags.
2973 new_symbols.push_back(eh_symbol);
2974 }
2975 }
2976 return true;
2977 });
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00002978
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002979 for (const Symbol& s : new_symbols)
2980 symbol_table->AddSymbol(s);
2981}
Tamas Berghammer5bfd4d02016-02-10 12:10:58 +00002982
Greg Clayton3046e662013-07-10 01:23:25 +00002983bool
2984ObjectFileELF::IsStripped ()
2985{
2986 // TODO: determine this for ELF
2987 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002988}
2989
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002990//===----------------------------------------------------------------------===//
2991// Dump
2992//
2993// Dump the specifics of the runtime file container (such as any headers
2994// segments, sections, etc).
2995//----------------------------------------------------------------------
2996void
2997ObjectFileELF::Dump(Stream *s)
2998{
2999 DumpELFHeader(s, m_header);
3000 s->EOL();
3001 DumpELFProgramHeaders(s);
3002 s->EOL();
3003 DumpELFSectionHeaders(s);
3004 s->EOL();
3005 SectionList *section_list = GetSectionList();
3006 if (section_list)
Greg Clayton10177aa2010-12-08 05:08:21 +00003007 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003008 Symtab *symtab = GetSymtab();
3009 if (symtab)
Greg Claytone0d378b2011-03-24 21:19:54 +00003010 symtab->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003011 s->EOL();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003012 DumpDependentModules(s);
3013 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003014}
3015
3016//----------------------------------------------------------------------
3017// DumpELFHeader
3018//
3019// Dump the ELF header to the specified output stream
3020//----------------------------------------------------------------------
3021void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003022ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003023{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003024 s->PutCString("ELF Header\n");
3025 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
3026 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
3027 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
3028 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
3029 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
3030 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
3031 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003032
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003033 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
3034 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003035 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
3036 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
3037 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
3038
3039 s->Printf("e_type = 0x%4.4x ", header.e_type);
3040 DumpELFHeader_e_type(s, header.e_type);
3041 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
3042 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Daniel Malead01b2952012-11-29 21:49:15 +00003043 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
3044 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
3045 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003046 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
3047 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
3048 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
3049 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
3050 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
3051 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
3052 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
3053}
3054
3055//----------------------------------------------------------------------
3056// DumpELFHeader_e_type
3057//
3058// Dump an token value for the ELF header member e_type
3059//----------------------------------------------------------------------
3060void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003061ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003062{
3063 switch (e_type)
3064 {
3065 case ET_NONE: *s << "ET_NONE"; break;
3066 case ET_REL: *s << "ET_REL"; break;
3067 case ET_EXEC: *s << "ET_EXEC"; break;
3068 case ET_DYN: *s << "ET_DYN"; break;
3069 case ET_CORE: *s << "ET_CORE"; break;
3070 default:
3071 break;
3072 }
3073}
3074
3075//----------------------------------------------------------------------
3076// DumpELFHeader_e_ident_EI_DATA
3077//
3078// Dump an token value for the ELF header member e_ident[EI_DATA]
3079//----------------------------------------------------------------------
3080void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003081ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003082{
3083 switch (ei_data)
3084 {
3085 case ELFDATANONE: *s << "ELFDATANONE"; break;
3086 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
3087 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
3088 default:
3089 break;
3090 }
3091}
3092
3093
3094//----------------------------------------------------------------------
3095// DumpELFProgramHeader
3096//
3097// Dump a single ELF program header to the specified output stream
3098//----------------------------------------------------------------------
3099void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003100ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003101{
3102 DumpELFProgramHeader_p_type(s, ph.p_type);
Daniel Malead01b2952012-11-29 21:49:15 +00003103 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr);
3104 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003105
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003106 DumpELFProgramHeader_p_flags(s, ph.p_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00003107 s->Printf(") %8.8" PRIx64, ph.p_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003108}
3109
3110//----------------------------------------------------------------------
3111// DumpELFProgramHeader_p_type
3112//
3113// Dump an token value for the ELF program header member p_type which
3114// describes the type of the program header
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003115// ----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003116void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003117ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003118{
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00003119 const int kStrWidth = 15;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003120 switch (p_type)
3121 {
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00003122 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
3123 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
3124 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
3125 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
3126 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
3127 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
3128 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
3129 CASE_AND_STREAM(s, PT_TLS , kStrWidth);
3130 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003131 default:
3132 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
3133 break;
3134 }
3135}
3136
3137
3138//----------------------------------------------------------------------
3139// DumpELFProgramHeader_p_flags
3140//
3141// Dump an token value for the ELF program header member p_flags
3142//----------------------------------------------------------------------
3143void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003144ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003145{
3146 *s << ((p_flags & PF_X) ? "PF_X" : " ")
3147 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
3148 << ((p_flags & PF_W) ? "PF_W" : " ")
3149 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
3150 << ((p_flags & PF_R) ? "PF_R" : " ");
3151}
3152
3153//----------------------------------------------------------------------
3154// DumpELFProgramHeaders
3155//
3156// Dump all of the ELF program header to the specified output stream
3157//----------------------------------------------------------------------
3158void
3159ObjectFileELF::DumpELFProgramHeaders(Stream *s)
3160{
Ed Maste3a8ab6e2015-02-23 15:33:11 +00003161 if (!ParseProgramHeaders())
3162 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003163
Ed Maste3a8ab6e2015-02-23 15:33:11 +00003164 s->PutCString("Program Headers\n");
3165 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
3166 "p_filesz p_memsz p_flags p_align\n");
3167 s->PutCString("==== --------------- -------- -------- -------- "
3168 "-------- -------- ------------------------- --------\n");
3169
3170 uint32_t idx = 0;
3171 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
3172 I != m_program_headers.end(); ++I, ++idx)
3173 {
3174 s->Printf("[%2u] ", idx);
3175 ObjectFileELF::DumpELFProgramHeader(s, *I);
3176 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003177 }
3178}
3179
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003180//----------------------------------------------------------------------
3181// DumpELFSectionHeader
3182//
3183// Dump a single ELF section header to the specified output stream
3184//----------------------------------------------------------------------
3185void
Michael Sartaina7499c92013-07-01 19:45:50 +00003186ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003187{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003188 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003189 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Daniel Malead01b2952012-11-29 21:49:15 +00003190 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003191 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00003192 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, sh.sh_offset, sh.sh_size);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003193 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
Daniel Malead01b2952012-11-29 21:49:15 +00003194 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003195}
3196
3197//----------------------------------------------------------------------
3198// DumpELFSectionHeader_sh_type
3199//
3200// Dump an token value for the ELF section header member sh_type which
3201// describes the type of the section
3202//----------------------------------------------------------------------
3203void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003204ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003205{
3206 const int kStrWidth = 12;
3207 switch (sh_type)
3208 {
3209 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
3210 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
3211 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
3212 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
3213 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
3214 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
3215 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
3216 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
3217 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
3218 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
3219 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
3220 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
3221 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
3222 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
3223 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
3224 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
3225 default:
3226 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
3227 break;
3228 }
3229}
3230
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003231//----------------------------------------------------------------------
3232// DumpELFSectionHeader_sh_flags
3233//
3234// Dump an token value for the ELF section header member sh_flags
3235//----------------------------------------------------------------------
3236void
Greg Claytonc7bece562013-01-25 18:06:21 +00003237ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003238{
3239 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
3240 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
3241 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
3242 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
3243 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
3244}
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003245
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003246//----------------------------------------------------------------------
3247// DumpELFSectionHeaders
3248//
3249// Dump all of the ELF section header to the specified output stream
3250//----------------------------------------------------------------------
3251void
3252ObjectFileELF::DumpELFSectionHeaders(Stream *s)
3253{
Michael Sartaina7499c92013-07-01 19:45:50 +00003254 if (!ParseSectionHeaders())
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003255 return;
3256
3257 s->PutCString("Section Headers\n");
3258 s->PutCString("IDX name type flags "
3259 "addr offset size link info addralgn "
3260 "entsize Name\n");
3261 s->PutCString("==== -------- ------------ -------------------------------- "
3262 "-------- -------- -------- -------- -------- -------- "
3263 "-------- ====================\n");
3264
3265 uint32_t idx = 0;
3266 for (SectionHeaderCollConstIter I = m_section_headers.begin();
3267 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003268 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003269 s->Printf("[%2u] ", idx);
3270 ObjectFileELF::DumpELFSectionHeader(s, *I);
Michael Sartaina7499c92013-07-01 19:45:50 +00003271 const char* section_name = I->section_name.AsCString("");
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003272 if (section_name)
3273 *s << ' ' << section_name << "\n";
3274 }
3275}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003276
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003277void
3278ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
3279{
3280 size_t num_modules = ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003281
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003282 if (num_modules > 0)
3283 {
3284 s->PutCString("Dependent Modules:\n");
3285 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003286 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003287 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
3288 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003289 }
3290 }
3291}
3292
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003293bool
Greg Clayton514487e2011-02-15 21:59:32 +00003294ObjectFileELF::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003295{
Stephen Wilson3f4200fd2011-02-24 19:16:15 +00003296 if (!ParseHeader())
3297 return false;
3298
Todd Fiala09512ec2014-07-11 15:43:51 +00003299 if (m_section_headers.empty())
3300 {
3301 // Allow elf notes to be parsed which may affect the detected architecture.
3302 ParseSectionHeaders();
3303 }
Todd Fialab91de782014-06-27 16:52:49 +00003304
Greg Claytonb704b692015-10-28 18:04:38 +00003305 if (CalculateType() == eTypeCoreFile && m_arch_spec.TripleOSIsUnspecifiedUnknown())
3306 {
3307 // Core files don't have section headers yet they have PT_NOTE program headers
3308 // that might shed more light on the architecture
3309 if (ParseProgramHeaders())
3310 {
3311 for (size_t i = 0, count = GetProgramHeaderCount(); i < count; ++i)
3312 {
3313 const elf::ELFProgramHeader* header = GetProgramHeaderByIndex(i);
3314 if (header && header->p_type == PT_NOTE && header->p_offset != 0 && header->p_filesz > 0)
3315 {
3316 DataExtractor data;
3317 if (data.SetData (m_data, header->p_offset, header->p_filesz) == header->p_filesz)
3318 {
3319 lldb_private::UUID uuid;
3320 RefineModuleDetailsFromNote (data, m_arch_spec, uuid);
3321 }
3322 }
3323 }
3324 }
3325 }
Todd Fialab91de782014-06-27 16:52:49 +00003326 arch = m_arch_spec;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003327 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003328}
3329
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003330ObjectFile::Type
3331ObjectFileELF::CalculateType()
3332{
3333 switch (m_header.e_type)
3334 {
3335 case llvm::ELF::ET_NONE:
3336 // 0 - No file type
3337 return eTypeUnknown;
3338
3339 case llvm::ELF::ET_REL:
3340 // 1 - Relocatable file
3341 return eTypeObjectFile;
3342
3343 case llvm::ELF::ET_EXEC:
3344 // 2 - Executable file
3345 return eTypeExecutable;
3346
3347 case llvm::ELF::ET_DYN:
3348 // 3 - Shared object file
3349 return eTypeSharedLibrary;
3350
3351 case ET_CORE:
3352 // 4 - Core file
3353 return eTypeCoreFile;
3354
3355 default:
3356 break;
3357 }
3358 return eTypeUnknown;
3359}
3360
3361ObjectFile::Strata
3362ObjectFileELF::CalculateStrata()
3363{
3364 switch (m_header.e_type)
3365 {
Ed Maste81b4c5f2016-01-04 01:43:47 +00003366 case llvm::ELF::ET_NONE:
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003367 // 0 - No file type
3368 return eStrataUnknown;
3369
3370 case llvm::ELF::ET_REL:
3371 // 1 - Relocatable file
3372 return eStrataUnknown;
3373
3374 case llvm::ELF::ET_EXEC:
3375 // 2 - Executable file
3376 // TODO: is there any way to detect that an executable is a kernel
Ed Maste81b4c5f2016-01-04 01:43:47 +00003377 // related executable by inspecting the program headers, section
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003378 // headers, symbols, or any other flag bits???
3379 return eStrataUser;
3380
3381 case llvm::ELF::ET_DYN:
3382 // 3 - Shared object file
3383 // TODO: is there any way to detect that an shared library is a kernel
Ed Maste81b4c5f2016-01-04 01:43:47 +00003384 // related executable by inspecting the program headers, section
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003385 // headers, symbols, or any other flag bits???
3386 return eStrataUnknown;
3387
3388 case ET_CORE:
3389 // 4 - Core file
3390 // TODO: is there any way to detect that an core file is a kernel
Ed Maste81b4c5f2016-01-04 01:43:47 +00003391 // related executable by inspecting the program headers, section
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003392 // headers, symbols, or any other flag bits???
3393 return eStrataUnknown;
3394
3395 default:
3396 break;
3397 }
3398 return eStrataUnknown;
3399}
3400