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

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