linux中四类文件使用ELF文件格式
https://man7.org/linux/man-pages/man5/elf.5.html
1999年86open项目选择ELF作为x86处理器上Unix和类Unix系统的标准二进制文件格式。使用ELF的原因包括:灵活性、可扩展性、对不同字节序格式支持、跨平台支持地址size。
例如这些扩展名的文件一般都是elf格式:.axf, .bin, .elf, .o, .prx, .puff, .ko, .so, and .mod
引用wiki的一张图: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
touch头结构:
# hexdump -C /usr/bin/touch | head -5
00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 02 00 3e 00 01 00 00 00 1d 26 40 00 00 00 00 00 |..>......&@.....|
00000020 40 00 00 00 00 00 00 00 90 ec 00 00 00 00 00 00 |@...............|
00000030 00 00 00 00 40 00 38 00 09 00 40 00 1e 00 1d 00 |....@.8...@.....|
00000040 06 00 00 00 05 00 00 00 40 00 00 00 00 00 00 00 |........@.......|
https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
e_ident的16个字节标识是个ELF文件(7F+’E’+’L’+’F’)。 e_type表示文件类型,2表示可执行文件。 e_machine说明机器类别,3表示386机器,8表示MIPS机器。 e_entry给出进程开始的虚地址,即系统将控制转移的位置。 e_phoff指出program header table的文件偏移。 e_phentsize表示一个program header表中的入口的长度(字节数表示)。 e_phnum给出program header表中的入口数目。类似的。 e_shoff,e_shentsize,e_shnum 分别表示section header表的文件偏移,表中每个入口的的字节数和入口数目。 e_flags给出与处理器相关的标志。 e_ehsize给出ELF文件头的长度(字节数表示)。 e_shstrndx表示section名表的位置,指出在section header表中的索引。
目标文件或者共享文件的program header table描述了系统执行一个程序所需要的段或者其它信息。目标文件的一个段(segment)包含一个或者多个section。Program header只对可执行文件和共享目标文件有意义,对于程序的链接没有任何意义。结构定义如下,可在/usr/include/elf.h中可以找到文件头结构定义:
其中p_type描述段的类型; p_offset给出该段相对于文件开关的偏移量; p_vaddr给出该段所在的虚拟地址; p_paddr给出该段的物理地址; p_filesz给出该段的大小,在字节为单元,可能为0; p_memsz给出该段在内存中所占的大小,可能为0; p_filesze与p_memsz的值可能会不相等。
目标文件的section header table可以定位所有的section,它是一个Elf64_Shdr结构的数组,Section头表的索引是这个数组的下标。有些索引号是保留的,目标文件不能使用这些特殊的索引。
Section包含目标文件除了ELF文件头、程序头表、section头表的所有信息,而且目标文件section满足几个条件:
sh_name指出section的名字,它的值是后面将会讲到的section header string table中的偏移,指出一个以null结尾的字符串。 sh_type是类别。 sh_flags指示该section在进程执行时的特性。 sh_addr指出若此section在进程的内存映像中出现,则给出开始的虚地址。 sh_offset给出此section在文件中的偏移。其它字段的意义不太常用,在此不细述。
文件的section含有程序和控制信息,系统使用一些特定的section,并有其固定的类型和属性(由sh_type和sh_info指出)。
下面介绍几个常用到的section:
Main article: GNU Binutils
readelf
is a Unix binary utility that displays information about one or more ELF files. A free software implementation is provided by GNU Binutils.elfutils
provides alternative tools to GNU Binutils purely for Linux.[10]elfdump
is a command for viewing ELF information in an ELF file, available under Solaris and FreeBSD.objdump
provides a wide range of information about ELF files and other object formats. objdump
uses the Binary File Descriptor library as a back-end to structure the ELF data.file
utility can display some information about ELF files, including the instruction set architecture for which the code in a relocatable, executable, or shared object file is intended, or on which an ELF core dump was produced.目标文件的符号表包含定位或重定位程序符号定义和引用时所需要的信息。符号表入口结构定义如下,可在/usr/include/elf.h中可以找到文件头结构定义:
typedef struct elf64_sym {
Elf64_Word st_name; /* Symbol name, index in string tbl */
unsigned char st_info; /* Type and binding attributes */
unsigned char st_other; /* No defined meaning, 0 */
Elf64_Half st_shndx; /* Associated section index */
Elf64_Addr st_value; /* Value of the symbol */
Elf64_Xword st_size; /* Associated symbol size */
} Elf64_Sym;
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有