Search code examples
linux-kerneldebiancross-compilingld

Cross-compiling kernel tools, cannot find -lelf even with -L set correctly


I'm trying to cross-compile a modified version of the official Debian kernel for armhf on my amd64 machine. I'm using the HowToCrossBuildAnOfficialDebianKernelPackage instructions on the Debian wiki.

The kernel itself builds fine, but I get an error from ld when trying to build objtool for the linux-kbuild package:

# make -f debian/rules.real build-kbuild KBUILD_HOSTLDFLAGS="-L/usr/lib/arm-linux-gnueabihf"
[...]
arm-linux-gnueabihf-gcc /usr/src/linux.buster-backports/debian/build/build-tools/tools/objtool/objtool-in.o -lelf /usr/src/linux.buster-backports/debian/build/build-tools/tools/objtool/libsubcmd.a -L/usr/lib/arm-linux-gnueabihf -o /usr/src/linux.buster-backports/debian/build/build-tools/tools/objtool/objtool
/usr/lib/gcc-cross/arm-linux-gnueabihf/8/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lelf
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:67: /usr/src/linux.buster-backports/debian/build/build-tools/tools/objtool/objtool] Error 1

You can see that make did add the -L/usr/lib/arm-linux-gnueabihf flag to gcc. And /usr/lib/arm-linux-gnueabihf does contain libelf:

# ls -l /usr/lib/arm-linux-gnueabihf/libelf*
-rw-r--r-- 1 root root 67296 May 28  2019 /usr/lib/arm-linux-gnueabihf/libelf-0.176.so
lrwxrwxrwx 1 root root    15 May 28  2019 /usr/lib/arm-linux-gnueabihf/libelf.so.1 -> libelf-0.176.so

# file /usr/lib/arm-linux-gnueabihf/libelf-0.176.so
/usr/lib/arm-linux-gnueabihf/libelf-0.176.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=51d60560aa6c0538f0bf34c07e4e2bc230c00834, stripped

I installed libelf like this:

dpkg --add-architecture armhf
apt-get update
apt-get install libelf1:armhf

The ld that's being used does appear to be for arm:

# /usr/lib/gcc-cross/arm-linux-gnueabihf/8/../../../../arm-linux-gnueabihf/bin/ld -V
GNU ld (GNU Binutils for Debian) 2.31.1
  Supported emulations:
   armelf_linux_eabi
   armelfb_linux_eabi

I'm stumped. How do I get ld to link libelf?


Solution

  • Neither libelf-0.176.so nor libelf.so.1 are searched by the -lelf option to the linker. You still need libelf.so file. This file can be created:

    • By installing libelf-dev package (as usual, the libraries with exact .so are installed by *-dev packages). Make sure to choose a package suitable for cross-compiling.
    • By creating symlink libelf.so pointed to the libelf-0.176.so file which you have.