]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/Symbol/Symbol.cpp
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / Symbol / Symbol.cpp
1 //===-- Symbol.cpp ----------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Symbol/Symbol.h"
10
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"
21
22 using namespace lldb;
23 using namespace lldb_private;
24
25 Symbol::Symbol()
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(),
32       m_flags() {}
33
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 &section_sp, addr_t offset, addr_t size,
38                bool size_is_valid, bool contains_linker_annotations,
39                uint32_t flags)
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), 
46       m_is_weak(false), m_type(type),
47       m_mangled(ConstString(name), name_is_mangled),
48       m_addr_range(section_sp, offset, size), m_flags(flags) {}
49
50 Symbol::Symbol(uint32_t symID, const Mangled &mangled, SymbolType type,
51                bool external, bool is_debug, bool is_trampoline,
52                bool is_artificial, const AddressRange &range,
53                bool size_is_valid, bool contains_linker_annotations,
54                uint32_t flags)
55     : SymbolContextScope(), m_uid(symID), m_type_data(0),
56       m_type_data_resolved(false), m_is_synthetic(is_artificial),
57       m_is_debug(is_debug), m_is_external(external), m_size_is_sibling(false),
58       m_size_is_synthesized(false),
59       m_size_is_valid(size_is_valid || range.GetByteSize() > 0),
60       m_demangled_is_synthesized(false),
61       m_contains_linker_annotations(contains_linker_annotations), 
62       m_is_weak(false), m_type(type), m_mangled(mangled), m_addr_range(range), 
63       m_flags(flags) {}
64
65 Symbol::Symbol(const Symbol &rhs)
66     : SymbolContextScope(rhs), m_uid(rhs.m_uid), m_type_data(rhs.m_type_data),
67       m_type_data_resolved(rhs.m_type_data_resolved),
68       m_is_synthetic(rhs.m_is_synthetic), m_is_debug(rhs.m_is_debug),
69       m_is_external(rhs.m_is_external),
70       m_size_is_sibling(rhs.m_size_is_sibling), m_size_is_synthesized(false),
71       m_size_is_valid(rhs.m_size_is_valid),
72       m_demangled_is_synthesized(rhs.m_demangled_is_synthesized),
73       m_contains_linker_annotations(rhs.m_contains_linker_annotations),
74       m_is_weak(rhs.m_is_weak), m_type(rhs.m_type), m_mangled(rhs.m_mangled),
75       m_addr_range(rhs.m_addr_range), m_flags(rhs.m_flags) {}
76
77 const Symbol &Symbol::operator=(const Symbol &rhs) {
78   if (this != &rhs) {
79     SymbolContextScope::operator=(rhs);
80     m_uid = rhs.m_uid;
81     m_type_data = rhs.m_type_data;
82     m_type_data_resolved = rhs.m_type_data_resolved;
83     m_is_synthetic = rhs.m_is_synthetic;
84     m_is_debug = rhs.m_is_debug;
85     m_is_external = rhs.m_is_external;
86     m_size_is_sibling = rhs.m_size_is_sibling;
87     m_size_is_synthesized = rhs.m_size_is_sibling;
88     m_size_is_valid = rhs.m_size_is_valid;
89     m_demangled_is_synthesized = rhs.m_demangled_is_synthesized;
90     m_contains_linker_annotations = rhs.m_contains_linker_annotations;
91     m_is_weak = rhs.m_is_weak;
92     m_type = rhs.m_type;
93     m_mangled = rhs.m_mangled;
94     m_addr_range = rhs.m_addr_range;
95     m_flags = rhs.m_flags;
96   }
97   return *this;
98 }
99
100 void Symbol::Clear() {
101   m_uid = UINT32_MAX;
102   m_mangled.Clear();
103   m_type_data = 0;
104   m_type_data_resolved = false;
105   m_is_synthetic = false;
106   m_is_debug = false;
107   m_is_external = false;
108   m_size_is_sibling = false;
109   m_size_is_synthesized = false;
110   m_size_is_valid = false;
111   m_demangled_is_synthesized = false;
112   m_contains_linker_annotations = false;
113   m_is_weak = false;
114   m_type = eSymbolTypeInvalid;
115   m_flags = 0;
116   m_addr_range.Clear();
117 }
118
119 bool Symbol::ValueIsAddress() const {
120   return m_addr_range.GetBaseAddress().GetSection().get() != nullptr;
121 }
122
123 ConstString Symbol::GetDisplayName() const {
124   return m_mangled.GetDisplayDemangledName(GetLanguage());
125 }
126
127 ConstString Symbol::GetReExportedSymbolName() const {
128   if (m_type == eSymbolTypeReExported) {
129     // For eSymbolTypeReExported, the "const char *" from a ConstString is used
130     // as the offset in the address range base address. We can then make this
131     // back into a string that is the re-exported name.
132     intptr_t str_ptr = m_addr_range.GetBaseAddress().GetOffset();
133     if (str_ptr != 0)
134       return ConstString((const char *)str_ptr);
135     else
136       return GetName();
137   }
138   return ConstString();
139 }
140
141 FileSpec Symbol::GetReExportedSymbolSharedLibrary() const {
142   if (m_type == eSymbolTypeReExported) {
143     // For eSymbolTypeReExported, the "const char *" from a ConstString is used
144     // as the offset in the address range base address. We can then make this
145     // back into a string that is the re-exported name.
146     intptr_t str_ptr = m_addr_range.GetByteSize();
147     if (str_ptr != 0)
148       return FileSpec((const char *)str_ptr);
149   }
150   return FileSpec();
151 }
152
153 void Symbol::SetReExportedSymbolName(ConstString name) {
154   SetType(eSymbolTypeReExported);
155   // For eSymbolTypeReExported, the "const char *" from a ConstString is used
156   // as the offset in the address range base address.
157   m_addr_range.GetBaseAddress().SetOffset((uintptr_t)name.GetCString());
158 }
159
160 bool Symbol::SetReExportedSymbolSharedLibrary(const FileSpec &fspec) {
161   if (m_type == eSymbolTypeReExported) {
162     // For eSymbolTypeReExported, the "const char *" from a ConstString is used
163     // as the offset in the address range base address.
164     m_addr_range.SetByteSize(
165         (uintptr_t)ConstString(fspec.GetPath().c_str()).GetCString());
166     return true;
167   }
168   return false;
169 }
170
171 uint32_t Symbol::GetSiblingIndex() const {
172   return m_size_is_sibling ? m_addr_range.GetByteSize() : UINT32_MAX;
173 }
174
175 bool Symbol::IsTrampoline() const { return m_type == eSymbolTypeTrampoline; }
176
177 bool Symbol::IsIndirect() const { return m_type == eSymbolTypeResolver; }
178
179 void Symbol::GetDescription(Stream *s, lldb::DescriptionLevel level,
180                             Target *target) const {
181   s->Printf("id = {0x%8.8x}", m_uid);
182
183   if (m_addr_range.GetBaseAddress().GetSection()) {
184     if (ValueIsAddress()) {
185       const lldb::addr_t byte_size = GetByteSize();
186       if (byte_size > 0) {
187         s->PutCString(", range = ");
188         m_addr_range.Dump(s, target, Address::DumpStyleLoadAddress,
189                           Address::DumpStyleFileAddress);
190       } else {
191         s->PutCString(", address = ");
192         m_addr_range.GetBaseAddress().Dump(s, target,
193                                            Address::DumpStyleLoadAddress,
194                                            Address::DumpStyleFileAddress);
195       }
196     } else
197       s->Printf(", value = 0x%16.16" PRIx64,
198                 m_addr_range.GetBaseAddress().GetOffset());
199   } else {
200     if (m_size_is_sibling)
201       s->Printf(", sibling = %5" PRIu64,
202                 m_addr_range.GetBaseAddress().GetOffset());
203     else
204       s->Printf(", value = 0x%16.16" PRIx64,
205                 m_addr_range.GetBaseAddress().GetOffset());
206   }
207   ConstString demangled = m_mangled.GetDemangledName(GetLanguage());
208   if (demangled)
209     s->Printf(", name=\"%s\"", demangled.AsCString());
210   if (m_mangled.GetMangledName())
211     s->Printf(", mangled=\"%s\"", m_mangled.GetMangledName().AsCString());
212 }
213
214 void Symbol::Dump(Stream *s, Target *target, uint32_t index) const {
215   s->Printf("[%5u] %6u %c%c%c %-15s ", index, GetID(), m_is_debug ? 'D' : ' ',
216             m_is_synthetic ? 'S' : ' ', m_is_external ? 'X' : ' ',
217             GetTypeAsString());
218
219   // Make sure the size of the symbol is up to date before dumping
220   GetByteSize();
221
222   ConstString name = m_mangled.GetName(GetLanguage());
223   if (ValueIsAddress()) {
224     if (!m_addr_range.GetBaseAddress().Dump(s, nullptr,
225                                             Address::DumpStyleFileAddress))
226       s->Printf("%*s", 18, "");
227
228     s->PutChar(' ');
229
230     if (!m_addr_range.GetBaseAddress().Dump(s, target,
231                                             Address::DumpStyleLoadAddress))
232       s->Printf("%*s", 18, "");
233
234     const char *format = m_size_is_sibling ? " Sibling -> [%5llu] 0x%8.8x %s\n"
235                                            : " 0x%16.16" PRIx64 " 0x%8.8x %s\n";
236     s->Printf(format, GetByteSize(), m_flags, name.AsCString(""));
237   } else if (m_type == eSymbolTypeReExported) {
238     s->Printf(
239         "                                                         0x%8.8x %s",
240         m_flags, name.AsCString(""));
241
242     ConstString reexport_name = GetReExportedSymbolName();
243     intptr_t shlib = m_addr_range.GetByteSize();
244     if (shlib)
245       s->Printf(" -> %s`%s\n", (const char *)shlib, reexport_name.GetCString());
246     else
247       s->Printf(" -> %s\n", reexport_name.GetCString());
248   } else {
249     const char *format =
250         m_size_is_sibling
251             ? "0x%16.16" PRIx64
252               "                    Sibling -> [%5llu] 0x%8.8x %s\n"
253             : "0x%16.16" PRIx64 "                    0x%16.16" PRIx64
254               " 0x%8.8x %s\n";
255     s->Printf(format, m_addr_range.GetBaseAddress().GetOffset(), GetByteSize(),
256               m_flags, name.AsCString(""));
257   }
258 }
259
260 uint32_t Symbol::GetPrologueByteSize() {
261   if (m_type == eSymbolTypeCode || m_type == eSymbolTypeResolver) {
262     if (!m_type_data_resolved) {
263       m_type_data_resolved = true;
264
265       const Address &base_address = m_addr_range.GetBaseAddress();
266       Function *function = base_address.CalculateSymbolContextFunction();
267       if (function) {
268         // Functions have line entries which can also potentially have end of
269         // prologue information. So if this symbol points to a function, use
270         // the prologue information from there.
271         m_type_data = function->GetPrologueByteSize();
272       } else {
273         ModuleSP module_sp(base_address.GetModule());
274         SymbolContext sc;
275         if (module_sp) {
276           uint32_t resolved_flags = module_sp->ResolveSymbolContextForAddress(
277               base_address, eSymbolContextLineEntry, sc);
278           if (resolved_flags & eSymbolContextLineEntry) {
279             // Default to the end of the first line entry.
280             m_type_data = sc.line_entry.range.GetByteSize();
281
282             // Set address for next line.
283             Address addr(base_address);
284             addr.Slide(m_type_data);
285
286             // Check the first few instructions and look for one that has a
287             // line number that is different than the first entry. This is also
288             // done in Function::GetPrologueByteSize().
289             uint16_t total_offset = m_type_data;
290             for (int idx = 0; idx < 6; ++idx) {
291               SymbolContext sc_temp;
292               resolved_flags = module_sp->ResolveSymbolContextForAddress(
293                   addr, eSymbolContextLineEntry, sc_temp);
294               // Make sure we got line number information...
295               if (!(resolved_flags & eSymbolContextLineEntry))
296                 break;
297
298               // If this line number is different than our first one, use it
299               // and we're done.
300               if (sc_temp.line_entry.line != sc.line_entry.line) {
301                 m_type_data = total_offset;
302                 break;
303               }
304
305               // Slide addr up to the next line address.
306               addr.Slide(sc_temp.line_entry.range.GetByteSize());
307               total_offset += sc_temp.line_entry.range.GetByteSize();
308               // If we've gone too far, bail out.
309               if (total_offset >= m_addr_range.GetByteSize())
310                 break;
311             }
312
313             // Sanity check - this may be a function in the middle of code that
314             // has debug information, but not for this symbol.  So the line
315             // entries surrounding us won't lie inside our function. In that
316             // case, the line entry will be bigger than we are, so we do that
317             // quick check and if that is true, we just return 0.
318             if (m_type_data >= m_addr_range.GetByteSize())
319               m_type_data = 0;
320           } else {
321             // TODO: expose something in Process to figure out the
322             // size of a function prologue.
323             m_type_data = 0;
324           }
325         }
326       }
327     }
328     return m_type_data;
329   }
330   return 0;
331 }
332
333 bool Symbol::Compare(ConstString name, SymbolType type) const {
334   if (type == eSymbolTypeAny || m_type == type)
335     return m_mangled.GetMangledName() == name ||
336            m_mangled.GetDemangledName(GetLanguage()) == name;
337   return false;
338 }
339
340 #define ENUM_TO_CSTRING(x)                                                     \
341   case eSymbolType##x:                                                         \
342     return #x;
343
344 const char *Symbol::GetTypeAsString() const {
345   switch (m_type) {
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);
375   default:
376     break;
377   }
378   return "<unknown SymbolType>";
379 }
380
381 void Symbol::CalculateSymbolContext(SymbolContext *sc) {
382   // Symbols can reconstruct the symbol and the module in the symbol context
383   sc->symbol = this;
384   if (ValueIsAddress())
385     sc->module_sp = GetAddressRef().GetModule();
386   else
387     sc->module_sp.reset();
388 }
389
390 ModuleSP Symbol::CalculateSymbolContextModule() {
391   if (ValueIsAddress())
392     return GetAddressRef().GetModule();
393   return ModuleSP();
394 }
395
396 Symbol *Symbol::CalculateSymbolContextSymbol() { return this; }
397
398 void Symbol::DumpSymbolContext(Stream *s) {
399   bool dumped_module = false;
400   if (ValueIsAddress()) {
401     ModuleSP module_sp(GetAddressRef().GetModule());
402     if (module_sp) {
403       dumped_module = true;
404       module_sp->DumpSymbolContext(s);
405     }
406   }
407   if (dumped_module)
408     s->PutCString(", ");
409
410   s->Printf("Symbol{0x%8.8x}", GetID());
411 }
412
413 lldb::addr_t Symbol::GetByteSize() const { return m_addr_range.GetByteSize(); }
414
415 Symbol *Symbol::ResolveReExportedSymbolInModuleSpec(
416     Target &target, ConstString &reexport_name, ModuleSpec &module_spec,
417     ModuleList &seen_modules) const {
418   ModuleSP module_sp;
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);
422     if (!module_sp) {
423       // Next try and find the module by basename in case environment variables
424       // or other runtime trickery causes shared libraries to be loaded from
425       // alternate paths
426       module_spec.GetFileSpec().GetDirectory().Clear();
427       module_sp = target.GetImages().FindFirstModule(module_spec);
428     }
429   }
430
431   if (module_sp) {
432     // There should not be cycles in the reexport list, but we don't want to
433     // crash if there are so make sure we haven't seen this before:
434     if (!seen_modules.AppendIfNeeded(module_sp))
435       return nullptr;
436
437     lldb_private::SymbolContextList sc_list;
438     module_sp->FindSymbolsWithNameAndType(reexport_name, eSymbolTypeAny,
439                                           sc_list);
440     const size_t num_scs = sc_list.GetSize();
441     if (num_scs > 0) {
442       for (size_t i = 0; i < num_scs; ++i) {
443         lldb_private::SymbolContext sc;
444         if (sc_list.GetContextAtIndex(i, sc)) {
445           if (sc.symbol->IsExternal())
446             return sc.symbol;
447         }
448       }
449     }
450     // If we didn't find the symbol in this module, it may be because this
451     // module re-exports some whole other library.  We have to search those as
452     // well:
453     seen_modules.Append(module_sp);
454
455     FileSpecList reexported_libraries =
456         module_sp->GetObjectFile()->GetReExportedLibraries();
457     size_t num_reexported_libraries = reexported_libraries.GetSize();
458     for (size_t idx = 0; idx < num_reexported_libraries; idx++) {
459       ModuleSpec reexported_module_spec;
460       reexported_module_spec.GetFileSpec() =
461           reexported_libraries.GetFileSpecAtIndex(idx);
462       Symbol *result_symbol = ResolveReExportedSymbolInModuleSpec(
463           target, reexport_name, reexported_module_spec, seen_modules);
464       if (result_symbol)
465         return result_symbol;
466     }
467   }
468   return nullptr;
469 }
470
471 Symbol *Symbol::ResolveReExportedSymbol(Target &target) const {
472   ConstString reexport_name(GetReExportedSymbolName());
473   if (reexport_name) {
474     ModuleSpec module_spec;
475     ModuleList seen_modules;
476     module_spec.GetFileSpec() = GetReExportedSymbolSharedLibrary();
477     if (module_spec.GetFileSpec()) {
478       return ResolveReExportedSymbolInModuleSpec(target, reexport_name,
479                                                  module_spec, seen_modules);
480     }
481   }
482   return nullptr;
483 }
484
485 lldb::addr_t Symbol::GetFileAddress() const {
486   if (ValueIsAddress())
487     return GetAddressRef().GetFileAddress();
488   else
489     return LLDB_INVALID_ADDRESS;
490 }
491
492 lldb::addr_t Symbol::GetLoadAddress(Target *target) const {
493   if (ValueIsAddress())
494     return GetAddressRef().GetLoadAddress(target);
495   else
496     return LLDB_INVALID_ADDRESS;
497 }
498
499 ConstString Symbol::GetName() const { return m_mangled.GetName(GetLanguage()); }
500
501 ConstString Symbol::GetNameNoArguments() const {
502   return m_mangled.GetName(GetLanguage(),
503                            Mangled::ePreferDemangledWithoutArguments);
504 }
505
506 lldb::addr_t Symbol::ResolveCallableAddress(Target &target) const {
507   if (GetType() == lldb::eSymbolTypeUndefined)
508     return LLDB_INVALID_ADDRESS;
509
510   Address func_so_addr;
511
512   bool is_indirect = IsIndirect();
513   if (GetType() == eSymbolTypeReExported) {
514     Symbol *reexported_symbol = ResolveReExportedSymbol(target);
515     if (reexported_symbol) {
516       func_so_addr = reexported_symbol->GetAddress();
517       is_indirect = reexported_symbol->IsIndirect();
518     }
519   } else {
520     func_so_addr = GetAddress();
521     is_indirect = IsIndirect();
522   }
523
524   if (func_so_addr.IsValid()) {
525     if (!target.GetProcessSP() && is_indirect) {
526       // can't resolve indirect symbols without calling a function...
527       return LLDB_INVALID_ADDRESS;
528     }
529
530     lldb::addr_t load_addr =
531         func_so_addr.GetCallableLoadAddress(&target, is_indirect);
532
533     if (load_addr != LLDB_INVALID_ADDRESS) {
534       return load_addr;
535     }
536   }
537
538   return LLDB_INVALID_ADDRESS;
539 }
540
541 lldb::DisassemblerSP Symbol::GetInstructions(const ExecutionContext &exe_ctx,
542                                              const char *flavor,
543                                              bool prefer_file_cache) {
544   ModuleSP module_sp(m_addr_range.GetBaseAddress().GetModule());
545   if (module_sp) {
546     const bool prefer_file_cache = false;
547     return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr,
548                                           flavor, exe_ctx, m_addr_range,
549                                           prefer_file_cache);
550   }
551   return lldb::DisassemblerSP();
552 }
553
554 bool Symbol::GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor,
555                             bool prefer_file_cache, Stream &strm) {
556   lldb::DisassemblerSP disassembler_sp =
557       GetInstructions(exe_ctx, flavor, prefer_file_cache);
558   if (disassembler_sp) {
559     const bool show_address = true;
560     const bool show_bytes = false;
561     disassembler_sp->GetInstructionList().Dump(&strm, show_address, show_bytes,
562                                                &exe_ctx);
563     return true;
564   }
565   return false;
566 }
567
568 bool Symbol::ContainsFileAddress(lldb::addr_t file_addr) const {
569   return m_addr_range.ContainsFileAddress(file_addr);
570 }