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

ld.gold crashes or produces invalid executables when Musl is used as a libc #49071

Closed
albertov opened this issue Oct 24, 2018 · 29 comments · Fixed by #49704
Closed

ld.gold crashes or produces invalid executables when Musl is used as a libc #49071

albertov opened this issue Oct 24, 2018 · 29 comments · Fixed by #49704

Comments

@albertov
Copy link

albertov commented Oct 24, 2018

Issue description

ld.gold crashes or produces invalid executables when using the musl libc. This prevents the use of musl and GHC on armv7l (where it also manifests itself) since GHC requires ld.gold on that platform to work around https://sourceware.org/bugzilla/show_bug.cgi?id=16177

This also affects lld from LLVM 4 apparently

Steps to reproduce

nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideDerivation(_: {CFLAGS="-fuse-ld=gold";})'

The above command either fails (on a bare-metal x86_64 system) or produces an executable which segfaults at start (on a x86_64 system running on VM in a laptop). Interestingly, if one passes -static_libgcc the produced executable is apparently correct inside the VM but also fails to be created on the bare-metal machine.

Technical details

$ nix-info -m
 - system: `"x86_64-linux"`
 - host os: `Linux 4.14.78, NixOS, 18.09.1007.f8d156f44fb (Jellyfish)`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.1pre6377_954d1f4d`
 - channels(root): `"nixos-18.09.1007.f8d156f44fb"`
 - channels(alberto): `"nixos-18.03pre120031.7ebacd1a43, nixpkgs-18.03pre119383.eafd703a63"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`

The above system is the desktop bare-metal one

@albertov albertov changed the title ld.gold crashes or produces invalid executables when Musl is used as a libc ld.gold crashes or produces invalid executables when Musl is used as a libc Oct 24, 2018
@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

Just for clarity, is this the error you get when compiling on plain x86?

NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideDerivation(_: {CFLAGS="-fuse-ld=gold";})'
these derivations will be built:
  /nix/store/c7d5hz6ynzc6gyg69w2ja8jdx11537zl-hello-2.10.drv
these paths will be fetched (0.69 MiB download, 0.69 MiB unpacked):
  /nix/store/3x7dwzq014bblazs7kq20p9hyzz0qh8g-hello-2.10.tar.gz
copying path '/nix/store/3x7dwzq014bblazs7kq20p9hyzz0qh8g-hello-2.10.tar.gz' from 'https://cache.nixos.org'...
building '/nix/store/c7d5hz6ynzc6gyg69w2ja8jdx11537zl-hello-2.10.drv'...
unpacking sources
unpacking source archive /nix/store/3x7dwzq014bblazs7kq20p9hyzz0qh8g-hello-2.10.tar.gz
source root is hello-2.10
setting SOURCE_DATE_EPOCH to timestamp 1416139241 of file hello-2.10/ChangeLog
patching sources
configuring
configure flags: --disable-dependency-tracking --prefix=/nix/store/wdl72bp6a41iiqv5ph37nwqbhx1sf825-hello-2.10
checking for a BSD-compatible install... /nix/store/bxwz6wdxr0lgj9c629azmxi35i68qrv2-coreutils-8.29/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /nix/store/bxwz6wdxr0lgj9c629azmxi35i68qrv2-coreutils-8.29/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... configure: error: in `/tmp/nix-build-hello-2.10.drv-0/hello-2.10':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details
builder for '/nix/store/c7d5hz6ynzc6gyg69w2ja8jdx11537zl-hello-2.10.drv' failed with exit code 1

@albertov
Copy link
Author

albertov commented Oct 25, 2018 via email

@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

I see, in config.log:

configure:4154: checking whether we are cross compiling
configure:4162: gcc -o conftest -fuse-ld=gold   conftest.c  >&5
configure:4166: $? = 0
configure:4173: ./conftest
./configure: line 4175: 14393 Segmentation fault      ./conftest$ac_cv_exeext
configure:4177: $? = 139
configure:4184: error: in `/tmp/nix-build-hello-2.10.drv-0/hello-2.10':
configure:4186: error: cannot run C compiled programs.

@albertov
Copy link
Author

albertov commented Oct 25, 2018 via email

@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

Are you running it inside a VM?

No, I'm running on Nix on a standard Ubuntu 16.04 64-bit.

@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

Related resources:

Looking at nm output like done in that thread:

niklas@ares:/tmp/nix-build-hello-2.10.drv-0/hello-2.10/src$ ls
core  hello.c  system.h  test.c
niklas@ares:/tmp/nix-build-hello-2.10.drv-0/hello-2.10/src$ cat test.c 
int main(int argc, const char *argv[]) {
  return 0;
}
niklas@ares:/tmp/nix-build-hello-2.10.drv-0/hello-2.10/src$ gcc -o hello.bfd test.c
niklas@ares:/tmp/nix-build-hello-2.10.drv-0/hello-2.10/src$ gcc -o hello.gold -fuse-ld=gold test.c
niklas@ares ...build-hello-2.10.drv-0/hello-2.10/src % nm -D conftest-gold
0000000000402008 D __bss_start
                 w __deregister_frame_info
0000000000402008 D _edata
0000000000402070 D _end
00000000004007e2 T _fini
00000000004005b8 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 U __libc_start_main
                 w __register_frame_info
niklas@ares ...build-hello-2.10.drv-0/hello-2.10/src % nm -D conftest-bfd
0000000000601008 B __bss_start
0000000000601008 D _edata
0000000000601070 B _end
00000000004006e2 T _fini
00000000004004e8 T _init
                 U __libc_start_main

@dtzWill
Copy link
Member

dtzWill commented Oct 25, 2018

FWIW it seems to work with clang, which is curious:

NIX_PATH=nixpkgs=channel:nixos-unstable nix-build -E 'with (import <nixpkgs> {}).pkgsMusl; (pkgs.hello.override { inherit (llvmPackages_6) stdenv; }).overrideAttrs (o:{ CFLAGS="-fuse-ld=gold"; NIX_DEBUG=1;})'

As for using gold, may want to try something like: 37b2f59

Or otherwise ensuring our wrappers always know what linker to invoke. I don't think this is the problem, but something to consider for future gold usage once this is resolved :).

@dtzWill
Copy link
Member

dtzWill commented Oct 25, 2018

The thread mentions building with -fPIE, which is interesting because with NIX_DEBUG=1 I'm seeing this in the log:

HARDENING: disabled flags: pie
HARDENING: Is active (not completely disabled with "all" flag)
HARDENING: enabling fortify
HARDENING: enabling stackprotector
HARDENING: enabling strictoverflow
HARDENING: enabling format
HARDENING: enabling pic

Which may be related? It's disabled on non-musl too, FWIW.

@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

Adding -pie to LDFLAGS makes it work:

NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideDerivation(_: {CFLAGS="-fuse-ld=gold"; LDFLAGS="-pie";})'

Adding -fPIE to CFLAGS doens't help, it seems to be removed (perhaps by the hardening)?

@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

Also does anybody have an idea why

NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideDerivation(_: {CFLAGS="-fuse-ld=gold -fPIE"; hardeningDisable = [ "all" ];})'

doesn't work (using CFLAGS="-fuse-ld=gold -fPIE" and hardeningDisable = [ "all" ])?

@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

seems to be removed (perhaps by the hardening)?

Just asked @fpletz at NixCon, he said that hardening should only add flags, not remove them; @cleverca22 said that happens here: https://github.com/NixOS/nixpkgs/blob/62feeed1a5aaf23b8c0883ae0cc17cd913cdce56/pkgs/build-support/cc-wrapper/add-hardening.sh

So not sure why -fPIE doesn't translate to -pie to the linker.

@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

So not sure why -fPIE doesn't translate to -pie to the linker.

With help of @cleverca22 and looking at

pie)
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling CFlags -fPIE >&2; fi
hardeningCFlags+=('-fPIE')
if [[ ! ("$*" =~ " -shared " || "$*" =~ " -static ") ]]; then
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling LDFlags -pie >&2; fi
hardeningCFlags+=('-pie')

It seems you need to pass both -pie and -fPIE to the compiler; see man gcc:

       -pie
           Produce a position independent executable on targets that support it.
           For predictable results, you must also specify the same set of
           options used for compilation (-fpie, -fPIE, or model suboptions) when
           you specify this linker option.
...
       -fpie
       -fPIE
           These options are similar to -fpic and -fPIC, but generated position
           independent code can be only linked into executables.  Usually these
           options are used when -pie GCC option is used during linking.

           -fpie and -fPIE both define the macros "__pie__" and "__PIE__".  The
           macros have the value 1 for -fpie and 2 for -fPIE.

And indeed

NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideDerivation(_: {CFLAGS="-fuse-ld=gold -fPIE -pie";})'

works.

@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

I'd expect that hardeningEnable = ["pie"]; should also work, but it doesn't:

NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideDerivation(_: {CFLAGS="-fuse-ld=gold"; hardeningEnable = ["pie"]; })'

Contrary to expectations, this doesn't seem to add the pie flags.

@dtzWill
Copy link
Member

dtzWill commented Oct 25, 2018 via email

@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

It works if you use overrideAttrs instead! \o/

Oh right, this works!

NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideAttrs(_: {CFLAGS="-fuse-ld=gold"; hardeningEnable = ["pie"]; })'                  

@dtzWill Should we add something to the musl bits that detects if gold is used and passes pie accordingly?

Also, I assume there should be some upstream gold bug filed somewhere? The musl thread didn't link any being filed.

@albertov
Copy link
Author

The command which worked for @nh2 :

NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideDerivation(_: {CFLAGS="-fuse-ld=gold -fPIE -pie";})'

Failed in my machine :( Same error as always:

checking whether the C compiler works... no
configure: error: in `/build/hello-2.10':
configure: error: C compiler cannot create executables
See `config.log' for more details
builder for '/nix/store/g6m7i019af8p838ldzv4jlfxds40fw2g-hello-2.10.drv' failed with exit code 77
error: build of '/nix/store/g6m7i019af8p838ldzv4jlfxds40fw2g-hello-2.10.drv' failed

@dtzWill
Copy link
Member

dtzWill commented Oct 25, 2018 via email

@albertov
Copy link
Author

Didn't know about overrideAttrs (thanks for letting me know!). Anyway, I get the same error with overrideAttrs too, this is what I'm running and the output:

3$ NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideAttrs(_: {CFLAGS="-fuse-ld=gold -fPIE -pie";})'
these derivations will be built:
  /nix/store/g6m7i019af8p838ldzv4jlfxds40fw2g-hello-2.10.drv
building '/nix/store/g6m7i019af8p838ldzv4jlfxds40fw2g-hello-2.10.drv'...
unpacking sources
unpacking source archive /nix/store/3x7dwzq014bblazs7kq20p9hyzz0qh8g-hello-2.10.tar.gz
source root is hello-2.10
setting SOURCE_DATE_EPOCH to timestamp 1416139241 of file hello-2.10/ChangeLog
patching sources
configuring
configure flags: --disable-dependency-tracking --prefix=/nix/store/rmwms3g2v6isnswxlm1w799vb82zfp24-hello-2.10
checking for a BSD-compatible install... /nix/store/bxwz6wdxr0lgj9c629azmxi35i68qrv2-coreutils-8.29/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /nix/store/bxwz6wdxr0lgj9c629azmxi35i68qrv2-coreutils-8.29/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... no
configure: error: in `/build/hello-2.10':
configure: error: C compiler cannot create executables
See `config.log' for more details
builder for '/nix/store/g6m7i019af8p838ldzv4jlfxds40fw2g-hello-2.10.drv' failed with exit code 77
error: build of '/nix/store/g6m7i019af8p838ldzv4jlfxds40fw2g-hello-2.10.drv' failed

The other command which worked for you, with hardeningEnable, also fails:

$ NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideAttrs(_: {CFLAGS="-fuse-ld=gold"; hardeningEnable = ["pie"]; })'                  
these derivations will be built:
  /nix/store/qllg7c478rnba15rzh73va3adlw88zhk-hello-2.10.drv
building '/nix/store/qllg7c478rnba15rzh73va3adlw88zhk-hello-2.10.drv'...
unpacking sources
unpacking source archive /nix/store/3x7dwzq014bblazs7kq20p9hyzz0qh8g-hello-2.10.tar.gz
source root is hello-2.10
setting SOURCE_DATE_EPOCH to timestamp 1416139241 of file hello-2.10/ChangeLog
patching sources
configuring
configure flags: --disable-dependency-tracking --prefix=/nix/store/2k3lazi3vkm73gm033dxnlc8svasnzid-hello-2.10
checking for a BSD-compatible install... /nix/store/bxwz6wdxr0lgj9c629azmxi35i68qrv2-coreutils-8.29/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /nix/store/bxwz6wdxr0lgj9c629azmxi35i68qrv2-coreutils-8.29/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... no
configure: error: in `/build/hello-2.10':
configure: error: C compiler cannot create executables
See `config.log' for more details
builder for '/nix/store/qllg7c478rnba15rzh73va3adlw88zhk-hello-2.10.drv' failed with exit code 77
error: build of '/nix/store/qllg7c478rnba15rzh73va3adlw88zhk-hello-2.10.drv' failed

My computer must be cursed :(

@nh2
Copy link
Contributor

nh2 commented Oct 25, 2018

Is this now also a plain x86_64 machine or something else?

@albertov
Copy link
Author

Yes, it's a plain x86_64 machine running Nixos on bare hardware.

@albertov
Copy link
Author

albertov commented Oct 26, 2018

For the record, inside a nixos VM in a laptop, passing hardeningEnable=["pie"] makes it build correctly and the executable works. Without hardeningEnable (the original command from the description) the executable segfaults.

I can't understand the discrepancies between machines, both are x86_64 systems. The laptop is a macbook pro while the desktop has this processor according to /proc/cpuinfo:

processor       : 15
vendor_id       : GenuineIntel
cpu family      : 6
model           : 79
model name      : Intel(R) Core(TM) i7-6900K CPU @ 3.20GHz
stepping        : 1
microcode       : 0xb00001c
cpu MHz         : 3499.257
cache size      : 20480 KB
physical id     : 0
siblings        : 16
core id         : 7
cpu cores       : 8
apicid          : 15
initial apicid  : 15
fpu             : yes
fpu_exception   : yes
cpuid level     : 20
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap intel_pt xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf
bogomips        : 6398.64
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 48 bits virtual
power management:

I'll try to run it on other machines today to see if I can spot a pattern

@albertov
Copy link
Author

This is /proc/cpuinfo from the laptop:

processor	: 3
vendor_id	: GenuineIntel
cpu family	: 6
model		: 70
model name	: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
stepping	: 1
microcode	: 0x19
cpu MHz		: 2194.268
cache size	: 6144 KB
physical id	: 6
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 6
initial apicid	: 6
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc cpuid aperfmperf pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm cpuid_fault epb invpcid_single pti tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt dtherm ida arat pln pts
bugs		: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf
bogomips	: 4388.53
clflush size	: 64
cache_alignment	: 64
address sizes	: 42 bits physical, 48 bits virtual
power management:

@nh2
Copy link
Contributor

nh2 commented Oct 26, 2018

That is pretty odd. Perhaps comparing the full config.log output gives a hint (ideally also with NIX_DEBUG=1 set)?

@albertov
Copy link
Author

albertov commented Oct 28, 2018

@nh2 I will try that and post the diff if different. I meanwhile tried to compile using clang 6 with:

NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'with (import <nixpkgs> {}).pkgsMusl; (pkgs.hello.override { inherit (llvmPackages_6) stdenv; }).overrideAttrs (o:{ CFLAGS="-fuse-ld=gold"; NIX_DEBUG=1;})'

but llvm fails to build since the gold tests fail with a very similar error:

<....snip....>
Command Output (stderr):
--
/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: fatal error: /build/llvm/build/test/tools/gold/X86/Output/visibility.ll.tmp.so: No error information

--

********************
Testing: 0 .. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..
Testing Time: 165.74s
********************
Failing Tests (34):
    LLVM :: tools/gold/PowerPC/mtriple.ll
    LLVM :: tools/gold/X86/asm_undefined.ll
    LLVM :: tools/gold/X86/asm_undefined2.ll
    LLVM :: tools/gold/X86/bcsection.ll
    LLVM :: tools/gold/X86/cache.ll
    LLVM :: tools/gold/X86/comdat.ll
    LLVM :: tools/gold/X86/common_thinlto.ll
    LLVM :: tools/gold/X86/emit-llvm.ll
    LLVM :: tools/gold/X86/global_with_section.ll
    LLVM :: tools/gold/X86/mixed_lto.ll
    LLVM :: tools/gold/X86/module_asm.ll
    LLVM :: tools/gold/X86/multiple-data.s
    LLVM :: tools/gold/X86/multiple-sections.ll
    LLVM :: tools/gold/X86/opt-level.ll
    LLVM :: tools/gold/X86/parallel.ll
    LLVM :: tools/gold/X86/pr19901.ll
    LLVM :: tools/gold/X86/pr19901_thinlto.ll
    LLVM :: tools/gold/X86/pr25907.ll
    LLVM :: tools/gold/X86/relax-relocs.ll
    LLVM :: tools/gold/X86/relocatable.ll
    LLVM :: tools/gold/X86/relocation-model-pic.ll
    LLVM :: tools/gold/X86/slp-vectorize.ll
    LLVM :: tools/gold/X86/strip_names.ll
1 warning(s) in tests.

    LLVM :: tools/gold/X86/thinlto.ll
    LLVM :: tools/gold/X86/thinlto_afdo.ll
    LLVM :: tools/gold/X86/thinlto_archive.ll
    LLVM :: tools/gold/X86/thinlto_funcimport.ll
    LLVM :: tools/gold/X86/thinlto_internalize.ll
    LLVM :: tools/gold/X86/thinlto_linkonceresolution.ll
    LLVM :: tools/gold/X86/thinlto_weak_library.ll
    LLVM :: tools/gold/X86/thinlto_weak_resolution.ll
    LLVM :: tools/gold/X86/type-merge2.ll
    LLVM :: tools/gold/X86/vectorize.ll
    LLVM :: tools/gold/X86/visibility.ll

  Expected Passes    : 22394
  Expected Failures  : 140
  Unsupported Tests  : 721
  Unexpected Failures: 34
make[3]: *** [CMakeFiles/check-all.dir/build.make:58: CMakeFiles/check-all] Error 1
make[3]: Leaving directory '/build/llvm/build'
make[2]: *** [CMakeFiles/Makefile2:356: CMakeFiles/check-all.dir/all] Error 2
make[2]: Leaving directory '/build/llvm/build'
make[1]: *** [CMakeFiles/Makefile2:363: CMakeFiles/check-all.dir/rule] Error 2
make[1]: Leaving directory '/build/llvm/build'
make: *** [Makefile:251: check-all] Error 2
builder for '/nix/store/qsgcngh2m66k188d1nmqsmdy8z3j312i-llvm-6.0.1.drv' failed with exit code 2

@albertov
Copy link
Author

I've compared the config.logs of the following command on both machines:

NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build -E 'let pkgs = (import <nixpkgs> {}).pkgsMusl; in pkgs.hello.overrideAttrs(_: {NIX_DEBUG="1"; CFLAGS="-fuse-ld=gold -Wl,--debug=all"; hardeningEnable = ["pie"]; postInstall="exit -1";})' -K

(the postIntall is to intentionally kill the build before it finishes correctly to force the temporary build dir to be preserved in the machine where it works properly)

If I understand it correctly, the "broken" ld.gold has some problem processing libgcc.a since when both logs begin to differ significantly is where the good version starts running task Add_archive_symbols while the bad version never reached that phase.

The beginning of the diff looks like this:

--- hello-config-bad.log    2018-10-28 21:27:33.391979234 +0100
+++ hello-config-good.log 2018-10-28 21:22:29.845499594 +0100
@@ -12,9 +12,9 @@
 
 hostname = localhost
 uname -m = x86_64
-uname -r = 4.14.78
+uname -r = 4.14.76
 uname -s = Linux
-uname -v = #1-NixOS SMP Sat Oct 20 07:48:54 UTC 2018
+uname -v = #1-NixOS SMP Sat Oct 13 07:27:30 UTC 2018
 
 /usr/bin/uname -p = unknown
 /bin/uname -X     = unknown
@@ -188,7 +188,7 @@
   -plugin
   /nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/libexec/gcc/x86_64-unknown-linux-musl/7.3.0/liblto_plugin.so
   -plugin-opt=/nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/libexec/gcc/x86_64-unknown-linux-musl/7.3.0/lto-wrapper
-  -plugin-opt=-fresolution=/build/cciodIpK.res
+  -plugin-opt=-fresolution=/build/ccbNLFDA.res
   -plugin-opt=-pass-through=-lgcc
   -plugin-opt=-pass-through=-lgcc_s
   -plugin-opt=-pass-through=-lc
@@ -214,7 +214,7 @@
   -dynamic-linker
   /nix/store/35mb6imifzx9316y7nknhh6apx1k7a24-musl-1.1.19/lib/ld-musl-x86_64.so.1
   --debug=all
-  /build/cceIfKHc.o
+  /build/ccnaDnPl.o
   -rpath
   /nix/store/xsngrd6w0r2aaqc3fdzyi16a4hpndwa2-hello-2.10/lib64
   -rpath
@@ -279,14 +279,14 @@
 /nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 completed task Read_symbols /nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/crtbeginS.o (user: 0.000000 sys: 0.000000 wall: 0.000000)
 /nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 running   task Add_symbols /nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/crtbeginS.o
 /nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 completed task Add_symbols /nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/crtbeginS.o (user: 0.000000 sys: 0.000000 wall: 0.000000)
-/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 running   task Read_symbols /build/cceIfKHc.o
-/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Opened new descriptor 9 for "/build/cceIfKHc.o"
-/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Attempt to open /build/cceIfKHc.o succeeded
-/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Unlocking file "/build/cceIfKHc.o"
-/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Released descriptor 9 for "/build/cceIfKHc.o"
-/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 completed task Read_symbols /build/cceIfKHc.o (user: 0.000000 sys: 0.000000 wall: 0.000000)
-/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 running   task Add_symbols /build/cceIfKHc.o
-/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 completed task Add_symbols /build/cceIfKHc.o (user: 0.000000 sys: 0.000000 wall: 0.000000)
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 running   task Read_symbols /build/ccnaDnPl.o
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Opened new descriptor 9 for "/build/ccnaDnPl.o"
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Attempt to open /build/ccnaDnPl.o succeeded
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Unlocking file "/build/ccnaDnPl.o"
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Released descriptor 9 for "/build/ccnaDnPl.o"
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 completed task Read_symbols /build/ccnaDnPl.o (user: 0.000000 sys: 0.000000 wall: 0.000000)
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 running   task Add_symbols /build/ccnaDnPl.o
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 completed task Add_symbols /build/ccnaDnPl.o (user: 0.000000 sys: 0.000000 wall: 0.000000)
 /nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 running   task Read_symbols /nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/crtendS.o
 /nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Opened new descriptor 10 for "/nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/crtendS.o"
 /nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Attempt to open /nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/crtendS.o succeeded
@@ -388,7 +388,25677 @@
 /nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Attempt to open /nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/libgcc.a succeeded
 /nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Unlocking file "/nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/libgcc.a"
 /nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Released descriptor 11 for "/nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/libgcc.a"
-/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 completed task Read_symbols -lgcc (user: 0.000000 sys: 0.010000 wall: 0.000000)
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 completed task Read_symbols -lgcc (user: 0.000000 sys: 0.000000 wall: 0.000000)
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 running   task Add_archive_symbols /nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/libgcc.a
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 completed task Add_archive_symbols /nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/libgcc.a (user: 0.000000 sys: 0.000000 wall: 0.000000)
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold:   0 running   task Read_symbols -lgcc_s
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Attempt to open /nix/store/35mb6imifzx9316y7nknhh6apx1k7a24-musl-1.1.19/lib/libgcc_s.so failed
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Attempt to open /nix/store/35mb6imifzx9316y7nknhh6apx1k7a24-musl-1.1.19/lib/libgcc_s.a failed
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Closed descriptor 11 for "/nix/store/4k79vw3p32n2w7z11d9rh4788chq73ja-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/libgcc.a"
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Opened new descriptor 12 for "/nix/store/4dw6k7sdkw8r2b5rgka8r659451l3xx9-gcc-7.3.0-lib/lib/libgcc_s.so"
+/nix/store/8jpp32r1yqzqcgjycnvjy0hm30ng2f86-binutils-2.30/bin/ld.gold: Attempt to open /nix/store/4dw6k7sdkw8r2b5rgka8r659451l3xx9-gcc-7.3.0-lib/lib/libgcc_s.so succeeded

@matthewbauer
Copy link
Member

matthewbauer commented Oct 31, 2018

"pie" hardening is not enabled by default. Maybe it should be?

@nh2
Copy link
Contributor

nh2 commented Nov 3, 2018

"pie" hardening is not enabled by default. Maybe it should be?

@matthewbauer The way I read the links is that gold for unknown reasons (likely a bug) doesn't work with musl unless pie is activated.

Perhaps it should be enabled by default for this combination (gold + musl)?

@matthewbauer
Copy link
Member

Yeah sounds like a good idea. We don't know whether you will use ld.gold ahead of time, so better to just do it with musl everywhere.

matthewbauer added a commit to matthewbauer/nixpkgs that referenced this issue Nov 3, 2018
Fixes NixOS#49071

On ld.gold, we produce broken executables when linking with the Musl
libc. This appears to be a known bug when using ld.gold and Musl. This
thread describes the workaround as enabling PIE when using ld.gold and
Musl:

https://www.openwall.com/lists/musl/2015/05/01/5

By default we don’t enable PIE to avoid breaking things. But in the
Musl case we are breaking things by not enabling PIE. So this adds a
special case for defaultHardeningFlags which keeps the pie hardening
for everything. Any packages that break with PIE can add the pie flag
to disableHardeningFlags array (a no-op for now on anything but Musl).
@nh2
Copy link
Contributor

nh2 commented Nov 4, 2018

I've filed an upstream issue for gold at https://sourceware.org/bugzilla/show_bug.cgi?id=23856

I recommend everyone involved subscribing to it.

globin pushed a commit that referenced this issue Nov 7, 2018
Fixes #49071

On ld.gold, we produce broken executables when linking with the Musl
libc. This appears to be a known bug when using ld.gold and Musl. This
thread describes the workaround as enabling PIE when using ld.gold and
Musl:

https://www.openwall.com/lists/musl/2015/05/01/5

By default we don’t enable PIE to avoid breaking things. But in the
Musl case we are breaking things by not enabling PIE. So this adds a
special case for defaultHardeningFlags which keeps the pie hardening
for everything. Any packages that break with PIE can add the pie flag
to disableHardeningFlags array (a no-op for now on anything but Musl).
adamse added a commit to adamse/nixpkgs that referenced this issue Apr 8, 2020
peti pushed a commit that referenced this issue Apr 10, 2020
nomeata pushed a commit to dfinity-lab/nixpkgs that referenced this issue Jun 25, 2020
ld.gold doesn't play well with musl as is documented in NixOS#49071 and
https://sourceware.org/bugzilla/show_bug.cgi?id=23856

(cherry picked from commit 4675649)
nh2 pushed a commit to nh2/nixpkgs that referenced this issue Jul 23, 2020
ld.gold doesn't play well with musl as is documented in NixOS#49071 and
https://sourceware.org/bugzilla/show_bug.cgi?id=23856

(cherry picked from commit 4675649)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants