[lld][WebAssembly] Perform data relocations during start function
[llvm-project.git] / lldb / source / Symbol / Function.cpp
blobdda9ec2327151608bccc3e9d6db473a6466dc6f8
1 //===-- Function.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 //===----------------------------------------------------------------------===//
9 #include "lldb/Symbol/Function.h"
10 #include "lldb/Core/Disassembler.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ModuleList.h"
13 #include "lldb/Core/Section.h"
14 #include "lldb/Host/Host.h"
15 #include "lldb/Symbol/CompileUnit.h"
16 #include "lldb/Symbol/CompilerType.h"
17 #include "lldb/Symbol/LineTable.h"
18 #include "lldb/Symbol/SymbolFile.h"
19 #include "lldb/Target/Language.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/Log.h"
22 #include "llvm/Support/Casting.h"
24 using namespace lldb;
25 using namespace lldb_private;
27 // Basic function information is contained in the FunctionInfo class. It is
28 // designed to contain the name, linkage name, and declaration location.
29 FunctionInfo::FunctionInfo(const char *name, const Declaration *decl_ptr)
30 : m_name(name), m_declaration(decl_ptr) {}
32 FunctionInfo::FunctionInfo(ConstString name, const Declaration *decl_ptr)
33 : m_name(name), m_declaration(decl_ptr) {}
35 FunctionInfo::~FunctionInfo() = default;
37 void FunctionInfo::Dump(Stream *s, bool show_fullpaths) const {
38 if (m_name)
39 *s << ", name = \"" << m_name << "\"";
40 m_declaration.Dump(s, show_fullpaths);
43 int FunctionInfo::Compare(const FunctionInfo &a, const FunctionInfo &b) {
44 int result = ConstString::Compare(a.GetName(), b.GetName());
45 if (result)
46 return result;
48 return Declaration::Compare(a.m_declaration, b.m_declaration);
51 Declaration &FunctionInfo::GetDeclaration() { return m_declaration; }
53 const Declaration &FunctionInfo::GetDeclaration() const {
54 return m_declaration;
57 ConstString FunctionInfo::GetName() const { return m_name; }
59 size_t FunctionInfo::MemorySize() const {
60 return m_name.MemorySize() + m_declaration.MemorySize();
63 InlineFunctionInfo::InlineFunctionInfo(const char *name,
64 llvm::StringRef mangled,
65 const Declaration *decl_ptr,
66 const Declaration *call_decl_ptr)
67 : FunctionInfo(name, decl_ptr), m_mangled(mangled),
68 m_call_decl(call_decl_ptr) {}
70 InlineFunctionInfo::InlineFunctionInfo(ConstString name,
71 const Mangled &mangled,
72 const Declaration *decl_ptr,
73 const Declaration *call_decl_ptr)
74 : FunctionInfo(name, decl_ptr), m_mangled(mangled),
75 m_call_decl(call_decl_ptr) {}
77 InlineFunctionInfo::~InlineFunctionInfo() = default;
79 void InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const {
80 FunctionInfo::Dump(s, show_fullpaths);
81 if (m_mangled)
82 m_mangled.Dump(s);
85 void InlineFunctionInfo::DumpStopContext(Stream *s) const {
86 // s->Indent("[inlined] ");
87 s->Indent();
88 if (m_mangled)
89 s->PutCString(m_mangled.GetName().AsCString());
90 else
91 s->PutCString(m_name.AsCString());
94 ConstString InlineFunctionInfo::GetName() const {
95 if (m_mangled)
96 return m_mangled.GetName();
97 return m_name;
100 ConstString InlineFunctionInfo::GetDisplayName() const {
101 if (m_mangled)
102 return m_mangled.GetDisplayDemangledName();
103 return m_name;
106 Declaration &InlineFunctionInfo::GetCallSite() { return m_call_decl; }
108 const Declaration &InlineFunctionInfo::GetCallSite() const {
109 return m_call_decl;
112 Mangled &InlineFunctionInfo::GetMangled() { return m_mangled; }
114 const Mangled &InlineFunctionInfo::GetMangled() const { return m_mangled; }
116 size_t InlineFunctionInfo::MemorySize() const {
117 return FunctionInfo::MemorySize() + m_mangled.MemorySize();
120 /// @name Call site related structures
121 /// @{
123 lldb::addr_t CallEdge::GetLoadAddress(lldb::addr_t unresolved_pc,
124 Function &caller, Target &target) {
125 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
127 const Address &caller_start_addr = caller.GetAddressRange().GetBaseAddress();
129 ModuleSP caller_module_sp = caller_start_addr.GetModule();
130 if (!caller_module_sp) {
131 LLDB_LOG(log, "GetLoadAddress: cannot get Module for caller");
132 return LLDB_INVALID_ADDRESS;
135 SectionList *section_list = caller_module_sp->GetSectionList();
136 if (!section_list) {
137 LLDB_LOG(log, "GetLoadAddress: cannot get SectionList for Module");
138 return LLDB_INVALID_ADDRESS;
141 Address the_addr = Address(unresolved_pc, section_list);
142 lldb::addr_t load_addr = the_addr.GetLoadAddress(&target);
143 return load_addr;
146 lldb::addr_t CallEdge::GetReturnPCAddress(Function &caller,
147 Target &target) const {
148 return GetLoadAddress(GetUnresolvedReturnPCAddress(), caller, target);
151 void DirectCallEdge::ParseSymbolFileAndResolve(ModuleList &images) {
152 if (resolved)
153 return;
155 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
156 LLDB_LOG(log, "DirectCallEdge: Lazily parsing the call graph for {0}",
157 lazy_callee.symbol_name);
159 auto resolve_lazy_callee = [&]() -> Function * {
160 ConstString callee_name{lazy_callee.symbol_name};
161 SymbolContextList sc_list;
162 images.FindFunctionSymbols(callee_name, eFunctionNameTypeAuto, sc_list);
163 size_t num_matches = sc_list.GetSize();
164 if (num_matches == 0 || !sc_list[0].symbol) {
165 LLDB_LOG(log,
166 "DirectCallEdge: Found no symbols for {0}, cannot resolve it",
167 callee_name);
168 return nullptr;
170 Address callee_addr = sc_list[0].symbol->GetAddress();
171 if (!callee_addr.IsValid()) {
172 LLDB_LOG(log, "DirectCallEdge: Invalid symbol address");
173 return nullptr;
175 Function *f = callee_addr.CalculateSymbolContextFunction();
176 if (!f) {
177 LLDB_LOG(log, "DirectCallEdge: Could not find complete function");
178 return nullptr;
180 return f;
182 lazy_callee.def = resolve_lazy_callee();
183 resolved = true;
186 Function *DirectCallEdge::GetCallee(ModuleList &images, ExecutionContext &) {
187 ParseSymbolFileAndResolve(images);
188 assert(resolved && "Did not resolve lazy callee");
189 return lazy_callee.def;
192 Function *IndirectCallEdge::GetCallee(ModuleList &images,
193 ExecutionContext &exe_ctx) {
194 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
195 Status error;
196 Value callee_addr_val;
197 if (!call_target.Evaluate(&exe_ctx, exe_ctx.GetRegisterContext(),
198 /*loclist_base_load_addr=*/LLDB_INVALID_ADDRESS,
199 /*initial_value_ptr=*/nullptr,
200 /*object_address_ptr=*/nullptr, callee_addr_val,
201 &error)) {
202 LLDB_LOGF(log, "IndirectCallEdge: Could not evaluate expression: %s",
203 error.AsCString());
204 return nullptr;
207 addr_t raw_addr = callee_addr_val.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
208 if (raw_addr == LLDB_INVALID_ADDRESS) {
209 LLDB_LOG(log, "IndirectCallEdge: Could not extract address from scalar");
210 return nullptr;
213 Address callee_addr;
214 if (!exe_ctx.GetTargetPtr()->ResolveLoadAddress(raw_addr, callee_addr)) {
215 LLDB_LOG(log, "IndirectCallEdge: Could not resolve callee's load address");
216 return nullptr;
219 Function *f = callee_addr.CalculateSymbolContextFunction();
220 if (!f) {
221 LLDB_LOG(log, "IndirectCallEdge: Could not find complete function");
222 return nullptr;
225 return f;
228 /// @}
231 Function::Function(CompileUnit *comp_unit, lldb::user_id_t func_uid,
232 lldb::user_id_t type_uid, const Mangled &mangled, Type *type,
233 const AddressRange &range)
234 : UserID(func_uid), m_comp_unit(comp_unit), m_type_uid(type_uid),
235 m_type(type), m_mangled(mangled), m_block(func_uid), m_range(range),
236 m_frame_base(), m_flags(), m_prologue_byte_size(0) {
237 m_block.SetParentScope(this);
238 assert(comp_unit != nullptr);
241 Function::~Function() = default;
243 void Function::GetStartLineSourceInfo(FileSpec &source_file,
244 uint32_t &line_no) {
245 line_no = 0;
246 source_file.Clear();
248 if (m_comp_unit == nullptr)
249 return;
251 // Initialize m_type if it hasn't been initialized already
252 GetType();
254 if (m_type != nullptr && m_type->GetDeclaration().GetLine() != 0) {
255 source_file = m_type->GetDeclaration().GetFile();
256 line_no = m_type->GetDeclaration().GetLine();
257 } else {
258 LineTable *line_table = m_comp_unit->GetLineTable();
259 if (line_table == nullptr)
260 return;
262 LineEntry line_entry;
263 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(),
264 line_entry, nullptr)) {
265 line_no = line_entry.line;
266 source_file = line_entry.file;
271 void Function::GetEndLineSourceInfo(FileSpec &source_file, uint32_t &line_no) {
272 line_no = 0;
273 source_file.Clear();
275 // The -1 is kind of cheesy, but I want to get the last line entry for the
276 // given function, not the first entry of the next.
277 Address scratch_addr(GetAddressRange().GetBaseAddress());
278 scratch_addr.SetOffset(scratch_addr.GetOffset() +
279 GetAddressRange().GetByteSize() - 1);
281 LineTable *line_table = m_comp_unit->GetLineTable();
282 if (line_table == nullptr)
283 return;
285 LineEntry line_entry;
286 if (line_table->FindLineEntryByAddress(scratch_addr, line_entry, nullptr)) {
287 line_no = line_entry.line;
288 source_file = line_entry.file;
292 llvm::ArrayRef<std::unique_ptr<CallEdge>> Function::GetCallEdges() {
293 std::lock_guard<std::mutex> guard(m_call_edges_lock);
295 if (m_call_edges_resolved)
296 return m_call_edges;
298 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
299 LLDB_LOG(log, "GetCallEdges: Attempting to parse call site info for {0}",
300 GetDisplayName());
302 m_call_edges_resolved = true;
304 // Find the SymbolFile which provided this function's definition.
305 Block &block = GetBlock(/*can_create*/true);
306 SymbolFile *sym_file = block.GetSymbolFile();
307 if (!sym_file)
308 return llvm::None;
310 // Lazily read call site information from the SymbolFile.
311 m_call_edges = sym_file->ParseCallEdgesInFunction(GetID());
313 // Sort the call edges to speed up return_pc lookups.
314 llvm::sort(m_call_edges, [](const std::unique_ptr<CallEdge> &LHS,
315 const std::unique_ptr<CallEdge> &RHS) {
316 return LHS->GetSortKey() < RHS->GetSortKey();
319 return m_call_edges;
322 llvm::ArrayRef<std::unique_ptr<CallEdge>> Function::GetTailCallingEdges() {
323 // Tail calling edges are sorted at the end of the list. Find them by dropping
324 // all non-tail-calls.
325 return GetCallEdges().drop_until(
326 [](const std::unique_ptr<CallEdge> &edge) { return edge->IsTailCall(); });
329 CallEdge *Function::GetCallEdgeForReturnAddress(addr_t return_pc,
330 Target &target) {
331 auto edges = GetCallEdges();
332 auto edge_it =
333 llvm::partition_point(edges, [&](const std::unique_ptr<CallEdge> &edge) {
334 return std::make_pair(edge->IsTailCall(),
335 edge->GetReturnPCAddress(*this, target)) <
336 std::make_pair(false, return_pc);
338 if (edge_it == edges.end() ||
339 edge_it->get()->GetReturnPCAddress(*this, target) != return_pc)
340 return nullptr;
341 return edge_it->get();
344 Block &Function::GetBlock(bool can_create) {
345 if (!m_block.BlockInfoHasBeenParsed() && can_create) {
346 ModuleSP module_sp = CalculateSymbolContextModule();
347 if (module_sp) {
348 module_sp->GetSymbolFile()->ParseBlocksRecursive(*this);
349 } else {
350 Host::SystemLog(Host::eSystemLogError,
351 "error: unable to find module "
352 "shared pointer for function '%s' "
353 "in %s\n",
354 GetName().GetCString(),
355 m_comp_unit->GetPrimaryFile().GetPath().c_str());
357 m_block.SetBlockInfoHasBeenParsed(true, true);
359 return m_block;
362 CompileUnit *Function::GetCompileUnit() { return m_comp_unit; }
364 const CompileUnit *Function::GetCompileUnit() const { return m_comp_unit; }
366 void Function::GetDescription(Stream *s, lldb::DescriptionLevel level,
367 Target *target) {
368 ConstString name = GetName();
369 ConstString mangled = m_mangled.GetMangledName();
371 *s << "id = " << (const UserID &)*this;
372 if (name)
373 s->AsRawOstream() << ", name = \"" << name << '"';
374 if (mangled)
375 s->AsRawOstream() << ", mangled = \"" << mangled << '"';
376 *s << ", range = ";
377 Address::DumpStyle fallback_style;
378 if (level == eDescriptionLevelVerbose)
379 fallback_style = Address::DumpStyleModuleWithFileAddress;
380 else
381 fallback_style = Address::DumpStyleFileAddress;
382 GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress,
383 fallback_style);
386 void Function::Dump(Stream *s, bool show_context) const {
387 s->Printf("%p: ", static_cast<const void *>(this));
388 s->Indent();
389 *s << "Function" << static_cast<const UserID &>(*this);
391 m_mangled.Dump(s);
393 if (m_type)
394 s->Printf(", type = %p", static_cast<void *>(m_type));
395 else if (m_type_uid != LLDB_INVALID_UID)
396 s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid);
398 s->EOL();
399 // Dump the root object
400 if (m_block.BlockInfoHasBeenParsed())
401 m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX,
402 show_context);
405 void Function::CalculateSymbolContext(SymbolContext *sc) {
406 sc->function = this;
407 m_comp_unit->CalculateSymbolContext(sc);
410 ModuleSP Function::CalculateSymbolContextModule() {
411 SectionSP section_sp(m_range.GetBaseAddress().GetSection());
412 if (section_sp)
413 return section_sp->GetModule();
415 return this->GetCompileUnit()->GetModule();
418 CompileUnit *Function::CalculateSymbolContextCompileUnit() {
419 return this->GetCompileUnit();
422 Function *Function::CalculateSymbolContextFunction() { return this; }
424 lldb::DisassemblerSP Function::GetInstructions(const ExecutionContext &exe_ctx,
425 const char *flavor,
426 bool prefer_file_cache) {
427 ModuleSP module_sp(GetAddressRange().GetBaseAddress().GetModule());
428 if (module_sp && exe_ctx.HasTargetScope()) {
429 return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr,
430 flavor, exe_ctx.GetTargetRef(),
431 GetAddressRange(), !prefer_file_cache);
433 return lldb::DisassemblerSP();
436 bool Function::GetDisassembly(const ExecutionContext &exe_ctx,
437 const char *flavor, Stream &strm,
438 bool prefer_file_cache) {
439 lldb::DisassemblerSP disassembler_sp =
440 GetInstructions(exe_ctx, flavor, prefer_file_cache);
441 if (disassembler_sp) {
442 const bool show_address = true;
443 const bool show_bytes = false;
444 disassembler_sp->GetInstructionList().Dump(&strm, show_address, show_bytes,
445 &exe_ctx);
446 return true;
448 return false;
451 // Symbol *
452 // Function::CalculateSymbolContextSymbol ()
454 // return // TODO: find the symbol for the function???
457 void Function::DumpSymbolContext(Stream *s) {
458 m_comp_unit->DumpSymbolContext(s);
459 s->Printf(", Function{0x%8.8" PRIx64 "}", GetID());
462 size_t Function::MemorySize() const {
463 size_t mem_size = sizeof(Function) + m_block.MemorySize();
464 return mem_size;
467 bool Function::GetIsOptimized() {
468 bool result = false;
470 // Currently optimization is only indicted by the vendor extension
471 // DW_AT_APPLE_optimized which is set on a compile unit level.
472 if (m_comp_unit) {
473 result = m_comp_unit->GetIsOptimized();
475 return result;
478 bool Function::IsTopLevelFunction() {
479 bool result = false;
481 if (Language *language = Language::FindPlugin(GetLanguage()))
482 result = language->IsTopLevelFunction(*this);
484 return result;
487 ConstString Function::GetDisplayName() const {
488 return m_mangled.GetDisplayDemangledName();
491 CompilerDeclContext Function::GetDeclContext() {
492 ModuleSP module_sp = CalculateSymbolContextModule();
494 if (module_sp) {
495 if (SymbolFile *sym_file = module_sp->GetSymbolFile())
496 return sym_file->GetDeclContextForUID(GetID());
498 return CompilerDeclContext();
501 Type *Function::GetType() {
502 if (m_type == nullptr) {
503 SymbolContext sc;
505 CalculateSymbolContext(&sc);
507 if (!sc.module_sp)
508 return nullptr;
510 SymbolFile *sym_file = sc.module_sp->GetSymbolFile();
512 if (sym_file == nullptr)
513 return nullptr;
515 m_type = sym_file->ResolveTypeUID(m_type_uid);
517 return m_type;
520 const Type *Function::GetType() const { return m_type; }
522 CompilerType Function::GetCompilerType() {
523 Type *function_type = GetType();
524 if (function_type)
525 return function_type->GetFullCompilerType();
526 return CompilerType();
529 uint32_t Function::GetPrologueByteSize() {
530 if (m_prologue_byte_size == 0 &&
531 m_flags.IsClear(flagsCalculatedPrologueSize)) {
532 m_flags.Set(flagsCalculatedPrologueSize);
533 LineTable *line_table = m_comp_unit->GetLineTable();
534 uint32_t prologue_end_line_idx = 0;
536 if (line_table) {
537 LineEntry first_line_entry;
538 uint32_t first_line_entry_idx = UINT32_MAX;
539 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(),
540 first_line_entry,
541 &first_line_entry_idx)) {
542 // Make sure the first line entry isn't already the end of the prologue
543 addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS;
544 addr_t line_zero_end_file_addr = LLDB_INVALID_ADDRESS;
546 if (first_line_entry.is_prologue_end) {
547 prologue_end_file_addr =
548 first_line_entry.range.GetBaseAddress().GetFileAddress();
549 prologue_end_line_idx = first_line_entry_idx;
550 } else {
551 // Check the first few instructions and look for one that has
552 // is_prologue_end set to true.
553 const uint32_t last_line_entry_idx = first_line_entry_idx + 6;
554 for (uint32_t idx = first_line_entry_idx + 1;
555 idx < last_line_entry_idx; ++idx) {
556 LineEntry line_entry;
557 if (line_table->GetLineEntryAtIndex(idx, line_entry)) {
558 if (line_entry.is_prologue_end) {
559 prologue_end_file_addr =
560 line_entry.range.GetBaseAddress().GetFileAddress();
561 prologue_end_line_idx = idx;
562 break;
568 // If we didn't find the end of the prologue in the line tables, then
569 // just use the end address of the first line table entry
570 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) {
571 // Check the first few instructions and look for one that has a line
572 // number that's different than the first entry.
573 uint32_t last_line_entry_idx = first_line_entry_idx + 6;
574 for (uint32_t idx = first_line_entry_idx + 1;
575 idx < last_line_entry_idx; ++idx) {
576 LineEntry line_entry;
577 if (line_table->GetLineEntryAtIndex(idx, line_entry)) {
578 if (line_entry.line != first_line_entry.line) {
579 prologue_end_file_addr =
580 line_entry.range.GetBaseAddress().GetFileAddress();
581 prologue_end_line_idx = idx;
582 break;
587 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) {
588 prologue_end_file_addr =
589 first_line_entry.range.GetBaseAddress().GetFileAddress() +
590 first_line_entry.range.GetByteSize();
591 prologue_end_line_idx = first_line_entry_idx;
595 const addr_t func_start_file_addr =
596 m_range.GetBaseAddress().GetFileAddress();
597 const addr_t func_end_file_addr =
598 func_start_file_addr + m_range.GetByteSize();
600 // Now calculate the offset to pass the subsequent line 0 entries.
601 uint32_t first_non_zero_line = prologue_end_line_idx;
602 while (true) {
603 LineEntry line_entry;
604 if (line_table->GetLineEntryAtIndex(first_non_zero_line,
605 line_entry)) {
606 if (line_entry.line != 0)
607 break;
609 if (line_entry.range.GetBaseAddress().GetFileAddress() >=
610 func_end_file_addr)
611 break;
613 first_non_zero_line++;
616 if (first_non_zero_line > prologue_end_line_idx) {
617 LineEntry first_non_zero_entry;
618 if (line_table->GetLineEntryAtIndex(first_non_zero_line,
619 first_non_zero_entry)) {
620 line_zero_end_file_addr =
621 first_non_zero_entry.range.GetBaseAddress().GetFileAddress();
625 // Verify that this prologue end file address in the function's address
626 // range just to be sure
627 if (func_start_file_addr < prologue_end_file_addr &&
628 prologue_end_file_addr < func_end_file_addr) {
629 m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr;
632 if (prologue_end_file_addr < line_zero_end_file_addr &&
633 line_zero_end_file_addr < func_end_file_addr) {
634 m_prologue_byte_size +=
635 line_zero_end_file_addr - prologue_end_file_addr;
641 return m_prologue_byte_size;
644 lldb::LanguageType Function::GetLanguage() const {
645 lldb::LanguageType lang = m_mangled.GuessLanguage();
646 if (lang != lldb::eLanguageTypeUnknown)
647 return lang;
649 if (m_comp_unit)
650 return m_comp_unit->GetLanguage();
652 return lldb::eLanguageTypeUnknown;
655 ConstString Function::GetName() const {
656 return m_mangled.GetName();
659 ConstString Function::GetNameNoArguments() const {
660 return m_mangled.GetName(Mangled::ePreferDemangledWithoutArguments);