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

From: Keith Owens <kaos_at_ocs.com.au>
Date: 2002-01-28 23:16:28
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.
Received on Mon Jan 28 04:17:06 2002

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