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