Skip to content

Commit de395d3

Browse files
Merge pull request #8849 from augusto2112/proc-address-lldb-mem-reader
[lldb] Calculate process address if file address + section fails
2 parents 7ca683c + 1b147a2 commit de395d3

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/LLDBMemoryReader.cpp

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,18 +619,54 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
619619
return {};
620620

621621
Address resolved(file_address, object_file->GetSectionList());
622-
if (!resolved.IsValid()) {
622+
623+
// If the address doesn't have a section it means we couldn't find a section
624+
// that contains that file address, and the "resolved" instance is wrong.
625+
// Calculate the virtual address by finding out the slide of the associated
626+
// module, and adding that to the file address.
627+
if (resolved.GetSection()) {
628+
LLDB_LOGV(log,
629+
"[MemoryReader] Successfully resolved mapped address {0:x} into "
630+
"file address {1:x}",
631+
address, resolved.GetFileAddress());
632+
return resolved;
633+
}
634+
auto *sec_list = module->GetSectionList();
635+
if (sec_list->GetSize() == 0) {
623636
LLDB_LOG(log,
624-
"[MemoryReader] Could not make a real address out of file address "
625-
"{0:x} and object file {1}",
637+
"[MemoryReader] Could not calculate virtual address from file "
638+
"address {0:x}, no sections in {1}",
626639
file_address, object_file->GetFileSpec().GetFilename());
627640
return {};
628641
}
642+
auto sec = sec_list->GetSectionAtIndex(0);
643+
auto sec_file_address = sec->GetFileAddress();
644+
auto sec_load_address = sec->GetLoadBaseAddress(&m_process.GetTarget());
629645

646+
bool overflow = false;
647+
auto slide =
648+
llvm::SaturatingAdd(sec_load_address, -sec_file_address, &overflow);
649+
if (overflow) {
650+
LLDB_LOG(log,
651+
"[MemoryReader] section load address {0:x} - file address {1:x} "
652+
"overflows",
653+
sec_load_address, sec_file_address);
654+
return {};
655+
}
656+
657+
auto virtual_address = llvm::SaturatingAdd(file_address, slide, &overflow);
658+
if (overflow) {
659+
LLDB_LOG(log, "[MemoryReader] file address {0:x} + slide {1:x} overflows",
660+
sec_load_address, sec_file_address);
661+
return {};
662+
}
663+
664+
resolved = Address(virtual_address);
630665
LLDB_LOGV(log,
631-
"[MemoryReader] Successfully resolved mapped address {0:x} into "
632-
"file address {1:x}",
633-
address, resolved.GetFileAddress());
666+
"[MemoryReader] Could not find section with file address {0:x} "
667+
"and file {1}, resolved it into virtual address {2:x}",
668+
file_address, object_file->GetFileSpec().GetFilename(),
669+
virtual_address);
634670
return resolved;
635671
}
636672

0 commit comments

Comments
 (0)