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

📄 simple-example.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="Simple%20Example">Simple Example</a>,

Next:<a rel="next" accesskey="n" href="Simple-Commands.html#Simple%20Commands">Simple Commands</a>,

Previous:<a rel="previous" accesskey="p" href="Script-Format.html#Script%20Format">Script Format</a>,

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

<hr><br>

</div>



<h3 class="section">Simple Linker Script Example</h3>



   <p>Many linker scripts are fairly simple.



   <p>The simplest possible linker script has just one command:

<code>SECTIONS</code>.  You use the <code>SECTIONS</code> command to describe the

memory layout of the output file.



   <p>The <code>SECTIONS</code> command is a powerful command.  Here we will

describe a simple use of it.  Let's assume your program consists only of

code, initialized data, and uninitialized data.  These will be in the

<code>.text</code>, <code>.data</code>, and <code>.bss</code> sections, respectively. 

Let's assume further that these are the only sections which appear in

your input files.



   <p>For this example, let's say that the code should be loaded at address

0x10000, and that the data should start at address 0x8000000.  Here is a

linker script which will do that:

<pre class="smallexample">     SECTIONS

     {

       . = 0x10000;

       .text : { *(.text) }

       . = 0x8000000;

       .data : { *(.data) }

       .bss : { *(.bss) }

     }

     </pre>



   <p>You write the <code>SECTIONS</code> command as the keyword <code>SECTIONS</code>,

followed by a series of symbol assignments and output section

descriptions enclosed in curly braces.



   <p>The first line inside the <code>SECTIONS</code> command of the above example

sets the value of the special symbol <code>.</code>, which is the location

counter.  If you do not specify the address of an output section in some

other way (other ways are described later), the address is set from the

current value of the location counter.  The location counter is then

incremented by the size of the output section.  At the start of the

<code>SECTIONS</code> command, the location counter has the value <code>0</code>.



   <p>The second line defines an output section, <code>.text</code>.  The colon is

required syntax which may be ignored for now.  Within the curly braces

after the output section name, you list the names of the input sections

which should be placed into this output section.  The <code>*</code> is a

wildcard which matches any file name.  The expression <code>*(.text)</code>

means all <code>.text</code> input sections in all input files.



   <p>Since the location counter is <code>0x10000</code> when the output section

<code>.text</code> is defined, the linker will set the address of the

<code>.text</code> section in the output file to be <code>0x10000</code>.



   <p>The remaining lines define the <code>.data</code> and <code>.bss</code> sections in

the output file.  The linker will place the <code>.data</code> output section

at address <code>0x8000000</code>.  After the linker places the <code>.data</code>

output section, the value of the location counter will be

<code>0x8000000</code> plus the size of the <code>.data</code> output section.  The

effect is that the linker will place the <code>.bss</code> output section

immediately after the <code>.data</code> output section in memory



   <p>The linker will ensure that each output section has the required

alignment, by increasing the location counter if necessary.  In this

example, the specified addresses for the <code>.text</code> and <code>.data</code>

sections will probably satisfy any alignment constraints, but the linker

may have to create a small gap between the <code>.data</code> and <code>.bss</code>

sections.



   <p>That's it!  That's a simple and complete linker script.



   </body></html>



⌨️ 快捷键说明

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