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

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