如何为 RISC-V 交叉编译 elfutils?

How to cross-compile elfutils for RISC-V?

我想为 RISC-V 目标交叉编译 elfutils,但出现链接器错误,我不知道如何解决。我使用 riscv-gnu-toolchain.

建立zlib

elfutils 是针对 zlib 构建的,所以我需要先构建它:

CC=riscv64-unknown-linux-gnu-gcc ./configure --prefix=/usr/local/riscv64-unknown-linux-gnu
make
make install

我使用此前缀将所有 RV64 库放在同一位置,以便链接器可以轻松找到它们(也许是错误的?)。构建 zlib 似乎是成功的,没有错误,并且 libz.so.1 和其他相关文件被放入 /usr/local/riscv64-unknown-linux-gnu/lib.

建造elfutils

使用 RISC-V 主机执行配置脚本:(我还使用与 zlib 相同的 CC 和 PREFIX 交叉编译了 zstdbzip2xz

$ ./configure --build=x86_64-linux-gnu --host=riscv64-unknown-linux-gnu --prefix=/usr/local/riscv64-unknown-linux-gnu --disable-libdebuginfod --disable-debuginfod
[checking a lot of stuff...]
=====================================================================
        elfutils: 0.183 (eu_version: 183)
=====================================================================

    Prefix                             : /usr/local/riscv64-unknown-linux-gnu
    Program prefix ("eu-" recommended) : eu-
    Source code location               : .
    Maintainer mode                    : 
    build arch                         : x86_64-pc-linux-gnu

  RECOMMENDED FEATURES (should all be yes)
    gzip support                       : yes
    bzip2 support                      : no
    lzma/xz support                    : yes
    zstd support                       : yes
    libstdc++ demangle support         : yes
    File textrel check                 : yes
    Symbol versioning                  : yes

  NOT RECOMMENDED FEATURES (should all be no)
    Experimental thread safety         : no
    install elf.h                      : no

  OTHER FEATURES
    Deterministic archives by default  : false
    Native language support            : yes
    Extra Valgrind annotations         : no
    libdebuginfod client support       : no
    Debuginfod server support          : no
    Default DEBUGINFOD_URLS            : 

  EXTRA TEST FEATURES (used with make check)
    have bunzip2 installed (required)  : yes
    have zstd installed                : no
    debug branch prediction            : no
    gprof support                      : no
    gcov support                       : no
    run all tests under valgrind       : no
    gcc undefined behaviour sanitizer  : no
    use rpath in tests                 : no
    test biarch                        : no

然后 运行 make 但是我得到这个错误:

$ make
make --no-print-directory all-recursive
Making all in config
make[2]: Nothing to be done for 'all'.
Making all in lib
  CC       xstrdup.o
  CC       xstrndup.o
  CC       xmalloc.o
[...]
  CC       disasm_str.os
  CC       symbolhash.os
  AR       libasm_pic.a
  CCLD     libasm.so
Making all in debuginfod
make[2]: Nothing to be done for 'all'.
Making all in src
  CC       readelf.o
  CCLD     readelf
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: warning: libz.so.1, needed by ../libdw/libdw.so, not found (try using -rpath or -rpath-link)
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: warning: libzstd.so.1, needed by ../libdw/libdw.so, not found (try using -rpath or -rpath-link)
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: warning: liblzma.so.5, needed by ../libdw/libdw.so, not found (try using -rpath or -rpath-link)
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `lzma_auto_decoder@XZ_5.0'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `gzclose'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libelf/libelf.so: undefined reference to `inflate'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `ZSTD_isError'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `gzdopen'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `ZSTD_freeDCtx'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libelf/libelf.so: undefined reference to `deflate'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libelf/libelf.so: undefined reference to `deflateInit_'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `gzread'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libelf/libelf.so: undefined reference to `inflateEnd'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `ZSTD_createDCtx'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `lzma_end@XZ_5.0'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `gzerror'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `lzma_code@XZ_5.0'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libelf/libelf.so: undefined reference to `deflateEnd'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libelf/libelf.so: undefined reference to `inflateInit_'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `ZSTD_decompressStream'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libelf/libelf.so: undefined reference to `inflateReset'
/usr/local/lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ../libdw/libdw.so: undefined reference to `gzdirect@ZLIB_1.2.2.3'
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:686: readelf] Error 1
make[1]: *** [Makefile:529: all-recursive] Error 1
make: *** [Makefile:445: all] Error 2

为什么链接器找不到 libz.so.1libzstd.so.1liblzma.so.5?您可以在此处看到这些库可用:

$ find /usr/local/riscv64-unknown-linux-gnu -name "libz.so.*"
/usr/local/riscv64-unknown-linux-gnu/lib/libz.so.1.2.11
/usr/local/riscv64-unknown-linux-gnu/lib/libz.so.1
$ find /usr/local/riscv64-unknown-linux-gnu -name "libzstd.so.*"
/usr/local/riscv64-unknown-linux-gnu/lib/libzstd.so.1
/usr/local/riscv64-unknown-linux-gnu/lib/libzstd.so.1.5.0
$ find /usr/local/riscv64-unknown-linux-gnu -name "liblzma.so.*"
/usr/local/riscv64-unknown-linux-gnu/lib/liblzma.so.5.2.5
/usr/local/riscv64-unknown-linux-gnu/lib/liblzma.so.5

我需要添加 LIBS="-lz -lzstd -llzma"。完整的配置命令如下所示:

LIBS="-lz -lzstd -llzma" ./configure --build=x86_64-linux-gnu --host=riscv64-unknown-linux-gnu --disable-libdebuginfod --disable-debuginfod --prefix=/usr/local/riscv64-unknown-linux-gnu

我预计,这些标志已经由配置脚本和 makefile 设置...但由于某些原因,如果库是动态链接的,则它们不是。

src/Makefile.am: (zip_LIBS= -lzstd -llzma)

[...]
if BUILD_STATIC
libasm = ../libasm/libasm.a
libdw = ../libdw/libdw.a -lz $(zip_LIBS) $(libelf) -ldl -lpthread
libelf = ../libelf/libelf.a -lz
else
libasm = ../libasm/libasm.so
libdw = ../libdw/libdw.so
libelf = ../libelf/libelf.so
endif
[...]