为 GNU .ld 脚本原点定义一个符号常量

Defining a symbolic constant for GNU .ld script origin

我正在创建一个 GNU .ld 链接描述文件,并想以符号方式定义一些内存部分的来源。以下无效:

BASE_ADDR = 0x4000;

MEMORY
{
  m_interrupts        (RX) : ORIGIN = BASE_ADDR, LENGTH = 0x0200
  m_bootloader_config (RX) : ORIGIN = BASE_ADDR + 0x3C0, LENGTH = 0x0040
  m_text              (RX) : ORIGIN = BASE_ADDR + 0x400, LENGTH = 0x10000 - (BASE_ADDR + 0x400)
  m_data              (RW) : ORIGIN = 0x1FFFF000, LENGTH = 0x4000
}

这会导致以下错误:

Invoking: Cross ARM C++ Linker
../MKL27Z64xxx4_flash.ld:67: nonconstant expression for origin
collect2: error: ld returned 1 exit status

错误指的是以下行:

m_interrupts        (RX) : ORIGIN = BASE_ADDR, LENGTH = 0x0200

让我感到困惑的是 BASE_ADDR 对我来说看起来很稳定。我需要调用一些特殊的语法来说服 ld BASE_ADDR 是常量吗?

LD手册状态,内存:

"The origin is an expression for the start address of the memory region. The expression must evaluate to a constant before memory allocation is performed, which means that you may not use any section relative symbols. The keyword ORIGIN may be abbreviated to org or o (but not, for example, ORG). "

而表达式:BASE_ADDR = 0X4000;是节相关符号

以下文字也适用:

"However, other values (such as symbol values) are not known or needed until after storage allocation. Such values are evaluated later, when other information (such as the sizes of output sections) is available for use in the symbol assignment expression. "

建议在内存分配中使用硬编码值