neingeist
/
arduinisten
Archived
1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

127 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>avr-libc: Memory Sections</title>
<link href="dox.css" rel="stylesheet" type="text/css">
</head>
<body>
<center>
<table width="80%">
<tr>
<td align="left"><a href="http://www.nongnu.org/avr-libc/">AVR Libc Home Page</a></td>
<td align="center" colspan=4><img src="avrs.png" alt="AVRs" align="middle" border="0"></td>
<td align="right"><a href="https://savannah.nongnu.org/projects/avr-libc/">AVR Libc Development Pages</a></td>
</tr>
<tr>
<td align="center" width="13%"><a href="index.html">Main Page</a></td>
<td align="center" width="13%"><a href="pages.html">User Manual</a></td>
<td align="center" width="13%"><a href="modules.html">Library Reference</a></td>
<td align="center" width="13%"><a href="FAQ.html">FAQ</a></td>
<td align="center" width="13%"><a href="globals.html">Alphabetical Index</a></td>
<td align="center" width="13%"><a href="group__demos.html">Example Projects</a></td>
</tr>
</table>
</center>
<hr width="80%">
<!-- Generated by Doxygen 1.5.6 -->
<div class="contents">
<h1><a class="anchor" name="mem_sections">Memory Sections </a></h1><dl class="remark" compact><dt><b>Remarks:</b></dt><dd>Need to list all the sections which are available to the avr.</dd></dl>
<dl class="user" compact><dt><b>Weak Bindings</b></dt><dd>FIXME: need to discuss the .weak directive.</dd></dl>
The following describes the various sections available.<h2><a class="anchor" name="sec_dot_text">
The .text Section</a></h2>
The .text section contains the actual machine instructions which make up your program. This section is further subdivided by the .initN and .finiN sections dicussed below.<p>
<dl class="note" compact><dt><b>Note:</b></dt><dd>The <code>avr-size</code> program (part of binutils), coming from a Unix background, doesn't account for the .data initialization space added to the .text section, so in order to know how much flash the final program will consume, one needs to add the values for both, .text and .data (but not .bss), while the amount of pre-allocated SRAM is the sum of .data and .bss.</dd></dl>
<h2><a class="anchor" name="sec_dot_data">
The .data Section</a></h2>
This section contains static data which was defined in your code. Things like the following would end up in .data:<p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">char</span> err_str[] = <span class="stringliteral">"Your program has died a horrible death!"</span>;
<span class="keyword">struct </span>point pt = { 1, 1 };
</pre></div><p>
It is possible to tell the linker the SRAM address of the beginning of the .data section. This is accomplished by adding <b><code>-Wl,-Tdata,<em>addr</em></code></b> to the <code>avr-gcc</code> command used to the link your program. Not that <em><code>addr</code></em> must be offset by adding 0x800000 the to real SRAM address so that the linker knows that the address is in the SRAM memory space. Thus, if you want the .data section to start at 0x1100, pass 0x801100 at the address to the linker. [offset <a class="el" href="mem_sections.html#harvard_arch">explained</a>]<p>
<dl class="note" compact><dt><b>Note:</b></dt><dd>When using <code><a class="el" href="group__avr__stdlib.html#g4996af830ebe744d9678e5251dfd3ebd">malloc()</a></code> in the application (which could even happen inside library calls), <a class="el" href="malloc.html#malloc_extram">additional adjustments</a> are required.</dd></dl>
<h2><a class="anchor" name="sec_dot_bss">
The .bss Section</a></h2>
Uninitialized global or static variables end up in the .bss section.<h2><a class="anchor" name="sec_dot_eeprom">
The .eeprom Section</a></h2>
This is where eeprom variables are stored.<h2><a class="anchor" name="sec_dot_noinit">
The .noinit Section</a></h2>
This sections is a part of the .bss section. What makes the .noinit section special is that variables which are defined as such:<p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> foo __attribute__ ((section (<span class="stringliteral">".noinit"</span>)));
</pre></div><p>
will not be initialized to zero during startup as would normal .bss data.<p>
Only uninitialized variables can be placed in the .noinit section. Thus, the following code will cause <code>avr-gcc</code> to issue an error:<p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> bar __attribute__ ((section (<span class="stringliteral">".noinit"</span>))) = 0xaa;
</pre></div><p>
It is possible to tell the linker explicitly where to place the .noinit section by adding <code>-Wl,--section-start=.noinit=0x802000</code> to the <code>avr-gcc</code> command line at the linking stage. For example, suppose you wish to place the .noinit section at SRAM address 0x2000:<p>
<div class="fragment"><pre class="fragment">
$ avr-gcc ... -Wl,--section-start=.noinit=0x802000 ...
</pre></div><p>
<a class="anchor" name="harvard_arch"></a> <dl class="note" compact><dt><b>Note:</b></dt><dd>Because of the Harvard architecture of the AVR devices, you must manually add 0x800000 to the address you pass to the linker as the start of the section. Otherwise, the linker thinks you want to put the .noinit section into the .text section instead of .data/.bss and will complain.</dd></dl>
Alternatively, you can write your own linker script to automate this. [FIXME: need an example or ref to dox for writing linker scripts.]<h2><a class="anchor" name="sec_dot_init">
The .initN Sections</a></h2>
These sections are used to define the startup code from reset up through the start of main(). These all are subparts of the <a class="el" href="mem_sections.html#sec_dot_text">.text section</a>.<p>
The purpose of these sections is to allow for more specific placement of code within your program.<p>
<dl class="note" compact><dt><b>Note:</b></dt><dd>Sometimes, it is convenient to think of the .initN and .finiN sections as functions, but in reality they are just symbolic names which tell the linker where to stick a chunk of code which is <em>not</em> a function. Notice that the examples for <a class="el" href="mem_sections.html#asm_sections">asm</a> and <a class="el" href="mem_sections.html#c_sections">C</a> can not be called as functions and should not be jumped into.</dd></dl>
The <b>.initN</b> sections are executed in order from 0 to 9.<p>
<dl class="user" compact><dt><b>.init0:</b></dt><dd>Weakly bound to __init(). If user defines __init(), it will be jumped into immediately after a reset.</dd></dl>
<dl class="user" compact><dt><b>.init1:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.init2:</b></dt><dd>In C programs, weakly bound to initialize the stack, and to clear __zero_reg__ (r1).</dd></dl>
<dl class="user" compact><dt><b>.init3:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.init4:</b></dt><dd></dd></dl>
For devices with &gt; 64 KB of ROM, .init4 defines the code which takes care of copying the contents of .data from the flash to SRAM. For all other devices, this code as well as the code to zero out the .bss section is loaded from libgcc.a.<p>
<dl class="user" compact><dt><b>.init5:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.init6:</b></dt><dd>Unused for C programs, but used for constructors in C++ programs.</dd></dl>
<dl class="user" compact><dt><b>.init7:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.init8:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.init9:</b></dt><dd>Jumps into main().</dd></dl>
<h2><a class="anchor" name="sec_dot_fini">
The .finiN Sections</a></h2>
These sections are used to define the exit code executed after return from main() or a call to <a class="el" href="group__avr__stdlib.html#g137096a48cc0c731052cadfb69c39b34">exit()</a>. These all are subparts of the <a class="el" href="mem_sections.html#sec_dot_text">.text section</a>.<p>
The <b>.finiN</b> sections are executed in descending order from 9 to 0.<p>
<dl class="user" compact><dt><b>.finit9:</b></dt><dd>Unused. User definable. This is effectively where _exit() starts.</dd></dl>
<dl class="user" compact><dt><b>.fini8:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.fini7:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.fini6:</b></dt><dd>Unused for C programs, but used for destructors in C++ programs.</dd></dl>
<dl class="user" compact><dt><b>.fini5:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.fini4:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.fini3:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.fini2:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.fini1:</b></dt><dd>Unused. User definable.</dd></dl>
<dl class="user" compact><dt><b>.fini0:</b></dt><dd>Goes into an infinite loop after program termination and completion of any _exit() code (execution of code in the .fini9 -&gt; .fini1 sections).</dd></dl>
<h2><a class="anchor" name="asm_sections">
Using Sections in Assembler Code</a></h2>
Example:<p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="io_8h.html">avr/io.h</a>&gt;</span>
.section .init1,<span class="stringliteral">"ax"</span>,@progbits
ldi r0, 0xff
out _SFR_IO_ADDR(PORTB), r0
out _SFR_IO_ADDR(DDRB), r0
</pre></div><p>
<dl class="note" compact><dt><b>Note:</b></dt><dd>The <b><code>,"ax",@progbits</code></b> tells the assembler that the section is allocatable ("a"), executable ("x") and contains data ("@progbits"). For more detailed information on the .section directive, see the gas user manual.</dd></dl>
<h2><a class="anchor" name="c_sections">
Using Sections in C Code</a></h2>
Example:<p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="io_8h.html">avr/io.h</a>&gt;</span>
<span class="keywordtype">void</span> my_init_portb (<span class="keywordtype">void</span>) __attribute__ ((naked)) \
__attribute__ ((section (".init3")));
<span class="keywordtype">void</span>
my_init_portb (<span class="keywordtype">void</span>)
{
PORTB = 0xff;
DDRB = 0xff;
}
</pre></div><p>
<dl class="note" compact><dt><b>Note:</b></dt><dd>Section .init3 is used in this example, as this ensures the inernal <code>__zero_reg__</code> has already been set up. The code generated by the compiler might blindly rely on <code>__zero_reg__</code> being really 0. </dd></dl>
</div>
<hr width="80%">
<p><center>Automatically generated by Doxygen 1.5.6 on 4 Dec 2008.</center></p>
</body>
</html>