__xstat 64 位上的动态符号解析错误
__xstat dynamic symbol resolving error on 64bit
我正在尝试使用 dlopen 和 dlsym 动态加载统计函数。stat 系列中的函数被包装在相应的函数__xstat
、 __xstat64
itp 中。
遵循代码的狙击,在 32 位模式下编译和工作(例如,包含 sys/stat.h
以获取统计信息结构(
#include <iostream>
#include <dlfcn.h>
#include <sys/stat.h>
typedef int (*xstat_f) (int __ver, const char *__filename, struct stat *__stat_buf);
int main()
{
auto* h = dlopen("libc.so.6", RTLD_LAZY);
if(!h)
{
return 1; // invalid handle
}
auto f = (xstat_f)dlsym(h, "__xstat");
if(!f)
{
return 1; // invalid handle
}
struct stat s = {};
const auto r = f(3, "/tmp", &s);
if (r != 0)
{
perror("stat");
return errno;
}
return 0;
}
g++ main.cpp -o main -ldl -m32
在 64 位计算机上没有-m32
开关的情况下编译的可执行文件返回 EINVAL(无效参数(。
原因是什么?
我也做了一个最小的测试
#include <iostream>
#include <sys/stat.h>
int main(){
struct stat s;
const auto x = stat("/tmp", &s);
if(x != 0) return errno;
return 0;
}
并且在两个可执行文件(32 位和 64 位(上使用 objdump -T
表明stat
解析为 __xstat
,所以我使用了正确的符号。我也尝试了__xstat/__xstat64
、struct stat/stat64
的组合,结果相同。
__xstat声明如下:
int __xstat(int ver, const char *path, (struct stat *stat_buf))
在文档中,参数ver被描述为ver shall be 3 or the behavior of these functions is undefined
,这并不完全正确,因为在源代码中,_STAT_VER_LINUX
的定义如下:
#ifndef __x86_64__
# define _STAT_VER_LINUX 3
#else
# define _STAT_VER_LINUX 1
#endif
这就是为什么__xstat
64 位调用失败的原因,参数 ver
应该设置为 1,32 位编译时应设置为 3。
相关文章:
- 在C++中使用内联方法时出现未定义的符号错误
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 函数指针给我"重新声明为不同类型的符号"错误
- "byte":使用Crypto++和Windows SDK时出现不明确的符号错误
- 函数路径追加上未解决的外部符号错误
- 作为成员的静态类模板实例会导致未解析的外部符号错误
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- 共享库中使用"使用命名空间"定义的符号错误
- 在FreesWitch中使用AWS C SDK加载模块时未定义的符号错误
- 在 Mac 上C++编译代码时处理“dyld:惰性符号绑定失败:找不到符号”错误
- C++编译错误LNK2019:未解决的外部符号错误
- VTK / ITK/ QT - 未解决的外部符号错误 (LNK2001)
- QT创建者:简单程序未解决的外部符号和外部符号错误
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- 标准::字符串::空的未定义符号错误;Mac OS High Sierra 上的 c++ 标准方法链接错误
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 由于使用未签名的INT,因此在Variadic模板C 上未解决的外部符号错误
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- 使用从WINRT网格类派生的抽象类时,Casttobase未解决的外部符号错误
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See