Application Note GCC Compiler for Cortex-M3 How to ... - Hitex
Application Note GCC Compiler for Cortex-M3 How to ... - Hitex
Application Note GCC Compiler for Cortex-M3 How to ... - Hitex
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Hitex</strong> Germany<br />
– Head Quarters –<br />
Greschbachstr. 12<br />
76229 Karlsruhe<br />
Germany<br />
� +049-721-9628-0<br />
Fax +049-721-9628-149<br />
E-mail: Sales@hitex.de<br />
WEB: www.hitex.de<br />
<strong>Hitex</strong> UK<br />
Warwick University<br />
Science Park<br />
Coventry CV47EZ<br />
United Kingdom<br />
� +44-24-7669-2066<br />
Fax +44-24-7669-2131<br />
E-mail: Info@hitex.co.uk<br />
WEB: www.hitex.co.uk<br />
<strong>Application</strong> <strong>Note</strong><br />
<strong>Hitex</strong> USA<br />
2062 Business Center Drive<br />
Suite 230<br />
Irvine, CA 92612<br />
U.S.A.<br />
� 800-45-HITEX (US only)<br />
� +1-949-863-0320<br />
Fax +1-949-863-0331<br />
E-mail: Info@hitex.com<br />
WEB: www.hitex.com<br />
<strong>GCC</strong> <strong>Compiler</strong> <strong>for</strong> <strong>Cortex</strong>-<strong>M3</strong><br />
<strong>How</strong> <strong>to</strong> optimize V4.4.0 Settings<br />
This documentation demonstrates how <strong>to</strong> use and<br />
optimize the <strong>GCC</strong> compiler settings <strong>for</strong> <strong>Cortex</strong>-<strong>M3</strong><br />
(Thumb2) code generation including detailed<br />
examples on 'eh-frame', 'stubs' and 'vec<strong>to</strong>rs'.<br />
Architecture: ARM/<strong>Cortex</strong><br />
Author: Stefan Grohmann / <strong>Hitex</strong> Germany<br />
Revision: 10/2009 - 001<br />
© Copyright 2009 - <strong>Hitex</strong> Development Tools GmbH<br />
All rights reserved. No part of this document may be copied or reproduced in any <strong>for</strong>m or by any means without prior written consent of <strong>Hitex</strong> Development Tools. <strong>Hitex</strong> Development Tools retains<br />
the right <strong>to</strong> make changes <strong>to</strong> these specifications at any time, without notice. <strong>Hitex</strong> Development Tools makes no commitment <strong>to</strong> update nor <strong>to</strong> keep current the in<strong>for</strong>mation contained in this<br />
document. <strong>Hitex</strong> Development Tools makes no warranty of any kind with regard <strong>to</strong> this material, including, but not limited <strong>to</strong>, the implied warranties of merchantability and fitness <strong>for</strong> a particular<br />
purpose. <strong>Hitex</strong> Development Tools assumes no responsibility <strong>for</strong> any errors that may appear in this document. DProbe, <strong>Hitex</strong>, HiTOP, Tan<strong>to</strong>, and Tantino are trademarks of <strong>Hitex</strong> Development<br />
Tools. All trademarks of other companies used in this document refer exclusively <strong>to</strong> the products of these companies.<br />
<strong>Application</strong><strong>Note</strong>.dot - 11/2007 - 005
<strong>Application</strong> <strong>Note</strong><br />
Preface<br />
In order <strong>to</strong> keep you up-<strong>to</strong>-date with the latest developments on our products, we provide <strong>Application</strong><br />
<strong>Note</strong>s containing additional <strong>to</strong>pics, special hints, examples and detailed procedures etc.<br />
For more in<strong>for</strong>mation on the current software and hardware revisions, as well as our update service,<br />
please visit www.hitex.de, www.hitex.co.uk or www.hitex.com.<br />
Contents<br />
1 What is the Motivation? 3<br />
1.1 Error Occurrences 3<br />
2 <strong>Compiler</strong> Settings <strong>for</strong> <strong>Cortex</strong>-<strong>M3</strong> Derivatives 4<br />
2.1 Assembler Parameters 4<br />
2.2 Assembler Code 5<br />
2.3 C <strong>Compiler</strong> Parameters 6<br />
2.4 C Code 7<br />
2.5 Linker Parameters 8<br />
2.6 Linker Description File (ld File) 9<br />
3 Conclusion 12<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 2/12
<strong>Application</strong> <strong>Note</strong><br />
1 What is the Motivation?<br />
The usage of the <strong>GCC</strong> compiler <strong>for</strong> ARM/<strong>Cortex</strong> version 4.4.0 provided by <strong>Hitex</strong> is a complex issue<br />
and sometimes not quite easy <strong>to</strong> handle <strong>for</strong> the user. Some settings may result in erroneous outputs of<br />
the compiler or linker when used <strong>for</strong> different target plat<strong>for</strong>ms.<br />
This document is intended <strong>to</strong> give support <strong>to</strong> the users on how <strong>to</strong> set and optimize some common<br />
parameters.<br />
The following code segments and settings are <strong>to</strong> be used with the HiTOP development environment<br />
and the current GNU C compiler version 4.4.0.<br />
The examples in chapter <strong>Compiler</strong> Settings <strong>for</strong> <strong>Cortex</strong>-<strong>M3</strong> Derivatives are chosen <strong>to</strong> give an overview<br />
and <strong>to</strong> enable the user <strong>to</strong> have a complete bunch of settings <strong>for</strong> a successful start of projects.<br />
<strong>Note</strong> that all examples are only usable <strong>for</strong> <strong>Cortex</strong>-<strong>M3</strong> target devices.<br />
For further in<strong>for</strong>mation on options <strong>for</strong> the compiler and linker, please have a look at the <strong>GCC</strong> web<br />
references, e.g. http://gcc.gnu.org/onlinedocs.<br />
To learn more about the HiTOP environment and compiler settings in the IDE, refer <strong>to</strong> the help system<br />
installed with HiTOP.<br />
1.1 Error Occurrences<br />
Errors occurring when compiling or using the debugger <strong>to</strong> single step compiled code are mostly the<br />
result of using wrong parameters ('options') <strong>for</strong> the compiler and linker.<br />
These options are set in the HiTOP environment related <strong>to</strong> complete projects or single files, and often<br />
a separate file with additional options <strong>for</strong> the linker is used.<br />
The main error behavior is:<br />
(1) Debug in<strong>for</strong>mation can not be used or is not bound <strong>to</strong> files and their code. In disassembly view,<br />
the modules are mixed up and in the module view empty labels are visible.<br />
(2) The linker terminates with an error<br />
overlapping section .data<br />
In both cases, the following sections should be read with attention.<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 3/12
<strong>Application</strong> <strong>Note</strong><br />
2 <strong>Compiler</strong> Settings <strong>for</strong> <strong>Cortex</strong>-<strong>M3</strong> Derivatives<br />
The common settings of the compiler call in the HiTOP environment are divided in<strong>to</strong><br />
• assembler-related<br />
• C-compiler-related<br />
• linker-related<br />
call parameters which will be covered in the following.<br />
2.1 Assembler Parameters<br />
In general, the assembler is running fine with the following options:<br />
-mcpu=cortex-m3 -gdwarf2 –mthumb<br />
-mcpu=cortex-m3 Specifies the core architecture used.<br />
The alternative –marm[vx] is not applicable.<br />
-gdwarf2 Specifies the debug in<strong>for</strong>mation output.<br />
Useful if you want <strong>to</strong> debug the code.<br />
-mthumb Specifies the code generation.<br />
It should be applied even if the architecture only specifies the Thumb2<br />
instruction set.<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 4/12
<strong>Application</strong> <strong>Note</strong><br />
2.2 Assembler Code<br />
Specific entries must be set in the assembler code:<br />
.text<br />
; /* <strong>to</strong> specify that code is following */<br />
.syntax unified<br />
.global _MyASMRoutine<br />
…<br />
.type _ MyASMRoutine, function<br />
.equ pattern1, 0xAAAAAAAA<br />
; /*.bit pattern */<br />
.thumb<br />
; /* this causes thumb code generation */<br />
_ MyASMRoutine:<br />
; /* Push ALL registers <strong>to</strong> stack */<br />
Push {r0-r12,r14}<br />
; /* some instructions */<br />
movw r0, #0x00AA<br />
cmp r0, #0xAA<br />
bne _ exitLabel<br />
; /* some more instructions */<br />
movw r0, #0xAA00<br />
lsr r0, #8<br />
cmp r0, #0xAA<br />
bne _ exitLabel<br />
_exitLabel:<br />
; /* Pop the stack back */<br />
pop {r0-r12,r14}<br />
; /* Branch back */<br />
bx lr<br />
.end<br />
If the functions are called from a C file without taking care of the architecture, the code may produce<br />
warnings with the linker, e.g. that "ARM calls" are enabled or that the "thumb-interworking" is not<br />
enabled.<br />
This behavior occurs because of the addressing method used by the C compiler according <strong>to</strong> the<br />
<strong>Cortex</strong>-<strong>M3</strong> architecture.<br />
Normally, the C compiler does not produce any long calls so that the addressing is limited. In other<br />
ARM architectures this is fixed by using the thumb-interworking <strong>to</strong> enable the compiler <strong>to</strong> build long<br />
veneers.<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 5/12
<strong>Application</strong> <strong>Note</strong><br />
2.3 C <strong>Compiler</strong> Parameters<br />
-c -gdwarf-2 -MD -O0 -trigraphs -mcpu=cortex-m3 -mthumb -Wall -fsigned-char -mlittle-endian<br />
-xc -mno-thumb-interwork -mno-tpcs-frame -mlong-calls<br />
-c Compiles or assembles the source files, but does not link.<br />
-gdwarf-2 Produces debugging in<strong>for</strong>mation in DWARF version 2 <strong>for</strong>mat.<br />
-MD The compiler generates intermediate files with the suffix '.d'.<br />
-O0 Do not optimize (best <strong>for</strong> debugging).<br />
-trigraphs Supports ISO C trigraphs.<br />
-mcpu=cortex-m3 Specifies the core architecture used.<br />
-mthumb Specifies the code generation. This option should be applied even if<br />
the architecture only specifies the Thumb2 instruction set.<br />
-Wall Prints all warnings.<br />
-fsigned-char Lets the type char be signed, like signed char.<br />
-mlittle-endian Generates code <strong>for</strong> a processor running in 'Little Endian' mode.<br />
-xc Specifies the source language; here: C<br />
-mno-thumb-interwork Does not use the interworking.<br />
-mno-tpcs-frame Generates no stack frame that is compliant with the Thumb Procedure<br />
Call Standard <strong>for</strong> all non-leaf functions.<br />
-mlong-calls Tells the compiler <strong>to</strong> per<strong>for</strong>m function calls by first loading the address<br />
of the function in<strong>to</strong> a register and then per<strong>for</strong>ming a subroutine call on<br />
this register. *)<br />
*) This parameter is used <strong>to</strong> generate a call over the whole address area which solves the problem <strong>to</strong> call the assembler<br />
routines.<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 6/12
<strong>Application</strong> <strong>Note</strong><br />
2.4 C Code<br />
Normally, the C code does not include any arguments <strong>for</strong> the linker or compiler. This will generate<br />
sections in<strong>for</strong>mation <strong>for</strong> the sections ".text", ".data" and ".bss". If modules or variables are intended <strong>to</strong><br />
be treated in a special way, the code must include this in<strong>for</strong>mation.<br />
Using Linker-Generated Address Labels<br />
extern unsigned long _etext;<br />
extern unsigned long _data;<br />
extern unsigned long _edata;<br />
extern unsigned long _bss;<br />
extern unsigned long _ebss;<br />
extern unsigned long _stack<strong>to</strong>p;<br />
The labels are available <strong>for</strong> usage in the code. The label "_stack<strong>to</strong>p" is defined in the linker file and<br />
defines the upper end of the stack area in the SRAM of the device. The label will be filled by the linker.<br />
All other labels are also generated by the linker and provide in<strong>for</strong>mation on the SRAM usage.<br />
Placing Constant Data <strong>to</strong> a Specified Location<br />
typedef void (* const FuncPointer)(void);<br />
A type "FuncPointer" is defined and a table of entries is generated which builds the vec<strong>to</strong>r table of a<br />
<strong>Cortex</strong>-<strong>M3</strong> controller. This table must be located at the address 0x0000 0000 on each <strong>Cortex</strong>-<strong>M3</strong><br />
controller.<br />
The address is well-known but the linker must be in<strong>for</strong>med about the way <strong>to</strong> handle this. The section<br />
".vec<strong>to</strong>rs" which is defined here, is used by the linker file <strong>to</strong> specify the placement (see section Linker<br />
Description File).<br />
void Reset(void);<br />
FuncPointer interrupts[] __attribute__ ((section(".vec<strong>to</strong>rs"))) =<br />
{<br />
(IntFctPointer)&_stack<strong>to</strong>p, // The initial stack pointer (see above)<br />
Reset, // first vec<strong>to</strong>r <strong>to</strong> Reset routine<br />
…<br />
Copying the Initialized Data<br />
unsigned long *Src, *Dest;<br />
The pointers defined are used <strong>to</strong> simply copy the ".data" section <strong>to</strong> the SRAM at the start of the<br />
application.<br />
// Initialize data<br />
// Copy the data segment initializers from flash <strong>to</strong> SRAM.<br />
Src = &_etext;<br />
<strong>for</strong>(Dest = &_data; Dest < &_edata; )<br />
{<br />
*Dest++ = *Src++;<br />
}<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 7/12
<strong>Application</strong> <strong>Note</strong><br />
Clearing the Uninitialized Data<br />
When deleting the uninitialized SRAM segment, the start and end must be known. The linker can<br />
provide this in<strong>for</strong>mation.<br />
// Zero fill the bss segment.<br />
<strong>for</strong>(Dest = &_bss; Dest < &_ebss; )<br />
{<br />
*Dest++ = 0;<br />
}<br />
In this case, the application must be called from here:<br />
// Call main.<br />
main();<br />
2.5 Linker Parameters<br />
The linker can be called with the following options:<br />
--cref -t -static -nostartfiles -Map=.\objects\main.map -stats -lc -lgcc<br />
--cref Does not output a cross reference table <strong>to</strong> the map file.<br />
-t Traces linked files.<br />
-static On systems supporting dynamic linking, this prevents linking with<br />
the shared libraries.<br />
-nostartfiles Does not use the standard system startup files when linking.<br />
-Map=.\objects\main.map Generates a map file.<br />
-stats Displays statistical outputs.<br />
-lc Uses the C library.<br />
-lgcc Uses the <strong>GCC</strong> library.<br />
In addition, HiTOP inserts the linker file option (-T[file]) <strong>to</strong> the calling options. The linker file describes<br />
further code segmentation and memory usage.<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 8/12
<strong>Application</strong> <strong>Note</strong><br />
2.6 Linker Description File (ld File)<br />
To enable the linker <strong>to</strong> take care of the derivative-specific memory layout and passing the code<br />
optimally in<strong>to</strong> the Flash and RAM segments, the linker has <strong>to</strong> know about this.<br />
Adding libraries <strong>to</strong> the linker is always in conjunction with the knowledge of their location. The linker<br />
must be referenced <strong>to</strong> the locations.<br />
Due <strong>to</strong> the variety of the current <strong>GCC</strong> versions, the path must be adjusted <strong>to</strong> the used version:<br />
SEARCH_DIR( "$(ToolDir)\..\arm-hitex-elf\lib\thumb2" )<br />
/* Path setting depends on the gnu compiler version */<br />
… or all possible paths must be entered as follows:<br />
SEARCH_DIR("$(ToolDir)\..\lib\gcc\arm-hitex-elf\4.3.0\thumb\thumb2" )<br />
SEARCH_DIR("$(ToolDir)\..\lib\gcc\arm-hitex-elf\4.3.1\thumb\thumb2" )<br />
SEARCH_DIR("$(ToolDir)\..\lib\gcc\arm-hitex-elf\4.3.3\thumb\thumb2" )<br />
SEARCH_DIR("$(ToolDir)\..\lib\gcc\arm-hitex-elf\4.4.0\thumb2" )<br />
SEARCH_DIR("$(ToolDir)\..\lib\gcc\arm-hitex-elf\$(GNU_CORTEX_VERSION)\thumb2" )<br />
The input is specified either by the file names or byte list which is generated by HiTOP:<br />
INPUT (<br />
/* HiTOP will au<strong>to</strong>matically put in here all object files <strong>to</strong> be linked.<br />
Leave this unchanged! */<br />
$(LinkObjects)<br />
)<br />
The target-specific parameters contain in<strong>for</strong>mation about the physical memory layout of the target<br />
derivative:<br />
/* Target Specific Parameters */<br />
MEMORY<br />
{<br />
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x80000<br />
SRAM (rwx) : ORIGIN = 0x10000000, LENGTH = 0x8000<br />
}<br />
Sections define physical opcode or data areas which are connected <strong>to</strong> specified or default code,<br />
constants or variables. In general the sections .text, .data and .bss are defined by default.<br />
Additional sections can be defined and located in the physical memory:<br />
/* Layout In<strong>for</strong>mation */<br />
/* Section Definitions */<br />
SECTIONS<br />
{<br />
The first section is the .text section containing the startup code and all other opcode. The start<br />
address is the first address defined or zero:<br />
.text :<br />
{<br />
KEEP(*(.vec<strong>to</strong>rs .vec<strong>to</strong>r.*))<br />
The label vec<strong>to</strong>rs must be located at the very beginning and must not be changed (see section<br />
C Code).<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 9/12
<strong>Application</strong> <strong>Note</strong><br />
Next is the startup code and an address label is generated (_startup_code_end__):<br />
__startup_code__ = .;<br />
$(TargetDir)startup.o (.text) /* Startup code */<br />
__startup_code_end__ = .;<br />
Then the rest of the code is put in<strong>to</strong> the .text section and is aligned <strong>to</strong> 4 bytes:<br />
*(.text .text.*)<br />
*(.gnu.linkonce.t.*)<br />
*(.glue_7)<br />
*(.glue_7t)<br />
. = ALIGN(4);<br />
.Eh_frame is recommended <strong>to</strong> be placed by the linker:<br />
*(.rodata .rodata*)<br />
*(.gnu.linkonce.r.*)<br />
*(.eh_frame)<br />
. = ALIGN(4);<br />
With the address label _etext the .text section is completed and all is located in the physical memory<br />
area defined as "FLASH":<br />
_etext = .;<br />
} > FLASH<br />
The next section is defined as .data containing all the initializing data <strong>for</strong> the pre-initialized variables:<br />
.data : AT (_etext)<br />
{<br />
Start the section at the end of the .text section <strong>to</strong> reside in the non-volatile memory. The startup code<br />
must copy this <strong>to</strong> the RAM be<strong>for</strong>e using any initialized variables. Add all segments defined as ".data":<br />
_data = .;<br />
*(.data .data.*)<br />
*(.gnu.linkonce.d*)<br />
The linker generates an address label edata and locates the heap with size 0x2000 followed by the<br />
stack:<br />
. = ALIGN(4);<br />
_edata = . ;<br />
/* HEAP!!! */<br />
. = . + 0x2000; /* reserved <strong>for</strong> heap */<br />
. = ALIGN(4);<br />
On the target segment (SRAM) at the very end, the _stack<strong>to</strong>p address is set <strong>to</strong> an absolute address<br />
of 0x8000-4:<br />
*(.stack .stack.*)<br />
. = ALIGN(4);<br />
_stack<strong>to</strong>p = 0x8000-4; /* Top of Stack */<br />
The address model <strong>for</strong> SRAM space is used here:<br />
} > SRAM<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 10/12
<strong>Application</strong> <strong>Note</strong><br />
Uninitialized data are located in the .bss section. The section can be cleared by the startup code if it is<br />
located and the address labels are known in the code.<br />
An address label _bss is built and all elements are filled in<strong>to</strong> this section:<br />
/* .bss section which is used <strong>for</strong> uninitialized data */<br />
.bss (NOLOAD) :<br />
{<br />
_bss = . ;<br />
Generate an address label _ebss:<br />
*(.bss .bss.*)<br />
*(.gnu.linkonce.b*)<br />
*(COMMON)<br />
. = ALIGN(4);<br />
_ebss = . ;<br />
} > SRAM<br />
Be<strong>for</strong>e ending the SECTION part of the linker file, the overall (natural) alignment of the device is<br />
specified <strong>to</strong> 4 byte:<br />
}<br />
. = ALIGN(4);<br />
_end = . ;<br />
PROVIDE (end = .);<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 11/12
<strong>Application</strong> <strong>Note</strong><br />
3 Conclusion<br />
The setup of linker and compiler is a complex issue and should be modified only by developers with<br />
expertise on <strong>GCC</strong> <strong>to</strong>ols.<br />
In general, all necessary settings have been already made in the examples available <strong>for</strong> the<br />
corresponding HiTOP installation and templates.<br />
<strong>Hitex</strong> permanently updates its web sites with additional in<strong>for</strong>mation and examples.<br />
So <strong>for</strong> the user it is an absolute must being updated regularly.<br />
Taking in<strong>to</strong> account the fact that GNU compilers are under permanent development, the<br />
version and the changes of newer compilers should be carefully checked and evaluated<br />
be<strong>for</strong>e updating.<br />
© Copyright 2009 <strong>Hitex</strong> Development Tools GmbH Page 12/12<br />
�