assembly002-2019-04-21

//"sleep.c"
#include <stdlib.h>
int main(){
     while(1){
         sleep(1000);
     }
     return 0;
}
root@vultr:~/clang# gcc -static sleep.c 
sleep.c: In function ‘main’:
sleep.c:4:7: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
       sleep(1000);
       ^
//"sleep.c"
#include<unistd.h>
#include <stdlib.h>
int main(){
   while(1){
      sleep(1000);
   }
   return 0;
}
root@vultr:~/clang# gcc -static sleep.c 
root@vultr:~/clang# ls
a.out  sleep.c
root@vultr:~/clang# ./a.out 
^C
root@vultr:~/clang# readelf -S a.out 
There are 33 section headers, starting at offset 0xde578:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .note.ABI-tag     NOTE             0000000000400190  00000190
       0000000000000020  0000000000000000   A       0     0     4
  [ 2] .note.gnu.build-i NOTE             00000000004001b0  000001b0
       0000000000000024  0000000000000000   A       0     0     4
  [ 3] .rela.plt         RELA             00000000004001d8  000001d8
       00000000000000f0  0000000000000018  AI       0    24     8
  [ 4] .init             PROGBITS         00000000004002c8  000002c8
       000000000000001a  0000000000000000  AX       0     0     4
  [ 5] .plt              PROGBITS         00000000004002f0  000002f0
       00000000000000a0  0000000000000000  AX       0     0     16
  [ 6] .text             PROGBITS         0000000000400390  00000390
       000000000009e634  0000000000000000  AX       0     0     16
  [ 7] __libc_freeres_fn PROGBITS         000000000049e9d0  0009e9d0
       0000000000002529  0000000000000000  AX       0     0     16
  [ 8] __libc_thread_fre PROGBITS         00000000004a0f00  000a0f00
       00000000000000de  0000000000000000  AX       0     0     16
  [ 9] .fini             PROGBITS         00000000004a0fe0  000a0fe0
       0000000000000009  0000000000000000  AX       0     0     4
  [10] .rodata           PROGBITS         00000000004a1000  000a1000
       000000000001d244  0000000000000000   A       0     0     32
  [11] __libc_subfreeres PROGBITS         00000000004be248  000be248
       0000000000000050  0000000000000000   A       0     0     8
  [12] __libc_atexit     PROGBITS         00000000004be298  000be298
       0000000000000008  0000000000000000   A       0     0     8
  [13] .stapsdt.base     PROGBITS         00000000004be2a0  000be2a0
       0000000000000001  0000000000000000   A       0     0     1
  [14] __libc_thread_sub PROGBITS         00000000004be2a8  000be2a8
       0000000000000008  0000000000000000   A       0     0     8
  [15] .eh_frame         PROGBITS         00000000004be2b0  000be2b0
       000000000000b00c  0000000000000000   A       0     0     8
  [16] .gcc_except_table PROGBITS         00000000004c92bc  000c92bc
       00000000000000a3  0000000000000000   A       0     0     1
  [17] .tdata            PROGBITS         00000000006c9eb8  000c9eb8
       0000000000000020  0000000000000000 WAT       0     0     8
  [18] .tbss             NOBITS           00000000006c9ed8  000c9ed8
       0000000000000030  0000000000000000 WAT       0     0     8
  [19] .init_array       INIT_ARRAY       00000000006c9ed8  000c9ed8
       0000000000000010  0000000000000000  WA       0     0     8
  [20] .fini_array       FINI_ARRAY       00000000006c9ee8  000c9ee8
       0000000000000010  0000000000000000  WA       0     0     8
  [21] .jcr              PROGBITS         00000000006c9ef8  000c9ef8
       0000000000000008  0000000000000000  WA       0     0     8
  [22] .data.rel.ro      PROGBITS         00000000006c9f00  000c9f00
       00000000000000e4  0000000000000000  WA       0     0     32
  [23] .got              PROGBITS         00000000006c9fe8  000c9fe8
       0000000000000010  0000000000000008  WA       0     0     8
  [24] .got.plt          PROGBITS         00000000006ca000  000ca000
       0000000000000068  0000000000000008  WA       0     0     8
  [25] .data             PROGBITS         00000000006ca080  000ca080
       0000000000001ad0  0000000000000000  WA       0     0     32
  [26] .bss              NOBITS           00000000006cbb60  000cbb50
       0000000000001878  0000000000000000  WA       0     0     32
  [27] __libc_freeres_pt NOBITS           00000000006cd3d8  000cbb50
       0000000000000030  0000000000000000  WA       0     0     8
  [28] .comment          PROGBITS         0000000000000000  000cbb50
       0000000000000035  0000000000000001  MS       0     0     1
  [29] .note.stapsdt     NOTE             0000000000000000  000cbb88
       0000000000000f18  0000000000000000           0     0     4
  [30] .shstrtab         STRTAB           0000000000000000  000de40f
       0000000000000169  0000000000000000           0     0     1
  [31] .symtab           SYMTAB           0000000000000000  000ccaa0
       000000000000b100  0000000000000018          32   712     8
  [32] .strtab           STRTAB           0000000000000000  000d7ba0
       000000000000686f  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
root@vultr:~/clang# readelf -l a.out 

Elf file type is EXEC (Executable file)
Entry point 0x400890
There are 6 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x00000000000c935f 0x00000000000c935f  R E    200000
  LOAD           0x00000000000c9eb8 0x00000000006c9eb8 0x00000000006c9eb8
                 0x0000000000001c98 0x0000000000003550  RW     200000
  NOTE           0x0000000000000190 0x0000000000400190 0x0000000000400190
                 0x0000000000000044 0x0000000000000044  R      4
  TLS            0x00000000000c9eb8 0x00000000006c9eb8 0x00000000006c9eb8
                 0x0000000000000020 0x0000000000000050  R      8
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     10
  GNU_RELRO      0x00000000000c9eb8 0x00000000006c9eb8 0x00000000006c9eb8
                 0x0000000000000148 0x0000000000000148  R      1

 Section to Segment mapping:
  Segment Sections...
   00     .note.ABI-tag .note.gnu.build-id .rela.plt .init .plt .text __libc_freeres_fn __libc_thread_freers_fn .fini .rodata __libc_subfreeres __libc_atexit .stapsdt.base __libc_thread_subfreeres .eh_frame .gcc_exept_table 
   01     .tdata .init_array .fini_array .jcr .data.rel.ro .got .got.plt .data .bss __libc_freeres_ptrs 
   02     .note.ABI-tag .note.gnu.build-id 
   03     .tdata .tbss 
   04     
   05     .tdata .init_array .fini_array .jcr .data.rel.ro .got 
root@vultr:~/clang# ./a.out &
[2] 18711
root@vultr:~/clang# cat /proc/18711/maps
00400000-004ca000 r-xp 00000000 fd:01 258898                             /root/clang/a.out
006c9000-006cc000 rw-p 000c9000 fd:01 258898                             /root/clang/a.out
006cc000-006ce000 rw-p 00000000 00:00 0 
021b1000-021d4000 rw-p 00000000 00:00 0                                  [heap]
7fff8a2fd000-7fff8a31e000 rw-p 00000000 00:00 0                          [stack]
7fff8a381000-7fff8a384000 r--p 00000000 00:00 0                          [vvar]
7fff8a384000-7fff8a386000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
root@vultr:~/clang# ps -a
  PID TTY          TIME CMD
18701 pts/1    00:00:00 a.out
18711 pts/1    00:00:00 a.out
18718 pts/0    00:00:00 ps
root@vultr:~/clang# kill 18701
root@vultr:~/clang# ps -a
  PID TTY          TIME CMD
18711 pts/1    00:00:00 a.out
18724 pts/0    00:00:00 ps

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容