]> sourceware.org Git - glibc.git/blame - elf/dl-reloc.c
Tue May 30 15:52:32 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / elf / dl-reloc.c
CommitLineData
d66e34cd
RM
1/* Relocate a shared object and resolve its references to other loaded objects.
2Copyright (C) 1995 Free Software Foundation, Inc.
3This file is part of the GNU C Library.
4
5The GNU C Library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10The GNU C Library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with the GNU C Library; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, MA 02139, USA. */
19
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"
26
27
28void
29_dl_relocate_object (struct link_map *l, int lazy)
30{
31 const size_t pagesize = getpagesize ();
32
33 if (l->l_relocated)
34 return;
35
36 if (l->l_info[DT_TEXTREL])
37 {
38 /* Bletch. We must make read-only segments writable
39 long enough to relocate them. */
40 const Elf32_Phdr *ph;
41 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
42 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
43 {
44 caddr_t mapstart = ((caddr_t) l->l_addr +
45 (ph->p_vaddr & ~(pagesize - 1)));
46 caddr_t mapend = ((caddr_t) l->l_addr +
47 ((ph->p_vaddr + ph->p_memsz + pagesize - 1)
48 & ~(pagesize - 1)));
49 if (mprotect (mapstart, mapend - mapstart,
50 PROT_READ|PROT_WRITE) < 0)
421f82e5 51 _dl_signal_error (errno, l->l_name,
d66e34cd
RM
52 "cannot make segment writable for relocation");
53 }
54 }
55
56 {
57 struct link_map *real_next, *scope;
58
59 const char *strtab
60 = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
61
62
63 Elf32_Addr resolve (const Elf32_Sym **ref)
64 {
421f82e5
RM
65 return _dl_lookup_symbol (strtab + (*ref)->st_name, ref, scope,
66 l->l_name);
d66e34cd
RM
67 }
68
69 real_next = l->l_next;
70 if (l->l_info[DT_SYMBOLIC])
71 {
72 l->l_prev->l_next = real_next;
73 l->l_next = _dl_loaded;
74 scope = l;
75 }
76 else
77 scope = _dl_loaded;
78
421f82e5 79 ELF_DYNAMIC_RELOCATE (l, lazy, resolve);
d66e34cd
RM
80
81 /* Restore list frobnication done above for DT_SYMBOLIC. */
82 l->l_next = real_next;
83 l->l_prev->l_next = l;
84 }
85
a1a9d215 86 if (l->l_info[DT_JMPREL] && lazy)
d66e34cd
RM
87 /* Set up the PLT so its unrelocated entries will
88 jump to _dl_runtime_resolve, which will relocate them. */
89 elf_machine_runtime_setup (l);
90
91 l->l_relocated = 1;
92
93 if (l->l_info[DT_TEXTREL])
94 {
95 /* Undo the protection change we made before relocating. */
96 const Elf32_Phdr *ph;
97 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
98 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
99 {
100 caddr_t mapstart = ((caddr_t) l->l_addr +
101 (ph->p_vaddr & ~(pagesize - 1)));
102 caddr_t mapend = ((caddr_t) l->l_addr +
103 ((ph->p_vaddr + ph->p_memsz + pagesize - 1)
104 & ~(pagesize - 1)));
105 int prot = 0;
106 if (ph->p_flags & PF_R)
107 prot |= PROT_READ;
108 if (ph->p_flags & PF_X)
109 prot |= PROT_EXEC;
110 if (mprotect (mapstart, mapend - mapstart, prot) < 0)
421f82e5 111 _dl_signal_error (errno, l->l_name,
d66e34cd
RM
112 "can't restore segment prot after reloc");
113 }
114 }
115
116}
This page took 0.040929 seconds and 5 git commands to generate.