xref: /openbsd/libexec/ld.so/mips64/boot_md.c (revision 76d0caae)
1 /*	$OpenBSD: boot_md.c,v 1.3 2019/11/10 22:21:54 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 "syscall.h"
40 #include "archdep.h"
41 #include "stdlib.h"
42 
43 #include "../../lib/csu/os-note-elf.h"
44 
45 #if RELOC_TAG == DT_RELA
46 typedef	Elf_RelA	RELOC_TYPE;
47 #elif RELOC_TAG == DT_REL
48 typedef	Elf_Rel		RELOC_TYPE;
49 #else
50 # error "unknown RELOC_TAG"
51 #endif
52 
53 /* The set of dynamic tags that we're interested in for bootstrapping */
54 struct boot_dyn {
55 	RELOC_TYPE	*dt_reloc;	/* DT_RELA   or DT_REL */
56 	Elf_Addr	dt_relocsz;	/* DT_RELASZ or DT_RELSZ */
57 	Elf_Addr	*dt_pltgot;
58 	const Elf_Sym	*dt_symtab;
59 #if DT_PROCNUM > 0
60 	u_long		dt_proc[DT_PROCNUM];
61 #endif
62 };
63 
64 /*
65  * Local decls.
66  */
67 void _dl_boot_bind(const long, long *, Elf_Dyn *) __boot;
68 
69 void
70 _dl_boot_bind(const long sp, long *dl_data, Elf_Dyn *dynp)
71 {
72 	struct boot_dyn	dynld;		/* Resolver data for the loader */
73 	AuxInfo		*auxstack;
74 	long		*stack;
75 	int		n, argc;
76 	char		**argv, **envp;
77 	long		loff;
78 	RELOC_TYPE	*rp, *rend;
79 
80 	/*
81 	 * Scan argument and environment vectors. Find dynamic
82 	 * data vector put after them.
83 	 */
84 	stack = (long *)sp;
85 	argc = *stack++;
86 	argv = (char **)stack;
87 	envp = &argv[argc + 1];
88 	stack = (long *)envp;
89 	while (*stack++ != 0L)
90 		;
91 
92 	/*
93 	 * Zero out dl_data.
94 	 */
95 	for (n = 0; n <= AUX_entry; n++)
96 		dl_data[n] = 0;
97 
98 	/*
99 	 * Dig out auxiliary data set up by exec call. Move all known
100 	 * tags to an indexed local table for easy access.
101 	 */
102 	for (auxstack = (AuxInfo *)stack; auxstack->au_id != AUX_null;
103 	    auxstack++) {
104 		if (auxstack->au_id > AUX_entry)
105 			continue;
106 		dl_data[auxstack->au_id] = auxstack->au_v;
107 	}
108 	loff = dl_data[AUX_base];	/* XXX assumes ld.so is linked at 0x0 */
109 
110 	/*
111 	 * We need to do 'selfreloc' in case the code weren't
112 	 * loaded at the address it was linked to.
113 	 *
114 	 * Scan the DYNAMIC section for the loader.
115 	 * Cache the data for easier access.
116 	 */
117 	_dl_memset(&dynld, 0, sizeof(dynld));
118 	while (dynp->d_tag != DT_NULL) {
119 		/* first the tags that are pointers to be relocated */
120 		if (dynp->d_tag == DT_PLTGOT)
121 			dynld.dt_pltgot = (void *)(dynp->d_un.d_ptr + loff);
122 		else if (dynp->d_tag == DT_SYMTAB)
123 			dynld.dt_symtab = (void *)(dynp->d_un.d_ptr + loff);
124 		else if (dynp->d_tag == RELOC_TAG)	/* DT_{RELA,REL} */
125 			dynld.dt_reloc = (void *)(dynp->d_un.d_ptr + loff);
126 
127 		/* Now for the tags that are just sizes or counts */
128 		else if (dynp->d_tag == RELOC_TAG+1)	/* DT_{RELA,REL}SZ */
129 			dynld.dt_relocsz = dynp->d_un.d_val;
130 #if DT_PROCNUM > 0
131 		else if (dynp->d_tag >= DT_LOPROC &&
132 		    dynp->d_tag < DT_LOPROC + DT_PROCNUM)
133 			dynld.dt_proc[dynp->d_tag - DT_LOPROC] =
134 			    dynp->d_un.d_val;
135 #endif /* DT_PROCNUM */
136 		dynp++;
137 	}
138 
139 	rp = dynld.dt_reloc;
140 	rend = (RELOC_TYPE *)((char *)rp + dynld.dt_relocsz);
141 	for (; rp < rend; rp++) {
142 		if (ELF_R_TYPE(rp->r_info) != R_MIPS_NONE) {
143 			Elf_Addr *ra = (Elf_Addr *)(rp->r_offset + loff);
144 
145 			*ra += loff;
146 		}
147 	}
148 
149 	RELOC_GOT(&dynld, loff);
150 }
151