headers.c revision 1.11 1 /* $NetBSD: headers.c,v 1.11 2002/09/05 16:58:16 mycroft Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by John Polstra.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Dynamic linker for ELF.
36 *
37 * John Polstra <jdp (at) polstra.com>.
38 */
39
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/types.h>
49 #include <sys/mman.h>
50 #include <dirent.h>
51
52 #include "debug.h"
53 #include "rtld.h"
54
55 /*
56 * Process a shared object's DYNAMIC section, and save the important
57 * information in its Obj_Entry structure.
58 */
59 void
60 _rtld_digest_dynamic(obj)
61 Obj_Entry *obj;
62 {
63 Elf_Dyn *dynp;
64 Needed_Entry **needed_tail = &obj->needed;
65 const Elf_Dyn *dyn_rpath = NULL;
66 Elf_Sword plttype = DT_REL;
67 Elf_Addr relsz = 0, relasz = 0;
68 Elf_Addr pltrelsz = 0;
69 Elf_Addr init = 0, fini = 0;
70
71 for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; ++dynp) {
72 switch (dynp->d_tag) {
73
74 case DT_REL:
75 obj->rel = (const Elf_Rel *)
76 (obj->relocbase + dynp->d_un.d_ptr);
77 break;
78
79 case DT_RELSZ:
80 relsz = dynp->d_un.d_val;
81 break;
82
83 case DT_RELENT:
84 assert(dynp->d_un.d_val == sizeof(Elf_Rel));
85 break;
86
87 case DT_JMPREL:
88 if (plttype == DT_REL) {
89 obj->pltrel = (const Elf_Rel *)
90 (obj->relocbase + dynp->d_un.d_ptr);
91 } else {
92 obj->pltrela = (const Elf_Rela *)
93 (obj->relocbase + dynp->d_un.d_ptr);
94 }
95 break;
96
97 case DT_PLTRELSZ:
98 pltrelsz = dynp->d_un.d_val;
99 break;
100
101 case DT_RELA:
102 obj->rela = (const Elf_Rela *)
103 (obj->relocbase + dynp->d_un.d_ptr);
104 break;
105
106 case DT_RELASZ:
107 relasz = dynp->d_un.d_val;
108 break;
109
110 case DT_RELAENT:
111 assert(dynp->d_un.d_val == sizeof(Elf_Rela));
112 break;
113
114 case DT_PLTREL:
115 plttype = dynp->d_un.d_val;
116 assert(plttype == DT_REL ||
117 plttype == DT_RELA);
118 #if !defined(__sparc__) && !defined(__archv9__) && !defined(__sparc_v9__)
119 /*
120 * sparc v9 has both DT_PLTREL and DT_JMPREL.
121 * But they point to different things.
122 * We want the DT_JMPREL which points to our jump table.
123 */
124 if (plttype == DT_RELA) {
125 obj->pltrela = (const Elf_Rela *) obj->pltrel;
126 obj->pltrel = NULL;
127 }
128 #endif
129 break;
130
131 case DT_SYMTAB:
132 obj->symtab = (const Elf_Sym *)
133 (obj->relocbase + dynp->d_un.d_ptr);
134 break;
135
136 case DT_SYMENT:
137 assert(dynp->d_un.d_val == sizeof(Elf_Sym));
138 break;
139
140 case DT_STRTAB:
141 obj->strtab = (const char *)
142 (obj->relocbase + dynp->d_un.d_ptr);
143 break;
144
145 case DT_STRSZ:
146 obj->strsize = dynp->d_un.d_val;
147 break;
148
149 case DT_HASH:
150 {
151 const Elf_Word *hashtab = (const Elf_Word *)
152 (obj->relocbase + dynp->d_un.d_ptr);
153
154 obj->nbuckets = hashtab[0];
155 obj->nchains = hashtab[1];
156 obj->buckets = hashtab + 2;
157 obj->chains = obj->buckets + obj->nbuckets;
158 }
159 break;
160
161 case DT_NEEDED:
162 assert(!obj->rtld);
163 {
164 Needed_Entry *nep = NEW(Needed_Entry);
165
166 nep->name = dynp->d_un.d_val;
167 nep->obj = NULL;
168 nep->next = NULL;
169
170 *needed_tail = nep;
171 needed_tail = &nep->next;
172 }
173 break;
174
175 case DT_PLTGOT:
176 obj->pltgot = (Elf_Addr *)
177 (obj->relocbase + dynp->d_un.d_ptr);
178 break;
179
180 case DT_TEXTREL:
181 obj->textrel = true;
182 break;
183
184 case DT_SYMBOLIC:
185 obj->symbolic = true;
186 break;
187
188 case DT_RPATH:
189 /*
190 * We have to wait until later to process this, because
191 * we might not have gotten the address of the string
192 * table yet.
193 */
194 dyn_rpath = dynp;
195 break;
196
197 case DT_SONAME:
198 /* Not used by the dynamic linker. */
199 break;
200
201 case DT_INIT:
202 init = dynp->d_un.d_ptr;
203 break;
204
205 case DT_FINI:
206 fini = dynp->d_un.d_ptr;
207 break;
208
209 case DT_DEBUG:
210 #ifdef RTLD_LOADER
211 dynp->d_un.d_ptr = (Elf_Addr)&_rtld_debug;
212 #endif
213 break;
214
215 #if defined(__mips__)
216 case DT_MIPS_LOCAL_GOTNO:
217 obj->local_gotno = dynp->d_un.d_val;
218 break;
219
220 case DT_MIPS_SYMTABNO:
221 obj->symtabno = dynp->d_un.d_val;
222 break;
223
224 case DT_MIPS_GOTSYM:
225 obj->gotsym = dynp->d_un.d_val;
226 break;
227
228 case DT_MIPS_RLD_MAP:
229 #ifdef RTLD_LOADER
230 *((Elf_Addr *)(dynp->d_un.d_ptr)) = (Elf_Addr)
231 &_rtld_debug;
232 #endif
233 break;
234 #endif
235 }
236 }
237
238 obj->rellim = (const Elf_Rel *)((caddr_t)obj->rel + relsz);
239 obj->relalim = (const Elf_Rela *)((caddr_t)obj->rela + relasz);
240 if (plttype == DT_REL) {
241 /* On PPC and SPARC, at least, REL(A)SZ may include PLTREL. */
242 if (obj->rellim && obj->pltrel && obj->rellim > obj->pltrel)
243 obj->rellim = obj->pltrel;
244 obj->pltrellim = (const Elf_Rel *)((caddr_t)obj->pltrel + pltrelsz);
245 obj->pltrelalim = 0;
246 } else {
247 /* On PPC and SPARC, at least, REL(A)SZ may include PLTREL. */
248 if (obj->relalim && obj->pltrela && obj->relalim > obj->pltrela)
249 obj->relalim = obj->pltrela;
250 obj->pltrellim = 0;
251 obj->pltrelalim = (const Elf_Rela *)((caddr_t)obj->pltrela + pltrelsz);
252 }
253
254 #if defined(RTLD_LOADER) && defined(__HAVE_FUNCTION_DESCRIPTORS)
255 if (init != 0)
256 obj->init = (void (*) __P((void)))
257 _rtld_function_descriptor_alloc(obj, NULL, init);
258 if (fini != 0)
259 obj->fini = (void (*) __P((void)))
260 _rtld_function_descriptor_alloc(obj, NULL, fini);
261 #else
262 if (init != 0)
263 obj->init = (void (*) __P((void)))
264 (obj->relocbase + init);
265 if (fini != 0)
266 obj->fini = (void (*) __P((void)))
267 (obj->relocbase + fini);
268 #endif
269
270 if (dyn_rpath != NULL) {
271 _rtld_add_paths(&obj->rpaths, obj->strtab +
272 dyn_rpath->d_un.d_val, true);
273 }
274 }
275
276 /*
277 * Process a shared object's program header. This is used only for the
278 * main program, when the kernel has already loaded the main program
279 * into memory before calling the dynamic linker. It creates and
280 * returns an Obj_Entry structure.
281 */
282 Obj_Entry *
283 _rtld_digest_phdr(phdr, phnum, entry)
284 const Elf_Phdr *phdr;
285 int phnum;
286 caddr_t entry;
287 {
288 Obj_Entry *obj;
289 const Elf_Phdr *phlimit = phdr + phnum;
290 const Elf_Phdr *ph;
291 int nsegs = 0;
292
293 obj = _rtld_obj_new();
294 for (ph = phdr; ph < phlimit; ++ph) {
295 switch (ph->p_type) {
296
297 case PT_PHDR:
298 assert((const Elf_Phdr *) ph->p_vaddr == phdr);
299 obj->phdr = (const Elf_Phdr *) ph->p_vaddr;
300 obj->phsize = ph->p_memsz;
301 break;
302
303 case PT_INTERP:
304 obj->interp = (const char *) ph->p_vaddr;
305 break;
306
307 case PT_LOAD:
308 assert(nsegs < 2);
309 if (nsegs == 0) { /* First load segment */
310 obj->vaddrbase = round_down(ph->p_vaddr);
311 obj->mapbase = (caddr_t) obj->vaddrbase;
312 obj->relocbase = obj->mapbase - obj->vaddrbase;
313 obj->textsize = round_up(ph->p_vaddr +
314 ph->p_memsz) - obj->vaddrbase;
315 } else { /* Last load segment */
316 obj->mapsize = round_up(ph->p_vaddr +
317 ph->p_memsz) - obj->vaddrbase;
318 }
319 ++nsegs;
320 break;
321
322 case PT_DYNAMIC:
323 obj->dynamic = (Elf_Dyn *) ph->p_vaddr;
324 break;
325 }
326 }
327 assert(nsegs == 2);
328
329 obj->entry = entry;
330 return obj;
331 }
332