* sysdeps/libm-i387/s_finite.S: Use %eax instead of %al in setnel insn.
[glibc.git] / elf / dl-reloc.c
blobbfa0174444cce1aa0933d46767b987eef312c683
1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995, 1996 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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <link.h>
21 #include <sys/types.h>
22 #include <sys/mman.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include "dynamic-link.h"
28 void
29 _dl_relocate_object (struct link_map *l, int lazy)
31 const size_t pagesize = getpagesize ();
33 if (l->l_relocated)
34 return;
36 if (l->l_info[DT_TEXTREL])
38 /* Bletch. We must make read-only segments writable
39 long enough to relocate them. */
40 const Elf32_Phdr *ph;
41 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
42 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
44 caddr_t mapstart = ((caddr_t) l->l_addr +
45 (ph->p_vaddr & ~(pagesize - 1)));
46 caddr_t mapend = ((caddr_t) l->l_addr +
47 ((ph->p_vaddr + ph->p_memsz + pagesize - 1)
48 & ~(pagesize - 1)));
49 if (mprotect (mapstart, mapend - mapstart,
50 PROT_READ|PROT_WRITE) < 0)
51 _dl_signal_error (errno, l->l_name,
52 "cannot make segment writable for relocation");
57 struct link_map *scope[2];
59 const char *strtab
60 = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
62 Elf32_Addr resolve (const Elf32_Sym **ref,
63 Elf32_Addr reloc_addr, int noplt)
65 return _dl_lookup_symbol (strtab + (*ref)->st_name, ref, scope,
66 l->l_name, reloc_addr, noplt);
69 if (l->l_info[DT_SYMBOLIC])
71 scope[0] = l;
72 scope[1] = _dl_loaded;
74 else
76 scope[0] = _dl_loaded;
77 scope[1] = l;
80 if (l->l_type == lt_interpreter)
81 /* We cannot be lazy when relocating the dynamic linker itself. It
82 was previously relocated eagerly (allowing us to be running now),
83 and needs always to be fully relocated so it can run without the
84 aid of run-time fixups (because it's the one to do them), so we
85 must always re-relocate its PLT eagerly. */
86 lazy = 0;
88 ELF_DYNAMIC_RELOCATE (l, lazy, resolve);
91 /* Set up the PLT so its unrelocated entries will
92 jump to _dl_runtime_resolve, which will relocate them. */
93 elf_machine_runtime_setup (l, lazy);
95 l->l_relocated = 1;
97 if (l->l_info[DT_TEXTREL])
99 /* Undo the protection change we made before relocating. */
100 const Elf32_Phdr *ph;
101 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
102 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
104 caddr_t mapstart = ((caddr_t) l->l_addr +
105 (ph->p_vaddr & ~(pagesize - 1)));
106 caddr_t mapend = ((caddr_t) l->l_addr +
107 ((ph->p_vaddr + ph->p_memsz + pagesize - 1)
108 & ~(pagesize - 1)));
109 int prot = 0;
110 if (ph->p_flags & PF_R)
111 prot |= PROT_READ;
112 if (ph->p_flags & PF_X)
113 prot |= PROT_EXEC;
114 if (mprotect (mapstart, mapend - mapstart, prot) < 0)
115 _dl_signal_error (errno, l->l_name,
116 "can't restore segment prot after reloc");