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