1 /* $OpenBSD: boot.c,v 1.19 2022/01/08 06:49:41 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 #if RELOC_TAG == DT_RELA
45 typedef Elf_RelA RELOC_TYPE;
46 # define RELOCATE_RELATIVE(w, r, b) *(w) = (r)->r_addend + (b)
47 #elif RELOC_TAG == DT_REL
48 typedef Elf_Rel RELOC_TYPE;
49 # define RELOCATE_RELATIVE(w, r, b) *(w) += (b)
50 #else
51 # error "unknown RELOC_TAG"
52 #endif
53
54 /*
55 * Local decls.
56 */
57 void _dl_boot_bind(const long, long *, Elf_Dyn *) __boot;
58
59 void
_dl_boot_bind(const long sp,long * dl_data,Elf_Dyn * dynp)60 _dl_boot_bind(const long sp, long *dl_data, Elf_Dyn *dynp)
61 {
62 AuxInfo *auxstack;
63 long *stack;
64 int n, argc;
65 char **argv, **envp;
66 long loff;
67 RELOC_TYPE *dt_reloc; /* DT_RELA or DT_REL */
68 Elf_Word dt_relocsz; /* DT_RELASZ or DT_RELSZ */
69 RELOC_TYPE *rend;
70
71 /*
72 * Scan argument and environment vectors. Find dynamic
73 * data vector put after them.
74 */
75 stack = (long *)sp;
76 argc = *stack++;
77 argv = (char **)stack;
78 envp = &argv[argc + 1];
79 stack = (long *)envp;
80 while (*stack++ != 0L)
81 ;
82
83 /*
84 * Zero out dl_data.
85 */
86 for (n = 0; n <= AUX_entry; n++)
87 dl_data[n] = 0;
88
89 /*
90 * Dig out auxiliary data set up by exec call. Move all known
91 * tags to an indexed local table for easy access.
92 */
93 for (auxstack = (AuxInfo *)stack; auxstack->au_id != AUX_null;
94 auxstack++) {
95 if (auxstack->au_id > AUX_entry)
96 continue;
97 dl_data[auxstack->au_id] = auxstack->au_v;
98 }
99 loff = dl_data[AUX_base]; /* XXX assumes ld.so is linked at 0x0 */
100
101 /*
102 * Scan the DYNAMIC section for the loader for the two items we need
103 */
104 dt_reloc = NULL;
105 dt_relocsz = 0;
106 while (dynp->d_tag != DT_NULL) {
107 if (dynp->d_tag == RELOC_TAG) /* DT_{RELA,REL} */
108 dt_reloc = (void *)(dynp->d_un.d_ptr + loff);
109 else if (dynp->d_tag == RELOC_TAG+1) /* DT_{RELA,REL}SZ */
110 dt_relocsz = dynp->d_un.d_val;
111 dynp++;
112 }
113
114 rend = (RELOC_TYPE *)((char *)dt_reloc + dt_relocsz);
115 for (; dt_reloc < rend; dt_reloc++) {
116 Elf_Addr *ra;
117
118 ra = (Elf_Addr *)(dt_reloc->r_offset + loff);
119 RELOCATE_RELATIVE(ra, dt_reloc, loff);
120 }
121 }
122