14

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.

XXO2
  • 184
  • 1
  • 1
  • 5
  • When linking, use the linker directly instead of calling the GCC front-end program. I.e. `i686-elf-ld` instead. – Some programmer dude Mar 13 '16 at 13:35
  • 1
    it might help to add "-v" flag to gcc command-line to see all paths. – max630 Mar 13 '16 at 13:39
  • @JoachimPileborg When I try `i686-elf-ld` command, it says it cannot find command. Any other suggestions? @max630 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? – XXO2 Mar 13 '16 at 15:57
  • 1
    You have built a binutils package for the cross-compilation target? And installed it at the same location as the compiler? – Some programmer dude Mar 13 '16 at 17:00
  • @JoachimPileborg After I found collect2 program source I went through it and I don't think that source and program output match. Any ideas? – XXO2 Mar 16 '16 at 17:41
  • 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](http://stackoverflow.com/questions/3476093/replacing-ld-with-gold-any-experience). 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. – benzeno Dec 08 '16 at 22:56
  • 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. – BrockLee Jun 04 '22 at 23:35

4 Answers4

6

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

Lord Boval
  • 69
  • 1
  • 2
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.

Info5ek
  • 1,227
  • 4
  • 17
  • 25
  • After reverting the (shared) Virtual Machine and trying this again, strangely it didn't work. Bizarre... – Info5ek Feb 09 '17 at 23:29
  • 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. – Jaquarh Dec 26 '21 at 06:08
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.

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.

Nicola Ben
  • 10,615
  • 8
  • 41
  • 65