我有以下 C 程序:
#include <sys/stat.h>
int main(int argc, char **argv) {
struct stat fileStat;
if(stat(argv[1],&fileStat) < 0)
return 1;
}
当我使用 Clang 将其编译为 LLVM IR 时,我可以看到 stat
声明如下:
declare i32 @stat(i8*, %struct.stat*)
通常,这种对系统函数的外部调用直接映射到C标准库函数。例如,我可以使用以下内容找到 malloc
:
nm -D /lib/x86_64-linux-gnu/libc.so.6 | grep malloc
但是,stat
函数似乎被不同地对待。当 grep 查找 stat
时,我可以找到相关函数,例如 __xstat
,但找不到 stat
函数本身。
当我使用 ltrace
跟踪对外部库的调用时,我看到以下调用:__xstat(1, ".", 0x7fff7928c6f0)
。此外,可执行文件中的代码确认调用的是 __xstat
函数,而不是调用 stat
函数。
我没有观察到对 C 标准库的其他函数调用的名称与 C 程序中声明的名称不同。为什么标准库中没有直接等效项?我的编译器如何发现它应该生成对 __xstat
而不是 stat
的调用?
最佳答案
头文件sys/stat.h
将stat
定义为在glibc中调用__xstat
的宏:
#define stat(fname, buf) __xstat (_STAT_VER, fname, buf)
关于c - 我的编译器如何找到 stat(文件状态)函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37276120/