ARM Compiler toolchain Using the Linker - ARM Information Center
ARM Compiler toolchain Using the Linker - ARM Information Center
ARM Compiler toolchain Using the Linker - ARM Information Center
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
8.11 Placing functions and data at specific addresses<br />
<strong>Using</strong> scatter files<br />
Normally, <strong>the</strong> compiler produces RO, RW and ZI sections from a single source file. These<br />
regions contain all <strong>the</strong> code and data from <strong>the</strong> source file. To place a single function or data item<br />
at a fixed address, you must enable <strong>the</strong> linker to process <strong>the</strong> function or data separately from <strong>the</strong><br />
rest of <strong>the</strong> input files.<br />
The linker has two methods that enable you to place a section at a specific address:<br />
• You can create a scatter file that defines an execution region at <strong>the</strong> required address with<br />
a section description that selects only one section.<br />
• For a specially-named section <strong>the</strong> linker can get <strong>the</strong> placement address from <strong>the</strong> section<br />
name. These specially-named sections are called __at sections.<br />
To place a function or variable at a specific address it must be placed in its own section. There<br />
are several ways to do this:<br />
• Place <strong>the</strong> function or data item in its own source file.<br />
• Use __attribute__((at(address))) to place variables in a separate section at a specific<br />
address.<br />
• Use __attribute__((section("name"))) to place functions and variables in a named<br />
section.<br />
• Use <strong>the</strong> AREA directive from assembly language. In assembly code, <strong>the</strong> smallest locatable<br />
unit is an AREA.<br />
• Use <strong>the</strong> --split_sections compiler option to generate one ELF section for each function<br />
in <strong>the</strong> source file.<br />
This option results in a small increase in code size for some functions because it reduces<br />
<strong>the</strong> potential for sharing addresses, data, and string literals between functions. However,<br />
this can help to reduce <strong>the</strong> final image size overall by enabling <strong>the</strong> linker to remove unused<br />
functions when you specify armlink --remove.<br />
8.11.1 Example of placing a variable at a specific address without scatter-loading<br />
This example shows how to modify your source code to place code and data at specific<br />
addresses, and does not require a scatter file:<br />
1. Create <strong>the</strong> source file main.c containing <strong>the</strong> following code:<br />
#include <br />
extern int sqr(int n1);<br />
int gSquared __attribute__((at(0x5000))); // Place at 0x5000<br />
int main()<br />
{<br />
gSquared=sqr(3);<br />
printf("Value squared is: %d\n", gSquared);<br />
}<br />
2. Create <strong>the</strong> source file function.c containing <strong>the</strong> following code:<br />
int sqr(int n1)<br />
{<br />
return n1*n1;<br />
}<br />
3. Compile and link <strong>the</strong> sources:<br />
<strong>ARM</strong> DUI 0474C Copyright © 2010-2011 <strong>ARM</strong>. All rights reserved. 8-18<br />
ID080411 Non-Confidential