1*61dad853Sdrahn/* $OpenBSD: ldasm.S,v 1.2 2020/06/28 17:58:40 drahn 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 45*61dad853Sdrahn 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). 63*61dad853Sdrahn 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 71*61dad853Sdrahn 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) 926a2e3db8Sdrahn mflr %r0 93*61dad853Sdrahn std %r0,16(%r1) # save lr 94*61dad853Sdrahn std %r2,24(%r1) # save toc 956a2e3db8Sdrahn 96*61dad853Sdrahn stdu 1,-104(%r1) 97*61dad853Sdrahn 98*61dad853Sdrahn std %r3,32(%r1) # save r3-r10, C calling convention 99*61dad853Sdrahn std %r4,40(%r1) # r13 - r31 are preserved by called code 100*61dad853Sdrahn std %r5,48(%r1) 101*61dad853Sdrahn std %r6,56(%r1) 102*61dad853Sdrahn std %r7,64(%r1) 103*61dad853Sdrahn std %r8,72(%r1) 104*61dad853Sdrahn std %r9,80(%r1) 105*61dad853Sdrahn std %r10,96(%r1) 1066a2e3db8Sdrahn 1076a2e3db8Sdrahn mr %r3,%r12 # obj 1086a2e3db8Sdrahn mr %r4,%r11 # reloff 1096a2e3db8Sdrahn bl _dl_bind # _rtld_bind(obj, reloff) 1106a2e3db8Sdrahn mtctr %r3 111*61dad853Sdrahn mr %r12, %r3 1126a2e3db8Sdrahn 113*61dad853Sdrahn ld %r3,32(%r1) 114*61dad853Sdrahn ld %r4,40(%r1) 115*61dad853Sdrahn ld %r5,48(%r1) 116*61dad853Sdrahn ld %r6,56(%r1) 117*61dad853Sdrahn ld %r7,64(%r1) 118*61dad853Sdrahn ld %r8,72(%r1) 119*61dad853Sdrahn ld %r9,80(%r1) 120*61dad853Sdrahn ld %r10,96(%r1) 1216a2e3db8Sdrahn 122*61dad853Sdrahn addi %r1,%r1,104 123*61dad853Sdrahn 124*61dad853Sdrahn ld %r0,16(%r1) # restore lr 1256a2e3db8Sdrahn mtlr %r0 1266a2e3db8Sdrahn 1276a2e3db8Sdrahn bctr 1286a2e3db8SdrahnEND(_dl_bind_start) 129