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

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