1 /* $NetBSD: md.c,v 1.20 2005/12/24 20:59:30 perry Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
44 /* Pull in the ld(1) bits as well */
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
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 */
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
*));
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.
110 md_get_addend(r
, addr
)
111 struct relocation_info
*r
;
118 md_relocate(r
, relocation
, addr
, relocatable_output
)
119 struct relocation_info
*r
;
122 int relocatable_output
;
124 register unsigned long mask
;
126 if (relocatable_output
) {
128 * Store relocation where the next link-edit run
131 r
->r_addend
= relocation
;
135 relocation
>>= RELOC_VALUE_RIGHTSHIFT(r
);
137 /* Unshifted mask for relocation */
138 mask
= 1 << (RELOC_TARGET_BITSIZE(r
) - 1);
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) \
149 (void)memcpy(&ad, addr, sizeof(ad)); \
150 if (RELOC_MEMORY_ADD_P(r)) \
151 relocation += (mask & ad); \
154 (void)memcpy(addr, &ad, sizeof(ad)); \
157 switch (RELOC_TARGET_SIZE(r
)) {
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.
185 md_fix_jmpslot(sp
, offset
, addr
, first
)
192 * Here comes a RELOC_{LO10,HI22} relocation pair
193 * The resulting code is:
194 * sethi %hi(addr), %g1
198 sp
->opcode1
= SETHI
| ((addr
>> 10) & 0x003fffff);
199 sp
->opcode2
= JMP
| (addr
& 0x000003ff);
200 sp
->reloc_index
= NOP
;
205 md_set_breakpoint(where
, savep
)
209 *savep
= *(long *)where
;
210 *(long *)where
= TRAP
;
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
;
224 r
->r_type
= rp
->r_type
;
225 r
->r_addend
= rp
->r_addend
;
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
;
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).
251 md_make_jmpslot(sp
, offset
, index
)
256 u_long fudge
= (u_long
) -(sizeof(sp
->opcode1
) + offset
);
258 /* The following is a RELOC_WDISP30 relocation */
259 sp
->opcode2
= CALL
| ((fudge
>> 2) & 0x3fffffff);
260 sp
->reloc_index
= NOP
| index
;
265 * Update the relocation record for a jmpslot.
268 md_make_jmpreloc(rp
, r
, type
)
269 struct relocation_info
*rp
, *r
;
272 if (type
& RELTYPE_RELATIVE
)
273 r
->r_type
= RELOC_RELATIVE
;
275 r
->r_type
= RELOC_JMP_SLOT
;
277 r
->r_addend
= rp
->r_addend
;
281 * Set relocation type for a GOT RRS relocation.
284 md_make_gotreloc(rp
, r
, type
)
285 struct relocation_info
*rp
, *r
;
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
;
297 r
->r_type
= RELOC_GLOB_DAT
;
303 * Set relocation type for a RRS copy operation.
306 md_make_cpyreloc(rp
, r
)
307 struct relocation_info
*rp
, *r
;
309 r
->r_type
= RELOC_COPY_DAT
;
314 * Initialize (output) exec header such that useful values are
315 * obtained from subsequent N_*() macro evaluations.
318 md_init_header(hp
, magic
, flags
)
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
;
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
);
341 * Check for acceptable foreign machine Ids
348 #define SUN_M_SPARC 3
349 return (((md_swap_long(hp
->a_midmag
)&0x00ff0000) >> 16) == SUN_M_SPARC
);
351 return hp
->a_machtype
== M_SPARC
;