]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/common/reloc_elf.c
MFV: zlib 1.3
[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 #include <sys/types.h>
32 #include <machine/elf.h>
33
34 #include <stand.h>
35
36 #define FREEBSD_ELF
37 #include <sys/link_elf.h>
38
39 #include "bootstrap.h"
40
41 #define COPYOUT(s,d,l)  archsw.arch_copyout((vm_offset_t)(s), d, l)
42
43 /*
44  * Apply a single intra-module relocation to the data. `relbase' is the
45  * target relocation base for the section (i.e. it corresponds to where
46  * r_offset == 0). `dataaddr' is the relocated address corresponding to
47  * the start of the data, and `len' is the number of bytes.
48  */
49 int
50 __elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata,
51     int reltype, Elf_Addr relbase, Elf_Addr dataaddr, void *data, size_t len)
52 {
53 #if (defined(__aarch64__) || defined(__amd64__) || defined(__i386__)) && \
54     __ELF_WORD_SIZE == 64
55         Elf64_Addr *where, val;
56         Elf_Addr addend, addr;
57         Elf_Size rtype;
58 #if defined(__amd64__) || defined(__i386__)
59         Elf_Size symidx;
60 #endif
61         const Elf_Rel *rel;
62         const Elf_Rela *rela;
63
64         switch (reltype) {
65         case ELF_RELOC_REL:
66                 rel = (const Elf_Rel *)reldata;
67                 where = (Elf_Addr *)((char *)data + relbase + rel->r_offset -
68                     dataaddr);
69                 addend = 0;
70                 rtype = ELF_R_TYPE(rel->r_info);
71 #if defined(__amd64__) || defined(__i386__)
72                 symidx = ELF_R_SYM(rel->r_info);
73 #endif
74                 addend = 0;
75                 break;
76         case ELF_RELOC_RELA:
77                 rela = (const Elf_Rela *)reldata;
78                 where = (Elf_Addr *)((char *)data + relbase + rela->r_offset -
79                     dataaddr);
80                 addend = rela->r_addend;
81                 rtype = ELF_R_TYPE(rela->r_info);
82 #if defined(__amd64__) || defined(__i386__)
83                 symidx = ELF_R_SYM(rela->r_info);
84 #endif
85                 break;
86         default:
87                 return (EINVAL);
88         }
89
90         if ((char *)where < (char *)data || (char *)where >= (char *)data + len)
91                 return (0);
92
93         if (reltype == ELF_RELOC_REL)
94                 addend = *where;
95
96 #if defined(__aarch64__)
97 #define RELOC_RELATIVE          R_AARCH64_RELATIVE
98 #define RELOC_IRELATIVE         R_AARCH64_IRELATIVE
99 #elif defined(__amd64__) || defined(__i386__)
100 /* XXX, definitions not available on i386. */
101 #define R_X86_64_64             1
102 #define R_X86_64_RELATIVE       8
103 #define R_X86_64_IRELATIVE      37
104
105 #define RELOC_RELATIVE          R_X86_64_RELATIVE
106 #define RELOC_IRELATIVE         R_X86_64_IRELATIVE
107 #endif
108
109         switch (rtype) {
110         case RELOC_RELATIVE:
111                 addr = (Elf_Addr)addend + relbase;
112                 val = addr;
113                 memcpy(where, &val, sizeof(val));
114                 break;
115         case RELOC_IRELATIVE:
116                 /* leave it to kernel */
117                 break;
118 #if defined(__amd64__) || defined(__i386__)
119         case R_X86_64_64:               /* S + A */
120                 addr = symaddr(ef, symidx);
121                 if (addr == 0)
122                         return (ESRCH);
123                 val = addr + addend;
124                 *where = val;
125                 break;
126 #endif
127         default:
128                 printf("\nunhandled relocation type %u\n", (u_int)rtype);
129                 return (EFTYPE);
130         }
131
132         return (0);
133 #elif defined(__i386__) && __ELF_WORD_SIZE == 32
134         Elf_Addr addend, addr, *where, val;
135         Elf_Size rtype, symidx;
136         const Elf_Rel *rel;
137         const Elf_Rela *rela;
138
139         switch (reltype) {
140         case ELF_RELOC_REL:
141                 rel = (const Elf_Rel *)reldata;
142                 where = (Elf_Addr *)((char *)data + relbase + rel->r_offset -
143                     dataaddr);
144                 addend = 0;
145                 rtype = ELF_R_TYPE(rel->r_info);
146                 symidx = ELF_R_SYM(rel->r_info);
147                 addend = 0;
148                 break;
149         case ELF_RELOC_RELA:
150                 rela = (const Elf_Rela *)reldata;
151                 where = (Elf_Addr *)((char *)data + relbase + rela->r_offset -
152                     dataaddr);
153                 addend = rela->r_addend;
154                 rtype = ELF_R_TYPE(rela->r_info);
155                 symidx = ELF_R_SYM(rela->r_info);
156                 break;
157         default:
158                 return (EINVAL);
159         }
160
161         if ((char *)where < (char *)data || (char *)where >= (char *)data + len)
162                 return (0);
163
164         if (reltype == ELF_RELOC_REL)
165                 addend = *where;
166
167 /* XXX, definitions not available on amd64. */
168 #define R_386_32        1       /* Add symbol value. */
169 #define R_386_GLOB_DAT  6       /* Set GOT entry to data address. */
170 #define R_386_RELATIVE  8       /* Add load address of shared object. */
171 #define R_386_IRELATIVE 42
172
173         switch (rtype) {
174         case R_386_RELATIVE:
175                 addr = addend + relbase;
176                 *where = addr;
177                 break;
178         case R_386_32:          /* S + A */
179                 addr = symaddr(ef, symidx);
180                 if (addr == 0)
181                         return (ESRCH);
182                 val = addr + addend;
183                 *where = val;
184                 break;
185         case R_386_IRELATIVE:
186                 /* leave it to kernel */
187                 break;
188         default:
189                 printf("\nunhandled relocation type %u\n", (u_int)rtype);
190                 return (EFTYPE);
191         }
192
193         return (0);
194 #elif defined(__powerpc__) || defined(__riscv)
195         Elf_Size w;
196         const Elf_Rela *rela;
197
198         switch (reltype) {
199         case ELF_RELOC_RELA:
200                 rela = reldata;
201                 if (relbase + rela->r_offset >= dataaddr &&
202                     relbase + rela->r_offset < dataaddr + len) {
203                         switch (ELF_R_TYPE(rela->r_info)) {
204 #if defined(__powerpc__)
205                         case R_PPC_RELATIVE:
206 #elif defined(__riscv)
207                         case R_RISCV_RELATIVE:
208 #endif
209                                 w = relbase + rela->r_addend;
210                                 bcopy(&w, (u_char *)data + (relbase +
211                                       rela->r_offset - dataaddr), sizeof(w));
212                                 break;
213                         default:
214                                 printf("\nunhandled relocation type %u\n",
215                                        (u_int)ELF_R_TYPE(rela->r_info));
216                                 return (EFTYPE);
217                         }
218                 }
219                 break;
220         }
221
222         return (0);
223 #else
224         return (EOPNOTSUPP);
225 #endif
226 }