Search code examples
gccportingiar

How to reference segment beginning and size from C code


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.


Solution

  • 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.

    extern uint8_t __start_MY_SEGMENT;
    extern uint8_t __stop_MY_SEGMENT;
    #define MY_SEGMENT_LEN (&__stop_MY_SEGMENT - &__start_MY_SEGMENT)