headers.c revision 1.4.4.1 1 /* $NetBSD: headers.c,v 1.4.4.1 1999/12/27 18:30:14 wrstuden 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_Word relsz = 0, relasz = 0;
68 Elf_Word pltrelsz = 0, pltrelasz = 0;
69
70 for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; ++dynp) {
71 switch (dynp->d_tag) {
72
73 case DT_REL:
74 obj->rel = (const Elf_Rel *)
75 (obj->relocbase + dynp->d_un.d_ptr);
76 break;
77
78 case DT_RELSZ:
79 relsz = dynp->d_un.d_val;
80 break;
81
82 case DT_RELENT:
83 assert(dynp->d_un.d_val == sizeof(Elf_Rel));
84 break;
85
86 case DT_JMPREL:
87 if (plttype == DT_REL) {
88 obj->pltrel = (const Elf_Rel *)
89 (obj->relocbase + dynp->d_un.d_ptr);
90 } else {
91 obj->pltrela = (const Elf_RelA *)
92 (obj->relocbase + dynp->d_un.d_ptr);
93 }
94 break;
95
96 case DT_PLTRELSZ:
97 if (plttype == DT_REL) {
98 pltrelsz = dynp->d_un.d_val;
99 } else {
100 pltrelasz = dynp->d_un.d_val;
101 }
102 break;
103
104 case DT_RELA:
105 obj->rela = (const Elf_RelA *)
106 (obj->relocbase + dynp->d_un.d_ptr);
107 break;
108
109 case DT_RELASZ:
110 relasz = dynp->d_un.d_val;
111 break;
112
113 case DT_RELAENT:
114 assert(dynp->d_un.d_val == sizeof(Elf_RelA));
115 break;
116
117 case DT_PLTREL:
118 plttype = dynp->d_un.d_val;
119 assert(plttype == DT_REL ||
120 plttype == DT_RELA);
121 if (plttype == DT_RELA) {
122 obj->pltrela = (const Elf_RelA *) obj->pltrel;
123 obj->pltrel = NULL;
124 pltrelasz = pltrelsz;
125 pltrelsz = 0;
126 }
127 break;
128
129 case DT_SYMTAB:
130 obj->symtab = (const Elf_Sym *)
131 (obj->relocbase + dynp->d_un.d_ptr);
132 break;
133
134 case DT_SYMENT:
135 assert(dynp->d_un.d_val == sizeof(Elf_Sym));
136 break;
137
138 case DT_STRTAB:
139 obj->strtab = (const char *)
140 (obj->relocbase + dynp->d_un.d_ptr);
141 break;
142
143 case DT_STRSZ:
144 obj->strsize = dynp->d_un.d_val;
145 break;
146
147 case DT_HASH:
148 {
149 const Elf_Word *hashtab = (const Elf_Word *)
150 (obj->relocbase + dynp->d_un.d_ptr);
151
152 obj->nbuckets = hashtab[0];
153 obj->nchains = hashtab[1];
154 obj->buckets = hashtab + 2;
155 obj->chains = obj->buckets + obj->nbuckets;
156 }
157 break;
158
159 case DT_NEEDED:
160 assert(!obj->rtld);
161 {
162 Needed_Entry *nep = NEW(Needed_Entry);
163
164 nep->name = dynp->d_un.d_val;
165 nep->obj = NULL;
166 nep->next = NULL;
167
168 *needed_tail = nep;
169 needed_tail = &nep->next;
170 }
171 break;
172
173 case DT_PLTGOT:
174 obj->pltgot = (Elf_Addr *)
175 (obj->relocbase + dynp->d_un.d_ptr);
176 break;
177
178 case DT_TEXTREL:
179 obj->textrel = true;
180 break;
181
182 case DT_SYMBOLIC:
183 obj->symbolic = true;
184 break;
185
186 case DT_RPATH:
187 /*
188 * We have to wait until later to process this, because
189 * we might not have gotten the address of the string
190 * table yet.
191 */
192 dyn_rpath = dynp;
193 break;
194
195 case DT_SONAME:
196 /* Not used by the dynamic linker. */
197 break;
198
199 case DT_INIT:
200 obj->init = (void (*) __P((void)))
201 (obj->relocbase + dynp->d_un.d_ptr);
202 break;
203
204 case DT_FINI:
205 obj->fini = (void (*) __P((void)))
206 (obj->relocbase + 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 obj->pltrellim = (const Elf_Rel *)((caddr_t)obj->pltrel + pltrelsz);
241 obj->pltrelalim = (const Elf_RelA *)((caddr_t)obj->pltrela + pltrelasz);
242
243 if (dyn_rpath != NULL) {
244 _rtld_add_paths(&obj->rpaths, obj->strtab +
245 dyn_rpath->d_un.d_val, true);
246 }
247 }
248
249 /*
250 * Process a shared object's program header. This is used only for the
251 * main program, when the kernel has already loaded the main program
252 * into memory before calling the dynamic linker. It creates and
253 * returns an Obj_Entry structure.
254 */
255 Obj_Entry *
256 _rtld_digest_phdr(phdr, phnum, entry)
257 const Elf_Phdr *phdr;
258 int phnum;
259 caddr_t entry;
260 {
261 Obj_Entry *obj;
262 const Elf_Phdr *phlimit = phdr + phnum;
263 const Elf_Phdr *ph;
264 int nsegs = 0;
265
266 obj = _rtld_obj_new();
267 for (ph = phdr; ph < phlimit; ++ph) {
268 switch (ph->p_type) {
269
270 case PT_PHDR:
271 assert((const Elf_Phdr *) ph->p_vaddr == phdr);
272 obj->phdr = (const Elf_Phdr *) ph->p_vaddr;
273 obj->phsize = ph->p_memsz;
274 break;
275
276 case PT_INTERP:
277 obj->interp = (const char *) ph->p_vaddr;
278 break;
279
280 case PT_LOAD:
281 assert(nsegs < 2);
282 if (nsegs == 0) { /* First load segment */
283 obj->vaddrbase = round_down(ph->p_vaddr);
284 obj->mapbase = (caddr_t) obj->vaddrbase;
285 obj->relocbase = obj->mapbase - obj->vaddrbase;
286 obj->textsize = round_up(ph->p_vaddr +
287 ph->p_memsz) - obj->vaddrbase;
288 } else { /* Last load segment */
289 obj->mapsize = round_up(ph->p_vaddr +
290 ph->p_memsz) - obj->vaddrbase;
291 }
292 ++nsegs;
293 break;
294
295 case PT_DYNAMIC:
296 obj->dynamic = (Elf_Dyn *) ph->p_vaddr;
297 break;
298 }
299 }
300 assert(nsegs == 2);
301
302 obj->entry = entry;
303 return obj;
304 }
305