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