编译适用于 linux arm64 的 GCC 时链接 .c 文件的 Makefile 链接器错误

Makefile linker error about linking .c files when compiling GCC for linux arm64

提问人:Ben 提问时间:12/8/2021 最后编辑:Ben 更新时间:12/8/2021 访问量:207

问:

我正在使用 Linux WSL 上的 Android NDK 交叉编译 GCC,因为我的目标是 ARM64 Android。我需要一些 Android 兼容文件,例如(pytorch 需要)这些文件显然是在编译 GCC 时创建的。将来我可能还需要一个可以在 Android 上运行的 GCC 可执行文件。.solibgomp.so

这是我编写的用于编译 GCC 的构建脚本:

#!/bin/bash

export PATH="/home/ben/.local/bin:/opt/gradle/latest/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/"
BUILD_DIR=./build

GCC_DIR=`realpath gcc`

#CONF_FLAGS="--disable-shared"
CONF_FLAGS="--enable-shared --enable-static"

mkdir $BUILD_DIR

PREFIX=`realpath ./install/`
mkdir $PREFIX

echo "install directory: $PREFIX"

pushd $BUILD_DIR

export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
export TARGET=aarch64-linux-android

export API=23
export AR=$TOOLCHAIN/bin/llvm-ar
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export AS=$CC
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/ld
export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
export STRIP=$TOOLCHAIN/bin/llvm-strip

echo "Cleaning gcc directory via git clean"
pushd $GCC_DIR
git clean -f
popd

$GCC_DIR/configure --host $TARGET --prefix=$PREFIX $CONF_FLAGS
make

popd

我需要重新定义,否则我在makefile中出现语法错误,可能是因为在Windows的PATH中也定义了语法错误。 其他变量的定义如下:https://developer.android.com/ndk/guides/other_build_systemsPATHmake

NDK指向/home/ben/android-ndk-r23b

./configure据我所知,似乎已经正确工作,但是当我运行时,过了一会儿,我收到以下错误:make

ld: error: /home/ben/gcc_build/gcc/gcc/c-family/c-opts.c:1090:(.text+0x1F50): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/c-family/c-opts.c:1271:(.text+0x2830): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/c-family/c-opts.c:0:(.text+0x2854): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/read-md.c:137:(.text+0x254): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/read-md.c:197:(.text+0x500): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/toplev.c:697:(.text+0x25F4): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/toplev.c:711:(.text+0x2770): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/gimple-ssa-warn-restrict.c:2159:(.text+0x3AF4): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/ggc-page.c:694:(.text+0x68): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/ggc-page.c:1744:(.text+0xD3C): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/dumpfile.c:0:(.text+0x2A7C): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/dumpfile.c:0:(.text+0x2A9C): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/dumpfile.c:0:(.text+0x30D0): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
ld: error: /home/ben/gcc_build/gcc/gcc/dumpfile.c:0:(.text+0x420C): improper alignment for relocation R_AARCH64_LDST64_ABS_LO12_NC: 0x1C842A4 is not aligned to 8 bytes
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

失败的命令是这样的:

/home/ben/android-ndk-r23b/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android23-clang++ -no-pie   -g -O2 -DIN_GCC    -fno-strict-aliasing -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wno-error=format-diag -Wmissing-format-attribute -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings   -DHAVE_CONFIG_H  -o cc1 c/c-lang.o c-family/stub-objc.o attribs.o c/c-errors.o c/c-decl.o c/c-typeck.o c/c-convert.o c/c-aux-info.o c/c-objc-common.o c/c-parser.o c/c-fold.o c/gimple-parser.o c-family/c-common.o c-family/c-cppbuiltin.o c-family/c-dump.o c-family/c-format.o c-family/c-gimplify.o c-family/c-indentation.o c-family/c-lex.o c-family/c-omp.o c-family/c-opts.o c-family/c-pch.o c-family/c-ppoutput.o c-family/c-pragma.o c-family/c-pretty-print.o c-family/c-semantics.o c-family/c-ada-spec.o c-family/c-ubsan.o c-family/known-headers.o c-family/c-attribs.o c-family/c-warn.o c-family/c-spellcheck.o aarch64-c.o glibc-c.o \
  cc1-checksum.o libbackend.a main.o libcommon-target.a libcommon.a ../libcpp/libcpp.a ../libdecnumber/libdecnumber.a libcommon.a ../libcpp/libcpp.a ./../intl/libintl.a  ../libbacktrace/.libs/libbacktrace.a ../libiberty/libiberty.a ../libdecnumber/libdecnumber.a   -L/home/ben/gcc_build/build/./gmp/.libs -L/home/ben/gcc_build/build/./mpfr/src/.libs -L/home/ben/gcc_build/build/./mpc/src/.libs -lmpc -lmpfr -lgmp -rdynamic  -L./../zlib -lz

我不确定发生了什么,因为它似乎正在尝试链接文件,我认为这不正常。我也不明白这些文件是如何涉及的,因为它们没有出现在命令中,目录也没有。 我不认为在 WSL 中编译是问题,尽管它以前引起了一些问题,但我认为我解决了它们。ld.cgcc/gcc/

我能做些什么来解决这个问题?

Linux GCC 交叉编译 链接器错误

评论

0赞 Bodo 12/8/2021
我看不出该命令会在哪里链接 .c 文件。我认为错误消息仅引用了与发现错误的 .o 文件相对应的 .c 文件。也许 .c 文件是用错误的设置编译的,导致错误的对齐方式。我是否理解您想构建一个可以在 ARM64 Android 上运行的 GCC 可执行文件?
0赞 Ben 12/8/2021
@Bodo我还不需要GCC可执行文件,但是我需要在此过程中创建一些.so文件,尤其是 libgomp.so .
0赞 Bodo 12/8/2021
编辑您的问题以添加此信息。你到底想实现什么?您究竟是如何配置和运行构建过程的?我没有找到足够的文档。是 GCC 构建的一部分吗?libgomp.so
0赞 Ben 12/8/2021
@Bodo我编辑了这个问题。 是 GCC 源代码树中的一个文件夹,因为 pytorch 和我需要在 Android 上使用的其他库可能需要其他“核心”库,我认为编译整个 GCC 生态系统会更容易。libgomp

答: 暂无答案