1 //===-- Symbol.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/Symbol.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleSpec.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Symbol/Function.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Symbol/SymbolVendor.h"
18 #include "lldb/Symbol/Symtab.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/Stream.h"
24 using namespace lldb_private;
27 : SymbolContextScope(), m_uid(UINT32_MAX), m_type_data(0),
28 m_type_data_resolved(false), m_is_synthetic(false), m_is_debug(false),
29 m_is_external(false), m_size_is_sibling(false),
30 m_size_is_synthesized(false), m_size_is_valid(false),
31 m_demangled_is_synthesized(false), m_contains_linker_annotations(false),
32 m_type(eSymbolTypeInvalid), m_mangled(), m_addr_range(), m_flags() {}
34 Symbol::Symbol(uint32_t symID, const char *name, bool name_is_mangled,
35 SymbolType type, bool external, bool is_debug,
36 bool is_trampoline, bool is_artificial,
37 const lldb::SectionSP §ion_sp, addr_t offset, addr_t size,
38 bool size_is_valid, bool contains_linker_annotations,
40 : SymbolContextScope(), m_uid(symID), m_type_data(0),
41 m_type_data_resolved(false), m_is_synthetic(is_artificial),
42 m_is_debug(is_debug), m_is_external(external), m_size_is_sibling(false),
43 m_size_is_synthesized(false), m_size_is_valid(size_is_valid || size > 0),
44 m_demangled_is_synthesized(false),
45 m_contains_linker_annotations(contains_linker_annotations), m_type(type),
46 m_mangled(ConstString(name), name_is_mangled),
47 m_addr_range(section_sp, offset, size), m_flags(flags) {}
49 Symbol::Symbol(uint32_t symID, const Mangled &mangled, SymbolType type,
50 bool external, bool is_debug, bool is_trampoline,
51 bool is_artificial, const AddressRange &range,
52 bool size_is_valid, bool contains_linker_annotations,
54 : SymbolContextScope(), m_uid(symID), m_type_data(0),
55 m_type_data_resolved(false), m_is_synthetic(is_artificial),
56 m_is_debug(is_debug), m_is_external(external), m_size_is_sibling(false),
57 m_size_is_synthesized(false),
58 m_size_is_valid(size_is_valid || range.GetByteSize() > 0),
59 m_demangled_is_synthesized(false),
60 m_contains_linker_annotations(contains_linker_annotations), m_type(type),
61 m_mangled(mangled), m_addr_range(range), m_flags(flags) {}
63 Symbol::Symbol(const Symbol &rhs)
64 : SymbolContextScope(rhs), m_uid(rhs.m_uid), m_type_data(rhs.m_type_data),
65 m_type_data_resolved(rhs.m_type_data_resolved),
66 m_is_synthetic(rhs.m_is_synthetic), m_is_debug(rhs.m_is_debug),
67 m_is_external(rhs.m_is_external),
68 m_size_is_sibling(rhs.m_size_is_sibling), m_size_is_synthesized(false),
69 m_size_is_valid(rhs.m_size_is_valid),
70 m_demangled_is_synthesized(rhs.m_demangled_is_synthesized),
71 m_contains_linker_annotations(rhs.m_contains_linker_annotations),
72 m_type(rhs.m_type), m_mangled(rhs.m_mangled),
73 m_addr_range(rhs.m_addr_range), m_flags(rhs.m_flags) {}
75 const Symbol &Symbol::operator=(const Symbol &rhs) {
77 SymbolContextScope::operator=(rhs);
79 m_type_data = rhs.m_type_data;
80 m_type_data_resolved = rhs.m_type_data_resolved;
81 m_is_synthetic = rhs.m_is_synthetic;
82 m_is_debug = rhs.m_is_debug;
83 m_is_external = rhs.m_is_external;
84 m_size_is_sibling = rhs.m_size_is_sibling;
85 m_size_is_synthesized = rhs.m_size_is_sibling;
86 m_size_is_valid = rhs.m_size_is_valid;
87 m_demangled_is_synthesized = rhs.m_demangled_is_synthesized;
88 m_contains_linker_annotations = rhs.m_contains_linker_annotations;
90 m_mangled = rhs.m_mangled;
91 m_addr_range = rhs.m_addr_range;
92 m_flags = rhs.m_flags;
97 void Symbol::Clear() {
101 m_type_data_resolved = false;
102 m_is_synthetic = false;
104 m_is_external = false;
105 m_size_is_sibling = false;
106 m_size_is_synthesized = false;
107 m_size_is_valid = false;
108 m_demangled_is_synthesized = false;
109 m_contains_linker_annotations = false;
110 m_type = eSymbolTypeInvalid;
112 m_addr_range.Clear();
115 bool Symbol::ValueIsAddress() const {
116 return m_addr_range.GetBaseAddress().GetSection().get() != nullptr;
119 ConstString Symbol::GetDisplayName() const {
120 return m_mangled.GetDisplayDemangledName(GetLanguage());
123 ConstString Symbol::GetReExportedSymbolName() const {
124 if (m_type == eSymbolTypeReExported) {
125 // For eSymbolTypeReExported, the "const char *" from a ConstString
126 // is used as the offset in the address range base address. We can
127 // then make this back into a string that is the re-exported name.
128 intptr_t str_ptr = m_addr_range.GetBaseAddress().GetOffset();
130 return ConstString((const char *)str_ptr);
134 return ConstString();
137 FileSpec Symbol::GetReExportedSymbolSharedLibrary() const {
138 if (m_type == eSymbolTypeReExported) {
139 // For eSymbolTypeReExported, the "const char *" from a ConstString
140 // is used as the offset in the address range base address. We can
141 // then make this back into a string that is the re-exported name.
142 intptr_t str_ptr = m_addr_range.GetByteSize();
144 return FileSpec((const char *)str_ptr, false);
149 void Symbol::SetReExportedSymbolName(const ConstString &name) {
150 SetType(eSymbolTypeReExported);
151 // For eSymbolTypeReExported, the "const char *" from a ConstString
152 // is used as the offset in the address range base address.
153 m_addr_range.GetBaseAddress().SetOffset((uintptr_t)name.GetCString());
156 bool Symbol::SetReExportedSymbolSharedLibrary(const FileSpec &fspec) {
157 if (m_type == eSymbolTypeReExported) {
158 // For eSymbolTypeReExported, the "const char *" from a ConstString
159 // is used as the offset in the address range base address.
160 m_addr_range.SetByteSize(
161 (uintptr_t)ConstString(fspec.GetPath().c_str()).GetCString());
167 uint32_t Symbol::GetSiblingIndex() const {
168 return m_size_is_sibling ? m_addr_range.GetByteSize() : UINT32_MAX;
171 bool Symbol::IsTrampoline() const { return m_type == eSymbolTypeTrampoline; }
173 bool Symbol::IsIndirect() const { return m_type == eSymbolTypeResolver; }
175 void Symbol::GetDescription(Stream *s, lldb::DescriptionLevel level,
176 Target *target) const {
177 s->Printf("id = {0x%8.8x}", m_uid);
179 if (m_addr_range.GetBaseAddress().GetSection()) {
180 if (ValueIsAddress()) {
181 const lldb::addr_t byte_size = GetByteSize();
183 s->PutCString(", range = ");
184 m_addr_range.Dump(s, target, Address::DumpStyleLoadAddress,
185 Address::DumpStyleFileAddress);
187 s->PutCString(", address = ");
188 m_addr_range.GetBaseAddress().Dump(s, target,
189 Address::DumpStyleLoadAddress,
190 Address::DumpStyleFileAddress);
193 s->Printf(", value = 0x%16.16" PRIx64,
194 m_addr_range.GetBaseAddress().GetOffset());
196 if (m_size_is_sibling)
197 s->Printf(", sibling = %5" PRIu64,
198 m_addr_range.GetBaseAddress().GetOffset());
200 s->Printf(", value = 0x%16.16" PRIx64,
201 m_addr_range.GetBaseAddress().GetOffset());
203 ConstString demangled = m_mangled.GetDemangledName(GetLanguage());
205 s->Printf(", name=\"%s\"", demangled.AsCString());
206 if (m_mangled.GetMangledName())
207 s->Printf(", mangled=\"%s\"", m_mangled.GetMangledName().AsCString());
210 void Symbol::Dump(Stream *s, Target *target, uint32_t index) const {
211 s->Printf("[%5u] %6u %c%c%c %-15s ", index, GetID(), m_is_debug ? 'D' : ' ',
212 m_is_synthetic ? 'S' : ' ', m_is_external ? 'X' : ' ',
215 // Make sure the size of the symbol is up to date before dumping
218 ConstString name = m_mangled.GetName(GetLanguage());
219 if (ValueIsAddress()) {
220 if (!m_addr_range.GetBaseAddress().Dump(s, nullptr,
221 Address::DumpStyleFileAddress))
222 s->Printf("%*s", 18, "");
226 if (!m_addr_range.GetBaseAddress().Dump(s, target,
227 Address::DumpStyleLoadAddress))
228 s->Printf("%*s", 18, "");
230 const char *format = m_size_is_sibling ? " Sibling -> [%5llu] 0x%8.8x %s\n"
231 : " 0x%16.16" PRIx64 " 0x%8.8x %s\n";
232 s->Printf(format, GetByteSize(), m_flags, name.AsCString(""));
233 } else if (m_type == eSymbolTypeReExported) {
236 m_flags, name.AsCString(""));
238 ConstString reexport_name = GetReExportedSymbolName();
239 intptr_t shlib = m_addr_range.GetByteSize();
241 s->Printf(" -> %s`%s\n", (const char *)shlib, reexport_name.GetCString());
243 s->Printf(" -> %s\n", reexport_name.GetCString());
248 " Sibling -> [%5llu] 0x%8.8x %s\n"
249 : "0x%16.16" PRIx64 " 0x%16.16" PRIx64
251 s->Printf(format, m_addr_range.GetBaseAddress().GetOffset(), GetByteSize(),
252 m_flags, name.AsCString(""));
256 uint32_t Symbol::GetPrologueByteSize() {
257 if (m_type == eSymbolTypeCode || m_type == eSymbolTypeResolver) {
258 if (!m_type_data_resolved) {
259 m_type_data_resolved = true;
261 const Address &base_address = m_addr_range.GetBaseAddress();
262 Function *function = base_address.CalculateSymbolContextFunction();
264 // Functions have line entries which can also potentially have end of
265 // prologue information.
266 // So if this symbol points to a function, use the prologue information
268 m_type_data = function->GetPrologueByteSize();
270 ModuleSP module_sp(base_address.GetModule());
273 uint32_t resolved_flags = module_sp->ResolveSymbolContextForAddress(
274 base_address, eSymbolContextLineEntry, sc);
275 if (resolved_flags & eSymbolContextLineEntry) {
276 // Default to the end of the first line entry.
277 m_type_data = sc.line_entry.range.GetByteSize();
279 // Set address for next line.
280 Address addr(base_address);
281 addr.Slide(m_type_data);
283 // Check the first few instructions and look for one that has a line
285 // different than the first entry. This is also done in
286 // Function::GetPrologueByteSize().
287 uint16_t total_offset = m_type_data;
288 for (int idx = 0; idx < 6; ++idx) {
289 SymbolContext sc_temp;
290 resolved_flags = module_sp->ResolveSymbolContextForAddress(
291 addr, eSymbolContextLineEntry, sc_temp);
292 // Make sure we got line number information...
293 if (!(resolved_flags & eSymbolContextLineEntry))
296 // If this line number is different than our first one, use it and
298 if (sc_temp.line_entry.line != sc.line_entry.line) {
299 m_type_data = total_offset;
303 // Slide addr up to the next line address.
304 addr.Slide(sc_temp.line_entry.range.GetByteSize());
305 total_offset += sc_temp.line_entry.range.GetByteSize();
306 // If we've gone too far, bail out.
307 if (total_offset >= m_addr_range.GetByteSize())
311 // Sanity check - this may be a function in the middle of code that
312 // has debug information, but
313 // not for this symbol. So the line entries surrounding us won't
314 // lie inside our function.
315 // In that case, the line entry will be bigger than we are, so we do
316 // that quick check and
317 // if that is true, we just return 0.
318 if (m_type_data >= m_addr_range.GetByteSize())
321 // TODO: expose something in Process to figure out the
322 // size of a function prologue.
333 bool Symbol::Compare(const ConstString &name, SymbolType type) const {
334 if (type == eSymbolTypeAny || m_type == type)
335 return m_mangled.GetMangledName() == name ||
336 m_mangled.GetDemangledName(GetLanguage()) == name;
340 #define ENUM_TO_CSTRING(x) \
341 case eSymbolType##x: \
344 const char *Symbol::GetTypeAsString() const {
346 ENUM_TO_CSTRING(Invalid);
347 ENUM_TO_CSTRING(Absolute);
348 ENUM_TO_CSTRING(Code);
349 ENUM_TO_CSTRING(Resolver);
350 ENUM_TO_CSTRING(Data);
351 ENUM_TO_CSTRING(Trampoline);
352 ENUM_TO_CSTRING(Runtime);
353 ENUM_TO_CSTRING(Exception);
354 ENUM_TO_CSTRING(SourceFile);
355 ENUM_TO_CSTRING(HeaderFile);
356 ENUM_TO_CSTRING(ObjectFile);
357 ENUM_TO_CSTRING(CommonBlock);
358 ENUM_TO_CSTRING(Block);
359 ENUM_TO_CSTRING(Local);
360 ENUM_TO_CSTRING(Param);
361 ENUM_TO_CSTRING(Variable);
362 ENUM_TO_CSTRING(VariableType);
363 ENUM_TO_CSTRING(LineEntry);
364 ENUM_TO_CSTRING(LineHeader);
365 ENUM_TO_CSTRING(ScopeBegin);
366 ENUM_TO_CSTRING(ScopeEnd);
367 ENUM_TO_CSTRING(Additional);
368 ENUM_TO_CSTRING(Compiler);
369 ENUM_TO_CSTRING(Instrumentation);
370 ENUM_TO_CSTRING(Undefined);
371 ENUM_TO_CSTRING(ObjCClass);
372 ENUM_TO_CSTRING(ObjCMetaClass);
373 ENUM_TO_CSTRING(ObjCIVar);
374 ENUM_TO_CSTRING(ReExported);
378 return "<unknown SymbolType>";
381 void Symbol::CalculateSymbolContext(SymbolContext *sc) {
382 // Symbols can reconstruct the symbol and the module in the symbol context
384 if (ValueIsAddress())
385 sc->module_sp = GetAddressRef().GetModule();
387 sc->module_sp.reset();
390 ModuleSP Symbol::CalculateSymbolContextModule() {
391 if (ValueIsAddress())
392 return GetAddressRef().GetModule();
396 Symbol *Symbol::CalculateSymbolContextSymbol() { return this; }
398 void Symbol::DumpSymbolContext(Stream *s) {
399 bool dumped_module = false;
400 if (ValueIsAddress()) {
401 ModuleSP module_sp(GetAddressRef().GetModule());
403 dumped_module = true;
404 module_sp->DumpSymbolContext(s);
410 s->Printf("Symbol{0x%8.8x}", GetID());
413 lldb::addr_t Symbol::GetByteSize() const { return m_addr_range.GetByteSize(); }
415 Symbol *Symbol::ResolveReExportedSymbolInModuleSpec(
416 Target &target, ConstString &reexport_name, ModuleSpec &module_spec,
417 ModuleList &seen_modules) const {
419 if (module_spec.GetFileSpec()) {
420 // Try searching for the module file spec first using the full path
421 module_sp = target.GetImages().FindFirstModule(module_spec);
423 // Next try and find the module by basename in case environment
424 // variables or other runtime trickery causes shared libraries
425 // to be loaded from alternate paths
426 module_spec.GetFileSpec().GetDirectory().Clear();
427 module_sp = target.GetImages().FindFirstModule(module_spec);
432 // There should not be cycles in the reexport list, but we don't want to
433 // crash if there are so make sure
434 // we haven't seen this before:
435 if (!seen_modules.AppendIfNeeded(module_sp))
438 lldb_private::SymbolContextList sc_list;
439 module_sp->FindSymbolsWithNameAndType(reexport_name, eSymbolTypeAny,
441 const size_t num_scs = sc_list.GetSize();
443 for (size_t i = 0; i < num_scs; ++i) {
444 lldb_private::SymbolContext sc;
445 if (sc_list.GetContextAtIndex(i, sc)) {
446 if (sc.symbol->IsExternal())
451 // If we didn't find the symbol in this module, it may be because this
452 // module re-exports some
453 // whole other library. We have to search those as well:
454 seen_modules.Append(module_sp);
456 FileSpecList reexported_libraries =
457 module_sp->GetObjectFile()->GetReExportedLibraries();
458 size_t num_reexported_libraries = reexported_libraries.GetSize();
459 for (size_t idx = 0; idx < num_reexported_libraries; idx++) {
460 ModuleSpec reexported_module_spec;
461 reexported_module_spec.GetFileSpec() =
462 reexported_libraries.GetFileSpecAtIndex(idx);
463 Symbol *result_symbol = ResolveReExportedSymbolInModuleSpec(
464 target, reexport_name, reexported_module_spec, seen_modules);
466 return result_symbol;
472 Symbol *Symbol::ResolveReExportedSymbol(Target &target) const {
473 ConstString reexport_name(GetReExportedSymbolName());
475 ModuleSpec module_spec;
476 ModuleList seen_modules;
477 module_spec.GetFileSpec() = GetReExportedSymbolSharedLibrary();
478 if (module_spec.GetFileSpec()) {
479 return ResolveReExportedSymbolInModuleSpec(target, reexport_name,
480 module_spec, seen_modules);
486 lldb::addr_t Symbol::GetFileAddress() const {
487 if (ValueIsAddress())
488 return GetAddressRef().GetFileAddress();
490 return LLDB_INVALID_ADDRESS;
493 lldb::addr_t Symbol::GetLoadAddress(Target *target) const {
494 if (ValueIsAddress())
495 return GetAddressRef().GetLoadAddress(target);
497 return LLDB_INVALID_ADDRESS;
500 ConstString Symbol::GetName() const { return m_mangled.GetName(GetLanguage()); }
502 ConstString Symbol::GetNameNoArguments() const {
503 return m_mangled.GetName(GetLanguage(),
504 Mangled::ePreferDemangledWithoutArguments);
507 lldb::addr_t Symbol::ResolveCallableAddress(Target &target) const {
508 if (GetType() == lldb::eSymbolTypeUndefined)
509 return LLDB_INVALID_ADDRESS;
511 Address func_so_addr;
513 bool is_indirect = IsIndirect();
514 if (GetType() == eSymbolTypeReExported) {
515 Symbol *reexported_symbol = ResolveReExportedSymbol(target);
516 if (reexported_symbol) {
517 func_so_addr = reexported_symbol->GetAddress();
518 is_indirect = reexported_symbol->IsIndirect();
521 func_so_addr = GetAddress();
522 is_indirect = IsIndirect();
525 if (func_so_addr.IsValid()) {
526 if (!target.GetProcessSP() && is_indirect) {
527 // can't resolve indirect symbols without calling a function...
528 return LLDB_INVALID_ADDRESS;
531 lldb::addr_t load_addr =
532 func_so_addr.GetCallableLoadAddress(&target, is_indirect);
534 if (load_addr != LLDB_INVALID_ADDRESS) {
539 return LLDB_INVALID_ADDRESS;
542 lldb::DisassemblerSP Symbol::GetInstructions(const ExecutionContext &exe_ctx,
544 bool prefer_file_cache) {
545 ModuleSP module_sp(m_addr_range.GetBaseAddress().GetModule());
547 const bool prefer_file_cache = false;
548 return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr,
549 flavor, exe_ctx, m_addr_range,
552 return lldb::DisassemblerSP();
555 bool Symbol::GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor,
556 bool prefer_file_cache, Stream &strm) {
557 lldb::DisassemblerSP disassembler_sp =
558 GetInstructions(exe_ctx, flavor, prefer_file_cache);
559 if (disassembler_sp) {
560 const bool show_address = true;
561 const bool show_bytes = false;
562 disassembler_sp->GetInstructionList().Dump(&strm, show_address, show_bytes,
569 bool Symbol::ContainsFileAddress(lldb::addr_t file_addr) const {
570 return m_addr_range.ContainsFileAddress(file_addr);