Mark Wielaard 75a7db
commit f4abcc05fdba3f25890a9b30b71d511ccc906d46
Mark Wielaard 75a7db
Author: Mark Wielaard <mark@klomp.org>
Mark Wielaard 75a7db
Date:   Mon Jul 27 22:43:28 2020 +0200
Mark Wielaard 75a7db
Mark Wielaard 75a7db
    Incorrect call-graph tracking due to new _dl_runtime_resolve_xsave*
Mark Wielaard 75a7db
    
Mark Wielaard 75a7db
    Newer glibc have alternate ld.so _ld_runtime_resolve functions.
Mark Wielaard 75a7db
    Namely _dl_runtime_resolve_xsave and _dl_runtime_resolve_xsave'2
Mark Wielaard 75a7db
    
Mark Wielaard 75a7db
    This patch recognizes the xsave, xsvec and fxsave variants and
Mark Wielaard 75a7db
    changes callgrind so that any variant counts as _dl_runtime_resolve.
Mark Wielaard 75a7db
    
Mark Wielaard 75a7db
    Original patch by paulo.cesar.pereira.de.andrade@gmail.com
Mark Wielaard 75a7db
    https://bugs.kde.org/show_bug.cgi?id=415293
Mark Wielaard 75a7db
Mark Wielaard 75a7db
diff --git a/callgrind/fn.c b/callgrind/fn.c
Mark Wielaard 75a7db
index e9d8dd214..7cce1a0c7 100644
Mark Wielaard 75a7db
--- a/callgrind/fn.c
Mark Wielaard 75a7db
+++ b/callgrind/fn.c
Mark Wielaard 75a7db
@@ -30,8 +30,11 @@
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
 static fn_array current_fn_active;
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
-static Addr runtime_resolve_addr = 0;
Mark Wielaard 75a7db
-static int  runtime_resolve_length = 0;
Mark Wielaard 75a7db
+/* x86_64 defines 4 variants.  */
Mark Wielaard 75a7db
+#define MAX_RESOLVE_ADDRS 4
Mark Wielaard 75a7db
+static int  runtime_resolve_addrs = 0;
Mark Wielaard 75a7db
+static Addr runtime_resolve_addr[MAX_RESOLVE_ADDRS];
Mark Wielaard 75a7db
+static int  runtime_resolve_length[MAX_RESOLVE_ADDRS];
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
 // a code pattern is a list of tuples (start offset, length)
Mark Wielaard 75a7db
 struct chunk_t { int start, len; };
Mark Wielaard 75a7db
@@ -56,6 +59,9 @@ static Bool check_code(obj_node* obj,
Mark Wielaard 75a7db
     /* first chunk of pattern should always start at offset 0 and
Mark Wielaard 75a7db
      * have at least 3 bytes */
Mark Wielaard 75a7db
     CLG_ASSERT((pat->chunk[0].start == 0) && (pat->chunk[0].len >2));
Mark Wielaard 75a7db
+
Mark Wielaard 75a7db
+    /* and we cannot be called more than MAX_RESOLVE_ADDRS times */
Mark Wielaard 75a7db
+    CLG_ASSERT(runtime_resolve_addrs < MAX_RESOLVE_ADDRS);
Mark Wielaard 75a7db
     
Mark Wielaard 75a7db
     CLG_DEBUG(1, "check_code: %s, pattern %s, check %d bytes of [%x %x %x...]\n",
Mark Wielaard 75a7db
               obj->name, pat->name, pat->chunk[0].len, code[0], code[1], code[2]);
Mark Wielaard 75a7db
@@ -93,8 +99,9 @@ static Bool check_code(obj_node* obj,
Mark Wielaard 75a7db
 				 pat->name, obj->name + obj->last_slash_pos,
Mark Wielaard 75a7db
 				 addr - obj->start, addr, pat->len);
Mark Wielaard 75a7db
 		    
Mark Wielaard 75a7db
-		runtime_resolve_addr   = addr;
Mark Wielaard 75a7db
-		runtime_resolve_length = pat->len;
Mark Wielaard 75a7db
+		runtime_resolve_addr[runtime_resolve_addrs] = addr;
Mark Wielaard 75a7db
+		runtime_resolve_length[runtime_resolve_addrs] = pat->len;
Mark Wielaard 75a7db
+		runtime_resolve_addrs++;
Mark Wielaard 75a7db
 		return True;
Mark Wielaard 75a7db
 	    }
Mark Wielaard 75a7db
         }
Mark Wielaard 75a7db
@@ -138,8 +145,9 @@ static Bool search_runtime_resolve(obj_node* obj)
Mark Wielaard 75a7db
 	"x86-glibc2.8", 30, {{ 0,12 }, { 16,14 }, { 30,0}} };
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
     if (VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) return False;
Mark Wielaard 75a7db
-    if (check_code(obj, code, &pat)) return True;
Mark Wielaard 75a7db
-    if (check_code(obj, code_28, &pat_28)) return True;
Mark Wielaard 75a7db
+    Bool pat_p    = check_code(obj, code, &pat;;
Mark Wielaard 75a7db
+    Bool pat_28_p = check_code(obj, code_28, &pat_28);
Mark Wielaard 75a7db
+    if (pat_p || pat_28_p) return True;
Mark Wielaard 75a7db
     return False;
Mark Wielaard 75a7db
 #endif
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
@@ -186,9 +194,98 @@ static Bool search_runtime_resolve(obj_node* obj)
Mark Wielaard 75a7db
     static struct pattern pat = {
Mark Wielaard 75a7db
 	"amd64-def", 110, {{ 0,62 }, { 66,44 }, { 110,0 }} };
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
+    static UChar code_xsavec[] = {
Mark Wielaard 75a7db
+	/* 0*/ 0x53, 0x48, 0x89, 0xe3, 0x48, 0x83, 0xe4, 0xc0,
Mark Wielaard 75a7db
+	/* 8*/ 0x48, 0x2b, 0x25, 0x00, 0x00, 0x00, 0x00, /* sub <i32>(%rip),%rsp */
Mark Wielaard 75a7db
+	/*15*/ 0x48,
Mark Wielaard 75a7db
+	/*16*/ 0x89, 0x04, 0x24, 0x48, 0x89, 0x4c, 0x24, 0x08,
Mark Wielaard 75a7db
+	/*24*/ 0x48, 0x89, 0x54, 0x24, 0x10, 0x48, 0x89, 0x74,
Mark Wielaard 75a7db
+	/*32*/ 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20, 0x4c,
Mark Wielaard 75a7db
+	/*40*/ 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24,
Mark Wielaard 75a7db
+	/*48*/ 0x30, 0xb8, 0xee, 0x00, 0x00, 0x00, 0x31, 0xd2,
Mark Wielaard 75a7db
+	/*56*/ 0x48, 0x89, 0x94, 0x24, 0x50, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*64*/ 0x48, 0x89, 0x94, 0x24, 0x58, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*72*/ 0x48, 0x89, 0x94, 0x24, 0x60, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*80*/ 0x48, 0x89, 0x94, 0x24, 0x68, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*88*/ 0x48, 0x89, 0x94, 0x24, 0x70, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*96*/ 0x48, 0x89, 0x94, 0x24, 0x78, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*04*/ 0x0f, 0xc7, 0x64, 0x24, 0x40, 0x48, 0x8b, 0x73,
Mark Wielaard 75a7db
+	/*112*/0x10, 0x48, 0x8b, 0x7b, 0x08,
Mark Wielaard 75a7db
+	/*117*/0xe8, 0x00, 0x00, 0x00, 0x00,		/* callq <_dl_fixup> */
Mark Wielaard 75a7db
+	/*122*/0x49, 0x89, 0xc3, 0xb8, 0xee, 0x00,
Mark Wielaard 75a7db
+	/*128*/0x00, 0x00, 0x31, 0xd2, 0x0f, 0xae, 0x6c, 0x24,
Mark Wielaard 75a7db
+	/*136*/0x40, 0x4c, 0x8b, 0x4c, 0x24, 0x30, 0x4c, 0x8b,
Mark Wielaard 75a7db
+	/*144*/0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20,
Mark Wielaard 75a7db
+	/*152*/0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54,
Mark Wielaard 75a7db
+	/*160*/0x24, 0x10, 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48,
Mark Wielaard 75a7db
+	/*168*/0x8b, 0x04, 0x24, 0x48, 0x89, 0xdc, 0x48, 0x8b,
Mark Wielaard 75a7db
+	/*176*/0x1c, 0x24, 0x48, 0x83, 0xc4, 0x18, 0xf2, 0x41,
Mark Wielaard 75a7db
+	/*184*/0xff, 0xe3 };
Mark Wielaard 75a7db
+    static struct pattern pat_xsavec = {
Mark Wielaard 75a7db
+	    "amd64-xsavec", 186, {{ 0,11 }, { 15,103 }, {122,64}, { 186,0 }} };
Mark Wielaard 75a7db
+
Mark Wielaard 75a7db
+    static UChar code_xsave[] = {
Mark Wielaard 75a7db
+	/* 0*/ 0x53, 0x48, 0x89, 0xe3, 0x48, 0x83, 0xe4, 0xc0,
Mark Wielaard 75a7db
+	/* 8*/ 0x48, 0x2b, 0x25, 0x00, 0x00, 0x00, 0x00, /* sub <i32>(%rip),%rsp */
Mark Wielaard 75a7db
+	/*15*/ 0x48,
Mark Wielaard 75a7db
+	/*16*/ 0x89, 0x04, 0x24, 0x48, 0x89, 0x4c, 0x24, 0x08,
Mark Wielaard 75a7db
+	/*24*/ 0x48, 0x89, 0x54, 0x24, 0x10, 0x48, 0x89, 0x74,
Mark Wielaard 75a7db
+	/*32*/ 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20, 0x4c,
Mark Wielaard 75a7db
+	/*40*/ 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24,
Mark Wielaard 75a7db
+	/*48*/ 0x30, 0xb8, 0xee, 0x00, 0x00, 0x00, 0x31, 0xd2,
Mark Wielaard 75a7db
+	/*56*/ 0x48, 0x89, 0x94, 0x24, 0x40, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*64*/ 0x48, 0x89, 0x94, 0x24, 0x48, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*72*/ 0x48, 0x89, 0x94, 0x24, 0x50, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*80*/ 0x48, 0x89, 0x94, 0x24, 0x58, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*88*/ 0x48, 0x89, 0x94, 0x24, 0x60, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*96*/ 0x48, 0x89, 0x94, 0x24, 0x68, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*104*/0x48, 0x89, 0x94, 0x24, 0x70, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*112*/0x48, 0x89, 0x94, 0x24, 0x78, 0x02, 0x00, 0x00,
Mark Wielaard 75a7db
+	/*120*/0x0f, 0xae, 0x64, 0x24, 0x40, 0x48, 0x8b, 0x73,
Mark Wielaard 75a7db
+	/*128*/0x10, 0x48, 0x8b, 0x7b, 0x08,
Mark Wielaard 75a7db
+	/*133*/0xe8, 0x00, 0x00, 0x00, 0x00,		/* callq <_dl_fixup> */
Mark Wielaard 75a7db
+	/*138*/0x49, 0x89, 0xc3, 0xb8, 0xee, 0x00,
Mark Wielaard 75a7db
+	/*144*/0x00, 0x00, 0x31, 0xd2, 0x0f, 0xae, 0x6c, 0x24,
Mark Wielaard 75a7db
+	/*152*/0x40, 0x4c, 0x8b, 0x4c, 0x24, 0x30, 0x4c, 0x8b,
Mark Wielaard 75a7db
+	/*160*/0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20,
Mark Wielaard 75a7db
+	/*168*/0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54,
Mark Wielaard 75a7db
+	/*176*/0x24, 0x10, 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48,
Mark Wielaard 75a7db
+	/*184*/0x8b, 0x04, 0x24, 0x48, 0x89, 0xdc, 0x48, 0x8b,
Mark Wielaard 75a7db
+	/*192*/0x1c, 0x24, 0x48, 0x83, 0xc4, 0x18, 0xf2, 0x41,
Mark Wielaard 75a7db
+	/*200*/0xff, 0xe3 };
Mark Wielaard 75a7db
+    static struct pattern pat_xsave = {
Mark Wielaard 75a7db
+	"amd64-xsave", 202, {{ 0,11 }, { 15,119 }, {138,64}, { 202,0 }} };
Mark Wielaard 75a7db
+
Mark Wielaard 75a7db
+    static UChar code_fxsave[] = {
Mark Wielaard 75a7db
+	/* 0*/ 0x53, 0x48, 0x89, 0xe3, 0x48, 0x83, 0xe4, 0xf0,
Mark Wielaard 75a7db
+	/* 8*/ 0x48, 0x81, 0xec, 0x40, 0x02, 0x00, 0x00, 0x48,
Mark Wielaard 75a7db
+	/*16*/ 0x89, 0x04, 0x24, 0x48, 0x89, 0x4c, 0x24, 0x08,
Mark Wielaard 75a7db
+	/*24*/ 0x48, 0x89, 0x54, 0x24, 0x10, 0x48, 0x89, 0x74,
Mark Wielaard 75a7db
+	/*32*/ 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20, 0x4c,
Mark Wielaard 75a7db
+	/*40*/ 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24,
Mark Wielaard 75a7db
+	/*48*/ 0x30, 0x0f, 0xae, 0x44, 0x24, 0x40, 0x48, 0x8b,
Mark Wielaard 75a7db
+	/*56*/ 0x73, 0x10, 0x48, 0x8b, 0x7b, 0x08,
Mark Wielaard 75a7db
+	/*62*/ 0xe8, 0x00, 0x00, 0x00, 0x00,		/* callq <_dl_fixup> */
Mark Wielaard 75a7db
+	/*67*/ 0x49, 0x89, 0xc3, 0x0f, 0xae,
Mark Wielaard 75a7db
+	/*72*/ 0x4c, 0x24, 0x40, 0x4c, 0x8b, 0x4c, 0x24, 0x30,
Mark Wielaard 75a7db
+	/*80*/ 0x4c, 0x8b, 0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c,
Mark Wielaard 75a7db
+	/*88*/ 0x24, 0x20, 0x48, 0x8b, 0x74, 0x24, 0x18, 0x48,
Mark Wielaard 75a7db
+	/*96*/ 0x8b, 0x54, 0x24, 0x10, 0x48, 0x8b, 0x4c, 0x24,
Mark Wielaard 75a7db
+	/*104*/0x08, 0x48, 0x8b, 0x04, 0x24, 0x48, 0x89, 0xdc,
Mark Wielaard 75a7db
+	/*112*/0x48, 0x8b, 0x1c, 0x24, 0x48, 0x83, 0xc4, 0x18,
Mark Wielaard 75a7db
+	/*120*/0xf2, 0x41, 0xff, 0xe3 };
Mark Wielaard 75a7db
+    static struct pattern pat_fxsave = {
Mark Wielaard 75a7db
+	"amd64-fxsave", 124, {{ 0,63 }, { 67,57 }, { 124,0 }} };
Mark Wielaard 75a7db
+
Mark Wielaard 75a7db
     if ((VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) &&
Mark Wielaard 75a7db
-	(VG_(strncmp)(obj->name, "/lib64/ld", 9) != 0)) return False;
Mark Wielaard 75a7db
-    return check_code(obj, code, &pat;;
Mark Wielaard 75a7db
+	(VG_(strncmp)(obj->name, "/lib64/ld", 9) != 0) &&
Mark Wielaard 75a7db
+	(VG_(strncmp)(obj->name, "/usr/lib/ld", 11) != 0) &&
Mark Wielaard 75a7db
+	(VG_(strncmp)(obj->name, "/usr/lib64/ld", 13) != 0)) return False;
Mark Wielaard 75a7db
+    Bool pat_p        = check_code(obj, code, &pat;;
Mark Wielaard 75a7db
+    Bool pat_xsavec_p = check_code(obj, code_xsavec, &pat_xsavec);
Mark Wielaard 75a7db
+    Bool pat_xsave_p  = check_code(obj, code_xsave, &pat_xsave);
Mark Wielaard 75a7db
+    Bool pat_fxsave_p = check_code(obj, code_fxsave, &pat_fxsave);
Mark Wielaard 75a7db
+    if (pat_p || pat_xsavec_p || pat_xsave_p || pat_fxsave_p) return True;
Mark Wielaard 75a7db
 #endif
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
     /* For other platforms, no patterns known */
Mark Wielaard 75a7db
@@ -254,7 +351,7 @@ obj_node* new_obj_node(DebugInfo* di, obj_node* next)
Mark Wielaard 75a7db
 	i++;
Mark Wielaard 75a7db
    }
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
-   if (runtime_resolve_addr == 0) search_runtime_resolve(obj);
Mark Wielaard 75a7db
+   if (runtime_resolve_addrs == 0) search_runtime_resolve(obj);
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
    return obj;
Mark Wielaard 75a7db
 }
Mark Wielaard 75a7db
@@ -490,6 +587,7 @@ fn_node* CLG_(get_fn_node)(BB* bb)
Mark Wielaard 75a7db
     DebugInfo* di;
Mark Wielaard 75a7db
     UInt       line_num;
Mark Wielaard 75a7db
     fn_node*   fn;
Mark Wielaard 75a7db
+    Int        i;
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
     /* fn from debug info is idempotent for a BB */
Mark Wielaard 75a7db
     if (bb->fn) return bb->fn;
Mark Wielaard 75a7db
@@ -538,12 +636,14 @@ fn_node* CLG_(get_fn_node)(BB* bb)
Mark Wielaard 75a7db
     }
Mark Wielaard 75a7db
     if (0 == VG_(strcmp)(fnname, "_exit") && !exit_bb)
Mark Wielaard 75a7db
 	exit_bb = bb;
Mark Wielaard 75a7db
-    
Mark Wielaard 75a7db
-    if (runtime_resolve_addr && 
Mark Wielaard 75a7db
-	(bb_addr(bb) >= runtime_resolve_addr) &&
Mark Wielaard 75a7db
-	(bb_addr(bb) < runtime_resolve_addr + runtime_resolve_length)) {
Mark Wielaard 75a7db
-	/* BB in runtime_resolve found by code check; use this name */
Mark Wielaard 75a7db
-      fnname = "_dl_runtime_resolve";
Mark Wielaard 75a7db
+
Mark Wielaard 75a7db
+    for (i = 0; i < runtime_resolve_addrs; i++) {
Mark Wielaard 75a7db
+      if ((bb_addr(bb) >= runtime_resolve_addr[i]) &&
Mark Wielaard 75a7db
+	  (bb_addr(bb) < runtime_resolve_addr[i] + runtime_resolve_length[i])) {
Mark Wielaard 75a7db
+	  /* BB in runtime_resolve found by code check; use this name */
Mark Wielaard 75a7db
+	  fnname = "_dl_runtime_resolve";
Mark Wielaard 75a7db
+	  break;
Mark Wielaard 75a7db
+      }
Mark Wielaard 75a7db
     }
Mark Wielaard 75a7db
 
Mark Wielaard 75a7db
     /* get fn_node struct for this function */