由於不善於在Makefile中調用shell的相關工具,因此關於asm-offsets.h中的產生的16進制數並不知如何作到。shell
所以本身寫了個腳本,能夠生成一樣的文件(再次造了輪子)。工具
參考: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(); }
使用:spa
1 gcc -S asm-offset.c; awk -f offset.awk asm-offset.s > asm-offset.h; rm asm-offset.s
輸出:3d
//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) */