检查打开的文件是不是ELF格式的文件,如果是就输出节头表表項的数目,各表項指向的节的地址

#include "elf.h"
#include <stdio.h>
int main(int argc, char  *argv[])
{
    FILE  *fp;
    int i = 0;
    int fd = 0;
    Elf64_Ehdr  ehdr;
    
    if ((fp=fopen(argv[1],"r"))==NULL)
    {
        perror("open");
        return -1;    
    }
    else
    {
        printf("%s\n",argv[1] );
        fd=fread(&ehdr,1,sizeof(Elf64_Ehdr),fp);
        if (fd==sizeof(Elf64_Ehdr))
        {
                     if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
               ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
                        ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
                  ehdr.e_ident[EI_MAG3] != ELFMAG3) {
            perror("the file is not a valid ELF file");
            return -1;
        }
        printf("section header table中表项的数目%ld\n",(long int)ehdr.e_shnum);//节表头数量
    
        }
        
        Elf64_Shdr shdr[ehdr.e_shnum];
        fseek(fp,ehdr.e_shoff,0);
        for(i=0;i<ehdr.e_shnum;i++)
        {
                fread(&shdr[i],ehdr.e_shentsize,1,fp);
                printf("表项(节头)指向的节的地址 %ld\n",(long int)shdr[i].sh_offset);
        }
        fclose(fp);
               
    }
        return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值