c - 我的编译器如何找到 stat(文件状态)函数?

标签 c linker llvm glibc llvm-ir

我有以下 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.hstat定义为在glibc中调用__xstat的宏:

#define stat(fname, buf) __xstat (_STAT_VER, fname, buf)

关于c - 我的编译器如何找到 stat(文件状态)函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37276120/

相关文章:

c - 在 C 中扫描字符串时自动分配

c++ - 测试执行后缺少某些 GCDA 文件

c++ - LLVM 拒绝编译 C++ 源代码,奇怪的错误

C : Best way to go to a known line of a file

c - 在 pthread_kill() 中使用 SIGUSR1 和 SIGUSR2 作为信号

c++ - 使用 Visual Studio 2013 编译 FFTW 3.3.4

c - C 程序中 undefined reference

javascript - 我可以使用 emscripten 在 i386 模式下编译应用程序吗?

c - C中函数的返回值

c++ - 在 Imake 文件中添加 -fPIC 选项