How to get the final address of my code

I am writing a real-time operating system from scratch for a course design. I want to know the final address of my code after downloading it to the chip, because I plan to use free memory for the stack space, and I need to make sure that I will not overwrite the existing code.

I heard about the __end variable provided by GCC, this is the end of the code, but I have no idea what the __end value is and how to use it in my code. Can someone explain this a bit or provide links to some materials because I could not google __end?

Many thanks.

+3
source share
4 answers

, , , .

gcc binutils ld linker script. ld , . , , - . .

script .text .

PROVIDE(), . PROVIDE, , . , , , .

script, - :

__SDRAM_CS1 = 0x10000000;

, ( ) , , SDRAM SDRAM-, C :

extern unsigned char __SDRAM_CS1[];

, , SDRAM.

, .text, script

- :
SECTIONS
{
    ...
    .text {
        _start_text = .;
        *(.text);
        ...
        _end_text = .;
    }
    ...
}

extern unsigned char _start_text[];
extern unsigned char _end_text[];

C. _start_text, _end_text - _start_text.

, . , , .text, , . , , , , const, RAM, . , .

- , , , .

+3

script. C- :

// call this whatever you want; the linker will fill this in with the address
// of the end of code
extern uint32_t endOfCode;

script .text:

PROVIDE(endOfCode = .);

, . script ( , ) , , -T , . !

+2

, , , -?

0

, GCC, , , - :

long foo()
{
    int anything[0];
    return *(&anything - 1);
}

long endOfProgram()
{
    return stackPointer();
}

, , :

  • endOfProgram()
  • endOfProgram foo, endOfProgram .
  • Push something [0] on the foo stack.
  • Take the location of the element directly in front of the address of something that should be the memory address of endOfProgram.

Some of my syntaxes may be incorrect, but I hope you can get the gist of what I'm trying to do.

EDIT:

... or just grab the value of the endOfProgram function pointer. I think it will work too.

-2
source

Source: https://habr.com/ru/post/1709880/


All Articles