1 /* $NetBSD: md.c,v 1.5 1998/12/17 20:14:44 pk 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>
35 #include <sys/types.h>
44 /* Pull in the ld(1) bits as well */
48 static int reloc_target_rightshift
[] = {
49 -1, 0, 2, 0, 0, 16, 16, 2,
50 -1, -1, 2, 2, -1, -1, 0, 0,
51 16, 16, 2, -1, -1, -1, -1, -1,
52 -1, -1, 0, 0, 0, 0, 16, 16
55 static int reloc_target_bitpos
[] = {
56 -1, 0, 2, 0, 0, 0, 0, 2,
57 -1, -1, 2, 2, -1, -1, 0, 0,
58 0, 0, 2, -1, -1, -1, -1, -1,
59 -1, -1, 0, 0, 0, 0, 0, 0
62 static int reloc_target_size
[] = {
63 -1, 4, 4, 2, 2, 2, 2, 4,
64 -1, -1, 4, 4, -1, -1, 2, 2,
65 2, 2, 4, -1, -1, -1, -1, -1,
66 -1, -1, 4, 4, 4, 2, 2, 2
69 static int reloc_target_bitsize
[] = {
70 -1, 32, 24, 16, 16, 16, 16, 14,
71 -1, -1, 24, 14, -1, -1, 16, 16,
72 16, 16, 24, -1, -1, -1, -1, -1,
73 -1, -1, 32, 32, 32, 16, 16, 16
77 * Get relocation addend corresponding to relocation record RP
81 md_get_addend(rp
, addr
)
82 struct relocation_info
*rp
;
89 * Put RELOCATION at ADDR according to relocation record RP.
92 md_relocate(rp
, relocation
, addr
, relocatable_output
)
93 struct relocation_info
*rp
;
96 int relocatable_output
;
98 register unsigned long mask
;
101 if (relocatable_output
) {
103 * Store relocation where the next link-edit run
106 rp
->r_addend
= relocation
;
109 if (rp
->r_type
== RELOC_16_HA
110 || rp
->r_type
== RELOC_GOT16_HA
111 || rp
->r_type
== RELOC_PLT16_HA
)
112 relocation
+= (relocation
& 0x8000) << 1;
114 relocation
>>= RELOC_VALUE_RIGHTSHIFT(rp
);
116 /* Unshifted mask for relocation */
117 mask
= 1 << RELOC_TARGET_BITSIZE(rp
) - 1;
121 /* Shift everything up to where it's going to be used */
122 relocation
<<= RELOC_TARGET_BITPOS(rp
);
123 mask
<<= RELOC_TARGET_BITPOS(rp
);
125 switch (RELOC_TARGET_SIZE(rp
)) {
127 *(u_long
*)addr
&= ~mask
;
128 *(u_long
*)addr
|= relocation
;
131 *(u_short
*)addr
&= ~mask
;
132 *(u_short
*)addr
|= relocation
;
135 errx(1, "Unknown relocation type %d", rp
->r_type
);
140 * Set up a "direct" transfer (ie. not through the run-time binder) from
141 * jmpslot at OFFSET to ADDR. Used by `ld' when the SYMBOLIC flag is on,
142 * and by `ld.so' after resolving the symbol.
145 md_fix_jmpslot(sp
, offset
, addr
, first
)
151 errx(1, "md_fix_jmpslot unimplemented");
155 md_set_breakpoint(where
, savep
)
159 *savep
= *(long *)where
;
160 *(char *)where
= TRAP
;
167 * Machine dependent part of claim_rrs_reloc().
168 * Set RRS relocation type.
171 md_make_reloc(rp
, r
, type
)
172 struct relocation_info
*rp
, *r
;
175 r
->r_type
= rp
->r_type
;
176 r
->r_addend
= rp
->r_addend
;
178 if (RELOC_PCREL_P(rp
))
179 r
->r_addend
-= pc_relocation
;
185 * Set up a transfer from jmpslot at OFFSET (relative to the PLT table)
186 * to the binder slot (which is at offset 0 of the PLT).
189 md_make_jmpslot(sp
, offset
, index
)
194 errx(1, "md_make_jmpslot unimplemented");
198 * Update the relocation record for a RRS jmpslot.
201 md_make_jmpreloc(rp
, r
, type
)
202 struct relocation_info
*rp
, *r
;
205 errx(1, "md_make_jmpreloc unimplemented");
209 * Set relocation type for a RRS GOT relocation.
212 md_make_gotreloc(rp
, r
, type
)
213 struct relocation_info
*rp
, *r
;
216 errx(1, "md_make_gotreloc unimplemented");
220 * Set relocation type for a RRS copy operation.
223 md_make_cpyreloc(rp
, r
)
224 struct relocation_info
*rp
, *r
;
226 errx(1, "md_make_cpyreloc unimplemented");
230 * Initialize (output) exec header such that useful values are
231 * obtained from subsequent N_*() macro evaluations.
234 md_init_header(hp
, magic
, flags
)
238 N_SETMAGIC((*hp
), magic
, MID_MACHINE
, flags
);
240 /* TEXT_START depends on the value of outheader.a_entry. */
241 if (!(link_mode
& SHAREABLE
))
242 hp
->a_entry
= PAGSIZ
;
249 * Byte swap routines for cross-linking.
253 md_swapin_exec_hdr(h
)
256 swap_longs((long *)h
+ 1, sizeof(*h
)/sizeof(long) - skip
);
260 md_swapout_exec_hdr(h
)
263 swap_longs((long *)h
+ 1, sizeof(*h
)/sizeof(long) - skip
);
268 md_swapin_reloc(r
, n
)
269 struct relocation_info
*r
;
272 errx(1, "md_swapin_reloc unimplemented");
276 md_swapout_reloc(r
, n
)
277 struct relocation_info
*r
;
280 errx(1, "md_swapout_reloc unimplemented");
284 md_swapout_jmpslot(j
, n
)
288 errx(1, "md_swapout_jmpslot unimplemented");
290 #endif /* NEED_SWAP */