Bug 23347 – ImportC: pragma pack causes asm label to set symbol name to be ignored
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-09-19T07:58:04Z
Last change time
2022-09-26T02:15:29Z
Keywords
ImportC, pull, wrong-code
Assigned to
No Owner
Creator
dave287091
Comments
Comment #0 by dave287091 — 2022-09-19T07:58:04Z
macOS headers use the asm(“”) syntax to set the symbol name, which is important for some functions like fstat that have both a 32 bit and 64bit version (but now fstat in code refers to the 64 bit version). For example:
// foo.c
int fstat(int, struct stat *) asm("_" "fstat" "$INODE64");
void foo(void){
int (*f)(int, struct stat*) = fstat;
}
$ dmd foo.c -c -offoo.o
$ nm -g foo.o
U __fstat$INODE64
0000000000000000 S _foo
(the extra underscore is from issue 23343).
However, if a pragma pack is active, the asm label seems to be ignored:
// foo2.c
#pragma pack(push, 4)
int fstat(int, struct stat *) asm("_" "fstat" "$INODE64");
#pragma pack(pop)
void foo(void){
int (*f)(int, struct stat*) = fstat;
}
$ dmd foo2.c -c -offoo2.o
$ nm -g foo2.o
0000000000000000 S _foo
U _fstat
On macOS, this means you link against the 32 bit fstat instead of 64 bit fstat$INODE64 (so silently incorrectly link to the wrong function), which means you read garbage after stat'ing as the structure is not filled out properly. Most other symbols mangle to the wrong name even without the asm label.
Comment #1 by dave287091 — 2022-09-19T08:00:03Z
(In reply to dave287091 from comment #0)
> Most other symbols mangle to the wrong name even without the asm
> label.
I meant the right name.
@WalterBright created dlang/dmd pull request #14481 "fix Issue 23347 - ImportC: pragma pack causes asm label to set symbol…" fixing this issue:
- fix Issue 23347 - ImportC: pragma pack causes asm label to set symbol name to be ignored
https://github.com/dlang/dmd/pull/14481
Comment #4 by dlang-bot — 2022-09-26T02:15:29Z
dlang/dmd pull request #14481 "fix Issue 23347 - ImportC: pragma pack causes asm label to set symbol…" was merged into master:
- 98ac0a4a142d09fee8db89af9bea7a31b26401af by Walter Bright:
fix Issue 23347 - ImportC: pragma pack causes asm label to set symbol name to be ignored
https://github.com/dlang/dmd/pull/14481