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

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