如何将变量放在内存中给定的绝对地址(使用GCC)

时间:2022-06-19 00:09:42

The RealView ARM C Compiler supports placing a variable at a given memory address using the variable attribute at(address):

RealView ARM C编译器支持在给定内存地址中使用变量属性(地址)放置变量:

int var __attribute__((at(0x40001000)));
var = 4;   // changes the memory located at 0x40001000

Does GCC have a similar variable attribute?

GCC是否有类似的变量属性?

4 个解决方案

#1


18  

I don't know, but you can easily create a workaround like this:

我不知道,但你可以很容易地创建这样的解决方案:

int *var = (int*)0x40001000;
*var = 4;

It's not exactly the same thing, but in most situations a perfect substitute. It will work with any compiler, not just GCC.

它不是完全相同的东西,但在大多数情况下,它是一个完美的替代品。它将与任何编译器一起工作,而不仅仅是GCC。

If you use GCC, i assume you also use GNU ld (although it is not a certainty, of course) and ld has support for placing variables wherever you want them.

如果您使用GCC,我假设您也使用GNU ld(当然,这并不是必然的),而且ld支持将变量放置在您想要的任何地方。

I imagine letting the linker do that job is pretty common.

我想让链接器做那个工作是很常见的。

Inspired by answer by @rib, I'll add that if the absolute address is for some control register, I'd add volatile to the pointer definition. If it is just RAM, it doesn't matter.

受到@rib的答复的启发,我将补充说,如果绝对地址是针对某个控件寄存器的,我将向指针定义中添加volatile。如果只是RAM,那也没关系。

#2


10  

You could use the section attributes and an ld linker script to define the desired address for that section. This is probably messier than your alternatives, but it is an option.

您可以使用section属性和ld链接器脚本来定义该部分所需的地址。这可能比你的选择更混乱,但这是一个选择。

#3


5  

You answered your question, In your link above it states:

你在上面的链接中回答了你的问题:

With the GNU GCC Compiler you may use only pointer definitions to access absolute memory locations. For example:

使用GNU GCC编译器,您可以只使用指针定义来访问绝对内存位置。例如:

#define IOPIN0         (*((volatile unsigned long *) 0xE0028000))
IOPIN0 = 0x4;

Btw http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Variable-Attributes.html#Variable%20Attributes

顺便说一句http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Variable-Attributes.html变量% 20属性

#4


2  

    extern const uint8_t dev_serial[12];
    asm(".equ dev_serial, 0x1FFFF7E8");
/* or    asm("dev_serial = 0x1FFFF7E8"); */
    ...

    for (i = 0 ; i < sizeof(dev_serial); i++)
        printf((char *)"%02x ", dev_serial[i]);

#1


18  

I don't know, but you can easily create a workaround like this:

我不知道,但你可以很容易地创建这样的解决方案:

int *var = (int*)0x40001000;
*var = 4;

It's not exactly the same thing, but in most situations a perfect substitute. It will work with any compiler, not just GCC.

它不是完全相同的东西,但在大多数情况下,它是一个完美的替代品。它将与任何编译器一起工作,而不仅仅是GCC。

If you use GCC, i assume you also use GNU ld (although it is not a certainty, of course) and ld has support for placing variables wherever you want them.

如果您使用GCC,我假设您也使用GNU ld(当然,这并不是必然的),而且ld支持将变量放置在您想要的任何地方。

I imagine letting the linker do that job is pretty common.

我想让链接器做那个工作是很常见的。

Inspired by answer by @rib, I'll add that if the absolute address is for some control register, I'd add volatile to the pointer definition. If it is just RAM, it doesn't matter.

受到@rib的答复的启发,我将补充说,如果绝对地址是针对某个控件寄存器的,我将向指针定义中添加volatile。如果只是RAM,那也没关系。

#2


10  

You could use the section attributes and an ld linker script to define the desired address for that section. This is probably messier than your alternatives, but it is an option.

您可以使用section属性和ld链接器脚本来定义该部分所需的地址。这可能比你的选择更混乱,但这是一个选择。

#3


5  

You answered your question, In your link above it states:

你在上面的链接中回答了你的问题:

With the GNU GCC Compiler you may use only pointer definitions to access absolute memory locations. For example:

使用GNU GCC编译器,您可以只使用指针定义来访问绝对内存位置。例如:

#define IOPIN0         (*((volatile unsigned long *) 0xE0028000))
IOPIN0 = 0x4;

Btw http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Variable-Attributes.html#Variable%20Attributes

顺便说一句http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Variable-Attributes.html变量% 20属性

#4


2  

    extern const uint8_t dev_serial[12];
    asm(".equ dev_serial, 0x1FFFF7E8");
/* or    asm("dev_serial = 0x1FFFF7E8"); */
    ...

    for (i = 0 ; i < sizeof(dev_serial); i++)
        printf((char *)"%02x ", dev_serial[i]);