2 * Copyright 1996-1998 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/param.h>
41 static Elf_Ehdr *get_elf_header(int, const char *);
42 static int convert_prot(int); /* Elf flags -> mmap protection */
43 static int convert_flags(int); /* Elf flags -> mmap flags */
46 * Map a shared object into memory. The "fd" argument is a file descriptor,
47 * which must be open on the object and positioned at its beginning.
48 * The "path" argument is a pathname that is used only for error messages.
50 * The return value is a pointer to a newly-allocated Obj_Entry structure
51 * for the shared object. Returns NULL on failure.
54 map_object(int fd, const char *path, const struct stat *sb)
81 size_t nclear, phsize;
91 hdr = get_elf_header(fd, path);
96 * Scan the program header entries, and save key information.
98 * We expect that the loadable segments are ordered by load address.
100 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
101 phsize = hdr->e_phnum * sizeof (phdr[0]);
102 phlimit = phdr + hdr->e_phnum;
104 phdyn = phinterp = phtls = NULL;
110 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
111 stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W;
112 while (phdr < phlimit) {
113 switch (phdr->p_type) {
120 segs[++nsegs] = phdr;
121 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) {
122 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
129 phdr_vaddr = phdr->p_vaddr;
130 phsize = phdr->p_memsz;
142 stack_flags = phdr->p_flags;
146 relro_page = phdr->p_vaddr;
147 relro_size = phdr->p_memsz;
151 if (phdr->p_offset > PAGE_SIZE ||
152 phdr->p_offset + phdr->p_filesz > PAGE_SIZE)
154 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset;
155 note_end = note_start + phdr->p_filesz;
162 _rtld_error("%s: object is not dynamically-linked", path);
167 _rtld_error("%s: too few PT_LOAD segments", path);
172 * Map the entire address space of the object, to stake out our
173 * contiguous region, and to establish the base address for relocation.
175 base_vaddr = trunc_page(segs[0]->p_vaddr);
176 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
177 mapsize = base_vlimit - base_vaddr;
178 base_addr = (caddr_t) base_vaddr;
180 mapbase = mmap(base_addr, mapsize, PROT_NONE, MAP_ANON | MAP_PRIVATE |
182 if (mapbase == (caddr_t) -1) {
183 _rtld_error("%s: mmap of entire address space failed: %s",
184 path, rtld_strerror(errno));
187 if (base_addr != NULL && mapbase != base_addr) {
188 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
189 path, base_addr, mapbase);
193 for (i = 0; i <= nsegs; i++) {
194 /* Overlay the segment onto the proper region. */
195 data_offset = trunc_page(segs[i]->p_offset);
196 data_vaddr = trunc_page(segs[i]->p_vaddr);
197 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
198 data_addr = mapbase + (data_vaddr - base_vaddr);
199 data_prot = convert_prot(segs[i]->p_flags);
200 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
201 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot,
202 data_flags, fd, data_offset) == (caddr_t) -1) {
203 _rtld_error("%s: mmap of data failed: %s", path,
204 rtld_strerror(errno));
209 if (segs[i]->p_filesz != segs[i]->p_memsz) {
211 /* Clear any BSS in the last page of the segment. */
212 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
213 clear_addr = mapbase + (clear_vaddr - base_vaddr);
214 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
216 if ((nclear = data_vlimit - clear_vaddr) > 0) {
217 /* Make sure the end of the segment is writable */
218 if ((data_prot & PROT_WRITE) == 0 && -1 ==
219 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
220 _rtld_error("%s: mprotect failed: %s", path,
221 rtld_strerror(errno));
225 memset(clear_addr, 0, nclear);
228 * reset the data protection back, enable the segment to be
229 * coredumped since we modified it.
231 if ((data_prot & PROT_WRITE) == 0) {
232 madvise(clear_page, PAGE_SIZE, MADV_CORE);
233 mprotect(clear_page, PAGE_SIZE, data_prot);
237 /* Overlay the BSS segment onto the proper region. */
238 bss_vaddr = data_vlimit;
239 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
240 bss_addr = mapbase + (bss_vaddr - base_vaddr);
241 if (bss_vlimit > bss_vaddr) { /* There is something to do */
242 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
243 data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) {
244 _rtld_error("%s: mmap of bss failed: %s", path,
245 rtld_strerror(errno));
251 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
252 (data_vlimit - data_vaddr + data_offset) >=
253 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
254 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
260 obj->dev = sb->st_dev;
261 obj->ino = sb->st_ino;
263 obj->mapbase = mapbase;
264 obj->mapsize = mapsize;
265 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
267 obj->vaddrbase = base_vaddr;
268 obj->relocbase = mapbase - base_vaddr;
269 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
270 if (hdr->e_entry != 0)
271 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
272 if (phdr_vaddr != 0) {
273 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr);
275 obj->phdr = malloc(phsize);
276 if (obj->phdr == NULL) {
278 _rtld_error("%s: cannot allocate program header", path);
281 memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize);
282 obj->phdr_alloc = true;
284 obj->phsize = phsize;
285 if (phinterp != NULL)
286 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
288 tls_dtv_generation++;
289 obj->tlsindex = ++tls_max_index;
290 obj->tlssize = phtls->p_memsz;
291 obj->tlsalign = phtls->p_align;
292 obj->tlsinitsize = phtls->p_filesz;
293 obj->tlsinit = mapbase + phtls->p_vaddr;
295 obj->stack_flags = stack_flags;
297 obj->relro_page = obj->relocbase + trunc_page(relro_page);
298 obj->relro_size = round_page(relro_size);
300 if (note_start < note_end)
301 digest_notes(obj, note_start, note_end);
302 munmap(hdr, PAGE_SIZE);
306 munmap(mapbase, mapsize);
308 munmap(hdr, PAGE_SIZE);
313 get_elf_header(int fd, const char *path)
317 /* DragonFly mmap does not have MAP_PREFAULT_READ */
318 hdr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
319 if (hdr == (Elf_Ehdr *)MAP_FAILED) {
320 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
324 /* Make sure the file is valid */
326 _rtld_error("%s: invalid file format", path);
329 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
330 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
331 _rtld_error("%s: unsupported file layout", path);
334 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
335 hdr->e_version != EV_CURRENT) {
336 _rtld_error("%s: unsupported file version", path);
339 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
340 _rtld_error("%s: unsupported file type", path);
343 if (hdr->e_machine != ELF_TARG_MACH) {
344 _rtld_error("%s: unsupported machine", path);
349 * We rely on the program header being in the first page. This is
350 * not strictly required by the ABI specification, but it seems to
351 * always true in practice. And, it simplifies things considerably.
353 if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
355 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
358 if (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) >
360 _rtld_error("%s: program header too large", path);
366 munmap(hdr, PAGE_SIZE);
371 obj_free(Obj_Entry *obj)
376 free_tls_offset(obj);
377 while (obj->needed != NULL) {
378 Needed_Entry *needed = obj->needed;
379 obj->needed = needed->next;
382 while (!STAILQ_EMPTY(&obj->names)) {
383 Name_Entry *entry = STAILQ_FIRST(&obj->names);
384 STAILQ_REMOVE_HEAD(&obj->names, link);
387 while (!STAILQ_EMPTY(&obj->dldags)) {
388 elm = STAILQ_FIRST(&obj->dldags);
389 STAILQ_REMOVE_HEAD(&obj->dldags, link);
392 while (!STAILQ_EMPTY(&obj->dagmembers)) {
393 elm = STAILQ_FIRST(&obj->dagmembers);
394 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
399 if (obj->origin_path)
400 free(obj->origin_path);
408 free((void *)obj->phdr);
417 obj = CNEW(Obj_Entry);
418 STAILQ_INIT(&obj->dldags);
419 STAILQ_INIT(&obj->dagmembers);
420 STAILQ_INIT(&obj->names);
425 * Given a set of ELF protection flags, return the corresponding protection
429 convert_prot(int elfflags)
442 convert_flags(int elfflags)
444 int flags = MAP_PRIVATE; /* All mappings are private */
447 * Readonly mappings are marked "MAP_NOCORE", because they can be
448 * reconstructed by a debugger.
450 if (!(elfflags & PF_W))