]> sourceware.org Git - glibc.git/blame - elf/dl-reloc.c
Update.
[glibc.git] / elf / dl-reloc.c
CommitLineData
d66e34cd 1/* Relocate a shared object and resolve its references to other loaded objects.
ea7eb7e3 2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
afd4eb37 3 This file is part of the GNU C Library.
d66e34cd 4
afd4eb37
UD
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.
d66e34cd 9
afd4eb37
UD
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.
d66e34cd 14
afd4eb37
UD
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. */
d66e34cd 19
ea7eb7e3 20#include <errno.h>
d66e34cd 21#include <link.h>
ea7eb7e3 22#include <stdlib.h>
d66e34cd 23#include <unistd.h>
ea7eb7e3
UD
24#include <sys/mman.h>
25#include <sys/types.h>
d66e34cd
RM
26#include "dynamic-link.h"
27
3996f34b 28
d66e34cd 29void
ba79d61b 30_dl_relocate_object (struct link_map *l, struct link_map *scope[], int lazy)
d66e34cd 31{
d66e34cd
RM
32 if (l->l_relocated)
33 return;
34
8193034b
UD
35 if (_dl_debug_reloc)
36 _dl_debug_message (1, "\nrelocation processing: ",
37 l->l_name[0] ? l->l_name : _dl_argv[0], "\n", NULL);
38
d66e34cd
RM
39 if (l->l_info[DT_TEXTREL])
40 {
41 /* Bletch. We must make read-only segments writable
42 long enough to relocate them. */
266180eb 43 const ElfW(Phdr) *ph;
d66e34cd
RM
44 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
45 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
46 {
47 caddr_t mapstart = ((caddr_t) l->l_addr +
266180eb 48 (ph->p_vaddr & ~(_dl_pagesize - 1)));
d66e34cd 49 caddr_t mapend = ((caddr_t) l->l_addr +
266180eb
RM
50 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
51 & ~(_dl_pagesize - 1)));
52 if (__mprotect (mapstart, mapend - mapstart,
53 PROT_READ|PROT_WRITE) < 0)
421f82e5 54 _dl_signal_error (errno, l->l_name,
d66e34cd
RM
55 "cannot make segment writable for relocation");
56 }
57 }
58
59 {
ba79d61b 60 /* Do the actual relocation of the object's GOT and other data. */
d66e34cd 61
ba79d61b 62 const char *strtab /* String table object symbols. */
d66e34cd 63 = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
d66e34cd 64
f51d1dfd 65 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
c84142e8 66#define RESOLVE(ref, version, flags) \
bc9f6000 67 ((version) != NULL && (version)->hash != 0 \
c84142e8
UD
68 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, (ref), scope, \
69 l->l_name, (version), (flags)) \
70 : _dl_lookup_symbol (strtab + (*ref)->st_name, (ref), scope, \
71 l->l_name, (flags)))
f51d1dfd
RM
72
73#include "dynamic-link.h"
3996f34b 74 ELF_DYNAMIC_RELOCATE (l, lazy, 1);
ea7eb7e3
UD
75
76 if (_dl_profile_map == l)
77 {
78 /* Allocate the array which will contain the already found
79 relocations. */
80 l->l_reloc_result =
81 (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
82 l->l_info[DT_PLTRELSZ]->d_un.d_val);
83 if (l->l_reloc_result == NULL)
84 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
85 "cannot allocate memory for profiling", NULL);
86 }
d66e34cd
RM
87 }
88
714a562f 89 /* Mark the object so we know this work has been done. */
d66e34cd
RM
90 l->l_relocated = 1;
91
92 if (l->l_info[DT_TEXTREL])
93 {
94 /* Undo the protection change we made before relocating. */
266180eb 95 const ElfW(Phdr) *ph;
d66e34cd
RM
96 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
97 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
98 {
99 caddr_t mapstart = ((caddr_t) l->l_addr +
266180eb 100 (ph->p_vaddr & ~(_dl_pagesize - 1)));
d66e34cd 101 caddr_t mapend = ((caddr_t) l->l_addr +
266180eb
RM
102 ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1)
103 & ~(_dl_pagesize - 1)));
d66e34cd
RM
104 int prot = 0;
105 if (ph->p_flags & PF_R)
106 prot |= PROT_READ;
107 if (ph->p_flags & PF_X)
108 prot |= PROT_EXEC;
266180eb 109 if (__mprotect (mapstart, mapend - mapstart, prot) < 0)
421f82e5 110 _dl_signal_error (errno, l->l_name,
d66e34cd
RM
111 "can't restore segment prot after reloc");
112 }
113 }
d66e34cd 114}
This page took 0.061933 seconds and 5 git commands to generate.