xref: /openbsd-src/libexec/ld.so/hppa/boot_md.c (revision fdacdf06ecf4c2c68e871fe2f3dc896fc2cc4a61)
1*fdacdf06Sguenther /*	$OpenBSD: boot_md.c,v 1.6 2022/01/31 05:43:22 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 
37*fdacdf06Sguenther #include <sys/exec_elf.h>
3817fa8871Sguenther 
39*fdacdf06Sguenther #include "util.h"
40*fdacdf06Sguenther #include "archdep.h"		/* for RELOC_TAG */
4117fa8871Sguenther 
4217fa8871Sguenther #include "../../lib/csu/os-note-elf.h"
4317fa8871Sguenther 
4417fa8871Sguenther typedef	Elf_RelA	RELOC_TYPE;
4517fa8871Sguenther 
4617fa8871Sguenther /*
4717fa8871Sguenther  * Local decls.
4817fa8871Sguenther  */
4917fa8871Sguenther void _dl_boot_bind(const long, long *, Elf_Dyn *) __boot;
5017fa8871Sguenther 
5117fa8871Sguenther void
_dl_boot_bind(const long sp,long * dl_data,Elf_Dyn * dynp)5217fa8871Sguenther _dl_boot_bind(const long sp, long *dl_data, Elf_Dyn *dynp)
5317fa8871Sguenther {
5417fa8871Sguenther 	AuxInfo			*auxstack;
5517fa8871Sguenther 	long			*stack;
5617fa8871Sguenther 	int			n, argc;
5717fa8871Sguenther 	char			**argv, **envp;
5817fa8871Sguenther 	long			loff;
59*fdacdf06Sguenther 	const RELOC_TYPE	*rend;
60*fdacdf06Sguenther 	const RELOC_TYPE	*dt_reloc;	/* DT_RELA */
61*fdacdf06Sguenther 	Elf_Addr		dt_relocsz;	/* DT_RELASZ */
62*fdacdf06Sguenther 	Elf_Addr		dt_pltgot;
63*fdacdf06Sguenther 	Elf_Addr		dt_pltrelsz;
64*fdacdf06Sguenther 	const Elf_Sym		*dt_symtab;
65*fdacdf06Sguenther 	const RELOC_TYPE	*dt_jmprel;
6617fa8871Sguenther 
6717fa8871Sguenther 	/*
6817fa8871Sguenther 	 * Scan argument and environment vectors. Find dynamic
6917fa8871Sguenther 	 * data vector put after them.
7017fa8871Sguenther 	 */
7117fa8871Sguenther 	stack = (long *)sp;
7217fa8871Sguenther 	argc = *stack++;
7317fa8871Sguenther 	argv = (char **)stack;
7417fa8871Sguenther 	envp = &argv[argc + 1];
7517fa8871Sguenther 	stack = (long *)envp;
7617fa8871Sguenther 	while (*stack++ != 0L)
7717fa8871Sguenther 		;
7817fa8871Sguenther 
7917fa8871Sguenther 	/*
8017fa8871Sguenther 	 * Zero out dl_data.
8117fa8871Sguenther 	 */
8217fa8871Sguenther 	for (n = 0; n <= AUX_entry; n++)
8317fa8871Sguenther 		dl_data[n] = 0;
8417fa8871Sguenther 
8517fa8871Sguenther 	/*
8617fa8871Sguenther 	 * Dig out auxiliary data set up by exec call. Move all known
8717fa8871Sguenther 	 * tags to an indexed local table for easy access.
8817fa8871Sguenther 	 */
8917fa8871Sguenther 	for (auxstack = (AuxInfo *)stack; auxstack->au_id != AUX_null;
9017fa8871Sguenther 	    auxstack++) {
9117fa8871Sguenther 		if (auxstack->au_id > AUX_entry)
9217fa8871Sguenther 			continue;
9317fa8871Sguenther 		dl_data[auxstack->au_id] = auxstack->au_v;
9417fa8871Sguenther 	}
9517fa8871Sguenther 	loff = dl_data[AUX_base];	/* XXX assumes ld.so is linked at 0x0 */
9617fa8871Sguenther 
9717fa8871Sguenther 	/*
98*fdacdf06Sguenther 	 * Scan the DYNAMIC section for the loader for the two items we need
9917fa8871Sguenther 	 */
100*fdacdf06Sguenther 	dt_pltrelsz = dt_relocsz = dt_pltgot = 0;
101*fdacdf06Sguenther 	dt_jmprel = dt_reloc = NULL;
102*fdacdf06Sguenther 	dt_symtab = NULL;
10317fa8871Sguenther 	while (dynp->d_tag != DT_NULL) {
10417fa8871Sguenther 		/* first the tags that are pointers to be relocated */
10517fa8871Sguenther 		if (dynp->d_tag == DT_PLTGOT)
106*fdacdf06Sguenther 			dt_pltgot = dynp->d_un.d_ptr + loff;
10717fa8871Sguenther 		else if (dynp->d_tag == DT_SYMTAB)
108*fdacdf06Sguenther 			dt_symtab = (void *)(dynp->d_un.d_ptr + loff);
10917fa8871Sguenther 		else if (dynp->d_tag == RELOC_TAG)	/* DT_{RELA,REL} */
110*fdacdf06Sguenther 			dt_reloc = (void *)(dynp->d_un.d_ptr + loff);
11117fa8871Sguenther 		else if (dynp->d_tag == DT_JMPREL)
112*fdacdf06Sguenther 			dt_jmprel = (void *)(dynp->d_un.d_ptr + loff);
11317fa8871Sguenther 
11417fa8871Sguenther 		/* Now for the tags that are just sizes or counts */
11517fa8871Sguenther 		else if (dynp->d_tag == DT_PLTRELSZ)
116*fdacdf06Sguenther 			dt_pltrelsz = dynp->d_un.d_val;
11717fa8871Sguenther 		else if (dynp->d_tag == RELOC_TAG+1)	/* DT_{RELA,REL}SZ */
118*fdacdf06Sguenther 			dt_relocsz = dynp->d_un.d_val;
11917fa8871Sguenther 		dynp++;
12017fa8871Sguenther 	}
12117fa8871Sguenther 
122*fdacdf06Sguenther 	rend = (RELOC_TYPE *)((char *)dt_jmprel + dt_pltrelsz);
123*fdacdf06Sguenther 	for (; dt_jmprel < rend; dt_jmprel++) {
12417fa8871Sguenther 		Elf_Addr *ra;
12517fa8871Sguenther 		const Elf_Sym *sp;
12617fa8871Sguenther 
127*fdacdf06Sguenther 		sp = dt_symtab + ELF_R_SYM(dt_jmprel->r_info);
128*fdacdf06Sguenther 		ra = (Elf_Addr *)(dt_jmprel->r_offset + loff);
129*fdacdf06Sguenther 		ra[0] = loff + sp->st_value + dt_jmprel->r_addend;
130*fdacdf06Sguenther 		ra[1] = dt_pltgot;
13117fa8871Sguenther 	}
13217fa8871Sguenther 
133*fdacdf06Sguenther 	rend = (RELOC_TYPE *)((char *)dt_reloc + dt_relocsz);
134*fdacdf06Sguenther 	for (; dt_reloc < rend; dt_reloc++) {
13517fa8871Sguenther 		Elf_Addr *ra;
13617fa8871Sguenther 		const Elf_Sym *sp;
13717fa8871Sguenther 
138*fdacdf06Sguenther 		sp = dt_symtab + ELF_R_SYM(dt_reloc->r_info);
139*fdacdf06Sguenther 		ra = (Elf_Addr *)(dt_reloc->r_offset + loff);
140*fdacdf06Sguenther 		*ra = loff + sp->st_value + dt_reloc->r_addend;
14117fa8871Sguenther 	}
14217fa8871Sguenther }
143