Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使libc覆盖函数在LD_预加载的情况下工作?_C_Gcc - Fatal编程技术网

无法使libc覆盖函数在LD_预加载的情况下工作?

无法使libc覆盖函数在LD_预加载的情况下工作?,c,gcc,C,Gcc,我试图使用LD_预加载技术覆盖一些libc函数,但无法使其工作。 这是STRIB.c #include <stdio.h> size_t strlen(const char *s){ printf("take strlen for %s\n",s); return 0; } gcc-o main.c 然后开始运行它 LD_PRELOAD=./strlib.so./main 它运行,但似乎没有调用我的覆盖函数 $ LD_PRELOAD=./strlib.so ./ma

我试图使用LD_预加载技术覆盖一些libc函数,但无法使其工作。 这是STRIB.c

#include <stdio.h>
size_t strlen(const char *s){
    printf("take strlen for %s\n",s);
    return 0;
}
gcc-o main.c

然后开始运行它

LD_PRELOAD=./strlib.so./main

它运行,但似乎没有调用我的覆盖函数

$ LD_PRELOAD=./strlib.so ./main 
length=5
我做错什么了吗

@编辑:正如Emest提到的,a更改了main.c以避免编译器优化,但它仍然不起作用

#include <string.h>
int main(int argc,char** argv){
    const char* s = "hello";
    printf("length=%d\n",strlen(argv[1]));
}
#包括
int main(int argc,字符**argv){
const char*s=“你好”;
printf(“长度=%d\n”,strlen(argv[1]);
}
$LD_PRELOAD=./strlib.so./main hehe
长度=4


检查汇编代码(使用gcc的-s参数)。编译器将优化对编译时常量字符串“hello”的strlen()调用,改为在编译时计算长度。尝试对一个直到运行时才知道长度的字符串调用函数,就像main()的一个参数一样,这应该可以像您所期望的那样工作。

谢谢,我忘记了,我反汇编了主文件,看到gcc是内联的
strlen
函数
#include <string.h>
int main(int argc,char** argv){
    const char* s = "hello";
    printf("length=%d\n",strlen(argv[1]));
}