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