IAR之函数和变量的绝对地址定位

转自:http://blog.csdn.net/slj_win/article/details/21516497

昨天我突然冒出个想法,能否利用函数和变量的绝对定位,实现程序的模块化更新。

也就是说,如果我要改变某个函数,只需要更新flash里面一个局部,也许只需要更新几百个字节,而无须重新下载整个上百K的程序。

经过查找资料和反复实验,终于实现了,现总结如下:

1) 把函数定位在FLASH高端的指定位置,以后更新,只更新那小块地方就可以了。

方法一:

IAR里面进行函数定位, 必须要在.icf里面,进行定义。

void sendstr(unsigned *buf,unsigned short  len) @".sendstr"
   {
    ....
   }

.icf文件,加入这样一句:
  place at address mem:0x08017000 { readonly section .sendstr};

方法二)  把要更新的函数,单独放在一个.c文件中,然后再.icf文件里面,对该文件进行定位:
  test.c

int f1(int a,int b){
  if(a>0){
   return (a+b)*1;
  }
  else return 0;
}
  int f2(int a,int b){
  if(a>0){
   return (a+b)*1;
  }
  else return 0;
}

那么在 .icf文件中,这样写:
place at address mem:0x08018000 { section .text object test.o };
编译完成后, f1就定位在0x08018000 处了,当然f2也紧跟在f1后面。整个test.c文件的所有函数,都在0x08018000 之后。

如果有多个函数需要单独更新,建议采用第二种方式, 只需要对c文件编译后的地址定位,那么该c文件的所有函数都定位了。

绝对定位的函数,只要指定了地址,那么在flash里面的位置就是固定的。

即使是两个不同的工程,比如第一个工程为实际工程,里面有所有的工程文件,  第二个工程为更新专用工程,里面仅仅只有test.c文件,里面的函数是同名的,定位地址与第一个工程也一样。

那么这样编译后,第二个工程里面的固件片断,是可以用来更新一个工程的固件的。

这样还可以派生出一个很怪的用法:
我可以把更新专用工程,公布给别人,他只需要在test.c里面,编写函数的具体内容。 然后一样可以更新产品的固件。
真正的实际工程,是不需要公布的。

以上是对函数的绝对定位处理。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2)变量定位

变量绝对定位:

__no_init char array1[100]@0x2000B000;

变量绝对定位,无须修改.icf,直接指定

这个array1就定位在RAM中的0x2000B000处

常量绝对定位:

const char str1[8]@".MYSEG"="test11!!";

常量绝对定位,需要改.icf文件:

place at address mem:0x08018500 { readonly section .MYSEG};

------------------------------------------------------------------------------------------------------------------------------------------

3)跨工程固件更新注意事项:

固件更新区的绝对定位的函数,不能随意调用其他库函数,那些被调用的函数也必须是绝对定位的。否则跨工程更新固件,会导致失败,因为被调用的函数在不同工程里,动态连接到的位置不同。

但是这个可以解决:被调用的函数,在两边工程都申明的绝对地址,并且在非固件更新区(就是两边工程的固件里,这些被调用函数的位置都一样,只需要函数名和地址一样即可,函数内部可以不同)。那么被这些调用的函数内,可以随意调用其他函数,如printf ,strcpy等库函数了。

绝对定位的函数,如果要使用常量,那么被使用的常量也必须是绝对定位的。否则跨工程更新固件,会导致失败。

绝对定位的函数,如果要使用全局变量,那么被使用的常量也必须是绝对定位的。否则跨工程更新固件,会导致失败。  而局部变量则不受此限制。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

###############################################################################
#                                                                             #
# IAR ELF Linker V5.50.0.51878/W32 for ARM              31/May/2012  12:50:09 #
# Copyright (C) 2007-2010 IAR Systems AB.                                     #
#                                                                             #
#    Output file  =  E:\stm32\software4.45.2\Debug\Exe\software.out           #
#    Map file     =  E:\stm32\software4.45.2\Debug\List\software.map          #
#    Command line =  E:\stm32\software4.45.2\Debug\Obj\main.o                 #
#                    E:\stm32\software4.45.2\Debug\Obj\test.o -o              #
#                    E:\stm32\software4.45.2\Debug\Exe\software.out --map     #
#                    E:\stm32\software4.45.2\Debug\List\software.map          #
#                    --config E:\stm32\software4.45.2\stm32f10x_flash.icf     #
#                    --semihosting --entry __iar_program_start                #
#                                                                             #
#                                                                             #
###############################################################################

*******************************************************************************
*** PLACEMENT SUMMARY
***

"A1":  place at 0x08004000 { ro section .intvec };
"A2":  place at 0x08017000 { ro section .sendstr };
"A3":  place at 0x08018500 { ro section .MYSEG };
"A4":  place at 0x08018000 { object test.o section .text };
"P1":  place in [from 0x08004000 to 0x08020000] { ro };
"P2":  place in [from 0x20000000 to 0x2000bfff] {
          rw, block CSTACK, block HEAP };

Section            Kind        Address   Size  Object
  -------            ----        -------   ----  ------
"A1":                                      0x40
  .intvec            ro code  0x08004000   0x40  vector_table_M.o [4]
                            - 0x08004040   0x40

"P1":                                     0x100
  .text              ro code  0x08004040   0x30  copy_init3.o [4]
  .text              ro code  0x08004070   0x2c  data_init3.o [4]
  .text              ro code  0x0800409c   0x28  iarttio.o [5]
  .iar.init_table    const    0x080040c4   0x14  - Linker created -
  .text              ro code  0x080040d8   0x16  cmain.o [4]
  .text              ro code  0x080040f0   0x14  exit.o [5]
  .text              ro code  0x08004104    0xc  cstartup_M.o [4]
  .text              ro code  0x08004110    0xa  cexit.o [4]
  .text              ro code  0x0800411a    0xa  main.o [1]
  .text              ro code  0x08004124    0x8  XShttio.o [3]
  .text              ro code  0x0800412c    0x6  exit.o [3]
  .text              ro code  0x08004132    0x4  low_level_init.o [3]
  .text              ro code  0x08004136    0x2  vector_table_M.o [4]
  Initializer bytes  ro data  0x08004138    0x8  <for P2 s0>
                            - 0x08004140  0x100

"A2":                                       0x2
  .sendstr           ro code  0x08017000    0x2  main.o [1]
                            - 0x08017002    0x2

"A4":                                      0x54
  .text              ro code  0x08018000   0x54  test.o [1]
                            - 0x08018054   0x54

"A3":                                      0x10
  .MYSEG             const    0x08018500   0x10  test.o [1]
                            - 0x08018510   0x10

"P2", part 1 of 2:                        0x400
  CSTACK                      0x20000000  0x400  <Block>
    CSTACK           uninit   0x20000000  0x400  <Block tail>
                            - 0x20000400  0x400

"P2", part 2 of 2:                          0x8
  P2 s0                       0x20000400    0x8  <Init block>
    .data            inited   0x20000400    0x8  XShttio.o [3]
                            - 0x20000408    0x8

*******************************************************************************
*** INIT TABLE
***

Address     Size
          -------     ----
Copy (__iar_copy_init3)
    1 source range, total size 0x8 (100% of destination):
          0x08004138   0x8
    1 destination range, total size 0x8:
          0x20000400   0x8

*******************************************************************************
*** MODULE SUMMARY
***

Module            ro code  ro data  rw data
    ------            -------  -------  -------
E:\stm32\software4.45.2\Debug\Obj: [1]
    main.o                 12
    test.o                 84       16
    -------------------------------------------
    Total:                 96       16

command line: [2]
    -------------------------------------------
    Total:

dl7M_tl_in.a: [3]
    XShttio.o               8        8        8
    exit.o                  6
    low_level_init.o        4
    -------------------------------------------
    Total:                 18        8        8

rt7M_tl.a: [4]
    cexit.o                10
    cmain.o                22
    copy_init3.o           48
    cstartup_M.o           12
    data_init3.o           44
    vector_table_M.o       66
    -------------------------------------------
    Total:                202

shb_l.a: [5]
    exit.o                 20
    iarttio.o              40
    -------------------------------------------
    Total:                 60

Gaps                    2
    Linker created                  20    1 024
-----------------------------------------------
    Grand Total:          378       44    1 032

*******************************************************************************
*** ENTRY LIST
***

Entry                    Address  Size  Type      Object
-----                    -------  ----  ----      ------
BusFault_Handler      0x08004137        Code  Wk  vector_table_M.o [4]
CSTACK$$Base          0x20000000         --   Gb  - Linker created -
CSTACK$$Limit         0x20000400         --   Gb  - Linker created -
DebugMon_Handler      0x08004137        Code  Wk  vector_table_M.o [4]
HardFault_Handler     0x08004137        Code  Wk  vector_table_M.o [4]
MemManage_Handler     0x08004137        Code  Wk  vector_table_M.o [4]
NMI_Handler           0x08004137        Code  Wk  vector_table_M.o [4]
PendSV_Handler        0x08004137        Code  Wk  vector_table_M.o [4]
Region

Base   0x080040c4         --   Gb  - Linker created -
RegionLimit  0x080040d8         --   Gb  - Linker created -
SVC_Handler           0x08004137        Code  Wk  vector_table_M.o [4]
SysTick_Handler       0x08004137        Code  Wk  vector_table_M.o [4]
UsageFault_Handler    0x08004137        Code  Wk  vector_table_M.o [4]
__cmain               0x080040d9        Code  Gb  cmain.o [4]
__exit                0x080040f1  0x14  Code  Gb  exit.o [5]
__iar_close_ttio      0x0800409d  0x26  Code  Gb  iarttio.o [5]
__iar_copy_init3      0x08004041  0x30  Code  Gb  copy_init3.o [4]
__iar_data_init3      0x08004071  0x2c  Code  Gb  data_init3.o [4]
__iar_lookup_ttioh    0x08004125   0x8  Code  Gb  XShttio.o [3]
__iar_program_start   0x08004105        Code  Gb  cstartup_M.o [4]
__iar_ttio_handles    0x20000400   0x8  Data  Lc  XShttio.o [3]
__low_level_init      0x08004133   0x4  Code  Gb  low_level_init.o [3]
__vector_table        0x08004000        Data  Gb  vector_table_M.o [4]
_call_main            0x080040e5        Code  Gb  cmain.o [4]
_exit                 0x08004111        Code  Gb  cexit.o [4]
_main                 0x080040eb        Code  Gb  cmain.o [4]
exit                  0x0800412d   0x6  Code  Gb  exit.o [3]
f1                    0x08018049   0xc  Code  Gb  test.o [1]
main                  0x0800411b   0xa  Code  Gb  main.o [1]
sendstr               0x08017001   0x2  Code  Gb  main.o [1]
str1                  0x08018500   0x8  Data  Gb  test.o [1]
str2                  0x08018508   0x8  Data  Gb  test.o [1]
test                  0x08018001  0x44  Code  Gb  test.o [1]

[1] = E:\stm32\software4.45.2\Debug\Obj
[2] = command line
[3] = dl7M_tl_in.a
[4] = rt7M_tl.a
[5] = shb_l.a

378 bytes of readonly  code memory
     44 bytes of readonly  data memory
  1 032 bytes of readwrite data memory

Errors: none
Warnings: none

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

这是.icf文件

/*###ICF### Section handled by ICF editor, don't touch! ****/
/*-Editor annotation file-*/
/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
/*-Specials-*/
define symbol __ICFEDIT_intvec_start__ = 0x08004000;
/*-Memory Regions-*/
define symbol __ICFEDIT_region_ROM_start__   = 0x08004000;
define symbol __ICFEDIT_region_ROM_end__     = 0x08020000;
define symbol __ICFEDIT_region_RAM_start__   = 0x20000000;
define symbol __ICFEDIT_region_RAM_end__     = 0x2000BFFF;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__   = 0x400;
define symbol __ICFEDIT_size_heap__     = 0x200;
/**** End of ICF editor section. ###ICF###*/

define memory mem with size = 4G;
define region ROM_region   = mem:[from __ICFEDIT_region_ROM_start__   to __ICFEDIT_region_ROM_end__];
define region RAM_region   = mem:[from __ICFEDIT_region_RAM_start__   to __ICFEDIT_region_RAM_end__];

define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };
define block HEAP      with alignment = 8, size = __ICFEDIT_size_heap__     { };

initialize by copy { readwrite };
do not initialize  { section .noinit };

place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };

place at address mem:0x08017000 { readonly section .sendstr};
place at address mem:0x08018500 { readonly section .MYSEG};

place at address mem:0x08018000 { section .text object test.o };

place in ROM_region   { readonly };
place in RAM_region   { readwrite,
                        block CSTACK, block HEAP };

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

main.c

/* Includes ------------------------------------------------------------------*/
//#include "stm32f10x.h"
#include "string.h"
#include "stdio.h"

void sendstr(unsigned *buf,unsigned short  len) @".sendstr"
{
}

extern void test(void);
extern void main1(void);
int main(void)
{
     test();
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)

  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/**
  * @}
  */

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

test.c

//__no_init char array1[100]@0x2000B000;

//char array1[100];

const char str1[8]@".MYSEG"="test11!!";
const char str2[8]@".MYSEG"="test66!!";

int f1(int a,int b);
void test(void)// @".test" //MYSEGMENT段可在XCL中开辟
{
    char arrayx[150];  
    char array1[150];
    int i,a,b,c;
    for (i=0;i<8;i++){
      array1[i]=str1[i];      
      arrayx[i]=str2[i];      
    }
    a=1;
    b=2;
    c=f1(a,b);
    for (i=0;i<c;i++) {
      sendstr(array1,8);    
      sendstr(arrayx,8);    
    }
}

int f1(int a,int b){
  if(a>0){
   return (a+b)*1;
  }
  else return 0;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

对于你的第三点,cortex和其他arm处理器都提供了一种特殊的中断。。。。Software Interrupt (SVC)
你只要把系统服务都通过svc来调用,那么就不需要知道调用的实际地址了。。。
当然你也可以在固定的位置放一个函数指针表来手动做这个,调用系统服务的时候从函数指针表上获得当前版本的函数的绝对定位

IAR之函数和变量的绝对地址定位相关推荐

  1. IAR STM32 函数和变量的绝对地址定位

    昨天我突然冒出个想法,能否利用函数和变量的绝对定位,实现程序的模块化更新. 也就是说,如果我要改变某个函数,只需要更新flash里面一个局部,也许只需要更新几百个字节,而无须重新下载整个上百K的程序. ...

  2. IAR的const,变量指定绝对地址,函数指定存取区域

    今天要用到ROM-DATA,看了下IAR编译器手册,记录如下:

  3. Python+Selenium WebDriver API:浏览器及元素的常用函数及变量整理总结

    由于网页自动化要操作浏览器以及浏览器页面元素,这里笔者就将浏览器及页面元素常用的函数及变量整理总结一下,以供读者在编写网页自动化测试时查阅. from selenium importwebdriver ...

  4. python文件定位函数_C语言中文件定位函数总结

    C语言中文件定位函数主要是:fseek, ftell, fsetpos, fgetpos. 先来讲前两个函数,这是最基本的定位函数: fseek函数:能把文件指针移动到文件任何位置,其原型是:int ...

  5. 未定义的函数或变量【一文讲透】(Matlab)

    目录 1 概述 2 问题所在 3 <葵花宝典>--解决策略 3.1 验证函数或变量名称的拼写 3.2 编写的文件名需要和函数名一致   3.3 验证输入是否符合函数语法 3.4 确保安装了 ...

  6. 怎样在javascript函数中将变量传递给服务端脚本程序?

    怎样在javascript函数中将变量传递给服务端脚本程序? (有朋友问上述问题, 愿把结论分享给大家). 摘 要:   服务器端脚本运行时, 它只会解释执行<% %>或<?php ...

  7. R语言使用GGally包的ggpairs函数可视化变量相关性分析图:包含散点图、密度图、柱状图、箱图等、并自定义数据点的大小

    R语言使用GGally包的ggpairs函数可视化变量相关性分析图:包含散点图.密度图.柱状图.箱图等.并自定义数据点的大小 目录

  8. php 函数 变量,PHP函数中变量的说明

    PHP的变量的范围 局部变量: 在函数中声明的变量就是局部变量,只能在自己的函数内部使用. 全局变量: 在函数外声明,在变量声明以后的,直到整个脚本结束前都可以使用,包括在函数中和{}中都可使用 PH ...

  9. javascript 红宝书笔记之函数、变量、参数、作用域

    ECMAScript 不介意传进来多少个参数,也不介意传进来的参数类型. 理解参数: 命名的参数只提供便利,不是必需的. ECMAScript 的变量包含两种不同的数据类型的值.分别是 简单的数据构成 ...

最新文章

  1. 性能测试场景设计之用户模式设置
  2. C#设计模式系列:原型模式(Prototype)
  3. Android:打造“万能”Adapter与ViewHolder
  4. 优雅得使用composer来安装各种PHP小工具
  5. 【Android 界面效果22】Android的Tab与TabHost
  6. 命名空间不能直接包含字段或方法之类的成员_Linux内存取证:解析用户空间进程堆(中)...
  7. Unity 编译apk启动出异常
  8. 一起学React--组件定义和组件通讯
  9. 腾讯云 Centos 配置 JDK Tomcat Mysql
  10. 应用css div进行页面布局设计,利用CSS与DIV进行页面布局.ppt
  11. React ref的转发
  12. FreeAnchor:令anchor自由匹配标签的策略
  13. ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory解决方案
  14. python怎么读数据库_Python如何读写SQLite数据库
  15. webpack + vue 学习看过的文章 网址
  16. Python实战题 · 计算圆面积
  17. 使用Matlab绘制gif动图
  18. java espresso_java – 带有片段的Android Espresso功能测试
  19. 深大uooc学术道德与学术规范教育第二章
  20. k8s集群管理(一)

热门文章

  1. 图解从 URL 到网页通信原理
  2. 6.MYSQL视图的使用和管理
  3. Android --- Android Device Monitor 在 3.0 版本之后废弃了怎么办
  4. 标签的属性和样式属性有什么区别
  5. win定时关机_如何设置电脑定时关机?电脑新手有必要掌握一波!
  6. laravel mysql json_mysql – Laravel JSON where子句不区分大小写
  7. 数据中心空调系统中的冷却塔应用手册
  8. 什么是数据中心,它们是如何变化的?
  9. 数据中心支持物联网的5种方式
  10. 综合布线管理系统之智能配线架的过去与将来