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

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