1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/* $OpenBSD: boot.h,v 1.21 2016/08/07 02:44:00 guenther Exp $ */
/*
* Copyright (c) 1998 Per Fogelstrom, Opsycon AB
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* IMPORTANT: any functions below are NOT protected by SSP. Please
* do not add anything except what is required to reach GOT with
* an adjustment.
*/
#define _DYN_LOADER
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/exec.h>
#include <sys/sysctl.h>
#include <nlist.h>
#include <link.h>
#include <dlfcn.h>
#include "syscall.h"
#include "archdep.h"
#include "path.h"
#include "resolve.h"
#include "sod.h"
#include "stdlib.h"
/*
* Use the internal, hidden name for any syscalls we need, to avoid
* accidental override by application code
*/
#define REDIRECT_SYSCALL(x) typeof(x) x asm("_libc_"#x) __dso_hidden
REDIRECT_SYSCALL(mprotect);
#ifdef RCRT0
#define DT_PROC(n) ((n) - DT_LOPROC)
#if RELOC_TAG == DT_RELA
typedef Elf_RelA RELOC_TYPE;
#elif RELOC_TAG == DT_REL
typedef Elf_Rel RELOC_TYPE;
#else
# error "unknown RELOC_TAG"
#endif
/* The set of dynamic tags that we're interested in for bootstrapping */
struct boot_dyn {
RELOC_TYPE *dt_reloc; /* DT_RELA or DT_REL */
Elf_Addr dt_relocsz; /* DT_RELASZ or DT_RELSZ */
Elf_Addr *dt_pltgot;
Elf_Addr dt_pltrelsz;
const Elf_Sym *dt_symtab;
RELOC_TYPE *dt_jmprel;
#if DT_PROCNUM > 0
u_long dt_proc[DT_PROCNUM];
#endif
};
/*
* Local decls.
*/
void _dl_boot_bind(const long, long *, Elf_Dyn *);
extern char __plt_start[];
extern char __plt_end[];
extern char __got_start[];
extern char __got_end[];
void
_dl_boot_bind(const long sp, long *dl_data, Elf_Dyn *dynamicp)
{
struct boot_dyn dynld; /* Resolver data for the loader */
AuxInfo *auxstack;
long *stack;
Elf_Dyn *dynp;
Elf_Addr start;
size_t size;
int pagesize;
int n, argc;
char **argv, **envp;
long loff;
int prot_exec = 0;
RELOC_TYPE *rp;
Elf_Addr i;
/*
* Scan argument and environment vectors. Find dynamic
* data vector put after them.
*/
stack = (long *)sp;
argc = *stack++;
argv = (char **)stack;
envp = &argv[argc + 1];
stack = (long *)envp;
while (*stack++ != 0L)
;
/*
* Zero out dl_data.
*/
for (n = 0; n <= AUX_entry; n++)
dl_data[n] = 0;
/*
* Dig out auxiliary data set up by exec call. Move all known
* tags to an indexed local table for easy access.
*/
for (auxstack = (AuxInfo *)stack; auxstack->au_id != AUX_null;
auxstack++) {
if (auxstack->au_id > AUX_entry)
continue;
dl_data[auxstack->au_id] = auxstack->au_v;
}
loff = dl_data[AUX_base]; /* XXX assumes ld.so is linked at 0x0 */
/*
* We need to do 'selfreloc' in case the code weren't
* loaded at the address it was linked to.
*
* Scan the DYNAMIC section for the loader.
* Cache the data for easier access.
*/
dynp = dynamicp;
_dl_memset(&dynld, 0, sizeof(dynld));
while (dynp->d_tag != DT_NULL) {
/* first the tags that are pointers to be relocated */
if (dynp->d_tag == DT_PLTGOT)
dynld.dt_pltgot = (void *)(dynp->d_un.d_ptr + loff);
else if (dynp->d_tag == DT_SYMTAB)
dynld.dt_symtab = (void *)(dynp->d_un.d_ptr + loff);
else if (dynp->d_tag == RELOC_TAG) /* DT_{RELA,REL} */
dynld.dt_reloc = (void *)(dynp->d_un.d_ptr + loff);
else if (dynp->d_tag == DT_JMPREL)
dynld.dt_jmprel = (void *)(dynp->d_un.d_ptr + loff);
/* Now for the tags that are just sizes or counts */
else if (dynp->d_tag == DT_PLTRELSZ)
dynld.dt_pltrelsz = dynp->d_un.d_val;
else if (dynp->d_tag == RELOC_TAG+1) /* DT_{RELA,REL}SZ */
dynld.dt_relocsz = dynp->d_un.d_val;
#if DT_PROCNUM > 0
else if (dynp->d_tag >= DT_LOPROC &&
dynp->d_tag < DT_LOPROC + DT_PROCNUM)
dynld.dt_proc[dynp->d_tag - DT_LOPROC] =
dynp->d_un.d_val;
#endif /* DT_PROCNUM */
dynp++;
}
rp = dynld.dt_jmprel;
for (i = 0; i < dynld.dt_pltrelsz; i += sizeof *rp) {
Elf_Addr *ra;
const Elf_Sym *sp;
sp = dynld.dt_symtab + ELF_R_SYM(rp->r_info);
if (!ELF_R_SYM(rp->r_info) || sp->st_value != 0) {
#ifdef HAVE_JMPREL
ra = (Elf_Addr *)(rp->r_offset + loff);
RELOC_JMPREL(rp, sp, ra, loff, dynld.dt_pltgot);
#else
_dl_exit(6);
#endif
}
rp++;
}
rp = dynld.dt_reloc;
for (i = 0; i < dynld.dt_relocsz; i += sizeof *rp) {
Elf_Addr *ra;
const Elf_Sym *sp;
sp = dynld.dt_symtab + ELF_R_SYM(rp->r_info);
if (!ELF_R_SYM(rp->r_info) || sp->st_value != 0) {
ra = (Elf_Addr *)(rp->r_offset + loff);
RELOC_DYN(rp, sp, ra, loff);
}
rp++;
}
RELOC_GOT(&dynld, loff);
/*
* we have been fully relocated here, so most things no longer
* need the loff adjustment
*/
/*
* No further changes to the PLT and/or GOT are needed so make
* them read-only.
*/
if (dl_data[AUX_pagesz] != 0)
pagesize = dl_data[AUX_pagesz];
else
pagesize = 4096;
#if defined(__alpha__) || defined(__powerpc__) || defined(__sparc__) || \
defined(__sparc64__)
start = ELF_TRUNC((Elf_Addr)__plt_start, pagesize);
size = ELF_ROUND((Elf_Addr)__plt_end - start, pagesize);
mprotect((void *)start, size, PROT_READ);
#endif
#if defined(__powerpc__)
if (dynld.dt_proc[DT_PROC(DT_PPC_GOT)] == 0)
prot_exec = PROT_EXEC;
#endif
start = ELF_TRUNC((Elf_Addr)__got_start, pagesize);
size = ELF_ROUND((Elf_Addr)__got_end - start, pagesize);
mprotect((void *)start, size, GOT_PERMS | prot_exec);
}
#ifdef __alpha__
void _reloc_alpha_got(Elf_Dyn *dynp, Elf_Addr relocbase);
void
_reloc_alpha_got(Elf_Dyn *dynp, Elf_Addr relocbase)
{
const Elf_RelA *rela = 0, *relalim;
Elf_Addr relasz = 0;
Elf_Addr *where;
for (; dynp->d_tag != DT_NULL; dynp++) {
switch (dynp->d_tag) {
case DT_RELA:
rela = (const Elf_RelA *)(relocbase + dynp->d_un.d_ptr);
break;
case DT_RELASZ:
relasz = dynp->d_un.d_val;
break;
}
}
relalim = (const Elf_RelA *)((caddr_t)rela + relasz);
for (; rela < relalim; rela++) {
if (ELF64_R_TYPE(rela->r_info) != RELOC_RELATIVE)
continue;
where = (Elf_Addr *)(relocbase + rela->r_offset);
*where += (Elf_Addr)relocbase;
}
}
#endif
#endif /* RCRT0 */
|