2017-09-13 5 views
1

Comment identifier une variable annotée dans un passage LLVM?Identification d'une variable annotée dans un passage LLVM

#include <stdio.h> 

int main(){ 
int x __attribute__((annotate("my_var")))= 0; 
int a,b; 
x = x + 1; 
a = 5; 
b = 6; 
x = x + a; 

return x; 
} 

Par exemple, je veux identifier les instructions qui ont la variable annotée (x dans ce cas) et les imprimer (x = x + 1 et x = x + a) Comment puis-je obtenir ce?

Ce fichier est le .ll généré à l'aide LLVM

; ModuleID = 'test.c' 
source_filename = "test.c" 
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" 
target triple = "aarch64" 

@.str = private unnamed_addr constant [7 x i8] c"my_var\00", section "llvm.metadata" 
@.str.1 = private unnamed_addr constant [7 x i8] c"test.c\00", section "llvm.metadata" 

; Function Attrs: noinline nounwind optnone 
define i32 @main() #0 { 
    %1 = alloca i32, align 4 
    %2 = alloca i32, align 4 
    %3 = alloca i32, align 4 
    %4 = alloca i32, align 4 
    store i32 0, i32* %1, align 4 
    %5 = bitcast i32* %2 to i8* 
    call void @llvm.var.annotation(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.s$ 
    store i32 0, i32* %2, align 4 
    %6 = load i32, i32* %2, align 4 
    %7 = add nsw i32 %6, 1 
    store i32 %7, i32* %2, align 4 
    store i32 5, i32* %3, align 4 
    store i32 6, i32* %4, align 4 
    %8 = load i32, i32* %2, align 4 
    %9 = load i32, i32* %3, align 4 
    %10 = add nsw i32 %8, %9 
    store i32 %10, i32* %2, align 4 
    %11 = load i32, i32* %2, align 4 
    ret i32 %11 
} 

; Function Attrs: nounwind 
declare void @llvm.var.annotation(i8*, i8*, i8*, i32) #1 

attributes #0 = { noinline nounwind optnone "correctly-rounded-divide-sqrt-fp-math"="false" $ 
attributes #1 = { nounwind } 

!llvm.module.flags = !{!0} 
!llvm.ident = !{!1} 
!0 = !{i32 1, !"wchar_size", i32 4} 
+0

Commencez par compiler ce code en IR et regardez ce qui est généré. – arrowd

+0

Je l'ai fait. Je peux voir les métadonnées générées comme une chaîne globale. Cependant, je ne suis pas clair sur la façon d'utiliser cette information pour identifier la variable particulière. – Dragonight

+0

Montrez-nous le code, peut-être que nous avons une idée. – arrowd

Répondre

0

Vous devez boucle sur les instructions et l'identification des appels à llvm.var.annotation

Le premier argument est un pointeur vers la variable annotée (i8 *).

Pour obtenir la variable annotée réelle, vous devez alors trouver à quoi pointe ce pointeur.

Dans votre cas, il s'agit de l'opérande source de l'instruction bitcast.

+0

Comment puis-je identifier les appels à llvm.var.annotation? Impossible de comprendre comment faire cela. – Dragonight