Can I change the default virtual address(ph_vaddr) in the elf to 0x0.
Yes, that is in fact how PIE
(position independent) executables are usually linked.
echo "int main() { return 0; }" | gcc -xc - -fPIE -pie -o a.out
readelf -l a.out | grep LOAD | head -1
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
Note: above makes an executable that is of type ET_DYN
.
will this allow access to null pointer?
No. When the kernel discovers that the .e_type == ET_DYN
for the executable, it will relocate all of its segments elsewhere.
You can also make an executable of type ET_EXEC
with .p_vaddr == 0
, like so:
echo "int main() { return 0; }" | gcc -xc - -o a.out -Wl,-Ttext=0
readelf -l a.out | grep LOAD | head -1
LOAD 0x0000000000200000 0x0000000000000000 0x0000000000000000
The kernel will refuse to run it:
./a.out
Killed