1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995, 1996, 1997, 1998 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. */
23 #include <elf/ldsodefs.h>
25 #include <sys/types.h>
26 #include "dynamic-link.h"
31 _dl_relocate_object (struct link_map *l, struct link_map *scope[], int lazy,
32 int consider_profiling)
38 _dl_debug_message (1, "\nrelocation processing: ",
39 l->l_name[0] ? l->l_name : _dl_argv[0], "\n", NULL);
41 if (l->l_info[DT_TEXTREL])
43 /* Bletch. We must make read-only segments writable
44 long enough to relocate them. */
46 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
47 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
49 caddr_t mapstart = ((caddr_t) l->l_addr +
50 (ph->p_vaddr & ~(_dl_pagesize - 1)));
51 caddr_t mapend = ((caddr_t) l->l_addr +
52 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
53 & ~(_dl_pagesize - 1)));
54 if (__mprotect (mapstart, mapend - mapstart,
55 PROT_READ|PROT_WRITE) < 0)
56 _dl_signal_error (errno, l->l_name,
57 "cannot make segment writable for relocation");
62 /* Do the actual relocation of the object's GOT and other data. */
64 const char *strtab /* String table object symbols. */
65 = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
67 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
68 #define RESOLVE(ref, version, flags) \
69 ((version) != NULL && (version)->hash != 0 \
70 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, (ref), scope, \
71 l->l_name, (version), (flags)) \
72 : _dl_lookup_symbol (strtab + (*ref)->st_name, (ref), scope, \
75 #include "dynamic-link.h"
76 ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
78 if (_dl_profile != NULL)
80 /* Allocate the array which will contain the already found
83 (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
84 l->l_info[DT_PLTRELSZ]->d_un.d_val);
85 if (l->l_reloc_result == NULL)
86 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
87 "cannot allocate memory for profiling", NULL);
91 /* Mark the object so we know this work has been done. */
94 if (l->l_info[DT_TEXTREL])
96 /* Undo the protection change we made before relocating. */
98 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
99 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
101 caddr_t mapstart = ((caddr_t) l->l_addr +
102 (ph->p_vaddr & ~(_dl_pagesize - 1)));
103 caddr_t mapend = ((caddr_t) l->l_addr +
104 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
105 & ~(_dl_pagesize - 1)));
107 if (ph->p_flags & PF_R)
109 if (ph->p_flags & PF_X)
111 if (__mprotect (mapstart, mapend - mapstart, prot) < 0)
112 _dl_signal_error (errno, l->l_name,
113 "can't restore segment prot after reloc");