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 标记是因为当使用 FLOAT-SHORT (C float)和 CALL BY VALUE 时,这会破坏 GnuCOBOL (生成 C 中间体),而 FLOAT-LONG (C double) CALL BY VALUE 可以工作,32 位整数也是如此。
顺便说一句,我很确定这不是 gcc 中的错误,因为 tcctcc -rdynamic -g -o tccmcve stackoverflow.c -ldl
显示相同的输出,浮动取消引用似乎很无聊,所以我倾向于(并希望)这是一个可修复的东西,给出正确的语法提示到编译器,或编译时选项。