【发布时间】:2016-03-06 01:28:35
【问题描述】:
我正在开源环境中使用 arm-none-eabi-gcc v4.9.3 为 ARM stm32 微控制器编译 C 代码。 代码在没有编译器优化的情况下运行良好(gcc -g -O0 ...)。
当我启用最轻微的优化 (gcc -g -O1 ...) 时,链接器定义变量的地址会改变。
文件内存.ld
register_cortexm3_ACTLR = 0xe000e008;
...
MEMORY
{
rom (rx ) : ORIGIN = 0x08000000, LENGTH = 256K
ram (rxw) : ORIGIN = 0x20000000, LENGTH = 64K
sram_bitband (rw ) : ORIGIN = 0x22000000, LENGTH = 32768K
peripheral (rw ) : ORIGIN = 0x40000000, LENGTH = 1024K
peripheral_bitband (rw ) : ORIGIN = 0x42000000, LENGTH = 32768K
sram (rwx) : ORIGIN = 0x60000000, LENGTH = 1048576K
ram_external (rwx) : ORIGIN = 0x60000000, LENGTH = 1048576K
code (rwx) : ORIGIN = 0x60000000, LENGTH = 1048576K
device_external (rw ) : ORIGIN = 0xa0000000, LENGTH = 1048576K
private_peripheral (rw ) : ORIGIN = 0xe0000000, LENGTH = 1024K
vendor_memory (rw ) : ORIGIN = 0xe0100000, LENGTH = 523264K
}
SECTIONS
{
/* Section for variables mapped onto registers */
.peripherals (OVERLAY) :
{
. = ALIGN(4);
*(.peripherals)
} >peripheral
.private_peripherals (OVERLAY) :
{
. = ALIGN(4);
*(.private_peripherals)
} >private_peripheral
...
文件 register_cortexm3.h
typedef struct {
unsigned DISMCYCINT : 1;
unsigned DISDEFWBUF : 1;
unsigned DISFOLD : 1;
unsigned reserved1 : 13;
unsigned reserved2 : 16;
} __attribute__( ( __packed__ ) ) register_cortexm3_actlr_t;
...
文件 register_cortexm3.c
void check_same( volatile void* Address1, volatile void* Address2 ) {
if ( Address1 != Address2 )
{ Assert_Halt_EC( ec_InvalidImplementation ); }
}
volatile register_cortexm3_actlr_t register_cortexm3_ACTLR
__attribute__( ( section( ".private_peripherals" ) ) );
void register_cortexm3_prepare() {
// checking for correct linker script settings
check_same(&( register_cortexm3_ACTLR ), ( volatile void* ) 0xe000e008);
}
启用优化后,上述比较失败,因为在 check_same() 函数内部,根据 gdb,第一个参数是 0xe0000000(其内存段的起始地址):
Breakpoint 1, Assert_Halt_EC (ErrorCode=ErrorCode@entry=ec_InvalidImplementation) at ttc-lib/ttc_basic.c:65
65 void Assert_Halt_EC( volatile ErrorCode_e ErrorCode ) { // block endless
(gdb) up
#1 0x08004440 in check_same (Address1=Address1@entry=0xe0000000, Address2=Address2@entry=0xe000e008) at ttc-lib/ttc_basic.c:58
58 { Assert_Halt_EC( ec_InvalidImplementation ); }
(gdb) up
#2 0x080034f6 in register_cortexm3_prepare () at ttc-lib/register/register_cortexm3.c:40
40 Assert_SameAddress( &( register_cortexm3_ACTLR ), ( void* ) 0xe000e008 );
(gdb) x &( register_cortexm3_ACTLR )
0xe000e008: 0x00000000
正如您在最后一个 gdb 输出行中看到的,gdb 知道 register_cortexm3_ACTLR 的正确地址。
这是 gcc 错误还是功能? 如何解决?
【问题讨论】:
-
在哪里定义register_cortexm3应该在0xe000e008?
-
在 memory.ld 的第一行,就在 ...
标签: c gcc optimization linker memory-mapping