1 //===-- 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"
13 #include "lldb/Core/ModuleSpec.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Core/Stream.h"
16 #include "lldb/Symbol/Function.h"
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Symbol/SymbolVendor.h"
19 #include "lldb/Symbol/Symtab.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 Symbol::Symbol()
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() {}
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), m_type(type),
46       m_mangled(ConstString(name), name_is_mangled),
47       m_addr_range(section_sp, offset, size), m_flags(flags) {}
48 
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,
53                uint32_t flags)
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) {}
62 
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) {}
74 
75 const Symbol &Symbol::operator=(const Symbol &rhs) {
76   if (this != &rhs) {
77     SymbolContextScope::operator=(rhs);
78     m_uid = rhs.m_uid;
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;
89     m_type = rhs.m_type;
90     m_mangled = rhs.m_mangled;
91     m_addr_range = rhs.m_addr_range;
92     m_flags = rhs.m_flags;
93   }
94   return *this;
95 }
96 
97 void Symbol::Clear() {
98   m_uid = UINT32_MAX;
99   m_mangled.Clear();
100   m_type_data = 0;
101   m_type_data_resolved = false;
102   m_is_synthetic = false;
103   m_is_debug = 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;
111   m_flags = 0;
112   m_addr_range.Clear();
113 }
114 
115 bool Symbol::ValueIsAddress() const {
116   return m_addr_range.GetBaseAddress().GetSection().get() != nullptr;
117 }
118 
119 ConstString Symbol::GetDisplayName() const {
120   if (!m_mangled)
121     return ConstString();
122   return m_mangled.GetDisplayDemangledName(GetLanguage());
123 }
124 
125 ConstString Symbol::GetReExportedSymbolName() const {
126   if (m_type == eSymbolTypeReExported) {
127     // For eSymbolTypeReExported, the "const char *" from a ConstString
128     // is used as the offset in the address range base address. We can
129     // then make this back into a string that is the re-exported name.
130     intptr_t str_ptr = m_addr_range.GetBaseAddress().GetOffset();
131     if (str_ptr != 0)
132       return ConstString((const char *)str_ptr);
133     else
134       return GetName();
135   }
136   return ConstString();
137 }
138 
139 FileSpec Symbol::GetReExportedSymbolSharedLibrary() const {
140   if (m_type == eSymbolTypeReExported) {
141     // For eSymbolTypeReExported, the "const char *" from a ConstString
142     // is used as the offset in the address range base address. We can
143     // then make this back into a string that is the re-exported name.
144     intptr_t str_ptr = m_addr_range.GetByteSize();
145     if (str_ptr != 0)
146       return FileSpec((const char *)str_ptr, false);
147   }
148   return FileSpec();
149 }
150 
151 void Symbol::SetReExportedSymbolName(const ConstString &name) {
152   SetType(eSymbolTypeReExported);
153   // For eSymbolTypeReExported, the "const char *" from a ConstString
154   // is used as the offset in the address range base address.
155   m_addr_range.GetBaseAddress().SetOffset((uintptr_t)name.GetCString());
156 }
157 
158 bool Symbol::SetReExportedSymbolSharedLibrary(const FileSpec &fspec) {
159   if (m_type == eSymbolTypeReExported) {
160     // For eSymbolTypeReExported, the "const char *" from a ConstString
161     // is used as the offset in the address range base address.
162     m_addr_range.SetByteSize(
163         (uintptr_t)ConstString(fspec.GetPath().c_str()).GetCString());
164     return true;
165   }
166   return false;
167 }
168 
169 uint32_t Symbol::GetSiblingIndex() const {
170   return m_size_is_sibling ? m_addr_range.GetByteSize() : UINT32_MAX;
171 }
172 
173 bool Symbol::IsTrampoline() const { return m_type == eSymbolTypeTrampoline; }
174 
175 bool Symbol::IsIndirect() const { return m_type == eSymbolTypeResolver; }
176 
177 void Symbol::GetDescription(Stream *s, lldb::DescriptionLevel level,
178                             Target *target) const {
179   s->Printf("id = {0x%8.8x}", m_uid);
180 
181   if (m_addr_range.GetBaseAddress().GetSection()) {
182     if (ValueIsAddress()) {
183       const lldb::addr_t byte_size = GetByteSize();
184       if (byte_size > 0) {
185         s->PutCString(", range = ");
186         m_addr_range.Dump(s, target, Address::DumpStyleLoadAddress,
187                           Address::DumpStyleFileAddress);
188       } else {
189         s->PutCString(", address = ");
190         m_addr_range.GetBaseAddress().Dump(s, target,
191                                            Address::DumpStyleLoadAddress,
192                                            Address::DumpStyleFileAddress);
193       }
194     } else
195       s->Printf(", value = 0x%16.16" PRIx64,
196                 m_addr_range.GetBaseAddress().GetOffset());
197   } else {
198     if (m_size_is_sibling)
199       s->Printf(", sibling = %5" PRIu64,
200                 m_addr_range.GetBaseAddress().GetOffset());
201     else
202       s->Printf(", value = 0x%16.16" PRIx64,
203                 m_addr_range.GetBaseAddress().GetOffset());
204   }
205   ConstString demangled = m_mangled.GetDemangledName(GetLanguage());
206   if (demangled)
207     s->Printf(", name=\"%s\"", demangled.AsCString());
208   if (m_mangled.GetMangledName())
209     s->Printf(", mangled=\"%s\"", m_mangled.GetMangledName().AsCString());
210 }
211 
212 void Symbol::Dump(Stream *s, Target *target, uint32_t index) const {
213   s->Printf("[%5u] %6u %c%c%c %-15s ", index, GetID(), m_is_debug ? 'D' : ' ',
214             m_is_synthetic ? 'S' : ' ', m_is_external ? 'X' : ' ',
215             GetTypeAsString());
216 
217   // Make sure the size of the symbol is up to date before dumping
218   GetByteSize();
219 
220   ConstString name = m_mangled.GetName(GetLanguage());
221   if (ValueIsAddress()) {
222     if (!m_addr_range.GetBaseAddress().Dump(s, nullptr,
223                                             Address::DumpStyleFileAddress))
224       s->Printf("%*s", 18, "");
225 
226     s->PutChar(' ');
227 
228     if (!m_addr_range.GetBaseAddress().Dump(s, target,
229                                             Address::DumpStyleLoadAddress))
230       s->Printf("%*s", 18, "");
231 
232     const char *format = m_size_is_sibling ? " Sibling -> [%5llu] 0x%8.8x %s\n"
233                                            : " 0x%16.16" PRIx64 " 0x%8.8x %s\n";
234     s->Printf(format, GetByteSize(), m_flags, name.AsCString(""));
235   } else if (m_type == eSymbolTypeReExported) {
236     s->Printf(
237         "                                                         0x%8.8x %s",
238         m_flags, name.AsCString(""));
239 
240     ConstString reexport_name = GetReExportedSymbolName();
241     intptr_t shlib = m_addr_range.GetByteSize();
242     if (shlib)
243       s->Printf(" -> %s`%s\n", (const char *)shlib, reexport_name.GetCString());
244     else
245       s->Printf(" -> %s\n", reexport_name.GetCString());
246   } else {
247     const char *format =
248         m_size_is_sibling
249             ? "0x%16.16" PRIx64
250               "                    Sibling -> [%5llu] 0x%8.8x %s\n"
251             : "0x%16.16" PRIx64 "                    0x%16.16" PRIx64
252               " 0x%8.8x %s\n";
253     s->Printf(format, m_addr_range.GetBaseAddress().GetOffset(), GetByteSize(),
254               m_flags, name.AsCString(""));
255   }
256 }
257 
258 uint32_t Symbol::GetPrologueByteSize() {
259   if (m_type == eSymbolTypeCode || m_type == eSymbolTypeResolver) {
260     if (!m_type_data_resolved) {
261       m_type_data_resolved = true;
262 
263       const Address &base_address = m_addr_range.GetBaseAddress();
264       Function *function = base_address.CalculateSymbolContextFunction();
265       if (function) {
266         // Functions have line entries which can also potentially have end of
267         // prologue information.
268         // So if this symbol points to a function, use the prologue information
269         // from there.
270         m_type_data = function->GetPrologueByteSize();
271       } else {
272         ModuleSP module_sp(base_address.GetModule());
273         SymbolContext sc;
274         if (module_sp) {
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();
280 
281             // Set address for next line.
282             Address addr(base_address);
283             addr.Slide(m_type_data);
284 
285             // Check the first few instructions and look for one that has a line
286             // number that is
287             // different than the first entry. This is also done in
288             // 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 and
299               // 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
315             // not for this symbol.  So the line entries surrounding us won't
316             // lie inside our function.
317             // In that case, the line entry will be bigger than we are, so we do
318             // that quick check and
319             // if that is true, we just return 0.
320             if (m_type_data >= m_addr_range.GetByteSize())
321               m_type_data = 0;
322           } else {
323             // TODO: expose something in Process to figure out the
324             // size of a function prologue.
325             m_type_data = 0;
326           }
327         }
328       }
329     }
330     return m_type_data;
331   }
332   return 0;
333 }
334 
335 bool Symbol::Compare(const ConstString &name, SymbolType type) const {
336   if (type == eSymbolTypeAny || m_type == type)
337     return m_mangled.GetMangledName() == name ||
338            m_mangled.GetDemangledName(GetLanguage()) == name;
339   return false;
340 }
341 
342 #define ENUM_TO_CSTRING(x)                                                     \
343   case eSymbolType##x:                                                         \
344     return #x;
345 
346 const char *Symbol::GetTypeAsString() const {
347   switch (m_type) {
348     ENUM_TO_CSTRING(Invalid);
349     ENUM_TO_CSTRING(Absolute);
350     ENUM_TO_CSTRING(Code);
351     ENUM_TO_CSTRING(Resolver);
352     ENUM_TO_CSTRING(Data);
353     ENUM_TO_CSTRING(Trampoline);
354     ENUM_TO_CSTRING(Runtime);
355     ENUM_TO_CSTRING(Exception);
356     ENUM_TO_CSTRING(SourceFile);
357     ENUM_TO_CSTRING(HeaderFile);
358     ENUM_TO_CSTRING(ObjectFile);
359     ENUM_TO_CSTRING(CommonBlock);
360     ENUM_TO_CSTRING(Block);
361     ENUM_TO_CSTRING(Local);
362     ENUM_TO_CSTRING(Param);
363     ENUM_TO_CSTRING(Variable);
364     ENUM_TO_CSTRING(VariableType);
365     ENUM_TO_CSTRING(LineEntry);
366     ENUM_TO_CSTRING(LineHeader);
367     ENUM_TO_CSTRING(ScopeBegin);
368     ENUM_TO_CSTRING(ScopeEnd);
369     ENUM_TO_CSTRING(Additional);
370     ENUM_TO_CSTRING(Compiler);
371     ENUM_TO_CSTRING(Instrumentation);
372     ENUM_TO_CSTRING(Undefined);
373     ENUM_TO_CSTRING(ObjCClass);
374     ENUM_TO_CSTRING(ObjCMetaClass);
375     ENUM_TO_CSTRING(ObjCIVar);
376     ENUM_TO_CSTRING(ReExported);
377   default:
378     break;
379   }
380   return "<unknown SymbolType>";
381 }
382 
383 void Symbol::CalculateSymbolContext(SymbolContext *sc) {
384   // Symbols can reconstruct the symbol and the module in the symbol context
385   sc->symbol = this;
386   if (ValueIsAddress())
387     sc->module_sp = GetAddressRef().GetModule();
388   else
389     sc->module_sp.reset();
390 }
391 
392 ModuleSP Symbol::CalculateSymbolContextModule() {
393   if (ValueIsAddress())
394     return GetAddressRef().GetModule();
395   return ModuleSP();
396 }
397 
398 Symbol *Symbol::CalculateSymbolContextSymbol() { return this; }
399 
400 void Symbol::DumpSymbolContext(Stream *s) {
401   bool dumped_module = false;
402   if (ValueIsAddress()) {
403     ModuleSP module_sp(GetAddressRef().GetModule());
404     if (module_sp) {
405       dumped_module = true;
406       module_sp->DumpSymbolContext(s);
407     }
408   }
409   if (dumped_module)
410     s->PutCString(", ");
411 
412   s->Printf("Symbol{0x%8.8x}", GetID());
413 }
414 
415 lldb::addr_t Symbol::GetByteSize() const { return m_addr_range.GetByteSize(); }
416 
417 Symbol *Symbol::ResolveReExportedSymbolInModuleSpec(
418     Target &target, ConstString &reexport_name, ModuleSpec &module_spec,
419     ModuleList &seen_modules) const {
420   ModuleSP module_sp;
421   if (module_spec.GetFileSpec()) {
422     // Try searching for the module file spec first using the full path
423     module_sp = target.GetImages().FindFirstModule(module_spec);
424     if (!module_sp) {
425       // Next try and find the module by basename in case environment
426       // variables or other runtime trickery causes shared libraries
427       // to be loaded from alternate paths
428       module_spec.GetFileSpec().GetDirectory().Clear();
429       module_sp = target.GetImages().FindFirstModule(module_spec);
430     }
431   }
432 
433   if (module_sp) {
434     // There should not be cycles in the reexport list, but we don't want to
435     // crash if there are so make sure
436     // we haven't seen this before:
437     if (!seen_modules.AppendIfNeeded(module_sp))
438       return nullptr;
439 
440     lldb_private::SymbolContextList sc_list;
441     module_sp->FindSymbolsWithNameAndType(reexport_name, eSymbolTypeAny,
442                                           sc_list);
443     const size_t num_scs = sc_list.GetSize();
444     if (num_scs > 0) {
445       for (size_t i = 0; i < num_scs; ++i) {
446         lldb_private::SymbolContext sc;
447         if (sc_list.GetContextAtIndex(i, sc)) {
448           if (sc.symbol->IsExternal())
449             return sc.symbol;
450         }
451       }
452     }
453     // If we didn't find the symbol in this module, it may be because this
454     // module re-exports some
455     // whole other library.  We have to search those as well:
456     seen_modules.Append(module_sp);
457 
458     FileSpecList reexported_libraries =
459         module_sp->GetObjectFile()->GetReExportedLibraries();
460     size_t num_reexported_libraries = reexported_libraries.GetSize();
461     for (size_t idx = 0; idx < num_reexported_libraries; idx++) {
462       ModuleSpec reexported_module_spec;
463       reexported_module_spec.GetFileSpec() =
464           reexported_libraries.GetFileSpecAtIndex(idx);
465       Symbol *result_symbol = ResolveReExportedSymbolInModuleSpec(
466           target, reexport_name, reexported_module_spec, seen_modules);
467       if (result_symbol)
468         return result_symbol;
469     }
470   }
471   return nullptr;
472 }
473 
474 Symbol *Symbol::ResolveReExportedSymbol(Target &target) const {
475   ConstString reexport_name(GetReExportedSymbolName());
476   if (reexport_name) {
477     ModuleSpec module_spec;
478     ModuleList seen_modules;
479     module_spec.GetFileSpec() = GetReExportedSymbolSharedLibrary();
480     if (module_spec.GetFileSpec()) {
481       return ResolveReExportedSymbolInModuleSpec(target, reexport_name,
482                                                  module_spec, seen_modules);
483     }
484   }
485   return nullptr;
486 }
487 
488 lldb::addr_t Symbol::GetFileAddress() const {
489   if (ValueIsAddress())
490     return GetAddressRef().GetFileAddress();
491   else
492     return LLDB_INVALID_ADDRESS;
493 }
494 
495 lldb::addr_t Symbol::GetLoadAddress(Target *target) const {
496   if (ValueIsAddress())
497     return GetAddressRef().GetLoadAddress(target);
498   else
499     return LLDB_INVALID_ADDRESS;
500 }
501 
502 ConstString Symbol::GetName() const { return m_mangled.GetName(GetLanguage()); }
503 
504 ConstString Symbol::GetNameNoArguments() const {
505   return m_mangled.GetName(GetLanguage(),
506                            Mangled::ePreferDemangledWithoutArguments);
507 }
508 
509 lldb::addr_t Symbol::ResolveCallableAddress(Target &target) const {
510   if (GetType() == lldb::eSymbolTypeUndefined)
511     return LLDB_INVALID_ADDRESS;
512 
513   Address func_so_addr;
514 
515   bool is_indirect = IsIndirect();
516   if (GetType() == eSymbolTypeReExported) {
517     Symbol *reexported_symbol = ResolveReExportedSymbol(target);
518     if (reexported_symbol) {
519       func_so_addr = reexported_symbol->GetAddress();
520       is_indirect = reexported_symbol->IsIndirect();
521     }
522   } else {
523     func_so_addr = GetAddress();
524     is_indirect = IsIndirect();
525   }
526 
527   if (func_so_addr.IsValid()) {
528     if (!target.GetProcessSP() && is_indirect) {
529       // can't resolve indirect symbols without calling a function...
530       return LLDB_INVALID_ADDRESS;
531     }
532 
533     lldb::addr_t load_addr =
534         func_so_addr.GetCallableLoadAddress(&target, is_indirect);
535 
536     if (load_addr != LLDB_INVALID_ADDRESS) {
537       return load_addr;
538     }
539   }
540 
541   return LLDB_INVALID_ADDRESS;
542 }
543 
544 lldb::DisassemblerSP Symbol::GetInstructions(const ExecutionContext &exe_ctx,
545                                              const char *flavor,
546                                              bool prefer_file_cache) {
547   ModuleSP module_sp(m_addr_range.GetBaseAddress().GetModule());
548   if (module_sp) {
549     const bool prefer_file_cache = false;
550     return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr,
551                                           flavor, exe_ctx, m_addr_range,
552                                           prefer_file_cache);
553   }
554   return lldb::DisassemblerSP();
555 }
556 
557 bool Symbol::GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor,
558                             bool prefer_file_cache, Stream &strm) {
559   lldb::DisassemblerSP disassembler_sp =
560       GetInstructions(exe_ctx, flavor, prefer_file_cache);
561   if (disassembler_sp) {
562     const bool show_address = true;
563     const bool show_bytes = false;
564     disassembler_sp->GetInstructionList().Dump(&strm, show_address, show_bytes,
565                                                &exe_ctx);
566     return true;
567   }
568   return false;
569 }
570 
571 bool Symbol::ContainsFileAddress(lldb::addr_t file_addr) const {
572   return m_addr_range.ContainsFileAddress(file_addr);
573 }
574