You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
7.4KB

  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 Arm Embedded Toolchain 10-2020-q4-major)
  5. version 2.35.1.
  6. Copyright (C) 1991-2020 Free Software Foundation, Inc.
  7. Permission is granted to copy, distribute and/or modify this document
  8. under the terms of the GNU Free Documentation License, Version 1.3
  9. or any later version published by the Free Software Foundation;
  10. with no Invariant Sections, with no Front-Cover Texts, and with no
  11. Back-Cover Texts. A copy of the license is included in the
  12. section entitled "GNU Free Documentation License". -->
  13. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>Simple Example (LD)</title>
  17. <meta name="description" content="Simple Example (LD)">
  18. <meta name="keywords" content="Simple Example (LD)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="LD-Index.html#LD-Index" rel="index" title="LD Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Scripts.html#Scripts" rel="up" title="Scripts">
  26. <link href="Simple-Commands.html#Simple-Commands" rel="next" title="Simple Commands">
  27. <link href="Script-Format.html#Script-Format" rel="prev" title="Script Format">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style: oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="Simple-Example"></a>
  58. <div class="header">
  59. <p>
  60. 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> &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>
  61. </div>
  62. <hr>
  63. <a name="Simple-Linker-Script-Example"></a>
  64. <h3 class="section">3.3 Simple Linker Script Example</h3>
  65. <a name="index-linker-script-example"></a>
  66. <a name="index-example-of-linker-script"></a>
  67. <p>Many linker scripts are fairly simple.
  68. </p>
  69. <p>The simplest possible linker script has just one command:
  70. &lsquo;<samp>SECTIONS</samp>&rsquo;. You use the &lsquo;<samp>SECTIONS</samp>&rsquo; command to describe the
  71. memory layout of the output file.
  72. </p>
  73. <p>The &lsquo;<samp>SECTIONS</samp>&rsquo; command is a powerful command. Here we will
  74. describe a simple use of it. Let&rsquo;s assume your program consists only of
  75. code, initialized data, and uninitialized data. These will be in the
  76. &lsquo;<samp>.text</samp>&rsquo;, &lsquo;<samp>.data</samp>&rsquo;, and &lsquo;<samp>.bss</samp>&rsquo; sections, respectively.
  77. Let&rsquo;s assume further that these are the only sections which appear in
  78. your input files.
  79. </p>
  80. <p>For this example, let&rsquo;s say that the code should be loaded at address
  81. 0x10000, and that the data should start at address 0x8000000. Here is a
  82. linker script which will do that:
  83. </p><div class="smallexample">
  84. <pre class="smallexample">SECTIONS
  85. {
  86. . = 0x10000;
  87. .text : { *(.text) }
  88. . = 0x8000000;
  89. .data : { *(.data) }
  90. .bss : { *(.bss) }
  91. }
  92. </pre></div>
  93. <p>You write the &lsquo;<samp>SECTIONS</samp>&rsquo; command as the keyword &lsquo;<samp>SECTIONS</samp>&rsquo;,
  94. followed by a series of symbol assignments and output section
  95. descriptions enclosed in curly braces.
  96. </p>
  97. <p>The first line inside the &lsquo;<samp>SECTIONS</samp>&rsquo; command of the above example
  98. sets the value of the special symbol &lsquo;<samp>.</samp>&rsquo;, which is the location
  99. counter. If you do not specify the address of an output section in some
  100. other way (other ways are described later), the address is set from the
  101. current value of the location counter. The location counter is then
  102. incremented by the size of the output section. At the start of the
  103. &lsquo;<samp>SECTIONS</samp>&rsquo; command, the location counter has the value &lsquo;<samp>0</samp>&rsquo;.
  104. </p>
  105. <p>The second line defines an output section, &lsquo;<samp>.text</samp>&rsquo;. The colon is
  106. required syntax which may be ignored for now. Within the curly braces
  107. after the output section name, you list the names of the input sections
  108. which should be placed into this output section. The &lsquo;<samp>*</samp>&rsquo; is a
  109. wildcard which matches any file name. The expression &lsquo;<samp>*(.text)</samp>&rsquo;
  110. means all &lsquo;<samp>.text</samp>&rsquo; input sections in all input files.
  111. </p>
  112. <p>Since the location counter is &lsquo;<samp>0x10000</samp>&rsquo; when the output section
  113. &lsquo;<samp>.text</samp>&rsquo; is defined, the linker will set the address of the
  114. &lsquo;<samp>.text</samp>&rsquo; section in the output file to be &lsquo;<samp>0x10000</samp>&rsquo;.
  115. </p>
  116. <p>The remaining lines define the &lsquo;<samp>.data</samp>&rsquo; and &lsquo;<samp>.bss</samp>&rsquo; sections in
  117. the output file. The linker will place the &lsquo;<samp>.data</samp>&rsquo; output section
  118. at address &lsquo;<samp>0x8000000</samp>&rsquo;. After the linker places the &lsquo;<samp>.data</samp>&rsquo;
  119. output section, the value of the location counter will be
  120. &lsquo;<samp>0x8000000</samp>&rsquo; plus the size of the &lsquo;<samp>.data</samp>&rsquo; output section. The
  121. effect is that the linker will place the &lsquo;<samp>.bss</samp>&rsquo; output section
  122. immediately after the &lsquo;<samp>.data</samp>&rsquo; output section in memory.
  123. </p>
  124. <p>The linker will ensure that each output section has the required
  125. alignment, by increasing the location counter if necessary. In this
  126. example, the specified addresses for the &lsquo;<samp>.text</samp>&rsquo; and &lsquo;<samp>.data</samp>&rsquo;
  127. sections will probably satisfy any alignment constraints, but the linker
  128. may have to create a small gap between the &lsquo;<samp>.data</samp>&rsquo; and &lsquo;<samp>.bss</samp>&rsquo;
  129. sections.
  130. </p>
  131. <p>That&rsquo;s it! That&rsquo;s a simple and complete linker script.
  132. </p>
  133. <hr>
  134. <div class="header">
  135. <p>
  136. 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> &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>
  137. </div>
  138. </body>
  139. </html>