1*a175a520Sguenther /* $OpenBSD: boot_md.c,v 1.4 2022/01/17 19:45:34 guenther Exp $ */
217fa8871Sguenther
317fa8871Sguenther /*
417fa8871Sguenther * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
517fa8871Sguenther *
617fa8871Sguenther * Redistribution and use in source and binary forms, with or without
717fa8871Sguenther * modification, are permitted provided that the following conditions
817fa8871Sguenther * are met:
917fa8871Sguenther * 1. Redistributions of source code must retain the above copyright
1017fa8871Sguenther * notice, this list of conditions and the following disclaimer.
1117fa8871Sguenther * 2. Redistributions in binary form must reproduce the above copyright
1217fa8871Sguenther * notice, this list of conditions and the following disclaimer in the
1317fa8871Sguenther * documentation and/or other materials provided with the distribution.
1417fa8871Sguenther *
1517fa8871Sguenther * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1617fa8871Sguenther * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1717fa8871Sguenther * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1817fa8871Sguenther * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
1917fa8871Sguenther * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2017fa8871Sguenther * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2117fa8871Sguenther * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2217fa8871Sguenther * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2317fa8871Sguenther * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2417fa8871Sguenther * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2517fa8871Sguenther * SUCH DAMAGE.
2617fa8871Sguenther *
2717fa8871Sguenther */
2817fa8871Sguenther
2917fa8871Sguenther /*
3017fa8871Sguenther * IMPORTANT: any functions below are NOT protected by SSP. Please
3117fa8871Sguenther * do not add anything except what is required to reach GOT with
3217fa8871Sguenther * an adjustment.
3317fa8871Sguenther */
3417fa8871Sguenther
3517fa8871Sguenther #define _DYN_LOADER
3617fa8871Sguenther
375b60b03cSguenther #include <sys/exec_elf.h>
3817fa8871Sguenther
39*a175a520Sguenther #include <machine/reloc.h>
40*a175a520Sguenther
41*a175a520Sguenther #include "util.h"
4217fa8871Sguenther #include "archdep.h"
4317fa8871Sguenther
4417fa8871Sguenther #include "../../lib/csu/os-note-elf.h"
4517fa8871Sguenther
4617fa8871Sguenther typedef Elf_Rel RELOC_TYPE;
4717fa8871Sguenther
4817fa8871Sguenther /*
4917fa8871Sguenther * Local decls.
5017fa8871Sguenther */
5117fa8871Sguenther void _dl_boot_bind(const long, long *, Elf_Dyn *) __boot;
5217fa8871Sguenther
5317fa8871Sguenther void
_dl_boot_bind(const long sp,long * dl_data,Elf_Dyn * dynp)5417fa8871Sguenther _dl_boot_bind(const long sp, long *dl_data, Elf_Dyn *dynp)
5517fa8871Sguenther {
5617fa8871Sguenther AuxInfo *auxstack;
5717fa8871Sguenther long *stack;
5817fa8871Sguenther int n, argc;
5917fa8871Sguenther char **argv, **envp;
6017fa8871Sguenther long loff;
61*a175a520Sguenther unsigned i;
62*a175a520Sguenther const RELOC_TYPE *rend;
63*a175a520Sguenther const RELOC_TYPE *dt_reloc; /* DT_REL */
64*a175a520Sguenther unsigned dt_relocsz; /* DT_RELSZ */
65*a175a520Sguenther const Elf_Sym *dt_symtab;
66*a175a520Sguenther Elf_Addr *dt_pltgot;
67*a175a520Sguenther unsigned dt_local_gotno, dt_gotsym, dt_symtabno;
6817fa8871Sguenther
6917fa8871Sguenther /*
7017fa8871Sguenther * Scan argument and environment vectors. Find dynamic
7117fa8871Sguenther * data vector put after them.
7217fa8871Sguenther */
7317fa8871Sguenther stack = (long *)sp;
7417fa8871Sguenther argc = *stack++;
7517fa8871Sguenther argv = (char **)stack;
7617fa8871Sguenther envp = &argv[argc + 1];
7717fa8871Sguenther stack = (long *)envp;
7817fa8871Sguenther while (*stack++ != 0L)
7917fa8871Sguenther ;
8017fa8871Sguenther
8117fa8871Sguenther /*
8217fa8871Sguenther * Zero out dl_data.
8317fa8871Sguenther */
8417fa8871Sguenther for (n = 0; n <= AUX_entry; n++)
8517fa8871Sguenther dl_data[n] = 0;
8617fa8871Sguenther
8717fa8871Sguenther /*
8817fa8871Sguenther * Dig out auxiliary data set up by exec call. Move all known
8917fa8871Sguenther * tags to an indexed local table for easy access.
9017fa8871Sguenther */
9117fa8871Sguenther for (auxstack = (AuxInfo *)stack; auxstack->au_id != AUX_null;
9217fa8871Sguenther auxstack++) {
9317fa8871Sguenther if (auxstack->au_id > AUX_entry)
9417fa8871Sguenther continue;
9517fa8871Sguenther dl_data[auxstack->au_id] = auxstack->au_v;
9617fa8871Sguenther }
9717fa8871Sguenther loff = dl_data[AUX_base]; /* XXX assumes ld.so is linked at 0x0 */
9817fa8871Sguenther
9917fa8871Sguenther /*
100*a175a520Sguenther * Scan the DYNAMIC section for the loader for the items we need
10117fa8871Sguenther */
102*a175a520Sguenther dt_reloc = NULL;
103*a175a520Sguenther dt_local_gotno = dt_gotsym = dt_symtabno = dt_relocsz = 0;
10417fa8871Sguenther while (dynp->d_tag != DT_NULL) {
10517fa8871Sguenther /* first the tags that are pointers to be relocated */
106*a175a520Sguenther if (dynp->d_tag == DT_SYMTAB)
107*a175a520Sguenther dt_symtab = (void *)(dynp->d_un.d_ptr + loff);
108*a175a520Sguenther else if (dynp->d_tag == RELOC_TAG) /* DT_REL */
109*a175a520Sguenther dt_reloc = (void *)(dynp->d_un.d_ptr + loff);
110*a175a520Sguenther else if (dynp->d_tag == DT_PLTGOT)
111*a175a520Sguenther dt_pltgot = (void *)(dynp->d_un.d_ptr + loff);
11217fa8871Sguenther
11317fa8871Sguenther /* Now for the tags that are just sizes or counts */
114*a175a520Sguenther else if (dynp->d_tag == RELOC_TAG+1) /* DT_RELSZ */
115*a175a520Sguenther dt_relocsz = dynp->d_un.d_val;
116*a175a520Sguenther else if (dynp->d_tag == DT_MIPS_LOCAL_GOTNO)
117*a175a520Sguenther dt_local_gotno = dynp->d_un.d_val;
118*a175a520Sguenther else if (dynp->d_tag == DT_MIPS_GOTSYM)
119*a175a520Sguenther dt_gotsym = dynp->d_un.d_val;
120*a175a520Sguenther else if (dynp->d_tag == DT_MIPS_SYMTABNO)
121*a175a520Sguenther dt_symtabno = dynp->d_un.d_val;
12217fa8871Sguenther dynp++;
12317fa8871Sguenther }
12417fa8871Sguenther
125*a175a520Sguenther rend = (RELOC_TYPE *)((char *)dt_reloc + dt_relocsz);
126*a175a520Sguenther for (; dt_reloc < rend; dt_reloc++) {
127*a175a520Sguenther if (ELF64_R_TYPE(dt_reloc->r_info) == R_MIPS_REL32_64) {
128*a175a520Sguenther Elf_Addr *ra;
129*a175a520Sguenther ra = (Elf_Addr *)(dt_reloc->r_offset + loff);
130b883d3d1Sguenther *ra += loff;
131b883d3d1Sguenther }
13217fa8871Sguenther }
13317fa8871Sguenther
134*a175a520Sguenther /* Do all local gots */
135*a175a520Sguenther for (i = 2; i < dt_local_gotno; i++)
136*a175a520Sguenther dt_pltgot[i] += loff;
137*a175a520Sguenther dt_pltgot += dt_local_gotno;
138*a175a520Sguenther
139*a175a520Sguenther /* Do symbol referencing gots. There should be no global... */
140*a175a520Sguenther i = dt_symtabno - dt_gotsym;
141*a175a520Sguenther dt_symtab += dt_gotsym;
142*a175a520Sguenther
143*a175a520Sguenther while (i--) {
144*a175a520Sguenther if (ELF64_ST_TYPE(dt_symtab->st_info) == STT_FUNC)
145*a175a520Sguenther *dt_pltgot += loff;
146*a175a520Sguenther else
147*a175a520Sguenther *dt_pltgot = dt_symtab->st_value + loff;
148*a175a520Sguenther dt_pltgot++;
149*a175a520Sguenther dt_symtab++;
150*a175a520Sguenther }
15117fa8871Sguenther }
152