1 //===-- Symbol.cpp --------------------------------------------------------===//
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/DataEncoder.h"
21 #include "lldb/Utility/Stream.h"
22
23 using namespace lldb;
24 using namespace lldb_private;
25
Symbol()26 Symbol::Symbol()
27 : SymbolContextScope(), m_type_data_resolved(false), m_is_synthetic(false),
28 m_is_debug(false), 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(),
32 m_addr_range() {}
33
Symbol(uint32_t symID,llvm::StringRef name,SymbolType type,bool external,bool is_debug,bool is_trampoline,bool is_artificial,const lldb::SectionSP & section_sp,addr_t offset,addr_t size,bool size_is_valid,bool contains_linker_annotations,uint32_t flags)34 Symbol::Symbol(uint32_t symID, llvm::StringRef name, SymbolType type,
35 bool external, bool is_debug, bool is_trampoline,
36 bool is_artificial, const lldb::SectionSP §ion_sp,
37 addr_t offset, addr_t size, bool size_is_valid,
38 bool contains_linker_annotations, uint32_t flags)
39 : SymbolContextScope(), m_uid(symID), m_type_data_resolved(false),
40 m_is_synthetic(is_artificial), m_is_debug(is_debug),
41 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), m_mangled(name),
46 m_addr_range(section_sp, offset, size), m_flags(flags) {}
47
Symbol(uint32_t symID,const Mangled & mangled,SymbolType type,bool external,bool is_debug,bool is_trampoline,bool is_artificial,const AddressRange & range,bool size_is_valid,bool contains_linker_annotations,uint32_t flags)48 Symbol::Symbol(uint32_t symID, const Mangled &mangled, SymbolType type,
49 bool external, bool is_debug, bool is_trampoline,
50 bool is_artificial, const AddressRange &range,
51 bool size_is_valid, bool contains_linker_annotations,
52 uint32_t flags)
53 : SymbolContextScope(), m_uid(symID), m_type_data_resolved(false),
54 m_is_synthetic(is_artificial), m_is_debug(is_debug),
55 m_is_external(external), m_size_is_sibling(false),
56 m_size_is_synthesized(false),
57 m_size_is_valid(size_is_valid || range.GetByteSize() > 0),
58 m_demangled_is_synthesized(false),
59 m_contains_linker_annotations(contains_linker_annotations),
60 m_is_weak(false), m_type(type), m_mangled(mangled), m_addr_range(range),
61 m_flags(flags) {}
62
Symbol(const Symbol & rhs)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_is_weak(rhs.m_is_weak), m_type(rhs.m_type), m_mangled(rhs.m_mangled),
73 m_addr_range(rhs.m_addr_range), m_flags(rhs.m_flags) {}
74
operator =(const Symbol & rhs)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_is_weak = rhs.m_is_weak;
90 m_type = rhs.m_type;
91 m_mangled = rhs.m_mangled;
92 m_addr_range = rhs.m_addr_range;
93 m_flags = rhs.m_flags;
94 }
95 return *this;
96 }
97
Clear()98 void Symbol::Clear() {
99 m_uid = UINT32_MAX;
100 m_mangled.Clear();
101 m_type_data = 0;
102 m_type_data_resolved = false;
103 m_is_synthetic = false;
104 m_is_debug = false;
105 m_is_external = false;
106 m_size_is_sibling = false;
107 m_size_is_synthesized = false;
108 m_size_is_valid = false;
109 m_demangled_is_synthesized = false;
110 m_contains_linker_annotations = false;
111 m_is_weak = false;
112 m_type = eSymbolTypeInvalid;
113 m_flags = 0;
114 m_addr_range.Clear();
115 }
116
ValueIsAddress() const117 bool Symbol::ValueIsAddress() const {
118 return (bool)m_addr_range.GetBaseAddress().GetSection();
119 }
120
GetDisplayName() const121 ConstString Symbol::GetDisplayName() const {
122 return GetMangled().GetDisplayDemangledName();
123 }
124
GetReExportedSymbolName() const125 ConstString Symbol::GetReExportedSymbolName() const {
126 if (m_type == eSymbolTypeReExported) {
127 // For eSymbolTypeReExported, the "const char *" from a ConstString is used
128 // as the offset in the address range base address. We can then make this
129 // 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
GetReExportedSymbolSharedLibrary() const139 FileSpec Symbol::GetReExportedSymbolSharedLibrary() const {
140 if (m_type == eSymbolTypeReExported) {
141 // For eSymbolTypeReExported, the "const char *" from a ConstString is used
142 // as the offset in the address range base address. We can then make this
143 // 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);
147 }
148 return FileSpec();
149 }
150
SetReExportedSymbolName(ConstString name)151 void Symbol::SetReExportedSymbolName(ConstString name) {
152 SetType(eSymbolTypeReExported);
153 // For eSymbolTypeReExported, the "const char *" from a ConstString is used
154 // as the offset in the address range base address.
155 m_addr_range.GetBaseAddress().SetOffset((uintptr_t)name.GetCString());
156 }
157
SetReExportedSymbolSharedLibrary(const FileSpec & fspec)158 bool Symbol::SetReExportedSymbolSharedLibrary(const FileSpec &fspec) {
159 if (m_type == eSymbolTypeReExported) {
160 // For eSymbolTypeReExported, the "const char *" from a ConstString is used
161 // 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
GetSiblingIndex() const169 uint32_t Symbol::GetSiblingIndex() const {
170 return m_size_is_sibling ? m_addr_range.GetByteSize() : UINT32_MAX;
171 }
172
IsTrampoline() const173 bool Symbol::IsTrampoline() const { return m_type == eSymbolTypeTrampoline; }
174
IsIndirect() const175 bool Symbol::IsIndirect() const { return m_type == eSymbolTypeResolver; }
176
GetDescription(Stream * s,lldb::DescriptionLevel level,Target * target) const177 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 = GetMangled().GetDemangledName();
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
Dump(Stream * s,Target * target,uint32_t index,Mangled::NamePreference name_preference) const212 void Symbol::Dump(Stream *s, Target *target, uint32_t index,
213 Mangled::NamePreference name_preference) 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' : ' ',
216 GetTypeAsString());
217
218 // Make sure the size of the symbol is up to date before dumping
219 GetByteSize();
220
221 ConstString name = GetMangled().GetName(name_preference);
222 if (ValueIsAddress()) {
223 if (!m_addr_range.GetBaseAddress().Dump(s, nullptr,
224 Address::DumpStyleFileAddress))
225 s->Printf("%*s", 18, "");
226
227 s->PutChar(' ');
228
229 if (!m_addr_range.GetBaseAddress().Dump(s, target,
230 Address::DumpStyleLoadAddress))
231 s->Printf("%*s", 18, "");
232
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) {
237 s->Printf(
238 " 0x%8.8x %s",
239 m_flags, name.AsCString(""));
240
241 ConstString reexport_name = GetReExportedSymbolName();
242 intptr_t shlib = m_addr_range.GetByteSize();
243 if (shlib)
244 s->Printf(" -> %s`%s\n", (const char *)shlib, reexport_name.GetCString());
245 else
246 s->Printf(" -> %s\n", reexport_name.GetCString());
247 } else {
248 const char *format =
249 m_size_is_sibling
250 ? "0x%16.16" PRIx64
251 " Sibling -> [%5llu] 0x%8.8x %s\n"
252 : "0x%16.16" PRIx64 " 0x%16.16" PRIx64
253 " 0x%8.8x %s\n";
254 s->Printf(format, m_addr_range.GetBaseAddress().GetOffset(), GetByteSize(),
255 m_flags, name.AsCString(""));
256 }
257 }
258
GetPrologueByteSize()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;
263
264 const Address &base_address = m_addr_range.GetBaseAddress();
265 Function *function = base_address.CalculateSymbolContextFunction();
266 if (function) {
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();
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
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))
295 break;
296
297 // If this line number is different than our first one, use it
298 // and we're done.
299 if (sc_temp.line_entry.line != sc.line_entry.line) {
300 m_type_data = total_offset;
301 break;
302 }
303
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())
309 break;
310 }
311
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())
318 m_type_data = 0;
319 } else {
320 // TODO: expose something in Process to figure out the
321 // size of a function prologue.
322 m_type_data = 0;
323 }
324 }
325 }
326 }
327 return m_type_data;
328 }
329 return 0;
330 }
331
Compare(ConstString name,SymbolType type) const332 bool Symbol::Compare(ConstString name, SymbolType type) const {
333 if (type == eSymbolTypeAny || m_type == type) {
334 const Mangled &mangled = GetMangled();
335 return mangled.GetMangledName() == name ||
336 mangled.GetDemangledName() == name;
337 }
338 return false;
339 }
340
341 #define ENUM_TO_CSTRING(x) \
342 case eSymbolType##x: \
343 return #x;
344
GetTypeAsString() const345 const char *Symbol::GetTypeAsString() const {
346 switch (m_type) {
347 ENUM_TO_CSTRING(Invalid);
348 ENUM_TO_CSTRING(Absolute);
349 ENUM_TO_CSTRING(Code);
350 ENUM_TO_CSTRING(Resolver);
351 ENUM_TO_CSTRING(Data);
352 ENUM_TO_CSTRING(Trampoline);
353 ENUM_TO_CSTRING(Runtime);
354 ENUM_TO_CSTRING(Exception);
355 ENUM_TO_CSTRING(SourceFile);
356 ENUM_TO_CSTRING(HeaderFile);
357 ENUM_TO_CSTRING(ObjectFile);
358 ENUM_TO_CSTRING(CommonBlock);
359 ENUM_TO_CSTRING(Block);
360 ENUM_TO_CSTRING(Local);
361 ENUM_TO_CSTRING(Param);
362 ENUM_TO_CSTRING(Variable);
363 ENUM_TO_CSTRING(VariableType);
364 ENUM_TO_CSTRING(LineEntry);
365 ENUM_TO_CSTRING(LineHeader);
366 ENUM_TO_CSTRING(ScopeBegin);
367 ENUM_TO_CSTRING(ScopeEnd);
368 ENUM_TO_CSTRING(Additional);
369 ENUM_TO_CSTRING(Compiler);
370 ENUM_TO_CSTRING(Instrumentation);
371 ENUM_TO_CSTRING(Undefined);
372 ENUM_TO_CSTRING(ObjCClass);
373 ENUM_TO_CSTRING(ObjCMetaClass);
374 ENUM_TO_CSTRING(ObjCIVar);
375 ENUM_TO_CSTRING(ReExported);
376 default:
377 break;
378 }
379 return "<unknown SymbolType>";
380 }
381
CalculateSymbolContext(SymbolContext * sc)382 void Symbol::CalculateSymbolContext(SymbolContext *sc) {
383 // Symbols can reconstruct the symbol and the module in the symbol context
384 sc->symbol = this;
385 if (ValueIsAddress())
386 sc->module_sp = GetAddressRef().GetModule();
387 else
388 sc->module_sp.reset();
389 }
390
CalculateSymbolContextModule()391 ModuleSP Symbol::CalculateSymbolContextModule() {
392 if (ValueIsAddress())
393 return GetAddressRef().GetModule();
394 return ModuleSP();
395 }
396
CalculateSymbolContextSymbol()397 Symbol *Symbol::CalculateSymbolContextSymbol() { return this; }
398
DumpSymbolContext(Stream * s)399 void Symbol::DumpSymbolContext(Stream *s) {
400 bool dumped_module = false;
401 if (ValueIsAddress()) {
402 ModuleSP module_sp(GetAddressRef().GetModule());
403 if (module_sp) {
404 dumped_module = true;
405 module_sp->DumpSymbolContext(s);
406 }
407 }
408 if (dumped_module)
409 s->PutCString(", ");
410
411 s->Printf("Symbol{0x%8.8x}", GetID());
412 }
413
GetByteSize() const414 lldb::addr_t Symbol::GetByteSize() const { return m_addr_range.GetByteSize(); }
415
ResolveReExportedSymbolInModuleSpec(Target & target,ConstString & reexport_name,ModuleSpec & module_spec,ModuleList & seen_modules) const416 Symbol *Symbol::ResolveReExportedSymbolInModuleSpec(
417 Target &target, ConstString &reexport_name, ModuleSpec &module_spec,
418 ModuleList &seen_modules) const {
419 ModuleSP module_sp;
420 if (module_spec.GetFileSpec()) {
421 // Try searching for the module file spec first using the full path
422 module_sp = target.GetImages().FindFirstModule(module_spec);
423 if (!module_sp) {
424 // Next try and find the module by basename in case environment variables
425 // or other runtime trickery causes shared libraries to be loaded from
426 // alternate paths
427 module_spec.GetFileSpec().ClearDirectory();
428 module_sp = target.GetImages().FindFirstModule(module_spec);
429 }
430 }
431
432 if (module_sp) {
433 // There should not be cycles in the reexport list, but we don't want to
434 // crash if there are so make sure we haven't seen this before:
435 if (!seen_modules.AppendIfNeeded(module_sp))
436 return nullptr;
437
438 lldb_private::SymbolContextList sc_list;
439 module_sp->FindSymbolsWithNameAndType(reexport_name, eSymbolTypeAny,
440 sc_list);
441 const size_t num_scs = sc_list.GetSize();
442 if (num_scs > 0) {
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())
447 return sc.symbol;
448 }
449 }
450 }
451 // If we didn't find the symbol in this module, it may be because this
452 // module re-exports some whole other library. We have to search those as
453 // well:
454 seen_modules.Append(module_sp);
455
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);
465 if (result_symbol)
466 return result_symbol;
467 }
468 }
469 return nullptr;
470 }
471
ResolveReExportedSymbol(Target & target) const472 Symbol *Symbol::ResolveReExportedSymbol(Target &target) const {
473 ConstString reexport_name(GetReExportedSymbolName());
474 if (reexport_name) {
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);
481 }
482 }
483 return nullptr;
484 }
485
GetFileAddress() const486 lldb::addr_t Symbol::GetFileAddress() const {
487 if (ValueIsAddress())
488 return GetAddressRef().GetFileAddress();
489 else
490 return LLDB_INVALID_ADDRESS;
491 }
492
GetLoadAddress(Target * target) const493 lldb::addr_t Symbol::GetLoadAddress(Target *target) const {
494 if (ValueIsAddress())
495 return GetAddressRef().GetLoadAddress(target);
496 else
497 return LLDB_INVALID_ADDRESS;
498 }
499
GetName() const500 ConstString Symbol::GetName() const { return GetMangled().GetName(); }
501
GetNameNoArguments() const502 ConstString Symbol::GetNameNoArguments() const {
503 return GetMangled().GetName(Mangled::ePreferDemangledWithoutArguments);
504 }
505
ResolveCallableAddress(Target & target) const506 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
GetInstructions(const ExecutionContext & exe_ctx,const char * flavor,bool prefer_file_cache)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 && exe_ctx.HasTargetScope()) {
546 return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr,
547 flavor, exe_ctx.GetTargetRef(),
548 m_addr_range, !prefer_file_cache);
549 }
550 return lldb::DisassemblerSP();
551 }
552
GetDisassembly(const ExecutionContext & exe_ctx,const char * flavor,bool prefer_file_cache,Stream & strm)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 const bool show_control_flow_kind = false;
561 disassembler_sp->GetInstructionList().Dump(
562 &strm, show_address, show_bytes, show_control_flow_kind, &exe_ctx);
563 return true;
564 }
565 return false;
566 }
567
ContainsFileAddress(lldb::addr_t file_addr) const568 bool Symbol::ContainsFileAddress(lldb::addr_t file_addr) const {
569 return m_addr_range.ContainsFileAddress(file_addr);
570 }
571
IsSyntheticWithAutoGeneratedName() const572 bool Symbol::IsSyntheticWithAutoGeneratedName() const {
573 if (!IsSynthetic())
574 return false;
575 if (!m_mangled)
576 return true;
577 ConstString demangled = m_mangled.GetDemangledName();
578 return demangled.GetStringRef().startswith(GetSyntheticSymbolPrefix());
579 }
580
SynthesizeNameIfNeeded() const581 void Symbol::SynthesizeNameIfNeeded() const {
582 if (m_is_synthetic && !m_mangled) {
583 // Synthetic symbol names don't mean anything, but they do uniquely
584 // identify individual symbols so we give them a unique name. The name
585 // starts with the synthetic symbol prefix, followed by a unique number.
586 // Typically the UserID of a real symbol is the symbol table index of the
587 // symbol in the object file's symbol table(s), so it will be the same
588 // every time you read in the object file. We want the same persistence for
589 // synthetic symbols so that users can identify them across multiple debug
590 // sessions, to understand crashes in those symbols and to reliably set
591 // breakpoints on them.
592 llvm::SmallString<256> name;
593 llvm::raw_svector_ostream os(name);
594 os << GetSyntheticSymbolPrefix() << GetID();
595 m_mangled.SetDemangledName(ConstString(os.str()));
596 }
597 }
598
Decode(const DataExtractor & data,lldb::offset_t * offset_ptr,const SectionList * section_list,const StringTableReader & strtab)599 bool Symbol::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
600 const SectionList *section_list,
601 const StringTableReader &strtab) {
602 if (!data.ValidOffsetForDataOfSize(*offset_ptr, 8))
603 return false;
604 m_uid = data.GetU32(offset_ptr);
605 m_type_data = data.GetU16(offset_ptr);
606 const uint16_t bitfields = data.GetU16(offset_ptr);
607 m_type_data_resolved = (1u << 15 & bitfields) != 0;
608 m_is_synthetic = (1u << 14 & bitfields) != 0;
609 m_is_debug = (1u << 13 & bitfields) != 0;
610 m_is_external = (1u << 12 & bitfields) != 0;
611 m_size_is_sibling = (1u << 11 & bitfields) != 0;
612 m_size_is_synthesized = (1u << 10 & bitfields) != 0;
613 m_size_is_valid = (1u << 9 & bitfields) != 0;
614 m_demangled_is_synthesized = (1u << 8 & bitfields) != 0;
615 m_contains_linker_annotations = (1u << 7 & bitfields) != 0;
616 m_is_weak = (1u << 6 & bitfields) != 0;
617 m_type = bitfields & 0x003f;
618 if (!m_mangled.Decode(data, offset_ptr, strtab))
619 return false;
620 if (!data.ValidOffsetForDataOfSize(*offset_ptr, 20))
621 return false;
622 const bool is_addr = data.GetU8(offset_ptr) != 0;
623 const uint64_t value = data.GetU64(offset_ptr);
624 if (is_addr) {
625 m_addr_range.GetBaseAddress().ResolveAddressUsingFileSections(
626 value, section_list);
627 } else {
628 m_addr_range.GetBaseAddress().Clear();
629 m_addr_range.GetBaseAddress().SetOffset(value);
630 }
631 m_addr_range.SetByteSize(data.GetU64(offset_ptr));
632 m_flags = data.GetU32(offset_ptr);
633 return true;
634 }
635
636 /// The encoding format for the symbol is as follows:
637 ///
638 /// uint32_t m_uid;
639 /// uint16_t m_type_data;
640 /// uint16_t bitfield_data;
641 /// Mangled mangled;
642 /// uint8_t is_addr;
643 /// uint64_t file_addr_or_value;
644 /// uint64_t size;
645 /// uint32_t flags;
646 ///
647 /// The only tricky thing in this encoding is encoding all of the bits in the
648 /// bitfields. We use a trick to store all bitfields as a 16 bit value and we
649 /// do the same thing when decoding the symbol. There are test that ensure this
650 /// encoding works for each individual bit. Everything else is very easy to
651 /// store.
Encode(DataEncoder & file,ConstStringTable & strtab) const652 void Symbol::Encode(DataEncoder &file, ConstStringTable &strtab) const {
653 file.AppendU32(m_uid);
654 file.AppendU16(m_type_data);
655 uint16_t bitfields = m_type;
656 if (m_type_data_resolved)
657 bitfields |= 1u << 15;
658 if (m_is_synthetic)
659 bitfields |= 1u << 14;
660 if (m_is_debug)
661 bitfields |= 1u << 13;
662 if (m_is_external)
663 bitfields |= 1u << 12;
664 if (m_size_is_sibling)
665 bitfields |= 1u << 11;
666 if (m_size_is_synthesized)
667 bitfields |= 1u << 10;
668 if (m_size_is_valid)
669 bitfields |= 1u << 9;
670 if (m_demangled_is_synthesized)
671 bitfields |= 1u << 8;
672 if (m_contains_linker_annotations)
673 bitfields |= 1u << 7;
674 if (m_is_weak)
675 bitfields |= 1u << 6;
676 file.AppendU16(bitfields);
677 m_mangled.Encode(file, strtab);
678 // A symbol's value might be an address, or it might be a constant. If the
679 // symbol's base address doesn't have a section, then it is a constant value.
680 // If it does have a section, we will encode the file address and re-resolve
681 // the address when we decode it.
682 bool is_addr = m_addr_range.GetBaseAddress().GetSection().get() != nullptr;
683 file.AppendU8(is_addr);
684 file.AppendU64(m_addr_range.GetBaseAddress().GetFileAddress());
685 file.AppendU64(m_addr_range.GetByteSize());
686 file.AppendU32(m_flags);
687 }
688
operator ==(const Symbol & rhs) const689 bool Symbol::operator==(const Symbol &rhs) const {
690 if (m_uid != rhs.m_uid)
691 return false;
692 if (m_type_data != rhs.m_type_data)
693 return false;
694 if (m_type_data_resolved != rhs.m_type_data_resolved)
695 return false;
696 if (m_is_synthetic != rhs.m_is_synthetic)
697 return false;
698 if (m_is_debug != rhs.m_is_debug)
699 return false;
700 if (m_is_external != rhs.m_is_external)
701 return false;
702 if (m_size_is_sibling != rhs.m_size_is_sibling)
703 return false;
704 if (m_size_is_synthesized != rhs.m_size_is_synthesized)
705 return false;
706 if (m_size_is_valid != rhs.m_size_is_valid)
707 return false;
708 if (m_demangled_is_synthesized != rhs.m_demangled_is_synthesized)
709 return false;
710 if (m_contains_linker_annotations != rhs.m_contains_linker_annotations)
711 return false;
712 if (m_is_weak != rhs.m_is_weak)
713 return false;
714 if (m_type != rhs.m_type)
715 return false;
716 if (m_mangled != rhs.m_mangled)
717 return false;
718 if (m_addr_range.GetBaseAddress() != rhs.m_addr_range.GetBaseAddress())
719 return false;
720 if (m_addr_range.GetByteSize() != rhs.m_addr_range.GetByteSize())
721 return false;
722 if (m_flags != rhs.m_flags)
723 return false;
724 return true;
725 }
726