且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为裸机臂应用程序编写动态加载程序

更新时间:2023-10-13 08:10:28

检查 本应用笔记.它详细描述了动态链接的工作原理以及编写自己的动态加载器需要做什么.它还提供了一些替代方案.我认为跳转表很容易实现,可以解决您更改 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);
  //...
}

希望这有帮助!