]>
sourceware.org Git - glibc.git/blob - elf/dl-reloc.c
94ffb7175978e11b1527a02bca969f432528de99
1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995 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
*real_next
, *scope
;
60 = ((void *) l
->l_addr
+ l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
);
63 Elf32_Addr
resolve (const Elf32_Sym
**ref
)
65 return _dl_lookup_symbol (strtab
+ (*ref
)->st_name
, ref
, scope
,
69 real_next
= l
->l_next
;
70 if (l
->l_info
[DT_SYMBOLIC
])
72 l
->l_prev
->l_next
= real_next
;
73 l
->l_next
= _dl_loaded
;
79 ELF_DYNAMIC_RELOCATE (l
, lazy
, resolve
);
81 /* Restore list frobnication done above for DT_SYMBOLIC. */
82 l
->l_next
= real_next
;
83 l
->l_prev
->l_next
= l
;
86 if (l
->l_info
[DT_JMPREL
] && ! lazy
)
87 /* Set up the PLT so its unrelocated entries will
88 jump to _dl_runtime_resolve, which will relocate them. */
89 elf_machine_runtime_setup (l
);
93 if (l
->l_info
[DT_TEXTREL
])
95 /* Undo the protection change we made before relocating. */
97 for (ph
= l
->l_phdr
; ph
< &l
->l_phdr
[l
->l_phnum
]; ++ph
)
98 if (ph
->p_type
== PT_LOAD
&& (ph
->p_flags
& PF_W
) == 0)
100 caddr_t mapstart
= ((caddr_t
) l
->l_addr
+
101 (ph
->p_vaddr
& ~(pagesize
- 1)));
102 caddr_t mapend
= ((caddr_t
) l
->l_addr
+
103 ((ph
->p_vaddr
+ ph
->p_memsz
+ pagesize
- 1)
106 if (ph
->p_flags
& PF_R
)
108 if (ph
->p_flags
& PF_X
)
110 if (mprotect (mapstart
, mapend
- mapstart
, prot
) < 0)
111 _dl_signal_error (errno
, l
->l_name
,
112 "can't restore segment prot after reloc");
This page took 0.041387 seconds and 4 git commands to generate.