最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

How do I prevent uninitialized data from being initialized to zero?

工作和技术 crifan 951浏览 0评论

How do I prevent uninitialized data from being initialized to zero?

Applies to: ARM Developer Suite (ADS), Compilers, General Build Issues, Linker, RealView Developer Kit for XScale (RVXDK), RealView Developer Suite (RVDS) 2.0, RealView Developer Suite (RVDS) 2.1, RealView Developer Suite (RVDS) 2.2, RealView Development Suite (RVDS) 3.0, RealView Development Suite (RVDS) 3.1

The ANSI C specification states that static data that is not initialized explicitly should be initialized to zero. The compiler therefore places both zero-initialized and uninitialized data in the same ZI section, which is zero-filled at run time by the C library initialization code.

There may be situations where you do not want uninitialized data to be initialized to zero. For example, you may have memory-mapped peripherals or non-volatile memory which you do not want to be initialized at run-time.

If you do not want ZI sections to be initialized to zero, use the UNINIT scatter-file execution region attribute. UNINIT is described in the Linker and Utilities Guide.

UNINIT prevents all of the ZI data in the execution region from being initialized to zero, including both zero-initialized and uninitialized data. To stop only uninitialized data from being initialized to zero, you must place it in a different section to any explicitly zero-initialized data, using the compiler pragma; ‘#pragma arm section‘. #pragma arm section is documented in your Compilers and Libraries Guide, and your Linker and Utilities Guide, found at our Software Development Tools Documentation page.

Note: #pragma arm section can only be used with ADS 1.2 and later compilers. In RVCT 2.1 and later the GNU __attribute__ ((section ("…")))syntax can be used instead of #pragma arm section, when compiling with –gnu.

For example, given the C code:

int i, j;         //uninit (in .bss section)<br />int k=0, l=0;     //zero-init (in .bss section)

You can use #pragma arm section to place the uninitialized data into a different section to the zero-initialized data, e.g:

#pragma arm section zidata = &quot;non_init&quot;<br />int i, j;                                 //uninit (in non_init section)<br />#pragma arm section zidata                //back to default (.bss section)<br />int k=0, l=0;                             //zero-init (in .bss section)

The non_init section can then be placed into its own UNINIT execution region, e.g:

LOAD_1 0x0<br />{<br />  EXEC_1 +0<br />  {<br />    * (+RO)<br />    * (+RW)<br />    * (+ZI)         ;ZI data will be initialized to zero<br />  }
EXEC_2 +0 UNINIT<br />  {<br />    * (non_init)    ;ZI data will not be initialized to zero<br />  }<br />}

转载请注明:在路上 » How do I prevent uninitialized data from being initialized to zero?

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

  1. 嘿嘿,谢谢,知道了,忘记了
    ssrainball11年前 (2008-11-13)回复
  2. 你的博客挺不错的,看到你EMC笔试题,我觉得很亲切,我明天要参加EMC的笔试了,谢谢你的资料啊,还有,你现在在哪里工作啊?觉得怎么样,做软件感觉辛苦吗?
    ssrainball11年前 (2008-10-24)回复
59 queries in 0.126 seconds, using 18.85MB memory