public inbox for linux-ia64@vger.kernel.org
 help / color / mirror / Atom feed
* [Linux-ia64] Offsets from C struct into assembler
@ 2002-01-28  8:56 Christian Hildner
  2002-01-28 12:16 ` Keith Owens
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Christian Hildner @ 2002-01-28  8:56 UTC (permalink / raw)
  To: linux-ia64

Hi!

Does anyone know how to bring offsets from a C structure into pure
assembly code (not inline-asm)?
I want to do something like:

struct abc {
    ...
    long varx;
    ...
    }

add rx=offset(varx),ry    // get address of variable
ld8 rx=[rx]                    // get variable varx

with ry being the base address of the structure abc and offset_varx
beeing compiled from struct abc.

Any ideas are welcome.

Christian



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Linux-ia64] Offsets from C struct into assembler
  2002-01-28  8:56 [Linux-ia64] Offsets from C struct into assembler Christian Hildner
@ 2002-01-28 12:16 ` Keith Owens
  2002-01-28 15:11 ` n0ano
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Keith Owens @ 2002-01-28 12:16 UTC (permalink / raw)
  To: linux-ia64

On Mon, 28 Jan 2002 09:56:27 +0100, 
Christian Hildner <christian.hildner@hob.de> wrote:
>Does anyone know how to bring offsets from a C structure into pure
>assembly code (not inline-asm)?
>I want to do something like:
>
>struct abc {
>    ...
>    long varx;
>    ...
>    }
>
>add rx=offset(varx),ry    // get address of variable
>ld8 rx=[rx]                    // get variable varx
>
>with ry being the base address of the structure abc and offset_varx
>beeing compiled from struct abc.

This is the standard method used by linux kernel build in 2.5 kernels.

== asm-offsets.c

/*
 * Generate definitions needed by assembly language modules.
 * This code generates raw asm output which is post-processed to extract
 * and format the required data.
 */

#include <linux/types.h>
#include <linux/stddef.h>
#include <linux/sched.h>

/* Use marker if you need to separate the values later */

#define DEFINE(sym, val, marker) \
  asm volatile("\n-> " #sym " %0 " #val " " #marker : : "i" (val))

#define BLANK() asm volatile("\n->" : : )

int
main(void)
{
  DEFINE(state,        offsetof(struct task_struct, state),);
  DEFINE(flags,        offsetof(struct task_struct, flags),);
  DEFINE(sigpending,   offsetof(struct task_struct, sigpending),);
  DEFINE(addr_limit,   offsetof(struct task_struct, addr_limit),);
  DEFINE(exec_domain,  offsetof(struct task_struct, exec_domain),);
  DEFINE(need_resched, offsetof(struct task_struct, need_resched),);
  DEFINE(tsk_ptrace,   offsetof(struct task_struct, ptrace),);
  DEFINE(processor,    offsetof(struct task_struct, processor),);
  BLANK();
  DEFINE(ENOSYS,       ENOSYS,);
  return 0;
}

== Compile 'gcc asm-offsets.c -S -o asm-offsets.s' then

# Convert raw asm offsets into something that can be included as
# assembler definitions.  It converts
#   -> symbol $value source
# into
#   #define symbol value /* 0xvalue source */

	(set -e;
	  (echo "#ifndef __ASM_OFFSETS_H__";
	   echo "#define __ASM_OFFSETS_H__";
	   echo "/*";
	   echo " * DO NOT MODIFY";
	   echo " *";
	   echo " * This file was generated by arch/${ARCH}/Makefile.in.";
	   echo " *";
	   echo " */";
	   echo "";
	   awk "/^->\$/{printf(\"\\n\");}
	     /^-> /{
	       sym = \$2;
	       val = \$3;
	       sub(/^\\\$/, \"\", val);
	       \$1 = \"\";
	       \$2 = \"\";
	       \$3 = \"\";
	       printf(\"#define %-40s %5d\\t\\t\\t/* 0x%x\\t%s */\\n\",
	         sym, val, val, \$0)
	     }";
	   echo "";
	   echo "#endif";
	  ) < asm-offsets.s > asm-offsets.h)

asm-offsets.h contains #define statements for each DEFINE in
asm-offsets.c.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Linux-ia64] Offsets from C struct into assembler
  2002-01-28  8:56 [Linux-ia64] Offsets from C struct into assembler Christian Hildner
  2002-01-28 12:16 ` Keith Owens
@ 2002-01-28 15:11 ` n0ano
  2002-01-28 20:59 ` Keith Owens
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: n0ano @ 2002-01-28 15:11 UTC (permalink / raw)
  To: linux-ia64

Or you can check out how the IA64 Linux kernel does this.  The
file `arch/ia64/tools/printoffsets.c' is a program that is compiled
and, when run, generates the file `include asm-ia64/offsets.h'
which is an ASM include file containing structure offsets.

On Mon, Jan 28, 2002 at 11:16:28PM +1100, Keith Owens wrote:
> On Mon, 28 Jan 2002 09:56:27 +0100, 
> Christian Hildner <christian.hildner@hob.de> wrote:
> >Does anyone know how to bring offsets from a C structure into pure
> >assembly code (not inline-asm)?
> >I want to do something like:
> >
> >struct abc {
> >    ...
> >    long varx;
> >    ...
> >    }
> >
> >add rx=offset(varx),ry    // get address of variable
> >ld8 rx=[rx]                    // get variable varx
> >
> >with ry being the base address of the structure abc and offset_varx
> >beeing compiled from struct abc.
> 
> This is the standard method used by linux kernel build in 2.5 kernels.
> 
> == asm-offsets.c
> 
> /*
>  * Generate definitions needed by assembly language modules.
>  * This code generates raw asm output which is post-processed to extract
>  * and format the required data.
>  */
> 
> #include <linux/types.h>
> #include <linux/stddef.h>
> #include <linux/sched.h>
> 
> /* Use marker if you need to separate the values later */
> 
> #define DEFINE(sym, val, marker) \
>   asm volatile("\n-> " #sym " %0 " #val " " #marker : : "i" (val))
> 
> #define BLANK() asm volatile("\n->" : : )
> 
> int
> main(void)
> {
>   DEFINE(state,        offsetof(struct task_struct, state),);
>   DEFINE(flags,        offsetof(struct task_struct, flags),);
>   DEFINE(sigpending,   offsetof(struct task_struct, sigpending),);
>   DEFINE(addr_limit,   offsetof(struct task_struct, addr_limit),);
>   DEFINE(exec_domain,  offsetof(struct task_struct, exec_domain),);
>   DEFINE(need_resched, offsetof(struct task_struct, need_resched),);
>   DEFINE(tsk_ptrace,   offsetof(struct task_struct, ptrace),);
>   DEFINE(processor,    offsetof(struct task_struct, processor),);
>   BLANK();
>   DEFINE(ENOSYS,       ENOSYS,);
>   return 0;
> }
> 
> == Compile 'gcc asm-offsets.c -S -o asm-offsets.s' then
> 
> # Convert raw asm offsets into something that can be included as
> # assembler definitions.  It converts
> #   -> symbol $value source
> # into
> #   #define symbol value /* 0xvalue source */
> 
> 	(set -e;
> 	  (echo "#ifndef __ASM_OFFSETS_H__";
> 	   echo "#define __ASM_OFFSETS_H__";
> 	   echo "/*";
> 	   echo " * DO NOT MODIFY";
> 	   echo " *";
> 	   echo " * This file was generated by arch/${ARCH}/Makefile.in.";
> 	   echo " *";
> 	   echo " */";
> 	   echo "";
> 	   awk "/^->\$/{printf(\"\\n\");}
> 	     /^-> /{
> 	       sym = \$2;
> 	       val = \$3;
> 	       sub(/^\\\$/, \"\", val);
> 	       \$1 = \"\";
> 	       \$2 = \"\";
> 	       \$3 = \"\";
> 	       printf(\"#define %-40s %5d\\t\\t\\t/* 0x%x\\t%s */\\n\",
> 	         sym, val, val, \$0)
> 	     }";
> 	   echo "";
> 	   echo "#endif";
> 	  ) < asm-offsets.s > asm-offsets.h)
> 
> asm-offsets.h contains #define statements for each DEFINE in
> asm-offsets.c.
> 
> 
> _______________________________________________
> Linux-IA64 mailing list
> Linux-IA64@linuxia64.org
> http://lists.linuxia64.org/lists/listinfo/linux-ia64

-- 
Don Dugger
"Censeo Toto nos in Kansa esse decisse." - D. Gale
n0ano@indstorage.com
Ph: 303/652-0870x117


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Linux-ia64] Offsets from C struct into assembler
  2002-01-28  8:56 [Linux-ia64] Offsets from C struct into assembler Christian Hildner
  2002-01-28 12:16 ` Keith Owens
  2002-01-28 15:11 ` n0ano
@ 2002-01-28 20:59 ` Keith Owens
  2002-01-28 21:15 ` David Mosberger
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Keith Owens @ 2002-01-28 20:59 UTC (permalink / raw)
  To: linux-ia64

On Mon, 28 Jan 2002 08:11:23 -0700, 
n0ano@indstorage.com wrote:
>Or you can check out how the IA64 Linux kernel does this.  The
>file `arch/ia64/tools/printoffsets.c' is a program that is compiled
>and, when run, generates the file `include asm-ia64/offsets.h'
>which is an ASM include file containing structure offsets.

That tool is disappearing in 2.5 kernels.  All architectures will use
the same method for generating asm offsets, the one I included in my
previous mail.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Linux-ia64] Offsets from C struct into assembler
  2002-01-28  8:56 [Linux-ia64] Offsets from C struct into assembler Christian Hildner
                   ` (2 preceding siblings ...)
  2002-01-28 20:59 ` Keith Owens
@ 2002-01-28 21:15 ` David Mosberger
  2002-01-29  7:06 ` Christian Hildner
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: David Mosberger @ 2002-01-28 21:15 UTC (permalink / raw)
  To: linux-ia64

>>>>> On Tue, 29 Jan 2002 07:59:43 +1100, Keith Owens <kaos@ocs.com.au> said:

  Keith> On Mon, 28 Jan 2002 08:11:23 -0700, n0ano@indstorage.com
  Keith> wrote:
  >> Or you can check out how the IA64 Linux kernel does this.  The
  >> file `arch/ia64/tools/printoffsets.c' is a program that is
  >> compiled and, when run, generates the file `include
  >> asm-ia64/offsets.h' which is an ASM include file containing
  >> structure offsets.

  Keith> That tool is disappearing in 2.5 kernels.  All architectures
  Keith> will use the same method for generating asm offsets, the one
  Keith> I included in my previous mail.

As of v2.5.3-pre5 I don't see any sign of your new kernel build
environment being part of Linus' tree.  Perhaps it will be accepted
some day, but until that happens, the scheme described by Don is the
correct one.

	--david


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Linux-ia64] Offsets from C struct into assembler
  2002-01-28  8:56 [Linux-ia64] Offsets from C struct into assembler Christian Hildner
                   ` (3 preceding siblings ...)
  2002-01-28 21:15 ` David Mosberger
@ 2002-01-29  7:06 ` Christian Hildner
  2002-01-29  8:39 ` David Mosberger
  2002-01-29  9:36 ` Andreas Schwab
  6 siblings, 0 replies; 8+ messages in thread
From: Christian Hildner @ 2002-01-29  7:06 UTC (permalink / raw)
  To: linux-ia64

David Mosberger schrieb:

> >>>>> On Tue, 29 Jan 2002 07:59:43 +1100, Keith Owens <kaos@ocs.com.au> said:
>
>   Keith> On Mon, 28 Jan 2002 08:11:23 -0700, n0ano@indstorage.com
>   Keith> wrote:
>   >> Or you can check out how the IA64 Linux kernel does this.  The
>   >> file `arch/ia64/tools/printoffsets.c' is a program that is
>   >> compiled and, when run, generates the file `include
>   >> asm-ia64/offsets.h' which is an ASM include file containing
>   >> structure offsets.
>
>   Keith> That tool is disappearing in 2.5 kernels.  All architectures
>   Keith> will use the same method for generating asm offsets, the one
>   Keith> I included in my previous mail.
>
> As of v2.5.3-pre5 I don't see any sign of your new kernel build
> environment being part of Linus' tree.  Perhaps it will be accepted
> some day, but until that happens, the scheme described by Don is the
> correct one.
>
>         --david

Although it would be a nice feature of the gcc if it could include C headers
combined with an assembler directive for using the offsets the simple solution
with print_offsets does it's job fine. The good thing here is that awk isn't
needed.

Thanks,

Christian



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Linux-ia64] Offsets from C struct into assembler
  2002-01-28  8:56 [Linux-ia64] Offsets from C struct into assembler Christian Hildner
                   ` (4 preceding siblings ...)
  2002-01-29  7:06 ` Christian Hildner
@ 2002-01-29  8:39 ` David Mosberger
  2002-01-29  9:36 ` Andreas Schwab
  6 siblings, 0 replies; 8+ messages in thread
From: David Mosberger @ 2002-01-29  8:39 UTC (permalink / raw)
  To: linux-ia64

>>>>> On Tue, 29 Jan 2002 08:06:46 +0100, Christian Hildner <christian.hildner@hob.de> said:

  Christian> Although it would be a nice feature of the gcc if it
  Christian> could include C headers combined with an assembler
  Christian> directive for using the offsets the simple solution with
  Christian> print_offsets does it's job fine. The good thing here is
  Christian> that awk isn't needed.

It's not difficult to fix print_offsets.c to avoid awk.  We could use
directives of the form:

  asm volatile ("uc_rnat = %0"::"i"(offsetof (struct _Unwind_Context, rnat)));

for this purpose (this is a cut & paste from gcc's unwind-ia64.c).

	--david


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Linux-ia64] Offsets from C struct into assembler
  2002-01-28  8:56 [Linux-ia64] Offsets from C struct into assembler Christian Hildner
                   ` (5 preceding siblings ...)
  2002-01-29  8:39 ` David Mosberger
@ 2002-01-29  9:36 ` Andreas Schwab
  6 siblings, 0 replies; 8+ messages in thread
From: Andreas Schwab @ 2002-01-29  9:36 UTC (permalink / raw)
  To: linux-ia64

David Mosberger <davidm@hpl.hp.com> writes:

|> >>>>> On Tue, 29 Jan 2002 08:06:46 +0100, Christian Hildner <christian.hildner@hob.de> said:
|> 
|>   Christian> Although it would be a nice feature of the gcc if it
|>   Christian> could include C headers combined with an assembler
|>   Christian> directive for using the offsets the simple solution with
|>   Christian> print_offsets does it's job fine. The good thing here is
|>   Christian> that awk isn't needed.
|> 
|> It's not difficult to fix print_offsets.c to avoid awk.  We could use
|> directives of the form:
|> 
|>   asm volatile ("uc_rnat = %0"::"i"(offsetof (struct _Unwind_Context, rnat)));
|> 
|> for this purpose (this is a cut & paste from gcc's unwind-ia64.c).

You still have to post-process the output to make it usefull for the
assembler.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE GmbH, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2002-01-29  9:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-01-28  8:56 [Linux-ia64] Offsets from C struct into assembler Christian Hildner
2002-01-28 12:16 ` Keith Owens
2002-01-28 15:11 ` n0ano
2002-01-28 20:59 ` Keith Owens
2002-01-28 21:15 ` David Mosberger
2002-01-29  7:06 ` Christian Hildner
2002-01-29  8:39 ` David Mosberger
2002-01-29  9:36 ` Andreas Schwab

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox