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