]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-reloc.c
Replace _dl_debug_* variables with _dl_debug_mask.
[thirdparty/glibc.git] / elf / dl-reloc.c
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.
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 <libintl.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <ldsodefs.h>
25 #include <sys/mman.h>
26 #include <sys/types.h>
27 #include "dynamic-link.h"
28
29
30 void
31 _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
32 int lazy, int consider_profiling)
33 {
34 if (l->l_relocated)
35 return;
36
37 /* If DT_BIND_NOW is set relocate all references in this object. We
38 do not do this if we are profiling, of course. */
39 if (!consider_profiling
40 && __builtin_expect (l->l_info[DT_BIND_NOW] != NULL, 0))
41 lazy = 0;
42
43 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_RELOC, 0))
44 _dl_debug_message (1, "\nrelocation processing: ",
45 l->l_name[0] ? l->l_name : _dl_argv[0],
46 lazy ? " (lazy)\n" : "\n", NULL);
47
48 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
49 {
50 /* Bletch. We must make read-only segments writable
51 long enough to relocate them. */
52 const ElfW(Phdr) *ph;
53 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
54 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
55 {
56 caddr_t mapstart = ((caddr_t) l->l_addr +
57 (ph->p_vaddr & ~(_dl_pagesize - 1)));
58 caddr_t mapend = ((caddr_t) l->l_addr +
59 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
60 & ~(_dl_pagesize - 1)));
61 if (__builtin_expect (__mprotect (mapstart, mapend - mapstart,
62 PROT_READ|PROT_WRITE), 0) < 0)
63 _dl_signal_error (errno, l->l_name, N_("\
64 cannot make segment writable for relocation"));
65 }
66 }
67
68 {
69 /* Do the actual relocation of the object's GOT and other data. */
70
71 /* String table object symbols. */
72 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
73
74 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
75 #define RESOLVE_MAP(ref, version, flags) \
76 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
77 ? ((version) != NULL && (version)->hash != 0 \
78 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, l, (ref), \
79 scope, (version), (flags), 0) \
80 : _dl_lookup_symbol (strtab + (*ref)->st_name, l, (ref), scope, \
81 (flags), 0)) \
82 : l)
83 #define RESOLVE(ref, version, flags) \
84 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
85 ? ((version) != NULL && (version)->hash != 0 \
86 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, l, (ref), \
87 scope, (version), (flags), 0) \
88 : _dl_lookup_symbol (strtab + (*ref)->st_name, l, (ref), scope, \
89 (flags), 0)) \
90 : l->l_addr)
91
92 #include "dynamic-link.h"
93 ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
94
95 if (__builtin_expect (_dl_profile != NULL, 0))
96 {
97 /* Allocate the array which will contain the already found
98 relocations. If the shared object lacks a PLT (for example
99 if it only contains lead function) the l_info[DT_PLTRELSZ]
100 will be NULL. */
101 if (l->l_info[DT_PLTRELSZ] == NULL)
102 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
103 ": profiler found no PLTREL in object ",
104 l->l_name, "\n", NULL);
105
106 l->l_reloc_result =
107 (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
108 l->l_info[DT_PLTRELSZ]->d_un.d_val);
109 if (l->l_reloc_result == NULL)
110 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
111 ": profiler out of memory shadowing PLTREL of ",
112 l->l_name, "\n", NULL);
113 }
114 }
115
116 /* Mark the object so we know this work has been done. */
117 l->l_relocated = 1;
118
119 /* DT_TEXTREL is now in level 2 and might phase out at some time.
120 But we rewrite the DT_FLAGS entry to make testing easier and
121 therefore it will be available at all time. */
122 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
123 {
124 /* Undo the protection change we made before relocating. */
125 const ElfW(Phdr) *ph;
126 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
127 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
128 {
129 caddr_t mapstart = ((caddr_t) l->l_addr +
130 (ph->p_vaddr & ~(_dl_pagesize - 1)));
131 caddr_t mapend = ((caddr_t) l->l_addr +
132 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
133 & ~(_dl_pagesize - 1)));
134 extern unsigned char _dl_pf_to_prot[8];
135 int prot;
136
137 if ((PF_R | PF_W | PF_X) == 7
138 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7)
139 prot = _dl_pf_to_prot[ph->p_flags & (PF_R | PF_X)];
140 else
141 {
142 prot = 0;
143 if (ph->p_flags & PF_R)
144 prot |= PROT_READ;
145 if (ph->p_flags & PF_X)
146 prot |= PROT_EXEC;
147 }
148
149 if (__builtin_expect (__mprotect (mapstart, mapend - mapstart,
150 prot), 0) < 0)
151 _dl_signal_error (errno, l->l_name,
152 N_("can't restore segment prot after reloc"));
153
154 #ifdef CLEAR_CACHE
155 CLEAR_CACHE (mapstart, mapend);
156 #endif
157 }
158 }
159 }
160
161 #include "../stdio-common/_itoa.h"
162 #define DIGIT(b) _itoa_lower_digits[(b) & 0xf];
163
164 void
165 internal_function
166 _dl_reloc_bad_type (struct link_map *map, uint_fast8_t type, int plt)
167 {
168 extern const char _itoa_lower_digits[];
169 if (plt)
170 {
171 /* XXX We cannot translate the message. */
172 static char msg[] = "unexpected PLT reloc type 0x??";
173 msg[sizeof msg - 3] = DIGIT(type >> 4);
174 msg[sizeof msg - 2] = DIGIT(type);
175 _dl_signal_error (0, map->l_name, msg);
176 }
177 else
178 {
179 /* XXX We cannot translate the message. */
180 static char msg[] = "unexpected reloc type 0x??";
181 msg[sizeof msg - 3] = DIGIT(type >> 4);
182 msg[sizeof msg - 2] = DIGIT(type);
183 _dl_signal_error (0, map->l_name, msg);
184 }
185 }