// 写一段对pe文件分析的程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <elf.h>
#include <errno.h>
#include <assert.h>
#include <stdbool.h>
int main(int argc, char *argv[])
{
int fd;
struct stat st;
void *ptr;
Elf32_Ehdr *ehdr;
Elf32_Shdr *shdr;
Elf32_Sym *sym;
int i;
int j;
char *strtab;
char *shstrtab;
char *name;
int num;
int n;
int num2;
int n2;
if (argc < 2)
{
printf("Usage: %s <file>\n", argv[0]);
exit(1);
}
fd = open(argv[1], O_RDONLY);
if (fd < 0)
{
perror("open");
exit(1);
}
if (fstat(fd, &st) < 0)
{
perror("fstat");
exit(1);
}
ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (ptr == MAP_FAILED)
{
perror("mmap");
exit(1);
close(fd);
return 1;
}
close(fd);
ehdr = (Elf32_Ehdr *)ptr;
shdr = (Elf32_Shdr *)(ptr + ehdr->e_shoff);
strtab = (char *)ptr + shdr[ehdr->e_shstrndx].sh_offset;
shstrtab = (char *)ptr + shdr[ehdr->e_shstrndx].sh_offset;
for (i = 0; i < ehdr->e_shnum; i++)
{
name = strtab + shdr[i].sh_name;
printf("%s:\n", name);
if (shdr[i].sh_type == SHT_SYMTAB)
{
sym = (Elf32_Sym *)(ptr + shdr[i].sh_offset);
num = shdr[i].sh_size / sizeof(Elf32_Sym);
for (j = 0; j < num; j++)
{
if (ELF32_ST_TYPE(sym[j].st_info) == STT_FUNC)
{
n = sym[j].st_name;
printf(" %s\n", strtab + n);
num2 = shdr[i].sh_link;
shdr[num2].sh_offset;
shdr[num2].sh_size;
shdr[num2].sh_addr;
shdr[num2].sh_name;
shdr[num2].sh_type;
shdr[num2].sh_flags;
shdr[num2].sh_link;
shdr[num2].sh_info;
shdr[num2].sh_addralign;
shdr[num2].sh_entsize;
}
else
{
continue;
}
}
}
}
}
© 版权声明
本网站内容及资料来源于网络,并不代表本站观点和对其真实性负责,也不构成任何其他建议;部分内容是由网友自主投稿和发布、编辑整理上传,对此类内容本站仅提供交流平台,不为其版权负责;所提供的信息,只供参考之用,不保证信息的准确性、有效性、及时性和完整性;如果您发现网站上有侵犯您的知识产权的内容,请与我们取得联系,我们会及时修改或删除。文章版权归作者所有,原创作品未经允许请勿转载。投诉请联系:admin@chnhonker.com
THE END
暂无评论内容