xref: /netbsd-src/libexec/ld.elf_so/headers.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: headers.c,v 1.20 2005/01/09 12:43:23 simonb Exp $	 */
2 
3 /*
4  * Copyright 1996 John D. Polstra.
5  * Copyright 1996 Matt Thomas <matt@3am-software.com>
6  * Copyright 2002 Charles M. Hannum <root@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@polstra.com>.
39  */
40 
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: headers.c,v 1.20 2005/01/09 12:43:23 simonb 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 		/*
196 		 * Don't process DT_DEBUG on MIPS as the dynamic section
197 		 * is mapped read-only. DT_MIPS_RLD_MAP is used instead.
198 		 * XXX: n32/n64 may use DT_DEBUG, not sure yet.
199 		 */
200 #ifndef __mips__
201 		case DT_DEBUG:
202 #ifdef RTLD_LOADER
203 			dynp->d_un.d_ptr = (Elf_Addr)&_rtld_debug;
204 #endif
205 			break;
206 #endif
207 
208 #ifdef __mips__
209 		case DT_MIPS_LOCAL_GOTNO:
210 			obj->local_gotno = dynp->d_un.d_val;
211 			break;
212 
213 		case DT_MIPS_SYMTABNO:
214 			obj->symtabno = dynp->d_un.d_val;
215 			break;
216 
217 		case DT_MIPS_GOTSYM:
218 			obj->gotsym = dynp->d_un.d_val;
219 			break;
220 
221 		case DT_MIPS_RLD_MAP:
222 #ifdef RTLD_LOADER
223 			*((Elf_Addr *)(dynp->d_un.d_ptr)) = (Elf_Addr)
224 			    &_rtld_debug;
225 #endif
226 			break;
227 #endif
228 		}
229 	}
230 
231 	obj->rellim = (const Elf_Rel *)((caddr_t)obj->rel + relsz);
232 	obj->relalim = (const Elf_Rela *)((caddr_t)obj->rela + relasz);
233 	if (plttype == DT_REL) {
234 		obj->pltrel = (const Elf_Rel *)(obj->relocbase + pltrel);
235 		obj->pltrellim = (const Elf_Rel *)(obj->relocbase + pltrel + pltrelsz);
236 		obj->pltrelalim = 0;
237 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
238 		   Trim rel(a)lim to save time later. */
239 		if (obj->rellim && obj->pltrel &&
240 		    obj->rellim > obj->pltrel &&
241 		    obj->rellim <= obj->pltrellim)
242 			obj->rellim = obj->pltrel;
243 	} else if (plttype == DT_RELA) {
244 		obj->pltrela = (const Elf_Rela *)(obj->relocbase + pltrel);
245 		obj->pltrellim = 0;
246 		obj->pltrelalim = (const Elf_Rela *)(obj->relocbase + pltrel + pltrelsz);
247 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
248 		   Trim rel(a)lim to save time later. */
249 		if (obj->relalim && obj->pltrela &&
250 		    obj->relalim > obj->pltrela &&
251 		    obj->relalim <= obj->pltrelalim)
252 			obj->relalim = obj->pltrela;
253 	}
254 
255 #if defined(RTLD_LOADER) && defined(__HAVE_FUNCTION_DESCRIPTORS)
256 	if (init != 0)
257 		obj->init = (void (*)(void))
258 		    _rtld_function_descriptor_alloc(obj, NULL, init);
259 	if (fini != 0)
260 		obj->fini = (void (*)(void))
261 		    _rtld_function_descriptor_alloc(obj, NULL, fini);
262 #else
263 	if (init != 0)
264 		obj->init = (void (*)(void))
265 		    (obj->relocbase + init);
266 	if (fini != 0)
267 		obj->fini = (void (*)(void))
268 		    (obj->relocbase + fini);
269 #endif
270 
271 	if (dyn_rpath != NULL) {
272 		_rtld_add_paths(&obj->rpaths, obj->strtab +
273 		    dyn_rpath->d_un.d_val);
274 	}
275 }
276 
277 /*
278  * Process a shared object's program header.  This is used only for the
279  * main program, when the kernel has already loaded the main program
280  * into memory before calling the dynamic linker.  It creates and
281  * returns an Obj_Entry structure.
282  */
283 Obj_Entry *
284 _rtld_digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry)
285 {
286 	Obj_Entry      *obj;
287 	const Elf_Phdr *phlimit = phdr + phnum;
288 	const Elf_Phdr *ph;
289 	int             nsegs = 0;
290 
291 	obj = _rtld_obj_new();
292 	for (ph = phdr; ph < phlimit; ++ph) {
293 		switch (ph->p_type) {
294 
295 		case PT_PHDR:
296 			assert((const Elf_Phdr *) ph->p_vaddr == phdr);
297 			break;
298 
299 		case PT_INTERP:
300 			obj->interp = (const char *) ph->p_vaddr;
301 			break;
302 
303 		case PT_LOAD:
304 			assert(nsegs < 2);
305 			if (nsegs == 0) {	/* First load segment */
306 				obj->vaddrbase = round_down(ph->p_vaddr);
307 				obj->mapbase = (caddr_t) obj->vaddrbase;
308 				obj->relocbase = obj->mapbase - obj->vaddrbase;
309 				obj->textsize = round_up(ph->p_vaddr +
310 				    ph->p_memsz) - obj->vaddrbase;
311 			} else {		/* Last load segment */
312 				obj->mapsize = round_up(ph->p_vaddr +
313 				    ph->p_memsz) - obj->vaddrbase;
314 			}
315 			++nsegs;
316 			break;
317 
318 		case PT_DYNAMIC:
319 			obj->dynamic = (Elf_Dyn *) ph->p_vaddr;
320 			break;
321 		}
322 	}
323 	assert(nsegs == 2);
324 
325 	obj->entry = entry;
326 	return obj;
327 }
328