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

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