Initial import from FreeBSD RELENG_4:
[games.git] / gnu / usr.bin / ld / sparc / md.c
1 /*
2  * Copyright (c) 1993 Paul Kranenburg
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
18  *
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.
29  *
30  * $FreeBSD: src/gnu/usr.bin/ld/sparc/md.c,v 1.12 1999/08/27 23:36:04 peter Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <a.out.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <err.h>
39 #include <fcntl.h>
40 #include <stab.h>
41 #include <string.h>
42
43 #include "dynamic.h"
44
45 /*
46  * Relocation masks and sizes for the Sparc architecture.
47  *
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
50  * changed.
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).
56  */
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 */
67 };
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 */
71         2, 2,           /* HI22, _22 */
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 */
78 };
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 */
89 };
90
91
92 /*
93  * Get relocation addend corresponding to relocation record RP
94  * ADDR unused by SPARC impl.
95  */
96 long
97 md_get_addend(r, addr)
98 struct relocation_info  *r;
99 unsigned char           *addr;
100 {
101         return r->r_addend;
102 }
103
104 void
105 md_relocate(r, relocation, addr, relocatable_output)
106 struct relocation_info  *r;
107 long                    relocation;
108 unsigned char           *addr;
109 int                     relocatable_output;
110 {
111         register unsigned long  mask;
112
113 #ifndef RTLD
114         if (relocatable_output) {
115                 /*
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.
120                  */
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))
126                         /*
127                          * External PC-relative relocations continue
128                          * to move around; update their relocations
129                          * by the amount they have moved so far.
130                          */
131                         RELOC_ADD_EXTRA(r) -= pc_relocation;
132                 return;
133         }
134 #endif
135
136         relocation >>= RELOC_VALUE_RIGHTSHIFT(r);
137
138         /* Unshifted mask for relocation */
139         mask = 1 << RELOC_TARGET_BITSIZE(r) - 1;
140         mask |= mask - 1;
141         relocation &= mask;
142
143         /* Shift everything up to where it's going to be used */
144         relocation <<= RELOC_TARGET_BITPOS(r);
145         mask <<= RELOC_TARGET_BITPOS(r);
146
147         switch (RELOC_TARGET_SIZE(r)) {
148         case 0:
149                 if (RELOC_MEMORY_ADD_P(r))
150                         relocation += (mask & *(u_char *) (addr));
151                 *(u_char *) (addr) &= ~mask;
152                 *(u_char *) (addr) |= relocation;
153                 break;
154
155         case 1:
156                 if (RELOC_MEMORY_ADD_P(r))
157                         relocation += (mask & *(u_short *) (addr));
158                 *(u_short *) (addr) &= ~mask;
159                 *(u_short *) (addr) |= relocation;
160                 break;
161
162         case 2:
163                 if (RELOC_MEMORY_ADD_P(r))
164                         relocation += (mask & *(u_long *) (addr));
165                 *(u_long *) (addr) &= ~mask;
166                 *(u_long *) (addr) |= relocation;
167                 break;
168         default:
169                 errx(1, "Unimplemented relocation field length: %d",
170                         RELOC_TARGET_SIZE(r));
171         }
172 }
173
174 #ifndef RTLD
175 /*
176  * Machine dependent part of claim_rrs_reloc().
177  * On the Sparc the relocation offsets are stored in the r_addend member.
178  */
179 int
180 md_make_reloc(rp, r, type)
181 struct relocation_info  *rp, *r;
182 int                     type;
183 {
184         r->r_type = rp->r_type;
185         r->r_addend = rp->r_addend;
186
187 #if 1
188         /*
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.
193          *
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.
198          */
199         if (RELOC_PCREL_P(rp))
200                 r->r_addend -= pc_relocation;
201 #endif
202
203         return 1;
204 }
205 #endif
206
207 /*
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).
210  */
211 void
212 md_make_jmpslot(sp, offset, index)
213 jmpslot_t               *sp;
214 long                    offset;
215 long                    index;
216 {
217         u_long  fudge = (u_long) -(sizeof(sp->opcode1) + offset);
218         sp->opcode1 = SAVE;
219         /* The following is a RELOC_WDISP30 relocation */
220         sp->opcode2 = CALL | ((fudge >> 2) & 0x3fffffff);
221         sp->reloc_index = NOP | index;
222 }
223
224 /*
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.
230  *
231  * OFFSET unused on Sparc.
232  */
233 void
234 md_fix_jmpslot(sp, offset, addr)
235 jmpslot_t       *sp;
236 long            offset;
237 u_long          addr;
238 {
239         /*
240          * Here comes a RELOC_{LO10,HI22} relocation pair
241          * The resulting code is:
242          *      sethi   %hi(addr), %g1
243          *      jmp     %g1+%lo(addr)
244          *      nop     ! delay slot
245          */
246         sp->opcode1 = SETHI | ((addr >> 10) & 0x003fffff);
247         sp->opcode2 = JMP | (addr & 0x000003ff);
248         sp->reloc_index = NOP;
249 }
250
251 /*
252  * Update the relocation record for a jmpslot.
253  */
254 void
255 md_make_jmpreloc(rp, r, type)
256 struct relocation_info  *rp, *r;
257 int                     type;
258 {
259         if (type & RELTYPE_RELATIVE)
260                 r->r_type = RELOC_RELATIVE;
261         else
262                 r->r_type = RELOC_JMP_SLOT;
263
264         r->r_addend = rp->r_addend;
265 }
266
267 /*
268  * Set relocation type for a GOT RRS relocation.
269  */
270 void
271 md_make_gotreloc(rp, r, type)
272 struct relocation_info  *rp, *r;
273 int                     type;
274 {
275         /*
276          * GOT value resolved (symbolic or entry point): R_32
277          * GOT not resolved: GLOB_DAT
278          *
279          * NOTE: I don't think it makes a difference.
280          */
281         if (type & RELTYPE_RELATIVE)
282                 r->r_type = RELOC_32;
283         else
284                 r->r_type = RELOC_GLOB_DAT;
285
286         r->r_addend = 0;
287 }
288
289 /*
290  * Set relocation type for a RRS copy operation.
291  */
292 void
293 md_make_cpyreloc(rp, r)
294 struct relocation_info  *rp, *r;
295 {
296         r->r_type = RELOC_COPY_DAT;
297         r->r_addend = 0;
298 }
299
300 void
301 md_set_breakpoint(where, savep)
302 long    where;
303 long    *savep;
304 {
305         *savep = *(long *)where;
306         *(long *)where = TRAP;
307 }
308
309 #ifndef RTLD
310 /*
311  * Initialize (output) exec header such that useful values are
312  * obtained from subsequent N_*() macro evaluations.
313  */
314 void
315 md_init_header(hp, magic, flags)
316 struct exec     *hp;
317 int             magic, flags;
318 {
319 #ifdef NetBSD
320         N_SETMAGIC((*hp), magic, MID_MACHINE, flags);
321
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;
325 #else
326         hp->a_magic = magic;
327         hp->a_machtype = M_SPARC;
328         hp->a_toolversion = 1;
329         hp->a_dynamic = ((flags) & EX_DYNAMIC);
330
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);
334 #endif
335 }
336
337 /*
338  * Check for acceptable foreign machine Ids
339  */
340 int
341 md_midcompat(hp)
342 struct exec *hp;
343 {
344 #ifdef NetBSD
345 #define SUN_M_SPARC     3
346         return (((md_swap_long(hp->a_midmag)&0x00ff0000) >> 16) == SUN_M_SPARC);
347 #else
348         return hp->a_machtype == M_SPARC;
349 #endif
350 }
351 #endif /* RTLD */