【问题标题】:Generic function from dlsym with dereferenced float来自 dlsym 的通用函数,带有取消引用的浮点数
【发布时间】: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 语句的函数的返回值是未定义的行为。
  • 好点,更新了列表。

标签: c cobol gnucobol


【解决方案1】:

C99 和 C11 6.5.2.2p6 状态

如果表示被调用函数的表达式的类型不包含原型,则对每个参数执行整数提升,而浮点类型的参数提升为双精度。这些称为默认参数提升。

6.5.2.2p7 继续

如果表示被调用函数的表达式具有包含原型的类型,则参数将隐式转换为相应参数的类型,就像通过赋值一样,将每个参数的类型作为非限定版本其声明的类型。函数原型声明器中的省略号会导致参数类型转换在最后一个声明的参数之后停止。默认参数提升是在尾随参数上执行的。

因此,当您调用具有float 参数的函数时,原型(您称之为“签名”)告诉编译器float 转换为double . (对于小于int 的整数类型也是如此。)

显然,解决方法是使用原型(“签名”)。你必须,如果你想传递floatcharshort,因为没有原型,它们会被提升为doubleint和@ 987654332@,分别。

但是,这真的不应该是一个负担。如果你有一些无原型的函数指针,比如说

int (*generic)() = dlsym(self, "aroutine");

如果你想调用一个原型为void foo(float, int, double)的函数,你总是可以转换函数指针:

((void (*)(float, int, double))generic)(f_arg, i_arg, d_arg);

虽然使用带有正确原型的临时函数指针肯定更容易阅读和维护:

{
    void (*call_foo)(float, int, double) = (void *)generic;
    call_foo(f_arg, i_arg, d_arg);
}

请参阅POSIX dlsym() documentation 以供参考。 (older versions 中推荐的 *(void **)(&amp;funcptr) 成语不再推荐;反正很傻。)

【讨论】:

  • 谢谢。修改代码生成会有点棘手,但这绝对是解决方案的途径(当参数类型已知时,添加函数指针的转换)。您可能刚刚让一些 COBOL 程序员更加高兴。在 MVCE 中测试,现在可以使用。是时候深入了解编译器补丁了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 2015-07-31
相关资源
最近更新 更多