mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:07:46 +00:00
LibELF: Use the first PT_LOAD
element to calculate base address
Other element types (like `PT_RISCV_ATTRIBUTES`) might not have a correct `p_vaddr`.
This commit is contained in:
parent
a95c2ed978
commit
a65d6e5e50
1 changed files with 15 additions and 2 deletions
|
@ -22,8 +22,21 @@ DynamicObject::DynamicObject(DeprecatedString const& filepath, VirtualAddress ba
|
||||||
, m_dynamic_address(dynamic_section_address)
|
, m_dynamic_address(dynamic_section_address)
|
||||||
{
|
{
|
||||||
auto* header = (ElfW(Ehdr)*)base_address.as_ptr();
|
auto* header = (ElfW(Ehdr)*)base_address.as_ptr();
|
||||||
auto* pheader = (ElfW(Phdr)*)(base_address.as_ptr() + header->e_phoff);
|
auto* const phdrs = program_headers();
|
||||||
m_elf_base_address = VirtualAddress(pheader->p_vaddr - pheader->p_offset);
|
|
||||||
|
// Calculate the base address using the PT_LOAD element with the lowest `p_vaddr` (which is the first element)
|
||||||
|
for (size_t i = 0; i < program_header_count(); ++i) {
|
||||||
|
auto pheader = phdrs[i];
|
||||||
|
if (pheader.p_type == PT_LOAD) {
|
||||||
|
m_elf_base_address = VirtualAddress { pheader.p_vaddr - pheader.p_offset };
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == program_header_count() - 1) {
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (header->e_type == ET_DYN)
|
if (header->e_type == ET_DYN)
|
||||||
m_is_elf_dynamic = true;
|
m_is_elf_dynamic = true;
|
||||||
else
|
else
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue