Secs-Background.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 Assembler "as".
  4. Copyright (C) 1991-2017 Free Software Foundation, Inc.
  5. Permission is granted to copy, distribute and/or modify this document
  6. under the terms of the GNU Free Documentation License, Version 1.3
  7. or any later version published by the Free Software Foundation;
  8. with no Invariant Sections, with no Front-Cover Texts, and with no
  9. Back-Cover Texts. A copy of the license is included in the
  10. section entitled "GNU Free Documentation License".
  11. -->
  12. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  13. <head>
  14. <title>Using as: Secs Background</title>
  15. <meta name="description" content="Using as: Secs Background">
  16. <meta name="keywords" content="Using as: Secs Background">
  17. <meta name="resource-type" content="document">
  18. <meta name="distribution" content="global">
  19. <meta name="Generator" content="makeinfo">
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  21. <link href="index.html#Top" rel="start" title="Top">
  22. <link href="AS-Index.html#AS-Index" rel="index" title="AS Index">
  23. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  24. <link href="Sections.html#Sections" rel="up" title="Sections">
  25. <link href="Ld-Sections.html#Ld-Sections" rel="next" title="Ld Sections">
  26. <link href="Sections.html#Sections" rel="prev" title="Sections">
  27. <style type="text/css">
  28. <!--
  29. a.summary-letter {text-decoration: none}
  30. blockquote.smallquotation {font-size: smaller}
  31. div.display {margin-left: 3.2em}
  32. div.example {margin-left: 3.2em}
  33. div.indentedblock {margin-left: 3.2em}
  34. div.lisp {margin-left: 3.2em}
  35. div.smalldisplay {margin-left: 3.2em}
  36. div.smallexample {margin-left: 3.2em}
  37. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  38. div.smalllisp {margin-left: 3.2em}
  39. kbd {font-style:oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. pre.smalldisplay {font-family: inherit; font-size: smaller}
  45. pre.smallexample {font-size: smaller}
  46. pre.smallformat {font-family: inherit; font-size: smaller}
  47. pre.smalllisp {font-size: smaller}
  48. span.nocodebreak {white-space:nowrap}
  49. span.nolinebreak {white-space:nowrap}
  50. span.roman {font-family:serif; 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" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  57. <a name="Secs-Background"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Ld-Sections.html#Ld-Sections" accesskey="n" rel="next">Ld Sections</a>, Up: <a href="Sections.html#Sections" accesskey="u" rel="up">Sections</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="AS-Index.html#AS-Index" title="Index" rel="index">Index</a>]</p>
  61. </div>
  62. <hr>
  63. <a name="Background"></a>
  64. <h3 class="section">4.1 Background</h3>
  65. <p>Roughly, a section is a range of addresses, with no gaps; all data
  66. &ldquo;in&rdquo; those addresses is treated the same for some particular purpose.
  67. For example there may be a &ldquo;read only&rdquo; section.
  68. </p>
  69. <a name="index-linker_002c-and-assembler"></a>
  70. <a name="index-assembler_002c-and-linker"></a>
  71. <p>The linker <code>ld</code> reads many object files (partial programs) and
  72. combines their contents to form a runnable program. When <code>as</code>
  73. emits an object file, the partial program is assumed to start at address 0.
  74. <code>ld</code> assigns the final addresses for the partial program, so that
  75. different partial programs do not overlap. This is actually an
  76. oversimplification, but it suffices to explain how <code>as</code> uses
  77. sections.
  78. </p>
  79. <p><code>ld</code> moves blocks of bytes of your program to their run-time
  80. addresses. These blocks slide to their run-time addresses as rigid
  81. units; their length does not change and neither does the order of bytes
  82. within them. Such a rigid unit is called a <em>section</em>. Assigning
  83. run-time addresses to sections is called <em>relocation</em>. It includes
  84. the task of adjusting mentions of object-file addresses so they refer to
  85. the proper run-time addresses.
  86. For the H8/300, and for the Renesas / SuperH SH,
  87. <code>as</code> pads sections if needed to
  88. ensure they end on a word (sixteen bit) boundary.
  89. </p>
  90. <a name="index-standard-assembler-sections"></a>
  91. <p>An object file written by <code>as</code> has at least three sections, any
  92. of which may be empty. These are named <em>text</em>, <em>data</em> and
  93. <em>bss</em> sections.
  94. </p>
  95. <p>When it generates COFF or ELF output,
  96. <code>as</code> can also generate whatever other named sections you specify
  97. using the &lsquo;<samp>.section</samp>&rsquo; directive (see <a href="Section.html#Section"><code>.section</code></a>).
  98. If you do not use any directives that place output in the &lsquo;<samp>.text</samp>&rsquo;
  99. or &lsquo;<samp>.data</samp>&rsquo; sections, these sections still exist, but are empty.
  100. </p>
  101. <p>When <code>as</code> generates SOM or ELF output for the HPPA,
  102. <code>as</code> can also generate whatever other named sections you
  103. specify using the &lsquo;<samp>.space</samp>&rsquo; and &lsquo;<samp>.subspace</samp>&rsquo; directives. See
  104. <cite>HP9000 Series 800 Assembly Language Reference Manual</cite>
  105. (HP 92432-90001) for details on the &lsquo;<samp>.space</samp>&rsquo; and &lsquo;<samp>.subspace</samp>&rsquo;
  106. assembler directives.
  107. </p>
  108. <p>Additionally, <code>as</code> uses different names for the standard
  109. text, data, and bss sections when generating SOM output. Program text
  110. is placed into the &lsquo;<samp>$CODE$</samp>&rsquo; section, data into &lsquo;<samp>$DATA$</samp>&rsquo;, and
  111. BSS into &lsquo;<samp>$BSS$</samp>&rsquo;.
  112. </p>
  113. <p>Within the object file, the text section starts at address <code>0</code>, the
  114. data section follows, and the bss section follows the data section.
  115. </p>
  116. <p>When generating either SOM or ELF output files on the HPPA, the text
  117. section starts at address <code>0</code>, the data section at address
  118. <code>0x4000000</code>, and the bss section follows the data section.
  119. </p>
  120. <p>To let <code>ld</code> know which data changes when the sections are
  121. relocated, and how to change that data, <code>as</code> also writes to the
  122. object file details of the relocation needed. To perform relocation
  123. <code>ld</code> must know, each time an address in the object
  124. file is mentioned:
  125. </p><ul>
  126. <li> Where in the object file is the beginning of this reference to
  127. an address?
  128. </li><li> How long (in bytes) is this reference?
  129. </li><li> Which section does the address refer to? What is the numeric value of
  130. <div class="display">
  131. <pre class="display">(<var>address</var>) - (<var>start-address of section</var>)?
  132. </pre></div>
  133. </li><li> Is the reference to an address &ldquo;Program-Counter relative&rdquo;?
  134. </li></ul>
  135. <a name="index-addresses_002c-format-of"></a>
  136. <a name="index-section_002drelative-addressing"></a>
  137. <p>In fact, every address <code>as</code> ever uses is expressed as
  138. </p><div class="display">
  139. <pre class="display">(<var>section</var>) + (<var>offset into section</var>)
  140. </pre></div>
  141. <p>Further, most expressions <code>as</code> computes have this section-relative
  142. nature.
  143. (For some object formats, such as SOM for the HPPA, some expressions are
  144. symbol-relative instead.)
  145. </p>
  146. <p>In this manual we use the notation {<var>secname</var> <var>N</var>} to mean &ldquo;offset
  147. <var>N</var> into section <var>secname</var>.&rdquo;
  148. </p>
  149. <p>Apart from text, data and bss sections you need to know about the
  150. <em>absolute</em> section. When <code>ld</code> mixes partial programs,
  151. addresses in the absolute section remain unchanged. For example, address
  152. <code>{absolute 0}</code> is &ldquo;relocated&rdquo; to run-time address 0 by
  153. <code>ld</code>. Although the linker never arranges two partial programs&rsquo;
  154. data sections with overlapping addresses after linking, <em>by definition</em>
  155. their absolute sections must overlap. Address <code>{absolute&nbsp;239}</code> in one
  156. part of a program is always the same address when the program is running as
  157. address <code>{absolute&nbsp;239}</code> in any other part of the program.
  158. </p>
  159. <p>The idea of sections is extended to the <em>undefined</em> section. Any
  160. address whose section is unknown at assembly time is by definition
  161. rendered {undefined <var>U</var>}&mdash;where <var>U</var> is filled in later.
  162. Since numbers are always defined, the only way to generate an undefined
  163. address is to mention an undefined symbol. A reference to a named
  164. common block would be such a symbol: its value is unknown at assembly
  165. time so it has section <em>undefined</em>.
  166. </p>
  167. <p>By analogy the word <em>section</em> is used to describe groups of sections in
  168. the linked program. <code>ld</code> puts all partial programs&rsquo; text
  169. sections in contiguous addresses in the linked program. It is
  170. customary to refer to the <em>text section</em> of a program, meaning all
  171. the addresses of all partial programs&rsquo; text sections. Likewise for
  172. data and bss sections.
  173. </p>
  174. <p>Some sections are manipulated by <code>ld</code>; others are invented for
  175. use of <code>as</code> and have no meaning except during assembly.
  176. </p>
  177. <hr>
  178. <div class="header">
  179. <p>
  180. Next: <a href="Ld-Sections.html#Ld-Sections" accesskey="n" rel="next">Ld Sections</a>, Up: <a href="Sections.html#Sections" accesskey="u" rel="up">Sections</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="AS-Index.html#AS-Index" title="Index" rel="index">Index</a>]</p>
  181. </div>
  182. </body>
  183. </html>