Path: blob/main/contrib/llvm-project/lldb/source/Plugins/DynamicLoader/Hexagon-DYLD/HexagonDYLDRendezvous.cpp
39653 views
//===-- HexagonDYLDRendezvous.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 "lldb/Core/Module.h"9#include "lldb/Symbol/Symbol.h"10#include "lldb/Symbol/SymbolContext.h"11#include "lldb/Target/Process.h"12#include "lldb/Target/Target.h"13#include "lldb/Utility/Log.h"14#include "lldb/Utility/Status.h"1516#include "lldb/Symbol/ObjectFile.h"17#include "lldb/Target/Process.h"18#include "lldb/Target/Target.h"1920#include "HexagonDYLDRendezvous.h"2122using namespace lldb;23using namespace lldb_private;2425/// Locates the address of the rendezvous structure. Returns the address on26/// success and LLDB_INVALID_ADDRESS on failure.27static addr_t ResolveRendezvousAddress(Process *process) {28addr_t info_location;29addr_t info_addr;30Status error;3132info_location = process->GetImageInfoAddress();3334if (info_location == LLDB_INVALID_ADDRESS)35return LLDB_INVALID_ADDRESS;3637info_addr = process->ReadPointerFromMemory(info_location, error);38if (error.Fail())39return LLDB_INVALID_ADDRESS;4041if (info_addr == 0)42return LLDB_INVALID_ADDRESS;4344return info_addr;45}4647HexagonDYLDRendezvous::HexagonDYLDRendezvous(Process *process)48: m_process(process), m_rendezvous_addr(LLDB_INVALID_ADDRESS), m_current(),49m_previous(), m_soentries(), m_added_soentries(), m_removed_soentries() {50m_thread_info.valid = false;51m_thread_info.dtv_offset = 0;52m_thread_info.dtv_slot_size = 0;53m_thread_info.modid_offset = 0;54m_thread_info.tls_offset = 0;5556// Cache a copy of the executable path57if (m_process) {58Module *exe_mod = m_process->GetTarget().GetExecutableModulePointer();59if (exe_mod)60exe_mod->GetFileSpec().GetPath(m_exe_path, PATH_MAX);61}62}6364bool HexagonDYLDRendezvous::Resolve() {65const size_t word_size = 4;66Rendezvous info;67size_t address_size;68size_t padding;69addr_t info_addr;70addr_t cursor;7172address_size = m_process->GetAddressByteSize();73padding = address_size - word_size;7475if (m_rendezvous_addr == LLDB_INVALID_ADDRESS)76cursor = info_addr = ResolveRendezvousAddress(m_process);77else78cursor = info_addr = m_rendezvous_addr;7980if (cursor == LLDB_INVALID_ADDRESS)81return false;8283if (!(cursor = ReadWord(cursor, &info.version, word_size)))84return false;8586if (!(cursor = ReadPointer(cursor + padding, &info.map_addr)))87return false;8889if (!(cursor = ReadPointer(cursor, &info.brk)))90return false;9192if (!(cursor = ReadWord(cursor, &info.state, word_size)))93return false;9495if (!(cursor = ReadPointer(cursor + padding, &info.ldbase)))96return false;9798// The rendezvous was successfully read. Update our internal state.99m_rendezvous_addr = info_addr;100m_previous = m_current;101m_current = info;102103return UpdateSOEntries();104}105106void HexagonDYLDRendezvous::SetRendezvousAddress(lldb::addr_t addr) {107m_rendezvous_addr = addr;108}109110bool HexagonDYLDRendezvous::IsValid() {111return m_rendezvous_addr != LLDB_INVALID_ADDRESS;112}113114bool HexagonDYLDRendezvous::UpdateSOEntries() {115SOEntry entry;116117if (m_current.map_addr == 0)118return false;119120// When the previous and current states are consistent this is the first time121// we have been asked to update. Just take a snapshot of the currently122// loaded modules.123if (m_previous.state == eConsistent && m_current.state == eConsistent)124return TakeSnapshot(m_soentries);125126// If we are about to add or remove a shared object clear out the current127// state and take a snapshot of the currently loaded images.128if (m_current.state == eAdd || m_current.state == eDelete) {129// this is a fudge so that we can clear the assert below.130m_previous.state = eConsistent;131// We hit this assert on the 2nd run of this function after running the132// calc example133assert(m_previous.state == eConsistent);134m_soentries.clear();135m_added_soentries.clear();136m_removed_soentries.clear();137return TakeSnapshot(m_soentries);138}139assert(m_current.state == eConsistent);140141// Otherwise check the previous state to determine what to expect and update142// accordingly.143if (m_previous.state == eAdd)144return UpdateSOEntriesForAddition();145else if (m_previous.state == eDelete)146return UpdateSOEntriesForDeletion();147148return false;149}150151bool HexagonDYLDRendezvous::UpdateSOEntriesForAddition() {152SOEntry entry;153iterator pos;154155assert(m_previous.state == eAdd);156157if (m_current.map_addr == 0)158return false;159160for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next) {161if (!ReadSOEntryFromMemory(cursor, entry))162return false;163164// Only add shared libraries and not the executable. On Linux this is165// indicated by an empty path in the entry. On FreeBSD it is the name of166// the executable.167if (entry.path.empty() || ::strcmp(entry.path.c_str(), m_exe_path) == 0)168continue;169170if (!llvm::is_contained(m_soentries, entry)) {171m_soentries.push_back(entry);172m_added_soentries.push_back(entry);173}174}175176return true;177}178179bool HexagonDYLDRendezvous::UpdateSOEntriesForDeletion() {180SOEntryList entry_list;181iterator pos;182183assert(m_previous.state == eDelete);184185if (!TakeSnapshot(entry_list))186return false;187188for (iterator I = begin(); I != end(); ++I) {189if (!llvm::is_contained(entry_list, *I))190m_removed_soentries.push_back(*I);191}192193m_soentries = entry_list;194return true;195}196197bool HexagonDYLDRendezvous::TakeSnapshot(SOEntryList &entry_list) {198SOEntry entry;199200if (m_current.map_addr == 0)201return false;202203for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next) {204if (!ReadSOEntryFromMemory(cursor, entry))205return false;206207// Only add shared libraries and not the executable. On Linux this is208// indicated by an empty path in the entry. On FreeBSD it is the name of209// the executable.210if (entry.path.empty() || ::strcmp(entry.path.c_str(), m_exe_path) == 0)211continue;212213entry_list.push_back(entry);214}215216return true;217}218219addr_t HexagonDYLDRendezvous::ReadWord(addr_t addr, uint64_t *dst,220size_t size) {221Status error;222223*dst = m_process->ReadUnsignedIntegerFromMemory(addr, size, 0, error);224if (error.Fail())225return 0;226227return addr + size;228}229230addr_t HexagonDYLDRendezvous::ReadPointer(addr_t addr, addr_t *dst) {231Status error;232233*dst = m_process->ReadPointerFromMemory(addr, error);234if (error.Fail())235return 0;236237return addr + m_process->GetAddressByteSize();238}239240std::string HexagonDYLDRendezvous::ReadStringFromMemory(addr_t addr) {241std::string str;242Status error;243size_t size;244char c;245246if (addr == LLDB_INVALID_ADDRESS)247return std::string();248249for (;;) {250size = m_process->ReadMemory(addr, &c, 1, error);251if (size != 1 || error.Fail())252return std::string();253if (c == 0)254break;255else {256str.push_back(c);257addr++;258}259}260261return str;262}263264bool HexagonDYLDRendezvous::ReadSOEntryFromMemory(lldb::addr_t addr,265SOEntry &entry) {266entry.clear();267entry.link_addr = addr;268269if (!(addr = ReadPointer(addr, &entry.base_addr)))270return false;271272if (!(addr = ReadPointer(addr, &entry.path_addr)))273return false;274275if (!(addr = ReadPointer(addr, &entry.dyn_addr)))276return false;277278if (!(addr = ReadPointer(addr, &entry.next)))279return false;280281if (!(addr = ReadPointer(addr, &entry.prev)))282return false;283284entry.path = ReadStringFromMemory(entry.path_addr);285286return true;287}288289bool HexagonDYLDRendezvous::FindMetadata(const char *name, PThreadField field,290uint32_t &value) {291Target &target = m_process->GetTarget();292293SymbolContextList list;294target.GetImages().FindSymbolsWithNameAndType(ConstString(name),295eSymbolTypeAny, list);296if (list.IsEmpty())297return false;298299Address address = list[0].symbol->GetAddress();300addr_t addr = address.GetLoadAddress(&target);301if (addr == LLDB_INVALID_ADDRESS)302return false;303304Status error;305value = (uint32_t)m_process->ReadUnsignedIntegerFromMemory(306addr + field * sizeof(uint32_t), sizeof(uint32_t), 0, error);307if (error.Fail())308return false;309310if (field == eSize)311value /= 8; // convert bits to bytes312313return true;314}315316const HexagonDYLDRendezvous::ThreadInfo &317HexagonDYLDRendezvous::GetThreadInfo() {318if (!m_thread_info.valid) {319bool ok = true;320321ok &= FindMetadata("_thread_db_pthread_dtvp", eOffset,322m_thread_info.dtv_offset);323ok &=324FindMetadata("_thread_db_dtv_dtv", eSize, m_thread_info.dtv_slot_size);325ok &= FindMetadata("_thread_db_link_map_l_tls_modid", eOffset,326m_thread_info.modid_offset);327ok &= FindMetadata("_thread_db_dtv_t_pointer_val", eOffset,328m_thread_info.tls_offset);329330if (ok)331m_thread_info.valid = true;332}333334return m_thread_info;335}336337void HexagonDYLDRendezvous::DumpToLog(Log *log) const {338int state = GetState();339340if (!log)341return;342343log->PutCString("HexagonDYLDRendezvous:");344LLDB_LOGF(log, " Address: %" PRIx64, GetRendezvousAddress());345LLDB_LOGF(log, " Version: %" PRIu64, GetVersion());346LLDB_LOGF(log, " Link : %" PRIx64, GetLinkMapAddress());347LLDB_LOGF(log, " Break : %" PRIx64, GetBreakAddress());348LLDB_LOGF(log, " LDBase : %" PRIx64, GetLDBase());349LLDB_LOGF(log, " State : %s",350(state == eConsistent)351? "consistent"352: (state == eAdd) ? "add"353: (state == eDelete) ? "delete" : "unknown");354355iterator I = begin();356iterator E = end();357358if (I != E)359log->PutCString("HexagonDYLDRendezvous SOEntries:");360361for (int i = 1; I != E; ++I, ++i) {362LLDB_LOGF(log, "\n SOEntry [%d] %s", i, I->path.c_str());363LLDB_LOGF(log, " Base : %" PRIx64, I->base_addr);364LLDB_LOGF(log, " Path : %" PRIx64, I->path_addr);365LLDB_LOGF(log, " Dyn : %" PRIx64, I->dyn_addr);366LLDB_LOGF(log, " Next : %" PRIx64, I->next);367LLDB_LOGF(log, " Prev : %" PRIx64, I->prev);368}369}370371372