gpt4 book ai didi

c - 按应用程序访问链接描述文件中定义的符号

转载 作者:太空狗 更新时间:2023-10-29 16:28:41 25 4
gpt4 key购买 nike

在我的链接描述文件中,我定义了两个符号

define symbol _region_RAM_start__     = 0xC0000000;
define symbol _region_RAM_end__ = 0xC00fffff;

然后我将它们导出并如下所示

export symbol _region_RAM_start__;
export symbol _region_RAM_end__;

我尝试从应用程序代码访问这些符号

extern const unsigned int _region_RAM_start__;
extern const unsigned int _region_RAM_end__;
....
int GetRAMSize()
{
int size = 0;
unsigned int address_1 = _region_RAM_start__;
unsigned int address_2 = _region_RAM_end__;
size = address_2 - address_1 + 1U;
return size;
}

现在,我预计返回值为 0x00100000,然而,我得到的只是 0。因此,当我转向调试器时,我注意到 _region_RAM_start___region_RAM_end__ 的值分别为 0xC0000000 和 0xC00fffff,但是 address_1 address_2 的值为 0。

编译器优化设置为“无”。这已经困扰我一段时间了。有没有什么很明显的东西是我在这里遗漏的(除了“我一开始就不应该这样做”)

解决方案感谢 nm求答案

  unsigned int address_1 = (unsigned int) (&_region_RAM_start__);

否则 address_1address_2 都包含垃圾值(即分别在地址 0xC0000000 和 0xC00fffff 处可用的值,但从这段代码的角度来看是垃圾)

最佳答案

这有点老了,但我还是会回答的......

来自ld manual :

Accessing a linker script defined variable from source code is not intuitive. In particular a linker script symbol is not equivalent to a variable declaration in a high level language, it is instead a symbol that does not have a value.

Before going further, it is important to note that compilers often transform names in the source code into different names when they are stored in the symbol table. For example, Fortran compilers commonly prepend or append an underscore, and C++ performs extensive name mangling. Therefore there might be a discrepancy between the name of a variable as it is used in source code and the name of the same variable as it is defined in a linker script. For example in C a linker script variable might be referred to as:

extern int foo;

But in the linker script it might be defined as:

_foo = 1000;

In the remaining examples however it is assumed that no name transformation has taken place.

When a symbol is declared in a high level language such as C, two things happen. The first is that the compiler reserves enough space in the program's memory to hold the value of the symbol. The second is that the compiler creates an entry in the program's symbol table which holds the symbol's address. ie the symbol table contains the address of the block of memory holding the symbol's value. So for example the following C declaration, at file scope:

int foo = 1000;

creates a entry called "foo" in the symbol table. This entry holds the address of an int sized block of memory where the number 1000 is initially stored.

When a program references a symbol the compiler generates code that first accesses the symbol table to find the address of the symbol's memory block and then code to read the value from that memory block. So:

foo = 1;

looks up the symbol foo in the symbol table, gets the address associated with this symbol and then writes the value 1 into that address. Whereas:

int * a = & foo;

looks up the symbol foo in the symbol table, gets it address and then copies this address into the block of memory associated with the variable "a".

Linker scripts symbol declarations, by contrast, create an entry in the symbol table but do not assign any memory to them. Thus they are an address without a value. So for example the linker script definition:

foo = 1000;

creates an entry in the symbol table called @samp{foo} which holds the address of memory location 1000, but nothing special is stored at address 1000. This means that you cannot access the value of a linker script defined symbol - it has no value - all you can do is use the address of a linker script defined symbol.

Hence when you are using a linker script defined symbol in source code you should always take the address of the symbol, and never attempt to use its value. For example suppose you want to copy the contents of a section of memory called .ROM into a section called .FLASH and the linker script contains these declarations:

start_of_ROM   = .ROM;
end_of_ROM = .ROM + sizeof (.ROM);
start_of_FLASH = .FLASH;

Then the C source code to perform the copy would be:

extern char start_of_ROM, end_of_ROM, start_of_FLASH;

memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM);

Note the use of the "&" operators. They are correct.

关于c - 按应用程序访问链接描述文件中定义的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8398755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com