As Adam Rosenfeld points out, you need to use the linker control file to define one or more symbols for you that is located at the precisely correct place.
For gcc and binutils, you will be using ld's linker script syntax. Refer to ld's manual for all the details I'm leaving out here. It is a tad opaque, but you should spend some time with it if you are going to spend any significant amount of effort in the embedded world. It will pay off for you.
In the linker script, you want to define a symbol before your .text segment, and one after.
Adam mentions the PROVIDE()
syntax, which will work. But you may not need to wrap the definition in PROVIDE if you can guarantee that your name is unique. Note that this is a guarantee that you must be able to make, or you risk a lot of confusion later.
In my script, I just use something like:
__SDRAM_CS1 = 0x10000000;
to define the symbol with a constant address that refers (in this case) to the location we decided that the SDRAM controller will map the SDRAM memory, and in C I declare it like:
extern unsigned char __SDRAM_CS1[];
so that it can be used in code that needs to know where the SDRAM actually is.
For a symbol locating the end of the .text segment, you would have something like the following in the linker script
SECTIONS
{
...
.text {
_start_text = .;
*(.text);
...
_end_text = .;
}
...
}
and declare the start and end symbols as
extern unsigned char _start_text[];
extern unsigned char _end_text[];
in C. Then, the start address is simply _start_text
, and the length in bytes of the text segment is _end_text - _start_text
.
Note that I've left out a lot of detail. You probably have sections named things other than .text that must be treated as if they were in the text segment. A prime example is read-only data which often can be located in the text segment safely because it is known to be const
and in an embedded system you'd rather not copy it to valuable RAM if you don't have to. Also, initializers for the data segment, and the internally generated lists of constructors of global objects all get located near the text segment.
Whether you include such things in your image size is a design decision that you need to make after understanding what they are used for.