1 //===-- DYLDRendezvous.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Module.h"
11 #include "lldb/Symbol/ObjectFile.h"
12 #include "lldb/Symbol/Symbol.h"
13 #include "lldb/Symbol/SymbolContext.h"
14 #include "lldb/Target/Platform.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Utility/ArchSpec.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/Status.h"
21 #include "llvm/Support/Path.h"
23 #include "DYLDRendezvous.h"
26 using namespace lldb_private;
28 /// Locates the address of the rendezvous structure. Returns the address on
29 /// success and LLDB_INVALID_ADDRESS on failure.
30 static addr_t ResolveRendezvousAddress(Process *process) {
31 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
38 log->Printf("%s null process provided", __FUNCTION__);
39 return LLDB_INVALID_ADDRESS;
42 // Try to get it from our process. This might be a remote process and might
43 // grab it via some remote-specific mechanism.
44 info_location = process->GetImageInfoAddress();
46 log->Printf("%s info_location = 0x%" PRIx64, __FUNCTION__, info_location);
48 // If the process fails to return an address, fall back to seeing if the
49 // local object file can help us find it.
50 if (info_location == LLDB_INVALID_ADDRESS) {
51 Target *target = &process->GetTarget();
53 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
54 Address addr = obj_file->GetImageInfoAddress(target);
57 info_location = addr.GetLoadAddress(target);
60 "%s resolved via direct object file approach to 0x%" PRIx64,
61 __FUNCTION__, info_location);
64 log->Printf("%s FAILED - direct object file approach did not yield a "
71 if (info_location == LLDB_INVALID_ADDRESS) {
73 log->Printf("%s FAILED - invalid info address", __FUNCTION__);
74 return LLDB_INVALID_ADDRESS;
78 log->Printf("%s reading pointer (%" PRIu32 " bytes) from 0x%" PRIx64,
79 __FUNCTION__, process->GetAddressByteSize(), info_location);
81 info_addr = process->ReadPointerFromMemory(info_location, error);
84 log->Printf("%s FAILED - could not read from the info location: %s",
85 __FUNCTION__, error.AsCString());
86 return LLDB_INVALID_ADDRESS;
91 log->Printf("%s FAILED - the rendezvous address contained at 0x%" PRIx64
92 " returned a null value",
93 __FUNCTION__, info_location);
94 return LLDB_INVALID_ADDRESS;
100 DYLDRendezvous::DYLDRendezvous(Process *process)
101 : m_process(process), m_rendezvous_addr(LLDB_INVALID_ADDRESS), m_current(),
102 m_previous(), m_loaded_modules(), m_soentries(), m_added_soentries(),
103 m_removed_soentries() {
104 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
106 m_thread_info.valid = false;
108 // Cache a copy of the executable path
110 Module *exe_mod = m_process->GetTarget().GetExecutableModulePointer();
112 m_exe_file_spec = exe_mod->GetPlatformFileSpec();
114 log->Printf("DYLDRendezvous::%s exe module executable path set: '%s'",
115 __FUNCTION__, m_exe_file_spec.GetCString());
118 log->Printf("DYLDRendezvous::%s cannot cache exe module path: null "
119 "executable module pointer",
125 bool DYLDRendezvous::Resolve() {
126 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
128 const size_t word_size = 4;
135 address_size = m_process->GetAddressByteSize();
136 padding = address_size - word_size;
138 log->Printf("DYLDRendezvous::%s address size: %" PRIu64
139 ", padding %" PRIu64,
140 __FUNCTION__, uint64_t(address_size), uint64_t(padding));
142 if (m_rendezvous_addr == LLDB_INVALID_ADDRESS)
143 cursor = info_addr = ResolveRendezvousAddress(m_process);
145 cursor = info_addr = m_rendezvous_addr;
147 log->Printf("DYLDRendezvous::%s cursor = 0x%" PRIx64, __FUNCTION__, cursor);
149 if (cursor == LLDB_INVALID_ADDRESS)
152 if (!(cursor = ReadWord(cursor, &info.version, word_size)))
155 if (!(cursor = ReadPointer(cursor + padding, &info.map_addr)))
158 if (!(cursor = ReadPointer(cursor, &info.brk)))
161 if (!(cursor = ReadWord(cursor, &info.state, word_size)))
164 if (!(cursor = ReadPointer(cursor + padding, &info.ldbase)))
167 // The rendezvous was successfully read. Update our internal state.
168 m_rendezvous_addr = info_addr;
169 m_previous = m_current;
172 if (UpdateSOEntries(true))
175 return UpdateSOEntries();
178 bool DYLDRendezvous::IsValid() {
179 return m_rendezvous_addr != LLDB_INVALID_ADDRESS;
182 bool DYLDRendezvous::UpdateSOEntries(bool fromRemote) {
184 LoadedModuleInfoList module_list;
186 // If we can't get the SO info from the remote, return failure.
187 if (fromRemote && m_process->LoadModules(module_list) == 0)
190 if (!fromRemote && m_current.map_addr == 0)
193 // When the previous and current states are consistent this is the first time
194 // we have been asked to update. Just take a snapshot of the currently
196 if (m_previous.state == eConsistent && m_current.state == eConsistent)
197 return fromRemote ? SaveSOEntriesFromRemote(module_list)
198 : TakeSnapshot(m_soentries);
200 // If we are about to add or remove a shared object clear out the current
201 // state and take a snapshot of the currently loaded images.
202 if (m_current.state == eAdd || m_current.state == eDelete) {
203 // Some versions of the android dynamic linker might send two notifications
204 // with state == eAdd back to back. Ignore them until we get an eConsistent
206 if (!(m_previous.state == eConsistent ||
207 (m_previous.state == eAdd && m_current.state == eDelete)))
212 return SaveSOEntriesFromRemote(module_list);
214 m_added_soentries.clear();
215 m_removed_soentries.clear();
216 return TakeSnapshot(m_soentries);
218 assert(m_current.state == eConsistent);
220 // Otherwise check the previous state to determine what to expect and update
222 if (m_previous.state == eAdd)
223 return fromRemote ? AddSOEntriesFromRemote(module_list) : AddSOEntries();
224 else if (m_previous.state == eDelete)
225 return fromRemote ? RemoveSOEntriesFromRemote(module_list)
231 bool DYLDRendezvous::FillSOEntryFromModuleInfo(
232 LoadedModuleInfoList::LoadedModuleInfo const &modInfo, SOEntry &entry) {
233 addr_t link_map_addr;
238 if (!modInfo.get_link_map(link_map_addr) || !modInfo.get_base(base_addr) ||
239 !modInfo.get_dynamic(dyn_addr) || !modInfo.get_name(name))
242 entry.link_addr = link_map_addr;
243 entry.base_addr = base_addr;
244 entry.dyn_addr = dyn_addr;
246 entry.file_spec.SetFile(name, false, FileSpec::Style::native);
248 UpdateBaseAddrIfNecessary(entry, name);
250 // not needed if we're using ModuleInfos
258 bool DYLDRendezvous::SaveSOEntriesFromRemote(
259 LoadedModuleInfoList &module_list) {
260 for (auto const &modInfo : module_list.m_list) {
262 if (!FillSOEntryFromModuleInfo(modInfo, entry))
265 // Only add shared libraries and not the executable.
266 if (!SOEntryIsMainExecutable(entry))
267 m_soentries.push_back(entry);
270 m_loaded_modules = module_list;
274 bool DYLDRendezvous::AddSOEntriesFromRemote(LoadedModuleInfoList &module_list) {
275 for (auto const &modInfo : module_list.m_list) {
277 for (auto const &existing : m_loaded_modules.m_list) {
278 if (modInfo == existing) {
288 if (!FillSOEntryFromModuleInfo(modInfo, entry))
291 // Only add shared libraries and not the executable.
292 if (!SOEntryIsMainExecutable(entry))
293 m_soentries.push_back(entry);
296 m_loaded_modules = module_list;
300 bool DYLDRendezvous::RemoveSOEntriesFromRemote(
301 LoadedModuleInfoList &module_list) {
302 for (auto const &existing : m_loaded_modules.m_list) {
304 for (auto const &modInfo : module_list.m_list) {
305 if (modInfo == existing) {
315 if (!FillSOEntryFromModuleInfo(existing, entry))
318 // Only add shared libraries and not the executable.
319 if (!SOEntryIsMainExecutable(entry)) {
320 auto pos = std::find(m_soentries.begin(), m_soentries.end(), entry);
321 if (pos == m_soentries.end())
324 m_soentries.erase(pos);
328 m_loaded_modules = module_list;
332 bool DYLDRendezvous::AddSOEntries() {
336 assert(m_previous.state == eAdd);
338 if (m_current.map_addr == 0)
341 for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next) {
342 if (!ReadSOEntryFromMemory(cursor, entry))
345 // Only add shared libraries and not the executable.
346 if (SOEntryIsMainExecutable(entry))
349 pos = std::find(m_soentries.begin(), m_soentries.end(), entry);
350 if (pos == m_soentries.end()) {
351 m_soentries.push_back(entry);
352 m_added_soentries.push_back(entry);
359 bool DYLDRendezvous::RemoveSOEntries() {
360 SOEntryList entry_list;
363 assert(m_previous.state == eDelete);
365 if (!TakeSnapshot(entry_list))
368 for (iterator I = begin(); I != end(); ++I) {
369 pos = std::find(entry_list.begin(), entry_list.end(), *I);
370 if (pos == entry_list.end())
371 m_removed_soentries.push_back(*I);
374 m_soentries = entry_list;
378 bool DYLDRendezvous::SOEntryIsMainExecutable(const SOEntry &entry) {
379 // On some systes the executable is indicated by an empty path in the entry.
380 // On others it is the full path to the executable.
382 auto triple = m_process->GetTarget().GetArchitecture().GetTriple();
383 switch (triple.getOS()) {
384 case llvm::Triple::FreeBSD:
385 case llvm::Triple::NetBSD:
386 return entry.file_spec == m_exe_file_spec;
387 case llvm::Triple::Linux:
388 if (triple.isAndroid())
389 return entry.file_spec == m_exe_file_spec;
390 return !entry.file_spec;
396 bool DYLDRendezvous::TakeSnapshot(SOEntryList &entry_list) {
399 if (m_current.map_addr == 0)
402 // Clear previous entries since we are about to obtain an up to date list.
405 for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next) {
406 if (!ReadSOEntryFromMemory(cursor, entry))
409 // Only add shared libraries and not the executable.
410 if (SOEntryIsMainExecutable(entry))
413 entry_list.push_back(entry);
419 addr_t DYLDRendezvous::ReadWord(addr_t addr, uint64_t *dst, size_t size) {
422 *dst = m_process->ReadUnsignedIntegerFromMemory(addr, size, 0, error);
429 addr_t DYLDRendezvous::ReadPointer(addr_t addr, addr_t *dst) {
432 *dst = m_process->ReadPointerFromMemory(addr, error);
436 return addr + m_process->GetAddressByteSize();
439 std::string DYLDRendezvous::ReadStringFromMemory(addr_t addr) {
443 if (addr == LLDB_INVALID_ADDRESS)
444 return std::string();
446 m_process->ReadCStringFromMemory(addr, str, error);
451 // Returns true if the load bias reported by the linker is incorrect for the
452 // given entry. This function is used to handle cases where we want to work
453 // around a bug in the system linker.
454 static bool isLoadBiasIncorrect(Target &target, const std::string &file_path) {
455 // On Android L (API 21, 22) the load address of the "/system/bin/linker"
456 // isn't filled in correctly.
457 unsigned os_major = target.GetPlatform()->GetOSVersion().getMajor();
458 if (target.GetArchitecture().GetTriple().isAndroid() &&
459 (os_major == 21 || os_major == 22) &&
460 (file_path == "/system/bin/linker" ||
461 file_path == "/system/bin/linker64")) {
468 void DYLDRendezvous::UpdateBaseAddrIfNecessary(SOEntry &entry,
469 std::string const &file_path) {
470 // If the load bias reported by the linker is incorrect then fetch the load
471 // address of the file from the proc file system.
472 if (isLoadBiasIncorrect(m_process->GetTarget(), file_path)) {
473 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
474 bool is_loaded = false;
476 m_process->GetFileLoadAddress(entry.file_spec, is_loaded, load_addr);
477 if (error.Success() && is_loaded)
478 entry.base_addr = load_addr;
482 bool DYLDRendezvous::ReadSOEntryFromMemory(lldb::addr_t addr, SOEntry &entry) {
485 entry.link_addr = addr;
487 if (!(addr = ReadPointer(addr, &entry.base_addr)))
490 // mips adds an extra load offset field to the link map struct on FreeBSD and
491 // NetBSD (need to validate other OSes).
492 // http://svnweb.freebsd.org/base/head/sys/sys/link_elf.h?revision=217153&view=markup#l57
493 const ArchSpec &arch = m_process->GetTarget().GetArchitecture();
494 if ((arch.GetTriple().getOS() == llvm::Triple::FreeBSD ||
495 arch.GetTriple().getOS() == llvm::Triple::NetBSD) &&
496 (arch.GetMachine() == llvm::Triple::mips ||
497 arch.GetMachine() == llvm::Triple::mipsel ||
498 arch.GetMachine() == llvm::Triple::mips64 ||
499 arch.GetMachine() == llvm::Triple::mips64el)) {
501 if (!(addr = ReadPointer(addr, &mips_l_offs)))
503 if (mips_l_offs != 0 && mips_l_offs != entry.base_addr)
507 if (!(addr = ReadPointer(addr, &entry.path_addr)))
510 if (!(addr = ReadPointer(addr, &entry.dyn_addr)))
513 if (!(addr = ReadPointer(addr, &entry.next)))
516 if (!(addr = ReadPointer(addr, &entry.prev)))
519 std::string file_path = ReadStringFromMemory(entry.path_addr);
520 entry.file_spec.SetFile(file_path, false, FileSpec::Style::native);
522 UpdateBaseAddrIfNecessary(entry, file_path);
527 bool DYLDRendezvous::FindMetadata(const char *name, PThreadField field,
529 Target &target = m_process->GetTarget();
531 SymbolContextList list;
532 if (!target.GetImages().FindSymbolsWithNameAndType(ConstString(name),
533 eSymbolTypeAny, list))
536 Address address = list[0].symbol->GetAddress();
537 addr_t addr = address.GetLoadAddress(&target);
538 if (addr == LLDB_INVALID_ADDRESS)
542 value = (uint32_t)m_process->ReadUnsignedIntegerFromMemory(
543 addr + field * sizeof(uint32_t), sizeof(uint32_t), 0, error);
548 value /= 8; // convert bits to bytes
553 const DYLDRendezvous::ThreadInfo &DYLDRendezvous::GetThreadInfo() {
554 if (!m_thread_info.valid) {
557 ok &= FindMetadata("_thread_db_pthread_dtvp", eOffset,
558 m_thread_info.dtv_offset);
560 FindMetadata("_thread_db_dtv_dtv", eSize, m_thread_info.dtv_slot_size);
561 ok &= FindMetadata("_thread_db_link_map_l_tls_modid", eOffset,
562 m_thread_info.modid_offset);
563 ok &= FindMetadata("_thread_db_dtv_t_pointer_val", eOffset,
564 m_thread_info.tls_offset);
567 m_thread_info.valid = true;
570 return m_thread_info;
573 void DYLDRendezvous::DumpToLog(Log *log) const {
574 int state = GetState();
579 log->PutCString("DYLDRendezvous:");
580 log->Printf(" Address: %" PRIx64, GetRendezvousAddress());
581 log->Printf(" Version: %" PRIu64, GetVersion());
582 log->Printf(" Link : %" PRIx64, GetLinkMapAddress());
583 log->Printf(" Break : %" PRIx64, GetBreakAddress());
584 log->Printf(" LDBase : %" PRIx64, GetLDBase());
585 log->Printf(" State : %s",
586 (state == eConsistent)
588 : (state == eAdd) ? "add" : (state == eDelete) ? "delete"
591 iterator I = begin();
595 log->PutCString("DYLDRendezvous SOEntries:");
597 for (int i = 1; I != E; ++I, ++i) {
598 log->Printf("\n SOEntry [%d] %s", i, I->file_spec.GetCString());
599 log->Printf(" Base : %" PRIx64, I->base_addr);
600 log->Printf(" Path : %" PRIx64, I->path_addr);
601 log->Printf(" Dyn : %" PRIx64, I->dyn_addr);
602 log->Printf(" Next : %" PRIx64, I->next);
603 log->Printf(" Prev : %" PRIx64, I->prev);