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/Core/Stream.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Symbol/Symtab.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Symbol/SymbolVendor.h"
24 using namespace lldb_private;
28 SymbolContextScope (),
31 m_type_data_resolved (false),
32 m_is_synthetic (false),
34 m_is_external (false),
35 m_size_is_sibling (false),
36 m_size_is_synthesized (false),
37 m_size_is_valid (false),
38 m_demangled_is_synthesized (false),
39 m_contains_linker_annotations (false),
40 m_type (eSymbolTypeInvalid),
57 const lldb::SectionSP §ion_sp,
61 bool contains_linker_annotations,
64 SymbolContextScope (),
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),
73 m_size_is_valid (size_is_valid || size > 0),
74 m_demangled_is_synthesized (false),
75 m_contains_linker_annotations (contains_linker_annotations),
77 m_mangled (ConstString(name), name_is_mangled),
78 m_addr_range (section_sp, offset, size),
86 const Mangled &mangled,
92 const AddressRange &range,
94 bool contains_linker_annotations,
97 SymbolContextScope (),
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),
106 m_size_is_valid (size_is_valid || range.GetByteSize() > 0),
107 m_demangled_is_synthesized (false),
108 m_contains_linker_annotations (contains_linker_annotations),
111 m_addr_range (range),
116 Symbol::Symbol(const Symbol& rhs):
117 SymbolContextScope (rhs),
119 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),
126 m_size_is_valid (rhs.m_size_is_valid),
127 m_demangled_is_synthesized (rhs.m_demangled_is_synthesized),
128 m_contains_linker_annotations (rhs.m_contains_linker_annotations),
130 m_mangled (rhs.m_mangled),
131 m_addr_range (rhs.m_addr_range),
132 m_flags (rhs.m_flags)
137 Symbol::operator= (const Symbol& rhs)
141 SymbolContextScope::operator= (rhs);
143 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;
150 m_size_is_valid = rhs.m_size_is_valid;
151 m_demangled_is_synthesized = rhs.m_demangled_is_synthesized;
152 m_contains_linker_annotations = rhs.m_contains_linker_annotations;
154 m_mangled = rhs.m_mangled;
155 m_addr_range = rhs.m_addr_range;
156 m_flags = rhs.m_flags;
167 m_type_data_resolved = false;
168 m_is_synthetic = false;
170 m_is_external = false;
171 m_size_is_sibling = false;
172 m_size_is_synthesized = false;
173 m_size_is_valid = false;
174 m_demangled_is_synthesized = false;
175 m_contains_linker_annotations = false;
176 m_type = eSymbolTypeInvalid;
178 m_addr_range.Clear();
182 Symbol::ValueIsAddress() const
184 return m_addr_range.GetBaseAddress().GetSection().get() != nullptr;
188 Symbol::GetDisplayName () const
191 return ConstString();
192 return m_mangled.GetDisplayDemangledName(GetLanguage());
196 Symbol::GetReExportedSymbolName() const
198 if (m_type == eSymbolTypeReExported)
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();
205 return ConstString((const char *)str_ptr);
209 return ConstString();
213 Symbol::GetReExportedSymbolSharedLibrary() const
215 if (m_type == eSymbolTypeReExported)
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();
222 return FileSpec((const char *)str_ptr, false);
228 Symbol::SetReExportedSymbolName(const ConstString &name)
230 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());
237 Symbol::SetReExportedSymbolSharedLibrary(const FileSpec &fspec)
239 if (m_type == eSymbolTypeReExported)
241 // For eSymbolTypeReExported, the "const char *" from a ConstString
242 // is used as the offset in the address range base address.
243 m_addr_range.SetByteSize((uintptr_t)ConstString(fspec.GetPath().c_str()).GetCString());
251 Symbol::GetSiblingIndex() const
253 return m_size_is_sibling ? m_addr_range.GetByteSize() : UINT32_MAX;
257 Symbol::IsTrampoline () const
259 return m_type == eSymbolTypeTrampoline;
263 Symbol::IsIndirect () const
265 return m_type == eSymbolTypeResolver;
269 Symbol::GetDescription (Stream *s, lldb::DescriptionLevel level, Target *target) const
271 s->Printf("id = {0x%8.8x}", m_uid);
273 if (m_addr_range.GetBaseAddress().GetSection())
275 if (ValueIsAddress())
277 const lldb::addr_t byte_size = GetByteSize();
280 s->PutCString (", range = ");
281 m_addr_range.Dump(s, target, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
285 s->PutCString (", address = ");
286 m_addr_range.GetBaseAddress().Dump(s, target, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
290 s->Printf (", value = 0x%16.16" PRIx64, m_addr_range.GetBaseAddress().GetOffset());
294 if (m_size_is_sibling)
295 s->Printf (", sibling = %5" PRIu64, m_addr_range.GetBaseAddress().GetOffset());
297 s->Printf (", value = 0x%16.16" PRIx64, m_addr_range.GetBaseAddress().GetOffset());
299 ConstString demangled = m_mangled.GetDemangledName(GetLanguage());
301 s->Printf(", name=\"%s\"", demangled.AsCString());
302 if (m_mangled.GetMangledName())
303 s->Printf(", mangled=\"%s\"", m_mangled.GetMangledName().AsCString());
308 Symbol::Dump(Stream *s, Target *target, uint32_t index) const
310 s->Printf("[%5u] %6u %c%c%c %-15s ",
313 m_is_debug ? 'D' : ' ',
314 m_is_synthetic ? 'S' : ' ',
315 m_is_external ? 'X' : ' ',
318 // Make sure the size of the symbol is up to date before dumping
321 ConstString name = m_mangled.GetName(GetLanguage());
322 if (ValueIsAddress())
324 if (!m_addr_range.GetBaseAddress().Dump(s, nullptr, Address::DumpStyleFileAddress))
325 s->Printf("%*s", 18, "");
329 if (!m_addr_range.GetBaseAddress().Dump(s, target, Address::DumpStyleLoadAddress))
330 s->Printf("%*s", 18, "");
332 const char *format = m_size_is_sibling ?
333 " Sibling -> [%5llu] 0x%8.8x %s\n":
334 " 0x%16.16" PRIx64 " 0x%8.8x %s\n";
340 else if (m_type == eSymbolTypeReExported)
342 s->Printf (" 0x%8.8x %s",
346 ConstString reexport_name = GetReExportedSymbolName();
347 intptr_t shlib = m_addr_range.GetByteSize();
349 s->Printf(" -> %s`%s\n", (const char *)shlib, reexport_name.GetCString());
351 s->Printf(" -> %s\n", reexport_name.GetCString());
355 const char *format = m_size_is_sibling ?
356 "0x%16.16" PRIx64 " Sibling -> [%5llu] 0x%8.8x %s\n":
357 "0x%16.16" PRIx64 " 0x%16.16" PRIx64 " 0x%8.8x %s\n";
359 m_addr_range.GetBaseAddress().GetOffset(),
367 Symbol::GetPrologueByteSize ()
369 if (m_type == eSymbolTypeCode || m_type == eSymbolTypeResolver)
371 if (!m_type_data_resolved)
373 m_type_data_resolved = true;
375 const Address &base_address = m_addr_range.GetBaseAddress();
376 Function *function = base_address.CalculateSymbolContextFunction();
379 // Functions have line entries which can also potentially have end of prologue information.
380 // So if this symbol points to a function, use the prologue information from there.
381 m_type_data = function->GetPrologueByteSize();
385 ModuleSP module_sp (base_address.GetModule());
389 uint32_t resolved_flags = module_sp->ResolveSymbolContextForAddress (base_address,
390 eSymbolContextLineEntry,
392 if (resolved_flags & eSymbolContextLineEntry)
394 // Default to the end of the first line entry.
395 m_type_data = sc.line_entry.range.GetByteSize();
397 // Set address for next line.
398 Address addr (base_address);
399 addr.Slide (m_type_data);
401 // Check the first few instructions and look for one that has a line number that is
402 // different than the first entry. This is also done in Function::GetPrologueByteSize().
403 uint16_t total_offset = m_type_data;
404 for (int idx = 0; idx < 6; ++idx)
406 SymbolContext sc_temp;
407 resolved_flags = module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextLineEntry, sc_temp);
408 // Make sure we got line number information...
409 if (!(resolved_flags & eSymbolContextLineEntry))
412 // If this line number is different than our first one, use it and we're done.
413 if (sc_temp.line_entry.line != sc.line_entry.line)
415 m_type_data = total_offset;
419 // Slide addr up to the next line address.
420 addr.Slide (sc_temp.line_entry.range.GetByteSize());
421 total_offset += sc_temp.line_entry.range.GetByteSize();
422 // If we've gone too far, bail out.
423 if (total_offset >= m_addr_range.GetByteSize())
427 // Sanity check - this may be a function in the middle of code that has debug information, but
428 // not for this symbol. So the line entries surrounding us won't lie inside our function.
429 // In that case, the line entry will be bigger than we are, so we do that quick check and
430 // if that is true, we just return 0.
431 if (m_type_data >= m_addr_range.GetByteSize())
436 // TODO: expose something in Process to figure out the
437 // size of a function prologue.
449 Symbol::Compare(const ConstString& name, SymbolType type) const
451 if (type == eSymbolTypeAny || m_type == type)
452 return m_mangled.GetMangledName() == name || m_mangled.GetDemangledName(GetLanguage()) == name;
456 #define ENUM_TO_CSTRING(x) case eSymbolType##x: return #x;
459 Symbol::GetTypeAsString() const
463 ENUM_TO_CSTRING(Invalid);
464 ENUM_TO_CSTRING(Absolute);
465 ENUM_TO_CSTRING(Code);
466 ENUM_TO_CSTRING(Resolver);
467 ENUM_TO_CSTRING(Data);
468 ENUM_TO_CSTRING(Trampoline);
469 ENUM_TO_CSTRING(Runtime);
470 ENUM_TO_CSTRING(Exception);
471 ENUM_TO_CSTRING(SourceFile);
472 ENUM_TO_CSTRING(HeaderFile);
473 ENUM_TO_CSTRING(ObjectFile);
474 ENUM_TO_CSTRING(CommonBlock);
475 ENUM_TO_CSTRING(Block);
476 ENUM_TO_CSTRING(Local);
477 ENUM_TO_CSTRING(Param);
478 ENUM_TO_CSTRING(Variable);
479 ENUM_TO_CSTRING(VariableType);
480 ENUM_TO_CSTRING(LineEntry);
481 ENUM_TO_CSTRING(LineHeader);
482 ENUM_TO_CSTRING(ScopeBegin);
483 ENUM_TO_CSTRING(ScopeEnd);
484 ENUM_TO_CSTRING(Additional);
485 ENUM_TO_CSTRING(Compiler);
486 ENUM_TO_CSTRING(Instrumentation);
487 ENUM_TO_CSTRING(Undefined);
488 ENUM_TO_CSTRING(ObjCClass);
489 ENUM_TO_CSTRING(ObjCMetaClass);
490 ENUM_TO_CSTRING(ObjCIVar);
491 ENUM_TO_CSTRING(ReExported);
495 return "<unknown SymbolType>";
499 Symbol::CalculateSymbolContext (SymbolContext *sc)
501 // Symbols can reconstruct the symbol and the module in the symbol context
503 if (ValueIsAddress())
504 sc->module_sp = GetAddressRef().GetModule();
506 sc->module_sp.reset();
510 Symbol::CalculateSymbolContextModule ()
512 if (ValueIsAddress())
513 return GetAddressRef().GetModule();
518 Symbol::CalculateSymbolContextSymbol ()
524 Symbol::DumpSymbolContext (Stream *s)
526 bool dumped_module = false;
527 if (ValueIsAddress())
529 ModuleSP module_sp (GetAddressRef().GetModule());
532 dumped_module = true;
533 module_sp->DumpSymbolContext(s);
539 s->Printf("Symbol{0x%8.8x}", GetID());
543 Symbol::GetByteSize () const
545 return m_addr_range.GetByteSize();
550 Symbol::ResolveReExportedSymbolInModuleSpec (Target &target,
551 ConstString &reexport_name,
552 ModuleSpec &module_spec,
553 ModuleList &seen_modules) const
556 if (module_spec.GetFileSpec())
558 // Try searching for the module file spec first using the full path
559 module_sp = target.GetImages().FindFirstModule(module_spec);
562 // Next try and find the module by basename in case environment
563 // variables or other runtime trickery causes shared libraries
564 // to be loaded from alternate paths
565 module_spec.GetFileSpec().GetDirectory().Clear();
566 module_sp = target.GetImages().FindFirstModule(module_spec);
572 // There should not be cycles in the reexport list, but we don't want to crash if there are so make sure
573 // we haven't seen this before:
574 if (!seen_modules.AppendIfNeeded(module_sp))
577 lldb_private::SymbolContextList sc_list;
578 module_sp->FindSymbolsWithNameAndType(reexport_name, eSymbolTypeAny, sc_list);
579 const size_t num_scs = sc_list.GetSize();
582 for (size_t i=0; i<num_scs; ++i)
584 lldb_private::SymbolContext sc;
585 if (sc_list.GetContextAtIndex(i, sc))
587 if (sc.symbol->IsExternal())
592 // If we didn't find the symbol in this module, it may be because this module re-exports some
593 // whole other library. We have to search those as well:
594 seen_modules.Append(module_sp);
596 FileSpecList reexported_libraries = module_sp->GetObjectFile()->GetReExportedLibraries();
597 size_t num_reexported_libraries = reexported_libraries.GetSize();
598 for (size_t idx = 0; idx < num_reexported_libraries; idx++)
600 ModuleSpec reexported_module_spec;
601 reexported_module_spec.GetFileSpec() = reexported_libraries.GetFileSpecAtIndex(idx);
602 Symbol *result_symbol = ResolveReExportedSymbolInModuleSpec(target,
604 reexported_module_spec,
607 return result_symbol;
614 Symbol::ResolveReExportedSymbol (Target &target) const
616 ConstString reexport_name (GetReExportedSymbolName());
619 ModuleSpec module_spec;
620 ModuleList seen_modules;
621 module_spec.GetFileSpec() = GetReExportedSymbolSharedLibrary();
622 if (module_spec.GetFileSpec())
624 return ResolveReExportedSymbolInModuleSpec(target, reexport_name, module_spec, seen_modules);
631 Symbol::GetFileAddress () const
633 if (ValueIsAddress())
634 return GetAddressRef().GetFileAddress();
636 return LLDB_INVALID_ADDRESS;
640 Symbol::GetLoadAddress (Target *target) const
642 if (ValueIsAddress())
643 return GetAddressRef().GetLoadAddress(target);
645 return LLDB_INVALID_ADDRESS;
649 Symbol::GetName () const
651 return m_mangled.GetName(GetLanguage());
655 Symbol::GetNameNoArguments () const
657 return m_mangled.GetName(GetLanguage(), Mangled::ePreferDemangledWithoutArguments);
662 Symbol::ResolveCallableAddress(Target &target) const
664 if (GetType() == lldb::eSymbolTypeUndefined)
665 return LLDB_INVALID_ADDRESS;
667 Address func_so_addr;
669 bool is_indirect = IsIndirect();
670 if (GetType() == eSymbolTypeReExported)
672 Symbol *reexported_symbol = ResolveReExportedSymbol(target);
673 if (reexported_symbol)
675 func_so_addr = reexported_symbol->GetAddress();
676 is_indirect = reexported_symbol->IsIndirect();
681 func_so_addr = GetAddress();
682 is_indirect = IsIndirect();
685 if (func_so_addr.IsValid())
687 if (!target.GetProcessSP() && is_indirect)
689 // can't resolve indirect symbols without calling a function...
690 return LLDB_INVALID_ADDRESS;
693 lldb::addr_t load_addr = func_so_addr.GetCallableLoadAddress (&target, is_indirect);
695 if (load_addr != LLDB_INVALID_ADDRESS)
701 return LLDB_INVALID_ADDRESS;
706 Symbol::GetInstructions (const ExecutionContext &exe_ctx,
708 bool prefer_file_cache)
710 ModuleSP module_sp (m_addr_range.GetBaseAddress().GetModule());
713 const bool prefer_file_cache = false;
714 return Disassembler::DisassembleRange (module_sp->GetArchitecture(),
721 return lldb::DisassemblerSP();
725 Symbol::GetDisassembly (const ExecutionContext &exe_ctx,
727 bool prefer_file_cache,
730 lldb::DisassemblerSP disassembler_sp = GetInstructions (exe_ctx, flavor, prefer_file_cache);
733 const bool show_address = true;
734 const bool show_bytes = false;
735 disassembler_sp->GetInstructionList().Dump (&strm, show_address, show_bytes, &exe_ctx);