1<html lang="en"> 2<head> 3<title>PHDRS - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Scripts.html#Scripts" title="Scripts"> 9<link rel="prev" href="MEMORY.html#MEMORY" title="MEMORY"> 10<link rel="next" href="VERSION.html#VERSION" title="VERSION"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13This file documents the GNU linker LD 14(Sourcery CodeBench Lite 2011.09-69) 15version 2.21.53. 16 17Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 182001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. 19 20Permission is granted to copy, distribute and/or modify this document 21under the terms of the GNU Free Documentation License, Version 1.3 22or any later version published by the Free Software Foundation; 23with no Invariant Sections, with no Front-Cover Texts, and with no 24Back-Cover Texts. A copy of the license is included in the 25section entitled ``GNU Free Documentation License''.--> 26<meta http-equiv="Content-Style-Type" content="text/css"> 27<style type="text/css"><!-- 28 pre.display { font-family:inherit } 29 pre.format { font-family:inherit } 30 pre.smalldisplay { font-family:inherit; font-size:smaller } 31 pre.smallformat { font-family:inherit; font-size:smaller } 32 pre.smallexample { font-size:smaller } 33 pre.smalllisp { font-size:smaller } 34 span.sc { font-variant:small-caps } 35 span.roman { font-family:serif; font-weight:normal; } 36 span.sansserif { font-family:sans-serif; font-weight:normal; } 37--></style> 38<link rel="stylesheet" type="text/css" href="../cs.css"> 39</head> 40<body> 41<div class="node"> 42<a name="PHDRS"></a> 43<p> 44Next: <a rel="next" accesskey="n" href="VERSION.html#VERSION">VERSION</a>, 45Previous: <a rel="previous" accesskey="p" href="MEMORY.html#MEMORY">MEMORY</a>, 46Up: <a rel="up" accesskey="u" href="Scripts.html#Scripts">Scripts</a> 47<hr> 48</div> 49 50<h3 class="section">3.8 PHDRS Command</h3> 51 52<p><a name="index-PHDRS-486"></a><a name="index-program-headers-487"></a><a name="index-ELF-program-headers-488"></a><a name="index-program-segments-489"></a><a name="index-segments_002c-ELF-490"></a>The ELF object file format uses <dfn>program headers</dfn>, also knows as 53<dfn>segments</dfn>. The program headers describe how the program should be 54loaded into memory. You can print them out by using the <code>objdump</code> 55program with the ‘<samp><span class="samp">-p</span></samp>’ option. 56 57 <p>When you run an ELF program on a native ELF system, the system loader 58reads the program headers in order to figure out how to load the 59program. This will only work if the program headers are set correctly. 60This manual does not describe the details of how the system loader 61interprets program headers; for more information, see the ELF ABI. 62 63 <p>The linker will create reasonable program headers by default. However, 64in some cases, you may need to specify the program headers more 65precisely. You may use the <code>PHDRS</code> command for this purpose. When 66the linker sees the <code>PHDRS</code> command in the linker script, it will 67not create any program headers other than the ones specified. 68 69 <p>The linker only pays attention to the <code>PHDRS</code> command when 70generating an ELF output file. In other cases, the linker will simply 71ignore <code>PHDRS</code>. 72 73 <p>This is the syntax of the <code>PHDRS</code> command. The words <code>PHDRS</code>, 74<code>FILEHDR</code>, <code>AT</code>, and <code>FLAGS</code> are keywords. 75 76<pre class="smallexample"> PHDRS 77 { 78 <var>name</var> <var>type</var> [ FILEHDR ] [ PHDRS ] [ AT ( <var>address</var> ) ] 79 [ FLAGS ( <var>flags</var> ) ] ; 80 } 81</pre> 82 <p>The <var>name</var> is used only for reference in the <code>SECTIONS</code> command 83of the linker script. It is not put into the output file. Program 84header names are stored in a separate name space, and will not conflict 85with symbol names, file names, or section names. Each program header 86must have a distinct name. The headers are processed in order and it 87is usual for them to map to sections in ascending load address order. 88 89 <p>Certain program header types describe segments of memory which the 90system loader will load from the file. In the linker script, you 91specify the contents of these segments by placing allocatable output 92sections in the segments. You use the ‘<samp><span class="samp">:</span><var>phdr</var></samp>’ output section 93attribute to place a section in a particular segment. See <a href="Output-Section-Phdr.html#Output-Section-Phdr">Output Section Phdr</a>. 94 95 <p>It is normal to put certain sections in more than one segment. This 96merely implies that one segment of memory contains another. You may 97repeat ‘<samp><span class="samp">:</span><var>phdr</var></samp>’, using it once for each segment which should 98contain the section. 99 100 <p>If you place a section in one or more segments using ‘<samp><span class="samp">:</span><var>phdr</var></samp>’, 101then the linker will place all subsequent allocatable sections which do 102not specify ‘<samp><span class="samp">:</span><var>phdr</var></samp>’ in the same segments. This is for 103convenience, since generally a whole set of contiguous sections will be 104placed in a single segment. You can use <code>:NONE</code> to override the 105default segment and tell the linker to not put the section in any 106segment at all. 107 108 <p><a name="index-FILEHDR-491"></a><a name="index-PHDRS-492"></a>You may use the <code>FILEHDR</code> and <code>PHDRS</code> keywords after 109the program header type to further describe the contents of the segment. 110The <code>FILEHDR</code> keyword means that the segment should include the ELF 111file header. The <code>PHDRS</code> keyword means that the segment should 112include the ELF program headers themselves. If applied to a loadable 113segment (<code>PT_LOAD</code>), all prior loadable segments must have one of 114these keywords. 115 116 <p>The <var>type</var> may be one of the following. The numbers indicate the 117value of the keyword. 118 119 <dl> 120<dt><code>PT_NULL</code> (0)<dd>Indicates an unused program header. 121 122 <br><dt><code>PT_LOAD</code> (1)<dd>Indicates that this program header describes a segment to be loaded from 123the file. 124 125 <br><dt><code>PT_DYNAMIC</code> (2)<dd>Indicates a segment where dynamic linking information can be found. 126 127 <br><dt><code>PT_INTERP</code> (3)<dd>Indicates a segment where the name of the program interpreter may be 128found. 129 130 <br><dt><code>PT_NOTE</code> (4)<dd>Indicates a segment holding note information. 131 132 <br><dt><code>PT_SHLIB</code> (5)<dd>A reserved program header type, defined but not specified by the ELF 133ABI. 134 135 <br><dt><code>PT_PHDR</code> (6)<dd>Indicates a segment where the program headers may be found. 136 137 <br><dt><var>expression</var><dd>An expression giving the numeric type of the program header. This may 138be used for types not defined above. 139</dl> 140 141 <p>You can specify that a segment should be loaded at a particular address 142in memory by using an <code>AT</code> expression. This is identical to the 143<code>AT</code> command used as an output section attribute (see <a href="Output-Section-LMA.html#Output-Section-LMA">Output Section LMA</a>). The <code>AT</code> command for a program header overrides the 144output section attribute. 145 146 <p>The linker will normally set the segment flags based on the sections 147which comprise the segment. You may use the <code>FLAGS</code> keyword to 148explicitly specify the segment flags. The value of <var>flags</var> must be 149an integer. It is used to set the <code>p_flags</code> field of the program 150header. 151 152 <p>Here is an example of <code>PHDRS</code>. This shows a typical set of program 153headers used on a native ELF system. 154 155<pre class="example"> PHDRS 156 { 157 headers PT_PHDR PHDRS ; 158 interp PT_INTERP ; 159 text PT_LOAD FILEHDR PHDRS ; 160 data PT_LOAD ; 161 dynamic PT_DYNAMIC ; 162 } 163 164 SECTIONS 165 { 166 . = SIZEOF_HEADERS; 167 .interp : { *(.interp) } :text :interp 168 .text : { *(.text) } :text 169 .rodata : { *(.rodata) } /* defaults to :text */ 170 ... 171 . = . + 0x1000; /* move to a new page in memory */ 172 .data : { *(.data) } :data 173 .dynamic : { *(.dynamic) } :data :dynamic 174 ... 175 } 176</pre> 177 </body></html> 178 179