Linker error compiling green threads in 200 lines of Rust

I've followed along with @cfsamson's excellent book green threads in 200 lines of Rust code. However, I get a massive linker error when compiling the final code.

The short bit I can understand says "Failed to link with cc". I am not aware that I'm using cc or any other external crate, though. I'm a bit stumped by it. Any ideas?

Full linker error:
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-L" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.14f339kk8f117ysa.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.1btbzwcya6vpiaqj.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.1f50n7w6yl6kv8vm.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.1ojxbpa870lm24a0.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.1p5ut9mlb7bzyntr.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.1tbjwx1pt6760s37.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.1vgi3u1wv4k523ea.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.293wlughsvrn2c0o.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.2bbtkh5rl1ghbf0d.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.2kj1t1tqokqgtqw2.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.2n526fe5pv38q4v7.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.2ngvdbfewr10vmdb.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.2o0vpxpnxmuqn23a.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.2s3maq04ye3ybuw0.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.2vchb3ri9xkhzl21.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.2wojp5rndjh0ly65.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.2yo1xviv691neawy.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.34bwmi9yd9pm90ht.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.36zgbvbdvxapf3uv.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.37akimzsqnk05zq9.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.386smqdol1rlupu2.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.39rh6r8q9xx9tfsh.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.3dak9o0k248vxfuy.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.3i650i0dacff61zp.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.3ma05z9lpbb7vco8.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.3oahqqchd24de0f4.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.4010b1zmy2b0yjfd.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.44g99ed7la1mwqsu.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.45du9r1vov78apvf.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.48ldx42396nf8kw8.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.49nt34gqqw2gja58.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.4p5m02wc2ylh1369.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.4q6x0rgltr43v8p5.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.4s6kasu4rx5i6si4.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.4stzsmb9l96vhh9u.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.50cv5v8vx4zkeyf.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.52kuwneajweew2in.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.55axrjyl6dc5fe0.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.838g87k5cn2m4tm.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.gou78067g7ikb4l.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.gyibpbgy4ht5to8.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.jdc6cnykxpitq9i.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.o4ad86eiie9jwwc.rcgu.o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.ozp0vafu3dpx4k.rcgu.o" "-o" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653" "/home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.1sjyouvpppi87w8j.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/louis/repos/Rust/green_threads/target/debug/deps" "-L" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-25c6acf8063a3802.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-539f13c9442f1597.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-37db28e905edb56b.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-9ef2480568df55af.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-1e0f0992cdbecd66.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-6c8e02b8fedc1e5f.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-807e5ad203594490.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-083fce1bea11612a.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-1af568081add9042.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-1395b54a3b3f45bf.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-caba820045f178d5.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-512eb53291f6de7e.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-5efacc5025f9f3d8.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-9c4002b5f79ba0e1.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-90996f4879673567.rlib" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-9ea09a899c3eda46.rlib" "-Wl,--end-group" "/home/louis/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-ef2408da76957905.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc"
  = note: /usr/bin/ld: /home/louis/repos/Rust/green_threads/target/debug/deps/green_threads-90cef27627738653.55axrjyl6dc5fe0.rcgu.o: relocation R_X86_64_32S against undefined symbol `rsi' can not be used when making a PIE object; recompile with -fPIE
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error; 2 warnings emitted

error: could not compile `green_threads`

To learn more, run the command again with --verbose.
The terminal process "/bin/bash '-c', 'cargo run --package green_threads --bin green_threads'" terminated with exit code: 101.

Sometimes it works when you delete the target folder and try again.

1 Like

Glad to hear you enjoyed it.
The example works on my machine as well as in the playground (besides a few warnings since it seems I have to move to the new "asm!" syntax in naked functions soon...).

I would try what @alice suggested.

1 Like

Thanks for the suggestions! I've tried deleting the target folder, but that changed nothing. Then I copied the final code from the book into my file. (I've been implementing the code as I read the book instead of copying and pasting.) This worked.

Then I ran diff on my code and the final code from the book. Strangely, only comments and whitespace diffs exist. However, the code I copied from the book worked, but mine didn't. This is extremely strange. I'll try to figure out what went wrong and report here if I can see something.

Yeah, that's very strange. I'm curious what caused that. Please report back if you find out.

I just tried to copy all the code blocks from the previous chapter myself and it worked as it should (the closing bracket of the impl Runtime block was missing from the walkthrough though, so I added a reminder in the code snippet).

@L0uisc Ok, so I think I figured out what might have caused it. If I change the following lines in t_yield from:

llvm_asm!(
                "mov $0, %rdi
                 mov $1, %rsi"::"r"(old), "r"(new)
            );

to (notice the missing "%" in front of "rsi"):

llvm_asm!(
                "mov $0, %rdi
                 mov $1, rsi"::"r"(old), "r"(new)
            );

I get the same error:

relocation R_X86_64_32S against undefined symbol `rsi' can not be used when making a PIE object; recompile with -fPIC 

I guess that the same error would come if any "%" is missing from the switch function too.

If I'm not mistaken, this kind of invalid assembly gave a template error when compiling earlier. It seems LLVM now accepts it as a symbol which the linker thinks it should substitute with an absolute address and tries to calculate it by looking for the "rsi" symbol + an offset (I'm a bit on thin ice here when it comes to linking in ELF files so this is just speculation).

I'm not sure this is the actual error you experienced but if you still have the diff, look for missing "%" before "rsi" in the assembly.

1 Like

That was indeed the problem. Now I just need to figure out why diff didn't show that line as different... Thanks for the help!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.