|
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <!-- This file documents the GNU linker LD
- (GNU Arm Embedded Toolchain 10-2020-q4-major)
- version 2.35.1.
-
- Copyright (C) 1991-2020 Free Software Foundation, Inc.
-
- Permission is granted to copy, distribute and/or modify this document
- under the terms of the GNU Free Documentation License, Version 1.3
- or any later version published by the Free Software Foundation;
- with no Invariant Sections, with no Front-Cover Texts, and with no
- Back-Cover Texts. A copy of the license is included in the
- section entitled "GNU Free Documentation License". -->
- <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Simple Example (LD)</title>
-
- <meta name="description" content="Simple Example (LD)">
- <meta name="keywords" content="Simple Example (LD)">
- <meta name="resource-type" content="document">
- <meta name="distribution" content="global">
- <meta name="Generator" content="makeinfo">
- <link href="index.html#Top" rel="start" title="Top">
- <link href="LD-Index.html#LD-Index" rel="index" title="LD Index">
- <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
- <link href="Scripts.html#Scripts" rel="up" title="Scripts">
- <link href="Simple-Commands.html#Simple-Commands" rel="next" title="Simple Commands">
- <link href="Script-Format.html#Script-Format" rel="prev" title="Script Format">
- <style type="text/css">
- <!--
- a.summary-letter {text-decoration: none}
- blockquote.indentedblock {margin-right: 0em}
- blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
- blockquote.smallquotation {font-size: smaller}
- div.display {margin-left: 3.2em}
- div.example {margin-left: 3.2em}
- div.lisp {margin-left: 3.2em}
- div.smalldisplay {margin-left: 3.2em}
- div.smallexample {margin-left: 3.2em}
- div.smalllisp {margin-left: 3.2em}
- kbd {font-style: oblique}
- pre.display {font-family: inherit}
- pre.format {font-family: inherit}
- pre.menu-comment {font-family: serif}
- pre.menu-preformatted {font-family: serif}
- pre.smalldisplay {font-family: inherit; font-size: smaller}
- pre.smallexample {font-size: smaller}
- pre.smallformat {font-family: inherit; font-size: smaller}
- pre.smalllisp {font-size: smaller}
- span.nolinebreak {white-space: nowrap}
- span.roman {font-family: initial; font-weight: normal}
- span.sansserif {font-family: sans-serif; font-weight: normal}
- ul.no-bullet {list-style: none}
- -->
- </style>
-
-
- </head>
-
- <body lang="en">
- <a name="Simple-Example"></a>
- <div class="header">
- <p>
- Next: <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="prev">Script Format</a>, Up: <a href="Scripts.html#Scripts" accesskey="u" rel="up">Scripts</a> [<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>
- </div>
- <hr>
- <a name="Simple-Linker-Script-Example"></a>
- <h3 class="section">3.3 Simple Linker Script Example</h3>
- <a name="index-linker-script-example"></a>
- <a name="index-example-of-linker-script"></a>
- <p>Many linker scripts are fairly simple.
- </p>
- <p>The simplest possible linker script has just one command:
- ‘<samp>SECTIONS</samp>’. You use the ‘<samp>SECTIONS</samp>’ command to describe the
- memory layout of the output file.
- </p>
- <p>The ‘<samp>SECTIONS</samp>’ 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
- ‘<samp>.text</samp>’, ‘<samp>.data</samp>’, and ‘<samp>.bss</samp>’ sections, respectively.
- Let’s assume further that these are the only sections which appear in
- your input files.
- </p>
- <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:
- </p><div class="smallexample">
- <pre class="smallexample">SECTIONS
- {
- . = 0x10000;
- .text : { *(.text) }
- . = 0x8000000;
- .data : { *(.data) }
- .bss : { *(.bss) }
- }
- </pre></div>
-
- <p>You write the ‘<samp>SECTIONS</samp>’ command as the keyword ‘<samp>SECTIONS</samp>’,
- followed by a series of symbol assignments and output section
- descriptions enclosed in curly braces.
- </p>
- <p>The first line inside the ‘<samp>SECTIONS</samp>’ command of the above example
- sets the value of the special symbol ‘<samp>.</samp>’, 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
- ‘<samp>SECTIONS</samp>’ command, the location counter has the value ‘<samp>0</samp>’.
- </p>
- <p>The second line defines an output section, ‘<samp>.text</samp>’. 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 ‘<samp>*</samp>’ is a
- wildcard which matches any file name. The expression ‘<samp>*(.text)</samp>’
- means all ‘<samp>.text</samp>’ input sections in all input files.
- </p>
- <p>Since the location counter is ‘<samp>0x10000</samp>’ when the output section
- ‘<samp>.text</samp>’ is defined, the linker will set the address of the
- ‘<samp>.text</samp>’ section in the output file to be ‘<samp>0x10000</samp>’.
- </p>
- <p>The remaining lines define the ‘<samp>.data</samp>’ and ‘<samp>.bss</samp>’ sections in
- the output file. The linker will place the ‘<samp>.data</samp>’ output section
- at address ‘<samp>0x8000000</samp>’. After the linker places the ‘<samp>.data</samp>’
- output section, the value of the location counter will be
- ‘<samp>0x8000000</samp>’ plus the size of the ‘<samp>.data</samp>’ output section. The
- effect is that the linker will place the ‘<samp>.bss</samp>’ output section
- immediately after the ‘<samp>.data</samp>’ output section in memory.
- </p>
- <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 ‘<samp>.text</samp>’ and ‘<samp>.data</samp>’
- sections will probably satisfy any alignment constraints, but the linker
- may have to create a small gap between the ‘<samp>.data</samp>’ and ‘<samp>.bss</samp>’
- sections.
- </p>
- <p>That’s it! That’s a simple and complete linker script.
- </p>
- <hr>
- <div class="header">
- <p>
- Next: <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="prev">Script Format</a>, Up: <a href="Scripts.html#Scripts" accesskey="u" rel="up">Scripts</a> [<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>
- </div>
-
-
-
- </body>
- </html>
|