1 /* $NetBSD: kobj_machdep.c,v 1.8 2023/04/28 07:33:56 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*-
30 * Copyright 1996-1998 John D. Polstra.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
46 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
51 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: kobj_machdep.c,v 1.8 2023/04/28 07:33:56 skrll Exp $");
56
57 #define ELFSIZE ARCH_ELFSIZE
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kobj.h>
62 #include <sys/exec.h>
63 #include <sys/exec_elf.h>
64
65 int
kobj_reloc(kobj_t ko,uintptr_t relocbase,const void * data,bool isrela,bool local)66 kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data,
67 bool isrela, bool local)
68 {
69 #ifdef _LP64
70 Elf64_Half *wwhere;
71 #endif
72 Elf_Addr *where;
73 Elf32_Half *hwhere;
74 Elf_Addr addr;
75 Elf_Sword addend; /* needs to be signed */
76 u_int rtype, symidx;
77 const Elf_Rela *rela;
78 int error;
79
80 if (!isrela) {
81 panic("kobj_reloc: REL relocations not supported");
82 }
83
84 rela = (const Elf_Rela *)data;
85 where = (Elf_Addr *) (relocbase + rela->r_offset);
86 hwhere = (Elf32_Half *) (relocbase + rela->r_offset);
87 #ifdef _LP64
88 wwhere = (Elf64_Half *) (relocbase + rela->r_offset);
89 #endif
90 addend = rela->r_addend;
91 rtype = ELF_R_TYPE(rela->r_info);
92 symidx = ELF_R_SYM(rela->r_info);
93
94 const Elf_Sym *sym = kobj_symbol(ko, symidx);
95
96 if (!local && ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
97 return 0;
98 }
99
100 switch (rtype) {
101 case R_PPC_NONE:
102 break;
103
104 case R_PPC_RELATIVE: /* word32 B + A */
105 addend += relocbase; /* A += B */
106 break;
107
108 case R_PPC_REL32: /* word32 S + A - P */
109 case R_PPC_REL16: /* half16* (S + A - P) */
110 case R_PPC_REL16_LO: /* half16 #lo(S + A - P) */
111 case R_PPC_REL16_HI: /* half16 #hi(S + A - P) */
112 case R_PPC_REL16_HA: /* half16 #ha(S + A - P) */
113 addend -= relocbase + rela->r_offset; /* A -= P */
114 /* FALLTHROUGH */
115
116 #ifdef _LP64
117 case R_PPC_ADDR64: /* doubleword64 S + A */
118 #endif
119 case R_PPC_ADDR32: /* word32 S + A */
120 case R_PPC_ADDR16: /* half16* S + A */
121 case R_PPC_ADDR16_LO: /* half16 #lo(S + A) */
122 case R_PPC_ADDR16_HA: /* half16 #ha(S + A) */
123 case R_PPC_ADDR16_HI: /* half16 #hi(S + A) */
124 error = kobj_sym_lookup(ko, symidx, &addr);
125 if (error)
126 return -1;
127
128 #if 0
129 /*
130 * addend values are sometimes relative to sections
131 * (i.e. .rodata) in rela, where in reality they
132 * are relative to relocbase. Detect this condition.
133 */
134 if (addr > relocbase && addr <= (relocbase + addend))
135 addr = relocbase + addend;
136 else
137 #endif
138 addend += addr; /* A += S */
139 break;
140
141 default:
142 printf("kobj_reloc: unexpected relocation type %u\n", rtype);
143 return -1;
144 }
145
146
147 switch (rtype) {
148 case R_PPC_REL32: /* word32 S + A - P */
149 #ifdef _LP64
150 *wwhere = addend;
151 break;
152 #endif
153 case R_PPC_RELATIVE: /* doubleword64/word32 B + A */
154 case R_PPC_ADDR32: /* doubleword64/word32 S + A */
155 *where = addend;
156 break;
157
158 case R_PPC_REL16: /* half16* (S + A - P) */
159 case R_PPC_ADDR16: /* half16* S + A */
160 if ((int16_t) addend != addend)
161 return -1;
162 /* FALLTHROUGH */
163 case R_PPC_REL16_LO: /* half16 #lo(S + A - P) */
164 case R_PPC_ADDR16_LO: /* half16 #lo(S + A) */
165 *hwhere = addend & 0xffff;
166 break;
167
168 case R_PPC_REL16_HA: /* half16 #ha(S + A - P) */
169 case R_PPC_ADDR16_HA: /* half16 #ha(S + A) */
170 addend += 0x8000;
171 /* FALLTHROUGH */
172 case R_PPC_REL16_HI: /* half16 #hi(S + A - P) */
173 case R_PPC_ADDR16_HI: /* half16 #hi(S + A) */
174 *hwhere = (addend >> 16) & 0xffff;
175 break;
176 }
177
178 return 0;
179 }
180
181 int
kobj_machdep(kobj_t ko,void * base,size_t size,bool load)182 kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
183 {
184 if (load)
185 __syncicache(base, size);
186
187 return 0;
188 }
189