2 * Copyright 1996, 1997, 1998, 1999 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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/amd64/reloc.c,v 1.18 2006/03/28 06:09:24 davidxu Exp $
29 * Dynamic linker for ELF.
31 * John Polstra <jdp@polstra.com>.
34 #include <sys/param.h>
38 #include <machine/sysarch.h>
39 #include <machine/tls.h>
55 * Process the special R_X86_64_COPY relocations in the main program. These
56 * copy data from a shared object into a region in the main program's BSS
59 * Returns 0 on success, -1 on failure.
62 do_copy_relocations(Obj_Entry
*dstobj
)
64 const Elf_Rela
*relalim
;
67 assert(dstobj
->mainprog
); /* COPY relocations are invalid elsewhere */
69 relalim
= (const Elf_Rela
*) ((caddr_t
) dstobj
->rela
+ dstobj
->relasize
);
70 for (rela
= dstobj
->rela
; rela
< relalim
; rela
++) {
71 if (ELF_R_TYPE(rela
->r_info
) == R_X86_64_COPY
) {
73 const Elf_Sym
*dstsym
;
78 const Elf_Sym
*srcsym
;
81 dstaddr
= (void *) (dstobj
->relocbase
+ rela
->r_offset
);
82 dstsym
= dstobj
->symtab
+ ELF_R_SYM(rela
->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
)
92 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
93 " relocation in %s", name
, dstobj
->path
);
97 srcaddr
= (const void *) (srcobj
->relocbase
+ srcsym
->st_value
);
98 memcpy(dstaddr
, srcaddr
, size
);
105 /* Initialize the special GOT entries. */
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
)
119 const Elf_Rela
*relalim
;
120 const Elf_Rela
*rela
;
122 int bytes
= obj
->nchains
* sizeof(SymCache
);
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
)
133 relalim
= (const Elf_Rela
*) ((caddr_t
) obj
->rela
+ obj
->relasize
);
134 for (rela
= obj
->rela
; rela
< relalim
; rela
++) {
135 Elf_Addr
*where
= (Elf_Addr
*) (obj
->relocbase
+ rela
->r_offset
);
136 Elf32_Addr
*where32
= (Elf32_Addr
*)where
;
138 switch (ELF_R_TYPE(rela
->r_info
)) {
146 const Obj_Entry
*defobj
;
148 def
= find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
,
153 *where
= (Elf_Addr
) (defobj
->relocbase
+ def
->st_value
+ rela
->r_addend
);
159 * I don't think the dynamic linker should ever see this
160 * type of relocation. But the binutils-2.6 tools sometimes
165 const Obj_Entry
*defobj
;
167 def
= find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
,
172 *where32
= (Elf32_Addr
) (unsigned long) (defobj
->relocbase
+
173 def
->st_value
+ rela
->r_addend
- (Elf_Addr
) where
);
176 /* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
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.
185 if (!obj
->mainprog
) {
186 _rtld_error("%s: Unexpected R_X86_64_COPY relocation"
187 " in shared library", obj
->path
);
192 case R_X86_64_GLOB_DAT
:
195 const Obj_Entry
*defobj
;
197 def
= find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
,
202 *where
= (Elf_Addr
) (defobj
->relocbase
+ def
->st_value
);
206 case R_X86_64_TPOFF64
:
209 const Obj_Entry
*defobj
;
211 def
= find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
,
217 * We lazily allocate offsets for static TLS as we
218 * see the first relocation that references the
219 * TLS block. This allows us to support (small
220 * amounts of) static TLS in dynamically loaded
221 * modules. If we run out of space, we generate an
224 if (!defobj
->tls_done
) {
225 if (!allocate_tls_offset((Obj_Entry
*) defobj
)) {
226 _rtld_error("%s: No space available for static "
227 "Thread Local Storage", obj
->path
);
232 *where
= (Elf_Addr
) (def
->st_value
- defobj
->tlsoffset
+
237 case R_X86_64_TPOFF32
:
240 const Obj_Entry
*defobj
;
242 def
= find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
,
248 * We lazily allocate offsets for static TLS as we
249 * see the first relocation that references the
250 * TLS block. This allows us to support (small
251 * amounts of) static TLS in dynamically loaded
252 * modules. If we run out of space, we generate an
255 if (!defobj
->tls_done
) {
256 if (!allocate_tls_offset((Obj_Entry
*) defobj
)) {
257 _rtld_error("%s: No space available for static "
258 "Thread Local Storage", obj
->path
);
263 *where32
= (Elf32_Addr
) (def
->st_value
-
269 case R_X86_64_DTPMOD64
:
272 const Obj_Entry
*defobj
;
274 def
= find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
,
279 *where
+= (Elf_Addr
) defobj
->tlsindex
;
283 case R_X86_64_DTPOFF64
:
286 const Obj_Entry
*defobj
;
288 def
= find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
,
293 *where
+= (Elf_Addr
) (def
->st_value
+ rela
->r_addend
);
297 case R_X86_64_DTPOFF32
:
300 const Obj_Entry
*defobj
;
302 def
= find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
,
307 *where32
+= (Elf32_Addr
) (def
->st_value
+ rela
->r_addend
);
311 case R_X86_64_RELATIVE
:
312 *where
= (Elf_Addr
)(obj
->relocbase
+ rela
->r_addend
);
315 /* missing: R_X86_64_GOTPCREL, R_X86_64_32, R_X86_64_32S, R_X86_64_16, R_X86_64_PC16, R_X86_64_8, R_X86_64_PC8 */
318 _rtld_error("%s: Unsupported relocation type %u"
319 " in non-PLT relocations\n", obj
->path
,
320 (unsigned int)ELF_R_TYPE(rela
->r_info
));
327 munmap(cache
, bytes
);
331 /* Process the PLT relocations. */
333 reloc_plt(Obj_Entry
*obj
)
335 const Elf_Rela
*relalim
;
336 const Elf_Rela
*rela
;
338 relalim
= (const Elf_Rela
*)((char *)obj
->pltrela
+ obj
->pltrelasize
);
339 for (rela
= obj
->pltrela
; rela
< relalim
; rela
++) {
342 assert(ELF_R_TYPE(rela
->r_info
) == R_X86_64_JMP_SLOT
);
344 /* Relocate the GOT slot pointing into the PLT. */
345 where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
346 *where
+= (Elf_Addr
)obj
->relocbase
;
351 /* Relocate the jump slots in an object. */
353 reloc_jmpslots(Obj_Entry
*obj
)
355 const Elf_Rela
*relalim
;
356 const Elf_Rela
*rela
;
358 if (obj
->jmpslots_done
)
360 relalim
= (const Elf_Rela
*)((char *)obj
->pltrela
+ obj
->pltrelasize
);
361 for (rela
= obj
->pltrela
; rela
< relalim
; rela
++) {
362 Elf_Addr
*where
, target
;
364 const Obj_Entry
*defobj
;
366 assert(ELF_R_TYPE(rela
->r_info
) == R_X86_64_JMP_SLOT
);
367 where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
368 def
= find_symdef(ELF_R_SYM(rela
->r_info
), obj
, &defobj
, true, NULL
);
371 target
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+ rela
->r_addend
);
372 reloc_jmpslot(where
, target
);
374 obj
->jmpslots_done
= true;
378 void *__tls_get_addr(tls_index
*ti
)
383 return tls_get_addr_common(&tcb
->tcb_dtv
, ti
->ti_module
, ti
->ti_offset
);
387 __tls_get_addr_tcb(struct tls_tcb
*tcb
, tls_index
*ti
)
389 return tls_get_addr_common(&tcb
->tcb_dtv
, ti
->ti_module
, ti
->ti_offset
);