Issue 23209 - Linker error for valid code.
Summary: Linker error for valid code.
Status: RESOLVED DUPLICATE of issue 20802
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: x86_64 All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-06-23 14:05 UTC by Andrea Fontana
Modified: 2024-02-20 13:23 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrea Fontana 2022-06-23 14:05:51 UTC
Dmd compiles this code, but linker fails. 


--- 
#!/usr/bin/rdmd 

import std;

void main() {
    auto httpClient = HTTP();
    httpClient.setAuthentication("", "");
    string src = `{}`;
    auto json = parseJSON(src);
    writeln(json);
}

---

It returns a (strange) linking error:

/usr/bin/ld: /tmp/.rdmd-1000/rdmd-test2.d-C954334114259D06EF20F7503F19D193/objs/test2.o: in function `_D4core8internal7switch___T14__switch_errorZQrFNaNbNiNfAyamZv':
/usr/include/dmd/phobos/std/package.d:(.text._D4core8internal7switch___T14__switch_errorZQrFNaNbNiNfAyamZv[_D4core8internal7switch___T14__switch_errorZQrFNaNbNiNfAyamZv]+0x19): undefined reference to `_D4core9exception__T15__switch_errorTZQsFNaNbNiNeAyamZv'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1


If you replace writeln(json) with writeln(json.toString) it works.
If you comment writeln, it works.
If you comment httpClient.setAuthentication, it works.
Comment 1 mhh 2022-06-23 19:28:25 UTC
What happens with dmd -run rather than dmd?
Comment 2 kdevel 2022-06-24 13:52:04 UTC
$ dmd k.d
k.o: In function `_D4core8internal7switch___T14__switch_errorZQrFNaNbNiNfAyamZv':
k.d:(.text._D4core8internal7switch___T14__switch_errorZQrFNaNbNiNfAyamZv[_D4core8internal7switch___T14__switch_errorZQrFNaNbNiNfAyamZv]+0x19): undefined reference to `_D4core9exception__T15__switch_errorTZQsFNaNbNiNeAyamZv'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1

$ dmd -allinst -run k.d
{}

[for the record: binutils < 2.26 raises unrecognized relocation (0x2a) in section `.text', https://stackoverflow.com/questions/52737698]
Comment 3 Luís Marques 2022-09-21 08:47:31 UTC
I also ran into this issue. The scenario is a bit different, I'm using LDC2 on macOS and I'm linking D code with C code. The linker driver is clang++. The same D code compiled with dub/ldc2 didn't seem to cause issues, but various D libraries are involved so it's hard to tell for sure without reducing it, which I haven't done yet. I also have two linker errors, instead of one. For now, I'm working around it with:

C code:

void _D4core9exception__T15__switch_errorTZQsFNaNbNiNeAyamZv(void) {
   assert(0);
}
void _D3std4math10operations__T17extractBitpatternTfZQwFNaNbNiNexfZSQCjQCiQCg__T23FloatingPointBitpatternTfZQBc(void) {
   assert(0);
}

I.e. trapping implementations for the missing:

pure nothrow @nogc @trusted void core.exception.__switch_errorT!().__switch_errorT(immutable(char)[], long);

pure nothrow @nogc @trusted std.math.operations.FloatingPointBitpattern!(float).FloatingPointBitpattern std.math.operations.extractBitpattern!(float).extractBitpattern(const(float))
Comment 4 Bastiaan Veelo 2024-01-08 10:22:29 UTC
Further reduced:

```d
void main()
{
    import std.net.curl, std.stdio;
    auto http = HTTP();
    http.setAuthentication("", "");
    writeln(1.0);
}
```

The missing symbol is `pure nothrow @nogc @trusted void core.exception.__switch_errorT!().__switch_errorT(immutable(char)[], ulong)`.
Comment 5 Bastiaan Veelo 2024-01-08 10:50:24 UTC
Workaround: use the `-allinst` option.
Comment 6 Iain Buclaw 2024-02-20 13:23:59 UTC
Marking this as a duplicate.

*** This issue has been marked as a duplicate of issue 20802 ***