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