Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / libexec / ld.aout_so / arch / sparc / md.c
blob2248f4ee014d4ed8b9ab2e13dc92c604906aba9e
1 /* $NetBSD: md.c,v 1.20 2005/12/24 20:59:30 perry Exp $ */
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <a.out.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <err.h>
38 #include <fcntl.h>
39 #include <stab.h>
40 #include <string.h>
42 #include "ld.h"
43 #ifndef RTLD
44 /* Pull in the ld(1) bits as well */
45 #include "ld_i.h"
46 #endif
49 * Relocation masks and sizes for the Sparc architecture.
51 * Note that these are very dependent on the order of the enums in
52 * enum reloc_type (in a.out.h); if they change the following must be
53 * changed.
54 * Also, note that RELOC_RELATIVE is handled as if it were a RELOC_HI22.
55 * This should work provided that relocations values have zeroes in their
56 * least significant 10 bits. As RELOC_RELATIVE is used only to relocate
57 * with load address values - which are page aligned - this condition is
58 * fulfilled as long as the system's page size is > 1024 (and a power of 2).
60 static int reloc_target_rightshift[] = {
61 0, 0, 0, /* RELOC_8, _16, _32 */
62 0, 0, 0, 2, 2, /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
63 10, 0, /* HI22, _22 */
64 0, 0, /* RELOC_13, _LO10 */
65 0, 0, /* _SFA_BASE, _SFA_OFF13 */
66 0, 0, 10, /* _BASE10, _BASE13, _BASE22 */
67 0, 10, /* _PC10, _PC22 */
68 2, 0, /* _JMP_TBL, _SEGOFF16 */
69 0, 0, 0 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
71 static int reloc_target_size[] = {
72 0, 1, 2, /* RELOC_8, _16, _32 */
73 0, 1, 2, 2, 2, /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
74 2, 2, /* HI22, _22 */
75 2, 2, /* RELOC_13, _LO10 */
76 2, 2, /* _SFA_BASE, _SFA_OFF13 */
77 2, 2, 2, /* _BASE10, _BASE13, _BASE22 */
78 2, 2, /* _PC10, _PC22 */
79 2, 0, /* _JMP_TBL, _SEGOFF16 */
80 2, 0, 2 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
82 static int reloc_target_bitsize[] = {
83 8, 16, 32, /* RELOC_8, _16, _32 */
84 8, 16, 32, 30, 22, /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
85 22, 22, /* HI22, _22 */
86 13, 10, /* RELOC_13, _LO10 */
87 32, 32, /* _SFA_BASE, _SFA_OFF13 */
88 10, 13, 22, /* _BASE10, _BASE13, _BASE22 */
89 10, 22, /* _PC10, _PC22 */
90 30, 0, /* _JMP_TBL, _SEGOFF16 */
91 32, 0, 22 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
94 static void iflush __P((jmpslot_t *));
96 static inline void
97 iflush(sp)
98 jmpslot_t *sp;
100 __asm volatile("iflush %0+0" : : "r" (sp));
101 __asm volatile("iflush %0+4" : : "r" (sp));
102 __asm volatile("iflush %0+8" : : "r" (sp));
106 * Get relocation addend corresponding to relocation record RP
107 * ADDR unused by SPARC impl.
109 long
110 md_get_addend(r, addr)
111 struct relocation_info *r;
112 unsigned char *addr;
114 return r->r_addend;
117 void
118 md_relocate(r, relocation, addr, relocatable_output)
119 struct relocation_info *r;
120 long relocation;
121 unsigned char *addr;
122 int relocatable_output;
124 register unsigned long mask;
126 if (relocatable_output) {
128 * Store relocation where the next link-edit run
129 * will look for it.
131 r->r_addend = relocation;
132 return;
135 relocation >>= RELOC_VALUE_RIGHTSHIFT(r);
137 /* Unshifted mask for relocation */
138 mask = 1 << (RELOC_TARGET_BITSIZE(r) - 1);
139 mask |= mask - 1;
140 relocation &= mask;
142 /* Shift everything up to where it's going to be used */
143 relocation <<= RELOC_TARGET_BITPOS(r);
144 mask <<= RELOC_TARGET_BITPOS(r);
146 #define RELOCATE(type) \
148 type ad; \
149 (void)memcpy(&ad, addr, sizeof(ad)); \
150 if (RELOC_MEMORY_ADD_P(r)) \
151 relocation += (mask & ad); \
152 ad &= ~mask; \
153 ad |= relocation; \
154 (void)memcpy(addr, &ad, sizeof(ad)); \
157 switch (RELOC_TARGET_SIZE(r)) {
158 case 0:
159 RELOCATE(u_char)
160 break;
162 case 1:
163 RELOCATE(u_short)
164 break;
166 case 2:
167 RELOCATE(u_long)
168 break;
169 default:
170 errx(1, "Unimplemented relocation field length: %d",
171 RELOC_TARGET_SIZE(r));
176 * Set up a "direct" transfer (ie. not through the run-time binder) from
177 * jmpslot at OFFSET to ADDR. Used by `ld' when the SYMBOLIC flag is on,
178 * and by `ld.so' after resolving the symbol.
179 * On the i386, we use the JMP instruction which is PC relative, so no
180 * further RRS relocations will be necessary for such a jmpslot.
182 * OFFSET unused on Sparc.
184 void
185 md_fix_jmpslot(sp, offset, addr, first)
186 jmpslot_t *sp;
187 long offset;
188 u_long addr;
189 int first;
192 * Here comes a RELOC_{LO10,HI22} relocation pair
193 * The resulting code is:
194 * sethi %hi(addr), %g1
195 * jmp %g1+%lo(addr)
196 * nop ! delay slot
198 sp->opcode1 = SETHI | ((addr >> 10) & 0x003fffff);
199 sp->opcode2 = JMP | (addr & 0x000003ff);
200 sp->reloc_index = NOP;
201 iflush(sp);
204 void
205 md_set_breakpoint(where, savep)
206 long where;
207 long *savep;
209 *savep = *(long *)where;
210 *(long *)where = TRAP;
214 #ifndef RTLD
216 * Machine dependent part of claim_rrs_reloc().
217 * On the Sparc the relocation offsets are stored in the r_addend member.
220 md_make_reloc(rp, r, type)
221 struct relocation_info *rp, *r;
222 int type;
224 r->r_type = rp->r_type;
225 r->r_addend = rp->r_addend;
227 #if 1
229 * This wouldn't be strictly necessary - we could record the
230 * relocation value "in situ" in stead of in the r_addend field -
231 * but we are being Sun compatible here. Besides, Sun's ld.so
232 * has a bug that prevents it from handling this alternate method.
234 * IT WOULD BE REALLY NICE TO HAVE CONSISTENCY THROUGHOUT THE ENTIRE
235 * RELOCATION PROCESS, ie. using `r_addend' for storing all partially
236 * completed relocations, in stead of mixing them in both relocation
237 * records and in the segment data.
239 if (RELOC_PCREL_P(rp))
240 r->r_addend -= pc_relocation;
241 #endif
243 return 1;
247 * Set up a transfer from jmpslot at OFFSET (relative to the PLT table)
248 * to the binder slot (which is at offset 0 of the PLT).
250 void
251 md_make_jmpslot(sp, offset, index)
252 jmpslot_t *sp;
253 long offset;
254 long index;
256 u_long fudge = (u_long) -(sizeof(sp->opcode1) + offset);
257 sp->opcode1 = SAVE;
258 /* The following is a RELOC_WDISP30 relocation */
259 sp->opcode2 = CALL | ((fudge >> 2) & 0x3fffffff);
260 sp->reloc_index = NOP | index;
261 iflush(sp);
265 * Update the relocation record for a jmpslot.
267 void
268 md_make_jmpreloc(rp, r, type)
269 struct relocation_info *rp, *r;
270 int type;
272 if (type & RELTYPE_RELATIVE)
273 r->r_type = RELOC_RELATIVE;
274 else
275 r->r_type = RELOC_JMP_SLOT;
277 r->r_addend = rp->r_addend;
281 * Set relocation type for a GOT RRS relocation.
283 void
284 md_make_gotreloc(rp, r, type)
285 struct relocation_info *rp, *r;
286 int type;
289 * GOT value resolved (symbolic or entry point): R_32
290 * GOT not resolved: GLOB_DAT
292 * NOTE: I don't think it makes a difference.
294 if (type & RELTYPE_RELATIVE)
295 r->r_type = RELOC_32;
296 else
297 r->r_type = RELOC_GLOB_DAT;
299 r->r_addend = 0;
303 * Set relocation type for a RRS copy operation.
305 void
306 md_make_cpyreloc(rp, r)
307 struct relocation_info *rp, *r;
309 r->r_type = RELOC_COPY_DAT;
310 r->r_addend = 0;
314 * Initialize (output) exec header such that useful values are
315 * obtained from subsequent N_*() macro evaluations.
317 void
318 md_init_header(hp, magic, flags)
319 struct exec *hp;
320 int magic, flags;
322 #ifdef __NetBSD__
323 N_SETMAGIC((*hp), magic, MID_MACHINE, flags);
325 /* TEXT_START depends on the value of outheader.a_entry. */
326 if (!(link_mode & SHAREABLE)) /*WAS: if (entry_symbol) */
327 hp->a_entry = PAGSIZ;
328 #else
329 hp->a_magic = magic;
330 hp->a_machtype = M_SPARC;
331 hp->a_toolversion = 1;
332 hp->a_dynamic = ((flags) & EX_DYNAMIC);
334 /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry. */
335 if (!(link_mode & SHAREABLE)) /*WAS: if (entry_symbol) */
336 hp->a_entry = N_PAGSIZ(*hp);
337 #endif
341 * Check for acceptable foreign machine Ids
344 md_midcompat(hp)
345 struct exec *hp;
347 #ifdef __NetBSD__
348 #define SUN_M_SPARC 3
349 return (((md_swap_long(hp->a_midmag)&0x00ff0000) >> 16) == SUN_M_SPARC);
350 #else
351 return hp->a_machtype == M_SPARC;
352 #endif
354 #endif /* RTLD */