1af9e5
commit f1ff8597ef9c37ff1a853411b9e3be1696c36d92
1af9e5
Author: Philippe Waroquiers <philippe.waroquiers@skynet.be>
1af9e5
Date:   Tue Sep 19 23:17:48 2017 +0200
1af9e5
1af9e5
    Implement static TLS code for more platforms
1af9e5
    
1af9e5
    gdbserver_tests/hgtls is failing on a number of platforms
1af9e5
    as it looks like static tls handling is now needed.
1af9e5
    So, omplement static tls for a few more platforms.
1af9e5
    The formulas that are platform dependent are somewhat wild guesses
1af9e5
    obtained with trial and errors.
1af9e5
    Note that arm/arm64/ppc32 are not (yet) done
1af9e5
1af9e5
diff --git a/coregrind/m_gdbserver/target.c b/coregrind/m_gdbserver/target.c
1af9e5
index 10e52fc..1f03c12 100644
1af9e5
--- a/coregrind/m_gdbserver/target.c
1af9e5
+++ b/coregrind/m_gdbserver/target.c
1af9e5
@@ -712,6 +712,7 @@ Bool valgrind_get_tls_addr (ThreadState *tst,
1af9e5
    // Check we can read the modid
1af9e5
    CHECK_DEREF(lm+lm_modid_offset, sizeof(unsigned long int), "link_map modid");
1af9e5
    modid = *(unsigned long int *)(lm+lm_modid_offset);
1af9e5
+   dlog (2, "tid %u modid %lu\n", tst->tid, modid);
1af9e5
 
1af9e5
    // Check we can access the dtv entry for modid
1af9e5
    CHECK_DEREF(dtv + 2 * modid, sizeof(CORE_ADDR), "dtv[2*modid]");
1af9e5
@@ -719,7 +720,6 @@ Bool valgrind_get_tls_addr (ThreadState *tst,
1af9e5
    // Compute the base address of the tls block.
1af9e5
    *tls_addr = *(dtv + 2 * modid);
1af9e5
 
1af9e5
-#if defined(VGA_mips32) || defined(VGA_mips64)
1af9e5
    if (*tls_addr & 1) {
1af9e5
       /* This means that computed address is not valid, most probably
1af9e5
          because given module uses Static TLS.
1af9e5
@@ -731,17 +731,24 @@ Bool valgrind_get_tls_addr (ThreadState *tst,
1af9e5
       CORE_ADDR tls_offset_addr;
1af9e5
       PtrdiffT tls_offset;
1af9e5
 
1af9e5
-      dlog(1, "computing tls_addr using static TLS\n");
1af9e5
+      dlog(2, "tls_addr (%p & 1) => computing tls_addr using static TLS\n",
1af9e5
+           (void*) *tls_addr);
1af9e5
 
1af9e5
       /* Assumes that tls_offset is placed right before tls_modid.
1af9e5
          To check the assumption, start a gdb on none/tests/tls and do:
1af9e5
-         p &((struct link_map*)0x0)->l_tls_modid
1af9e5
-         p &((struct link_map*)0x0)->l_tls_offset */
1af9e5
+           p &((struct link_map*)0x0)->l_tls_modid
1af9e5
+           p &((struct link_map*)0x0)->l_tls_offset
1af9e5
+         Instead of assuming this, we could calculate this similarly to
1af9e5
+         lm_modid_offset, by extending getplatformoffset to support querying
1af9e5
+         more than one offset.
1af9e5
+      */
1af9e5
       tls_offset_addr = lm + lm_modid_offset - sizeof(PtrdiffT);
1af9e5
 
1af9e5
       // Check we can read the tls_offset.
1af9e5
       CHECK_DEREF(tls_offset_addr, sizeof(PtrdiffT), "link_map tls_offset");
1af9e5
       tls_offset = *(PtrdiffT *)(tls_offset_addr);
1af9e5
+      dlog(2, "tls_offset_addr %p tls_offset %ld\n",
1af9e5
+           (void*)tls_offset_addr, (long)tls_offset);
1af9e5
 
1af9e5
       /* Following two values represent platform dependent constants
1af9e5
          NO_TLS_OFFSET and FORCED_DYNAMIC_TLS_OFFSET, respectively. */
1af9e5
@@ -751,9 +758,18 @@ Bool valgrind_get_tls_addr (ThreadState *tst,
1af9e5
       }
1af9e5
 
1af9e5
       // This calculation is also platform dependent.
1af9e5
+#if defined(VGA_mips32) || defined(VGA_mips64)
1af9e5
       *tls_addr = ((CORE_ADDR)dtv_loc + 2 * sizeof(CORE_ADDR) + tls_offset);
1af9e5
-   }
1af9e5
+#elif defined(VGA_ppc64be) || defined(VGA_ppc64le)
1af9e5
+      *tls_addr = ((CORE_ADDR)dtv_loc + sizeof(CORE_ADDR) + tls_offset);
1af9e5
+#elif defined(VGA_x86) || defined(VGA_amd64) || defined(VGA_s390x)
1af9e5
+      *tls_addr = (CORE_ADDR)dtv_loc - tls_offset - sizeof(CORE_ADDR);
1af9e5
+#else
1af9e5
+      // ppc32, arm, arm64
1af9e5
+      dlog(0, "target.c is missing platform code for static TLS\n");
1af9e5
+      return False;
1af9e5
 #endif
1af9e5
+   }
1af9e5
 
1af9e5
    // Finally, add tls variable offset to tls block base address.
1af9e5
    *tls_addr += offset;