Bug 23343 – ImportC: functions declared with asm label to set symbol name gets extra underscore prepended
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
Mac OS X
Creation time
2022-09-18T04:32:31Z
Last change time
2022-10-10T05:21:29Z
Keywords
ImportC, link-failure, pull
Assigned to
No Owner
Creator
dave287091
Comments
Comment #0 by dave287091 — 2022-09-18T04:32:31Z
Lots of libc functions on macOS are declared like:
int open(const char *, int, ...) asm("_" "open" );
with the asm label to (pointlessly) set the symbol name.
Apparently an extra underscore gets prepended at some point as the following C program will fail to link:
---
// o.c
int open(const char*, int, ...) asm("_" "open");
int main(){
int fd = open("/dev/null", 0);
return fd >= 0 ? 0 : 1;
}
---
$ dmd o.c
Undefined symbols for architecture x86_64:
"__open", referenced from:
_main in o.o
ld: symbol(s) not found for architecture x86_64
Strangely though, if you write a D file to print the mangle of `open`:
---
// op.d
import o;
pragma(msg, o.open.mangleof); // _open
---
The extra underscore is not present.
Output of `nm` also shows the extra underscore.
$ nm -g o.o
U __open
0000000000000000 T _main
0000000000000058 S _o.main.eh
I don’t know if this issue affects other platforms.
Comment #1 by dave287091 — 2022-09-18T04:33:31Z
This same issue also affects LDC.
Comment #2 by maxhaton — 2022-09-18T09:21:27Z
The extra underscore is added by the backend/s, will take a look at fixing this
@WalterBright created dlang/dmd pull request #14485 "fix Issue 23343 - ImportC: functions declared with asm label to set s…" fixing this issue:
- fix Issue 23343 - ImportC: functions declared with asm label to set symbol name gets extra underscore prepended
https://github.com/dlang/dmd/pull/14485
Comment #5 by dlang-bot — 2022-09-25T07:02:26Z
@WalterBright created dlang/dmd pull request #14486 "fix Issue 23343 - ImportC: functions declared with asm label to set s…" fixing this issue:
- fix Issue 23343 - ImportC: functions declared with asm label to set symbol name gets extra underscore prepended
https://github.com/dlang/dmd/pull/14486