1 /*- 2 * Copyright 1996, 1997, 1998, 1999 John D. Polstra. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28 /* 29 * Dynamic linker for ELF. 30 * 31 * John Polstra <jdp@polstra.com>. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/mman.h> 36 37 #include <dlfcn.h> 38 #include <err.h> 39 #include <errno.h> 40 #include <fcntl.h> 41 #include <stdarg.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 #include "debug.h" 48 #include "rtld.h" 49 50 /* 51 * Process the special R_386_COPY relocations in the main program. These 52 * copy data from a shared object into a region in the main program's BSS 53 * segment. 54 * 55 * Returns 0 on success, -1 on failure. 56 */ 57 int 58 do_copy_relocations(Obj_Entry *dstobj) 59 { 60 const Elf_Rel *rellim; 61 const Elf_Rel *rel; 62 63 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */ 64 65 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize); 66 for (rel = dstobj->rel; rel < rellim; rel++) { 67 if (ELF_R_TYPE(rel->r_info) == R_386_COPY) { 68 void *dstaddr; 69 const Elf_Sym *dstsym; 70 const char *name; 71 unsigned long hash; 72 size_t size; 73 const void *srcaddr; 74 const Elf_Sym *srcsym; 75 Obj_Entry *srcobj; 76 77 dstaddr = (void *) (dstobj->relocbase + rel->r_offset); 78 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info); 79 name = dstobj->strtab + dstsym->st_name; 80 hash = elf_hash(name); 81 size = dstsym->st_size; 82 83 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) 84 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL) 85 break; 86 87 if (srcobj == NULL) { 88 _rtld_error("Undefined symbol \"%s\" referenced from COPY" 89 " relocation in %s", name, dstobj->path); 90 return -1; 91 } 92 93 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value); 94 memcpy(dstaddr, srcaddr, size); 95 } 96 } 97 98 return 0; 99 } 100 101 /* Initialize the special GOT entries. */ 102 void 103 init_pltgot(Obj_Entry *obj) 104 { 105 if (obj->pltgot != NULL) { 106 obj->pltgot[1] = (Elf_Addr) obj; 107 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 108 } 109 } 110 111 /* Process the non-PLT relocations. */ 112 int 113 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld) 114 { 115 const Elf_Rel *rellim; 116 const Elf_Rel *rel; 117 SymCache *cache; 118 int bytes = obj->nchains * sizeof(SymCache); 119 int r = -1; 120 121 /* 122 * The dynamic loader may be called from a thread, we have 123 * limited amounts of stack available so we cannot use alloca(). 124 */ 125 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0); 126 if (cache == MAP_FAILED) 127 cache = NULL; 128 129 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize); 130 for (rel = obj->rel; rel < rellim; rel++) { 131 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset); 132 133 switch (ELF_R_TYPE(rel->r_info)) { 134 135 case R_386_NONE: 136 break; 137 138 case R_386_32: 139 { 140 const Elf_Sym *def; 141 const Obj_Entry *defobj; 142 143 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 144 false, cache); 145 if (def == NULL) 146 goto done; 147 148 *where += (Elf_Addr) (defobj->relocbase + def->st_value); 149 } 150 break; 151 152 case R_386_PC32: 153 /* 154 * I don't think the dynamic linker should ever see this 155 * type of relocation. But the binutils-2.6 tools sometimes 156 * generate it. 157 */ 158 { 159 const Elf_Sym *def; 160 const Obj_Entry *defobj; 161 162 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 163 false, cache); 164 if (def == NULL) 165 goto done; 166 167 *where += 168 (Elf_Addr) (defobj->relocbase + def->st_value) - 169 (Elf_Addr) where; 170 } 171 break; 172 173 case R_386_COPY: 174 /* 175 * These are deferred until all other relocations have 176 * been done. All we do here is make sure that the COPY 177 * relocation is not in a shared library. They are allowed 178 * only in executable files. 179 */ 180 if (!obj->mainprog) { 181 _rtld_error("%s: Unexpected R_386_COPY relocation" 182 " in shared library", obj->path); 183 goto done; 184 } 185 break; 186 187 case R_386_GLOB_DAT: 188 { 189 const Elf_Sym *def; 190 const Obj_Entry *defobj; 191 192 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 193 false, cache); 194 if (def == NULL) 195 goto done; 196 197 *where = (Elf_Addr) (defobj->relocbase + def->st_value); 198 } 199 break; 200 201 case R_386_RELATIVE: 202 *where += (Elf_Addr) obj->relocbase; 203 break; 204 205 default: 206 _rtld_error("%s: Unsupported relocation type %d" 207 " in non-PLT relocations\n", obj->path, 208 ELF_R_TYPE(rel->r_info)); 209 goto done; 210 } 211 } 212 r = 0; 213 done: 214 if (cache) 215 munmap(cache, bytes); 216 return(r); 217 } 218 219 /* Process the PLT relocations. */ 220 int 221 reloc_plt(Obj_Entry *obj) 222 { 223 const Elf_Rel *rellim; 224 const Elf_Rel *rel; 225 226 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 227 for (rel = obj->pltrel; rel < rellim; rel++) { 228 Elf_Addr *where; 229 230 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT); 231 232 /* Relocate the GOT slot pointing into the PLT. */ 233 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 234 *where += (Elf_Addr)obj->relocbase; 235 } 236 return 0; 237 } 238 239 /* Relocate the jump slots in an object. */ 240 int 241 reloc_jmpslots(Obj_Entry *obj) 242 { 243 const Elf_Rel *rellim; 244 const Elf_Rel *rel; 245 246 if (obj->jmpslots_done) 247 return 0; 248 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 249 for (rel = obj->pltrel; rel < rellim; rel++) { 250 Elf_Addr *where, target; 251 const Elf_Sym *def; 252 const Obj_Entry *defobj; 253 254 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT); 255 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 256 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL); 257 if (def == NULL) 258 return -1; 259 target = (Elf_Addr)(defobj->relocbase + def->st_value); 260 reloc_jmpslot(where, target, defobj, obj, rel); 261 } 262 obj->jmpslots_done = true; 263 return 0; 264 } 265