歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Linux可執行文件的深入分析(一)

 一)可執行腳本

  可執行腳本是內核指向解釋程序的一個文本文件,這個文件必須是一個可執行目標文件,如果不可執行,execve會產生錯誤.

  二)可執行目標文件

  可執行目標文件主要有三種格式:a.out,COFF,ELF

  1)a.out

  a.out是assembler output的縮寫格式,代表匯編程序輸出.

  a.out是早期UNIX系統使用的可執行文件,現在已被ELF文件格式代替,用gcc編譯出的a.out也只是一個可執行文件,而不再是文件格式.

  用file a.out或od -c a.out|head或者readelf -h a.out來確認它的文件格式確實是ELF.

  2)ECOFF

  在MIPS結構下進行編譯時,內核也可以識別ECOFF格式,它是一個ELF格式出現之前的COFF(通用對象文件格式)的變體.

  同樣微軟的PE文件格式也是從COFF發展而來的.

  3)ELF

  ELF文件有三種類型:

  可重定位文件:也就是通常稱的目標文件,後綴為.o

  共享文件:也就是通常稱的庫文件,後綴為.so

  可執行文件:也就是二進制可執行程序,我們所指的ELF文件正是這種類型.

  內核通過查找文件中的魔法數來識別目標文件,例如,ELF文件的標簽在文件的頭4個字節,分別是7F,45,4c,46

  也就是大家經常看到177,'E','L','F'.

  例如:

  readelf -h test

  ELF Header:

  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00

  Class:                             ELF64

  Data:                              2's complement, little endian

  Version:                           1 (current)

  OS/ABI:                            UNIX - System V

  ABI Version:                       0

  Type:                              EXEC (Executable file)

  Machine:                           Advanced Micro Devices X86-64

  Version:                           0x1

  Entry point address:               0x4003e0

  Start of program headers:          64 (bytes into file)

  Start of section headers:          4304 (bytes into file)

  Flags:                             0x0

  Size of this header:               64 (bytes)

  Size of program headers:           56 (bytes)

  Number of program headers:         8

  Size of section headers:           64 (bytes)

  Number of section headers:         37

  Section header string table index: 34

  但是可重定位文件(.o文件)的前四個字節也是7F,45,4C,46

  例如:

  readelf -h test.o

  ELF Header:

  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00

  Class:                             ELF64

  Data:                              2's complement, little endian

  Version:                           1 (current)

  OS/ABI:                            UNIX - System V

  ABI Version:                       0

  Type:                              REL (Relocatable file)

  Machine:                           Advanced Micro Devices X86-64

  Version:                           0x1

  Entry point address:               0x0

  Start of program headers:          0 (bytes into file)

  Start of section headers:          288 (bytes into file)

  Flags:                             0x0

  Size of this header:               64 (bytes)

  Size of program headers:           0 (bytes)

  Number of program headers:         0

  Size of section headers:           64 (bytes)

  Number of section headers:         13

  Section header string table index: 10

Copyright © Linux教程網 All Rights Reserved