9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Michael Teichgräber" <mt@wmipf.in-berlin.de>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] getcallerpc on arm7
Date: Mon, 31 Mar 2008 19:23:11 +0200	[thread overview]
Message-ID: <47F11DFF.8010406@wmipf.in-berlin.de> (raw)
In-Reply-To: <20080331162530.C70ED1E8C3A@holo.morphisms.net>

> On architectures that don't push the return address on the
> stack during the call instruction, the caller of getcallerpc
> will have saved the desired return address somewhere in
> its stack frame, and getcallerpc must root it out.

It appears the implementation below
works well with arm-elf-gcc on arm7tdmi:

	/*
	 * Functions generated by arm-elf-gcc start
	 * like this (always?):
	 * 	mov	ip, sp
	 *	stmdb	sp!, {fp, ip, lr, pc}
	 *
	 * (The first argument, which is still in r0,
	 * is pushed later, when its address is taken so
	 * that it can be provided to getcallerpc.)
	 *
	 * To get the value of `lr', the return address,
	 * walk up the stack, starting with x, until a
	 * value *u is found which is the previous stack
	 * pointer, i.e. which equals the address u+3.
	 * In that case, the return address is expected
	 * to be in u[1].
	 *
	 * If no such address is found, return an invalid value.
	 */
	ulong
	getcallerpc(void *x)
	{
		ulong *u;
		int	i;

		u = (ulong*)x;
		for (i=0; i<128; i++, u++)
			if (*u == (ulong)(u+3))
				return u[1];
		return ~0;
	}

A helpful document was http://www.cems.uwe.ac.uk/~cduffy/es/5.ppt,
to get an idea of how function arguments are provided
and the stack is managed by the gcc on arm.

If the stack contains a value which matches the
condition for `ip', but which is not ip, getcallerpc
will find it before the real ip/lr, and report a
garbage address.

Michael



  reply	other threads:[~2008-03-31 17:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-27 10:25 Michael Teichgräber
2008-03-31 16:24 ` Russ Cox
2008-03-31 17:23   ` Michael Teichgräber [this message]
2008-03-31 19:09   ` Charles Forsyth
2008-03-31 22:34     ` Michael Teichgräber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47F11DFF.8010406@wmipf.in-berlin.de \
    --to=mt@wmipf.in-berlin.de \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).