1 /* $NetBSD: reloc.c,v 1.81 2003/08/12 09:18:46 skrll Exp $ */ 2 3 /* 4 * Copyright 1996 John D. Polstra. 5 * Copyright 1996 Matt Thomas <matt@3am-software.com> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 21 * 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. 32 */ 33 34 /* 35 * Dynamic linker for ELF. 36 * 37 * John Polstra <jdp@polstra.com>. 38 */ 39 40 #include <err.h> 41 #include <errno.h> 42 #include <fcntl.h> 43 #include <stdarg.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <sys/types.h> 49 #include <sys/mman.h> 50 #include <dirent.h> 51 52 #include "debug.h" 53 #include "rtld.h" 54 55 #ifndef RTLD_INHIBIT_COPY_RELOCS 56 static int _rtld_do_copy_relocation(const Obj_Entry *, const Elf_Rela *); 57 58 static int 59 _rtld_do_copy_relocation(const Obj_Entry *dstobj, const Elf_Rela *rela) 60 { 61 void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset); 62 const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info); 63 const char *name = dstobj->strtab + dstsym->st_name; 64 unsigned long hash = _rtld_elf_hash(name); 65 size_t size = dstsym->st_size; 66 const void *srcaddr; 67 const Elf_Sym *srcsym = NULL; 68 Obj_Entry *srcobj; 69 70 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) 71 if ((srcsym = _rtld_symlook_obj(name, hash, srcobj, false)) != NULL) 72 break; 73 74 if (srcobj == NULL) { 75 _rtld_error("Undefined symbol \"%s\" referenced from COPY" 76 " relocation in %s", name, dstobj->path); 77 return (-1); 78 } 79 srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value); 80 (void)memcpy(dstaddr, srcaddr, size); 81 rdbg(("COPY %s %s %s --> src=%p dst=%p *dst= %p size %ld", 82 dstobj->path, srcobj->path, name, (void *)srcaddr, 83 (void *)dstaddr, (void *)*(long *)dstaddr, (long)size)); 84 return (0); 85 } 86 #endif /* RTLD_INHIBIT_COPY_RELOCS */ 87 88 89 /* 90 * Process the special R_xxx_COPY relocations in the main program. These 91 * copy data from a shared object into a region in the main program's BSS 92 * segment. 93 * 94 * Returns 0 on success, -1 on failure. 95 */ 96 int 97 _rtld_do_copy_relocations(const Obj_Entry *dstobj) 98 { 99 #ifndef RTLD_INHIBIT_COPY_RELOCS 100 101 /* COPY relocations are invalid elsewhere */ 102 assert(!dstobj->isdynamic); 103 104 if (dstobj->rel != NULL) { 105 const Elf_Rel *rel; 106 for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) { 107 if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) { 108 Elf_Rela ourrela; 109 ourrela.r_info = rel->r_info; 110 ourrela.r_offset = rel->r_offset; 111 ourrela.r_addend = 0; 112 if (_rtld_do_copy_relocation(dstobj, 113 &ourrela) < 0) 114 return (-1); 115 } 116 } 117 } 118 if (dstobj->rela != NULL) { 119 const Elf_Rela *rela; 120 for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) { 121 if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) { 122 if (_rtld_do_copy_relocation(dstobj, rela) < 0) 123 return (-1); 124 } 125 } 126 } 127 #endif /* RTLD_INHIBIT_COPY_RELOCS */ 128 129 return (0); 130 } 131 132 /* 133 * Relocate newly-loaded shared objects. The argument is a pointer to 134 * the Obj_Entry for the first such object. All objects from the first 135 * to the end of the list of objects are relocated. Returns 0 on success, 136 * or -1 on failure. 137 */ 138 int 139 _rtld_relocate_objects(Obj_Entry *first, bool bind_now) 140 { 141 Obj_Entry *obj; 142 int ok = 1; 143 144 for (obj = first; obj != NULL; obj = obj->next) { 145 if (obj->nbuckets == 0 || obj->nchains == 0 || 146 obj->buckets == NULL || obj->symtab == NULL || 147 obj->strtab == NULL) { 148 _rtld_error("%s: Shared object has no run-time" 149 " symbol table", obj->path); 150 return -1; 151 } 152 rdbg((" relocating %s (%ld/%ld rel/rela, %ld/%ld plt rel/rela)", 153 obj->path, 154 (long)(obj->rellim - obj->rel), 155 (long)(obj->relalim - obj->rela), 156 (long)(obj->pltrellim - obj->pltrel), 157 (long)(obj->pltrelalim - obj->pltrela))); 158 159 if (obj->textrel) { 160 /* 161 * There are relocations to the write-protected text 162 * segment. 163 */ 164 if (mprotect(obj->mapbase, obj->textsize, 165 PROT_READ | PROT_WRITE | PROT_EXEC) == -1) { 166 _rtld_error("%s: Cannot write-enable text " 167 "segment: %s", obj->path, xstrerror(errno)); 168 return -1; 169 } 170 } 171 dbg(("doing non-PLT relocations")); 172 if (_rtld_relocate_nonplt_objects(obj) < 0) 173 ok = 0; 174 if (obj->textrel) { /* Re-protected the text segment. */ 175 if (mprotect(obj->mapbase, obj->textsize, 176 PROT_READ | PROT_EXEC) == -1) { 177 _rtld_error("%s: Cannot write-protect text " 178 "segment: %s", obj->path, xstrerror(errno)); 179 return -1; 180 } 181 } 182 dbg(("doing lazy PLT binding")); 183 if (_rtld_relocate_plt_lazy(obj) < 0) 184 ok = 0; 185 #if defined(__i386__) || defined(__arm__) 186 if (bind_now) 187 if (_rtld_relocate_plt_objects(obj) < 0) 188 ok = 0; 189 #endif 190 if (!ok) 191 return -1; 192 193 194 /* Set some sanity-checking numbers in the Obj_Entry. */ 195 obj->magic = RTLD_MAGIC; 196 obj->version = RTLD_VERSION; 197 198 /* Fill in the dynamic linker entry points. */ 199 obj->dlopen = dlopen; 200 obj->dlsym = dlsym; 201 obj->dlerror = dlerror; 202 obj->dlclose = dlclose; 203 obj->dladdr = dladdr; 204 205 dbg(("fixing up PLTGOT")); 206 /* Set the special PLTGOT entries. */ 207 if (obj->pltgot != NULL) 208 _rtld_setup_pltgot(obj); 209 } 210 211 return 0; 212 } 213