ldso: don't reclaim zero-memory-sized segments

Some weird linkers may emit PT_LOAD segments with memsz = 0. ELF
specification does not forbid this, but such a segment with non-zero
p_vaddr will result in reclaiming of invalid memory address.

This patch skips such segments during reclaiming for better
compatibility.
This commit is contained in:
Yao Zi 2024-11-18 03:44:47 +00:00 committed by Rich Felker
parent 1a98576401
commit 5ccf05d86d

View file

@ -616,6 +616,7 @@ static void reclaim_gaps(struct dso *dso)
for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) {
if (ph->p_type!=PT_LOAD) continue;
if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
if (ph->p_memsz == 0) continue;
reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
reclaim(dso, ph->p_vaddr+ph->p_memsz,
ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);