Path: blob/main/contrib/llvm-project/lldb/source/Plugins/SymbolFile/Symtab/SymbolFileSymtab.cpp
39642 views
//===-- SymbolFileSymtab.cpp ----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "SymbolFileSymtab.h"910#include "lldb/Core/Module.h"11#include "lldb/Core/PluginManager.h"12#include "lldb/Symbol/CompileUnit.h"13#include "lldb/Symbol/Function.h"14#include "lldb/Symbol/ObjectFile.h"15#include "lldb/Symbol/Symbol.h"16#include "lldb/Symbol/SymbolContext.h"17#include "lldb/Symbol/Symtab.h"18#include "lldb/Symbol/TypeList.h"19#include "lldb/Utility/RegularExpression.h"20#include "lldb/Utility/Timer.h"2122#include <memory>23#include <optional>2425using namespace lldb;26using namespace lldb_private;2728LLDB_PLUGIN_DEFINE(SymbolFileSymtab)2930char SymbolFileSymtab::ID;3132void SymbolFileSymtab::Initialize() {33PluginManager::RegisterPlugin(GetPluginNameStatic(),34GetPluginDescriptionStatic(), CreateInstance);35}3637void SymbolFileSymtab::Terminate() {38PluginManager::UnregisterPlugin(CreateInstance);39}4041llvm::StringRef SymbolFileSymtab::GetPluginDescriptionStatic() {42return "Reads debug symbols from an object file's symbol table.";43}4445SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFileSP objfile_sp) {46return new SymbolFileSymtab(std::move(objfile_sp));47}4849void SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope,50TypeClass type_mask,51lldb_private::TypeList &type_list) {}5253SymbolFileSymtab::SymbolFileSymtab(ObjectFileSP objfile_sp)54: SymbolFileCommon(std::move(objfile_sp)), m_source_indexes(),55m_func_indexes(), m_code_indexes(), m_objc_class_name_to_index() {}5657uint32_t SymbolFileSymtab::CalculateAbilities() {58uint32_t abilities = 0;59if (m_objfile_sp) {60const Symtab *symtab = m_objfile_sp->GetSymtab();61if (symtab) {62// The snippet of code below will get the indexes the module symbol table63// entries that are code, data, or function related (debug info), sort64// them by value (address) and dump the sorted symbols.65if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile,66m_source_indexes)) {67abilities |= CompileUnits;68}6970if (symtab->AppendSymbolIndexesWithType(71eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny,72m_func_indexes)) {73symtab->SortSymbolIndexesByValue(m_func_indexes, true);74abilities |= Functions;75}7677if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo,78Symtab::eVisibilityAny,79m_code_indexes)) {80symtab->SortSymbolIndexesByValue(m_code_indexes, true);81abilities |= Functions;82}8384if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData,85m_data_indexes)) {86symtab->SortSymbolIndexesByValue(m_data_indexes, true);87abilities |= GlobalVariables;88}8990lldb_private::Symtab::IndexCollection objc_class_indexes;91if (symtab->AppendSymbolIndexesWithType(eSymbolTypeObjCClass,92objc_class_indexes)) {93symtab->AppendSymbolNamesToMap(objc_class_indexes, true, true,94m_objc_class_name_to_index);95m_objc_class_name_to_index.Sort();96}97}98}99return abilities;100}101102uint32_t SymbolFileSymtab::CalculateNumCompileUnits() {103// If we don't have any source file symbols we will just have one compile104// unit for the entire object file105if (m_source_indexes.empty())106return 0;107108// If we have any source file symbols we will logically organize the object109// symbols using these.110return m_source_indexes.size();111}112113CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) {114CompUnitSP cu_sp;115116// If we don't have any source file symbols we will just have one compile117// unit for the entire object file118if (idx < m_source_indexes.size()) {119const Symbol *cu_symbol =120m_objfile_sp->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);121if (cu_symbol)122cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr,123cu_symbol->GetName().AsCString(), 0,124eLanguageTypeUnknown, eLazyBoolNo);125}126return cu_sp;127}128129lldb::LanguageType SymbolFileSymtab::ParseLanguage(CompileUnit &comp_unit) {130return eLanguageTypeUnknown;131}132133size_t SymbolFileSymtab::ParseFunctions(CompileUnit &comp_unit) {134std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());135size_t num_added = 0;136// We must at least have a valid compile unit137const Symtab *symtab = m_objfile_sp->GetSymtab();138const Symbol *curr_symbol = nullptr;139const Symbol *next_symbol = nullptr;140// const char *prefix = m_objfile_sp->SymbolPrefix();141// if (prefix == NULL)142// prefix == "";143//144// const uint32_t prefix_len = strlen(prefix);145146// If we don't have any source file symbols we will just have one compile147// unit for the entire object file148if (m_source_indexes.empty()) {149// The only time we will have a user ID of zero is when we don't have and150// source file symbols and we declare one compile unit for the entire151// object file152if (!m_func_indexes.empty()) {153}154155if (!m_code_indexes.empty()) {156// StreamFile s(stdout);157// symtab->Dump(&s, m_code_indexes);158159uint32_t idx = 0; // Index into the indexes160const uint32_t num_indexes = m_code_indexes.size();161for (idx = 0; idx < num_indexes; ++idx) {162uint32_t symbol_idx = m_code_indexes[idx];163curr_symbol = symtab->SymbolAtIndex(symbol_idx);164if (curr_symbol) {165// Union of all ranges in the function DIE (if the function is166// discontiguous)167AddressRange func_range(curr_symbol->GetAddress(), 0);168if (func_range.GetBaseAddress().IsSectionOffset()) {169uint32_t symbol_size = curr_symbol->GetByteSize();170if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())171func_range.SetByteSize(symbol_size);172else if (idx + 1 < num_indexes) {173next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);174if (next_symbol) {175func_range.SetByteSize(176next_symbol->GetAddressRef().GetOffset() -177curr_symbol->GetAddressRef().GetOffset());178}179}180181FunctionSP func_sp(182new Function(&comp_unit,183symbol_idx, // UserID is the DIE offset184LLDB_INVALID_UID, // We don't have any type info185// for this function186curr_symbol->GetMangled(), // Linker/mangled name187nullptr, // no return type for a code symbol...188func_range)); // first address range189190if (func_sp.get() != nullptr) {191comp_unit.AddFunction(func_sp);192++num_added;193}194}195}196}197}198} else {199// We assume we200}201return num_added;202}203204size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }205206bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }207208bool SymbolFileSymtab::ParseDebugMacros(CompileUnit &comp_unit) {209return false;210}211212bool SymbolFileSymtab::ParseSupportFiles(CompileUnit &comp_unit,213SupportFileList &support_files) {214return false;215}216217bool SymbolFileSymtab::ParseImportedModules(218const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {219return false;220}221222size_t SymbolFileSymtab::ParseBlocksRecursive(Function &func) { return 0; }223224size_t SymbolFileSymtab::ParseVariablesForContext(const SymbolContext &sc) {225return 0;226}227228Type *SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid) {229return nullptr;230}231232std::optional<SymbolFile::ArrayInfo>233SymbolFileSymtab::GetDynamicArrayInfoForUID(234lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {235return std::nullopt;236}237238bool SymbolFileSymtab::CompleteType(lldb_private::CompilerType &compiler_type) {239return false;240}241242uint32_t SymbolFileSymtab::ResolveSymbolContext(const Address &so_addr,243SymbolContextItem resolve_scope,244SymbolContext &sc) {245std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());246if (m_objfile_sp->GetSymtab() == nullptr)247return 0;248249uint32_t resolved_flags = 0;250if (resolve_scope & eSymbolContextSymbol) {251sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress(252so_addr.GetFileAddress());253if (sc.symbol)254resolved_flags |= eSymbolContextSymbol;255}256return resolved_flags;257}258259260