Update.
[glibc.git] / elf / dl-reloc.c
blob931c9f455d041f4c67e8c303407b1fece4632406
1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995,96,97,98,99,2000,2001 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <libintl.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <ldsodefs.h>
25 #include <sys/mman.h>
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include "dynamic-link.h"
30 /* Statistics function. */
31 unsigned long int _dl_num_cache_relocations;
34 void
35 _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
36 int lazy, int consider_profiling)
38 if (l->l_relocated)
39 return;
41 /* If DT_BIND_NOW is set relocate all references in this object. We
42 do not do this if we are profiling, of course. */
43 if (!consider_profiling
44 && __builtin_expect (l->l_info[DT_BIND_NOW] != NULL, 0))
45 lazy = 0;
47 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_RELOC, 0))
48 _dl_printf ("\nrelocation processing: %s%s\n",
49 l->l_name[0] ? l->l_name : _dl_argv[0], lazy ? " (lazy)" : "");
51 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
53 /* Bletch. We must make read-only segments writable
54 long enough to relocate them. */
55 const ElfW(Phdr) *ph;
56 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
57 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
59 caddr_t mapstart = ((caddr_t) l->l_addr +
60 (ph->p_vaddr & ~(_dl_pagesize - 1)));
61 caddr_t mapend = ((caddr_t) l->l_addr +
62 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
63 & ~(_dl_pagesize - 1)));
64 if (__builtin_expect (__mprotect (mapstart, mapend - mapstart,
65 PROT_READ|PROT_WRITE), 0) < 0)
66 _dl_signal_error (errno, l->l_name, NULL, N_("\
67 cannot make segment writable for relocation"));
72 /* Do the actual relocation of the object's GOT and other data. */
74 /* String table object symbols. */
75 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
77 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
78 #define RESOLVE_MAP(ref, version, r_type) \
79 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
80 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
81 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
82 ? (++_dl_num_cache_relocations, \
83 (*ref) = l->l_lookup_cache.ret, \
84 l->l_lookup_cache.value) \
85 : ({ lookup_t _lr; \
86 int _tc = elf_machine_type_class (r_type); \
87 l->l_lookup_cache.type_class = _tc; \
88 l->l_lookup_cache.sym = (*ref); \
89 _lr = ((version) != NULL && (version)->hash != 0 \
90 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, \
91 l, (ref), scope, \
92 (version), _tc, 0) \
93 : _dl_lookup_symbol (strtab + (*ref)->st_name, l, (ref), \
94 scope, _tc, 0)); \
95 l->l_lookup_cache.ret = (*ref); \
96 l->l_lookup_cache.value = _lr; })) \
97 : l)
98 #define RESOLVE(ref, version, r_type) \
99 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
100 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
101 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
102 ? (++_dl_num_cache_relocations, \
103 (*ref) = l->l_lookup_cache.ret, \
104 l->l_lookup_cache.value) \
105 : ({ lookup_t _lr; \
106 int _tc = elf_machine_type_class (r_type); \
107 l->l_lookup_cache.type_class = _tc; \
108 l->l_lookup_cache.sym = (*ref); \
109 _lr = ((version) != NULL && (version)->hash != 0 \
110 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, \
111 l, (ref), scope, \
112 (version), _tc, 0) \
113 : _dl_lookup_symbol (strtab + (*ref)->st_name, l, (ref), \
114 scope, _tc, 0)); \
115 l->l_lookup_cache.ret = (*ref); \
116 l->l_lookup_cache.value = _lr; })) \
117 : l->l_addr)
119 #include "dynamic-link.h"
121 ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
123 if (__builtin_expect (consider_profiling, 0))
125 /* Allocate the array which will contain the already found
126 relocations. If the shared object lacks a PLT (for example
127 if it only contains lead function) the l_info[DT_PLTRELSZ]
128 will be NULL. */
129 if (l->l_info[DT_PLTRELSZ] == NULL)
130 _dl_fatal_printf ("%s: profiler found no PLTREL in object %s\n",
131 _dl_argv[0] ?: "<program name unknown>",
132 l->l_name);
134 l->l_reloc_result =
135 (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
136 l->l_info[DT_PLTRELSZ]->d_un.d_val);
137 if (l->l_reloc_result == NULL)
138 _dl_fatal_printf ("\
139 %s: profiler out of memory shadowing PLTREL of %s\n",
140 _dl_argv[0] ?: "<program name unknown>",
141 l->l_name);
145 /* Mark the object so we know this work has been done. */
146 l->l_relocated = 1;
148 /* DT_TEXTREL is now in level 2 and might phase out at some time.
149 But we rewrite the DT_FLAGS entry to make testing easier and
150 therefore it will be available at all time. */
151 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
153 /* Undo the protection change we made before relocating. */
154 const ElfW(Phdr) *ph;
155 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
156 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
158 caddr_t mapstart = ((caddr_t) l->l_addr +
159 (ph->p_vaddr & ~(_dl_pagesize - 1)));
160 caddr_t mapend = ((caddr_t) l->l_addr +
161 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
162 & ~(_dl_pagesize - 1)));
163 extern unsigned char _dl_pf_to_prot[8];
164 int prot;
166 if ((PF_R | PF_W | PF_X) == 7
167 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7)
168 prot = _dl_pf_to_prot[ph->p_flags & (PF_R | PF_X)];
169 else
171 prot = 0;
172 if (ph->p_flags & PF_R)
173 prot |= PROT_READ;
174 if (ph->p_flags & PF_X)
175 prot |= PROT_EXEC;
178 if (__builtin_expect (__mprotect (mapstart, mapend - mapstart,
179 prot), 0) < 0)
180 _dl_signal_error (errno, l->l_name, NULL,
181 N_("can't restore segment prot after reloc"));
183 #ifdef CLEAR_CACHE
184 CLEAR_CACHE (mapstart, mapend);
185 #endif
190 #include "../stdio-common/_itoa.h"
191 #define DIGIT(b) _itoa_lower_digits[(b) & 0xf];
193 void
194 internal_function
195 _dl_reloc_bad_type (struct link_map *map, uint_fast8_t type, int plt)
197 extern const char _itoa_lower_digits[];
198 if (plt)
200 /* XXX We cannot translate the message. */
201 static char msg[] = "unexpected PLT reloc type 0x??";
202 msg[sizeof msg - 3] = DIGIT(type >> 4);
203 msg[sizeof msg - 2] = DIGIT(type);
204 _dl_signal_error (0, map->l_name, NULL, msg);
206 else
208 /* XXX We cannot translate the message. */
209 static char msg[] = "unexpected reloc type 0x??";
210 msg[sizeof msg - 3] = DIGIT(type >> 4);
211 msg[sizeof msg - 2] = DIGIT(type);
212 _dl_signal_error (0, map->l_name, NULL, msg);