From mboxrd@z Thu Jan 1 00:00:00 1970 From: Keith Owens Date: Mon, 28 Jan 2002 12:16:28 +0000 Subject: Re: [Linux-ia64] Offsets from C struct into assembler Message-Id: List-Id: References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: linux-ia64@vger.kernel.org On Mon, 28 Jan 2002 09:56:27 +0100, Christian Hildner 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 #include #include /* 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.