Update to 2.1.x development version
[glibc.git] / elf / dl-reloc.c
blobf1c43ea174490f1a67b90ee493e028491094a69b
1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995, 1996, 1997 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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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"
27 void
28 _dl_relocate_object (struct link_map *l, struct link_map *scope[], int lazy)
30 if (l->l_relocated)
31 return;
33 if (l->l_info[DT_TEXTREL])
35 /* Bletch. We must make read-only segments writable
36 long enough to relocate them. */
37 const ElfW(Phdr) *ph;
38 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
39 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
41 caddr_t mapstart = ((caddr_t) l->l_addr +
42 (ph->p_vaddr & ~(_dl_pagesize - 1)));
43 caddr_t mapend = ((caddr_t) l->l_addr +
44 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
45 & ~(_dl_pagesize - 1)));
46 if (__mprotect (mapstart, mapend - mapstart,
47 PROT_READ|PROT_WRITE) < 0)
48 _dl_signal_error (errno, l->l_name,
49 "cannot make segment writable for relocation");
54 /* Do the actual relocation of the object's GOT and other data. */
56 const char *strtab /* String table object symbols. */
57 = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
59 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
60 #define RESOLVE(ref, version, flags) \
61 ((version) != NULL && (version)->hash != 0 \
62 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, (ref), scope, \
63 l->l_name, (version), (flags)) \
64 : _dl_lookup_symbol (strtab + (*ref)->st_name, (ref), scope, \
65 l->l_name, (flags)))
67 #include "dynamic-link.h"
68 ELF_DYNAMIC_RELOCATE (l, lazy);
71 /* Set up the PLT so its unrelocated entries will jump to
72 _dl_runtime_resolve (dl-runtime.c), which will relocate them. */
73 elf_machine_runtime_setup (l, lazy);
75 /* Mark the object so we know ths work has been done. */
76 l->l_relocated = 1;
78 if (l->l_info[DT_TEXTREL])
80 /* Undo the protection change we made before relocating. */
81 const ElfW(Phdr) *ph;
82 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
83 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
85 caddr_t mapstart = ((caddr_t) l->l_addr +
86 (ph->p_vaddr & ~(_dl_pagesize - 1)));
87 caddr_t mapend = ((caddr_t) l->l_addr +
88 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
89 & ~(_dl_pagesize - 1)));
90 int prot = 0;
91 if (ph->p_flags & PF_R)
92 prot |= PROT_READ;
93 if (ph->p_flags & PF_X)
94 prot |= PROT_EXEC;
95 if (__mprotect (mapstart, mapend - mapstart, prot) < 0)
96 _dl_signal_error (errno, l->l_name,
97 "can't restore segment prot after reloc");