2 * Copyright (c) 1993 Paul Kranenburg
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Paul Kranenburg.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/gnu/usr.bin/ld/sparc/md.c,v 1.12 1999/08/27 23:36:04 peter Exp $
33 #include <sys/param.h>
34 #include <sys/types.h>
46 * Relocation masks and sizes for the Sparc architecture.
48 * Note that these are very dependent on the order of the enums in
49 * enum reloc_type (in a.out.h); if they change the following must be
51 * Also, note that RELOC_RELATIVE is handled as if it were a RELOC_HI22.
52 * This should work provided that relocations values have zeroes in their
53 * least significant 10 bits. As RELOC_RELATIVE is used only to relocate
54 * with load address values - which are page aligned - this condition is
55 * fulfilled as long as the system's page size is > 1024 (and a power of 2).
57 static int reloc_target_rightshift[] = {
58 0, 0, 0, /* RELOC_8, _16, _32 */
59 0, 0, 0, 2, 2, /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
60 10, 0, /* HI22, _22 */
61 0, 0, /* RELOC_13, _LO10 */
62 0, 0, /* _SFA_BASE, _SFA_OFF13 */
63 0, 0, 10, /* _BASE10, _BASE13, _BASE22 */
64 0, 10, /* _PC10, _PC22 */
65 2, 0, /* _JMP_TBL, _SEGOFF16 */
66 0, 0, 0 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
68 static int reloc_target_size[] = {
69 0, 1, 2, /* RELOC_8, _16, _32 */
70 0, 1, 2, 2, 2, /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
72 2, 2, /* RELOC_13, _LO10 */
73 2, 2, /* _SFA_BASE, _SFA_OFF13 */
74 2, 2, 2, /* _BASE10, _BASE13, _BASE22 */
75 2, 2, /* _PC10, _PC22 */
76 2, 0, /* _JMP_TBL, _SEGOFF16 */
77 2, 0, 2 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
79 static int reloc_target_bitsize[] = {
80 8, 16, 32, /* RELOC_8, _16, _32 */
81 8, 16, 32, 30, 22, /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
82 22, 22, /* HI22, _22 */
83 13, 10, /* RELOC_13, _LO10 */
84 32, 32, /* _SFA_BASE, _SFA_OFF13 */
85 10, 13, 22, /* _BASE10, _BASE13, _BASE22 */
86 10, 22, /* _PC10, _PC22 */
87 30, 0, /* _JMP_TBL, _SEGOFF16 */
88 32, 0, 22 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
93 * Get relocation addend corresponding to relocation record RP
94 * ADDR unused by SPARC impl.
97 md_get_addend(r, addr)
98 struct relocation_info *r;
105 md_relocate(r, relocation, addr, relocatable_output)
106 struct relocation_info *r;
109 int relocatable_output;
111 register unsigned long mask;
114 if (relocatable_output) {
116 * Non-PC relative relocations which are absolute or
117 * which have become non-external now have fixed
118 * relocations. Set the ADD_EXTRA of this relocation
119 * to be the relocation we have now determined.
121 if (!RELOC_PCREL_P(r)) {
122 if ((int) r->r_type <= RELOC_32
123 || RELOC_EXTERN_P(r) == 0)
124 RELOC_ADD_EXTRA(r) = relocation;
125 } else if (RELOC_EXTERN_P(r))
127 * External PC-relative relocations continue
128 * to move around; update their relocations
129 * by the amount they have moved so far.
131 RELOC_ADD_EXTRA(r) -= pc_relocation;
136 relocation >>= RELOC_VALUE_RIGHTSHIFT(r);
138 /* Unshifted mask for relocation */
139 mask = 1 << RELOC_TARGET_BITSIZE(r) - 1;
143 /* Shift everything up to where it's going to be used */
144 relocation <<= RELOC_TARGET_BITPOS(r);
145 mask <<= RELOC_TARGET_BITPOS(r);
147 switch (RELOC_TARGET_SIZE(r)) {
149 if (RELOC_MEMORY_ADD_P(r))
150 relocation += (mask & *(u_char *) (addr));
151 *(u_char *) (addr) &= ~mask;
152 *(u_char *) (addr) |= relocation;
156 if (RELOC_MEMORY_ADD_P(r))
157 relocation += (mask & *(u_short *) (addr));
158 *(u_short *) (addr) &= ~mask;
159 *(u_short *) (addr) |= relocation;
163 if (RELOC_MEMORY_ADD_P(r))
164 relocation += (mask & *(u_long *) (addr));
165 *(u_long *) (addr) &= ~mask;
166 *(u_long *) (addr) |= relocation;
169 errx(1, "Unimplemented relocation field length: %d",
170 RELOC_TARGET_SIZE(r));
176 * Machine dependent part of claim_rrs_reloc().
177 * On the Sparc the relocation offsets are stored in the r_addend member.
180 md_make_reloc(rp, r, type)
181 struct relocation_info *rp, *r;
184 r->r_type = rp->r_type;
185 r->r_addend = rp->r_addend;
189 * This wouldn't be strictly necessary - we could record the
190 * relocation value "in situ" in stead of in the r_addend field -
191 * but we are being Sun compatible here. Besides, Sun's ld.so
192 * has a bug that prevents it from handling this alternate method.
194 * IT WOULD BE REALLY NICE TO HAVE CONSISTENCY THROUGHOUT THE ENTIRE
195 * RELOCATION PROCESS, ie. using `r_addend' for storing all partially
196 * completed relocations, in stead of mixing them in both relocation
197 * records and in the segment data.
199 if (RELOC_PCREL_P(rp))
200 r->r_addend -= pc_relocation;
208 * Set up a transfer from jmpslot at OFFSET (relative to the PLT table)
209 * to the binder slot (which is at offset 0 of the PLT).
212 md_make_jmpslot(sp, offset, index)
217 u_long fudge = (u_long) -(sizeof(sp->opcode1) + offset);
219 /* The following is a RELOC_WDISP30 relocation */
220 sp->opcode2 = CALL | ((fudge >> 2) & 0x3fffffff);
221 sp->reloc_index = NOP | index;
225 * Set up a "direct" transfer (ie. not through the run-time binder) from
226 * jmpslot at OFFSET to ADDR. Used by `ld' when the SYMBOLIC flag is on,
227 * and by `ld.so' after resolving the symbol.
228 * On the i386, we use the JMP instruction which is PC relative, so no
229 * further RRS relocations will be necessary for such a jmpslot.
231 * OFFSET unused on Sparc.
234 md_fix_jmpslot(sp, offset, addr)
240 * Here comes a RELOC_{LO10,HI22} relocation pair
241 * The resulting code is:
242 * sethi %hi(addr), %g1
246 sp->opcode1 = SETHI | ((addr >> 10) & 0x003fffff);
247 sp->opcode2 = JMP | (addr & 0x000003ff);
248 sp->reloc_index = NOP;
252 * Update the relocation record for a jmpslot.
255 md_make_jmpreloc(rp, r, type)
256 struct relocation_info *rp, *r;
259 if (type & RELTYPE_RELATIVE)
260 r->r_type = RELOC_RELATIVE;
262 r->r_type = RELOC_JMP_SLOT;
264 r->r_addend = rp->r_addend;
268 * Set relocation type for a GOT RRS relocation.
271 md_make_gotreloc(rp, r, type)
272 struct relocation_info *rp, *r;
276 * GOT value resolved (symbolic or entry point): R_32
277 * GOT not resolved: GLOB_DAT
279 * NOTE: I don't think it makes a difference.
281 if (type & RELTYPE_RELATIVE)
282 r->r_type = RELOC_32;
284 r->r_type = RELOC_GLOB_DAT;
290 * Set relocation type for a RRS copy operation.
293 md_make_cpyreloc(rp, r)
294 struct relocation_info *rp, *r;
296 r->r_type = RELOC_COPY_DAT;
301 md_set_breakpoint(where, savep)
305 *savep = *(long *)where;
306 *(long *)where = TRAP;
311 * Initialize (output) exec header such that useful values are
312 * obtained from subsequent N_*() macro evaluations.
315 md_init_header(hp, magic, flags)
320 N_SETMAGIC((*hp), magic, MID_MACHINE, flags);
322 /* TEXT_START depends on the value of outheader.a_entry. */
323 if (!(link_mode & SHAREABLE)) /*WAS: if (entry_symbol) */
324 hp->a_entry = PAGSIZ;
327 hp->a_machtype = M_SPARC;
328 hp->a_toolversion = 1;
329 hp->a_dynamic = ((flags) & EX_DYNAMIC);
331 /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry. */
332 if (!(link_mode & SHAREABLE)) /*WAS: if (entry_symbol) */
333 hp->a_entry = N_PAGSIZ(*hp);
338 * Check for acceptable foreign machine Ids
345 #define SUN_M_SPARC 3
346 return (((md_swap_long(hp->a_midmag)&0x00ff0000) >> 16) == SUN_M_SPARC);
348 return hp->a_machtype == M_SPARC;