Update.
[glibc.git] / elf / dl-reloc.c
bloba1c235a3984a059f5aa467a20b12cf0ff15ea1ce
1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 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 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.
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.
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. */
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <elf/ldsodefs.h>
24 #include <sys/mman.h>
25 #include <sys/types.h>
26 #include "dynamic-link.h"
29 void
30 _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
31 int lazy, int consider_profiling)
33 if (l->l_relocated)
34 return;
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;
41 if (_dl_debug_reloc)
42 _dl_debug_message (1, "\nrelocation processing: ",
43 l->l_name[0] ? l->l_name : _dl_argv[0],
44 lazy ? " (lazy)\n" : "\n", NULL);
46 if (l->l_info[DT_TEXTREL])
48 /* Bletch. We must make read-only segments writable
49 long enough to relocate them. */
50 const ElfW(Phdr) *ph;
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)
54 caddr_t mapstart = ((caddr_t) l->l_addr +
55 (ph->p_vaddr & ~(_dl_pagesize - 1)));
56 caddr_t mapend = ((caddr_t) l->l_addr +
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)
61 _dl_signal_error (errno, l->l_name,
62 "cannot make segment writable for relocation");
67 /* Do the actual relocation of the object's GOT and other data. */
69 /* String table object symbols. */
70 const char *strtab = (const void *) l->l_info[DT_STRTAB]->d_un.d_ptr;
72 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
73 #define RESOLVE(ref, version, flags) \
74 (ELFW(ST_VISIBILITY) ((*ref)->st_other) != STV_PROTECTED \
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->l_addr)
82 #include "dynamic-link.h"
83 ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
85 if (_dl_profile != NULL)
87 /* Allocate the array which will contain the already found
88 relocations. */
89 l->l_reloc_result =
90 (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
91 l->l_info[DT_PLTRELSZ]->d_un.d_val);
92 if (l->l_reloc_result == NULL)
93 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
94 "cannot allocate memory for profiling", NULL);
98 /* Mark the object so we know this work has been done. */
99 l->l_relocated = 1;
101 /* DT_TEXTREL is now in level 2 and might phase out at some time.
102 But we rewrite the DT_FLAGS entry to make testing easier and
103 therefore it will be available at all time. */
104 if (l->l_info[DT_TEXTREL])
106 /* Undo the protection change we made before relocating. */
107 const ElfW(Phdr) *ph;
108 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
109 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
111 caddr_t mapstart = ((caddr_t) l->l_addr +
112 (ph->p_vaddr & ~(_dl_pagesize - 1)));
113 caddr_t mapend = ((caddr_t) l->l_addr +
114 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
115 & ~(_dl_pagesize - 1)));
116 extern unsigned char _dl_pf_to_prot[8];
117 int prot;
119 if ((PF_R | PF_W | PF_X) == 7
120 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7)
121 prot = _dl_pf_to_prot[ph->p_flags & (PF_R | PF_X)];
122 else
124 prot = 0;
125 if (ph->p_flags & PF_R)
126 prot |= PROT_READ;
127 if (ph->p_flags & PF_X)
128 prot |= PROT_EXEC;
131 if (__mprotect (mapstart, mapend - mapstart, prot) < 0)
132 _dl_signal_error (errno, l->l_name,
133 "can't restore segment prot after reloc");
138 #include "../stdio-common/_itoa.h"
139 #define DIGIT(b) _itoa_lower_digits[(b) & 0xf];
141 void
142 internal_function
143 _dl_reloc_bad_type (struct link_map *map, uint_fast8_t type, int plt)
145 extern const char _itoa_lower_digits[];
146 if (plt)
148 char msg[] = "unexpected reloc type 0x??";
149 msg[sizeof msg - 2] = DIGIT(type >> 8);
150 msg[sizeof msg - 1] = DIGIT(type);
151 _dl_signal_error (0, map->l_name, msg);
153 else
155 char msg[] = "unexpected PLT reloc type 0x??";
156 msg[sizeof msg - 2] = DIGIT(type >> 8);
157 msg[sizeof msg - 1] = DIGIT(type);
158 _dl_signal_error (0, map->l_name, msg);