Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”"— Presentation transcript:

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

2 2 Author: D L Johnson toolchain The compile process cc1 as ld cpp

3 3 Author: D L Johnson Toolchain – C compiler options u The most simple compile line +gcc myprog.c u Only call the preprocessor (cpp) and c compiler (cc1) +gcc myprog.c -c u Show verbose output on the compile process +gcc -v myprog.c u Produce debugging information for gdb +gcc -g hello.c u Turns on more warnings +gcc -Wall hello.c

4 4 Author: D L Johnson Toolchain – C compiler options u Optimize 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 u Add extra include directories +gcc -c hello.c -I/home/djohnson/include u Create assembler code from the c source code +gcc -S hello.c u Do not search the standard system directories for header files only the directories specified with –I +gcc -c –nostdinc -I/home/djohnson/include myprog.c

5 5 Author: D L Johnson Toolchain – C compiler options u Predrfined macros +gcc -DNO_MM myprog.c u Warn if function is declared or defined without argument type +gcc -Wstrict-prototypes myprog.c u Compiling multiple source files +gcc file1.c file2.c -o myprog u Alternative method for compiling multiple source files +gcc -c file1.c +gcc -c file2.c +gcc file1.o file2.o -o myprog

6 6 Author: D L Johnson Assignment 2 u Part1: Understand all the gcc options when uClinux compiles a file arm-elf-gcc -D__KERNEL__ - I/home/djohnson/uclinux_project/uClinux- 20030909/linux-2.4.x/include -Wall -Wstrict- prototypes -Wno-trigraphs -O2 -fno-strict- aliasing -fno-common -fno-common -pipe -fno- builtin -D__linux__ -g -DNO_MM -mapcs-32 - march=armv4 -mtune=arm7tdmi -mshort-load-bytes -msoft-float -nostdinc -iwithprefix include -DKBUILD_BASENAME=filemap -c -o filemap.o filemap.c

7 7 Author: D L Johnson Toolchain – Linker options u Specifying libraries archives to link in +gcc myprog.c –lmylib –This will search in the default library paths for libraries libmylib.a, libmylib.so u Adding a library path to the list of paths to search +gcc myprog.c –L/home/djohnson/mylibraries u Strip all symbol information from output file +gcc –s myprog.c u Only search library directories specified on common line +gcc –nostdlib myprog.c u First function called when executable loaded +gcc –init mystart myprog.c –Normally linker uses _init as the first function to call

8 8 Author: D L Johnson Toolchain – binutils - objdump u objdump displays information from object and executable files u Disassemble executable file +objdump –d a.out u Display contents of symbol table +objdump –t a.out u Disassemble from specified start address +objdump –d –start-address=0x8000000

9 9 Author: D L Johnson Toolchain – binutils - objcopy u Objcopy copies and translates object and executable files into different formats or copies sections out of the file into a new file u Removing sections out of file +Objcopy –O binary –remove-section=.text linux linux.data u Changing the execute address of the binary +Objcopy –O binary –change-section-vma.data=0x5000000 linux linux.data u Changing the load address of the binary +Objcopy –O binary –change-section-lma.data=0x2000000 linux linux.data

10 10 Author: D L Johnson ELF file format u ELF = Executable and Linkable format u Originally created by Unix system labs u Used in virtually every recent Unix u Three main types of ELF files +Relocatable file – object file to be linked with other +Executable +Shared object (library u Elf divides the file into sections u A sections is a collection of information of similar type u As seen in the first lecture, for example executable code is placed in.text u Different to eg. MS-DOS binaries where everything is jumbled together

11 11 Author: D L Johnson ELF file format u Advantage of sections architecture: when executable.text section placed in memory, these locations won’t change u When you ask a kernel to load and run an executable it starts looking in the elf image header for clues on how to load the image u It moves.text into memory and marks read-only u Moves.data into user’s address space as read-write u Finds location and size of.bss section, adds the pages of memory to user’s address space and initialises this memory section to zero u THE uclinux kernel executable is a flat binary not ELF as nothing exists to load it – it must be self existent u Only the files in the romfs file system are elf format as they are loaded by the kernel

12 12 Author: D L Johnson ELF file format u For hello world > cat hello.c void main(void) { printf(“Hello World”) }; u Type gcc –c hello.c u ELF files contain a table to describe sections within the file u Type readelf –S hello.o

13 13 Author: D L Johnson ELF file format u The.rel.text section contains the relocations for the.text section of the file u Notice printf needs to be relocated

14 14 Author: D L Johnson ELF file format u This is done through the PLT (Procedure Link Table)

15 15 Author: D L Johnson ELF file format u Compare the assembler code of the hello.o object file gcc –c hello.c objdump –d hello.o u To this assembler code of the a.out executable gcc hello.c objdump –d a.out u Notice all the extra assembler in a.out such as the.init section, the.plt section which points to the real address of printf

16 16 Author: D L Johnson ELF file format u More info on the ELF file format can be found in documents in day3 folder +Elf format presentation.pdf +Elf_format.pdf

17 17 Author: D L Johnson Assignment 3 u Take a standard hello.c file int main(void) { char name[10] = “albert”; printf(“my name is %s”, name); return 0 } u Change “albert” to “david” using assembler (change the hello.s file) u Create a new executable +Hint useful tools –gcc will compile c code files (myfile.c) and assember files (myfile.s) u If you’re bored do this with arm environment –Hint – try changing the Makefile in user/hello to create assembler code


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

Similar presentations


Ads by Google