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: Simple Example</title>
18
19<meta name="description" content="LD: Simple Example">
20<meta name="keywords" content="LD: Simple Example">
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="Simple-Commands.html#Simple-Commands" rel="next" title="Simple Commands">
30<link href="Script-Format.html#Script-Format" rel="previous" title="Script Format">
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="Simple-Example"></a>
65<div class="header">
66<p>
67Next: <a href="Simple-Commands.html#Simple-Commands" accesskey="n" rel="next">Simple Commands</a>, Previous: <a href="Script-Format.html#Script-Format" accesskey="p" rel="previous">Script Format</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>
68</div>
69<hr>
70<a name="Simple-Linker-Script-Example"></a>
71<h3 class="section">3.3 Simple Linker Script Example</h3>
72<a name="index-linker-script-example"></a>
73<a name="index-example-of-linker-script"></a>
74<p>Many linker scripts are fairly simple.
75</p>
76<p>The simplest possible linker script has just one command:
77&lsquo;<samp>SECTIONS</samp>&rsquo;.  You use the &lsquo;<samp>SECTIONS</samp>&rsquo; command to describe the
78memory layout of the output file.
79</p>
80<p>The &lsquo;<samp>SECTIONS</samp>&rsquo; command is a powerful command.  Here we will
81describe a simple use of it.  Let&rsquo;s assume your program consists only of
82code, initialized data, and uninitialized data.  These will be in the
83&lsquo;<samp>.text</samp>&rsquo;, &lsquo;<samp>.data</samp>&rsquo;, and &lsquo;<samp>.bss</samp>&rsquo; sections, respectively.
84Let&rsquo;s assume further that these are the only sections which appear in
85your input files.
86</p>
87<p>For this example, let&rsquo;s say that the code should be loaded at address
880x10000, and that the data should start at address 0x8000000.  Here is a
89linker script which will do that:
90</p><div class="smallexample">
91<pre class="smallexample">SECTIONS
92{
93  . = 0x10000;
94  .text : { *(.text) }
95  . = 0x8000000;
96  .data : { *(.data) }
97  .bss : { *(.bss) }
98}
99</pre></div>
100
101<p>You write the &lsquo;<samp>SECTIONS</samp>&rsquo; command as the keyword &lsquo;<samp>SECTIONS</samp>&rsquo;,
102followed by a series of symbol assignments and output section
103descriptions enclosed in curly braces.
104</p>
105<p>The first line inside the &lsquo;<samp>SECTIONS</samp>&rsquo; command of the above example
106sets the value of the special symbol &lsquo;<samp>.</samp>&rsquo;, which is the location
107counter.  If you do not specify the address of an output section in some
108other way (other ways are described later), the address is set from the
109current value of the location counter.  The location counter is then
110incremented by the size of the output section.  At the start of the
111&lsquo;<samp>SECTIONS</samp>&rsquo; command, the location counter has the value &lsquo;<samp>0</samp>&rsquo;.
112</p>
113<p>The second line defines an output section, &lsquo;<samp>.text</samp>&rsquo;.  The colon is
114required syntax which may be ignored for now.  Within the curly braces
115after the output section name, you list the names of the input sections
116which should be placed into this output section.  The &lsquo;<samp>*</samp>&rsquo; is a
117wildcard which matches any file name.  The expression &lsquo;<samp>*(.text)</samp>&rsquo;
118means all &lsquo;<samp>.text</samp>&rsquo; input sections in all input files.
119</p>
120<p>Since the location counter is &lsquo;<samp>0x10000</samp>&rsquo; when the output section
121&lsquo;<samp>.text</samp>&rsquo; is defined, the linker will set the address of the
122&lsquo;<samp>.text</samp>&rsquo; section in the output file to be &lsquo;<samp>0x10000</samp>&rsquo;.
123</p>
124<p>The remaining lines define the &lsquo;<samp>.data</samp>&rsquo; and &lsquo;<samp>.bss</samp>&rsquo; sections in
125the output file.  The linker will place the &lsquo;<samp>.data</samp>&rsquo; output section
126at address &lsquo;<samp>0x8000000</samp>&rsquo;.  After the linker places the &lsquo;<samp>.data</samp>&rsquo;
127output section, the value of the location counter will be
128&lsquo;<samp>0x8000000</samp>&rsquo; plus the size of the &lsquo;<samp>.data</samp>&rsquo; output section.  The
129effect is that the linker will place the &lsquo;<samp>.bss</samp>&rsquo; output section
130immediately after the &lsquo;<samp>.data</samp>&rsquo; output section in memory.
131</p>
132<p>The linker will ensure that each output section has the required
133alignment, by increasing the location counter if necessary.  In this
134example, the specified addresses for the &lsquo;<samp>.text</samp>&rsquo; and &lsquo;<samp>.data</samp>&rsquo;
135sections will probably satisfy any alignment constraints, but the linker
136may have to create a small gap between the &lsquo;<samp>.data</samp>&rsquo; and &lsquo;<samp>.bss</samp>&rsquo;
137sections.
138</p>
139<p>That&rsquo;s it!  That&rsquo;s a simple and complete linker script.
140</p>
141<hr>
142<div class="header">
143<p>
144Next: <a href="Simple-Commands.html#Simple-Commands" accesskey="n" rel="next">Simple Commands</a>, Previous: <a href="Script-Format.html#Script-Format" accesskey="p" rel="previous">Script Format</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>
145</div>
146
147
148
149</body>
150</html>
151