1*0a6a1f1dSLionel Sambuc /* $NetBSD: reloc.c,v 1.107 2014/08/25 20:40:52 joerg Exp $ */
2e83f7ba2SBen Gras
3e83f7ba2SBen Gras /*
4e83f7ba2SBen Gras * Copyright 1996 John D. Polstra.
5e83f7ba2SBen Gras * Copyright 1996 Matt Thomas <matt@3am-software.com>
6e83f7ba2SBen Gras * All rights reserved.
7e83f7ba2SBen Gras *
8e83f7ba2SBen Gras * Redistribution and use in source and binary forms, with or without
9e83f7ba2SBen Gras * modification, are permitted provided that the following conditions
10e83f7ba2SBen Gras * are met:
11e83f7ba2SBen Gras * 1. Redistributions of source code must retain the above copyright
12e83f7ba2SBen Gras * notice, this list of conditions and the following disclaimer.
13e83f7ba2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
14e83f7ba2SBen Gras * notice, this list of conditions and the following disclaimer in the
15e83f7ba2SBen Gras * documentation and/or other materials provided with the distribution.
16e83f7ba2SBen Gras * 3. All advertising materials mentioning features or use of this software
17e83f7ba2SBen Gras * must display the following acknowledgement:
18e83f7ba2SBen Gras * This product includes software developed by John Polstra.
19e83f7ba2SBen Gras * 4. The name of the author may not be used to endorse or promote products
20e83f7ba2SBen Gras * derived from this software without specific prior written permission.
21e83f7ba2SBen Gras *
22e83f7ba2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23e83f7ba2SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24e83f7ba2SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25e83f7ba2SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26e83f7ba2SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27e83f7ba2SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28e83f7ba2SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29e83f7ba2SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30e83f7ba2SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31e83f7ba2SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32e83f7ba2SBen Gras */
33e83f7ba2SBen Gras
34e83f7ba2SBen Gras /*
35e83f7ba2SBen Gras * Dynamic linker for ELF.
36e83f7ba2SBen Gras *
37e83f7ba2SBen Gras * John Polstra <jdp@polstra.com>.
38e83f7ba2SBen Gras */
39e83f7ba2SBen Gras
40e83f7ba2SBen Gras #include <sys/cdefs.h>
41e83f7ba2SBen Gras #ifndef lint
42*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: reloc.c,v 1.107 2014/08/25 20:40:52 joerg Exp $");
43e83f7ba2SBen Gras #endif /* not lint */
44e83f7ba2SBen Gras
45e83f7ba2SBen Gras #include <err.h>
46e83f7ba2SBen Gras #include <errno.h>
47e83f7ba2SBen Gras #include <fcntl.h>
48e83f7ba2SBen Gras #include <stdarg.h>
49e83f7ba2SBen Gras #include <stdio.h>
50e83f7ba2SBen Gras #include <stdlib.h>
51e83f7ba2SBen Gras #include <string.h>
52e83f7ba2SBen Gras #include <unistd.h>
53e83f7ba2SBen Gras #include <sys/types.h>
54e83f7ba2SBen Gras #include <sys/mman.h>
55e83f7ba2SBen Gras #include <sys/bitops.h>
56e83f7ba2SBen Gras #include <dirent.h>
57e83f7ba2SBen Gras
58e83f7ba2SBen Gras #include "debug.h"
59e83f7ba2SBen Gras #include "rtld.h"
60e83f7ba2SBen Gras
61e83f7ba2SBen Gras #ifndef RTLD_INHIBIT_COPY_RELOCS
62e83f7ba2SBen Gras static int _rtld_do_copy_relocation(const Obj_Entry *, const Elf_Rela *);
63e83f7ba2SBen Gras
64e83f7ba2SBen Gras static int
_rtld_do_copy_relocation(const Obj_Entry * dstobj,const Elf_Rela * rela)65e83f7ba2SBen Gras _rtld_do_copy_relocation(const Obj_Entry *dstobj, const Elf_Rela *rela)
66e83f7ba2SBen Gras {
67e83f7ba2SBen Gras void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
68e83f7ba2SBen Gras const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
69e83f7ba2SBen Gras const char *name = dstobj->strtab + dstsym->st_name;
70e83f7ba2SBen Gras unsigned long hash = _rtld_elf_hash(name);
71e83f7ba2SBen Gras size_t size = dstsym->st_size;
72e83f7ba2SBen Gras const void *srcaddr;
73e83f7ba2SBen Gras const Elf_Sym *srcsym = NULL;
74e83f7ba2SBen Gras Obj_Entry *srcobj;
75e83f7ba2SBen Gras
76f14fb602SLionel Sambuc for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) {
77f14fb602SLionel Sambuc srcsym = _rtld_symlook_obj(name, hash, srcobj, 0,
78f14fb602SLionel Sambuc _rtld_fetch_ventry(dstobj, ELF_R_SYM(rela->r_info)));
79f14fb602SLionel Sambuc if (srcsym != NULL)
80e83f7ba2SBen Gras break;
81f14fb602SLionel Sambuc }
82e83f7ba2SBen Gras
83e83f7ba2SBen Gras if (srcobj == NULL) {
84e83f7ba2SBen Gras _rtld_error("Undefined symbol \"%s\" referenced from COPY"
85e83f7ba2SBen Gras " relocation in %s", name, dstobj->path);
86e83f7ba2SBen Gras return (-1);
87e83f7ba2SBen Gras }
88e83f7ba2SBen Gras srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
89e83f7ba2SBen Gras (void)memcpy(dstaddr, srcaddr, size);
90e83f7ba2SBen Gras rdbg(("COPY %s %s %s --> src=%p dst=%p size %ld",
91e83f7ba2SBen Gras dstobj->path, srcobj->path, name, srcaddr,
92e83f7ba2SBen Gras (void *)dstaddr, (long)size));
93e83f7ba2SBen Gras return (0);
94e83f7ba2SBen Gras }
95e83f7ba2SBen Gras #endif /* RTLD_INHIBIT_COPY_RELOCS */
96e83f7ba2SBen Gras
97e83f7ba2SBen Gras
98e83f7ba2SBen Gras /*
99e83f7ba2SBen Gras * Process the special R_xxx_COPY relocations in the main program. These
100e83f7ba2SBen Gras * copy data from a shared object into a region in the main program's BSS
101e83f7ba2SBen Gras * segment.
102e83f7ba2SBen Gras *
103e83f7ba2SBen Gras * Returns 0 on success, -1 on failure.
104e83f7ba2SBen Gras */
105e83f7ba2SBen Gras int
_rtld_do_copy_relocations(const Obj_Entry * dstobj)106e83f7ba2SBen Gras _rtld_do_copy_relocations(const Obj_Entry *dstobj)
107e83f7ba2SBen Gras {
108e83f7ba2SBen Gras #ifndef RTLD_INHIBIT_COPY_RELOCS
109e83f7ba2SBen Gras
110e83f7ba2SBen Gras /* COPY relocations are invalid elsewhere */
111e83f7ba2SBen Gras assert(!dstobj->isdynamic);
112e83f7ba2SBen Gras
113e83f7ba2SBen Gras if (dstobj->rel != NULL) {
114e83f7ba2SBen Gras const Elf_Rel *rel;
115e83f7ba2SBen Gras for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
116e83f7ba2SBen Gras if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
117e83f7ba2SBen Gras Elf_Rela ourrela;
118e83f7ba2SBen Gras ourrela.r_info = rel->r_info;
119e83f7ba2SBen Gras ourrela.r_offset = rel->r_offset;
120e83f7ba2SBen Gras ourrela.r_addend = 0;
121e83f7ba2SBen Gras if (_rtld_do_copy_relocation(dstobj,
122e83f7ba2SBen Gras &ourrela) < 0)
123e83f7ba2SBen Gras return (-1);
124e83f7ba2SBen Gras }
125e83f7ba2SBen Gras }
126e83f7ba2SBen Gras }
127e83f7ba2SBen Gras if (dstobj->rela != NULL) {
128e83f7ba2SBen Gras const Elf_Rela *rela;
129e83f7ba2SBen Gras for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
130e83f7ba2SBen Gras if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
131e83f7ba2SBen Gras if (_rtld_do_copy_relocation(dstobj, rela) < 0)
132e83f7ba2SBen Gras return (-1);
133e83f7ba2SBen Gras }
134e83f7ba2SBen Gras }
135e83f7ba2SBen Gras }
136e83f7ba2SBen Gras #endif /* RTLD_INHIBIT_COPY_RELOCS */
137e83f7ba2SBen Gras
138e83f7ba2SBen Gras return (0);
139e83f7ba2SBen Gras }
140e83f7ba2SBen Gras
141e83f7ba2SBen Gras /*
142e83f7ba2SBen Gras * Relocate newly-loaded shared objects. The argument is a pointer to
143e83f7ba2SBen Gras * the Obj_Entry for the first such object. All objects from the first
144e83f7ba2SBen Gras * to the end of the list of objects are relocated. Returns 0 on success,
145e83f7ba2SBen Gras * or -1 on failure.
146e83f7ba2SBen Gras */
147e83f7ba2SBen Gras int
_rtld_relocate_objects(Obj_Entry * first,bool bind_now)148e83f7ba2SBen Gras _rtld_relocate_objects(Obj_Entry *first, bool bind_now)
149e83f7ba2SBen Gras {
150e83f7ba2SBen Gras Obj_Entry *obj;
151e83f7ba2SBen Gras int ok = 1;
152e83f7ba2SBen Gras
153e83f7ba2SBen Gras for (obj = first; obj != NULL; obj = obj->next) {
154e83f7ba2SBen Gras if (obj->nbuckets == 0 || obj->nchains == 0 ||
155e83f7ba2SBen Gras obj->buckets == NULL || obj->symtab == NULL ||
156e83f7ba2SBen Gras obj->strtab == NULL) {
157e83f7ba2SBen Gras _rtld_error("%s: Shared object has no run-time"
158e83f7ba2SBen Gras " symbol table", obj->path);
159e83f7ba2SBen Gras return -1;
160e83f7ba2SBen Gras }
161e83f7ba2SBen Gras if (obj->nbuckets == UINT32_MAX) {
162e83f7ba2SBen Gras _rtld_error("%s: Symbol table too large", obj->path);
163e83f7ba2SBen Gras return -1;
164e83f7ba2SBen Gras }
165e83f7ba2SBen Gras rdbg((" relocating %s (%ld/%ld rel/rela, %ld/%ld plt rel/rela)",
166e83f7ba2SBen Gras obj->path,
167e83f7ba2SBen Gras (long)(obj->rellim - obj->rel),
168e83f7ba2SBen Gras (long)(obj->relalim - obj->rela),
169e83f7ba2SBen Gras (long)(obj->pltrellim - obj->pltrel),
170e83f7ba2SBen Gras (long)(obj->pltrelalim - obj->pltrela)));
171e83f7ba2SBen Gras
17284d9c625SLionel Sambuc #if !defined(__minix)
173e83f7ba2SBen Gras if (obj->textrel) {
174e83f7ba2SBen Gras /*
175e83f7ba2SBen Gras * There are relocations to the write-protected text
176e83f7ba2SBen Gras * segment.
177e83f7ba2SBen Gras */
178e83f7ba2SBen Gras if (mprotect(obj->mapbase, obj->textsize,
179e83f7ba2SBen Gras PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
180e83f7ba2SBen Gras _rtld_error("%s: Cannot write-enable text "
181e83f7ba2SBen Gras "segment: %s", obj->path, xstrerror(errno));
182e83f7ba2SBen Gras return -1;
183e83f7ba2SBen Gras }
184e83f7ba2SBen Gras }
18584d9c625SLionel Sambuc #endif /* !defined(__minix) */
186e83f7ba2SBen Gras dbg(("doing non-PLT relocations"));
187e83f7ba2SBen Gras if (_rtld_relocate_nonplt_objects(obj) < 0)
188e83f7ba2SBen Gras ok = 0;
18984d9c625SLionel Sambuc #if !defined(__minix)
190e83f7ba2SBen Gras if (obj->textrel) { /* Re-protected the text segment. */
191e83f7ba2SBen Gras if (mprotect(obj->mapbase, obj->textsize,
192e83f7ba2SBen Gras PROT_READ | PROT_EXEC) == -1) {
193e83f7ba2SBen Gras _rtld_error("%s: Cannot write-protect text "
194e83f7ba2SBen Gras "segment: %s", obj->path, xstrerror(errno));
195e83f7ba2SBen Gras return -1;
196e83f7ba2SBen Gras }
197e83f7ba2SBen Gras }
19884d9c625SLionel Sambuc #endif /* !defined(__minix) */
199e83f7ba2SBen Gras dbg(("doing lazy PLT binding"));
200e83f7ba2SBen Gras if (_rtld_relocate_plt_lazy(obj) < 0)
201e83f7ba2SBen Gras ok = 0;
202e83f7ba2SBen Gras if (obj->z_now || bind_now) {
203e83f7ba2SBen Gras dbg(("doing immediate PLT binding"));
204e83f7ba2SBen Gras if (_rtld_relocate_plt_objects(obj) < 0)
205e83f7ba2SBen Gras ok = 0;
206e83f7ba2SBen Gras }
207e83f7ba2SBen Gras if (!ok)
208e83f7ba2SBen Gras return -1;
209e83f7ba2SBen Gras
210e83f7ba2SBen Gras /* Set some sanity-checking numbers in the Obj_Entry. */
211e83f7ba2SBen Gras obj->magic = RTLD_MAGIC;
212e83f7ba2SBen Gras obj->version = RTLD_VERSION;
213e83f7ba2SBen Gras
214f14fb602SLionel Sambuc /*
215f14fb602SLionel Sambuc * Fill in the backwards compatibility dynamic linker entry points.
216f14fb602SLionel Sambuc *
217f14fb602SLionel Sambuc * DO NOT ADD TO THIS LIST
218f14fb602SLionel Sambuc */
219e83f7ba2SBen Gras obj->dlopen = dlopen;
220e83f7ba2SBen Gras obj->dlsym = dlsym;
221e83f7ba2SBen Gras obj->dlerror = dlerror;
222e83f7ba2SBen Gras obj->dlclose = dlclose;
223e83f7ba2SBen Gras obj->dladdr = dladdr;
224e83f7ba2SBen Gras
225e83f7ba2SBen Gras dbg(("fixing up PLTGOT"));
226e83f7ba2SBen Gras /* Set the special PLTGOT entries. */
227e83f7ba2SBen Gras if (obj->pltgot != NULL)
228e83f7ba2SBen Gras _rtld_setup_pltgot(obj);
229e83f7ba2SBen Gras }
230e83f7ba2SBen Gras
231e83f7ba2SBen Gras return 0;
232e83f7ba2SBen Gras }
233*0a6a1f1dSLionel Sambuc
234*0a6a1f1dSLionel Sambuc Elf_Addr
_rtld_resolve_ifunc(const Obj_Entry * obj,const Elf_Sym * def)235*0a6a1f1dSLionel Sambuc _rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def)
236*0a6a1f1dSLionel Sambuc {
237*0a6a1f1dSLionel Sambuc Elf_Addr target;
238*0a6a1f1dSLionel Sambuc
239*0a6a1f1dSLionel Sambuc _rtld_shared_exit();
240*0a6a1f1dSLionel Sambuc target = _rtld_call_function_addr(obj,
241*0a6a1f1dSLionel Sambuc (Elf_Addr)obj->relocbase + def->st_value);
242*0a6a1f1dSLionel Sambuc _rtld_shared_enter();
243*0a6a1f1dSLionel Sambuc
244*0a6a1f1dSLionel Sambuc return target;
245*0a6a1f1dSLionel Sambuc }
246