brintos

brintos / llvm-project-archived public Read only

0
0
Text · 19.6 KiB · 480500e Raw
599 lines · cpp
1//===-- DynamicLoaderHexagonDYLD.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Breakpoint/BreakpointLocation.h"10#include "lldb/Core/Module.h"11#include "lldb/Core/ModuleSpec.h"12#include "lldb/Core/PluginManager.h"13#include "lldb/Core/Section.h"14#include "lldb/Symbol/ObjectFile.h"15#include "lldb/Target/Process.h"16#include "lldb/Target/Target.h"17#include "lldb/Target/Thread.h"18#include "lldb/Target/ThreadPlanRunToAddress.h"19#include "lldb/Utility/LLDBLog.h"20#include "lldb/Utility/Log.h"21 22#include "DynamicLoaderHexagonDYLD.h"23 24#include <memory>25 26using namespace lldb;27using namespace lldb_private;28 29LLDB_PLUGIN_DEFINE(DynamicLoaderHexagonDYLD)30 31// Aidan 21/05/201432//33// Notes about hexagon dynamic loading:34//35//      When we connect to a target we find the dyld breakpoint address.  We put36//      a37//      breakpoint there with a callback 'RendezvousBreakpointHit()'.38//39//      It is possible to find the dyld structure address from the ELF symbol40//      table,41//      but in the case of the simulator it has not been initialized before the42//      target calls dlinit().43//44//      We can only safely parse the dyld structure after we hit the dyld45//      breakpoint46//      since at that time we know dlinit() must have been called.47//48 49// Find the load address of a symbol50static lldb::addr_t findSymbolAddress(Process *proc, ConstString findName) {51  assert(proc != nullptr);52 53  ModuleSP module = proc->GetTarget().GetExecutableModule();54  assert(module.get() != nullptr);55 56  ObjectFile *exe = module->GetObjectFile();57  assert(exe != nullptr);58 59  lldb_private::Symtab *symtab = exe->GetSymtab();60  assert(symtab != nullptr);61 62  for (size_t i = 0; i < symtab->GetNumSymbols(); i++) {63    const Symbol *sym = symtab->SymbolAtIndex(i);64    assert(sym != nullptr);65    ConstString symName = sym->GetName();66 67    if (ConstString::Compare(findName, symName) == 0) {68      Address addr = sym->GetAddress();69      return addr.GetLoadAddress(&proc->GetTarget());70    }71  }72  return LLDB_INVALID_ADDRESS;73}74 75void DynamicLoaderHexagonDYLD::Initialize() {76  PluginManager::RegisterPlugin(GetPluginNameStatic(),77                                GetPluginDescriptionStatic(), CreateInstance);78}79 80void DynamicLoaderHexagonDYLD::Terminate() {}81 82llvm::StringRef DynamicLoaderHexagonDYLD::GetPluginDescriptionStatic() {83  return "Dynamic loader plug-in that watches for shared library "84         "loads/unloads in Hexagon processes.";85}86 87DynamicLoader *DynamicLoaderHexagonDYLD::CreateInstance(Process *process,88                                                        bool force) {89  bool create = force;90  if (!create) {91    const llvm::Triple &triple_ref =92        process->GetTarget().GetArchitecture().GetTriple();93    if (triple_ref.getArch() == llvm::Triple::hexagon)94      create = true;95  }96 97  if (create)98    return new DynamicLoaderHexagonDYLD(process);99  return nullptr;100}101 102DynamicLoaderHexagonDYLD::DynamicLoaderHexagonDYLD(Process *process)103    : DynamicLoader(process), m_rendezvous(process),104      m_load_offset(LLDB_INVALID_ADDRESS), m_entry_point(LLDB_INVALID_ADDRESS),105      m_dyld_bid(LLDB_INVALID_BREAK_ID) {}106 107DynamicLoaderHexagonDYLD::~DynamicLoaderHexagonDYLD() {108  if (m_dyld_bid != LLDB_INVALID_BREAK_ID) {109    m_process->GetTarget().RemoveBreakpointByID(m_dyld_bid);110    m_dyld_bid = LLDB_INVALID_BREAK_ID;111  }112}113 114void DynamicLoaderHexagonDYLD::DidAttach() {115  ModuleSP executable;116  addr_t load_offset;117 118  executable = GetTargetExecutable();119 120  // Find the difference between the desired load address in the elf file and121  // the real load address in memory122  load_offset = ComputeLoadOffset();123 124  // Check that there is a valid executable125  if (executable.get() == nullptr)126    return;127 128  // Disable JIT for hexagon targets because its not supported129  m_process->SetCanJIT(false);130 131  // Enable Interpreting of function call expressions132  m_process->SetCanInterpretFunctionCalls(true);133 134  // Add the current executable to the module list135  ModuleList module_list;136  module_list.Append(executable);137 138  // Map the loaded sections of this executable139  if (load_offset != LLDB_INVALID_ADDRESS)140    UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset, true);141 142  // AD: confirm this?143  // Load into LLDB all of the currently loaded executables in the stub144  LoadAllCurrentModules();145 146  // AD: confirm this?147  // Callback for the target to give it the loaded module list148  m_process->GetTarget().ModulesDidLoad(module_list);149 150  // Try to set a breakpoint at the rendezvous breakpoint. DidLaunch uses151  // ProbeEntry() instead.  That sets a breakpoint, at the dyld breakpoint152  // address, with a callback so that when hit, the dyld structure can be153  // parsed.154  if (!SetRendezvousBreakpoint()) {155    // fail156  }157}158 159void DynamicLoaderHexagonDYLD::DidLaunch() {}160 161/// Checks to see if the target module has changed, updates the target162/// accordingly and returns the target executable module.163ModuleSP DynamicLoaderHexagonDYLD::GetTargetExecutable() {164  Target &target = m_process->GetTarget();165  ModuleSP executable = target.GetExecutableModule();166 167  // There is no executable168  if (!executable.get())169    return executable;170 171  // The target executable file does not exits172  if (!FileSystem::Instance().Exists(executable->GetFileSpec()))173    return executable;174 175  // Prep module for loading176  ModuleSpec module_spec(executable->GetFileSpec(),177                         executable->GetArchitecture());178  ModuleSP module_sp(new Module(module_spec));179 180  // Check if the executable has changed and set it to the target executable if181  // they differ.182  if (module_sp.get() && module_sp->GetUUID().IsValid() &&183      executable->GetUUID().IsValid()) {184    // if the executable has changed ??185    if (module_sp->GetUUID() != executable->GetUUID())186      executable.reset();187  } else if (executable->FileHasChanged())188    executable.reset();189 190  if (executable.get())191    return executable;192 193  // TODO: What case is this code used?194  executable = target.GetOrCreateModule(module_spec, true /* notify */);195  if (executable.get() != target.GetExecutableModulePointer()) {196    // Don't load dependent images since we are in dyld where we will know and197    // find out about all images that are loaded198    target.SetExecutableModule(executable, eLoadDependentsNo);199  }200 201  return executable;202}203 204// AD: Needs to be updated?205Status DynamicLoaderHexagonDYLD::CanLoadImage() { return Status(); }206 207void DynamicLoaderHexagonDYLD::UpdateLoadedSections(ModuleSP module,208                                                    addr_t link_map_addr,209                                                    addr_t base_addr,210                                                    bool base_addr_is_offset) {211  Target &target = m_process->GetTarget();212  const SectionList *sections = GetSectionListFromModule(module);213 214  assert(sections && "SectionList missing from loaded module.");215 216  m_loaded_modules[module] = link_map_addr;217 218  const size_t num_sections = sections->GetSize();219 220  for (unsigned i = 0; i < num_sections; ++i) {221    SectionSP section_sp(sections->GetSectionAtIndex(i));222    lldb::addr_t new_load_addr = section_sp->GetFileAddress() + base_addr;223 224    // AD: 02/05/14225    //   since our memory map starts from address 0, we must not ignore226    //   sections that load to address 0.  This violates the reference227    //   ELF spec, however is used for Hexagon.228 229    // If the file address of the section is zero then this is not an230    // allocatable/loadable section (property of ELF sh_addr).  Skip it.231    //      if (new_load_addr == base_addr)232    //          continue;233 234    target.SetSectionLoadAddress(section_sp, new_load_addr);235  }236}237 238/// Removes the loaded sections from the target in \p module.239///240/// \param module The module to traverse.241void DynamicLoaderHexagonDYLD::UnloadSections(const ModuleSP module) {242  Target &target = m_process->GetTarget();243  const SectionList *sections = GetSectionListFromModule(module);244 245  assert(sections && "SectionList missing from unloaded module.");246 247  m_loaded_modules.erase(module);248 249  const size_t num_sections = sections->GetSize();250  for (size_t i = 0; i < num_sections; ++i) {251    SectionSP section_sp(sections->GetSectionAtIndex(i));252    target.SetSectionUnloaded(section_sp);253  }254}255 256// Place a breakpoint on <_rtld_debug_state>257bool DynamicLoaderHexagonDYLD::SetRendezvousBreakpoint() {258  Log *log = GetLog(LLDBLog::DynamicLoader);259 260  // This is the original code, which want to look in the rendezvous structure261  // to find the breakpoint address.  Its backwards for us, since we can easily262  // find the breakpoint address, since it is exported in our executable. We263  // however know that we cant read the Rendezvous structure until we have hit264  // the breakpoint once.265  const ConstString dyldBpName("_rtld_debug_state");266  addr_t break_addr = findSymbolAddress(m_process, dyldBpName);267 268  Target &target = m_process->GetTarget();269 270  // Do not try to set the breakpoint if we don't know where to put it271  if (break_addr == LLDB_INVALID_ADDRESS) {272    LLDB_LOGF(log, "Unable to locate _rtld_debug_state breakpoint address");273 274    return false;275  }276 277  // Save the address of the rendezvous structure278  m_rendezvous.SetBreakAddress(break_addr);279 280  // If we haven't set the breakpoint before then set it281  if (m_dyld_bid == LLDB_INVALID_BREAK_ID) {282    Breakpoint *dyld_break =283        target.CreateBreakpoint(break_addr, true, false).get();284    dyld_break->SetCallback(RendezvousBreakpointHit, this, true);285    dyld_break->SetBreakpointKind("shared-library-event");286    m_dyld_bid = dyld_break->GetID();287 288    // Make sure our breakpoint is at the right address.289    assert(target.GetBreakpointByID(m_dyld_bid)290               ->FindLocationByAddress(break_addr)291               ->GetBreakpoint()292               .GetID() == m_dyld_bid);293 294    if (log && dyld_break == nullptr)295      LLDB_LOGF(log, "Failed to create _rtld_debug_state breakpoint");296 297    // check we have successfully set bp298    return (dyld_break != nullptr);299  } else300    // rendezvous already set301    return true;302}303 304// We have just hit our breakpoint at <_rtld_debug_state>305bool DynamicLoaderHexagonDYLD::RendezvousBreakpointHit(306    void *baton, StoppointCallbackContext *context, user_id_t break_id,307    user_id_t break_loc_id) {308  Log *log = GetLog(LLDBLog::DynamicLoader);309 310  LLDB_LOGF(log, "Rendezvous breakpoint hit!");311 312  DynamicLoaderHexagonDYLD *dyld_instance = nullptr;313  dyld_instance = static_cast<DynamicLoaderHexagonDYLD *>(baton);314 315  // if the dyld_instance is still not valid then try to locate it on the316  // symbol table317  if (!dyld_instance->m_rendezvous.IsValid()) {318    Process *proc = dyld_instance->m_process;319 320    const ConstString dyldStructName("_rtld_debug");321    addr_t structAddr = findSymbolAddress(proc, dyldStructName);322 323    if (structAddr != LLDB_INVALID_ADDRESS) {324      dyld_instance->m_rendezvous.SetRendezvousAddress(structAddr);325 326      LLDB_LOGF(log, "Found _rtld_debug structure @ 0x%08" PRIx64, structAddr);327    } else {328      LLDB_LOGF(log, "Unable to resolve the _rtld_debug structure");329    }330  }331 332  dyld_instance->RefreshModules();333 334  // Return true to stop the target, false to just let the target run.335  return dyld_instance->GetStopWhenImagesChange();336}337 338/// Helper method for RendezvousBreakpointHit.  Updates LLDB's current set339/// of loaded modules.340void DynamicLoaderHexagonDYLD::RefreshModules() {341  Log *log = GetLog(LLDBLog::DynamicLoader);342 343  if (!m_rendezvous.Resolve())344    return;345 346  HexagonDYLDRendezvous::iterator I;347  HexagonDYLDRendezvous::iterator E;348 349  ModuleList &loaded_modules = m_process->GetTarget().GetImages();350 351  if (m_rendezvous.ModulesDidLoad()) {352    ModuleList new_modules;353 354    E = m_rendezvous.loaded_end();355    for (I = m_rendezvous.loaded_begin(); I != E; ++I) {356      FileSpec file(I->path);357      FileSystem::Instance().Resolve(file);358      ModuleSP module_sp =359          LoadModuleAtAddress(file, I->link_addr, I->base_addr, true);360      if (module_sp.get()) {361        loaded_modules.AppendIfNeeded(module_sp);362        new_modules.Append(module_sp);363      }364 365      if (log) {366        LLDB_LOGF(log, "Target is loading '%s'", I->path.c_str());367        if (!module_sp.get())368          LLDB_LOGF(log, "LLDB failed to load '%s'", I->path.c_str());369        else370          LLDB_LOGF(log, "LLDB successfully loaded '%s'", I->path.c_str());371      }372    }373    m_process->GetTarget().ModulesDidLoad(new_modules);374  }375 376  if (m_rendezvous.ModulesDidUnload()) {377    ModuleList old_modules;378 379    E = m_rendezvous.unloaded_end();380    for (I = m_rendezvous.unloaded_begin(); I != E; ++I) {381      FileSpec file(I->path);382      FileSystem::Instance().Resolve(file);383      ModuleSpec module_spec(file);384      ModuleSP module_sp = loaded_modules.FindFirstModule(module_spec);385 386      if (module_sp.get()) {387        old_modules.Append(module_sp);388        UnloadSections(module_sp);389      }390 391      LLDB_LOGF(log, "Target is unloading '%s'", I->path.c_str());392    }393    loaded_modules.Remove(old_modules);394    m_process->GetTarget().ModulesDidUnload(old_modules, false);395  }396}397 398// AD:	This is very different to the Static Loader code.399//		It may be wise to look over this and its relation to stack400//		unwinding.401ThreadPlanSP402DynamicLoaderHexagonDYLD::GetStepThroughTrampolinePlan(Thread &thread,403                                                       bool stop) {404  ThreadPlanSP thread_plan_sp;405 406  StackFrame *frame = thread.GetStackFrameAtIndex(0).get();407  const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol);408  Symbol *sym = context.symbol;409 410  if (sym == nullptr || !sym->IsTrampoline())411    return thread_plan_sp;412 413  const ConstString sym_name =414      sym->GetMangled().GetName(Mangled::ePreferMangled);415  if (!sym_name)416    return thread_plan_sp;417 418  SymbolContextList target_symbols;419  Target &target = thread.GetProcess()->GetTarget();420  const ModuleList &images = target.GetImages();421 422  images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols);423  if (target_symbols.GetSize() == 0)424    return thread_plan_sp;425 426  typedef std::vector<lldb::addr_t> AddressVector;427  AddressVector addrs;428  for (const SymbolContext &context : target_symbols) {429    addr_t addr = context.GetFunctionOrSymbolAddress().GetLoadAddress(&target);430    if (addr != LLDB_INVALID_ADDRESS)431      addrs.push_back(addr);432  }433 434  if (addrs.size() > 0) {435    AddressVector::iterator start = addrs.begin();436    AddressVector::iterator end = addrs.end();437 438    llvm::sort(start, end);439    addrs.erase(std::unique(start, end), end);440    thread_plan_sp =441        std::make_shared<ThreadPlanRunToAddress>(thread, addrs, stop);442  }443 444  return thread_plan_sp;445}446 447/// Helper for the entry breakpoint callback.  Resolves the load addresses448/// of all dependent modules.449void DynamicLoaderHexagonDYLD::LoadAllCurrentModules() {450  HexagonDYLDRendezvous::iterator I;451  HexagonDYLDRendezvous::iterator E;452  ModuleList module_list;453 454  if (!m_rendezvous.Resolve()) {455    Log *log = GetLog(LLDBLog::DynamicLoader);456    LLDB_LOGF(457        log,458        "DynamicLoaderHexagonDYLD::%s unable to resolve rendezvous address",459        __FUNCTION__);460    return;461  }462 463  // The rendezvous class doesn't enumerate the main module, so track that464  // ourselves here.465  ModuleSP executable = GetTargetExecutable();466  m_loaded_modules[executable] = m_rendezvous.GetLinkMapAddress();467 468  for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) {469    const char *module_path = I->path.c_str();470    FileSpec file(module_path);471    ModuleSP module_sp =472        LoadModuleAtAddress(file, I->link_addr, I->base_addr, true);473    if (module_sp.get()) {474      module_list.Append(module_sp);475    } else {476      Log *log = GetLog(LLDBLog::DynamicLoader);477      LLDB_LOGF(log,478                "DynamicLoaderHexagonDYLD::%s failed loading module %s at "479                "0x%" PRIx64,480                __FUNCTION__, module_path, I->base_addr);481    }482  }483 484  m_process->GetTarget().ModulesDidLoad(module_list);485}486 487/// Computes a value for m_load_offset returning the computed address on488/// success and LLDB_INVALID_ADDRESS on failure.489addr_t DynamicLoaderHexagonDYLD::ComputeLoadOffset() {490  // Here we could send a GDB packet to know the load offset491  //492  // send:    $qOffsets#4b493  // get:     Text=0;Data=0;Bss=0494  //495  // Currently qOffsets is not supported by pluginProcessGDBRemote496  //497  return 0;498}499 500// Here we must try to read the entry point directly from the elf header.  This501// is possible if the process is not relocatable or dynamically linked.502//503// an alternative is to look at the PC if we can be sure that we have connected504// when the process is at the entry point.505// I dont think that is reliable for us.506addr_t DynamicLoaderHexagonDYLD::GetEntryPoint() {507  if (m_entry_point != LLDB_INVALID_ADDRESS)508    return m_entry_point;509  // check we have a valid process510  if (m_process == nullptr)511    return LLDB_INVALID_ADDRESS;512  // Get the current executable module513  Module &module = *(m_process->GetTarget().GetExecutableModule().get());514  // Get the object file (elf file) for this module515  lldb_private::ObjectFile &object = *(module.GetObjectFile());516  // Check if the file is executable (ie, not shared object or relocatable)517  if (object.IsExecutable()) {518    // Get the entry point address for this object519    lldb_private::Address entry = object.GetEntryPointAddress();520    // Return the entry point address521    return entry.GetFileAddress();522  }523  // No idea so back out524  return LLDB_INVALID_ADDRESS;525}526 527const SectionList *DynamicLoaderHexagonDYLD::GetSectionListFromModule(528    const ModuleSP module) const {529  SectionList *sections = nullptr;530  if (module.get()) {531    ObjectFile *obj_file = module->GetObjectFile();532    if (obj_file) {533      sections = obj_file->GetSectionList();534    }535  }536  return sections;537}538 539static int ReadInt(Process *process, addr_t addr) {540  Status error;541  int value = (int)process->ReadUnsignedIntegerFromMemory(542      addr, sizeof(uint32_t), 0, error);543  if (error.Fail())544    return -1;545  else546    return value;547}548 549lldb::addr_t550DynamicLoaderHexagonDYLD::GetThreadLocalData(const lldb::ModuleSP module,551                                             const lldb::ThreadSP thread,552                                             lldb::addr_t tls_file_addr) {553  auto it = m_loaded_modules.find(module);554  if (it == m_loaded_modules.end())555    return LLDB_INVALID_ADDRESS;556 557  addr_t link_map = it->second;558  if (link_map == LLDB_INVALID_ADDRESS)559    return LLDB_INVALID_ADDRESS;560 561  const HexagonDYLDRendezvous::ThreadInfo &metadata =562      m_rendezvous.GetThreadInfo();563  if (!metadata.valid)564    return LLDB_INVALID_ADDRESS;565 566  // Get the thread pointer.567  addr_t tp = thread->GetThreadPointer();568  if (tp == LLDB_INVALID_ADDRESS)569    return LLDB_INVALID_ADDRESS;570 571  // Find the module's modid.572  int modid = ReadInt(m_process, link_map + metadata.modid_offset);573  if (modid == -1)574    return LLDB_INVALID_ADDRESS;575 576  // Lookup the DTV structure for this thread.577  addr_t dtv_ptr = tp + metadata.dtv_offset;578  addr_t dtv = ReadPointer(dtv_ptr);579  if (dtv == LLDB_INVALID_ADDRESS)580    return LLDB_INVALID_ADDRESS;581 582  // Find the TLS block for this module.583  addr_t dtv_slot = dtv + metadata.dtv_slot_size * modid;584  addr_t tls_block = ReadPointer(dtv_slot + metadata.tls_offset);585 586  Module *mod = module.get();587  Log *log = GetLog(LLDBLog::DynamicLoader);588  LLDB_LOGF(log,589            "DynamicLoaderHexagonDYLD::Performed TLS lookup: "590            "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64591            ", modid=%i, tls_block=0x%" PRIx64,592            mod->GetObjectName().AsCString(""), link_map, tp, modid, tls_block);593 594  if (tls_block == LLDB_INVALID_ADDRESS)595    return LLDB_INVALID_ADDRESS;596  else597    return tls_block + tls_file_addr;598}599