xref: /openbsd-src/libexec/ld.so/hppa/boot_md.c (revision fdacdf06ecf4c2c68e871fe2f3dc896fc2cc4a61)
1 /*	$OpenBSD: boot_md.c,v 1.6 2022/01/31 05:43:22 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 "util.h"
40 #include "archdep.h"		/* for RELOC_TAG */
41 
42 #include "../../lib/csu/os-note-elf.h"
43 
44 typedef	Elf_RelA	RELOC_TYPE;
45 
46 /*
47  * Local decls.
48  */
49 void _dl_boot_bind(const long, long *, Elf_Dyn *) __boot;
50 
51 void
_dl_boot_bind(const long sp,long * dl_data,Elf_Dyn * dynp)52 _dl_boot_bind(const long sp, long *dl_data, Elf_Dyn *dynp)
53 {
54 	AuxInfo			*auxstack;
55 	long			*stack;
56 	int			n, argc;
57 	char			**argv, **envp;
58 	long			loff;
59 	const RELOC_TYPE	*rend;
60 	const RELOC_TYPE	*dt_reloc;	/* DT_RELA */
61 	Elf_Addr		dt_relocsz;	/* DT_RELASZ */
62 	Elf_Addr		dt_pltgot;
63 	Elf_Addr		dt_pltrelsz;
64 	const Elf_Sym		*dt_symtab;
65 	const RELOC_TYPE	*dt_jmprel;
66 
67 	/*
68 	 * Scan argument and environment vectors. Find dynamic
69 	 * data vector put after them.
70 	 */
71 	stack = (long *)sp;
72 	argc = *stack++;
73 	argv = (char **)stack;
74 	envp = &argv[argc + 1];
75 	stack = (long *)envp;
76 	while (*stack++ != 0L)
77 		;
78 
79 	/*
80 	 * Zero out dl_data.
81 	 */
82 	for (n = 0; n <= AUX_entry; n++)
83 		dl_data[n] = 0;
84 
85 	/*
86 	 * Dig out auxiliary data set up by exec call. Move all known
87 	 * tags to an indexed local table for easy access.
88 	 */
89 	for (auxstack = (AuxInfo *)stack; auxstack->au_id != AUX_null;
90 	    auxstack++) {
91 		if (auxstack->au_id > AUX_entry)
92 			continue;
93 		dl_data[auxstack->au_id] = auxstack->au_v;
94 	}
95 	loff = dl_data[AUX_base];	/* XXX assumes ld.so is linked at 0x0 */
96 
97 	/*
98 	 * Scan the DYNAMIC section for the loader for the two items we need
99 	 */
100 	dt_pltrelsz = dt_relocsz = dt_pltgot = 0;
101 	dt_jmprel = dt_reloc = NULL;
102 	dt_symtab = NULL;
103 	while (dynp->d_tag != DT_NULL) {
104 		/* first the tags that are pointers to be relocated */
105 		if (dynp->d_tag == DT_PLTGOT)
106 			dt_pltgot = dynp->d_un.d_ptr + loff;
107 		else if (dynp->d_tag == DT_SYMTAB)
108 			dt_symtab = (void *)(dynp->d_un.d_ptr + loff);
109 		else if (dynp->d_tag == RELOC_TAG)	/* DT_{RELA,REL} */
110 			dt_reloc = (void *)(dynp->d_un.d_ptr + loff);
111 		else if (dynp->d_tag == DT_JMPREL)
112 			dt_jmprel = (void *)(dynp->d_un.d_ptr + loff);
113 
114 		/* Now for the tags that are just sizes or counts */
115 		else if (dynp->d_tag == DT_PLTRELSZ)
116 			dt_pltrelsz = dynp->d_un.d_val;
117 		else if (dynp->d_tag == RELOC_TAG+1)	/* DT_{RELA,REL}SZ */
118 			dt_relocsz = dynp->d_un.d_val;
119 		dynp++;
120 	}
121 
122 	rend = (RELOC_TYPE *)((char *)dt_jmprel + dt_pltrelsz);
123 	for (; dt_jmprel < rend; dt_jmprel++) {
124 		Elf_Addr *ra;
125 		const Elf_Sym *sp;
126 
127 		sp = dt_symtab + ELF_R_SYM(dt_jmprel->r_info);
128 		ra = (Elf_Addr *)(dt_jmprel->r_offset + loff);
129 		ra[0] = loff + sp->st_value + dt_jmprel->r_addend;
130 		ra[1] = dt_pltgot;
131 	}
132 
133 	rend = (RELOC_TYPE *)((char *)dt_reloc + dt_relocsz);
134 	for (; dt_reloc < rend; dt_reloc++) {
135 		Elf_Addr *ra;
136 		const Elf_Sym *sp;
137 
138 		sp = dt_symtab + ELF_R_SYM(dt_reloc->r_info);
139 		ra = (Elf_Addr *)(dt_reloc->r_offset + loff);
140 		*ra = loff + sp->st_value + dt_reloc->r_addend;
141 	}
142 }
143