New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/link: use more standard calling convention on arm #5113

Open
lexprfuncall opened this Issue Mar 23, 2013 · 19 comments

Comments

Projects
None yet
5 participants
@lexprfuncall

lexprfuncall commented Mar 23, 2013

Unlike the 386 and amd64, arm stores the return address in the word at the top of the
stack rather than in the word below the incoming parameter area.

This is a deviation from the native arm calling convention which also stores the return
address below the incoming parameter area.

Following the native convention would make it easier and less error prone to translate
runtime code between all the supported targets.

The documentation for the native calling convention can be found at following address

http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042e/IHI0042E_aapcs.pdf
@DanielMorsing

This comment has been minimized.

Contributor

DanielMorsing commented Mar 23, 2013

Comment 1:

386 and amd64 uses the top of the stack as well for the return address.
Unless I'm misunderstanding something here.
@lexprfuncall

This comment has been minimized.

lexprfuncall commented Mar 25, 2013

Comment 2:

No, on the 386 and amd64 the return address is stored below the top of the stack.
On 386 and amd64 a function call is performed by first doing a call S (push pc and jump
to S), then a sub esp, N to allocate N bytes for locals.  In this scheme, the return
address is stored at a higher address than any space for locals.
On the arm a function call is befored by doing a bl S then a "str lr, [sp - #-N]!" to
allocate N bytes for locals and store lr at the top of stack.  In this scheme, the
return address is stored at a lower address than any space for locals.
@rsc

This comment has been minimized.

Contributor

rsc commented Mar 25, 2013

Comment 3:

The confusion in these comments owes to the conflicting meanings of bottom
and top. In operating systems it is common to talk about the bottom or top
of a memory segment, with bottom corresponding to numerically lower
addresses. In the definition of a stack, the top element is the one most
recently pushed. This conflicts with hardware stacks that grow from top to
bottom of memory.
For better or worse most Go code and comments use the hardware-inspired
terms, so that "top of stack" refers to the top of the stack memory (i.e.,
oldest thing on the stack) and "bottom of stack" refers to the bottom of
the stack memory (i.e. newest thing on the stack). Carl is using the data
structure-inspired terms, which have the opposite meaning. If you are aware
that the meaning may be the opposite of what you expect, you can usually
figure it out from context.
@DanielMorsing

This comment has been minimized.

Contributor

DanielMorsing commented Mar 27, 2013

Comment 4:

I figured out what the confusion was. I was talking about the return address of a called
function (which is pushed onto the top of the stack), while Carl is talking about the
return address of the currently executing function (which is between local stack space
and incoming args).
I'll stop being confused and confusing everyone else :)
@remyoudompheng

This comment has been minimized.

Contributor

remyoudompheng commented Jun 2, 2013

Comment 5:

Labels changed: added arch-arm.

@rsc

This comment has been minimized.

Contributor

rsc commented Jun 3, 2013

Comment 6:

cmd/5l: use more standard calling convention
@rsc

This comment has been minimized.

Contributor

rsc commented Jul 16, 2013

Comment 8:

Carl, I opened
http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042e/IHI0042E_aapcs.pdf but I
cannot find the part that talks about where to save LR on the stack. Is it really there?
I agree that what you've described is what gcc does. I just can't find it in the PDF.
@lexprfuncall

This comment has been minimized.

lexprfuncall commented Jul 16, 2013

Comment 9:

I looked at the document so I could send you a page number but I am having trouble
finding the very information I intended for the reader.  Sorry about that.
The traditional ARM prologue uses a store-multiple instruction to save the LR and all of
the callee save registers.  A matching load-multiple will assign the saved LR to the PC,
thereby returning to the caller.  You can see an example by looking at GCC generated
code or here, at the ARM documentation site
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0473i/BABBJEBD.html
This idiom is supported by the control flow prediction
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0438h/BABGEAEF.html
Because the LR is a high numbered register the store-multiple always leaves the return
address in the bottom most (highest address) slot in the frame, just like on the x86.
@rsc

This comment has been minimized.

Contributor

rsc commented Jul 30, 2013

Comment 10:

I have this working in a local client and just need to clean it up and double-check cgo.

Labels changed: added go1.2maybe.

@rsc

This comment has been minimized.

Contributor

rsc commented Jul 30, 2013

Comment 11:

Owner changed to @rsc.

Status changed to Started.

@rsc

This comment has been minimized.

Contributor

rsc commented Jul 30, 2013

Comment 12:

Labels changed: added feature.

@lexprfuncall

This comment has been minimized.

lexprfuncall commented Aug 26, 2013

Comment 13:

For what it is worth, there is some additional information about the ARM calling
convention and its "conventions" on this Wikipedia page
https://en.wikipedia.org/wiki/Calling_convention#ARM
@rsc

This comment has been minimized.

Contributor

rsc commented Sep 9, 2013

Comment 14:

Labels changed: added go1.3, removed go1.2maybe.

@rsc

This comment has been minimized.

Contributor

rsc commented Sep 9, 2013

Comment 15:

Labels changed: added priority-later, removed priority-triage.

@rsc

This comment has been minimized.

Contributor

rsc commented Dec 4, 2013

Comment 16:

Labels changed: added release-go1.3.

@rsc

This comment has been minimized.

Contributor

rsc commented Dec 4, 2013

Comment 17:

Labels changed: removed go1.3.

@rsc

This comment has been minimized.

Contributor

rsc commented Dec 4, 2013

Comment 18:

Labels changed: added repo-main.

@rsc

This comment has been minimized.

Contributor

rsc commented Apr 3, 2014

Comment 19:

Too late.

Labels changed: added release-go1.4, removed release-go1.3.

@rsc

This comment has been minimized.

Contributor

rsc commented Sep 15, 2014

Comment 20:

Labels changed: added release-none, removed release-go1.4.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@rsc rsc removed repo-main labels Apr 14, 2015

@rsc rsc changed the title from cmd/5l: use more standard calling convention to cmd/link: use more standard calling convention on arm Jun 8, 2015

@bradfitz bradfitz removed the Started label Jan 6, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment