200 likes | 375 Views
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
E N D
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 • Show verbose output on the compile process • gcc -v myprog.c • Produce debugging information for gdb • gcc -g hello.c • Turns on more warnings • gcc -Wall hello.c
Toolchain – C compiler options • 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 • Add extra include directories • gcc -c hello.c -I/home/djohnson/include • Create assembler code from the c source code • gcc -S hello.c • 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
Toolchain – C compiler options • Predrfined macros • gcc -DNO_MM myprog.c • Warn if function is declared or defined without argument type • gcc -Wstrict-prototypes myprog.c • Compiling multiple source files • gcc file1.c file2.c -o myprog • Alternative method for compiling multiple source files • gcc -c file1.c • gcc -c file2.c • gcc file1.o file2.o -o myprog
Assignment 2 • 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
Toolchain – Linker options • Specifying 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 search • gcc myprog.c –L/home/djohnson/mylibraries • Strip all symbol information from output file • gcc –s myprog.c • Only search library directories specified on common line • gcc –nostdlib myprog.c • First function called when executable loaded • gcc –init mystart myprog.c • Normally linker uses _init as the first function to call
Toolchain – binutils - objdump • objdump displays information from object and executable files • Disassemble executable file • objdump –d a.out • Display contents of symbol table • objdump –t a.out • Disassemble from specified start address • objdump –d –start-address=0x8000000
Toolchain – binutils - objcopy • Objcopy copies and translates object and executable files into different formats or copies sections out of the file into a new file • Removing sections out of file • Objcopy –O binary –remove-section=.text linux linux.data • Changing the execute address of the binary • Objcopy –O binary –change-section-vma .data=0x5000000 linux linux.data • Changing the load address of the binary • Objcopy –O binary –change-section-lma .data=0x2000000 linux linux.data
ELF file format • ELF = Executable and Linkable format • Originally created by Unix system labs • Used in virtually every recent Unix • Three main types of ELF files • Relocatable file – object file to be linked with other • Executable • Shared object (library • Elf divides the file into sections • A sections is a collection of information of similar type • As seen in the first lecture, for example executable code is placed in .text • Different to eg. MS-DOS binaries where everything is jumbled together
ELF file format • Advantage of sections architecture: when executable .text section placed in memory, these locations won’t change • 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 • It moves .text into memory and marks read-only • Moves .data into user’s address space as read-write • Finds location and size of .bss section, adds the pages of memory to user’s address space and initialises this memory section to zero • THE uclinux kernel executable is a flat binary not ELF as nothing exists to load it – it must be self existent • Only the files in the romfs file system are elf format as they are loaded by the kernel
ELF file format • For hello world > cat hello.c void main(void) { printf(“Hello World”) }; • Type gcc –c hello.c • ELF files contain a table to describe sections within the file • Type readelf –S hello.o
ELF file format • The .rel.text section contains the relocations for the .text section of the file • Notice printf needs to be relocated
ELF file format • This is done through the PLT (Procedure Link Table)
ELF file format • Compare the assembler code of the hello.o object file gcc –c hello.c objdump –d hello.o • To 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 section, the .plt section which points to the real address of printf
ELF file format • More info on the ELF file format can be found in documents in day3 folder • Elf format presentation.pdf • Elf_format.pdf
Assignment 3 • Take a standard hello.c file int main(void) { char name[10] = “albert”; printf(“my name is %s”, name); return 0 } • Change “albert” to “david” using assembler (change the hello.s file) • Create 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 environment • Hint – try changing the Makefile in user/hello to create assembler code