xref: /freebsd/stand/common/reloc_elf.c (revision 4d846d26)
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(__aarch64__) || defined(__amd64__) || defined(__i386__)) && \
56     __ELF_WORD_SIZE == 64
57 	Elf64_Addr *where, val;
58 	Elf_Addr addend, addr;
59 	Elf_Size rtype;
60 #if defined(__amd64__) || defined(__i386__)
61 	Elf_Size symidx;
62 #endif
63 	const Elf_Rel *rel;
64 	const Elf_Rela *rela;
65 
66 	switch (reltype) {
67 	case ELF_RELOC_REL:
68 		rel = (const Elf_Rel *)reldata;
69 		where = (Elf_Addr *)((char *)data + relbase + rel->r_offset -
70 		    dataaddr);
71 		addend = 0;
72 		rtype = ELF_R_TYPE(rel->r_info);
73 #if defined(__amd64__) || defined(__i386__)
74 		symidx = ELF_R_SYM(rel->r_info);
75 #endif
76 		addend = 0;
77 		break;
78 	case ELF_RELOC_RELA:
79 		rela = (const Elf_Rela *)reldata;
80 		where = (Elf_Addr *)((char *)data + relbase + rela->r_offset -
81 		    dataaddr);
82 		addend = rela->r_addend;
83 		rtype = ELF_R_TYPE(rela->r_info);
84 #if defined(__amd64__) || defined(__i386__)
85 		symidx = ELF_R_SYM(rela->r_info);
86 #endif
87 		break;
88 	default:
89 		return (EINVAL);
90 	}
91 
92 	if ((char *)where < (char *)data || (char *)where >= (char *)data + len)
93 		return (0);
94 
95 	if (reltype == ELF_RELOC_REL)
96 		addend = *where;
97 
98 #if defined(__aarch64__)
99 #define	RELOC_RELATIVE		R_AARCH64_RELATIVE
100 #define	RELOC_IRELATIVE		R_AARCH64_IRELATIVE
101 #elif defined(__amd64__) || defined(__i386__)
102 /* XXX, definitions not available on i386. */
103 #define	R_X86_64_64		1
104 #define	R_X86_64_RELATIVE	8
105 #define	R_X86_64_IRELATIVE	37
106 
107 #define	RELOC_RELATIVE		R_X86_64_RELATIVE
108 #define	RELOC_IRELATIVE		R_X86_64_IRELATIVE
109 #endif
110 
111 	switch (rtype) {
112 	case RELOC_RELATIVE:
113 		addr = (Elf_Addr)addend + relbase;
114 		val = addr;
115 		memcpy(where, &val, sizeof(val));
116 		break;
117 	case RELOC_IRELATIVE:
118 		/* leave it to kernel */
119 		break;
120 #if defined(__amd64__) || defined(__i386__)
121 	case R_X86_64_64:		/* S + A */
122 		addr = symaddr(ef, symidx);
123 		if (addr == 0)
124 			return (ESRCH);
125 		val = addr + addend;
126 		*where = val;
127 		break;
128 #endif
129 	default:
130 		printf("\nunhandled relocation type %u\n", (u_int)rtype);
131 		return (EFTYPE);
132 	}
133 
134 	return (0);
135 #elif defined(__i386__) && __ELF_WORD_SIZE == 32
136 	Elf_Addr addend, addr, *where, val;
137 	Elf_Size rtype, symidx;
138 	const Elf_Rel *rel;
139 	const Elf_Rela *rela;
140 
141 	switch (reltype) {
142 	case ELF_RELOC_REL:
143 		rel = (const Elf_Rel *)reldata;
144 		where = (Elf_Addr *)((char *)data + relbase + rel->r_offset -
145 		    dataaddr);
146 		addend = 0;
147 		rtype = ELF_R_TYPE(rel->r_info);
148 		symidx = ELF_R_SYM(rel->r_info);
149 		addend = 0;
150 		break;
151 	case ELF_RELOC_RELA:
152 		rela = (const Elf_Rela *)reldata;
153 		where = (Elf_Addr *)((char *)data + relbase + rela->r_offset -
154 		    dataaddr);
155 		addend = rela->r_addend;
156 		rtype = ELF_R_TYPE(rela->r_info);
157 		symidx = ELF_R_SYM(rela->r_info);
158 		break;
159 	default:
160 		return (EINVAL);
161 	}
162 
163 	if ((char *)where < (char *)data || (char *)where >= (char *)data + len)
164 		return (0);
165 
166 	if (reltype == ELF_RELOC_REL)
167 		addend = *where;
168 
169 /* XXX, definitions not available on amd64. */
170 #define R_386_32	1	/* Add symbol value. */
171 #define R_386_GLOB_DAT	6	/* Set GOT entry to data address. */
172 #define R_386_RELATIVE	8	/* Add load address of shared object. */
173 #define	R_386_IRELATIVE	42
174 
175 	switch (rtype) {
176 	case R_386_RELATIVE:
177 		addr = addend + relbase;
178 		*where = addr;
179 		break;
180 	case R_386_32:		/* S + A */
181 		addr = symaddr(ef, symidx);
182 		if (addr == 0)
183 			return (ESRCH);
184 		val = addr + addend;
185 		*where = val;
186 		break;
187 	case R_386_IRELATIVE:
188 		/* leave it to kernel */
189 		break;
190 	default:
191 		printf("\nunhandled relocation type %u\n", (u_int)rtype);
192 		return (EFTYPE);
193 	}
194 
195 	return (0);
196 #elif defined(__powerpc__) || defined(__riscv)
197 	Elf_Size w;
198 	const Elf_Rela *rela;
199 
200 	switch (reltype) {
201 	case ELF_RELOC_RELA:
202 		rela = reldata;
203 		if (relbase + rela->r_offset >= dataaddr &&
204 		    relbase + rela->r_offset < dataaddr + len) {
205 			switch (ELF_R_TYPE(rela->r_info)) {
206 #if defined(__powerpc__)
207 			case R_PPC_RELATIVE:
208 #elif defined(__riscv)
209 			case R_RISCV_RELATIVE:
210 #endif
211 				w = relbase + rela->r_addend;
212 				bcopy(&w, (u_char *)data + (relbase +
213 				      rela->r_offset - dataaddr), sizeof(w));
214 				break;
215 			default:
216 				printf("\nunhandled relocation type %u\n",
217 				       (u_int)ELF_R_TYPE(rela->r_info));
218 				return (EFTYPE);
219 			}
220 		}
221 		break;
222 	}
223 
224 	return (0);
225 #else
226 	return (EOPNOTSUPP);
227 #endif
228 }
229