从 header 读取 sh_name 每次都会 return 一个值
Reading sh_name from header will return one value everytime
每当调用 isstatic32 时,即使程序是动态编译的,它也会 return STATIC。我不知道该怎么做。每次它从 sh_name 检测到 .dynamic 时,我都尝试过,它会向变量添加 1,如果变量 > 1,它将 return 动态,但这没有用。 (不会让我 post 在这里编码)
#include <stdio.h>
#include <elf.h>
#define DYNAMIC 1
#define STATIC 2
static int isstatic32(FILE* fd, Elf32_Ehdr eh, Elf32_Shdr sh_table[])
{
static int i;
static int kek = 0;
static char* sh_str;
static char* buff;
buff = malloc(sh_table[eh.e_shstrndx].sh_size);
if(buff != NULL)
{
fseek(fd, sh_table[eh.e_shstrndx].sh_offset, SEEK_SET);
fread(buff, 1, sh_table[eh.e_shstrndx].sh_size, fd);
}
sh_str = buff;
for(i=0; i<eh.e_shnum; i++)
{
printf("%d", i);
if(!strcmp(".dynamic", (sh_str + sh_table[i].sh_name)))
{
return DYNAMIC;
}
}
return STATIC;
}
int main()
{
FILE *fp = NULL;
char* f;
f = "/proc/self/exe";
Elf32_Ehdr elf_header;
Elf32_Shdr* sh_table;
fp = fopen(f, "r");
fseek(fp, 0, SEEK_SET);
fread(&elf_header, 1, sizeof(Elf32_Ehdr), fp);
sh_table = malloc(elf_header.e_shentsize*elf_header.e_shnum);
fseek(fp, elf_header.e_shoff, SEEK_SET);
fread(sh_table, 1, elf_header.e_shentsize*elf_header.e_shnum, fp);
if(isstatic32(fp, elf_header, sh_table) == STATIC)
{
printf("statically linked");
}
else
{
printf("dynamic");
}
fp = NULL;
f = NULL;
}
the program is dynamically compiled
程序不能动态编译(或者更确切地说,"dynamically compiled"意味着完全不同的东西)。你的意思是程序动态链接.
大胆猜测:您在 64 位模式下编译了代码(因为您在默认为 64 位模式的 64 位系统上)。
这个:
Elf32_Ehdr elf_header;
如果您试图确定 64 位可执行文件是否静态链接,是错误的类型。
P.S。您的程序中还有许多其他错误。
P.P.S。 .dynamic
的存在与否 不是 确定二进制文件是静态链接还是动态链接的正确方法(部分不是必需的,可以完全剥离)。您应该寻找 PT_DYNAMIC
程序头。
P.P.P.S。你应该学习 how to debug small programs.
每当调用 isstatic32 时,即使程序是动态编译的,它也会 return STATIC。我不知道该怎么做。每次它从 sh_name 检测到 .dynamic 时,我都尝试过,它会向变量添加 1,如果变量 > 1,它将 return 动态,但这没有用。 (不会让我 post 在这里编码)
#include <stdio.h>
#include <elf.h>
#define DYNAMIC 1
#define STATIC 2
static int isstatic32(FILE* fd, Elf32_Ehdr eh, Elf32_Shdr sh_table[])
{
static int i;
static int kek = 0;
static char* sh_str;
static char* buff;
buff = malloc(sh_table[eh.e_shstrndx].sh_size);
if(buff != NULL)
{
fseek(fd, sh_table[eh.e_shstrndx].sh_offset, SEEK_SET);
fread(buff, 1, sh_table[eh.e_shstrndx].sh_size, fd);
}
sh_str = buff;
for(i=0; i<eh.e_shnum; i++)
{
printf("%d", i);
if(!strcmp(".dynamic", (sh_str + sh_table[i].sh_name)))
{
return DYNAMIC;
}
}
return STATIC;
}
int main()
{
FILE *fp = NULL;
char* f;
f = "/proc/self/exe";
Elf32_Ehdr elf_header;
Elf32_Shdr* sh_table;
fp = fopen(f, "r");
fseek(fp, 0, SEEK_SET);
fread(&elf_header, 1, sizeof(Elf32_Ehdr), fp);
sh_table = malloc(elf_header.e_shentsize*elf_header.e_shnum);
fseek(fp, elf_header.e_shoff, SEEK_SET);
fread(sh_table, 1, elf_header.e_shentsize*elf_header.e_shnum, fp);
if(isstatic32(fp, elf_header, sh_table) == STATIC)
{
printf("statically linked");
}
else
{
printf("dynamic");
}
fp = NULL;
f = NULL;
}
the program is dynamically compiled
程序不能动态编译(或者更确切地说,"dynamically compiled"意味着完全不同的东西)。你的意思是程序动态链接.
大胆猜测:您在 64 位模式下编译了代码(因为您在默认为 64 位模式的 64 位系统上)。
这个:
Elf32_Ehdr elf_header;
如果您试图确定 64 位可执行文件是否静态链接,是错误的类型。
P.S。您的程序中还有许多其他错误。
P.P.S。 .dynamic
的存在与否 不是 确定二进制文件是静态链接还是动态链接的正确方法(部分不是必需的,可以完全剥离)。您应该寻找 PT_DYNAMIC
程序头。
P.P.P.S。你应该学习 how to debug small programs.