HAMMER Utilities: Remove time/transaction-id conversion directives.
[dragonfly.git] / libexec / rtld-elf / i386 / reloc.c
blobb5ed9be14c909bafe2aac832805afb0ca9b24036
1 /*-
2 * Copyright 1996, 1997, 1998, 1999 John D. Polstra.
3 * All rights reserved.
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.
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.
25 * $FreeBSD: src/libexec/rtld-elf/i386/reloc.c,v 1.6.2.2 2002/06/16 20:02:09 dillon Exp $
26 * $DragonFly: src/libexec/rtld-elf/i386/reloc.c,v 1.13 2005/05/11 19:47:09 dillon Exp $
30 * Dynamic linker for ELF.
32 * John Polstra <jdp@polstra.com>.
35 #include <sys/param.h>
36 #include <sys/mman.h>
37 #include <sys/tls.h>
39 #include <machine/tls.h>
41 #include <dlfcn.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
51 #include "debug.h"
52 #include "rtld.h"
55 * Process the special R_386_COPY relocations in the main program. These
56 * copy data from a shared object into a region in the main program's BSS
57 * segment.
59 * Returns 0 on success, -1 on failure.
61 int
62 do_copy_relocations(Obj_Entry *dstobj)
64 const Elf_Rel *rellim;
65 const Elf_Rel *rel;
67 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
69 rellim = (const Elf_Rel *) ((c_caddr_t) dstobj->rel + dstobj->relsize);
70 for (rel = dstobj->rel; rel < rellim; rel++) {
71 if (ELF_R_TYPE(rel->r_info) == R_386_COPY) {
72 void *dstaddr;
73 const Elf_Sym *dstsym;
74 const char *name;
75 unsigned long hash;
76 size_t size;
77 const void *srcaddr;
78 const Elf_Sym *srcsym;
79 Obj_Entry *srcobj;
81 dstaddr = (void *) (dstobj->relocbase + rel->r_offset);
82 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
83 name = dstobj->strtab + dstsym->st_name;
84 hash = elf_hash(name);
85 size = dstsym->st_size;
87 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
88 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL)
89 break;
91 if (srcobj == NULL) {
92 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
93 " relocation in %s", name, dstobj->path);
94 return -1;
97 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
98 memcpy(dstaddr, srcaddr, size);
102 return 0;
105 /* Initialize the special GOT entries. */
106 void
107 init_pltgot(Obj_Entry *obj)
109 if (obj->pltgot != NULL) {
110 obj->pltgot[1] = (Elf_Addr) obj;
111 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
115 /* Process the non-PLT relocations. */
117 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld __unused)
119 const Elf_Rel *rellim;
120 const Elf_Rel *rel;
121 SymCache *cache;
122 int bytes = obj->nchains * sizeof(SymCache);
123 int r = -1;
126 * The dynamic loader may be called from a thread, we have
127 * limited amounts of stack available so we cannot use alloca().
129 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
130 if (cache == MAP_FAILED)
131 cache = NULL;
133 rellim = (const Elf_Rel *) ((c_caddr_t) obj->rel + obj->relsize);
134 for (rel = obj->rel; rel < rellim; rel++) {
135 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
137 switch (ELF_R_TYPE(rel->r_info)) {
139 case R_386_NONE:
140 break;
142 case R_386_32:
144 const Elf_Sym *def;
145 const Obj_Entry *defobj;
147 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
148 false, cache);
149 if (def == NULL)
150 goto done;
152 *where += (Elf_Addr) (defobj->relocbase + def->st_value);
154 break;
156 case R_386_PC32:
158 * I don't think the dynamic linker should ever see this
159 * type of relocation. But the binutils-2.6 tools sometimes
160 * generate it.
163 const Elf_Sym *def;
164 const Obj_Entry *defobj;
166 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
167 false, cache);
168 if (def == NULL)
169 goto done;
171 *where +=
172 (Elf_Addr) (defobj->relocbase + def->st_value) -
173 (Elf_Addr) where;
175 break;
177 case R_386_COPY:
179 * These are deferred until all other relocations have
180 * been done. All we do here is make sure that the COPY
181 * relocation is not in a shared library. They are allowed
182 * only in executable files.
184 if (!obj->mainprog) {
185 _rtld_error("%s: Unexpected R_386_COPY relocation"
186 " in shared library", obj->path);
187 goto done;
189 break;
191 case R_386_GLOB_DAT:
193 const Elf_Sym *def;
194 const Obj_Entry *defobj;
196 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
197 false, cache);
198 if (def == NULL)
199 goto done;
201 *where = (Elf_Addr) (defobj->relocbase + def->st_value);
203 break;
205 case R_386_RELATIVE:
206 *where += (Elf_Addr) obj->relocbase;
207 break;
209 case R_386_TLS_TPOFF:
211 const Elf_Sym *def;
212 const Obj_Entry *defobj;
214 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
215 false, cache);
216 if (def == NULL)
217 goto done;
220 * We lazily allocate offsets for static TLS as we
221 * see the first relocation that references the
222 * TLS block. This allows us to support (small
223 * amounts of) static TLS in dynamically loaded
224 * modules. If we run out of space, we generate an
225 * error.
227 if (!defobj->tls_done) {
228 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
229 _rtld_error("%s: No space available for static "
230 "Thread Local Storage", obj->path);
231 goto done;
235 *where += (Elf_Addr) (def->st_value - defobj->tlsoffset);
237 break;
239 case R_386_TLS_TPOFF32:
241 const Elf_Sym *def;
242 const Obj_Entry *defobj;
244 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
245 false, cache);
246 if (def == NULL)
247 goto done;
250 * We lazily allocate offsets for static TLS as we
251 * see the first relocation that references the
252 * TLS block. This allows us to support (small
253 * amounts of) static TLS in dynamically loaded
254 * modules. If we run out of space, we generate an
255 * error.
257 if (!defobj->tls_done) {
258 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
259 _rtld_error("%s: No space available for static "
260 "Thread Local Storage", obj->path);
261 goto done;
265 *where += (Elf_Addr) (defobj->tlsoffset - def->st_value);
267 break;
269 case R_386_TLS_DTPMOD32:
271 const Elf_Sym *def;
272 const Obj_Entry *defobj;
274 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
275 false, cache);
276 if (def == NULL)
277 goto done;
279 *where += (Elf_Addr) defobj->tlsindex;
281 break;
283 case R_386_TLS_DTPOFF32:
285 const Elf_Sym *def;
286 const Obj_Entry *defobj;
288 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
289 false, cache);
290 if (def == NULL)
291 goto done;
293 *where += (Elf_Addr) def->st_value;
295 break;
297 default:
298 _rtld_error("%s: Unsupported relocation type %d"
299 " in non-PLT relocations\n", obj->path,
300 ELF_R_TYPE(rel->r_info));
301 goto done;
304 r = 0;
305 done:
306 if (cache)
307 munmap(cache, bytes);
308 return(r);
311 /* Process the PLT relocations. */
313 reloc_plt(Obj_Entry *obj)
315 const Elf_Rel *rellim;
316 const Elf_Rel *rel;
318 rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
319 for (rel = obj->pltrel; rel < rellim; rel++) {
320 Elf_Addr *where;
322 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
324 /* Relocate the GOT slot pointing into the PLT. */
325 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
326 *where += (Elf_Addr)obj->relocbase;
328 return 0;
331 /* Relocate the jump slots in an object. */
333 reloc_jmpslots(Obj_Entry *obj)
335 const Elf_Rel *rellim;
336 const Elf_Rel *rel;
338 if (obj->jmpslots_done)
339 return 0;
340 rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
341 for (rel = obj->pltrel; rel < rellim; rel++) {
342 Elf_Addr *where;
343 const Elf_Sym *def;
344 const Obj_Entry *defobj;
346 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
347 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
348 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
349 if (def == NULL)
350 return -1;
351 reloc_jmpslot(where, (Elf_Addr)(defobj->relocbase + def->st_value));
353 obj->jmpslots_done = true;
354 return 0;
357 /* GNU ABI */
358 __attribute__((__regparm__(1)))
359 void *
360 ___tls_get_addr(tls_index *ti)
362 struct tls_tcb *tcb;
364 tcb = tls_get_tcb();
365 return tls_get_addr_common(&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
368 /* Sun ABI */
369 void *
370 __tls_get_addr(tls_index *ti)
372 struct tls_tcb *tcb;
374 tcb = tls_get_tcb();
375 return tls_get_addr_common(&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
378 /* Sun ABI */
379 void *
380 __tls_get_addr_tcb(struct tls_tcb *tcb, tls_index *ti)
382 return tls_get_addr_common(&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);