Issue 23877 - ImportC: Importing byteswap.h results in undefined reference to core.bitop.byteswap
Summary: ImportC: Importing byteswap.h results in undefined reference to core.bitop.by...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 enhancement
Assignee: No Owner
URL:
Keywords: ImportC, pull
Depends on:
Blocks:
 
Reported: 2023-05-02 14:50 UTC by naydef
Modified: 2023-05-11 11:06 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description naydef 2023-05-02 14:50:43 UTC
Not sure if this is a valid issue.

xawd.c
-----------------
#include <byteswap.h>
-----------------



Compile with:
dmd xawd.c -betterC

Error:
-----------------
/usr/bin/ld: /usr/lib64/gcc/x86_64-solus-linux/12/../../../../lib64/Scrt1.o: in function `_start':
/home/build/YPKG/root/glibc/build/glibc-2.36/csu/../sysdeps/x86_64/start.S:103: undefined reference to `main'
/usr/bin/ld: xawd.o: in function `_D10__builtins__T17__builtin_bswap16ZQuFNaNbNiNftZt':
xawd.c:(.text._D10__builtins__T17__builtin_bswap16ZQuFNaNbNiNftZt[_D10__builtins__T17__builtin_bswap16ZQuFNaNbNiNftZt]+0x10): undefined reference to `_D4core5bitop8byteswapFNaNbNiNftZt'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1
-----------------

This specific byteswap.h file has:
-----------------
static __inline __uint16_t
__bswap_16 (__uint16_t __bsx)
{
#if __GNUC_PREREQ (4, 8)
  return __builtin_bswap16 (__bsx);
#else
  return __bswap_constant_16 (__bsx);
#endif
}
-----------------

DMD64 D Compiler v2.103.0 Linux
Comment 1 Walter Bright 2023-05-11 07:50:06 UTC
The problem is that in druntime/src/__builtins.di, which is loaded on every compile of an ImportC program, defines:

    ushort __builtin_bswap16()(ushort value)
    {
        import core.bitop;
        return core.bitop.byteswap(value);
    }

and core.bitop.byteswap is defined in the D runtime library, not the C one. Hence it shows up as an undefined symbol when a link is attempted.
Comment 2 Walter Bright 2023-05-11 08:02:40 UTC
-betterC is irrelevant for ImportC.
Comment 3 Dlang Bot 2023-05-11 08:04:04 UTC
@WalterBright created dlang/dmd pull request #15224 "fix Issue 23877 - ImportC: Importing byteswap.h results in undefined …" fixing this issue:

- fix Issue 23877 - ImportC: Importing byteswap.h results in undefined reference to core.bitop.byteswap

https://github.com/dlang/dmd/pull/15224
Comment 4 Dlang Bot 2023-05-11 11:06:08 UTC
dlang/dmd pull request #15224 "fix Issue 23877 - ImportC: Importing byteswap.h results in undefined …" was merged into master:

- c9b63234d03cd95ca4da665c2765ec355525b398 by Walter Bright:
  fix Issue 23877 - ImportC: Importing byteswap.h results in undefined reference to core.bitop.byteswap

https://github.com/dlang/dmd/pull/15224