17
1 uClinux course uClinux course Day 3 of 5 Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello The uclinux toolchain, elf format and ripping a “hello world” world”

uClinux course

Embed Size (px)

DESCRIPTION

uClinux course. Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”. toolchain. The compile process. cpp. cc1. as. ld. Toolchain – C compiler options. The most simple compile line gcc myprog.c Only call the preprocessor (cpp) and c compiler (cc1) gcc myprog.c -c - PowerPoint PPT Presentation

Citation preview

Page 1: uClinux course

1

uClinux courseuClinux courseuClinux courseuClinux course

Day 3 of 5Day 3 of 5The uclinux toolchain, elf format and ripping The uclinux toolchain, elf format and ripping a “hello world”a “hello world”

Page 2: uClinux course

2Author: D L Johnson

toolchaintoolchain

The compile processThe compile process

cc1cc1

asas

ldld

cppcpp

Page 3: uClinux course

3Author: D L Johnson

Toolchain – C compiler optionsToolchain – C compiler options

The most simple compile lineThe most simple compile line+ gcc myprog.c

Only call the preprocessor (cpp) and c compiler (cc1)Only call the preprocessor (cpp) and c compiler (cc1)+ gcc myprog.c -c

Show verbose output on the compile processShow verbose output on the compile process+ gcc -v myprog.c

Produce debugging information for gdbProduce debugging information for gdb+ gcc -g hello.c

Turns on more warningsTurns on more warnings+ gcc -Wall hello.c

Page 4: uClinux course

4Author: D L Johnson

Toolchain – C compiler optionsToolchain – C compiler options

Optimize the codeOptimize the code+ gcc –O1 myprog.c … optimise level1+ gcc –O2 myprog.c … optimize level2+ gcc –O3 myprog.c … highest level of optimization+ gcc –Os myprog.c … Optimize for size

Add extra include directories Add extra include directories + gcc -c hello.c -I/home/djohnson/include

Create assembler code from the c source codeCreate assembler code from the c source code+ gcc -S hello.c

Do not search the standard system directories for header Do not search the standard system directories for header files only the directories specified with –Ifiles only the directories specified with –I+ gcc -c –nostdinc -I/home/djohnson/include myprog.c

Page 5: uClinux course

5Author: D L Johnson

Toolchain – C compiler optionsToolchain – C compiler options

Predrfined macrosPredrfined macros+ gcc -DNO_MM myprog.c

Warn if function is declared or defined without argument Warn if function is declared or defined without argument typetype+ gcc -Wstrict-prototypes myprog.c

Compiling multiple source filesCompiling multiple source files+ gcc file1.c file2.c -o myprog

Alternative method for compiling multiple source filesAlternative method for compiling multiple source files+ gcc -c file1.c+ gcc -c file2.c+ gcc file1.o file2.o -o myprog

Page 6: uClinux course

6Author: D L Johnson

Assignment 2Assignment 2

Part1: Understand all the gcc options when uClinux Part1: Understand all the gcc options when uClinux compiles a filecompiles a file

arm-elf-gcc -D__KERNEL__ arm-elf-gcc -D__KERNEL__ -I/home/djohnson/uclinux_project/uClinux--I/home/djohnson/uclinux_project/uClinux-20030909/linux-2.4.x/include -Wall -Wstrict-20030909/linux-2.4.x/include -Wall -Wstrict-prototypes -Wno-trigraphs -O2 -fno-strict-prototypes -Wno-trigraphs -O2 -fno-strict-aliasing -fno-common -fno-common -pipe -fno-aliasing -fno-common -fno-common -pipe -fno-builtin -D__linux__ -g -DNO_MM -mapcs-32 -builtin -D__linux__ -g -DNO_MM -mapcs-32 -march=armv4 -mtune=arm7tdmi -mshort-load-bytes march=armv4 -mtune=arm7tdmi -mshort-load-bytes -msoft-float -nostdinc -iwithprefix include -msoft-float -nostdinc -iwithprefix include -DKBUILD_BASENAME=filemap -c -o filemap.o -DKBUILD_BASENAME=filemap -c -o filemap.o filemap.cfilemap.c

Page 7: uClinux course

7Author: D L Johnson

Toolchain – Linker optionsToolchain – Linker options

Specifying libraries archives to link inSpecifying libraries archives to link in+ gcc myprog.c –lmylib

– This will search in the default library paths for libraries libmylib.a, libmylib.so

Adding a library path to the list of paths to searchAdding a library path to the list of paths to search+ gcc myprog.c –L/home/djohnson/mylibraries

Strip all symbol information from output fileStrip all symbol information from output file+ gcc –s myprog.c

Only search library directories specified on common lineOnly search library directories specified on common line+gcc –nostdlib myprog.c

First function called when executable loadedFirst function called when executable loaded+ gcc –init mystart myprog.c

– Normally linker uses _init as the first function to call

Page 8: uClinux course

8Author: D L Johnson

Toolchain – binutils - objdumpToolchain – binutils - objdump

objdump displays information from object and executable objdump displays information from object and executable filesfiles

Disassemble executable fileDisassemble executable file+objdump –d a.out

Display contents of symbol tableDisplay contents of symbol table+objdump –t a.out

Disassemble from specified start addressDisassemble from specified start address+objdump –d –start-address=0x8000000

Page 9: uClinux course

9Author: D L Johnson

Toolchain – binutils - objcopyToolchain – binutils - objcopy

Objcopy copies and translates object and executable files Objcopy copies and translates object and executable files into different formats or copies sections out of the file into different formats or copies sections out of the file into a new fileinto a new file

Removing sections out of fileRemoving sections out of file+Objcopy –O binary –remove-section=.text linux

linux.data Changing the execute address of the binaryChanging the execute address of the binary

+Objcopy –O binary –change-section-vma .data=0x5000000 linux linux.data

Changing the load address of the binaryChanging the load address of the binary+Objcopy –O binary –change-section-

lma .data=0x2000000 linux linux.data

Page 10: uClinux course

10Author: D L Johnson

ELF file formatELF file format

ELF = Executable and Linkable formatELF = Executable and Linkable format Originally created by Unix system labsOriginally created by Unix system labs Used in virtually every recent UnixUsed in virtually every recent Unix Three main types of ELF filesThree main types of ELF files

+Relocatable file – object file to be linked with other+Executable+Shared object (library

Elf divides the file into sectionsElf divides the file into sections A sections is a collection of information of similar typeA sections is a collection of information of similar type As seen in the first lecture, for example executable code is As seen in the first lecture, for example executable code is

placed in .textplaced in .text Different to eg. MS-DOS binaries where everything is Different to eg. MS-DOS binaries where everything is

jumbled togetherjumbled together

Page 11: uClinux course

11Author: D L Johnson

ELF file formatELF file format

Advantage of sections architecture: when executable .text Advantage of sections architecture: when executable .text section placed in memory, these locations won’t changesection placed in memory, these locations won’t change

When you ask a kernel to load and run an executable it When you ask a kernel to load and run an executable it starts looking in the elf image header for clues on how to starts looking in the elf image header for clues on how to load the imageload the image

It moves .text into memory and marks read-onlyIt moves .text into memory and marks read-only Moves .data into user’s address space as read-writeMoves .data into user’s address space as read-write Finds location and size of .bss section, adds the pages of Finds location and size of .bss section, adds the pages of

memory to user’s address space and initialises this memory to user’s address space and initialises this memory section to zeromemory section to zero

THE uclinux kernel executable is a flat binary not ELF as THE uclinux kernel executable is a flat binary not ELF as nothing exists to load it – it must be self existentnothing exists to load it – it must be self existent

Only the files in the romfs file system are elf format as Only the files in the romfs file system are elf format as they are loaded by the kernelthey are loaded by the kernel

Page 12: uClinux course

12Author: D L Johnson

ELF file formatELF file format

For hello world For hello world > cat hello.c

void main(void)

{ printf(“Hello World”) }; TypeType

gcc –c hello.c ELF files contain a table to describe sections within the ELF files contain a table to describe sections within the

filefile TypeType

readelf –S hello.o

Page 13: uClinux course

13Author: D L Johnson

ELF file formatELF file format

The .rel.text section contains the relocations for the .text section of the fileThe .rel.text section contains the relocations for the .text section of the file

Notice printf needs to be relocatedNotice printf needs to be relocated

Page 14: uClinux course

14Author: D L Johnson

ELF file formatELF file format

This is done through the PLT (Procedure Link Table)This is done through the PLT (Procedure Link Table)

Page 15: uClinux course

15Author: D L Johnson

ELF file formatELF file format

Compare the assembler code of the hello.o object fileCompare the assembler code of the hello.o object filegcc –c hello.c

objdump –d hello.o To this assembler code of the a.out executableTo this assembler code of the a.out executable

gcc hello.c

objdump –d a.out Notice all the extra assembler in a.out such as the .init Notice all the extra assembler in a.out such as the .init

section, the .plt section which points to the real address section, the .plt section which points to the real address of printfof printf

Page 16: uClinux course

16Author: D L Johnson

ELF file formatELF file format

More info on the ELF file format can be found in More info on the ELF file format can be found in documents in day3 folderdocuments in day3 folder+Elf format presentation.pdf+Elf_format.pdf

Page 17: uClinux course

17Author: D L Johnson

Assignment 3Assignment 3

Take a standard hello.c fileTake a standard hello.c fileint main(void) {char name[10] = “albert”;printf(“my name is %s”, name);return 0

} Change “albert” to “david” using assembler (change the Change “albert” to “david” using assembler (change the

hello.s file)hello.s file)

Create a new executableCreate a new executable+Hint useful tools

– gcc will compile c code files (myfile.c) and assember files (myfile.s) If you’re bored do this with arm environmentIf you’re bored do this with arm environment

– Hint – try changing the Makefile in user/hello to create assembler code