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