将结构偏移量属性赋予汇编器 [英] give structure offset attribute to assembler
问题描述
如何将C结构的偏移量发送到汇编代码? 例如
how can I send the offset of a C struct to en assembly code ? For example
在我的C代码中,
typedef struct
{
unsigned int a;
unsigned int b;
} CMyStruct;
我将CMyStruct结构的指针发送给ASM函数 假设我的指针进入R0
I send to an ASM function a pointer of a CMyStruct structure Let suppose that my pointer is into R0
要访问a和b属性,我需要这样做.
To access to a and b attribute I need to do that.
ldr r1, [r0, #0] // read a
ldr r2, [r0, #4] // read b
总有没有将#0和#4指定为连续值吗? 像
Is there anyway to not specify #0 and #4 as contant value ? Something like
ldr r1, [r0, CMyStruct.a] // read a
ldr r2, [r0, CMyStruct.b] // read b
谢谢 艾蒂安(Etienne)
Thank's Etienne
推荐答案
实际上有一种方法.解决方案是包含一点魔力,但它是可行的.就是这样.
There is a way, actually. The solution is contains a little bit of magic, but it's works. It's just works.
在c文件中:
#define DEFINE(sym, val) asm volatile("\n-> " #sym " %0 " #val "\n" : : "i" (val))
#define OFFSETOF(s, m) \
DEFINE(offsetof_##s##_##m, offsetof(s, m));
#define SIZEOF(s) \
DEFINE(sizeof_##s, sizeof(s));
void foo()
{
OFFSETOF(KERNEL, error);
OFFSETOF(KERNEL, pool);
SIZEOF(KERNEL);
}
在Makefile中:
in Makefile:
asm_defines.h: asm_defines.c
$(GCC) $(FLAGS_CC) -S $< -o - | awk '($$1 == "->") { print "#define " $$2 " " $$3 }' > $(BUILD_DIR)/$@
最后,您将获得asm_defines.h,您可以将其包含在.S文件中.
Finally you will got asm_defines.h, which you can include in your .S file.
#define offsetof_KERNEL_error #16
#define offsetof_KERNEL_pool #4
#define sizeof_KERNEL #120
这篇关于将结构偏移量属性赋予汇编器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!