1/* $OpenBSD: boot_md.c,v 1.4 2022/01/17 19:45:34 guenther Exp $ */ 2 3/* 4 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29/* 30 * IMPORTANT: any functions below are NOT protected by SSP. Please 31 * do not add anything except what is required to reach GOT with 32 * an adjustment. 33 */ 34 35#define _DYN_LOADER 36 37#include <sys/exec_elf.h> 38 39#include <machine/reloc.h> 40 41#include "util.h" 42#include "archdep.h" 43 44#include "../../lib/csu/os-note-elf.h" 45 46typedef Elf_Rel RELOC_TYPE; 47 48/* 49 * Local decls. 50 */ 51void _dl_boot_bind(const long, long *, Elf_Dyn *) __boot; 52 53void 54_dl_boot_bind(const long sp, long *dl_data, Elf_Dyn *dynp) 55{ 56 AuxInfo *auxstack; 57 long *stack; 58 int n, argc; 59 char **argv, **envp; 60 long loff; 61 unsigned i; 62 const RELOC_TYPE *rend; 63 const RELOC_TYPE *dt_reloc; /* DT_REL */ 64 unsigned dt_relocsz; /* DT_RELSZ */ 65 const Elf_Sym *dt_symtab; 66 Elf_Addr *dt_pltgot; 67 unsigned dt_local_gotno, dt_gotsym, dt_symtabno; 68 69 /* 70 * Scan argument and environment vectors. Find dynamic 71 * data vector put after them. 72 */ 73 stack = (long *)sp; 74 argc = *stack++; 75 argv = (char **)stack; 76 envp = &argv[argc + 1]; 77 stack = (long *)envp; 78 while (*stack++ != 0L) 79 ; 80 81 /* 82 * Zero out dl_data. 83 */ 84 for (n = 0; n <= AUX_entry; n++) 85 dl_data[n] = 0; 86 87 /* 88 * Dig out auxiliary data set up by exec call. Move all known 89 * tags to an indexed local table for easy access. 90 */ 91 for (auxstack = (AuxInfo *)stack; auxstack->au_id != AUX_null; 92 auxstack++) { 93 if (auxstack->au_id > AUX_entry) 94 continue; 95 dl_data[auxstack->au_id] = auxstack->au_v; 96 } 97 loff = dl_data[AUX_base]; /* XXX assumes ld.so is linked at 0x0 */ 98 99 /* 100 * Scan the DYNAMIC section for the loader for the items we need 101 */ 102 dt_reloc = NULL; 103 dt_local_gotno = dt_gotsym = dt_symtabno = dt_relocsz = 0; 104 while (dynp->d_tag != DT_NULL) { 105 /* first the tags that are pointers to be relocated */ 106 if (dynp->d_tag == DT_SYMTAB) 107 dt_symtab = (void *)(dynp->d_un.d_ptr + loff); 108 else if (dynp->d_tag == RELOC_TAG) /* DT_REL */ 109 dt_reloc = (void *)(dynp->d_un.d_ptr + loff); 110 else if (dynp->d_tag == DT_PLTGOT) 111 dt_pltgot = (void *)(dynp->d_un.d_ptr + loff); 112 113 /* Now for the tags that are just sizes or counts */ 114 else if (dynp->d_tag == RELOC_TAG+1) /* DT_RELSZ */ 115 dt_relocsz = dynp->d_un.d_val; 116 else if (dynp->d_tag == DT_MIPS_LOCAL_GOTNO) 117 dt_local_gotno = dynp->d_un.d_val; 118 else if (dynp->d_tag == DT_MIPS_GOTSYM) 119 dt_gotsym = dynp->d_un.d_val; 120 else if (dynp->d_tag == DT_MIPS_SYMTABNO) 121 dt_symtabno = dynp->d_un.d_val; 122 dynp++; 123 } 124 125 rend = (RELOC_TYPE *)((char *)dt_reloc + dt_relocsz); 126 for (; dt_reloc < rend; dt_reloc++) { 127 if (ELF64_R_TYPE(dt_reloc->r_info) == R_MIPS_REL32_64) { 128 Elf_Addr *ra; 129 ra = (Elf_Addr *)(dt_reloc->r_offset + loff); 130 *ra += loff; 131 } 132 } 133 134 /* Do all local gots */ 135 for (i = 2; i < dt_local_gotno; i++) 136 dt_pltgot[i] += loff; 137 dt_pltgot += dt_local_gotno; 138 139 /* Do symbol referencing gots. There should be no global... */ 140 i = dt_symtabno - dt_gotsym; 141 dt_symtab += dt_gotsym; 142 143 while (i--) { 144 if (ELF64_ST_TYPE(dt_symtab->st_info) == STT_FUNC) 145 *dt_pltgot += loff; 146 else 147 *dt_pltgot = dt_symtab->st_value + loff; 148 dt_pltgot++; 149 dt_symtab++; 150 } 151} 152