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: <a rel="next" accesskey="n" href="Simple-Commands.html#Simple-Commands">Simple Commands</a>, 45Previous: <a rel="previous" accesskey="p" href="Script-Format.html#Script-Format">Script Format</a>, 46Up: <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‘<samp><span class="samp">SECTIONS</span></samp>’. You use the ‘<samp><span class="samp">SECTIONS</span></samp>’ command to describe the 56memory layout of the output file. 57 58 <p>The ‘<samp><span class="samp">SECTIONS</span></samp>’ 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‘<samp><span class="samp">.text</span></samp>’, ‘<samp><span class="samp">.data</span></samp>’, and ‘<samp><span class="samp">.bss</span></samp>’ 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 ‘<samp><span class="samp">SECTIONS</span></samp>’ command as the keyword ‘<samp><span class="samp">SECTIONS</span></samp>’, 78followed by a series of symbol assignments and output section 79descriptions enclosed in curly braces. 80 81 <p>The first line inside the ‘<samp><span class="samp">SECTIONS</span></samp>’ command of the above example 82sets the value of the special symbol ‘<samp><span class="samp">.</span></samp>’, 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‘<samp><span class="samp">SECTIONS</span></samp>’ command, the location counter has the value ‘<samp><span class="samp">0</span></samp>’. 88 89 <p>The second line defines an output section, ‘<samp><span class="samp">.text</span></samp>’. 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 ‘<samp><span class="samp">*</span></samp>’ is a 93wildcard which matches any file name. The expression ‘<samp><span class="samp">*(.text)</span></samp>’ 94means all ‘<samp><span class="samp">.text</span></samp>’ input sections in all input files. 95 96 <p>Since the location counter is ‘<samp><span class="samp">0x10000</span></samp>’ when the output section 97‘<samp><span class="samp">.text</span></samp>’ is defined, the linker will set the address of the 98‘<samp><span class="samp">.text</span></samp>’ section in the output file to be ‘<samp><span class="samp">0x10000</span></samp>’. 99 100 <p>The remaining lines define the ‘<samp><span class="samp">.data</span></samp>’ and ‘<samp><span class="samp">.bss</span></samp>’ sections in 101the output file. The linker will place the ‘<samp><span class="samp">.data</span></samp>’ output section 102at address ‘<samp><span class="samp">0x8000000</span></samp>’. After the linker places the ‘<samp><span class="samp">.data</span></samp>’ 103output section, the value of the location counter will be 104‘<samp><span class="samp">0x8000000</span></samp>’ plus the size of the ‘<samp><span class="samp">.data</span></samp>’ output section. The 105effect is that the linker will place the ‘<samp><span class="samp">.bss</span></samp>’ output section 106immediately after the ‘<samp><span class="samp">.data</span></samp>’ 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 ‘<samp><span class="samp">.text</span></samp>’ and ‘<samp><span class="samp">.data</span></samp>’ 111sections will probably satisfy any alignment constraints, but the linker 112may have to create a small gap between the ‘<samp><span class="samp">.data</span></samp>’ and ‘<samp><span class="samp">.bss</span></samp>’ 113sections. 114 115 <p>That's it! That's a simple and complete linker script. 116 117 </body></html> 118 119