xref: /freebsd/libexec/rtld-elf/amd64/reloc.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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 
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  * Process the special R_X86_64_COPY relocations in the main program.  These
52  * copy data from a shared object into a region in the main program's BSS
53  * segment.
54  *
55  * Returns 0 on success, -1 on failure.
56  */
57 int
58 do_copy_relocations(Obj_Entry *dstobj)
59 {
60     const Elf_Rela *relalim;
61     const Elf_Rela *rela;
62 
63     assert(dstobj->mainprog);	/* COPY relocations are invalid elsewhere */
64 
65     relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela + dstobj->relasize);
66     for (rela = dstobj->rela;  rela < relalim;  rela++) {
67 	if (ELF_R_TYPE(rela->r_info) == R_X86_64_COPY) {
68 	    void *dstaddr;
69 	    const Elf_Sym *dstsym;
70 	    const char *name;
71 	    unsigned long hash;
72 	    size_t size;
73 	    const void *srcaddr;
74 	    const Elf_Sym *srcsym;
75 	    Obj_Entry *srcobj;
76 
77 	    dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
78 	    dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
79 	    name = dstobj->strtab + dstsym->st_name;
80 	    hash = elf_hash(name);
81 	    size = dstsym->st_size;
82 
83 	    for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next)
84 		if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL)
85 		    break;
86 
87 	    if (srcobj == NULL) {
88 		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
89 		  " relocation in %s", name, dstobj->path);
90 		return -1;
91 	    }
92 
93 	    srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
94 	    memcpy(dstaddr, srcaddr, size);
95 	}
96     }
97 
98     return 0;
99 }
100 
101 /* Initialize the special GOT entries. */
102 void
103 init_pltgot(Obj_Entry *obj)
104 {
105     if (obj->pltgot != NULL) {
106 	obj->pltgot[1] = (Elf_Addr) obj;
107 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
108     }
109 }
110 
111 /* Process the non-PLT relocations. */
112 int
113 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
114 {
115 	const Elf_Rela *relalim;
116 	const Elf_Rela *rela;
117 	SymCache *cache;
118 	int bytes = obj->nchains * sizeof(SymCache);
119 	int r = -1;
120 
121 	/*
122 	 * The dynamic loader may be called from a thread, we have
123 	 * limited amounts of stack available so we cannot use alloca().
124 	 */
125 	cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
126 	if (cache == MAP_FAILED)
127 	    cache = NULL;
128 
129 	relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
130 	for (rela = obj->rela;  rela < relalim;  rela++) {
131 	    Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
132 	    Elf32_Addr *where32 = (Elf32_Addr *)where;
133 
134 	    switch (ELF_R_TYPE(rela->r_info)) {
135 
136 	    case R_X86_64_NONE:
137 		break;
138 
139 	    case R_X86_64_64:
140 		{
141 		    const Elf_Sym *def;
142 		    const Obj_Entry *defobj;
143 
144 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
145 		      false, cache);
146 		    if (def == NULL)
147 			goto done;
148 
149 		    *where = (Elf_Addr) (defobj->relocbase + def->st_value + rela->r_addend);
150 		}
151 		break;
152 
153 	    case R_X86_64_PC32:
154 		/*
155 		 * I don't think the dynamic linker should ever see this
156 		 * type of relocation.  But the binutils-2.6 tools sometimes
157 		 * generate it.
158 		 */
159 		{
160 		    const Elf_Sym *def;
161 		    const Obj_Entry *defobj;
162 
163 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
164 		      false, cache);
165 		    if (def == NULL)
166 			goto done;
167 
168 		    *where32 = (Elf32_Addr) (unsigned long) (defobj->relocbase +
169 		        def->st_value + rela->r_addend - (Elf_Addr) where);
170 		}
171 		break;
172 	/* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
173 
174 	    case R_X86_64_COPY:
175 		/*
176 		 * These are deferred until all other relocations have
177 		 * been done.  All we do here is make sure that the COPY
178 		 * relocation is not in a shared library.  They are allowed
179 		 * only in executable files.
180 		 */
181 		if (!obj->mainprog) {
182 		    _rtld_error("%s: Unexpected R_X86_64_COPY relocation"
183 		      " in shared library", obj->path);
184 		    goto done;
185 		}
186 		break;
187 
188 	    case R_X86_64_GLOB_DAT:
189 		{
190 		    const Elf_Sym *def;
191 		    const Obj_Entry *defobj;
192 
193 		    def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
194 		      false, cache);
195 		    if (def == NULL)
196 			goto done;
197 
198 		    *where = (Elf_Addr) (defobj->relocbase + def->st_value);
199 		}
200 		break;
201 
202 	    case R_X86_64_RELATIVE:
203 		*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
204 		break;
205 
206 	/* 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 */
207 
208 	    default:
209 		_rtld_error("%s: Unsupported relocation type %d"
210 		  " in non-PLT relocations\n", obj->path,
211 		  ELF_R_TYPE(rela->r_info));
212 		goto done;
213 	    }
214 	}
215 	r = 0;
216 done:
217 	if (cache)
218 	    munmap(cache, bytes);
219 	return(r);
220 }
221 
222 /* Process the PLT relocations. */
223 int
224 reloc_plt(Obj_Entry *obj)
225 {
226     const Elf_Rela *relalim;
227     const Elf_Rela *rela;
228 
229     relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
230     for (rela = obj->pltrela;  rela < relalim;  rela++) {
231 	Elf_Addr *where;
232 
233 	assert(ELF_R_TYPE(rela->r_info) == R_X86_64_JMP_SLOT);
234 
235 	/* Relocate the GOT slot pointing into the PLT. */
236 	where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
237 	*where += (Elf_Addr)obj->relocbase;
238     }
239     return 0;
240 }
241 
242 /* Relocate the jump slots in an object. */
243 int
244 reloc_jmpslots(Obj_Entry *obj)
245 {
246     const Elf_Rela *relalim;
247     const Elf_Rela *rela;
248 
249     if (obj->jmpslots_done)
250 	return 0;
251     relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
252     for (rela = obj->pltrela;  rela < relalim;  rela++) {
253 	Elf_Addr *where, target;
254 	const Elf_Sym *def;
255 	const Obj_Entry *defobj;
256 
257 	assert(ELF_R_TYPE(rela->r_info) == R_X86_64_JMP_SLOT);
258 	where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
259 	def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true, NULL);
260 	if (def == NULL)
261 	    return -1;
262 	target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
263 	reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
264     }
265     obj->jmpslots_done = true;
266     return 0;
267 }
268