1 /*- 2 * Copyright 1996-1998 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 * $Id: reloc.c,v 1.1 1998/09/04 19:03:57 dfr Exp $ 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 * Debugging support. 52 */ 53 54 #define assert(cond) ((cond) ? (void) 0 :\ 55 (msg("oops: " __XSTRING(__LINE__) "\n"), abort())) 56 #define msg(s) (write(1, s, strlen(s))) 57 #define trace() msg("trace: " __XSTRING(__LINE__) "\n"); 58 59 /* 60 * Process the special R_386_COPY relocations in the main program. These 61 * copy data from a shared object into a region in the main program's BSS 62 * segment. 63 * 64 * Returns 0 on success, -1 on failure. 65 */ 66 int 67 do_copy_relocations(Obj_Entry *dstobj) 68 { 69 const Elf_Rel *rellim; 70 const Elf_Rel *rel; 71 72 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */ 73 74 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize); 75 for (rel = dstobj->rel; rel < rellim; rel++) { 76 if (ELF_R_TYPE(rel->r_info) == R_386_COPY) { 77 void *dstaddr; 78 const Elf_Sym *dstsym; 79 const char *name; 80 unsigned long hash; 81 size_t size; 82 const void *srcaddr; 83 const Elf_Sym *srcsym; 84 Obj_Entry *srcobj; 85 86 dstaddr = (void *) (dstobj->relocbase + rel->r_offset); 87 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info); 88 name = dstobj->strtab + dstsym->st_name; 89 hash = elf_hash(name); 90 size = dstsym->st_size; 91 92 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) 93 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL) 94 break; 95 96 if (srcobj == NULL) { 97 _rtld_error("Undefined symbol \"%s\" referenced from COPY" 98 " relocation in %s", name, dstobj->path); 99 return -1; 100 } 101 102 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value); 103 memcpy(dstaddr, srcaddr, size); 104 } 105 } 106 107 return 0; 108 } 109 110 /* Initialize the special GOT entries. */ 111 void 112 init_pltgot(Obj_Entry *obj) 113 { 114 if (obj->pltgot != NULL) { 115 obj->pltgot[1] = (Elf_Addr) obj; 116 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 117 } 118 } 119 120 /* Process the non-PLT relocations. */ 121 int 122 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld) 123 { 124 const Elf_Rel *rellim; 125 const Elf_Rel *rel; 126 127 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize); 128 for (rel = obj->rel; rel < rellim; rel++) { 129 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset); 130 131 switch (ELF_R_TYPE(rel->r_info)) { 132 133 case R_386_NONE: 134 break; 135 136 case R_386_32: 137 { 138 const Elf_Sym *def; 139 const Obj_Entry *defobj; 140 141 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 142 false); 143 if (def == NULL) 144 return -1; 145 146 *where += (Elf_Addr) (defobj->relocbase + def->st_value); 147 } 148 break; 149 150 case R_386_PC32: 151 /* 152 * I don't think the dynamic linker should ever see this 153 * type of relocation. But the binutils-2.6 tools sometimes 154 * generate it. 155 */ 156 { 157 const Elf_Sym *def; 158 const Obj_Entry *defobj; 159 160 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 161 false); 162 if (def == NULL) 163 return -1; 164 165 *where += 166 (Elf_Addr) (defobj->relocbase + def->st_value) - 167 (Elf_Addr) where; 168 } 169 break; 170 171 case R_386_COPY: 172 /* 173 * These are deferred until all other relocations have 174 * been done. All we do here is make sure that the COPY 175 * relocation is not in a shared library. They are allowed 176 * only in executable files. 177 */ 178 if (!obj->mainprog) { 179 _rtld_error("%s: Unexpected R_386_COPY relocation" 180 " in shared library", obj->path); 181 return -1; 182 } 183 break; 184 185 case R_386_GLOB_DAT: 186 { 187 const Elf_Sym *def; 188 const Obj_Entry *defobj; 189 190 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 191 false); 192 if (def == NULL) 193 return -1; 194 195 *where = (Elf_Addr) (defobj->relocbase + def->st_value); 196 } 197 break; 198 199 case R_386_RELATIVE: 200 *where += (Elf_Addr) obj->relocbase; 201 break; 202 203 default: 204 _rtld_error("%s: Unsupported relocation type %d" 205 " in non-PLT relocations\n", obj->path, 206 ELF_R_TYPE(rel->r_info)); 207 return -1; 208 } 209 } 210 return 0; 211 } 212 213 /* Process the PLT relocations. */ 214 int 215 reloc_plt(Obj_Entry *obj, bool bind_now) 216 { 217 const Elf_Rel *rellim; 218 const Elf_Rel *rel; 219 220 /* Process the PLT relocations. */ 221 rellim = (const Elf_Rel *) ((caddr_t) obj->pltrel + obj->pltrelsize); 222 if (bind_now) { 223 /* Fully resolve procedure addresses now */ 224 for (rel = obj->pltrel; rel < rellim; rel++) { 225 Elf_Addr *where = (Elf_Addr *) 226 (obj->relocbase + rel->r_offset); 227 const Elf_Sym *def; 228 const Obj_Entry *defobj; 229 230 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT); 231 232 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true); 233 if (def == NULL) 234 return -1; 235 236 *where = (Elf_Addr) (defobj->relocbase + def->st_value); 237 } 238 } else { /* Just relocate the GOT slots pointing into the PLT */ 239 for (rel = obj->pltrel; rel < rellim; rel++) { 240 Elf_Addr *where = (Elf_Addr *) 241 (obj->relocbase + rel->r_offset); 242 *where += (Elf_Addr) obj->relocbase; 243 } 244 } 245 return 0; 246 } 247