ELF檔案認知(一 可執行檔案的生成

2022-03-09 15:48:45 字數 4204 閱讀 7669

以下兩條指令可以均可以使原始檔(.c)預處理,得到乙個原始檔(.i)

~$ cat hello.c

#include"stdio.h"

int main()

cpp hello.c >hello.i

gcc -e hello.c -o hello.i

~$ cat hello.i

typedef unsigned char __u_char;

typedef unsigned short int __u_short;

typedef unsigned int __u_int;

typedef unsigned long int __u_long;

extern int fprintf (file *__restrict __stream,

const char *__restrict __format, ...);

extern int printf (const char *__restrict __format, ...);

extern int sprintf (char *__restrict __s,

const char *__restrict __format, ...) __attribute__ ((__nothrow__));

編譯過程就是將預處理後得到的預處理檔案(如 hello.i)進行

詞法分析、語法分析、語義分析、優化後,生成彙編**檔案。

由編譯器(compiler)對編譯程式處理

從hello.i->hello.s 組合語言的出現,但cpu認識0和1

gcc -s hello.i -o hello.s

gcc -s hello.c -o hello.s

/usr/lib/gcc/x86_64-linux-gnu/5/cc1 hello.c //可以用gcc -v來檢視gcc的路徑及具體問題具體分析。

/usr/lib/gcc/x86_64-linux-gnu/5/cc1 hello.c

main

analyzing compilation unit

performing interprocedural optimizations

assembling functions:

main

execution times (seconds)

phase setup : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.03 (16%) wall 1093 kb (65%) ggc

phase parsing : 0.01 (100%) usr 0.01 (33%) sys 0.05 (26%) wall 520 kb (31%) ggc

phase opt and generate : 0.00 ( 0%) usr 0.02 (67%) sys 0.10 (53%) wall 56 kb ( 3%) ggc

ipa inlining heuristics : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 ( 5%) wall 0 kb ( 0%) ggc

preprocessing : 0.00 ( 0%) usr 0.01 (33%) sys 0.03 (16%) wall 218 kb (13%) ggc

parser (global) : 0.01 (100%) usr 0.00 ( 0%) sys 0.00 ( 0%) wall 286 kb (17%) ggc

parser struct body : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 ( 5%) wall 12 kb ( 1%) ggc

parser function body : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 ( 5%) wall 2 kb ( 0%) ggc

tree gimplify : 0.00 ( 0%) usr 0.01 (33%) sys 0.01 ( 5%) wall 2 kb ( 0%) ggc

tree cfg construction : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 ( 5%) wall 1 kb ( 0%) ggc

expand : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 ( 5%) wall 2 kb ( 0%) ggc

integrated ra : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 ( 5%) wall 24 kb ( 1%) ggc

lra non-specific : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 ( 5%) wall 0 kb ( 0%) ggc

shorten branches : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 ( 5%) wall 0 kb ( 0%) ggc

rest of compilation : 0.00 ( 0%) usr 0.00 ( 0%) sys 0.01 ( 5%) wall 14 kb ( 1%) ggc

unaccounted todo : 0.00 ( 0%) usr 0.01 (33%) sys 0.01 ( 5%) wall 0 kb ( 0%) ggc

total : 0.01 0.03 0.19 1686 kb

chen@ubuntu:~$ cat hello.s

.file "hello.c"

.section .rodata

.lc0:

.string "hello world"

.text

.globl main

.type main, @function

main:

.lfb0:

.cfi_startproc

pushq %rbp

.cfi_def_cfa_offset 16

.cfi_offset 6, -16

movq %rsp, %rbp

.cfi_def_cfa_register 6

movl $.lc0, %edi

call puts

movl $0, %eax

popq %rbp

.cfi_def_cfa 7, 8

ret.cfi_endproc

.lfe0:

.size main, .-main

.ident "gcc: (ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"

.section .note.gnu-stack,"",@progbits

組合語言-->機器指令

此處產生的hello.o是二進位制檔案,可重定位目標檔案

as hello.s -o hello.o

gcc –c hello.s –o hello.o

gcc –c hello.c –o hello.o

多個.o檔案鏈結產生可執行檔案

將a.0與b.o鏈結成可執行檔案

從磁碟對映到虛擬空間

參考:

可執行檔案 ELF 格式

elf executable and linking format 是一種物件檔案的格式,用於定義不同型別的物件檔案 object files 中都放了什麼東西 以及都以什麼樣的格式去放這些東西。它自最早在 system v 系統上出現後,被 unix 世界所廣泛接受,作為預設的二進位制檔案格式來使...

ELF學習 可執行檔案

相比較重定位檔案,可執行檔案的elf header中入口位址是0x8048320.而且除了section header外,還存在program header.program header起始於第52個位元組,因此program header應該是緊接著elf header。可執行檔案的elf 布局如...

Linux下ELF可執行檔案裝載與執行

1 建立子程序 核心建立task struct資料結構,繼承父程序的虛擬位址空間 virtual memory space,vms 2 呼叫execve 系統呼叫執行指定的elf檔案 1 呼叫核心態函式sys execve 動態申請乙個linux binprm資料結構,並用elf可執行檔案的資料填充...