日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

elf section类型_ELF格式解析库之基本数据类型

發布時間:2023/12/14 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 elf section类型_ELF格式解析库之基本数据类型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ELF格式簡介

ELF是現代linux/unix流行的程序可執行鏈接格式,它有官方定義文檔,具體的資料在Tool Interface Standard Executable and Linking Format Specification version 1.2有其對應的詳細描述。我在這里不會講述關于這個格式詳細描述,如果我敘述的有什么不對的地方,歡迎指正出來。當然一切都以官方的參考文檔為標準,建議您在閱讀本文的時候,手邊最好有一份官方定義文檔參考。

ELF由三個頭表描述:ELF header,section header,programer (segment) header。

簡單的說,ELF格式描述的是編譯生成的二進制可鏈接文件,可共享庫文件,以及可執行文件的數據組織形式,或者說內存組織形式。我們知道,程序可以分為從編譯生成的靜態內存分布和動態鏈接執行兩個方面來理解,ELF格式也是基于此來構成兩個基本視圖的:section view和 segment view;前者描述了靜態的視圖,對應的詳細描述定義在section header里,后者則描述了動態鏈接執行的視圖,對應的詳細描述定義在segment header。可以說ELF確實是一個抽象良好的格式描述形式,因為這樣做簡化了格式描述的復雜性,也節省了在內核中管理和描述格式的內存開銷。這也可以理解到為什么現代unix/linux流行逐漸放棄a.out格式和COEFF格式,而是采用ELF來作為二進制程序文件的描述的原因。

另外不同的計算機體系結構在字長,CPU架構,大小端等方面是有明顯的差異。ELF格式的二進制文件為了在不同的體系結構中正確的被內核理解,所以它會在ELF header 中的16字節長的e_indent數組詳細描述該二進制文件對應的體系結構,包括對應的字長,CPU架構,大小端等。我們知道系統的字長直接在系統位數反應出來;所以,為了兼容不同位數的系統,我們對于每一個視圖的詳細描述都定義了兩種字長的結構。當然,在ELF header 還詳細描述了其它的信息,比如程序的入口點e_entry, 節表(section header)的文件頭偏移等等。

從通俗的角度來說,ELF header 是整個文件的總管家,對程序數據組織形式的理解都是從它開始的,通過它再轉到其它對應信息的二管家,例如:segment header,section header,然后層層深入,最后到達想要的信息地址。

在這里我只是給出我自己關于ELF header/section header/segment header 的代碼,不詳細解釋定義的結構中各個字段的意思,官方文檔里邊已經描述的很詳細了,請自己去那里仔細的閱讀,它可以很好的解釋您的疑問。

ELF header

1:typedef struct

2:{

3: unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */

4: Elf32_Half e_type; /* Object file type */

5: Elf32_Half e_machine; /* Architecture */

6: Elf32_Word e_version; /* Object file version */

7: Elf32_Addr e_entry; /* Entry point virtual address */

8: Elf32_Off e_phoff; /* Program header table file offset */

9: Elf32_Off e_shoff; /* Section header table file offset */

10: Elf32_Word e_flags; /* Processor-specific flags */

11: Elf32_Half e_ehsize; /* ELF header size in bytes */

12: Elf32_Half e_phentsize; /* Program header table entry size */

13: Elf32_Half e_phnum; /* Program header table entry count */

14: Elf32_Half e_shentsize;

15: Elf32_Half e_shnum;

16: Elf32_Half e_shstrndx;

17:} SEF_ELFHEADER_32;

18:

19:typedef struct

20:{

21: unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */

22: Elf64_Half e_type; /* Object file type */

23: Elf64_Half e_machine; /* Architecture */

24: Elf64_Word e_version; /* Object file version */

25: Elf64_Addr e_entry; /* Entry point virtual address */

26: Elf64_Off e_phoff; /* Program header table file offset */

27: Elf64_Off e_shoff; /* Section header table file offset */

28: Elf64_Word e_flags; /* Processor-specific flags */

29: Elf64_Half e_ehsize; /* ELF header size in bytes */

30: Elf64_Half e_phentsize; /* Program header table entry size */

31: Elf64_Half e_phnum; /* Program header table entry count */

32: Elf64_Half e_shentsize; /* Section header table entry size */

33: Elf64_Half e_shnum; /* Section header table entry count */

34: Elf64_Half e_shstrndx; /* Section header string table index */

35:}SEF_ELFHEADER_64;

SECTION header

1:/* Section header. */

2:

3:typedef struct

4:{

5: Elf32_Word sh_name; /* Section name (string tbl index) */

6: Elf32_Word sh_type; /* Section type */

7: Elf32_Word sh_flags; /* Section flags */

8: Elf32_Addr sh_addr; /* Section virtual addr at execution */

9: Elf32_Off sh_offset; /* Section file offset */

10: Elf32_Word sh_size; /* Section size in bytes */

11: Elf32_Word sh_link; /* Link to another section */

12: Elf32_Word sh_info; /* Additional section information */

13: Elf32_Word sh_addralign; /* Section alignment */

14: Elf32_Word sh_entsize; /* Entry size if section holds table */

15:} SEF_SECHEADER_32;

16:

17:typedef struct

18:{

19: Elf64_Word sh_name; /* Section name (string tbl index) */

20: Elf64_Word sh_type; /* Section type */

21: Elf64_Xword sh_flags; /* Section flags */

22: Elf64_Addr sh_addr; /* Section virtual addr at execution */

23: Elf64_Off sh_offset; /* Section file offset */

24: Elf64_Xword sh_size; /* Section size in bytes */

25: Elf64_Word sh_link; /* Link to another section */

26: Elf64_Word sh_info; /* Additional section information */

27: Elf64_Xword sh_addralign; /* Section alignment */

28: Elf64_Xword sh_entsize; /* Entry size if section holds table */

29:} SEF_SECHEADER_64;

SEGMENT header

1:/* Program segment header. */

2:

3:typedef struct

4:{

5: Elf32_Word p_type; /* Segment type */

6: Elf32_Off p_offset; /* Segment file offset */

7: Elf32_Addr p_vaddr; /* Segment virtual address */

8: Elf32_Addr p_paddr; /* Segment physical address */

9: Elf32_Word p_filesz; /* Segment size in file */

10: Elf32_Word p_memsz; /* Segment size in memory */

11: Elf32_Word p_flags; /* Segment flags */

12: Elf32_Word p_align; /* Segment alignment */

13:} SEF_PROHEADER_32;

14:

15:typedef struct

16:{

17: Elf64_Word p_type; /* Segment type */

18: Elf64_Word p_flags; /* Segment flags */

19: Elf64_Off p_offset; /* Segment file offset */

20: Elf64_Addr p_vaddr; /* Segment virtual address */

21: Elf64_Addr p_paddr; /* Segment physical address */

22: Elf64_Xword p_filesz; /* Segment size in file */

23: Elf64_Xword p_memsz; /* Segment size in memory */

24: Elf64_Xword p_align; /* Segment alignment */

25:} SEF_PROHEADER_64;

這里給出我的頭文件:elf_type.h

1:#ifndef _ELF_TYPE_H

2:#define _ELF_TYPE_H

3:#include

4:

5:#define ELFCLASSNONE (0)

6:#define ELFCLASS32 (1)

7:#define ELFCLASS64 (2)

8:

9:#define ELFDATANONE (0)

10:#define ELFDATA2LSB (1)

11:#define ELFDATA2MSB (2)

12:

13:#define ET_NONE (0)

14:#define ET_REL (1)

15:#define ET_EXEC (2)

16:#define ET_DYN (3)

17:

18:#define E_PHNUM (65535)

19:#define NULL ((void *)0)

20:

21:/* Type for a 16-bit quantity. */

22:typedef uint16_t Elf32_Half;

23:typedef uint16_t Elf64_Half;

24:

25:/* Types for signed and unsigned 32-bit quantities. */

26:typedef uint32_t Elf32_Word;

27:typedef int32_t Elf32_Sword;

28:typedef uint32_t Elf64_Word;

29:typedef int32_t Elf64_Sword;

30:

31:/* Types for signed and unsigned 64-bit quantities. */

32:typedef uint64_t Elf32_Xword;

33:typedef int64_t Elf32_Sxword;

34:typedef uint64_t Elf64_Xword;

35:typedef int64_t Elf64_Sxword;

36:

37:/* Type of addresses. */

38:typedef uint32_t Elf32_Addr;

39:typedef uint64_t Elf64_Addr;

40:

41:/* Type of file offsets. */

42:typedef uint32_t Elf32_Off;

43:typedef uint64_t Elf64_Off;

44:

45:/* Type for section indices, which are 16-bit quantities. */

46:typedef uint16_t Elf32_Section;

47:typedef uint16_t Elf64_Section;

48:

49:/* Type for version symbol information. */

50:typedef Elf32_Half Elf32_Versym;

51:typedef Elf64_Half Elf64_Versym;

52:

53://char *pBuffer;

54:/* The ELF file header. This appears at the start of every ELF file. */

55:

56:#define EI_NIDENT (16)

57:

58:typedef struct

59:{

60: unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */

61: Elf32_Half e_type; /* Object file type */

62: Elf32_Half e_machine; /* Architecture */

63: Elf32_Word e_version; /* Object file version */

64: Elf32_Addr e_entry; /* Entry point virtual address */

65: Elf32_Off e_phoff; /* Program header table file offset */

66: Elf32_Off e_shoff; /* Section header table file offset */

67: Elf32_Word e_flags; /* Processor-specific flags */

68: Elf32_Half e_ehsize; /* ELF header size in bytes */

69: Elf32_Half e_phentsize; /* Program header table entry size */

70: Elf32_Half e_phnum; /* Program header table entry count */

71: Elf32_Half e_shentsize;

72: Elf32_Half e_shnum;

73: Elf32_Half e_shstrndx;

74:} SEF_ELFHEADER_32;

75:

76:typedef struct

77:{

78: unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */

79: Elf64_Half e_type; /* Object file type */

80: Elf64_Half e_machine; /* Architecture */

81: Elf64_Word e_version; /* Object file version */

82: Elf64_Addr e_entry; /* Entry point virtual address */

83: Elf64_Off e_phoff; /* Program header table file offset */

84: Elf64_Off e_shoff; /* Section header table file offset */

85: Elf64_Word e_flags; /* Processor-specific flags */

86: Elf64_Half e_ehsize; /* ELF header size in bytes */

87: Elf64_Half e_phentsize; /* Program header table entry size */

88: Elf64_Half e_phnum; /* Program header table entry count */

89: Elf64_Half e_shentsize; /* Section header table entry size */

90: Elf64_Half e_shnum; /* Section header table entry count */

91: Elf64_Half e_shstrndx; /* Section header string table index */

92:}SEF_ELFHEADER_64;

93:

94:

95:

96:/* Program segment header. */

97:

98:typedef struct

99:{

100: Elf32_Word p_type; /* Segment type */

101: Elf32_Off p_offset; /* Segment file offset */

102: Elf32_Addr p_vaddr; /* Segment virtual address */

103: Elf32_Addr p_paddr; /* Segment physical address */

104: Elf32_Word p_filesz; /* Segment size in file */

105: Elf32_Word p_memsz; /* Segment size in memory */

106: Elf32_Word p_flags; /* Segment flags */

107: Elf32_Word p_align; /* Segment alignment */

108:} SEF_PROHEADER_32;

109:

110:typedef struct

111:{

112: Elf64_Word p_type; /* Segment type */

113: Elf64_Word p_flags; /* Segment flags */

114: Elf64_Off p_offset; /* Segment file offset */

115: Elf64_Addr p_vaddr; /* Segment virtual address */

116: Elf64_Addr p_paddr; /* Segment physical address */

117: Elf64_Xword p_filesz; /* Segment size in file */

118: Elf64_Xword p_memsz; /* Segment size in memory */

119: Elf64_Xword p_align; /* Segment alignment */

120:} SEF_PROHEADER_64;

121:

122:/* Section header. */

123:

124:typedef struct

125:{

126: Elf32_Word sh_name; /* Section name (string tbl index) */

127: Elf32_Word sh_type; /* Section type */

128: Elf32_Word sh_flags; /* Section flags */

129: Elf32_Addr sh_addr; /* Section virtual addr at execution */

130: Elf32_Off sh_offset; /* Section file offset */

131: Elf32_Word sh_size; /* Section size in bytes */

132: Elf32_Word sh_link; /* Link to another section */

133: Elf32_Word sh_info; /* Additional section information */

134: Elf32_Word sh_addralign; /* Section alignment */

135: Elf32_Word sh_entsize; /* Entry size if section holds table */

136:} SEF_SECHEADER_32;

137:

138:typedef struct

139:{

140: Elf64_Word sh_name; /* Section name (string tbl index) */

141: Elf64_Word sh_type; /* Section type */

142: Elf64_Xword sh_flags; /* Section flags */

143: Elf64_Addr sh_addr; /* Section virtual addr at execution */

144: Elf64_Off sh_offset; /* Section file offset */

145: Elf64_Xword sh_size; /* Section size in bytes */

146: Elf64_Word sh_link; /* Link to another section */

147: Elf64_Word sh_info; /* Additional section information */

148: Elf64_Xword sh_addralign; /* Section alignment */

149: Elf64_Xword sh_entsize; /* Entry size if section holds table */

150:} SEF_SECHEADER_64;

151:

152:#endif

總結

以上是生活随笔為你收集整理的elf section类型_ELF格式解析库之基本数据类型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。