从标题读取sh_name每次都会返回一个值 [英] Reading sh_name from header will return one value everytime

查看:344
本文介绍了从标题读取sh_name每次都会返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论何时调用isstatic32,即使程序是动态编译的,它也将返回STATIC。我不知道该怎么做。我尝试过每次从sh_name检测到.dynamic时,都会将1加到变量中;如果该变量> 1,它将返回dynamic,但这是行不通的。 (不允许我在此处发布代码)

Whenever isstatic32 is called, it will return STATIC even though the program is dynamically compiled. I have no idea what to do. I've tried everytime it detects .dynamic from sh_name, it adds 1 to a variable and if the variable is > 1 it will return dynamic, but that didn't work. (won't let me post code here)

    #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

程序不能被动态编译(或者,动态编译表示完全不同的东西)。您的意思是该程序是动态链接的

The program can't be dynamically compiled (or rather, "dynamically compiled" means something totally different). What you mean is that the program is dynamically linked.

疯狂的猜测:您在64位模式下编译了代码(因为您处于一个默认为64位模式的64位系统)。

Wild guess: you compiled your code in 64-bit mode (because you are on a 64-bit system which defaults to 64-bit mode).

此:

Elf32_Ehdr elf_header;

是错误的类型,如果您试图确定64位可执行文件是静态链接还是

is the wrong type to use if you are trying to determine whether 64-bit executable is statically linked or not.

PS程序中还有许多其他错误。

P.S. There are many other bugs in your program as well.

P.P.S。是否存在 .dynamic 不是确定二进制文件是静态链接还是动态链接的正确方法(不需要节,并且可以完全剥离)。您应该寻找 PT_DYNAMIC 程序标头。

P.P.S. Presence or absence of .dynamic is not correct way to determine whether the binary was statically or dynamically linked (sections aren't required, and can be completely stripped). You should look for PT_DYNAMIC program header instead.

P.P.P.S。您应该学习如何调试小型程序

P.P.P.S. You should learn how to debug small programs.

这篇关于从标题读取sh_name每次都会返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆