1 //===-- Symbol.cpp ----------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Symbol/Symbol.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ModuleSpec.h"
13 #include "lldb/Core/Section.h"
14 #include "lldb/Symbol/Function.h"
15 #include "lldb/Symbol/ObjectFile.h"
16 #include "lldb/Symbol/SymbolVendor.h"
17 #include "lldb/Symbol/Symtab.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/Stream.h"
23 using namespace lldb_private;
26 : SymbolContextScope(), m_uid(UINT32_MAX), m_type_data(0),
27 m_type_data_resolved(false), m_is_synthetic(false), m_is_debug(false),
28 m_is_external(false), m_size_is_sibling(false),
29 m_size_is_synthesized(false), m_size_is_valid(false),
30 m_demangled_is_synthesized(false), m_contains_linker_annotations(false),
31 m_is_weak(false), m_type(eSymbolTypeInvalid), m_mangled(), m_addr_range(),
34 Symbol::Symbol(uint32_t symID, llvm::StringRef name, SymbolType type, bool external,
35 bool is_debug, bool is_trampoline, bool is_artificial,
36 const lldb::SectionSP §ion_sp, addr_t offset, addr_t size,
37 bool size_is_valid, bool contains_linker_annotations,
39 : SymbolContextScope(), m_uid(symID), m_type_data(0),
40 m_type_data_resolved(false), m_is_synthetic(is_artificial),
41 m_is_debug(is_debug), m_is_external(external), m_size_is_sibling(false),
42 m_size_is_synthesized(false), m_size_is_valid(size_is_valid || size > 0),
43 m_demangled_is_synthesized(false),
44 m_contains_linker_annotations(contains_linker_annotations),
45 m_is_weak(false), m_type(type),
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),
61 m_is_weak(false), m_type(type), m_mangled(mangled), m_addr_range(range),
64 Symbol::Symbol(const Symbol &rhs)
65 : SymbolContextScope(rhs), m_uid(rhs.m_uid), m_type_data(rhs.m_type_data),
66 m_type_data_resolved(rhs.m_type_data_resolved),
67 m_is_synthetic(rhs.m_is_synthetic), m_is_debug(rhs.m_is_debug),
68 m_is_external(rhs.m_is_external),
69 m_size_is_sibling(rhs.m_size_is_sibling), m_size_is_synthesized(false),
70 m_size_is_valid(rhs.m_size_is_valid),
71 m_demangled_is_synthesized(rhs.m_demangled_is_synthesized),
72 m_contains_linker_annotations(rhs.m_contains_linker_annotations),
73 m_is_weak(rhs.m_is_weak), m_type(rhs.m_type), m_mangled(rhs.m_mangled),
74 m_addr_range(rhs.m_addr_range), m_flags(rhs.m_flags) {}
76 const Symbol &Symbol::operator=(const Symbol &rhs) {
78 SymbolContextScope::operator=(rhs);
80 m_type_data = rhs.m_type_data;
81 m_type_data_resolved = rhs.m_type_data_resolved;
82 m_is_synthetic = rhs.m_is_synthetic;
83 m_is_debug = rhs.m_is_debug;
84 m_is_external = rhs.m_is_external;
85 m_size_is_sibling = rhs.m_size_is_sibling;
86 m_size_is_synthesized = rhs.m_size_is_sibling;
87 m_size_is_valid = rhs.m_size_is_valid;
88 m_demangled_is_synthesized = rhs.m_demangled_is_synthesized;
89 m_contains_linker_annotations = rhs.m_contains_linker_annotations;
90 m_is_weak = rhs.m_is_weak;
92 m_mangled = rhs.m_mangled;
93 m_addr_range = rhs.m_addr_range;
94 m_flags = rhs.m_flags;
99 void Symbol::Clear() {
103 m_type_data_resolved = false;
104 m_is_synthetic = false;
106 m_is_external = false;
107 m_size_is_sibling = false;
108 m_size_is_synthesized = false;
109 m_size_is_valid = false;
110 m_demangled_is_synthesized = false;
111 m_contains_linker_annotations = false;
113 m_type = eSymbolTypeInvalid;
115 m_addr_range.Clear();
118 bool Symbol::ValueIsAddress() const {
119 return m_addr_range.GetBaseAddress().GetSection().get() != nullptr;
122 ConstString Symbol::GetDisplayName() const {
123 return m_mangled.GetDisplayDemangledName(GetLanguage());
126 ConstString Symbol::GetReExportedSymbolName() const {
127 if (m_type == eSymbolTypeReExported) {
128 // For eSymbolTypeReExported, the "const char *" from a ConstString is used
129 // as the offset in the address range base address. We can then make this
130 // back into a string that is the re-exported name.
131 intptr_t str_ptr = m_addr_range.GetBaseAddress().GetOffset();
133 return ConstString((const char *)str_ptr);
137 return ConstString();
140 FileSpec Symbol::GetReExportedSymbolSharedLibrary() const {
141 if (m_type == eSymbolTypeReExported) {
142 // For eSymbolTypeReExported, the "const char *" from a ConstString is used
143 // as the offset in the address range base address. We can then make this
144 // back into a string that is the re-exported name.
145 intptr_t str_ptr = m_addr_range.GetByteSize();
147 return FileSpec((const char *)str_ptr);
152 void Symbol::SetReExportedSymbolName(ConstString name) {
153 SetType(eSymbolTypeReExported);
154 // For eSymbolTypeReExported, the "const char *" from a ConstString is used
155 // as the offset in the address range base address.
156 m_addr_range.GetBaseAddress().SetOffset((uintptr_t)name.GetCString());
159 bool Symbol::SetReExportedSymbolSharedLibrary(const FileSpec &fspec) {
160 if (m_type == eSymbolTypeReExported) {
161 // For eSymbolTypeReExported, the "const char *" from a ConstString is used
162 // as the offset in the address range base address.
163 m_addr_range.SetByteSize(
164 (uintptr_t)ConstString(fspec.GetPath().c_str()).GetCString());
170 uint32_t Symbol::GetSiblingIndex() const {
171 return m_size_is_sibling ? m_addr_range.GetByteSize() : UINT32_MAX;
174 bool Symbol::IsTrampoline() const { return m_type == eSymbolTypeTrampoline; }
176 bool Symbol::IsIndirect() const { return m_type == eSymbolTypeResolver; }
178 void Symbol::GetDescription(Stream *s, lldb::DescriptionLevel level,
179 Target *target) const {
180 s->Printf("id = {0x%8.8x}", m_uid);
182 if (m_addr_range.GetBaseAddress().GetSection()) {
183 if (ValueIsAddress()) {
184 const lldb::addr_t byte_size = GetByteSize();
186 s->PutCString(", range = ");
187 m_addr_range.Dump(s, target, Address::DumpStyleLoadAddress,
188 Address::DumpStyleFileAddress);
190 s->PutCString(", address = ");
191 m_addr_range.GetBaseAddress().Dump(s, target,
192 Address::DumpStyleLoadAddress,
193 Address::DumpStyleFileAddress);
196 s->Printf(", value = 0x%16.16" PRIx64,
197 m_addr_range.GetBaseAddress().GetOffset());
199 if (m_size_is_sibling)
200 s->Printf(", sibling = %5" PRIu64,
201 m_addr_range.GetBaseAddress().GetOffset());
203 s->Printf(", value = 0x%16.16" PRIx64,
204 m_addr_range.GetBaseAddress().GetOffset());
206 ConstString demangled = m_mangled.GetDemangledName(GetLanguage());
208 s->Printf(", name=\"%s\"", demangled.AsCString());
209 if (m_mangled.GetMangledName())
210 s->Printf(", mangled=\"%s\"", m_mangled.GetMangledName().AsCString());
213 void Symbol::Dump(Stream *s, Target *target, uint32_t index) const {
214 s->Printf("[%5u] %6u %c%c%c %-15s ", index, GetID(), m_is_debug ? 'D' : ' ',
215 m_is_synthetic ? 'S' : ' ', m_is_external ? 'X' : ' ',
218 // Make sure the size of the symbol is up to date before dumping
221 ConstString name = m_mangled.GetName(GetLanguage());
222 if (ValueIsAddress()) {
223 if (!m_addr_range.GetBaseAddress().Dump(s, nullptr,
224 Address::DumpStyleFileAddress))
225 s->Printf("%*s", 18, "");
229 if (!m_addr_range.GetBaseAddress().Dump(s, target,
230 Address::DumpStyleLoadAddress))
231 s->Printf("%*s", 18, "");
233 const char *format = m_size_is_sibling ? " Sibling -> [%5llu] 0x%8.8x %s\n"
234 : " 0x%16.16" PRIx64 " 0x%8.8x %s\n";
235 s->Printf(format, GetByteSize(), m_flags, name.AsCString(""));
236 } else if (m_type == eSymbolTypeReExported) {
239 m_flags, name.AsCString(""));
241 ConstString reexport_name = GetReExportedSymbolName();
242 intptr_t shlib = m_addr_range.GetByteSize();
244 s->Printf(" -> %s`%s\n", (const char *)shlib, reexport_name.GetCString());
246 s->Printf(" -> %s\n", reexport_name.GetCString());
251 " Sibling -> [%5llu] 0x%8.8x %s\n"
252 : "0x%16.16" PRIx64 " 0x%16.16" PRIx64
254 s->Printf(format, m_addr_range.GetBaseAddress().GetOffset(), GetByteSize(),
255 m_flags, name.AsCString(""));
259 uint32_t Symbol::GetPrologueByteSize() {
260 if (m_type == eSymbolTypeCode || m_type == eSymbolTypeResolver) {
261 if (!m_type_data_resolved) {
262 m_type_data_resolved = true;
264 const Address &base_address = m_addr_range.GetBaseAddress();
265 Function *function = base_address.CalculateSymbolContextFunction();
267 // Functions have line entries which can also potentially have end of
268 // prologue information. So if this symbol points to a function, use
269 // the prologue information from there.
270 m_type_data = function->GetPrologueByteSize();
272 ModuleSP module_sp(base_address.GetModule());
275 uint32_t resolved_flags = module_sp->ResolveSymbolContextForAddress(
276 base_address, eSymbolContextLineEntry, sc);
277 if (resolved_flags & eSymbolContextLineEntry) {
278 // Default to the end of the first line entry.
279 m_type_data = sc.line_entry.range.GetByteSize();
281 // Set address for next line.
282 Address addr(base_address);
283 addr.Slide(m_type_data);
285 // Check the first few instructions and look for one that has a
286 // line number that is different than the first entry. This is also
287 // done in Function::GetPrologueByteSize().
288 uint16_t total_offset = m_type_data;
289 for (int idx = 0; idx < 6; ++idx) {
290 SymbolContext sc_temp;
291 resolved_flags = module_sp->ResolveSymbolContextForAddress(
292 addr, eSymbolContextLineEntry, sc_temp);
293 // Make sure we got line number information...
294 if (!(resolved_flags & eSymbolContextLineEntry))
297 // If this line number is different than our first one, use it
299 if (sc_temp.line_entry.line != sc.line_entry.line) {
300 m_type_data = total_offset;
304 // Slide addr up to the next line address.
305 addr.Slide(sc_temp.line_entry.range.GetByteSize());
306 total_offset += sc_temp.line_entry.range.GetByteSize();
307 // If we've gone too far, bail out.
308 if (total_offset >= m_addr_range.GetByteSize())
312 // Sanity check - this may be a function in the middle of code that
313 // has debug information, but not for this symbol. So the line
314 // entries surrounding us won't lie inside our function. In that
315 // case, the line entry will be bigger than we are, so we do that
316 // quick check and if that is true, we just return 0.
317 if (m_type_data >= m_addr_range.GetByteSize())
320 // TODO: expose something in Process to figure out the
321 // size of a function prologue.
332 bool Symbol::Compare(ConstString name, SymbolType type) const {
333 if (type == eSymbolTypeAny || m_type == type)
334 return m_mangled.GetMangledName() == name ||
335 m_mangled.GetDemangledName(GetLanguage()) == name;
339 #define ENUM_TO_CSTRING(x) \
340 case eSymbolType##x: \
343 const char *Symbol::GetTypeAsString() const {
345 ENUM_TO_CSTRING(Invalid);
346 ENUM_TO_CSTRING(Absolute);
347 ENUM_TO_CSTRING(Code);
348 ENUM_TO_CSTRING(Resolver);
349 ENUM_TO_CSTRING(Data);
350 ENUM_TO_CSTRING(Trampoline);
351 ENUM_TO_CSTRING(Runtime);
352 ENUM_TO_CSTRING(Exception);
353 ENUM_TO_CSTRING(SourceFile);
354 ENUM_TO_CSTRING(HeaderFile);
355 ENUM_TO_CSTRING(ObjectFile);
356 ENUM_TO_CSTRING(CommonBlock);
357 ENUM_TO_CSTRING(Block);
358 ENUM_TO_CSTRING(Local);
359 ENUM_TO_CSTRING(Param);
360 ENUM_TO_CSTRING(Variable);
361 ENUM_TO_CSTRING(VariableType);
362 ENUM_TO_CSTRING(LineEntry);
363 ENUM_TO_CSTRING(LineHeader);
364 ENUM_TO_CSTRING(ScopeBegin);
365 ENUM_TO_CSTRING(ScopeEnd);
366 ENUM_TO_CSTRING(Additional);
367 ENUM_TO_CSTRING(Compiler);
368 ENUM_TO_CSTRING(Instrumentation);
369 ENUM_TO_CSTRING(Undefined);
370 ENUM_TO_CSTRING(ObjCClass);
371 ENUM_TO_CSTRING(ObjCMetaClass);
372 ENUM_TO_CSTRING(ObjCIVar);
373 ENUM_TO_CSTRING(ReExported);
377 return "<unknown SymbolType>";
380 void Symbol::CalculateSymbolContext(SymbolContext *sc) {
381 // Symbols can reconstruct the symbol and the module in the symbol context
383 if (ValueIsAddress())
384 sc->module_sp = GetAddressRef().GetModule();
386 sc->module_sp.reset();
389 ModuleSP Symbol::CalculateSymbolContextModule() {
390 if (ValueIsAddress())
391 return GetAddressRef().GetModule();
395 Symbol *Symbol::CalculateSymbolContextSymbol() { return this; }
397 void Symbol::DumpSymbolContext(Stream *s) {
398 bool dumped_module = false;
399 if (ValueIsAddress()) {
400 ModuleSP module_sp(GetAddressRef().GetModule());
402 dumped_module = true;
403 module_sp->DumpSymbolContext(s);
409 s->Printf("Symbol{0x%8.8x}", GetID());
412 lldb::addr_t Symbol::GetByteSize() const { return m_addr_range.GetByteSize(); }
414 Symbol *Symbol::ResolveReExportedSymbolInModuleSpec(
415 Target &target, ConstString &reexport_name, ModuleSpec &module_spec,
416 ModuleList &seen_modules) const {
418 if (module_spec.GetFileSpec()) {
419 // Try searching for the module file spec first using the full path
420 module_sp = target.GetImages().FindFirstModule(module_spec);
422 // Next try and find the module by basename in case environment variables
423 // or other runtime trickery causes shared libraries to be loaded from
425 module_spec.GetFileSpec().GetDirectory().Clear();
426 module_sp = target.GetImages().FindFirstModule(module_spec);
431 // There should not be cycles in the reexport list, but we don't want to
432 // crash if there are so make sure we haven't seen this before:
433 if (!seen_modules.AppendIfNeeded(module_sp))
436 lldb_private::SymbolContextList sc_list;
437 module_sp->FindSymbolsWithNameAndType(reexport_name, eSymbolTypeAny,
439 const size_t num_scs = sc_list.GetSize();
441 for (size_t i = 0; i < num_scs; ++i) {
442 lldb_private::SymbolContext sc;
443 if (sc_list.GetContextAtIndex(i, sc)) {
444 if (sc.symbol->IsExternal())
449 // If we didn't find the symbol in this module, it may be because this
450 // module re-exports some whole other library. We have to search those as
452 seen_modules.Append(module_sp);
454 FileSpecList reexported_libraries =
455 module_sp->GetObjectFile()->GetReExportedLibraries();
456 size_t num_reexported_libraries = reexported_libraries.GetSize();
457 for (size_t idx = 0; idx < num_reexported_libraries; idx++) {
458 ModuleSpec reexported_module_spec;
459 reexported_module_spec.GetFileSpec() =
460 reexported_libraries.GetFileSpecAtIndex(idx);
461 Symbol *result_symbol = ResolveReExportedSymbolInModuleSpec(
462 target, reexport_name, reexported_module_spec, seen_modules);
464 return result_symbol;
470 Symbol *Symbol::ResolveReExportedSymbol(Target &target) const {
471 ConstString reexport_name(GetReExportedSymbolName());
473 ModuleSpec module_spec;
474 ModuleList seen_modules;
475 module_spec.GetFileSpec() = GetReExportedSymbolSharedLibrary();
476 if (module_spec.GetFileSpec()) {
477 return ResolveReExportedSymbolInModuleSpec(target, reexport_name,
478 module_spec, seen_modules);
484 lldb::addr_t Symbol::GetFileAddress() const {
485 if (ValueIsAddress())
486 return GetAddressRef().GetFileAddress();
488 return LLDB_INVALID_ADDRESS;
491 lldb::addr_t Symbol::GetLoadAddress(Target *target) const {
492 if (ValueIsAddress())
493 return GetAddressRef().GetLoadAddress(target);
495 return LLDB_INVALID_ADDRESS;
498 ConstString Symbol::GetName() const { return m_mangled.GetName(GetLanguage()); }
500 ConstString Symbol::GetNameNoArguments() const {
501 return m_mangled.GetName(GetLanguage(),
502 Mangled::ePreferDemangledWithoutArguments);
505 lldb::addr_t Symbol::ResolveCallableAddress(Target &target) const {
506 if (GetType() == lldb::eSymbolTypeUndefined)
507 return LLDB_INVALID_ADDRESS;
509 Address func_so_addr;
511 bool is_indirect = IsIndirect();
512 if (GetType() == eSymbolTypeReExported) {
513 Symbol *reexported_symbol = ResolveReExportedSymbol(target);
514 if (reexported_symbol) {
515 func_so_addr = reexported_symbol->GetAddress();
516 is_indirect = reexported_symbol->IsIndirect();
519 func_so_addr = GetAddress();
520 is_indirect = IsIndirect();
523 if (func_so_addr.IsValid()) {
524 if (!target.GetProcessSP() && is_indirect) {
525 // can't resolve indirect symbols without calling a function...
526 return LLDB_INVALID_ADDRESS;
529 lldb::addr_t load_addr =
530 func_so_addr.GetCallableLoadAddress(&target, is_indirect);
532 if (load_addr != LLDB_INVALID_ADDRESS) {
537 return LLDB_INVALID_ADDRESS;
540 lldb::DisassemblerSP Symbol::GetInstructions(const ExecutionContext &exe_ctx,
542 bool prefer_file_cache) {
543 ModuleSP module_sp(m_addr_range.GetBaseAddress().GetModule());
545 const bool prefer_file_cache = false;
546 return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr,
547 flavor, exe_ctx, m_addr_range,
550 return lldb::DisassemblerSP();
553 bool Symbol::GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor,
554 bool prefer_file_cache, Stream &strm) {
555 lldb::DisassemblerSP disassembler_sp =
556 GetInstructions(exe_ctx, flavor, prefer_file_cache);
557 if (disassembler_sp) {
558 const bool show_address = true;
559 const bool show_bytes = false;
560 disassembler_sp->GetInstructionList().Dump(&strm, show_address, show_bytes,
567 bool Symbol::ContainsFileAddress(lldb::addr_t file_addr) const {
568 return m_addr_range.ContainsFileAddress(file_addr);