arm linux 下最小的hello world程序

14 views
Skip to first unread message

cediy2088

unread,
Feb 11, 2012, 3:46:21 AM2/11/12
to jhkdroid+ga...@googlegroups.com

/*
*
=====================================================================================
*
* Filename: tinyhelloworld.c
*
* Description: tinyhelloworld
*
* Version: 1.0
* Created: 2009年07月04日 10时15分54秒
* Revision: none
* Compiler: gcc
*
* Author: (btree), booktr...@gmail.com
* Company: none
*
*
=====================================================================================
*/

char *str = "hello world!\n";
/**********************************************
*
* function :print
*description :use the syscall write
*prototype of wirte:
* ssize_t write(int fd, const void *buf, size_t count)
**********************************************/
void print(void)
{
__asm__ __volatile__(
"mov r0, #0x0\n\t" /*fd*/
"mov r1, %0\n\t" /*buf*/
"mov r2, #0x0d\n\t" /*count*/
"swi (0x00900000 + 4)\n\t"
::"r"(str):"r0","r1","r2");
}

/********************************************
* function: exit
* description: use the syscall exit
********************************************/
void exit(void)
{
__asm__ __volatile__(
"mov r0, #42\n\t"
"swi (0x00900000 + 1)\n\t");
}

void nomain(void)
{
print();
exit();
}

arm-linux-gcc -c -fno-builtin tinyhelloworld.c
arm-linux-ld -static -e nomain -o tinyhelloworld tinyhelloworld.o

运行:
/home/tester $ ls
helloworld tinyhelloworld
/home/tester $ ./tinyhelloworld
hello world!
/home/tester $ echo $?
42
你会发现tinyhelloworld的大小为:
-rwxr-xr-x 1 btree btree 1.2K 2009-07-04 16:30 tinyhelloworld
--------------------------------------------------------------------------------------------------------------------------------------------
添加tinyhelloworld.lds,进一步对section裁减
ENTRY(nomain)
SECTIONS
{
. = 0x00008000 + SIZEOF_HEADERS;
tinytext :
{
*(.text)
*(.data)
*(.rodata)
}
/DISCARD/ :
{
*(.comment)
}
}
下面是Makefile
inyhelloworld: tinyhelloworld.c
arm-linux-gcc -c -fno-builtin tinyhelloworld.c
arm-linux-ld -static -T tinyhelloworld.lds -o tinyhelloworld
tinyhelloworld.o
clean:
-rm tinyhelloworld *.o
执行make后你会发现,tinyhelloworld更加小
-rwxr-xr-x 1 root root 513352 Jul 4 2009 helloworld //C代码写的helloworld
-rwxr-xr-x 1 root root 926 Jul 4 2009 tinyhelloworld //对section 裁减后的
tinyhelloworld,只有926个字节

Reply all
Reply to author
Forward
0 new messages