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