Blame SOURCES/ltrace-0.7.91-x86_64-irelative.patch

39d69a
@@ -, +, @@ 
39d69a
 relocation
39d69a
- In general they are.  But IRELATIVE relocations are sorted to come
39d69a
  last, and PLT entries are not sorted accordingly.
39d69a
---
39d69a
 sysdeps/linux-gnu/x86/arch.h |   11 +++++
39d69a
 sysdeps/linux-gnu/x86/plt.c  |  101 +++++++++++++++++++++++++++++++++++++++++-
39d69a
 2 files changed, 111 insertions(+), 1 deletions(-)
39d69a
--- a/sysdeps/linux-gnu/x86/arch.h	
39d69a
+++ a/sysdeps/linux-gnu/x86/arch.h	
39d69a
@@ -19,6 +19,10 @@ 
39d69a
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
39d69a
  * 02110-1301 USA
39d69a
  */
39d69a
+#ifndef LTRACE_X86_ARCH_H
39d69a
+#define LTRACE_X86_ARCH_H
39d69a
+
39d69a
+#include "vect.h"
39d69a
 
39d69a
 #define BREAKPOINT_VALUE {0xcc}
39d69a
 #define BREAKPOINT_LENGTH 1
39d69a
@@ -30,9 +34,16 @@ 
39d69a
 
39d69a
 #define ARCH_HAVE_ADD_PLT_ENTRY
39d69a
 
39d69a
+#define ARCH_HAVE_LTELF_DATA
39d69a
+struct arch_ltelf_data {
39d69a
+	struct vect plt_map;
39d69a
+};
39d69a
+
39d69a
 #ifdef __x86_64__
39d69a
 #define LT_ELFCLASS	ELFCLASS64
39d69a
 #define LT_ELF_MACHINE	EM_X86_64
39d69a
 #endif
39d69a
 #define LT_ELFCLASS2	ELFCLASS32
39d69a
 #define LT_ELF_MACHINE2	EM_386
39d69a
+
39d69a
+#endif /* LTRACE_X86_ARCH_H */
39d69a
--- a/sysdeps/linux-gnu/x86/plt.c	
39d69a
+++ a/sysdeps/linux-gnu/x86/plt.c	
39d69a
@@ -27,10 +27,19 @@ 
39d69a
 #include "library.h"
39d69a
 #include "trace.h"
39d69a
 
39d69a
+static GElf_Addr
39d69a
+x86_plt_offset(uint32_t i)
39d69a
+{
39d69a
+	/* Skip the first PLT entry, which contains a stub to call the
39d69a
+	 * resolver.  */
39d69a
+	return (i + 1) * 16;
39d69a
+}
39d69a
+
39d69a
 GElf_Addr
39d69a
 arch_plt_sym_val(struct ltelf *lte, size_t ndx, GElf_Rela *rela)
39d69a
 {
39d69a
-	return lte->plt_addr + (ndx + 1) * 16;
39d69a
+	uint32_t i = *VECT_ELEMENT(&lte->arch.plt_map, uint32_t, ndx);
39d69a
+	return x86_plt_offset(i) + lte->plt_addr;
39d69a
 }
39d69a
 
39d69a
 void *
39d69a
@@ -62,3 +71,93 @@ arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
39d69a
 
39d69a
 	return PLT_DEFAULT;
39d69a
 }
39d69a
+
39d69a
+int
39d69a
+arch_elf_init(struct ltelf *lte, struct library *lib)
39d69a
+{
39d69a
+	VECT_INIT(&lte->arch.plt_map, unsigned int);
39d69a
+
39d69a
+	/* IRELATIVE slots may make the whole situation a fair deal
39d69a
+	 * more complex.  On x86{,_64}, the PLT slots are not
39d69a
+	 * presented in the order of the corresponding relocations,
39d69a
+	 * but in the order it which these symbols are in the symbol
39d69a
+	 * table.  That's static symbol table, which may be stripped
39d69a
+	 * off, not dynsym--that doesn't contain IFUNC symbols at all.
39d69a
+	 * So we have to decode each PLT entry to figure out what
39d69a
+	 * entry it corresponds to.  We need to interpret the PLT
39d69a
+	 * table to figure this out.
39d69a
+	 *
39d69a
+	 * On i386, the PLT entry format is as follows:
39d69a
+	 *
39d69a
+	 *	8048300:   ff 25 0c a0 04 08       jmp    *0x804a00c
39d69a
+	 *	8048306:   68 20 00 00 00          push   $0x20
39d69a
+	 *	804830b:   e9 e0 ff ff ff          jmp    80482f0 <_init+0x30>
39d69a
+	 *
39d69a
+	 * For PIE binaries it is the following:
39d69a
+	 *
39d69a
+	 *	    410:   ff a3 10 00 00 00       jmp    *0x10(%ebx)
39d69a
+	 *	    416:   68 00 00 00 00          push   $0x0
39d69a
+	 *	    41b:   e9 d0 ff ff ff          jmp    3f0 <_init+0x30>
39d69a
+	 *
39d69a
+	 * On x86_64, it is:
39d69a
+	 *
39d69a
+	 *	 400420:   ff 25 f2 0b 20 00       jmpq   *0x200bf2(%rip)        # 601018 <_GLOBAL_OFFSET_TABLE_+0x18>
39d69a
+	 *	 400426:   68 00 00 00 00          pushq  $0x0
39d69a
+	 *	 40042b:   e9 e0 ff ff ff          jmpq   400410 <_init+0x18>
39d69a
+	 *
39d69a
+         * On i386, the argument to push is an offset of relocation to
39d69a
+	 * use.  The first PLT slot has an offset of 0x0, the second
39d69a
+	 * 0x8, etc.  On x86_64, it's directly the index that we are
39d69a
+	 * looking for.
39d69a
+	 */
39d69a
+
39d69a
+	/* Here we scan the PLT table and initialize a map of
39d69a
+	 * relocation->slot number in lte->arch.plt_map.  */
39d69a
+
39d69a
+	size_t i;
39d69a
+	for (i = 0; i < vect_size(&lte->plt_relocs); ++i) {
39d69a
+
39d69a
+		GElf_Addr offset = x86_plt_offset(i);
39d69a
+		uint32_t reloc_arg = 0;
39d69a
+
39d69a
+		uint8_t byte;
39d69a
+		if (elf_read_next_u8(lte->plt_data, &offset, &byte) < 0
39d69a
+		    || byte != 0xff
39d69a
+		    || elf_read_next_u8(lte->plt_data, &offset, &byte) < 0
39d69a
+		    || (byte != 0xa3 && byte != 0x25))
39d69a
+			goto next;
39d69a
+
39d69a
+		/* Skip immediate argument in the instruction.  */
39d69a
+		offset += 4;
39d69a
+
39d69a
+		if (elf_read_next_u8(lte->plt_data, &offset, &byte) < 0
39d69a
+		    || byte != 0x68
39d69a
+		    || elf_read_next_u32(lte->plt_data,
39d69a
+					 &offset, &reloc_arg) < 0) {
39d69a
+			reloc_arg = 0;
39d69a
+			goto next;
39d69a
+		}
39d69a
+
39d69a
+		if (lte->ehdr.e_machine == EM_386) {
39d69a
+			if (reloc_arg % 8 != 0) {
39d69a
+				reloc_arg = 0;
39d69a
+				goto next;
39d69a
+			}
39d69a
+			reloc_arg /= 8;
39d69a
+		}
39d69a
+
39d69a
+	next:
39d69a
+		if (VECT_PUSHBACK(&lte->arch.plt_map, &reloc_arg) < 0) {
39d69a
+			arch_elf_destroy(lte);
39d69a
+			return -1;
39d69a
+		}
39d69a
+	}
39d69a
+
39d69a
+	return 0;
39d69a
+}
39d69a
+
39d69a
+void
39d69a
+arch_elf_destroy(struct ltelf *lte)
39d69a
+{
39d69a
+	VECT_DESTROY(&lte->arch.plt_map, uint32_t, NULL, NULL);
39d69a
+}
39d69a
--