reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487
  488
  489
  490
  491
  492
  493
  494
  495
  496
  497
  498
  499
  500
  501
  502
  503
  504
  505
  506
  507
  508
  509
  510
  511
  512
  513
  514
  515
  516
  517
  518
  519
  520
  521
  522
  523
  524
  525
  526
  527
  528
  529
  530
  531
  532
  533
  534
  535
  536
  537
  538
  539
  540
  541
  542
  543
  544
  545
  546
  547
  548
  549
  550
  551
  552
  553
  554
  555
  556
  557
  558
  559
  560
  561
  562
  563
  564
  565
  566
  567
  568
  569
  570
  571
  572
  573
  574
  575
  576
  577
  578
  579
  580
  581
  582
  583
  584
  585
  586
  587
  588
  589
  590
  591
  592
  593
  594
  595
  596
  597
  598
  599
  600
  601
  602
  603
  604
  605
  606
  607
  608
  609
  610
  611
  612
//===-- DynamicLoaderHexagonDYLD.cpp ----------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Breakpoint/BreakpointLocation.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Section.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/ThreadPlanRunToAddress.h"
#include "lldb/Utility/Log.h"

#include "DynamicLoaderHexagonDYLD.h"

#include <memory>

using namespace lldb;
using namespace lldb_private;

// Aidan 21/05/2014
//
// Notes about hexagon dynamic loading:
//
//      When we connect to a target we find the dyld breakpoint address.  We put
//      a
//      breakpoint there with a callback 'RendezvousBreakpointHit()'.
//
//      It is possible to find the dyld structure address from the ELF symbol
//      table,
//      but in the case of the simulator it has not been initialized before the
//      target calls dlinit().
//
//      We can only safely parse the dyld structure after we hit the dyld
//      breakpoint
//      since at that time we know dlinit() must have been called.
//

// Find the load address of a symbol
static lldb::addr_t findSymbolAddress(Process *proc, ConstString findName) {
  assert(proc != nullptr);

  ModuleSP module = proc->GetTarget().GetExecutableModule();
  assert(module.get() != nullptr);

  ObjectFile *exe = module->GetObjectFile();
  assert(exe != nullptr);

  lldb_private::Symtab *symtab = exe->GetSymtab();
  assert(symtab != nullptr);

  for (size_t i = 0; i < symtab->GetNumSymbols(); i++) {
    const Symbol *sym = symtab->SymbolAtIndex(i);
    assert(sym != nullptr);
    ConstString symName = sym->GetName();

    if (ConstString::Compare(findName, symName) == 0) {
      Address addr = sym->GetAddress();
      return addr.GetLoadAddress(&proc->GetTarget());
    }
  }
  return LLDB_INVALID_ADDRESS;
}

void DynamicLoaderHexagonDYLD::Initialize() {
  PluginManager::RegisterPlugin(GetPluginNameStatic(),
                                GetPluginDescriptionStatic(), CreateInstance);
}

void DynamicLoaderHexagonDYLD::Terminate() {}

lldb_private::ConstString DynamicLoaderHexagonDYLD::GetPluginName() {
  return GetPluginNameStatic();
}

lldb_private::ConstString DynamicLoaderHexagonDYLD::GetPluginNameStatic() {
  static ConstString g_name("hexagon-dyld");
  return g_name;
}

const char *DynamicLoaderHexagonDYLD::GetPluginDescriptionStatic() {
  return "Dynamic loader plug-in that watches for shared library "
         "loads/unloads in Hexagon processes.";
}

uint32_t DynamicLoaderHexagonDYLD::GetPluginVersion() { return 1; }

DynamicLoader *DynamicLoaderHexagonDYLD::CreateInstance(Process *process,
                                                        bool force) {
  bool create = force;
  if (!create) {
    const llvm::Triple &triple_ref =
        process->GetTarget().GetArchitecture().GetTriple();
    if (triple_ref.getArch() == llvm::Triple::hexagon)
      create = true;
  }

  if (create)
    return new DynamicLoaderHexagonDYLD(process);
  return nullptr;
}

DynamicLoaderHexagonDYLD::DynamicLoaderHexagonDYLD(Process *process)
    : DynamicLoader(process), m_rendezvous(process),
      m_load_offset(LLDB_INVALID_ADDRESS), m_entry_point(LLDB_INVALID_ADDRESS),
      m_dyld_bid(LLDB_INVALID_BREAK_ID) {}

DynamicLoaderHexagonDYLD::~DynamicLoaderHexagonDYLD() {
  if (m_dyld_bid != LLDB_INVALID_BREAK_ID) {
    m_process->GetTarget().RemoveBreakpointByID(m_dyld_bid);
    m_dyld_bid = LLDB_INVALID_BREAK_ID;
  }
}

void DynamicLoaderHexagonDYLD::DidAttach() {
  ModuleSP executable;
  addr_t load_offset;

  executable = GetTargetExecutable();

  // Find the difference between the desired load address in the elf file and
  // the real load address in memory
  load_offset = ComputeLoadOffset();

  // Check that there is a valid executable
  if (executable.get() == nullptr)
    return;

  // Disable JIT for hexagon targets because its not supported
  m_process->SetCanJIT(false);

  // Enable Interpreting of function call expressions
  m_process->SetCanInterpretFunctionCalls(true);

  // Add the current executable to the module list
  ModuleList module_list;
  module_list.Append(executable);

  // Map the loaded sections of this executable
  if (load_offset != LLDB_INVALID_ADDRESS)
    UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset, true);

  // AD: confirm this?
  // Load into LLDB all of the currently loaded executables in the stub
  LoadAllCurrentModules();

  // AD: confirm this?
  // Callback for the target to give it the loaded module list
  m_process->GetTarget().ModulesDidLoad(module_list);

  // Try to set a breakpoint at the rendezvous breakpoint. DidLaunch uses
  // ProbeEntry() instead.  That sets a breakpoint, at the dyld breakpoint
  // address, with a callback so that when hit, the dyld structure can be
  // parsed.
  if (!SetRendezvousBreakpoint()) {
    // fail
  }
}

void DynamicLoaderHexagonDYLD::DidLaunch() {}

/// Checks to see if the target module has changed, updates the target
/// accordingly and returns the target executable module.
ModuleSP DynamicLoaderHexagonDYLD::GetTargetExecutable() {
  Target &target = m_process->GetTarget();
  ModuleSP executable = target.GetExecutableModule();

  // There is no executable
  if (!executable.get())
    return executable;

  // The target executable file does not exits
  if (!FileSystem::Instance().Exists(executable->GetFileSpec()))
    return executable;

  // Prep module for loading
  ModuleSpec module_spec(executable->GetFileSpec(),
                         executable->GetArchitecture());
  ModuleSP module_sp(new Module(module_spec));

  // Check if the executable has changed and set it to the target executable if
  // they differ.
  if (module_sp.get() && module_sp->GetUUID().IsValid() &&
      executable->GetUUID().IsValid()) {
    // if the executable has changed ??
    if (module_sp->GetUUID() != executable->GetUUID())
      executable.reset();
  } else if (executable->FileHasChanged())
    executable.reset();

  if (executable.get())
    return executable;

  // TODO: What case is this code used?
  executable = target.GetOrCreateModule(module_spec, true /* notify */);
  if (executable.get() != target.GetExecutableModulePointer()) {
    // Don't load dependent images since we are in dyld where we will know and
    // find out about all images that are loaded
    target.SetExecutableModule(executable, eLoadDependentsNo);
  }

  return executable;
}

// AD: Needs to be updated?
Status DynamicLoaderHexagonDYLD::CanLoadImage() { return Status(); }

void DynamicLoaderHexagonDYLD::UpdateLoadedSections(ModuleSP module,
                                                    addr_t link_map_addr,
                                                    addr_t base_addr,
                                                    bool base_addr_is_offset) {
  Target &target = m_process->GetTarget();
  const SectionList *sections = GetSectionListFromModule(module);

  assert(sections && "SectionList missing from loaded module.");

  m_loaded_modules[module] = link_map_addr;

  const size_t num_sections = sections->GetSize();

  for (unsigned i = 0; i < num_sections; ++i) {
    SectionSP section_sp(sections->GetSectionAtIndex(i));
    lldb::addr_t new_load_addr = section_sp->GetFileAddress() + base_addr;

    // AD: 02/05/14
    //   since our memory map starts from address 0, we must not ignore
    //   sections that load to address 0.  This violates the reference
    //   ELF spec, however is used for Hexagon.

    // If the file address of the section is zero then this is not an
    // allocatable/loadable section (property of ELF sh_addr).  Skip it.
    //      if (new_load_addr == base_addr)
    //          continue;

    target.SetSectionLoadAddress(section_sp, new_load_addr);
  }
}

/// Removes the loaded sections from the target in \p module.
///
/// \param module The module to traverse.
void DynamicLoaderHexagonDYLD::UnloadSections(const ModuleSP module) {
  Target &target = m_process->GetTarget();
  const SectionList *sections = GetSectionListFromModule(module);

  assert(sections && "SectionList missing from unloaded module.");

  m_loaded_modules.erase(module);

  const size_t num_sections = sections->GetSize();
  for (size_t i = 0; i < num_sections; ++i) {
    SectionSP section_sp(sections->GetSectionAtIndex(i));
    target.SetSectionUnloaded(section_sp);
  }
}

// Place a breakpoint on <_rtld_debug_state>
bool DynamicLoaderHexagonDYLD::SetRendezvousBreakpoint() {
  Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));

  // This is the original code, which want to look in the rendezvous structure
  // to find the breakpoint address.  Its backwards for us, since we can easily
  // find the breakpoint address, since it is exported in our executable. We
  // however know that we cant read the Rendezvous structure until we have hit
  // the breakpoint once.
  const ConstString dyldBpName("_rtld_debug_state");
  addr_t break_addr = findSymbolAddress(m_process, dyldBpName);

  Target &target = m_process->GetTarget();

  // Do not try to set the breakpoint if we don't know where to put it
  if (break_addr == LLDB_INVALID_ADDRESS) {
    LLDB_LOGF(log, "Unable to locate _rtld_debug_state breakpoint address");

    return false;
  }

  // Save the address of the rendezvous structure
  m_rendezvous.SetBreakAddress(break_addr);

  // If we haven't set the breakpoint before then set it
  if (m_dyld_bid == LLDB_INVALID_BREAK_ID) {
    Breakpoint *dyld_break =
        target.CreateBreakpoint(break_addr, true, false).get();
    dyld_break->SetCallback(RendezvousBreakpointHit, this, true);
    dyld_break->SetBreakpointKind("shared-library-event");
    m_dyld_bid = dyld_break->GetID();

    // Make sure our breakpoint is at the right address.
    assert(target.GetBreakpointByID(m_dyld_bid)
               ->FindLocationByAddress(break_addr)
               ->GetBreakpoint()
               .GetID() == m_dyld_bid);

    if (log && dyld_break == nullptr)
      LLDB_LOGF(log, "Failed to create _rtld_debug_state breakpoint");

    // check we have successfully set bp
    return (dyld_break != nullptr);
  } else
    // rendezvous already set
    return true;
}

// We have just hit our breakpoint at <_rtld_debug_state>
bool DynamicLoaderHexagonDYLD::RendezvousBreakpointHit(
    void *baton, StoppointCallbackContext *context, user_id_t break_id,
    user_id_t break_loc_id) {
  Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));

  LLDB_LOGF(log, "Rendezvous breakpoint hit!");

  DynamicLoaderHexagonDYLD *dyld_instance = nullptr;
  dyld_instance = static_cast<DynamicLoaderHexagonDYLD *>(baton);

  // if the dyld_instance is still not valid then try to locate it on the
  // symbol table
  if (!dyld_instance->m_rendezvous.IsValid()) {
    Process *proc = dyld_instance->m_process;

    const ConstString dyldStructName("_rtld_debug");
    addr_t structAddr = findSymbolAddress(proc, dyldStructName);

    if (structAddr != LLDB_INVALID_ADDRESS) {
      dyld_instance->m_rendezvous.SetRendezvousAddress(structAddr);

      LLDB_LOGF(log, "Found _rtld_debug structure @ 0x%08" PRIx64, structAddr);
    } else {
      LLDB_LOGF(log, "Unable to resolve the _rtld_debug structure");
    }
  }

  dyld_instance->RefreshModules();

  // Return true to stop the target, false to just let the target run.
  return dyld_instance->GetStopWhenImagesChange();
}

/// Helper method for RendezvousBreakpointHit.  Updates LLDB's current set
/// of loaded modules.
void DynamicLoaderHexagonDYLD::RefreshModules() {
  Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));

  if (!m_rendezvous.Resolve())
    return;

  HexagonDYLDRendezvous::iterator I;
  HexagonDYLDRendezvous::iterator E;

  ModuleList &loaded_modules = m_process->GetTarget().GetImages();

  if (m_rendezvous.ModulesDidLoad()) {
    ModuleList new_modules;

    E = m_rendezvous.loaded_end();
    for (I = m_rendezvous.loaded_begin(); I != E; ++I) {
      FileSpec file(I->path);
      FileSystem::Instance().Resolve(file);
      ModuleSP module_sp =
          LoadModuleAtAddress(file, I->link_addr, I->base_addr, true);
      if (module_sp.get()) {
        loaded_modules.AppendIfNeeded(module_sp);
        new_modules.Append(module_sp);
      }

      if (log) {
        LLDB_LOGF(log, "Target is loading '%s'", I->path.c_str());
        if (!module_sp.get())
          LLDB_LOGF(log, "LLDB failed to load '%s'", I->path.c_str());
        else
          LLDB_LOGF(log, "LLDB successfully loaded '%s'", I->path.c_str());
      }
    }
    m_process->GetTarget().ModulesDidLoad(new_modules);
  }

  if (m_rendezvous.ModulesDidUnload()) {
    ModuleList old_modules;

    E = m_rendezvous.unloaded_end();
    for (I = m_rendezvous.unloaded_begin(); I != E; ++I) {
      FileSpec file(I->path);
      FileSystem::Instance().Resolve(file);
      ModuleSpec module_spec(file);
      ModuleSP module_sp = loaded_modules.FindFirstModule(module_spec);

      if (module_sp.get()) {
        old_modules.Append(module_sp);
        UnloadSections(module_sp);
      }

      LLDB_LOGF(log, "Target is unloading '%s'", I->path.c_str());
    }
    loaded_modules.Remove(old_modules);
    m_process->GetTarget().ModulesDidUnload(old_modules, false);
  }
}

// AD:	This is very different to the Static Loader code.
//		It may be wise to look over this and its relation to stack
//		unwinding.
ThreadPlanSP
DynamicLoaderHexagonDYLD::GetStepThroughTrampolinePlan(Thread &thread,
                                                       bool stop) {
  ThreadPlanSP thread_plan_sp;

  StackFrame *frame = thread.GetStackFrameAtIndex(0).get();
  const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol);
  Symbol *sym = context.symbol;

  if (sym == nullptr || !sym->IsTrampoline())
    return thread_plan_sp;

  const ConstString sym_name = sym->GetMangled().GetName(
      lldb::eLanguageTypeUnknown, Mangled::ePreferMangled);
  if (!sym_name)
    return thread_plan_sp;

  SymbolContextList target_symbols;
  Target &target = thread.GetProcess()->GetTarget();
  const ModuleList &images = target.GetImages();

  images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols);
  size_t num_targets = target_symbols.GetSize();
  if (!num_targets)
    return thread_plan_sp;

  typedef std::vector<lldb::addr_t> AddressVector;
  AddressVector addrs;
  for (size_t i = 0; i < num_targets; ++i) {
    SymbolContext context;
    AddressRange range;
    if (target_symbols.GetContextAtIndex(i, context)) {
      context.GetAddressRange(eSymbolContextEverything, 0, false, range);
      lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target);
      if (addr != LLDB_INVALID_ADDRESS)
        addrs.push_back(addr);
    }
  }

  if (addrs.size() > 0) {
    AddressVector::iterator start = addrs.begin();
    AddressVector::iterator end = addrs.end();

    llvm::sort(start, end);
    addrs.erase(std::unique(start, end), end);
    thread_plan_sp =
        std::make_shared<ThreadPlanRunToAddress>(thread, addrs, stop);
  }

  return thread_plan_sp;
}

/// Helper for the entry breakpoint callback.  Resolves the load addresses
/// of all dependent modules.
void DynamicLoaderHexagonDYLD::LoadAllCurrentModules() {
  HexagonDYLDRendezvous::iterator I;
  HexagonDYLDRendezvous::iterator E;
  ModuleList module_list;

  if (!m_rendezvous.Resolve()) {
    Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
    LLDB_LOGF(
        log,
        "DynamicLoaderHexagonDYLD::%s unable to resolve rendezvous address",
        __FUNCTION__);
    return;
  }

  // The rendezvous class doesn't enumerate the main module, so track that
  // ourselves here.
  ModuleSP executable = GetTargetExecutable();
  m_loaded_modules[executable] = m_rendezvous.GetLinkMapAddress();

  for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) {
    const char *module_path = I->path.c_str();
    FileSpec file(module_path);
    ModuleSP module_sp =
        LoadModuleAtAddress(file, I->link_addr, I->base_addr, true);
    if (module_sp.get()) {
      module_list.Append(module_sp);
    } else {
      Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
      LLDB_LOGF(log,
                "DynamicLoaderHexagonDYLD::%s failed loading module %s at "
                "0x%" PRIx64,
                __FUNCTION__, module_path, I->base_addr);
    }
  }

  m_process->GetTarget().ModulesDidLoad(module_list);
}

/// Computes a value for m_load_offset returning the computed address on
/// success and LLDB_INVALID_ADDRESS on failure.
addr_t DynamicLoaderHexagonDYLD::ComputeLoadOffset() {
  // Here we could send a GDB packet to know the load offset
  //
  // send:    $qOffsets#4b
  // get:     Text=0;Data=0;Bss=0
  //
  // Currently qOffsets is not supported by pluginProcessGDBRemote
  //
  return 0;
}

// Here we must try to read the entry point directly from the elf header.  This
// is possible if the process is not relocatable or dynamically linked.
//
// an alternative is to look at the PC if we can be sure that we have connected
// when the process is at the entry point.
// I dont think that is reliable for us.
addr_t DynamicLoaderHexagonDYLD::GetEntryPoint() {
  if (m_entry_point != LLDB_INVALID_ADDRESS)
    return m_entry_point;
  // check we have a valid process
  if (m_process == nullptr)
    return LLDB_INVALID_ADDRESS;
  // Get the current executable module
  Module &module = *(m_process->GetTarget().GetExecutableModule().get());
  // Get the object file (elf file) for this module
  lldb_private::ObjectFile &object = *(module.GetObjectFile());
  // Check if the file is executable (ie, not shared object or relocatable)
  if (object.IsExecutable()) {
    // Get the entry point address for this object
    lldb_private::Address entry = object.GetEntryPointAddress();
    // Return the entry point address
    return entry.GetFileAddress();
  }
  // No idea so back out
  return LLDB_INVALID_ADDRESS;
}

const SectionList *DynamicLoaderHexagonDYLD::GetSectionListFromModule(
    const ModuleSP module) const {
  SectionList *sections = nullptr;
  if (module.get()) {
    ObjectFile *obj_file = module->GetObjectFile();
    if (obj_file) {
      sections = obj_file->GetSectionList();
    }
  }
  return sections;
}

static int ReadInt(Process *process, addr_t addr) {
  Status error;
  int value = (int)process->ReadUnsignedIntegerFromMemory(
      addr, sizeof(uint32_t), 0, error);
  if (error.Fail())
    return -1;
  else
    return value;
}

lldb::addr_t
DynamicLoaderHexagonDYLD::GetThreadLocalData(const lldb::ModuleSP module,
                                             const lldb::ThreadSP thread,
                                             lldb::addr_t tls_file_addr) {
  auto it = m_loaded_modules.find(module);
  if (it == m_loaded_modules.end())
    return LLDB_INVALID_ADDRESS;

  addr_t link_map = it->second;
  if (link_map == LLDB_INVALID_ADDRESS)
    return LLDB_INVALID_ADDRESS;

  const HexagonDYLDRendezvous::ThreadInfo &metadata =
      m_rendezvous.GetThreadInfo();
  if (!metadata.valid)
    return LLDB_INVALID_ADDRESS;

  // Get the thread pointer.
  addr_t tp = thread->GetThreadPointer();
  if (tp == LLDB_INVALID_ADDRESS)
    return LLDB_INVALID_ADDRESS;

  // Find the module's modid.
  int modid = ReadInt(m_process, link_map + metadata.modid_offset);
  if (modid == -1)
    return LLDB_INVALID_ADDRESS;

  // Lookup the DTV structure for this thread.
  addr_t dtv_ptr = tp + metadata.dtv_offset;
  addr_t dtv = ReadPointer(dtv_ptr);
  if (dtv == LLDB_INVALID_ADDRESS)
    return LLDB_INVALID_ADDRESS;

  // Find the TLS block for this module.
  addr_t dtv_slot = dtv + metadata.dtv_slot_size * modid;
  addr_t tls_block = ReadPointer(dtv_slot + metadata.tls_offset);

  Module *mod = module.get();
  Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
  LLDB_LOGF(log,
            "DynamicLoaderHexagonDYLD::Performed TLS lookup: "
            "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64
            ", modid=%i, tls_block=0x%" PRIx64,
            mod->GetObjectName().AsCString(""), link_map, tp, modid, tls_block);

  if (tls_block == LLDB_INVALID_ADDRESS)
    return LLDB_INVALID_ADDRESS;
  else
    return tls_block + tls_file_addr;
}