1 real-time system design developing a cross compiler and libraries for a target system

21
1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

Post on 21-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

1

Real-Time System Design

Developing a Cross Compiler and libraries for a target system

Page 2: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

2

Why port a cross compiler?

• In real-time and embedded systems you will frequently come across new hardware for which there either isn’t a compiler or the current compiler doesn’t meet your specifications

• In this situation one way forward is to port a compiler for this new board

Page 3: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

3

What compiler to select?

• Short of writing a new compiler yourself you will probably want to port an existing compiler

• Selection of the base compiler is important

• The target hardware might force a choice – If it is only supported by one compiler– If it runs an OS that is close to a particular

compiler

Page 4: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

4

Selection Criteria

• There are a number of criteria that can be used– Mature compiler– Portable – Widely used/known– Large tool set– Runs on same/compatible hardware– Variety of OS/RTOS interfaces– Good library support– Supports a number of languages– Good documentation– Open source/open standard

Page 5: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

5

Hardware considerations

• If your selected compiler does not have support for the processor you will have to – Write a back end for the compiler to generate

the correct assembler code– Port many low level startup and compiler

routines– Write linker scripts and library files

Page 6: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

6

Operating System considerations

• If your compiler is not supported by the target OS then you will have to – Write initialisation routines – crt0.o for

example– Port library routines– Port some compiler startup routines

• Frequently boards have very minimal OS/monitors

Page 7: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

7

Using GCC as an example

• GCC scores very highly on many of the points in slide four

• I will use it as an example

• Although the name implies the C compiler and this is what I will concentrate on, it does support a number of other languages – Ada, C++, Java, etc

Page 8: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

8

Building a cross compiler

• Firstly select the host machine, although it can help if it is the same processor and OS, it is frequently the case that neither are supported on the host machine

• However it is very useful that the cross compiler runs in native mode on your host machine– i.e. you can compiler at least some of your cross

compiler with the host’s native compiler

• The link between GNU and Linux is obvious therefore I will use a GNU i686 Linux platform

Page 9: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

9

Cross compiler tools

• Aside from the compiler itself you will require a number of tools. Key ones are– A cross linker – gas in GNU– A cross assembler – gld in GNU– A downloader – will depend upon the target

• Others that will be very useful are– A librarian – ranlib in GNU– An object copier – objcopy in GNU– A debugger – GDB in GNU– A symbol table dumper – nm in GNU

Page 10: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

10

Building the cross utilities

• The GNU suite comes with a set of compiler utilities that are bundled together called binutils.

• You must build binutils before building the cross compiler.

• It is possible to use other tools with the compiler – it may be required by an on-board OS for example, however GNU does work well with its own tools.

• You must check compatibility issues between the GCC release and binutils

Page 11: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

11

Building the cross compiler #1

• The first pass of the cross compiler must be built with the native compiler

• This will be a i686 program that generates target output

• There are a variety of target outputs to configure GNU with

• The standard is– CPU-VENDOR-OS – i.e. – m68k-sun-bsd4.2– arm-unknown-elf– m88k-unknown-a.out

Page 12: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

12

Using a GNU template

• Selecting the correct template is important• Firstly

– Select the correct processor and check that the exact family is supported – i.e. m68k and 68040. This means that GCC can generate the correct assembler code

• Then– If you have the correct manufacturer and OS life is simple,

Otherwise, – Go for the nearest (or unknown) and at least get the code format

correct – coff, elf and so on

• In the later option you will have to do some porting and interfacing.

Page 13: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

13

Creating a GNU template

• GCC is built with portability in mind.• It is possible to create new assembler and

OS interfaces for GNU• This requires learning BDF to create

binary definition files for the new target• You may have to write a version of gas,

the GNU assembler.• This is non-trivial, and first off try to use

what already exists.

Page 14: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

14

Building the cross compiler #2

• As stated earlier, most of the cross compiler will be generated in native mode

• However GNU will need to generate target code and some of this may be difficult or impossible for the native compiler

• The code generation may require extra (target) library code that it can’t generate– For example, the m68k -68000 option only has 16 bit

multiple and divide instructions (mulu/s and divs/u) and the compiler requires 32 bit library functions. These must be created by either a 68000 compiler or assembler.

Page 15: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

15

Building the cross compiler #2

• The compiler itself requires some special startup functions written in target code– The compiler has some functions _exit for

example– These can be suppressed using the

–nostdfunc option

• All of the above cannot be generate by the native compiler. They may have to be hand coded or stubbed out if not required.

Page 16: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

16

Includes and libraries

• The standard include files - /usr/include – will probably not work with the target and a minimal set must generated

• Much is standard and can be copied, so can be taken from the target OS

• GCC, like most C compilers, requires a minimal, standard library, libc.a. Again this will have to be created.

Page 17: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

17

Startup files

• The cross compiler will generate all the target code from your program code and the linker will load in any library code, in the correct format.

• However there is an initialisation file that is required to initialise and execute the target code

• This is crt0.o on GNU – cstart on Crossware and OS9

Page 18: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

18

A GCC configuration settings

#!/bin/sh# This file was generated automatically by configure. Do not edit.# This directory was configured as follows:./configure --with-gcc-version-trigger=/usr/local/gcc/gcc-3.2/gcc/version.c --host=i686-pc-linux-gnu --target=m68k-unknown-coff --with-gnu-as --with-gnu-ld --enable-languages=c,c++ --with-headers=/usr/local/m68k-unknown-coff/include --with-libs=/usr/local/m68k-unknown-coff/lib/m68000 --with-newlib #

Page 19: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

19

A GCC gld linker scriptMEMORY{

ROM : ORIGIN = 0xf00000, LENGTH = 64K

RAM : ORIGIN = 0xf10000, LENGTH = 64K}SECTIONS{

fixed : {crt0.o(.text)*(.text)__data_start = .;

} > ROMidata : AT (__data_start) {

__idata_start = .;*(.data)__idata_end = .;

} > RAMvariable : {

__bss_start = . ;*(.bss)*(COMMON)__bss_end = .;

} > RAM__ram_size = (__bss_end -

__bss_start) + (__idata_end - __idata_start);}

Page 20: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

20

A crt0.s file#include "/usr/local/m68k-unknown-coff/include/asm.h"

.title "crt0.S for m68k-coff"

.align 2

.text

.global _start

.global startup

_header: .word 0x4afc // 0 Start marker.word 0 // 2 Checksum.long 0 // 4 Module size.long _name-_header // 8 Offset to mod name.long _start-_header //12 Offset to start of code.word 6 // 16 Module type.word 0 // 18 Spare.long 0 // 20 Spare.long __ram_size // 24 Fixed RAM required.long _header // 28 Absolute load address.word 0xa55a // 32 End marker

//=======================================================================//This space is used by sbin to fill in the module name. The only// restriction is that the names must be less than 31 characters with a 0// on the end. The module header points to this space.// _name: .long 0

.long 0

.long 0

.long 0

.long 0

.long 0

.long 0

.long 0

Page 21: 1 Real-Time System Design Developing a Cross Compiler and libraries for a target system

21

A crt0.s file/=====================================================================//Put the start and end addresses of the various sections on the stack// along with the command line tail then call the C startup code

_start: move.l #__data_start,-(a7)move.l #__idata_end,-(a7)move.l #__idata_start,-(a7)move.l #__bss_end,-(a7)move.l #__bss_start,-(a7)move.l a1,-(a7) // Pointer to command line tailmove.l #_name,-(a7) // save _name for argv[0] jsr startup

//=================================================================//drop down into exit incase the user doesn't. This should drop// control back to the ROM monitor, if there is one.//

.global _exit_exit: trap #0

.word 7