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

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