Showing posts with label Hello World. Show all posts
Showing posts with label Hello World. Show all posts

Tuesday, December 24, 2013

Hello World 補充

這個連結提供了一些方式,讓code size更加精簡。
Shortest ELF for “Hello world\n”

Sunday, December 22, 2013

Hello World - Assembly Language (SPARC V8)

This is a "Hello World" program written in pure assembly language on Linux/SPARC V8 platform.

  1 #
  2 # Makefile for Hello World on Linux/SPARC V8 platform
  3 #
  4 all:
  5     sparc-linux-as hello.s -o hello.o
  6     sparc-linux-ld hello.o -o hello
  7 
  8 clean:
  9     rm -f hello.o hello

  1 /* hello.s */
  2 /* sparc-linux-as hello.s -o hello.o */
  3 /* sparc-linuc-ld hello.o -o hello   */
  4 
  5 .text
  6 .globl  _start
  7 
  8 _start:
  9 
 10     mov 1,      %o0
 11     set msg,    %o1
 12     mov 14,     %o2
 13     mov 4,      %g1
 14     ta  0x10
 15 
 16     mov 0,      %o0
 17     mov 1,      %g1
 18     ta  0x10
 19 
 20     msg:
 21     .string "Hello World\n"

Result...

$ uname -a
Linux SPARC V8 2.6.24.1 #37 Thu Jan 28 10:11:06 CST 2010 sparc unknown
$
$ ./hello
Hello World
$

Of Linux on SPARC V8 CPU...
The Linux kernel entry, from user mode application into kernel mode, is triggled by ta 0x10 (trap 0x90) instrusion.
The system call number is got from the CPU register g1.
The system call parameter is placed in the sequence of o0, o1, o2...

In the example of the WRITE() system call

ssize_t write (int __fd, __const void *__buf, size_t __n);
 The system call index (g1) is 0x04
 __fd  is o0
 __buf is o1
 __n   is o2

Tuesday, March 2, 2010

Hello World - Assembly Language (ARM 920T)

This is a "Hello World" program written in pure assembly language on Linux/ARM 920T platform.

1 #
2 # Makefile for Hello World on Linux/ARM 920T platform
3 #
4 all:
5 arm-linux-as hello.s -o hello.o
6 arm-linux-ld hello.o -o hello
7
8 clean:
9 rm -rf hello hello.o

1
2 .text
3 .global _start
4 _start:
5
6 mov r0, #1
7 adr r1, msg
8 mov r2, #14
9 swi 0x00900004
10
11 mov r0, #0
12 swi 0x00900001
13
14 msg: .asciz "Hello World\n"

Result...

$ uname -a
Linux ARM 2.6.11.1armzone_linux2.6 #6 Fri Dec 12 12:12:23 CST 2008 armv4tl unknown
$ ./hello
$
Hello World
$

Of Linux on ARM 920T CPU...
  • The Linux kernel entry, from user mode application into kernel mode, is triggled by swi (0x00900001 or index) instrusion.
  • The system call number is got from the low byte of swi parameter (swi 0x009000XX).
  • The system call parameter is placed in the sequence of r0, r1, r2...

    In the example of the WRITE() system call

    ssize_t write (int __fd, __const void *__buf, size_t __n);
  • The system call index (g1) is 0x04 (swi 0x00900004)
  • __fd is r0
  • __buf is r1
  • __n is r2
  • Hello World - Assembly Language (x86)

    This is a "Hello World" program written in pure assembly language on Linux/X86 platform.

    1 /* as helloworld.s -o helloworld.o; ld helloworld.o -o helloworld */
    2 /* gcc helloworld.s -nostartfiles -nostdlib -static -o helloworld */
    3
    4 .text
    5 .globl _start
    6
    7 _start:
    8 mov $13, %edx
    9 mov $msg, %ecx
    10 mov $1, %ebx
    11 mov $4, %eax
    12 int $0x80
    13
    14 xorl %ebx, %ebx /* exit code */
    15 movl $1, %eax /* exit function */
    16 int $0x80
    17
    18 msg:
    19 .string "Hello world\n"
    20

    The result is...

    $ gcc helloworld.s -nostartfiles -nostdlib -static -o helloworld
    $
    $ uname -a
    Linux lungswu-mobile 2.6.28-11-generic #42-Ubuntu SMP Fri Apr 17 01:57:59 UTC 2009 i686 GNU/Linux
    $
    $ ./helloworld
    Hello world
    $
    $

    Of Linux on X86 CPU...
  • The Linux kernel entry, from user mode application into kernel mode, is triggled by INT 0x80 instrusion.
  • The system call number is got from the CPU register EAX.
  • The system call parameter is placed in the sequence of EBX, ECX, EDX...

    In the example of the WRITE() system call

    ssize_t write (int __fd, __const void *__buf, size_t __n);
  • The system call index (EAX) is 0x04
  • __fd is EBX
  • __buf is ECX
  • __n is EDX
  • Hello World - 最小 code size

    Hello World的最小code size為29 bytes(由128 到 29 byte !!!)

    $ ls -l helloworld
    -rwxr-xr-x 1 lungswu lungswu 29 2010-03-02 11:42 helloworld
    $
    $ ./helloworld
    Hello World
    $

    其方式....















































    $ cat > helloworld
    #!/bin/sh
    echo "Hello World"
    $
    $ chmod a+x helloworld
    $ ./helloworld
    Hello World
    $
    $ ls -l helloworld
    -rwxr-xr-x 1 lungswu lungswu 29 2010-03-02 11:47 helloworld
    $
    $ file helloworld
    helloworld: POSIX shell script text executable
    $
    $

    (Hello World - code size系列 完)

    Hello World - code size 手動極小化

    於手動更動內容之前,可以再將helloworld2.s做小小的更動:Hello World - 更加精小的 code size 之中程式helloworld2.s於14行可以做以下改變:

    movl $0, %ebx ==>
    xorl %ebx, %ebx

    編譯出來結果雖然總體檔案不變(一樣為268 byte大小,是因為執行檔中byte alignment原因),但如此產生的machine code會更小(少3個byte):

    mov $0, %ebx ==> 0xBB 0x00 0x00 0x00 0x00
    xor %ebx, %ebx ==> 0x31 0xDB



    接下來以hexdump命令觀察執行檔的內容:

    $ hexdump -C -v helloworld2
    00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
    00000010 02 00 03 00 01 00 00 00 54 80 04 08 34 00 00 00 |........T...4...|
    00000020 94 00 00 00 00 00 00 00 34 00 20 00 01 00 28 00 |........4. ...(.|
    00000030 03 00 02 00 01 00 00 00 00 00 00 00 00 80 04 08 |................|
    00000040 00 80 04 08 80 00 00 00 80 00 00 00 05 00 00 00 |................|
    00000050 00 10 00 00 ba 0d 00 00 00 b9 73 80 04 08 bb 01 |..........s.....|
    00000060 00 00 00 b8 04 00 00 00 cd 80 31 db b8 01 00 00 |..........1.....|
    00000070 00 cd 80 48 65 6c 6c 6f 20 77 6f 72 6c 64 0a 00 |...Hello world..|
    00000080 00 2e 73 68 73 74 72 74 61 62 00 2e 74 65 78 74 |..shstrtab..text|
    00000090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
    000000a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
    000000b0 00 00 00 00 00 00 00 00 00 00 00 00 0b 00 00 00 |................|
    000000c0 01 00 00 00 06 00 00 00 54 80 04 08 54 00 00 00 |........T...T...|
    000000d0 2c 00 00 00 00 00 00 00 00 00 00 00 04 00 00 00 |,...............|
    000000e0 00 00 00 00 01 00 00 00 03 00 00 00 00 00 00 00 |................|
    000000f0 00 00 00 00 80 00 00 00 11 00 00 00 00 00 00 00 |................|
    00000100 00 00 00 00 01 00 00 00 00 00 00 00 |............|
    0000010c

    在對應執行檔的Spec
    。於Figure 1-1: Object File Format中提到,若一個ELF檔案為執行檔時,其檔案起始為ELF header以及Program header table。也就是說:
  • 0x00 ~ 0x33 為ELF header
  • 0x34 ~ 0x53 為Program header table
    這裡一個很簡的此兩個資料結構的parser:

    1 #include <unistd.h>
    2 #include <stdio.h>
    3 #include <stdlib.h>
    4 #include <elf.h>
    5
    6 Elf32_Ehdr e;
    7 Elf32_Phdr p;
    8
    9 #define member_p(t, s, m, o) printf(#s"."#m"(0x%08x,%d)....0x%08X\n", (unsigned int)&(((t *) 0)->m) + o, sizeof(s.m), s.m);
    10
    11 int main(void)
    12 {
    13 read(STDIN_FILENO, &e, sizeof(e));
    14
    15 member_p(Elf32_Ehdr, e, e_type , 0);
    16 member_p(Elf32_Ehdr, e, e_machine , 0);
    17 member_p(Elf32_Ehdr, e, e_version , 0);
    18 member_p(Elf32_Ehdr, e, e_entry , 0);
    19 member_p(Elf32_Ehdr, e, e_phoff , 0);
    20 member_p(Elf32_Ehdr, e, e_shoff , 0);
    21 member_p(Elf32_Ehdr, e, e_flags , 0);
    22 member_p(Elf32_Ehdr, e, e_ehsize , 0);
    23 member_p(Elf32_Ehdr, e, e_phentsize , 0);
    24 member_p(Elf32_Ehdr, e, e_phnum , 0);
    25 member_p(Elf32_Ehdr, e, e_shentsize , 0);
    26 member_p(Elf32_Ehdr, e, e_shnum , 0);
    27 member_p(Elf32_Ehdr, e, e_shstrndx , 0);
    28
    29 printf("\n");
    30
    31 read(STDIN_FILENO, &p, sizeof(p));
    32
    33 member_p(Elf32_Phdr, p, p_type , sizeof(e));
    34 member_p(Elf32_Phdr, p, p_offset , sizeof(e));
    35 member_p(Elf32_Phdr, p, p_vaddr , sizeof(e));
    36 member_p(Elf32_Phdr, p, p_paddr , sizeof(e));
    37 member_p(Elf32_Phdr, p, p_filesz , sizeof(e));
    38 member_p(Elf32_Phdr, p, p_memsz , sizeof(e));
    39 member_p(Elf32_Phdr, p, p_flags , sizeof(e));
    40 member_p(Elf32_Phdr, p, p_align , sizeof(e));
    41
    42 return (0);
    43 }
    44
    $
    $ gcc elf.c -o elf
    $ ./elf < helloworld2
    e.e_type(0x00000010,2)....0x00000002
    e.e_machine(0x00000012,2)....0x00000003
    e.e_version(0x00000014,4)....0x00000001
    e.e_entry(0x00000018,4)....0x08048054
    e.e_phoff(0x0000001c,4)....0x00000034
    e.e_shoff(0x00000020,4)....0x00000094
    e.e_flags(0x00000024,4)....0x00000000
    e.e_ehsize(0x00000028,2)....0x00000034
    e.e_phentsize(0x0000002a,2)....0x00000020
    e.e_phnum(0x0000002c,2)....0x00000001
    e.e_shentsize(0x0000002e,2)....0x00000028
    e.e_shnum(0x00000030,2)....0x00000003
    e.e_shstrndx(0x00000032,2)....0x00000002

    p.p_type(0x00000034,4)....0x00000001
    p.p_offset(0x00000038,4)....0x00000000
    p.p_vaddr(0x0000003c,4)....0x08048000
    p.p_paddr(0x00000040,4)....0x08048000
    p.p_filesz(0x00000044,4)....0x00000080
    p.p_memsz(0x00000048,4)....0x00000080
    p.p_flags(0x0000004c,4)....0x00000005
    p.p_align(0x00000050,4)....0x00001000
    $

    ㄧ個ELF的執行檔於最簡單形式時(例如helloworld.s編譯出來的執行檔),這兩個資料結構及程式本身是最基本的。
    或者覺得太麻煩,可以用以下方式讀取更詳細的資料:

    $
    $ readelf -a helloworld2
    ELF Header:
    Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
    Class: ELF32
    Data: 2's complement, little endian
    Version: 1 (current)
    OS/ABI: UNIX - System V
    ABI Version: 0
    Type: EXEC (Executable file)
    Machine: Intel 80386
    Version: 0x1
    Entry point address: 0x8048054
    Start of program headers: 52 (bytes into file)
    Start of section headers: 148 (bytes into file)
    Flags: 0x0
    Size of this header: 52 (bytes)
    Size of program headers: 32 (bytes)
    Number of program headers: 1
    Size of section headers: 40 (bytes)
    Number of section headers: 3
    Section header string table index: 2

    Section Headers:
    [Nr] Name Type Addr Off Size ES Flg Lk Inf Al
    [ 0] NULL 00000000 000000 000000 00 0 0 0
    [ 1] .text PROGBITS 08048054 000054 00002c 00 AX 0 0 4
    [ 2] .shstrtab STRTAB 00000000 000080 000011 00 0 0 1
    Key to Flags:
    W (write), A (alloc), X (execute), M (merge), S (strings)
    I (info), L (link order), G (group), x (unknown)
    O (extra OS processing required) o (OS specific), p (processor specific)

    There are no section groups in this file.

    Program Headers:
    Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
    LOAD 0x000000 0x08048000 0x08048000 0x00080 0x00080 R E 0x1000

    Section to Segment mapping:
    Segment Sections...
    00 .text

    There is no dynamic section in this file.

    There are no relocations in this file.

    There are no unwind sections in this file.

    No version information found in this file.
    $
    $

    這邊只需要ELF header,Program header table以及.text區段,其餘可以刪除:

    $
    $ hexdump -C -v helloworld2.bin
    00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
    00000010 02 00 03 00 01 00 00 00 54 80 04 08 34 00 00 00 |........T...4...|
    00000020 94 00 00 00 00 00 00 00 34 00 20 00 01 00 28 00 |........4. ...(.|
    00000030 03 00 02 00 01 00 00 00 00 00 00 00 00 80 04 08 |................|
    00000040 00 80 04 08 80 00 00 00 80 00 00 00 05 00 00 00 |................|
    00000050 00 10 00 00 ba 0d 00 00 00 b9 73 80 04 08 bb 01 |..........s.....|
    00000060 00 00 00 b8 04 00 00 00 cd 80 31 db b8 01 00 00 |..........1.....|
    00000070 00 cd 80 48 65 6c 6c 6f 20 77 6f 72 6c 64 0a 00 |...Hello world..|
    00000080
    $
    $ chmod a+x helloworld2.bin
    $ ./helloworld2.bin
    Hello world
    $
    $ ls -l helloworld2.bin
    -rwxr-xr-x 1 lungswu lungswu 128 2010-03-02 11:23 helloworld2.bin
    $
    $

    最後一個Hello World的檔案被縮減到剩下128 bytes。
  • Hello World - 更加精小的 code size

    這裡所知道,要產生更小的Hello World程式,就需要用到純組合語言來完成,其純組合語言的優點...
  • 可以精確的控式Program Counter(PC)的變化(C語言內嵌組合語言雖可以做到,但程式碼需存在於function之中,也就是說在編譯C語言中,編譯器會做stack處理)
  • 可以精確的處理section的內容
  • 可以加入機械語言
    所以使用組合語言可以使code size做的更小 (當然一般來說,一個有經驗的programmer使用組合語言編寫程式會比使用C語言編寫程式所產生的code size還小的原因,這裡所描述的理由只是眾多原因之一)

    1 /*
    2 * hello2.c
    3 */
    4 unsigned char *str= "Hello World\n";
    5
    6 int _start(void)
    7 {
    8 /* write(STDOUT_FILENO, str, sizeof(str)); */
    9 asm("movl $14, %%edx;\
    10 movl %0, %%ecx;\
    11 movl $1, %%ebx;\
    12 movl $4, %%eax;\
    13 int $0x80;"
    14 : :"m"(str));
    15
    16 /* exit(0); */
    17 asm("xorl %ebx, %ebx;\
    18 movl $1, %eax;\
    19 int $0x80;");
    20 }

    第4行字串的安排及第6行_start() function,轉或成組合語言結果...

    1 /* as helloworld.s -o helloworld.o; ld helloworld.o -o helloworld */
    2 /* gcc helloworld.s -nostartfiles -nostdlib -static -o hellworld */
    3
    4 .text
    5 .globl _start
    6
    7 _start:
    8 mov $13, %edx
    9 mov $msg, %ecx
    10 mov $1, %ebx
    11 mov $4, %eax
    12 int $0x80
    13
    14 movl $0, %ebx /* exit code */
    15 movl $1, %eax /* exit function */
    16 int $0x80
    17
    18 .data
    19 msg:
    20 .string "Hello world\n"

    其編譯方式有兩種(於1、2行註解中),此程式中的進入點直接執行第8行程式,且只有安排一個data section(.data)。
    其編譯、執行結果...

    $ gcc helloworld.s -nostartfiles -nostdlib -static -o hellworld
    $ strip hellworld
    $ objdump -h hellworld

    hellworld: file format elf32-i386

    Sections:
    Idx Name Size VMA LMA File off Algn
    0 .text 00000022 08048074 08048074 00000074 2**2
    CONTENTS, ALLOC, LOAD, READONLY, CODE
    1 .data 0000000d 08049098 08049098 00000098 2**2
    CONTENTS, ALLOC, LOAD, DATA
    $
    $ ls -l hellworld
    -rwxr-xr-x 1 lungswu lungswu 348 2010-03-01 11:40 hellworld
    $
    $ ./hellworld
    Hello world
    $
    $

    所以產生348 bytes 大小的執行檔,組合語言中因為沒有定義.rodata section,所以編譯連結出來的執行檔也無.rodata section。



    本文開頭第2點有說到....組合語言可以"可以精確的處理section的內容"...
    先來將hellworld執行檔反組譯...

    $ objdump -S helloworld

    helloworld: file format elf32-i386


    Disassembly of section .text:

    08048074 <.text>:
    8048074: ba 0d 00 00 00 mov $0xd,%edx
    8048079: b9 98 90 04 08 mov $0x8049098,%ecx
    804807e: bb 01 00 00 00 mov $0x1,%ebx
    8048083: b8 04 00 00 00 mov $0x4,%eax
    8048088: cd 80 int $0x80
    804808a: bb 00 00 00 00 mov $0x0,%ebx
    804808f: b8 01 00 00 00 mov $0x1,%eax
    8048094: cd 80 int $0x80


    在上述反組譯的內容中,其執行碼剛好跟helloworld.s的組合語言做一對一的對應。
    1. 其中程式進入點為0x8048074。
    2. 於0x8048079位址,是將0x8049098的十六進制值填入站存器ECX中。值0x8049098剛好是symbol msg的位址。
    3. 程式執行到0x8048094便結束。
    所以...於位址 0x8048094 + 2 = 0x8048096 可以自行加入所需要額外的資料也不會對整個執行造成不良的影響 ....

    1 /* as helloworld2.s -o helloworld2.o; ld helloworld.o -o helloworld2 */
    2 /* gcc helloworld2.s -nostartfiles -nostdlib -static -o helloworld2 */
    3
    4 .text
    5 .globl _start
    6
    7 _start:
    8 mov $13, %edx
    9 mov $msg, %ecx
    10 mov $1, %ebx
    11 mov $4, %eax
    12 int $0x80
    13
    14 movl $0, %ebx /* exit code */
    15 movl $1, %eax /* exit function */
    16 int $0x80
    17
    18 msg:
    19 .string "Hello world\n"

    所以...這樣可以將"Hello World\n"的字串內容接到int 0x80之後,也將.data的section移除...

    $ gcc helloworld2.s -nostartfiles -nostdlib -static -o hellworld2
    $ strip helloworld2
    $ objdump -h helloworld2

    helloworld2: file format elf32-i386

    Sections:
    Idx Name Size VMA LMA File off Algn
    0 .text 0000002f 08048054 08048054 00000054 2**2
    CONTENTS, ALLOC, LOAD, READONLY, CODE
    $ ./helloworld2
    Hello world
    $
    $ ls -l helloworld2
    -rwxr-xr-x 1 lungswu lungswu 268 2010-03-01 13:33 hellworld2
    $
    這樣的結果...
    .data section已經移除,整個執行檔剩下.text section,大小變為 268 bytes。



    附註...組合語言中加入"機械語言"...

    因為組合語言中可以自行將入不同二進制的資料,如以下反組譯的結果...

    $ objdump -S helloworld2

    helloworld2: file format elf32-i386


    Disassembly of section .text:

    08048054 <.text>:
    8048054: ba 0d 00 00 00 mov $0xd,%edx
    8048059: b9 76 80 04 08 mov $0x8048076,%ecx
    804805e: bb 01 00 00 00 mov $0x1,%ebx
    8048063: b8 04 00 00 00 mov $0x4,%eax
    8048068: cd 80 int $0x80
    804806a: bb 00 00 00 00 mov $0x0,%ebx
    804806f: b8 01 00 00 00 mov $0x1,%eax
    8048074: cd 80 int $0x80
    8048076: 48 dec %eax
    8048077: 65 gs
    8048078: 6c insb (%dx),%es:(%edi)
    8048079: 6c insb (%dx),%es:(%edi)
    804807a: 6f outsl %ds:(%esi),(%dx)
    804807b: 20 77 6f and %dh,0x6f(%edi)
    804807e: 72 6c jb 0x80480ec
    8048080: 64 0a 00 or %fs:(%eax),%al
    $

    其實上面反組譯後位址 0x8048076 到 0x8048080 + 3 的地方是symbol msg的資料內容,因為放置於.text中,objdump或者program loader會將這新資料當作程式碼放置。

    所以,helloworld中要插入機械語言(比方說int 0x80的機械碼為0xCD 0x80,則可以這樣編寫(注意下面程式16行)...

    1 /* as helloworld3.s -o helloworld3.o; ld helloworld3.o -o helloworld3 */
    2 /* gcc helloworld3.s -nostartfiles -nostdlib -static -o helloworld3 */
    3
    4 .text
    5 .globl _start
    6
    7 _start:
    8 mov $13, %edx
    9 mov $msg, %ecx
    10 mov $1, %ebx
    11 mov $4, %eax
    12 int $0x80
    13
    14 movl $0, %ebx /* exit code */
    15 movl $1, %eax /* exit function */
    16 .byte 0xCD, 0x80 /* Machine code int 0x80 */
    17
    18 .data
    19 msg:
    20 .string "Hello world\n"

    結果如下...

    $ gcc helloworld3.s -nostartfiles -nostdlib -static -o helloworld3
    $ objdump -S helloworld3

    helloworld3: file format elf32-i386


    Disassembly of section .text:

    08048074 <_start>:
    8048074: ba 0d 00 00 00 mov $0xd,%edx
    8048079: b9 98 90 04 08 mov $0x8049098,%ecx
    804807e: bb 01 00 00 00 mov $0x1,%ebx
    8048083: b8 04 00 00 00 mov $0x4,%eax
    8048088: cd 80 int $0x80
    804808a: bb 00 00 00 00 mov $0x0,%ebx
    804808f: b8 01 00 00 00 mov $0x1,%eax
    8048094: cd 80 int $0x80
    $ ./helloworld3
    $
    Hello world
    $
    $
  • Hello World - 讓code size小於512 byts

    在上一篇中,code size已經縮小到536 bytes。其反組譯的內容為...

    $ objdump -S hello2

    hello2: file format elf32-i386


    Disassembly of section .text:

    08048094 <.text>:
    8048094: 55 push %ebp
    8048095: 89 e5 mov %esp,%ebp
    8048097: ba 0e 00 00 00 mov $0xe,%edx
    804809c: 8b 0d c8 90 04 08 mov 0x80490c8,%ecx
    80480a2: bb 01 00 00 00 mov $0x1,%ebx
    80480a7: b8 04 00 00 00 mov $0x4,%eax
    80480ac: cd 80 int $0x80
    80480ae: 31 db xor %ebx,%ebx
    80480b0: b8 01 00 00 00 mov $0x1,%eax
    80480b5: cd 80 int $0x80
    80480b7: 5d pop %ebp
    80480b8: c3 ret
    $


    這裡有一點需注意...由於編譯、執行時沒有將正確的stack建立,所以_start() function雖然有做stack的動作,但一不小心還是會造成stack錯誤,所以_start中只有push後並沒有做額外的stack存取。
    另外...因為int 0x80(eax<1)為程式的結束點,所以可以不需要做return動作。至此...

    $ gcc -nostartfiles -nostdlib -static hello2.c -o hello2
    $ strip hello2
    $ ./hello2
    Hello World
    $ ls -l hello2
    -rwxr-xr-x 1 lungswu lungswu 528 2010-03-01 10:08 hello2
    $ $

    縮小為528 byte。


    再過來...我們觀察編譯後的執行檔結構...

    $ objdump -h hello2

    hello2: file format elf32-i386

    Sections:
    Idx Name Size VMA LMA File off Algn
    0 .text 00000025 08048094 08048094 00000094 2**2
    CONTENTS, ALLOC, LOAD, READONLY, CODE
    1 .rodata 0000000d 080480b9 080480b9 000000b9 2**0
    CONTENTS, ALLOC, LOAD, READONLY, DATA
    2 .data 00000004 080490c8 080490c8 000000c8 2**2
    CONTENTS, ALLOC, LOAD, DATA
    3 .comment 0000002a 00000000 00000000 000000cc 2**0
    CONTENTS, READONLY

    裡面有一個.comment 的section,其內容為...

    $ objdump -s hello2

    hello2: file format elf32-i386

    Contents of section .text:
    8048094 5589e5ba 0e000000 8b0dc890 0408bb01 U...............
    80480a4 000000b8 04000000 cd8031db b8010000 ..........1.....
    80480b4 00cd805d c3 ...].
    Contents of section .rodata:
    80480b9 48656c6c 6f20576f 726c640a 00 Hello World..
    Contents of section .data:
    80490c8 b9800408 ....
    Contents of section .comment:
    0000 00474343 3a202847 4e552920 332e342e .GCC: (GNU) 3.4.
    0010 36202855 62756e74 7520332e 342e362d 6 (Ubuntu 3.4.6-
    0020 38756275 6e747532 2900 8ubuntu2).

    它是gcc編譯後的額外資料,是否可以將它移除???

    答案是肯定的,GNU Binary utility中有一個命令...objcopy...它可以做一些object file(ELF)檔案的處理...

    $ man objcopy
    OBJCOPY(1) GNU Development Tools OBJCOPY(1)

    NAME
    objcopy - copy and translate object files

    SYNOPSIS
    .... 省略 ....
    --remove-section=sectionname
    Remove any section named sectionname from the output file. This option may be
    given more than once. Note that using this option inappropriately may make the
    output file unusable.
    "--remove-section"這個參數正可以用上...

    $ ls -l hello2
    -rwxr-xr-x 1 lungswu lungswu 528 2010-03-01 10:08 hello2
    $
    $ objcopy --remove-section=.comment hello2 hello2.1
    $
    $ ./hello2.1
    Hello World
    $ ls -l hello2.1
    -rwxr-xr-x 1 lungswu lungswu 436 2010-03-01 10:15 hello2.1
    $

    所以已經達到初步的目標,Hello world的code size已經小於512 byte,達到436 bytes.

    Hello World - 再更小code size

    在gcc compiler的參數中,除了nostartfiles可以移除一些code(註:一般來說這是不正常的用法)之外,還有一個參數 nostdlib:
    主要的目的是告知編譯器,編譯時不需要用上default的函式庫。所以...
    (當然在這hello world中,雖然只有利用write(), exit()兩個system call,但是這兩個system call呼叫int 0x80的程式碼還是借用libc的實作來完成,所以預計底下的code以nostdlib的方式連結會發出找不到此兩個function的錯誤。)

    $ cat helloworld.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    #define HELLO_WORLD "Hello World\n"

    int main(void)
    {
    write(STDOUT_FILENO, HELLO_WORLD, sizeof(HELLO_WORLD));

    exit(0);

    return 0;
    }

    $ gcc -nostdlib -nostartfiles helloworld.c -o helloworld
    /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000008048074
    /tmp/cckQkLTF.o: In function `main':
    helloworld.c:(.text+0x34): undefined reference to `write'
    helloworld.c:(.text+0x40): undefined reference to `exit'
    collect2: ld returned 1 exit status
    $
    $

    所以乾脆...利用組合語言的技巧自己寫個write(),exit()的system call...
    (又有一個問題,因為利用nostartfiles的參數編譯,如此並不會建立process正確的stack,以下的code執行時會發生嚴重錯誤。)
    1. 因為之前compiler一直抱怨找不到_start,所以將main改成_start
    2. 這邊暫時先忽略exit() function的警告訊息。

    $ cat hello1.c
    #include <unistd.h>

    unsigned char *str = "Hello World\n";

    ssize_t write (int __fd, __const void *__buf, size_t __n)
    {
    asm("movl %2, %%edx;\
    movl %0, %%ecx;\
    movl %1, %%ebx;\
    movl $4, %%eax;\
    int $0x80;"
    : :"m"(__buf), "r"(__fd), "r"(__n));

    }

    void exit(int ret)
    {
    asm("movl %0, %%ebx;\
    movl $1, %%eax;\
    int $0x80;"
    : : "r"(ret));
    }

    int _start(void)
    {
    write(STDOUT_FILENO, str, sizeof(str));

    exit(0);

    return 0;
    }

    $ gcc -nostartfiles -nostdlib -static hello1.c -o hello1
    hello1.c: In function `exit':
    hello1.c:22: warning: function declared `noreturn' has a `return' statement
    hello1.c:23: warning: `noreturn' function does return
    $
    $ ./hello1
    程式記憶體區段錯誤
    $
    $


    所以...將程式中的stack拿掉....

    $ cat hello2.c
    unsigned char *str = "Hello World\n";

    int _start(void)
    {
    /* write(STDOUT_FILENO, str, sizeof(str)); */
    asm("movl $14, %%edx;\
    movl %0, %%ecx;\
    movl $1, %%ebx;\
    movl $4, %%eax;\
    int $0x80;"
    : :"m"(str));

    /* exit(0); */
    asm("movl $0, %ebx;\
    movl $1, %eax;\
    int $0x80;");

    return 0;
    }

    $ gcc -nostartfiles -nostdlib -static hello2.c -o hello2
    $
    $ ./hello2
    Hello World
    $


    成功了...可以編譯並執行 :)
    來看一下code size...

    $ gcc -nostartfiles -nostdlib -static hello2.c -o hello2
    $ strip hello2
    $ ./hello2
    Hello World
    $
    $ ls -l hello2
    -rwxr-xr-x 1 lungswu lungswu 536 2010-02-26 18:15 hello2
    $

    code size突破低於1024 bytes, 只有536 bytes, 幾乎是0.5K bytes。

    Hello World - 更小code size(3)






    由上面兩圖可知,Hello World程式用printf()跟write()之差別,Hello World字串的輸出可以用write()取代,而不需要使用上libc的函式庫。

    接下來...剩下程式的進入點、離開點的處理。



    進入點的觀察:
    到目前為止(包括上兩張圖)並未解釋如何知道通常一般應用程式的進入點是_start(而C語言的進入點是main)?
    最簡單的方式...

    $ cat helloworld.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    #define HELLO_WORLD "Hello World\n"

    int main(void)
    {
    write(STDOUT_FILENO, HELLO_WORLD, sizeof(HELLO_WORLD));

    return 0;
    }

    $ gcc -nostartfiles helloworld.c -o helloworld
    /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000080481a4
    $

    於編譯中加入-nostartfiles的參數,將編譯時不需要額外的"起跑"程式段,直接呼叫main() function,會有警告訊息,找不到預設的程式進入點(_start),就是它啦.... _start是一個程式的進入點。

    當然,這編譯出來的程式也可以執行只是會有問題(注意:是用動態連結方式編譯)...

    $ gcc -nostartfiles helloworld.c -o helloworld
    /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000080480c0
    $ ./helloworld
    程式記憶體區段錯誤
    $

    程式離開(執行return)時發生問題,所以改成...

    $ cat helloworld.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    #define HELLO_WORLD "Hello World\n"

    int main(void)
    {
    write(STDOUT_FILENO, HELLO_WORLD, sizeof(HELLO_WORLD));

    exit(0);

    return 0;
    }

    $ gcc -nostartfiles helloworld.c -o helloworld
    /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000080481d8
    $
    $ ./helloworld
    Hello World
    $

    來看看它的code size...

    $ gcc helloworld.c -o helloworld.normal -O3
    $ strip helloworld.normal
    $ gcc helloworld.c -o helloworld.nostartfiles -O3 -nostartfiles
    /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000080481d8
    $ strip helloworld.nostartfiles
    $ ls -l
    總計 12
    -rw-r--r-- 1 lungswu lungswu 206 2010-02-26 13:57 helloworld.c
    -rwxr-xr-x 1 lungswu lungswu 2928 2010-02-26 14:02 helloworld.normal
    -rwxr-xr-x 1 lungswu lungswu 1484 2010-02-26 14:02 helloworld.nostartfiles

    code size又減小了(變成1484 bytes)

    Hello World - 更小code size(2)

    來trace 上一篇中用write()寫的Hello World程式吧:)

    $ gcc fn.c -g -static -o fn
    $ objdump -S fn
    .... 省略 ....
    int main(void)
    {
    80481f4: 55 push %ebp
    80481f5: 89 e5 mov %esp,%ebp
    80481f7: 83 ec 18 sub $0x18,%esp
    80481fa: 83 e4 f0 and $0xfffffff0,%esp
    80481fd: b8 00 00 00 00 mov $0x0,%eax
    8048202: 83 c0 0f add $0xf,%eax
    8048205: 83 c0 0f add $0xf,%eax
    8048208: c1 e8 04 shr $0x4,%eax
    804820b: c1 e0 04 shl $0x4,%eax
    804820e: 29 c4 sub %eax,%esp
    write(STDOUT_FILENO, HELLO_WORLD, sizeof(HELLO_WORLD));
    8048210: c7 44 24 08 0d 00 00 movl $0xd,0x8(%esp)
    8048217: 00
    8048218: c7 44 24 04 c8 64 0a movl $0x80a64c8,0x4(%esp)
    804821f: 08
    8048220: c7 04 24 01 00 00 00 movl $0x1,(%esp)
    8048227: e8 34 66 00 00 call 804e860 <__libc_write>

    return 0;
    804822c: b8 00 00 00 00 mov $0x0,%eax
    }
    8048231: c9 leave
    8048232: c3 ret
    8048233: 90 nop
    .... 省略 ....
    0804e860 <__libc_write>:
    .... 省略 ....
    804e86a: 53 push %ebx
    804e86b: 8b 54 24 10 mov 0x10(%esp),%edx
    804e86f: 8b 4c 24 0c mov 0xc(%esp),%ecx
    804e873: 8b 5c 24 08 mov 0x8(%esp),%ebx
    804e877: b8 04 00 00 00 mov $0x4,%eax
    804e87c: cd 80 int $0x80
    .... 省略 ....

    所以...他的順序... main() -> write -> __libc_write
    那之後的????
    注意!!!有一個
    int 0x80

    這是一個Linux kernel的進入點(學名稱做system call)
    所以到這邊先一個總結...(接下一篇)

    Hello World - 更小code size(1)

    在做更小的code size之前,先來追蹤printf到底呼叫了哪些function,達到輸出字串的功能...
    一個tool.... objdump, 它可以做反組譯的工作...

    $ gcc -g -static helloworld.c -o helloworld
    $ objdump -S helloworld | less
    .... 省略 ....
    int main(void)
    {
    80481f4: 55 push %ebp
    80481f5: 89 e5 mov %esp,%ebp
    .... 省略 ....
    printf("Hello World\n");
    8048210: c7 04 24 e8 64 0a 08 movl $0x80a64e8,(%esp)
    8048217: e8 e4 09 00 00 call 8048c00 <_IO_printf>
    .... 省略 ....
    08048c00 <_IO_printf>:
    8048c00: 55 push %ebp
    8048c01: 89 e5 mov %esp,%ebp
    8048c03: 83 ec 0c sub $0xc,%esp
    8048c06: 8d 45 0c lea 0xc(%ebp),%eax
    8048c09: 89 44 24 08 mov %eax,0x8(%esp)
    8048c0d: 8b 45 08 mov 0x8(%ebp),%eax
    8048c10: 89 44 24 04 mov %eax,0x4(%esp)
    8048c14: a1 b8 2e 0c 08 mov 0x80c2eb8,%eax
    8048c19: 89 04 24 mov %eax,(%esp)
    8048c1c: e8 af e9 00 00 call 80575d0 <_IO_vfprintf>
    .... 省略 .... 以下不追了

    總而言之 printf() -> _IO_printf() -> _IO_vfprintf() -> ...
    printf()這函數經過很多處理,但是最後看printf的文件...

    $ man 3 printf
    ....
    The functions printf() and vprintf() write output to stdout, the standard output stream; fprintf() and vfprintf() write output to the given output stream;

    所以,printf(); 同等於 fprintf(stdout, ...);

    stdout是POSIX中預設的file descriptor。

    除了file descriptor之外,還有file number,他們之間一定有什麼關係???

    printf(fprintf)處理到最後,一定會呼叫write();此function,看以下的程式以及執行結果吧...

    $ cat fn.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    #define HELLO_WORLD "Hello World\n"

    int main(void)
    {
    printf("%d, %d\n", STDOUT_FILENO, fileno(stdout));

    write(fileno(stdout), HELLO_WORLD, sizeof(HELLO_WORLD));

    return 0;
    }

    $ gcc -Wall fn.c -o fn
    $ ./fn
    1, 1
    Hello World
    $

    如此,hello world程式執些寫成這樣就行...

    #include <unistd.h>

    #define HELLO_WORLD "Hello World\n"

    int main(void)
    {
    write(STDOUT_FILENO, HELLO_WORLD, sizeof(HELLO_WORLD));

    return 0;
    }

    Hello World code size (2)

    於Hello World code size (1)之中,還有一個編譯不一樣之處....

    -O3

    這是指編譯時所採用最佳化的選項,其一般可以選擇的選項計有...

    -O -O0 -O1 -O2 -O3 -Os

    其中異同可以參考Linux中的man page,裡面有詳細的說明。
    ....
    使用不同最佳化方式所編譯出來的執行檔,其檔案大小觀察如下

    $ gcc -Wall helloworld.c -o helloworld
    $ gcc -Wall helloworld.c -o helloworld.O3 -O3
    $ gcc -Wall helloworld.c -o helloworld.Os -Os
    $ ls -l
    總計 28
    -rwxr-xr-x 1 lungswu lungswu 6351 2010-02-25 10:37 helloworld
    -rw-r--r-- 1 lungswu lungswu 123 2010-02-25 10:00 helloworld.c
    -rwxr-xr-x 1 lungswu lungswu 6325 2010-02-25 10:37 helloworld.O3
    -rwxr-xr-x 1 lungswu lungswu 6309 2010-02-25 10:37 helloworld.Os
    $

    當然,以上是以gcc-3.4編譯出來的結果,目前所做實驗中,最新的編譯器版本是gcc-4.3,同時,其編譯出來的結果會隨著編譯器的版本不同而產生不同結果,以下是以gcc-3.4以及gcc-4.3做不同編譯方式所產生的結果....

    $ gcc -v
    Reading specs from /usr/lib/gcc/i486-linux-gnu/3.4.6/specs
    Configured with: ../src/configure -v --enable-languages=c,f77 --prefix=/usr --libexecdir=/usr/lib --with-gxx-include-dir=/usr/include/c++/3.4 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --program-suffix=-3.4 --enable-__cxa_atexit --with-tune=pentium4 i486-linux-gnu
    Thread model: posix
    gcc version 3.4.6 (Ubuntu 3.4.6-8ubuntu2)
    $ gcc -Wall helloworld.c -o helloworld.3.4
    $ gcc -Wall helloworld.c -o helloworld.3.4.Os -Os
    $ gcc -Wall helloworld.c -o helloworld.3.4.O3 -O3
    $ gcc -Wall helloworld.c -o helloworld.3.4.O0 -O0
    $
    $ gcc -v
    使用內建 specs。
    目的:i486-linux-gnu
    配置為:../src/configure -v --with-pkgversion='Ubuntu 4.3.3-5ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
    執行緒模型:posix
    gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)
    $ gcc -Wall helloworld.c -o helloworld.4.3
    $ gcc -Wall helloworld.c -o helloworld.4.3.Os -Os
    $ gcc -Wall helloworld.c -o helloworld.4.3.O3 -O3
    $ gcc -Wall helloworld.c -o helloworld.4.3.O0 -O0
    $ ls -l
    總計 84
    -rwxr-xr-x 1 lungswu lungswu 6351 2010-02-25 10:43 helloworld.3.4
    -rwxr-xr-x 1 lungswu lungswu 6351 2010-02-25 10:43 helloworld.3.4.O0
    -rwxr-xr-x 1 lungswu lungswu 6325 2010-02-25 10:43 helloworld.3.4.O3
    -rwxr-xr-x 1 lungswu lungswu 6309 2010-02-25 10:43 helloworld.3.4.Os
    -rwxr-xr-x 1 lungswu lungswu 9150 2010-02-25 10:43 helloworld.4.3
    -rwxr-xr-x 1 lungswu lungswu 9150 2010-02-25 10:44 helloworld.4.3.O0
    -rwxr-xr-x 1 lungswu lungswu 9150 2010-02-25 10:44 helloworld.4.3.O3
    -rwxr-xr-x 1 lungswu lungswu 9150 2010-02-25 10:43 helloworld.4.3.Os
    -rw-r--r-- 1 lungswu lungswu 123 2010-02-25 10:00 helloworld.c
    $

    gcc-4.3做了不少改變,簡單程式不會因為使用不同的最佳化參數而產生不同的code size,其結果都一樣。


    另外回到Hello World code size (1),之中提到一個命令:"file"

    $ gcc -Wall helloworld.c -o helloworld
    $ file helloworld
    helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
    $

    file的結果中,有提到一個key word:"not stripped",這裡可以用以下方式找strip這個keyword之涵義:

    $ whatis strip
    strip (1) - Discard symbols from object files.
    $

    所以strip這命令可以將執行檔中所有symbol移除,所以應該會使code size小很多...

    $ file helloworld
    helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
    $ ls -l helloworld
    -rwxr-xr-x 1 lungswu lungswu 6351 2010-02-25 10:52 helloworld
    $ strip helloworld
    $ ls -l helloworld
    -rwxr-xr-x 1 lungswu lungswu 2876 2010-02-25 11:06 helloworld
    $

    使用strip command之後,code size減少了6351-2876=3475 bytes (with gcc-3.4)

    Hello World code size (1)

    目的為...
  • 觀察簡單程式使用不同編譯方式得到的結果有何不同。
  • 以用不同編譯方式找出較小的code size,以及他們之間的差異。
  • 最後找出減少code size的方式。
    Source code如下...

    1 /*
    2 * File name : helloworld.c
    3 */
    4
    5 #include <stdio.h>
    6
    7 int main(void)
    8 {
    9
    10 printf("Hello World\n");
    11
    12 return 0;
    13 }
    14

    用了以下四種不同的編譯參數以及其結果...

    $ gcc helloworld.c -o helloworld
    $ ls -l helloworld
    -rwxr-xr-x 1 lungswu lungswu 6159 2010-02-24 22:57 helloworld
    $
    $ gcc helloworld.c -o helloworld -O3
    $ ls -l helloworld
    -rwxr-xr-x 1 lungswu lungswu 6141 2010-02-24 22:57 helloworld
    $
    $ gcc helloworld.c -o helloworld -static
    $ ls -l helloworld
    -rwxr-xr-x 1 lungswu lungswu 558138 2010-02-24 22:58 helloworld
    $
    $ gcc helloworld.c -o helloworld -static -O3
    $ ls -l helloworld
    -rwxr-xr-x 1 lungswu lungswu 558740 2010-02-24 22:58 helloworld
    $

    發現以上四種編譯方式中,使用"gcc helloworld.c -o helloworld -O3"的編譯方式可以產生最小的code size(總共6141 bytes)

    但是有一點需注意:編譯的時候加入"-static",code size變的很大....
    原因無它,這是因為一般程式在做連結/執行的時候,會需要用到額外標準函式庫,很明顯的此HelloWorld程式於執行時會連結標準函式庫(libc),且至少printf()此函數為libc之中的一函數。

    一般程式於編譯時,會將這些標準函式庫的函式主體放於別的檔案,程式於執行時再由OS(loader)判斷,且適時載入記憶體之中供執行中程式呼叫使用,此種機制稱做為"動態聯結"(dynamically link)。
    反之,若連結程式時將這些函數一起連結於程式之中,稱之為靜態連結(statically link),可以以-static指定為靜態連結。
    由於靜態連結會將標準函式庫編入程式之中,所以程式大小會差很多,有558740/6141=91倍之多。

    要如何判別一檔案是動態連結還是靜態連結,可以用UNIX下的一個command:"file"

    $ gcc helloworld.c -o helloworld
    $ file helloworld
    helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, not stripped
    $
    $
    $ gcc helloworld.c -o helloworld -static
    $ file helloworld
    helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, for GNU/Linux 2.6.8, not stripped
    $

    另外一個問題,要知道動態聯結的程式有用到哪些額外的函式庫?可以使用"ldd" command。
    由以下的實做可以得知,執行檔"helloworld"會使用到"linux-gate.so.1","libc.so.6"以及"/lib/ld-linux.so.2"此三的動態函式庫。

    $ ldd helloworld
    linux-gate.so.1 => (0xb7fa2000)
    libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb7e36000)
    /lib/ld-linux.so.2 (0xb7fa3000)
    $
  • Tuesday, August 25, 2009

    Hello World入門(3)

    於Hello World入門(2)中一個檔案HelloWorld.c,它是一隻C語言程式。
    傳統上,C語言程式檔案都以.c或者.h檔為結尾,本程式也不例外。

    要使此程式可以執行,需經過C compiler編譯、連結無誤之後產生一可執行檔。

    這裡補充說明,C語言語法是一個標準,現在標準官方網站。
    而有不同的廠商、團體根據C語言標準製作他們自產的編譯器,比方說Microsoft,GNU,Intel...。

    現在以GNU製作的編譯器為例,要將HelloWorld.c編譯成執行檔,
    我習慣以底下命令編譯C語言程式...

    gcc -Wall HelloWorld.c -o HelloWorld

    這將會產生HelloWorld的執行檔。
  • gcc:GNU C編譯器的命令
  • -Wall:gcc參數,列出所有gcc認為有危險、不妥當程式碼片段
  • HelloWorld.c:程式設計者所編寫程式檔名
  • -o HelloWorld:指定編譯完成的執行檔為名稱,若無此參數預設檔名為a.out


    很多理論的書本中提到,將一的原始程式要編譯成執行檔的過程,需經過以下步驟...
    1.編譯。2.連結。
    或者...
    1.編譯。2.組譯。3.連結。

    GNU C編譯器也不例外,遵循以上三步驟的慣例,我們可以用以下方式編譯
    $gcc -Wall -v HelloWorld.c -o HelloWorld
    得到編譯時的詳細內容、步驟
    Using built-in specs.
    Target: i486-linux-gnu
    Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-cld --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
    Thread model: posix
    gcc version 4.3.2 (Debian 4.3.2-1.1)
    COLLECT_GCC_OPTIONS='-Wall' '-v' '-o' 'HelloWorld' '-mtune=generic'
    /usr/lib/gcc/i486-linux-gnu/4.3.2/cc1 -quiet -v HelloWorld.c -quiet -dumpbase HelloWorld.c -mtune=generic -auxbase HelloWorld -Wall -version -o /tmp/cc3KesF0.s
    ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
    ignoring nonexistent directory "/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../i486-linux-gnu/include"
    #include "..." search starts here:
    #include <...> search starts here:
    /usr/local/include
    /usr/lib/gcc/i486-linux-gnu/4.3.2/include
    /usr/lib/gcc/i486-linux-gnu/4.3.2/include-fixed
    /usr/include/i486-linux-gnu
    /usr/include
    End of search list.
    GNU C (Debian 4.3.2-1.1) version 4.3.2 (i486-linux-gnu)
    compiled by GNU C version 4.3.2, GMP version 4.2.2, MPFR version 2.3.2.
    warning: MPFR header version 2.3.2 differs from library version 2.3.1.
    GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=129511
    Compiler executable checksum: c4cd35f2318868df36b839e33a8e2089
    COLLECT_GCC_OPTIONS='-Wall' '-v' '-o' 'HelloWorld' '-mtune=generic'
    as -V -Qy -o /tmp/ccA8J2A1.o /tmp/cc3KesF0.s
    GNU assembler version 2.18.0 (i486-linux-gnu) using BFD version (GNU Binutils for Debian) 2.18.0.20080103
    COMPILER_PATH=/usr/lib/gcc/i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/:/usr/lib/gcc/i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/:/usr/lib/gcc/i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/
    LIBRARY_PATH=/usr/lib/gcc/i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../:/lib/:/usr/lib/
    COLLECT_GCC_OPTIONS='-Wall' '-v' '-o' 'HelloWorld' '-mtune=generic'
    /usr/lib/gcc/i486-linux-gnu/4.3.2/collect2 --eh-frame-hdr -m elf_i386 --hash-style=both -dynamic-linker /lib/ld-linux.so.2 -o HelloWorld /usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/crt1.o /usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/crti.o /usr/lib/gcc/i486-linux-gnu/4.3.2/crtbegin.o -L/usr/lib/gcc/i486-linux-gnu/4.3.2 -L/usr/lib/gcc/i486-linux-gnu/4.3.2 -L/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/i486-linux-gnu/4.3.2/../../.. /tmp/ccA8J2A1.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i486-linux-gnu/4.3.2/crtend.o /usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/crtn.o

    以上囉哩八說這麼多,只要抓住底下的要點
    /usr/lib/gcc/i486-linux-gnu/4.3.2/cc1
    為第一步驟...編譯,產生一個組合語言程式。

    as -V -Qy -o /tmp/ccA8J2A1.o /tmp/cc3KesF0.s
    為第二步驟...將第一步驟產生的組合語言程式做組譯工作,
    產生object code(中文我不會翻譯)。

    /usr/lib/gcc/i486-linux-gnu/4.3.2/collect2
    為第二步驟...將第二步驟產生的object code做聯結(link)工作,
    產稱執行程式。



    做最後補充,連結的步驟可以將一個以上的object聯結(link)成一個執行程式。
    之所以會有聯結(link)的步驟,最主要目的是...
  • 可以將很大一段的程式碼分割成很多的程式檔,個別編譯,方便程式管理。
  • 將程式是分割,若發生編譯錯誤,只需修改、編譯錯誤的程式,不需所有程式重新編譯,以節省編譯時間。
  • 可以將已編譯的object code交給第三者,達到資料隱藏。
  • 連結他人撰寫編譯好的程式庫(動作和上一點相反)
  • 使執行檔於執行時,做好程式碼於記憶體中的佈局規劃。
  • 增加執行檔檔頭,使program loader辨認、讀取相關訊息。
  • 一般狀況而言,也會自動聯結系統預設的程式庫。
  • Saturday, August 22, 2009

    Hello World閒聊

    我真是井底之蛙....

    這blog有關Hello World的文章前幾天貼在BBS上之後,心想我是否可以以個人的力量寫出不同程式語言的Hello World程式,方可號稱說這是真正的Hello World大全...

    很顯然的我事前沒有做足夠功課,看了以下的網站就可以知道我這想法是多可笑了...哈哈哈
    中文維基百科中Hello World條目
    英文維基百科中Hello World條目
    很多程式語言看都都沒看過

    Thursday, August 20, 2009

    Hello World 入門(2)

    之前寫了兩個很件單的程式,程式目的在電腦螢幕上秀出"Hello World"字串。
    但是要使它能夠正確的執行,需要一些步驟。


    在cygwin(或是Linux/UNIX)下的操作範例,shell程式(1)....
    $ cat > HelloWorld.sh
    #!/bin/sh
    echo "Hello World"
    ^Z
    $ sh ./HelloWorld.sh
    Hello World
    
    $
    



    在cygwin下的操作範例,C程式(2)....
    $ cat > HelloWorld.c
    #include <stdio.h>
    
    int main(void)
    {
    
    printf("Hello World\n");
    
    return 0;
    }
    ^Z
    $ gcc -Wall HelloWorld.c -o HelloWorld
    
    $ ./HelloWorld.exe
    Hello World
    
    $
    



    如果是windows的使用者,如何秀出Hello World字串?
    在console底下,如下步驟...
    C:\>copt con HelloWorld.bat
    @echo Hello World
    ^Z
    C:\>HelloWorld.bat
    Hello World
    
    C:\>
    



    但若如果想要將Hello World不單單秀在電腦螢幕上,還要全世界的人都有機會看到我的程式...
    可能如下
    1 <html>
    2
    3   <head>
    4     <title>
    5     </title>
    6   </head>
    7
    8   <body>
    9     Hello World
    10   </body>
    11
    12 </html>
    

    將此程式儲存成index.html,再放入網站伺服器上,就可以了... :)


    補充...
    當然,還有多的方式讓電腦用命令的方式達到相同的目的,比方說....
    1.需藉助編譯程式方式:C++,JAVA,PASCAL,assembly,Win32,MFC,X lib,COBOL,QT...
    2.只需利用直譯程式的方式:Basic,Perl,Python,lisp,Lua...

    再加個python的例子
    $ which python
    /usr/bin/python
    
    $ cat > HelloWorld.py
    #!/usr/bin/python
    print "Hello World"
    ^Z
    $ chmod a+x HelloWorld.py
    
    $ ./HelloWorld.py
    Hello World
    
    $
    

    這邊再加個JAVA的Hello World程式(2010.02.23)
      1 /*
      2  * File name: helloworld.java
      3  *
      4  * compile: javac helloworld.java
      5  * running: java helloworld
      6  */
      7 public class helloworld
      8 {
      9     public static void main(String[] args)
     10     {
     11         System.out.println("Hello World");
     12     }
     13 }
     14 
    

    這裡我只是學習C語言,所有大部分是介紹C語言寫Hello World程式

    Wednesday, August 19, 2009

    Hello World 入門(1)

    最簡單的程式(-)...

    1 #!/bin/sh
    2 echo "Hello World"


    最簡單的程式(二)...

    1 #include <stdio.h>
    2
    3 int main(void)
    4 {
    5
    6 printf("Hello World\n");
    7
    8 return 0;
    9 }