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

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