1 /* $NetBSD: reloc.c,v 1.103 2010/12/24 12:41:43 skrll Exp $ */
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by John Polstra.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Dynamic linker for ELF.
37 * John Polstra <jdp@polstra.com>.
40 #include <sys/cdefs.h>
42 __RCSID("$NetBSD: reloc.c,v 1.103 2010/12/24 12:41:43 skrll Exp $");
53 #include <sys/types.h>
55 #include <sys/bitops.h>
61 #ifndef RTLD_INHIBIT_COPY_RELOCS
62 static int _rtld_do_copy_relocation(const Obj_Entry
*, const Elf_Rela
*);
65 _rtld_do_copy_relocation(const Obj_Entry
*dstobj
, const Elf_Rela
*rela
)
67 void *dstaddr
= (void *)(dstobj
->relocbase
+ rela
->r_offset
);
68 const Elf_Sym
*dstsym
= dstobj
->symtab
+ ELF_R_SYM(rela
->r_info
);
69 const char *name
= dstobj
->strtab
+ dstsym
->st_name
;
70 unsigned long hash
= _rtld_elf_hash(name
);
71 size_t size
= dstsym
->st_size
;
73 const Elf_Sym
*srcsym
= NULL
;
76 for (srcobj
= dstobj
->next
; srcobj
!= NULL
; srcobj
= srcobj
->next
)
77 if ((srcsym
= _rtld_symlook_obj(name
, hash
, srcobj
, false)) != NULL
)
81 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
82 " relocation in %s", name
, dstobj
->path
);
85 srcaddr
= (const void *)(srcobj
->relocbase
+ srcsym
->st_value
);
86 (void)memcpy(dstaddr
, srcaddr
, size
);
87 rdbg(("COPY %s %s %s --> src=%p dst=%p size %ld",
88 dstobj
->path
, srcobj
->path
, name
, srcaddr
,
89 (void *)dstaddr
, (long)size
));
92 #endif /* RTLD_INHIBIT_COPY_RELOCS */
96 * Process the special R_xxx_COPY relocations in the main program. These
97 * copy data from a shared object into a region in the main program's BSS
100 * Returns 0 on success, -1 on failure.
103 _rtld_do_copy_relocations(const Obj_Entry
*dstobj
)
105 #ifndef RTLD_INHIBIT_COPY_RELOCS
107 /* COPY relocations are invalid elsewhere */
108 assert(!dstobj
->isdynamic
);
110 if (dstobj
->rel
!= NULL
) {
112 for (rel
= dstobj
->rel
; rel
< dstobj
->rellim
; ++rel
) {
113 if (ELF_R_TYPE(rel
->r_info
) == R_TYPE(COPY
)) {
115 ourrela
.r_info
= rel
->r_info
;
116 ourrela
.r_offset
= rel
->r_offset
;
117 ourrela
.r_addend
= 0;
118 if (_rtld_do_copy_relocation(dstobj
,
124 if (dstobj
->rela
!= NULL
) {
125 const Elf_Rela
*rela
;
126 for (rela
= dstobj
->rela
; rela
< dstobj
->relalim
; ++rela
) {
127 if (ELF_R_TYPE(rela
->r_info
) == R_TYPE(COPY
)) {
128 if (_rtld_do_copy_relocation(dstobj
, rela
) < 0)
133 #endif /* RTLD_INHIBIT_COPY_RELOCS */
139 * Relocate newly-loaded shared objects. The argument is a pointer to
140 * the Obj_Entry for the first such object. All objects from the first
141 * to the end of the list of objects are relocated. Returns 0 on success,
145 _rtld_relocate_objects(Obj_Entry
*first
, bool bind_now
)
150 for (obj
= first
; obj
!= NULL
; obj
= obj
->next
) {
151 if (obj
->nbuckets
== 0 || obj
->nchains
== 0 ||
152 obj
->buckets
== NULL
|| obj
->symtab
== NULL
||
153 obj
->strtab
== NULL
) {
154 _rtld_error("%s: Shared object has no run-time"
155 " symbol table", obj
->path
);
158 if (obj
->nbuckets
== UINT32_MAX
) {
159 _rtld_error("%s: Symbol table too large", obj
->path
);
162 rdbg((" relocating %s (%ld/%ld rel/rela, %ld/%ld plt rel/rela)",
164 (long)(obj
->rellim
- obj
->rel
),
165 (long)(obj
->relalim
- obj
->rela
),
166 (long)(obj
->pltrellim
- obj
->pltrel
),
167 (long)(obj
->pltrelalim
- obj
->pltrela
)));
172 * There are relocations to the write-protected text
175 if (mprotect(obj
->mapbase
, obj
->textsize
,
176 PROT_READ
| PROT_WRITE
| PROT_EXEC
) == -1) {
177 _rtld_error("%s: Cannot write-enable text "
178 "segment: %s", obj
->path
, xstrerror(errno
));
184 dbg(("doing non-PLT relocations"));
185 if (_rtld_relocate_nonplt_objects(obj
) < 0)
189 if (obj
->textrel
) { /* Re-protected the text segment. */
190 if (mprotect(obj
->mapbase
, obj
->textsize
,
191 PROT_READ
| PROT_EXEC
) == -1) {
192 _rtld_error("%s: Cannot write-protect text "
193 "segment: %s", obj
->path
, xstrerror(errno
));
199 dbg(("doing lazy PLT binding"));
200 if (_rtld_relocate_plt_lazy(obj
) < 0)
202 #if defined(__hppa__)
205 if (obj
->z_now
|| bind_now
) {
206 dbg(("doing immediate PLT binding"));
207 if (_rtld_relocate_plt_objects(obj
) < 0)
213 /* Set some sanity-checking numbers in the Obj_Entry. */
214 obj
->magic
= RTLD_MAGIC
;
215 obj
->version
= RTLD_VERSION
;
217 /* Fill in the dynamic linker entry points. */
218 obj
->dlopen
= dlopen
;
220 obj
->dlerror
= dlerror
;
221 obj
->dlclose
= dlclose
;
222 obj
->dladdr
= dladdr
;
224 dbg(("fixing up PLTGOT"));
225 /* Set the special PLTGOT entries. */
226 if (obj
->pltgot
!= NULL
)
227 _rtld_setup_pltgot(obj
);