Thread overview
[sdc] linker problems when building programs with sdc
Oct 18, 2015
Marco Leise
October 18, 2015
Hello all,

I recently decided to have another play with sdc to see how it's doing.  Since my dmd is installed in /opt/dmd/ I had to do a couple of tricks to get sdc itself to build:

(i) put a dlang.conf in /etc/ld.so.conf.d/ containing the /opt/dmd/lib64 path;

(ii) call 'make LD_PATH=/opt/dmd/lib64' when building sdc

sdc itself then builds successfully, and I wind up with a bin/ directory containing sdc and sdc.conf (which contains includePath and libPath options) and a lib/ directory containing libd.a, libd-llvm.a, libphobos.a and libsdrt.a.

However, when I try to build any program, even a simple hello-world, I get a linker error:

$ ./sdc hello.d
hello.o: In function `_D5hello4mainFMZv':
hello.d:(.text+0x1c): undefined reference to `_D3std5stdio7writelnFMAyaZv'
collect2: error: ld returned 1 exit status

To solve this, I tried adding in a library-path flag, but this simply resulted in an exception being thrown by sdc's options parsing:

$ ./sdc -L$MYHOMEDIR/code/D/sdc/lib hello.d
std.getopt.GetOptException@/opt/dmd/bin/../import/std/getopt.d(604): Unrecognized option -L$MYHOMEDIR/code/D/sdc/lib

[cut great big backtrace]

Can anyone advise what's missing in my setup?  I did also try adding $MYHOMEDIR/code/D/sdc/lib to the /etc/ld.so.conf.d/dlang.conf file, and re-running ldconfig, but that didn't seem to make any difference.

Thanks & best wishes,

    -- Joe
October 18, 2015
Am Sun, 18 Oct 2015 11:35:16 +0200
schrieb Joseph Rushton Wakeling via Digitalmars-d-learn
<digitalmars-d-learn@puremagic.com>:

> Hello all,
> 
> I recently decided to have another play with sdc to see how it's doing.  Since my dmd is installed in /opt/dmd/ I had to do a couple of tricks to get sdc itself to build:
> 
> (i) put a dlang.conf in /etc/ld.so.conf.d/ containing the /opt/dmd/lib64 path;
> 
> (ii) call 'make LD_PATH=/opt/dmd/lib64' when building sdc
> 
> sdc itself then builds successfully, and I wind up with a bin/ directory containing sdc and sdc.conf (which contains includePath and libPath options) and a lib/ directory containing libd.a, libd-llvm.a, libphobos.a and libsdrt.a.
> 
> However, when I try to build any program, even a simple hello-world, I get a linker error:
> 
> $ ./sdc hello.d
> hello.o: In function `_D5hello4mainFMZv':
> hello.d:(.text+0x1c): undefined reference to `_D3std5stdio7writelnFMAyaZv'
> collect2: error: ld returned 1 exit status
> 
> To solve this, I tried adding in a library-path flag, but this simply resulted in an exception being thrown by sdc's options parsing:
> 
> $ ./sdc -L$MYHOMEDIR/code/D/sdc/lib hello.d std.getopt.GetOptException@/opt/dmd/bin/../import/std/getopt.d(604): Unrecognized option -L$MYHOMEDIR/code/D/sdc/lib
> 
> [cut great big backtrace]
> 
> Can anyone advise what's missing in my setup?  I did also try adding $MYHOMEDIR/code/D/sdc/lib to the /etc/ld.so.conf.d/dlang.conf file, and re-running ldconfig, but that didn't seem to make any difference.
> 
> Thanks & best wishes,
> 
>      -- Joe

Maybe you should have started with `return 42;`? :D
writeln is not a light-weight in terms of exercised compiler
features. I didn't even know that it compiles yet. Last time I
heard it was not usable.

-- 
Marco

October 18, 2015
On 18/10/15 19:43, Marco Leise via Digitalmars-d-learn wrote:
> Maybe you should have started with `return 42;`? :D
> writeln is not a light-weight in terms of exercised compiler
> features. I didn't even know that it compiles yet. Last time I
> heard it was not usable.

Hahahahahahahaha :-D

Turns out even `return 42;` is a bit heavy-duty.  I do really like the way that sdc is obviously hooking into llvm's error reporting mechanism, though:

$ ./sdc nohello.d
; ModuleID = 'nohello.d'

define i32 @_Dmain() {
  br label %body

body:                                             ; preds = %0
  call void @_D7nohello4mainFMZv()
  ret i32 0
}

define void @_D7nohello4mainFMZv() {

body:                                             ; No predecessors!
}
nohello.d:3:4: error: d.ir.error.ErrorExpression is not supported
    return 42;
    ^~~~~~~~~~
d.exception.CompileException@libd/src/d/exception.d(15): d.ir.error.ErrorExpression is not supported
----------------
./sdc(llvm.c.core.__LLVMOpaqueValue* util.visitor.dispatchImpl!(_D1d4llvm10expression13ExpressionGen5visitMFC1d2ir10expression10ExpressionZ14__funcliteral2FC1d2ir10expression10ExpressionZPS4llvm1c4core17__LLVMOpaqueValue, d.llvm.expression.ExpressionGen, d.ir.expression.Expression).dispatchImpl(ref d.llvm.expression.ExpressionGen, d.ir.expression.Expression)+0x538) [0x138fae0]
./sdc(llvm.c.core.__LLVMOpaqueValue* util.visitor.dispatch!(_D1d4llvm10expression13ExpressionGen5visitMFC1d2ir10expression10ExpressionZ14__funcliteral2FC1d2ir10expression10ExpressionZPS4llvm1c4core17__LLVMOpaqueValue, d.llvm.expression.ExpressionGen, d.ir.expression.Expression).dispatch(ref d.llvm.expression.ExpressionGen, d.ir.expression.Expression)+0x1d) [0x138f5a5]
./sdc(llvm.c.core.__LLVMOpaqueValue* d.llvm.expression.ExpressionGen.visit(d.ir.expression.Expression)+0x55) [0x137ec55]
./sdc(llvm.c.core.__LLVMOpaqueValue* d.llvm.statement.StatementGen.genExpression(d.ir.expression.Expression)+0x3f) [0x138a627]
./sdc(void d.llvm.statement.StatementGen.visit(d.ast.statement.ReturnStatement!(d.ir.expression.Expression, d.ir.statement.Statement).ReturnStatement)+0x75) [0x138b1bd]
./sdc(void util.visitor.__T12dispatchImplS294util7visitor14__funcliteral6TS1d4llvm9statement12StatementGenTC1d2ir9statement9StatementZ.dispatchImpl(ref d.llvm.statement.StatementGen, d.ir.statement.Statement)+0x274) [0x13907ac]
./sdc(void util.visitor.__T8dispatchS294util7visitor14__funcliteral6TS1d4llvm9statement12StatementGenTC1d2ir9statement9StatementZ.dispatch(ref d.llvm.statement.StatementGen, d.ir.statement.Statement)+0x1d) [0x1390535]
./sdc(void d.llvm.statement.StatementGen.visit(d.ir.statement.Statement)+0x55) [0x138a4ad]
./sdc(void d.llvm.statement.StatementGen.visit(d.ast.statement.BlockStatement!(d.ir.statement.Statement).BlockStatement)+0xc2) [0x138ad3a]
./sdc(void d.llvm.local.LocalGen.genBody(d.ir.symbol.Function, llvm.c.core.__LLVMOpaqueValue*)+0xbb8) [0x1388730]
./sdc(bool d.llvm.local.LocalGen.maybeDefine(d.ir.symbol.Function, llvm.c.core.__LLVMOpaqueValue*)+0x10e) [0x1387b56]
./sdc(llvm.c.core.__LLVMOpaqueValue* d.llvm.local.LocalGen.define(d.ir.symbol.Function)+0x8e) [0x138798e]
./sdc(llvm.c.core.__LLVMOpaqueValue* d.llvm.global.GlobalGen.define(d.ir.symbol.Function)+0x15b) [0x13860cb]
./sdc(void d.llvm.global.GlobalGen.define(d.ir.symbol.Symbol)+0xd0) [0x1385db8]
./sdc(d.ir.symbol.Module d.llvm.codegen.CodeGenPass.visit(d.ir.symbol.Module)+0xe2) [0x132c0f2]
./sdc(void d.llvm.backend.LLVMBackend.emitObject(d.ir.symbol.Module[], immutable(char)[])+0xd1) [0x1328c79]
./sdc(void sdc.sdc.SDC.codeGen(immutable(char)[])+0x87) [0x12c7d17]
./sdc(void sdc.sdc.SDC.codeGen(immutable(char)[], immutable(char)[])+0x74) [0x12c7d94]
./sdc(_Dmain+0x59a) [0x12be462]
/opt/dmd/lib64/libphobos2.so.0.68(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x28) [0x7f7c02d54ee8]
/opt/dmd/lib64/libphobos2.so.0.68(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2d) [0x7f7c02d54e2d]
/opt/dmd/lib64/libphobos2.so.0.68(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x2d) [0x7f7c02d54e8d]
/opt/dmd/lib64/libphobos2.so.0.68(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2d) [0x7f7c02d54e2d]
/opt/dmd/lib64/libphobos2.so.0.68(_d_run_main+0x1e7) [0x7f7c02d54da7]
./sdc(main+0x20) [0x12c7708]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7f7c0173fa40]

October 22, 2015
... or, it turns out, sdc doesn't like it when you forget to rewrite
`void main()` as `int main()`, and its error messages are still in the cryptic stage :-P

Onwards and upwards, then ... :-)

On 18/10/15 19:58, Joseph Rushton Wakeling via Digitalmars-d-learn wrote:
> Turns out even `return 42;` is a bit heavy-duty.  I do really like the way that
> sdc is obviously hooking into llvm's error reporting mechanism, though:
>
> $ ./sdc nohello.d
> ; ModuleID = 'nohello.d'
>
> define i32 @_Dmain() {
>    br label %body
>
> body:                                             ; preds = %0
>    call void @_D7nohello4mainFMZv()
>    ret i32 0
> }
>
> define void @_D7nohello4mainFMZv() {
>
> body:                                             ; No predecessors!
> }
> nohello.d:3:4: error: d.ir.error.ErrorExpression is not supported
>      return 42;
>      ^~~~~~~~~~
> d.exception.CompileException@libd/src/d/exception.d(15):
> d.ir.error.ErrorExpression is not supported
> ----------------
> ./sdc(llvm.c.core.__LLVMOpaqueValue*
> util.visitor.dispatchImpl!(_D1d4llvm10expression13ExpressionGen5visitMFC1d2ir10expression10ExpressionZ14__funcliteral2FC1d2ir10expression10ExpressionZPS4llvm1c4core17__LLVMOpaqueValue,
> d.llvm.expression.ExpressionGen, d.ir.expression.Expression).dispatchImpl(ref
> d.llvm.expression.ExpressionGen, d.ir.expression.Expression)+0x538) [0x138fae0]
> ./sdc(llvm.c.core.__LLVMOpaqueValue*
> util.visitor.dispatch!(_D1d4llvm10expression13ExpressionGen5visitMFC1d2ir10expression10ExpressionZ14__funcliteral2FC1d2ir10expression10ExpressionZPS4llvm1c4core17__LLVMOpaqueValue,
> d.llvm.expression.ExpressionGen, d.ir.expression.Expression).dispatch(ref
> d.llvm.expression.ExpressionGen, d.ir.expression.Expression)+0x1d) [0x138f5a5]
> ./sdc(llvm.c.core.__LLVMOpaqueValue*
> d.llvm.expression.ExpressionGen.visit(d.ir.expression.Expression)+0x55) [0x137ec55]
> ./sdc(llvm.c.core.__LLVMOpaqueValue*
> d.llvm.statement.StatementGen.genExpression(d.ir.expression.Expression)+0x3f)
> [0x138a627]
> ./sdc(void
> d.llvm.statement.StatementGen.visit(d.ast.statement.ReturnStatement!(d.ir.expression.Expression,
> d.ir.statement.Statement).ReturnStatement)+0x75) [0x138b1bd]
> ./sdc(void
> util.visitor.__T12dispatchImplS294util7visitor14__funcliteral6TS1d4llvm9statement12StatementGenTC1d2ir9statement9StatementZ.dispatchImpl(ref
> d.llvm.statement.StatementGen, d.ir.statement.Statement)+0x274) [0x13907ac]
> ./sdc(void
> util.visitor.__T8dispatchS294util7visitor14__funcliteral6TS1d4llvm9statement12StatementGenTC1d2ir9statement9StatementZ.dispatch(ref
> d.llvm.statement.StatementGen, d.ir.statement.Statement)+0x1d) [0x1390535]
> ./sdc(void d.llvm.statement.StatementGen.visit(d.ir.statement.Statement)+0x55)
> [0x138a4ad]
> ./sdc(void
> d.llvm.statement.StatementGen.visit(d.ast.statement.BlockStatement!(d.ir.statement.Statement).BlockStatement)+0xc2)
> [0x138ad3a]
> ./sdc(void d.llvm.local.LocalGen.genBody(d.ir.symbol.Function,
> llvm.c.core.__LLVMOpaqueValue*)+0xbb8) [0x1388730]
> ./sdc(bool d.llvm.local.LocalGen.maybeDefine(d.ir.symbol.Function,
> llvm.c.core.__LLVMOpaqueValue*)+0x10e) [0x1387b56]
> ./sdc(llvm.c.core.__LLVMOpaqueValue*
> d.llvm.local.LocalGen.define(d.ir.symbol.Function)+0x8e) [0x138798e]
> ./sdc(llvm.c.core.__LLVMOpaqueValue*
> d.llvm.global.GlobalGen.define(d.ir.symbol.Function)+0x15b) [0x13860cb]
> ./sdc(void d.llvm.global.GlobalGen.define(d.ir.symbol.Symbol)+0xd0) [0x1385db8]
> ./sdc(d.ir.symbol.Module
> d.llvm.codegen.CodeGenPass.visit(d.ir.symbol.Module)+0xe2) [0x132c0f2]
> ./sdc(void d.llvm.backend.LLVMBackend.emitObject(d.ir.symbol.Module[],
> immutable(char)[])+0xd1) [0x1328c79]
> ./sdc(void sdc.sdc.SDC.codeGen(immutable(char)[])+0x87) [0x12c7d17]
> ./sdc(void sdc.sdc.SDC.codeGen(immutable(char)[], immutable(char)[])+0x74)
> [0x12c7d94]
> ./sdc(_Dmain+0x59a) [0x12be462]
> /opt/dmd/lib64/libphobos2.so.0.68(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x28)
> [0x7f7c02d54ee8]
> /opt/dmd/lib64/libphobos2.so.0.68(void rt.dmain2._d_run_main(int, char**, extern
> (C) int function(char[][])*).tryExec(scope void delegate())+0x2d) [0x7f7c02d54e2d]
> /opt/dmd/lib64/libphobos2.so.0.68(void rt.dmain2._d_run_main(int, char**, extern
> (C) int function(char[][])*).runAll()+0x2d) [0x7f7c02d54e8d]
> /opt/dmd/lib64/libphobos2.so.0.68(void rt.dmain2._d_run_main(int, char**, extern
> (C) int function(char[][])*).tryExec(scope void delegate())+0x2d) [0x7f7c02d54e2d]
> /opt/dmd/lib64/libphobos2.so.0.68(_d_run_main+0x1e7) [0x7f7c02d54da7]
> ./sdc(main+0x20) [0x12c7708]
> /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7f7c0173fa40]
>
>