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

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