]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-reloc.c
Update.
[thirdparty/glibc.git] / elf / dl-reloc.c
CommitLineData
d66e34cd 1/* Relocate a shared object and resolve its references to other loaded objects.
a42195db 2 Copyright (C) 1995,96,97,98,99,2000 Free Software Foundation, Inc.
afd4eb37 3 This file is part of the GNU C Library.
d66e34cd 4
afd4eb37
UD
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.
d66e34cd 9
afd4eb37
UD
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.
d66e34cd 14
afd4eb37
UD
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. */
d66e34cd 19
ea7eb7e3 20#include <errno.h>
ea7eb7e3 21#include <stdlib.h>
d66e34cd 22#include <unistd.h>
a42195db 23#include <ldsodefs.h>
ea7eb7e3
UD
24#include <sys/mman.h>
25#include <sys/types.h>
d66e34cd
RM
26#include "dynamic-link.h"
27
3996f34b 28
d66e34cd 29void
be935610
UD
30_dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
31 int lazy, int consider_profiling)
d66e34cd 32{
d66e34cd
RM
33 if (l->l_relocated)
34 return;
35
88187dcc
UD
36 /* If DT_BIND_NOW is set relocate all references in this object. We
37 do not do this if we are profiling, of course. */
38 if (!consider_profiling && l->l_info[DT_BIND_NOW])
39 lazy = 0;
40
466a0ec9 41 if (__builtin_expect (_dl_debug_reloc, 0))
8193034b 42 _dl_debug_message (1, "\nrelocation processing: ",
88187dcc
UD
43 l->l_name[0] ? l->l_name : _dl_argv[0],
44 lazy ? " (lazy)\n" : "\n", NULL);
8193034b 45
dc051821 46 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
d66e34cd
RM
47 {
48 /* Bletch. We must make read-only segments writable
49 long enough to relocate them. */
266180eb 50 const ElfW(Phdr) *ph;
d66e34cd
RM
51 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
52 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
53 {
54 caddr_t mapstart = ((caddr_t) l->l_addr +
266180eb 55 (ph->p_vaddr & ~(_dl_pagesize - 1)));
d66e34cd 56 caddr_t mapend = ((caddr_t) l->l_addr +
266180eb
RM
57 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
58 & ~(_dl_pagesize - 1)));
59 if (__mprotect (mapstart, mapend - mapstart,
60 PROT_READ|PROT_WRITE) < 0)
421f82e5 61 _dl_signal_error (errno, l->l_name,
d66e34cd
RM
62 "cannot make segment writable for relocation");
63 }
64 }
65
66 {
ba79d61b 67 /* Do the actual relocation of the object's GOT and other data. */
d66e34cd 68
f420344c 69 /* String table object symbols. */
a42195db 70 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
d66e34cd 71
f51d1dfd 72 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
c0282c06
UD
73#define RESOLVE_MAP(ref, version, flags) \
74 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
75 ? ((version) != NULL && (version)->hash != 0 \
76 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, l, (ref), \
77 scope, (version), (flags)) \
78 : _dl_lookup_symbol (strtab + (*ref)->st_name, l, (ref), scope, \
79 (flags))) \
80 : l)
c84142e8 81#define RESOLVE(ref, version, flags) \
6aa29abe 82 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
06535ae9
UD
83 ? ((version) != NULL && (version)->hash != 0 \
84 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, l, (ref), \
85 scope, (version), (flags)) \
86 : _dl_lookup_symbol (strtab + (*ref)->st_name, l, (ref), scope, \
87 (flags))) \
88 : l->l_addr)
f51d1dfd
RM
89
90#include "dynamic-link.h"
c0fb8a56 91 ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
ea7eb7e3 92
655ab929 93 if (__builtin_expect (_dl_profile != NULL, 0))
ea7eb7e3
UD
94 {
95 /* Allocate the array which will contain the already found
96 relocations. */
97 l->l_reloc_result =
98 (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
99 l->l_info[DT_PLTRELSZ]->d_un.d_val);
100 if (l->l_reloc_result == NULL)
101 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
102 "cannot allocate memory for profiling", NULL);
103 }
d66e34cd
RM
104 }
105
714a562f 106 /* Mark the object so we know this work has been done. */
d66e34cd
RM
107 l->l_relocated = 1;
108
06535ae9
UD
109 /* DT_TEXTREL is now in level 2 and might phase out at some time.
110 But we rewrite the DT_FLAGS entry to make testing easier and
111 therefore it will be available at all time. */
dc051821 112 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
d66e34cd
RM
113 {
114 /* Undo the protection change we made before relocating. */
266180eb 115 const ElfW(Phdr) *ph;
d66e34cd
RM
116 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
117 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
118 {
119 caddr_t mapstart = ((caddr_t) l->l_addr +
266180eb 120 (ph->p_vaddr & ~(_dl_pagesize - 1)));
d66e34cd 121 caddr_t mapend = ((caddr_t) l->l_addr +
266180eb
RM
122 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
123 & ~(_dl_pagesize - 1)));
24d60840
UD
124 extern unsigned char _dl_pf_to_prot[8];
125 int prot;
126
127 if ((PF_R | PF_W | PF_X) == 7
128 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7)
129 prot = _dl_pf_to_prot[ph->p_flags & (PF_R | PF_X)];
130 else
131 {
132 prot = 0;
133 if (ph->p_flags & PF_R)
134 prot |= PROT_READ;
135 if (ph->p_flags & PF_X)
136 prot |= PROT_EXEC;
137 }
138
266180eb 139 if (__mprotect (mapstart, mapend - mapstart, prot) < 0)
421f82e5 140 _dl_signal_error (errno, l->l_name,
d66e34cd 141 "can't restore segment prot after reloc");
63a34b0f
UD
142
143#ifdef CLEAR_CACHE
144 CLEAR_CACHE (mapstart, mapend);
736d0841 145#endif
d66e34cd
RM
146 }
147 }
d66e34cd 148}
421c80d2
RM
149
150#include "../stdio-common/_itoa.h"
151#define DIGIT(b) _itoa_lower_digits[(b) & 0xf];
152
153void
154internal_function
155_dl_reloc_bad_type (struct link_map *map, uint_fast8_t type, int plt)
156{
157 extern const char _itoa_lower_digits[];
158 if (plt)
159 {
b227a3a6 160 char msg[] = "unexpected PLT reloc type 0x??";
5d693ae3 161 msg[sizeof msg - 3] = DIGIT(type >> 4);
676d12fd 162 msg[sizeof msg - 2] = DIGIT(type);
421c80d2
RM
163 _dl_signal_error (0, map->l_name, msg);
164 }
165 else
166 {
b227a3a6 167 char msg[] = "unexpected reloc type 0x??";
5d693ae3 168 msg[sizeof msg - 3] = DIGIT(type >> 4);
676d12fd 169 msg[sizeof msg - 2] = DIGIT(type);
421c80d2
RM
170 _dl_signal_error (0, map->l_name, msg);
171 }
172}