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

f84355
From eea4ad2cce289753aaa35b4e0258a76d8f8f367c Mon Sep 17 00:00:00 2001
f84355
From: Thierry Fauck <thierry@linux.vnet.ibm.com>
f84355
Date: Tue, 13 May 2014 07:48:24 -0400
f84355
Subject: [PATCH] Support for powerpc64 arch ppc64el
f84355
f84355
Signed-off-by: Thierry Fauck <thierry@linux.vnet.ibm.com>
f84355
f84355
	Add support for ppc64le proc and ELF ABIv2.
f84355
	Provides support for irelative and wchar
f84355
---
f84355
 ltrace-elf.c                           |    2 +-
f84355
 ltrace-elf.h                           |    1 +
f84355
 sysdeps/linux-gnu/ppc/arch.h           |   35 ++++-
f84355
 sysdeps/linux-gnu/ppc/fetch.c          |  244 +++++++++++++++++++++++++++++---
f84355
 sysdeps/linux-gnu/ppc/plt.c            |   98 ++++++++++++--
f84355
 sysdeps/linux-gnu/ppc/trace.c          |   10 ++
f84355
 testsuite/ltrace.main/system_calls.exp |    2 +-
f84355
 7 files changed, 356 insertions(+), 36 deletions(-)
f84355
f84355
diff --git a/ltrace-elf.c b/ltrace-elf.c
f84355
index 8997518..f638342 100644
f84355
--- a/ltrace-elf.c
f84355
+++ b/ltrace-elf.c
f84355
@@ -859,7 +859,7 @@ populate_plt(struct process *proc, const char *filename,
f84355
 	return 0;
f84355
 }
f84355
 
f84355
-static void
f84355
+void
f84355
 delete_symbol_chain(struct library_symbol *libsym)
f84355
 {
f84355
 	while (libsym != NULL) {
f84355
diff --git a/ltrace-elf.h b/ltrace-elf.h
f84355
index db4ffe9..4a824c4 100644
f84355
--- a/ltrace-elf.h
f84355
+++ b/ltrace-elf.h
f84355
@@ -166,6 +166,7 @@ int elf_read_next_uleb128(Elf_Data *data, GElf_Xword *offset, uint64_t *retp);
f84355
 /* Return whether there's AMOUNT more bytes after OFFSET in DATA.  */
f84355
 int elf_can_read_next(Elf_Data *data, GElf_Xword offset, GElf_Xword amount);
f84355
 
f84355
+void delete_symbol_chain(struct library_symbol *);
f84355
 #if __WORDSIZE == 32
f84355
 #define PRI_ELF_ADDR		PRIx32
f84355
 #define GELF_ADDR_CAST(x)	(void *)(uint32_t)(x)
f84355
diff --git a/sysdeps/linux-gnu/ppc/arch.h b/sysdeps/linux-gnu/ppc/arch.h
f84355
index bf9b5dc..7918a13 100644
f84355
--- a/sysdeps/linux-gnu/ppc/arch.h
f84355
+++ b/sysdeps/linux-gnu/ppc/arch.h
f84355
@@ -23,8 +23,8 @@
f84355
 #define LTRACE_PPC_ARCH_H
f84355
 
f84355
 #include <gelf.h>
f84355
+#include <stdbool.h>
f84355
 
f84355
-#define BREAKPOINT_VALUE { 0x7f, 0xe0, 0x00, 0x08 }
f84355
 #define BREAKPOINT_LENGTH 4
f84355
 #define DECR_PC_AFTER_BREAK 0
f84355
 
f84355
@@ -34,8 +34,33 @@
f84355
 #ifdef __powerpc64__ // Says 'ltrace' is 64 bits, says nothing about target.
f84355
 #define LT_ELFCLASS2	ELFCLASS64
f84355
 #define LT_ELF_MACHINE2	EM_PPC64
f84355
-#define ARCH_SUPPORTS_OPD
f84355
-#endif
f84355
+
f84355
+# ifdef __LITTLE_ENDIAN__
f84355
+# define BREAKPOINT_VALUE { 0x08, 0x00, 0xe0, 0x7f }
f84355
+# define ARCH_ENDIAN_LITTLE
f84355
+# else
f84355
+# define BREAKPOINT_VALUE { 0x7f, 0xe0, 0x00, 0x08 }
f84355
+# define ARCH_SUPPORTS_OPD
f84355
+# define ARCH_ENDIAN_BIG
f84355
+# endif
f84355
+
f84355
+# if _CALL_ELF != 2
f84355
+# define ARCH_SUPPORTS_OPD
f84355
+# define STACK_FRAME_OVERHEAD 112
f84355
+#  ifndef EF_PPC64_ABI
f84355
+#  define EF_PPC64_ABI 3
f84355
+#  endif
f84355
+# else /* _CALL_ELF == 2 ABIv2 */
f84355
+# define STACK_FRAME_OVERHEAD 32
f84355
+# endif /* CALL_ELF */
f84355
+
f84355
+#else
f84355
+#define BREAKPOINT_VALUE { 0x7f, 0xe0, 0x00, 0x08 }
f84355
+#define ARCH_ENDIAN_BIG
f84355
+# ifndef EF_PPC64_ABI
f84355
+# define EF_PPC64_ABI 3
f84355
+# endif
f84355
+#endif 	/* __powerpc64__ */
f84355
 
f84355
 #define ARCH_HAVE_SW_SINGLESTEP
f84355
 #define ARCH_HAVE_ADD_PLT_ENTRY
f84355
@@ -43,7 +68,6 @@
f84355
 #define ARCH_HAVE_TRANSLATE_ADDRESS
f84355
 #define ARCH_HAVE_DYNLINK_DONE
f84355
 #define ARCH_HAVE_FETCH_ARG
f84355
-#define ARCH_ENDIAN_BIG
f84355
 #define ARCH_HAVE_SIZEOF
f84355
 #define ARCH_HAVE_ALIGNOF
f84355
 
f84355
@@ -56,7 +80,8 @@ struct arch_ltelf_data {
f84355
 	Elf_Data *opd_data;
f84355
 	GElf_Addr opd_base;
f84355
 	GElf_Xword opd_size;
f84355
-	int secure_plt;
f84355
+	bool secure_plt : 1;
f84355
+	bool elfv2_abi  : 1;
f84355
 
f84355
 	Elf_Data *reladyn;
f84355
 	size_t reladyn_count;
f84355
diff --git a/sysdeps/linux-gnu/ppc/fetch.c b/sysdeps/linux-gnu/ppc/fetch.c
f84355
index ed38336..c9381c3 100644
f84355
--- a/sysdeps/linux-gnu/ppc/fetch.c
f84355
+++ b/sysdeps/linux-gnu/ppc/fetch.c
f84355
@@ -30,9 +30,11 @@
f84355
 #include "ptrace.h"
f84355
 #include "proc.h"
f84355
 #include "value.h"
f84355
+#include "ltrace-elf.h"
f84355
 
f84355
 static int allocate_gpr(struct fetch_context *ctx, struct process *proc,
f84355
-			struct arg_type_info *info, struct value *valuep);
f84355
+			struct arg_type_info *info, struct value *valuep,
f84355
+			size_t off, bool is_hfa_type);
f84355
 
f84355
 /* Floating point registers have the same width on 32-bit as well as
f84355
  * 64-bit PPC, but <ucontext.h> presents a different API depending on
f84355
@@ -62,7 +64,10 @@ struct fetch_context {
f84355
 		gregs64_t r64;
f84355
 	} regs;
f84355
 	struct fpregs_t fpregs;
f84355
-
f84355
+	int vgreg;
f84355
+	int struct_size;
f84355
+	int struct_hfa_size;
f84355
+	int struct_hfa_count;
f84355
 };
f84355
 
f84355
 static int
f84355
@@ -74,7 +79,8 @@ fetch_context_init(struct process *proc, struct fetch_context *context)
f84355
 	if (proc->e_machine == EM_PPC)
f84355
 		context->stack_pointer = proc->stack_pointer + 8;
f84355
 	else
f84355
-		context->stack_pointer = proc->stack_pointer + 112;
f84355
+		context->stack_pointer = proc->stack_pointer
f84355
+			+ STACK_FRAME_OVERHEAD;
f84355
 
f84355
 	/* When ltrace is 64-bit, we might use PTRACE_GETREGS to
f84355
 	 * obtain 64-bit as well as 32-bit registers.  But if we do it
f84355
@@ -118,6 +124,11 @@ arch_fetch_arg_init(enum tof type, struct process *proc,
f84355
 		return NULL;
f84355
 	}
f84355
 
f84355
+	context->vgreg = context->greg;
f84355
+	context->struct_size = 0;
f84355
+	context->struct_hfa_size = 0;
f84355
+	context->struct_hfa_count = 0;
f84355
+
f84355
 	/* Aggregates or unions of any length, and character strings
f84355
 	 * of length longer than 8 bytes, will be returned in a
f84355
 	 * storage buffer allocated by the caller. The caller will
f84355
@@ -125,8 +136,20 @@ arch_fetch_arg_init(enum tof type, struct process *proc,
f84355
 	 * in r3, causing the first explicit argument to be passed in
f84355
 	 * r4.  */
f84355
 	context->ret_struct = ret_info->type == ARGTYPE_STRUCT;
f84355
-	if (context->ret_struct)
f84355
+	if (context->ret_struct) {
f84355
+#if _CALL_ELF == 2
f84355
+		/* if R3 points to stack, parameters will be in R4.  */
f84355
+		uint64_t pstack_end = ptrace(PTRACE_PEEKTEXT, proc->pid,
f84355
+					proc->stack_pointer, 0);
f84355
+		if (((arch_addr_t)context->regs.r64[3] > proc->stack_pointer)
f84355
+		    && (context->regs.r64[3] < pstack_end)) {
f84355
+			context->greg++;
f84355
+			context->stack_pointer += 8;
f84355
+		}
f84355
+#else
f84355
 		context->greg++;
f84355
+#endif
f84355
+	}
f84355
 
f84355
 	return context;
f84355
 }
f84355
@@ -144,7 +167,8 @@ arch_fetch_arg_clone(struct process *proc,
f84355
 
f84355
 static int
f84355
 allocate_stack_slot(struct fetch_context *ctx, struct process *proc,
f84355
-		    struct arg_type_info *info, struct value *valuep)
f84355
+		    struct arg_type_info *info, struct value *valuep,
f84355
+		    bool is_hfa_type)
f84355
 {
f84355
 	size_t sz = type_sizeof(proc, info);
f84355
 	if (sz == (size_t)-1)
f84355
@@ -154,7 +178,14 @@ allocate_stack_slot(struct fetch_context *ctx, struct process *proc,
f84355
 	size_t off = 0;
f84355
 	if (proc->e_machine == EM_PPC && a < 4)
f84355
 		a = 4;
f84355
+#if _CALL_ELF == 2
f84355
+	else if (proc->e_machine == EM_PPC64 && sz == 4 && is_hfa_type)
f84355
+		a = 4;
f84355
+	else
f84355
+		a = 8;
f84355
+#else
f84355
 	else if (proc->e_machine == EM_PPC64 && a < 8)
f84355
+#endif
f84355
 		a = 8;
f84355
 
f84355
 	/* XXX Remove the two double casts when arch_addr_t
f84355
@@ -164,7 +195,7 @@ allocate_stack_slot(struct fetch_context *ctx, struct process *proc,
f84355
 
f84355
 	if (valuep != NULL)
f84355
 		value_in_inferior(valuep, ctx->stack_pointer + off);
f84355
-	ctx->stack_pointer += sz;
f84355
+	ctx->stack_pointer += a;
f84355
 
f84355
 	return 0;
f84355
 }
f84355
@@ -216,19 +247,34 @@ align_small_int(unsigned char *buf, size_t w, size_t sz)
f84355
 
f84355
 static int
f84355
 allocate_gpr(struct fetch_context *ctx, struct process *proc,
f84355
-	     struct arg_type_info *info, struct value *valuep)
f84355
+	     struct arg_type_info *info, struct value *valuep,
f84355
+	     size_t off, bool is_hfa_type)
f84355
 {
f84355
 	if (ctx->greg > 10)
f84355
-		return allocate_stack_slot(ctx, proc, info, valuep);
f84355
+		return allocate_stack_slot(ctx, proc, info, valuep, is_hfa_type);
f84355
 
f84355
-	int reg_num = ctx->greg++;
f84355
-	if (valuep == NULL)
f84355
-		return 0;
f84355
+	int reg_num = ctx->greg;
f84355
 
f84355
 	size_t sz = type_sizeof(proc, info);
f84355
 	if (sz == (size_t)-1)
f84355
 		return -1;
f84355
 	assert(sz == 1 || sz == 2 || sz == 4 || sz == 8);
f84355
+#if _CALL_ELF == 2
f84355
+	/* Consume the stack slot corresponding to this arg.  */
f84355
+	if ((sz + off) >= 8)
f84355
+		ctx->greg++;
f84355
+
f84355
+	if (is_hfa_type)
f84355
+		ctx->stack_pointer += sz;
f84355
+	else
f84355
+		ctx->stack_pointer += 8;
f84355
+#else
f84355
+	ctx->greg++;
f84355
+#endif
f84355
+
f84355
+	if (valuep == NULL)
f84355
+		return 0;
f84355
+
f84355
 	if (value_reserve(valuep, sz) == NULL)
f84355
 		return -1;
f84355
 
f84355
@@ -240,13 +286,14 @@ allocate_gpr(struct fetch_context *ctx, struct process *proc,
f84355
 	u.i64 = read_gpr(ctx, proc, reg_num);
f84355
 	if (proc->e_machine == EM_PPC)
f84355
 		align_small_int(u.buf, 8, sz);
f84355
-	memcpy(value_get_raw_data(valuep), u.buf, sz);
f84355
+	memcpy(value_get_raw_data(valuep), u.buf + off, sz);
f84355
 	return 0;
f84355
 }
f84355
 
f84355
 static int
f84355
 allocate_float(struct fetch_context *ctx, struct process *proc,
f84355
-	       struct arg_type_info *info, struct value *valuep)
f84355
+	       struct arg_type_info *info, struct value *valuep,
f84355
+	       size_t off, bool is_hfa_type)
f84355
 {
f84355
 	int pool = proc->e_machine == EM_PPC64 ? 13 : 8;
f84355
 	if (ctx->freg <= pool) {
f84355
@@ -257,8 +304,12 @@ allocate_float(struct fetch_context *ctx, struct process *proc,
f84355
 		} u = { .d = ctx->fpregs.fpregs[ctx->freg] };
f84355
 
f84355
 		ctx->freg++;
f84355
+
f84355
+		if (!is_hfa_type)
f84355
+			ctx->vgreg++;
f84355
+
f84355
 		if (proc->e_machine == EM_PPC64)
f84355
-			allocate_gpr(ctx, proc, info, NULL);
f84355
+			allocate_gpr(ctx, proc, info, NULL, off, is_hfa_type);
f84355
 
f84355
 		size_t sz = sizeof(double);
f84355
 		if (info->type == ARGTYPE_FLOAT) {
f84355
@@ -272,8 +323,128 @@ allocate_float(struct fetch_context *ctx, struct process *proc,
f84355
 		memcpy(value_get_raw_data(valuep), u.buf, sz);
f84355
 		return 0;
f84355
 	}
f84355
-	return allocate_stack_slot(ctx, proc, info, valuep);
f84355
+	return allocate_stack_slot(ctx, proc, info, valuep, is_hfa_type);
f84355
+}
f84355
+
f84355
+#if _CALL_ELF == 2
f84355
+static int
f84355
+allocate_hfa(struct fetch_context *ctx, struct process *proc,
f84355
+	     struct arg_type_info *info, struct value *valuep,
f84355
+	     enum arg_type hfa_type, size_t hfa_count)
f84355
+{
f84355
+	size_t sz = type_sizeof(proc, info);
f84355
+	if (sz == (size_t)-1)
f84355
+		return -1;
f84355
+
f84355
+	ctx->struct_hfa_size += sz;
f84355
+
f84355
+	/* There are two changes regarding structure return types:
f84355
+	 * * heterogeneous float/vector structs are returned
f84355
+	 *   in (multiple) FP/vector registers,
f84355
+	 *   instead of via implicit reference.
f84355
+	 * * small structs (up to 16 bytes) are return
f84355
+	 *   in one or two GPRs, instead of via implicit reference.
f84355
+	 *
f84355
+	 * Other structures (larger than 16 bytes, not heterogeneous)
f84355
+	 * are still returned via implicit reference (i.e. a pointer
f84355
+	 * to memory where to return the struct being passed in r3).
f84355
+	 * Of course, whether or not an implicit reference pointer
f84355
+	 * is present will shift the remaining arguments,
f84355
+	 * so you need to get this right for ELFv2 in order
f84355
+	 * to get the arguments correct.
f84355
+	 * If an actual parameter is known to correspond to an HFA
f84355
+	 * formal parameter, each element is passed in the next
f84355
+	 * available floating-point argument register starting at fp1
f84355
+	 * until the fp13. The remaining elements of the aggregate are
f84355
+	 * passed on the stack.  */
f84355
+	size_t slot_off = 0;
f84355
+
f84355
+	unsigned char *buf = value_reserve(valuep, sz);
f84355
+	if (buf == NULL)
f84355
+		return -1;
f84355
+
f84355
+	struct arg_type_info *hfa_info = type_get_simple(hfa_type);
f84355
+	size_t hfa_sz = type_sizeof(proc, hfa_info);
f84355
+
f84355
+	if (hfa_count > 8)
f84355
+		ctx->struct_hfa_count += hfa_count;
f84355
+
f84355
+	while (hfa_count > 0 && ctx->freg <= 13) {
f84355
+		int rc;
f84355
+		struct value tmp;
f84355
+
f84355
+		value_init(&tmp, proc, NULL, hfa_info, 0);
f84355
+
f84355
+		/* Hetereogeneous struct - get value on GPR or stack.  */
f84355
+		if (((hfa_type == ARGTYPE_FLOAT
f84355
+		    || hfa_type == ARGTYPE_DOUBLE)
f84355
+		      && hfa_count <= 8))
f84355
+			rc = allocate_float(ctx, proc, hfa_info, &tmp,
f84355
+						slot_off, true);
f84355
+		else
f84355
+			rc = allocate_gpr(ctx, proc, hfa_info, &tmp,
f84355
+						slot_off, true);
f84355
+
f84355
+		memcpy(buf, value_get_data(&tmp, NULL), hfa_sz);
f84355
+
f84355
+		slot_off += hfa_sz;
f84355
+		buf += hfa_sz;
f84355
+		hfa_count--;
f84355
+		if (slot_off == 8) {
f84355
+			slot_off = 0;
f84355
+			ctx->vgreg++;
f84355
+		}
f84355
+
f84355
+		value_destroy(&tmp);
f84355
+		if (rc < 0)
f84355
+			return -1;
f84355
+	}
f84355
+	if (hfa_count == 0)
f84355
+		return 0;
f84355
+
f84355
+	/* if no remaining FP, GPR corresponding to slot is used
f84355
+	* Mostly it is in part of r10.  */
f84355
+	if (ctx->struct_hfa_size <= 64 && ctx->vgreg == 10) {
f84355
+		while (ctx->vgreg <= 10) {
f84355
+			struct value tmp;
f84355
+			value_init(&tmp, proc, NULL, hfa_info, 0);
f84355
+			union {
f84355
+				uint64_t i64;
f84355
+				unsigned char buf[0];
f84355
+			} u;
f84355
+
f84355
+			u.i64 = read_gpr(ctx, proc, ctx->vgreg);
f84355
+
f84355
+			memcpy(buf, u.buf + slot_off, hfa_sz);
f84355
+			slot_off += hfa_sz;
f84355
+			buf += hfa_sz;
f84355
+			hfa_count--;
f84355
+			ctx->stack_pointer += hfa_sz;
f84355
+			if (slot_off >= 8 ) {
f84355
+				slot_off = 0;
f84355
+				ctx->vgreg++;
f84355
+			}
f84355
+			value_destroy(&tmp);
f84355
+		}
f84355
+	}
f84355
+
f84355
+	if (hfa_count == 0)
f84355
+		return 0;
f84355
+
f84355
+	/* Remaining values are on stack */
f84355
+	while (hfa_count) {
f84355
+		struct value tmp;
f84355
+		value_init(&tmp, proc, NULL, hfa_info, 0);
f84355
+
f84355
+		value_in_inferior(&tmp, ctx->stack_pointer);
f84355
+		memcpy(buf, value_get_data(&tmp, NULL), hfa_sz);
f84355
+		ctx->stack_pointer += hfa_sz;
f84355
+		buf += hfa_sz;
f84355
+		hfa_count--;
f84355
+	}
f84355
+	return 0;
f84355
 }
f84355
+#endif
f84355
 
f84355
 static int
f84355
 allocate_argument(struct fetch_context *ctx, struct process *proc,
f84355
@@ -287,13 +458,25 @@ allocate_argument(struct fetch_context *ctx, struct process *proc,
f84355
 
f84355
 	case ARGTYPE_FLOAT:
f84355
 	case ARGTYPE_DOUBLE:
f84355
-		return allocate_float(ctx, proc, info, valuep);
f84355
+		return allocate_float(ctx, proc, info, valuep,
f84355
+					8 - type_sizeof(proc,info), false);
f84355
 
f84355
 	case ARGTYPE_STRUCT:
f84355
 		if (proc->e_machine == EM_PPC) {
f84355
 			if (value_pass_by_reference(valuep) < 0)
f84355
 				return -1;
f84355
 		} else {
f84355
+#if _CALL_ELF == 2
f84355
+			struct arg_type_info *hfa_info;
f84355
+			size_t hfa_size;
f84355
+			hfa_info = type_get_hfa_type(info, &hfa_size);
f84355
+			if (hfa_info != NULL ) {
f84355
+				size_t sz = type_sizeof(proc, info);
f84355
+				ctx->struct_size += sz;
f84355
+				return allocate_hfa(ctx, proc, info, valuep,
f84355
+						hfa_info->type, hfa_size);
f84355
+			}
f84355
+#endif
f84355
 			/* PPC64: Fixed size aggregates and unions passed by
f84355
 			 * value are mapped to as many doublewords of the
f84355
 			 * parameter save area as the value uses in memory.
f84355
@@ -326,6 +509,10 @@ allocate_argument(struct fetch_context *ctx, struct process *proc,
f84355
 	size_t sz = type_sizeof(proc, valuep->type);
f84355
 	if (sz == (size_t)-1)
f84355
 		return -1;
f84355
+
f84355
+	if (ctx->ret_struct)
f84355
+		ctx->struct_size += sz;
f84355
+
f84355
 	size_t slots = (sz + width - 1) / width;  /* Round up.  */
f84355
 	unsigned char *buf = value_reserve(valuep, slots * width);
f84355
 	if (buf == NULL)
f84355
@@ -346,9 +533,11 @@ allocate_argument(struct fetch_context *ctx, struct process *proc,
f84355
 		struct arg_type_info *fp_info
f84355
 			= type_get_fp_equivalent(valuep->type);
f84355
 		if (fp_info != NULL)
f84355
-			rc = allocate_float(ctx, proc, fp_info, &val;;
f84355
+			rc = allocate_float(ctx, proc, fp_info, &val,
f84355
+					8-type_sizeof(proc,info), false);
f84355
 		else
f84355
-			rc = allocate_gpr(ctx, proc, long_info, &val;;
f84355
+			rc = allocate_gpr(ctx, proc, long_info, &val,
f84355
+					0, false);
f84355
 
f84355
 		if (rc >= 0) {
f84355
 			memcpy(ptr, value_get_data(&val, NULL), width);
f84355
@@ -363,6 +552,7 @@ allocate_argument(struct fetch_context *ctx, struct process *proc,
f84355
 			return rc;
f84355
 	}
f84355
 
f84355
+#ifndef __LITTLE_ENDIAN__
f84355
 	/* Small values need post-processing.  */
f84355
 	if (sz < width) {
f84355
 		switch (info->type) {
f84355
@@ -394,6 +584,7 @@ allocate_argument(struct fetch_context *ctx, struct process *proc,
f84355
 			break;
f84355
 		}
f84355
 	}
f84355
+#endif
f84355
 
f84355
 	return 0;
f84355
 }
f84355
@@ -411,7 +602,22 @@ arch_fetch_retval(struct fetch_context *ctx, enum tof type,
f84355
 		  struct process *proc, struct arg_type_info *info,
f84355
 		  struct value *valuep)
f84355
 {
f84355
+	if (fetch_context_init(proc, ctx) < 0)
f84355
+		return -1;
f84355
+
f84355
+#if _CALL_ELF == 2
f84355
+	void *ptr = (void *)(ctx->regs.r64[1]+32);
f84355
+	uint64_t val = ptrace(PTRACE_PEEKTEXT, proc->pid, ptr, 0);
f84355
+
f84355
+	if (ctx->ret_struct
f84355
+	   && ((ctx->struct_size > 64
f84355
+	      || ctx->struct_hfa_count > 8
f84355
+	      || (ctx->struct_hfa_size == 0 && ctx->struct_size > 56)
f84355
+	      || (ctx->regs.r64[3] == ctx->regs.r64[1]+32)
f84355
+	      || (ctx->regs.r64[3] == val )))) {
f84355
+#else
f84355
 	if (ctx->ret_struct) {
f84355
+#endif
f84355
 		assert(info->type == ARGTYPE_STRUCT);
f84355
 
f84355
 		uint64_t addr = read_gpr(ctx, proc, 3);
f84355
@@ -424,8 +630,6 @@ arch_fetch_retval(struct fetch_context *ctx, enum tof type,
f84355
 		return 0;
f84355
 	}
f84355
 
f84355
-	if (fetch_context_init(proc, ctx) < 0)
f84355
-		return -1;
f84355
 	return allocate_argument(ctx, proc, info, valuep);
f84355
 }
f84355
 
f84355
diff --git a/sysdeps/linux-gnu/ppc/plt.c b/sysdeps/linux-gnu/ppc/plt.c
f84355
index 332daa8..45ed7fb 100644
f84355
--- a/sysdeps/linux-gnu/ppc/plt.c
f84355
+++ b/sysdeps/linux-gnu/ppc/plt.c
f84355
@@ -136,7 +136,11 @@
f84355
  */
f84355
 
f84355
 #define PPC_PLT_STUB_SIZE 16
f84355
-#define PPC64_PLT_STUB_SIZE 8 //xxx
f84355
+#if _CALL_ELF != 2
f84355
+#define PPC64_PLT_STUB_SIZE 8
f84355
+#else
f84355
+#define PPC64_PLT_STUB_SIZE 4
f84355
+#endif
f84355
 
f84355
 static inline int
f84355
 host_powerpc64()
f84355
@@ -186,8 +190,13 @@ ppc32_delayed_symbol(struct library_symbol *libsym)
f84355
 	if ((insn1 & BRANCH_MASK) == B_INSN
f84355
 	    || ((insn2 & BRANCH_MASK) == B_INSN
f84355
 		/* XXX double cast  */
f84355
+#ifdef __LITTLE_ENDIAN__
f84355
+		&& (ppc_branch_dest(libsym->enter_addr + 4, insn1)
f84355
+		    == (arch_addr_t) (long) libsym->lib->arch.pltgot_addr)))
f84355
+#else
f84355
 		&& (ppc_branch_dest(libsym->enter_addr + 4, insn2)
f84355
 		    == (arch_addr_t) (long) libsym->lib->arch.pltgot_addr)))
f84355
+#endif
f84355
 	{
f84355
 		mark_as_resolved(libsym, libsym->arch.resolved_value);
f84355
 	}
f84355
@@ -206,7 +215,7 @@ arch_dynlink_done(struct process *proc)
f84355
 				"couldn't read PLT value for %s(%p): %s\n",
f84355
 				libsym->name, libsym->enter_addr,
f84355
 				strerror(errno));
f84355
-			return;
f84355
+				return;
f84355
 		}
f84355
 
f84355
 		if (proc->e_machine == EM_PPC)
f84355
@@ -227,8 +236,14 @@ reloc_is_irelative(int machine, GElf_Rela *rela)
f84355
 {
f84355
 	bool irelative = false;
f84355
 	if (machine == EM_PPC64) {
f84355
-#ifdef R_PPC64_JMP_IREL
f84355
+#ifdef __LITTLE_ENDIAN__
f84355
+# ifdef R_PPC64_IRELATIVE
f84355
+		irelative = GELF_R_TYPE(rela->r_info) == R_PPC64_IRELATIVE;
f84355
+# endif
f84355
+#else
f84355
+# ifdef R_PPC64_JMP_IREL
f84355
 		irelative = GELF_R_TYPE(rela->r_info) == R_PPC64_JMP_IREL;
f84355
+# endif
f84355
 #endif
f84355
 	} else {
f84355
 		assert(machine == EM_PPC);
f84355
@@ -285,6 +300,7 @@ arch_translate_address_dyn(struct process *proc,
f84355
 			   arch_addr_t addr, arch_addr_t *ret)
f84355
 {
f84355
 	if (proc->e_machine == EM_PPC64) {
f84355
+#if _CALL_ELF != 2
f84355
 		uint64_t value;
f84355
 		if (proc_read_64(proc, addr, &value) < 0) {
f84355
 			fprintf(stderr,
f84355
@@ -296,6 +312,7 @@ arch_translate_address_dyn(struct process *proc,
f84355
 		 * arch_addr_t becomes integral type.  */
f84355
 		*ret = (arch_addr_t)(uintptr_t)value;
f84355
 		return 0;
f84355
+#endif
f84355
 	}
f84355
 
f84355
 	*ret = addr;
f84355
@@ -306,7 +323,8 @@ int
f84355
 arch_translate_address(struct ltelf *lte,
f84355
 		       arch_addr_t addr, arch_addr_t *ret)
f84355
 {
f84355
-	if (lte->ehdr.e_machine == EM_PPC64) {
f84355
+	if (lte->ehdr.e_machine == EM_PPC64
f84355
+	    && !lte->arch.elfv2_abi) {
f84355
 		/* XXX The double cast should be removed when
f84355
 		 * arch_addr_t becomes integral type.  */
f84355
 		GElf_Xword offset
f84355
@@ -430,7 +448,16 @@ reloc_copy_if_irelative(GElf_Rela *rela, void *data)
f84355
 int
f84355
 arch_elf_init(struct ltelf *lte, struct library *lib)
f84355
 {
f84355
+
f84355
+	/* Check for ABIv2 in ELF header processor specific flag.  */
f84355
+#ifndef EF_PPC64_ABI
f84355
+	assert (! (lte->ehdr.e_flags & 3 ) == 2)
f84355
+#else
f84355
+	lte->arch.elfv2_abi=((lte->ehdr.e_flags & EF_PPC64_ABI) == 2) ;
f84355
+#endif
f84355
+
f84355
 	if (lte->ehdr.e_machine == EM_PPC64
f84355
+	    && !lte->arch.elfv2_abi
f84355
 	    && load_opd_data(lte, lib) < 0)
f84355
 		return -1;
f84355
 
f84355
@@ -599,7 +626,7 @@ read_plt_slot_value(struct process *proc, GElf_Addr addr, GElf_Addr *valp)
f84355
 	uint64_t l;
f84355
 	/* XXX double cast.  */
f84355
 	if (proc_read_64(proc, (arch_addr_t)(uintptr_t)addr, &l) < 0) {
f84355
-		fprintf(stderr, "ptrace .plt slot value @%#" PRIx64": %s\n",
f84355
+		debug(DEBUG_EVENT, "ptrace .plt slot value @%#" PRIx64": %s",
f84355
 			addr, strerror(errno));
f84355
 		return -1;
f84355
 	}
f84355
@@ -616,7 +643,7 @@ unresolve_plt_slot(struct process *proc, GElf_Addr addr, GElf_Addr value)
f84355
 	 * pointers intact.  Hence the only adjustment that we need to
f84355
 	 * do is to IP.  */
f84355
 	if (ptrace(PTRACE_POKETEXT, proc->pid, addr, value) < 0) {
f84355
-		fprintf(stderr, "failed to unresolve .plt slot: %s\n",
f84355
+		debug(DEBUG_EVENT, "failed to unresolve .plt slot: %s",
f84355
 			strerror(errno));
f84355
 		return -1;
f84355
 	}
f84355
@@ -629,9 +656,48 @@ arch_elf_add_func_entry(struct process *proc, struct ltelf *lte,
f84355
 			arch_addr_t addr, const char *name,
f84355
 			struct library_symbol **ret)
f84355
 {
f84355
-	if (lte->ehdr.e_machine != EM_PPC || lte->ehdr.e_type == ET_DYN)
f84355
+#ifndef PPC64_LOCAL_ENTRY_OFFSET
f84355
+	assert(! lte->arch.elfv2_abi);
f84355
+#else
f84355
+	/* With ABIv2 st_other field contains an offset.  */
f84355
+	 if (lte->arch.elfv2_abi)
f84355
+		addr += PPC64_LOCAL_ENTRY_OFFSET(sym->st_other);
f84355
+#endif
f84355
+
f84355
+	int st_info = GELF_ST_TYPE(sym->st_info);
f84355
+
f84355
+	if ((lte->ehdr.e_machine != EM_PPC && sym->st_other == 0)
f84355
+	    || lte->ehdr.e_type == ET_DYN
f84355
+	    || (st_info == STT_FUNC && ! sym->st_other))
f84355
 		return PLT_DEFAULT;
f84355
 
f84355
+	if (st_info == STT_FUNC) {
f84355
+		/* Put the default symbol to the chain.
f84355
+		 * The addr has already been updated with
f84355
+		 * symbol offset  */
f84355
+		char *full_name = strdup(name);
f84355
+		if (full_name == NULL) {
f84355
+			fprintf(stderr, "couldn't copy name of %s: %s\n",
f84355
+			name, strerror(errno));
f84355
+			free(full_name);
f84355
+			return PLT_FAIL;
f84355
+		}
f84355
+		struct library_symbol *libsym = malloc(sizeof *libsym);
f84355
+		if (libsym == NULL
f84355
+		    || library_symbol_init(libsym, addr, full_name, 1,
f84355
+					   LS_TOPLT_NONE) < 0) {
f84355
+			free(libsym);
f84355
+			delete_symbol_chain(libsym);
f84355
+			libsym = NULL;
f84355
+			fprintf(stderr, "Couldn't add symbol %s"
f84355
+				"for tracing.\n", name);
f84355
+		}
f84355
+		full_name = NULL;
f84355
+		libsym->next = *ret;
f84355
+		*ret = libsym;
f84355
+		return PLT_OK;
f84355
+	}
f84355
+
f84355
 	bool ifunc = false;
f84355
 #ifdef STT_GNU_IFUNC
f84355
 	ifunc = GELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC;
f84355
@@ -761,9 +827,15 @@ arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
f84355
 	assert(plt_slot_addr >= lte->plt_addr
f84355
 	       || plt_slot_addr < lte->plt_addr + lte->plt_size);
f84355
 
f84355
+	/* Should avoid to do read if dynamic linker hasn't run yet
f84355
+	 * or allow -1 a valid return code.  */
f84355
 	GElf_Addr plt_slot_value;
f84355
-	if (read_plt_slot_value(proc, plt_slot_addr, &plt_slot_value) < 0)
f84355
-		goto fail;
f84355
+	if (read_plt_slot_value(proc, plt_slot_addr, &plt_slot_value) < 0) {
f84355
+		if (!lte->arch.elfv2_abi)
f84355
+			goto fail;
f84355
+		else
f84355
+			return PPC_PLT_UNRESOLVED;
f84355
+	}
f84355
 
f84355
 	struct library_symbol *libsym = malloc(sizeof(*libsym));
f84355
 	if (libsym == NULL) {
f84355
@@ -997,8 +1069,12 @@ ppc_plt_bp_continue(struct breakpoint *bp, struct process *proc)
f84355
 			return;
f84355
 		}
f84355
 
f84355
+#if _CALL_ELF == 2
f84355
+		continue_after_breakpoint(proc, bp);
f84355
+#else
f84355
 		jump_to_entry_point(proc, bp);
f84355
 		continue_process(proc->pid);
f84355
+#endif
f84355
 		return;
f84355
 
f84355
 	case PPC64_PLT_STUB:
f84355
@@ -1123,7 +1199,11 @@ arch_library_symbol_init(struct library_symbol *libsym)
f84355
 	/* We set type explicitly in the code above, where we have the
f84355
 	 * necessary context.  This is for calls from ltrace-elf.c and
f84355
 	 * such.  */
f84355
+#if _CALL_ELF == 2
f84355
+	libsym->arch.type = PPC_PLT_UNRESOLVED;
f84355
+#else
f84355
 	libsym->arch.type = PPC_DEFAULT;
f84355
+#endif
f84355
 	return 0;
f84355
 }
f84355
 
f84355
diff --git a/sysdeps/linux-gnu/ppc/trace.c b/sysdeps/linux-gnu/ppc/trace.c
f84355
index ee9a6b5..5aab538 100644
f84355
--- a/sysdeps/linux-gnu/ppc/trace.c
f84355
+++ b/sysdeps/linux-gnu/ppc/trace.c
f84355
@@ -65,9 +65,15 @@ syscall_p(struct process *proc, int status, int *sysnum)
f84355
 	if (WIFSTOPPED(status)
f84355
 	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
f84355
 		long pc = (long)get_instruction_pointer(proc);
f84355
+#ifndef __LITTLE_ENDIAN__
f84355
 		int insn =
f84355
 		    (int)ptrace(PTRACE_PEEKTEXT, proc->pid, pc - sizeof(long),
f84355
 				0);
f84355
+#else
f84355
+		int insn =
f84355
+		    (int)ptrace(PTRACE_PEEKTEXT, proc->pid, pc - sizeof(int),
f84355
+				0);
f84355
+#endif
f84355
 
f84355
 		if (insn == SYSCALL_INSN) {
f84355
 			*sysnum =
f84355
diff -up ltrace-0.7.91/sysdeps/linux-gnu/ppc/trace.c\~ ltrace-0.7.91/sysdeps/linux-gnu/ppc/trace.c
f84355
--- ltrace-0.7.91/sysdeps/linux-gnu/ppc/trace.c~	2014-08-08 14:05:58.000000000 +0200
f84355
+++ ltrace-0.7.91/sysdeps/linux-gnu/ppc/trace.c	2014-08-08 14:07:55.000000000 +0200
f84355
@@ -133,7 +133,11 @@ arch_sw_singlestep(struct process *proc,
f84355
 			return SWS_FAIL;
f84355
 		uint32_t insn;
f84355
 #ifdef __powerpc64__
f84355
+# ifdef __LITTLE_ENDIAN__
f84355
+		insn = (uint32_t) l;
f84355
+# else
f84355
 		insn = l >> 32;
f84355
+# endif
f84355
 #else
f84355
 		insn = l;
f84355
 #endif
f84355
diff -up ltrace-0.7.91/configure\~ ltrace-0.7.91/configure
f84355
--- ltrace-0.7.91/configure~	2014-08-08 14:09:12.000000000 +0200
f84355
+++ ltrace-0.7.91/configure	2014-08-08 14:18:30.000000000 +0200
f84355
@@ -2555,7 +2555,7 @@ case "${host_cpu}" in
f84355
     arm*|sa110)		HOST_CPU="arm" ;;
f84355
     cris*)		HOST_CPU="cris" ;;
f84355
     mips*)		HOST_CPU="mips" ;;
f84355
-    powerpc|powerpc64)	HOST_CPU="ppc" ;;
f84355
+    powerpc|powerpc64|powerpc64le)	HOST_CPU="ppc" ;;
f84355
     sun4u|sparc64)	HOST_CPU="sparc" ;;
f84355
     s390x)		HOST_CPU="s390" ;;
f84355
     i?86|x86_64)	HOST_CPU="x86" ;;
f84355
@@ -12094,7 +12094,7 @@ if test x"$enable_libunwind" = xyes; the
f84355
       arm*|sa110)         UNWIND_ARCH="arm" ;;
f84355
       i?86)               UNWIND_ARCH="x86" ;;
f84355
       powerpc)            UNWIND_ARCH="ppc32" ;;
f84355
-      powerpc64)          UNWIND_ARCH="ppc64" ;;
f84355
+      powerpc64|powerpc64le)          UNWIND_ARCH="ppc64" ;;
f84355
       mips*)              UNWIND_ARCH="mips" ;;
f84355
       *)                  UNWIND_ARCH="${host_cpu}" ;;
f84355
   esac