Friday, December 27, 2013

Linux GDB breakpoint at _start

寫一個Linux上user mode最簡單的AP
$ cat foo.s
.text
.globl _start

_start:
        movl    $0,%ebx         /* exit code */
        movl    $1,%eax         /* exit function */
        int     $0x80
這樣就可以正常的執行並結束。
但怪的事...

$ as foo.s -o foo.o
$ ld foo.o -o foo

這樣邊一之後再用GDB來debug...
(gdb) break _start
Breakpoint 1 at 0x8048054
(gdb) run
Starting program: /home/lungswu/Working/application/hello/foo 
(no debugging symbols found)

Program exited normally.
(gdb) 
程式竟然直接結速,並不能breaking在_start上????

類似的編譯方式也會發生同樣問題
$ gcc -nostartfiles --static foo.s -o foo
$ gcc -nostartfiles -nostdlib foo.s -o foo
但是以下的方式就OK
$ gcc -nostartfiles foo.s -o foo

原因不明,但至少可以確定的事...

(1)可以用GDB停在_start上是一個動態連結檔
$ ldd foo
 linux-gate.so.1 =>  (0xb80d2000)
 libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7f60000)
 /lib/ld-linux.so.2 (0xb80d3000)
(2)觀察他的各區段,明顯複雜很多
$ objdump -h foo

foo:     file format elf32-i386

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .interp       00000013  080480f4  080480f4  000000f4  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .hash         0000000c  08048108  08048108  00000108  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  2 .gnu.hash     00000018  08048114  08048114  00000114  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 .dynsym       00000000  0804812c  0804812c  0000012c  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .dynstr       0000000b  0804812c  0804812c  0000012c  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  5 .text         0000000c  08048138  08048138  00000138  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  6 .dynamic      00000070  08049f84  08049f84  00000f84  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  7 .got.plt      0000000c  08049ff4  08049ff4  00000ff4  2**2
                  CONTENTS, ALLOC, LOAD, DATA

No comments:

Post a Comment