如何从 C 代码引用段开头和大小
我正在将 ARM 芯片的程序从 IAR 编译器移植到 gcc。
在原始代码中,IAR 特定运算符(例如__segment_begin
和__segment_size
)用于分别获取某些内存段的开头和大小。
有什么办法可以用 GCC 做同样的事情吗?我搜索了 GCC 手册,但找不到任何相关内容。
更多详情:
所讨论的内存段必须位于固定位置,以便程序可以与芯片上的某些外设正确连接。原始代码使用__segment_begin
运算符来获取这块内存的地址,并使用__segment_size
来确保不会溢出这块内存。
我可以通过添加变量来指示这些内存段的开始和结束来实现相同的功能,但如果 GCC 有类似的运算符,这将有助于最大限度地减少我最终必须编写和维护的编译器相关代码的数量。
I am porting a program for an ARM chip from a IAR compiler to gcc.
In the original code, IAR specific operators such as __segment_begin
and __segment_size
are used to obtain the beginning and size respectively of certain memory segments.
Is there any way to do the same thing with GCC? I've searched the GCC manual but was unable to find anything relevant.
More details:
The memory segments in question have to be in fixed locations so that the program can interface correctly with certain peripherals on the chip. The original code uses the __segment_begin
operator to get the address of this memory and the __segment_size
to ensure that it doesn't overflow this memory.
I can achieve the same functionality by adding variables to indicate the start and end of these memory segments but if GCC had similar operators that would help minimise the amount of compiler dependent code I end up having to write and maintain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
链接器的标志
--section-start
怎么样?我读到的支持此处。有关如何使用它的示例可以在 AVR Freaks Forum:
然后您必须添加到链接器的选项:
-Wl,--section-start=.honk=address
。What about the linker's flag
--section-start
? Which I read is supported here.An example on how to use it can be found on the AVR Freaks Forum:
You will then have to add to the linker's options:
-Wl,--section-start=.honk=address
.现代版本的 GCC 将为每个段声明两个变量,即 __start_MY_SEGMENT 和 __stop_MY_SEGMENT。要使用这些变量,您需要将它们声明为具有所需类型的外部变量。接下来,您可以使用“&”运算符来获取该段的开始和结束地址。
Modern versions of GCC will declare two variables for each segment, namely __start_MY_SEGMENT and __stop_MY_SEGMENT. To use these variables, you need to declare them as externs with the desired type. Following that, you and then use the '&' operator to get the address of the start and end of that segment.