arm 交叉编译 thumb 与 arm 指令的方法

2024-05-05 15:36

本文主要是介绍arm 交叉编译 thumb 与 arm 指令的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

arm 交叉编译 thumb 与 arm 指令的方法

  • 本文实现了在 x86 的 ubuntu 的机器上,使用 arm-linux-gnueabihf-gcc 交叉编译链工具,编译出在 arm 开发板上可以运行的 thumb 指令集的可执行文件。后续会使用 vscode 使用网络进行远程调试。

1. 编译器 arm-linux-gnueabihf-gcc

# arm-linux-gnueabihf-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/arm-linux-gnueabihf/11/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --without-target-system-zlib --enable-multiarch --disable-sjlj-exceptions --with-arch=armv7-a+fp --with-float=hard --with-mode=thumb --disable-werror --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=arm-linux-gnueabihf --program-prefix=arm-linux-gnueabihf- --includedir=/usr/arm-linux-gnueabihf/include --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 

ubuntu 中安装方法 sudo apt-get install binutils-arm-linux-gnueabi gcc-arm-linux-gnuabihf

2. 代码中实现

使用 __attribute__((target("thumb"))) void thumb_ins(void); 指定函数编译为 thumb 指令

//完整的 main.c 的示例
#include <stdio.h>__attribute__((target("thumb"))) void thumb_ins(void);
void thumb_ins(void) {int ia = 0;int ib = 0;int ic = 0;while (1) {ia = 0;ib = 0;ic = 0xab;asm volatile ("mov r0, #1");asm volatile ("mov r4, #0");asm volatile ("cmp r4, #0");asm volatile ("itte  eq");asm volatile ("moveq r1, #55");asm volatile ("moveq r2, #66");asm volatile ("movne r2, #77");asm volatile ("mov %0, r1" : "=r" (ia));asm volatile ("mov %0, r2" : "=r" (ib));asm volatile ("mov %0, r0" : "=r" (ic));if ((ia != 55) || (ib != 66)) {printf("error!!!");printf(" ia %d ib %d  %d\r\n", ia, ib, ic);}}
}int main(void)
{printf("I run!!! %s %s\r\n", __TIME__, __DATE__);while (1) {thumb_ins();}return 0;
}

3. 编译方法

  • 编译指令 arm-linux-gnueabihf-gcc -g -static -mthumb-interwork -mthumb main.c

    参数解释:

    -g 添加调试信息

    -static 使用静态链接的方式

    -mthumb 使用 thumb 指令集

    -mthumb-interwork 支持 arm 与 thumb 混合

  • 反汇编查看是否正确 arm-linux-gnueabihf-objdump -S a.out > a.asm

__attribute__((target("thumb"))) void thumb_ins(void);
void thumb_ins(void) {10438:	b580      	push	{r7, lr}1043a:	b084      	sub	sp, #161043c:	af00      	add	r7, sp, #0int ia = 0;1043e:	2300      	movs	r3, #010440:	607b      	str	r3, [r7, #4]int ib = 0;10442:	2300      	movs	r3, #010444:	60bb      	str	r3, [r7, #8]int ic = 0;10446:	2300      	movs	r3, #010448:	60fb      	str	r3, [r7, #12]while (1) {ia = 0;1044a:	2300      	movs	r3, #01044c:	607b      	str	r3, [r7, #4]ib = 0;1044e:	2300      	movs	r3, #010450:	60bb      	str	r3, [r7, #8]ic = 0xab;10452:	23ab      	movs	r3, #171	; 0xab10454:	60fb      	str	r3, [r7, #12]asm volatile ("mov r0, #1");10456:	f04f 0001 	mov.w	r0, #1asm volatile ("mov r4, #0");1045a:	f04f 0400 	mov.w	r4, #0asm volatile ("cmp r4, #0");1045e:	2c00      	cmp	r4, #0asm volatile ("itte  eq");10460:	bf06      	itte	eqasm volatile ("moveq r1, #55");10462:	2137      	moveq	r1, #55	; 0x37asm volatile ("moveq r2, #66");10464:	2242      	moveq	r2, #66	; 0x42asm volatile ("movne r2, #77");10466:	224d      	movne	r2, #77	; 0x4dasm volatile ("mov %0, r1" : "=r" (ia));10468:	460b      	mov	r3, r11046a:	607b      	str	r3, [r7, #4]asm volatile ("mov %0, r2" : "=r" (ib));1046c:	4613      	mov	r3, r21046e:	60bb      	str	r3, [r7, #8]asm volatile ("mov %0, r0" : "=r" (ic));10470:	4603      	mov	r3, r010472:	60fb      	str	r3, [r7, #12]if ((ia != 55) || (ib != 66)) {10474:	687b      	ldr	r3, [r7, #4]10476:	2b37      	cmp	r3, #55	; 0x3710478:	d102      	bne.n	10480 <thumb_ins+0x48>1047a:	68bb      	ldr	r3, [r7, #8]1047c:	2b42      	cmp	r3, #66	; 0x421047e:	d0e4      	beq.n	1044a <thumb_ins+0x12>printf("error!!!");10480:	4b06      	ldr	r3, [pc, #24]	; (1049c <thumb_ins+0x64>)10482:	447b      	add	r3, pc10484:	4618      	mov	r0, r310486:	f004 fb67 	bl	14b58 <_IO_printf>printf(" ia %d ib %d  %d\r\n", ia, ib, ic);1048a:	68fb      	ldr	r3, [r7, #12]1048c:	68ba      	ldr	r2, [r7, #8]1048e:	6879      	ldr	r1, [r7, #4]10490:	4803      	ldr	r0, [pc, #12]	; (104a0 <thumb_ins+0x68>)10492:	4478      	add	r0, pc10494:	f004 fb60 	bl	14b58 <_IO_printf>ia = 0;10498:	e7d7      	b.n	1044a <thumb_ins+0x12>1049a:	bf00      	nop1049c:	0003f1be 	.word	0x0003f1be104a0:	0003f1ba 	.word	0x0003f1ba000104a4 <main>:}
}

最后将 a.out 放入目标板卡使用 chmod +x 添加执行权限,即可运行。

这篇关于arm 交叉编译 thumb 与 arm 指令的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/962039

相关文章

检查 Nginx 是否启动的几种方法

《检查Nginx是否启动的几种方法》本文主要介绍了检查Nginx是否启动的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1. 使用 systemctl 命令(推荐)2. 使用 service 命令3. 检查进程是否存在4

Java方法重载与重写之同名方法的双面魔法(最新整理)

《Java方法重载与重写之同名方法的双面魔法(最新整理)》文章介绍了Java中的方法重载Overloading和方法重写Overriding的区别联系,方法重载是指在同一个类中,允许存在多个方法名相同... 目录Java方法重载与重写:同名方法的双面魔法方法重载(Overloading):同门师兄弟的不同绝

MySQL字符串转数值的方法全解析

《MySQL字符串转数值的方法全解析》在MySQL开发中,字符串与数值的转换是高频操作,本文从隐式转换原理、显式转换方法、典型场景案例、风险防控四个维度系统梳理,助您精准掌握这一核心技能,需要的朋友可... 目录一、隐式转换:自动但需警惕的&ld编程quo;双刃剑”二、显式转换:三大核心方法详解三、典型场景

MySQL快速复制一张表的四种核心方法(包括表结构和数据)

《MySQL快速复制一张表的四种核心方法(包括表结构和数据)》本文详细介绍了四种复制MySQL表(结构+数据)的方法,并对每种方法进行了对比分析,适用于不同场景和数据量的复制需求,特别是针对超大表(1... 目录一、mysql 复制表(结构+数据)的 4 种核心方法(面试结构化回答)方法 1:CREATE

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

springboot中配置logback-spring.xml的方法

《springboot中配置logback-spring.xml的方法》文章介绍了如何在SpringBoot项目中配置logback-spring.xml文件来进行日志管理,包括如何定义日志输出方式、... 目录一、在src/main/resources目录下,也就是在classpath路径下创建logba

SQL Server中行转列方法详细讲解

《SQLServer中行转列方法详细讲解》SQL行转列、列转行可以帮助我们更方便地处理数据,生成需要的报表和结果集,:本文主要介绍SQLServer中行转列方法的相关资料,需要的朋友可以参考下... 目录前言一、为什么需要行转列二、行转列的基本概念三、使用PIVOT运算符进行行转列1.创建示例数据表并插入数

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

python项目打包成docker容器镜像的两种方法实现

《python项目打包成docker容器镜像的两种方法实现》本文介绍两种将Python项目打包为Docker镜像的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录简单版:(一次成功,后续下载对应的软件依赖)第一步:肯定是构建dockerfile,如下:第二步