1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<!-- This file documents the GNU linker LD 4(GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)) 5version 2.36.1. 6 7Copyright (C) 1991-2021 Free Software Foundation, Inc. 8 9Permission is granted to copy, distribute and/or modify this document 10under the terms of the GNU Free Documentation License, Version 1.3 11or any later version published by the Free Software Foundation; 12with no Invariant Sections, with no Front-Cover Texts, and with no 13Back-Cover Texts. A copy of the license is included in the 14section entitled "GNU Free Documentation License". --> 15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ --> 16<head> 17<title>LD: PHDRS</title> 18 19<meta name="description" content="LD: PHDRS"> 20<meta name="keywords" content="LD: PHDRS"> 21<meta name="resource-type" content="document"> 22<meta name="distribution" content="global"> 23<meta name="Generator" content="makeinfo"> 24<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 25<link href="index.html#Top" rel="start" title="Top"> 26<link href="LD-Index.html#LD-Index" rel="index" title="LD Index"> 27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> 28<link href="Scripts.html#Scripts" rel="up" title="Scripts"> 29<link href="VERSION.html#VERSION" rel="next" title="VERSION"> 30<link href="MEMORY.html#MEMORY" rel="previous" title="MEMORY"> 31<style type="text/css"> 32<!-- 33a.summary-letter {text-decoration: none} 34blockquote.smallquotation {font-size: smaller} 35div.display {margin-left: 3.2em} 36div.example {margin-left: 3.2em} 37div.indentedblock {margin-left: 3.2em} 38div.lisp {margin-left: 3.2em} 39div.smalldisplay {margin-left: 3.2em} 40div.smallexample {margin-left: 3.2em} 41div.smallindentedblock {margin-left: 3.2em; font-size: smaller} 42div.smalllisp {margin-left: 3.2em} 43kbd {font-style:oblique} 44pre.display {font-family: inherit} 45pre.format {font-family: inherit} 46pre.menu-comment {font-family: serif} 47pre.menu-preformatted {font-family: serif} 48pre.smalldisplay {font-family: inherit; font-size: smaller} 49pre.smallexample {font-size: smaller} 50pre.smallformat {font-family: inherit; font-size: smaller} 51pre.smalllisp {font-size: smaller} 52span.nocodebreak {white-space:nowrap} 53span.nolinebreak {white-space:nowrap} 54span.roman {font-family:serif; font-weight:normal} 55span.sansserif {font-family:sans-serif; font-weight:normal} 56ul.no-bullet {list-style: none} 57--> 58</style> 59 60 61</head> 62 63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> 64<a name="PHDRS"></a> 65<div class="header"> 66<p> 67Next: <a href="VERSION.html#VERSION" accesskey="n" rel="next">VERSION</a>, Previous: <a href="MEMORY.html#MEMORY" accesskey="p" rel="previous">MEMORY</a>, Up: <a href="Scripts.html#Scripts" accesskey="u" rel="up">Scripts</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="LD-Index.html#LD-Index" title="Index" rel="index">Index</a>]</p> 68</div> 69<hr> 70<a name="PHDRS-Command"></a> 71<h3 class="section">3.8 PHDRS Command</h3> 72<a name="index-PHDRS"></a> 73<a name="index-program-headers"></a> 74<a name="index-ELF-program-headers"></a> 75<a name="index-program-segments"></a> 76<a name="index-segments_002c-ELF"></a> 77<p>The ELF object file format uses <em>program headers</em>, also knows as 78<em>segments</em>. The program headers describe how the program should be 79loaded into memory. You can print them out by using the <code>objdump</code> 80program with the ‘<samp>-p</samp>’ option. 81</p> 82<p>When you run an ELF program on a native ELF system, the system loader 83reads the program headers in order to figure out how to load the 84program. This will only work if the program headers are set correctly. 85This manual does not describe the details of how the system loader 86interprets program headers; for more information, see the ELF ABI. 87</p> 88<p>The linker will create reasonable program headers by default. However, 89in some cases, you may need to specify the program headers more 90precisely. You may use the <code>PHDRS</code> command for this purpose. When 91the linker sees the <code>PHDRS</code> command in the linker script, it will 92not create any program headers other than the ones specified. 93</p> 94<p>The linker only pays attention to the <code>PHDRS</code> command when 95generating an ELF output file. In other cases, the linker will simply 96ignore <code>PHDRS</code>. 97</p> 98<p>This is the syntax of the <code>PHDRS</code> command. The words <code>PHDRS</code>, 99<code>FILEHDR</code>, <code>AT</code>, and <code>FLAGS</code> are keywords. 100</p> 101<div class="smallexample"> 102<pre class="smallexample">PHDRS 103{ 104 <var>name</var> <var>type</var> [ FILEHDR ] [ PHDRS ] [ AT ( <var>address</var> ) ] 105 [ FLAGS ( <var>flags</var> ) ] ; 106} 107</pre></div> 108 109<p>The <var>name</var> is used only for reference in the <code>SECTIONS</code> command 110of the linker script. It is not put into the output file. Program 111header names are stored in a separate name space, and will not conflict 112with symbol names, file names, or section names. Each program header 113must have a distinct name. The headers are processed in order and it 114is usual for them to map to sections in ascending load address order. 115</p> 116<p>Certain program header types describe segments of memory which the 117system loader will load from the file. In the linker script, you 118specify the contents of these segments by placing allocatable output 119sections in the segments. You use the ‘<samp>:<var>phdr</var></samp>’ output section 120attribute to place a section in a particular segment. See <a href="Output-Section-Phdr.html#Output-Section-Phdr">Output Section Phdr</a>. 121</p> 122<p>It is normal to put certain sections in more than one segment. This 123merely implies that one segment of memory contains another. You may 124repeat ‘<samp>:<var>phdr</var></samp>’, using it once for each segment which should 125contain the section. 126</p> 127<p>If you place a section in one or more segments using ‘<samp>:<var>phdr</var></samp>’, 128then the linker will place all subsequent allocatable sections which do 129not specify ‘<samp>:<var>phdr</var></samp>’ in the same segments. This is for 130convenience, since generally a whole set of contiguous sections will be 131placed in a single segment. You can use <code>:NONE</code> to override the 132default segment and tell the linker to not put the section in any 133segment at all. 134</p> 135<a name="index-FILEHDR"></a> 136<a name="index-PHDRS-1"></a> 137<p>You may use the <code>FILEHDR</code> and <code>PHDRS</code> keywords after 138the program header type to further describe the contents of the segment. 139The <code>FILEHDR</code> keyword means that the segment should include the ELF 140file header. The <code>PHDRS</code> keyword means that the segment should 141include the ELF program headers themselves. If applied to a loadable 142segment (<code>PT_LOAD</code>), all prior loadable segments must have one of 143these keywords. 144</p> 145<p>The <var>type</var> may be one of the following. The numbers indicate the 146value of the keyword. 147</p> 148<dl compact="compact"> 149<dt><code>PT_NULL</code> (0)</dt> 150<dd><p>Indicates an unused program header. 151</p> 152</dd> 153<dt><code>PT_LOAD</code> (1)</dt> 154<dd><p>Indicates that this program header describes a segment to be loaded from 155the file. 156</p> 157</dd> 158<dt><code>PT_DYNAMIC</code> (2)</dt> 159<dd><p>Indicates a segment where dynamic linking information can be found. 160</p> 161</dd> 162<dt><code>PT_INTERP</code> (3)</dt> 163<dd><p>Indicates a segment where the name of the program interpreter may be 164found. 165</p> 166</dd> 167<dt><code>PT_NOTE</code> (4)</dt> 168<dd><p>Indicates a segment holding note information. 169</p> 170</dd> 171<dt><code>PT_SHLIB</code> (5)</dt> 172<dd><p>A reserved program header type, defined but not specified by the ELF 173ABI. 174</p> 175</dd> 176<dt><code>PT_PHDR</code> (6)</dt> 177<dd><p>Indicates a segment where the program headers may be found. 178</p> 179</dd> 180<dt><code>PT_TLS</code> (7)</dt> 181<dd><p>Indicates a segment containing thread local storage. 182</p> 183</dd> 184<dt><var>expression</var></dt> 185<dd><p>An expression giving the numeric type of the program header. This may 186be used for types not defined above. 187</p></dd> 188</dl> 189 190<p>You can specify that a segment should be loaded at a particular address 191in memory by using an <code>AT</code> expression. This is identical to the 192<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 193output section attribute. 194</p> 195<p>The linker will normally set the segment flags based on the sections 196which comprise the segment. You may use the <code>FLAGS</code> keyword to 197explicitly specify the segment flags. The value of <var>flags</var> must be 198an integer. It is used to set the <code>p_flags</code> field of the program 199header. 200</p> 201<p>Here is an example of <code>PHDRS</code>. This shows a typical set of program 202headers used on a native ELF system. 203</p> 204<div class="example"> 205<pre class="example">PHDRS 206{ 207 headers PT_PHDR PHDRS ; 208 interp PT_INTERP ; 209 text PT_LOAD FILEHDR PHDRS ; 210 data PT_LOAD ; 211 dynamic PT_DYNAMIC ; 212} 213 214SECTIONS 215{ 216 . = SIZEOF_HEADERS; 217 .interp : { *(.interp) } :text :interp 218 .text : { *(.text) } :text 219 .rodata : { *(.rodata) } /* defaults to :text */ 220 … 221 . = . + 0x1000; /* move to a new page in memory */ 222 .data : { *(.data) } :data 223 .dynamic : { *(.dynamic) } :data :dynamic 224 … 225} 226</pre></div> 227 228<hr> 229<div class="header"> 230<p> 231Next: <a href="VERSION.html#VERSION" accesskey="n" rel="next">VERSION</a>, Previous: <a href="MEMORY.html#MEMORY" accesskey="p" rel="previous">MEMORY</a>, Up: <a href="Scripts.html#Scripts" accesskey="u" rel="up">Scripts</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="LD-Index.html#LD-Index" title="Index" rel="index">Index</a>]</p> 232</div> 233 234 235 236</body> 237</html> 238