blob: 76416843fd55c8841ccb9e12a9738ee2856997af [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"
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +000034#include "llvm/Support/ARMBuildAttributes.h"
Zachary Turner736d4d82014-06-25 05:42:32 +000035#include "llvm/Support/MathExtras.h"
Stephen Wilson499b40e2011-03-30 16:07:05 +000036
Stephen Wilsonf325ba92010-07-13 23:07:23 +000037#define CASE_AND_STREAM(s, def, width) \
38 case def: s->Printf("%-*s", width, #def); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040using namespace lldb;
41using namespace lldb_private;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000042using namespace elf;
43using namespace llvm::ELF;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044
Stephen Wilson499b40e2011-03-30 16:07:05 +000045namespace {
Todd Fialab91de782014-06-27 16:52:49 +000046
47// ELF note owner definitions
48const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD";
49const char *const LLDB_NT_OWNER_GNU = "GNU";
50const char *const LLDB_NT_OWNER_NETBSD = "NetBSD";
Todd Fialacfee9632014-07-16 15:03:10 +000051const char *const LLDB_NT_OWNER_CSR = "csr";
Tamas Berghammerdb037d92015-03-18 10:36:27 +000052const char *const LLDB_NT_OWNER_ANDROID = "Android";
Greg Claytonb704b692015-10-28 18:04:38 +000053const char *const LLDB_NT_OWNER_CORE = "CORE";
54const char *const LLDB_NT_OWNER_LINUX = "LINUX";
Todd Fialab91de782014-06-27 16:52:49 +000055
56// ELF note type definitions
57const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01;
58const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4;
59
60const elf_word LLDB_NT_GNU_ABI_TAG = 0x01;
61const elf_word LLDB_NT_GNU_ABI_SIZE = 16;
62
63const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03;
64
65const elf_word LLDB_NT_NETBSD_ABI_TAG = 0x01;
66const elf_word LLDB_NT_NETBSD_ABI_SIZE = 4;
67
68// GNU ABI note OS constants
69const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00;
70const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01;
71const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02;
72
Greg Claytonb704b692015-10-28 18:04:38 +000073// LLDB_NT_OWNER_CORE and LLDB_NT_OWNER_LINUX note contants
74#define NT_PRSTATUS 1
75#define NT_PRFPREG 2
76#define NT_PRPSINFO 3
77#define NT_TASKSTRUCT 4
78#define NT_AUXV 6
79#define NT_SIGINFO 0x53494749
80#define NT_FILE 0x46494c45
81#define NT_PRXFPREG 0x46e62b7f
82#define NT_PPC_VMX 0x100
83#define NT_PPC_SPE 0x101
84#define NT_PPC_VSX 0x102
85#define NT_386_TLS 0x200
86#define NT_386_IOPERM 0x201
87#define NT_X86_XSTATE 0x202
88#define NT_S390_HIGH_GPRS 0x300
89#define NT_S390_TIMER 0x301
90#define NT_S390_TODCMP 0x302
91#define NT_S390_TODPREG 0x303
92#define NT_S390_CTRS 0x304
93#define NT_S390_PREFIX 0x305
94#define NT_S390_LAST_BREAK 0x306
95#define NT_S390_SYSTEM_CALL 0x307
96#define NT_S390_TDB 0x308
97#define NT_S390_VXRS_LOW 0x309
98#define NT_S390_VXRS_HIGH 0x30a
99#define NT_ARM_VFP 0x400
100#define NT_ARM_TLS 0x401
101#define NT_ARM_HW_BREAK 0x402
102#define NT_ARM_HW_WATCH 0x403
103#define NT_ARM_SYSTEM_CALL 0x404
104#define NT_METAG_CBUF 0x500
105#define NT_METAG_RPIPE 0x501
106#define NT_METAG_TLS 0x502
107
Stephen Wilson499b40e2011-03-30 16:07:05 +0000108//===----------------------------------------------------------------------===//
109/// @class ELFRelocation
110/// @brief Generic wrapper for ELFRel and ELFRela.
111///
112/// This helper class allows us to parse both ELFRel and ELFRela relocation
113/// entries in a generic manner.
114class ELFRelocation
115{
116public:
117
118 /// Constructs an ELFRelocation entry with a personality as given by @p
119 /// type.
120 ///
121 /// @param type Either DT_REL or DT_RELA. Any other value is invalid.
122 ELFRelocation(unsigned type);
Ed Maste81b4c5f2016-01-04 01:43:47 +0000123
Stephen Wilson499b40e2011-03-30 16:07:05 +0000124 ~ELFRelocation();
125
126 bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000127 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000128
129 static unsigned
130 RelocType32(const ELFRelocation &rel);
131
132 static unsigned
133 RelocType64(const ELFRelocation &rel);
134
135 static unsigned
136 RelocSymbol32(const ELFRelocation &rel);
137
138 static unsigned
139 RelocSymbol64(const ELFRelocation &rel);
140
Andrew MacPherson17220c12014-03-05 10:12:43 +0000141 static unsigned
142 RelocOffset32(const ELFRelocation &rel);
143
144 static unsigned
145 RelocOffset64(const ELFRelocation &rel);
146
147 static unsigned
148 RelocAddend32(const ELFRelocation &rel);
149
150 static unsigned
151 RelocAddend64(const ELFRelocation &rel);
152
Stephen Wilson499b40e2011-03-30 16:07:05 +0000153private:
154 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion;
155
156 RelocUnion reloc;
157};
158
159ELFRelocation::ELFRelocation(unsigned type)
Ed Maste81b4c5f2016-01-04 01:43:47 +0000160{
Andrew MacPherson17220c12014-03-05 10:12:43 +0000161 if (type == DT_REL || type == SHT_REL)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000162 reloc = new ELFRel();
Andrew MacPherson17220c12014-03-05 10:12:43 +0000163 else if (type == DT_RELA || type == SHT_RELA)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000164 reloc = new ELFRela();
165 else {
166 assert(false && "unexpected relocation type");
167 reloc = static_cast<ELFRel*>(NULL);
168 }
169}
170
171ELFRelocation::~ELFRelocation()
172{
173 if (reloc.is<ELFRel*>())
174 delete reloc.get<ELFRel*>();
175 else
Ed Maste81b4c5f2016-01-04 01:43:47 +0000176 delete reloc.get<ELFRela*>();
Stephen Wilson499b40e2011-03-30 16:07:05 +0000177}
178
179bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000180ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000181{
182 if (reloc.is<ELFRel*>())
183 return reloc.get<ELFRel*>()->Parse(data, offset);
184 else
185 return reloc.get<ELFRela*>()->Parse(data, offset);
186}
187
188unsigned
189ELFRelocation::RelocType32(const ELFRelocation &rel)
190{
191 if (rel.reloc.is<ELFRel*>())
192 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>());
193 else
194 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>());
195}
196
197unsigned
198ELFRelocation::RelocType64(const ELFRelocation &rel)
199{
200 if (rel.reloc.is<ELFRel*>())
201 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>());
202 else
203 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>());
204}
205
206unsigned
207ELFRelocation::RelocSymbol32(const ELFRelocation &rel)
208{
209 if (rel.reloc.is<ELFRel*>())
210 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>());
211 else
212 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>());
213}
214
215unsigned
216ELFRelocation::RelocSymbol64(const ELFRelocation &rel)
217{
218 if (rel.reloc.is<ELFRel*>())
219 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>());
220 else
221 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>());
222}
223
Andrew MacPherson17220c12014-03-05 10:12:43 +0000224unsigned
225ELFRelocation::RelocOffset32(const ELFRelocation &rel)
226{
227 if (rel.reloc.is<ELFRel*>())
228 return rel.reloc.get<ELFRel*>()->r_offset;
229 else
230 return rel.reloc.get<ELFRela*>()->r_offset;
231}
232
233unsigned
234ELFRelocation::RelocOffset64(const ELFRelocation &rel)
235{
236 if (rel.reloc.is<ELFRel*>())
237 return rel.reloc.get<ELFRel*>()->r_offset;
238 else
239 return rel.reloc.get<ELFRela*>()->r_offset;
240}
241
242unsigned
243ELFRelocation::RelocAddend32(const ELFRelocation &rel)
244{
245 if (rel.reloc.is<ELFRel*>())
246 return 0;
247 else
248 return rel.reloc.get<ELFRela*>()->r_addend;
249}
250
251unsigned
252ELFRelocation::RelocAddend64(const ELFRelocation &rel)
253{
254 if (rel.reloc.is<ELFRel*>())
255 return 0;
256 else
257 return rel.reloc.get<ELFRela*>()->r_addend;
258}
259
Stephen Wilson499b40e2011-03-30 16:07:05 +0000260} // end anonymous namespace
261
Ed Mastec113ff82013-12-02 17:49:13 +0000262bool
263ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset)
264{
265 // Read all fields.
266 if (data.GetU32(offset, &n_namesz, 3) == NULL)
267 return false;
268
269 // The name field is required to be nul-terminated, and n_namesz
270 // includes the terminating nul in observed implementations (contrary
271 // to the ELF-64 spec). A special case is needed for cores generated
272 // by some older Linux versions, which write a note named "CORE"
273 // without a nul terminator and n_namesz = 4.
274 if (n_namesz == 4)
275 {
276 char buf[4];
277 if (data.ExtractBytes (*offset, 4, data.GetByteOrder(), buf) != 4)
278 return false;
279 if (strncmp (buf, "CORE", 4) == 0)
280 {
281 n_name = "CORE";
282 *offset += 4;
283 return true;
284 }
285 }
286
Rafael Espindolaa94ae1e2016-01-18 20:57:54 +0000287 const char *cstr = data.GetCStr(offset, llvm::alignTo (n_namesz, 4));
Ed Mastec113ff82013-12-02 17:49:13 +0000288 if (cstr == NULL)
289 {
290 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
291 if (log)
292 log->Printf("Failed to parse note name lacking nul terminator");
293
294 return false;
295 }
296 n_name = cstr;
297 return true;
298}
299
Matthew Gardiner5f675792014-08-27 12:09:39 +0000300static uint32_t
301kalimbaVariantFromElfFlags(const elf::elf_word e_flags)
302{
303 const uint32_t dsp_rev = e_flags & 0xFF;
304 uint32_t kal_arch_variant = LLDB_INVALID_CPUTYPE;
305 switch(dsp_rev)
306 {
307 // TODO(mg11) Support more variants
308 case 10:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000309 kal_arch_variant = llvm::Triple::KalimbaSubArch_v3;
Matthew Gardiner5f675792014-08-27 12:09:39 +0000310 break;
311 case 14:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000312 kal_arch_variant = llvm::Triple::KalimbaSubArch_v4;
313 break;
314 case 17:
315 case 20:
316 kal_arch_variant = llvm::Triple::KalimbaSubArch_v5;
Matthew Gardiner5f675792014-08-27 12:09:39 +0000317 break;
318 default:
Ed Maste81b4c5f2016-01-04 01:43:47 +0000319 break;
Matthew Gardiner5f675792014-08-27 12:09:39 +0000320 }
321 return kal_arch_variant;
322}
323
324static uint32_t
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000325mipsVariantFromElfFlags(const elf::elf_word e_flags, uint32_t endian)
326{
327 const uint32_t mips_arch = e_flags & llvm::ELF::EF_MIPS_ARCH;
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +0000328 uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown;
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000329
330 switch (mips_arch)
331 {
Sagar Thakur40fc2e32015-12-15 05:50:55 +0000332 case llvm::ELF::EF_MIPS_ARCH_1:
333 case llvm::ELF::EF_MIPS_ARCH_2:
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +0000334 case llvm::ELF::EF_MIPS_ARCH_32:
335 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el : ArchSpec::eMIPSSubType_mips32;
336 case llvm::ELF::EF_MIPS_ARCH_32R2:
337 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el : ArchSpec::eMIPSSubType_mips32r2;
338 case llvm::ELF::EF_MIPS_ARCH_32R6:
339 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el : ArchSpec::eMIPSSubType_mips32r6;
Mohit K. Bhakkad884fc3e2016-01-12 06:03:01 +0000340 case llvm::ELF::EF_MIPS_ARCH_3:
341 case llvm::ELF::EF_MIPS_ARCH_4:
342 case llvm::ELF::EF_MIPS_ARCH_5:
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000343 case llvm::ELF::EF_MIPS_ARCH_64:
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +0000344 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el : ArchSpec::eMIPSSubType_mips64;
345 case llvm::ELF::EF_MIPS_ARCH_64R2:
346 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el : ArchSpec::eMIPSSubType_mips64r2;
347 case llvm::ELF::EF_MIPS_ARCH_64R6:
348 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el : ArchSpec::eMIPSSubType_mips64r6;
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000349 default:
350 break;
351 }
352
353 return arch_variant;
354}
355
356static uint32_t
Matthew Gardiner5f675792014-08-27 12:09:39 +0000357subTypeFromElfHeader(const elf::ELFHeader& header)
358{
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000359 if (header.e_machine == llvm::ELF::EM_MIPS)
360 return mipsVariantFromElfFlags (header.e_flags,
361 header.e_ident[EI_DATA]);
362
Matthew Gardiner5f675792014-08-27 12:09:39 +0000363 return
364 llvm::ELF::EM_CSR_KALIMBA == header.e_machine ?
365 kalimbaVariantFromElfFlags(header.e_flags) :
366 LLDB_INVALID_CPUTYPE;
367}
368
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000369//! The kalimba toolchain identifies a code section as being
370//! one with the SHT_PROGBITS set in the section sh_type and the top
371//! bit in the 32-bit address field set.
372static lldb::SectionType
373kalimbaSectionType(
374 const elf::ELFHeader& header,
375 const elf::ELFSectionHeader& sect_hdr)
376{
Matthew Gardiner6e7b0a02014-10-15 08:21:54 +0000377 if (llvm::ELF::EM_CSR_KALIMBA != header.e_machine)
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000378 {
379 return eSectionTypeOther;
380 }
381
Matthew Gardiner6e7b0a02014-10-15 08:21:54 +0000382 if (llvm::ELF::SHT_NOBITS == sect_hdr.sh_type)
383 {
384 return eSectionTypeZeroFill;
385 }
386
387 if (llvm::ELF::SHT_PROGBITS == sect_hdr.sh_type)
388 {
389 const lldb::addr_t KAL_CODE_BIT = 1 << 31;
390 return KAL_CODE_BIT & sect_hdr.sh_addr ?
391 eSectionTypeCode : eSectionTypeData;
392 }
393
394 return eSectionTypeOther;
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000395}
396
Todd Fiala4339f3a2014-03-25 19:29:09 +0000397// Arbitrary constant used as UUID prefix for core files.
398const uint32_t
399ObjectFileELF::g_core_uuid_magic(0xE210C);
400
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000401//------------------------------------------------------------------
402// Static methods.
403//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404void
405ObjectFileELF::Initialize()
406{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000407 PluginManager::RegisterPlugin(GetPluginNameStatic(),
408 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000409 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000410 CreateMemoryInstance,
411 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
413
414void
415ObjectFileELF::Terminate()
416{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000417 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418}
419
Greg Clayton57abc5d2013-05-10 21:47:16 +0000420lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421ObjectFileELF::GetPluginNameStatic()
422{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000423 static ConstString g_name("elf");
424 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425}
426
427const char *
428ObjectFileELF::GetPluginDescriptionStatic()
429{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000430 return "ELF object file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431}
432
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +0000434ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp,
435 DataBufferSP &data_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000436 lldb::offset_t data_offset,
437 const lldb_private::FileSpec* file,
438 lldb::offset_t file_offset,
439 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000441 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442 {
Greg Clayton736888c2015-02-23 23:47:09 +0000443 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
Greg Clayton5ce9c562013-02-06 17:22:03 +0000444 data_offset = 0;
445 }
446
447 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
448 {
449 const uint8_t *magic = data_sp->GetBytes() + data_offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000450 if (ELFHeader::MagicBytesMatch(magic))
451 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000452 // Update the data to contain the entire file if it doesn't already
Andrew Kaylor213f6722013-02-07 21:30:54 +0000453 if (data_sp->GetByteSize() < length) {
Greg Clayton736888c2015-02-23 23:47:09 +0000454 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
Greg Clayton64ff6c72013-02-07 21:49:54 +0000455 data_offset = 0;
456 magic = data_sp->GetBytes();
Andrew Kaylor213f6722013-02-07 21:30:54 +0000457 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000458 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
459 if (address_size == 4 || address_size == 8)
460 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000461 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 +0000462 ArchSpec spec;
463 if (objfile_ap->GetArchitecture(spec) &&
464 objfile_ap->SetModulesArchitecture(spec))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000465 return objfile_ap.release();
466 }
467 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468 }
469 return NULL;
470}
471
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000472
Greg Claytonc9660542012-02-05 02:38:54 +0000473ObjectFile*
Ed Maste81b4c5f2016-01-04 01:43:47 +0000474ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
475 DataBufferSP& data_sp,
476 const lldb::ProcessSP &process_sp,
Greg Claytone72dfb32012-02-24 01:59:29 +0000477 lldb::addr_t header_addr)
Greg Claytonc9660542012-02-05 02:38:54 +0000478{
Andrew MacPherson17220c12014-03-05 10:12:43 +0000479 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT))
480 {
481 const uint8_t *magic = data_sp->GetBytes();
482 if (ELFHeader::MagicBytesMatch(magic))
483 {
484 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
485 if (address_size == 4 || address_size == 8)
486 {
487 std::auto_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, process_sp, header_addr));
488 ArchSpec spec;
489 if (objfile_ap->GetArchitecture(spec) &&
490 objfile_ap->SetModulesArchitecture(spec))
491 return objfile_ap.release();
492 }
493 }
494 }
Greg Claytonc9660542012-02-05 02:38:54 +0000495 return NULL;
496}
497
Michael Sartain9f0013d2013-05-17 00:20:21 +0000498bool
499ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp,
500 lldb::addr_t data_offset,
501 lldb::addr_t data_length)
502{
503 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
504 {
505 const uint8_t *magic = data_sp->GetBytes() + data_offset;
506 return ELFHeader::MagicBytesMatch(magic);
507 }
508 return false;
509}
Greg Claytonc9660542012-02-05 02:38:54 +0000510
Michael Sartain9f4517a2013-07-03 01:52:14 +0000511/*
512 * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c
513 *
514 * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
515 * code or tables extracted from it, as desired without restriction.
516 */
517static uint32_t
Todd Fiala4339f3a2014-03-25 19:29:09 +0000518calc_crc32(uint32_t crc, const void *buf, size_t size)
Michael Sartain9f4517a2013-07-03 01:52:14 +0000519{
520 static const uint32_t g_crc32_tab[] =
521 {
522 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
523 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
524 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
525 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
526 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
527 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
528 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
529 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
530 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
531 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
532 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
533 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
534 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
535 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
536 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
537 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
538 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
539 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
540 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
541 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
542 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
543 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
544 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
545 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
546 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
547 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
548 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
549 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
550 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
551 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
552 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
553 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
554 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
555 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
556 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
557 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
558 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
559 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
560 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
561 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
562 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
563 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
564 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
Ed Maste81b4c5f2016-01-04 01:43:47 +0000565 };
Michael Sartain9f4517a2013-07-03 01:52:14 +0000566 const uint8_t *p = (const uint8_t *)buf;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000567
Todd Fiala4339f3a2014-03-25 19:29:09 +0000568 crc = crc ^ ~0U;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000569 while (size--)
570 crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
571 return crc ^ ~0U;
572}
573
Todd Fiala4339f3a2014-03-25 19:29:09 +0000574static uint32_t
575calc_gnu_debuglink_crc32(const void *buf, size_t size)
576{
577 return calc_crc32(0U, buf, size);
578}
579
580uint32_t
581ObjectFileELF::CalculateELFNotesSegmentsCRC32 (const ProgramHeaderColl& program_headers,
582 DataExtractor& object_data)
583{
584 typedef ProgramHeaderCollConstIter Iter;
585
586 uint32_t core_notes_crc = 0;
587
588 for (Iter I = program_headers.begin(); I != program_headers.end(); ++I)
589 {
590 if (I->p_type == llvm::ELF::PT_NOTE)
591 {
592 const elf_off ph_offset = I->p_offset;
593 const size_t ph_size = I->p_filesz;
594
595 DataExtractor segment_data;
596 if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size)
597 {
598 // The ELF program header contained incorrect data,
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000599 // probably corefile is incomplete or corrupted.
Todd Fiala4339f3a2014-03-25 19:29:09 +0000600 break;
601 }
602
603 core_notes_crc = calc_crc32(core_notes_crc,
604 segment_data.GetDataStart(),
605 segment_data.GetByteSize());
606 }
607 }
608
609 return core_notes_crc;
610}
611
Todd Fialab91de782014-06-27 16:52:49 +0000612static const char*
613OSABIAsCString (unsigned char osabi_byte)
614{
615#define _MAKE_OSABI_CASE(x) case x: return #x
616 switch (osabi_byte)
617 {
618 _MAKE_OSABI_CASE(ELFOSABI_NONE);
619 _MAKE_OSABI_CASE(ELFOSABI_HPUX);
620 _MAKE_OSABI_CASE(ELFOSABI_NETBSD);
621 _MAKE_OSABI_CASE(ELFOSABI_GNU);
622 _MAKE_OSABI_CASE(ELFOSABI_HURD);
623 _MAKE_OSABI_CASE(ELFOSABI_SOLARIS);
624 _MAKE_OSABI_CASE(ELFOSABI_AIX);
625 _MAKE_OSABI_CASE(ELFOSABI_IRIX);
626 _MAKE_OSABI_CASE(ELFOSABI_FREEBSD);
627 _MAKE_OSABI_CASE(ELFOSABI_TRU64);
628 _MAKE_OSABI_CASE(ELFOSABI_MODESTO);
629 _MAKE_OSABI_CASE(ELFOSABI_OPENBSD);
630 _MAKE_OSABI_CASE(ELFOSABI_OPENVMS);
631 _MAKE_OSABI_CASE(ELFOSABI_NSK);
632 _MAKE_OSABI_CASE(ELFOSABI_AROS);
633 _MAKE_OSABI_CASE(ELFOSABI_FENIXOS);
634 _MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI);
635 _MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX);
636 _MAKE_OSABI_CASE(ELFOSABI_ARM);
637 _MAKE_OSABI_CASE(ELFOSABI_STANDALONE);
638 default:
639 return "<unknown-osabi>";
640 }
641#undef _MAKE_OSABI_CASE
642}
643
Ed Mastef6a13122015-06-05 13:03:08 +0000644//
645// WARNING : This function is being deprecated
646// It's functionality has moved to ArchSpec::SetArchitecture
647// This function is only being kept to validate the move.
648//
649// TODO : Remove this function
Todd Fialab91de782014-06-27 16:52:49 +0000650static bool
651GetOsFromOSABI (unsigned char osabi_byte, llvm::Triple::OSType &ostype)
652{
653 switch (osabi_byte)
654 {
655 case ELFOSABI_AIX: ostype = llvm::Triple::OSType::AIX; break;
656 case ELFOSABI_FREEBSD: ostype = llvm::Triple::OSType::FreeBSD; break;
657 case ELFOSABI_GNU: ostype = llvm::Triple::OSType::Linux; break;
658 case ELFOSABI_NETBSD: ostype = llvm::Triple::OSType::NetBSD; break;
659 case ELFOSABI_OPENBSD: ostype = llvm::Triple::OSType::OpenBSD; break;
660 case ELFOSABI_SOLARIS: ostype = llvm::Triple::OSType::Solaris; break;
661 default:
662 ostype = llvm::Triple::OSType::UnknownOS;
663 }
664 return ostype != llvm::Triple::OSType::UnknownOS;
665}
666
Greg Claytonf4d6de62013-04-24 22:29:28 +0000667size_t
668ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file,
669 lldb::DataBufferSP& data_sp,
670 lldb::offset_t data_offset,
671 lldb::offset_t file_offset,
672 lldb::offset_t length,
673 lldb_private::ModuleSpecList &specs)
674{
Todd Fialab91de782014-06-27 16:52:49 +0000675 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MODULES));
676
Michael Sartain9f0013d2013-05-17 00:20:21 +0000677 const size_t initial_count = specs.GetSize();
Michael Sartainc836ae72013-05-23 20:57:03 +0000678
Michael Sartain9f0013d2013-05-17 00:20:21 +0000679 if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
680 {
681 DataExtractor data;
682 data.SetData(data_sp);
683 elf::ELFHeader header;
684 if (header.Parse(data, &data_offset))
685 {
686 if (data_sp)
687 {
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000688 ModuleSpec spec (file);
Matthew Gardiner5f675792014-08-27 12:09:39 +0000689
690 const uint32_t sub_type = subTypeFromElfHeader(header);
Michael Sartain9f0013d2013-05-17 00:20:21 +0000691 spec.GetArchitecture().SetArchitecture(eArchTypeELF,
692 header.e_machine,
Ed Mastef6a13122015-06-05 13:03:08 +0000693 sub_type,
694 header.e_ident[EI_OSABI]);
Matthew Gardiner5f675792014-08-27 12:09:39 +0000695
Michael Sartain9f0013d2013-05-17 00:20:21 +0000696 if (spec.GetArchitecture().IsValid())
697 {
Todd Fialab91de782014-06-27 16:52:49 +0000698 llvm::Triple::OSType ostype;
Ed Mastef6a13122015-06-05 13:03:08 +0000699 llvm::Triple::VendorType vendor;
700 llvm::Triple::OSType spec_ostype = spec.GetArchitecture ().GetTriple ().getOS ();
Todd Fialab91de782014-06-27 16:52:49 +0000701
702 if (log)
703 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 +0000704
705 // SetArchitecture should have set the vendor to unknown
706 vendor = spec.GetArchitecture ().GetTriple ().getVendor ();
707 assert(vendor == llvm::Triple::UnknownVendor);
708
709 //
710 // Validate it is ok to remove GetOsFromOSABI
711 GetOsFromOSABI (header.e_ident[EI_OSABI], ostype);
712 assert(spec_ostype == ostype);
713 if (spec_ostype != llvm::Triple::OSType::UnknownOS)
Todd Fialab91de782014-06-27 16:52:49 +0000714 {
Todd Fialab91de782014-06-27 16:52:49 +0000715 if (log)
716 log->Printf ("ObjectFileELF::%s file '%s' set ELF module OS type from ELF header OSABI.", __FUNCTION__, file.GetPath ().c_str ());
717 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000718
719 // Try to get the UUID from the section list. Usually that's at the end, so
720 // map the file in if we don't have it already.
721 size_t section_header_end = header.e_shoff + header.e_shnum * header.e_shentsize;
722 if (section_header_end > data_sp->GetByteSize())
723 {
Greg Clayton736888c2015-02-23 23:47:09 +0000724 data_sp = file.MemoryMapFileContentsIfLocal (file_offset, section_header_end);
Michael Sartaina7499c92013-07-01 19:45:50 +0000725 data.SetData(data_sp);
726 }
727
Michael Sartain9f4517a2013-07-03 01:52:14 +0000728 uint32_t gnu_debuglink_crc = 0;
Michael Sartaina7499c92013-07-01 19:45:50 +0000729 std::string gnu_debuglink_file;
730 SectionHeaderColl section_headers;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000731 lldb_private::UUID &uuid = spec.GetUUID();
Michael Sartain9f4517a2013-07-03 01:52:14 +0000732
Ravitheja Addepally15f89c42016-01-19 12:55:21 +0000733 using namespace std::placeholders;
734 const SetDataFunction set_data = std::bind(&ObjectFileELF::SetData, std::cref(data), _1, _2, _3);
735 GetSectionHeaderInfo(section_headers, set_data, header, uuid, gnu_debuglink_file, gnu_debuglink_crc, spec.GetArchitecture ());
736
Todd Fialab91de782014-06-27 16:52:49 +0000737
Todd Fialab91de782014-06-27 16:52:49 +0000738 llvm::Triple &spec_triple = spec.GetArchitecture ().GetTriple ();
Todd Fialab91de782014-06-27 16:52:49 +0000739
740 if (log)
741 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 +0000742
Michael Sartain9f4517a2013-07-03 01:52:14 +0000743 if (!uuid.IsValid())
744 {
Todd Fiala4339f3a2014-03-25 19:29:09 +0000745 uint32_t core_notes_crc = 0;
746
Michael Sartain9f4517a2013-07-03 01:52:14 +0000747 if (!gnu_debuglink_crc)
748 {
Todd Fiala4339f3a2014-03-25 19:29:09 +0000749 lldb_private::Timer scoped_timer (__PRETTY_FUNCTION__,
750 "Calculating module crc32 %s with size %" PRIu64 " KiB",
751 file.GetLastPathComponent().AsCString(),
752 (file.GetByteSize()-file_offset)/1024);
753
754 // For core files - which usually don't happen to have a gnu_debuglink,
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000755 // and are pretty bulky - calculating whole contents crc32 would be too much of luxury.
Todd Fiala4339f3a2014-03-25 19:29:09 +0000756 // Thus we will need to fallback to something simpler.
757 if (header.e_type == llvm::ELF::ET_CORE)
758 {
759 size_t program_headers_end = header.e_phoff + header.e_phnum * header.e_phentsize;
760 if (program_headers_end > data_sp->GetByteSize())
761 {
Greg Clayton736888c2015-02-23 23:47:09 +0000762 data_sp = file.MemoryMapFileContentsIfLocal(file_offset, program_headers_end);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000763 data.SetData(data_sp);
764 }
765 ProgramHeaderColl program_headers;
Ravitheja Addepally15f89c42016-01-19 12:55:21 +0000766 GetProgramHeaderInfo(program_headers, set_data, header);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000767
768 size_t segment_data_end = 0;
769 for (ProgramHeaderCollConstIter I = program_headers.begin();
770 I != program_headers.end(); ++I)
771 {
Enrico Granataafcbdb12014-03-25 20:53:33 +0000772 segment_data_end = std::max<unsigned long long> (I->p_offset + I->p_filesz, segment_data_end);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000773 }
774
775 if (segment_data_end > data_sp->GetByteSize())
776 {
Greg Clayton736888c2015-02-23 23:47:09 +0000777 data_sp = file.MemoryMapFileContentsIfLocal(file_offset, segment_data_end);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000778 data.SetData(data_sp);
779 }
780
781 core_notes_crc = CalculateELFNotesSegmentsCRC32 (program_headers, data);
782 }
783 else
784 {
785 // Need to map entire file into memory to calculate the crc.
Greg Clayton736888c2015-02-23 23:47:09 +0000786 data_sp = file.MemoryMapFileContentsIfLocal (file_offset, SIZE_MAX);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000787 data.SetData(data_sp);
788 gnu_debuglink_crc = calc_gnu_debuglink_crc32 (data.GetDataStart(), data.GetByteSize());
789 }
Michael Sartain9f4517a2013-07-03 01:52:14 +0000790 }
791 if (gnu_debuglink_crc)
792 {
793 // Use 4 bytes of crc from the .gnu_debuglink section.
794 uint32_t uuidt[4] = { gnu_debuglink_crc, 0, 0, 0 };
795 uuid.SetBytes (uuidt, sizeof(uuidt));
796 }
Todd Fiala4339f3a2014-03-25 19:29:09 +0000797 else if (core_notes_crc)
798 {
799 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it look different form
800 // .gnu_debuglink crc followed by 4 bytes of note segments crc.
801 uint32_t uuidt[4] = { g_core_uuid_magic, core_notes_crc, 0, 0 };
802 uuid.SetBytes (uuidt, sizeof(uuidt));
803 }
Michael Sartain9f4517a2013-07-03 01:52:14 +0000804 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000805
Michael Sartain9f0013d2013-05-17 00:20:21 +0000806 specs.Append(spec);
807 }
808 }
809 }
810 }
Michael Sartainc836ae72013-05-23 20:57:03 +0000811
Michael Sartain9f0013d2013-05-17 00:20:21 +0000812 return specs.GetSize() - initial_count;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000813}
814
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000815//------------------------------------------------------------------
816// PluginInterface protocol
817//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000818lldb_private::ConstString
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000819ObjectFileELF::GetPluginName()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000821 return GetPluginNameStatic();
822}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000824uint32_t
825ObjectFileELF::GetPluginVersion()
826{
827 return m_plugin_version;
828}
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000829//------------------------------------------------------------------
830// ObjectFile protocol
831//------------------------------------------------------------------
832
Ed Maste81b4c5f2016-01-04 01:43:47 +0000833ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000834 DataBufferSP& data_sp,
835 lldb::offset_t data_offset,
Ed Maste81b4c5f2016-01-04 01:43:47 +0000836 const FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000837 lldb::offset_t file_offset,
Ed Maste81b4c5f2016-01-04 01:43:47 +0000838 lldb::offset_t length) :
Greg Clayton5ce9c562013-02-06 17:22:03 +0000839 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytone72dfb32012-02-24 01:59:29 +0000840 m_header(),
Todd Fialab91de782014-06-27 16:52:49 +0000841 m_uuid(),
842 m_gnu_debuglink_file(),
843 m_gnu_debuglink_crc(0),
Greg Claytone72dfb32012-02-24 01:59:29 +0000844 m_program_headers(),
845 m_section_headers(),
Todd Fialab91de782014-06-27 16:52:49 +0000846 m_dynamic_symbols(),
847 m_filespec_ap(),
848 m_entry_point_address(),
849 m_arch_spec()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000850{
851 if (file)
852 m_file = *file;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000853 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854}
855
Andrew MacPherson17220c12014-03-05 10:12:43 +0000856ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
Tamas Berghammerf2561842015-06-30 10:41:23 +0000857 DataBufferSP& header_data_sp,
Andrew MacPherson17220c12014-03-05 10:12:43 +0000858 const lldb::ProcessSP &process_sp,
859 addr_t header_addr) :
Tamas Berghammerf2561842015-06-30 10:41:23 +0000860 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Andrew MacPherson17220c12014-03-05 10:12:43 +0000861 m_header(),
Todd Fialab91de782014-06-27 16:52:49 +0000862 m_uuid(),
863 m_gnu_debuglink_file(),
864 m_gnu_debuglink_crc(0),
Andrew MacPherson17220c12014-03-05 10:12:43 +0000865 m_program_headers(),
866 m_section_headers(),
Todd Fialab91de782014-06-27 16:52:49 +0000867 m_dynamic_symbols(),
868 m_filespec_ap(),
869 m_entry_point_address(),
870 m_arch_spec()
Andrew MacPherson17220c12014-03-05 10:12:43 +0000871{
872 ::memset(&m_header, 0, sizeof(m_header));
873}
874
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000875ObjectFileELF::~ObjectFileELF()
876{
877}
878
Jim Ingham5aee1622010-08-09 23:31:02 +0000879bool
880ObjectFileELF::IsExecutable() const
881{
Deepak Panickal71f5b502014-07-22 12:01:43 +0000882 return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0);
Jim Ingham5aee1622010-08-09 23:31:02 +0000883}
884
Steve Pucci9e02dac2014-02-06 19:02:19 +0000885bool
Greg Clayton751caf62014-02-07 22:54:47 +0000886ObjectFileELF::SetLoadAddress (Target &target,
887 lldb::addr_t value,
888 bool value_is_offset)
Steve Pucci9e02dac2014-02-06 19:02:19 +0000889{
Steve Pucci9e02dac2014-02-06 19:02:19 +0000890 ModuleSP module_sp = GetModule();
891 if (module_sp)
892 {
893 size_t num_loaded_sections = 0;
894 SectionList *section_list = GetSectionList ();
895 if (section_list)
896 {
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000897 if (!value_is_offset)
Steve Pucci9e02dac2014-02-06 19:02:19 +0000898 {
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000899 bool found_offset = false;
900 for (size_t i = 0, count = GetProgramHeaderCount(); i < count; ++i)
Steve Pucci9e02dac2014-02-06 19:02:19 +0000901 {
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000902 const elf::ELFProgramHeader* header = GetProgramHeaderByIndex(i);
903 if (header == nullptr)
904 continue;
Tamas Berghammerf2561842015-06-30 10:41:23 +0000905
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000906 if (header->p_type != PT_LOAD || header->p_offset != 0)
907 continue;
Ed Maste81b4c5f2016-01-04 01:43:47 +0000908
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000909 value = value - header->p_vaddr;
910 found_offset = true;
911 break;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000912 }
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000913 if (!found_offset)
914 return false;
Greg Clayton751caf62014-02-07 22:54:47 +0000915 }
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000916
917 const size_t num_sections = section_list->GetSize();
918 size_t sect_idx = 0;
919
920 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
Greg Clayton751caf62014-02-07 22:54:47 +0000921 {
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000922 // Iterate through the object file sections to find all
923 // of the sections that have SHF_ALLOC in their flag bits.
924 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000925 if (section_sp && section_sp->Test(SHF_ALLOC))
926 {
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +0000927 lldb::addr_t load_addr = section_sp->GetFileAddress();
928 // We don't want to update the load address of a section with type
929 // eSectionTypeAbsoluteAddress as they already have the absolute load address
930 // already specified
931 if (section_sp->GetType() != eSectionTypeAbsoluteAddress)
932 load_addr += value;
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000933
934 // On 32-bit systems the load address have to fit into 4 bytes. The rest of
935 // the bytes are the overflow from the addition.
936 if (GetAddressByteSize() == 4)
937 load_addr &= 0xFFFFFFFF;
938
939 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
940 ++num_loaded_sections;
941 }
Steve Pucci9e02dac2014-02-06 19:02:19 +0000942 }
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000943 return num_loaded_sections > 0;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000944 }
Steve Pucci9e02dac2014-02-06 19:02:19 +0000945 }
Tamas Berghammer42ecef32015-08-24 10:21:55 +0000946 return false;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000947}
948
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949ByteOrder
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000950ObjectFileELF::GetByteOrder() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951{
952 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
953 return eByteOrderBig;
954 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
955 return eByteOrderLittle;
956 return eByteOrderInvalid;
957}
958
Greg Claytonc7bece562013-01-25 18:06:21 +0000959uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000960ObjectFileELF::GetAddressByteSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961{
962 return m_data.GetAddressByteSize();
963}
964
Todd Fialafbd703a2014-09-15 22:33:39 +0000965AddressClass
966ObjectFileELF::GetAddressClass (addr_t file_addr)
967{
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000968 Symtab* symtab = GetSymtab();
969 if (!symtab)
970 return eAddressClassUnknown;
Todd Fialafbd703a2014-09-15 22:33:39 +0000971
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000972 // The address class is determined based on the symtab. Ask it from the object file what
973 // contains the symtab information.
974 ObjectFile* symtab_objfile = symtab->GetObjectFile();
975 if (symtab_objfile != nullptr && symtab_objfile != this)
976 return symtab_objfile->GetAddressClass(file_addr);
977
978 auto res = ObjectFile::GetAddressClass (file_addr);
Todd Fialafbd703a2014-09-15 22:33:39 +0000979 if (res != eAddressClassCode)
980 return res;
981
Tamas Berghammer83544cf2015-04-07 10:43:50 +0000982 auto ub = m_address_class_map.upper_bound(file_addr);
983 if (ub == m_address_class_map.begin())
984 {
985 // No entry in the address class map before the address. Return
986 // default address class for an address in a code section.
987 return eAddressClassCode;
988 }
Todd Fialafbd703a2014-09-15 22:33:39 +0000989
Tamas Berghammer83544cf2015-04-07 10:43:50 +0000990 // Move iterator to the address class entry preceding address
991 --ub;
Todd Fialafbd703a2014-09-15 22:33:39 +0000992
Tamas Berghammer83544cf2015-04-07 10:43:50 +0000993 return ub->second;
Todd Fialafbd703a2014-09-15 22:33:39 +0000994}
995
Greg Claytonc7bece562013-01-25 18:06:21 +0000996size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000997ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998{
Greg Claytonc7bece562013-01-25 18:06:21 +0000999 return std::distance(m_section_headers.begin(), I) + 1u;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001000}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001
Greg Claytonc7bece562013-01-25 18:06:21 +00001002size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001003ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
1004{
Greg Claytonc7bece562013-01-25 18:06:21 +00001005 return std::distance(m_section_headers.begin(), I) + 1u;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001006}
1007
1008bool
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001009ObjectFileELF::ParseHeader()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010{
Filipe Cabecinhas22b40f72013-05-16 23:29:36 +00001011 lldb::offset_t offset = 0;
Tamas Berghammerf2561842015-06-30 10:41:23 +00001012 if (!m_header.Parse(m_data, &offset))
1013 return false;
1014
1015 if (!IsInMemory())
1016 return true;
1017
1018 // For in memory object files m_data might not contain the full object file. Try to load it
1019 // until the end of the "Section header table" what is at the end of the ELF file.
1020 addr_t file_size = m_header.e_shoff + m_header.e_shnum * m_header.e_shentsize;
1021 if (m_data.GetByteSize() < file_size)
1022 {
1023 ProcessSP process_sp (m_process_wp.lock());
1024 if (!process_sp)
1025 return false;
1026
1027 DataBufferSP data_sp = ReadMemory(process_sp, m_memory_addr, file_size);
1028 if (!data_sp)
1029 return false;
1030 m_data.SetData(data_sp, 0, file_size);
1031 }
1032
1033 return true;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001034}
1035
1036bool
Greg Clayton60830262011-02-04 18:53:10 +00001037ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001038{
Michael Sartaina7499c92013-07-01 19:45:50 +00001039 // Need to parse the section list to get the UUIDs, so make sure that's been done.
Todd Fiala4339f3a2014-03-25 19:29:09 +00001040 if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile)
Michael Sartaina7499c92013-07-01 19:45:50 +00001041 return false;
1042
Michael Sartainc836ae72013-05-23 20:57:03 +00001043 if (m_uuid.IsValid())
1044 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001045 // We have the full build id uuid.
Michael Sartainc836ae72013-05-23 20:57:03 +00001046 *uuid = m_uuid;
1047 return true;
1048 }
Todd Fiala4339f3a2014-03-25 19:29:09 +00001049 else if (GetType() == ObjectFile::eTypeCoreFile)
1050 {
1051 uint32_t core_notes_crc = 0;
1052
1053 if (!ParseProgramHeaders())
1054 return false;
1055
1056 core_notes_crc = CalculateELFNotesSegmentsCRC32(m_program_headers, m_data);
1057
1058 if (core_notes_crc)
1059 {
1060 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it
1061 // look different form .gnu_debuglink crc - followed by 4 bytes of note
1062 // segments crc.
1063 uint32_t uuidt[4] = { g_core_uuid_magic, core_notes_crc, 0, 0 };
1064 m_uuid.SetBytes (uuidt, sizeof(uuidt));
1065 }
1066 }
1067 else
Michael Sartaina7499c92013-07-01 19:45:50 +00001068 {
Michael Sartain9f4517a2013-07-03 01:52:14 +00001069 if (!m_gnu_debuglink_crc)
1070 m_gnu_debuglink_crc = calc_gnu_debuglink_crc32 (m_data.GetDataStart(), m_data.GetByteSize());
Michael Sartaina7499c92013-07-01 19:45:50 +00001071 if (m_gnu_debuglink_crc)
1072 {
1073 // Use 4 bytes of crc from the .gnu_debuglink section.
1074 uint32_t uuidt[4] = { m_gnu_debuglink_crc, 0, 0, 0 };
Todd Fiala4339f3a2014-03-25 19:29:09 +00001075 m_uuid.SetBytes (uuidt, sizeof(uuidt));
Michael Sartaina7499c92013-07-01 19:45:50 +00001076 }
1077 }
1078
Todd Fiala4339f3a2014-03-25 19:29:09 +00001079 if (m_uuid.IsValid())
1080 {
1081 *uuid = m_uuid;
1082 return true;
1083 }
1084
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001085 return false;
1086}
1087
Michael Sartaina7499c92013-07-01 19:45:50 +00001088lldb_private::FileSpecList
1089ObjectFileELF::GetDebugSymbolFilePaths()
1090{
1091 FileSpecList file_spec_list;
1092
1093 if (!m_gnu_debuglink_file.empty())
1094 {
1095 FileSpec file_spec (m_gnu_debuglink_file.c_str(), false);
1096 file_spec_list.Append (file_spec);
1097 }
1098 return file_spec_list;
1099}
1100
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001102ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001103{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001104 size_t num_modules = ParseDependentModules();
1105 uint32_t num_specs = 0;
1106
1107 for (unsigned i = 0; i < num_modules; ++i)
1108 {
1109 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
1110 num_specs++;
1111 }
1112
1113 return num_specs;
1114}
1115
Stephen Wilson499b40e2011-03-30 16:07:05 +00001116Address
Ed Maste54803652013-10-11 17:39:07 +00001117ObjectFileELF::GetImageInfoAddress(Target *target)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001118{
1119 if (!ParseDynamicSymbols())
Stephen Wilson2ab0a582011-01-15 00:08:44 +00001120 return Address();
1121
1122 SectionList *section_list = GetSectionList();
1123 if (!section_list)
1124 return Address();
1125
Greg Clayton3046e662013-07-10 01:23:25 +00001126 // Find the SHT_DYNAMIC (.dynamic) section.
1127 SectionSP dynsym_section_sp (section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true));
1128 if (!dynsym_section_sp)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001129 return Address();
Greg Clayton3046e662013-07-10 01:23:25 +00001130 assert (dynsym_section_sp->GetObjectFile() == this);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001131
Greg Clayton3046e662013-07-10 01:23:25 +00001132 user_id_t dynsym_id = dynsym_section_sp->GetID();
Michael Sartaina7499c92013-07-01 19:45:50 +00001133 const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001134 if (!dynsym_hdr)
1135 return Address();
1136
Greg Clayton3046e662013-07-10 01:23:25 +00001137 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
Stephen Wilson2ab0a582011-01-15 00:08:44 +00001138 {
Greg Clayton3046e662013-07-10 01:23:25 +00001139 ELFDynamic &symbol = m_dynamic_symbols[i];
Greg Claytone72dfb32012-02-24 01:59:29 +00001140
Ed Maste54803652013-10-11 17:39:07 +00001141 if (symbol.d_tag == DT_DEBUG)
Greg Clayton3046e662013-07-10 01:23:25 +00001142 {
1143 // Compute the offset as the number of previous entries plus the
1144 // size of d_tag.
1145 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
1146 return Address(dynsym_section_sp, offset);
Stephen Wilson2ab0a582011-01-15 00:08:44 +00001147 }
Bhushan D. Attarde1bcc7ba2015-09-15 05:45:29 +00001148 // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP exists in non-PIE.
1149 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 +00001150 {
1151 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
1152 addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);
1153 if (dyn_base == LLDB_INVALID_ADDRESS)
1154 return Address();
Bhushan D. Attarde1bcc7ba2015-09-15 05:45:29 +00001155
Ed Maste54803652013-10-11 17:39:07 +00001156 Error error;
Bhushan D. Attarde1bcc7ba2015-09-15 05:45:29 +00001157 if (symbol.d_tag == DT_MIPS_RLD_MAP)
1158 {
1159 // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer.
1160 Address addr;
1161 if (target->ReadPointerFromMemory(dyn_base + offset, false, error, addr))
1162 return addr;
1163 }
1164 if (symbol.d_tag == DT_MIPS_RLD_MAP_REL)
1165 {
1166 // DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer, relative to the address of the tag.
1167 uint64_t rel_offset;
1168 rel_offset = target->ReadUnsignedIntegerFromMemory(dyn_base + offset, false, GetAddressByteSize(), UINT64_MAX, error);
1169 if (error.Success() && rel_offset != UINT64_MAX)
1170 {
1171 Address addr;
1172 addr_t debug_ptr_address = dyn_base + (offset - GetAddressByteSize()) + rel_offset;
1173 addr.SetOffset (debug_ptr_address);
1174 return addr;
1175 }
1176 }
Ed Maste54803652013-10-11 17:39:07 +00001177 }
Stephen Wilson2ab0a582011-01-15 00:08:44 +00001178 }
1179
1180 return Address();
1181}
1182
Jim Ingham672e6f52011-03-07 23:44:08 +00001183lldb_private::Address
Ed Maste81b4c5f2016-01-04 01:43:47 +00001184ObjectFileELF::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00001185{
Stephen Wilsond126c8c2011-03-08 04:12:15 +00001186 if (m_entry_point_address.IsValid())
1187 return m_entry_point_address;
1188
1189 if (!ParseHeader() || !IsExecutable())
1190 return m_entry_point_address;
1191
Greg Clayton3046e662013-07-10 01:23:25 +00001192 SectionList *section_list = GetSectionList();
1193 addr_t offset = m_header.e_entry;
Stephen Wilsond126c8c2011-03-08 04:12:15 +00001194
Ed Maste81b4c5f2016-01-04 01:43:47 +00001195 if (!section_list)
Stephen Wilsond126c8c2011-03-08 04:12:15 +00001196 m_entry_point_address.SetOffset(offset);
Greg Clayton3046e662013-07-10 01:23:25 +00001197 else
1198 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
Stephen Wilsond126c8c2011-03-08 04:12:15 +00001199 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00001200}
1201
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001202//----------------------------------------------------------------------
1203// ParseDependentModules
1204//----------------------------------------------------------------------
1205size_t
1206ObjectFileELF::ParseDependentModules()
1207{
1208 if (m_filespec_ap.get())
1209 return m_filespec_ap->GetSize();
1210
1211 m_filespec_ap.reset(new FileSpecList());
1212
Michael Sartaina7499c92013-07-01 19:45:50 +00001213 if (!ParseSectionHeaders())
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001214 return 0;
1215
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001216 SectionList *section_list = GetSectionList();
1217 if (!section_list)
1218 return 0;
1219
Greg Clayton3046e662013-07-10 01:23:25 +00001220 // Find the SHT_DYNAMIC section.
1221 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
1222 if (!dynsym)
1223 return 0;
1224 assert (dynsym->GetObjectFile() == this);
1225
1226 const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex (dynsym->GetID());
1227 if (!header)
1228 return 0;
1229 // sh_link: section header index of string table used by entries in the section.
1230 Section *dynstr = section_list->FindSectionByID (header->sh_link + 1).get();
1231 if (!dynstr)
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001232 return 0;
1233
1234 DataExtractor dynsym_data;
1235 DataExtractor dynstr_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001236 if (ReadSectionData(dynsym, dynsym_data) &&
1237 ReadSectionData(dynstr, dynstr_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001238 {
1239 ELFDynamic symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00001240 const lldb::offset_t section_size = dynsym_data.GetByteSize();
1241 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001242
1243 // The only type of entries we are concerned with are tagged DT_NEEDED,
1244 // yielding the name of a required library.
1245 while (offset < section_size)
1246 {
1247 if (!symbol.Parse(dynsym_data, &offset))
1248 break;
1249
1250 if (symbol.d_tag != DT_NEEDED)
1251 continue;
1252
1253 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
1254 const char *lib_name = dynstr_data.PeekCStr(str_index);
Greg Clayton274060b2010-10-20 20:54:39 +00001255 m_filespec_ap->Append(FileSpec(lib_name, true));
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001256 }
1257 }
1258
1259 return m_filespec_ap->GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001260}
1261
1262//----------------------------------------------------------------------
Todd Fiala4339f3a2014-03-25 19:29:09 +00001263// GetProgramHeaderInfo
1264//----------------------------------------------------------------------
1265size_t
1266ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001267 const SetDataFunction &set_data,
Todd Fiala4339f3a2014-03-25 19:29:09 +00001268 const ELFHeader &header)
1269{
1270 // We have already parsed the program headers
1271 if (!program_headers.empty())
1272 return program_headers.size();
1273
1274 // If there are no program headers to read we are done.
1275 if (header.e_phnum == 0)
1276 return 0;
1277
1278 program_headers.resize(header.e_phnum);
1279 if (program_headers.size() != header.e_phnum)
1280 return 0;
1281
1282 const size_t ph_size = header.e_phnum * header.e_phentsize;
1283 const elf_off ph_offset = header.e_phoff;
1284 DataExtractor data;
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001285 if (set_data(data, ph_offset, ph_size) != ph_size)
Todd Fiala4339f3a2014-03-25 19:29:09 +00001286 return 0;
1287
1288 uint32_t idx;
1289 lldb::offset_t offset;
1290 for (idx = 0, offset = 0; idx < header.e_phnum; ++idx)
1291 {
1292 if (program_headers[idx].Parse(data, &offset) == false)
1293 break;
1294 }
1295
1296 if (idx < program_headers.size())
1297 program_headers.resize(idx);
1298
1299 return program_headers.size();
1300
1301}
1302
1303//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001304// ParseProgramHeaders
1305//----------------------------------------------------------------------
1306size_t
1307ObjectFileELF::ParseProgramHeaders()
1308{
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001309 using namespace std::placeholders;
1310 return GetProgramHeaderInfo(m_program_headers,
1311 std::bind(&ObjectFileELF::SetDataWithReadMemoryFallback, this, _1, _2, _3),
1312 m_header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313}
1314
Todd Fialab91de782014-06-27 16:52:49 +00001315lldb_private::Error
1316ObjectFileELF::RefineModuleDetailsFromNote (lldb_private::DataExtractor &data, lldb_private::ArchSpec &arch_spec, lldb_private::UUID &uuid)
Michael Sartainc836ae72013-05-23 20:57:03 +00001317{
Todd Fialab91de782014-06-27 16:52:49 +00001318 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MODULES));
1319 Error error;
1320
Michael Sartainc836ae72013-05-23 20:57:03 +00001321 lldb::offset_t offset = 0;
Michael Sartainc836ae72013-05-23 20:57:03 +00001322
1323 while (true)
1324 {
Todd Fialab91de782014-06-27 16:52:49 +00001325 // Parse the note header. If this fails, bail out.
Greg Claytonb704b692015-10-28 18:04:38 +00001326 const lldb::offset_t note_offset = offset;
Ed Mastec113ff82013-12-02 17:49:13 +00001327 ELFNote note = ELFNote();
1328 if (!note.Parse(data, &offset))
Michael Sartainc836ae72013-05-23 20:57:03 +00001329 {
Todd Fialab91de782014-06-27 16:52:49 +00001330 // We're done.
1331 return error;
Michael Sartainc836ae72013-05-23 20:57:03 +00001332 }
Todd Fialab91de782014-06-27 16:52:49 +00001333
Todd Fialab91de782014-06-27 16:52:49 +00001334 if (log)
1335 log->Printf ("ObjectFileELF::%s parsing note name='%s', type=%" PRIu32, __FUNCTION__, note.n_name.c_str (), note.n_type);
1336
1337 // Process FreeBSD ELF notes.
1338 if ((note.n_name == LLDB_NT_OWNER_FREEBSD) &&
1339 (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) &&
1340 (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE))
1341 {
Todd Fialab91de782014-06-27 16:52:49 +00001342 // Pull out the min version info.
1343 uint32_t version_info;
1344 if (data.GetU32 (&offset, &version_info, 1) == nullptr)
1345 {
1346 error.SetErrorString ("failed to read FreeBSD ABI note payload");
1347 return error;
1348 }
1349
1350 // Convert the version info into a major/minor number.
1351 const uint32_t version_major = version_info / 100000;
1352 const uint32_t version_minor = (version_info / 1000) % 100;
1353
1354 char os_name[32];
1355 snprintf (os_name, sizeof (os_name), "freebsd%" PRIu32 ".%" PRIu32, version_major, version_minor);
1356
1357 // Set the elf OS version to FreeBSD. Also clear the vendor.
1358 arch_spec.GetTriple ().setOSName (os_name);
1359 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
1360
1361 if (log)
1362 log->Printf ("ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_major, version_minor, static_cast<uint32_t> (version_info % 1000));
1363 }
1364 // Process GNU ELF notes.
1365 else if (note.n_name == LLDB_NT_OWNER_GNU)
1366 {
1367 switch (note.n_type)
1368 {
1369 case LLDB_NT_GNU_ABI_TAG:
1370 if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE)
1371 {
Todd Fialab91de782014-06-27 16:52:49 +00001372 // Pull out the min OS version supporting the ABI.
1373 uint32_t version_info[4];
1374 if (data.GetU32 (&offset, &version_info[0], note.n_descsz / 4) == nullptr)
1375 {
1376 error.SetErrorString ("failed to read GNU ABI note payload");
1377 return error;
1378 }
1379
1380 // Set the OS per the OS field.
1381 switch (version_info[0])
1382 {
1383 case LLDB_NT_GNU_ABI_OS_LINUX:
1384 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::Linux);
1385 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
1386 if (log)
1387 log->Printf ("ObjectFileELF::%s detected Linux, min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[1], version_info[2], version_info[3]);
1388 // 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.
1389 break;
1390 case LLDB_NT_GNU_ABI_OS_HURD:
1391 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::UnknownOS);
1392 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
1393 if (log)
1394 log->Printf ("ObjectFileELF::%s detected Hurd (unsupported), min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[1], version_info[2], version_info[3]);
1395 break;
1396 case LLDB_NT_GNU_ABI_OS_SOLARIS:
1397 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::Solaris);
1398 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
1399 if (log)
1400 log->Printf ("ObjectFileELF::%s detected Solaris, min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, __FUNCTION__, version_info[1], version_info[2], version_info[3]);
1401 break;
1402 default:
1403 if (log)
1404 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]);
1405 break;
1406 }
1407 }
1408 break;
1409
1410 case LLDB_NT_GNU_BUILD_ID_TAG:
1411 // Only bother processing this if we don't already have the uuid set.
1412 if (!uuid.IsValid())
1413 {
Ed Maste81955f82016-03-14 14:06:00 +00001414 // 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a build-id of a different
1415 // length. Accept it as long as it's at least 4 bytes as it will be better than our own crc32.
1416 if (note.n_descsz >= 4 && note.n_descsz <= 20)
Todd Fialab91de782014-06-27 16:52:49 +00001417 {
1418 uint8_t uuidbuf[20];
1419 if (data.GetU8 (&offset, &uuidbuf, note.n_descsz) == nullptr)
1420 {
1421 error.SetErrorString ("failed to read GNU_BUILD_ID note payload");
1422 return error;
1423 }
1424
1425 // Save the build id as the UUID for the module.
1426 uuid.SetBytes (uuidbuf, note.n_descsz);
1427 }
1428 }
1429 break;
1430 }
1431 }
1432 // Process NetBSD ELF notes.
1433 else if ((note.n_name == LLDB_NT_OWNER_NETBSD) &&
1434 (note.n_type == LLDB_NT_NETBSD_ABI_TAG) &&
1435 (note.n_descsz == LLDB_NT_NETBSD_ABI_SIZE))
1436 {
Todd Fialab91de782014-06-27 16:52:49 +00001437 // Pull out the min version info.
1438 uint32_t version_info;
1439 if (data.GetU32 (&offset, &version_info, 1) == nullptr)
1440 {
1441 error.SetErrorString ("failed to read NetBSD ABI note payload");
1442 return error;
1443 }
1444
1445 // Set the elf OS version to NetBSD. Also clear the vendor.
1446 arch_spec.GetTriple ().setOS (llvm::Triple::OSType::NetBSD);
1447 arch_spec.GetTriple ().setVendor (llvm::Triple::VendorType::UnknownVendor);
1448
1449 if (log)
1450 log->Printf ("ObjectFileELF::%s detected NetBSD, min version constant %" PRIu32, __FUNCTION__, version_info);
1451 }
Todd Fialacfee9632014-07-16 15:03:10 +00001452 // Process CSR kalimba notes
1453 else if ((note.n_type == LLDB_NT_GNU_ABI_TAG) &&
1454 (note.n_name == LLDB_NT_OWNER_CSR))
1455 {
Todd Fialacfee9632014-07-16 15:03:10 +00001456 arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS);
1457 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::CSR);
1458
1459 // TODO At some point the description string could be processed.
1460 // It could provide a steer towards the kalimba variant which
1461 // this ELF targets.
1462 if(note.n_descsz)
1463 {
Rafael Espindolaa94ae1e2016-01-18 20:57:54 +00001464 const char *cstr = data.GetCStr(&offset, llvm::alignTo (note.n_descsz, 4));
Todd Fialacfee9632014-07-16 15:03:10 +00001465 (void)cstr;
1466 }
1467 }
Tamas Berghammerdb037d92015-03-18 10:36:27 +00001468 else if (note.n_name == LLDB_NT_OWNER_ANDROID)
1469 {
1470 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1471 arch_spec.GetTriple().setEnvironment(llvm::Triple::EnvironmentType::Android);
1472 }
Greg Claytonb704b692015-10-28 18:04:38 +00001473 else if (note.n_name == LLDB_NT_OWNER_LINUX)
1474 {
1475 // This is sometimes found in core files and usually contains extended register info
1476 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1477 }
1478 else if (note.n_name == LLDB_NT_OWNER_CORE)
1479 {
1480 // Parse the NT_FILE to look for stuff in paths to shared libraries
1481 // As the contents look like:
1482 // count = 0x000000000000000a (10)
1483 // page_size = 0x0000000000001000 (4096)
1484 // Index start end file_ofs path
1485 // ===== ------------------ ------------------ ------------------ -------------------------------------
1486 // [ 0] 0x0000000000400000 0x0000000000401000 0x0000000000000000 /tmp/a.out
1487 // [ 1] 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out
1488 // [ 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out
1489 // [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 /lib/x86_64-linux-gnu/libc-2.19.so
1490 // [ 4] 0x00007fa79cba8000 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux-gnu/libc-2.19.so
1491 // [ 5] 0x00007fa79cda7000 0x00007fa79cdab000 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so
1492 // [ 6] 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64-linux-gnu/libc-2.19.so
1493 // [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so
1494 // [ 8] 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64-linux-gnu/ld-2.19.so
1495 // [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so
1496 if (note.n_type == NT_FILE)
1497 {
1498 uint64_t count = data.GetU64(&offset);
1499 offset += 8 + 3*8*count; // Skip page size and all start/end/file_ofs
1500 for (size_t i=0; i<count; ++i)
1501 {
1502 llvm::StringRef path(data.GetCStr(&offset));
1503 if (path.startswith("/lib/x86_64-linux-gnu"))
1504 {
1505 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1506 break;
1507 }
1508 }
1509 }
1510 }
Todd Fialab91de782014-06-27 16:52:49 +00001511
Greg Claytonb704b692015-10-28 18:04:38 +00001512 // Calculate the offset of the next note just in case "offset" has been used
1513 // to poke at the contents of the note data
1514 offset = note_offset + note.GetByteSize();
Michael Sartainc836ae72013-05-23 20:57:03 +00001515 }
Todd Fialab91de782014-06-27 16:52:49 +00001516
1517 return error;
Michael Sartainc836ae72013-05-23 20:57:03 +00001518}
Michael Sartaina7499c92013-07-01 19:45:50 +00001519
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001520void
1521ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length, ArchSpec &arch_spec)
1522{
1523 lldb::offset_t Offset = 0;
1524
1525 uint8_t FormatVersion = data.GetU8(&Offset);
1526 if (FormatVersion != llvm::ARMBuildAttrs::Format_Version)
1527 return;
1528
1529 Offset = Offset + sizeof(uint32_t); // Section Length
1530 llvm::StringRef VendorName = data.GetCStr(&Offset);
1531
1532 if (VendorName != "aeabi")
1533 return;
1534
1535 if (arch_spec.GetTriple().getEnvironment() == llvm::Triple::UnknownEnvironment)
1536 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);
1537
1538 while (Offset < length)
1539 {
1540 uint8_t Tag = data.GetU8(&Offset);
1541 uint32_t Size = data.GetU32(&Offset);
1542
1543 if (Tag != llvm::ARMBuildAttrs::File || Size == 0)
1544 continue;
1545
1546 while (Offset < length)
1547 {
1548 uint64_t Tag = data.GetULEB128(&Offset);
1549 switch (Tag)
1550 {
1551 default:
1552 if (Tag < 32)
1553 data.GetULEB128(&Offset);
1554 else if (Tag % 2 == 0)
1555 data.GetULEB128(&Offset);
1556 else
1557 data.GetCStr(&Offset);
1558
1559 break;
1560
1561 case llvm::ARMBuildAttrs::CPU_raw_name:
1562 case llvm::ARMBuildAttrs::CPU_name:
1563 data.GetCStr(&Offset);
1564
1565 break;
1566
1567 case llvm::ARMBuildAttrs::THUMB_ISA_use:
1568 {
1569 uint64_t ThumbISA = data.GetULEB128(&Offset);
1570
1571 // NOTE: ignore ThumbISA == llvm::ARMBuildAttrs::AllowThumbDerived
1572 // since that derives it based on the architecutre/profile
1573 if (ThumbISA == llvm::ARMBuildAttrs::AllowThumb32)
1574 if (arch_spec.GetTriple().getArch() == llvm::Triple::UnknownArch ||
1575 arch_spec.GetTriple().getArch() == llvm::Triple::arm)
1576 arch_spec.GetTriple().setArch(llvm::Triple::thumb);
1577
1578 break;
1579 }
1580 case llvm::ARMBuildAttrs::ABI_VFP_args:
1581 {
1582 uint64_t VFPArgs = data.GetULEB128(&Offset);
1583
1584 if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS)
1585 {
1586 if (arch_spec.GetTriple().getEnvironment() == llvm::Triple::UnknownEnvironment ||
1587 arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF)
1588 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);
1589
1590 arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);
1591 }
1592 else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS)
1593 {
1594 if (arch_spec.GetTriple().getEnvironment() == llvm::Triple::UnknownEnvironment ||
1595 arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI)
1596 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF);
1597
1598 arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);
1599 }
1600
1601 break;
1602 }
1603 }
1604 }
1605 }
1606}
Todd Fialab91de782014-06-27 16:52:49 +00001607
Michael Sartaina7499c92013-07-01 19:45:50 +00001608//----------------------------------------------------------------------
1609// GetSectionHeaderInfo
1610//----------------------------------------------------------------------
1611size_t
1612ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl &section_headers,
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001613 const SetDataFunction &set_data,
Michael Sartaina7499c92013-07-01 19:45:50 +00001614 const elf::ELFHeader &header,
1615 lldb_private::UUID &uuid,
1616 std::string &gnu_debuglink_file,
Todd Fialab91de782014-06-27 16:52:49 +00001617 uint32_t &gnu_debuglink_crc,
1618 ArchSpec &arch_spec)
Michael Sartaina7499c92013-07-01 19:45:50 +00001619{
Todd Fiala6477ea82014-07-11 15:13:33 +00001620 // Don't reparse the section headers if we already did that.
1621 if (!section_headers.empty())
1622 return section_headers.size();
1623
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001624 // Only initialize the arch_spec to okay defaults if they're not already set.
Todd Fialab91de782014-06-27 16:52:49 +00001625 // We'll refine this with note data as we parse the notes.
1626 if (arch_spec.GetTriple ().getOS () == llvm::Triple::OSType::UnknownOS)
1627 {
Ed Mastef6a13122015-06-05 13:03:08 +00001628 llvm::Triple::OSType ostype;
1629 llvm::Triple::OSType spec_ostype;
Matthew Gardiner5f675792014-08-27 12:09:39 +00001630 const uint32_t sub_type = subTypeFromElfHeader(header);
Ed Mastef6a13122015-06-05 13:03:08 +00001631 arch_spec.SetArchitecture (eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]);
1632 //
1633 // Validate if it is ok to remove GetOsFromOSABI
1634 GetOsFromOSABI (header.e_ident[EI_OSABI], ostype);
1635 spec_ostype = arch_spec.GetTriple ().getOS ();
1636 assert(spec_ostype == ostype);
Todd Fialab91de782014-06-27 16:52:49 +00001637 }
1638
Jaydeep Patil501a7812015-07-16 03:51:55 +00001639 if (arch_spec.GetMachine() == llvm::Triple::mips || arch_spec.GetMachine() == llvm::Triple::mipsel
1640 || arch_spec.GetMachine() == llvm::Triple::mips64 || arch_spec.GetMachine() == llvm::Triple::mips64el)
1641 {
1642 switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE)
1643 {
Ed Maste81b4c5f2016-01-04 01:43:47 +00001644 case llvm::ELF::EF_MIPS_MICROMIPS:
1645 arch_spec.SetFlags (ArchSpec::eMIPSAse_micromips);
Jaydeep Patil501a7812015-07-16 03:51:55 +00001646 break;
Ed Maste81b4c5f2016-01-04 01:43:47 +00001647 case llvm::ELF::EF_MIPS_ARCH_ASE_M16:
1648 arch_spec.SetFlags (ArchSpec::eMIPSAse_mips16);
Jaydeep Patil501a7812015-07-16 03:51:55 +00001649 break;
Ed Maste81b4c5f2016-01-04 01:43:47 +00001650 case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX:
1651 arch_spec.SetFlags (ArchSpec::eMIPSAse_mdmx);
Jaydeep Patil501a7812015-07-16 03:51:55 +00001652 break;
Ed Maste81b4c5f2016-01-04 01:43:47 +00001653 default:
Jaydeep Patil501a7812015-07-16 03:51:55 +00001654 break;
1655 }
1656 }
1657
Omair Javaid0a767222016-02-05 14:37:53 +00001658 if (arch_spec.GetMachine() == llvm::Triple::arm ||
1659 arch_spec.GetMachine() == llvm::Triple::thumb)
1660 {
1661 if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT)
1662 arch_spec.SetFlags (ArchSpec::eARM_abi_soft_float);
1663 else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT)
1664 arch_spec.SetFlags (ArchSpec::eARM_abi_hard_float);
1665 }
1666
Michael Sartaina7499c92013-07-01 19:45:50 +00001667 // If there are no section headers we are done.
1668 if (header.e_shnum == 0)
1669 return 0;
1670
Todd Fialab91de782014-06-27 16:52:49 +00001671 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MODULES));
1672
Michael Sartaina7499c92013-07-01 19:45:50 +00001673 section_headers.resize(header.e_shnum);
1674 if (section_headers.size() != header.e_shnum)
1675 return 0;
1676
1677 const size_t sh_size = header.e_shnum * header.e_shentsize;
1678 const elf_off sh_offset = header.e_shoff;
1679 DataExtractor sh_data;
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001680 if (set_data (sh_data, sh_offset, sh_size) != sh_size)
Michael Sartaina7499c92013-07-01 19:45:50 +00001681 return 0;
1682
1683 uint32_t idx;
1684 lldb::offset_t offset;
1685 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx)
1686 {
1687 if (section_headers[idx].Parse(sh_data, &offset) == false)
1688 break;
1689 }
1690 if (idx < section_headers.size())
1691 section_headers.resize(idx);
1692
1693 const unsigned strtab_idx = header.e_shstrndx;
1694 if (strtab_idx && strtab_idx < section_headers.size())
1695 {
1696 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx];
1697 const size_t byte_size = sheader.sh_size;
1698 const Elf64_Off offset = sheader.sh_offset;
1699 lldb_private::DataExtractor shstr_data;
1700
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001701 if (set_data (shstr_data, offset, byte_size) == byte_size)
Michael Sartaina7499c92013-07-01 19:45:50 +00001702 {
1703 for (SectionHeaderCollIter I = section_headers.begin();
1704 I != section_headers.end(); ++I)
1705 {
1706 static ConstString g_sect_name_gnu_debuglink (".gnu_debuglink");
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001707 const ELFSectionHeaderInfo &sheader = *I;
1708 const uint64_t section_size = sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size;
Michael Sartaina7499c92013-07-01 19:45:50 +00001709 ConstString name(shstr_data.PeekCStr(I->sh_name));
1710
1711 I->section_name = name;
1712
Jaydeep Patil501a7812015-07-16 03:51:55 +00001713 if (arch_spec.GetMachine() == llvm::Triple::mips || arch_spec.GetMachine() == llvm::Triple::mipsel
1714 || arch_spec.GetMachine() == llvm::Triple::mips64 || arch_spec.GetMachine() == llvm::Triple::mips64el)
1715 {
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001716 uint32_t arch_flags = arch_spec.GetFlags ();
1717 DataExtractor data;
1718 if (sheader.sh_type == SHT_MIPS_ABIFLAGS)
Jaydeep Patil501a7812015-07-16 03:51:55 +00001719 {
Ed Maste81b4c5f2016-01-04 01:43:47 +00001720
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001721 if (section_size && (set_data (data, sheader.sh_offset, section_size) == section_size))
Jaydeep Patil501a7812015-07-16 03:51:55 +00001722 {
1723 lldb::offset_t ase_offset = 12; // MIPS ABI Flags Version: 0
Jaydeep Patil501a7812015-07-16 03:51:55 +00001724 arch_flags |= data.GetU32 (&ase_offset);
Jaydeep Patil501a7812015-07-16 03:51:55 +00001725 }
1726 }
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001727 // Settings appropriate ArchSpec ABI Flags
1728 if (header.e_flags & llvm::ELF::EF_MIPS_ABI2)
Ed Maste81b4c5f2016-01-04 01:43:47 +00001729 {
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001730 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32;
1731 }
1732 else if (header.e_flags & llvm::ELF::EF_MIPS_ABI_O32)
1733 {
Ed Maste81b4c5f2016-01-04 01:43:47 +00001734 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32;
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001735 }
1736 arch_spec.SetFlags (arch_flags);
Jaydeep Patil501a7812015-07-16 03:51:55 +00001737 }
1738
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001739 if (arch_spec.GetMachine() == llvm::Triple::arm || arch_spec.GetMachine() == llvm::Triple::thumb)
1740 {
1741 DataExtractor data;
1742
1743 if (sheader.sh_type != SHT_ARM_ATTRIBUTES)
1744 continue;
1745 if (section_size == 0 || set_data(data, sheader.sh_offset, section_size) != section_size)
1746 continue;
1747
1748 ParseARMAttributes(data, section_size, arch_spec);
1749 }
1750
Michael Sartaina7499c92013-07-01 19:45:50 +00001751 if (name == g_sect_name_gnu_debuglink)
1752 {
1753 DataExtractor data;
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001754 if (section_size && (set_data (data, sheader.sh_offset, section_size) == section_size))
Michael Sartaina7499c92013-07-01 19:45:50 +00001755 {
1756 lldb::offset_t gnu_debuglink_offset = 0;
1757 gnu_debuglink_file = data.GetCStr (&gnu_debuglink_offset);
Rafael Espindolaa94ae1e2016-01-18 20:57:54 +00001758 gnu_debuglink_offset = llvm::alignTo (gnu_debuglink_offset, 4);
Michael Sartaina7499c92013-07-01 19:45:50 +00001759 data.GetU32 (&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
1760 }
1761 }
1762
Todd Fialab91de782014-06-27 16:52:49 +00001763 // Process ELF note section entries.
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +00001764 bool is_note_header = (sheader.sh_type == SHT_NOTE);
Tamas Berghammerdb037d92015-03-18 10:36:27 +00001765
1766 // The section header ".note.android.ident" is stored as a
1767 // PROGBITS type header but it is actually a note header.
1768 static ConstString g_sect_name_android_ident (".note.android.ident");
1769 if (!is_note_header && name == g_sect_name_android_ident)
1770 is_note_header = true;
1771
1772 if (is_note_header)
Michael Sartaina7499c92013-07-01 19:45:50 +00001773 {
Todd Fialab91de782014-06-27 16:52:49 +00001774 // Allow notes to refine module info.
Michael Sartaina7499c92013-07-01 19:45:50 +00001775 DataExtractor data;
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001776 if (section_size && (set_data (data, sheader.sh_offset, section_size) == section_size))
Michael Sartaina7499c92013-07-01 19:45:50 +00001777 {
Todd Fialab91de782014-06-27 16:52:49 +00001778 Error error = RefineModuleDetailsFromNote (data, arch_spec, uuid);
1779 if (error.Fail ())
1780 {
1781 if (log)
1782 log->Printf ("ObjectFileELF::%s ELF note processing failed: %s", __FUNCTION__, error.AsCString ());
1783 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001784 }
1785 }
1786 }
1787
Todd Fiala7df337f2015-10-13 23:41:19 +00001788 // Make any unknown triple components to be unspecified unknowns.
1789 if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
1790 arch_spec.GetTriple().setVendorName (llvm::StringRef());
1791 if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS)
1792 arch_spec.GetTriple().setOSName (llvm::StringRef());
1793
Michael Sartaina7499c92013-07-01 19:45:50 +00001794 return section_headers.size();
1795 }
1796 }
1797
1798 section_headers.clear();
1799 return 0;
1800}
1801
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001802size_t
1803ObjectFileELF::GetProgramHeaderCount()
1804{
1805 return ParseProgramHeaders();
1806}
1807
1808const elf::ELFProgramHeader *
1809ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id)
1810{
1811 if (!id || !ParseProgramHeaders())
1812 return NULL;
1813
1814 if (--id < m_program_headers.size())
1815 return &m_program_headers[id];
1816
1817 return NULL;
1818}
1819
Ed Maste81b4c5f2016-01-04 01:43:47 +00001820DataExtractor
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001821ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id)
1822{
1823 const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id);
1824 if (segment_header == NULL)
1825 return DataExtractor();
1826 return DataExtractor(m_data, segment_header->p_offset, segment_header->p_filesz);
1827}
1828
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00001829std::string
1830ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const
1831{
Bruce Mitchener0b6ba7c2015-07-04 05:16:58 +00001832 size_t pos = symbol_name.find('@');
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00001833 return symbol_name.substr(0, pos).str();
1834}
1835
Michael Sartaina7499c92013-07-01 19:45:50 +00001836//----------------------------------------------------------------------
1837// ParseSectionHeaders
1838//----------------------------------------------------------------------
1839size_t
1840ObjectFileELF::ParseSectionHeaders()
1841{
Ravitheja Addepally15f89c42016-01-19 12:55:21 +00001842 using namespace std::placeholders;
1843
1844 return GetSectionHeaderInfo(m_section_headers,
1845 std::bind(&ObjectFileELF::SetDataWithReadMemoryFallback, this, _1, _2, _3),
1846 m_header,
1847 m_uuid,
1848 m_gnu_debuglink_file,
1849 m_gnu_debuglink_crc,
1850 m_arch_spec);
1851}
1852
1853lldb::offset_t
1854ObjectFileELF::SetData(const lldb_private::DataExtractor &src, lldb_private::DataExtractor &dst, lldb::offset_t offset, lldb::offset_t length)
1855{
1856 return dst.SetData(src, offset, length);
1857}
1858
1859lldb::offset_t
1860ObjectFileELF::SetDataWithReadMemoryFallback(lldb_private::DataExtractor &dst, lldb::offset_t offset, lldb::offset_t length)
1861{
1862 if (offset + length <= m_data.GetByteSize())
1863 return dst.SetData(m_data, offset, length);
1864
1865 const auto process_sp = m_process_wp.lock();
1866 if (process_sp != nullptr)
1867 {
1868 addr_t file_size = offset + length;
1869
1870 DataBufferSP data_sp = ReadMemory(process_sp, m_memory_addr, file_size);
1871 if (!data_sp)
1872 return false;
1873 m_data.SetData(data_sp, 0, file_size);
1874 }
1875
1876 return dst.SetData(m_data, offset, length);
Michael Sartaina7499c92013-07-01 19:45:50 +00001877}
1878
Michael Sartaina7499c92013-07-01 19:45:50 +00001879const ObjectFileELF::ELFSectionHeaderInfo *
1880ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
1881{
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001882 if (!id || !ParseSectionHeaders())
Michael Sartaina7499c92013-07-01 19:45:50 +00001883 return NULL;
1884
1885 if (--id < m_section_headers.size())
1886 return &m_section_headers[id];
1887
1888 return NULL;
1889}
1890
Tamas Berghammer85fadd92015-05-08 09:40:05 +00001891lldb::user_id_t
1892ObjectFileELF::GetSectionIndexByName(const char* name)
1893{
1894 if (!name || !name[0] || !ParseSectionHeaders())
1895 return 0;
1896 for (size_t i = 1; i < m_section_headers.size(); ++i)
1897 if (m_section_headers[i].section_name == ConstString(name))
1898 return i;
1899 return 0;
1900}
1901
Greg Clayton3046e662013-07-10 01:23:25 +00001902void
1903ObjectFileELF::CreateSections(SectionList &unified_section_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001904{
Greg Clayton3046e662013-07-10 01:23:25 +00001905 if (!m_sections_ap.get() && ParseSectionHeaders())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001906 {
1907 m_sections_ap.reset(new SectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001908
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001909 for (SectionHeaderCollIter I = m_section_headers.begin();
1910 I != m_section_headers.end(); ++I)
1911 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001912 const ELFSectionHeaderInfo &header = *I;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001913
Michael Sartaina7499c92013-07-01 19:45:50 +00001914 ConstString& name = I->section_name;
Greg Clayton47037bc2012-03-27 02:40:46 +00001915 const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
1916 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001917
Greg Clayton4ceb9982010-07-21 22:54:26 +00001918 static ConstString g_sect_name_text (".text");
1919 static ConstString g_sect_name_data (".data");
1920 static ConstString g_sect_name_bss (".bss");
Greg Clayton741f3f92012-03-27 21:10:07 +00001921 static ConstString g_sect_name_tdata (".tdata");
1922 static ConstString g_sect_name_tbss (".tbss");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001923 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001924 static ConstString g_sect_name_dwarf_debug_addr (".debug_addr");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001925 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
1926 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
1927 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
1928 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
1929 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
1930 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
Siva Chandrad8335e92015-12-16 00:22:08 +00001931 static ConstString g_sect_name_dwarf_debug_macro (".debug_macro");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001932 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
1933 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
1934 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
1935 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001936 static ConstString g_sect_name_dwarf_debug_str_offsets (".debug_str_offsets");
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001937 static ConstString g_sect_name_dwarf_debug_abbrev_dwo (".debug_abbrev.dwo");
1938 static ConstString g_sect_name_dwarf_debug_info_dwo (".debug_info.dwo");
1939 static ConstString g_sect_name_dwarf_debug_line_dwo (".debug_line.dwo");
Siva Chandrad8335e92015-12-16 00:22:08 +00001940 static ConstString g_sect_name_dwarf_debug_macro_dwo (".debug_macro.dwo");
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001941 static ConstString g_sect_name_dwarf_debug_loc_dwo (".debug_loc.dwo");
1942 static ConstString g_sect_name_dwarf_debug_str_dwo (".debug_str.dwo");
1943 static ConstString g_sect_name_dwarf_debug_str_offsets_dwo (".debug_str_offsets.dwo");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001944 static ConstString g_sect_name_eh_frame (".eh_frame");
Tamas Berghammer648f3c72015-09-30 13:50:14 +00001945 static ConstString g_sect_name_arm_exidx (".ARM.exidx");
1946 static ConstString g_sect_name_arm_extab (".ARM.extab");
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001947 static ConstString g_sect_name_go_symtab (".gosymtab");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001948
1949 SectionType sect_type = eSectionTypeOther;
1950
Greg Clayton741f3f92012-03-27 21:10:07 +00001951 bool is_thread_specific = false;
Michael Sartaina7499c92013-07-01 19:45:50 +00001952
Greg Clayton4ceb9982010-07-21 22:54:26 +00001953 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
1954 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
1955 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
Greg Clayton741f3f92012-03-27 21:10:07 +00001956 else if (name == g_sect_name_tdata)
1957 {
1958 sect_type = eSectionTypeData;
Ed Maste81b4c5f2016-01-04 01:43:47 +00001959 is_thread_specific = true;
Greg Clayton741f3f92012-03-27 21:10:07 +00001960 }
1961 else if (name == g_sect_name_tbss)
1962 {
Ed Maste81b4c5f2016-01-04 01:43:47 +00001963 sect_type = eSectionTypeZeroFill;
1964 is_thread_specific = true;
Greg Clayton741f3f92012-03-27 21:10:07 +00001965 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001966 // .debug_abbrev – Abbreviations used in the .debug_info section
1967 // .debug_aranges – Lookup table for mapping addresses to compilation units
1968 // .debug_frame – Call frame information
1969 // .debug_info – The core DWARF information section
1970 // .debug_line – Line number information
1971 // .debug_loc – Location lists used in DW_AT_location attributes
1972 // .debug_macinfo – Macro information
1973 // .debug_pubnames – Lookup table for mapping object and function names to compilation units
1974 // .debug_pubtypes – Lookup table for mapping type names to compilation units
1975 // .debug_ranges – Address ranges used in DW_AT_ranges attributes
1976 // .debug_str – String table used in .debug_info
Michael Sartain3cf443d2013-07-17 00:26:30 +00001977 // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html
1978 // MISSING? .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644
Michael Sartaina7499c92013-07-01 19:45:50 +00001979 // MISSING? .debug_types - Type descriptions from DWARF 4? See http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001980 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
1981 else if (name == g_sect_name_dwarf_debug_addr) sect_type = eSectionTypeDWARFDebugAddr;
1982 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
1983 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
1984 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
1985 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
1986 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
1987 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
Siva Chandrad8335e92015-12-16 00:22:08 +00001988 else if (name == g_sect_name_dwarf_debug_macro) sect_type = eSectionTypeDWARFDebugMacro;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001989 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
1990 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
1991 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
1992 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
1993 else if (name == g_sect_name_dwarf_debug_str_offsets) sect_type = eSectionTypeDWARFDebugStrOffsets;
1994 else if (name == g_sect_name_dwarf_debug_abbrev_dwo) sect_type = eSectionTypeDWARFDebugAbbrev;
1995 else if (name == g_sect_name_dwarf_debug_info_dwo) sect_type = eSectionTypeDWARFDebugInfo;
1996 else if (name == g_sect_name_dwarf_debug_line_dwo) sect_type = eSectionTypeDWARFDebugLine;
Siva Chandrad8335e92015-12-16 00:22:08 +00001997 else if (name == g_sect_name_dwarf_debug_macro_dwo) sect_type = eSectionTypeDWARFDebugMacro;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001998 else if (name == g_sect_name_dwarf_debug_loc_dwo) sect_type = eSectionTypeDWARFDebugLoc;
1999 else if (name == g_sect_name_dwarf_debug_str_dwo) sect_type = eSectionTypeDWARFDebugStr;
2000 else if (name == g_sect_name_dwarf_debug_str_offsets_dwo) sect_type = eSectionTypeDWARFDebugStrOffsets;
2001 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
Tamas Berghammer648f3c72015-09-30 13:50:14 +00002002 else if (name == g_sect_name_arm_exidx) sect_type = eSectionTypeARMexidx;
2003 else if (name == g_sect_name_arm_extab) sect_type = eSectionTypeARMextab;
Ryan Brown65d4d5c2015-09-16 21:20:44 +00002004 else if (name == g_sect_name_go_symtab) sect_type = eSectionTypeGoSymtab;
Michael Sartaina7499c92013-07-01 19:45:50 +00002005
2006 switch (header.sh_type)
Michael Sartainc836ae72013-05-23 20:57:03 +00002007 {
Michael Sartaina7499c92013-07-01 19:45:50 +00002008 case SHT_SYMTAB:
2009 assert (sect_type == eSectionTypeOther);
2010 sect_type = eSectionTypeELFSymbolTable;
2011 break;
2012 case SHT_DYNSYM:
2013 assert (sect_type == eSectionTypeOther);
2014 sect_type = eSectionTypeELFDynamicSymbols;
2015 break;
2016 case SHT_RELA:
2017 case SHT_REL:
2018 assert (sect_type == eSectionTypeOther);
2019 sect_type = eSectionTypeELFRelocationEntries;
2020 break;
2021 case SHT_DYNAMIC:
2022 assert (sect_type == eSectionTypeOther);
2023 sect_type = eSectionTypeELFDynamicLinkInfo;
2024 break;
Michael Sartainc836ae72013-05-23 20:57:03 +00002025 }
Michael Sartaina7499c92013-07-01 19:45:50 +00002026
Matthew Gardinerf03e6d842014-09-29 08:02:24 +00002027 if (eSectionTypeOther == sect_type)
2028 {
2029 // the kalimba toolchain assumes that ELF section names are free-form. It does
Bruce Mitchenere171da52015-07-22 00:16:02 +00002030 // support linkscripts which (can) give rise to various arbitrarily named
Ed Maste81b4c5f2016-01-04 01:43:47 +00002031 // sections being "Code" or "Data".
Matthew Gardinerf03e6d842014-09-29 08:02:24 +00002032 sect_type = kalimbaSectionType(m_header, header);
2033 }
2034
2035 const uint32_t target_bytes_size =
Ed Maste81b4c5f2016-01-04 01:43:47 +00002036 (eSectionTypeData == sect_type || eSectionTypeZeroFill == sect_type) ?
Matthew Gardinerf03e6d842014-09-29 08:02:24 +00002037 m_arch_spec.GetDataByteSize() :
2038 eSectionTypeCode == sect_type ?
2039 m_arch_spec.GetCodeByteSize() : 1;
2040
Zachary Turner736d4d82014-06-25 05:42:32 +00002041 elf::elf_xword log2align = (header.sh_addralign==0)
2042 ? 0
2043 : llvm::Log2_64(header.sh_addralign);
Greg Clayton3046e662013-07-10 01:23:25 +00002044 SectionSP section_sp (new Section(GetModule(), // Module to which this section belongs.
2045 this, // ObjectFile to which this section belongs and should read section data from.
2046 SectionIndex(I), // Section ID.
2047 name, // Section name.
2048 sect_type, // Section type.
2049 header.sh_addr, // VM address.
2050 vm_size, // VM size in bytes of this section.
2051 header.sh_offset, // Offset of this section in the file.
2052 file_size, // Size of the section as found in the file.
Zachary Turner736d4d82014-06-25 05:42:32 +00002053 log2align, // Alignment of the section
Matthew Gardinerf03e6d842014-09-29 08:02:24 +00002054 header.sh_flags, // Flags for this section.
2055 target_bytes_size));// Number of host bytes per target byte
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002056
Greg Clayton741f3f92012-03-27 21:10:07 +00002057 if (is_thread_specific)
2058 section_sp->SetIsThreadSpecific (is_thread_specific);
2059 m_sections_ap->AddSection(section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002060 }
2061 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002062
Greg Clayton3046e662013-07-10 01:23:25 +00002063 if (m_sections_ap.get())
2064 {
2065 if (GetType() == eTypeDebugInfo)
2066 {
2067 static const SectionType g_sections[] =
2068 {
Greg Clayton3046e662013-07-10 01:23:25 +00002069 eSectionTypeDWARFDebugAbbrev,
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00002070 eSectionTypeDWARFDebugAddr,
2071 eSectionTypeDWARFDebugAranges,
Greg Clayton3046e662013-07-10 01:23:25 +00002072 eSectionTypeDWARFDebugFrame,
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00002073 eSectionTypeDWARFDebugInfo,
Greg Clayton3046e662013-07-10 01:23:25 +00002074 eSectionTypeDWARFDebugLine,
Greg Clayton3046e662013-07-10 01:23:25 +00002075 eSectionTypeDWARFDebugLoc,
2076 eSectionTypeDWARFDebugMacInfo,
2077 eSectionTypeDWARFDebugPubNames,
2078 eSectionTypeDWARFDebugPubTypes,
2079 eSectionTypeDWARFDebugRanges,
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00002080 eSectionTypeDWARFDebugStr,
2081 eSectionTypeDWARFDebugStrOffsets,
Greg Clayton3046e662013-07-10 01:23:25 +00002082 eSectionTypeELFSymbolTable,
2083 };
2084 SectionList *elf_section_list = m_sections_ap.get();
2085 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx)
2086 {
2087 SectionType section_type = g_sections[idx];
2088 SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true));
2089 if (section_sp)
2090 {
2091 SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true));
2092 if (module_section_sp)
2093 unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp);
2094 else
2095 unified_section_list.AddSection (section_sp);
2096 }
2097 }
2098 }
2099 else
2100 {
2101 unified_section_list = *m_sections_ap;
2102 }
2103 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002104}
2105
Stephane Sezer9187e732015-09-11 18:56:59 +00002106// Find the arm/aarch64 mapping symbol character in the given symbol name. Mapping symbols have the
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002107// form of "$<char>[.<any>]*". Additionally we recognize cases when the mapping symbol prefixed by
2108// an arbitrary string because if a symbol prefix added to each symbol in the object file with
2109// objcopy then the mapping symbols are also prefixed.
2110static char
2111FindArmAarch64MappingSymbol(const char* symbol_name)
2112{
2113 if (!symbol_name)
2114 return '\0';
2115
2116 const char* dollar_pos = ::strchr(symbol_name, '$');
2117 if (!dollar_pos || dollar_pos[1] == '\0')
2118 return '\0';
2119
2120 if (dollar_pos[2] == '\0' || dollar_pos[2] == '.')
2121 return dollar_pos[1];
2122 return '\0';
2123}
2124
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002125#define STO_MIPS_ISA (3 << 6)
2126#define STO_MICROMIPS (2 << 6)
2127#define IS_MICROMIPS(ST_OTHER) (((ST_OTHER) & STO_MIPS_ISA) == STO_MICROMIPS)
2128
Greg Clayton3046e662013-07-10 01:23:25 +00002129// private
Michael Sartaina7499c92013-07-01 19:45:50 +00002130unsigned
Greg Clayton3046e662013-07-10 01:23:25 +00002131ObjectFileELF::ParseSymbols (Symtab *symtab,
2132 user_id_t start_id,
2133 SectionList *section_list,
2134 const size_t num_symbols,
2135 const DataExtractor &symtab_data,
2136 const DataExtractor &strtab_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002137{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002138 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00002139 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002140
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002141 static ConstString text_section_name(".text");
2142 static ConstString init_section_name(".init");
2143 static ConstString fini_section_name(".fini");
2144 static ConstString ctors_section_name(".ctors");
2145 static ConstString dtors_section_name(".dtors");
2146
2147 static ConstString data_section_name(".data");
2148 static ConstString rodata_section_name(".rodata");
2149 static ConstString rodata1_section_name(".rodata1");
2150 static ConstString data2_section_name(".data1");
2151 static ConstString bss_section_name(".bss");
Justin Hibbits6256a0e2014-10-31 02:34:28 +00002152 static ConstString opd_section_name(".opd"); // For ppc64
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002153
Tamas Berghammerc9627ae2015-07-13 09:54:41 +00002154 // On Android the oatdata and the oatexec symbols in system@framework@boot.oat covers the full
2155 // .text section what causes issues with displaying unusable symbol name to the user and very
2156 // slow unwinding speed because the instruction emulation based unwind plans try to emulate all
2157 // instructions in these symbols. Don't add these symbols to the symbol list as they have no
2158 // use for the debugger and they are causing a lot of trouble.
2159 // Filtering can't be restricted to Android because this special object file don't contain the
2160 // note section specifying the environment to Android but the custom extension and file name
2161 // makes it highly unlikely that this will collide with anything else.
2162 bool skip_oatdata_oatexec = m_file.GetFilename() == ConstString("system@framework@boot.oat");
2163
Tamas Berghammer9fa11472015-10-27 10:43:27 +00002164 ArchSpec arch;
2165 GetArchitecture(arch);
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002166 ModuleSP module_sp(GetModule());
2167 SectionList* module_section_list = module_sp ? module_sp->GetSectionList() : nullptr;
Tamas Berghammer9fa11472015-10-27 10:43:27 +00002168
2169 // Local cache to avoid doing a FindSectionByName for each symbol. The "const char*" key must
2170 // came from a ConstString object so they can be compared by pointer
2171 std::unordered_map<const char*, lldb::SectionSP> section_name_to_section;
2172
Stephen Wilson499b40e2011-03-30 16:07:05 +00002173 unsigned i;
2174 for (i = 0; i < num_symbols; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002175 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002176 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002177 break;
Ed Maste81b4c5f2016-01-04 01:43:47 +00002178
Greg Clayton9594f4c2013-04-13 23:17:23 +00002179 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
2180
Andrew MacPherson17220c12014-03-05 10:12:43 +00002181 // No need to add non-section symbols that have no names
2182 if (symbol.getType() != STT_SECTION &&
2183 (symbol_name == NULL || symbol_name[0] == '\0'))
Greg Clayton9594f4c2013-04-13 23:17:23 +00002184 continue;
2185
Tamas Berghammerc9627ae2015-07-13 09:54:41 +00002186 // Skipping oatdata and oatexec sections if it is requested. See details above the
2187 // definition of skip_oatdata_oatexec for the reasons.
2188 if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 || ::strcmp(symbol_name, "oatexec") == 0))
2189 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002190
Greg Claytone72dfb32012-02-24 01:59:29 +00002191 SectionSP symbol_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002192 SymbolType symbol_type = eSymbolTypeInvalid;
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002193 Elf64_Half section_idx = symbol.st_shndx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002194
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002195 switch (section_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002196 {
2197 case SHN_ABS:
2198 symbol_type = eSymbolTypeAbsolute;
2199 break;
2200 case SHN_UNDEF:
2201 symbol_type = eSymbolTypeUndefined;
2202 break;
2203 default:
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002204 symbol_section_sp = section_list->GetSectionAtIndex(section_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002205 break;
2206 }
2207
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002208 // If a symbol is undefined do not process it further even if it has a STT type
2209 if (symbol_type != eSymbolTypeUndefined)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002210 {
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002211 switch (symbol.getType())
2212 {
2213 default:
2214 case STT_NOTYPE:
2215 // The symbol's type is not specified.
2216 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002217
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002218 case STT_OBJECT:
2219 // The symbol is associated with a data object, such as a variable,
2220 // an array, etc.
2221 symbol_type = eSymbolTypeData;
2222 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002223
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002224 case STT_FUNC:
2225 // The symbol is associated with a function or other executable code.
2226 symbol_type = eSymbolTypeCode;
2227 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002228
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002229 case STT_SECTION:
2230 // The symbol is associated with a section. Symbol table entries of
2231 // this type exist primarily for relocation and normally have
2232 // STB_LOCAL binding.
2233 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002234
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002235 case STT_FILE:
2236 // Conventionally, the symbol's name gives the name of the source
2237 // file associated with the object file. A file symbol has STB_LOCAL
2238 // binding, its section index is SHN_ABS, and it precedes the other
2239 // STB_LOCAL symbols for the file, if it is present.
Greg Clayton9594f4c2013-04-13 23:17:23 +00002240 symbol_type = eSymbolTypeSourceFile;
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002241 break;
Matt Kopec00049b82013-02-27 20:13:38 +00002242
2243 case STT_GNU_IFUNC:
2244 // The symbol is associated with an indirect function. The actual
2245 // function will be resolved if it is referenced.
2246 symbol_type = eSymbolTypeResolver;
2247 break;
Matt Kopec92dd5cf2013-02-12 18:30:30 +00002248 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002249 }
2250
2251 if (symbol_type == eSymbolTypeInvalid)
2252 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002253 if (symbol_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002254 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002255 const ConstString &sect_name = symbol_section_sp->GetName();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002256 if (sect_name == text_section_name ||
2257 sect_name == init_section_name ||
2258 sect_name == fini_section_name ||
2259 sect_name == ctors_section_name ||
2260 sect_name == dtors_section_name)
2261 {
2262 symbol_type = eSymbolTypeCode;
2263 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002264 else if (sect_name == data_section_name ||
2265 sect_name == data2_section_name ||
2266 sect_name == rodata_section_name ||
2267 sect_name == rodata1_section_name ||
2268 sect_name == bss_section_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002269 {
2270 symbol_type = eSymbolTypeData;
2271 }
2272 }
2273 }
2274
Todd Fialafbd703a2014-09-15 22:33:39 +00002275 int64_t symbol_value_offset = 0;
2276 uint32_t additional_flags = 0;
Todd Fiala1a088662014-09-15 16:27:44 +00002277
Tamas Berghammer9fa11472015-10-27 10:43:27 +00002278 if (arch.IsValid())
Todd Fiala1a088662014-09-15 16:27:44 +00002279 {
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002280 if (arch.GetMachine() == llvm::Triple::arm)
Todd Fialafbd703a2014-09-15 22:33:39 +00002281 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002282 if (symbol.getBinding() == STB_LOCAL)
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002283 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002284 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002285 if (symbol_type == eSymbolTypeCode)
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002286 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002287 switch (mapping_symbol)
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002288 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002289 case 'a':
2290 // $a[.<any>]* - marks an ARM instruction sequence
2291 m_address_class_map[symbol.st_value] = eAddressClassCode;
2292 break;
2293 case 'b':
2294 case 't':
2295 // $b[.<any>]* - marks a THUMB BL instruction sequence
2296 // $t[.<any>]* - marks a THUMB instruction sequence
2297 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA;
2298 break;
2299 case 'd':
2300 // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2301 m_address_class_map[symbol.st_value] = eAddressClassData;
2302 break;
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002303 }
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002304 }
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002305 if (mapping_symbol)
2306 continue;
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002307 }
2308 }
2309 else if (arch.GetMachine() == llvm::Triple::aarch64)
2310 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002311 if (symbol.getBinding() == STB_LOCAL)
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002312 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002313 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002314 if (symbol_type == eSymbolTypeCode)
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002315 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002316 switch (mapping_symbol)
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002317 {
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002318 case 'x':
2319 // $x[.<any>]* - marks an A64 instruction sequence
2320 m_address_class_map[symbol.st_value] = eAddressClassCode;
2321 break;
2322 case 'd':
2323 // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2324 m_address_class_map[symbol.st_value] = eAddressClassData;
2325 break;
Tamas Berghammerb52bbd12015-04-17 09:36:59 +00002326 }
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002327 }
Tamas Berghammerc7776e42015-09-11 10:04:00 +00002328 if (mapping_symbol)
2329 continue;
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002330 }
2331 }
2332
2333 if (arch.GetMachine() == llvm::Triple::arm)
2334 {
Aidan Dodds5f2d0c32015-05-28 15:37:01 +00002335 if (symbol_type == eSymbolTypeCode)
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002336 {
Aidan Dodds5f2d0c32015-05-28 15:37:01 +00002337 if (symbol.st_value & 1)
2338 {
2339 // Subtracting 1 from the address effectively unsets
2340 // the low order bit, which results in the address
2341 // actually pointing to the beginning of the symbol.
2342 // This delta will be used below in conjunction with
2343 // symbol.st_value to produce the final symbol_value
2344 // that we store in the symtab.
2345 symbol_value_offset = -1;
Aidan Dodds5f2d0c32015-05-28 15:37:01 +00002346 m_address_class_map[symbol.st_value^1] = eAddressClassCodeAlternateISA;
2347 }
2348 else
2349 {
2350 // This address is ARM
2351 m_address_class_map[symbol.st_value] = eAddressClassCode;
2352 }
Tamas Berghammer83544cf2015-04-07 10:43:50 +00002353 }
Todd Fialafbd703a2014-09-15 22:33:39 +00002354 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002355
2356 /*
2357 * MIPS:
2358 * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for MIPS).
2359 * This allows processer to switch between microMIPS and MIPS without any need
2360 * for special mode-control register. However, apart from .debug_line, none of
2361 * the ELF/DWARF sections set the ISA bit (for symbol or section). Use st_other
2362 * flag to check whether the symbol is microMIPS and then set the address class
2363 * accordingly.
2364 */
2365 const llvm::Triple::ArchType llvm_arch = arch.GetMachine();
2366 if (llvm_arch == llvm::Triple::mips || llvm_arch == llvm::Triple::mipsel
2367 || llvm_arch == llvm::Triple::mips64 || llvm_arch == llvm::Triple::mips64el)
2368 {
2369 if (IS_MICROMIPS(symbol.st_other))
2370 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA;
2371 else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode))
2372 {
2373 symbol.st_value = symbol.st_value & (~1ull);
2374 m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA;
2375 }
2376 else
2377 {
2378 if (symbol_type == eSymbolTypeCode)
2379 m_address_class_map[symbol.st_value] = eAddressClassCode;
2380 else if (symbol_type == eSymbolTypeData)
2381 m_address_class_map[symbol.st_value] = eAddressClassData;
2382 else
2383 m_address_class_map[symbol.st_value] = eAddressClassUnknown;
2384 }
2385 }
Todd Fiala1a088662014-09-15 16:27:44 +00002386 }
2387
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002388 // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB symbols. See above for
2389 // more details.
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002390 uint64_t symbol_value = symbol.st_value + symbol_value_offset;
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002391
2392 if (symbol_section_sp == nullptr && section_idx == SHN_ABS && symbol.st_size != 0)
2393 {
2394 // We don't have a section for a symbol with non-zero size. Create a new section for it
2395 // so the address range covered by the symbol is also covered by the module (represented
2396 // through the section list). It is needed so module lookup for the addresses covered
2397 // by this symbol will be successfull. This case happens for absolute symbols.
2398 ConstString fake_section_name(std::string(".absolute.") + symbol_name);
2399 symbol_section_sp = std::make_shared<Section>(module_sp,
2400 this,
2401 SHN_ABS,
2402 fake_section_name,
2403 eSectionTypeAbsoluteAddress,
2404 symbol_value,
2405 symbol.st_size,
2406 0, 0, 0,
2407 SHF_ALLOC);
2408
2409 module_section_list->AddSection(symbol_section_sp);
2410 section_list->AddSection(symbol_section_sp);
2411 }
2412
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002413 if (symbol_section_sp && CalculateType() != ObjectFile::Type::eTypeObjectFile)
2414 symbol_value -= symbol_section_sp->GetFileAddress();
2415
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002416 if (symbol_section_sp && module_section_list && module_section_list != section_list)
Michael Sartaina7499c92013-07-01 19:45:50 +00002417 {
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00002418 const ConstString &sect_name = symbol_section_sp->GetName();
2419 auto section_it = section_name_to_section.find(sect_name.GetCString());
2420 if (section_it == section_name_to_section.end())
2421 section_it = section_name_to_section.emplace(
2422 sect_name.GetCString(),
2423 module_section_list->FindSectionByName (sect_name)).first;
2424 if (section_it->second && section_it->second->GetFileSize())
2425 symbol_section_sp = section_it->second;
Michael Sartaina7499c92013-07-01 19:45:50 +00002426 }
2427
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002428 bool is_global = symbol.getBinding() == STB_GLOBAL;
Todd Fialafbd703a2014-09-15 22:33:39 +00002429 uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags;
Greg Clayton47037bc2012-03-27 02:40:46 +00002430 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Todd Fialafbd703a2014-09-15 22:33:39 +00002431
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002432 llvm::StringRef symbol_ref(symbol_name);
2433
2434 // Symbol names may contain @VERSION suffixes. Find those and strip them temporarily.
2435 size_t version_pos = symbol_ref.find('@');
2436 bool has_suffix = version_pos != llvm::StringRef::npos;
2437 llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos);
2438 Mangled mangled(ConstString(symbol_bare), is_mangled);
2439
2440 // Now append the suffix back to mangled and unmangled names. Only do it if the
Bruce Mitchenere171da52015-07-22 00:16:02 +00002441 // demangling was successful (string is not empty).
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002442 if (has_suffix)
2443 {
2444 llvm::StringRef suffix = symbol_ref.substr(version_pos);
2445
2446 llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef();
2447 if (! mangled_name.empty())
2448 mangled.SetMangledName( ConstString((mangled_name + suffix).str()) );
2449
Greg Claytonddaf6a72015-07-08 22:32:23 +00002450 ConstString demangled = mangled.GetDemangledName(lldb::eLanguageTypeUnknown);
2451 llvm::StringRef demangled_name = demangled.GetStringRef();
2452 if (!demangled_name.empty())
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002453 mangled.SetDemangledName( ConstString((demangled_name + suffix).str()) );
2454 }
2455
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002456 // In ELF all symbol should have a valid size but it is not true for some function symbols
2457 // coming from hand written assembly. As none of the function symbol should have 0 size we
2458 // try to calculate the size for these symbols in the symtab with saying that their original
Tamas Berghammer8c6996f72016-01-19 10:24:51 +00002459 // size is not valid.
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002460 bool symbol_size_valid = symbol.st_size != 0 || symbol.getType() != STT_FUNC;
Tamas Berghammer8c6996f72016-01-19 10:24:51 +00002461
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002462 Symbol dc_symbol(
Greg Claytone72dfb32012-02-24 01:59:29 +00002463 i + start_id, // ID is the original symbol table index.
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002464 mangled,
Greg Claytone72dfb32012-02-24 01:59:29 +00002465 symbol_type, // Type of this symbol
2466 is_global, // Is this globally visible?
2467 false, // Is this symbol debug info?
2468 false, // Is this symbol a trampoline?
2469 false, // Is this symbol artificial?
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002470 AddressRange(
2471 symbol_section_sp, // Section in which this symbol is defined or null.
2472 symbol_value, // Offset in section or symbol value.
2473 symbol.st_size), // Size in bytes of this symbol.
Tamas Berghammer8c6996f72016-01-19 10:24:51 +00002474 symbol_size_valid, // Symbol size is valid
Tamas Berghammer44ff9cce2015-06-24 11:27:32 +00002475 has_suffix, // Contains linker annotations?
2476 flags); // Symbol flags.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002477 symtab->AddSymbol(dc_symbol);
2478 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00002479 return i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002480}
2481
Stephen Wilson499b40e2011-03-30 16:07:05 +00002482unsigned
Tamas Berghammer8c6996f72016-01-19 10:24:51 +00002483ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
2484 user_id_t start_id,
2485 lldb_private::Section *symtab)
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002486{
Greg Clayton3046e662013-07-10 01:23:25 +00002487 if (symtab->GetObjectFile() != this)
2488 {
2489 // If the symbol table section is owned by a different object file, have it do the
2490 // parsing.
2491 ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile());
2492 return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab);
2493 }
2494
2495 // Get section list for this object file.
2496 SectionList *section_list = m_sections_ap.get();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002497 if (!section_list)
Stephen Wilson499b40e2011-03-30 16:07:05 +00002498 return 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002499
Greg Clayton3046e662013-07-10 01:23:25 +00002500 user_id_t symtab_id = symtab->GetID();
2501 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
Ed Maste81b4c5f2016-01-04 01:43:47 +00002502 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
Michael Sartaina7499c92013-07-01 19:45:50 +00002503 symtab_hdr->sh_type == SHT_DYNSYM);
2504
Greg Clayton3046e662013-07-10 01:23:25 +00002505 // sh_link: section header index of associated string table.
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002506 // Section ID's are ones based.
Stephen Wilson499b40e2011-03-30 16:07:05 +00002507 user_id_t strtab_id = symtab_hdr->sh_link + 1;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002508 Section *strtab = section_list->FindSectionByID(strtab_id).get();
Greg Clayton3046e662013-07-10 01:23:25 +00002509
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002510 if (symtab && strtab)
2511 {
Greg Clayton3046e662013-07-10 01:23:25 +00002512 assert (symtab->GetObjectFile() == this);
2513 assert (strtab->GetObjectFile() == this);
2514
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002515 DataExtractor symtab_data;
2516 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00002517 if (ReadSectionData(symtab, symtab_data) &&
2518 ReadSectionData(strtab, strtab_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002519 {
Greg Clayton3046e662013-07-10 01:23:25 +00002520 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
2521
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +00002522 return ParseSymbols(symbol_table, start_id, section_list,
2523 num_symbols, symtab_data, strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002524 }
2525 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00002526
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +00002527 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002528}
2529
2530size_t
2531ObjectFileELF::ParseDynamicSymbols()
2532{
2533 if (m_dynamic_symbols.size())
2534 return m_dynamic_symbols.size();
2535
Stephen Wilson499b40e2011-03-30 16:07:05 +00002536 SectionList *section_list = GetSectionList();
2537 if (!section_list)
Bill Wendlinged24dcc2012-04-03 07:50:11 +00002538 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002539
Greg Clayton3046e662013-07-10 01:23:25 +00002540 // Find the SHT_DYNAMIC section.
2541 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
Stephen Wilson499b40e2011-03-30 16:07:05 +00002542 if (!dynsym)
Bill Wendlinged24dcc2012-04-03 07:50:11 +00002543 return 0;
Greg Clayton3046e662013-07-10 01:23:25 +00002544 assert (dynsym->GetObjectFile() == this);
Stephen Wilson499b40e2011-03-30 16:07:05 +00002545
2546 ELFDynamic symbol;
2547 DataExtractor dynsym_data;
Greg Claytonc9660542012-02-05 02:38:54 +00002548 if (ReadSectionData(dynsym, dynsym_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00002549 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002550 const lldb::offset_t section_size = dynsym_data.GetByteSize();
2551 lldb::offset_t cursor = 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002552
2553 while (cursor < section_size)
2554 {
Stephen Wilson499b40e2011-03-30 16:07:05 +00002555 if (!symbol.Parse(dynsym_data, &cursor))
2556 break;
2557
2558 m_dynamic_symbols.push_back(symbol);
2559 }
2560 }
2561
2562 return m_dynamic_symbols.size();
2563}
2564
2565const ELFDynamic *
2566ObjectFileELF::FindDynamicSymbol(unsigned tag)
2567{
2568 if (!ParseDynamicSymbols())
2569 return NULL;
2570
Stephen Wilson499b40e2011-03-30 16:07:05 +00002571 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
2572 DynamicSymbolCollIter E = m_dynamic_symbols.end();
2573 for ( ; I != E; ++I)
2574 {
2575 ELFDynamic *symbol = &*I;
2576
2577 if (symbol->d_tag == tag)
2578 return symbol;
2579 }
2580
2581 return NULL;
2582}
2583
Stephen Wilson499b40e2011-03-30 16:07:05 +00002584unsigned
2585ObjectFileELF::PLTRelocationType()
2586{
Michael Sartainf7899542013-08-22 21:25:53 +00002587 // DT_PLTREL
2588 // This member specifies the type of relocation entry to which the
2589 // procedure linkage table refers. The d_val member holds DT_REL or
2590 // DT_RELA, as appropriate. All relocations in a procedure linkage table
2591 // must use the same relocation.
Stephen Wilson499b40e2011-03-30 16:07:05 +00002592 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
2593
2594 if (symbol)
2595 return symbol->d_val;
2596
2597 return 0;
2598}
2599
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002600// 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 +00002601// 0th entry in the plt table is usually a resolution entry which have different size in some
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002602// architectures then the rest of the plt entries.
2603static std::pair<uint64_t, uint64_t>
2604GetPltEntrySizeAndOffset(const ELFSectionHeader* rel_hdr, const ELFSectionHeader* plt_hdr)
2605{
2606 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2607
2608 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 bytes.
2609 // So round the entsize up by the alignment if addralign is set.
2610 elf_xword plt_entsize = plt_hdr->sh_addralign ?
Rafael Espindolaa94ae1e2016-01-18 20:57:54 +00002611 llvm::alignTo (plt_hdr->sh_entsize, plt_hdr->sh_addralign) : plt_hdr->sh_entsize;
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002612
2613 if (plt_entsize == 0)
2614 {
2615 // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the size of the plt
2616 // entries based on the number of entries and the size of the plt section with the
Bruce Mitchenere171da52015-07-22 00:16:02 +00002617 // assumption that the size of the 0th entry is at least as big as the size of the normal
2618 // entries and it isn't much bigger then that.
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002619 if (plt_hdr->sh_addralign)
2620 plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign / (num_relocations + 1) * plt_hdr->sh_addralign;
2621 else
2622 plt_entsize = plt_hdr->sh_size / (num_relocations + 1);
2623 }
2624
2625 elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize;
2626
2627 return std::make_pair(plt_entsize, plt_offset);
2628}
2629
Stephen Wilson499b40e2011-03-30 16:07:05 +00002630static unsigned
2631ParsePLTRelocations(Symtab *symbol_table,
2632 user_id_t start_id,
2633 unsigned rel_type,
2634 const ELFHeader *hdr,
2635 const ELFSectionHeader *rel_hdr,
2636 const ELFSectionHeader *plt_hdr,
2637 const ELFSectionHeader *sym_hdr,
Greg Claytone72dfb32012-02-24 01:59:29 +00002638 const lldb::SectionSP &plt_section_sp,
Stephen Wilson499b40e2011-03-30 16:07:05 +00002639 DataExtractor &rel_data,
2640 DataExtractor &symtab_data,
2641 DataExtractor &strtab_data)
2642{
2643 ELFRelocation rel(rel_type);
2644 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00002645 lldb::offset_t offset = 0;
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002646
2647 uint64_t plt_offset, plt_entsize;
2648 std::tie(plt_entsize, plt_offset) = GetPltEntrySizeAndOffset(rel_hdr, plt_hdr);
Greg Claytonc7bece562013-01-25 18:06:21 +00002649 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002650
2651 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2652 reloc_info_fn reloc_type;
2653 reloc_info_fn reloc_symbol;
2654
Greg Claytond091afe2012-11-12 22:53:16 +00002655 if (hdr->Is32Bit())
Stephen Wilson499b40e2011-03-30 16:07:05 +00002656 {
2657 reloc_type = ELFRelocation::RelocType32;
2658 reloc_symbol = ELFRelocation::RelocSymbol32;
2659 }
2660 else
2661 {
2662 reloc_type = ELFRelocation::RelocType64;
2663 reloc_symbol = ELFRelocation::RelocSymbol64;
2664 }
2665
2666 unsigned slot_type = hdr->GetRelocationJumpSlotType();
2667 unsigned i;
2668 for (i = 0; i < num_relocations; ++i)
2669 {
2670 if (rel.Parse(rel_data, &offset) == false)
2671 break;
2672
2673 if (reloc_type(rel) != slot_type)
2674 continue;
2675
Greg Claytonc7bece562013-01-25 18:06:21 +00002676 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002677 if (!symbol.Parse(symtab_data, &symbol_offset))
2678 break;
2679
2680 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Greg Clayton47037bc2012-03-27 02:40:46 +00002681 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002682 uint64_t plt_index = plt_offset + i * plt_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002683
2684 Symbol jump_symbol(
2685 i + start_id, // Symbol table index
2686 symbol_name, // symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +00002687 is_mangled, // is the symbol name mangled?
Stephen Wilson499b40e2011-03-30 16:07:05 +00002688 eSymbolTypeTrampoline, // Type of this symbol
2689 false, // Is this globally visible?
2690 false, // Is this symbol debug info?
2691 true, // Is this symbol a trampoline?
2692 true, // Is this symbol artificial?
Greg Claytone72dfb32012-02-24 01:59:29 +00002693 plt_section_sp, // Section in which this symbol is defined or null.
Stephen Wilson499b40e2011-03-30 16:07:05 +00002694 plt_index, // Offset in section or symbol value.
2695 plt_entsize, // Size in bytes of this symbol.
Greg Clayton9594f4c2013-04-13 23:17:23 +00002696 true, // Size is valid
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00002697 false, // Contains linker annotations?
Stephen Wilson499b40e2011-03-30 16:07:05 +00002698 0); // Symbol flags.
2699
2700 symbol_table->AddSymbol(jump_symbol);
2701 }
2702
2703 return i;
2704}
2705
2706unsigned
2707ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
2708 user_id_t start_id,
Michael Sartaina7499c92013-07-01 19:45:50 +00002709 const ELFSectionHeaderInfo *rel_hdr,
Stephen Wilson499b40e2011-03-30 16:07:05 +00002710 user_id_t rel_id)
2711{
2712 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2713
Ulrich Weigandbd526262016-04-14 14:36:29 +00002714 // The link field points to the associated symbol table.
Stephen Wilson499b40e2011-03-30 16:07:05 +00002715 user_id_t symtab_id = rel_hdr->sh_link;
Stephen Wilson499b40e2011-03-30 16:07:05 +00002716
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002717 // If the link field doesn't point to the appropriate symbol name table then
2718 // try to find it by name as some compiler don't fill in the link fields.
2719 if (!symtab_id)
2720 symtab_id = GetSectionIndexByName(".dynsym");
Ulrich Weigandbd526262016-04-14 14:36:29 +00002721
2722 // Get PLT section. We cannot use rel_hdr->sh_info, since current linkers
2723 // point that to the .got.plt or .got section instead of .plt.
2724 user_id_t plt_id = GetSectionIndexByName(".plt");
Tamas Berghammer85fadd92015-05-08 09:40:05 +00002725
Stephen Wilson499b40e2011-03-30 16:07:05 +00002726 if (!symtab_id || !plt_id)
2727 return 0;
2728
2729 // Section ID's are ones based;
2730 symtab_id++;
2731 plt_id++;
2732
Michael Sartaina7499c92013-07-01 19:45:50 +00002733 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +00002734 if (!plt_hdr)
2735 return 0;
2736
Michael Sartaina7499c92013-07-01 19:45:50 +00002737 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +00002738 if (!sym_hdr)
2739 return 0;
2740
Greg Clayton3046e662013-07-10 01:23:25 +00002741 SectionList *section_list = m_sections_ap.get();
Stephen Wilson499b40e2011-03-30 16:07:05 +00002742 if (!section_list)
2743 return 0;
2744
2745 Section *rel_section = section_list->FindSectionByID(rel_id).get();
2746 if (!rel_section)
2747 return 0;
2748
Greg Claytone72dfb32012-02-24 01:59:29 +00002749 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
2750 if (!plt_section_sp)
Stephen Wilson499b40e2011-03-30 16:07:05 +00002751 return 0;
2752
2753 Section *symtab = section_list->FindSectionByID(symtab_id).get();
2754 if (!symtab)
2755 return 0;
2756
Greg Clayton3046e662013-07-10 01:23:25 +00002757 // sh_link points to associated string table.
Stephen Wilson499b40e2011-03-30 16:07:05 +00002758 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
2759 if (!strtab)
2760 return 0;
2761
2762 DataExtractor rel_data;
Greg Claytonc9660542012-02-05 02:38:54 +00002763 if (!ReadSectionData(rel_section, rel_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00002764 return 0;
2765
2766 DataExtractor symtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00002767 if (!ReadSectionData(symtab, symtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00002768 return 0;
2769
2770 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00002771 if (!ReadSectionData(strtab, strtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00002772 return 0;
2773
2774 unsigned rel_type = PLTRelocationType();
2775 if (!rel_type)
2776 return 0;
2777
Ed Maste81b4c5f2016-01-04 01:43:47 +00002778 return ParsePLTRelocations (symbol_table,
2779 start_id,
Greg Claytone72dfb32012-02-24 01:59:29 +00002780 rel_type,
Ed Maste81b4c5f2016-01-04 01:43:47 +00002781 &m_header,
2782 rel_hdr,
2783 plt_hdr,
Greg Claytone72dfb32012-02-24 01:59:29 +00002784 sym_hdr,
Ed Maste81b4c5f2016-01-04 01:43:47 +00002785 plt_section_sp,
2786 rel_data,
2787 symtab_data,
Greg Claytone72dfb32012-02-24 01:59:29 +00002788 strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002789}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002790
Andrew MacPherson17220c12014-03-05 10:12:43 +00002791unsigned
2792ObjectFileELF::RelocateSection(Symtab* symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
2793 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,
2794 DataExtractor &rel_data, DataExtractor &symtab_data,
2795 DataExtractor &debug_data, Section* rel_section)
2796{
2797 ELFRelocation rel(rel_hdr->sh_type);
2798 lldb::addr_t offset = 0;
2799 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2800 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2801 reloc_info_fn reloc_type;
2802 reloc_info_fn reloc_symbol;
2803
2804 if (hdr->Is32Bit())
2805 {
2806 reloc_type = ELFRelocation::RelocType32;
2807 reloc_symbol = ELFRelocation::RelocSymbol32;
2808 }
2809 else
2810 {
2811 reloc_type = ELFRelocation::RelocType64;
2812 reloc_symbol = ELFRelocation::RelocSymbol64;
2813 }
2814
2815 for (unsigned i = 0; i < num_relocations; ++i)
2816 {
2817 if (rel.Parse(rel_data, &offset) == false)
2818 break;
2819
2820 Symbol* symbol = NULL;
2821
2822 if (hdr->Is32Bit())
2823 {
2824 switch (reloc_type(rel)) {
2825 case R_386_32:
2826 case R_386_PC32:
2827 default:
2828 assert(false && "unexpected relocation type");
2829 }
2830 } else {
2831 switch (reloc_type(rel)) {
2832 case R_X86_64_64:
2833 {
2834 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
2835 if (symbol)
2836 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00002837 addr_t value = symbol->GetAddressRef().GetFileAddress();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002838 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer();
2839 uint64_t* dst = reinterpret_cast<uint64_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset64(rel));
2840 *dst = value + ELFRelocation::RelocAddend64(rel);
2841 }
2842 break;
2843 }
2844 case R_X86_64_32:
2845 case R_X86_64_32S:
2846 {
2847 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
2848 if (symbol)
2849 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00002850 addr_t value = symbol->GetAddressRef().GetFileAddress();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002851 value += ELFRelocation::RelocAddend32(rel);
2852 assert((reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) ||
2853 (reloc_type(rel) == R_X86_64_32S &&
2854 ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN)));
2855 uint32_t truncated_addr = (value & 0xFFFFFFFF);
2856 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer();
2857 uint32_t* dst = reinterpret_cast<uint32_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel));
2858 *dst = truncated_addr;
2859 }
2860 break;
2861 }
2862 case R_X86_64_PC32:
2863 default:
2864 assert(false && "unexpected relocation type");
2865 }
2866 }
2867 }
2868
2869 return 0;
2870}
2871
2872unsigned
2873ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, user_id_t rel_id)
2874{
2875 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2876
2877 // Parse in the section list if needed.
2878 SectionList *section_list = GetSectionList();
2879 if (!section_list)
2880 return 0;
2881
2882 // Section ID's are ones based.
2883 user_id_t symtab_id = rel_hdr->sh_link + 1;
2884 user_id_t debug_id = rel_hdr->sh_info + 1;
2885
2886 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
2887 if (!symtab_hdr)
2888 return 0;
2889
2890 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);
2891 if (!debug_hdr)
2892 return 0;
2893
2894 Section *rel = section_list->FindSectionByID(rel_id).get();
2895 if (!rel)
2896 return 0;
2897
2898 Section *symtab = section_list->FindSectionByID(symtab_id).get();
2899 if (!symtab)
2900 return 0;
2901
2902 Section *debug = section_list->FindSectionByID(debug_id).get();
2903 if (!debug)
2904 return 0;
2905
2906 DataExtractor rel_data;
2907 DataExtractor symtab_data;
2908 DataExtractor debug_data;
2909
2910 if (ReadSectionData(rel, rel_data) &&
2911 ReadSectionData(symtab, symtab_data) &&
2912 ReadSectionData(debug, debug_data))
2913 {
2914 RelocateSection(m_symtab_ap.get(), &m_header, rel_hdr, symtab_hdr, debug_hdr,
2915 rel_data, symtab_data, debug_data, debug);
2916 }
2917
2918 return 0;
2919}
2920
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002921Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +00002922ObjectFileELF::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002923{
Michael Sartaina7499c92013-07-01 19:45:50 +00002924 ModuleSP module_sp(GetModule());
Greg Clayton3046e662013-07-10 01:23:25 +00002925 if (!module_sp)
2926 return NULL;
Michael Sartaina7499c92013-07-01 19:45:50 +00002927
Greg Clayton3046e662013-07-10 01:23:25 +00002928 // We always want to use the main object file so we (hopefully) only have one cached copy
2929 // of our symtab, dynamic sections, etc.
2930 ObjectFile *module_obj_file = module_sp->GetObjectFile();
2931 if (module_obj_file && module_obj_file != this)
2932 return module_obj_file->GetSymtab();
2933
2934 if (m_symtab_ap.get() == NULL)
2935 {
Pavel Labath783cbdc2015-03-04 10:28:15 +00002936 SectionList *section_list = module_sp->GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00002937 if (!section_list)
2938 return NULL;
2939
Greg Clayton3046e662013-07-10 01:23:25 +00002940 uint64_t symbol_id = 0;
2941 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Stephen Wilson499b40e2011-03-30 16:07:05 +00002942
Michael Sartaina7499c92013-07-01 19:45:50 +00002943 // Sharable objects and dynamic executables usually have 2 distinct symbol
2944 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller
2945 // version of the symtab that only contains global symbols. The information found
2946 // in the dynsym is therefore also found in the symtab, while the reverse is not
2947 // necessarily true.
Greg Clayton3046e662013-07-10 01:23:25 +00002948 Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get();
2949 if (!symtab)
Michael Sartaina7499c92013-07-01 19:45:50 +00002950 {
2951 // The symtab section is non-allocable and can be stripped, so if it doesn't exist
2952 // then use the dynsym section which should always be there.
Greg Clayton3046e662013-07-10 01:23:25 +00002953 symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get();
Michael Sartaina7499c92013-07-01 19:45:50 +00002954 }
Greg Clayton3046e662013-07-10 01:23:25 +00002955 if (symtab)
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002956 {
2957 m_symtab_ap.reset(new Symtab(symtab->GetObjectFile()));
Greg Clayton3046e662013-07-10 01:23:25 +00002958 symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab);
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002959 }
Michael Sartaina7499c92013-07-01 19:45:50 +00002960
Michael Sartainf7899542013-08-22 21:25:53 +00002961 // DT_JMPREL
2962 // If present, this entry's d_ptr member holds the address of relocation
2963 // entries associated solely with the procedure linkage table. Separating
2964 // these relocation entries lets the dynamic linker ignore them during
2965 // process initialization, if lazy binding is enabled. If this entry is
2966 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must
2967 // also be present.
Greg Clayton3046e662013-07-10 01:23:25 +00002968 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
2969 if (symbol)
Michael Sartaina7499c92013-07-01 19:45:50 +00002970 {
Michael Sartainf7899542013-08-22 21:25:53 +00002971 // Synthesize trampoline symbols to help navigate the PLT.
Greg Clayton3046e662013-07-10 01:23:25 +00002972 addr_t addr = symbol->d_ptr;
2973 Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get();
Ed Maste81b4c5f2016-01-04 01:43:47 +00002974 if (reloc_section)
Michael Sartaina7499c92013-07-01 19:45:50 +00002975 {
Greg Clayton3046e662013-07-10 01:23:25 +00002976 user_id_t reloc_id = reloc_section->GetID();
2977 const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id);
2978 assert(reloc_header);
Ed Maste81b4c5f2016-01-04 01:43:47 +00002979
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002980 if (m_symtab_ap == nullptr)
2981 m_symtab_ap.reset(new Symtab(reloc_section->GetObjectFile()));
Michael Sartaina7499c92013-07-01 19:45:50 +00002982
Greg Clayton3046e662013-07-10 01:23:25 +00002983 ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id);
Michael Sartaina7499c92013-07-01 19:45:50 +00002984 }
2985 }
Ed Maste81b4c5f2016-01-04 01:43:47 +00002986
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002987 DWARFCallFrameInfo* eh_frame = GetUnwindTable().GetEHFrameInfo();
2988 if (eh_frame)
2989 {
2990 if (m_symtab_ap == nullptr)
2991 m_symtab_ap.reset(new Symtab(this));
2992 ParseUnwindSymbols (m_symtab_ap.get(), eh_frame);
2993 }
2994
Tamas Berghammerd00438e2015-07-30 12:38:18 +00002995 // If we still don't have any symtab then create an empty instance to avoid do the section
2996 // lookup next time.
2997 if (m_symtab_ap == nullptr)
2998 m_symtab_ap.reset(new Symtab(this));
Ed Maste81b4c5f2016-01-04 01:43:47 +00002999
Tamas Berghammer2480fe02015-06-24 12:31:25 +00003000 m_symtab_ap->CalculateSymbolSizes();
Michael Sartaina7499c92013-07-01 19:45:50 +00003001 }
Andrew MacPherson17220c12014-03-05 10:12:43 +00003002
3003 for (SectionHeaderCollIter I = m_section_headers.begin();
3004 I != m_section_headers.end(); ++I)
3005 {
3006 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL)
3007 {
3008 if (CalculateType() == eTypeObjectFile)
3009 {
3010 const char *section_name = I->section_name.AsCString("");
3011 if (strstr(section_name, ".rela.debug") ||
3012 strstr(section_name, ".rel.debug"))
3013 {
3014 const ELFSectionHeader &reloc_header = *I;
3015 user_id_t reloc_id = SectionIndex(I);
3016 RelocateDebugSections(&reloc_header, reloc_id);
3017 }
3018 }
3019 }
3020 }
Greg Clayton3046e662013-07-10 01:23:25 +00003021 return m_symtab_ap.get();
3022}
3023
Tamas Berghammer6b63b142016-02-18 11:12:18 +00003024void
3025ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table, DWARFCallFrameInfo* eh_frame)
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00003026{
Tamas Berghammer6b63b142016-02-18 11:12:18 +00003027 SectionList* section_list = GetSectionList();
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00003028 if (!section_list)
Tamas Berghammer6b63b142016-02-18 11:12:18 +00003029 return;
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00003030
Tamas Berghammer6b63b142016-02-18 11:12:18 +00003031 // First we save the new symbols into a separate list and add them to the symbol table after
3032 // we colleced all symbols we want to add. This is neccessary because adding a new symbol
3033 // invalidates the internal index of the symtab what causing the next lookup to be slow because
3034 // it have to recalculate the index first.
3035 std::vector<Symbol> new_symbols;
3036
3037 eh_frame->ForEachFDEEntries(
3038 [this, symbol_table, section_list, &new_symbols](lldb::addr_t file_addr,
3039 uint32_t size,
3040 dw_offset_t) {
3041 Symbol* symbol = symbol_table->FindSymbolAtFileAddress(file_addr);
3042 if (symbol)
Tamas Berghammer5bfd4d02016-02-10 12:10:58 +00003043 {
Tamas Berghammer6b63b142016-02-18 11:12:18 +00003044 if (!symbol->GetByteSizeIsValid())
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00003045 {
Tamas Berghammer6b63b142016-02-18 11:12:18 +00003046 symbol->SetByteSize(size);
3047 symbol->SetSizeIsSynthesized(true);
Tamas Berghammered844cb2016-02-10 10:43:34 +00003048 }
3049 }
Tamas Berghammer6b63b142016-02-18 11:12:18 +00003050 else
3051 {
3052 SectionSP section_sp = section_list->FindSectionContainingFileAddress(file_addr);
3053 if (section_sp)
3054 {
3055 addr_t offset = file_addr - section_sp->GetFileAddress();
3056 const char* symbol_name = GetNextSyntheticSymbolName().GetCString();
3057 uint64_t symbol_id = symbol_table->GetNumSymbols();
3058 Symbol eh_symbol(
3059 symbol_id, // Symbol table index.
3060 symbol_name, // Symbol name.
3061 false, // Is the symbol name mangled?
3062 eSymbolTypeCode, // Type of this symbol.
3063 true, // Is this globally visible?
3064 false, // Is this symbol debug info?
3065 false, // Is this symbol a trampoline?
3066 true, // Is this symbol artificial?
3067 section_sp, // Section in which this symbol is defined or null.
3068 offset, // Offset in section or symbol value.
3069 0, // Size: Don't specify the size as an FDE can
3070 false, // Size is valid: cover multiple symbols.
3071 false, // Contains linker annotations?
3072 0); // Symbol flags.
3073 new_symbols.push_back(eh_symbol);
3074 }
3075 }
3076 return true;
3077 });
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00003078
Tamas Berghammer6b63b142016-02-18 11:12:18 +00003079 for (const Symbol& s : new_symbols)
3080 symbol_table->AddSymbol(s);
3081}
Tamas Berghammer5bfd4d02016-02-10 12:10:58 +00003082
Greg Clayton3046e662013-07-10 01:23:25 +00003083bool
3084ObjectFileELF::IsStripped ()
3085{
3086 // TODO: determine this for ELF
3087 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003088}
3089
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003090//===----------------------------------------------------------------------===//
3091// Dump
3092//
3093// Dump the specifics of the runtime file container (such as any headers
3094// segments, sections, etc).
3095//----------------------------------------------------------------------
3096void
3097ObjectFileELF::Dump(Stream *s)
3098{
Adrian McCarthy543725c2016-04-04 21:21:49 +00003099 ModuleSP module_sp(GetModule());
3100 if (!module_sp)
3101 {
3102 return;
3103 }
3104
3105 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3106 s->Printf("%p: ", static_cast<void *>(this));
3107 s->Indent();
3108 s->PutCString("ObjectFileELF");
3109
3110 ArchSpec header_arch;
3111 GetArchitecture(header_arch);
3112
3113 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
3114
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003115 DumpELFHeader(s, m_header);
3116 s->EOL();
3117 DumpELFProgramHeaders(s);
3118 s->EOL();
3119 DumpELFSectionHeaders(s);
3120 s->EOL();
3121 SectionList *section_list = GetSectionList();
3122 if (section_list)
Greg Clayton10177aa2010-12-08 05:08:21 +00003123 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003124 Symtab *symtab = GetSymtab();
3125 if (symtab)
Greg Claytone0d378b2011-03-24 21:19:54 +00003126 symtab->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003127 s->EOL();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003128 DumpDependentModules(s);
3129 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003130}
3131
3132//----------------------------------------------------------------------
3133// DumpELFHeader
3134//
3135// Dump the ELF header to the specified output stream
3136//----------------------------------------------------------------------
3137void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003138ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003139{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003140 s->PutCString("ELF Header\n");
3141 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
3142 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
3143 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
3144 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
3145 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
3146 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
3147 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003148
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003149 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
3150 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003151 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
3152 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
3153 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
3154
3155 s->Printf("e_type = 0x%4.4x ", header.e_type);
3156 DumpELFHeader_e_type(s, header.e_type);
3157 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
3158 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Daniel Malead01b2952012-11-29 21:49:15 +00003159 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
3160 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
3161 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003162 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
3163 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
3164 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
3165 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
3166 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
3167 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
3168 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
3169}
3170
3171//----------------------------------------------------------------------
3172// DumpELFHeader_e_type
3173//
3174// Dump an token value for the ELF header member e_type
3175//----------------------------------------------------------------------
3176void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003177ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003178{
3179 switch (e_type)
3180 {
3181 case ET_NONE: *s << "ET_NONE"; break;
3182 case ET_REL: *s << "ET_REL"; break;
3183 case ET_EXEC: *s << "ET_EXEC"; break;
3184 case ET_DYN: *s << "ET_DYN"; break;
3185 case ET_CORE: *s << "ET_CORE"; break;
3186 default:
3187 break;
3188 }
3189}
3190
3191//----------------------------------------------------------------------
3192// DumpELFHeader_e_ident_EI_DATA
3193//
3194// Dump an token value for the ELF header member e_ident[EI_DATA]
3195//----------------------------------------------------------------------
3196void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003197ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003198{
3199 switch (ei_data)
3200 {
3201 case ELFDATANONE: *s << "ELFDATANONE"; break;
3202 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
3203 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
3204 default:
3205 break;
3206 }
3207}
3208
3209
3210//----------------------------------------------------------------------
3211// DumpELFProgramHeader
3212//
3213// Dump a single ELF program header to the specified output stream
3214//----------------------------------------------------------------------
3215void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003216ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003217{
3218 DumpELFProgramHeader_p_type(s, ph.p_type);
Daniel Malead01b2952012-11-29 21:49:15 +00003219 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr);
3220 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 +00003221
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003222 DumpELFProgramHeader_p_flags(s, ph.p_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00003223 s->Printf(") %8.8" PRIx64, ph.p_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003224}
3225
3226//----------------------------------------------------------------------
3227// DumpELFProgramHeader_p_type
3228//
3229// Dump an token value for the ELF program header member p_type which
3230// describes the type of the program header
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003231// ----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003232void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003233ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003234{
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00003235 const int kStrWidth = 15;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003236 switch (p_type)
3237 {
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00003238 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
3239 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
3240 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
3241 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
3242 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
3243 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
3244 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
3245 CASE_AND_STREAM(s, PT_TLS , kStrWidth);
3246 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003247 default:
3248 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
3249 break;
3250 }
3251}
3252
3253
3254//----------------------------------------------------------------------
3255// DumpELFProgramHeader_p_flags
3256//
3257// Dump an token value for the ELF program header member p_flags
3258//----------------------------------------------------------------------
3259void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003260ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003261{
3262 *s << ((p_flags & PF_X) ? "PF_X" : " ")
3263 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
3264 << ((p_flags & PF_W) ? "PF_W" : " ")
3265 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
3266 << ((p_flags & PF_R) ? "PF_R" : " ");
3267}
3268
3269//----------------------------------------------------------------------
3270// DumpELFProgramHeaders
3271//
3272// Dump all of the ELF program header to the specified output stream
3273//----------------------------------------------------------------------
3274void
3275ObjectFileELF::DumpELFProgramHeaders(Stream *s)
3276{
Ed Maste3a8ab6e2015-02-23 15:33:11 +00003277 if (!ParseProgramHeaders())
3278 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003279
Ed Maste3a8ab6e2015-02-23 15:33:11 +00003280 s->PutCString("Program Headers\n");
3281 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
3282 "p_filesz p_memsz p_flags p_align\n");
3283 s->PutCString("==== --------------- -------- -------- -------- "
3284 "-------- -------- ------------------------- --------\n");
3285
3286 uint32_t idx = 0;
3287 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
3288 I != m_program_headers.end(); ++I, ++idx)
3289 {
3290 s->Printf("[%2u] ", idx);
3291 ObjectFileELF::DumpELFProgramHeader(s, *I);
3292 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003293 }
3294}
3295
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003296//----------------------------------------------------------------------
3297// DumpELFSectionHeader
3298//
3299// Dump a single ELF section header to the specified output stream
3300//----------------------------------------------------------------------
3301void
Michael Sartaina7499c92013-07-01 19:45:50 +00003302ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003303{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003304 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003305 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Daniel Malead01b2952012-11-29 21:49:15 +00003306 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003307 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00003308 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 +00003309 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
Daniel Malead01b2952012-11-29 21:49:15 +00003310 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003311}
3312
3313//----------------------------------------------------------------------
3314// DumpELFSectionHeader_sh_type
3315//
3316// Dump an token value for the ELF section header member sh_type which
3317// describes the type of the section
3318//----------------------------------------------------------------------
3319void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003320ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003321{
3322 const int kStrWidth = 12;
3323 switch (sh_type)
3324 {
3325 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
3326 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
3327 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
3328 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
3329 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
3330 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
3331 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
3332 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
3333 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
3334 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
3335 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
3336 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
3337 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
3338 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
3339 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
3340 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
3341 default:
3342 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
3343 break;
3344 }
3345}
3346
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003347//----------------------------------------------------------------------
3348// DumpELFSectionHeader_sh_flags
3349//
3350// Dump an token value for the ELF section header member sh_flags
3351//----------------------------------------------------------------------
3352void
Greg Claytonc7bece562013-01-25 18:06:21 +00003353ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003354{
3355 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
3356 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
3357 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
3358 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
3359 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
3360}
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003361
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003362//----------------------------------------------------------------------
3363// DumpELFSectionHeaders
3364//
3365// Dump all of the ELF section header to the specified output stream
3366//----------------------------------------------------------------------
3367void
3368ObjectFileELF::DumpELFSectionHeaders(Stream *s)
3369{
Michael Sartaina7499c92013-07-01 19:45:50 +00003370 if (!ParseSectionHeaders())
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003371 return;
3372
3373 s->PutCString("Section Headers\n");
3374 s->PutCString("IDX name type flags "
3375 "addr offset size link info addralgn "
3376 "entsize Name\n");
3377 s->PutCString("==== -------- ------------ -------------------------------- "
3378 "-------- -------- -------- -------- -------- -------- "
3379 "-------- ====================\n");
3380
3381 uint32_t idx = 0;
3382 for (SectionHeaderCollConstIter I = m_section_headers.begin();
3383 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003384 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003385 s->Printf("[%2u] ", idx);
3386 ObjectFileELF::DumpELFSectionHeader(s, *I);
Michael Sartaina7499c92013-07-01 19:45:50 +00003387 const char* section_name = I->section_name.AsCString("");
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003388 if (section_name)
3389 *s << ' ' << section_name << "\n";
3390 }
3391}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003392
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003393void
3394ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
3395{
3396 size_t num_modules = ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003397
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003398 if (num_modules > 0)
3399 {
3400 s->PutCString("Dependent Modules:\n");
3401 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003402 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003403 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
3404 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003405 }
3406 }
3407}
3408
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003409bool
Greg Clayton514487e2011-02-15 21:59:32 +00003410ObjectFileELF::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003411{
Stephen Wilson3f4200fd2011-02-24 19:16:15 +00003412 if (!ParseHeader())
3413 return false;
3414
Todd Fiala09512ec2014-07-11 15:43:51 +00003415 if (m_section_headers.empty())
3416 {
3417 // Allow elf notes to be parsed which may affect the detected architecture.
3418 ParseSectionHeaders();
3419 }
Todd Fialab91de782014-06-27 16:52:49 +00003420
Greg Claytonb704b692015-10-28 18:04:38 +00003421 if (CalculateType() == eTypeCoreFile && m_arch_spec.TripleOSIsUnspecifiedUnknown())
3422 {
3423 // Core files don't have section headers yet they have PT_NOTE program headers
3424 // that might shed more light on the architecture
3425 if (ParseProgramHeaders())
3426 {
3427 for (size_t i = 0, count = GetProgramHeaderCount(); i < count; ++i)
3428 {
3429 const elf::ELFProgramHeader* header = GetProgramHeaderByIndex(i);
3430 if (header && header->p_type == PT_NOTE && header->p_offset != 0 && header->p_filesz > 0)
3431 {
3432 DataExtractor data;
3433 if (data.SetData (m_data, header->p_offset, header->p_filesz) == header->p_filesz)
3434 {
3435 lldb_private::UUID uuid;
3436 RefineModuleDetailsFromNote (data, m_arch_spec, uuid);
3437 }
3438 }
3439 }
3440 }
3441 }
Todd Fialab91de782014-06-27 16:52:49 +00003442 arch = m_arch_spec;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003443 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003444}
3445
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003446ObjectFile::Type
3447ObjectFileELF::CalculateType()
3448{
3449 switch (m_header.e_type)
3450 {
3451 case llvm::ELF::ET_NONE:
3452 // 0 - No file type
3453 return eTypeUnknown;
3454
3455 case llvm::ELF::ET_REL:
3456 // 1 - Relocatable file
3457 return eTypeObjectFile;
3458
3459 case llvm::ELF::ET_EXEC:
3460 // 2 - Executable file
3461 return eTypeExecutable;
3462
3463 case llvm::ELF::ET_DYN:
3464 // 3 - Shared object file
3465 return eTypeSharedLibrary;
3466
3467 case ET_CORE:
3468 // 4 - Core file
3469 return eTypeCoreFile;
3470
3471 default:
3472 break;
3473 }
3474 return eTypeUnknown;
3475}
3476
3477ObjectFile::Strata
3478ObjectFileELF::CalculateStrata()
3479{
3480 switch (m_header.e_type)
3481 {
Ed Maste81b4c5f2016-01-04 01:43:47 +00003482 case llvm::ELF::ET_NONE:
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003483 // 0 - No file type
3484 return eStrataUnknown;
3485
3486 case llvm::ELF::ET_REL:
3487 // 1 - Relocatable file
3488 return eStrataUnknown;
3489
3490 case llvm::ELF::ET_EXEC:
3491 // 2 - Executable file
3492 // TODO: is there any way to detect that an executable is a kernel
Ed Maste81b4c5f2016-01-04 01:43:47 +00003493 // related executable by inspecting the program headers, section
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003494 // headers, symbols, or any other flag bits???
3495 return eStrataUser;
3496
3497 case llvm::ELF::ET_DYN:
3498 // 3 - Shared object file
3499 // TODO: is there any way to detect that an shared library is a kernel
Ed Maste81b4c5f2016-01-04 01:43:47 +00003500 // related executable by inspecting the program headers, section
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003501 // headers, symbols, or any other flag bits???
3502 return eStrataUnknown;
3503
3504 case ET_CORE:
3505 // 4 - Core file
3506 // TODO: is there any way to detect that an core file is a kernel
Ed Maste81b4c5f2016-01-04 01:43:47 +00003507 // related executable by inspecting the program headers, section
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003508 // headers, symbols, or any other flag bits???
3509 return eStrataUnknown;
3510
3511 default:
3512 break;
3513 }
3514 return eStrataUnknown;
3515}
3516