Skip to content

Commit

Permalink
[lldb] Correct address calculation for reading segment data (#120655)
Browse files Browse the repository at this point in the history
This commit addresses a bug introduced in commit bcf654c, which
prevented LLDB from parsing the GNU build ID for the main executable
from a core file. The fix finds the `p_vaddr` of the first `PT_LOAD`
segment as the `base_addr` and subtract this `base_addr` from the
virtual address being read.

Co-authored-by: George Hu <[email protected]>
  • Loading branch information
GeorgeHuyubo and George Hu authored Jan 7, 2025
1 parent 6c3c90b commit a15fedc
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,8 @@ UUID ProcessElfCore::FindBuidIdInCoreMemory(lldb::addr_t address) {

std::vector<uint8_t> ph_bytes;
ph_bytes.resize(elf_header.e_phentsize);
lldb::addr_t base_addr = 0;
bool found_first_load_segment = false;
for (unsigned int i = 0; i < elf_header.e_phnum; ++i) {
byte_read = ReadMemory(ph_addr + i * elf_header.e_phentsize,
ph_bytes.data(), elf_header.e_phentsize, error);
Expand All @@ -1041,6 +1043,11 @@ UUID ProcessElfCore::FindBuidIdInCoreMemory(lldb::addr_t address) {
offset = 0;
elf::ELFProgramHeader program_header;
program_header.Parse(program_header_data, &offset);
if (program_header.p_type == llvm::ELF::PT_LOAD &&
!found_first_load_segment) {
base_addr = program_header.p_vaddr;
found_first_load_segment = true;
}
if (program_header.p_type != llvm::ELF::PT_NOTE)
continue;

Expand All @@ -1049,7 +1056,7 @@ UUID ProcessElfCore::FindBuidIdInCoreMemory(lldb::addr_t address) {

// We need to slide the address of the p_vaddr as these values don't get
// relocated in memory.
const lldb::addr_t vaddr = program_header.p_vaddr + address;
const lldb::addr_t vaddr = program_header.p_vaddr + address - base_addr;
byte_read =
ReadMemory(vaddr, note_bytes.data(), program_header.p_memsz, error);
if (byte_read != program_header.p_memsz)
Expand Down

0 comments on commit a15fedc

Please sign in to comment.