mdreloc.c revision 1.1 1 1.1 christos /* $NetBSD: mdreloc.c,v 1.1 1999/02/24 18:25:41 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Paul Kranenburg.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos #include <errno.h>
40 1.1 christos #include <stdio.h>
41 1.1 christos #include <stdlib.h>
42 1.1 christos #include <string.h>
43 1.1 christos #include <unistd.h>
44 1.1 christos
45 1.1 christos #include "rtldenv.h"
46 1.1 christos #include "debug.h"
47 1.1 christos #include "rtld.h"
48 1.1 christos
49 1.1 christos /*
50 1.1 christos * The following table holds for each relocation type:
51 1.1 christos * - the width in bits of the memory location the relocation
52 1.1 christos * applies to (not currently used)
53 1.1 christos * - the number of bits the relocation value must be shifted to the
54 1.1 christos * right (i.e. discard least significant bits) to fit into
55 1.1 christos * the appropriate field in the instruction word.
56 1.1 christos * - flags indicating whether
57 1.1 christos * * the relocation involves a symbol
58 1.1 christos * * the relocation is relative to the current position
59 1.1 christos * * the relocation is for a GOT entry
60 1.1 christos * * the relocation is relative to the load address
61 1.1 christos *
62 1.1 christos */
63 1.1 christos #define _RF_S 0x80000000 /* Resolve symbol */
64 1.1 christos #define _RF_A 0x40000000 /* Use addend */
65 1.1 christos #define _RF_P 0x20000000 /* Location relative */
66 1.1 christos #define _RF_G 0x10000000 /* GOT offset */
67 1.1 christos #define _RF_B 0x08000000 /* Load address relative */
68 1.1 christos #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */
69 1.1 christos #define _RF_RS(s) ( (s) & 0xff) /* right shift */
70 1.1 christos static int reloc_target_flags[] = {
71 1.1 christos 0, /* NONE */
72 1.1 christos _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */
73 1.1 christos _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */
74 1.1 christos _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */
75 1.1 christos _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */
76 1.1 christos _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */
77 1.1 christos _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */
78 1.1 christos _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */
79 1.1 christos _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */
80 1.1 christos _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */
81 1.1 christos _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */
82 1.1 christos _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */
83 1.1 christos _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */
84 1.1 christos _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */
85 1.1 christos _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */
86 1.1 christos _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */
87 1.1 christos _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */
88 1.1 christos _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */
89 1.1 christos _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */
90 1.1 christos _RF_SZ(32) | _RF_RS(0), /* COPY */
91 1.1 christos _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_DAT */
92 1.1 christos _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */
93 1.1 christos _RF_A| _RF_SZ(32) | _RF_RS(0), /* RELATIVE */
94 1.1 christos _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* UA_32 */
95 1.1 christos
96 1.1 christos /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* PLT32 */
97 1.1 christos /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* HIPLT22 */
98 1.1 christos /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */
99 1.1 christos /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */
100 1.1 christos /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* PCPLT22 */
101 1.1 christos /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */
102 1.1 christos _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 10 */
103 1.1 christos _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 11 */
104 1.1 christos _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 64 */
105 1.1 christos _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */
106 1.1 christos _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* HH22 */
107 1.1 christos _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* HM10 */
108 1.1 christos _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* LM22 */
109 1.1 christos _RF_S|_RF_A|_RF_P|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* WDISP16 */
110 1.1 christos _RF_S|_RF_A|_RF_P|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* WDISP19 */
111 1.1 christos /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */
112 1.1 christos /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 7 */
113 1.1 christos /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 5 */
114 1.1 christos /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 6 */
115 1.1 christos };
116 1.1 christos
117 1.1 christos #ifdef RTLD_DEBUG_RELOC
118 1.1 christos static const char *reloc_names[] = {
119 1.1 christos "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
120 1.1 christos "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
121 1.1 christos "22", "13", "LO10", "GOT10", "GOT13",
122 1.1 christos "GOT22", "PC10", "PC22", "WPLT30", "COPY",
123 1.1 christos "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
124 1.1 christos "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
125 1.1 christos "10", "11", "64", "OLO10", "HH22",
126 1.1 christos "HM10", "LM22", "WDISP16", "WDISP19", "GLOB_JMP",
127 1.1 christos "7", "5", "6"
128 1.1 christos };
129 1.1 christos #endif
130 1.1 christos
131 1.1 christos #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
132 1.1 christos #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
133 1.1 christos #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
134 1.1 christos #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
135 1.1 christos
136 1.1 christos static int reloc_target_bitmask[] = {
137 1.1 christos #define _BM(x) (~(-(1ULL << (x))))
138 1.1 christos 0, /* NONE */
139 1.1 christos _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */
140 1.1 christos _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */
141 1.1 christos _BM(30), _BM(22), /* WDISP30, WDISP22 */
142 1.1 christos _BM(22), _BM(22), /* HI22, _22 */
143 1.1 christos _BM(13), _BM(10), /* RELOC_13, _LO10 */
144 1.1 christos _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */
145 1.1 christos _BM(10), _BM(22), /* _PC10, _PC22 */
146 1.1 christos _BM(30), 0, /* _WPLT30, _COPY */
147 1.1 christos -1, -1, _BM(22), /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
148 1.1 christos _BM(32), _BM(32), /* _UA32, PLT32 */
149 1.1 christos _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */
150 1.1 christos _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */
151 1.1 christos _BM(10), _BM(11), -1, /* _10, _11, _64 */
152 1.1 christos _BM(10), _BM(22), /* _OLO10, _HH22 */
153 1.1 christos _BM(10), _BM(22), /* _HM10, _LM22 */
154 1.1 christos _BM(16), _BM(19), /* _WDISP16, _WDISP19 */
155 1.1 christos -1, /* GLOB_JMP */
156 1.1 christos _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */
157 1.1 christos #undef _BM
158 1.1 christos };
159 1.1 christos #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
160 1.1 christos
161 1.1 christos int
162 1.1 christos _rtld_relocate_nonplt_object(
163 1.1 christos const Obj_Entry *obj,
164 1.1 christos const Elf_RelA *rela,
165 1.1 christos bool dodebug)
166 1.1 christos {
167 1.1 christos Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
168 1.1 christos Elf_Word type, value, mask;
169 1.1 christos const Elf_Sym *def = NULL;
170 1.1 christos const Obj_Entry *defobj = NULL;
171 1.1 christos
172 1.1 christos type = ELF_R_TYPE(rela->r_info);
173 1.1 christos if (type == R_TYPE(NONE))
174 1.1 christos return (0);
175 1.1 christos
176 1.1 christos /*
177 1.1 christos * We use the fact that relocation types are an `enum'
178 1.1 christos * Note: R_SPARC_6 is currently numerically largest.
179 1.1 christos */
180 1.1 christos if (type > R_TYPE(6))
181 1.1 christos return (-1);
182 1.1 christos
183 1.1 christos /*
184 1.1 christos * Handle relative relocs here, because we might not
185 1.1 christos * be able to access globals yet
186 1.1 christos */
187 1.1 christos if (!dodebug && type == R_TYPE(RELATIVE)) {
188 1.1 christos *where += (Elf_Addr) obj->relocbase;
189 1.1 christos return 0;
190 1.1 christos }
191 1.1 christos
192 1.1 christos value = rela->r_addend;
193 1.1 christos if (RELOC_RESOLVE_SYMBOL(type)) {
194 1.1 christos
195 1.1 christos /* Find the symbol */
196 1.1 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
197 1.1 christos NULL, obj, &defobj, false);
198 1.1 christos if (def == NULL)
199 1.1 christos return (-1);
200 1.1 christos
201 1.1 christos /* Add in the symbol's absolute address */
202 1.1 christos value += (Elf_Word)(defobj->relocbase + def->st_value);
203 1.1 christos }
204 1.1 christos
205 1.1 christos if (RELOC_PC_RELATIVE(type)) {
206 1.1 christos value -= (Elf_Word)where;
207 1.1 christos }
208 1.1 christos
209 1.1 christos mask = RELOC_VALUE_BITMASK(type);
210 1.1 christos value >>= RELOC_VALUE_RIGHTSHIFT(type);
211 1.1 christos value &= mask;
212 1.1 christos
213 1.1 christos /* We ignore alignment restrictions here */
214 1.1 christos *where &= ~mask;
215 1.1 christos *where |= value;
216 1.1 christos #ifdef RTLD_DEBUG_RELOC
217 1.1 christos if (RELOC_RESOLVE_SYMBOL(type)) {
218 1.1 christos rdbg(dodebug, "%s %s in %s --> %p %s",
219 1.1 christos reloc_names[type],
220 1.1 christos defobj->strtab + def->st_name, obj->path,
221 1.1 christos (void *)*where, defobj->path);
222 1.1 christos }
223 1.1 christos else {
224 1.1 christos rdbg(dodebug, "%s --> %p", reloc_names[type],
225 1.1 christos (void *)*where);
226 1.1 christos }
227 1.1 christos #endif
228 1.1 christos return (0);
229 1.1 christos }
230 1.1 christos
231 1.1 christos int
232 1.1 christos _rtld_relocate_plt_object(
233 1.1 christos const Obj_Entry *obj,
234 1.1 christos const Elf_RelA *rela,
235 1.1 christos caddr_t *addrp,
236 1.1 christos bool bind_now,
237 1.1 christos bool dodebug)
238 1.1 christos {
239 1.1 christos const Elf_Sym *def;
240 1.1 christos const Obj_Entry *defobj;
241 1.1 christos Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
242 1.1 christos Elf_Addr value;
243 1.1 christos
244 1.1 christos if (bind_now == 0 && obj->pltgot != NULL)
245 1.1 christos return (0);
246 1.1 christos
247 1.1 christos /* Fully resolve procedure addresses now */
248 1.1 christos
249 1.1 christos assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
250 1.1 christos
251 1.1 christos def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
252 1.1 christos NULL, obj, &defobj, true);
253 1.1 christos if (def == NULL)
254 1.1 christos return (-1);
255 1.1 christos
256 1.1 christos value = (Elf_Addr) (defobj->relocbase + def->st_value);
257 1.1 christos
258 1.1 christos rdbg(dodebug, "bind now %d/fixup in %s --> old=%p new=%p",
259 1.1 christos (int)bind_now, defobj->strtab + def->st_name,
260 1.1 christos (void *)*where, (void *)value);
261 1.1 christos
262 1.1 christos /*
263 1.1 christos * At the PLT entry pointed at by `where', we now construct
264 1.1 christos * a direct transfer to the now fully resolved function
265 1.1 christos * address. The resulting code in the jump slot is:
266 1.1 christos *
267 1.1 christos * sethi %hi(addr), %g1
268 1.1 christos * jmp %g1+%lo(addr)
269 1.1 christos * nop ! delay slot
270 1.1 christos */
271 1.1 christos #define SETHI 0x03000000
272 1.1 christos #define JMP 0x81c06000
273 1.1 christos #define NOP 0x01000000
274 1.1 christos where[0] = SETHI | ((value >> 10) & 0x003fffff);
275 1.1 christos where[1] = JMP | (value & 0x000003ff);
276 1.1 christos where[2] = NOP;
277 1.1 christos
278 1.1 christos if (addrp != NULL)
279 1.1 christos *addrp = (caddr_t)value;
280 1.1 christos
281 1.1 christos return (0);
282 1.1 christos }
283