Skip to content
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
Open

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

lexprfuncall opened this issue Mar 23, 2013 · 19 comments
Milestone

Comments

@lexprfuncall
Copy link

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
Copy link
Contributor

Comment 1:

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

@lexprfuncall
Copy link
Author

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
Copy link
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
Copy link
Contributor

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
Copy link
Contributor

Comment 5:

Labels changed: added arch-arm.

@rsc
Copy link
Contributor

rsc commented Jun 3, 2013

Comment 6:

cmd/5l: use more standard calling convention

@rsc
Copy link
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
Copy link
Author

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
Copy link
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
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 11:

Owner changed to @rsc.

Status changed to Started.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 12:

Labels changed: added feature.

@lexprfuncall
Copy link
Author

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
Copy link
Contributor

rsc commented Sep 9, 2013

Comment 14:

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

@rsc
Copy link
Contributor

rsc commented Sep 9, 2013

Comment 15:

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

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 16:

Labels changed: added release-go1.3.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 17:

Labels changed: removed go1.3.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 18:

Labels changed: added repo-main.

@rsc
Copy link
Contributor

rsc commented Apr 3, 2014

Comment 19:

Too late.

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

@rsc
Copy link
Contributor

rsc commented Sep 15, 2014

Comment 20:

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

@rsc rsc changed the title cmd/5l: use more standard calling convention cmd/link: use more standard calling convention on arm Jun 8, 2015
@bradfitz bradfitz removed the Started label Jan 6, 2017
@rsc rsc removed their assignment Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants