compiler,runtime: use __builtin_dwarf_cfa for getcallersp

Currently, the compiler lowers runtime.getcallersp to
__builtin_frame_address(1). In the C side of the runtime,
getcallersp is defined as __builtin_frame_address(0). They don't
match. Further, neither of them actually returns the caller's SP.
On AMD64, __builtin_frame_address(0) just returns the frame
pointer. __builtin_frame_address(1) returns the memory content
where the frame pointer points to, which is typically the
caller's frame pointer but can also be garbage if the frame
pointer is not enabled.

This CL changes it to use __builtin_dwarf_cfa(), which returns
the caller's SP at the call site. This matches the SP we get
from unwinding the stack.

Currently getcallersp is not used for anything real. It will be
used for precise stack scan (a new version of CL 159098).

Corresponding GCC backend change:

Index: go/go-gcc.cc
===================================================================
--- go/go-gcc.cc	(revision 268923)
+++ go/go-gcc.cc	(working copy)
@@ -735,8 +735,9 @@
   this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
 		       NULL, t, false, false);

-  // The runtime calls __builtin_frame_address for runtime.getcallersp.
-  this->define_builtin(BUILT_IN_FRAME_ADDRESS, "__builtin_frame_address",
+  // The runtime calls __builtin_dwarf_cfa for runtime.getcallersp.
+  t = build_function_type_list(ptr_type_node, NULL_TREE);
+  this->define_builtin(BUILT_IN_DWARF_CFA, "__builtin_dwarf_cfa",
 		       NULL, t, false, false);

   // The runtime calls __builtin_extract_return_addr when recording

Change-Id: I55aa9dcf0622074ba6808f1ea25cf466683e3329
Reviewed-on: https://go-review.googlesource.com/c/162905
Reviewed-by: Ian Lance Taylor <iant@golang.org>
5 files changed