Blame SOURCES/ltrace-0.7.91-x86-plt_map.patch

0678d0
From fba95ad936f1d8c1052259bae811f1fc07f9a215 Mon Sep 17 00:00:00 2001
0678d0
From: Petr Machata <pmachata@redhat.com>
0678d0
Date: Thu, 30 Oct 2014 01:48:17 +0100
0678d0
Subject: [PATCH] Initialize the PLT slot map correctly on x86 and x86_64
0678d0
0678d0
The PLT slot map translates relocation numbers to PLT slot numbers,
0678d0
but was actually initialized in the opposite direction.  Fix the way
0678d0
it's initialized.  This bug can be seen on glibc in particular:
0678d0
0678d0
  $ ltrace -e free ls
0678d0
  libc.so.6->free(0x5)           = <void>
0678d0
  libc.so.6->free(0x78)          = <void>
0678d0
  libc.so.6->free(0xc)           = <void>
0678d0
  libc.so.6->free(0x308)         = <void>
0678d0
0678d0
Note the nonsense values passed to free.  The problem is that these
0678d0
are not free calls at all, but malloc calls that are assigned to wrong
0678d0
PLT slots due to above bug.
0678d0
---
0678d0
 sysdeps/linux-gnu/x86/plt.c | 38 +++++++++++++++++++++-----------------
0678d0
 1 file changed, 21 insertions(+), 17 deletions(-)
0678d0
0678d0
diff --git a/sysdeps/linux-gnu/x86/plt.c b/sysdeps/linux-gnu/x86/plt.c
0678d0
index c860af6..97f6c3e 100644
0678d0
--- a/sysdeps/linux-gnu/x86/plt.c
0678d0
+++ b/sysdeps/linux-gnu/x86/plt.c
0678d0
@@ -77,6 +77,18 @@ arch_elf_init(struct ltelf *lte, struct library *lib)
0678d0
 {
0678d0
 	VECT_INIT(&lte->arch.plt_map, unsigned int);
0678d0
 
0678d0
+	if (vect_reserve(&lte->arch.plt_map, vect_size(&lte->plt_relocs)) < 0) {
0678d0
+	fail:
0678d0
+		arch_elf_destroy(lte);
0678d0
+		return -1;
0678d0
+	}
0678d0
+
0678d0
+	{
0678d0
+		unsigned int i, sz = vect_size(&lte->plt_relocs);
0678d0
+		for (i = 0; i < sz; ++i)
0678d0
+			vect_pushback (&lte->arch.plt_map, &i);
0678d0
+	}
0678d0
+
0678d0
 	/* IRELATIVE slots may make the whole situation a fair deal
0678d0
 	 * more complex.  On x86{,_64}, the PLT slots are not
0678d0
 	 * presented in the order of the corresponding relocations,
0678d0
@@ -114,43 +126,35 @@ arch_elf_init(struct ltelf *lte, struct library *lib)
0678d0
 	/* Here we scan the PLT table and initialize a map of
0678d0
 	 * relocation->slot number in lte->arch.plt_map.  */
0678d0
 
0678d0
-	size_t i;
0678d0
-	for (i = 0; i < vect_size(&lte->plt_relocs); ++i) {
0678d0
+	unsigned int i, sz = vect_size(&lte->plt_relocs);
0678d0
+	for (i = 0; i < sz; ++i) {
0678d0
 
0678d0
 		GElf_Addr offset = x86_plt_offset(i);
0678d0
-		uint32_t reloc_arg = 0;
0678d0
 
0678d0
 		uint8_t byte;
0678d0
 		if (elf_read_next_u8(lte->plt_data, &offset, &byte) < 0
0678d0
 		    || byte != 0xff
0678d0
 		    || elf_read_next_u8(lte->plt_data, &offset, &byte) < 0
0678d0
 		    || (byte != 0xa3 && byte != 0x25))
0678d0
-			goto next;
0678d0
+			continue;
0678d0
 
0678d0
 		/* Skip immediate argument in the instruction.  */
0678d0
 		offset += 4;
0678d0
 
0678d0
+		uint32_t reloc_arg;
0678d0
 		if (elf_read_next_u8(lte->plt_data, &offset, &byte) < 0
0678d0
 		    || byte != 0x68
0678d0
 		    || elf_read_next_u32(lte->plt_data,
0678d0
-					 &offset, &reloc_arg) < 0) {
0678d0
-			reloc_arg = 0;
0678d0
-			goto next;
0678d0
-		}
0678d0
+					 &offset, &reloc_arg) < 0)
0678d0
+			continue;
0678d0
 
0678d0
 		if (lte->ehdr.e_machine == EM_386) {
0678d0
-			if (reloc_arg % 8 != 0) {
0678d0
-				reloc_arg = 0;
0678d0
-				goto next;
0678d0
-			}
0678d0
+			if (reloc_arg % 8 != 0)
0678d0
+				continue;
0678d0
 			reloc_arg /= 8;
0678d0
 		}
0678d0
 
0678d0
-	next:
0678d0
-		if (VECT_PUSHBACK(&lte->arch.plt_map, &reloc_arg) < 0) {
0678d0
-			arch_elf_destroy(lte);
0678d0
-			return -1;
0678d0
-		}
0678d0
+		*VECT_ELEMENT(&lte->arch.plt_map, unsigned int, reloc_arg) = i;
0678d0
 	}
0678d0
 
0678d0
 	return 0;
0678d0
-- 
0678d0
2.1.0
0678d0