Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

37
Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen

Transcript of Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

Page 1: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

Building a Cross Compiler, Assembler and Linker

Designed by Prof. Peng-Sheng Chen

Page 2: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

2

What can You Learn?During embedded-system development, why do

we need to use cross compiler, assembler / linker and C standard library

How to build cross compiler、 assembler / linker

How to build cross C standard library

Page 3: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

3

Equipment Requirement

PC with Linux OS installed

Native gcc compiler installed

Internet access

Page 4: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

4

Software RequirementGNU binutils source code (version 2.16.1)

ftp://sources.redhat.com/pub/binutils/releases/binutils-2.16.1.tar.gz

http://ftp.gnu.org/gnu/binutils/binutils-2.16.1.tar.gz

GNU C/C++ compiler (version 3.3.6) ftp://sources.redhat.com/pub/gcc/releases/gcc-3.3.6/gcc-3.3.6.tar.

gz

ftp://ftp.nctu.edu.tw/computer-languages/C/gcc/releases/gcc-3.3.6/gcc-3.3.6.tar.gz

GNU newlib (version 1.14.0) ftp://sources.redhat.com/pub/newlib/newlib-1.14.0.tar.gz

Page 5: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

5

Outline

IntroductionBasic Linux commandsBuild cross assembler / linkerBuild cross compilerBuild cross C standard libraryTesting

Page 6: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

6

Outline

IntroductionBasic Linux commandsBuild cross assembler / linkerBuild cross compilerBuild cross C standard libraryTesting

Page 7: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

7

IntroductionA total solution for embedded system development

Hardware

Software => software-development tools, applications

Software-development tools

Compiler, assembler, linker, debugger and C standard library

Commercial => very high cost

Open source => free. Users can modify, distribute and use the software

Page 8: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

8

GNU BinutilsThe GNU binutils are a collection of binary tools

ld - the GNU linker as - the GNU assembler Other binary tools

ar - A utility for creating, modifying and extracting from archives

gprof - Displays profiling information objcopy - Copys and translates object files objdump - Displays information from object files …

Easy to port binutils to other platformsSuitable for embedded-system development

Page 9: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

9

GNU C/C++ CompilerRetargettable

Available from the Free Software Foundation

GPL Licensed

Free

Written by Richard Stallman originally (FSF)

Front-end, back-end system with support for many of each

Code quality is better than some known compiler, very close to DEC’s compiler on Alpha

Supplied as vendor compiler by NeXT, BeOS, Linux, BSD

Page 10: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

10

Front EndsC、 C++、 Objective CAda 95 (GNAT)Fortran77、 Fortran95PascalModula-2、Modula-3 Java (supported from gcc 3.0)

Java->Bytecode, Bytecode->native code Java->Native code

CobolChill (Cygnus, a language in the Modula II family used in

European telecommunications )

Page 11: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

11

Machines Supported by GCCEssentially all machines in widespread.Acorn (Advanced) RISC Machine.Alpha (DEC) Intel x86 Families、 i860、 i960.Motorola 680x0、 68C11、 DSP56000Hitachi SH、 H8300MIPS、 IBM PowerPC、 HP PA-RISC、 SUN SPARC…… Intel IA64, AMD x86-64Cell processor (used by PS3)

Page 12: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

12

GCC Execution

gcc

cppcc1

g++

Input file

output file

gas(assembler)

ld(linker)

Page 13: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

13

GNU C Standard Library

GLIBCNewlib

A C standard library intended for use on embedded systems

Usually work on any architecture with the addition of a few low-level routines

Page 14: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

14

Cross System-Development Tools

The characteristics of most embedded systems Limited memory

Diskless

No output screen

Poor performance

Sometimes, it is impossible to execute compiler, assembler / linker and debugger on target platform

For embedded system, we need cross system-development tools to help software development

Cross-compilerRemote debug

Page 15: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

15

Machine Classification

Build machine The machine builds cross-toolchains

Host machine The machine cross-toolchains will execute on

Target machine The assembly code of the machine cross-toolc

hains will produce output for

Page 16: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

16

Machine IdentificationThree items to identify machine

CPU type Company name System type

Example vax-dec-ultrix4.2 i386-redhat-linux m68k-coff arm-elf

Page 17: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

17

Outline

IntroductionBasic Linux commandsBuild cross assembler / linkerBuild cross compilerBuild cross C standard libraryTesting

Page 18: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

18

Basic Linux Commands (1)Command Description

cd Change directory

cd /home Change the current working directory to /home

cd .. Move to the parent directory of the current directory

cd ~ Move to the user's home directory

cp Copy files

cp file1 file2 Copy the file “file1” to the file “file2”

Page 19: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

19

Basic Linux Commands (2)Command Description

df Show the amount of disk space used on each mounted filesystem

ls List files

ls List files in the current working directory except those starting with . and only show the file name

ls -al List all files in the current working directory in long listing format showing permissions, ownership, size, and time and date stamp

Page 20: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

20

Basic Linux Commands (3)Command Description

cat Sends file contents to standard output

cat /etc/hosts Sends the contents of the “/etc/hosts" file to the screen

less Similar to the more command, but the user can page up and down through the file

less file1 The example displays the contents of file1

Page 21: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

21

Basic Linux Commands (4)Command Description

more Allows file contents or piped output to be sent to the screen one page at a time

more /etc/hosts Lists the contents of the "/etc/profile" file to the screen one page at a time

ls –al |more Performs a directory listing of all files and pipes the output of the listing through more. If the directory listing is longer than a page, it will be listed one page at a time

Page 22: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

22

Basic Linux Commands (5)Command Description

rm Delete the files

rm file1 Delete the file “file1”

mv Move or rename files

mv file1 file2 Move the file from “file1" to “file2". This effectively changes the name of “file1" to “file2”

mv file1 . Locates binaries and manual pages for the ls command

Page 23: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

23

Basic Linux Commands (6)Command Description

pwd Show the name of the current working directory

whereis Show where the binary, source and manual page files are for a command

whereis ls Locates binaries and manual pages for the ls command

man format and display the on-line manual pages

man ls Display the on-line manual of “ls”

Page 24: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

24

Setting Environment Variables (1)

Which shell do you use?

Command Description

ps Report process status

$ ps PID TTY TIME CMD13580 pts/11 00:00:00 bash13626 pts/11 00:00:00 ps$

$ ps PID TTY TIME CMD13580 pts/11 00:00:00 csh13626 pts/11 00:00:00 ps$

C shell

Bash Shell

Page 25: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

25

Setting Environment Variables (2)

Set PATH Ex: Add “/foo/bin” to PATH

$ export PATH=/foo/bin:$PATH

$ setenv PATH /foo/bin:$PATH

Bash Shell

C shell

Page 26: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

26

Log Information: Redirection

Before a command is executed, its input and output may be redirected Executable file: myexec Log file: log.txt Direct both standard output and standard error

to the file “log.txt”

$ myexec > log.txt 2>&1Bash Shell

$ myexec >& log.txtC Shell

Page 27: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

27

Outline

IntroductionBasic Linux commandsBuild cross assembler / linkerBuild cross compilerBuild cross C standard libraryTesting

Page 28: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

28

Build Cross-Binutilsdownload source code of GNU binutils

uncompress source code

configure binutils This step will detect system status, adjust related building parame

ters and generate corresponding Makefile file

--target=arm-elf sets target machine for ARM, and supported file format for ELF

--prefix=/foo sets the package will be installed in directory /foo

make

make install

configure --target=arm-elf --prefix=/foo

Page 29: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

29

Outline

IntroductionBasic Linux commandsBuild cross assembler / linkerBuild cross compilerBuild cross C standard libraryTesting

Page 30: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

30

Build Cross-Compiler (1)Add the directory of binutils executable to PATHdownload source code of GNU gccuncompress source code configure gcc

This step will detect system status, adjust related building parameters and generate corresponding Makefile file

--target=arm-elf sets target machine for ARM, and supported file format for ELF (The setting should consist with the setting in binutils)

--prefix=/foo sets the package will be installed in directory /foo (The setting should consist with the setting in binutils)

--enable-languages=c sets front-end will support C (That is we will build C compiler only)

--with-newlib sets the compiler will use newlib as standard C library

Page 31: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

31

Build Cross-Compiler (2)

makemake install

configure --target=arm-elf --prefix=/foo --enable-languages=c --with-newlib

Page 32: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

32

Outline

IntroductionBasic Linux commandsBuild cross assembler / linkerBuild cross compilerBuild cross C standard libraryTesting

Page 33: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

33

Build Cross-NewlibAdd the directory of binutils、 gcc executable to PATHdownload source code of GNU newlibuncompress source code configure newlib

This step will detect system status, adjust related building parameters and generate corresponding Makefile file

--target=arm-elf sets target machine for ARM, and supported file format for ELF (The setting should consist with the setting in binutils and gcc)

--prefix=/foo sets the package will be installed in directory /foo (The setting should consist with the setting in binutils and gcc)

makemake install

configure --target=arm-elf --prefix=/foo

Page 34: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

34

Directory Structure

/foo bin

include

lib

gcc-lib

arm-elf

man

info

share

bin

include

lib

Page 35: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

35

Outline

IntroductionBasic Linux commandsBuild cross assembler / linkerBuild cross compilerBuild cross C standard libraryTesting

Page 36: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

36

Testing

/foo/bin/arm-elf-gcc -S test1.c

/* test1.c */#include <stdio.h>

int main(void){ printf(“ Hello World\n” ); return 0;}

.file "test1.c" .section .rodata .align 2.LC0: .ascii "Hello World\n\000" .text .align 2 .global main .type main, %functionmain: @ args = 0, pretend = 0, frame = 0 @ frame_needed = 1, uses_anonymous_args = 0 mov ip, sp stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #4 ldr r0, .L2 bl printf mov r3, #0 mov r0, r3 ldmea fp, {fp, sp, pc}.L3: .align 2.L2: .word .LC0 .size main, .-main .ident "GCC: (GNU) 3.3.6"

Page 37: Building a Cross Compiler, Assembler and Linker Designed by Prof. Peng-Sheng Chen.

37

ReferenceLinux/UNIX commands

http://www.computerhope.com/unix.htm#04

GNU binutils http://sources.redhat.com/binutils

GCC http://gcc.gnu.org

GNU newlib http://sources.redhat.com/newlib