1*af856e3aSkettenis/* $OpenBSD: ldasm.S,v 1.3 2020/07/16 21:18:09 kettenis Exp $ */ 26a2e3db8Sdrahn 36a2e3db8Sdrahn/* 46a2e3db8Sdrahn * Copyright (c) 1999 Dale Rahn 56a2e3db8Sdrahn * 66a2e3db8Sdrahn * Redistribution and use in source and binary forms, with or without 76a2e3db8Sdrahn * modification, are permitted provided that the following conditions 86a2e3db8Sdrahn * are met: 96a2e3db8Sdrahn * 1. Redistributions of source code must retain the above copyright 106a2e3db8Sdrahn * notice, this list of conditions and the following disclaimer. 116a2e3db8Sdrahn * 2. Redistributions in binary form must reproduce the above copyright 126a2e3db8Sdrahn * notice, this list of conditions and the following disclaimer in the 136a2e3db8Sdrahn * documentation and/or other materials provided with the distribution. 146a2e3db8Sdrahn * 156a2e3db8Sdrahn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 166a2e3db8Sdrahn * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 176a2e3db8Sdrahn * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 186a2e3db8Sdrahn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 196a2e3db8Sdrahn * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 206a2e3db8Sdrahn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 216a2e3db8Sdrahn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 226a2e3db8Sdrahn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 236a2e3db8Sdrahn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 246a2e3db8Sdrahn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 256a2e3db8Sdrahn * SUCH DAMAGE. 266a2e3db8Sdrahn * 276a2e3db8Sdrahn */ 286a2e3db8Sdrahn 296a2e3db8Sdrahn#define AUX_entry 9 306a2e3db8Sdrahn 316a2e3db8Sdrahn#include <machine/asm.h> 326a2e3db8Sdrahn#include <sys/syscall.h> 336a2e3db8Sdrahn 346a2e3db8Sdrahn .section .boot.text,"ax",@progbits 356a2e3db8Sdrahn .align 2 366a2e3db8Sdrahn .globl _dl_start 376a2e3db8Sdrahn .type _dl_start,@function 386a2e3db8Sdrahn_dl_start: 396a2e3db8Sdrahn bl 1f 406a2e3db8Sdrahn1: 416a2e3db8Sdrahn mflr %r30 426a2e3db8Sdrahn addis %r2, %r30, .TOC.-1b@ha 436a2e3db8Sdrahn addi %r2, %r2, .TOC.-1b@l 446a2e3db8Sdrahn 4561dad853Sdrahn stdu %r1, (-64 -((AUX_entry+3)*8))(%r1) # Some space. 466a2e3db8Sdrahn 476a2e3db8Sdrahn # squirrel away the arguments for main 486a2e3db8Sdrahn mr %r20, %r3 #argc 496a2e3db8Sdrahn mr %r21, %r4 #argv 506a2e3db8Sdrahn mr %r22, %r5 #envp 516a2e3db8Sdrahn mr %r23, %r6 # ??? 526a2e3db8Sdrahn nop 536a2e3db8Sdrahn 546a2e3db8Sdrahn addis %r18, %r2, _DYNAMIC@toc@ha 556a2e3db8Sdrahn addi %r18, %r18, _DYNAMIC@toc@l 566a2e3db8Sdrahn 576a2e3db8Sdrahn ld %r4, -0x8000(%r2) # First entry of TOC is TOC base 586a2e3db8Sdrahn sub %r4, %r2, %r4 # determine load offset 596a2e3db8Sdrahn 606a2e3db8Sdrahn mr %r17, %r4 # save for _dl_boot 616a2e3db8Sdrahn 626a2e3db8Sdrahn subi %r3, %r21, 8 # Get stack pointer (arg0 for _dl_boot). 6361dad853Sdrahn addi %r4, %r1, 32 # dl_data 646a2e3db8Sdrahn mr %r5, %r18 # dynamicp 656a2e3db8Sdrahn 666a2e3db8Sdrahn bl _dl_boot_bind 676a2e3db8Sdrahn 686a2e3db8Sdrahn mr %r3, %r21 # argv 696a2e3db8Sdrahn mr %r4, %r22 # envp 706a2e3db8Sdrahn mr %r5, %r17 # loff 7161dad853Sdrahn addi %r6, %r1, 32 # dl_data 726a2e3db8Sdrahn 736a2e3db8Sdrahn bl _dl_boot 746a2e3db8Sdrahn 756a2e3db8Sdrahn mtctr %r3 # put return value into ctr to execute 766a2e3db8Sdrahn 776a2e3db8Sdrahn # get back the squirreled away the arguments for main 786a2e3db8Sdrahn mr %r3, %r20 796a2e3db8Sdrahn mr %r4, %r21 806a2e3db8Sdrahn mr %r5, %r22 816a2e3db8Sdrahn mr %r6, %r23 826a2e3db8Sdrahn 836a2e3db8Sdrahn addis %r7, %r2, _dl_dtors@toc@ha 846a2e3db8Sdrahn addi %r7, %r7, _dl_dtors@toc@l 856a2e3db8Sdrahn 866a2e3db8Sdrahn mtlr %r27 876a2e3db8Sdrahn ld %r1, 0(%r1) # Restore stack pointer. 886a2e3db8Sdrahn bctr # Go execute the 'real' program. 896a2e3db8SdrahnEND(_dl_start) 906a2e3db8Sdrahn 916a2e3db8SdrahnENTRY(_dl_bind_start) 92*af856e3aSkettenis # r0 contains offset, do not overwrite 93*af856e3aSkettenis # r2 ld.so toc is loaded on entry to this function. 94*af856e3aSkettenis mflr %r12 95*af856e3aSkettenis std %r12,16(%r1) # save lr 966a2e3db8Sdrahn 9761dad853Sdrahn stdu 1,-104(%r1) 9861dad853Sdrahn 9961dad853Sdrahn std %r3,32(%r1) # save r3-r10, C calling convention 10061dad853Sdrahn std %r4,40(%r1) # r13 - r31 are preserved by called code 10161dad853Sdrahn std %r5,48(%r1) 10261dad853Sdrahn std %r6,56(%r1) 10361dad853Sdrahn std %r7,64(%r1) 10461dad853Sdrahn std %r8,72(%r1) 10561dad853Sdrahn std %r9,80(%r1) 106*af856e3aSkettenis std %r10,88(%r1) 1076a2e3db8Sdrahn 108*af856e3aSkettenis mr %r3,%r11 # obj 109*af856e3aSkettenis mr %r4,%r0 # relidx 110*af856e3aSkettenis bl _dl_bind # _rtld_bind(obj, relidx) 1116a2e3db8Sdrahn mtctr %r3 11261dad853Sdrahn mr %r12, %r3 1136a2e3db8Sdrahn 11461dad853Sdrahn ld %r3,32(%r1) 11561dad853Sdrahn ld %r4,40(%r1) 11661dad853Sdrahn ld %r5,48(%r1) 11761dad853Sdrahn ld %r6,56(%r1) 11861dad853Sdrahn ld %r7,64(%r1) 11961dad853Sdrahn ld %r8,72(%r1) 12061dad853Sdrahn ld %r9,80(%r1) 121*af856e3aSkettenis ld %r10,88(%r1) 1226a2e3db8Sdrahn 12361dad853Sdrahn addi %r1,%r1,104 12461dad853Sdrahn 12561dad853Sdrahn ld %r0,16(%r1) # restore lr 1266a2e3db8Sdrahn mtlr %r0 1276a2e3db8Sdrahn 1286a2e3db8Sdrahn bctr 1296a2e3db8SdrahnEND(_dl_bind_start) 130