c++ - __xstat 64 位动态符号解析错误
问题描述
我正在尝试使用 dlopen 和 dlsym 动态加载 stat 函数。stat 系列中的函数被包装在相应的函数itp 中__xstat
。__xstat64
截取代码后,在 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
-m32
在 64 位机器上没有开关的情况下编译的可执行文件返回 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;
}
并objdump -T
在 32 位和 64 位的两个可执行文件上使用 ,显示stat
已解析为__xstat
,因此我使用了正确的符号。我也试过组合__xstat/__xstat64
, struct stat/stat64
, 相同的结果。
解决方案
__xstat 声明如下:
int __xstat(int ver, const char *path, (struct stat *stat_buf))
in documentation parameter ver is described like, ver shall be 3 or the behavior of these functions is undefined
, which is not entirely true, because in source code, definition of _STAT_VER_LINUX
follows:
#ifndef __x86_64__
# define _STAT_VER_LINUX 3
#else
# define _STAT_VER_LINUX 1
#endif
That's why __xstat
call on 64bits was failling, parameter ver
should have been set to 1, and 3 on 32bits compilation.
推荐阅读
- c# - Xamarin C# - INotifyPropertyChanged 模型 - JSON 反序列化错误
- python - 将 2D numpy 数组转换为 STL 文件
- setup.py - 是否可以在 wheel/bdist/sdist 中包含 Python 包之外的文件?
- amazon-web-services - gulp-awspublish 使用 AWS 配置文件而不是 AWS_ACCESS_KEY 和秘密
- html - 将整个页面缩小为一个 div
- c# - 如何检查是否在 KeyUp 事件的文本框中输入了数字或字母
- flutter - 设备锁定时颤动动画侦听器不起作用
- eclipse - 限制 Eclipse RCP 项目中打开的选项卡
- r - 根据日期/时间范围和匹配 id 过滤数据
- java - 如何解决读取 JSON 文件以获取名字和姓氏的失败执行?