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

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