0
這裏的偏移量是一個簡單的C程序:確定字節GetElementPtr
struct S {char c; short arr[16]; char dummy2;};
extern struct S A[20];
extern short* p;
int main() {
p = &A[10].arr[6];
return 0;
}
這裏是LLVM IR:
%struct.S = type { i8, [16 x i16], i8 }
@A = external global [20 x %struct.S]
@p = external global i16*
; Function Attrs: nounwind
define i32 @main() #0 {
entry:
store i16* getelementptr inbounds ([20 x %struct.S]* @A, i64 0, i64 10, i32 1, i64 6), i16** @p, align 8, !tbaa !1
ret i32 0
}
我如何通過計算字節將被加入到@A
偏移getelementptr?
我可以遍歷並打印出GEP足夠容易:
auto& P = *GEP.getPointerOperand();
Out << "GEP(";
GEP.getType()->print(Out); // return type
Out << ", ";
P.printAsOperand(Out); // base
for (auto i=0U; i<GEP.getNumIndices(); i++) {
Out << ", ";
GEP.getOperand(i+1)->printAsOperand(Out); // index i
}
Out << ")\n";
此打印:
GEP(i16*, [20 x %struct.S]* @A, i64 0, i64 10, i32 1, i64 6)
假設所有指數是不變的整數,你怎麼能確定的字節偏移相對於基地指針?