Sat Jun 22 21:29:52 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / elf / dl-reloc.c
blobe6778e7de3cd18d5484b3170efaef26c4c975c88
1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <link.h>
21 #include <sys/types.h>
22 #include <sys/mman.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include "dynamic-link.h"
28 void
29 _dl_relocate_object (struct link_map *l, struct link_map *scope[], int lazy)
31 if (l->l_relocated)
32 return;
34 if (l->l_info[DT_TEXTREL])
36 /* Bletch. We must make read-only segments writable
37 long enough to relocate them. */
38 const ElfW(Phdr) *ph;
39 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
40 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
42 caddr_t mapstart = ((caddr_t) l->l_addr +
43 (ph->p_vaddr & ~(_dl_pagesize - 1)));
44 caddr_t mapend = ((caddr_t) l->l_addr +
45 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
46 & ~(_dl_pagesize - 1)));
47 if (__mprotect (mapstart, mapend - mapstart,
48 PROT_READ|PROT_WRITE) < 0)
49 _dl_signal_error (errno, l->l_name,
50 "cannot make segment writable for relocation");
55 /* Do the actual relocation of the object's GOT and other data. */
57 const char *strtab /* String table object symbols. */
58 = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
59 ElfW(Addr) resolve (const ElfW(Sym) **ref,
60 ElfW(Addr) reloc_addr, int noplt)
62 /* Look up the referenced symbol in the specified scope. */
63 return _dl_lookup_symbol (strtab + (*ref)->st_name, ref, scope,
64 l->l_name, reloc_addr, noplt);
67 ELF_DYNAMIC_RELOCATE (l, lazy, resolve);
70 /* Set up the PLT so its unrelocated entries will jump to
71 _dl_runtime_resolve (dl-runtime.c), which will relocate them. */
72 elf_machine_runtime_setup (l, lazy);
74 /* Mark the object so we know ths work has been done. */
75 l->l_relocated = 1;
77 if (l->l_info[DT_TEXTREL])
79 /* Undo the protection change we made before relocating. */
80 const ElfW(Phdr) *ph;
81 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
82 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
84 caddr_t mapstart = ((caddr_t) l->l_addr +
85 (ph->p_vaddr & ~(_dl_pagesize - 1)));
86 caddr_t mapend = ((caddr_t) l->l_addr +
87 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
88 & ~(_dl_pagesize - 1)));
89 int prot = 0;
90 if (ph->p_flags & PF_R)
91 prot |= PROT_READ;
92 if (ph->p_flags & PF_X)
93 prot |= PROT_EXEC;
94 if (__mprotect (mapstart, mapend - mapstart, prot) < 0)
95 _dl_signal_error (errno, l->l_name,
96 "can't restore segment prot after reloc");