blob: 688b64cca8149d9037ac944dcab85953fb9eb04d [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 {
Enrico Granataafcbdb12014-03-25 20:53:33 +0000529 segment_data_end = std::max<unsigned long long> (I->p_offset + I->p_filesz, segment_data_end);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000530 }
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.
Greg Clayton48672af2014-06-24 22:22:43 +00001239 __builtin_ffs(header.sh_addralign), // Alignment of the section
Greg Clayton3046e662013-07-10 01:23:25 +00001240 header.sh_flags)); // Flags for this section.
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001241
Greg Clayton741f3f92012-03-27 21:10:07 +00001242 if (is_thread_specific)
1243 section_sp->SetIsThreadSpecific (is_thread_specific);
1244 m_sections_ap->AddSection(section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245 }
1246 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001247
Greg Clayton3046e662013-07-10 01:23:25 +00001248 if (m_sections_ap.get())
1249 {
1250 if (GetType() == eTypeDebugInfo)
1251 {
1252 static const SectionType g_sections[] =
1253 {
1254 eSectionTypeDWARFDebugAranges,
1255 eSectionTypeDWARFDebugInfo,
1256 eSectionTypeDWARFDebugAbbrev,
1257 eSectionTypeDWARFDebugFrame,
1258 eSectionTypeDWARFDebugLine,
1259 eSectionTypeDWARFDebugStr,
1260 eSectionTypeDWARFDebugLoc,
1261 eSectionTypeDWARFDebugMacInfo,
1262 eSectionTypeDWARFDebugPubNames,
1263 eSectionTypeDWARFDebugPubTypes,
1264 eSectionTypeDWARFDebugRanges,
1265 eSectionTypeELFSymbolTable,
1266 };
1267 SectionList *elf_section_list = m_sections_ap.get();
1268 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx)
1269 {
1270 SectionType section_type = g_sections[idx];
1271 SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true));
1272 if (section_sp)
1273 {
1274 SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true));
1275 if (module_section_sp)
1276 unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp);
1277 else
1278 unified_section_list.AddSection (section_sp);
1279 }
1280 }
1281 }
1282 else
1283 {
1284 unified_section_list = *m_sections_ap;
1285 }
1286 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287}
1288
Greg Clayton3046e662013-07-10 01:23:25 +00001289// private
Michael Sartaina7499c92013-07-01 19:45:50 +00001290unsigned
Greg Clayton3046e662013-07-10 01:23:25 +00001291ObjectFileELF::ParseSymbols (Symtab *symtab,
1292 user_id_t start_id,
1293 SectionList *section_list,
1294 const size_t num_symbols,
1295 const DataExtractor &symtab_data,
1296 const DataExtractor &strtab_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001298 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00001299 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001300
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001301 static ConstString text_section_name(".text");
1302 static ConstString init_section_name(".init");
1303 static ConstString fini_section_name(".fini");
1304 static ConstString ctors_section_name(".ctors");
1305 static ConstString dtors_section_name(".dtors");
1306
1307 static ConstString data_section_name(".data");
1308 static ConstString rodata_section_name(".rodata");
1309 static ConstString rodata1_section_name(".rodata1");
1310 static ConstString data2_section_name(".data1");
1311 static ConstString bss_section_name(".bss");
1312
Greg Clayton9594f4c2013-04-13 23:17:23 +00001313 //StreamFile strm(stdout, false);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001314 unsigned i;
1315 for (i = 0; i < num_symbols; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001317 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318 break;
Greg Clayton9594f4c2013-04-13 23:17:23 +00001319
1320 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
1321
Andrew MacPherson17220c12014-03-05 10:12:43 +00001322 // No need to add non-section symbols that have no names
1323 if (symbol.getType() != STT_SECTION &&
1324 (symbol_name == NULL || symbol_name[0] == '\0'))
Greg Clayton9594f4c2013-04-13 23:17:23 +00001325 continue;
1326
1327 //symbol.Dump (&strm, i, &strtab_data, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328
Greg Claytone72dfb32012-02-24 01:59:29 +00001329 SectionSP symbol_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330 SymbolType symbol_type = eSymbolTypeInvalid;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001331 Elf64_Half symbol_idx = symbol.st_shndx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001333 switch (symbol_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334 {
1335 case SHN_ABS:
1336 symbol_type = eSymbolTypeAbsolute;
1337 break;
1338 case SHN_UNDEF:
1339 symbol_type = eSymbolTypeUndefined;
1340 break;
1341 default:
Greg Claytone72dfb32012-02-24 01:59:29 +00001342 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343 break;
1344 }
1345
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001346 // If a symbol is undefined do not process it further even if it has a STT type
1347 if (symbol_type != eSymbolTypeUndefined)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348 {
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001349 switch (symbol.getType())
1350 {
1351 default:
1352 case STT_NOTYPE:
1353 // The symbol's type is not specified.
1354 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001356 case STT_OBJECT:
1357 // The symbol is associated with a data object, such as a variable,
1358 // an array, etc.
1359 symbol_type = eSymbolTypeData;
1360 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001362 case STT_FUNC:
1363 // The symbol is associated with a function or other executable code.
1364 symbol_type = eSymbolTypeCode;
1365 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001366
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001367 case STT_SECTION:
1368 // The symbol is associated with a section. Symbol table entries of
1369 // this type exist primarily for relocation and normally have
1370 // STB_LOCAL binding.
1371 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001372
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001373 case STT_FILE:
1374 // Conventionally, the symbol's name gives the name of the source
1375 // file associated with the object file. A file symbol has STB_LOCAL
1376 // binding, its section index is SHN_ABS, and it precedes the other
1377 // STB_LOCAL symbols for the file, if it is present.
Greg Clayton9594f4c2013-04-13 23:17:23 +00001378 symbol_type = eSymbolTypeSourceFile;
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001379 break;
Matt Kopec00049b82013-02-27 20:13:38 +00001380
1381 case STT_GNU_IFUNC:
1382 // The symbol is associated with an indirect function. The actual
1383 // function will be resolved if it is referenced.
1384 symbol_type = eSymbolTypeResolver;
1385 break;
Matt Kopec92dd5cf2013-02-12 18:30:30 +00001386 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387 }
1388
1389 if (symbol_type == eSymbolTypeInvalid)
1390 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001391 if (symbol_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001393 const ConstString &sect_name = symbol_section_sp->GetName();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001394 if (sect_name == text_section_name ||
1395 sect_name == init_section_name ||
1396 sect_name == fini_section_name ||
1397 sect_name == ctors_section_name ||
1398 sect_name == dtors_section_name)
1399 {
1400 symbol_type = eSymbolTypeCode;
1401 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001402 else if (sect_name == data_section_name ||
1403 sect_name == data2_section_name ||
1404 sect_name == rodata_section_name ||
1405 sect_name == rodata1_section_name ||
1406 sect_name == bss_section_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407 {
1408 symbol_type = eSymbolTypeData;
1409 }
1410 }
1411 }
1412
Greg Clayton3046e662013-07-10 01:23:25 +00001413 // If the symbol section we've found has no data (SHT_NOBITS), then check the module section
1414 // 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 +00001415 if (symbol_section_sp && (symbol_section_sp->GetFileSize() == 0))
1416 {
1417 ModuleSP module_sp(GetModule());
1418 if (module_sp)
1419 {
Greg Clayton3046e662013-07-10 01:23:25 +00001420 SectionList *module_section_list = module_sp->GetSectionList();
1421 if (module_section_list && module_section_list != section_list)
Michael Sartaina7499c92013-07-01 19:45:50 +00001422 {
1423 const ConstString &sect_name = symbol_section_sp->GetName();
Greg Clayton3046e662013-07-10 01:23:25 +00001424 lldb::SectionSP section_sp (module_section_list->FindSectionByName (sect_name));
Michael Sartaina7499c92013-07-01 19:45:50 +00001425 if (section_sp && section_sp->GetFileSize())
1426 {
1427 symbol_section_sp = section_sp;
1428 }
1429 }
1430 }
1431 }
1432
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433 uint64_t symbol_value = symbol.st_value;
Andrew MacPherson17220c12014-03-05 10:12:43 +00001434 if (symbol_section_sp && CalculateType() != ObjectFile::Type::eTypeObjectFile)
Greg Claytone72dfb32012-02-24 01:59:29 +00001435 symbol_value -= symbol_section_sp->GetFileAddress();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001436 bool is_global = symbol.getBinding() == STB_GLOBAL;
1437 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
Greg Clayton47037bc2012-03-27 02:40:46 +00001438 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001439 Symbol dc_symbol(
Greg Claytone72dfb32012-02-24 01:59:29 +00001440 i + start_id, // ID is the original symbol table index.
1441 symbol_name, // Symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +00001442 is_mangled, // Is the symbol name mangled?
Greg Claytone72dfb32012-02-24 01:59:29 +00001443 symbol_type, // Type of this symbol
1444 is_global, // Is this globally visible?
1445 false, // Is this symbol debug info?
1446 false, // Is this symbol a trampoline?
1447 false, // Is this symbol artificial?
1448 symbol_section_sp, // Section in which this symbol is defined or null.
1449 symbol_value, // Offset in section or symbol value.
1450 symbol.st_size, // Size in bytes of this symbol.
Greg Clayton9594f4c2013-04-13 23:17:23 +00001451 true, // Size is valid
Greg Claytone72dfb32012-02-24 01:59:29 +00001452 flags); // Symbol flags.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001453 symtab->AddSymbol(dc_symbol);
1454 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00001455
1456 return i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457}
1458
Stephen Wilson499b40e2011-03-30 16:07:05 +00001459unsigned
Greg Clayton3046e662013-07-10 01:23:25 +00001460ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, lldb_private::Section *symtab)
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001461{
Greg Clayton3046e662013-07-10 01:23:25 +00001462 if (symtab->GetObjectFile() != this)
1463 {
1464 // If the symbol table section is owned by a different object file, have it do the
1465 // parsing.
1466 ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile());
1467 return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab);
1468 }
1469
1470 // Get section list for this object file.
1471 SectionList *section_list = m_sections_ap.get();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001472 if (!section_list)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001473 return 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001474
Greg Clayton3046e662013-07-10 01:23:25 +00001475 user_id_t symtab_id = symtab->GetID();
1476 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
Michael Sartaina7499c92013-07-01 19:45:50 +00001477 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
1478 symtab_hdr->sh_type == SHT_DYNSYM);
1479
Greg Clayton3046e662013-07-10 01:23:25 +00001480 // sh_link: section header index of associated string table.
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001481 // Section ID's are ones based.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001482 user_id_t strtab_id = symtab_hdr->sh_link + 1;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001483 Section *strtab = section_list->FindSectionByID(strtab_id).get();
Greg Clayton3046e662013-07-10 01:23:25 +00001484
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001485 if (symtab && strtab)
1486 {
Greg Clayton3046e662013-07-10 01:23:25 +00001487 assert (symtab->GetObjectFile() == this);
1488 assert (strtab->GetObjectFile() == this);
1489
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001490 DataExtractor symtab_data;
1491 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001492 if (ReadSectionData(symtab, symtab_data) &&
1493 ReadSectionData(strtab, strtab_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001494 {
Greg Clayton3046e662013-07-10 01:23:25 +00001495 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
1496
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +00001497 return ParseSymbols(symbol_table, start_id, section_list,
1498 num_symbols, symtab_data, strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001499 }
1500 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00001501
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +00001502 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001503}
1504
1505size_t
1506ObjectFileELF::ParseDynamicSymbols()
1507{
1508 if (m_dynamic_symbols.size())
1509 return m_dynamic_symbols.size();
1510
Stephen Wilson499b40e2011-03-30 16:07:05 +00001511 SectionList *section_list = GetSectionList();
1512 if (!section_list)
Bill Wendlinged24dcc2012-04-03 07:50:11 +00001513 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001514
Greg Clayton3046e662013-07-10 01:23:25 +00001515 // Find the SHT_DYNAMIC section.
1516 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
Stephen Wilson499b40e2011-03-30 16:07:05 +00001517 if (!dynsym)
Bill Wendlinged24dcc2012-04-03 07:50:11 +00001518 return 0;
Greg Clayton3046e662013-07-10 01:23:25 +00001519 assert (dynsym->GetObjectFile() == this);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001520
1521 ELFDynamic symbol;
1522 DataExtractor dynsym_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001523 if (ReadSectionData(dynsym, dynsym_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001524 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001525 const lldb::offset_t section_size = dynsym_data.GetByteSize();
1526 lldb::offset_t cursor = 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001527
1528 while (cursor < section_size)
1529 {
Stephen Wilson499b40e2011-03-30 16:07:05 +00001530 if (!symbol.Parse(dynsym_data, &cursor))
1531 break;
1532
1533 m_dynamic_symbols.push_back(symbol);
1534 }
1535 }
1536
1537 return m_dynamic_symbols.size();
1538}
1539
1540const ELFDynamic *
1541ObjectFileELF::FindDynamicSymbol(unsigned tag)
1542{
1543 if (!ParseDynamicSymbols())
1544 return NULL;
1545
Stephen Wilson499b40e2011-03-30 16:07:05 +00001546 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
1547 DynamicSymbolCollIter E = m_dynamic_symbols.end();
1548 for ( ; I != E; ++I)
1549 {
1550 ELFDynamic *symbol = &*I;
1551
1552 if (symbol->d_tag == tag)
1553 return symbol;
1554 }
1555
1556 return NULL;
1557}
1558
Stephen Wilson499b40e2011-03-30 16:07:05 +00001559unsigned
1560ObjectFileELF::PLTRelocationType()
1561{
Michael Sartainf7899542013-08-22 21:25:53 +00001562 // DT_PLTREL
1563 // This member specifies the type of relocation entry to which the
1564 // procedure linkage table refers. The d_val member holds DT_REL or
1565 // DT_RELA, as appropriate. All relocations in a procedure linkage table
1566 // must use the same relocation.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001567 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
1568
1569 if (symbol)
1570 return symbol->d_val;
1571
1572 return 0;
1573}
1574
1575static unsigned
1576ParsePLTRelocations(Symtab *symbol_table,
1577 user_id_t start_id,
1578 unsigned rel_type,
1579 const ELFHeader *hdr,
1580 const ELFSectionHeader *rel_hdr,
1581 const ELFSectionHeader *plt_hdr,
1582 const ELFSectionHeader *sym_hdr,
Greg Claytone72dfb32012-02-24 01:59:29 +00001583 const lldb::SectionSP &plt_section_sp,
Stephen Wilson499b40e2011-03-30 16:07:05 +00001584 DataExtractor &rel_data,
1585 DataExtractor &symtab_data,
1586 DataExtractor &strtab_data)
1587{
1588 ELFRelocation rel(rel_type);
1589 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00001590 lldb::offset_t offset = 0;
Michael Sartainf7899542013-08-22 21:25:53 +00001591 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 bytes.
1592 // So round the entsize up by the alignment if addralign is set.
1593 const elf_xword plt_entsize = plt_hdr->sh_addralign ?
1594 llvm::RoundUpToAlignment (plt_hdr->sh_entsize, plt_hdr->sh_addralign) : plt_hdr->sh_entsize;
Greg Claytonc7bece562013-01-25 18:06:21 +00001595 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001596
1597 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
1598 reloc_info_fn reloc_type;
1599 reloc_info_fn reloc_symbol;
1600
Greg Claytond091afe2012-11-12 22:53:16 +00001601 if (hdr->Is32Bit())
Stephen Wilson499b40e2011-03-30 16:07:05 +00001602 {
1603 reloc_type = ELFRelocation::RelocType32;
1604 reloc_symbol = ELFRelocation::RelocSymbol32;
1605 }
1606 else
1607 {
1608 reloc_type = ELFRelocation::RelocType64;
1609 reloc_symbol = ELFRelocation::RelocSymbol64;
1610 }
1611
1612 unsigned slot_type = hdr->GetRelocationJumpSlotType();
1613 unsigned i;
1614 for (i = 0; i < num_relocations; ++i)
1615 {
1616 if (rel.Parse(rel_data, &offset) == false)
1617 break;
1618
1619 if (reloc_type(rel) != slot_type)
1620 continue;
1621
Greg Claytonc7bece562013-01-25 18:06:21 +00001622 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001623 uint64_t plt_index = (i + 1) * plt_entsize;
1624
1625 if (!symbol.Parse(symtab_data, &symbol_offset))
1626 break;
1627
1628 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Greg Clayton47037bc2012-03-27 02:40:46 +00001629 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001630
1631 Symbol jump_symbol(
1632 i + start_id, // Symbol table index
1633 symbol_name, // symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +00001634 is_mangled, // is the symbol name mangled?
Stephen Wilson499b40e2011-03-30 16:07:05 +00001635 eSymbolTypeTrampoline, // Type of this symbol
1636 false, // Is this globally visible?
1637 false, // Is this symbol debug info?
1638 true, // Is this symbol a trampoline?
1639 true, // Is this symbol artificial?
Greg Claytone72dfb32012-02-24 01:59:29 +00001640 plt_section_sp, // Section in which this symbol is defined or null.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001641 plt_index, // Offset in section or symbol value.
1642 plt_entsize, // Size in bytes of this symbol.
Greg Clayton9594f4c2013-04-13 23:17:23 +00001643 true, // Size is valid
Stephen Wilson499b40e2011-03-30 16:07:05 +00001644 0); // Symbol flags.
1645
1646 symbol_table->AddSymbol(jump_symbol);
1647 }
1648
1649 return i;
1650}
1651
1652unsigned
1653ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
1654 user_id_t start_id,
Michael Sartaina7499c92013-07-01 19:45:50 +00001655 const ELFSectionHeaderInfo *rel_hdr,
Stephen Wilson499b40e2011-03-30 16:07:05 +00001656 user_id_t rel_id)
1657{
1658 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1659
Greg Clayton3046e662013-07-10 01:23:25 +00001660 // The link field points to the associated symbol table. The info field
Stephen Wilson499b40e2011-03-30 16:07:05 +00001661 // points to the section holding the plt.
1662 user_id_t symtab_id = rel_hdr->sh_link;
1663 user_id_t plt_id = rel_hdr->sh_info;
1664
1665 if (!symtab_id || !plt_id)
1666 return 0;
1667
1668 // Section ID's are ones based;
1669 symtab_id++;
1670 plt_id++;
1671
Michael Sartaina7499c92013-07-01 19:45:50 +00001672 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001673 if (!plt_hdr)
1674 return 0;
1675
Michael Sartaina7499c92013-07-01 19:45:50 +00001676 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001677 if (!sym_hdr)
1678 return 0;
1679
Greg Clayton3046e662013-07-10 01:23:25 +00001680 SectionList *section_list = m_sections_ap.get();
Stephen Wilson499b40e2011-03-30 16:07:05 +00001681 if (!section_list)
1682 return 0;
1683
1684 Section *rel_section = section_list->FindSectionByID(rel_id).get();
1685 if (!rel_section)
1686 return 0;
1687
Greg Claytone72dfb32012-02-24 01:59:29 +00001688 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
1689 if (!plt_section_sp)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001690 return 0;
1691
1692 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1693 if (!symtab)
1694 return 0;
1695
Greg Clayton3046e662013-07-10 01:23:25 +00001696 // sh_link points to associated string table.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001697 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1698 if (!strtab)
1699 return 0;
1700
1701 DataExtractor rel_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001702 if (!ReadSectionData(rel_section, rel_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001703 return 0;
1704
1705 DataExtractor symtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001706 if (!ReadSectionData(symtab, symtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001707 return 0;
1708
1709 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001710 if (!ReadSectionData(strtab, strtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001711 return 0;
1712
1713 unsigned rel_type = PLTRelocationType();
1714 if (!rel_type)
1715 return 0;
1716
Greg Claytone72dfb32012-02-24 01:59:29 +00001717 return ParsePLTRelocations (symbol_table,
1718 start_id,
1719 rel_type,
1720 &m_header,
1721 rel_hdr,
1722 plt_hdr,
1723 sym_hdr,
1724 plt_section_sp,
1725 rel_data,
1726 symtab_data,
1727 strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001728}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729
Andrew MacPherson17220c12014-03-05 10:12:43 +00001730unsigned
1731ObjectFileELF::RelocateSection(Symtab* symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
1732 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,
1733 DataExtractor &rel_data, DataExtractor &symtab_data,
1734 DataExtractor &debug_data, Section* rel_section)
1735{
1736 ELFRelocation rel(rel_hdr->sh_type);
1737 lldb::addr_t offset = 0;
1738 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
1739 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
1740 reloc_info_fn reloc_type;
1741 reloc_info_fn reloc_symbol;
1742
1743 if (hdr->Is32Bit())
1744 {
1745 reloc_type = ELFRelocation::RelocType32;
1746 reloc_symbol = ELFRelocation::RelocSymbol32;
1747 }
1748 else
1749 {
1750 reloc_type = ELFRelocation::RelocType64;
1751 reloc_symbol = ELFRelocation::RelocSymbol64;
1752 }
1753
1754 for (unsigned i = 0; i < num_relocations; ++i)
1755 {
1756 if (rel.Parse(rel_data, &offset) == false)
1757 break;
1758
1759 Symbol* symbol = NULL;
1760
1761 if (hdr->Is32Bit())
1762 {
1763 switch (reloc_type(rel)) {
1764 case R_386_32:
1765 case R_386_PC32:
1766 default:
1767 assert(false && "unexpected relocation type");
1768 }
1769 } else {
1770 switch (reloc_type(rel)) {
1771 case R_X86_64_64:
1772 {
1773 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
1774 if (symbol)
1775 {
1776 addr_t value = symbol->GetAddress().GetFileAddress();
1777 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer();
1778 uint64_t* dst = reinterpret_cast<uint64_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset64(rel));
1779 *dst = value + ELFRelocation::RelocAddend64(rel);
1780 }
1781 break;
1782 }
1783 case R_X86_64_32:
1784 case R_X86_64_32S:
1785 {
1786 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
1787 if (symbol)
1788 {
1789 addr_t value = symbol->GetAddress().GetFileAddress();
1790 value += ELFRelocation::RelocAddend32(rel);
1791 assert((reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) ||
1792 (reloc_type(rel) == R_X86_64_32S &&
1793 ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN)));
1794 uint32_t truncated_addr = (value & 0xFFFFFFFF);
1795 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer();
1796 uint32_t* dst = reinterpret_cast<uint32_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel));
1797 *dst = truncated_addr;
1798 }
1799 break;
1800 }
1801 case R_X86_64_PC32:
1802 default:
1803 assert(false && "unexpected relocation type");
1804 }
1805 }
1806 }
1807
1808 return 0;
1809}
1810
1811unsigned
1812ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, user_id_t rel_id)
1813{
1814 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1815
1816 // Parse in the section list if needed.
1817 SectionList *section_list = GetSectionList();
1818 if (!section_list)
1819 return 0;
1820
1821 // Section ID's are ones based.
1822 user_id_t symtab_id = rel_hdr->sh_link + 1;
1823 user_id_t debug_id = rel_hdr->sh_info + 1;
1824
1825 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
1826 if (!symtab_hdr)
1827 return 0;
1828
1829 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);
1830 if (!debug_hdr)
1831 return 0;
1832
1833 Section *rel = section_list->FindSectionByID(rel_id).get();
1834 if (!rel)
1835 return 0;
1836
1837 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1838 if (!symtab)
1839 return 0;
1840
1841 Section *debug = section_list->FindSectionByID(debug_id).get();
1842 if (!debug)
1843 return 0;
1844
1845 DataExtractor rel_data;
1846 DataExtractor symtab_data;
1847 DataExtractor debug_data;
1848
1849 if (ReadSectionData(rel, rel_data) &&
1850 ReadSectionData(symtab, symtab_data) &&
1851 ReadSectionData(debug, debug_data))
1852 {
1853 RelocateSection(m_symtab_ap.get(), &m_header, rel_hdr, symtab_hdr, debug_hdr,
1854 rel_data, symtab_data, debug_data, debug);
1855 }
1856
1857 return 0;
1858}
1859
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001860Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +00001861ObjectFileELF::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862{
Michael Sartaina7499c92013-07-01 19:45:50 +00001863 ModuleSP module_sp(GetModule());
Greg Clayton3046e662013-07-10 01:23:25 +00001864 if (!module_sp)
1865 return NULL;
Michael Sartaina7499c92013-07-01 19:45:50 +00001866
Greg Clayton3046e662013-07-10 01:23:25 +00001867 // We always want to use the main object file so we (hopefully) only have one cached copy
1868 // of our symtab, dynamic sections, etc.
1869 ObjectFile *module_obj_file = module_sp->GetObjectFile();
1870 if (module_obj_file && module_obj_file != this)
1871 return module_obj_file->GetSymtab();
1872
1873 if (m_symtab_ap.get() == NULL)
1874 {
1875 SectionList *section_list = GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001876 if (!section_list)
1877 return NULL;
1878
Greg Clayton3046e662013-07-10 01:23:25 +00001879 uint64_t symbol_id = 0;
1880 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Stephen Wilson499b40e2011-03-30 16:07:05 +00001881
Greg Clayton3046e662013-07-10 01:23:25 +00001882 m_symtab_ap.reset(new Symtab(this));
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001883
Michael Sartaina7499c92013-07-01 19:45:50 +00001884 // Sharable objects and dynamic executables usually have 2 distinct symbol
1885 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller
1886 // version of the symtab that only contains global symbols. The information found
1887 // in the dynsym is therefore also found in the symtab, while the reverse is not
1888 // necessarily true.
Greg Clayton3046e662013-07-10 01:23:25 +00001889 Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get();
1890 if (!symtab)
Michael Sartaina7499c92013-07-01 19:45:50 +00001891 {
1892 // The symtab section is non-allocable and can be stripped, so if it doesn't exist
1893 // then use the dynsym section which should always be there.
Greg Clayton3046e662013-07-10 01:23:25 +00001894 symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get();
Michael Sartaina7499c92013-07-01 19:45:50 +00001895 }
Greg Clayton3046e662013-07-10 01:23:25 +00001896 if (symtab)
1897 symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab);
Michael Sartaina7499c92013-07-01 19:45:50 +00001898
Michael Sartainf7899542013-08-22 21:25:53 +00001899 // DT_JMPREL
1900 // If present, this entry's d_ptr member holds the address of relocation
1901 // entries associated solely with the procedure linkage table. Separating
1902 // these relocation entries lets the dynamic linker ignore them during
1903 // process initialization, if lazy binding is enabled. If this entry is
1904 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must
1905 // also be present.
Greg Clayton3046e662013-07-10 01:23:25 +00001906 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
1907 if (symbol)
Michael Sartaina7499c92013-07-01 19:45:50 +00001908 {
Michael Sartainf7899542013-08-22 21:25:53 +00001909 // Synthesize trampoline symbols to help navigate the PLT.
Greg Clayton3046e662013-07-10 01:23:25 +00001910 addr_t addr = symbol->d_ptr;
1911 Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get();
1912 if (reloc_section)
Michael Sartaina7499c92013-07-01 19:45:50 +00001913 {
Greg Clayton3046e662013-07-10 01:23:25 +00001914 user_id_t reloc_id = reloc_section->GetID();
1915 const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id);
1916 assert(reloc_header);
Michael Sartaina7499c92013-07-01 19:45:50 +00001917
Greg Clayton3046e662013-07-10 01:23:25 +00001918 ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id);
Michael Sartaina7499c92013-07-01 19:45:50 +00001919 }
1920 }
Michael Sartaina7499c92013-07-01 19:45:50 +00001921 }
Andrew MacPherson17220c12014-03-05 10:12:43 +00001922
1923 for (SectionHeaderCollIter I = m_section_headers.begin();
1924 I != m_section_headers.end(); ++I)
1925 {
1926 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL)
1927 {
1928 if (CalculateType() == eTypeObjectFile)
1929 {
1930 const char *section_name = I->section_name.AsCString("");
1931 if (strstr(section_name, ".rela.debug") ||
1932 strstr(section_name, ".rel.debug"))
1933 {
1934 const ELFSectionHeader &reloc_header = *I;
1935 user_id_t reloc_id = SectionIndex(I);
1936 RelocateDebugSections(&reloc_header, reloc_id);
1937 }
1938 }
1939 }
1940 }
Greg Clayton3046e662013-07-10 01:23:25 +00001941 return m_symtab_ap.get();
1942}
1943
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00001944Symbol *
1945ObjectFileELF::ResolveSymbolForAddress(const Address& so_addr, bool verify_unique)
1946{
1947 if (!m_symtab_ap.get())
1948 return nullptr; // GetSymtab() should be called first.
1949
1950 const SectionList *section_list = GetSectionList();
1951 if (!section_list)
1952 return nullptr;
1953
1954 if (DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo())
1955 {
1956 AddressRange range;
1957 if (eh_frame->GetAddressRange (so_addr, range))
1958 {
1959 const addr_t file_addr = range.GetBaseAddress().GetFileAddress();
1960 Symbol * symbol = verify_unique ? m_symtab_ap->FindSymbolContainingFileAddress(file_addr) : nullptr;
1961 if (symbol)
1962 return symbol;
1963
1964 // Note that a (stripped) symbol won't be found by GetSymtab()...
1965 lldb::SectionSP eh_sym_section_sp = section_list->FindSectionContainingFileAddress(file_addr);
1966 if (eh_sym_section_sp.get())
1967 {
1968 addr_t section_base = eh_sym_section_sp->GetFileAddress();
1969 addr_t offset = file_addr - section_base;
1970 uint64_t symbol_id = m_symtab_ap->GetNumSymbols();
1971
1972 Symbol eh_symbol(
1973 symbol_id, // Symbol table index.
1974 "???", // Symbol name.
1975 false, // Is the symbol name mangled?
1976 eSymbolTypeCode, // Type of this symbol.
1977 true, // Is this globally visible?
1978 false, // Is this symbol debug info?
1979 false, // Is this symbol a trampoline?
1980 true, // Is this symbol artificial?
1981 eh_sym_section_sp, // Section in which this symbol is defined or null.
1982 offset, // Offset in section or symbol value.
1983 range.GetByteSize(), // Size in bytes of this symbol.
1984 true, // Size is valid.
1985 0); // Symbol flags.
1986 if (symbol_id == m_symtab_ap->AddSymbol(eh_symbol))
1987 return m_symtab_ap->SymbolAtIndex(symbol_id);
1988 }
1989 }
1990 }
1991 return nullptr;
1992}
1993
1994
Greg Clayton3046e662013-07-10 01:23:25 +00001995bool
1996ObjectFileELF::IsStripped ()
1997{
1998 // TODO: determine this for ELF
1999 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002000}
2001
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002002//===----------------------------------------------------------------------===//
2003// Dump
2004//
2005// Dump the specifics of the runtime file container (such as any headers
2006// segments, sections, etc).
2007//----------------------------------------------------------------------
2008void
2009ObjectFileELF::Dump(Stream *s)
2010{
2011 DumpELFHeader(s, m_header);
2012 s->EOL();
2013 DumpELFProgramHeaders(s);
2014 s->EOL();
2015 DumpELFSectionHeaders(s);
2016 s->EOL();
2017 SectionList *section_list = GetSectionList();
2018 if (section_list)
Greg Clayton10177aa2010-12-08 05:08:21 +00002019 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002020 Symtab *symtab = GetSymtab();
2021 if (symtab)
Greg Claytone0d378b2011-03-24 21:19:54 +00002022 symtab->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002023 s->EOL();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002024 DumpDependentModules(s);
2025 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002026}
2027
2028//----------------------------------------------------------------------
2029// DumpELFHeader
2030//
2031// Dump the ELF header to the specified output stream
2032//----------------------------------------------------------------------
2033void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002034ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002035{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002036 s->PutCString("ELF Header\n");
2037 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
2038 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
2039 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
2040 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
2041 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
2042 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
2043 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002044
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002045 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
2046 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002047 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
2048 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
2049 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
2050
2051 s->Printf("e_type = 0x%4.4x ", header.e_type);
2052 DumpELFHeader_e_type(s, header.e_type);
2053 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
2054 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Daniel Malead01b2952012-11-29 21:49:15 +00002055 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
2056 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
2057 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002058 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
2059 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
2060 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
2061 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
2062 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
2063 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
2064 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
2065}
2066
2067//----------------------------------------------------------------------
2068// DumpELFHeader_e_type
2069//
2070// Dump an token value for the ELF header member e_type
2071//----------------------------------------------------------------------
2072void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002073ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002074{
2075 switch (e_type)
2076 {
2077 case ET_NONE: *s << "ET_NONE"; break;
2078 case ET_REL: *s << "ET_REL"; break;
2079 case ET_EXEC: *s << "ET_EXEC"; break;
2080 case ET_DYN: *s << "ET_DYN"; break;
2081 case ET_CORE: *s << "ET_CORE"; break;
2082 default:
2083 break;
2084 }
2085}
2086
2087//----------------------------------------------------------------------
2088// DumpELFHeader_e_ident_EI_DATA
2089//
2090// Dump an token value for the ELF header member e_ident[EI_DATA]
2091//----------------------------------------------------------------------
2092void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002093ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002094{
2095 switch (ei_data)
2096 {
2097 case ELFDATANONE: *s << "ELFDATANONE"; break;
2098 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
2099 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
2100 default:
2101 break;
2102 }
2103}
2104
2105
2106//----------------------------------------------------------------------
2107// DumpELFProgramHeader
2108//
2109// Dump a single ELF program header to the specified output stream
2110//----------------------------------------------------------------------
2111void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002112ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002113{
2114 DumpELFProgramHeader_p_type(s, ph.p_type);
Daniel Malead01b2952012-11-29 21:49:15 +00002115 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr);
2116 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 +00002117
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002118 DumpELFProgramHeader_p_flags(s, ph.p_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00002119 s->Printf(") %8.8" PRIx64, ph.p_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002120}
2121
2122//----------------------------------------------------------------------
2123// DumpELFProgramHeader_p_type
2124//
2125// Dump an token value for the ELF program header member p_type which
2126// describes the type of the program header
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002127// ----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002128void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002129ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002130{
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00002131 const int kStrWidth = 15;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002132 switch (p_type)
2133 {
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00002134 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
2135 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
2136 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
2137 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
2138 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
2139 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
2140 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
2141 CASE_AND_STREAM(s, PT_TLS , kStrWidth);
2142 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002143 default:
2144 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
2145 break;
2146 }
2147}
2148
2149
2150//----------------------------------------------------------------------
2151// DumpELFProgramHeader_p_flags
2152//
2153// Dump an token value for the ELF program header member p_flags
2154//----------------------------------------------------------------------
2155void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002156ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002157{
2158 *s << ((p_flags & PF_X) ? "PF_X" : " ")
2159 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
2160 << ((p_flags & PF_W) ? "PF_W" : " ")
2161 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
2162 << ((p_flags & PF_R) ? "PF_R" : " ");
2163}
2164
2165//----------------------------------------------------------------------
2166// DumpELFProgramHeaders
2167//
2168// Dump all of the ELF program header to the specified output stream
2169//----------------------------------------------------------------------
2170void
2171ObjectFileELF::DumpELFProgramHeaders(Stream *s)
2172{
2173 if (ParseProgramHeaders())
2174 {
2175 s->PutCString("Program Headers\n");
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00002176 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002177 "p_filesz p_memsz p_flags p_align\n");
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00002178 s->PutCString("==== --------------- -------- -------- -------- "
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002179 "-------- -------- ------------------------- --------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002180
2181 uint32_t idx = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002182 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
2183 I != m_program_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002184 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002185 s->Printf("[%2u] ", idx);
2186 ObjectFileELF::DumpELFProgramHeader(s, *I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002187 s->EOL();
2188 }
2189 }
2190}
2191
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002192//----------------------------------------------------------------------
2193// DumpELFSectionHeader
2194//
2195// Dump a single ELF section header to the specified output stream
2196//----------------------------------------------------------------------
2197void
Michael Sartaina7499c92013-07-01 19:45:50 +00002198ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002199{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002200 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002201 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Daniel Malead01b2952012-11-29 21:49:15 +00002202 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002203 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00002204 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 +00002205 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
Daniel Malead01b2952012-11-29 21:49:15 +00002206 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002207}
2208
2209//----------------------------------------------------------------------
2210// DumpELFSectionHeader_sh_type
2211//
2212// Dump an token value for the ELF section header member sh_type which
2213// describes the type of the section
2214//----------------------------------------------------------------------
2215void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002216ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002217{
2218 const int kStrWidth = 12;
2219 switch (sh_type)
2220 {
2221 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
2222 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
2223 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
2224 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
2225 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
2226 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
2227 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
2228 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
2229 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
2230 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
2231 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
2232 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
2233 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
2234 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
2235 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
2236 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
2237 default:
2238 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
2239 break;
2240 }
2241}
2242
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243//----------------------------------------------------------------------
2244// DumpELFSectionHeader_sh_flags
2245//
2246// Dump an token value for the ELF section header member sh_flags
2247//----------------------------------------------------------------------
2248void
Greg Claytonc7bece562013-01-25 18:06:21 +00002249ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002250{
2251 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
2252 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
2253 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
2254 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
2255 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
2256}
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002257
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002258//----------------------------------------------------------------------
2259// DumpELFSectionHeaders
2260//
2261// Dump all of the ELF section header to the specified output stream
2262//----------------------------------------------------------------------
2263void
2264ObjectFileELF::DumpELFSectionHeaders(Stream *s)
2265{
Michael Sartaina7499c92013-07-01 19:45:50 +00002266 if (!ParseSectionHeaders())
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002267 return;
2268
2269 s->PutCString("Section Headers\n");
2270 s->PutCString("IDX name type flags "
2271 "addr offset size link info addralgn "
2272 "entsize Name\n");
2273 s->PutCString("==== -------- ------------ -------------------------------- "
2274 "-------- -------- -------- -------- -------- -------- "
2275 "-------- ====================\n");
2276
2277 uint32_t idx = 0;
2278 for (SectionHeaderCollConstIter I = m_section_headers.begin();
2279 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002280 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002281 s->Printf("[%2u] ", idx);
2282 ObjectFileELF::DumpELFSectionHeader(s, *I);
Michael Sartaina7499c92013-07-01 19:45:50 +00002283 const char* section_name = I->section_name.AsCString("");
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002284 if (section_name)
2285 *s << ' ' << section_name << "\n";
2286 }
2287}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002288
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002289void
2290ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
2291{
2292 size_t num_modules = ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002293
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002294 if (num_modules > 0)
2295 {
2296 s->PutCString("Dependent Modules:\n");
2297 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002298 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002299 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
2300 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002301 }
2302 }
2303}
2304
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002305bool
Greg Clayton514487e2011-02-15 21:59:32 +00002306ObjectFileELF::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002307{
Stephen Wilson3f4200fd2011-02-24 19:16:15 +00002308 if (!ParseHeader())
2309 return false;
2310
Greg Claytone0d378b2011-03-24 21:19:54 +00002311 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
Greg Clayton64195a22011-02-23 00:35:02 +00002312 arch.GetTriple().setOSName (Host::GetOSString().GetCString());
2313 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
Stephen Wilsonf325ba92010-07-13 23:07:23 +00002314 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002315}
2316
Greg Clayton9e00b6a652011-07-09 00:41:34 +00002317ObjectFile::Type
2318ObjectFileELF::CalculateType()
2319{
2320 switch (m_header.e_type)
2321 {
2322 case llvm::ELF::ET_NONE:
2323 // 0 - No file type
2324 return eTypeUnknown;
2325
2326 case llvm::ELF::ET_REL:
2327 // 1 - Relocatable file
2328 return eTypeObjectFile;
2329
2330 case llvm::ELF::ET_EXEC:
2331 // 2 - Executable file
2332 return eTypeExecutable;
2333
2334 case llvm::ELF::ET_DYN:
2335 // 3 - Shared object file
2336 return eTypeSharedLibrary;
2337
2338 case ET_CORE:
2339 // 4 - Core file
2340 return eTypeCoreFile;
2341
2342 default:
2343 break;
2344 }
2345 return eTypeUnknown;
2346}
2347
2348ObjectFile::Strata
2349ObjectFileELF::CalculateStrata()
2350{
2351 switch (m_header.e_type)
2352 {
2353 case llvm::ELF::ET_NONE:
2354 // 0 - No file type
2355 return eStrataUnknown;
2356
2357 case llvm::ELF::ET_REL:
2358 // 1 - Relocatable file
2359 return eStrataUnknown;
2360
2361 case llvm::ELF::ET_EXEC:
2362 // 2 - Executable file
2363 // TODO: is there any way to detect that an executable is a kernel
2364 // related executable by inspecting the program headers, section
2365 // headers, symbols, or any other flag bits???
2366 return eStrataUser;
2367
2368 case llvm::ELF::ET_DYN:
2369 // 3 - Shared object file
2370 // TODO: is there any way to detect that an shared library is a kernel
2371 // related executable by inspecting the program headers, section
2372 // headers, symbols, or any other flag bits???
2373 return eStrataUnknown;
2374
2375 case ET_CORE:
2376 // 4 - Core file
2377 // TODO: is there any way to detect that an core file is a kernel
2378 // related executable by inspecting the program headers, section
2379 // headers, symbols, or any other flag bits???
2380 return eStrataUnknown;
2381
2382 default:
2383 break;
2384 }
2385 return eStrataUnknown;
2386}
2387