Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference segment beginning and size from C code

Tags:

gcc

porting

iar

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.

like image 990
tomsgd Avatar asked Mar 07 '11 07:03

tomsgd


People also ask

What is code segment in C?

Text Segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions.

What is data segment and code segment?

In computing, a code segment, also known as a text segment or simply as text, is a portion of an object file or the corresponding section of the program's virtual address space that contains executable instructions.

What is data segment in assembly language?

The data segment is the place in RAM that a program stores its global and static data. This data is defined at compile time. The data segment does not hold variables that are allocated at run time (the heap is used for this purpose) or variables that are local to subprocedures (the stack is used to hold these).

Are text segments read-only?

This is in contrast to the read-only data segment (rodata segment or . rodata), which contains static constants rather than variables; it also contrasts to the code segment, also known as the text segment, which is read-only on many architectures.


2 Answers

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:

const  char  __attribute__((section (".honk"))) ProjString[16] = "MY PROJECT V1.1";

You will then have to add to the linker's options: -Wl,--section-start=.honk=address.

like image 108
JoeSlav Avatar answered Oct 02 '22 08:10

JoeSlav


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)
like image 41
Brandon Shields Avatar answered Oct 02 '22 09:10

Brandon Shields