...access section addresses from C or C++?
How can I determine the start and end addresses of a section from within a program?
The CrossWorks for ARM linker script generator creates __section_name_start__ and __section_name_end__ symbols to mark the start and end of each section.
As an example, if you want to set a pointer to the start address of the .text section you could use the following code:
extern unsigned char __text_start__;
void
func(void)
{
unsigned char *ptr = &__text_start__;
...
}
-
The above reference code works for CrossWorks 2.0, but generates a compiler warning when used under Crossworks 2.1; since I'm required to compile using "-Wall", this generates an error and kills my build. Is there a simple solution other than disabling the -Wall option?
I'd like to try casting the void object to a uint8_t in order to take its address, as in the sample below, but I'm unsure if this is wise, being somewhat unfamiliar with entities of type void. Will this code work reliably and safely?
extern void __text_start__; void func(void) { unsigned long *ptr = &(uint8_t)(__text_start__); ... } -
Well, the casting trick I mention above won't work; the compiler complains that __text_start__ is not a complete type.
I've also tried the #pragma GCC diagnostic ignore "-Werror" route, but while the compiler WILL complain if the pragma is malformed, it WON'T actually prevent the compiler error on the original reference code, regardless of what the GCC docs say about diagnostic pragmas overriding command-line options. The best solution now appears to be to set the "Treat warnings as errors" property for the affected files to "No" in the Properties pane of the IDE.
-
Thanks! That seems to work. As I'm working in C, C++ isn't an issue.
If I understand the situation correctly, we could declare __text_start__ to be of any type we please here. Since all that we do is take its address, the fact that there's no actual variable (just an address with that name) won't matter, and the compiler and linker will be happy.
Please sign in to leave a comment.
Comments
7 comments