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

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