xref: /freebsd/stand/common/reloc_elf.c (revision e17f5b1d)
1 /*-
2  * Copyright (c) 2003 Jake Burkholder.
3  * Copyright 1996-1998 John D. Polstra.
4  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5  * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/types.h>
34 #include <machine/elf.h>
35 
36 #include <stand.h>
37 
38 #define FREEBSD_ELF
39 #include <sys/link_elf.h>
40 
41 #include "bootstrap.h"
42 
43 #define COPYOUT(s,d,l)	archsw.arch_copyout((vm_offset_t)(s), d, l)
44 
45 /*
46  * Apply a single intra-module relocation to the data. `relbase' is the
47  * target relocation base for the section (i.e. it corresponds to where
48  * r_offset == 0). `dataaddr' is the relocated address corresponding to
49  * the start of the data, and `len' is the number of bytes.
50  */
51 int
52 __elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata,
53     int reltype, Elf_Addr relbase, Elf_Addr dataaddr, void *data, size_t len)
54 {
55 #if (defined(__i386__) || defined(__amd64__)) && __ELF_WORD_SIZE == 64
56 	Elf64_Addr *where, val;
57 	Elf_Addr addend, addr;
58 	Elf_Size rtype, symidx;
59 	const Elf_Rel *rel;
60 	const Elf_Rela *rela;
61 
62 	switch (reltype) {
63 	case ELF_RELOC_REL:
64 		rel = (const Elf_Rel *)reldata;
65 		where = (Elf_Addr *)((char *)data + relbase + rel->r_offset -
66 		    dataaddr);
67 		addend = 0;
68 		rtype = ELF_R_TYPE(rel->r_info);
69 		symidx = ELF_R_SYM(rel->r_info);
70 		addend = 0;
71 		break;
72 	case ELF_RELOC_RELA:
73 		rela = (const Elf_Rela *)reldata;
74 		where = (Elf_Addr *)((char *)data + relbase + rela->r_offset -
75 		    dataaddr);
76 		addend = rela->r_addend;
77 		rtype = ELF_R_TYPE(rela->r_info);
78 		symidx = ELF_R_SYM(rela->r_info);
79 		break;
80 	default:
81 		return (EINVAL);
82 	}
83 
84 	if ((char *)where < (char *)data || (char *)where >= (char *)data + len)
85 		return (0);
86 
87 	if (reltype == ELF_RELOC_REL)
88 		addend = *where;
89 
90 /* XXX, definitions not available on i386. */
91 #define	R_X86_64_64		1
92 #define	R_X86_64_RELATIVE	8
93 #define	R_X86_64_IRELATIVE	37
94 
95 	switch (rtype) {
96 	case R_X86_64_64:		/* S + A */
97 		addr = symaddr(ef, symidx);
98 		if (addr == 0)
99 			return (ESRCH);
100 		val = addr + addend;
101 		*where = val;
102 		break;
103 	case R_X86_64_RELATIVE:
104 		addr = (Elf_Addr)addend + relbase;
105 		val = addr;
106 		*where = val;
107 		break;
108 	case R_X86_64_IRELATIVE:
109 		/* leave it to kernel */
110 		break;
111 	default:
112 		printf("\nunhandled relocation type %u\n", (u_int)rtype);
113 		return (EFTYPE);
114 	}
115 
116 	return (0);
117 #elif defined(__i386__) && __ELF_WORD_SIZE == 32
118 	Elf_Addr addend, addr, *where, val;
119 	Elf_Size rtype, symidx;
120 	const Elf_Rel *rel;
121 	const Elf_Rela *rela;
122 
123 	switch (reltype) {
124 	case ELF_RELOC_REL:
125 		rel = (const Elf_Rel *)reldata;
126 		where = (Elf_Addr *)((char *)data + relbase + rel->r_offset -
127 		    dataaddr);
128 		addend = 0;
129 		rtype = ELF_R_TYPE(rel->r_info);
130 		symidx = ELF_R_SYM(rel->r_info);
131 		addend = 0;
132 		break;
133 	case ELF_RELOC_RELA:
134 		rela = (const Elf_Rela *)reldata;
135 		where = (Elf_Addr *)((char *)data + relbase + rela->r_offset -
136 		    dataaddr);
137 		addend = rela->r_addend;
138 		rtype = ELF_R_TYPE(rela->r_info);
139 		symidx = ELF_R_SYM(rela->r_info);
140 		break;
141 	default:
142 		return (EINVAL);
143 	}
144 
145 	if ((char *)where < (char *)data || (char *)where >= (char *)data + len)
146 		return (0);
147 
148 	if (reltype == ELF_RELOC_REL)
149 		addend = *where;
150 
151 /* XXX, definitions not available on amd64. */
152 #define R_386_32	1	/* Add symbol value. */
153 #define R_386_GLOB_DAT	6	/* Set GOT entry to data address. */
154 #define R_386_RELATIVE	8	/* Add load address of shared object. */
155 #define	R_386_IRELATIVE	42
156 
157 	switch (rtype) {
158 	case R_386_RELATIVE:
159 		addr = addend + relbase;
160 		*where = addr;
161 		break;
162 	case R_386_32:		/* S + A */
163 		addr = symaddr(ef, symidx);
164 		if (addr == 0)
165 			return (ESRCH);
166 		val = addr + addend;
167 		*where = val;
168 		break;
169 	case R_386_IRELATIVE:
170 		/* leave it to kernel */
171 		break;
172 	default:
173 		printf("\nunhandled relocation type %u\n", (u_int)rtype);
174 		return (EFTYPE);
175 	}
176 
177 	return (0);
178 #elif defined(__powerpc__)
179 	Elf_Size w;
180 	const Elf_Rela *rela;
181 
182 	switch (reltype) {
183 	case ELF_RELOC_RELA:
184 		rela = reldata;
185 		if (relbase + rela->r_offset >= dataaddr &&
186 		    relbase + rela->r_offset < dataaddr + len) {
187 			switch (ELF_R_TYPE(rela->r_info)) {
188 			case R_PPC_RELATIVE:
189 				w = relbase + rela->r_addend;
190 				bcopy(&w, (u_char *)data + (relbase +
191 				      rela->r_offset - dataaddr), sizeof(w));
192 				break;
193 			default:
194 				printf("\nunhandled relocation type %u\n",
195 				       (u_int)ELF_R_TYPE(rela->r_info));
196 				return (EFTYPE);
197 			}
198 		}
199 		break;
200 	}
201 
202 	return (0);
203 #else
204 	return (EOPNOTSUPP);
205 #endif
206 }
207