Download presentation
1
Embedded Systems GNU tool
2
Reference Book (1)
3
Reference Book (2)
4
Useful Links http://www.sthoward.com/docs/ www.gnu.org
Some GNU Documents All about GNU Lecture on Linux & GNU
5
Outline Overview Toolchain Source RT Programming
6
Embedded System Memory Processor Data in Data out Control out
Control in
7
From Source to Machine Code
*.cpp *.s Compiler Assembler Lib 101001 Linker Executable Binary
8
Simple Example Source Files hello.c disp_dat.c startup.s hello.lds
C source, contain function main() disp_dat.c C source, functions in it is called by main() of hello.c startup.s Start up code for embedded board hello.lds Link description file Makefile Commands to build them all forward
9
Code of hello.c int main() { char a = ‘a’; char b = ‘b’; disp_dat(a); disp_dat(b); while (1); // loop forever return (0); // never comes here }
10
Code of disp_dat.c void disp_dat(U8 data) {
// Wait until send buffer empty while (!((*((volatile U32 *)(0x207098))) & 1)); // send on character into send buffer (*((volatile U32 *)(0x207040))) = (U16)data; }
11
Makefile will explain later
hello: hello.o disp_dat.o startup.o arm-linux-ld –o hello –T hello.lds \ hello.o \ disp_dat.o\ startuop.o hello.o: hello.c arm-linux-gcc –c hello.c disp_dat.o: disp_dat.c arm-linux-gcc –c disp_dat.c stratup.o: startup.s arm-linux-gcc –c startup.s will explain later Use command “make” to automatically execute the steps listed in Makefile forward
12
Download Code To ROM
13
Outline Overview Toolchain Source File RT Programming
14
CDK Cross Development Kits Toolchain C/C++ Compiler Assembler Linker
Runtime Lib
15
Components of Toolchain
Cross-Compiler gcc Binutil … Cross-Assembler as Cross-Archiver ar Cross-Linker ld ranlib libc `as‘ This should be the cross-assembler. `ld‘ This should be the cross-linker. `ar‘ This should be the cross-archiver: a program which can manipulate archive files (linker libraries) in the target machine's format. `ranlib‘ This should be a program to construct a symbol table in an archive file. Linux Kernel Headers
16
Build Toolchain
17
C/C++ Compiler C/C++ 10011100 source 10001011 Code 11101110 11111110
*.o, *.obj *.c, *.cpp, *.cxx *.h. *.hpp, *.hxx object file is not intended to be executed directly The structure of the file is usually COFF or ELF Example: arm-linux-gcc –c hello.c hello.c hello.o
18
Assembler 10011100 ASM code 10001011 11101110 11111110 *.o, *.obj
*.a, *.s, *.asm Example: arm-linux-gcc –c startup.s startup.s startup.o (note that gcc will automatically call assembler according to the postfix of *.s)
19
What’s in object File? Data: 1001001 1011100 1111100 BSS: 1100001
Binary Data Blocks in it Text:
20
Object File Example objdump –x file.o … … Sections:
Idx Name Size VMA LMA File off Algn 0 .text **2 CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 1 .data **2 CONTENTS, ALLOC, LOAD, DATA 2 .bss **0 ALLOC 3 .stab fc **2 CONTENTS, RELOC, READONLY, DEBUGGING 4 .stabstr **0 CONTENTS, READONLY, DEBUGGING 5 .comment aeb 2**0 CONTENTS, READONLY SYMBOL TABLE: l df *ABS* Helloworld.c
21
Function of linker (LD)
Data: BSS: Data: BSS: Data: BSS: Text: Data: BSS: Text: Text: Text: *.lds Link description file Object files, *.o Executable files *.exe, *.elf
22
Function of linker (contd.)
Combine Data Blocks Fill Symbol Address The work is under control of file *.lds
23
Outline Overview Toolchain Source File RT Programming
24
With/Without OS Without OS With OS Startup.s Main.c lds Makefile
25
Tasks of Startup Code Disable all INT Init Memory & Hardware
Copy Data from ROM RAM Initialize Data Area Initialize StacK Enable interrupts. Call main( ). backward
26
Name of Startup code startup.asm startup.s startup.a51 start.asm
start.s start.a51 crt0.s C Runtime
27
Example startup.s (1) Interrupt vectors
@ exception vectors .globl Helloworld_start Helloworld_start: b reset b UNDEF_Handler b SWI_Handler b PABT_Handler b DABT_Handler b NULL_Handler b IRQ_Handler b FIQ_Handler Example startup.s (1) Interrupt vectors
28
Define points to the beginning of the stack
@ Define stack position .extern Helloworld_end .equ UND_STACK_START, __STACK_START x @ 1kB .equ SVC_STACK_START, __STACK_START x @ 1kB .equ ABT_STACK_START, __STACK_START xc @ 1kB .equ IRQ_STACK_START, __STACK_START x @ 1kB .equ FIQ_STACK_START, __STACK_START x @ 1kB .equ SYS_STACK_START, __STACK_START xFFF0 @ UND_SP: .long UND_STACK_START SVC_SP: .long SVC_STACK_START ABT_SP: .long ABT_STACK_START IRQ_SP: .long IRQ_STACK_START FIQ_SP: .long FIQ_STACK_START SYS_SP: .long SYS_STACK_START Example startup.s (2) Define points to the beginning of the stack
29
Reset interrupt routine
Example startup.s (3) @ Reset handler reset: @ Init SDRAM LDR r1, =0x221000 STR r3, [r1] … LDR r3, =0x8212C267; STR r3, [r1] Reset interrupt routine (init SDRAM)
30
Example startup.s (4) Reset interrupt routine (init stacks)
@ setup IRQ stack mrs r0, cpsr bic r0, r0, #0x1f @ set CPSR to IRQ mode orr r0, r0, #0x msr cpsr, r ldr r13, =IRQ_SP @ setup sp_irq ldr sp, [r13] … @ setup SVC stack mrs r0, cpsr bic r0, r0, #0xff @ set CPSR to SVC mode orr r0, r0, #0x msr cpsr, r0 ldr r13, =SVC_SP @ setup sp_svc ldr sp, [r13] bl init_sect b main @ C main code Reset interrupt routine (init stacks) (call function to init data area) (Jump to main)
31
Compile All With Command Line
gcc -g –Wall –T hello.lds –o hello\ hello.c \ disp_dat.c \ startup.s The same function of Makefile (so why we use Makefile?) backward forward
32
Format of Several Widely Used Output File
ELF Include Symbol table and relocation information Binary Image Only include machine code Intel Hex File ASCII file, for programmer to download code Motorola S Record File Another ASCII file format for programmer
33
Generating Output Files
Gcc generate ELF file by default Objcopy Motorola Srecord File objcopy –O srec hello.elf hello.srec Intel HEX file objcopy –O ihex hello.elf hello.hex Binary File objcopy –O binary hello.elf hello.bin Objdump Display disassemble data objdump –S hello.elf Display section and symbol table location objdump –x hello.elf`
34
LDS File *.lds Ld script Linker Object files Executable files Data:
BSS: Data: BSS: Data: BSS: Data: BSS: Text: Text: Text: *.lds Text: vect: How to merge files Ld script Object files Executable files
35
LDS File (code example)
ENTRY(_start) /* list of our memory sections */ MEMORY { vect : o = 0, l = 1k rom : o = 0x400, l = 127k ram : o = 0x400000, l = 128k sram : o = 0xfffff000,l = 4k } RAM ROM CPU SRAM
36
LDS File (MEMORY command )
Target system's memory map Used in the SECTIONS Syntax MEMORY { name : o = origin, l = length name : o = origin, l = length }
37
LDS File (1) SECTIONS { .vect : { __vect_start = .; *(.vect); __vect_end = .; } > vect .text : { __text_start = .; *(.text) *(.strings) __text_end = .; } > rom } .bss : { __bss_start = . ; *(.bss) *(COMMON) __bss_end = . ; } > ram
38
LDS File (2) Object files Executable files .Data: 1001001 1011100
.Text: .Data: .BSS: .Data: .BSS: .Text: .Data: .BSS: Text: .Text: .vect: .BSS: Object files .vect: Executable files
39
Memory are spitted into sections
LDS File (3) RAM ROM Data: BSS: Memory are spitted into sections Text: Executable files
40
LDS File (Section Command)
Sections { … … /* initialized data */ .init : AT (__text_end) { __data_start = .; *(.data) __data_end = .; } > ram /* application stack */ .stack : { __stack_start = .; *(.stack) __stack_end = .; } > ram }
41
LDS File (AT directive )
ROM LDS File (AT directive ) Initialization data is stored in the ROM Data: BSS: RAM Text: Executable files
42
Initialization Data Section
ROM Executable Code *.lds .init : AT (__text_end) { __data_start = .; *(.data) __data_end = .; } > ram Constant Data Init Table For Global Data *.c RAM int global_dat=1234; void main() { disp_dat(global_dat) } Program Stack Global data & Static Data If global_dat not initialized, will print out wrong result
43
Function to Initialize Sections (1)
// init sections, run it before main() void init_sect(void) { extern void *__TEXT_END; extern void *__DATA_START, *__DATA_END; extern void *__BSS_START, *__BSS_END; char *src = (char*)(&__TEXT_END); char *dst = (char*)(&__DATA_START); // ROM has data at end of text; copy it. while (dst < (char*)(&__DATA_END)) *dst++ = *src++; // Zero bss for (dst = (char*)(&__BSS_START); dst < (char*)(&__BSS_END); dst++) *dst = 0; } .init : AT (__text_end) { __data_start = .; *(.data) __data_end = .; } > ram
44
Function to Initialize Sections (2)
… Sections { … = ALIGN(2); .text : { __TEXT_START = .; *(.text); __TEXT_END = .; } > ROM = ALIGN(2); .init : AT(__TEXT_END) { __DATA_START = .; *(.data); __DATA_END = .; } > RAM . = ALIGN(2); bss : { __BSS_START = .; *(.bss); __BSS_END = .; } > RAM … } Function to Initialize Sections (2) MEMORY { VECT : o = 0x , l = 0x ROM : o = 0x , l = 0x RAM : o = 0x , l = 0x } …
45
In the end of Start.s file
End of Startup file (3) In the end of Start.s file @ setup SVC stack mrs r0, cpsr bic r0, r0, #0xff @ set CPSR to SVC mode orr r0, r0, #0x13 msr cpsr, r0 ldr r13, =SVC_SP @ setup sp_svc ldr sp, [r13] bl init_sect b main @ C main code
46
Require a copy function in bootloader to move ROM image into RAM
Running Mode Executable Code & Constant Data ROM Mode RAM Mode ROM RAM Boot loader & Code Image Stack & Changing Data RAM Executable Code, Stack & Changing Data Require a copy function in bootloader to move ROM image into RAM
47
Running Mode (contd.) ROM Mode RAM Mode Simple Require smaller memory
Fixed code address Relative Small Code RAM Mode Complex Re-locatable code Faster Large Code (SDRAM)
48
Code Image (Maybe Compressed)
RAM Mode Bootloader Task of Bootloader Init Basic System Init RAM System Copy (de-compress) ROM Image into RAM Init .DATA section Jump To RAM to Run Code Code Image (Maybe Compressed) ROM RAM
49
Library File (1) glibc uclibc newlib Why use Library Files?
Share code with others How to build a library? ar -r ./mylib.a disp_dat.o sum_dat.o This command add 2 *.o files into library *.a (create the library if not found) How to assign a Library for code building? gcc -o hello hello.o mylib.a What’s the standard library? glibc uclibc newlib
50
Library File (2) Executable File A Executable File B 111010111
Library File
51
Using GCC (1) Function Of GCC Input File Name
Compile and link Source File Input File Name *.c, *.C, *.cxx, *,cpp, *.hxx, *.hpp, *.h, *.s, *.S Gcc find file type by ther suffix, and will automatically call related tool Any file name with no recognized suffix is send to linker directly C++
52
Gcc example (GCC options)
gcc -o –g hello hello.o mylib.a gcc -c –g –O3 –Wall hello.c
53
Makefile Why using make command? Execute a script, save time
Explore file dependence, shorten the compile time Provide extra code maintains script Clean built object file Install output file into desired directory Build a software in different compile option ..
54
Example hello.h hello.c disp_dat.c gcc –o hello hello.c disp_dat.c
void disp(int c); hello.c disp_dat.c #include “hello.h” int main() { disp(3); return 0; } #include <stdio.h> void disp(int c) { printf(“%d\n”, c); } gcc –o hello hello.c disp_dat.c
55
File Dependence hello.h hello.o gcc hello hello.c ld gcc disp_dat.o
disp_dat.c hello: hello.o disp_dat.o ld –o hello hello.o disp_dat.o hello.o: hello.c hello.h gcc –c hello.c disp_dat.o: disp_dat.c gcc –c disp_dat.c Makefile make
56
Details of make and Makefile (1)
# Define macros for name of compiler CC = gcc # Define a macr o for the CC flags CCFLAGS = -D_DEBUG -g -m486 # A rule for building a object file test.o: test.c test.h $(CC) -c $(CCFLAGS) test.c gcc –c –D_DEBUG –g –m486
57
Details of make and Makefile (2)
clean: rm -f *.o rm -f hello_mxl
58
Detail of make & Makefile (3)
CC = arm-elf-linux-gcc AS = arm-elf-linux-gcc LD = arm-elf-linux-ld OBJDUMP = arm-elf-linux-objdump BIN = arm-elf-linux-objcopy CPFLAGS = -Wall -pipe -g
59
Detail of make & Makefile (4)
CSRC = os_cpu.c ucos_ii.c mx1.c dataproc.c init_sect.c ASRC = os_cpu_a.s start.s OBJ = ucos_ii.o os_cpu_a.o os_cpu.o \ start.o mx1.o dataproc.o init_sect.o all: ucos_ii.elf .c.o: $(CC) -c $(CPFLAGS) -I$(INCDIR) $< .s.o: $(AS) -c $(ASFLAGS) -I$(INCDIR) $< ucos_ii.elf: $(OBJ) $(LD) $(LDFLAGS) $(OBJ) -o ucos_ii.elf $(OBJDUMP) -S ucos_ii.elf >> ucos_ii.dasm $(READELF) -a ucos_ii.elf >> ucos_ii.r $(NM) ucos_ii.elf >> ucos_ii.n clean: rm -f *.o rm -f *.map rm -f *.elf rm -f *.dasm rm -f *~ rm -f *.n rm -f *.r Detail of make & Makefile (4)
60
Other GNU Tools Tar Objdump Gdb tar xvfz foo.tar.gz
tar cvfz foo.tgz /opt/code Objdump objdump –S foo.elf objdump –x foo.elf Gdb
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.