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

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