reloc.c revision 50476
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 50476 1999-08-28 00:22:10Z peter $
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 * Debugging support.
52 */
53
54#define assert(cond)	((cond) ? (void) 0 :\
55    (msg("oops: " __XSTRING(__LINE__) "\n"), abort()))
56#define msg(s)		(write(1, s, strlen(s)))
57#define trace()		msg("trace: " __XSTRING(__LINE__) "\n");
58
59/*
60 * Process the special R_386_COPY relocations in the main program.  These
61 * copy data from a shared object into a region in the main program's BSS
62 * segment.
63 *
64 * Returns 0 on success, -1 on failure.
65 */
66int
67do_copy_relocations(Obj_Entry *dstobj)
68{
69    const Elf_Rel *rellim;
70    const Elf_Rel *rel;
71
72    assert(dstobj->mainprog);	/* COPY relocations are invalid elsewhere */
73
74    rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
75    for (rel = dstobj->rel;  rel < rellim;  rel++) {
76	if (ELF_R_TYPE(rel->r_info) == R_386_COPY) {
77	    void *dstaddr;
78	    const Elf_Sym *dstsym;
79	    const char *name;
80	    unsigned long hash;
81	    size_t size;
82	    const void *srcaddr;
83	    const Elf_Sym *srcsym;
84	    Obj_Entry *srcobj;
85
86	    dstaddr = (void *) (dstobj->relocbase + rel->r_offset);
87	    dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
88	    name = dstobj->strtab + dstsym->st_name;
89	    hash = elf_hash(name);
90	    size = dstsym->st_size;
91
92	    for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next)
93		if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL)
94		    break;
95
96	    if (srcobj == NULL) {
97		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
98		  " relocation in %s", name, dstobj->path);
99		return -1;
100	    }
101
102	    srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
103	    memcpy(dstaddr, srcaddr, size);
104	}
105    }
106
107    return 0;
108}
109
110/* Initialize the special GOT entries. */
111void
112init_pltgot(Obj_Entry *obj)
113{
114    if (obj->pltgot != NULL) {
115	obj->pltgot[1] = (Elf_Addr) obj;
116	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
117    }
118}
119
120/* Process the non-PLT relocations. */
121int
122reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
123{
124	const Elf_Rel *rellim;
125	const Elf_Rel *rel;
126
127	rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
128	for (rel = obj->rel;  rel < rellim;  rel++) {
129	    Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
130
131	    switch (ELF_R_TYPE(rel->r_info)) {
132
133	    case R_386_NONE:
134		break;
135
136	    case R_386_32:
137		{
138		    const Elf_Sym *def;
139		    const Obj_Entry *defobj;
140
141		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
142		      false);
143		    if (def == NULL)
144			return -1;
145
146		    *where += (Elf_Addr) (defobj->relocbase + def->st_value);
147		}
148		break;
149
150	    case R_386_PC32:
151		/*
152		 * I don't think the dynamic linker should ever see this
153		 * type of relocation.  But the binutils-2.6 tools sometimes
154		 * generate it.
155		 */
156		{
157		    const Elf_Sym *def;
158		    const Obj_Entry *defobj;
159
160		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
161		      false);
162		    if (def == NULL)
163			return -1;
164
165		    *where +=
166		      (Elf_Addr) (defobj->relocbase + def->st_value) -
167		      (Elf_Addr) where;
168		}
169		break;
170
171	    case R_386_COPY:
172		/*
173		 * These are deferred until all other relocations have
174		 * been done.  All we do here is make sure that the COPY
175		 * relocation is not in a shared library.  They are allowed
176		 * only in executable files.
177		 */
178		if (!obj->mainprog) {
179		    _rtld_error("%s: Unexpected R_386_COPY relocation"
180		      " in shared library", obj->path);
181		    return -1;
182		}
183		break;
184
185	    case R_386_GLOB_DAT:
186		{
187		    const Elf_Sym *def;
188		    const Obj_Entry *defobj;
189
190		    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
191		      false);
192		    if (def == NULL)
193			return -1;
194
195		    *where = (Elf_Addr) (defobj->relocbase + def->st_value);
196		}
197		break;
198
199	    case R_386_RELATIVE:
200		*where += (Elf_Addr) obj->relocbase;
201		break;
202
203	    default:
204		_rtld_error("%s: Unsupported relocation type %d"
205		  " in non-PLT relocations\n", obj->path,
206		  ELF_R_TYPE(rel->r_info));
207		return -1;
208	    }
209	}
210    return 0;
211}
212
213/* Process the PLT relocations. */
214int
215reloc_plt(Obj_Entry *obj, bool bind_now)
216{
217    const Elf_Rel *rellim;
218    const Elf_Rel *rel;
219
220    rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
221    for (rel = obj->pltrel;  rel < rellim;  rel++) {
222	Elf_Addr *where;
223
224	assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
225
226	/* Relocate the GOT slot pointing into the PLT. */
227	where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
228	*where += (Elf_Addr)obj->relocbase;
229
230	if (bind_now) {	/* Fully resolve the procedure address. */
231	    const Elf_Sym *def;
232	    const Obj_Entry *defobj;
233
234	    def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
235	    if (def == NULL)
236		return -1;
237	    reloc_jmpslot(where,
238	      (Elf_Addr)(defobj->relocbase + def->st_value));
239	}
240    }
241    return 0;
242}
243