Blame SOURCES/ltrace-0.7.91-ppc64-unprelink.patch

f84355
From a0093ca43cf40d7e5f6cebeb64156062d2de46d9 Mon Sep 17 00:00:00 2001
f84355
From: Petr Machata <pmachata@redhat.com>
f84355
Date: Fri, 10 Jan 2014 20:06:51 +0100
f84355
Subject: [PATCH 2/2] Don't crash untraced calls via PLT in prelinked PPC64
f84355
 binaries
f84355
f84355
In prelinked binaries, ltrace has to unprelinks PLT slots in order to
f84355
catch calls done through PLT.  This makes the calls done through these
f84355
slots invalid, because the special first PLT slot is not initialized,
f84355
and dynamic linker SIGSEGVs because of this.  Ltrace relies on
f84355
arranging breakpoints such that the dynamic linker is not actually
f84355
entered, and moves PC around itself to simulate the effects of a call
f84355
through PLT.
f84355
f84355
Originally, arch_elf_add_plt_entry was called only for symbols that
f84355
were actually traced.  Later this was changed and it's now called for
f84355
all PLT entries, and the resulting candidate list is filtered
f84355
afterwards.  This gives backends a chance to rename the symbol, as is
f84355
useful with IRELATIVE PLT calls, where symbol name may not be
f84355
available at all.  But the PPC backend was never updated to reflect
f84355
this, and unresolved all symbols for which arch_elf_add_plt_entry was
f84355
called, thus rendering _all_ PLT slots invalid, even those that
f84355
weren't later procted by breakpoints.  Thus calls done through any
f84355
untraced slots failed.
f84355
f84355
This patch fixes this problem by deferring the unprelinking of PLT
f84355
slots into the on_install hook of breakpoints.
f84355
---
f84355
 sysdeps/linux-gnu/ppc/arch.h |   21 ++++++++-
f84355
 sysdeps/linux-gnu/ppc/plt.c  |   94 +++++++++++++++++++++++++++++++++--------
f84355
 2 files changed, 94 insertions(+), 21 deletions(-)
f84355
f84355
diff --git a/sysdeps/linux-gnu/ppc/arch.h b/sysdeps/linux-gnu/ppc/arch.h
f84355
index 2add3b8..bf9b5dc 100644
f84355
--- a/sysdeps/linux-gnu/ppc/arch.h
f84355
+++ b/sysdeps/linux-gnu/ppc/arch.h
f84355
@@ -1,6 +1,6 @@
f84355
 /*
f84355
  * This file is part of ltrace.
f84355
- * Copyright (C) 2012,2013 Petr Machata
f84355
+ * Copyright (C) 2012,2013,2014 Petr Machata
f84355
  * Copyright (C) 2006 Paul Gilliam
f84355
  * Copyright (C) 2002,2004 Juan Cespedes
f84355
  *
f84355
@@ -87,12 +87,29 @@ enum ppc64_plt_type {
f84355
 	/* Very similar to PPC_PLT_UNRESOLVED, but for JMP_IREL
f84355
 	 * slots.  */
f84355
 	PPC_PLT_IRELATIVE,
f84355
+
f84355
+	/* Transitional state before the breakpoint is enabled.  */
f84355
+	PPC_PLT_NEED_UNRESOLVE,
f84355
 };
f84355
 
f84355
 #define ARCH_HAVE_LIBRARY_SYMBOL_DATA
f84355
+struct ppc_unresolve_data;
f84355
 struct arch_library_symbol_data {
f84355
 	enum ppc64_plt_type type;
f84355
-	GElf_Addr resolved_value;
f84355
+
f84355
+	/* State		Contents
f84355
+	 *
f84355
+	 * PPC_DEFAULT		N/A
f84355
+	 * PPC64_PLT_STUB	N/A
f84355
+	 * PPC_PLT_UNRESOLVED	PLT entry address.
f84355
+	 * PPC_PLT_IRELATIVE	Likewise.
f84355
+	 * PPC_PLT_RESOLVED	The original value the slot was resolved to.
f84355
+	 * PPC_PLT_NEED_UNRESOLVE	DATA.
f84355
+	 */
f84355
+	union {
f84355
+		GElf_Addr resolved_value;
f84355
+		struct ppc_unresolve_data *data;
f84355
+	};
f84355
 
f84355
 	/* Address of corresponding slot in .plt.  */
f84355
 	GElf_Addr plt_slot_addr;
f84355
diff --git a/sysdeps/linux-gnu/ppc/plt.c b/sysdeps/linux-gnu/ppc/plt.c
f84355
index 8715da6..332daa8 100644
f84355
--- a/sysdeps/linux-gnu/ppc/plt.c
f84355
+++ b/sysdeps/linux-gnu/ppc/plt.c
f84355
@@ -679,6 +679,14 @@ arch_elf_add_func_entry(struct process *proc, struct ltelf *lte,
f84355
 	return PLT_OK;
f84355
 }
f84355
 
f84355
+struct ppc_unresolve_data {
f84355
+	struct ppc_unresolve_data *self; /* A canary.  */
f84355
+	GElf_Addr plt_entry_addr;
f84355
+	GElf_Addr plt_slot_addr;
f84355
+	GElf_Addr plt_slot_value;
f84355
+	bool is_irelative;
f84355
+};
f84355
+
f84355
 enum plt_status
f84355
 arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
f84355
 		       const char *a_name, GElf_Rela *rela, size_t ndx,
f84355
@@ -778,28 +786,23 @@ arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
f84355
 	    && (plt_slot_value == plt_entry_addr || plt_slot_value == 0)) {
f84355
 		libsym->arch.type = PPC_PLT_UNRESOLVED;
f84355
 		libsym->arch.resolved_value = plt_entry_addr;
f84355
-
f84355
 	} else {
f84355
-		/* Unresolve the .plt slot.  If the binary was
f84355
-		 * prelinked, this makes the code invalid, because in
f84355
-		 * case of prelinked binary, the dynamic linker
f84355
-		 * doesn't update .plt[0] and .plt[1] with addresses
f84355
-		 * of the resover.  But we don't care, we will never
f84355
-		 * need to enter the resolver.  That just means that
f84355
-		 * we have to un-un-resolve this back before we
f84355
-		 * detach.  */
f84355
-
f84355
-		if (unresolve_plt_slot(proc, plt_slot_addr, plt_entry_addr) < 0) {
f84355
-			library_symbol_destroy(libsym);
f84355
+		/* Mark the symbol for later unresolving.  We may not
f84355
+		 * do this right away, as this is called by ltrace
f84355
+		 * core for all symbols, and only later filtered.  We
f84355
+		 * only unresolve the symbol before the breakpoint is
f84355
+		 * enabled.  */
f84355
+
f84355
+		libsym->arch.type = PPC_PLT_NEED_UNRESOLVE;
f84355
+		libsym->arch.data = malloc(sizeof *libsym->arch.data);
f84355
+		if (libsym->arch.data == NULL)
f84355
 			goto fail2;
f84355
-		}
f84355
 
f84355
-		if (! is_irelative) {
f84355
-			mark_as_resolved(libsym, plt_slot_value);
f84355
-		} else {
f84355
-			libsym->arch.type = PPC_PLT_IRELATIVE;
f84355
-			libsym->arch.resolved_value = plt_entry_addr;
f84355
-		}
f84355
+		libsym->arch.data->self = libsym->arch.data;
f84355
+		libsym->arch.data->plt_entry_addr = plt_entry_addr;
f84355
+		libsym->arch.data->plt_slot_addr = plt_slot_addr;
f84355
+		libsym->arch.data->plt_slot_value = plt_slot_value;
f84355
+		libsym->arch.data->is_irelative = is_irelative;
f84355
 	}
f84355
 
f84355
 	*ret = libsym;
f84355
@@ -999,6 +1002,7 @@ ppc_plt_bp_continue(struct breakpoint *bp, struct process *proc)
f84355
 		return;
f84355
 
f84355
 	case PPC64_PLT_STUB:
f84355
+	case PPC_PLT_NEED_UNRESOLVE:
f84355
 		/* These should never hit here.  */
f84355
 		break;
f84355
 	}
f84355
@@ -1050,6 +1054,52 @@ ppc_plt_bp_retract(struct breakpoint *bp, struct process *proc)
f84355
 	}
f84355
 }
f84355
 
f84355
+static void
f84355
+ppc_plt_bp_install(struct breakpoint *bp, struct process *proc)
f84355
+{
f84355
+	/* This should not be an artificial breakpoint.  */
f84355
+	struct library_symbol *libsym = bp->libsym;
f84355
+	if (libsym == NULL)
f84355
+		libsym = bp->arch.irel_libsym;
f84355
+	assert(libsym != NULL);
f84355
+
f84355
+	if (libsym->arch.type == PPC_PLT_NEED_UNRESOLVE) {
f84355
+		/* Unresolve the .plt slot.  If the binary was
f84355
+		 * prelinked, this makes the code invalid, because in
f84355
+		 * case of prelinked binary, the dynamic linker
f84355
+		 * doesn't update .plt[0] and .plt[1] with addresses
f84355
+		 * of the resover.  But we don't care, we will never
f84355
+		 * need to enter the resolver.  That just means that
f84355
+		 * we have to un-un-resolve this back before we
f84355
+		 * detach.  */
f84355
+
f84355
+		struct ppc_unresolve_data *data = libsym->arch.data;
f84355
+		libsym->arch.data = NULL;
f84355
+		assert(data->self == data);
f84355
+
f84355
+		GElf_Addr plt_slot_addr = data->plt_slot_addr;
f84355
+		GElf_Addr plt_slot_value = data->plt_slot_value;
f84355
+		GElf_Addr plt_entry_addr = data->plt_entry_addr;
f84355
+
f84355
+		if (unresolve_plt_slot(proc, plt_slot_addr,
f84355
+				       plt_entry_addr) == 0) {
f84355
+			if (! data->is_irelative) {
f84355
+				mark_as_resolved(libsym, plt_slot_value);
f84355
+			} else {
f84355
+				libsym->arch.type = PPC_PLT_IRELATIVE;
f84355
+				libsym->arch.resolved_value = plt_entry_addr;
f84355
+			}
f84355
+		} else {
f84355
+			fprintf(stderr, "Couldn't unresolve %s@%p.  Not tracing"
f84355
+				" this symbol.\n",
f84355
+				breakpoint_name(bp), bp->addr);
f84355
+			proc_remove_breakpoint(proc, bp);
f84355
+		}
f84355
+
f84355
+		free(data);
f84355
+	}
f84355
+}
f84355
+
f84355
 int
f84355
 arch_library_init(struct library *lib)
f84355
 {
f84355
@@ -1080,6 +1130,11 @@ arch_library_symbol_init(struct library_symbol *libsym)
f84355
 void
f84355
 arch_library_symbol_destroy(struct library_symbol *libsym)
f84355
 {
f84355
+	if (libsym->arch.type == PPC_PLT_NEED_UNRESOLVE) {
f84355
+		assert(libsym->arch.data->self == libsym->arch.data);
f84355
+		free(libsym->arch.data);
f84355
+		libsym->arch.data = NULL;
f84355
+	}
f84355
 }
f84355
 
f84355
 int
f84355
@@ -1115,6 +1170,7 @@ arch_breakpoint_init(struct process *proc, struct breakpoint *bp)
f84355
 	static struct bp_callbacks cbs = {
f84355
 		.on_continue = ppc_plt_bp_continue,
f84355
 		.on_retract = ppc_plt_bp_retract,
f84355
+		.on_install = ppc_plt_bp_install,
f84355
 	};
f84355
 	breakpoint_set_callbacks(bp, &cbs);
f84355
 
f84355
-- 
f84355
1.7.6.5
f84355