1/* $OpenBSD: ldasm.S,v 1.2 2001/09/24 21:35:09 drahn Exp $ */ 2 3/* 4 * Copyright (c) 1999 Dale Rahn 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed under OpenBSD by 17 * Dale Rahn. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 */ 34 35/* 36 * #ifdef blrl_never_causes_illegal_access 37 * background for the else of this define 38 * On the machine this code is being written, on a large portion 39 * of the tims this code is executed the blrl instruct causes an 40 * illegal instruction, hopefully by touching the page with a load 41 * will prevent the execute access from faulting. 42 */ 43 44#define AUX_entry 9 45 46#include <machine/asm.h> 47 48ENTRY(_dl_start) 49 mr 19, 1 50 stwu 1, (-16 -((AUX_entry+3)*4))(1) # Some space. 51 52 mflr 27 /* save off old link register */ 53 stw 27, 4(19) /* save in normal location */ 54 55 # squirrel away the arguments for main 56 mr 20, 3 #argc 57 mr 21, 4 #argv 58 mr 22, 5 #envp 59 mr 23, 6 # ??? 60 61 .local .L___offset_sym 62 .type .L___offset_sym,@function 63 bl 1f 64.L___offset_sym: 65 # this instruction never gets executed but can be used 66 # to find the virtual address where the page is loaded. 67 bl _GLOBAL_OFFSET_TABLE_@local-4 68 bl _DYNAMIC@local 691: 70 mflr 5 # this stores where we are (+4) 71 lwz 18, 0(5) # load the instruction at offset_sym 72 # it contains an offset to the location 73 # of the GOT. 74 75 rlwinm 18,18,0,8,30 # mask off the offset portion of the instr. 76 77 /* 78 * these adds effectively calculates the value the 79 * bl _GLOBAL_OFFSET_TABLE_@local-4 80 * operation that would be below would calulate. 81 */ 82 add 28, 18, 5 83 84 li 0, 0 85 dcbf 5, 18 86 sync 87 isync 88 icbi 5, 18 # make certain that the got table addr is 89 # not in the icache 90 91 sync 92 isync 93 94 addi 28, 28, 4 95 mr 29, 28 96 97 lwz 30, 0(28) # this loads the first entry of the GOT 98 # thus forcing it to be paged in when 99 # we jump to it below. 100 101 102 # calculate where we want to be (via the got) 103 bl _GLOBAL_OFFSET_TABLE_@local-4 104 mflr 28 105 106 107 lwz 4, .L___offset_sym@got(28) 108 mr 6, 4 # make copy of register for debugging 109 110 /* This cheats and calculates the address of _DYNAMIC 111 * the same way that the GLOBAL_OFFSET_TABLE was calcuated 112 */ 113 lwz 18, 4(5) 114 rlwinm 18,18,0,8,30 # mask off the offset portion of the instr. 115 add 8, 18, 5 # address of _DYNAMIC (arg6 for _dl_boot) 116 addi 18, 8, 4 # correction. 117 118 sub 4, 5, 4 # calculate offset (arg1 for _dl_boot) 119 mr 17, 4 120 121 122 subi 3, 21, 4 # Get stack pointer (arg0 for _dl_boot). 123 mr 4, 17 # loff 124 mr 5, 18 # dynamicp 125 addi 6, 1, 8 # dl_data 126 127 bl _dl_boot_bind@local 128 129 mr 3, 21 # argv 130 mr 4, 22 # envp 131 mr 5, 17 # loff 132 mr 6, 18 # dynamicp 133 addi 7, 1, 8 # dl_data 134 135 bl _dl_boot@local 136 137 mtctr 3 # put return value into ctr to execute 138 139 # get back the squirreled away the arguments for main 140 mr 3, 20 141 mr 4, 21 142 mr 5, 22 143 mr 6, 23 144 145 146 mtlr 27 147 lwz 1, 0(1) # Restore stack pointer. 148 bctr # Go execute the 'real' program. 149 150 .globl _dl_rt_resolve 151 .data 152_dl_rt_resolve: 153 .long 0 154