您的位置:首页 > 其它

ELF格式解析库之基本数据类型

2014-07-28 08:54 274 查看

ELF格式简介

ELF是现代linux/unix流行的程序可执行链接格式,它有官方定义文档,具体的资料在ToolInterfaceStandardExecutableandLinkingFormatSpecificationversion1.2有其对应的详细描述。我在这里不会讲述关于这个格式详细描述,如果我叙述的有什么不对的地方,欢迎指正出来。当然一切都以官方的参考文档为标准,建议您在阅读本文的时候,手边最好有一份官方定义文档参考。

ELF由三个头表描述:ELFheader,sectionheader,programer(segment)header。

简单的说,ELF格式描述的是编译生成的二进制可链接文件,可共享库文件,以及可执行文件的数据组织形式,或者说内存组织形式。我们知道,程序可以分为从编译生成的静态内存分布和动态链接执行两个方面来理解,ELF格式也是基于此来构成两个基本视图的:sectionview和segmentview;前者描述了静态的视图,对应的详细描述定义在sectionheader里,后者则描述了动态链接执行的视图,对应的详细描述定义在segmentheader。可以说ELF确实是一个抽象良好的格式描述形式,因为这样做简化了格式描述的复杂性,也节省了在内核中管理和描述格式的内存开销。这也可以理解到为什么现代unix/linux流行逐渐放弃a.out格式和COEFF格式,而是采用ELF来作为二进制程序文件的描述的原因。

另外不同的计算机体系结构在字长,CPU架构,大小端等方面是有明显的差异。ELF格式的二进制文件为了在不同的体系结构中正确的被内核理解,所以它会在ELFheader中的16字节长的e_indent数组详细描述该二进制文件对应的体系结构,包括对应的字长,CPU架构,大小端等。我们知道系统的字长直接在系统位数反应出来;所以,为了兼容不同位数的系统,我们对于每一个视图的详细描述都定义了两种字长的结构。当然,在ELFheader还详细描述了其它的信息,比如程序的入口点e_entry,节表(sectionheader)的文件头偏移等等。

从通俗的角度来说,ELFheader是整个文件的总管家,对程序数据组织形式的理解都是从它开始的,通过它再转到其它对应信息的二管家,例如:segmentheader,sectionheader,然后层层深入,最后到达想要的信息地址。

在这里我只是给出我自己关于ELFheader/sectionheader/segmentheader的代码,不详细解释定义的结构中各个字段的意思,官方文档里边已经描述的很详细了,请自己去那里仔细的阅读,它可以很好的解释您的疑问。

ELFheader

typedefstruct

[code]{
unsignedchare_ident[EI_NIDENT];/*Magicnumberandotherinfo*/

Elf32_Halfe_type;/*Objectfiletype*/

Elf32_Halfe_machine;/*Architecture*/

Elf32_Worde_version;/*Objectfileversion*/

Elf32_Addre_entry;/*Entrypointvirtualaddress*/

Elf32_Offe_phoff;/*Programheadertablefileoffset*/

Elf32_Offe_shoff;/*Sectionheadertablefileoffset*/

Elf32_Worde_flags;/*Processor-specificflags*/

Elf32_Halfe_ehsize;/*ELFheadersizeinbytes*/

Elf32_Halfe_phentsize;/*Programheadertableentrysize*/

Elf32_Halfe_phnum;/*Programheadertableentrycount*/

Elf32_Halfe_shentsize;

Elf32_Halfe_shnum;

Elf32_Halfe_shstrndx;

}SEF_ELFHEADER_32;

typedefstruct

{

unsignedchare_ident[EI_NIDENT];/*Magicnumberandotherinfo*/

Elf64_Halfe_type;/*Objectfiletype*/

Elf64_Halfe_machine;/*Architecture*/

Elf64_Worde_version;/*Objectfileversion*/

Elf64_Addre_entry;/*Entrypointvirtualaddress*/

Elf64_Offe_phoff;/*Programheadertablefileoffset*/

Elf64_Offe_shoff;/*Sectionheadertablefileoffset*/

Elf64_Worde_flags;/*Processor-specificflags*/

Elf64_Halfe_ehsize;/*ELFheadersizeinbytes*/

Elf64_Halfe_phentsize;/*Programheadertableentrysize*/

Elf64_Halfe_phnum;/*Programheadertableentrycount*/

Elf64_Halfe_shentsize;/*Sectionheadertableentrysize*/

Elf64_Halfe_shnum;/*Sectionheadertableentrycount*/

Elf64_Halfe_shstrndx;/*Sectionheaderstringtableindex*/

}SEF_ELFHEADER_64;

[/code]

SECTIONheader


/*Sectionheader.*/

[code]
typedefstruct

{

Elf32_Wordsh_name;/*Sectionname(stringtblindex)*/

Elf32_Wordsh_type;/*Sectiontype*/

Elf32_Wordsh_flags;/*Sectionflags*/

Elf32_Addrsh_addr;/*Sectionvirtualaddratexecution*/

Elf32_Offsh_offset;/*Sectionfileoffset*/

Elf32_Wordsh_size;/*Sectionsizeinbytes*/

Elf32_Wordsh_link;/*Linktoanothersection*/

Elf32_Wordsh_info;/*Additionalsectioninformation*/

Elf32_Wordsh_addralign;/*Sectionalignment*/

Elf32_Wordsh_entsize;/*Entrysizeifsectionholdstable*/

}SEF_SECHEADER_32;

typedefstruct

{

Elf64_Wordsh_name;/*Sectionname(stringtblindex)*/

Elf64_Wordsh_type;/*Sectiontype*/

Elf64_Xwordsh_flags;/*Sectionflags*/

Elf64_Addrsh_addr;/*Sectionvirtualaddratexecution*/

Elf64_Offsh_offset;/*Sectionfileoffset*/

Elf64_Xwordsh_size;/*Sectionsizeinbytes*/

Elf64_Wordsh_link;/*Linktoanothersection*/

Elf64_Wordsh_info;/*Additionalsectioninformation*/

Elf64_Xwordsh_addralign;/*Sectionalignment*/

Elf64_Xwordsh_entsize;/*Entrysizeifsectionholdstable*/

}SEF_SECHEADER_64;

[/code]

SEGMENTheader


/*Programsegmentheader.*/

[code]
typedefstruct

{

Elf32_Wordp_type;/*Segmenttype*/

Elf32_Offp_offset;/*Segmentfileoffset*/

Elf32_Addrp_vaddr;/*Segmentvirtualaddress*/

Elf32_Addrp_paddr;/*Segmentphysicaladdress*/

Elf32_Wordp_filesz;/*Segmentsizeinfile*/

Elf32_Wordp_memsz;/*Segmentsizeinmemory*/

Elf32_Wordp_flags;/*Segmentflags*/

Elf32_Wordp_align;/*Segmentalignment*/

}SEF_PROHEADER_32;

typedefstruct

{

Elf64_Wordp_type;/*Segmenttype*/

Elf64_Wordp_flags;/*Segmentflags*/

Elf64_Offp_offset;/*Segmentfileoffset*/

Elf64_Addrp_vaddr;/*Segmentvirtualaddress*/

Elf64_Addrp_paddr;/*Segmentphysicaladdress*/

Elf64_Xwordp_filesz;/*Segmentsizeinfile*/

Elf64_Xwordp_memsz;/*Segmentsizeinmemory*/

Elf64_Xwordp_align;/*Segmentalignment*/

}SEF_PROHEADER_64;

[/code]

这里给出我的头文件:elf_type.h


#ifndef_ELF_TYPE_H

[code]#define_ELF_TYPE_H
#include<stdint.h>


#defineELFCLASSNONE(0)

#defineELFCLASS32(1)

#defineELFCLASS64(2)


#defineELFDATANONE(0)

#defineELFDATA2LSB(1)

#defineELFDATA2MSB(2)


#defineET_NONE(0)

#defineET_REL(1)

#defineET_EXEC(2)

#defineET_DYN(3)


#defineE_PHNUM(65535)

#defineNULL((void*)0)


/*Typefora16-bitquantity.*/

typedefuint16_tElf32_Half;

typedefuint16_tElf64_Half;


/*Typesforsignedandunsigned32-bitquantities.*/

typedefuint32_tElf32_Word;

typedefint32_tElf32_Sword;

typedefuint32_tElf64_Word;

typedefint32_tElf64_Sword;


/*Typesforsignedandunsigned64-bitquantities.*/

typedefuint64_tElf32_Xword;

typedefint64_tElf32_Sxword;

typedefuint64_tElf64_Xword;

typedefint64_tElf64_Sxword;


/*Typeofaddresses.*/

typedefuint32_tElf32_Addr;

typedefuint64_tElf64_Addr;


/*Typeoffileoffsets.*/

typedefuint32_tElf32_Off;

typedefuint64_tElf64_Off;


/*Typeforsectionindices,whichare16-bitquantities.*/

typedefuint16_tElf32_Section;

typedefuint16_tElf64_Section;


/*Typeforversionsymbolinformation.*/

typedefElf32_HalfElf32_Versym;

typedefElf64_HalfElf64_Versym;


//char*pBuffer;

/*TheELFfileheader.ThisappearsatthestartofeveryELFfile.*/


#defineEI_NIDENT(16)

typedefstruct

{

unsignedchare_ident[EI_NIDENT];/*Magicnumberandotherinfo*/

Elf32_Halfe_type;/*Objectfiletype*/

Elf32_Halfe_machine;/*Architecture*/

Elf32_Worde_version;/*Objectfileversion*/

Elf32_Addre_entry;/*Entrypointvirtualaddress*/

Elf32_Offe_phoff;/*Programheadertablefileoffset*/

Elf32_Offe_shoff;/*Sectionheadertablefileoffset*/

Elf32_Worde_flags;/*Processor-specificflags*/

Elf32_Halfe_ehsize;/*ELFheadersizeinbytes*/

Elf32_Halfe_phentsize;/*Programheadertableentrysize*/

Elf32_Halfe_phnum;/*Programheadertableentrycount*/

Elf32_Halfe_shentsize;

Elf32_Halfe_shnum;

Elf32_Halfe_shstrndx;

}SEF_ELFHEADER_32;

typedefstruct

{

unsignedchare_ident[EI_NIDENT];/*Magicnumberandotherinfo*/

Elf64_Halfe_type;/*Objectfiletype*/

Elf64_Halfe_machine;/*Architecture*/

Elf64_Worde_version;/*Objectfileversion*/

Elf64_Addre_entry;/*Entrypointvirtualaddress*/

Elf64_Offe_phoff;/*Programheadertablefileoffset*/

Elf64_Offe_shoff;/*Sectionheadertablefileoffset*/

Elf64_Worde_flags;/*Processor-specificflags*/

Elf64_Halfe_ehsize;/*ELFheadersizeinbytes*/

Elf64_Halfe_phentsize;/*Programheadertableentrysize*/

Elf64_Halfe_phnum;/*Programheadertableentrycount*/

Elf64_Halfe_shentsize;/*Sectionheadertableentrysize*/

Elf64_Halfe_shnum;/*Sectionheadertableentrycount*/

Elf64_Halfe_shstrndx;/*Sectionheaderstringtableindex*/

}SEF_ELFHEADER_64;




/*Programsegmentheader.*/

typedefstruct

{

Elf32_Wordp_type;/*Segmenttype*/

Elf32_Offp_offset;/*Segmentfileoffset*/

Elf32_Addrp_vaddr;/*Segmentvirtualaddress*/

Elf32_Addrp_paddr;/*Segmentphysicaladdress*/

Elf32_Wordp_filesz;/*Segmentsizeinfile*/

Elf32_Wordp_memsz;/*Segmentsizeinmemory*/

Elf32_Wordp_flags;/*Segmentflags*/

Elf32_Wordp_align;/*Segmentalignment*/

}SEF_PROHEADER_32;

typedefstruct

{

Elf64_Wordp_type;/*Segmenttype*/

Elf64_Wordp_flags;/*Segmentflags*/

Elf64_Offp_offset;/*Segmentfileoffset*/

Elf64_Addrp_vaddr;/*Segmentvirtualaddress*/

Elf64_Addrp_paddr;/*Segmentphysicaladdress*/

Elf64_Xwordp_filesz;/*Segmentsizeinfile*/

Elf64_Xwordp_memsz;/*Segmentsizeinmemory*/

Elf64_Xwordp_align;/*Segmentalignment*/

}SEF_PROHEADER_64;


/*Sectionheader.*/

typedefstruct

{

Elf32_Wordsh_name;/*Sectionname(stringtblindex)*/

Elf32_Wordsh_type;/*Sectiontype*/

Elf32_Wordsh_flags;/*Sectionflags*/

Elf32_Addrsh_addr;/*Sectionvirtualaddratexecution*/

Elf32_Offsh_offset;/*Sectionfileoffset*/

Elf32_Wordsh_size;/*Sectionsizeinbytes*/

Elf32_Wordsh_link;/*Linktoanothersection*/

Elf32_Wordsh_info;/*Additionalsectioninformation*/

Elf32_Wordsh_addralign;/*Sectionalignment*/

Elf32_Wordsh_entsize;/*Entrysizeifsectionholdstable*/

}SEF_SECHEADER_32;

typedefstruct

{

Elf64_Wordsh_name;/*Sectionname(stringtblindex)*/

Elf64_Wordsh_type;/*Sectiontype*/

Elf64_Xwordsh_flags;/*Sectionflags*/

Elf64_Addrsh_addr;/*Sectionvirtualaddratexecution*/

Elf64_Offsh_offset;/*Sectionfileoffset*/

Elf64_Xwordsh_size;/*Sectionsizeinbytes*/

Elf64_Wordsh_link;/*Linktoanothersection*/

Elf64_Wordsh_info;/*Additionalsectioninformation*/

Elf64_Xwordsh_addralign;/*Sectionalignment*/

Elf64_Xwordsh_entsize;/*Entrysizeifsectionholdstable*/

}SEF_SECHEADER_64;


#endif

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: