Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the program header executable?

Tags:

linux

elf

I used readelf on several binaries on my linux box and saw something that surprised me in the program headers. This eample is from the 'ld' utility, but it also occurs with anything I compile with gcc.

PHDR 0x000034 0x08048034 0x08048034 0x00120 0x00120 R E 0x4

This segment spans the entirety of the program headers. Why is is marked as executable? It doesn't contain machine code. But also, why is even this present in the headers? I don't really want it in my program image.

like image 918
eyesathousand Avatar asked May 31 '11 10:05

eyesathousand


1 Answers

The PHDR pointing to the PHDRs tells the loader that the PHDRs themselves should be mapped to the process address space, in order to make them accessible to the program itself.

This is useful mainly for dynamic linking.

The reason the memory is marked as executable is because the PHDRs are smaller than one page, and live right next to the start of the executable code. If the permissions for the PHDRs were different from those of the program text, the linker would have to insert padding between them.

like image 51
Simon Richter Avatar answered Sep 29 '22 11:09

Simon Richter