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

相关文章

基于Python实现读取嵌套压缩包下文件的方法

《基于Python实现读取嵌套压缩包下文件的方法》工作中遇到的问题,需要用Python实现嵌套压缩包下文件读取,本文给大家介绍了详细的解决方法,并有相关的代码示例供大家参考,需要的朋友可以参考下... 目录思路完整代码代码优化思路打开外层zip压缩包并遍历文件:使用with zipfile.ZipFil

Python处理函数调用超时的四种方法

《Python处理函数调用超时的四种方法》在实际开发过程中,我们可能会遇到一些场景,需要对函数的执行时间进行限制,例如,当一个函数执行时间过长时,可能会导致程序卡顿、资源占用过高,因此,在某些情况下,... 目录前言func-timeout1. 安装 func-timeout2. 基本用法自定义进程subp

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

电脑死机无反应怎么强制重启? 一文读懂方法及注意事项

《电脑死机无反应怎么强制重启?一文读懂方法及注意事项》在日常使用电脑的过程中,我们难免会遇到电脑无法正常启动的情况,本文将详细介绍几种常见的电脑强制开机方法,并探讨在强制开机后应注意的事项,以及如何... 在日常生活和工作中,我们经常会遇到电脑突然无反应的情况,这时候强制重启就成了解决问题的“救命稻草”。那

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

Python中的魔术方法__new__详解

《Python中的魔术方法__new__详解》:本文主要介绍Python中的魔术方法__new__的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、核心意义与机制1.1 构造过程原理1.2 与 __init__ 对比二、核心功能解析2.1 核心能力2.2