Go to the first, previous, next, last section, table of contents.

ELF Program Headers

The ELF object file format uses program headers, which are read by the system loader and describe how the program should be loaded into memory. These program headers must be set correctly in order to run the program on a native ELF system. The linker will create reasonable program headers by default. However, in some cases, it is desirable to specify the program headers more precisely; the PHDRS command may be used for this purpose. When the PHDRS command is used, the linker will not generate any program headers itself.

The PHDRS command is only meaningful when generating an ELF output file. It is ignored in other cases. This manual does not describe the details of how the system loader interprets program headers; for more information, see the ELF ABI. The program headers of an ELF file may be displayed using the `-p' option of the objdump command.

This is the syntax of the PHDRS command. The words PHDRS, FILEHDR, AT, and FLAGS are keywords.

  name type [ FILEHDR ] [ PHDRS ] [ AT ( address ) ]
        [ FLAGS ( flags ) ] ;

The name is used only for reference in the SECTIONS command of the linker script. It does not get put into the output file.

Certain program header types describe segments of memory which are loaded from the file by the system loader. In the linker script, the contents of these segments are specified by directing allocated output sections to be placed in the segment. To do this, the command describing the output section in the SECTIONS command should use `:name', where name is the name of the program header as it appears in the PHDRS command. See section Optional Section Attributes.

It is normal for certain sections to appear in more than one segment. This merely implies that one segment of memory contains another. This is specified by repeating `:name', using it once for each program header in which the section is to appear.

If a section is placed in one or more segments using `:name', then all subsequent allocated sections which do not specify `:name' are placed in the same segments. This is for convenience, since generally a whole set of contiguous sections will be placed in a single segment. To prevent a section from being assigned to a segment when it would normally default to one, use :NONE.

The FILEHDR and PHDRS keywords which may appear after the program header type also indicate contents of the segment of memory. The FILEHDR keyword means that the segment should include the ELF file header. The PHDRS keyword means that the segment should include the ELF program headers themselves.

The type may be one of the following. The numbers indicate the value of the keyword.

Indicates an unused program header.
Indicates that this program header describes a segment to be loaded from the file.
Indicates a segment where dynamic linking information can be found.
Indicates a segment where the name of the program interpreter may be found.
Indicates a segment holding note information.
A reserved program header type, defined but not specified by the ELF ABI.
Indicates a segment where the program headers may be found.
An expression giving the numeric type of the program header. This may be used for types not defined above.

It is possible to specify that a segment should be loaded at a particular address in memory. This is done using an AT expression. This is identical to the AT command used in the SECTIONS command (see section Optional Section Attributes). Using the AT command for a program header overrides any information in the SECTIONS command.

Normally the segment flags are set based on the sections. The FLAGS keyword may be used to explicitly specify the segment flags. The value of flags must be an integer. It is used to set the p_flags field of the program header.

Here is an example of the use of PHDRS. This shows a typical set of program headers used on a native ELF system.

  headers PT_PHDR PHDRS ;
  interp PT_INTERP ;
  data PT_LOAD ;
  dynamic PT_DYNAMIC ;

  .interp : { *(.interp) } :text :interp
  .text : { *(.text) } :text
  .rodata : { *(.rodata) } /* defaults to :text */
  . = . + 0x1000; /* move to a new page in memory */
  .data : { *(.data) } :data
  .dynamic : { *(.dynamic) } :data :dynamic

Go to the first, previous, next, last section, table of contents.