外部变量似乎有两个地址?

时间:2016-12-22 17:06:50

标签: c gcc arm extern memory-address

我正在使用一个在.h中声明uint32_t并在.c中定义它的库。程序无法读取变量并崩溃。

宣言:

extern uint32_t SystemCoreClock;

定义:

uint32_t SystemCoreClock = 4000000;

使用变量:

uint32_t tickNum=123, *ptr1=NULL, *ptr2=NULL; // added by me to debug
ptr1 = &tickNum;                              // added by me to debug
ptr2 = &SystemCoreClock;                      // added by me to debug
tickNum = SystemCoreClock;                    // added by me to debug

然后我用gdb检查地址。

执行前3行。

普通内容(ptr1 == &ticknum*ptr1 == ticknum):

(gdb) p &tickNum
$3 = (uint32_t *) 0x20017fdc
(gdb) p ptr1
$4 = (uint32_t *) 0x20017fdc
(gdb) p *ptr1
$5 = 123

现在执行最后一行代码。

奇怪的东西(ptr2 != &SystemCoreClock*ptr2 != SystemCoreClock):

(gdb) p &SystemCoreClock
$6 = (uint32_t *) 0x20000004 <SystemCoreClock>
(gdb) p ptr2
$7 = (uint32_t *) 0x681b4b20           // should be 0x20000004, beyond end of memory (see memory map bellow)
(gdb) p SystemCoreClock
$8 = 4000000
(gdb) p *ptr2
$9 = 0                                 // should be 4000000
(gdb) p tickNum 
$10 = 0                                // should be 4000000

怎么可能?!

内存映射,我对链接器脚本的猜测:

0x20000000 - 0x20018000, LENGTH = 96K       RAM
0x10000000 - 0x10008000, LENGTH = 32K       RAM
0x08000000 - 0x08100000, LENGTH = 1024K     FLASH

真正的链接描述文件:

/* Specify the memory areas */
MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 96K
RAM2 (xrw)      : ORIGIN = 0x10000000, LENGTH = 32K
FLASH (rx)      : ORIGIN = 0x8000000, LENGTH = 1024K
}
  • 目标:STM32L4 ARM-CortexM4 MCU
  • 主持人:Linux,Qt Creator,arm-none-eabi-gcc,OpenOCD
  • 版本:CubeMX v4.18.0,STM32L4包v1.6.0,gcc v5.4.1

0 个答案:

没有答案