如何为裸机臂应用程序编写动态加载程序 [英] How to write dynamic loader for bare-metal arm-application

查看:59
本文介绍了如何为裸机臂应用程序编写动态加载程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于 arm9 处理器的项目.我们只使用没有任何操作系统的裸机,所以很遗憾我们还不支持共享库/动态加载器.

I'm working on a project based on arm9 processor. We use only bare-metal without any operating system, so unfortunately we have no support for shared libraries / dynamic loader yet.

我希望能够从例如 SD 卡加载库,这也可以从主应用程序调用函数.

I would like to be able to have libraries loaded for example from SD card, which can also call the functions from the main application.

我的第一次尝试是使用链接器覆盖功能(将库放置在特定的绝对定位部分),但是正如我之前提到的,调用主应用程序函数存在问题 -> 随着主应用程序的每次更改,库必须重新编译才能回调.

My first try was to use linker overlay capability (placing the library in specific absolutely positioned sections), but here is a problem with calling main app functions as I mentioned earlier -> with each change of the main application the libraries has to be recompiled again to be able to callback.

根据这一点,我将不得不编写自己的动态加载器,但我是这方面的新手.请有人给我任何例子如何处理它或如何开始这样的项目?我们使用 gcc 作为 arm-elf 目标.

According to this I thing I will have to write my own dynamic loader, but I'm newbie in this area. Could please someone give me any example how to deal with it or how to start with such project? We are using gcc for arm-elf target.

问候扬

推荐答案

检查 本应用笔记.它详细描述了动态链接的工作原理以及编写自己的动态加载器需要做什么.它还提供了一些替代方案.我认为跳转表很容易实现,可以解决您更改 API 地址的问题.

Check this application note. It describes in some details how dynamic linking works and what you need to do to write your own dynamic loader. It also gives some alternatives to that. I think the jump tables one is quite easy to implement and will resolve your issue with changing API addresses.

编辑:这里是如何做一个简单的跳转表.首先,确定需要从主程序导出哪些函数.然后做一个函数指针的结构:

Edit: Here's how to do a simple jump table. First, decide which functions you need exported from your main program. Then make a structure of function pointers:

typedef struct _MyAPI
{
  int    (*init)(int flags);
  int    (*exit)(int exitcode);
  void * (*getmem)(size_t size);
  void   (*freemem)(void *ptr);
} MyAPI;

在主程序中,定义这个结构的一个实例,填写指针,并将其放置在某个预定义的地址:

In the main program, define an instance of this structure, fill in the pointers, and place it at some predefined address:

#include <jumptbl.h>
int    main_init(int flags)
{
  return 0;
}
//...
MyAPI main_API __attribute__((section(".jumptbl"))) = 
{
  &main_init,
  &main_exit,
  &main_getmem,
  &main_freemem,
};

(如果你使用这种方法,你需要在链接器文件中描述 .jumptbl 部分并确保它得到一个固定的地址)

(if you use this approach, you will need to describe .jumptbl section in the linker file and make sure it gets a fixed address)

在加载的模块中,获取跳转表的指针,并用它来调用主程序:

In the loaded module, get the pointer to the jumptable and use it to call the main program:

#include <jumptbl.h>

MyAPI *pAPI = (MyAPI*)(0x1000000); // there should be a better way to do this

int main()
{
  pAPI->init(0);
  void *block = pAPI->getmem(0x30);
  //...
}

希望这会有所帮助!

这篇关于如何为裸机臂应用程序编写动态加载程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆