Blame SOURCES/ltrace-0.7.91-ppc64le-fixes.patch

0c51c6
diff --git a/sysdeps/linux-gnu/ppc/arch.h b/sysdeps/linux-gnu/ppc/arch.h
0c51c6
index d5ad759..a8b67bb 100644
0c51c6
--- a/sysdeps/linux-gnu/ppc/arch.h
0c51c6
+++ b/sysdeps/linux-gnu/ppc/arch.h
0c51c6
@@ -32,36 +32,45 @@
0c51c6
 #define LT_ELF_MACHINE	EM_PPC
0c51c6
 
0c51c6
 #ifdef __powerpc64__ // Says 'ltrace' is 64 bits, says nothing about target.
0c51c6
-#define LT_ELFCLASS2	ELFCLASS64
0c51c6
-#define LT_ELF_MACHINE2	EM_PPC64
0c51c6
+# define LT_ELFCLASS2	ELFCLASS64
0c51c6
+# define LT_ELF_MACHINE2	EM_PPC64
0c51c6
 
0c51c6
 # ifdef __LITTLE_ENDIAN__
0c51c6
-# define BREAKPOINT_VALUE { 0x08, 0x00, 0xe0, 0x7f }
0c51c6
-# define ARCH_ENDIAN_LITTLE
0c51c6
+#  define BREAKPOINT_VALUE { 0x08, 0x00, 0xe0, 0x7f }
0c51c6
+#  define ARCH_ENDIAN_LITTLE
0c51c6
 # else
0c51c6
-# define BREAKPOINT_VALUE { 0x7f, 0xe0, 0x00, 0x08 }
0c51c6
-# define ARCH_SUPPORTS_OPD
0c51c6
-# define ARCH_ENDIAN_BIG
0c51c6
+#  define BREAKPOINT_VALUE { 0x7f, 0xe0, 0x00, 0x08 }
0c51c6
+#  define ARCH_SUPPORTS_OPD
0c51c6
+#  define ARCH_ENDIAN_BIG
0c51c6
 # endif
0c51c6
 
0c51c6
-# if _CALL_ELF != 2
0c51c6
-# define ARCH_SUPPORTS_OPD
0c51c6
-# define STACK_FRAME_OVERHEAD 112
0c51c6
+# if !defined(_CALL_ELF) || _CALL_ELF < 2
0c51c6
+#  define ARCH_SUPPORTS_OPD
0c51c6
+#  define STACK_FRAME_OVERHEAD 112
0c51c6
 #  ifndef EF_PPC64_ABI
0c51c6
-#  define EF_PPC64_ABI 3
0c51c6
+#   define EF_PPC64_ABI 3
0c51c6
 #  endif
0c51c6
-# else /* _CALL_ELF == 2 ABIv2 */
0c51c6
-# define STACK_FRAME_OVERHEAD 32
0c51c6
+# elif _CALL_ELF == 2  /* ELFv2 ABI */
0c51c6
+#  define STACK_FRAME_OVERHEAD 32
0c51c6
+# else
0c51c6
+#  error Unsupported PowerPC64 ABI.
0c51c6
 # endif /* CALL_ELF */
0c51c6
 
0c51c6
 #else
0c51c6
-#define BREAKPOINT_VALUE { 0x7f, 0xe0, 0x00, 0x08 }
0c51c6
-#define ARCH_ENDIAN_BIG
0c51c6
+# define STACK_FRAME_OVERHEAD 112
0c51c6
+# define BREAKPOINT_VALUE { 0x7f, 0xe0, 0x00, 0x08 }
0c51c6
+# define ARCH_ENDIAN_BIG
0c51c6
 # ifndef EF_PPC64_ABI
0c51c6
-# define EF_PPC64_ABI 3
0c51c6
+#  define EF_PPC64_ABI 3
0c51c6
 # endif
0c51c6
 #endif 	/* __powerpc64__ */
0c51c6
 
0c51c6
+#ifdef _CALL_ELF
0c51c6
+enum { ppc64_call_elf_abi = _CALL_ELF };
0c51c6
+#else
0c51c6
+enum { ppc64_call_elf_abi = 0 };
0c51c6
+#endif
0c51c6
+
0c51c6
 #define ARCH_HAVE_SW_SINGLESTEP
0c51c6
 #define ARCH_HAVE_ADD_PLT_ENTRY
0c51c6
 #define ARCH_HAVE_ADD_FUNC_ENTRY
0c51c6
diff --git a/sysdeps/linux-gnu/ppc/fetch.c b/sysdeps/linux-gnu/ppc/fetch.c
0c51c6
index c9381c3..c6cbd71 100644
0c51c6
--- a/sysdeps/linux-gnu/ppc/fetch.c
0c51c6
+++ b/sysdeps/linux-gnu/ppc/fetch.c
0c51c6
@@ -1,6 +1,6 @@
0c51c6
 /*
0c51c6
  * This file is part of ltrace.
0c51c6
- * Copyright (C) 2012 Petr Machata, Red Hat Inc.
0c51c6
+ * Copyright (C) 2012, 2014 Petr Machata, Red Hat Inc.
0c51c6
  *
0c51c6
  * This program is free software; you can redistribute it and/or
0c51c6
  * modify it under the terms of the GNU General Public License as
0c51c6
@@ -23,6 +23,7 @@
0c51c6
 #include <stdlib.h>
0c51c6
 #include <string.h>
0c51c6
 #include <sys/ucontext.h>
0c51c6
+#include <stdio.h>
0c51c6
 
0c51c6
 #include "backend.h"
0c51c6
 #include "fetch.h"
0c51c6
@@ -57,7 +58,7 @@ struct fetch_context {
0c51c6
 	arch_addr_t stack_pointer;
0c51c6
 	int greg;
0c51c6
 	int freg;
0c51c6
-	int ret_struct;
0c51c6
+	bool ret_struct;
0c51c6
 
0c51c6
 	union {
0c51c6
 		gregs32_t r32;
0c51c6
@@ -65,11 +66,29 @@ struct fetch_context {
0c51c6
 	} regs;
0c51c6
 	struct fpregs_t fpregs;
0c51c6
 	int vgreg;
0c51c6
-	int struct_size;
0c51c6
-	int struct_hfa_size;
0c51c6
-	int struct_hfa_count;
0c51c6
 };
0c51c6
 
0c51c6
+static bool
0c51c6
+is_eligible_hfa(struct arg_type_info *info,
0c51c6
+		struct arg_type_info **hfa_infop, size_t *hfa_countp)
0c51c6
+{
0c51c6
+	size_t hfa_count;
0c51c6
+	struct arg_type_info *hfa_info = type_get_hfa_type(info, &hfa_count);
0c51c6
+
0c51c6
+	if (hfa_info != NULL && hfa_count <= 8
0c51c6
+	    && (hfa_info->type == ARGTYPE_FLOAT
0c51c6
+		|| hfa_info->type == ARGTYPE_DOUBLE)) {
0c51c6
+
0c51c6
+		if (hfa_infop != NULL)
0c51c6
+			*hfa_infop = hfa_info;
0c51c6
+		if (hfa_countp != NULL)
0c51c6
+			*hfa_countp = hfa_count;
0c51c6
+		return true;
0c51c6
+	}
0c51c6
+
0c51c6
+	return false;
0c51c6
+}
0c51c6
+
0c51c6
 static int
0c51c6
 fetch_context_init(struct process *proc, struct fetch_context *context)
0c51c6
 {
0c51c6
@@ -125,30 +144,37 @@ arch_fetch_arg_init(enum tof type, struct process *proc,
0c51c6
 	}
0c51c6
 
0c51c6
 	context->vgreg = context->greg;
0c51c6
-	context->struct_size = 0;
0c51c6
-	context->struct_hfa_size = 0;
0c51c6
-	context->struct_hfa_count = 0;
0c51c6
 
0c51c6
 	/* Aggregates or unions of any length, and character strings
0c51c6
 	 * of length longer than 8 bytes, will be returned in a
0c51c6
 	 * storage buffer allocated by the caller. The caller will
0c51c6
 	 * pass the address of this buffer as a hidden first argument
0c51c6
 	 * in r3, causing the first explicit argument to be passed in
0c51c6
-	 * r4.  */
0c51c6
-	context->ret_struct = ret_info->type == ARGTYPE_STRUCT;
0c51c6
-	if (context->ret_struct) {
0c51c6
-#if _CALL_ELF == 2
0c51c6
-		/* if R3 points to stack, parameters will be in R4.  */
0c51c6
-		uint64_t pstack_end = ptrace(PTRACE_PEEKTEXT, proc->pid,
0c51c6
-					proc->stack_pointer, 0);
0c51c6
-		if (((arch_addr_t)context->regs.r64[3] > proc->stack_pointer)
0c51c6
-		    && (context->regs.r64[3] < pstack_end)) {
0c51c6
+	 * r4.
0c51c6
+	 */
0c51c6
+
0c51c6
+	context->ret_struct = false;
0c51c6
+
0c51c6
+	if (ppc64_call_elf_abi == 2) {
0c51c6
+		/* With ELFv2 ABI, aggregates that consist
0c51c6
+		 * (recursively) only of members of the same
0c51c6
+		 * floating-point or vector type, are passed in a
0c51c6
+		 * series of floating-point resp. vector registers.
0c51c6
+		 * Additionally, when returning any aggregate of up to
0c51c6
+		 * 16 bytes, general-purpose registers are used.  */
0c51c6
+
0c51c6
+		if (ret_info->type == ARGTYPE_STRUCT
0c51c6
+		    && ! is_eligible_hfa(ret_info, NULL, NULL)
0c51c6
+		    && type_sizeof(proc, ret_info) > 16) {
0c51c6
+
0c51c6
+			context->ret_struct = true;
0c51c6
 			context->greg++;
0c51c6
 			context->stack_pointer += 8;
0c51c6
 		}
0c51c6
-#else
0c51c6
+
0c51c6
+	} else if (ret_info->type == ARGTYPE_STRUCT) {
0c51c6
+		context->ret_struct = true;
0c51c6
 		context->greg++;
0c51c6
-#endif
0c51c6
 	}
0c51c6
 
0c51c6
 	return context;
0c51c6
@@ -176,17 +202,16 @@ allocate_stack_slot(struct fetch_context *ctx, struct process *proc,
0c51c6
 
0c51c6
 	size_t a = type_alignof(proc, info);
0c51c6
 	size_t off = 0;
0c51c6
-	if (proc->e_machine == EM_PPC && a < 4)
0c51c6
-		a = 4;
0c51c6
-#if _CALL_ELF == 2
0c51c6
-	else if (proc->e_machine == EM_PPC64 && sz == 4 && is_hfa_type)
0c51c6
+	if (proc->e_machine == EM_PPC && a < 4) {
0c51c6
 		a = 4;
0c51c6
-	else
0c51c6
-		a = 8;
0c51c6
-#else
0c51c6
-	else if (proc->e_machine == EM_PPC64 && a < 8)
0c51c6
-#endif
0c51c6
+	} else if (ppc64_call_elf_abi == 2) {
0c51c6
+		if (proc->e_machine == EM_PPC64 && sz == 4 && is_hfa_type) {
0c51c6
+			a = 4;
0c51c6
+		} else
0c51c6
+			a = 8;
0c51c6
+	} else if (proc->e_machine == EM_PPC64 && a < 8) {
0c51c6
 		a = 8;
0c51c6
+	}
0c51c6
 
0c51c6
 	/* XXX Remove the two double casts when arch_addr_t
0c51c6
 	 * becomes integral type.  */
0c51c6
@@ -259,18 +284,19 @@ allocate_gpr(struct fetch_context *ctx, struct process *proc,
0c51c6
 	if (sz == (size_t)-1)
0c51c6
 		return -1;
0c51c6
 	assert(sz == 1 || sz == 2 || sz == 4 || sz == 8);
0c51c6
-#if _CALL_ELF == 2
0c51c6
-	/* Consume the stack slot corresponding to this arg.  */
0c51c6
-	if ((sz + off) >= 8)
0c51c6
-		ctx->greg++;
0c51c6
 
0c51c6
-	if (is_hfa_type)
0c51c6
-		ctx->stack_pointer += sz;
0c51c6
-	else
0c51c6
-		ctx->stack_pointer += 8;
0c51c6
-#else
0c51c6
-	ctx->greg++;
0c51c6
-#endif
0c51c6
+	if (ppc64_call_elf_abi == 2) {
0c51c6
+		/* Consume the stack slot corresponding to this arg.  */
0c51c6
+		if ((sz + off) >= 8)
0c51c6
+			ctx->greg++;
0c51c6
+
0c51c6
+		if (is_hfa_type)
0c51c6
+			ctx->stack_pointer += sz;
0c51c6
+		else
0c51c6
+			ctx->stack_pointer += 8;
0c51c6
+	} else {
0c51c6
+		ctx->greg++;
0c51c6
+	}
0c51c6
 
0c51c6
 	if (valuep == NULL)
0c51c6
 		return 0;
0c51c6
@@ -326,7 +352,6 @@ allocate_float(struct fetch_context *ctx, struct process *proc,
0c51c6
 	return allocate_stack_slot(ctx, proc, info, valuep, is_hfa_type);
0c51c6
 }
0c51c6
 
0c51c6
-#if _CALL_ELF == 2
0c51c6
 static int
0c51c6
 allocate_hfa(struct fetch_context *ctx, struct process *proc,
0c51c6
 	     struct arg_type_info *info, struct value *valuep,
0c51c6
@@ -336,27 +361,27 @@ allocate_hfa(struct fetch_context *ctx, struct process *proc,
0c51c6
 	if (sz == (size_t)-1)
0c51c6
 		return -1;
0c51c6
 
0c51c6
-	ctx->struct_hfa_size += sz;
0c51c6
-
0c51c6
 	/* There are two changes regarding structure return types:
0c51c6
-	 * * heterogeneous float/vector structs are returned
0c51c6
-	 *   in (multiple) FP/vector registers,
0c51c6
-	 *   instead of via implicit reference.
0c51c6
-	 * * small structs (up to 16 bytes) are return
0c51c6
-	 *   in one or two GPRs, instead of via implicit reference.
0c51c6
+	 * * heterogeneous float/vector structs are returned in
0c51c6
+	 *   (multiple) FP/vector registers, instead of via implicit
0c51c6
+	 *   reference.
0c51c6
+	 * * small structs (up to 16 bytes) are return in one or two
0c51c6
+	 *   GPRs, instead of via implicit reference.
0c51c6
 	 *
0c51c6
 	 * Other structures (larger than 16 bytes, not heterogeneous)
0c51c6
 	 * are still returned via implicit reference (i.e. a pointer
0c51c6
 	 * to memory where to return the struct being passed in r3).
0c51c6
-	 * Of course, whether or not an implicit reference pointer
0c51c6
-	 * is present will shift the remaining arguments,
0c51c6
-	 * so you need to get this right for ELFv2 in order
0c51c6
-	 * to get the arguments correct.
0c51c6
+	 * Of course, whether or not an implicit reference pointer is
0c51c6
+	 * present will shift the remaining arguments, so you need to
0c51c6
+	 * get this right for ELFv2 in order to get the arguments
0c51c6
+	 * correct.
0c51c6
+	 *
0c51c6
 	 * If an actual parameter is known to correspond to an HFA
0c51c6
 	 * formal parameter, each element is passed in the next
0c51c6
 	 * available floating-point argument register starting at fp1
0c51c6
 	 * until the fp13. The remaining elements of the aggregate are
0c51c6
-	 * passed on the stack.  */
0c51c6
+	 * passed on the stack.
0c51c6
+	 */
0c51c6
 	size_t slot_off = 0;
0c51c6
 
0c51c6
 	unsigned char *buf = value_reserve(valuep, sz);
0c51c6
@@ -366,26 +391,17 @@ allocate_hfa(struct fetch_context *ctx, struct process *proc,
0c51c6
 	struct arg_type_info *hfa_info = type_get_simple(hfa_type);
0c51c6
 	size_t hfa_sz = type_sizeof(proc, hfa_info);
0c51c6
 
0c51c6
-	if (hfa_count > 8)
0c51c6
-		ctx->struct_hfa_count += hfa_count;
0c51c6
-
0c51c6
 	while (hfa_count > 0 && ctx->freg <= 13) {
0c51c6
-		int rc;
0c51c6
 		struct value tmp;
0c51c6
-
0c51c6
 		value_init(&tmp, proc, NULL, hfa_info, 0);
0c51c6
+		int rc = allocate_float(ctx, proc, hfa_info,
0c51c6
+					&tmp, slot_off, true);
0c51c6
+		if (rc == 0)
0c51c6
+			memcpy(buf, value_get_data(&tmp, NULL), hfa_sz);
0c51c6
+		value_destroy(&tmp);
0c51c6
 
0c51c6
-		/* Hetereogeneous struct - get value on GPR or stack.  */
0c51c6
-		if (((hfa_type == ARGTYPE_FLOAT
0c51c6
-		    || hfa_type == ARGTYPE_DOUBLE)
0c51c6
-		      && hfa_count <= 8))
0c51c6
-			rc = allocate_float(ctx, proc, hfa_info, &tmp,
0c51c6
-						slot_off, true);
0c51c6
-		else
0c51c6
-			rc = allocate_gpr(ctx, proc, hfa_info, &tmp,
0c51c6
-						slot_off, true);
0c51c6
-
0c51c6
-		memcpy(buf, value_get_data(&tmp, NULL), hfa_sz);
0c51c6
+		if (rc < 0)
0c51c6
+			return -1;
0c51c6
 
0c51c6
 		slot_off += hfa_sz;
0c51c6
 		buf += hfa_sz;
0c51c6
@@ -394,17 +410,13 @@ allocate_hfa(struct fetch_context *ctx, struct process *proc,
0c51c6
 			slot_off = 0;
0c51c6
 			ctx->vgreg++;
0c51c6
 		}
0c51c6
-
0c51c6
-		value_destroy(&tmp);
0c51c6
-		if (rc < 0)
0c51c6
-			return -1;
0c51c6
 	}
0c51c6
 	if (hfa_count == 0)
0c51c6
 		return 0;
0c51c6
 
0c51c6
 	/* if no remaining FP, GPR corresponding to slot is used
0c51c6
-	* Mostly it is in part of r10.  */
0c51c6
-	if (ctx->struct_hfa_size <= 64 && ctx->vgreg == 10) {
0c51c6
+	 * Mostly it is in part of r10.  */
0c51c6
+	if (ctx->vgreg == 10) {
0c51c6
 		while (ctx->vgreg <= 10) {
0c51c6
 			struct value tmp;
0c51c6
 			value_init(&tmp, proc, NULL, hfa_info, 0);
0c51c6
@@ -428,11 +440,8 @@ allocate_hfa(struct fetch_context *ctx, struct process *proc,
0c51c6
 		}
0c51c6
 	}
0c51c6
 
0c51c6
-	if (hfa_count == 0)
0c51c6
-		return 0;
0c51c6
-
0c51c6
 	/* Remaining values are on stack */
0c51c6
-	while (hfa_count) {
0c51c6
+	while (hfa_count > 0) {
0c51c6
 		struct value tmp;
0c51c6
 		value_init(&tmp, proc, NULL, hfa_info, 0);
0c51c6
 
0c51c6
@@ -444,7 +453,6 @@ allocate_hfa(struct fetch_context *ctx, struct process *proc,
0c51c6
 	}
0c51c6
 	return 0;
0c51c6
 }
0c51c6
-#endif
0c51c6
 
0c51c6
 static int
0c51c6
 allocate_argument(struct fetch_context *ctx, struct process *proc,
0c51c6
@@ -459,24 +467,20 @@ allocate_argument(struct fetch_context *ctx, struct process *proc,
0c51c6
 	case ARGTYPE_FLOAT:
0c51c6
 	case ARGTYPE_DOUBLE:
0c51c6
 		return allocate_float(ctx, proc, info, valuep,
0c51c6
-					8 - type_sizeof(proc,info), false);
0c51c6
+				      8 - type_sizeof(proc,info), false);
0c51c6
 
0c51c6
 	case ARGTYPE_STRUCT:
0c51c6
 		if (proc->e_machine == EM_PPC) {
0c51c6
 			if (value_pass_by_reference(valuep) < 0)
0c51c6
 				return -1;
0c51c6
-		} else {
0c51c6
-#if _CALL_ELF == 2
0c51c6
+		} else if (ppc64_call_elf_abi == 2) {
0c51c6
 			struct arg_type_info *hfa_info;
0c51c6
-			size_t hfa_size;
0c51c6
-			hfa_info = type_get_hfa_type(info, &hfa_size);
0c51c6
-			if (hfa_info != NULL ) {
0c51c6
-				size_t sz = type_sizeof(proc, info);
0c51c6
-				ctx->struct_size += sz;
0c51c6
+			size_t hfa_count;
0c51c6
+			if (is_eligible_hfa(info, &hfa_info, &hfa_count)) {
0c51c6
 				return allocate_hfa(ctx, proc, info, valuep,
0c51c6
-						hfa_info->type, hfa_size);
0c51c6
+						hfa_info->type, hfa_count);
0c51c6
 			}
0c51c6
-#endif
0c51c6
+		} else {
0c51c6
 			/* PPC64: Fixed size aggregates and unions passed by
0c51c6
 			 * value are mapped to as many doublewords of the
0c51c6
 			 * parameter save area as the value uses in memory.
0c51c6
@@ -510,9 +514,6 @@ allocate_argument(struct fetch_context *ctx, struct process *proc,
0c51c6
 	if (sz == (size_t)-1)
0c51c6
 		return -1;
0c51c6
 
0c51c6
-	if (ctx->ret_struct)
0c51c6
-		ctx->struct_size += sz;
0c51c6
-
0c51c6
 	size_t slots = (sz + width - 1) / width;  /* Round up.  */
0c51c6
 	unsigned char *buf = value_reserve(valuep, slots * width);
0c51c6
 	if (buf == NULL)
0c51c6
@@ -605,19 +606,7 @@ arch_fetch_retval(struct fetch_context *ctx, enum tof type,
0c51c6
 	if (fetch_context_init(proc, ctx) < 0)
0c51c6
 		return -1;
0c51c6
 
0c51c6
-#if _CALL_ELF == 2
0c51c6
-	void *ptr = (void *)(ctx->regs.r64[1]+32);
0c51c6
-	uint64_t val = ptrace(PTRACE_PEEKTEXT, proc->pid, ptr, 0);
0c51c6
-
0c51c6
-	if (ctx->ret_struct
0c51c6
-	   && ((ctx->struct_size > 64
0c51c6
-	      || ctx->struct_hfa_count > 8
0c51c6
-	      || (ctx->struct_hfa_size == 0 && ctx->struct_size > 56)
0c51c6
-	      || (ctx->regs.r64[3] == ctx->regs.r64[1]+32)
0c51c6
-	      || (ctx->regs.r64[3] == val )))) {
0c51c6
-#else
0c51c6
 	if (ctx->ret_struct) {
0c51c6
-#endif
0c51c6
 		assert(info->type == ARGTYPE_STRUCT);
0c51c6
 
0c51c6
 		uint64_t addr = read_gpr(ctx, proc, 3);