⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phdrs.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>Untitled</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="Untitled">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

</head>

<body>

<div class="node">

<p>

Node:<a name="PHDRS">PHDRS</a>,

Next:<a rel="next" accesskey="n" href="VERSION.html#VERSION">VERSION</a>,

Previous:<a rel="previous" accesskey="p" href="MEMORY.html#MEMORY">MEMORY</a>,

Up:<a rel="up" accesskey="u" href="Scripts.html#Scripts">Scripts</a>

<hr><br>

</div>



<h3 class="section">PHDRS Command</h3>



   <p>The ELF object file format uses <dfn>program headers</dfn>, also knows as

<dfn>segments</dfn>.  The program headers describe how the program should be

loaded into memory.  You can print them out by using the <code>objdump</code>

program with the <code>-p</code> option.



   <p>When you run an ELF program on a native ELF system, the system loader

reads the program headers in order to figure out how to load the

program.  This will only work if the program headers are set correctly. 

This manual does not describe the details of how the system loader

interprets program headers; for more information, see the ELF ABI.



   <p>The linker will create reasonable program headers by default.  However,

in some cases, you may need to specify the program headers more

precisely.  You may use the <code>PHDRS</code> command for this purpose.  When

the linker sees the <code>PHDRS</code> command in the linker script, it will

not create any program headers other than the ones specified.



   <p>The linker only pays attention to the <code>PHDRS</code> command when

generating an ELF output file.  In other cases, the linker will simply

ignore <code>PHDRS</code>.



   <p>This is the syntax of the <code>PHDRS</code> command.  The words <code>PHDRS</code>,

<code>FILEHDR</code>, <code>AT</code>, and <code>FLAGS</code> are keywords.



<pre class="smallexample">     PHDRS

     {

       <var>name</var> <var>type</var> [ FILEHDR ] [ PHDRS ] [ AT ( <var>address</var> ) ]

             [ FLAGS ( <var>flags</var> ) ] ;

     }

     </pre>



   <p>The <var>name</var> is used only for reference in the <code>SECTIONS</code> command

of the linker script.  It is not put into the output file.  Program

header names are stored in a separate name space, and will not conflict

with symbol names, file names, or section names.  Each program header

must have a distinct name.



   <p>Certain program header types describe segments of memory which the

system loader will load from the file.  In the linker script, you

specify the contents of these segments by placing allocatable output

sections in the segments.  You use the <code>:</code><var>phdr</var><code></code> output section

attribute to place a section in a particular segment.  See <a href="Output-Section-Phdr.html#Output%20Section%20Phdr">Output Section Phdr</a>.



   <p>It is normal to put certain sections in more than one segment.  This

merely implies that one segment of memory contains another.  You may

repeat <code>:</code><var>phdr</var><code></code>, using it once for each segment which should

contain the section.



   <p>If you place a section in one or more segments using <code>:</code><var>phdr</var><code></code>,

then the linker will place all subsequent allocatable sections which do

not specify <code>:</code><var>phdr</var><code></code> in the same segments.  This is for

convenience, since generally a whole set of contiguous sections will be

placed in a single segment.  You can use <code>:NONE</code> to override the

default segment and tell the linker to not put the section in any

segment at all.



   <p>You may use the <code>FILEHDR</code> and <code>PHDRS</code> keywords appear after

the program header type to further describe the contents of the segment. 

The <code>FILEHDR</code> keyword means that the segment should include the ELF

file header.  The <code>PHDRS</code> keyword means that the segment should

include the ELF program headers themselves.



   <p>The <var>type</var> may be one of the following.  The numbers indicate the

value of the keyword.



     <dl>

<dt><code>PT_NULL</code> (0)

     <dd>Indicates an unused program header.



     <br><dt><code>PT_LOAD</code> (1)

     <dd>Indicates that this program header describes a segment to be loaded from

the file.



     <br><dt><code>PT_DYNAMIC</code> (2)

     <dd>Indicates a segment where dynamic linking information can be found.



     <br><dt><code>PT_INTERP</code> (3)

     <dd>Indicates a segment where the name of the program interpreter may be

found.



     <br><dt><code>PT_NOTE</code> (4)

     <dd>Indicates a segment holding note information.



     <br><dt><code>PT_SHLIB</code> (5)

     <dd>A reserved program header type, defined but not specified by the ELF

ABI.



     <br><dt><code>PT_PHDR</code> (6)

     <dd>Indicates a segment where the program headers may be found.



     <br><dt><var>expression</var>

     <dd>An expression giving the numeric type of the program header.  This may

be used for types not defined above. 

</dl>



   <p>You can specify that a segment should be loaded at a particular address

in memory by using an <code>AT</code> expression.  This is identical to the

<code>AT</code> command used as an output section attribute (see <a href="Output-Section-LMA.html#Output%20Section%20LMA">Output Section LMA</a>).  The <code>AT</code> command for a program header overrides the

output section attribute.



   <p>The linker will normally set the segment flags based on the sections

which comprise the segment.  You may use the <code>FLAGS</code> keyword to

explicitly specify the segment flags.  The value of <var>flags</var> must be

an integer.  It is used to set the <code>p_flags</code> field of the program

header.



   <p>Here is an example of <code>PHDRS</code>.  This shows a typical set of program

headers used on a native ELF system.



<pre class="example">     PHDRS

     {

       headers PT_PHDR PHDRS ;

       interp PT_INTERP ;

       text PT_LOAD FILEHDR PHDRS ;

       data PT_LOAD ;

       dynamic PT_DYNAMIC ;

     }

     

     SECTIONS

     {

       . = SIZEOF_HEADERS;

       .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

       ...

     }

     </pre>



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -