f0rmiga / gcc-toolchain

A fully-hermetic Bazel GCC toolchain for Linux.
Apache License 2.0
103 stars 24 forks source link

[Bug]: Hermetic GCC toolchain and @io_bazel_rules_go produce warnings building debug-mode shared libraries #160

Open rbosetti opened 10 months ago

rbosetti commented 10 months ago

What happened?

What happened: linking a Cgo library into a Go binary produces a warning about incorrect use of DT_TEXTREL in a PIE target. What I expected: no warnings, because consistent link strategies are used for intermediate and final results.

A Go package that uses cgo, compiled as a library in debug mode, will normally produce a .gopclntab that requires relocations. When using the standard Go rules with a system C toolchain on various flavors of Ubuntu, cgo=True libraries are built such that .gopclntab and other relocated read-only sections are prefixed with .data.relro. It appears that some interaction between the Aspect GCC toolchain and the Go rules is leading to a situation where intermediate .o files for Go builds are using internal linkage, while the final link step is using GNU ld as an external linker.

That conflict causes warnings and has in the past been a sign of binaries with executable data sections that need to be specially handled by the system loader. Notably, various musl-based systems are not able to correctly load such binaries.

One possible cause here is that the PIE-by-default nature of the GCC toolchain is not correctly detected by the Go rules.

See golang/go issue 10914, 17847, and 59866 for related discussions.

Version

Development (host) and target OS/architectures: Host is Ubuntu 22.04.3 LTS, uname -srv Linux 6.2.0-1018-gcp #20~22.04.1-Ubuntu SMP Mon Oct 23 12:29:43 UTC 2023 Target is Linux x86-64, with similar kernel revisions

Output of bazel --version: bazel 6.4.0

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file: @io_bazel_rules_go = v0.43.0, Go toolchain = 1.20.2, Aspect GCC toolchain = 0.4.2

Complete repo available at https://github.com/rbosetti/aspect-repro

Language(s) and/or frameworks involved: Go, C

How to reproduce

`bazel build //:demobin` from the workspace in https://github.com/rbosetti/aspect-repro

Any other information?

Output from the repro step.

INFO: Analyzed target //:demobin (55 packages loaded, 16855 targets configured).
INFO: Found 1 target...
INFO: From GoLink demobin_/demobin:
/home/rlb/.cache/bazel/_bazel_rlb/5d655e0d5670b9e1307c236b8abb2e79/external/gcc_toolchain_x86_64_files/bin/x86_64-linux-ld: /tmp/go-link-2297262172/go.o: warning: relocation in read-only section `.gopclntab'
/home/rlb/.cache/bazel/_bazel_rlb/5d655e0d5670b9e1307c236b8abb2e79/external/gcc_toolchain_x86_64_files/bin/x86_64-linux-ld: warning: creating DT_TEXTREL in a PIE
Target //:demobin up-to-date:
  bazel-bin/demobin_/demobin
INFO: Elapsed time: 191.332s, Critical Path: 166.08s
INFO: 13 processes: 7 internal, 6 linux-sandbox.
INFO: Build completed successfully, 13 total actions
f0rmiga commented 7 months ago

Thank you for the complete report and repro. I'll look into this.