]> sourceware.org Git - glibc.git/blob - elf/dl-reloc.c
531da9618ac21bc9ee73f4e5eb74542faa22ab30
[glibc.git] / elf / dl-reloc.c
1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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. */
19
20 #include <errno.h>
21 #include <link.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 #include <sys/types.h>
26 #include "dynamic-link.h"
27
28
29 void
30 _dl_relocate_object (struct link_map *l, struct link_map *scope[], int lazy)
31 {
32 if (l->l_relocated)
33 return;
34
35 if (l->l_info[DT_TEXTREL])
36 {
37 /* Bletch. We must make read-only segments writable
38 long enough to relocate them. */
39 const ElfW(Phdr) *ph;
40 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
41 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
42 {
43 caddr_t mapstart = ((caddr_t) l->l_addr +
44 (ph->p_vaddr & ~(_dl_pagesize - 1)));
45 caddr_t mapend = ((caddr_t) l->l_addr +
46 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
47 & ~(_dl_pagesize - 1)));
48 if (__mprotect (mapstart, mapend - mapstart,
49 PROT_READ|PROT_WRITE) < 0)
50 _dl_signal_error (errno, l->l_name,
51 "cannot make segment writable for relocation");
52 }
53 }
54
55 {
56 /* Do the actual relocation of the object's GOT and other data. */
57
58 const char *strtab /* String table object symbols. */
59 = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
60
61 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
62 #define RESOLVE(ref, version, flags) \
63 ((version) != NULL && (version)->hash != 0 \
64 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, (ref), scope, \
65 l->l_name, (version), (flags)) \
66 : _dl_lookup_symbol (strtab + (*ref)->st_name, (ref), scope, \
67 l->l_name, (flags)))
68
69 #include "dynamic-link.h"
70 ELF_DYNAMIC_RELOCATE (l, lazy, 1);
71
72 if (_dl_profile_map == l)
73 {
74 /* Allocate the array which will contain the already found
75 relocations. */
76 l->l_reloc_result =
77 (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
78 l->l_info[DT_PLTRELSZ]->d_un.d_val);
79 if (l->l_reloc_result == NULL)
80 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
81 "cannot allocate memory for profiling", NULL);
82 }
83 }
84
85 /* Mark the object so we know this work has been done. */
86 l->l_relocated = 1;
87
88 if (l->l_info[DT_TEXTREL])
89 {
90 /* Undo the protection change we made before relocating. */
91 const ElfW(Phdr) *ph;
92 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
93 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
94 {
95 caddr_t mapstart = ((caddr_t) l->l_addr +
96 (ph->p_vaddr & ~(_dl_pagesize - 1)));
97 caddr_t mapend = ((caddr_t) l->l_addr +
98 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
99 & ~(_dl_pagesize - 1)));
100 int prot = 0;
101 if (ph->p_flags & PF_R)
102 prot |= PROT_READ;
103 if (ph->p_flags & PF_X)
104 prot |= PROT_EXEC;
105 if (__mprotect (mapstart, mapend - mapstart, prot) < 0)
106 _dl_signal_error (errno, l->l_name,
107 "can't restore segment prot after reloc");
108 }
109 }
110 }
This page took 0.042869 seconds and 4 git commands to generate.