瀏覽代碼

import start of ldso based on glibc

Mike Frysinger 19 年之前
父節點
當前提交
000311a8dd
共有 3 個文件被更改,包括 194 次插入0 次删除
  1. 69 0
      ldso/ldso/x86_64/dl-startup.h
  2. 5 0
      ldso/ldso/x86_64/dl-syscalls.h
  3. 120 0
      ldso/ldso/x86_64/dl-sysdep.h

+ 69 - 0
ldso/ldso/x86_64/dl-startup.h

@@ -0,0 +1,69 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Architecture specific code used by dl-startup.c
+ * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org>
+ */
+asm(
+	"	.text\n"
+	"	.align 16\n"
+	"	.global _start\n"
+	"	.type   _start,%function\n"
+	"_start:\n"
+	"	movq %rsp, %rdi\n"
+	"	call _dl_start\n"
+	"	# Save the user entry point address in %r12.\n"
+	"	movq %rax, %r12\n"
+	"	# See if we were run as a command with the executable file\n"
+	"	# name as an extra leading argument.\n"
+	"	movl _dl_skip_args(%rip), %eax\n"
+	"	# Pop the original argument count.\n"
+	"	popq %rdx\n"
+	"	# Adjust the stack pointer to skip _dl_skip_args words.\n"
+	"	leaq (%rsp,%rax,8), %rsp\n"
+	"	# Subtract _dl_skip_args from argc.\n"
+	"	subl %eax, %edx\n"
+	"	# Push argc back on the stack.\n"
+	"	pushq %rdx\n"
+	"	# Pass our finalizer function to the user in %rdx, as per ELF ABI.\n"
+	"	leaq _dl_fini(%rip), %rdx\n"
+	"	# And make sure %rsp points to argc stored on the stack.\n"
+	"	movq %r13, %rsp\n"
+	"	# Jump to the user's entry point.\n"
+	"	jmp *%r12\n"
+	"	.size	_start,.-_start\n"
+	"	.previous\n"
+);
+
+/* Get a pointer to the argv array.  On many platforms this can be just
+ * the address if the first argument, on other platforms we need to
+ * do something a little more subtle here.  */
+#define GET_ARGV(ARGVP, ARGS) ARGVP = (((unsigned long*) & ARGS)+1)
+
+/* Handle relocation of the symbols in the dynamic loader. */
+static __always_inline
+void PERFORM_BOOTSTRAP_RELOC(ELF_RELOC *rpnt, unsigned long *reloc_addr,
+	unsigned long symbol_addr, unsigned long load_addr, Elf64_Sym *sym)
+{
+	switch (ELF64_R_TYPE(rpnt->r_info)) {
+		case R_X86_64_GLOB_DAT:
+		case R_X86_64_JUMP_SLOT:
+			*reloc_addr = symbol_addr + rpnt->r_addend;
+			break;
+		case R_X86_64_DTPMOD64:
+			*reloc_addr = 1;
+			break;
+		case R_X86_64_NONE:
+		case R_X86_64_DTPOFF64:
+			break;
+		case R_X86_64_TPOFF64:
+			*reloc_addr = sym->st_value + rpnt->r_addend - symbol_addr;
+			break;
+		default:
+			_dl_exit(1);
+	}
+}
+
+/* Transfer control to the user's application, once the dynamic loader is
+ * done.  This routine has to exit the current function, then call the
+ * _dl_elf_main function.  */
+#define START() return _dl_elf_main

+ 5 - 0
ldso/ldso/x86_64/dl-syscalls.h

@@ -0,0 +1,5 @@
+/* We can't use the real errno in ldso, since it has not yet
+ * been dynamicly linked in yet. */
+extern int _dl_errno;
+#define __set_errno(X) {(_dl_errno) = (X);}
+#include "sys/syscall.h"

+ 120 - 0
ldso/ldso/x86_64/dl-sysdep.h

@@ -0,0 +1,120 @@
+/* vi: set sw=4 ts=4: */
+/* yoinked from glibc/sysdeps/x86_64/dl-machine.h */
+/* Machine-dependent ELF dynamic relocation inline functions.  x86-64 version.
+   Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Andreas Jaeger <aj@suse.de>.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+/* Define this if the system uses RELOCA.  */
+#define ELF_USES_RELOCA
+#include <elf.h>
+/* Initialization sequence for the GOT.  */
+#define INIT_GOT(GOT_BASE,MODULE)							\
+do {														\
+	GOT_BASE[2] = (unsigned long) _dl_linux_resolve;		\
+	GOT_BASE[1] = (unsigned long) MODULE;					\
+} while(0)
+
+/* Here we define the magic numbers that this dynamic loader should accept */
+#define MAGIC1 EM_X86_64
+#undef  MAGIC2
+
+/* Used for error messages */
+#define ELF_TARGET "x86_64"
+
+struct elf_resolve;
+extern unsigned long _dl_linux_resolver(struct elf_resolve * tpnt, int reloc_entry);
+
+#define do_rem(result, n, base) ((result) = (n) % (base))
+
+/* 4096 bytes alignment */
+#define PAGE_ALIGN 0xfffff000
+#define ADDR_ALIGN 0xfff
+#define OFFS_ALIGN 0x7ffff000
+
+/* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
+   TLS variable, so undefined references should not be allowed to
+   define the value.
+   ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
+   of the main executable's symbols, as for a COPY reloc.  */
+#define elf_machine_type_class(type) \
+  ((((type) == R_X86_64_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
+   | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
+
+/* Return the link-time address of _DYNAMIC.  Conveniently, this is the
+   first element of the GOT.  This must be inlined in a function which
+   uses global data.  */
+static __always_inline Elf64_Addr __attribute__ ((unused))
+elf_machine_dynamic (void)
+{
+  Elf64_Addr addr;
+
+#if 0
+  /* This works because we have our GOT address available in the small PIC
+     model.  */
+  addr = (Elf64_Addr) &_DYNAMIC;
+
+  return addr;
+#else
+  asm("mov 0(%%rip), %0"
+      : "=r" (addr));
+  return addr;
+#endif
+}
+
+
+/* Return the run-time load address of the shared object.  */
+static __always_inline Elf64_Addr __attribute__ ((unused))
+elf_machine_load_address (void)
+{
+  register Elf64_Addr addr, tmp;
+
+  /* The easy way is just the same as on x86:
+       leaq _dl_start, %0
+       leaq _dl_start(%%rip), %1
+       subq %0, %1
+     but this does not work with binutils since we then have
+     a R_X86_64_32S relocation in a shared lib.
+
+     Instead we store the address of _dl_start in the data section
+     and compare it with the current value that we can get via
+     an RIP relative addressing mode.  */
+
+  asm ("movq 1f(%%rip), %1\n"
+       "0:\tleaq _dl_start(%%rip), %0\n\t"
+       "subq %1, %0\n\t"
+       ".section\t.data\n"
+       "1:\t.quad _dl_start\n\t"
+       ".previous\n\t"
+       : "=r" (addr), "=r" (tmp) : : "cc");
+
+  return addr;
+}
+
+static __always_inline void
+elf_machine_relative (Elf64_Addr load_off, const Elf64_Addr rel_addr,
+                      Elf64_Word relative_count)
+{
+	Elf64_Rel * rpnt = (void *) rel_addr;
+	--rpnt;
+	do {
+		Elf64_Addr *const reloc_addr = (void *) (load_off + (++rpnt)->r_offset);
+
+		*reloc_addr += load_off;
+	} while (--relative_count);
+}