ETH 780 Information Security

Download Report

Transcript ETH 780 Information Security

uClinux course
Day 3 of 5
The uclinux toolchain, elf format and ripping a “hello
world”
1
toolchain
The compile process
cpp
cc1
as
ld
Author: D L Johnson
2
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
Author: D L Johnson
3
Toolchain – C compiler options

Optimize the code
+
+
+
+

gcc
gcc
gcc
gcc
–O1
–O2
–O3
–Os
myprog.c
myprog.c
myprog.c
myprog.c
… optimise level1
… optimize level2
… highest level of optimization
… 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
Author: D L Johnson
4
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
Author: D L Johnson
5
Assignment 2

Part1: Understand all the gcc options when uClinux
compiles a file
arm-elf-gcc -D__KERNEL__ I/home/djohnson/uclinux_project/uClinux20030909/linux-2.4.x/include -Wall -Wstrictprototypes -Wno-trigraphs -O2 -fno-strictaliasing -fno-common -fno-common -pipe -fnobuiltin -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
Author: D L Johnson
6
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
Author: D L Johnson
7
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
Author: D L Johnson
8
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
Author: D L Johnson
9
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
Author: D L Johnson
10
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
Author: D L Johnson
11
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
Author: D L Johnson
12
ELF file format

The .rel.text section contains the relocations for the .text section of the file

Notice printf needs to be relocated
Author: D L Johnson
13
ELF file format

Author: D L Johnson
This is done through the PLT (Procedure Link
Table)
14
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
Author: D L Johnson
15
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
Author: D L Johnson
16
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
Author: D L Johnson
17