map_object.c revision 1.43 1 1.43 christos /* $NetBSD: map_object.c,v 1.43 2011/08/13 22:25:20 christos Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright 1996 John D. Polstra.
5 1.1 cgd * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 1.24 mycroft * Copyright 2002 Charles M. Hannum <root (at) ihack.net>
7 1.1 cgd * All rights reserved.
8 1.1 cgd *
9 1.1 cgd * Redistribution and use in source and binary forms, with or without
10 1.1 cgd * modification, are permitted provided that the following conditions
11 1.1 cgd * are met:
12 1.1 cgd * 1. Redistributions of source code must retain the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer.
14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer in the
16 1.1 cgd * documentation and/or other materials provided with the distribution.
17 1.1 cgd * 3. All advertising materials mentioning features or use of this software
18 1.1 cgd * must display the following acknowledgement:
19 1.1 cgd * This product includes software developed by John Polstra.
20 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
21 1.1 cgd * derived from this software without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.31 skrll #include <sys/cdefs.h>
36 1.31 skrll #ifndef lint
37 1.43 christos __RCSID("$NetBSD: map_object.c,v 1.43 2011/08/13 22:25:20 christos Exp $");
38 1.31 skrll #endif /* not lint */
39 1.31 skrll
40 1.1 cgd #include <errno.h>
41 1.1 cgd #include <stddef.h>
42 1.10 mycroft #include <stdlib.h>
43 1.1 cgd #include <string.h>
44 1.1 cgd #include <unistd.h>
45 1.10 mycroft #include <sys/stat.h>
46 1.1 cgd #include <sys/types.h>
47 1.1 cgd #include <sys/mman.h>
48 1.1 cgd
49 1.41 skrll #include "debug.h"
50 1.1 cgd #include "rtld.h"
51 1.7 hannken
52 1.30 skrll static int protflags(int); /* Elf flags -> mmap protection */
53 1.1 cgd
54 1.41 skrll #define EA_UNDEF (~(Elf_Addr)0)
55 1.41 skrll
56 1.1 cgd /*
57 1.1 cgd * Map a shared object into memory. The argument is a file descriptor,
58 1.1 cgd * which must be open on the object and positioned at its beginning.
59 1.1 cgd *
60 1.1 cgd * The return value is a pointer to a newly-allocated Obj_Entry structure
61 1.1 cgd * for the shared object. Returns NULL on failure.
62 1.1 cgd */
63 1.1 cgd Obj_Entry *
64 1.34 christos _rtld_map_object(const char *path, int fd, const struct stat *sb)
65 1.1 cgd {
66 1.18 junyoung Obj_Entry *obj;
67 1.18 junyoung Elf_Ehdr *ehdr;
68 1.18 junyoung Elf_Phdr *phdr;
69 1.42 joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
70 1.42 joerg Elf_Phdr *phtls;
71 1.42 joerg #endif
72 1.41 skrll size_t phsize;
73 1.18 junyoung Elf_Phdr *phlimit;
74 1.18 junyoung Elf_Phdr *segs[2];
75 1.18 junyoung int nsegs;
76 1.22 mycroft caddr_t mapbase = MAP_FAILED;
77 1.32 lukem size_t mapsize = 0;
78 1.27 matt int mapflags;
79 1.18 junyoung Elf_Off base_offset;
80 1.28 taca #ifdef MAP_ALIGNED
81 1.27 matt Elf_Addr base_alignment;
82 1.28 taca #endif
83 1.18 junyoung Elf_Addr base_vaddr;
84 1.18 junyoung Elf_Addr base_vlimit;
85 1.18 junyoung Elf_Addr text_vlimit;
86 1.22 mycroft int text_flags;
87 1.18 junyoung caddr_t base_addr;
88 1.18 junyoung Elf_Off data_offset;
89 1.18 junyoung Elf_Addr data_vaddr;
90 1.18 junyoung Elf_Addr data_vlimit;
91 1.22 mycroft int data_flags;
92 1.18 junyoung caddr_t data_addr;
93 1.42 joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
94 1.42 joerg Elf_Addr tls_vaddr = 0; /* Noise GCC */
95 1.42 joerg #endif
96 1.41 skrll Elf_Addr phdr_vaddr;
97 1.41 skrll size_t phdr_memsz;
98 1.18 junyoung caddr_t gap_addr;
99 1.18 junyoung size_t gap_size;
100 1.41 skrll int i;
101 1.1 cgd #ifdef RTLD_LOADER
102 1.18 junyoung Elf_Addr clear_vaddr;
103 1.18 junyoung caddr_t clear_addr;
104 1.18 junyoung size_t nclear;
105 1.1 cgd #endif
106 1.26 fvdl
107 1.38 christos if (sb != NULL && sb->st_size < (off_t)sizeof (Elf_Ehdr)) {
108 1.37 mrg _rtld_error("%s: unrecognized file format1", path);
109 1.26 fvdl return NULL;
110 1.26 fvdl }
111 1.1 cgd
112 1.22 mycroft obj = _rtld_obj_new();
113 1.34 christos obj->path = xstrdup(path);
114 1.25 junyoung obj->pathlen = strlen(path);
115 1.22 mycroft if (sb != NULL) {
116 1.22 mycroft obj->dev = sb->st_dev;
117 1.22 mycroft obj->ino = sb->st_ino;
118 1.22 mycroft }
119 1.22 mycroft
120 1.20 mycroft ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
121 1.16 mycroft (off_t)0);
122 1.36 ad obj->ehdr = ehdr;
123 1.20 mycroft if (ehdr == MAP_FAILED) {
124 1.4 christos _rtld_error("%s: read error: %s", path, xstrerror(errno));
125 1.22 mycroft goto bad;
126 1.4 christos }
127 1.4 christos /* Make sure the file is valid */
128 1.17 junyoung if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
129 1.17 junyoung ehdr->e_ident[EI_CLASS] != ELFCLASS) {
130 1.40 skrll _rtld_error("%s: unrecognized file format2 [%x != %x]", path,
131 1.40 skrll ehdr->e_ident[EI_CLASS], ELFCLASS);
132 1.16 mycroft goto bad;
133 1.4 christos }
134 1.4 christos /* Elf_e_ident includes class */
135 1.17 junyoung if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
136 1.17 junyoung ehdr->e_version != EV_CURRENT ||
137 1.17 junyoung ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
138 1.16 mycroft _rtld_error("%s: unsupported file version", path);
139 1.16 mycroft goto bad;
140 1.16 mycroft }
141 1.17 junyoung if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
142 1.16 mycroft _rtld_error("%s: unsupported file type", path);
143 1.16 mycroft goto bad;
144 1.4 christos }
145 1.17 junyoung switch (ehdr->e_machine) {
146 1.4 christos ELFDEFNNAME(MACHDEP_ID_CASES)
147 1.4 christos default:
148 1.16 mycroft _rtld_error("%s: unsupported machine", path);
149 1.16 mycroft goto bad;
150 1.4 christos }
151 1.4 christos
152 1.4 christos /*
153 1.4 christos * We rely on the program header being in the first page. This is
154 1.4 christos * not strictly required by the ABI specification, but it seems to
155 1.4 christos * always true in practice. And, it simplifies things considerably.
156 1.4 christos */
157 1.17 junyoung assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
158 1.19 junyoung assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
159 1.19 junyoung _rtld_pagesz);
160 1.4 christos
161 1.4 christos /*
162 1.4 christos * Scan the program header entries, and save key information.
163 1.4 christos *
164 1.4 christos * We rely on there being exactly two load segments, text and data,
165 1.4 christos * in that order.
166 1.4 christos */
167 1.17 junyoung phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
168 1.42 joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
169 1.42 joerg phtls = NULL;
170 1.42 joerg #endif
171 1.41 skrll phsize = ehdr->e_phnum * sizeof(phdr[0]);
172 1.41 skrll obj->phdr = NULL;
173 1.41 skrll phdr_vaddr = EA_UNDEF;
174 1.41 skrll phdr_memsz = 0;
175 1.17 junyoung phlimit = phdr + ehdr->e_phnum;
176 1.4 christos nsegs = 0;
177 1.4 christos while (phdr < phlimit) {
178 1.4 christos switch (phdr->p_type) {
179 1.10 mycroft case PT_INTERP:
180 1.37 mrg obj->interp = (void *)(uintptr_t)phdr->p_vaddr;
181 1.41 skrll dbg(("%s: PT_INTERP %p", obj->path, obj->interp));
182 1.10 mycroft break;
183 1.1 cgd
184 1.8 kleink case PT_LOAD:
185 1.12 mycroft if (nsegs < 2)
186 1.12 mycroft segs[nsegs] = phdr;
187 1.4 christos ++nsegs;
188 1.43 christos dbg(("%s: %s %p phsize %zu", obj->path, "PT_LOAD",
189 1.43 christos (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
190 1.4 christos break;
191 1.4 christos
192 1.41 skrll case PT_PHDR:
193 1.41 skrll phdr_vaddr = phdr->p_vaddr;
194 1.41 skrll phdr_memsz = phdr->p_memsz;
195 1.43 christos dbg(("%s: %s %p phsize %zu", obj->path, "PT_PHDR",
196 1.43 christos (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
197 1.41 skrll break;
198 1.41 skrll
199 1.8 kleink case PT_DYNAMIC:
200 1.37 mrg obj->dynamic = (void *)(uintptr_t)phdr->p_vaddr;
201 1.43 christos dbg(("%s: %s %p phsize %zu", obj->path, "PT_DYNAMIC",
202 1.43 christos (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
203 1.4 christos break;
204 1.42 joerg
205 1.42 joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
206 1.42 joerg case PT_TLS:
207 1.42 joerg phtls = phdr;
208 1.43 christos dbg(("%s: %s %p phsize %zu", obj->path, "PT_TLS",
209 1.43 christos (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
210 1.42 joerg break;
211 1.42 joerg #endif
212 1.4 christos }
213 1.1 cgd
214 1.4 christos ++phdr;
215 1.4 christos }
216 1.41 skrll phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
217 1.37 mrg obj->entry = (void *)(uintptr_t)ehdr->e_entry;
218 1.22 mycroft if (!obj->dynamic) {
219 1.12 mycroft _rtld_error("%s: not dynamically linked", path);
220 1.16 mycroft goto bad;
221 1.12 mycroft }
222 1.12 mycroft if (nsegs != 2) {
223 1.12 mycroft _rtld_error("%s: wrong number of segments (%d != 2)", path,
224 1.12 mycroft nsegs);
225 1.16 mycroft goto bad;
226 1.4 christos }
227 1.1 cgd
228 1.4 christos /*
229 1.11 chs * Map the entire address space of the object as a file
230 1.5 thorpej * region to stake out our contiguous region and establish a
231 1.11 chs * base for relocation. We use a file mapping so that
232 1.11 chs * the kernel will give us whatever alignment is appropriate
233 1.11 chs * for the platform we're running on.
234 1.5 thorpej *
235 1.11 chs * We map it using the text protection, map the data segment
236 1.11 chs * into the right place, then map an anon segment for the bss
237 1.11 chs * and unmap the gaps left by padding to alignment.
238 1.5 thorpej */
239 1.11 chs
240 1.27 matt #ifdef MAP_ALIGNED
241 1.27 matt base_alignment = segs[0]->p_align;
242 1.27 matt #endif
243 1.4 christos base_offset = round_down(segs[0]->p_offset);
244 1.4 christos base_vaddr = round_down(segs[0]->p_vaddr);
245 1.4 christos base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
246 1.11 chs text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
247 1.22 mycroft text_flags = protflags(segs[0]->p_flags);
248 1.22 mycroft data_offset = round_down(segs[1]->p_offset);
249 1.22 mycroft data_vaddr = round_down(segs[1]->p_vaddr);
250 1.22 mycroft data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
251 1.22 mycroft data_flags = protflags(segs[1]->p_flags);
252 1.23 mycroft #ifdef RTLD_LOADER
253 1.22 mycroft clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
254 1.23 mycroft #endif
255 1.22 mycroft
256 1.22 mycroft obj->textsize = text_vlimit - base_vaddr;
257 1.22 mycroft obj->vaddrbase = base_vaddr;
258 1.22 mycroft obj->isdynamic = ehdr->e_type == ET_DYN;
259 1.22 mycroft
260 1.42 joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
261 1.42 joerg if (phtls != NULL) {
262 1.42 joerg ++_rtld_tls_dtv_generation;
263 1.42 joerg obj->tlsindex = ++_rtld_tls_max_index;
264 1.42 joerg obj->tlssize = phtls->p_memsz;
265 1.42 joerg obj->tlsalign = phtls->p_align;
266 1.42 joerg obj->tlsinitsize = phtls->p_filesz;
267 1.42 joerg tls_vaddr = phtls->p_vaddr;
268 1.42 joerg }
269 1.42 joerg #endif
270 1.42 joerg
271 1.41 skrll obj->phdr_loaded = false;
272 1.41 skrll for (i = 0; i < nsegs; i++) {
273 1.41 skrll if (phdr_vaddr != EA_UNDEF &&
274 1.41 skrll segs[i]->p_vaddr <= phdr_vaddr &&
275 1.41 skrll segs[i]->p_memsz >= phdr_memsz) {
276 1.41 skrll obj->phdr_loaded = true;
277 1.41 skrll break;
278 1.41 skrll }
279 1.41 skrll if (segs[i]->p_offset <= ehdr->e_phoff &&
280 1.41 skrll segs[i]->p_memsz >= phsize) {
281 1.41 skrll phdr_vaddr = segs[i]->p_vaddr + ehdr->e_phoff;
282 1.41 skrll phdr_memsz = phsize;
283 1.41 skrll obj->phdr_loaded = true;
284 1.41 skrll break;
285 1.41 skrll }
286 1.41 skrll }
287 1.41 skrll if (obj->phdr_loaded) {
288 1.41 skrll obj->phdr = (void *)(uintptr_t)phdr_vaddr;
289 1.41 skrll obj->phsize = phdr_memsz;
290 1.41 skrll } else {
291 1.41 skrll Elf_Phdr *buf;
292 1.41 skrll buf = xmalloc(phsize);
293 1.41 skrll if (buf == NULL) {
294 1.41 skrll _rtld_error("%s: cannot allocate program header", path);
295 1.41 skrll goto bad;
296 1.41 skrll }
297 1.41 skrll memcpy(buf, phdr, phsize);
298 1.41 skrll obj->phdr = buf;
299 1.41 skrll obj->phsize = phsize;
300 1.41 skrll }
301 1.41 skrll dbg(("%s: phdr %p phsize %zu (%s)", obj->path, obj->phdr, obj->phsize,
302 1.41 skrll obj->phdr_loaded ? "loaded" : "allocated"));
303 1.41 skrll
304 1.36 ad /* Unmap header if it overlaps the first load section. */
305 1.36 ad if (base_offset < _rtld_pagesz) {
306 1.36 ad munmap(ehdr, _rtld_pagesz);
307 1.36 ad obj->ehdr = MAP_FAILED;
308 1.36 ad }
309 1.11 chs
310 1.27 matt /*
311 1.27 matt * Calculate log2 of the base section alignment.
312 1.27 matt */
313 1.27 matt mapflags = 0;
314 1.27 matt #ifdef MAP_ALIGNED
315 1.27 matt if (base_alignment > _rtld_pagesz) {
316 1.27 matt unsigned int log2 = 0;
317 1.27 matt for (; base_alignment > 1; base_alignment >>= 1)
318 1.27 matt log2++;
319 1.27 matt mapflags = MAP_ALIGNED(log2);
320 1.27 matt }
321 1.27 matt #endif
322 1.27 matt
323 1.1 cgd #ifdef RTLD_LOADER
324 1.22 mycroft base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr;
325 1.1 cgd #else
326 1.4 christos base_addr = NULL;
327 1.1 cgd #endif
328 1.22 mycroft mapsize = base_vlimit - base_vaddr;
329 1.27 matt mapbase = mmap(base_addr, mapsize, text_flags,
330 1.27 matt mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset);
331 1.5 thorpej if (mapbase == MAP_FAILED) {
332 1.4 christos _rtld_error("mmap of entire address space failed: %s",
333 1.4 christos xstrerror(errno));
334 1.16 mycroft goto bad;
335 1.4 christos }
336 1.11 chs
337 1.4 christos /* Overlay the data segment onto the proper region. */
338 1.4 christos data_addr = mapbase + (data_vaddr - base_vaddr);
339 1.22 mycroft if (mmap(data_addr, data_vlimit - data_vaddr, data_flags,
340 1.22 mycroft MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) ==
341 1.22 mycroft MAP_FAILED) {
342 1.4 christos _rtld_error("mmap of data failed: %s", xstrerror(errno));
343 1.22 mycroft goto bad;
344 1.11 chs }
345 1.11 chs
346 1.11 chs /* Overlay the bss segment onto the proper region. */
347 1.11 chs if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
348 1.22 mycroft data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) ==
349 1.22 mycroft MAP_FAILED) {
350 1.11 chs _rtld_error("mmap of bss failed: %s", xstrerror(errno));
351 1.22 mycroft goto bad;
352 1.4 christos }
353 1.5 thorpej
354 1.5 thorpej /* Unmap the gap between the text and data. */
355 1.22 mycroft gap_addr = mapbase + round_up(text_vlimit - base_vaddr);
356 1.5 thorpej gap_size = data_addr - gap_addr;
357 1.21 mycroft if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) {
358 1.21 mycroft _rtld_error("mprotect of text -> data gap failed: %s",
359 1.5 thorpej xstrerror(errno));
360 1.22 mycroft goto bad;
361 1.5 thorpej }
362 1.5 thorpej
363 1.1 cgd #ifdef RTLD_LOADER
364 1.4 christos /* Clear any BSS in the last page of the data segment. */
365 1.4 christos clear_addr = mapbase + (clear_vaddr - base_vaddr);
366 1.4 christos if ((nclear = data_vlimit - clear_vaddr) > 0)
367 1.4 christos memset(clear_addr, 0, nclear);
368 1.4 christos
369 1.5 thorpej /* Non-file portion of BSS mapped above. */
370 1.1 cgd #endif
371 1.1 cgd
372 1.42 joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
373 1.42 joerg if (phtls != NULL)
374 1.42 joerg obj->tlsinit = mapbase + tls_vaddr;
375 1.42 joerg #endif
376 1.42 joerg
377 1.4 christos obj->mapbase = mapbase;
378 1.4 christos obj->mapsize = mapsize;
379 1.4 christos obj->relocbase = mapbase - base_vaddr;
380 1.10 mycroft
381 1.22 mycroft if (obj->dynamic)
382 1.37 mrg obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->dynamic);
383 1.22 mycroft if (obj->entry)
384 1.37 mrg obj->entry = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->entry);
385 1.22 mycroft if (obj->interp)
386 1.37 mrg obj->interp = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->interp);
387 1.41 skrll if (obj->phdr_loaded)
388 1.41 skrll obj->phdr = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->phdr);
389 1.22 mycroft
390 1.10 mycroft return obj;
391 1.16 mycroft
392 1.16 mycroft bad:
393 1.36 ad if (obj->ehdr != MAP_FAILED)
394 1.36 ad munmap(obj->ehdr, _rtld_pagesz);
395 1.22 mycroft if (mapbase != MAP_FAILED)
396 1.22 mycroft munmap(mapbase, mapsize);
397 1.22 mycroft _rtld_obj_free(obj);
398 1.16 mycroft return NULL;
399 1.10 mycroft }
400 1.10 mycroft
401 1.10 mycroft void
402 1.30 skrll _rtld_obj_free(Obj_Entry *obj)
403 1.10 mycroft {
404 1.10 mycroft Objlist_Entry *elm;
405 1.10 mycroft
406 1.42 joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
407 1.42 joerg if (obj->tls_done)
408 1.42 joerg _rtld_tls_offset_free(obj);
409 1.42 joerg #endif
410 1.35 ad xfree(obj->path);
411 1.10 mycroft while (obj->needed != NULL) {
412 1.10 mycroft Needed_Entry *needed = obj->needed;
413 1.10 mycroft obj->needed = needed->next;
414 1.35 ad xfree(needed);
415 1.10 mycroft }
416 1.13 lukem while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
417 1.13 lukem SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
418 1.35 ad xfree(elm);
419 1.10 mycroft }
420 1.13 lukem while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
421 1.13 lukem SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
422 1.35 ad xfree(elm);
423 1.10 mycroft }
424 1.41 skrll if (!obj->phdr_loaded)
425 1.41 skrll xfree((void *)(uintptr_t)obj->phdr);
426 1.35 ad xfree(obj);
427 1.39 skrll #ifdef COMBRELOC
428 1.39 skrll _rtld_combreloc_reset(obj);
429 1.39 skrll #endif
430 1.10 mycroft }
431 1.10 mycroft
432 1.10 mycroft Obj_Entry *
433 1.10 mycroft _rtld_obj_new(void)
434 1.10 mycroft {
435 1.10 mycroft Obj_Entry *obj;
436 1.10 mycroft
437 1.10 mycroft obj = CNEW(Obj_Entry);
438 1.10 mycroft SIMPLEQ_INIT(&obj->dldags);
439 1.10 mycroft SIMPLEQ_INIT(&obj->dagmembers);
440 1.4 christos return obj;
441 1.1 cgd }
442 1.1 cgd
443 1.1 cgd /*
444 1.1 cgd * Given a set of ELF protection flags, return the corresponding protection
445 1.1 cgd * flags for MMAP.
446 1.1 cgd */
447 1.1 cgd static int
448 1.30 skrll protflags(int elfflags)
449 1.1 cgd {
450 1.4 christos int prot = 0;
451 1.29 simonb
452 1.8 kleink if (elfflags & PF_R)
453 1.4 christos prot |= PROT_READ;
454 1.1 cgd #ifdef RTLD_LOADER
455 1.8 kleink if (elfflags & PF_W)
456 1.4 christos prot |= PROT_WRITE;
457 1.1 cgd #endif
458 1.8 kleink if (elfflags & PF_X)
459 1.4 christos prot |= PROT_EXEC;
460 1.4 christos return prot;
461 1.1 cgd }
462