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