生成asm-offset
因为不善于在Makefile中调用shell的相关工具,所以关于asm-offsets.h中的产生的16进制数并不知如何做到。
因此自己写了个脚本,可以生成同样的文件(再次造了轮子)。
参考:https://lkml.org/lkml/2001/10/6/3
#脚本offset.awk
1 /^->$/{printf("\n");next} 2 /^->.*/{sym = $1; val = $2; $1 = ""; $2 = ""; sub(/^\->/,"",sym);sub(/^\$/,"", val);printf("#define %-15s %3d\t/* 0x%x\t%s */\n", sym, val, val, $0)}
//测试文件asm-offset.c #include <stddef.h> #ifndef offsetof #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #endif #define DEFINE(sym, val) \ asm volatile("\n->" #sym " %0 " #val : : "i" (val)) #define BLANK() asm volatile("\n->" : : ) struct university { int soochow; int seu; int tsing; int peking; int nju; int sjtu; }; int main(void) { BLANK(); DEFINE(SOOCHOW, offsetof(struct university, soochow)); BLANK(); DEFINE(SEU, offsetof(struct university, seu)); BLANK(); DEFINE(TSING, offsetof(struct university, tsing)); BLANK(); DEFINE(PEKING, offsetof(struct university, peking)); BLANK(); DEFINE(NJU, offsetof(struct university, nju)); BLANK(); DEFINE(SJTU, offsetof(struct university, sjtu)); BLANK(); }
使用:
1 gcc -S asm-offset.c; awk -f offset.awk asm-offset.s > asm-offset.h; rm asm-offset.s
输出:
//asm-offset.h #define SOOCHOW 0 /* 0x0 offsetof(struct university, soochow) */ #define SEU 4 /* 0x4 offsetof(struct university, seu) */ #define TSING 8 /* 0x8 offsetof(struct university, tsing) */ #define PEKING 12 /* 0xc offsetof(struct university, peking) */ #define NJU 16 /* 0x10 offsetof(struct university, nju) */ #define SJTU 20 /* 0x14 offsetof(struct university, sjtu) */