blob: 9aad296c47fd09ec9f551428d39a92ce35fe777d [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>
14
Stephen Wilson2ab0a582011-01-15 00:08:44 +000015#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/DataBuffer.h"
17#include "lldb/Core/Error.h"
Stephen Wilsonf325ba92010-07-13 23:07:23 +000018#include "lldb/Core/FileSpecList.h"
Ed Mastec113ff82013-12-02 17:49:13 +000019#include "lldb/Core/Log.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000020#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000021#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/PluginManager.h"
23#include "lldb/Core/Section.h"
24#include "lldb/Core/Stream.h"
Todd Fiala4339f3a2014-03-25 19:29:09 +000025#include "lldb/Core/Timer.h"
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +000026#include "lldb/Symbol/DWARFCallFrameInfo.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000027#include "lldb/Symbol/SymbolContext.h"
Steve Pucci9e02dac2014-02-06 19:02:19 +000028#include "lldb/Target/SectionLoadList.h"
Ed Maste54803652013-10-11 17:39:07 +000029#include "lldb/Target/Target.h"
Greg Clayton64195a22011-02-23 00:35:02 +000030#include "lldb/Host/Host.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Stephen Wilson499b40e2011-03-30 16:07:05 +000032#include "llvm/ADT/PointerUnion.h"
33
Stephen Wilsonf325ba92010-07-13 23:07:23 +000034#define CASE_AND_STREAM(s, def, width) \
35 case def: s->Printf("%-*s", width, #def); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037using namespace lldb;
38using namespace lldb_private;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000039using namespace elf;
40using namespace llvm::ELF;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041
Stephen Wilson499b40e2011-03-30 16:07:05 +000042namespace {
43//===----------------------------------------------------------------------===//
44/// @class ELFRelocation
45/// @brief Generic wrapper for ELFRel and ELFRela.
46///
47/// This helper class allows us to parse both ELFRel and ELFRela relocation
48/// entries in a generic manner.
49class ELFRelocation
50{
51public:
52
53 /// Constructs an ELFRelocation entry with a personality as given by @p
54 /// type.
55 ///
56 /// @param type Either DT_REL or DT_RELA. Any other value is invalid.
57 ELFRelocation(unsigned type);
58
59 ~ELFRelocation();
60
61 bool
Greg Claytonc7bece562013-01-25 18:06:21 +000062 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilson499b40e2011-03-30 16:07:05 +000063
64 static unsigned
65 RelocType32(const ELFRelocation &rel);
66
67 static unsigned
68 RelocType64(const ELFRelocation &rel);
69
70 static unsigned
71 RelocSymbol32(const ELFRelocation &rel);
72
73 static unsigned
74 RelocSymbol64(const ELFRelocation &rel);
75
Andrew MacPherson17220c12014-03-05 10:12:43 +000076 static unsigned
77 RelocOffset32(const ELFRelocation &rel);
78
79 static unsigned
80 RelocOffset64(const ELFRelocation &rel);
81
82 static unsigned
83 RelocAddend32(const ELFRelocation &rel);
84
85 static unsigned
86 RelocAddend64(const ELFRelocation &rel);
87
Stephen Wilson499b40e2011-03-30 16:07:05 +000088private:
89 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion;
90
91 RelocUnion reloc;
92};
93
94ELFRelocation::ELFRelocation(unsigned type)
95{
Andrew MacPherson17220c12014-03-05 10:12:43 +000096 if (type == DT_REL || type == SHT_REL)
Stephen Wilson499b40e2011-03-30 16:07:05 +000097 reloc = new ELFRel();
Andrew MacPherson17220c12014-03-05 10:12:43 +000098 else if (type == DT_RELA || type == SHT_RELA)
Stephen Wilson499b40e2011-03-30 16:07:05 +000099 reloc = new ELFRela();
100 else {
101 assert(false && "unexpected relocation type");
102 reloc = static_cast<ELFRel*>(NULL);
103 }
104}
105
106ELFRelocation::~ELFRelocation()
107{
108 if (reloc.is<ELFRel*>())
109 delete reloc.get<ELFRel*>();
110 else
111 delete reloc.get<ELFRela*>();
112}
113
114bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000115ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000116{
117 if (reloc.is<ELFRel*>())
118 return reloc.get<ELFRel*>()->Parse(data, offset);
119 else
120 return reloc.get<ELFRela*>()->Parse(data, offset);
121}
122
123unsigned
124ELFRelocation::RelocType32(const ELFRelocation &rel)
125{
126 if (rel.reloc.is<ELFRel*>())
127 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>());
128 else
129 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>());
130}
131
132unsigned
133ELFRelocation::RelocType64(const ELFRelocation &rel)
134{
135 if (rel.reloc.is<ELFRel*>())
136 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>());
137 else
138 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>());
139}
140
141unsigned
142ELFRelocation::RelocSymbol32(const ELFRelocation &rel)
143{
144 if (rel.reloc.is<ELFRel*>())
145 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>());
146 else
147 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>());
148}
149
150unsigned
151ELFRelocation::RelocSymbol64(const ELFRelocation &rel)
152{
153 if (rel.reloc.is<ELFRel*>())
154 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>());
155 else
156 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>());
157}
158
Andrew MacPherson17220c12014-03-05 10:12:43 +0000159unsigned
160ELFRelocation::RelocOffset32(const ELFRelocation &rel)
161{
162 if (rel.reloc.is<ELFRel*>())
163 return rel.reloc.get<ELFRel*>()->r_offset;
164 else
165 return rel.reloc.get<ELFRela*>()->r_offset;
166}
167
168unsigned
169ELFRelocation::RelocOffset64(const ELFRelocation &rel)
170{
171 if (rel.reloc.is<ELFRel*>())
172 return rel.reloc.get<ELFRel*>()->r_offset;
173 else
174 return rel.reloc.get<ELFRela*>()->r_offset;
175}
176
177unsigned
178ELFRelocation::RelocAddend32(const ELFRelocation &rel)
179{
180 if (rel.reloc.is<ELFRel*>())
181 return 0;
182 else
183 return rel.reloc.get<ELFRela*>()->r_addend;
184}
185
186unsigned
187ELFRelocation::RelocAddend64(const ELFRelocation &rel)
188{
189 if (rel.reloc.is<ELFRel*>())
190 return 0;
191 else
192 return rel.reloc.get<ELFRela*>()->r_addend;
193}
194
Stephen Wilson499b40e2011-03-30 16:07:05 +0000195} // end anonymous namespace
196
Ed Mastec113ff82013-12-02 17:49:13 +0000197bool
198ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset)
199{
200 // Read all fields.
201 if (data.GetU32(offset, &n_namesz, 3) == NULL)
202 return false;
203
204 // The name field is required to be nul-terminated, and n_namesz
205 // includes the terminating nul in observed implementations (contrary
206 // to the ELF-64 spec). A special case is needed for cores generated
207 // by some older Linux versions, which write a note named "CORE"
208 // without a nul terminator and n_namesz = 4.
209 if (n_namesz == 4)
210 {
211 char buf[4];
212 if (data.ExtractBytes (*offset, 4, data.GetByteOrder(), buf) != 4)
213 return false;
214 if (strncmp (buf, "CORE", 4) == 0)
215 {
216 n_name = "CORE";
217 *offset += 4;
218 return true;
219 }
220 }
221
222 const char *cstr = data.GetCStr(offset, llvm::RoundUpToAlignment (n_namesz, 4));
223 if (cstr == NULL)
224 {
225 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
226 if (log)
227 log->Printf("Failed to parse note name lacking nul terminator");
228
229 return false;
230 }
231 n_name = cstr;
232 return true;
233}
234
Todd Fiala4339f3a2014-03-25 19:29:09 +0000235// Arbitrary constant used as UUID prefix for core files.
236const uint32_t
237ObjectFileELF::g_core_uuid_magic(0xE210C);
238
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000239//------------------------------------------------------------------
240// Static methods.
241//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242void
243ObjectFileELF::Initialize()
244{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000245 PluginManager::RegisterPlugin(GetPluginNameStatic(),
246 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000247 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000248 CreateMemoryInstance,
249 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250}
251
252void
253ObjectFileELF::Terminate()
254{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000255 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256}
257
Greg Clayton57abc5d2013-05-10 21:47:16 +0000258lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259ObjectFileELF::GetPluginNameStatic()
260{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000261 static ConstString g_name("elf");
262 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263}
264
265const char *
266ObjectFileELF::GetPluginDescriptionStatic()
267{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000268 return "ELF object file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269}
270
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +0000272ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp,
273 DataBufferSP &data_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000274 lldb::offset_t data_offset,
275 const lldb_private::FileSpec* file,
276 lldb::offset_t file_offset,
277 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000279 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000281 data_sp = file->MemoryMapFileContents(file_offset, length);
282 data_offset = 0;
283 }
284
285 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
286 {
287 const uint8_t *magic = data_sp->GetBytes() + data_offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000288 if (ELFHeader::MagicBytesMatch(magic))
289 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000290 // Update the data to contain the entire file if it doesn't already
Andrew Kaylor213f6722013-02-07 21:30:54 +0000291 if (data_sp->GetByteSize() < length) {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000292 data_sp = file->MemoryMapFileContents(file_offset, length);
Greg Clayton64ff6c72013-02-07 21:49:54 +0000293 data_offset = 0;
294 magic = data_sp->GetBytes();
Andrew Kaylor213f6722013-02-07 21:30:54 +0000295 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000296 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
297 if (address_size == 4 || address_size == 8)
298 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000299 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 +0000300 ArchSpec spec;
301 if (objfile_ap->GetArchitecture(spec) &&
302 objfile_ap->SetModulesArchitecture(spec))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000303 return objfile_ap.release();
304 }
305 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306 }
307 return NULL;
308}
309
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000310
Greg Claytonc9660542012-02-05 02:38:54 +0000311ObjectFile*
Greg Claytone72dfb32012-02-24 01:59:29 +0000312ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
313 DataBufferSP& data_sp,
314 const lldb::ProcessSP &process_sp,
315 lldb::addr_t header_addr)
Greg Claytonc9660542012-02-05 02:38:54 +0000316{
Andrew MacPherson17220c12014-03-05 10:12:43 +0000317 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT))
318 {
319 const uint8_t *magic = data_sp->GetBytes();
320 if (ELFHeader::MagicBytesMatch(magic))
321 {
322 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
323 if (address_size == 4 || address_size == 8)
324 {
325 std::auto_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, process_sp, header_addr));
326 ArchSpec spec;
327 if (objfile_ap->GetArchitecture(spec) &&
328 objfile_ap->SetModulesArchitecture(spec))
329 return objfile_ap.release();
330 }
331 }
332 }
Greg Claytonc9660542012-02-05 02:38:54 +0000333 return NULL;
334}
335
Michael Sartain9f0013d2013-05-17 00:20:21 +0000336bool
337ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp,
338 lldb::addr_t data_offset,
339 lldb::addr_t data_length)
340{
341 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
342 {
343 const uint8_t *magic = data_sp->GetBytes() + data_offset;
344 return ELFHeader::MagicBytesMatch(magic);
345 }
346 return false;
347}
Greg Claytonc9660542012-02-05 02:38:54 +0000348
Michael Sartain9f4517a2013-07-03 01:52:14 +0000349/*
350 * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c
351 *
352 * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
353 * code or tables extracted from it, as desired without restriction.
354 */
355static uint32_t
Todd Fiala4339f3a2014-03-25 19:29:09 +0000356calc_crc32(uint32_t crc, const void *buf, size_t size)
Michael Sartain9f4517a2013-07-03 01:52:14 +0000357{
358 static const uint32_t g_crc32_tab[] =
359 {
360 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
361 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
362 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
363 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
364 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
365 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
366 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
367 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
368 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
369 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
370 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
371 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
372 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
373 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
374 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
375 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
376 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
377 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
378 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
379 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
380 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
381 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
382 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
383 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
384 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
385 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
386 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
387 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
388 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
389 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
390 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
391 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
392 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
393 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
394 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
395 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
396 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
397 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
398 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
399 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
400 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
401 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
402 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
403 };
404 const uint8_t *p = (const uint8_t *)buf;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000405
Todd Fiala4339f3a2014-03-25 19:29:09 +0000406 crc = crc ^ ~0U;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000407 while (size--)
408 crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
409 return crc ^ ~0U;
410}
411
Todd Fiala4339f3a2014-03-25 19:29:09 +0000412static uint32_t
413calc_gnu_debuglink_crc32(const void *buf, size_t size)
414{
415 return calc_crc32(0U, buf, size);
416}
417
418uint32_t
419ObjectFileELF::CalculateELFNotesSegmentsCRC32 (const ProgramHeaderColl& program_headers,
420 DataExtractor& object_data)
421{
422 typedef ProgramHeaderCollConstIter Iter;
423
424 uint32_t core_notes_crc = 0;
425
426 for (Iter I = program_headers.begin(); I != program_headers.end(); ++I)
427 {
428 if (I->p_type == llvm::ELF::PT_NOTE)
429 {
430 const elf_off ph_offset = I->p_offset;
431 const size_t ph_size = I->p_filesz;
432
433 DataExtractor segment_data;
434 if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size)
435 {
436 // The ELF program header contained incorrect data,
437 // prabably corefile is incomplete or corrupted.
438 break;
439 }
440
441 core_notes_crc = calc_crc32(core_notes_crc,
442 segment_data.GetDataStart(),
443 segment_data.GetByteSize());
444 }
445 }
446
447 return core_notes_crc;
448}
449
Greg Claytonf4d6de62013-04-24 22:29:28 +0000450size_t
451ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file,
452 lldb::DataBufferSP& data_sp,
453 lldb::offset_t data_offset,
454 lldb::offset_t file_offset,
455 lldb::offset_t length,
456 lldb_private::ModuleSpecList &specs)
457{
Michael Sartain9f0013d2013-05-17 00:20:21 +0000458 const size_t initial_count = specs.GetSize();
Michael Sartainc836ae72013-05-23 20:57:03 +0000459
Michael Sartain9f0013d2013-05-17 00:20:21 +0000460 if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
461 {
462 DataExtractor data;
463 data.SetData(data_sp);
464 elf::ELFHeader header;
465 if (header.Parse(data, &data_offset))
466 {
467 if (data_sp)
468 {
469 ModuleSpec spec;
470 spec.GetFileSpec() = file;
471 spec.GetArchitecture().SetArchitecture(eArchTypeELF,
472 header.e_machine,
473 LLDB_INVALID_CPUTYPE);
474 if (spec.GetArchitecture().IsValid())
475 {
Michael Sartainc836ae72013-05-23 20:57:03 +0000476 // We could parse the ABI tag information (in .note, .notes, or .note.ABI-tag) to get the
Michael Sartaina7499c92013-07-01 19:45:50 +0000477 // machine information. However, this info isn't guaranteed to exist or be correct. Details:
Michael Sartainc836ae72013-05-23 20:57:03 +0000478 // http://refspecs.linuxfoundation.org/LSB_1.2.0/gLSB/noteabitag.html
479 // Instead of passing potentially incorrect information down the pipeline, grab
480 // the host information and use it.
481 spec.GetArchitecture().GetTriple().setOSName (Host::GetOSString().GetCString());
482 spec.GetArchitecture().GetTriple().setVendorName(Host::GetVendorString().GetCString());
Michael Sartaina7499c92013-07-01 19:45:50 +0000483
484 // Try to get the UUID from the section list. Usually that's at the end, so
485 // map the file in if we don't have it already.
486 size_t section_header_end = header.e_shoff + header.e_shnum * header.e_shentsize;
487 if (section_header_end > data_sp->GetByteSize())
488 {
489 data_sp = file.MemoryMapFileContents (file_offset, section_header_end);
490 data.SetData(data_sp);
491 }
492
Michael Sartain9f4517a2013-07-03 01:52:14 +0000493 uint32_t gnu_debuglink_crc = 0;
Michael Sartaina7499c92013-07-01 19:45:50 +0000494 std::string gnu_debuglink_file;
495 SectionHeaderColl section_headers;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000496 lldb_private::UUID &uuid = spec.GetUUID();
497 GetSectionHeaderInfo(section_headers, data, header, uuid, gnu_debuglink_file, gnu_debuglink_crc);
498
Todd Fiala4339f3a2014-03-25 19:29:09 +0000499
Michael Sartain9f4517a2013-07-03 01:52:14 +0000500 if (!uuid.IsValid())
501 {
Todd Fiala4339f3a2014-03-25 19:29:09 +0000502 uint32_t core_notes_crc = 0;
503
Michael Sartain9f4517a2013-07-03 01:52:14 +0000504 if (!gnu_debuglink_crc)
505 {
Todd Fiala4339f3a2014-03-25 19:29:09 +0000506 lldb_private::Timer scoped_timer (__PRETTY_FUNCTION__,
507 "Calculating module crc32 %s with size %" PRIu64 " KiB",
508 file.GetLastPathComponent().AsCString(),
509 (file.GetByteSize()-file_offset)/1024);
510
511 // For core files - which usually don't happen to have a gnu_debuglink,
512 // and are pretty bulky - calulating whole contents crc32 would be too much of luxury.
513 // Thus we will need to fallback to something simpler.
514 if (header.e_type == llvm::ELF::ET_CORE)
515 {
516 size_t program_headers_end = header.e_phoff + header.e_phnum * header.e_phentsize;
517 if (program_headers_end > data_sp->GetByteSize())
518 {
519 data_sp = file.MemoryMapFileContents(file_offset, program_headers_end);
520 data.SetData(data_sp);
521 }
522 ProgramHeaderColl program_headers;
523 GetProgramHeaderInfo(program_headers, data, header);
524
525 size_t segment_data_end = 0;
526 for (ProgramHeaderCollConstIter I = program_headers.begin();
527 I != program_headers.end(); ++I)
528 {
529 segment_data_end = std::max (I->p_offset + I->p_filesz, segment_data_end);
530 }
531
532 if (segment_data_end > data_sp->GetByteSize())
533 {
534 data_sp = file.MemoryMapFileContents(file_offset, segment_data_end);
535 data.SetData(data_sp);
536 }
537
538 core_notes_crc = CalculateELFNotesSegmentsCRC32 (program_headers, data);
539 }
540 else
541 {
542 // Need to map entire file into memory to calculate the crc.
543 data_sp = file.MemoryMapFileContents (file_offset, SIZE_MAX);
544 data.SetData(data_sp);
545 gnu_debuglink_crc = calc_gnu_debuglink_crc32 (data.GetDataStart(), data.GetByteSize());
546 }
Michael Sartain9f4517a2013-07-03 01:52:14 +0000547 }
548 if (gnu_debuglink_crc)
549 {
550 // Use 4 bytes of crc from the .gnu_debuglink section.
551 uint32_t uuidt[4] = { gnu_debuglink_crc, 0, 0, 0 };
552 uuid.SetBytes (uuidt, sizeof(uuidt));
553 }
Todd Fiala4339f3a2014-03-25 19:29:09 +0000554 else if (core_notes_crc)
555 {
556 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it look different form
557 // .gnu_debuglink crc followed by 4 bytes of note segments crc.
558 uint32_t uuidt[4] = { g_core_uuid_magic, core_notes_crc, 0, 0 };
559 uuid.SetBytes (uuidt, sizeof(uuidt));
560 }
Michael Sartain9f4517a2013-07-03 01:52:14 +0000561 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000562
Michael Sartain9f0013d2013-05-17 00:20:21 +0000563 specs.Append(spec);
564 }
565 }
566 }
567 }
Michael Sartainc836ae72013-05-23 20:57:03 +0000568
Michael Sartain9f0013d2013-05-17 00:20:21 +0000569 return specs.GetSize() - initial_count;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000570}
571
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000572//------------------------------------------------------------------
573// PluginInterface protocol
574//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000575lldb_private::ConstString
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000576ObjectFileELF::GetPluginName()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000578 return GetPluginNameStatic();
579}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000581uint32_t
582ObjectFileELF::GetPluginVersion()
583{
584 return m_plugin_version;
585}
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000586//------------------------------------------------------------------
587// ObjectFile protocol
588//------------------------------------------------------------------
589
Greg Claytone72dfb32012-02-24 01:59:29 +0000590ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000591 DataBufferSP& data_sp,
592 lldb::offset_t data_offset,
Greg Claytone72dfb32012-02-24 01:59:29 +0000593 const FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000594 lldb::offset_t file_offset,
595 lldb::offset_t length) :
596 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytone72dfb32012-02-24 01:59:29 +0000597 m_header(),
598 m_program_headers(),
599 m_section_headers(),
Michael Sartaina7499c92013-07-01 19:45:50 +0000600 m_filespec_ap()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601{
602 if (file)
603 m_file = *file;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000604 ::memset(&m_header, 0, sizeof(m_header));
Michael Sartaina7499c92013-07-01 19:45:50 +0000605 m_gnu_debuglink_crc = 0;
606 m_gnu_debuglink_file.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607}
608
Andrew MacPherson17220c12014-03-05 10:12:43 +0000609ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
610 DataBufferSP& data_sp,
611 const lldb::ProcessSP &process_sp,
612 addr_t header_addr) :
613 ObjectFile(module_sp, process_sp, LLDB_INVALID_ADDRESS, data_sp),
614 m_header(),
615 m_program_headers(),
616 m_section_headers(),
617 m_filespec_ap()
618{
619 ::memset(&m_header, 0, sizeof(m_header));
620}
621
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622ObjectFileELF::~ObjectFileELF()
623{
624}
625
Jim Ingham5aee1622010-08-09 23:31:02 +0000626bool
627ObjectFileELF::IsExecutable() const
628{
Stephen Wilson7f3b57c2011-01-15 00:09:50 +0000629 return m_header.e_entry != 0;
Jim Ingham5aee1622010-08-09 23:31:02 +0000630}
631
Steve Pucci9e02dac2014-02-06 19:02:19 +0000632bool
Greg Clayton751caf62014-02-07 22:54:47 +0000633ObjectFileELF::SetLoadAddress (Target &target,
634 lldb::addr_t value,
635 bool value_is_offset)
Steve Pucci9e02dac2014-02-06 19:02:19 +0000636{
Steve Pucci9e02dac2014-02-06 19:02:19 +0000637 ModuleSP module_sp = GetModule();
638 if (module_sp)
639 {
640 size_t num_loaded_sections = 0;
641 SectionList *section_list = GetSectionList ();
642 if (section_list)
643 {
Greg Clayton751caf62014-02-07 22:54:47 +0000644 if (value_is_offset)
Steve Pucci9e02dac2014-02-06 19:02:19 +0000645 {
Greg Clayton751caf62014-02-07 22:54:47 +0000646 const size_t num_sections = section_list->GetSize();
647 size_t sect_idx = 0;
Sylvestre Ledruf561a012014-02-21 18:08:09 +0000648
Greg Clayton751caf62014-02-07 22:54:47 +0000649 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
Steve Pucci9e02dac2014-02-06 19:02:19 +0000650 {
Greg Clayton751caf62014-02-07 22:54:47 +0000651 // Iterate through the object file sections to find all
652 // of the sections that have SHF_ALLOC in their flag bits.
653 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
654 // if (section_sp && !section_sp->IsThreadSpecific())
655 if (section_sp && section_sp->Test(SHF_ALLOC))
656 {
657 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
658 ++num_loaded_sections;
659 }
Steve Pucci9e02dac2014-02-06 19:02:19 +0000660 }
Greg Clayton751caf62014-02-07 22:54:47 +0000661 return num_loaded_sections > 0;
662 }
663 else
664 {
665 // Not sure how to slide an ELF file given the base address
666 // of the ELF file in memory
Steve Pucci9e02dac2014-02-06 19:02:19 +0000667 }
668 }
Steve Pucci9e02dac2014-02-06 19:02:19 +0000669 }
Sylvestre Ledruf561a012014-02-21 18:08:09 +0000670 return false; // If it changed
Steve Pucci9e02dac2014-02-06 19:02:19 +0000671}
672
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673ByteOrder
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000674ObjectFileELF::GetByteOrder() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675{
676 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
677 return eByteOrderBig;
678 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
679 return eByteOrderLittle;
680 return eByteOrderInvalid;
681}
682
Greg Claytonc7bece562013-01-25 18:06:21 +0000683uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000684ObjectFileELF::GetAddressByteSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685{
686 return m_data.GetAddressByteSize();
687}
688
Greg Claytonc7bece562013-01-25 18:06:21 +0000689size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000690ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691{
Greg Claytonc7bece562013-01-25 18:06:21 +0000692 return std::distance(m_section_headers.begin(), I) + 1u;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000693}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694
Greg Claytonc7bece562013-01-25 18:06:21 +0000695size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000696ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
697{
Greg Claytonc7bece562013-01-25 18:06:21 +0000698 return std::distance(m_section_headers.begin(), I) + 1u;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699}
700
701bool
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000702ObjectFileELF::ParseHeader()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703{
Filipe Cabecinhas22b40f72013-05-16 23:29:36 +0000704 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000705 return m_header.Parse(m_data, &offset);
706}
707
708bool
Greg Clayton60830262011-02-04 18:53:10 +0000709ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000710{
Michael Sartaina7499c92013-07-01 19:45:50 +0000711 // Need to parse the section list to get the UUIDs, so make sure that's been done.
Todd Fiala4339f3a2014-03-25 19:29:09 +0000712 if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile)
Michael Sartaina7499c92013-07-01 19:45:50 +0000713 return false;
714
Michael Sartainc836ae72013-05-23 20:57:03 +0000715 if (m_uuid.IsValid())
716 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000717 // We have the full build id uuid.
Michael Sartainc836ae72013-05-23 20:57:03 +0000718 *uuid = m_uuid;
719 return true;
720 }
Todd Fiala4339f3a2014-03-25 19:29:09 +0000721 else if (GetType() == ObjectFile::eTypeCoreFile)
722 {
723 uint32_t core_notes_crc = 0;
724
725 if (!ParseProgramHeaders())
726 return false;
727
728 core_notes_crc = CalculateELFNotesSegmentsCRC32(m_program_headers, m_data);
729
730 if (core_notes_crc)
731 {
732 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it
733 // look different form .gnu_debuglink crc - followed by 4 bytes of note
734 // segments crc.
735 uint32_t uuidt[4] = { g_core_uuid_magic, core_notes_crc, 0, 0 };
736 m_uuid.SetBytes (uuidt, sizeof(uuidt));
737 }
738 }
739 else
Michael Sartaina7499c92013-07-01 19:45:50 +0000740 {
Michael Sartain9f4517a2013-07-03 01:52:14 +0000741 if (!m_gnu_debuglink_crc)
742 m_gnu_debuglink_crc = calc_gnu_debuglink_crc32 (m_data.GetDataStart(), m_data.GetByteSize());
Michael Sartaina7499c92013-07-01 19:45:50 +0000743 if (m_gnu_debuglink_crc)
744 {
745 // Use 4 bytes of crc from the .gnu_debuglink section.
746 uint32_t uuidt[4] = { m_gnu_debuglink_crc, 0, 0, 0 };
Todd Fiala4339f3a2014-03-25 19:29:09 +0000747 m_uuid.SetBytes (uuidt, sizeof(uuidt));
Michael Sartaina7499c92013-07-01 19:45:50 +0000748 }
749 }
750
Todd Fiala4339f3a2014-03-25 19:29:09 +0000751 if (m_uuid.IsValid())
752 {
753 *uuid = m_uuid;
754 return true;
755 }
756
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 return false;
758}
759
Michael Sartaina7499c92013-07-01 19:45:50 +0000760lldb_private::FileSpecList
761ObjectFileELF::GetDebugSymbolFilePaths()
762{
763 FileSpecList file_spec_list;
764
765 if (!m_gnu_debuglink_file.empty())
766 {
767 FileSpec file_spec (m_gnu_debuglink_file.c_str(), false);
768 file_spec_list.Append (file_spec);
769 }
770 return file_spec_list;
771}
772
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000774ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000776 size_t num_modules = ParseDependentModules();
777 uint32_t num_specs = 0;
778
779 for (unsigned i = 0; i < num_modules; ++i)
780 {
781 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
782 num_specs++;
783 }
784
785 return num_specs;
786}
787
Stephen Wilson499b40e2011-03-30 16:07:05 +0000788Address
Ed Maste54803652013-10-11 17:39:07 +0000789ObjectFileELF::GetImageInfoAddress(Target *target)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000790{
791 if (!ParseDynamicSymbols())
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000792 return Address();
793
794 SectionList *section_list = GetSectionList();
795 if (!section_list)
796 return Address();
797
Greg Clayton3046e662013-07-10 01:23:25 +0000798 // Find the SHT_DYNAMIC (.dynamic) section.
799 SectionSP dynsym_section_sp (section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true));
800 if (!dynsym_section_sp)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000801 return Address();
Greg Clayton3046e662013-07-10 01:23:25 +0000802 assert (dynsym_section_sp->GetObjectFile() == this);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000803
Greg Clayton3046e662013-07-10 01:23:25 +0000804 user_id_t dynsym_id = dynsym_section_sp->GetID();
Michael Sartaina7499c92013-07-01 19:45:50 +0000805 const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000806 if (!dynsym_hdr)
807 return Address();
808
Greg Clayton3046e662013-07-10 01:23:25 +0000809 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000810 {
Greg Clayton3046e662013-07-10 01:23:25 +0000811 ELFDynamic &symbol = m_dynamic_symbols[i];
Greg Claytone72dfb32012-02-24 01:59:29 +0000812
Ed Maste54803652013-10-11 17:39:07 +0000813 if (symbol.d_tag == DT_DEBUG)
Greg Clayton3046e662013-07-10 01:23:25 +0000814 {
815 // Compute the offset as the number of previous entries plus the
816 // size of d_tag.
817 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
818 return Address(dynsym_section_sp, offset);
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000819 }
Ed Maste54803652013-10-11 17:39:07 +0000820 else if (symbol.d_tag == DT_MIPS_RLD_MAP && target)
821 {
822 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
823 addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);
824 if (dyn_base == LLDB_INVALID_ADDRESS)
825 return Address();
826 Address addr;
827 Error error;
828 if (target->ReadPointerFromMemory(dyn_base + offset, false, error, addr))
829 return addr;
830 }
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000831 }
832
833 return Address();
834}
835
Jim Ingham672e6f52011-03-07 23:44:08 +0000836lldb_private::Address
837ObjectFileELF::GetEntryPointAddress ()
838{
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000839 if (m_entry_point_address.IsValid())
840 return m_entry_point_address;
841
842 if (!ParseHeader() || !IsExecutable())
843 return m_entry_point_address;
844
Greg Clayton3046e662013-07-10 01:23:25 +0000845 SectionList *section_list = GetSectionList();
846 addr_t offset = m_header.e_entry;
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000847
Greg Clayton3046e662013-07-10 01:23:25 +0000848 if (!section_list)
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000849 m_entry_point_address.SetOffset(offset);
Greg Clayton3046e662013-07-10 01:23:25 +0000850 else
851 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000852 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +0000853}
854
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000855//----------------------------------------------------------------------
856// ParseDependentModules
857//----------------------------------------------------------------------
858size_t
859ObjectFileELF::ParseDependentModules()
860{
861 if (m_filespec_ap.get())
862 return m_filespec_ap->GetSize();
863
864 m_filespec_ap.reset(new FileSpecList());
865
Michael Sartaina7499c92013-07-01 19:45:50 +0000866 if (!ParseSectionHeaders())
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000867 return 0;
868
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000869 SectionList *section_list = GetSectionList();
870 if (!section_list)
871 return 0;
872
Greg Clayton3046e662013-07-10 01:23:25 +0000873 // Find the SHT_DYNAMIC section.
874 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
875 if (!dynsym)
876 return 0;
877 assert (dynsym->GetObjectFile() == this);
878
879 const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex (dynsym->GetID());
880 if (!header)
881 return 0;
882 // sh_link: section header index of string table used by entries in the section.
883 Section *dynstr = section_list->FindSectionByID (header->sh_link + 1).get();
884 if (!dynstr)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000885 return 0;
886
887 DataExtractor dynsym_data;
888 DataExtractor dynstr_data;
Greg Claytonc9660542012-02-05 02:38:54 +0000889 if (ReadSectionData(dynsym, dynsym_data) &&
890 ReadSectionData(dynstr, dynstr_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000891 {
892 ELFDynamic symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +0000893 const lldb::offset_t section_size = dynsym_data.GetByteSize();
894 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000895
896 // The only type of entries we are concerned with are tagged DT_NEEDED,
897 // yielding the name of a required library.
898 while (offset < section_size)
899 {
900 if (!symbol.Parse(dynsym_data, &offset))
901 break;
902
903 if (symbol.d_tag != DT_NEEDED)
904 continue;
905
906 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
907 const char *lib_name = dynstr_data.PeekCStr(str_index);
Greg Clayton274060b2010-10-20 20:54:39 +0000908 m_filespec_ap->Append(FileSpec(lib_name, true));
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000909 }
910 }
911
912 return m_filespec_ap->GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913}
914
915//----------------------------------------------------------------------
Todd Fiala4339f3a2014-03-25 19:29:09 +0000916// GetProgramHeaderInfo
917//----------------------------------------------------------------------
918size_t
919ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
920 DataExtractor &object_data,
921 const ELFHeader &header)
922{
923 // We have already parsed the program headers
924 if (!program_headers.empty())
925 return program_headers.size();
926
927 // If there are no program headers to read we are done.
928 if (header.e_phnum == 0)
929 return 0;
930
931 program_headers.resize(header.e_phnum);
932 if (program_headers.size() != header.e_phnum)
933 return 0;
934
935 const size_t ph_size = header.e_phnum * header.e_phentsize;
936 const elf_off ph_offset = header.e_phoff;
937 DataExtractor data;
938 if (data.SetData(object_data, ph_offset, ph_size) != ph_size)
939 return 0;
940
941 uint32_t idx;
942 lldb::offset_t offset;
943 for (idx = 0, offset = 0; idx < header.e_phnum; ++idx)
944 {
945 if (program_headers[idx].Parse(data, &offset) == false)
946 break;
947 }
948
949 if (idx < program_headers.size())
950 program_headers.resize(idx);
951
952 return program_headers.size();
953
954}
955
956//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000957// ParseProgramHeaders
958//----------------------------------------------------------------------
959size_t
960ObjectFileELF::ParseProgramHeaders()
961{
Todd Fiala4339f3a2014-03-25 19:29:09 +0000962 return GetProgramHeaderInfo(m_program_headers, m_data, m_header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963}
964
Michael Sartainc836ae72013-05-23 20:57:03 +0000965static bool
Michael Sartaina7499c92013-07-01 19:45:50 +0000966ParseNoteGNUBuildID(DataExtractor &data, lldb_private::UUID &uuid)
Michael Sartainc836ae72013-05-23 20:57:03 +0000967{
968 // Try to parse the note section (ie .note.gnu.build-id|.notes|.note|...) and get the build id.
969 // BuildID documentation: https://fedoraproject.org/wiki/Releases/FeatureBuildId
Michael Sartainc836ae72013-05-23 20:57:03 +0000970 lldb::offset_t offset = 0;
971 static const uint32_t g_gnu_build_id = 3; // NT_GNU_BUILD_ID from elf.h
972
973 while (true)
974 {
Ed Mastec113ff82013-12-02 17:49:13 +0000975 ELFNote note = ELFNote();
976 if (!note.Parse(data, &offset))
Michael Sartainc836ae72013-05-23 20:57:03 +0000977 return false;
978
Michael Sartainc836ae72013-05-23 20:57:03 +0000979 // 16 bytes is UUID|MD5, 20 bytes is SHA1
Ed Mastec113ff82013-12-02 17:49:13 +0000980 if (note.n_name == "GNU" && (note.n_type == g_gnu_build_id) &&
981 (note.n_descsz == 16 || note.n_descsz == 20))
Michael Sartainc836ae72013-05-23 20:57:03 +0000982 {
Ed Mastec113ff82013-12-02 17:49:13 +0000983 uint8_t uuidbuf[20];
984 if (data.GetU8 (&offset, &uuidbuf, note.n_descsz) == NULL)
Michael Sartainc836ae72013-05-23 20:57:03 +0000985 return false;
Ed Mastec113ff82013-12-02 17:49:13 +0000986 uuid.SetBytes (uuidbuf, note.n_descsz);
987 return true;
Michael Sartainc836ae72013-05-23 20:57:03 +0000988 }
Ed Mastec113ff82013-12-02 17:49:13 +0000989 offset += llvm::RoundUpToAlignment(note.n_descsz, 4);
Michael Sartainc836ae72013-05-23 20:57:03 +0000990 }
991 return false;
992}
Michael Sartaina7499c92013-07-01 19:45:50 +0000993
994//----------------------------------------------------------------------
995// GetSectionHeaderInfo
996//----------------------------------------------------------------------
997size_t
998ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl &section_headers,
999 lldb_private::DataExtractor &object_data,
1000 const elf::ELFHeader &header,
1001 lldb_private::UUID &uuid,
1002 std::string &gnu_debuglink_file,
1003 uint32_t &gnu_debuglink_crc)
1004{
1005 // We have already parsed the section headers
1006 if (!section_headers.empty())
1007 return section_headers.size();
1008
1009 // If there are no section headers we are done.
1010 if (header.e_shnum == 0)
1011 return 0;
1012
1013 section_headers.resize(header.e_shnum);
1014 if (section_headers.size() != header.e_shnum)
1015 return 0;
1016
1017 const size_t sh_size = header.e_shnum * header.e_shentsize;
1018 const elf_off sh_offset = header.e_shoff;
1019 DataExtractor sh_data;
1020 if (sh_data.SetData (object_data, sh_offset, sh_size) != sh_size)
1021 return 0;
1022
1023 uint32_t idx;
1024 lldb::offset_t offset;
1025 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx)
1026 {
1027 if (section_headers[idx].Parse(sh_data, &offset) == false)
1028 break;
1029 }
1030 if (idx < section_headers.size())
1031 section_headers.resize(idx);
1032
1033 const unsigned strtab_idx = header.e_shstrndx;
1034 if (strtab_idx && strtab_idx < section_headers.size())
1035 {
1036 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx];
1037 const size_t byte_size = sheader.sh_size;
1038 const Elf64_Off offset = sheader.sh_offset;
1039 lldb_private::DataExtractor shstr_data;
1040
1041 if (shstr_data.SetData (object_data, offset, byte_size) == byte_size)
1042 {
1043 for (SectionHeaderCollIter I = section_headers.begin();
1044 I != section_headers.end(); ++I)
1045 {
1046 static ConstString g_sect_name_gnu_debuglink (".gnu_debuglink");
1047 const ELFSectionHeaderInfo &header = *I;
1048 const uint64_t section_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
1049 ConstString name(shstr_data.PeekCStr(I->sh_name));
1050
1051 I->section_name = name;
1052
1053 if (name == g_sect_name_gnu_debuglink)
1054 {
1055 DataExtractor data;
1056 if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size))
1057 {
1058 lldb::offset_t gnu_debuglink_offset = 0;
1059 gnu_debuglink_file = data.GetCStr (&gnu_debuglink_offset);
1060 gnu_debuglink_offset = llvm::RoundUpToAlignment (gnu_debuglink_offset, 4);
1061 data.GetU32 (&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
1062 }
1063 }
1064
1065 if (header.sh_type == SHT_NOTE && !uuid.IsValid())
1066 {
1067 DataExtractor data;
1068 if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size))
1069 {
1070 ParseNoteGNUBuildID (data, uuid);
1071 }
1072 }
1073 }
1074
1075 return section_headers.size();
1076 }
1077 }
1078
1079 section_headers.clear();
1080 return 0;
1081}
1082
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001083size_t
1084ObjectFileELF::GetProgramHeaderCount()
1085{
1086 return ParseProgramHeaders();
1087}
1088
1089const elf::ELFProgramHeader *
1090ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id)
1091{
1092 if (!id || !ParseProgramHeaders())
1093 return NULL;
1094
1095 if (--id < m_program_headers.size())
1096 return &m_program_headers[id];
1097
1098 return NULL;
1099}
1100
1101DataExtractor
1102ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id)
1103{
1104 const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id);
1105 if (segment_header == NULL)
1106 return DataExtractor();
1107 return DataExtractor(m_data, segment_header->p_offset, segment_header->p_filesz);
1108}
1109
Michael Sartaina7499c92013-07-01 19:45:50 +00001110//----------------------------------------------------------------------
1111// ParseSectionHeaders
1112//----------------------------------------------------------------------
1113size_t
1114ObjectFileELF::ParseSectionHeaders()
1115{
1116 return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, m_gnu_debuglink_file, m_gnu_debuglink_crc);
1117}
1118
Michael Sartaina7499c92013-07-01 19:45:50 +00001119const ObjectFileELF::ELFSectionHeaderInfo *
1120ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
1121{
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001122 if (!id || !ParseSectionHeaders())
Michael Sartaina7499c92013-07-01 19:45:50 +00001123 return NULL;
1124
1125 if (--id < m_section_headers.size())
1126 return &m_section_headers[id];
1127
1128 return NULL;
1129}
1130
Greg Clayton3046e662013-07-10 01:23:25 +00001131void
1132ObjectFileELF::CreateSections(SectionList &unified_section_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133{
Greg Clayton3046e662013-07-10 01:23:25 +00001134 if (!m_sections_ap.get() && ParseSectionHeaders())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135 {
1136 m_sections_ap.reset(new SectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001138 for (SectionHeaderCollIter I = m_section_headers.begin();
1139 I != m_section_headers.end(); ++I)
1140 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001141 const ELFSectionHeaderInfo &header = *I;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001142
Michael Sartaina7499c92013-07-01 19:45:50 +00001143 ConstString& name = I->section_name;
Greg Clayton47037bc2012-03-27 02:40:46 +00001144 const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
1145 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001146
Greg Clayton4ceb9982010-07-21 22:54:26 +00001147 static ConstString g_sect_name_text (".text");
1148 static ConstString g_sect_name_data (".data");
1149 static ConstString g_sect_name_bss (".bss");
Greg Clayton741f3f92012-03-27 21:10:07 +00001150 static ConstString g_sect_name_tdata (".tdata");
1151 static ConstString g_sect_name_tbss (".tbss");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001152 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
1153 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
1154 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
1155 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
1156 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
1157 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
1158 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
1159 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
1160 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
1161 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
1162 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
1163 static ConstString g_sect_name_eh_frame (".eh_frame");
1164
1165 SectionType sect_type = eSectionTypeOther;
1166
Greg Clayton741f3f92012-03-27 21:10:07 +00001167 bool is_thread_specific = false;
Michael Sartaina7499c92013-07-01 19:45:50 +00001168
Greg Clayton4ceb9982010-07-21 22:54:26 +00001169 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
1170 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
1171 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
Greg Clayton741f3f92012-03-27 21:10:07 +00001172 else if (name == g_sect_name_tdata)
1173 {
1174 sect_type = eSectionTypeData;
1175 is_thread_specific = true;
1176 }
1177 else if (name == g_sect_name_tbss)
1178 {
1179 sect_type = eSectionTypeZeroFill;
1180 is_thread_specific = true;
1181 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001182 // .debug_abbrev – Abbreviations used in the .debug_info section
1183 // .debug_aranges – Lookup table for mapping addresses to compilation units
1184 // .debug_frame – Call frame information
1185 // .debug_info – The core DWARF information section
1186 // .debug_line – Line number information
1187 // .debug_loc – Location lists used in DW_AT_location attributes
1188 // .debug_macinfo – Macro information
1189 // .debug_pubnames – Lookup table for mapping object and function names to compilation units
1190 // .debug_pubtypes – Lookup table for mapping type names to compilation units
1191 // .debug_ranges – Address ranges used in DW_AT_ranges attributes
1192 // .debug_str – String table used in .debug_info
Michael Sartain3cf443d2013-07-17 00:26:30 +00001193 // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html
1194 // MISSING? .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644
Michael Sartaina7499c92013-07-01 19:45:50 +00001195 // MISSING? .debug_types - Type descriptions from DWARF 4? See http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo
Greg Clayton4ceb9982010-07-21 22:54:26 +00001196 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
1197 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
1198 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
1199 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
1200 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
1201 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
1202 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
1203 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
1204 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
1205 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
1206 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
1207 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
Michael Sartaina7499c92013-07-01 19:45:50 +00001208
1209 switch (header.sh_type)
Michael Sartainc836ae72013-05-23 20:57:03 +00001210 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001211 case SHT_SYMTAB:
1212 assert (sect_type == eSectionTypeOther);
1213 sect_type = eSectionTypeELFSymbolTable;
1214 break;
1215 case SHT_DYNSYM:
1216 assert (sect_type == eSectionTypeOther);
1217 sect_type = eSectionTypeELFDynamicSymbols;
1218 break;
1219 case SHT_RELA:
1220 case SHT_REL:
1221 assert (sect_type == eSectionTypeOther);
1222 sect_type = eSectionTypeELFRelocationEntries;
1223 break;
1224 case SHT_DYNAMIC:
1225 assert (sect_type == eSectionTypeOther);
1226 sect_type = eSectionTypeELFDynamicLinkInfo;
1227 break;
Michael Sartainc836ae72013-05-23 20:57:03 +00001228 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001229
Greg Clayton3046e662013-07-10 01:23:25 +00001230 SectionSP section_sp (new Section(GetModule(), // Module to which this section belongs.
1231 this, // ObjectFile to which this section belongs and should read section data from.
1232 SectionIndex(I), // Section ID.
1233 name, // Section name.
1234 sect_type, // Section type.
1235 header.sh_addr, // VM address.
1236 vm_size, // VM size in bytes of this section.
1237 header.sh_offset, // Offset of this section in the file.
1238 file_size, // Size of the section as found in the file.
1239 header.sh_flags)); // Flags for this section.
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001240
Greg Clayton741f3f92012-03-27 21:10:07 +00001241 if (is_thread_specific)
1242 section_sp->SetIsThreadSpecific (is_thread_specific);
1243 m_sections_ap->AddSection(section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244 }
1245 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001246
Greg Clayton3046e662013-07-10 01:23:25 +00001247 if (m_sections_ap.get())
1248 {
1249 if (GetType() == eTypeDebugInfo)
1250 {
1251 static const SectionType g_sections[] =
1252 {
1253 eSectionTypeDWARFDebugAranges,
1254 eSectionTypeDWARFDebugInfo,
1255 eSectionTypeDWARFDebugAbbrev,
1256 eSectionTypeDWARFDebugFrame,
1257 eSectionTypeDWARFDebugLine,
1258 eSectionTypeDWARFDebugStr,
1259 eSectionTypeDWARFDebugLoc,
1260 eSectionTypeDWARFDebugMacInfo,
1261 eSectionTypeDWARFDebugPubNames,
1262 eSectionTypeDWARFDebugPubTypes,
1263 eSectionTypeDWARFDebugRanges,
1264 eSectionTypeELFSymbolTable,
1265 };
1266 SectionList *elf_section_list = m_sections_ap.get();
1267 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx)
1268 {
1269 SectionType section_type = g_sections[idx];
1270 SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true));
1271 if (section_sp)
1272 {
1273 SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true));
1274 if (module_section_sp)
1275 unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp);
1276 else
1277 unified_section_list.AddSection (section_sp);
1278 }
1279 }
1280 }
1281 else
1282 {
1283 unified_section_list = *m_sections_ap;
1284 }
1285 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286}
1287
Greg Clayton3046e662013-07-10 01:23:25 +00001288// private
Michael Sartaina7499c92013-07-01 19:45:50 +00001289unsigned
Greg Clayton3046e662013-07-10 01:23:25 +00001290ObjectFileELF::ParseSymbols (Symtab *symtab,
1291 user_id_t start_id,
1292 SectionList *section_list,
1293 const size_t num_symbols,
1294 const DataExtractor &symtab_data,
1295 const DataExtractor &strtab_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001297 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00001298 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001299
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001300 static ConstString text_section_name(".text");
1301 static ConstString init_section_name(".init");
1302 static ConstString fini_section_name(".fini");
1303 static ConstString ctors_section_name(".ctors");
1304 static ConstString dtors_section_name(".dtors");
1305
1306 static ConstString data_section_name(".data");
1307 static ConstString rodata_section_name(".rodata");
1308 static ConstString rodata1_section_name(".rodata1");
1309 static ConstString data2_section_name(".data1");
1310 static ConstString bss_section_name(".bss");
1311
Greg Clayton9594f4c2013-04-13 23:17:23 +00001312 //StreamFile strm(stdout, false);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001313 unsigned i;
1314 for (i = 0; i < num_symbols; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001315 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001316 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317 break;
Greg Clayton9594f4c2013-04-13 23:17:23 +00001318
1319 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
1320
Andrew MacPherson17220c12014-03-05 10:12:43 +00001321 // No need to add non-section symbols that have no names
1322 if (symbol.getType() != STT_SECTION &&
1323 (symbol_name == NULL || symbol_name[0] == '\0'))
Greg Clayton9594f4c2013-04-13 23:17:23 +00001324 continue;
1325
1326 //symbol.Dump (&strm, i, &strtab_data, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327
Greg Claytone72dfb32012-02-24 01:59:29 +00001328 SectionSP symbol_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001329 SymbolType symbol_type = eSymbolTypeInvalid;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001330 Elf64_Half symbol_idx = symbol.st_shndx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001332 switch (symbol_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333 {
1334 case SHN_ABS:
1335 symbol_type = eSymbolTypeAbsolute;
1336 break;
1337 case SHN_UNDEF:
1338 symbol_type = eSymbolTypeUndefined;
1339 break;
1340 default:
Greg Claytone72dfb32012-02-24 01:59:29 +00001341 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342 break;
1343 }
1344
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001345 // If a symbol is undefined do not process it further even if it has a STT type
1346 if (symbol_type != eSymbolTypeUndefined)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001347 {
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001348 switch (symbol.getType())
1349 {
1350 default:
1351 case STT_NOTYPE:
1352 // The symbol's type is not specified.
1353 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001355 case STT_OBJECT:
1356 // The symbol is associated with a data object, such as a variable,
1357 // an array, etc.
1358 symbol_type = eSymbolTypeData;
1359 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001360
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001361 case STT_FUNC:
1362 // The symbol is associated with a function or other executable code.
1363 symbol_type = eSymbolTypeCode;
1364 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001366 case STT_SECTION:
1367 // The symbol is associated with a section. Symbol table entries of
1368 // this type exist primarily for relocation and normally have
1369 // STB_LOCAL binding.
1370 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001371
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001372 case STT_FILE:
1373 // Conventionally, the symbol's name gives the name of the source
1374 // file associated with the object file. A file symbol has STB_LOCAL
1375 // binding, its section index is SHN_ABS, and it precedes the other
1376 // STB_LOCAL symbols for the file, if it is present.
Greg Clayton9594f4c2013-04-13 23:17:23 +00001377 symbol_type = eSymbolTypeSourceFile;
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001378 break;
Matt Kopec00049b82013-02-27 20:13:38 +00001379
1380 case STT_GNU_IFUNC:
1381 // The symbol is associated with an indirect function. The actual
1382 // function will be resolved if it is referenced.
1383 symbol_type = eSymbolTypeResolver;
1384 break;
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001385 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001386 }
1387
1388 if (symbol_type == eSymbolTypeInvalid)
1389 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001390 if (symbol_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001392 const ConstString &sect_name = symbol_section_sp->GetName();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 if (sect_name == text_section_name ||
1394 sect_name == init_section_name ||
1395 sect_name == fini_section_name ||
1396 sect_name == ctors_section_name ||
1397 sect_name == dtors_section_name)
1398 {
1399 symbol_type = eSymbolTypeCode;
1400 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001401 else if (sect_name == data_section_name ||
1402 sect_name == data2_section_name ||
1403 sect_name == rodata_section_name ||
1404 sect_name == rodata1_section_name ||
1405 sect_name == bss_section_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406 {
1407 symbol_type = eSymbolTypeData;
1408 }
1409 }
1410 }
1411
Greg Clayton3046e662013-07-10 01:23:25 +00001412 // If the symbol section we've found has no data (SHT_NOBITS), then check the module section
1413 // list. This can happen if we're parsing the debug file and it has no .text section, for example.
Michael Sartaina7499c92013-07-01 19:45:50 +00001414 if (symbol_section_sp && (symbol_section_sp->GetFileSize() == 0))
1415 {
1416 ModuleSP module_sp(GetModule());
1417 if (module_sp)
1418 {
Greg Clayton3046e662013-07-10 01:23:25 +00001419 SectionList *module_section_list = module_sp->GetSectionList();
1420 if (module_section_list && module_section_list != section_list)
Michael Sartaina7499c92013-07-01 19:45:50 +00001421 {
1422 const ConstString &sect_name = symbol_section_sp->GetName();
Greg Clayton3046e662013-07-10 01:23:25 +00001423 lldb::SectionSP section_sp (module_section_list->FindSectionByName (sect_name));
Michael Sartaina7499c92013-07-01 19:45:50 +00001424 if (section_sp && section_sp->GetFileSize())
1425 {
1426 symbol_section_sp = section_sp;
1427 }
1428 }
1429 }
1430 }
1431
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432 uint64_t symbol_value = symbol.st_value;
Andrew MacPherson17220c12014-03-05 10:12:43 +00001433 if (symbol_section_sp && CalculateType() != ObjectFile::Type::eTypeObjectFile)
Greg Claytone72dfb32012-02-24 01:59:29 +00001434 symbol_value -= symbol_section_sp->GetFileAddress();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001435 bool is_global = symbol.getBinding() == STB_GLOBAL;
1436 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
Greg Clayton47037bc2012-03-27 02:40:46 +00001437 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001438 Symbol dc_symbol(
Greg Claytone72dfb32012-02-24 01:59:29 +00001439 i + start_id, // ID is the original symbol table index.
1440 symbol_name, // Symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +00001441 is_mangled, // Is the symbol name mangled?
Greg Claytone72dfb32012-02-24 01:59:29 +00001442 symbol_type, // Type of this symbol
1443 is_global, // Is this globally visible?
1444 false, // Is this symbol debug info?
1445 false, // Is this symbol a trampoline?
1446 false, // Is this symbol artificial?
1447 symbol_section_sp, // Section in which this symbol is defined or null.
1448 symbol_value, // Offset in section or symbol value.
1449 symbol.st_size, // Size in bytes of this symbol.
Greg Clayton9594f4c2013-04-13 23:17:23 +00001450 true, // Size is valid
Greg Claytone72dfb32012-02-24 01:59:29 +00001451 flags); // Symbol flags.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452 symtab->AddSymbol(dc_symbol);
1453 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00001454
1455 return i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001456}
1457
Stephen Wilson499b40e2011-03-30 16:07:05 +00001458unsigned
Greg Clayton3046e662013-07-10 01:23:25 +00001459ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, lldb_private::Section *symtab)
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001460{
Greg Clayton3046e662013-07-10 01:23:25 +00001461 if (symtab->GetObjectFile() != this)
1462 {
1463 // If the symbol table section is owned by a different object file, have it do the
1464 // parsing.
1465 ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile());
1466 return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab);
1467 }
1468
1469 // Get section list for this object file.
1470 SectionList *section_list = m_sections_ap.get();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001471 if (!section_list)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001472 return 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001473
Greg Clayton3046e662013-07-10 01:23:25 +00001474 user_id_t symtab_id = symtab->GetID();
1475 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
Michael Sartaina7499c92013-07-01 19:45:50 +00001476 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
1477 symtab_hdr->sh_type == SHT_DYNSYM);
1478
Greg Clayton3046e662013-07-10 01:23:25 +00001479 // sh_link: section header index of associated string table.
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001480 // Section ID's are ones based.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001481 user_id_t strtab_id = symtab_hdr->sh_link + 1;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001482 Section *strtab = section_list->FindSectionByID(strtab_id).get();
Greg Clayton3046e662013-07-10 01:23:25 +00001483
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001484 if (symtab && strtab)
1485 {
Greg Clayton3046e662013-07-10 01:23:25 +00001486 assert (symtab->GetObjectFile() == this);
1487 assert (strtab->GetObjectFile() == this);
1488
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001489 DataExtractor symtab_data;
1490 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001491 if (ReadSectionData(symtab, symtab_data) &&
1492 ReadSectionData(strtab, strtab_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001493 {
Greg Clayton3046e662013-07-10 01:23:25 +00001494 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
1495
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +00001496 return ParseSymbols(symbol_table, start_id, section_list,
1497 num_symbols, symtab_data, strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001498 }
1499 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00001500
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +00001501 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001502}
1503
1504size_t
1505ObjectFileELF::ParseDynamicSymbols()
1506{
1507 if (m_dynamic_symbols.size())
1508 return m_dynamic_symbols.size();
1509
Stephen Wilson499b40e2011-03-30 16:07:05 +00001510 SectionList *section_list = GetSectionList();
1511 if (!section_list)
Bill Wendlinged24dcc2012-04-03 07:50:11 +00001512 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001513
Greg Clayton3046e662013-07-10 01:23:25 +00001514 // Find the SHT_DYNAMIC section.
1515 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
Stephen Wilson499b40e2011-03-30 16:07:05 +00001516 if (!dynsym)
Bill Wendlinged24dcc2012-04-03 07:50:11 +00001517 return 0;
Greg Clayton3046e662013-07-10 01:23:25 +00001518 assert (dynsym->GetObjectFile() == this);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001519
1520 ELFDynamic symbol;
1521 DataExtractor dynsym_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001522 if (ReadSectionData(dynsym, dynsym_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001523 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001524 const lldb::offset_t section_size = dynsym_data.GetByteSize();
1525 lldb::offset_t cursor = 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001526
1527 while (cursor < section_size)
1528 {
Stephen Wilson499b40e2011-03-30 16:07:05 +00001529 if (!symbol.Parse(dynsym_data, &cursor))
1530 break;
1531
1532 m_dynamic_symbols.push_back(symbol);
1533 }
1534 }
1535
1536 return m_dynamic_symbols.size();
1537}
1538
1539const ELFDynamic *
1540ObjectFileELF::FindDynamicSymbol(unsigned tag)
1541{
1542 if (!ParseDynamicSymbols())
1543 return NULL;
1544
Stephen Wilson499b40e2011-03-30 16:07:05 +00001545 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
1546 DynamicSymbolCollIter E = m_dynamic_symbols.end();
1547 for ( ; I != E; ++I)
1548 {
1549 ELFDynamic *symbol = &*I;
1550
1551 if (symbol->d_tag == tag)
1552 return symbol;
1553 }
1554
1555 return NULL;
1556}
1557
Stephen Wilson499b40e2011-03-30 16:07:05 +00001558unsigned
1559ObjectFileELF::PLTRelocationType()
1560{
Michael Sartainf7899542013-08-22 21:25:53 +00001561 // DT_PLTREL
1562 // This member specifies the type of relocation entry to which the
1563 // procedure linkage table refers. The d_val member holds DT_REL or
1564 // DT_RELA, as appropriate. All relocations in a procedure linkage table
1565 // must use the same relocation.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001566 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
1567
1568 if (symbol)
1569 return symbol->d_val;
1570
1571 return 0;
1572}
1573
1574static unsigned
1575ParsePLTRelocations(Symtab *symbol_table,
1576 user_id_t start_id,
1577 unsigned rel_type,
1578 const ELFHeader *hdr,
1579 const ELFSectionHeader *rel_hdr,
1580 const ELFSectionHeader *plt_hdr,
1581 const ELFSectionHeader *sym_hdr,
Greg Claytone72dfb32012-02-24 01:59:29 +00001582 const lldb::SectionSP &plt_section_sp,
Stephen Wilson499b40e2011-03-30 16:07:05 +00001583 DataExtractor &rel_data,
1584 DataExtractor &symtab_data,
1585 DataExtractor &strtab_data)
1586{
1587 ELFRelocation rel(rel_type);
1588 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00001589 lldb::offset_t offset = 0;
Michael Sartainf7899542013-08-22 21:25:53 +00001590 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 bytes.
1591 // So round the entsize up by the alignment if addralign is set.
1592 const elf_xword plt_entsize = plt_hdr->sh_addralign ?
1593 llvm::RoundUpToAlignment (plt_hdr->sh_entsize, plt_hdr->sh_addralign) : plt_hdr->sh_entsize;
Greg Claytonc7bece562013-01-25 18:06:21 +00001594 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001595
1596 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
1597 reloc_info_fn reloc_type;
1598 reloc_info_fn reloc_symbol;
1599
Greg Claytond091afe2012-11-12 22:53:16 +00001600 if (hdr->Is32Bit())
Stephen Wilson499b40e2011-03-30 16:07:05 +00001601 {
1602 reloc_type = ELFRelocation::RelocType32;
1603 reloc_symbol = ELFRelocation::RelocSymbol32;
1604 }
1605 else
1606 {
1607 reloc_type = ELFRelocation::RelocType64;
1608 reloc_symbol = ELFRelocation::RelocSymbol64;
1609 }
1610
1611 unsigned slot_type = hdr->GetRelocationJumpSlotType();
1612 unsigned i;
1613 for (i = 0; i < num_relocations; ++i)
1614 {
1615 if (rel.Parse(rel_data, &offset) == false)
1616 break;
1617
1618 if (reloc_type(rel) != slot_type)
1619 continue;
1620
Greg Claytonc7bece562013-01-25 18:06:21 +00001621 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001622 uint64_t plt_index = (i + 1) * plt_entsize;
1623
1624 if (!symbol.Parse(symtab_data, &symbol_offset))
1625 break;
1626
1627 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Greg Clayton47037bc2012-03-27 02:40:46 +00001628 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001629
1630 Symbol jump_symbol(
1631 i + start_id, // Symbol table index
1632 symbol_name, // symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +00001633 is_mangled, // is the symbol name mangled?
Stephen Wilson499b40e2011-03-30 16:07:05 +00001634 eSymbolTypeTrampoline, // Type of this symbol
1635 false, // Is this globally visible?
1636 false, // Is this symbol debug info?
1637 true, // Is this symbol a trampoline?
1638 true, // Is this symbol artificial?
Greg Claytone72dfb32012-02-24 01:59:29 +00001639 plt_section_sp, // Section in which this symbol is defined or null.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001640 plt_index, // Offset in section or symbol value.
1641 plt_entsize, // Size in bytes of this symbol.
Greg Clayton9594f4c2013-04-13 23:17:23 +00001642 true, // Size is valid
Stephen Wilson499b40e2011-03-30 16:07:05 +00001643 0); // Symbol flags.
1644
1645 symbol_table->AddSymbol(jump_symbol);
1646 }
1647
1648 return i;
1649}
1650
1651unsigned
1652ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
1653 user_id_t start_id,
Michael Sartaina7499c92013-07-01 19:45:50 +00001654 const ELFSectionHeaderInfo *rel_hdr,
Stephen Wilson499b40e2011-03-30 16:07:05 +00001655 user_id_t rel_id)
1656{
1657 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1658
Greg Clayton3046e662013-07-10 01:23:25 +00001659 // The link field points to the associated symbol table. The info field
Stephen Wilson499b40e2011-03-30 16:07:05 +00001660 // points to the section holding the plt.
1661 user_id_t symtab_id = rel_hdr->sh_link;
1662 user_id_t plt_id = rel_hdr->sh_info;
1663
1664 if (!symtab_id || !plt_id)
1665 return 0;
1666
1667 // Section ID's are ones based;
1668 symtab_id++;
1669 plt_id++;
1670
Michael Sartaina7499c92013-07-01 19:45:50 +00001671 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001672 if (!plt_hdr)
1673 return 0;
1674
Michael Sartaina7499c92013-07-01 19:45:50 +00001675 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001676 if (!sym_hdr)
1677 return 0;
1678
Greg Clayton3046e662013-07-10 01:23:25 +00001679 SectionList *section_list = m_sections_ap.get();
Stephen Wilson499b40e2011-03-30 16:07:05 +00001680 if (!section_list)
1681 return 0;
1682
1683 Section *rel_section = section_list->FindSectionByID(rel_id).get();
1684 if (!rel_section)
1685 return 0;
1686
Greg Claytone72dfb32012-02-24 01:59:29 +00001687 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
1688 if (!plt_section_sp)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001689 return 0;
1690
1691 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1692 if (!symtab)
1693 return 0;
1694
Greg Clayton3046e662013-07-10 01:23:25 +00001695 // sh_link points to associated string table.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001696 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1697 if (!strtab)
1698 return 0;
1699
1700 DataExtractor rel_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001701 if (!ReadSectionData(rel_section, rel_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001702 return 0;
1703
1704 DataExtractor symtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001705 if (!ReadSectionData(symtab, symtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001706 return 0;
1707
1708 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001709 if (!ReadSectionData(strtab, strtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001710 return 0;
1711
1712 unsigned rel_type = PLTRelocationType();
1713 if (!rel_type)
1714 return 0;
1715
Greg Claytone72dfb32012-02-24 01:59:29 +00001716 return ParsePLTRelocations (symbol_table,
1717 start_id,
1718 rel_type,
1719 &m_header,
1720 rel_hdr,
1721 plt_hdr,
1722 sym_hdr,
1723 plt_section_sp,
1724 rel_data,
1725 symtab_data,
1726 strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001727}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001728
Andrew MacPherson17220c12014-03-05 10:12:43 +00001729unsigned
1730ObjectFileELF::RelocateSection(Symtab* symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
1731 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,
1732 DataExtractor &rel_data, DataExtractor &symtab_data,
1733 DataExtractor &debug_data, Section* rel_section)
1734{
1735 ELFRelocation rel(rel_hdr->sh_type);
1736 lldb::addr_t offset = 0;
1737 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
1738 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
1739 reloc_info_fn reloc_type;
1740 reloc_info_fn reloc_symbol;
1741
1742 if (hdr->Is32Bit())
1743 {
1744 reloc_type = ELFRelocation::RelocType32;
1745 reloc_symbol = ELFRelocation::RelocSymbol32;
1746 }
1747 else
1748 {
1749 reloc_type = ELFRelocation::RelocType64;
1750 reloc_symbol = ELFRelocation::RelocSymbol64;
1751 }
1752
1753 for (unsigned i = 0; i < num_relocations; ++i)
1754 {
1755 if (rel.Parse(rel_data, &offset) == false)
1756 break;
1757
1758 Symbol* symbol = NULL;
1759
1760 if (hdr->Is32Bit())
1761 {
1762 switch (reloc_type(rel)) {
1763 case R_386_32:
1764 case R_386_PC32:
1765 default:
1766 assert(false && "unexpected relocation type");
1767 }
1768 } else {
1769 switch (reloc_type(rel)) {
1770 case R_X86_64_64:
1771 {
1772 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
1773 if (symbol)
1774 {
1775 addr_t value = symbol->GetAddress().GetFileAddress();
1776 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer();
1777 uint64_t* dst = reinterpret_cast<uint64_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset64(rel));
1778 *dst = value + ELFRelocation::RelocAddend64(rel);
1779 }
1780 break;
1781 }
1782 case R_X86_64_32:
1783 case R_X86_64_32S:
1784 {
1785 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
1786 if (symbol)
1787 {
1788 addr_t value = symbol->GetAddress().GetFileAddress();
1789 value += ELFRelocation::RelocAddend32(rel);
1790 assert((reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) ||
1791 (reloc_type(rel) == R_X86_64_32S &&
1792 ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN)));
1793 uint32_t truncated_addr = (value & 0xFFFFFFFF);
1794 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer();
1795 uint32_t* dst = reinterpret_cast<uint32_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel));
1796 *dst = truncated_addr;
1797 }
1798 break;
1799 }
1800 case R_X86_64_PC32:
1801 default:
1802 assert(false && "unexpected relocation type");
1803 }
1804 }
1805 }
1806
1807 return 0;
1808}
1809
1810unsigned
1811ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, user_id_t rel_id)
1812{
1813 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1814
1815 // Parse in the section list if needed.
1816 SectionList *section_list = GetSectionList();
1817 if (!section_list)
1818 return 0;
1819
1820 // Section ID's are ones based.
1821 user_id_t symtab_id = rel_hdr->sh_link + 1;
1822 user_id_t debug_id = rel_hdr->sh_info + 1;
1823
1824 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
1825 if (!symtab_hdr)
1826 return 0;
1827
1828 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);
1829 if (!debug_hdr)
1830 return 0;
1831
1832 Section *rel = section_list->FindSectionByID(rel_id).get();
1833 if (!rel)
1834 return 0;
1835
1836 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1837 if (!symtab)
1838 return 0;
1839
1840 Section *debug = section_list->FindSectionByID(debug_id).get();
1841 if (!debug)
1842 return 0;
1843
1844 DataExtractor rel_data;
1845 DataExtractor symtab_data;
1846 DataExtractor debug_data;
1847
1848 if (ReadSectionData(rel, rel_data) &&
1849 ReadSectionData(symtab, symtab_data) &&
1850 ReadSectionData(debug, debug_data))
1851 {
1852 RelocateSection(m_symtab_ap.get(), &m_header, rel_hdr, symtab_hdr, debug_hdr,
1853 rel_data, symtab_data, debug_data, debug);
1854 }
1855
1856 return 0;
1857}
1858
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001859Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +00001860ObjectFileELF::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001861{
Michael Sartaina7499c92013-07-01 19:45:50 +00001862 ModuleSP module_sp(GetModule());
Greg Clayton3046e662013-07-10 01:23:25 +00001863 if (!module_sp)
1864 return NULL;
Michael Sartaina7499c92013-07-01 19:45:50 +00001865
Greg Clayton3046e662013-07-10 01:23:25 +00001866 // We always want to use the main object file so we (hopefully) only have one cached copy
1867 // of our symtab, dynamic sections, etc.
1868 ObjectFile *module_obj_file = module_sp->GetObjectFile();
1869 if (module_obj_file && module_obj_file != this)
1870 return module_obj_file->GetSymtab();
1871
1872 if (m_symtab_ap.get() == NULL)
1873 {
1874 SectionList *section_list = GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001875 if (!section_list)
1876 return NULL;
1877
Greg Clayton3046e662013-07-10 01:23:25 +00001878 uint64_t symbol_id = 0;
1879 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Stephen Wilson499b40e2011-03-30 16:07:05 +00001880
Greg Clayton3046e662013-07-10 01:23:25 +00001881 m_symtab_ap.reset(new Symtab(this));
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001882
Michael Sartaina7499c92013-07-01 19:45:50 +00001883 // Sharable objects and dynamic executables usually have 2 distinct symbol
1884 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller
1885 // version of the symtab that only contains global symbols. The information found
1886 // in the dynsym is therefore also found in the symtab, while the reverse is not
1887 // necessarily true.
Greg Clayton3046e662013-07-10 01:23:25 +00001888 Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get();
1889 if (!symtab)
Michael Sartaina7499c92013-07-01 19:45:50 +00001890 {
1891 // The symtab section is non-allocable and can be stripped, so if it doesn't exist
1892 // then use the dynsym section which should always be there.
Greg Clayton3046e662013-07-10 01:23:25 +00001893 symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get();
Michael Sartaina7499c92013-07-01 19:45:50 +00001894 }
Greg Clayton3046e662013-07-10 01:23:25 +00001895 if (symtab)
1896 symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab);
Michael Sartaina7499c92013-07-01 19:45:50 +00001897
Michael Sartainf7899542013-08-22 21:25:53 +00001898 // DT_JMPREL
1899 // If present, this entry's d_ptr member holds the address of relocation
1900 // entries associated solely with the procedure linkage table. Separating
1901 // these relocation entries lets the dynamic linker ignore them during
1902 // process initialization, if lazy binding is enabled. If this entry is
1903 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must
1904 // also be present.
Greg Clayton3046e662013-07-10 01:23:25 +00001905 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
1906 if (symbol)
Michael Sartaina7499c92013-07-01 19:45:50 +00001907 {
Michael Sartainf7899542013-08-22 21:25:53 +00001908 // Synthesize trampoline symbols to help navigate the PLT.
Greg Clayton3046e662013-07-10 01:23:25 +00001909 addr_t addr = symbol->d_ptr;
1910 Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get();
1911 if (reloc_section)
Michael Sartaina7499c92013-07-01 19:45:50 +00001912 {
Greg Clayton3046e662013-07-10 01:23:25 +00001913 user_id_t reloc_id = reloc_section->GetID();
1914 const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id);
1915 assert(reloc_header);
Michael Sartaina7499c92013-07-01 19:45:50 +00001916
Greg Clayton3046e662013-07-10 01:23:25 +00001917 ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id);
Michael Sartaina7499c92013-07-01 19:45:50 +00001918 }
1919 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001920 }
Andrew MacPherson17220c12014-03-05 10:12:43 +00001921
1922 for (SectionHeaderCollIter I = m_section_headers.begin();
1923 I != m_section_headers.end(); ++I)
1924 {
1925 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL)
1926 {
1927 if (CalculateType() == eTypeObjectFile)
1928 {
1929 const char *section_name = I->section_name.AsCString("");
1930 if (strstr(section_name, ".rela.debug") ||
1931 strstr(section_name, ".rel.debug"))
1932 {
1933 const ELFSectionHeader &reloc_header = *I;
1934 user_id_t reloc_id = SectionIndex(I);
1935 RelocateDebugSections(&reloc_header, reloc_id);
1936 }
1937 }
1938 }
1939 }
Greg Clayton3046e662013-07-10 01:23:25 +00001940 return m_symtab_ap.get();
1941}
1942
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00001943Symbol *
1944ObjectFileELF::ResolveSymbolForAddress(const Address& so_addr, bool verify_unique)
1945{
1946 if (!m_symtab_ap.get())
1947 return nullptr; // GetSymtab() should be called first.
1948
1949 const SectionList *section_list = GetSectionList();
1950 if (!section_list)
1951 return nullptr;
1952
1953 if (DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo())
1954 {
1955 AddressRange range;
1956 if (eh_frame->GetAddressRange (so_addr, range))
1957 {
1958 const addr_t file_addr = range.GetBaseAddress().GetFileAddress();
1959 Symbol * symbol = verify_unique ? m_symtab_ap->FindSymbolContainingFileAddress(file_addr) : nullptr;
1960 if (symbol)
1961 return symbol;
1962
1963 // Note that a (stripped) symbol won't be found by GetSymtab()...
1964 lldb::SectionSP eh_sym_section_sp = section_list->FindSectionContainingFileAddress(file_addr);
1965 if (eh_sym_section_sp.get())
1966 {
1967 addr_t section_base = eh_sym_section_sp->GetFileAddress();
1968 addr_t offset = file_addr - section_base;
1969 uint64_t symbol_id = m_symtab_ap->GetNumSymbols();
1970
1971 Symbol eh_symbol(
1972 symbol_id, // Symbol table index.
1973 "???", // Symbol name.
1974 false, // Is the symbol name mangled?
1975 eSymbolTypeCode, // Type of this symbol.
1976 true, // Is this globally visible?
1977 false, // Is this symbol debug info?
1978 false, // Is this symbol a trampoline?
1979 true, // Is this symbol artificial?
1980 eh_sym_section_sp, // Section in which this symbol is defined or null.
1981 offset, // Offset in section or symbol value.
1982 range.GetByteSize(), // Size in bytes of this symbol.
1983 true, // Size is valid.
1984 0); // Symbol flags.
1985 if (symbol_id == m_symtab_ap->AddSymbol(eh_symbol))
1986 return m_symtab_ap->SymbolAtIndex(symbol_id);
1987 }
1988 }
1989 }
1990 return nullptr;
1991}
1992
1993
Greg Clayton3046e662013-07-10 01:23:25 +00001994bool
1995ObjectFileELF::IsStripped ()
1996{
1997 // TODO: determine this for ELF
1998 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001999}
2000
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002001//===----------------------------------------------------------------------===//
2002// Dump
2003//
2004// Dump the specifics of the runtime file container (such as any headers
2005// segments, sections, etc).
2006//----------------------------------------------------------------------
2007void
2008ObjectFileELF::Dump(Stream *s)
2009{
2010 DumpELFHeader(s, m_header);
2011 s->EOL();
2012 DumpELFProgramHeaders(s);
2013 s->EOL();
2014 DumpELFSectionHeaders(s);
2015 s->EOL();
2016 SectionList *section_list = GetSectionList();
2017 if (section_list)
Greg Clayton10177aa2010-12-08 05:08:21 +00002018 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002019 Symtab *symtab = GetSymtab();
2020 if (symtab)
Greg Claytone0d378b2011-03-24 21:19:54 +00002021 symtab->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002022 s->EOL();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002023 DumpDependentModules(s);
2024 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002025}
2026
2027//----------------------------------------------------------------------
2028// DumpELFHeader
2029//
2030// Dump the ELF header to the specified output stream
2031//----------------------------------------------------------------------
2032void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002033ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002034{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002035 s->PutCString("ELF Header\n");
2036 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
2037 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
2038 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
2039 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
2040 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
2041 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
2042 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002043
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002044 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
2045 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002046 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
2047 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
2048 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
2049
2050 s->Printf("e_type = 0x%4.4x ", header.e_type);
2051 DumpELFHeader_e_type(s, header.e_type);
2052 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
2053 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Daniel Malead01b2952012-11-29 21:49:15 +00002054 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
2055 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
2056 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002057 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
2058 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
2059 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
2060 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
2061 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
2062 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
2063 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
2064}
2065
2066//----------------------------------------------------------------------
2067// DumpELFHeader_e_type
2068//
2069// Dump an token value for the ELF header member e_type
2070//----------------------------------------------------------------------
2071void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002072ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002073{
2074 switch (e_type)
2075 {
2076 case ET_NONE: *s << "ET_NONE"; break;
2077 case ET_REL: *s << "ET_REL"; break;
2078 case ET_EXEC: *s << "ET_EXEC"; break;
2079 case ET_DYN: *s << "ET_DYN"; break;
2080 case ET_CORE: *s << "ET_CORE"; break;
2081 default:
2082 break;
2083 }
2084}
2085
2086//----------------------------------------------------------------------
2087// DumpELFHeader_e_ident_EI_DATA
2088//
2089// Dump an token value for the ELF header member e_ident[EI_DATA]
2090//----------------------------------------------------------------------
2091void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002092ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002093{
2094 switch (ei_data)
2095 {
2096 case ELFDATANONE: *s << "ELFDATANONE"; break;
2097 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
2098 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
2099 default:
2100 break;
2101 }
2102}
2103
2104
2105//----------------------------------------------------------------------
2106// DumpELFProgramHeader
2107//
2108// Dump a single ELF program header to the specified output stream
2109//----------------------------------------------------------------------
2110void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002111ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002112{
2113 DumpELFProgramHeader_p_type(s, ph.p_type);
Daniel Malead01b2952012-11-29 21:49:15 +00002114 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr);
2115 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 +00002116
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002117 DumpELFProgramHeader_p_flags(s, ph.p_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00002118 s->Printf(") %8.8" PRIx64, ph.p_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002119}
2120
2121//----------------------------------------------------------------------
2122// DumpELFProgramHeader_p_type
2123//
2124// Dump an token value for the ELF program header member p_type which
2125// describes the type of the program header
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002126// ----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002127void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002128ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002129{
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00002130 const int kStrWidth = 15;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131 switch (p_type)
2132 {
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00002133 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
2134 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
2135 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
2136 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
2137 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
2138 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
2139 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
2140 CASE_AND_STREAM(s, PT_TLS , kStrWidth);
2141 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002142 default:
2143 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
2144 break;
2145 }
2146}
2147
2148
2149//----------------------------------------------------------------------
2150// DumpELFProgramHeader_p_flags
2151//
2152// Dump an token value for the ELF program header member p_flags
2153//----------------------------------------------------------------------
2154void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002155ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002156{
2157 *s << ((p_flags & PF_X) ? "PF_X" : " ")
2158 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
2159 << ((p_flags & PF_W) ? "PF_W" : " ")
2160 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
2161 << ((p_flags & PF_R) ? "PF_R" : " ");
2162}
2163
2164//----------------------------------------------------------------------
2165// DumpELFProgramHeaders
2166//
2167// Dump all of the ELF program header to the specified output stream
2168//----------------------------------------------------------------------
2169void
2170ObjectFileELF::DumpELFProgramHeaders(Stream *s)
2171{
2172 if (ParseProgramHeaders())
2173 {
2174 s->PutCString("Program Headers\n");
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00002175 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002176 "p_filesz p_memsz p_flags p_align\n");
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00002177 s->PutCString("==== --------------- -------- -------- -------- "
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002178 "-------- -------- ------------------------- --------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002179
2180 uint32_t idx = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002181 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
2182 I != m_program_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002183 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002184 s->Printf("[%2u] ", idx);
2185 ObjectFileELF::DumpELFProgramHeader(s, *I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002186 s->EOL();
2187 }
2188 }
2189}
2190
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002191//----------------------------------------------------------------------
2192// DumpELFSectionHeader
2193//
2194// Dump a single ELF section header to the specified output stream
2195//----------------------------------------------------------------------
2196void
Michael Sartaina7499c92013-07-01 19:45:50 +00002197ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002198{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002199 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002200 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Daniel Malead01b2952012-11-29 21:49:15 +00002201 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002202 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00002203 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 +00002204 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
Daniel Malead01b2952012-11-29 21:49:15 +00002205 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002206}
2207
2208//----------------------------------------------------------------------
2209// DumpELFSectionHeader_sh_type
2210//
2211// Dump an token value for the ELF section header member sh_type which
2212// describes the type of the section
2213//----------------------------------------------------------------------
2214void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002215ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002216{
2217 const int kStrWidth = 12;
2218 switch (sh_type)
2219 {
2220 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
2221 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
2222 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
2223 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
2224 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
2225 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
2226 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
2227 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
2228 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
2229 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
2230 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
2231 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
2232 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
2233 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
2234 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
2235 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
2236 default:
2237 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
2238 break;
2239 }
2240}
2241
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002242//----------------------------------------------------------------------
2243// DumpELFSectionHeader_sh_flags
2244//
2245// Dump an token value for the ELF section header member sh_flags
2246//----------------------------------------------------------------------
2247void
Greg Claytonc7bece562013-01-25 18:06:21 +00002248ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002249{
2250 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
2251 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
2252 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
2253 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
2254 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
2255}
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002256
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002257//----------------------------------------------------------------------
2258// DumpELFSectionHeaders
2259//
2260// Dump all of the ELF section header to the specified output stream
2261//----------------------------------------------------------------------
2262void
2263ObjectFileELF::DumpELFSectionHeaders(Stream *s)
2264{
Michael Sartaina7499c92013-07-01 19:45:50 +00002265 if (!ParseSectionHeaders())
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002266 return;
2267
2268 s->PutCString("Section Headers\n");
2269 s->PutCString("IDX name type flags "
2270 "addr offset size link info addralgn "
2271 "entsize Name\n");
2272 s->PutCString("==== -------- ------------ -------------------------------- "
2273 "-------- -------- -------- -------- -------- -------- "
2274 "-------- ====================\n");
2275
2276 uint32_t idx = 0;
2277 for (SectionHeaderCollConstIter I = m_section_headers.begin();
2278 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002279 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002280 s->Printf("[%2u] ", idx);
2281 ObjectFileELF::DumpELFSectionHeader(s, *I);
Michael Sartaina7499c92013-07-01 19:45:50 +00002282 const char* section_name = I->section_name.AsCString("");
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002283 if (section_name)
2284 *s << ' ' << section_name << "\n";
2285 }
2286}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002287
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002288void
2289ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
2290{
2291 size_t num_modules = ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002292
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002293 if (num_modules > 0)
2294 {
2295 s->PutCString("Dependent Modules:\n");
2296 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002297 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002298 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
2299 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002300 }
2301 }
2302}
2303
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002304bool
Greg Clayton514487e2011-02-15 21:59:32 +00002305ObjectFileELF::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002306{
Stephen Wilson3f4200fd2011-02-24 19:16:15 +00002307 if (!ParseHeader())
2308 return false;
2309
Greg Claytone0d378b2011-03-24 21:19:54 +00002310 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
Greg Clayton64195a22011-02-23 00:35:02 +00002311 arch.GetTriple().setOSName (Host::GetOSString().GetCString());
2312 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002313 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002314}
2315
Greg Clayton9e00b6a652011-07-09 00:41:34 +00002316ObjectFile::Type
2317ObjectFileELF::CalculateType()
2318{
2319 switch (m_header.e_type)
2320 {
2321 case llvm::ELF::ET_NONE:
2322 // 0 - No file type
2323 return eTypeUnknown;
2324
2325 case llvm::ELF::ET_REL:
2326 // 1 - Relocatable file
2327 return eTypeObjectFile;
2328
2329 case llvm::ELF::ET_EXEC:
2330 // 2 - Executable file
2331 return eTypeExecutable;
2332
2333 case llvm::ELF::ET_DYN:
2334 // 3 - Shared object file
2335 return eTypeSharedLibrary;
2336
2337 case ET_CORE:
2338 // 4 - Core file
2339 return eTypeCoreFile;
2340
2341 default:
2342 break;
2343 }
2344 return eTypeUnknown;
2345}
2346
2347ObjectFile::Strata
2348ObjectFileELF::CalculateStrata()
2349{
2350 switch (m_header.e_type)
2351 {
2352 case llvm::ELF::ET_NONE:
2353 // 0 - No file type
2354 return eStrataUnknown;
2355
2356 case llvm::ELF::ET_REL:
2357 // 1 - Relocatable file
2358 return eStrataUnknown;
2359
2360 case llvm::ELF::ET_EXEC:
2361 // 2 - Executable file
2362 // TODO: is there any way to detect that an executable is a kernel
2363 // related executable by inspecting the program headers, section
2364 // headers, symbols, or any other flag bits???
2365 return eStrataUser;
2366
2367 case llvm::ELF::ET_DYN:
2368 // 3 - Shared object file
2369 // TODO: is there any way to detect that an shared library is a kernel
2370 // related executable by inspecting the program headers, section
2371 // headers, symbols, or any other flag bits???
2372 return eStrataUnknown;
2373
2374 case ET_CORE:
2375 // 4 - Core file
2376 // TODO: is there any way to detect that an core file is a kernel
2377 // related executable by inspecting the program headers, section
2378 // headers, symbols, or any other flag bits???
2379 return eStrataUnknown;
2380
2381 default:
2382 break;
2383 }
2384 return eStrataUnknown;
2385}
2386