xref: /freebsd/libexec/rtld-elf/i386/reloc.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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_386_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_Rel *rellim;
61     const Elf_Rel *rel;
62 
63     assert(dstobj->mainprog);	/* COPY relocations are invalid elsewhere */
64 
65     rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
66     for (rel = dstobj->rel;  rel < rellim;  rel++) {
67 	if (ELF_R_TYPE(rel->r_info) == R_386_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 + rel->r_offset);
78 	    dstsym = dstobj->symtab + ELF_R_SYM(rel->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_Rel *rellim;
116 	const Elf_Rel *rel;
117 	SymCache *cache;
118 
119 	cache = (SymCache *)alloca(obj->nchains * sizeof(SymCache));
120 	if (cache != NULL)
121 	    memset(cache, 0, obj->nchains * sizeof(SymCache));
122 
123 	rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
124 	for (rel = obj->rel;  rel < rellim;  rel++) {
125 	    Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
126 
127 	    switch (ELF_R_TYPE(rel->r_info)) {
128 
129 	    case R_386_NONE:
130 		break;
131 
132 	    case R_386_32:
133 		{
134 		    const Elf_Sym *def;
135 		    const Obj_Entry *defobj;
136 
137 		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
138 		      false, cache);
139 		    if (def == NULL)
140 			return -1;
141 
142 		    *where += (Elf_Addr) (defobj->relocbase + def->st_value);
143 		}
144 		break;
145 
146 	    case R_386_PC32:
147 		/*
148 		 * I don't think the dynamic linker should ever see this
149 		 * type of relocation.  But the binutils-2.6 tools sometimes
150 		 * generate it.
151 		 */
152 		{
153 		    const Elf_Sym *def;
154 		    const Obj_Entry *defobj;
155 
156 		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
157 		      false, cache);
158 		    if (def == NULL)
159 			return -1;
160 
161 		    *where +=
162 		      (Elf_Addr) (defobj->relocbase + def->st_value) -
163 		      (Elf_Addr) where;
164 		}
165 		break;
166 
167 	    case R_386_COPY:
168 		/*
169 		 * These are deferred until all other relocations have
170 		 * been done.  All we do here is make sure that the COPY
171 		 * relocation is not in a shared library.  They are allowed
172 		 * only in executable files.
173 		 */
174 		if (!obj->mainprog) {
175 		    _rtld_error("%s: Unexpected R_386_COPY relocation"
176 		      " in shared library", obj->path);
177 		    return -1;
178 		}
179 		break;
180 
181 	    case R_386_GLOB_DAT:
182 		{
183 		    const Elf_Sym *def;
184 		    const Obj_Entry *defobj;
185 
186 		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
187 		      false, cache);
188 		    if (def == NULL)
189 			return -1;
190 
191 		    *where = (Elf_Addr) (defobj->relocbase + def->st_value);
192 		}
193 		break;
194 
195 	    case R_386_RELATIVE:
196 		*where += (Elf_Addr) obj->relocbase;
197 		break;
198 
199 	    default:
200 		_rtld_error("%s: Unsupported relocation type %d"
201 		  " in non-PLT relocations\n", obj->path,
202 		  ELF_R_TYPE(rel->r_info));
203 		return -1;
204 	    }
205 	}
206     return 0;
207 }
208 
209 /* Process the PLT relocations. */
210 int
211 reloc_plt(Obj_Entry *obj)
212 {
213     const Elf_Rel *rellim;
214     const Elf_Rel *rel;
215 
216     rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
217     for (rel = obj->pltrel;  rel < rellim;  rel++) {
218 	Elf_Addr *where;
219 
220 	assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
221 
222 	/* Relocate the GOT slot pointing into the PLT. */
223 	where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
224 	*where += (Elf_Addr)obj->relocbase;
225     }
226     return 0;
227 }
228 
229 /* Relocate the jump slots in an object. */
230 int
231 reloc_jmpslots(Obj_Entry *obj)
232 {
233     const Elf_Rel *rellim;
234     const Elf_Rel *rel;
235 
236     if (obj->jmpslots_done)
237 	return 0;
238     rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
239     for (rel = obj->pltrel;  rel < rellim;  rel++) {
240 	Elf_Addr *where;
241 	const Elf_Sym *def;
242 	const Obj_Entry *defobj;
243 
244 	assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
245 	where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
246 	def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
247 	if (def == NULL)
248 	    return -1;
249 	reloc_jmpslot(where, (Elf_Addr)(defobj->relocbase + def->st_value));
250     }
251     obj->jmpslots_done = true;
252     return 0;
253 }
254