linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Michael Ellerman <michael@ellerman.id.au>
To: HongWoo Lee <hongwoo7@gmail.com>
Cc: linuxppc-dev <linuxppc-dev@lists.ozlabs.org>
Subject: Re: why do we need reloc_offset ??
Date: Thu, 03 Sep 2009 12:19:49 +1000	[thread overview]
Message-ID: <1251944389.6261.11.camel@concordia> (raw)
In-Reply-To: <5e2889710909012333q69874b24qf6e3c0abfceb8dfd@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1902 bytes --]

On Wed, 2009-09-02 at 15:33 +0900, HongWoo Lee wrote:
> Hi everyone~ 
> 
> In ther linux kernel code, I found the reloc_offset. 
> 
> {{{
> // file : misc.S 
> /* Returns (address we are running at) - (address we were linked at)
>  * for use before the text and data are mapped to KERNELBASE.
>  */
> _GLOBAL(reloc_offset)
> }}}
> 
> I couldn't understand the comment saying "Returns (address we are
> running at) - (address we were linked at)". 
> For now, I'm studying each instruction. 
> 
> And below is best comment I can explain for each instruction. 
> 
> _GLOBAL(reloc_offset)
>         mflr    r0                // move from link register, save the return address
>         bl      1f                 // bl 1f
> 1:     mflr    r3                // move from link register, r3 is just return address pointing itself 

At this point r3 contains the value of LR based on the branch we just
did. So it's the address of the current instruction, based on where the
code is _running_.

>         LOAD_REG_IMMEDIATE(r4,1b)    // get the 1b address, r4 is the address 

Here we load into r4 the address of the previous instruction, but based
on the label "1b". The address of the label is calculated by the linker,
so r4 contains the address the instruction was linked at.

>         subf    r3,r4,r3        // r3 = r3 – r4 

So here we calculate any difference between the address the code was
linked at and the address it's running at.

>         mtlr    r0                // restore return address 
>         blr
> 
> After this, I still don't know why "r3-r4" is the offset. 
> And what does it mean ?? 

The offset is just the difference between the address the code was
linked at and the address it's running it. It's used in places where the
code might be (or is always) running at an address other than the
address it was linked at.

cheers

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

      reply	other threads:[~2009-09-03  2:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-02  6:33 why do we need reloc_offset ?? HongWoo Lee
2009-09-03  2:19 ` Michael Ellerman [this message]

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=1251944389.6261.11.camel@concordia \
    --to=michael@ellerman.id.au \
    --cc=hongwoo7@gmail.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    /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).