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

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