【发布时间】:2016-03-13 22:55:42
【问题描述】:
GnuCOBOL 编译器通过使用动态符号查找来支持动态 CALL,但这里的 MCVE 严格来说是 C,并且比最小证明(我认为)4 和 8 字节大小都有效。
这是 AMD-64,所以 sizeof *float 不等于 sizeof float。
问题仅在由 dlsym 查找中的通用(在本例中未签名)函数指针调用时取消引用浮点数时才会出现。
// gcc -Wl,--export-dynamic -g -o byval byval.c -ldl
#include <stdio.h>
#include <dlfcn.h>
// hack in a 1 / 3 float 0.303030, 1050355402 as 32bit int
unsigned char field[4] = {0xca, 0x26, 0x9b, 0x3e};
// and a 1 / 6 double, 0.151515
unsigned char dtype[8] = {0x64, 0x93, 0x4d, 0x36, 0xd9, 0x64, 0xc3, 0x3f};
int aroutine(float);
int
main(int argc, char** argv)
{
float* fp = (float*)field;
double g;
void* this;
int (*calling)();
int result;
/* call the routines using generic data treated as float */
float f = *fp;
printf("Initial: %f \n", f);
printf("\nBy signature\n");
result = aroutine(*(float*)(field));
this = dlopen("", RTLD_LAZY);
printf("\nGeneric: (busted, stack gets 0x40000000)\n");
calling = dlsym(this, "aroutine");
result = calling(*(float*)(field));
printf("\nBy reference: (works when callee dereferences)\n");
calling = dlsym(this, "proutine");
result = calling((float*)(field));
printf("\nGeneric double (works):\n");
calling = dlsym(this, "droutine");
result = calling(*(double*)(dtype));
printf("\nGeneric int and double (works):\n");
calling = dlsym(this, "idroutine");
result = calling(*(int*)(field),*(double*)(dtype));
printf("\nGeneric int and float (busted) and int:\n");
calling = dlsym(this, "ifiroutine");
result = calling(*(int*)(field), *(float*)(field), *(int*)(field));
return 0;
}
int aroutine(float f) {
printf("aroutine: %f\n", f);
return 0;
}
int proutine(float *fp) {
printf("proutine: %f\n", *fp);
return 0;
}
int droutine(double g) {
printf("droutine: %g\n", g);
return 0;
}
int idroutine(int i, double g) {
printf("idroutine: %d %g\n", i, g);
return 0;
}
int ifiroutine(int i, float f, int j) {
printf("ifiroutine: %d %f %d\n", i, f, j);
return 0;
}
一连
prompt$ gcc -Wl,--export-dynamic -g -o mcve stackoverflow.c -ldl
prompt$ ./mcve
Initial: 0.303030
By signature
aroutine: 0.303030
Generic: (busted, stack gets 0x40000000)
aroutine: 2.000000
By reference: (works when callee dereferences)
proutine: 0.303030
Generic double (works):
droutine: 0.151515
Generic int and double (works):
idroutine: 1050355402 0.151515
Generic int and float (busted) and int:
ifiroutine: 1050355402 2.000000 1050355402
我想我需要了解一下 64 位 ABI 在取消引用浮点数据时如何处理未签名的调用。
包含 COBOL 标记是因为这会破坏 GnuCOBOL (生成 C 中间体),当使用 FLOAT-SHORT (C float) 和 CALL BY VALUE 时,而 FLOAT- LONG (C double) CALL BY VALUE 和 32 位整数一样有效。
顺便说一句,我很确定这不是 gcc 中的错误,因为 tcc tcc -rdynamic -g -o tccmcve stackoverflow.c -ldl 显示相同的输出,浮动取消引用似乎很无聊,所以我倾向于(并希望)这是一个可修复的东西,给编译器适当的语法提示,或编译时选项。
【问题讨论】:
-
我怀疑这与您的问题有关,位使用返回类型不是
void且没有return语句的函数的返回值是未定义的行为。 -
好点,更新了列表。