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

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