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

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