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

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