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 #include <machine/segments.h> 37 #include <machine/sysarch.h> 38 39 #include <dlfcn.h> 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 49 #include "debug.h" 50 #include "rtld.h" 51 52 /* 53 * Process the special R_386_COPY relocations in the main program. These 54 * copy data from a shared object into a region in the main program's BSS 55 * segment. 56 * 57 * Returns 0 on success, -1 on failure. 58 */ 59 int 60 do_copy_relocations(Obj_Entry *dstobj) 61 { 62 const Elf_Rel *rellim; 63 const Elf_Rel *rel; 64 65 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */ 66 67 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize); 68 for (rel = dstobj->rel; rel < rellim; rel++) { 69 if (ELF_R_TYPE(rel->r_info) == R_386_COPY) { 70 void *dstaddr; 71 const Elf_Sym *dstsym; 72 const char *name; 73 unsigned long hash; 74 size_t size; 75 const void *srcaddr; 76 const Elf_Sym *srcsym; 77 const Ver_Entry *ve; 78 Obj_Entry *srcobj; 79 80 dstaddr = (void *) (dstobj->relocbase + rel->r_offset); 81 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info); 82 name = dstobj->strtab + dstsym->st_name; 83 hash = elf_hash(name); 84 size = dstsym->st_size; 85 ve = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info)); 86 87 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) 88 if ((srcsym = symlook_obj(name, hash, srcobj, ve, 0)) != NULL) 89 break; 90 91 if (srcobj == NULL) { 92 _rtld_error("Undefined symbol \"%s\" referenced from COPY" 93 " relocation in %s", name, dstobj->path); 94 return -1; 95 } 96 97 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value); 98 memcpy(dstaddr, srcaddr, size); 99 } 100 } 101 102 return 0; 103 } 104 105 /* Initialize the special GOT entries. */ 106 void 107 init_pltgot(Obj_Entry *obj) 108 { 109 if (obj->pltgot != NULL) { 110 obj->pltgot[1] = (Elf_Addr) obj; 111 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 112 } 113 } 114 115 /* Process the non-PLT relocations. */ 116 int 117 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld) 118 { 119 const Elf_Rel *rellim; 120 const Elf_Rel *rel; 121 SymCache *cache; 122 int r = -1; 123 124 /* 125 * The dynamic loader may be called from a thread, we have 126 * limited amounts of stack available so we cannot use alloca(). 127 */ 128 if (obj != obj_rtld) { 129 cache = calloc(obj->nchains, sizeof(SymCache)); 130 /* No need to check for NULL here */ 131 } else 132 cache = NULL; 133 134 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize); 135 for (rel = obj->rel; rel < rellim; rel++) { 136 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset); 137 138 switch (ELF_R_TYPE(rel->r_info)) { 139 140 case R_386_NONE: 141 break; 142 143 case R_386_32: 144 { 145 const Elf_Sym *def; 146 const Obj_Entry *defobj; 147 148 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 149 false, cache); 150 if (def == NULL) 151 goto done; 152 153 *where += (Elf_Addr) (defobj->relocbase + def->st_value); 154 } 155 break; 156 157 case R_386_PC32: 158 /* 159 * I don't think the dynamic linker should ever see this 160 * type of relocation. But the binutils-2.6 tools sometimes 161 * generate it. 162 */ 163 { 164 const Elf_Sym *def; 165 const Obj_Entry *defobj; 166 167 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 168 false, cache); 169 if (def == NULL) 170 goto done; 171 172 *where += 173 (Elf_Addr) (defobj->relocbase + def->st_value) - 174 (Elf_Addr) where; 175 } 176 break; 177 178 case R_386_COPY: 179 /* 180 * These are deferred until all other relocations have 181 * been done. All we do here is make sure that the COPY 182 * relocation is not in a shared library. They are allowed 183 * only in executable files. 184 */ 185 if (!obj->mainprog) { 186 _rtld_error("%s: Unexpected R_386_COPY relocation" 187 " in shared library", obj->path); 188 goto done; 189 } 190 break; 191 192 case R_386_GLOB_DAT: 193 { 194 const Elf_Sym *def; 195 const Obj_Entry *defobj; 196 197 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 198 false, cache); 199 if (def == NULL) 200 goto done; 201 202 *where = (Elf_Addr) (defobj->relocbase + def->st_value); 203 } 204 break; 205 206 case R_386_RELATIVE: 207 *where += (Elf_Addr) obj->relocbase; 208 break; 209 210 case R_386_TLS_TPOFF: 211 { 212 const Elf_Sym *def; 213 const Obj_Entry *defobj; 214 215 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 216 false, cache); 217 if (def == NULL) 218 goto done; 219 220 /* 221 * We lazily allocate offsets for static TLS as we 222 * see the first relocation that references the 223 * TLS block. This allows us to support (small 224 * amounts of) static TLS in dynamically loaded 225 * modules. If we run out of space, we generate an 226 * error. 227 */ 228 if (!defobj->tls_done) { 229 if (!allocate_tls_offset((Obj_Entry*) defobj)) { 230 _rtld_error("%s: No space available for static " 231 "Thread Local Storage", obj->path); 232 goto done; 233 } 234 } 235 236 *where += (Elf_Addr) (def->st_value - defobj->tlsoffset); 237 } 238 break; 239 240 case R_386_TLS_DTPMOD32: 241 { 242 const Elf_Sym *def; 243 const Obj_Entry *defobj; 244 245 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 246 false, cache); 247 if (def == NULL) 248 goto done; 249 250 *where += (Elf_Addr) defobj->tlsindex; 251 } 252 break; 253 254 case R_386_TLS_DTPOFF32: 255 { 256 const Elf_Sym *def; 257 const Obj_Entry *defobj; 258 259 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 260 false, cache); 261 if (def == NULL) 262 goto done; 263 264 *where += (Elf_Addr) def->st_value; 265 } 266 break; 267 268 default: 269 _rtld_error("%s: Unsupported relocation type %d" 270 " in non-PLT relocations\n", obj->path, 271 ELF_R_TYPE(rel->r_info)); 272 goto done; 273 } 274 } 275 r = 0; 276 done: 277 if (cache != NULL) 278 free(cache); 279 return(r); 280 } 281 282 /* Process the PLT relocations. */ 283 int 284 reloc_plt(Obj_Entry *obj) 285 { 286 const Elf_Rel *rellim; 287 const Elf_Rel *rel; 288 289 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 290 for (rel = obj->pltrel; rel < rellim; rel++) { 291 Elf_Addr *where; 292 293 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT); 294 295 /* Relocate the GOT slot pointing into the PLT. */ 296 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 297 *where += (Elf_Addr)obj->relocbase; 298 } 299 return 0; 300 } 301 302 /* Relocate the jump slots in an object. */ 303 int 304 reloc_jmpslots(Obj_Entry *obj) 305 { 306 const Elf_Rel *rellim; 307 const Elf_Rel *rel; 308 309 if (obj->jmpslots_done) 310 return 0; 311 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 312 for (rel = obj->pltrel; rel < rellim; rel++) { 313 Elf_Addr *where, target; 314 const Elf_Sym *def; 315 const Obj_Entry *defobj; 316 317 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT); 318 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 319 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL); 320 if (def == NULL) 321 return -1; 322 target = (Elf_Addr)(defobj->relocbase + def->st_value); 323 reloc_jmpslot(where, target, defobj, obj, rel); 324 } 325 obj->jmpslots_done = true; 326 return 0; 327 } 328 329 void 330 allocate_initial_tls(Obj_Entry *objs) 331 { 332 void* tls; 333 334 /* 335 * Fix the size of the static TLS block by using the maximum 336 * offset allocated so far and adding a bit for dynamic modules to 337 * use. 338 */ 339 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA; 340 tls = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr)); 341 i386_set_gsbase(tls); 342 } 343 344 /* GNU ABI */ 345 __attribute__((__regparm__(1))) 346 void *___tls_get_addr(tls_index *ti) 347 { 348 Elf_Addr** segbase; 349 Elf_Addr* dtv; 350 351 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase)); 352 dtv = segbase[1]; 353 354 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset); 355 } 356 357 /* Sun ABI */ 358 void *__tls_get_addr(tls_index *ti) 359 { 360 Elf_Addr** segbase; 361 Elf_Addr* dtv; 362 363 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase)); 364 dtv = segbase[1]; 365 366 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset); 367 } 368