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 if (cache != NULL) 129 memset(cache, 0, bytes); 130 131 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize); 132 for (rel = obj->rel; rel < rellim; rel++) { 133 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset); 134 135 switch (ELF_R_TYPE(rel->r_info)) { 136 137 case R_386_NONE: 138 break; 139 140 case R_386_32: 141 { 142 const Elf_Sym *def; 143 const Obj_Entry *defobj; 144 145 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 146 false, cache); 147 if (def == NULL) 148 goto done; 149 150 *where += (Elf_Addr) (defobj->relocbase + def->st_value); 151 } 152 break; 153 154 case R_386_PC32: 155 /* 156 * I don't think the dynamic linker should ever see this 157 * type of relocation. But the binutils-2.6 tools sometimes 158 * generate it. 159 */ 160 { 161 const Elf_Sym *def; 162 const Obj_Entry *defobj; 163 164 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 165 false, cache); 166 if (def == NULL) 167 goto done; 168 169 *where += 170 (Elf_Addr) (defobj->relocbase + def->st_value) - 171 (Elf_Addr) where; 172 } 173 break; 174 175 case R_386_COPY: 176 /* 177 * These are deferred until all other relocations have 178 * been done. All we do here is make sure that the COPY 179 * relocation is not in a shared library. They are allowed 180 * only in executable files. 181 */ 182 if (!obj->mainprog) { 183 _rtld_error("%s: Unexpected R_386_COPY relocation" 184 " in shared library", obj->path); 185 goto done; 186 } 187 break; 188 189 case R_386_GLOB_DAT: 190 { 191 const Elf_Sym *def; 192 const Obj_Entry *defobj; 193 194 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 195 false, cache); 196 if (def == NULL) 197 goto done; 198 199 *where = (Elf_Addr) (defobj->relocbase + def->st_value); 200 } 201 break; 202 203 case R_386_RELATIVE: 204 *where += (Elf_Addr) obj->relocbase; 205 break; 206 207 default: 208 _rtld_error("%s: Unsupported relocation type %d" 209 " in non-PLT relocations\n", obj->path, 210 ELF_R_TYPE(rel->r_info)); 211 goto done; 212 } 213 } 214 r = 0; 215 done: 216 if (cache) 217 munmap(cache, bytes); 218 return(r); 219 } 220 221 /* Process the PLT relocations. */ 222 int 223 reloc_plt(Obj_Entry *obj) 224 { 225 const Elf_Rel *rellim; 226 const Elf_Rel *rel; 227 228 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 229 for (rel = obj->pltrel; rel < rellim; rel++) { 230 Elf_Addr *where; 231 232 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT); 233 234 /* Relocate the GOT slot pointing into the PLT. */ 235 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 236 *where += (Elf_Addr)obj->relocbase; 237 } 238 return 0; 239 } 240 241 /* Relocate the jump slots in an object. */ 242 int 243 reloc_jmpslots(Obj_Entry *obj) 244 { 245 const Elf_Rel *rellim; 246 const Elf_Rel *rel; 247 248 if (obj->jmpslots_done) 249 return 0; 250 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 251 for (rel = obj->pltrel; rel < rellim; rel++) { 252 Elf_Addr *where, target; 253 const Elf_Sym *def; 254 const Obj_Entry *defobj; 255 256 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT); 257 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 258 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL); 259 if (def == NULL) 260 return -1; 261 target = (Elf_Addr)(defobj->relocbase + def->st_value); 262 reloc_jmpslot(where, target, defobj, obj, rel); 263 } 264 obj->jmpslots_done = true; 265 return 0; 266 } 267