Gcc collect2: fatal error: cannot find 'ld'
Asked Answered
L

7

16

I'm going through the tutorial on making an OS on http://wiki.osdev.org/Bare_Bones. When I try to link boot.o and kernel.o using this command: i686-elf-gcc -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc , I just get this error:

collect2: fatal error: cannot find 'ld'
compilation terminated.

I just installed fresh Ubuntu 15.10 that with gcc-5.2.1 and binutils-2.25.1 . I have searched the internet for answers but nothing helped.

Lita answered 13/3, 2016 at 13:28 Comment(7)
When linking, use the linker directly instead of calling the GCC front-end program. I.e. i686-elf-ld instead.Siding
it might help to add "-v" flag to gcc command-line to see all paths.Katherine
@JoachimPileborg When I try i686-elf-ld command, it says it cannot find command. Any other suggestions? @Katherine It says this: COLLECT_GCC=i686-elf-gcc COLLECT_LTO_WRAPPER=$HOME/opt/cross/libexec/gcc/i686-elf/5.2.0/lto-wrapper Target: i686-elf Configured with: ../gcc-5.2.0/configure --target=i686-elf --prefix=$HOME/opt/cross --disable-nls --enable-languages=c,c++ --without-headers Thread model: single gcc version 5.2.0 (GCC) So, what exactly am I trying to see?Lita
You have built a binutils package for the cross-compilation target? And installed it at the same location as the compiler?Siding
@JoachimPileborg After I found collect2 program source I went through it and I don't think that source and program output match. Any ideas?Lita
I have a similar problem with an ARM toolchain. Maybe it is the whole gold vs bfd linker thing. See this SO question on the gold linker. In the accepted answer, it is mentioned the gold linker has problems linking Linux kernel stuff. Your kernel isn't Linux obviously but it may be related. Try -fuse-ld=bfd when calling gcc to change to the bfd linker.Loft
In case anyone stumbles upon this and is instead using a clang toolchain, I've been able to fix this problem by adding my toolchain to PATH. If you add -debug flag to the lowest collect2 invocation, I saw that it was not actually looking for the binary ld, but instead a bunch of other ld-like names (real-ld, collect-ld, ld.lld, etc.) It just so happens that I wanted to use ld.lld provided in my toolchain, so adding it to the search PATH allowed collect2 to use my toolchain's ld.lld.Andvari
D
5

I also got once the same error during a pentest while I was trying to compile my exploit on the victim server.

In my case, the directory where the "ld" program was located had not been defined in the PATH environment variable, so I simply added it.

eg. export PATH=$PATH:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin

Decarburize answered 29/10, 2019 at 7:51 Comment(0)
D
2

I tried to install the following packages, and it works for me.

sudo zypper install binutils binutils-devel binutils-gold
Doro answered 19/12, 2023 at 6:38 Comment(2)
How is zypper relative to the question?Gonick
Fixes cargo-afl for me. Somehow, cargo-afl managed to invoke gcc in some way that made it not find ld. That despite Gcc working fine, and the binary I'm trying to fuzz doesn't even have any C dependencies. @273K, Zypper is OpenSuse's package manager. Debian users would translate zypper to apt and devel to dev. For once, it wasn't me that had to translate an apt command to a zypper command.Herbage
T
0

I had this error while hacking a remote machine and trying to use gcc to compile an exploit on the victim machine.

I simply copied the program ld to /tmp/, the working directory where i was compiling my exploit exploit.c by running cp /usr/bin/ld /tmp/ld followed by the original gcc compile command and the compile worked.

Turgor answered 9/2, 2017 at 23:2 Comment(2)
After reverting the (shared) Virtual Machine and trying this again, strangely it didn't work. Bizarre...Turgor
It isn't Bizarre, the tmp by default is not in the environmental path. Someone would of used export PATH=$PATH+:/tmp which then lets the OS find your binary.Bertle
D
0

I searched a lot to fix this issue and nothing worked but lastly i uninstalled MinGw and reinstalled it then did edited the environment variables again and then uninstalled and reinstalled vs code extensions then it worked.

Dioscuri answered 7/4, 2022 at 14:41 Comment(0)
R
0

Today I had your same error about ld missing command.

First of all I install build-essential package (debian) for my arm64 architecture.

Then I create a symbolic link to /usr/bin/lld-11 file:

sudo ln -s /usr/bin/lld-11 /usr/bin/ld

Now my rust environment works like a charm.

Rotator answered 12/8, 2023 at 11:22 Comment(0)
P
0

remove -g -O2 and try again, if works for me at CGO

Pernick answered 10/10, 2023 at 8:43 Comment(0)
S
0

For me the issue was with LDFLAGS env variable set for another linker (-fuse-ld=lld). Solution was to unset variable:

unset LDFLAGS
Subcommittee answered 8/3 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.