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 Obj_Entry *srcobj; 78 79 dstaddr = (void *) (dstobj->relocbase + rel->r_offset); 80 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info); 81 name = dstobj->strtab + dstsym->st_name; 82 hash = elf_hash(name); 83 size = dstsym->st_size; 84 85 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) 86 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL) 87 break; 88 89 if (srcobj == NULL) { 90 _rtld_error("Undefined symbol \"%s\" referenced from COPY" 91 " relocation in %s", name, dstobj->path); 92 return -1; 93 } 94 95 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value); 96 memcpy(dstaddr, srcaddr, size); 97 } 98 } 99 100 return 0; 101 } 102 103 /* Initialize the special GOT entries. */ 104 void 105 init_pltgot(Obj_Entry *obj) 106 { 107 if (obj->pltgot != NULL) { 108 obj->pltgot[1] = (Elf_Addr) obj; 109 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 110 } 111 } 112 113 /* Process the non-PLT relocations. */ 114 int 115 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld) 116 { 117 const Elf_Rel *rellim; 118 const Elf_Rel *rel; 119 SymCache *cache; 120 int bytes = obj->nchains * sizeof(SymCache); 121 int r = -1; 122 123 /* 124 * The dynamic loader may be called from a thread, we have 125 * limited amounts of stack available so we cannot use alloca(). 126 */ 127 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0); 128 if (cache == MAP_FAILED) 129 cache = NULL; 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 case R_386_TLS_TPOFF: 208 { 209 const Elf_Sym *def; 210 const Obj_Entry *defobj; 211 212 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 213 false, cache); 214 if (def == NULL) 215 goto done; 216 217 /* 218 * We lazily allocate offsets for static TLS as we 219 * see the first relocation that references the 220 * TLS block. This allows us to support (small 221 * amounts of) static TLS in dynamically loaded 222 * modules. If we run out of space, we generate an 223 * error. 224 */ 225 if (!defobj->tls_done) { 226 if (!allocate_tls_offset((Obj_Entry*) defobj)) { 227 _rtld_error("%s: No space available for static " 228 "Thread Local Storage", obj->path); 229 goto done; 230 } 231 } 232 233 *where += (Elf_Addr) (def->st_value - defobj->tlsoffset); 234 } 235 break; 236 237 case R_386_TLS_DTPMOD32: 238 { 239 const Elf_Sym *def; 240 const Obj_Entry *defobj; 241 242 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 243 false, cache); 244 if (def == NULL) 245 goto done; 246 247 *where += (Elf_Addr) defobj->tlsindex; 248 } 249 break; 250 251 case R_386_TLS_DTPOFF32: 252 { 253 const Elf_Sym *def; 254 const Obj_Entry *defobj; 255 256 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 257 false, cache); 258 if (def == NULL) 259 goto done; 260 261 *where += (Elf_Addr) def->st_value; 262 } 263 break; 264 265 default: 266 _rtld_error("%s: Unsupported relocation type %d" 267 " in non-PLT relocations\n", obj->path, 268 ELF_R_TYPE(rel->r_info)); 269 goto done; 270 } 271 } 272 r = 0; 273 done: 274 if (cache) 275 munmap(cache, bytes); 276 return(r); 277 } 278 279 /* Process the PLT relocations. */ 280 int 281 reloc_plt(Obj_Entry *obj) 282 { 283 const Elf_Rel *rellim; 284 const Elf_Rel *rel; 285 286 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 287 for (rel = obj->pltrel; rel < rellim; rel++) { 288 Elf_Addr *where; 289 290 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT); 291 292 /* Relocate the GOT slot pointing into the PLT. */ 293 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 294 *where += (Elf_Addr)obj->relocbase; 295 } 296 return 0; 297 } 298 299 /* Relocate the jump slots in an object. */ 300 int 301 reloc_jmpslots(Obj_Entry *obj) 302 { 303 const Elf_Rel *rellim; 304 const Elf_Rel *rel; 305 306 if (obj->jmpslots_done) 307 return 0; 308 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 309 for (rel = obj->pltrel; rel < rellim; rel++) { 310 Elf_Addr *where, target; 311 const Elf_Sym *def; 312 const Obj_Entry *defobj; 313 314 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT); 315 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 316 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL); 317 if (def == NULL) 318 return -1; 319 target = (Elf_Addr)(defobj->relocbase + def->st_value); 320 reloc_jmpslot(where, target, defobj, obj, rel); 321 } 322 obj->jmpslots_done = true; 323 return 0; 324 } 325 326 void 327 allocate_initial_tls(Obj_Entry *objs) 328 { 329 void* tls; 330 #ifndef COMPAT_32BIT 331 union descriptor ldt; 332 int sel; 333 #endif 334 335 /* 336 * Fix the size of the static TLS block by using the maximum 337 * offset allocated so far and adding a bit for dynamic modules to 338 * use. 339 */ 340 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA; 341 tls = allocate_tls(objs, NULL, 2*sizeof(Elf_Addr), sizeof(Elf_Addr)); 342 343 #ifndef COMPAT_32BIT 344 memset(&ldt, 0, sizeof(ldt)); 345 ldt.sd.sd_lolimit = 0xffff; /* 4G limit */ 346 ldt.sd.sd_lobase = ((Elf_Addr)tls) & 0xffffff; 347 ldt.sd.sd_type = SDT_MEMRWA; 348 ldt.sd.sd_dpl = SEL_UPL; 349 ldt.sd.sd_p = 1; /* present */ 350 ldt.sd.sd_hilimit = 0xf; /* 4G limit */ 351 ldt.sd.sd_def32 = 1; /* 32 bit */ 352 ldt.sd.sd_gran = 1; /* limit in pages */ 353 ldt.sd.sd_hibase = (((Elf_Addr)tls) >> 24) & 0xff; 354 sel = i386_set_ldt(LDT_AUTO_ALLOC, &ldt, 1); 355 __asm __volatile("movl %0,%%gs" : : "rm" ((sel << 3) | 7)); 356 #else 357 _amd64_set_gsbase(tls); 358 #endif 359 } 360 361 /* GNU ABI */ 362 __attribute__((__regparm__(1))) 363 void *___tls_get_addr(tls_index *ti) 364 { 365 Elf_Addr** segbase; 366 Elf_Addr* dtv; 367 368 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase)); 369 dtv = segbase[1]; 370 371 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset); 372 } 373 374 /* Sun ABI */ 375 void *__tls_get_addr(tls_index *ti) 376 { 377 Elf_Addr** segbase; 378 Elf_Addr* dtv; 379 380 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase)); 381 dtv = segbase[1]; 382 383 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset); 384 } 385