简体   繁体   English

如何将C代码编译为固件补丁并获取所有功能的位置?

[英]How can I compile c code into a firmware patch and get the location of all the functions?

I'm compiling some firmware patches. 我正在编译一些固件补丁。 These are normally written in C then patched into a firmware image to change the behavior of a hardware device. 这些通常以C编写,然后修补到固件映像中以更改硬件设备的行为。 The trick is that I will replace a function call in the original firmware to dispatch to my patch, do my trickery then call the original function and finally return. 诀窍是,我将替换原始固件中的函数调用以分派给我的补丁程序,进行欺骗后再调用原始函数,最后返回。

Here is a simple example: 这是一个简单的示例:

__attribute__ ((section (".patches")))

void clean_memory() __attribute__ ((section (".patches")));

void clean_memory() {
  int *i;
  for (i=(int *)0x80000;i<(int *)0x84000;i++)
    *(i)=0;
  asm("jsr      0x12345\n\t"); // call the original function
}

I'm compiling it with the following: 我正在用以下代码进行编译:

m68k-rtems-gcc -mcpu32 -Wl,--section-start=.patches=0x31000 -O2 -c patches.c
m68k-rtems-ld -nostdlib --oformat=binary -M patches.o -o patches.bin

I would really like to automate this process rather than hand-patching the file every time I make a modification to the patch. 我真的很想自动化这个过程,而不是每次修改补丁时都手动打补丁。

How can I get a list of the offsets and lengths where each function in the patches.bin file exist? 如何获取patchs.bin文件中每个函数存在的偏移量和长度的列表? This is important when I patch the original function call since the offsets will change as the size of every function in the patch changes 当我修补原始函数调用时,这一点很重要,因为偏移量会随着修补程序中每个函数大小的变化而变化

This information should be contained in the mapfile which you are already generating with the -M option. 此信息应包含在已经使用-M选项生成的-M This sends it to a default mapfile. 这会将其发送到默认的映射文件。 You can specify your own with something like -Map patches.map . 您可以使用-Map patches.map类的-Map patches.map来指定自己的-Map patches.map

Generating a cross reference table in the mapfile with -cref may also contain some useful information. 使用-cref-cref生成交叉引用表也可能包含一些有用的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM