• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/toolchains/hndtools-armeabi-2011.09/share/doc/arm-arm-none-eabi/html/ld.html/
1<html lang="en">
2<head>
3<title>Simple Example - 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="Script-Format.html#Script-Format" title="Script Format">
10<link rel="next" href="Simple-Commands.html#Simple-Commands" title="Simple Commands">
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="Simple-Example"></a>
43<p>
44Next:&nbsp;<a rel="next" accesskey="n" href="Simple-Commands.html#Simple-Commands">Simple Commands</a>,
45Previous:&nbsp;<a rel="previous" accesskey="p" href="Script-Format.html#Script-Format">Script Format</a>,
46Up:&nbsp;<a rel="up" accesskey="u" href="Scripts.html#Scripts">Scripts</a>
47<hr>
48</div>
49
50<h3 class="section">3.3 Simple Linker Script Example</h3>
51
52<p><a name="index-linker-script-example-347"></a><a name="index-example-of-linker-script-348"></a>Many linker scripts are fairly simple.
53
54   <p>The simplest possible linker script has just one command:
55&lsquo;<samp><span class="samp">SECTIONS</span></samp>&rsquo;.  You use the &lsquo;<samp><span class="samp">SECTIONS</span></samp>&rsquo; command to describe the
56memory layout of the output file.
57
58   <p>The &lsquo;<samp><span class="samp">SECTIONS</span></samp>&rsquo; command is a powerful command.  Here we will
59describe a simple use of it.  Let's assume your program consists only of
60code, initialized data, and uninitialized data.  These will be in the
61&lsquo;<samp><span class="samp">.text</span></samp>&rsquo;, &lsquo;<samp><span class="samp">.data</span></samp>&rsquo;, and &lsquo;<samp><span class="samp">.bss</span></samp>&rsquo; sections, respectively. 
62Let's assume further that these are the only sections which appear in
63your input files.
64
65   <p>For this example, let's say that the code should be loaded at address
660x10000, and that the data should start at address 0x8000000.  Here is a
67linker script which will do that:
68<pre class="smallexample">     SECTIONS
69     {
70       . = 0x10000;
71       .text : { *(.text) }
72       . = 0x8000000;
73       .data : { *(.data) }
74       .bss : { *(.bss) }
75     }
76</pre>
77   <p>You write the &lsquo;<samp><span class="samp">SECTIONS</span></samp>&rsquo; command as the keyword &lsquo;<samp><span class="samp">SECTIONS</span></samp>&rsquo;,
78followed by a series of symbol assignments and output section
79descriptions enclosed in curly braces.
80
81   <p>The first line inside the &lsquo;<samp><span class="samp">SECTIONS</span></samp>&rsquo; command of the above example
82sets the value of the special symbol &lsquo;<samp><span class="samp">.</span></samp>&rsquo;, which is the location
83counter.  If you do not specify the address of an output section in some
84other way (other ways are described later), the address is set from the
85current value of the location counter.  The location counter is then
86incremented by the size of the output section.  At the start of the
87&lsquo;<samp><span class="samp">SECTIONS</span></samp>&rsquo; command, the location counter has the value &lsquo;<samp><span class="samp">0</span></samp>&rsquo;.
88
89   <p>The second line defines an output section, &lsquo;<samp><span class="samp">.text</span></samp>&rsquo;.  The colon is
90required syntax which may be ignored for now.  Within the curly braces
91after the output section name, you list the names of the input sections
92which should be placed into this output section.  The &lsquo;<samp><span class="samp">*</span></samp>&rsquo; is a
93wildcard which matches any file name.  The expression &lsquo;<samp><span class="samp">*(.text)</span></samp>&rsquo;
94means all &lsquo;<samp><span class="samp">.text</span></samp>&rsquo; input sections in all input files.
95
96   <p>Since the location counter is &lsquo;<samp><span class="samp">0x10000</span></samp>&rsquo; when the output section
97&lsquo;<samp><span class="samp">.text</span></samp>&rsquo; is defined, the linker will set the address of the
98&lsquo;<samp><span class="samp">.text</span></samp>&rsquo; section in the output file to be &lsquo;<samp><span class="samp">0x10000</span></samp>&rsquo;.
99
100   <p>The remaining lines define the &lsquo;<samp><span class="samp">.data</span></samp>&rsquo; and &lsquo;<samp><span class="samp">.bss</span></samp>&rsquo; sections in
101the output file.  The linker will place the &lsquo;<samp><span class="samp">.data</span></samp>&rsquo; output section
102at address &lsquo;<samp><span class="samp">0x8000000</span></samp>&rsquo;.  After the linker places the &lsquo;<samp><span class="samp">.data</span></samp>&rsquo;
103output section, the value of the location counter will be
104&lsquo;<samp><span class="samp">0x8000000</span></samp>&rsquo; plus the size of the &lsquo;<samp><span class="samp">.data</span></samp>&rsquo; output section.  The
105effect is that the linker will place the &lsquo;<samp><span class="samp">.bss</span></samp>&rsquo; output section
106immediately after the &lsquo;<samp><span class="samp">.data</span></samp>&rsquo; output section in memory.
107
108   <p>The linker will ensure that each output section has the required
109alignment, by increasing the location counter if necessary.  In this
110example, the specified addresses for the &lsquo;<samp><span class="samp">.text</span></samp>&rsquo; and &lsquo;<samp><span class="samp">.data</span></samp>&rsquo;
111sections will probably satisfy any alignment constraints, but the linker
112may have to create a small gap between the &lsquo;<samp><span class="samp">.data</span></samp>&rsquo; and &lsquo;<samp><span class="samp">.bss</span></samp>&rsquo;
113sections.
114
115   <p>That's it!  That's a simple and complete linker script.
116
117   </body></html>
118
119