installer: Finish incomplete commit to merge nrelease/{installer,root}.
[dragonfly.git] / libexec / rtld-elf / amd64 / 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: src/libexec/rtld-elf/amd64/rtld_start.S,v 1.5 2004/03/21 01:43:39 peter Exp $
26  * $DragonFly: src/libexec/rtld-elf/amd64/rtld_start.S,v 1.1 2006/07/27 00:40:35 corecode Exp $
27  */
28
29 /*
30  * Dynamic program start.  argv pointer is in %rdi.
31  */
32         .text
33         .align  4
34         .globl  resident_start
35         .globl  .rtld_start
36         .type   .rtld_start,@function
37 resident_start:
38 .rtld_start:
39         xorq    %rbp,%rbp               # Clear frame pointer for good form
40         subq    $24,%rsp                # A place to store exit procedure addr
41         movq    %rdi,%r12
42         movq    %rsp,%rsi               # save address of exit proc
43         movq    %rsp,%rdx               # construct address of obj_main
44         addq    $8,%rdx
45         call    _rtld@PLT               # Call rtld(sp); returns entry point
46         popq    %rsi                    # Get exit procedure address
47         movq    %r12,%rdi               # *ap
48 /*
49  * At this point, %rax contains the entry point of the main program, and
50  * %rdx contains a pointer to a termination function that should be
51  * registered with atexit().  (crt1.o registers it.)
52  */
53 .globl .rtld_goto_main
54 .rtld_goto_main:        # This symbol exists just to make debugging easier.
55         jmp     *%rax                   # Enter main program
56
57
58 /*
59  * Binder entry point.  Control is transferred to here by code in the PLT.
60  * On entry, there are two arguments on the stack.  In ascending address
61  * order, they are (1) "obj", a pointer to the calling object's Obj_Entry,
62  * and (2) "reloff", the byte offset of the appropriate relocation entry
63  * in the PLT relocation table.
64  *
65  * We are careful to preserve all registers, even the the caller-save
66  * registers.  That is because this code may be invoked by low-level
67  * assembly-language code that is not ABI-compliant.
68  *
69  * Stack map:
70  * reloff       0x60
71  * obj          0x58
72  * spare        0x50
73  * rflags       0x48
74  * rax          0x40
75  * rdx          0x38
76  * rcx          0x30
77  * rsi          0x28
78  * rdi          0x20
79  * r8           0x18
80  * r9           0x10
81  * r10          0x8
82  * r11          0x0
83  */
84         .align  4
85         .globl  _rtld_bind_start
86         .type   _rtld_bind_start,@function
87 _rtld_bind_start:
88         subq    $8,%rsp
89         pushfq                          # Save rflags
90         pushq   %rax                    # Save %rax
91         pushq   %rdx                    # Save %rdx
92         pushq   %rcx                    # Save %rcx
93         pushq   %rsi                    # Save %rsi
94         pushq   %rdi                    # Save %rdi
95         pushq   %r8                     # Save %r8
96         pushq   %r9                     # Save %r9
97         pushq   %r10                    # Save %r10
98         pushq   %r11                    # Save %r11
99         movq    0x58(%rsp),%rdi         # Fetch obj argument    (arg 1)
100         movq    0x60(%rsp),%rsi         # Fetch reloff argument (arg 2)
101         leaq    0x68(%rsp),%rdx         # Fetch original stack pointer (arg 3)
102         leaq    (%rsi,%rsi,2),%rsi      # multiply by 3
103         leaq    (,%rsi,8),%rsi          # now 8, for 24 (sizeof Elf_Rela)
104
105         call    _rtld_bind@PLT          # Transfer control to the binder
106         /* Now %rax contains the entry point of the function being called. */
107
108         movq    %rax,0x60(%rsp)         # Store target over reloff argument
109         popq    %r11                    # Restore %r11
110         popq    %r10                    # Restore %r10
111         popq    %r9                     # Restore %r9
112         popq    %r8                     # Restore %r8
113         popq    %rdi                    # Restore %rdi
114         popq    %rsi                    # Restore %rsi
115         popq    %rcx                    # Restore %rcx
116         popq    %rdx                    # Restore %rdx
117         popq    %rax                    # Restore %rax
118         popfq                           # Restore rflags
119         leaq    16(%rsp),%rsp           # Discard spare, obj, do not change rflags
120         ret                             # "Return" to target address