使用 arm-none-eabi-gcc 编译器编译 RaspberryPI2 和 BeableBoneBlack 的



我是这种类型的编码新手,我正在尝试为Cortex-A处理器执行裸机软件进行一些测试。我有使用Cortex-M MCU的经验,我使用像ARM-Keil这样的IDE和Nordic的SDK编译代码,用于一些BLE工作。现在我想尝试更好地理解Cortex-A的世界,我将以裸机的方式学习编码,从我在网上找到的一些例子开始。我有一些使用 Cortex-M 的 freeRTOS 的经验,所以我在网上找到了一些来自某个好人的 git 存储库,它为 RaspberryPI 和 BeableBoneBlack 移植了 freeRTOS。从现在开始,我将只解释我对BeableBoneBlack的问题,对于RaspberryPI来说,这是类似的。

我在Linux上,我安装了gcc-arm-none-eabi编译器,所以我从这个链接克隆了带有freeRTOS存储库的BeagleBone Black。

我通常使用 VS Code 来编写代码,因此在集成终端中,当我运行命令时,使一切正常并且我的 BBB 正确闪烁。 所以现在我想改进我的代码,为了做一些测试,我想使用来自 stdlib.hrand()函数。不幸的是,我发现有一些错误:undefined reference to rand。 在过去的几个月里,在使用 BBB 进行测试之前,我找到了 RPi2 的其他存储库,并了解了一些关于 arm-none-eabi 编译器的链接器的知识,它需要添加一些参数以在此过程中链接库文件。 从这个存储库现在我有两个文件:makedefs_timakefile。打开 make 文件,我发现在第 26-27 行是文件应用程序生成的一部分,因此调用链接器的位置。在这一行中,有对LIB_GCCLIB_C的引用,它们在makedefs_ti中定义。在第 49-50 行中,引用了安装编译器的目录(我用安装在 linux PC 上的正确 9.2.1 更改了 4.7.3)。 在 makefile 中,-L$(LIB_C)之后,如果我添加链接器参数(如-lc-lg并尝试重新编译),则会出现类似arm-none-eabi-ld: cannot find -lc的错误。 根据在线资源的一些了解,我修改了makedefs_ti如下:

# Toolchain/library path. LIB_PATH is an exported environmental variable which
# shall point to the installation of toolchain
#
#LIB_GCC=${LIB_PATH}/lib/gcc/arm-none-eabi/4.7.3/
#LIB_C=${LIB_PATH}/arm-none-eabi/lib/
LIB_GCC= /usr/lib/gcc/arm-none-eabi/9.2.1
LIB_C= /usr/lib/arm-none-eabi/lib

制作文件

$(LD) -o $@.out $< -T bbb.ld -Map bbb.map $(APP_LIB) $(LDFLAGS) $(RUNTIMELIB) -L $(LPATH) 
-L $(LIB_GCC) -lgcc -L $(LIB_C) -lc -lg

这样,调用rand()函数的main.c文件就可以正确编译。 在另一个测试中,我试图编译另一个代码。现在我写了一对 .c 和 .h 文件(我将它们放在 rtos 目录中以利用那里存在的相同 makefile),它们包含一个调用sqrt()的函数。因此,在main()中调用函数并进行编译,即使我包含在所有地方<math.h>我也有错误:

undefined reference to `sqrt'

显然,编译停止了。 从这个开始,我尝试向链接器添加另一个参数,所以我修改了makefile,如下所示:

$(LD) -o $@.out $< -T bbb.ld -Map bbb.map $(APP_LIB) $(LDFLAGS) $(RUNTIMELIB) -L $(LPATH) 
-L $(LIB_GCC) -lgcc -L $(LIB_C) -lc -lg -lm

我收到其他无法以任何方式解决的错误:

arm-none-eabi-ld: /usr/lib/arm-none-eabi/lib/libm.a(lib_a-w_sqrt.o): in function `sqrt':
/build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/math/../../../../../newlib/libm/math/w_sqrt.c:62: undefined reference to `__aeabi_dcmpun'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/math/../../../../../newlib/libm/math/w_sqrt.c:63: undefined reference to `__aeabi_dcmplt'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/math/../../../../../newlib/libm/math/w_sqrt.c:64: undefined reference to `__errno'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/math/../../../../../newlib/libm/math/w_sqrt.c:65: undefined reference to `__aeabi_ddiv'
arm-none-eabi-ld: /usr/lib/arm-none-eabi/lib/libm.a(lib_a-e_sqrt.o): in function `__ieee754_sqrt':
/build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/machine/arm/../../../../../../newlib/libm/machine/arm/../../math/e_sqrt.c:110: undefined reference to `__aeabi_dmul'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/machine/arm/../../../../../../newlib/libm/machine/arm/../../math/e_sqrt.c:110: undefined reference to `__aeabi_dadd'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/machine/arm/../../../../../../newlib/libm/machine/arm/../../math/e_sqrt.c:117: undefined reference to `__aeabi_dsub'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/machine/arm/../../../../../../newlib/libm/machine/arm/../../math/e_sqrt.c:117: undefined reference to `__aeabi_ddiv'
make: *** [makefile:26: app] Errore 1

我要说的是,在 main 中调用sqrt()可以正常工作,当我在其他 .c 文件中调用sqrt()时会出现问题。相反,.c 附加文件中的rand()函数可以正常工作。

我不知道如何解决这些错误,有人可以帮助我吗?

我另一天尝试的是在Code Composer Studio项目中编写相同的代码,并且它起作用了(具有在附加.c文件中调用sqrt和rand的相同函数)。有什么区别?

如果这是一个冗长的解释,我很抱歉,但我没有找到另一种方法来解释我的情况。

提前非常感谢。

齐射

start.s

.globl _start
_start:
;@ enable fpu
mrc p15, 0, r0, c1, c0, 2
orr r0,r0,#0x300000 ;@ single precision
orr r0,r0,#0xC00000 ;@ double precision
mcr p15, 0, r0, c1, c0, 2
mov r0,#0x40000000
fmxr fpexc,r0
mov sp,#0x8000
bl notmain
hang: b hang

notmain.c

double notmain ( double a, double b )
{
return(a+b);
}

(不是真正的裸机应用程序,派生自一个)

专为硬浮子而构建

arm-none-eabi-as --warn --fatal-warnings -mcpu=arm1176jzf-s -march=armv6 -mfpu=vfp start.s -o start.o
arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -mhard-float -mfpu=vfp -c notmain.c -o notmain.o
arm-none-eabi-ld -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy --srec-forceS3 notmain.elf -O srec notmain.srec
arm-none-eabi-objcopy notmain.elf -O binary kernel.img

使用的硬件浮点数

00008024 <notmain>:
8024:   ee300b01    vadd.f64    d0, d0, d1
8028:   e12fff1e    bx  lr

专为软浮子而构建

arm-none-eabi-as --warn --fatal-warnings -mcpu=arm1176jzf-s -march=armv6 -mfpu=vfp start.s -o start.o
arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -c notmain.c -o notmain.o
arm-none-eabi-ld -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf
arm-none-eabi-ld: notmain.o: in function `notmain':
notmain.c:(.text+0x4): undefined reference to `__aeabi_dadd'
Makefile:33: recipe for target 'notmain.elf' failed
make: *** [notmain.elf] Error 1

它需要gcclib。

使用 gcc 作为链接器(我知道这是多么可怕的想法)。

arm-none-eabi-as --warn --fatal-warnings -mcpu=arm1176jzf-s -march=armv6 -mfpu=vfp start.s -o start.o
arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -c notmain.c -o notmain.o
arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf -lgcc
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy --srec-forceS3 notmain.elf -O srec notmain.srec
arm-none-eabi-objcopy notmain.elf -O binary kernel.img

00008024 <notmain>:
8024:   e92d4010    push    {r4, lr}
8028:   eb000003    bl  803c <__adddf3>
802c:   e8bd8010    pop {r4, pc}
00008030 <__aeabi_drsub>:
8030:   e2211102    eor r1, r1, #-2147483648    ; 0x80000000
8034:   ea000000    b   803c <__adddf3>
00008038 <__aeabi_dsub>:
8038:   e2233102    eor r3, r3, #-2147483648    ; 0x80000000
0000803c <__adddf3>:
803c:   e92d4030    push    {r4, r5, lr}

GCC 正在将这些传递给 LD。

[0][/opt/gnuarm/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/bin/ld]
[1][-plugin]
[2][/opt/gnuarm/libexec/gcc/arm-none-eabi/10.2.0/liblto_plugin.so]
[3][-plugin-opt=/opt/gnuarm/libexec/gcc/arm-none-eabi/10.2.0/lto-wrapper]
[4][-plugin-opt=-fresolution=/tmp/ccRiWZtk.res]
[5][-X]
[6][-o]
[7][notmain.elf]
[8][-L/opt/gnuarm/lib/gcc/arm-none-eabi/10.2.0]
[9][-L/opt/gnuarm/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/lib]
[10][start.o]
[11][notmain.o]
[12][-lgcc]
[13][-T]
[14][memmap]
find | grep libgcc
./lib/gcc/arm-none-eabi/10.2.0/libgcc.a
./lib/gcc/arm-none-eabi/10.2.0/thumb/libgcc.a
./lib/gcc/arm-none-eabi/10.2.0/thumb/autofp/v7/fpu/libgcc.a
./lib/gcc/arm-none-eabi/10.2.0/arm/autofp/v5te/fpu/libgcc.a

所以使用 ld 那么

arm-none-eabi-as --warn --fatal-warnings -mcpu=arm1176jzf-s -march=armv6 -mfpu=vfp start.s -o start.o
arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -c notmain.c -o notmain.o
arm-none-eabi-ld -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf -L/opt/gnuarm/lib/gcc/arm-none-eabi/10.2.0/ -lgcc
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy --srec-forceS3 notmain.elf -O srec notmain.srec
arm-none-eabi-objcopy notmain.elf -O binary kernel.img

为什么它现在使用 addf3? 不知道。

00008024 <notmain>:
8024:   e92d4010    push    {r4, lr}
8028:   eb000003    bl  803c <__adddf3>
802c:   e8bd8010    pop {r4, pc}
00008030 <__aeabi_drsub>:
8030:   e2211102    eor r1, r1, #-2147483648    ; 0x80000000
8034:   ea000000    b   803c <__adddf3>
00008038 <__aeabi_dsub>:
8038:   e2233102    eor r3, r3, #-2147483648    ; 0x80000000
0000803c <__adddf3>:
803c:   e92d4030    push    {r4, r5, lr}
8040:   e1a04081    lsl r4, r1, #1
8044:   e1a05083    lsl r5, r3, #1

这就是你处理 aeabi 的东西的方式,你需要使用硬浮点或包含 gcclib 的路径,或者只是将其添加到行中

arm-none-eabi-as --warn --fatal-warnings -mcpu=arm1176jzf-s -march=armv6 -mfpu=vfp start.s -o start.o
arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -c notmain.c -o notmain.o
arm-none-eabi-ld -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf /opt/gnuarm/lib/gcc/arm-none-eabi/10.2.0/libgcc.a
#arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf -lgcc
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy --srec-forceS3 notmain.elf -O srec notmain.srec
arm-none-eabi-objcopy notmain.elf -O binary kernel.img

现在 sqrt() 是一个 C 库的东西,而不是一个 gcc 库的东西,所以你需要一个 C 库(看起来你正在尝试使用 newlib),你需要构建 C 库来匹配 gcc,所以你需要为硬浮点或软浮点构建它,并且需要包含正确的库。 我没有...井。。。。

arm-none-eabi-as --warn --fatal-warnings -mcpu=arm1176jzf-s -march=armv6 -mfpu=vfp start.s -o start.o
arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -c notmain.c -o notmain.o
arm-none-eabi-ld -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf /home/so/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/libgcc.a
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy --srec-forceS3 notmain.elf -O srec notmain.srec
arm-none-eabi-objcopy notmain.elf -O binary kernel.img

所以

double sqrt(double x);
double notmain ( double a )
{
return(sqrt(a));
}

看起来很熟悉

arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -c notmain.c -o notmain.o
arm-none-eabi-ld -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf /home/so/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/libgcc.a /home/so/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/lib/libm.a
arm-none-eabi-ld: /home/so/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/lib/libm.a(lib_a-w_sqrt.o): in function `sqrt':
w_sqrt.c:(.text.sqrt+0x34): undefined reference to `__aeabi_dcmpun'
arm-none-eabi-ld: w_sqrt.c:(.text.sqrt+0x6c): undefined reference to `__aeabi_dcmplt'
arm-none-eabi-ld: w_sqrt.c:(.text.sqrt+0xc4): undefined reference to `__aeabi_ddiv'
arm-none-eabi-ld: w_sqrt.c:(.text.sqrt+0xd8): undefined reference to `__errno'
arm-none-eabi-ld: w_sqrt.c:(.text.sqrt+0x120): undefined reference to `__errno'
arm-none-eabi-ld: /home/so/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/lib/libm.a(lib_a-e_sqrt.o): in function `__ieee754_sqrt':
e_sqrt.c:(.text.__ieee754_sqrt+0x200): undefined reference to `__aeabi_dmul'
arm-none-eabi-ld: e_sqrt.c:(.text.__ieee754_sqrt+0x20c): undefined reference to `__aeabi_dadd'
arm-none-eabi-ld: e_sqrt.c:(.text.__ieee754_sqrt+0x22c): undefined reference to `__aeabi_dsub'
arm-none-eabi-ld: e_sqrt.c:(.text.__ieee754_sqrt+0x238): undefined reference to `__aeabi_ddiv'
Makefile:33: recipe for target 'notmain.elf' failed
make: *** [notmain.elf] Error 1

找到

./arm-none-eabi/lib/libm.a
./arm-none-eabi/lib/thumb/v7e-m/nofp/libm.a
./arm-none-eabi/lib/thumb/v6-m/nofp/libm.a
./arm-none-eabi/lib/thumb/v8-m.base/nofp/libm.a
./arm-none-eabi/lib/thumb/v7/nofp/libm.a
./arm-none-eabi/lib/thumb/v8-m.main+dp/hard/libm.a
./arm-none-eabi/lib/thumb/v8-m.main+dp/softfp/libm.a
./arm-none-eabi/lib/thumb/v7e-m+dp/hard/libm.a
./arm-none-eabi/lib/thumb/v7e-m+dp/softfp/libm.a
./arm-none-eabi/lib/thumb/v7-r+fp.sp/hard/libm.a
./arm-none-eabi/lib/thumb/v7-r+fp.sp/softfp/libm.a
./arm-none-eabi/lib/thumb/v7e-m+fp/hard/libm.a
./arm-none-eabi/lib/thumb/v7e-m+fp/softfp/libm.a
./arm-none-eabi/lib/thumb/v8-m.main+fp/hard/libm.a
./arm-none-eabi/lib/thumb/v8-m.main+fp/softfp/libm.a
./arm-none-eabi/lib/thumb/v7-m/nofp/libm.a
./arm-none-eabi/lib/thumb/nofp/libm.a
./arm-none-eabi/lib/thumb/v7+fp/hard/libm.a
./arm-none-eabi/lib/thumb/v7+fp/softfp/libm.a
./arm-none-eabi/lib/thumb/v8-m.main/nofp/libm.a
./arm-none-eabi/lib/arm/v5te/hard/libm.a
./arm-none-eabi/lib/arm/v5te/softfp/libm.a

armv5te 应该适用于 armv6

arm-none-eabi-ld -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf /home/so/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/libgcc.a /home/so/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/lib/arm/v5te/softfp/libm.a
arm-none-eabi-ld: /home/so/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/lib/arm/v5te/softfp/libm.a(lib_a-w_sqrt.o): in function `sqrt':
w_sqrt.c:(.text.sqrt+0x94): undefined reference to `__errno'
arm-none-eabi-ld: w_sqrt.c:(.text.sqrt+0xdc): undefined reference to `__errno'
Makefile:33: recipe for target 'notmain.elf' failed
make: *** [notmain.elf] Error 1

errno 是一个全局变量。 C 库可能希望将其用于软浮点。

int __errno;
double sqrt(double x);
double notmain ( double a )
{
return(sqrt(a));
}

现在该工具很高兴

arm-none-eabi-as --warn --fatal-warnings -mcpu=arm1176jzf-s -march=armv6 -mfpu=vfp start.s -o start.o
arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -c notmain.c -o notmain.o
arm-none-eabi-ld -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf /home/so/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/libgcc.a /home/so/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/lib/arm/v5te/softfp/libm.a
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy --srec-forceS3 notmain.elf -O srec notmain.srec
arm-none-eabi-objcopy notmain.elf -O binary kernel.img

但这是硬浮子而不是软浮子。

00008024 <notmain>:
8024:   eaffffff    b   8028 <sqrt>
00008028 <sqrt>:
8028:   e52de004    push    {lr}        ; (str lr, [sp, #-4]!)
802c:   ed2d8b02    vpush   {d8}
8030:   e1a02000    mov r2, r0
8034:   e1a03001    mov r3, r1
8038:   e24dd02c    sub sp, sp, #44 ; 0x2c
803c:   ec432b18    vmov    d8, r2, r3
8040:   eb000038    bl  8128 <__ieee754_sqrt>
8044:   e59f30d4    ldr r3, [pc, #212]  ; 8120 <sqrt+0xf8>
8048:   eeb48b48    vcmp.f64    d8, d8
804c:   e1d3c0d0    ldrsb   ip, [r3]
8050:   eef1fa10    vmrs    APSR_nzcv, fpscr

不过,您知道解决此问题的途径是什么。 您需要一个正确匹配/构建的库,然后需要链接它。 Keil和其他人只是在工具链中为你做这件事,根据项目匹配东西。 和/或与工具链集成的 C 库。

硬浮子建筑

arm-none-eabi-as --warn --fatal-warnings -mcpu=arm1176jzf-s -march=armv6 -mfpu=vfp start.s -o start.o
arm-none-eabi-gcc -Wall -O2 -ffreestanding -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -mhard-float -mfpu=vfp -c notmain.c -o notmain.o
arm-none-eabi-ld -nostdlib -nostartfiles start.o notmain.o -T memmap -o notmain.elf /home/so/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/libgcc.a /home/so/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/lib/arm/v5te/hard/libm.a
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy --srec-forceS3 notmain.elf -O srec notmain.srec
arm-none-eabi-objcopy notmain.elf -O binary kernel.img

工具很高兴。

00008024 <notmain>:
8024:   eaffffff    b   8028 <sqrt>
00008028 <sqrt>:
8028:   e52de004    push    {lr}        ; (str lr, [sp, #-4]!)
802c:   ed2d8b02    vpush   {d8}
8030:   eeb08b40    vmov.f64    d8, d0
8034:   e24dd02c    sub sp, sp, #44 ; 0x2c
8038:   eb000038    bl  8120 <__ieee754_sqrt>
803c:   eeb48b48    vcmp.f64    d8, d8
8040:   e59f30d0    ldr r3, [pc, #208]  ; 8118 <sqrt+0xf0>
8044:   e1d310d0    ldrsb   

必须使其成为真正的应用程序主调用另一个调用 sqrt 的函数。 然后在硬件上运行它和/或检查这些说明。 或者更好的是获取 sqrt 的 C 库实现并在项目中构建它,以便它与目标和指令集匹配。 (与 libgcc 调用相同的故事根据需要,测试它们或构建它们)。

相关内容

  • 没有找到相关文章

最新更新