例如,我想在0x10000000,0x100100000,0x100200000,.中放置一个特殊的数据.为此,我想在头文件中做,

#define DATA_START 0x100000000
#define DATA_GAP 0x100000
#define DATA0_START DATA_START + 0*DATA_GAP
#define DATA1_START DATA_START + 1*DATA_GAP
#define DATA2_START DATA_START + 2*DATA_GAP

现在,我想要有一个链接器脚本,并在那里定义部分,并想做一些类似的事情.(以便C程序和链接器脚本匹配).

. = DATA_START;
.mydata : {
  *(.data0)
   . = ALIGN(DATA_GAP);
  *(.data1)
   . = ALIGN(DATA_GAP);
...
}

of course with enough value for DATA_GAP so that data don't overlap.
But in linker script, the define statement is not "#define DATA_START 0x100000000" but "DATA_START = 0x100000000;". So if I want to use a single file to be used in C and linker script, how can I do it?? (I read somewhere that in programs there should be single point of truth, without having to fix data in multiple places..)
I know how to declare a variable in linker script and use it C program (using "var = ." in linker sciprt and using &var in C program..), but this time I want to statically allocate some data.

推荐答案

Marco Bonelli's answer can be a solution but I though this method is simpler.
In Makefile, I added this command in the recipe before the actual compilation.

Sed-e‘S/#定义([A-Z0-9_][A-Z0-9_]) (.)/\1=\2;/’ Sections.h>;Linkadd.h

现在,实际的链接器脚本的开头有这一行.

INCLUDE linkadd.h

因此,在编译之前,C头文件如下所示

#define DATA_START 0x82000000
#define DATA_GAP 0x100000
#define ARGBUF0 DATA_START
#define ARGBUF1 DATA_START + DATA_GAP
#define ARGBUF2 DATA_START + 2*DATA_GAP

链接器脚本看起来像

INCLUDE linkadd.h

  . = DATA_START;
  .mydata : {
    _mydata_start = .;
        . = ARGBUF0;
        *(.data_args0)
        . = ARGBUF1;
        *(.data_args1)
        . = ARGBUF2;
        *(.data_args2)

我觉得这个看起来好多了.

C++相关问答推荐

intellisense不工作,甚至已经下载了c/c++扩展

来自stdarg.h的c中的va_args无法正常工作<>

如何将字符串传递给函数并返回在C中更改的相同字符串?

LONG_DOUBLE_T是否存在(标准C:C23)

字符是否必须转换为无符号字符,然后才能与getc家族的返回值进行比较?

`#if`条件中是否允许`sizeof`?

#定义SSL_CONNECTION_NO_CONST

如何在GET_STRING输入后对少数几个特定字符串进行C判断?

在进程之间重定向输出和输入流的问题

在函数外部使用内联ASM时无法指定操作数

用C++构建和使用DLL的困惑

Fprintf正在写入多个 struct 成员,并且数据过剩

我可以创建适用于不同endian的 colored颜色 struct 吗?

在运行时判断C/C++指针是否指向只读内存(在Linux操作系统中)

Realloc():中止的下一个大小无效(核心转储)

如何在C宏定义中包含双引号?

为什么会导致分段故障?(C语言中的一个程序,统计文件中某个单词的出现次数)

从文件到链表读取日期

'printf("%s", user_input)' 危险吗?

const struct 成员的 typedef 中的灵活数组大小