PHDRS.html 9.7 KB

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