Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way of linking libgcc statically #29527

Open
taralx opened this issue Nov 2, 2015 · 10 comments
Open

Provide a way of linking libgcc statically #29527

taralx opened this issue Nov 2, 2015 · 10 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@taralx
Copy link
Contributor

taralx commented Nov 2, 2015

gcc et al provide "-static-libgcc" for platforms where a link to libgcc_s.so is undesirable. I need similar functionality in rustc because the cloud I'm deploying to doesn't have that in its runtime.

See issue #29482 for some background.

/cc @alexcrichton

@alexcrichton
Copy link
Member

I'd personally be interested in just doing this unconditionally, I don't recall if there's a technical reason to link to the dynamic version beyond "just because", but there may be a good reason to!

@taralx
Copy link
Contributor Author

taralx commented Nov 3, 2015

I seem to remember that libgcc keeps some singleton state around, so there's questions of how dynlibs avoid getting their own state. I could be wrong.

@ianlancetaylor
Copy link

Once upon a time, it was necessary to use libgcc_s in order to throw a C++ exception from one shared library to another. That is no longer true on systems that use the GNU linker or gold. See http://www.airs.com/blog/archives/166 for background.

@steveklabnik
Copy link
Member

Triage: no changes i'm aware of

@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 24, 2017
@sanmai-NL
Copy link

@alexcrichton: Could you please assess how hard it would be to implement this / provide instructions if you prefer for others to pick it up? Thanks.

@alexcrichton
Copy link
Member

Sorry I don't think I understand enough about how this change would be done in Rust to write up such instructions or evaluate it.

@jonas-schievink jonas-schievink added T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) and removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-build labels Apr 21, 2019
@nehaljwani
Copy link

After inspecting the output from ...

$ diff <(echo 'int main() {}' | g++ -static-libgcc --verbose -xc++ - 2>&1 | tr ' ' '\n') \
       <(echo 'int main() {}' | g++ --verbose -xc++ - 2>&1 | tr ' ' '\n')
...
269a270
> -lgcc_s
271d271
< -lgcc_eh
272a273
> -lgcc_s
274d274
< -lgcc_eh
277,278c277,278
...

... a plausible workaround for this issue (tested on Linux only) could be:

$ cat linkerdriver.wrap 
#!/bin/bash

for arg do
  shift
  case $arg in
    (-lgcc_s) : ;;
       (*) set -- "$@" "$arg" ;;
  esac
done

# replace 'cc' with the linker driver you actually want to invoke (most likely $CC)
exec cc "$@" -lgcc_eh -lc

$ # Change linker env var according to your target
$ CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=$PWD/linkerdriver.wrap cargo build --verbose --package <my-package>

$ ldd target/debug/my-binary
	linux-vdso.so.1 (0x00007ffcd291f000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f81c9656000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f81c9507000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f81c9501000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f81c930f000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f81ca741000)

@Ralith
Copy link
Contributor

Ralith commented Oct 21, 2021

The current version of the Steam runtime provides a build of GCC 9 that does not include a dynamic libgcc at all, so this would be nice for being able to use a consistent, modern gcc build in that environment.

@nehaljwani's workaround, slightly tweaked to replace -lgcc_eh -lc with -lgcc, got steam runtime gcc 9 working for me.

@jyn514
Copy link
Member

jyn514 commented May 24, 2023

This looks like a general request for the binaries rustc generates, it's not specific to bootstrapping.

@rustbot label -T-bootstrap

@rustbot rustbot removed the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label May 24, 2023
@jyn514 jyn514 added A-linkage Area: linking into static, shared libraries and binaries T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 24, 2023
@TheComputerGuy96
Copy link

TheComputerGuy96 commented Jan 26, 2025

a plausible workaround for this issue (tested on Linux only) could be:

Here's a (much) more simplified version of this workaround wrapper using well-known Bashisms:

#!/bin/bash

exec "${CC:-cc}" "${@/-lgcc_s/-lgcc_eh}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests