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