blob: 3e8af2f0c9e8c34b978a70a7f4a6c320c99f2752 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Symbol.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Symbol/Symbol.h"
11
12#include "lldb/Core/Module.h"
Greg Clayton9191db42013-10-21 18:40:51 +000013#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Section.h"
15#include "lldb/Core/Stream.h"
Greg Clayton1f746072012-08-29 21:13:06 +000016#include "lldb/Symbol/ObjectFile.h"
17#include "lldb/Symbol/Symtab.h"
Michael Sartainbf43d1a2013-07-03 16:35:41 +000018#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Target/Process.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000020#include "lldb/Target/Target.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000021#include "lldb/Symbol/SymbolVendor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23using namespace lldb;
24using namespace lldb_private;
25
26
27Symbol::Symbol() :
Stephen Wilson71c21d12011-04-11 19:41:40 +000028 SymbolContextScope (),
Greg Clayton81c22f62011-10-19 18:09:39 +000029 m_uid (UINT32_MAX),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030 m_type_data (0),
31 m_type_data_resolved (false),
32 m_is_synthetic (false),
33 m_is_debug (false),
34 m_is_external (false),
35 m_size_is_sibling (false),
36 m_size_is_synthesized (false),
Greg Clayton3046e662013-07-10 01:23:25 +000037 m_size_is_valid (false),
Greg Clayton3d51b9f2012-11-27 01:52:16 +000038 m_demangled_is_synthesized (false),
Pavel Labathc6ae7ea2015-03-04 10:25:22 +000039 m_contains_linker_annotations (false),
Greg Clayton01575f82011-11-22 21:20:33 +000040 m_type (eSymbolTypeInvalid),
Greg Claytond8c3d4b2013-06-19 21:50:28 +000041 m_mangled (),
42 m_addr_range (),
43 m_flags ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044{
45}
46
47Symbol::Symbol
48(
Greg Clayton81c22f62011-10-19 18:09:39 +000049 uint32_t symID,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050 const char *name,
51 bool name_is_mangled,
52 SymbolType type,
53 bool external,
54 bool is_debug,
55 bool is_trampoline,
56 bool is_artificial,
Greg Claytone72dfb32012-02-24 01:59:29 +000057 const lldb::SectionSP &section_sp,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058 addr_t offset,
Greg Claytonc7bece562013-01-25 18:06:21 +000059 addr_t size,
Greg Clayton9594f4c2013-04-13 23:17:23 +000060 bool size_is_valid,
Pavel Labathc6ae7ea2015-03-04 10:25:22 +000061 bool contains_linker_annotations,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 uint32_t flags
63) :
Stephen Wilson71c21d12011-04-11 19:41:40 +000064 SymbolContextScope (),
Greg Clayton81c22f62011-10-19 18:09:39 +000065 m_uid (symID),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066 m_type_data (0),
67 m_type_data_resolved (false),
68 m_is_synthetic (is_artificial),
69 m_is_debug (is_debug),
70 m_is_external (external),
71 m_size_is_sibling (false),
72 m_size_is_synthesized (false),
Greg Clayton3046e662013-07-10 01:23:25 +000073 m_size_is_valid (size_is_valid || size > 0),
Greg Clayton3d51b9f2012-11-27 01:52:16 +000074 m_demangled_is_synthesized (false),
Pavel Labathc6ae7ea2015-03-04 10:25:22 +000075 m_contains_linker_annotations (contains_linker_annotations),
Greg Clayton01575f82011-11-22 21:20:33 +000076 m_type (type),
Greg Claytond8c3d4b2013-06-19 21:50:28 +000077 m_mangled (ConstString(name), name_is_mangled),
78 m_addr_range (section_sp, offset, size),
79 m_flags (flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080{
81}
82
83Symbol::Symbol
84(
Greg Clayton81c22f62011-10-19 18:09:39 +000085 uint32_t symID,
Pavel Labathc6ae7ea2015-03-04 10:25:22 +000086 const Mangled &mangled,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 SymbolType type,
88 bool external,
89 bool is_debug,
90 bool is_trampoline,
91 bool is_artificial,
92 const AddressRange &range,
Greg Clayton9594f4c2013-04-13 23:17:23 +000093 bool size_is_valid,
Pavel Labathc6ae7ea2015-03-04 10:25:22 +000094 bool contains_linker_annotations,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095 uint32_t flags
96) :
Stephen Wilson71c21d12011-04-11 19:41:40 +000097 SymbolContextScope (),
Greg Clayton81c22f62011-10-19 18:09:39 +000098 m_uid (symID),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099 m_type_data (0),
100 m_type_data_resolved (false),
101 m_is_synthetic (is_artificial),
102 m_is_debug (is_debug),
103 m_is_external (external),
104 m_size_is_sibling (false),
105 m_size_is_synthesized (false),
Greg Clayton3046e662013-07-10 01:23:25 +0000106 m_size_is_valid (size_is_valid || range.GetByteSize() > 0),
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000107 m_demangled_is_synthesized (false),
Pavel Labathc6ae7ea2015-03-04 10:25:22 +0000108 m_contains_linker_annotations (contains_linker_annotations),
Greg Clayton01575f82011-11-22 21:20:33 +0000109 m_type (type),
Pavel Labathc6ae7ea2015-03-04 10:25:22 +0000110 m_mangled (mangled),
Greg Claytond8c3d4b2013-06-19 21:50:28 +0000111 m_addr_range (range),
112 m_flags (flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113{
114}
115
116Symbol::Symbol(const Symbol& rhs):
Stephen Wilson71c21d12011-04-11 19:41:40 +0000117 SymbolContextScope (rhs),
Greg Clayton81c22f62011-10-19 18:09:39 +0000118 m_uid (rhs.m_uid),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119 m_type_data (rhs.m_type_data),
120 m_type_data_resolved (rhs.m_type_data_resolved),
121 m_is_synthetic (rhs.m_is_synthetic),
122 m_is_debug (rhs.m_is_debug),
123 m_is_external (rhs.m_is_external),
124 m_size_is_sibling (rhs.m_size_is_sibling),
125 m_size_is_synthesized (false),
Greg Clayton3046e662013-07-10 01:23:25 +0000126 m_size_is_valid (rhs.m_size_is_valid),
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000127 m_demangled_is_synthesized (rhs.m_demangled_is_synthesized),
Pavel Labathc6ae7ea2015-03-04 10:25:22 +0000128 m_contains_linker_annotations (rhs.m_contains_linker_annotations),
Greg Clayton01575f82011-11-22 21:20:33 +0000129 m_type (rhs.m_type),
Greg Claytond8c3d4b2013-06-19 21:50:28 +0000130 m_mangled (rhs.m_mangled),
131 m_addr_range (rhs.m_addr_range),
132 m_flags (rhs.m_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133{
134}
135
136const Symbol&
137Symbol::operator= (const Symbol& rhs)
138{
139 if (this != &rhs)
140 {
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000141 SymbolContextScope::operator= (rhs);
Greg Clayton81c22f62011-10-19 18:09:39 +0000142 m_uid = rhs.m_uid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 m_type_data = rhs.m_type_data;
144 m_type_data_resolved = rhs.m_type_data_resolved;
145 m_is_synthetic = rhs.m_is_synthetic;
146 m_is_debug = rhs.m_is_debug;
147 m_is_external = rhs.m_is_external;
148 m_size_is_sibling = rhs.m_size_is_sibling;
149 m_size_is_synthesized = rhs.m_size_is_sibling;
Greg Clayton3046e662013-07-10 01:23:25 +0000150 m_size_is_valid = rhs.m_size_is_valid;
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000151 m_demangled_is_synthesized = rhs.m_demangled_is_synthesized;
Pavel Labathc6ae7ea2015-03-04 10:25:22 +0000152 m_contains_linker_annotations = rhs.m_contains_linker_annotations;
Greg Clayton01575f82011-11-22 21:20:33 +0000153 m_type = rhs.m_type;
Greg Claytond8c3d4b2013-06-19 21:50:28 +0000154 m_mangled = rhs.m_mangled;
Greg Clayton01575f82011-11-22 21:20:33 +0000155 m_addr_range = rhs.m_addr_range;
Greg Claytond8c3d4b2013-06-19 21:50:28 +0000156 m_flags = rhs.m_flags;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 }
158 return *this;
159}
160
Greg Clayton81c22f62011-10-19 18:09:39 +0000161void
162Symbol::Clear()
163{
164 m_uid = UINT32_MAX;
165 m_mangled.Clear();
Greg Clayton81c22f62011-10-19 18:09:39 +0000166 m_type_data = 0;
167 m_type_data_resolved = false;
168 m_is_synthetic = false;
169 m_is_debug = false;
170 m_is_external = false;
171 m_size_is_sibling = false;
172 m_size_is_synthesized = false;
Greg Clayton3046e662013-07-10 01:23:25 +0000173 m_size_is_valid = false;
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000174 m_demangled_is_synthesized = false;
Pavel Labathc6ae7ea2015-03-04 10:25:22 +0000175 m_contains_linker_annotations = false;
Greg Clayton01575f82011-11-22 21:20:33 +0000176 m_type = eSymbolTypeInvalid;
Greg Clayton81c22f62011-10-19 18:09:39 +0000177 m_flags = 0;
Greg Clayton01575f82011-11-22 21:20:33 +0000178 m_addr_range.Clear();
Greg Clayton81c22f62011-10-19 18:09:39 +0000179}
180
Greg Claytone7612132012-03-07 21:03:09 +0000181bool
182Symbol::ValueIsAddress() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183{
Ed Masted4612ad2014-04-20 13:17:36 +0000184 return m_addr_range.GetBaseAddress().GetSection().get() != nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185}
186
Greg Clayton9191db42013-10-21 18:40:51 +0000187ConstString
Enrico Granatac1f705c2015-07-06 18:28:46 +0000188Symbol::GetDisplayName () const
189{
190 if (!m_mangled)
191 return ConstString();
192 return m_mangled.GetDisplayDemangledName();
193}
194
195ConstString
Greg Clayton9191db42013-10-21 18:40:51 +0000196Symbol::GetReExportedSymbolName() const
197{
198 if (m_type == eSymbolTypeReExported)
199 {
200 // For eSymbolTypeReExported, the "const char *" from a ConstString
201 // is used as the offset in the address range base address. We can
202 // then make this back into a string that is the re-exported name.
203 intptr_t str_ptr = m_addr_range.GetBaseAddress().GetOffset();
204 if (str_ptr != 0)
205 return ConstString((const char *)str_ptr);
206 else
207 return GetName();
208 }
209 return ConstString();
210}
211
212FileSpec
213Symbol::GetReExportedSymbolSharedLibrary() const
214{
215 if (m_type == eSymbolTypeReExported)
216 {
217 // For eSymbolTypeReExported, the "const char *" from a ConstString
218 // is used as the offset in the address range base address. We can
219 // then make this back into a string that is the re-exported name.
220 intptr_t str_ptr = m_addr_range.GetByteSize();
221 if (str_ptr != 0)
222 return FileSpec((const char *)str_ptr, false);
223 }
224 return FileSpec();
225}
226
Greg Clayton60038be2015-02-14 00:51:13 +0000227void
Greg Clayton9191db42013-10-21 18:40:51 +0000228Symbol::SetReExportedSymbolName(const ConstString &name)
229{
Greg Clayton60038be2015-02-14 00:51:13 +0000230 SetType (eSymbolTypeReExported);
231 // For eSymbolTypeReExported, the "const char *" from a ConstString
232 // is used as the offset in the address range base address.
233 m_addr_range.GetBaseAddress().SetOffset((uintptr_t)name.GetCString());
Greg Clayton9191db42013-10-21 18:40:51 +0000234}
235
236bool
237Symbol::SetReExportedSymbolSharedLibrary(const FileSpec &fspec)
238{
239 if (m_type == eSymbolTypeReExported)
240 {
241 // For eSymbolTypeReExported, the "const char *" from a ConstString
242 // is used as the offset in the address range base address.
Greg Clayton60038be2015-02-14 00:51:13 +0000243 m_addr_range.SetByteSize((uintptr_t)ConstString(fspec.GetPath().c_str()).GetCString());
Greg Clayton9191db42013-10-21 18:40:51 +0000244 return true;
245 }
246 return false;
247
248}
249
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250uint32_t
251Symbol::GetSiblingIndex() const
252{
Greg Claytonbc63aac2015-02-25 22:41:34 +0000253 return m_size_is_sibling ? m_addr_range.GetByteSize() : UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254}
255
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256bool
257Symbol::IsTrampoline () const
258{
259 return m_type == eSymbolTypeTrampoline;
260}
261
Matt Kopec00049b82013-02-27 20:13:38 +0000262bool
263Symbol::IsIndirect () const
264{
265 return m_type == eSymbolTypeResolver;
266}
267
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268void
Greg Claytonf5e56de2010-09-14 23:36:40 +0000269Symbol::GetDescription (Stream *s, lldb::DescriptionLevel level, Target *target) const
Greg Clayton0c5cd902010-06-28 21:30:43 +0000270{
Greg Claytonc4a8a762012-05-15 18:43:44 +0000271 s->Printf("id = {0x%8.8x}", m_uid);
Greg Claytond9dc52d2011-09-24 05:04:40 +0000272
Greg Claytone72dfb32012-02-24 01:59:29 +0000273 if (m_addr_range.GetBaseAddress().GetSection())
Greg Clayton0c5cd902010-06-28 21:30:43 +0000274 {
Greg Clayton8f89a7b2012-03-07 23:30:39 +0000275 if (ValueIsAddress())
Greg Clayton0c5cd902010-06-28 21:30:43 +0000276 {
Greg Clayton8f89a7b2012-03-07 23:30:39 +0000277 const lldb::addr_t byte_size = GetByteSize();
278 if (byte_size > 0)
Greg Claytonc9800662010-09-10 01:30:46 +0000279 {
280 s->PutCString (", range = ");
Greg Claytonf5e56de2010-09-14 23:36:40 +0000281 m_addr_range.Dump(s, target, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
Greg Claytonc9800662010-09-10 01:30:46 +0000282 }
283 else
284 {
285 s->PutCString (", address = ");
Greg Claytonf5e56de2010-09-14 23:36:40 +0000286 m_addr_range.GetBaseAddress().Dump(s, target, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
Greg Claytonc9800662010-09-10 01:30:46 +0000287 }
Greg Clayton0c5cd902010-06-28 21:30:43 +0000288 }
289 else
Daniel Malead01b2952012-11-29 21:49:15 +0000290 s->Printf (", value = 0x%16.16" PRIx64, m_addr_range.GetBaseAddress().GetOffset());
Greg Claytond9dc52d2011-09-24 05:04:40 +0000291 }
292 else
293 {
294 if (m_size_is_sibling)
Daniel Malead01b2952012-11-29 21:49:15 +0000295 s->Printf (", sibling = %5" PRIu64, m_addr_range.GetBaseAddress().GetOffset());
Greg Claytond9dc52d2011-09-24 05:04:40 +0000296 else
Daniel Malead01b2952012-11-29 21:49:15 +0000297 s->Printf (", value = 0x%16.16" PRIx64, m_addr_range.GetBaseAddress().GetOffset());
Greg Clayton0c5cd902010-06-28 21:30:43 +0000298 }
Greg Clayton6b2bd932012-02-01 08:09:32 +0000299 if (m_mangled.GetDemangledName())
300 s->Printf(", name=\"%s\"", m_mangled.GetDemangledName().AsCString());
301 if (m_mangled.GetMangledName())
302 s->Printf(", mangled=\"%s\"", m_mangled.GetMangledName().AsCString());
303
Greg Clayton0c5cd902010-06-28 21:30:43 +0000304}
305
306void
Greg Claytonf5e56de2010-09-14 23:36:40 +0000307Symbol::Dump(Stream *s, Target *target, uint32_t index) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308{
Greg Clayton2d9ce192015-02-25 17:22:05 +0000309 s->Printf("[%5u] %6u %c%c%c %-15s ",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310 index,
311 GetID(),
312 m_is_debug ? 'D' : ' ',
313 m_is_synthetic ? 'S' : ' ',
314 m_is_external ? 'X' : ' ',
315 GetTypeAsString());
316
Greg Claytone7612132012-03-07 21:03:09 +0000317 // Make sure the size of the symbol is up to date before dumping
318 GetByteSize();
319
320 if (ValueIsAddress())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 {
Ed Masted4612ad2014-04-20 13:17:36 +0000322 if (!m_addr_range.GetBaseAddress().Dump(s, nullptr, Address::DumpStyleFileAddress))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 s->Printf("%*s", 18, "");
324
325 s->PutChar(' ');
326
Greg Claytonf5e56de2010-09-14 23:36:40 +0000327 if (!m_addr_range.GetBaseAddress().Dump(s, target, Address::DumpStyleLoadAddress))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 s->Printf("%*s", 18, "");
329
330 const char *format = m_size_is_sibling ?
331 " Sibling -> [%5llu] 0x%8.8x %s\n":
Daniel Malead01b2952012-11-29 21:49:15 +0000332 " 0x%16.16" PRIx64 " 0x%8.8x %s\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 s->Printf( format,
Greg Clayton8f89a7b2012-03-07 23:30:39 +0000334 GetByteSize(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 m_flags,
336 m_mangled.GetName().AsCString(""));
337 }
Greg Clayton9191db42013-10-21 18:40:51 +0000338 else if (m_type == eSymbolTypeReExported)
339 {
340 s->Printf (" 0x%8.8x %s",
341 m_flags,
342 m_mangled.GetName().AsCString(""));
343
344 ConstString reexport_name = GetReExportedSymbolName();
345 intptr_t shlib = m_addr_range.GetByteSize();
346 if (shlib)
347 s->Printf(" -> %s`%s\n", (const char *)shlib, reexport_name.GetCString());
348 else
349 s->Printf(" -> %s\n", reexport_name.GetCString());
350 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351 else
352 {
353 const char *format = m_size_is_sibling ?
Daniel Malead01b2952012-11-29 21:49:15 +0000354 "0x%16.16" PRIx64 " Sibling -> [%5llu] 0x%8.8x %s\n":
355 "0x%16.16" PRIx64 " 0x%16.16" PRIx64 " 0x%8.8x %s\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356 s->Printf( format,
357 m_addr_range.GetBaseAddress().GetOffset(),
Greg Clayton8f89a7b2012-03-07 23:30:39 +0000358 GetByteSize(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 m_flags,
360 m_mangled.GetName().AsCString(""));
361 }
362}
363
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364uint32_t
365Symbol::GetPrologueByteSize ()
366{
Matt Kopec00049b82013-02-27 20:13:38 +0000367 if (m_type == eSymbolTypeCode || m_type == eSymbolTypeResolver)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368 {
369 if (!m_type_data_resolved)
370 {
371 m_type_data_resolved = true;
Michael Sartainbf43d1a2013-07-03 16:35:41 +0000372
373 const Address &base_address = m_addr_range.GetBaseAddress();
374 Function *function = base_address.CalculateSymbolContextFunction();
375 if (function)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 {
Michael Sartainbf43d1a2013-07-03 16:35:41 +0000377 // Functions have line entries which can also potentially have end of prologue information.
378 // So if this symbol points to a function, use the prologue information from there.
379 m_type_data = function->GetPrologueByteSize();
380 }
381 else
382 {
383 ModuleSP module_sp (base_address.GetModule());
384 SymbolContext sc;
385 if (module_sp)
Michael Sartaina7499c92013-07-01 19:45:50 +0000386 {
Michael Sartainbf43d1a2013-07-03 16:35:41 +0000387 uint32_t resolved_flags = module_sp->ResolveSymbolContextForAddress (base_address,
388 eSymbolContextLineEntry,
389 sc);
390 if (resolved_flags & eSymbolContextLineEntry)
Michael Sartaina7499c92013-07-01 19:45:50 +0000391 {
Michael Sartainbf43d1a2013-07-03 16:35:41 +0000392 // Default to the end of the first line entry.
393 m_type_data = sc.line_entry.range.GetByteSize();
Michael Sartaina7499c92013-07-01 19:45:50 +0000394
Michael Sartainbf43d1a2013-07-03 16:35:41 +0000395 // Set address for next line.
396 Address addr (base_address);
397 addr.Slide (m_type_data);
398
399 // Check the first few instructions and look for one that has a line number that is
400 // different than the first entry. This is also done in Function::GetPrologueByteSize().
401 uint16_t total_offset = m_type_data;
402 for (int idx = 0; idx < 6; ++idx)
Michael Sartaina7499c92013-07-01 19:45:50 +0000403 {
Michael Sartainbf43d1a2013-07-03 16:35:41 +0000404 SymbolContext sc_temp;
405 resolved_flags = module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextLineEntry, sc_temp);
406 // Make sure we got line number information...
407 if (!(resolved_flags & eSymbolContextLineEntry))
408 break;
409
410 // If this line number is different than our first one, use it and we're done.
411 if (sc_temp.line_entry.line != sc.line_entry.line)
412 {
413 m_type_data = total_offset;
414 break;
415 }
416
417 // Slide addr up to the next line address.
418 addr.Slide (sc_temp.line_entry.range.GetByteSize());
419 total_offset += sc_temp.line_entry.range.GetByteSize();
420 // If we've gone too far, bail out.
421 if (total_offset >= m_addr_range.GetByteSize())
422 break;
Michael Sartaina7499c92013-07-01 19:45:50 +0000423 }
424
Michael Sartainbf43d1a2013-07-03 16:35:41 +0000425 // Sanity check - this may be a function in the middle of code that has debug information, but
426 // not for this symbol. So the line entries surrounding us won't lie inside our function.
427 // In that case, the line entry will be bigger than we are, so we do that quick check and
428 // if that is true, we just return 0.
429 if (m_type_data >= m_addr_range.GetByteSize())
430 m_type_data = 0;
Michael Sartaina7499c92013-07-01 19:45:50 +0000431 }
Michael Sartainbf43d1a2013-07-03 16:35:41 +0000432 else
433 {
434 // TODO: expose something in Process to figure out the
435 // size of a function prologue.
Michael Sartaina7499c92013-07-01 19:45:50 +0000436 m_type_data = 0;
Michael Sartainbf43d1a2013-07-03 16:35:41 +0000437 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000438 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439 }
440 }
441 return m_type_data;
442 }
443 return 0;
444}
445
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446bool
447Symbol::Compare(const ConstString& name, SymbolType type) const
448{
Jim Inghamc30ee562011-10-29 00:54:12 +0000449 if (type == eSymbolTypeAny || m_type == type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 return m_mangled.GetMangledName() == name || m_mangled.GetDemangledName() == name;
451 return false;
452}
453
454#define ENUM_TO_CSTRING(x) case eSymbolType##x: return #x;
455
456const char *
457Symbol::GetTypeAsString() const
458{
459 switch (m_type)
460 {
461 ENUM_TO_CSTRING(Invalid);
462 ENUM_TO_CSTRING(Absolute);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 ENUM_TO_CSTRING(Code);
Greg Clayton9191db42013-10-21 18:40:51 +0000464 ENUM_TO_CSTRING(Resolver);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 ENUM_TO_CSTRING(Data);
466 ENUM_TO_CSTRING(Trampoline);
467 ENUM_TO_CSTRING(Runtime);
468 ENUM_TO_CSTRING(Exception);
469 ENUM_TO_CSTRING(SourceFile);
470 ENUM_TO_CSTRING(HeaderFile);
471 ENUM_TO_CSTRING(ObjectFile);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 ENUM_TO_CSTRING(CommonBlock);
473 ENUM_TO_CSTRING(Block);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474 ENUM_TO_CSTRING(Local);
475 ENUM_TO_CSTRING(Param);
476 ENUM_TO_CSTRING(Variable);
477 ENUM_TO_CSTRING(VariableType);
478 ENUM_TO_CSTRING(LineEntry);
479 ENUM_TO_CSTRING(LineHeader);
480 ENUM_TO_CSTRING(ScopeBegin);
481 ENUM_TO_CSTRING(ScopeEnd);
482 ENUM_TO_CSTRING(Additional);
483 ENUM_TO_CSTRING(Compiler);
484 ENUM_TO_CSTRING(Instrumentation);
485 ENUM_TO_CSTRING(Undefined);
Greg Clayton456809c2011-12-03 02:30:59 +0000486 ENUM_TO_CSTRING(ObjCClass);
487 ENUM_TO_CSTRING(ObjCMetaClass);
488 ENUM_TO_CSTRING(ObjCIVar);
Greg Clayton9191db42013-10-21 18:40:51 +0000489 ENUM_TO_CSTRING(ReExported);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 default:
491 break;
492 }
493 return "<unknown SymbolType>";
494}
495
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000496void
497Symbol::CalculateSymbolContext (SymbolContext *sc)
498{
499 // Symbols can reconstruct the symbol and the module in the symbol context
500 sc->symbol = this;
Greg Claytone7612132012-03-07 21:03:09 +0000501 if (ValueIsAddress())
Greg Clayton358cf1e2015-06-25 21:46:34 +0000502 sc->module_sp = GetAddressRef().GetModule();
Greg Claytone1cd1be2012-01-29 20:56:30 +0000503 else
504 sc->module_sp.reset();
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000505}
506
Greg Claytone72dfb32012-02-24 01:59:29 +0000507ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000508Symbol::CalculateSymbolContextModule ()
509{
Greg Claytone7612132012-03-07 21:03:09 +0000510 if (ValueIsAddress())
Greg Clayton358cf1e2015-06-25 21:46:34 +0000511 return GetAddressRef().GetModule();
Greg Claytone72dfb32012-02-24 01:59:29 +0000512 return ModuleSP();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000513}
514
515Symbol *
516Symbol::CalculateSymbolContextSymbol ()
517{
518 return this;
519}
520
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000521void
522Symbol::DumpSymbolContext (Stream *s)
523{
524 bool dumped_module = false;
Greg Claytone7612132012-03-07 21:03:09 +0000525 if (ValueIsAddress())
526 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000527 ModuleSP module_sp (GetAddressRef().GetModule());
Greg Claytone72dfb32012-02-24 01:59:29 +0000528 if (module_sp)
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000529 {
530 dumped_module = true;
Greg Claytone72dfb32012-02-24 01:59:29 +0000531 module_sp->DumpSymbolContext(s);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000532 }
533 }
534 if (dumped_module)
535 s->PutCString(", ");
536
537 s->Printf("Symbol{0x%8.8x}", GetID());
538}
539
Greg Claytonda171f12012-03-02 03:01:16 +0000540lldb::addr_t
541Symbol::GetByteSize () const
542{
Greg Clayton3046e662013-07-10 01:23:25 +0000543 return m_addr_range.GetByteSize();
Greg Claytonda171f12012-03-02 03:01:16 +0000544}
545
Jim Inghamfbe0b9a2014-05-21 03:58:03 +0000546
547Symbol *
548Symbol::ResolveReExportedSymbolInModuleSpec (Target &target,
549 ConstString &reexport_name,
550 ModuleSpec &module_spec,
Sean Callanan25ea6a12014-05-23 02:30:48 +0000551 ModuleList &seen_modules) const
Jim Inghamfbe0b9a2014-05-21 03:58:03 +0000552{
553 ModuleSP module_sp;
554 if (module_spec.GetFileSpec())
555 {
556 // Try searching for the module file spec first using the full path
557 module_sp = target.GetImages().FindFirstModule(module_spec);
558 if (!module_sp)
559 {
560 // Next try and find the module by basename in case environment
561 // variables or other runtime trickery causes shared libraries
562 // to be loaded from alternate paths
563 module_spec.GetFileSpec().GetDirectory().Clear();
564 module_sp = target.GetImages().FindFirstModule(module_spec);
565 }
566 }
567
568 if (module_sp)
569 {
570 // There should not be cycles in the reexport list, but we don't want to crash if there are so make sure
571 // we haven't seen this before:
572 if (!seen_modules.AppendIfNeeded(module_sp))
573 return nullptr;
574
575 lldb_private::SymbolContextList sc_list;
576 module_sp->FindSymbolsWithNameAndType(reexport_name, eSymbolTypeAny, sc_list);
577 const size_t num_scs = sc_list.GetSize();
578 if (num_scs > 0)
579 {
580 for (size_t i=0; i<num_scs; ++i)
581 {
582 lldb_private::SymbolContext sc;
583 if (sc_list.GetContextAtIndex(i, sc))
584 {
585 if (sc.symbol->IsExternal())
586 return sc.symbol;
587 }
588 }
589 }
590 // If we didn't find the symbol in this module, it may be because this module re-exports some
591 // whole other library. We have to search those as well:
592 seen_modules.Append(module_sp);
593
594 FileSpecList reexported_libraries = module_sp->GetObjectFile()->GetReExportedLibraries();
595 size_t num_reexported_libraries = reexported_libraries.GetSize();
596 for (size_t idx = 0; idx < num_reexported_libraries; idx++)
597 {
598 ModuleSpec reexported_module_spec;
599 reexported_module_spec.GetFileSpec() = reexported_libraries.GetFileSpecAtIndex(idx);
600 Symbol *result_symbol = ResolveReExportedSymbolInModuleSpec(target,
601 reexport_name,
602 reexported_module_spec,
603 seen_modules);
604 if (result_symbol)
605 return result_symbol;
606 }
607 }
608 return nullptr;
609}
610
Greg Clayton9191db42013-10-21 18:40:51 +0000611Symbol *
Sean Callanan25ea6a12014-05-23 02:30:48 +0000612Symbol::ResolveReExportedSymbol (Target &target) const
Greg Clayton9191db42013-10-21 18:40:51 +0000613{
614 ConstString reexport_name (GetReExportedSymbolName());
615 if (reexport_name)
616 {
617 ModuleSpec module_spec;
Jim Inghamfbe0b9a2014-05-21 03:58:03 +0000618 ModuleList seen_modules;
Greg Clayton9191db42013-10-21 18:40:51 +0000619 module_spec.GetFileSpec() = GetReExportedSymbolSharedLibrary();
620 if (module_spec.GetFileSpec())
621 {
Jim Inghamfbe0b9a2014-05-21 03:58:03 +0000622 return ResolveReExportedSymbolInModuleSpec(target, reexport_name, module_spec, seen_modules);
Greg Clayton9191db42013-10-21 18:40:51 +0000623 }
624 }
Ed Masted4612ad2014-04-20 13:17:36 +0000625 return nullptr;
Greg Clayton9191db42013-10-21 18:40:51 +0000626}
Greg Clayton44d93782014-01-27 23:43:24 +0000627
Sean Callanan25ea6a12014-05-23 02:30:48 +0000628lldb::addr_t
Greg Clayton358cf1e2015-06-25 21:46:34 +0000629Symbol::GetFileAddress () const
630{
631 if (ValueIsAddress())
632 return GetAddressRef().GetFileAddress();
633 else
634 return LLDB_INVALID_ADDRESS;
635}
636
637lldb::addr_t
638Symbol::GetLoadAddress (Target *target) const
639{
640 if (ValueIsAddress())
641 return GetAddressRef().GetLoadAddress(target);
642 else
643 return LLDB_INVALID_ADDRESS;
644}
645
646
647lldb::addr_t
Sean Callanan25ea6a12014-05-23 02:30:48 +0000648Symbol::ResolveCallableAddress(Target &target) const
649{
650 if (GetType() == lldb::eSymbolTypeUndefined)
651 return LLDB_INVALID_ADDRESS;
652
653 Address func_so_addr;
654
Jason Molenda9aae9fc2014-10-15 03:05:38 +0000655 bool is_indirect = IsIndirect();
Sean Callanan25ea6a12014-05-23 02:30:48 +0000656 if (GetType() == eSymbolTypeReExported)
657 {
658 Symbol *reexported_symbol = ResolveReExportedSymbol(target);
659 if (reexported_symbol)
660 {
661 func_so_addr = reexported_symbol->GetAddress();
662 is_indirect = reexported_symbol->IsIndirect();
663 }
664 }
665 else
666 {
667 func_so_addr = GetAddress();
668 is_indirect = IsIndirect();
669 }
670
671 if (func_so_addr.IsValid())
672 {
673 if (!target.GetProcessSP() && is_indirect)
674 {
675 // can't resolve indirect symbols without calling a function...
676 return LLDB_INVALID_ADDRESS;
677 }
678
679 lldb::addr_t load_addr = func_so_addr.GetCallableLoadAddress (&target, is_indirect);
680
681 if (load_addr != LLDB_INVALID_ADDRESS)
682 {
683 return load_addr;
684 }
685 }
686
687 return LLDB_INVALID_ADDRESS;
688}
689
Greg Clayton44d93782014-01-27 23:43:24 +0000690
691lldb::DisassemblerSP
692Symbol::GetInstructions (const ExecutionContext &exe_ctx,
693 const char *flavor,
694 bool prefer_file_cache)
695{
696 ModuleSP module_sp (m_addr_range.GetBaseAddress().GetModule());
697 if (module_sp)
698 {
699 const bool prefer_file_cache = false;
700 return Disassembler::DisassembleRange (module_sp->GetArchitecture(),
Ed Masted4612ad2014-04-20 13:17:36 +0000701 nullptr,
Greg Clayton44d93782014-01-27 23:43:24 +0000702 flavor,
703 exe_ctx,
704 m_addr_range,
705 prefer_file_cache);
706 }
707 return lldb::DisassemblerSP();
708}
709
710bool
711Symbol::GetDisassembly (const ExecutionContext &exe_ctx,
712 const char *flavor,
713 bool prefer_file_cache,
714 Stream &strm)
715{
716 lldb::DisassemblerSP disassembler_sp = GetInstructions (exe_ctx, flavor, prefer_file_cache);
717 if (disassembler_sp)
718 {
719 const bool show_address = true;
720 const bool show_bytes = false;
721 disassembler_sp->GetInstructionList().Dump (&strm, show_address, show_bytes, &exe_ctx);
722 return true;
723 }
724 return false;
725}