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

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