This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

GCC linker equivalent of __STACK_END?

Hi, I'm trying to convert the Compute through power loss assembly file (ctpl_low_level.asm) in to a GCC equivalent .S.

I'm pretty sure I've got everything figured out, except the equivalent to the linker symbol __STACK_END used in the file.

I've found that gcc linker has a __stack defined, but I believe it's to the top of the Stack.

Is there an equivalent to the __STACK_END in GCC?

I'm not yet well versed in linker files, but if there isn't one predefined

would modifying the linker file with something like this be equivalent (based from things found here sourceware.org/.../Assignments.html

     SECTIONS
     {
       .stack :
         {
           *(.stack)
           __STACK_END = .;
           PROVIDE(__STACK_END = .);
         }
     }

Any help is appreciated. Sorry if it's been asked before, I did try searching though.

The code in question I'm trying to convert:
ctpl_saveCpuStackEnterLpm:
    pushx.a SR                                  ; Save SR to stack
    dint                                        ; disable interrupts
    nop                                         ; disable interrupts
    pushm.a #12,R15                             ; Save R4-R15 to stack
    movx.a  #__STACK_END,R4                     ; Calculate stack usage
    subx.a  SP,R4                               ; Calculate stack usage
    movx.w  R4,&ctpl_stackUsage                 ; Save stack usage
    movx.a  #ctpl_stackCopy,R6                  ; dest ptr
    movx.a  SP,R5                               ; src ptr
    copyx   R5,R6,R4                            ; copy the stack
ctpl_wakeup:
    mov.w   #WDTPW+WDTHOLD,&WDTCTL              ; stop WDT
    mov.b   #CSKEY_H,&CSCTL0_H                  ; unlock CS registers
    mov.w   #DIVM__2,&CSCTL3                    ; set DCO to 4MHz (maximum boot freq)
    clr.b   &CSCTL0_H                           ; lock CS registers
    movx.a  #__STACK_END,SP                     ; Reset stack pointer
    movx.w  &ctpl_stackUsage,R4                 ; loop counter
    subx.a  R4,SP                               ; Reset stack pointer
    movx.a  SP,R6                               ; dest ptr
    movx.a  #ctpl_stackCopy,R5                  ; src ptr
    copyx   R5,R6,R4                            ; copy the stack
    popm.a  #12,R15                             ; Restore R4-R15 from stack
    popx.a  SR                                  ; Restore interrupts
    movx.w  #CTPL_STATE_INVALID,&ctpl_state     ; Mark the state as invalid
    retx                                        ; Return
  • The stack has only one fixed end, the top (by default, the end of the RAM); the other end grows dynamically, and there's only the SP register to indicate the bottom.

    The __stack defined in the gcc linker script is the same as __STACK_END.

  • Ahh yes, thanks. After reading more in to what the code was doing I was able to figure out they were equivalent, but I didn't realize the SP was the only way to indicate the bottom, which makes sense I'll have to keep that in mind.