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

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