Skip to content

Make get_php_base_addr more reliable - #152

Merged
adsr merged 1 commit into
masterfrom
addr-find-2
Apr 19, 2026
Merged

Make get_php_base_addr more reliable#152
adsr merged 1 commit into
masterfrom
addr-find-2

Conversation

@adsr

@adsr adsr commented Apr 19, 2026

Copy link
Copy Markdown
Owner

In order to calculate the base address of PHP's memory space, we were previously using the first PHP mapping in procfs_maps, which may not have been an executable range. Similarly we were using the first vaddr in ELF headers, which again may not have been an executable segment. In both cases we now look for the first entry with an "x" flag (executable).

For example, consider:

$ # procfs_maps
557a78c00000-557a78d5f000 r--p 00000000 fe:01 15623632                   /home/adam/php/bin/php
557a78e00000-557a7966d000 r-xp 00200000 fe:01 15623632                   /home/adam/php/bin/php
557a79800000-557a7a5b7000 r--p 00c00000 fe:01 15623632                   /home/adam/php/bin/php
557a7a72d000-557a7a800000 r--p 01b2d000 fe:01 15623632                   /home/adam/php/bin/php
557a7a800000-557a7a85d000 rw-p 01c00000 fe:01 15623632                   /home/adam/php/bin/php

$ # objdump -p
LOAD off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**21
     filesz 0x000000000015e438 memsz 0x000000000015e438 flags r--
LOAD off    0x0000000000200000 vaddr 0x0000000000200000 paddr 0x0000000000200000 align 2**21
     filesz 0x000000000086c87d memsz 0x000000000086c87d flags r-x
LOAD off    0x0000000000c00000 vaddr 0x0000000000c00000 paddr 0x0000000000c00000 align 2**21
     filesz 0x0000000000db63d8 memsz 0x0000000000db63d8 flags r--
LOAD off    0x0000000001b2ddc8 vaddr 0x0000000001b2ddc8 paddr 0x0000000001b2ddc8 align 2**21
     filesz 0x000000000012f186 memsz 0x0000000000156848 flags rw-

We were calculating base addr as:

557a78c00000 - 0x0000000000000000 == 557a78c00000

but really we should have been calculating it as:

557a78e00000 - 0x0000000000200000 == 557a78c00000

Note you get the same answer both ways, so this went unnoticed.

Recently aarch64 support was added, and on that architecture sometimes the executable mapping or executable vaddr record does not come first, leading to a bad base address. As the inline comment mentions, if PHP happens to have multiple executable segments, this may not work, since it just looks for the first executable record in both cases. I wouldn't expect that to happen in practice but I'm not sure. At least this seems to be an improvement over the existing function.

In order to calculate the base address of PHP's memory space, we
were previously using the first PHP mapping in procfs_maps, which
may not have been an executable range. Similarly we were using the
first vaddr in ELF headers, which again may not have been an
executable segment. In both cases we now look for the first entry
with an "x" flag (executable).

For example, consider:

```
$ # procfs_maps
557a78c00000-557a78d5f000 r--p 00000000 fe:01 15623632                   /home/adam/php/bin/php
557a78e00000-557a7966d000 r-xp 00200000 fe:01 15623632                   /home/adam/php/bin/php
557a79800000-557a7a5b7000 r--p 00c00000 fe:01 15623632                   /home/adam/php/bin/php
557a7a72d000-557a7a800000 r--p 01b2d000 fe:01 15623632                   /home/adam/php/bin/php
557a7a800000-557a7a85d000 rw-p 01c00000 fe:01 15623632                   /home/adam/php/bin/php

$ # objdump -p
LOAD off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**21
     filesz 0x000000000015e438 memsz 0x000000000015e438 flags r--
LOAD off    0x0000000000200000 vaddr 0x0000000000200000 paddr 0x0000000000200000 align 2**21
     filesz 0x000000000086c87d memsz 0x000000000086c87d flags r-x
LOAD off    0x0000000000c00000 vaddr 0x0000000000c00000 paddr 0x0000000000c00000 align 2**21
     filesz 0x0000000000db63d8 memsz 0x0000000000db63d8 flags r--
LOAD off    0x0000000001b2ddc8 vaddr 0x0000000001b2ddc8 paddr 0x0000000001b2ddc8 align 2**21
     filesz 0x000000000012f186 memsz 0x0000000000156848 flags rw-
```

We were calculating base addr as:

    557a78c00000 - 0x0000000000000000 == 557a78c00000

but really we should have been calculating it as:

    557a78e00000 - 0x0000000000200000 == 557a78c00000

Note you get the same answer both ways, so this went unnoticed.

Recently aarch64 support was added, and on that architecture
sometimes the executable mapping or executable vaddr record does not
come first, leading to a bad base address. As the inline comment
mentions, if PHP happens to have multiple executable segments, this
may not work, since it just looks for the first executable record in
both cases. I wouldn't expect that to happen in practice but I'm not
sure. At least this seems to be an improvement over the existing
function.
@adsr
adsr merged commit 893444c into master Apr 19, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant