如何在晶心平台实作ROM patch
(2) patch在编译之前,先汇入主程序的symbol table。(将export.txt档案放在一起编译)。Patch的linker script要汇入主程序的symbol,写法如下面红色字体。
本文引用地址:https://www.eepw.com.cn/article/135126.htm ENTRY(_start)
/* Do we need any of these for elf?
__DYNAMIC = 0; */
INCLUDE "..export.txt"
SECTIONS
{
(3) patch的程序代码里如下,没有main function,也不要加入startup files。改写func2。func2放在flash的FUNC_PATCH section。并且将jump_table里的func2,改成指向新的func2。
#include
#include
extern int func1(int);
extern int func3(int);
int func2(int) __attribute__ ((section ("FUNC_PATCH")));
extern int num2;
typedef struct strfunptr {
int (*func_a)(int);
int (*func_b)(int);
int (*func_c)(int);
}sfptr;
sfptr jump_table __attribute__ ((section ("FUNC_TABLE")))= {func1, func2, func3};
int func2(int x){
return x*num2*100;
}
(4) patch的linker script,加入FUNC_PATH在jump_table之后。
FUNC_PATCH 0x510020 :
{
*(.FUNC_PATCH)
}
3. 如何除错
首先,将程序代码存放在IC的ROM及flash里。(本文为了示范,我们的做法是在AndeShape™ ADP-XC5的FPGA板上,用RAM模拟ROM及flash,分别将主程序和patch的bin文件restore到板子上。)
当gdb debug时,载入patch 的symbol。以下节录gdb指令。
core0(gdb) file mainprog.adx
core0(gdb) add-symbol-file patch.adx 0x500000 -s FUNC_TABLE 0x510000 -s FUNC_PATCH 0x510020
core0(gdb) set $pc=0x500000
core0(gdb) b main
Breakpoint 1 at 0x50010c: file ../main.c, line 20.
core0(gdb) c
Breakpoint 1, main () at ../main.c:20
20 printf("func1(30)=%dn",jump_table.func_a(30));
core0(gdb) s
func1 (x=30) at ../main.c:28
28 return x*num1;
core0(gdb) n
29 }
core0(gdb) s
main () at ../main.c:21
21 printf("func2(30)=%dn",jump_table.func_b(30));
core0(gdb) s
func2 (x=30) at ../patchprog.c:24
24 return x*num2*100;
core0(gdb)
上面过程中,先加载main的symbol,再加载patch的symbol及debug information。"add-symbol-file patch.adx 0x500000 -s FUNC_TABLE 0x510000 -s FUNC_PATCH 0x510020"是将patch section的symbol及debug information也载入gdb以debug。读者可以在gdb里,打"help add-symbol-file"查阅add-symbol-file的用法。
3.1 主程序patch后的执行结果
func1(30)=30
func2(30)=6000
func3(30)=90
4. 结语
目前晶心科技使用GNU的toolchain,其功能非常强大。读者可多动手试试不同的linker script写法,使得开发firmware更有弹性及效率。
评论