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

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