23 January 2010

gcc internals - 2

.globl _start

.section .rodata
LC0: .string "hello world!!!\n"

.section .text
_start:
mov $LC0, %ecx
mov $15, %edx
mov $1, %ebx
mov $4, %eax
int $0x80

mov $42, %ebx
mov $1, %eax
int $0x80
as tiny.s -o tiny.o
gcc -Wall -s -nostdlib tiny.o -o tiny; (448 bytes)
ld -s tiny.o -o tiny; (320 bytes)
./tiny ; echo $?

#define VADDR 0x08048000

.globl _start

.section .text
ehdr:
.byte 0x7F
.ascii "ELF"
.byte 1,1,1,0,0,0,0,0,0,0,0,0 #16 (ELF magic header)
.short 2 #2 (type)
.short 3 #2 (machine)
.long 1 #4 (version)
.long start + VADDR #4 (entry)
.long phdr #4 (phdr offset)
.long 0 #4 ()
.long 0 #4 ()
.short ehdrsize #2 (ehdr size)
.short phdrsize #2 (phdr size)
.short 1 #2 (phdr no.)
.short 0
.short 0
.short 0
.equ ehdrsize, phdr - ehdr

phdr:
.long 1 #4 (type)
.long 0 #4 ()
.long VADDR #4 (virtual addr)
.long VADDR #4 (physic addr)
.long filesize #4 (file size)
.long filesize #4 (mem size)
.long 5 #4 ()
.long 0x1000 #4 ()
.equ phdrsize, start - phdr

start:
mov $LC0+VADDR, %ecx
mov $15, %edx
mov $1, %ebx
mov $4, %eax
int $0x80

mov $42, %ebx
mov $1, %eax
int $0x80

LC0:
.ascii "hello world!!!\n"
.equ filesize, end - ehdr

end:
cpp tiny.s > tiny.i
as tiny.i -o tiny.o
objcopy -O binary tiny.o tiny
chmod +x tiny


Referance:
http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
http://svn.gna.org/svn/grub4dos/branches/test/stage2/bootlacestart.S
http://timelessname.com/elfbin

No comments:

Post a Comment