×
单片机 > 单片机程序设计 > 详情

在ARM64位开发板上兼容ARM32位的可执行程序

发布时间:2021-03-16 发布时间:
|

  在嵌入式开发时,经常会通过编译busybox来制作rootfs,然后在上面跑可执行程序。那么如何通过修改配置,让一个rootfs同时兼容32位和64位的可执行程序呢?


  我们知道,如果EL1运行在64位时,那么EL0既可以运行在64位,也可以运行在32位。具体到这里,我们让内核运行在64位(EL1),然后在EL0运行64位或者32位的可执行程序,在加载32位的可执行程序时,会先陷入Linux内核(EL1),然后通过解析elf文件发现EL0要运行在32位模式,那么在eret异常返回到EL0时会通过SPSR将当前cpu切到32位模式运行。

正文

1、配置linux kernel

使kernel支持在EL0上运行32位可执行程序:(arch/arm64/Kconfig中)


config COMPAT

    bool "Kernel support for 32-bit EL0"

    depends on ARM64_4K_PAGES || EXPERT

    select COMPAT_BINFMT_ELF if BINFMT_ELF

    select HAVE_UID16

    select OLD_SIGSUSPEND3

    select COMPAT_OLD_SIGACTION

    help

      This option enables support for a 32-bit EL0 running under a 64-bit

      kernel at EL1. AArch32-specific components such as system calls,

      the user helper functions, VFP support and the ptrace interface are

      handled appropriately by the kernel.


      If you use a page size other than 4KB (i.e, 16KB or 64KB), please be aware

      that you will only be able to execute AArch32 binaries that were compiled

      with page size aligned segments.


      If you want to execute 32-bit userspace applications, say Y.


具体是在:Userspace binary formats --> Kernel support for 32-bit EL0

然后重新编译内核

 

2、配置根文件系统

这一步需要视情况而定,如果32位的可执行程序采用的是静态链接的,那么此时应该就可以直接运行。如果采用的是动态链接,那么就需要专门的设置了。

  • 在rootfs的根目录下创建/lib32目录,然后将要用到的32位的动态库拷贝到其中

  • 设置LD_LIBRARY_PATH环境变量,将刚才的路径添加到其中:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib32

  • 进入/lib目录,在其中创建一个指向/lib32/ld-linux-armhf.so.3的软连接:ld-linux-armhf.so.3

 

3、测试

  • 运行32位可执行程序

在64位系统里运行一个32位的测试程序:

然后可以查看其虚拟内存地址分配

 

  • 运行64位可执行程序

 

 查看其地址分配:

 

完。



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
如何为单片机选择合适的负载电容