QCC链接QCC库汇编指令报错:是链接命令还是环境问题?
问题
使用QCC构建项目时,编译器自带库文件出现链接错误。链接使用的Makefile命令如下:
$(OUTPUT): $(OBJ_FILES) $(QCC) $(QCC_FLAGS) -o $@ $(OBJ_FILES) -Wl,-T,$(LINKER_SCRIPT)
命令行报错信息:
DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: C:/Users/ashraf/qos_224//target/qnx7/aarch64le/lib/crt1.o: in function `_start': /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:59: undefined reference to `__preinit_array_start' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: C:/Users/ashraf/qos_224//target/qnx7/aarch64le/lib/crt1.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `__preinit_array_start' which may bind externally can not be used when making a shared object; recompile with -fPIC /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:59:(.text+0x34): dangerous relocation: unsupported relocation DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:60: undefined reference to `__preinit_array_end' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: C:/Users/ashraf/qos_224//target/qnx7/aarch64le/lib/crt1.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `__preinit_array_end' which may bind externally can not be used when making a shared object; recompile with -fPIC /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:60:(.text+0x38): dangerous relocation: unsupported relocation DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:61: undefined reference to `__preinit_array_start' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:62: undefined reference to `__preinit_array_end' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:64: undefined reference to `__fini_array_start' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: C:/Users/ashraf/qos_224//target/qnx7/aarch64le/lib/crt1.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `__fini_array_start' which may bind externally can not be used when making a shared object; recompile with -fPIC /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:64:(.text+0x48): dangerous relocation: unsupported relocation DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:65: undefined reference to `__fini_array_end' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: C:/Users/ashraf/qos_224//target/qnx7/aarch64le/lib/crt1.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `__fini_array_end' which may bind externally can not be used when making a shared object; recompile with -fPIC /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:65:(.text+0x4c): dangerous relocation: unsupported relocation DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:66: undefined reference to `__fini_array_start' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:67: undefined reference to `__fini_array_end' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:69: undefined reference to `__init_array_start' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: C:/Users/ashraf/qos_224//target/qnx7/aarch64le/lib/crt1.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `__init_array_start' which may bind externally can not be used when making a shared object; recompile with -fPIC /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:69:(.text+0x5c): dangerous relocation: unsupported relocation DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:70: undefined reference to `__init_array_end' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: C:/Users/ashraf/qos_224//target/qnx7/aarch64le/lib/crt1.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `__init_array_end' which may bind externally can not be used when making a shared object; recompile with -fPIC /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:70:(.text+0x60): dangerous relocation: unsupported relocation DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:71: undefined reference to `__init_array_start' DIR\qos_224\\host\win64\x86_64\usr\bin\aarch64-unknown-nto-qnx7.1.0-ld: /builds/workspace/QOS224-SDP/build_aarch64/lib/c/startup/aarch64/crt1.S:72: undefined reference to `__init_array_end' cc: C:/Users/ashraf/qos_224//host/win64/x86_64/usr/bin/aarch64-unknown-nto-qnx7.1.0-ld caught signal 1
疑问:出现上述错误是因为链接命令不正确,还是环境存在问题?
分析与结论
这类错误核心是链接配置问题,QCC工具链环境大概率正常,具体原因如下:
自定义链接脚本缺失关键段定义
__preinit_array_start、__init_array_end这类符号是链接器自动生成的,但前提是链接脚本里必须声明.preinit_array、.init_array、.fini_array对应的段。如果你的$(LINKER_SCRIPT)里没加这些段的定义,链接器就无法生成这些符号,导致启动文件crt1.o找不到它们。动态/静态链接模式不匹配
报错里提到"can not be used when making a shared object",说明当前是按共享库模式链接,但crt1.o是可执行程序的启动文件,不能用于动态库构建。如果目标是生成可执行文件,检查QCC_FLAGS里是不是误加了-shared参数;如果确实要做动态库,就不该链接crt1.o这类启动文件。QCC参数顺序或缺失系统库链接
QCC对参数顺序有要求,系统库(比如标准C库-lc)应该放在目标文件之后。当前命令只传了$(OBJ_FILES),可能缺少了必要的系统库链接参数,或者参数顺序不对导致启动库没被正确关联。
修复建议
- 补充链接脚本的段定义(参考QNX默认脚本):
.preinit_array : { PROVIDE_HIDDEN (__preinit_array_start = .); KEEP (*(.preinit_array)) PROVIDE_HIDDEN (__preinit_array_end = .); } .init_array : { PROVIDE_HIDDEN (__init_array_start = .); KEEP (*(SORT(.init_array.*))) KEEP (*(.init_array)) PROVIDE_HIDDEN (__init_array_end = .); } .fini_array : { PROVIDE_HIDDEN (__fini_array_start = .); KEEP (*(SORT(.fini_array.*))) KEEP (*(.fini_array)) PROVIDE_HIDDEN (__fini_array_end = .); } - 确认产物类型:如果是可执行程序,移除
QCC_FLAGS中的-shared;如果是动态库,去掉crt1.o的链接(QCC会自动处理)。 - 调整QCC命令参数顺序,补充系统库:
$(OUTPUT): $(OBJ_FILES) $(QCC) $(QCC_FLAGS) -o $@ $(OBJ_FILES) -Wl,-T,$(LINKER_SCRIPT) -lc
内容的提问来源于stack exchange,提问作者Khaled Ashraf
相关产品推荐
相关产品推荐

