ELF Section Header的结构还是要从/usr/include/elf.h中看。

typedef struct
{
  Elf32_Word sh_name; /* Section name (string tbl index) */
  Elf32_Word sh_type; /* Section type */
  Elf32_Word sh_flags; /* Section flags */
  Elf32_Addr sh_addr; /* Section virtual addr at execution */
  Elf32_Off sh_offset; /* Section file offset */
  Elf32_Word sh_size; /* Section size in bytes */
  Elf32_Word sh_link; /* Link to another section */
  Elf32_Word sh_info; /* Additional section information */
  Elf32_Word sh_addralign; /* Section alignment */
  Elf32_Word sh_entsize; /* Entry size if section holds table */
} Elf32_Shdr;
同样,可以算出来Elf32_Shdr的大小是40B。其结构中的各个元素数据类型在elf.h中同样可以找到,含义见注释。整个section header table就是一个数组,数组的每一个元素就是Elf32_Shdr。Elf32_Shdr仅仅是section的一个索引,包括一些属性信息,并不是section本省。简单的介绍一下各个元素的含义。
sh_name: section name。不过其实sh_name中存放的是index,不是字符串,这一点从sh_name是定长应该能看出来,那么index又是什么意思呢?index的含义是在string tabl总的第几个字节数。其实是这样的,所有的section name都存放在一个叫做string table的表中,index就是该section名字的第一个字符在表中的位置,名字一直到遇到一个'\0'为止结束。至于string table怎么找,还记得上一篇中提到的elf header中的e_shstrndx成员吗,它就指明了string table是在section header table中的第几个入口。
sh_offset:这个元素就指明了这个Elf32_Shdr描述的section在文件中的偏移量。
其他各个变量可以查询《Executable and Linkable Format(ELF)》这个文档。
为了检验程序输出结果的对错,我们与readelf这个命令的输出结果作比较。
下面编程序来输出文件的section信息。首先要找到section header table在文件中的位置,回忆一下,section header table的偏移量在elf header中的e_shoff中告诉了我们,所以,我们就知道了section header table在文件中的位置了,同样,我们在elf header中可以找到section header的大小及个数,这样我们用一个循环就可以输出出来每个section的信息了。

fseek(fp, elfheader.e_shoff, SEEK_SET);
    shnum = elfheader.e_shnum;
    printf("Number of Section headers: %d\n\n", shnum);
    while(shnum != 0){
        fread(&shdr, sizeof(char), sizeof(shdr), fp);
        printf("sh_name : %d\n", shdr.sh_name);
        printf("sh_type : %#x\n", shdr.sh_type);
        printf("sh_flags : %d\n", shdr.sh_flags);
        printf("sh_addr : %#x\n", shdr.sh_addr);
        printf("sh_offset : %d\n", shdr.sh_offset);
        printf("sh_size : %d\n", shdr.sh_size);
        printf("sh_link : %d\n", shdr.sh_link);
        printf("sh_info : %d\n", shdr.sh_info);
        printf("sh_addralign : %d\n", shdr.sh_addralign);
        printf("sh_entsize : %d\n\n", shdr.sh_entsize);
        shnum--;
    }
输出的结果如下:
Number of Section headers: 34
sh_name : 0
sh_type : 0
sh_flags : 0
sh_addr : 0
sh_offset : 0
sh_size : 0
sh_link : 0
sh_info : 0
sh_addralign : 0
sh_entsize : 0
sh_name : 27
sh_type : 0x1
sh_flags : 2
sh_addr : 0x80480f4
sh_offset : 244
sh_size : 19
sh_link : 0
sh_info : 0
sh_addralign : 1
sh_entsize : 0
sh_name : 35
sh_type : 0x7
sh_flags : 2
sh_addr : 0x8048108
sh_offset : 264
sh_size : 32
sh_link : 0
sh_info : 0
sh_addralign : 4
sh_entsize : 0
...

观察上面的结果可以看到,sh_name并不是section的名字,而是一个数字。回忆前面提到的,sh_name存放的只是section name在string table的index。因此,为了输出section name我们必须先得到string table。string table的信息也在section header table中存放,只要我们得到了string table header的内容,就可以从里面的sh_offset知道string table的位置,从sh_size知道string table的大小。此时问题就成为如何找到string talbe header在section header table中的位置,即index.回忆前面讲到的,elf header中有一个变量e_shstrndx存放的就是string table header在section header中的index。此时整个思路已经明白了,即elfheader.e_shstrndx --> string table header --> string table。string table里面到底是什么呢?其实就是一些带'\0'的字符串。比如\0.bss\0.text\0\0,那么如果sh_name是1,则该sh_name代表的是从string table中零开始的直到遇到\0为止的字符串,即.bss。整个程序如下:
    fseek(fp, elfheader.e_shoff + elfheader.e_shstrndx*elfheader.e_shentsize, SEEK_SET);
    fread(&strhdr, sizeof(char), sizeof(strhdr), fp);//get the string table header
    fseek(fp, strhdr.sh_offset, SEEK_SET);
    strtable = (unsigned char *)malloc(sizeof(unsigned char)*strhdr.sh_size);
    fread(strtable, sizeof(char), strhdr.sh_size, fp);
    fseek(fp, elfheader.e_shoff, SEEK_SET);
    shnum = elfheader.e_shnum;
    printf("Number of Section headers: %d\n\n", shnum);
    while(shnum != 0){
        fread(&shdr, sizeof(char), sizeof(shdr), fp);
        printf("sh_name : %s\n", strtable+shdr.sh_name);
        printf("sh_type : %#x\n", shdr.sh_type);
        printf("sh_flags : %d\n", shdr.sh_flags);
        printf("sh_addr : %#x\n", shdr.sh_addr);
        printf("sh_offset : %d\n", shdr.sh_offset);
        printf("sh_size : %d\n", shdr.sh_size);
        printf("sh_link : %d\n", shdr.sh_link);
        printf("sh_info : %d\n", shdr.sh_info);
        printf("sh_addralign : %d\n", shdr.sh_addralign);
        printf("sh_entsize : %d\n\n", shdr.sh_entsize);
        shnum--;
    }
程序的输出结果为:
Number of Section headers: 34
sh_name :
sh_type : 0
sh_flags : 0
sh_addr : 0
sh_offset : 0
sh_size : 0
sh_link : 0
sh_info : 0
sh_addralign : 0
sh_entsize : 0
sh_name : .interp
sh_type : 0x1
sh_flags : 2
sh_addr : 0x80480f4
sh_offset : 244
sh_size : 19
sh_link : 0
sh_info : 0
sh_addralign : 1
sh_entsize : 0
sh_name : .note.ABI-tag
sh_type : 0x7
sh_flags : 2
sh_addr : 0x8048108
sh_offset : 264
sh_size : 32
sh_link : 0
sh_info : 0
sh_addralign : 4
sh_entsize : 0
sh_name : .hash
sh_type : 0x5
sh_flags : 2
sh_addr : 0x8048128
sh_offset : 296
sh_size : 64
sh_link : 4
sh_info : 0
sh_addralign : 4
sh_entsize : 4

ELF Section Header 分析相关推荐

  1. Linux ELF 详解2 -- Section Header Section

    上一篇:ELF 详解1 – ELF Header ELF Section Header & Section 先看 Section Header 的定义 typedef struct {Elf3 ...

  2. elf section类型_ELF结构(主要是符号表)

    1.1.1   整体结构 ELF对象格式用于目标文件(.o扩展名)和执行文件. 有些信息只出现在目标文件或执行文件中. ELF文件由下列部件构成. ELF header必须放在文件的开始;其他部件可以 ...

  3. 程序的本质之二ELF文件的文件头、section header和program header

    操作系统:CentOS Linux release 7.7.1908 内核版本:3.10.0-1062.1.1.el7.x86_64 运行平台:x86_64 参考文献:http://refspecs. ...

  4. elf section类型_ELF文件格式分析(示例代码)

    要求: 1.分析文件头. 2.通过文件头找到section header table,理解其内容. 3.通过section header table找到各section. 4.理解常见的.text . ...

  5. 一步步编写操作系统 47 elf格式文件分析实验

    在上一节中,我们讲述了elf格式的部分理论知识,为什么是部分呢?因为我们本着"够用"的原则,只把我们需要了解的部分说完啦.不过,我相信大部分同学仅仅凭上一节中的理论知识还是领悟不到 ...

  6. elf section类型_ELF文件解析(一):Segment和Section

    ELF 是Executable and Linking Format的缩写,即可执行和可链接的格式,是Unix/Linux系统ABI (Application Binary Interface)规范的 ...

  7. elf section类型_ELF文件格式解析

    ELF文件格式解析 ELF(Executable and Linking Format) 1) 可重定位的对象文件(Relocatable file) 2) 可执行的对象文件(Executable f ...

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

    ELF格式简介 ELF是现代linux/unix流行的程序可执行链接格式,它有官方定义文档,具体的资料在Tool Interface Standard Executable and Linking F ...

  9. ART世界探险(12) - OAT文件分析(2) - ELF文件头分析(中)

    ART世界探险(12) - OAT文件分析(2) - ELF文件头分析(中) 段(section)的概念 一块内存分配给应用程序之后,从代码的组织上,我们就有将它们分段的需求. 比如,可以分为代码段, ...

最新文章

  1. Oracle中PL/SQL的循环语句
  2. 提升篇——SELINUX相关介绍
  3. 整合初步---------SSH(注解版)
  4. java sql server连接字符串_关于Java:SQL Server的等效jdbc连接字符串
  5. CodeForces 931C Laboratory Work 水题,构造
  6. json.hpp参数错误
  7. Hbase 二级索引 Solr int字段排序问题 can not sort on multivalued field
  8. 95-150-544-源码-Sink-Flink BucketingSink 源码分析
  9. 语音识别之——mfcc什么是汉明窗,为什么加汉明窗
  10. 数据挖掘 应用案例集
  11. [Swift]集成京东联盟SDK
  12. matlab和cuda版本对应适配关系
  13. 手机电子书分享:大前研一经典《专业主义》
  14. 第十二周练兵区——编程题——不计入总分
  15. 中央电大 c语言程序设计a 试题,中央电大开放本科计算机科学与技术专业C语言程序设计(A)试题_1007...
  16. 2020TI省级大学生电子竞赛推荐芯片简介
  17. 图像识别python模块_人工智能之Python人脸识别技术--face_recognition模块
  18. 黄峥为拼多多拼了五年多
  19. java中怎么定义true或false_java 中的true、false、null有何不同
  20. access 江苏计算机二级_计算机等级考试该选哪一科目?

热门文章

  1. Matlab抽样和,《统计学原理》与MATLAB编程-第三章 抽样和抽样分布
  2. 单片机生成二维码 C语言代码
  3. 跨境电商erp软件的选择要看这几点
  4. Excel学习笔记-函数(基础数学函数、数组、Indirect)
  5. 基于Psoc4 capsense的液位传感器的使用
  6. notify、notifyAll、wait思考
  7. 对我影响最大的4句名言
  8. Java游戏开发《超级玛丽》附课件+源码+资料
  9. cockroach官方文档翻译---1.3 学习cockroachSQL
  10. 转载之码率(Bitrate)、帧率(FPS)、分辨率和清晰度的联系与区别