nrelease - fix/improve livecd
[dragonfly.git] / libexec / rtld-elf / x86_64 / rtld_start.S
1 /*-
2  * Copyright 1996-1998 John D. Polstra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27
28 /*
29  * Dynamic program start.  argv pointer is in %rdi.
30  */
31         .text
32         .align  4
33         .globl  resident_start
34         .globl  .rtld_start
35         .type   .rtld_start,@function
36 resident_start:
37 .rtld_start:
38         xorq    %rbp,%rbp               # Clear frame pointer for good form
39         subq    $24,%rsp                # A place to store exit procedure addr
40         movq    %rdi,%r12
41         movq    %rsp,%rsi               # save address of exit proc
42         movq    %rsp,%rdx               # construct address of obj_main
43         addq    $8,%rdx
44         call    _rtld@PLT               # Call rtld(sp); returns entry point
45         popq    %rsi                    # Get exit procedure address
46         movq    %r12,%rdi               # *ap
47 /*
48  * At this point, %rax contains the entry point of the main program, and
49  * %rdx contains a pointer to a termination function that should be
50  * registered with atexit().  (crt1.o registers it.)
51  */
52 .globl .rtld_goto_main
53 .rtld_goto_main:        # This symbol exists just to make debugging easier.
54         jmp     *%rax                   # Enter main program
55
56
57 /*
58  * Binder entry point.  Control is transferred to here by code in the PLT.
59  * On entry, there are two arguments on the stack.  In ascending address
60  * order, they are (1) "obj", a pointer to the calling object's Obj_Entry,
61  * and (2) "reloff", the byte offset of the appropriate relocation entry
62  * in the PLT relocation table.
63  *
64  * We are careful to preserve all registers, even the caller-save
65  * registers.  That is because this code may be invoked by low-level
66  * assembly-language code that is not ABI-compliant.
67  *
68  * Stack map:
69  * reloff       0x60
70  * obj          0x58
71  * spare        0x50
72  * rflags       0x48
73  * rax          0x40
74  * rdx          0x38
75  * rcx          0x30
76  * rsi          0x28
77  * rdi          0x20
78  * r8           0x18
79  * r9           0x10
80  * r10          0x8
81  * r11          0x0
82  */
83         .align  4
84         .globl  _rtld_bind_start
85         .type   _rtld_bind_start,@function
86 _rtld_bind_start:
87         subq    $8,%rsp
88         pushfq                          # Save rflags
89         pushq   %rax                    # Save %rax
90         pushq   %rdx                    # Save %rdx
91         pushq   %rcx                    # Save %rcx
92         pushq   %rsi                    # Save %rsi
93         pushq   %rdi                    # Save %rdi
94         pushq   %r8                     # Save %r8
95         pushq   %r9                     # Save %r9
96         pushq   %r10                    # Save %r10
97         pushq   %r11                    # Save %r11
98         subq    $128,%rsp
99         movups  %xmm0,0(%rsp)           # Save xmm regs that might be
100         movups  %xmm1,16(%rsp)          # used for scratch.  clang may
101         movups  %xmm2,32(%rsp)          # use them for fast structural copies.
102         movups  %xmm3,48(%rsp)
103         movups  %xmm4,64(%rsp)
104         movups  %xmm5,80(%rsp)
105         movups  %xmm6,96(%rsp)
106         movups  %xmm7,112(%rsp)
107
108         movq    128+0x58(%rsp),%rdi     # Fetch obj argument    (arg 1)
109         movq    128+0x60(%rsp),%rsi     # Fetch reloff argument (arg 2)
110         leaq    128+0x68(%rsp),%rdx     # Fetch original stack pointer (arg 3)
111         leaq    (%rsi,%rsi,2),%rsi      # multiply by 3
112         leaq    (,%rsi,8),%rsi          # now 8, for 24 (sizeof Elf_Rela)
113
114         call    _rtld_bind@PLT          # Transfer control to the binder
115         /* Now %rax contains the entry point of the function being called. */
116
117         movq    %rax,128+0x60(%rsp)     # Store target over reloff argument
118
119         movups  0(%rsp),%xmm0
120         movups  16(%rsp),%xmm1
121         movups  32(%rsp),%xmm2
122         movups  48(%rsp),%xmm3
123         movups  64(%rsp),%xmm4
124         movups  80(%rsp),%xmm5
125         movups  96(%rsp),%xmm6
126         movups  112(%rsp),%xmm7
127         leaq    128(%rsp),%rsp
128
129         popq    %r11                    # Restore %r11
130         popq    %r10                    # Restore %r10
131         popq    %r9                     # Restore %r9
132         popq    %r8                     # Restore %r8
133         popq    %rdi                    # Restore %rdi
134         popq    %rsi                    # Restore %rsi
135         popq    %rcx                    # Restore %rcx
136         popq    %rdx                    # Restore %rdx
137         popq    %rax                    # Restore %rax
138         popfq                           # Restore rflags
139         leaq    16(%rsp),%rsp           # Discard spare, obj,
140                                         #  do not change rflags
141         ret                             # "Return" to target address
142
143         .section .note.GNU-stack,"",%progbits