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

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