gdb/dwarf: use offset in dwarf_expr_context::read_mem

The `offset` variable is the offset within the passed-in object where
`addr` falls.  We use it to verify whether `addr` falls within that
object's bounds, but then the memcpy fails to consider it, meaning that
we always copy from the beginning of the passed-in object, even if
`addr` lands in the middle of the object.  Fix that by adding `offset`
to the source argument of the memcpy.  I caught this by writing a patch
later in this series, so this fix is covered by the test added in that
later patch.

Also, I find it a bit odd to compute the offset of `addr` within the
passed-in object, before knowing if `addr` even lands within the
passed-in object's address range.  If `addr` is before the object's
address, it does an unsigned underflow, which I guess works, but is not
really intuitive.  Change it to check whether `addr` falls within the
object first, and if so, compute the offset of `addr` within the object.

Change-Id: Ibbacab6d57e693e02e2bdfec4f3a7d42d9a1bd4b
Approved-By: Tom Tromey <tom@tromey.com>
diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c
index 2125577..aa7b8cd 100644
--- a/gdb/dwarf2/expr.c
+++ b/gdb/dwarf2/expr.c
@@ -865,12 +865,12 @@ dwarf_expr_context::read_mem (gdb_byte *buf, CORE_ADDR addr,
   /* Prefer the passed-in memory, if it exists.  */
   if (this->m_addr_info != nullptr)
     {
-      CORE_ADDR offset = addr - this->m_addr_info->addr;
-
-      if (offset < this->m_addr_info->valaddr.size ()
-	  && offset + length <= this->m_addr_info->valaddr.size ())
+      if (addr >= this->m_addr_info->addr
+	  && addr + length <= (this->m_addr_info->addr
+			       + this->m_addr_info->valaddr.size ()))
 	{
-	  memcpy (buf, this->m_addr_info->valaddr.data (), length);
+	  CORE_ADDR offset = addr - this->m_addr_info->addr;
+	  memcpy (buf, this->m_addr_info->valaddr.data () + offset, length);
 	  return;
 	}
     }