Re: [Linux-ia64] Offsets from C struct into assembler

From: <n0ano_at_indstorage.com>
Date: 2002-01-29 02:11:23
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
Received on Mon Jan 28 07:14:19 2002

This archive was generated by hypermail 2.1.8 : 2005-08-02 09:20:07 EST