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. */
21 #include <sys/types.h>
25 #include "dynamic-link.h"
29 _dl_relocate_object (struct link_map
*l
, int lazy
)
31 const size_t pagesize
= getpagesize ();
36 if (l
->l_info
[DT_TEXTREL
])
38 /* Bletch. We must make read-only segments writable
39 long enough to relocate them. */
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)
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];
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
])
72 scope
[1] = _dl_loaded
;
76 scope
[0] = _dl_loaded
;
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. */
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
);
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)
110 if (ph
->p_flags
& PF_R
)
112 if (ph
->p_flags
& PF_X
)
114 if (mprotect (mapstart
, mapend
- mapstart
, prot
) < 0)
115 _dl_signal_error (errno
, l
->l_name
,
116 "can't restore segment prot after reloc");