Update.
[platform/upstream/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 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.
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    Lesser General Public License for more details.
14
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.  */
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/param.h>
27 #include <sys/types.h>
28 #include "dynamic-link.h"
29
30 /* Statistics function.  */
31 unsigned long int _dl_num_cache_relocations;
32
33
34 void
35 _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
36                      int lazy, int consider_profiling)
37 {
38   struct textrels
39   {
40     caddr_t start;
41     size_t len;
42     int prot;
43     struct textrels *next;
44   } *textrels = NULL;
45   /* Initialize it to make the compiler happy.  */
46   const char *errstring = NULL;
47
48   if (l->l_relocated)
49     return;
50
51   /* If DT_BIND_NOW is set relocate all references in this object.  We
52      do not do this if we are profiling, of course.  */
53   if (!consider_profiling
54       && __builtin_expect (l->l_info[DT_BIND_NOW] != NULL, 0))
55     lazy = 0;
56
57   if (__builtin_expect (_dl_debug_mask & DL_DEBUG_RELOC, 0))
58     _dl_printf ("\nrelocation processing: %s%s\n",
59                 l->l_name[0] ? l->l_name : _dl_argv[0], lazy ? " (lazy)" : "");
60
61   /* DT_TEXTREL is now in level 2 and might phase out at some time.
62      But we rewrite the DT_FLAGS entry to a DT_TEXTREL entry to make
63      testing easier and therefore it will be available at all time.  */
64   if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
65     {
66       /* Bletch.  We must make read-only segments writable
67          long enough to relocate them.  */
68       const ElfW(Phdr) *ph;
69       for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
70         if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
71           {
72             struct textrels *newp;
73
74             newp = (struct textrels *) alloca (sizeof (*newp));
75             newp->len = (((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
76                           & ~(_dl_pagesize - 1))
77                          - (ph->p_vaddr & ~(_dl_pagesize - 1)));
78             newp->start = ((ph->p_vaddr & ~(_dl_pagesize - 1))
79                            + (caddr_t) l->l_addr);
80
81             if (__mprotect (newp->start, newp->len, PROT_READ|PROT_WRITE) < 0)
82               {
83                 errstring = N_("cannot make segment writable for relocation");
84               call_error:
85                 _dl_signal_error (errno, l->l_name, NULL, errstring);
86               }
87
88 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
89             newp->prot = (PF_TO_PROT
90                           >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
91 #else
92             newp->prot = 0;
93             if (ph->p_flags & PF_R)
94               newp->prot |= PROT_READ;
95             if (ph->p_flags & PF_W)
96               newp->prot |= PROT_WRITE;
97             if (ph->p_flags & PF_X)
98               newp->prot |= PROT_EXEC;
99 #endif
100             newp->next = textrels;
101             textrels = newp;
102           }
103     }
104
105   {
106     /* Do the actual relocation of the object's GOT and other data.  */
107
108     /* String table object symbols.  */
109     const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
110
111     /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code.  */
112 #define RESOLVE_MAP(ref, version, r_type) \
113     (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL                             \
114      ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0)                \
115          && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class)  \
116         ? (++_dl_num_cache_relocations,                                       \
117            (*ref) = l->l_lookup_cache.ret,                                    \
118            l->l_lookup_cache.value)                                           \
119         : ({ lookup_t _lr;                                                    \
120              int _tc = elf_machine_type_class (r_type);                       \
121              l->l_lookup_cache.type_class = _tc;                              \
122              l->l_lookup_cache.sym = (*ref);                                  \
123              _lr = ((version) != NULL && (version)->hash != 0                 \
124                     ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name,  \
125                                                    l, (ref), scope,           \
126                                                    (version), _tc, 0)         \
127                     : _dl_lookup_symbol (strtab + (*ref)->st_name, l, (ref),  \
128                                          scope, _tc, 0));                     \
129              l->l_lookup_cache.ret = (*ref);                                  \
130              l->l_lookup_cache.value = _lr; }))                               \
131      : l)
132 #define RESOLVE(ref, version, r_type) \
133     (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL                             \
134      ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0)                \
135          && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class)  \
136         ? (++_dl_num_cache_relocations,                                       \
137            (*ref) = l->l_lookup_cache.ret,                                    \
138            l->l_lookup_cache.value)                                           \
139         : ({ lookup_t _lr;                                                    \
140              int _tc = elf_machine_type_class (r_type);                       \
141              l->l_lookup_cache.type_class = _tc;                              \
142              l->l_lookup_cache.sym = (*ref);                                  \
143              _lr = ((version) != NULL && (version)->hash != 0                 \
144                     ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name,  \
145                                                    l, (ref), scope,           \
146                                                    (version), _tc, 0)         \
147                     : _dl_lookup_symbol (strtab + (*ref)->st_name, l, (ref),  \
148                                          scope, _tc, 0));                     \
149              l->l_lookup_cache.ret = (*ref);                                  \
150              l->l_lookup_cache.value = _lr; }))                               \
151      : l->l_addr)
152
153 #include "dynamic-link.h"
154
155     ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
156
157     if (__builtin_expect (consider_profiling, 0))
158       {
159         /* Allocate the array which will contain the already found
160            relocations.  If the shared object lacks a PLT (for example
161            if it only contains lead function) the l_info[DT_PLTRELSZ]
162            will be NULL.  */
163         if (l->l_info[DT_PLTRELSZ] == NULL)
164           {
165             errstring = N_("%s: profiler found no PLTREL in object %s\n");
166           fatal:
167             _dl_fatal_printf (errstring,
168                               _dl_argv[0] ?: "<program name unknown>",
169                               l->l_name);
170           }
171
172         l->l_reloc_result =
173           (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
174                                  l->l_info[DT_PLTRELSZ]->d_un.d_val);
175         if (l->l_reloc_result == NULL)
176           {
177             errstring = N_("\
178 %s: profiler out of memory shadowing PLTREL of %s\n");
179             goto fatal;
180           }
181       }
182   }
183
184   /* Mark the object so we know this work has been done.  */
185   l->l_relocated = 1;
186
187   /* Undo the segment protection changes.  */
188   while (__builtin_expect (textrels != NULL, 0))
189     {
190       if (__mprotect (textrels->start, textrels->len, textrels->prot) < 0)
191         {
192           errstring = N_("cannot restore segment prot after reloc");
193           goto call_error;
194         }
195
196       textrels = textrels->next;
197     }
198 }
199
200
201 void
202 internal_function
203 _dl_reloc_bad_type (struct link_map *map, unsigned int type, int plt)
204 {
205   extern const char _itoa_lower_digits[];
206 #define DIGIT(b)        _itoa_lower_digits[(b) & 0xf];
207
208   /* XXX We cannot translate these messages.  */
209   static const char msg[2][32] = { "unexpected reloc type 0x",
210                                    "unexpected PLT reloc type 0x" };
211   char msgbuf[sizeof (msg[0])];
212   char *cp;
213
214   cp = __stpcpy (msgbuf, msg[plt]);
215   *cp++ = DIGIT (type >> 4);
216   *cp = DIGIT (type);
217
218   _dl_signal_error (0, map->l_name, NULL, msgbuf);
219 }