Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.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  * $DragonFly: src/gnu/usr.bin/ld/sparc/Attic/md.c,v 1.2 2003/06/17 04:25:46 dillon Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <a.out.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <stab.h>
42 #include <string.h>
43
44 #include "dynamic.h"
45
46 /*
47  * Relocation masks and sizes for the Sparc architecture.
48  *
49  * Note that these are very dependent on the order of the enums in
50  * enum reloc_type (in a.out.h); if they change the following must be
51  * changed.
52  * Also, note that RELOC_RELATIVE is handled as if it were a RELOC_HI22.
53  * This should work provided that relocations values have zeroes in their
54  * least significant 10 bits. As RELOC_RELATIVE is used only to relocate
55  * with load address values - which are page aligned - this condition is
56  * fulfilled as long as the system's page size is > 1024 (and a power of 2).
57  */
58 static int reloc_target_rightshift[] = {
59         0, 0, 0,        /* RELOC_8, _16, _32 */
60         0, 0, 0, 2, 2,  /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
61         10, 0,          /* HI22, _22 */
62         0, 0,           /* RELOC_13, _LO10 */
63         0, 0,           /* _SFA_BASE, _SFA_OFF13 */
64         0, 0, 10,       /* _BASE10, _BASE13, _BASE22 */
65         0, 10,          /* _PC10, _PC22 */
66         2, 0,           /* _JMP_TBL, _SEGOFF16 */
67         0, 0, 0         /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
68 };
69 static int reloc_target_size[] = {
70         0, 1, 2,        /* RELOC_8, _16, _32 */
71         0, 1, 2, 2, 2,  /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
72         2, 2,           /* HI22, _22 */
73         2, 2,           /* RELOC_13, _LO10 */
74         2, 2,           /* _SFA_BASE, _SFA_OFF13 */
75         2, 2, 2,        /* _BASE10, _BASE13, _BASE22 */
76         2, 2,           /* _PC10, _PC22 */
77         2, 0,           /* _JMP_TBL, _SEGOFF16 */
78         2, 0, 2         /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
79 };
80 static int reloc_target_bitsize[] = {
81         8, 16, 32,      /* RELOC_8, _16, _32 */
82         8, 16, 32, 30, 22,      /* DISP8, DISP16, DISP32, WDISP30, WDISP22 */
83         22, 22,         /* HI22, _22 */
84         13, 10,         /* RELOC_13, _LO10 */
85         32, 32,         /* _SFA_BASE, _SFA_OFF13 */
86         10, 13, 22,     /* _BASE10, _BASE13, _BASE22 */
87         10, 22,         /* _PC10, _PC22 */
88         30, 0,          /* _JMP_TBL, _SEGOFF16 */
89         32, 0, 22       /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
90 };
91
92
93 /*
94  * Get relocation addend corresponding to relocation record RP
95  * ADDR unused by SPARC impl.
96  */
97 long
98 md_get_addend(r, addr)
99 struct relocation_info  *r;
100 unsigned char           *addr;
101 {
102         return r->r_addend;
103 }
104
105 void
106 md_relocate(r, relocation, addr, relocatable_output)
107 struct relocation_info  *r;
108 long                    relocation;
109 unsigned char           *addr;
110 int                     relocatable_output;
111 {
112         register unsigned long  mask;
113
114 #ifndef RTLD
115         if (relocatable_output) {
116                 /*
117                  * Non-PC relative relocations which are absolute or
118                  * which have become non-external now have fixed
119                  * relocations.  Set the ADD_EXTRA of this relocation
120                  * to be the relocation we have now determined.
121                  */
122                 if (!RELOC_PCREL_P(r)) {
123                         if ((int) r->r_type <= RELOC_32
124                                             || RELOC_EXTERN_P(r) == 0)
125                                 RELOC_ADD_EXTRA(r) = relocation;
126                 } else if (RELOC_EXTERN_P(r))
127                         /*
128                          * External PC-relative relocations continue
129                          * to move around; update their relocations
130                          * by the amount they have moved so far.
131                          */
132                         RELOC_ADD_EXTRA(r) -= pc_relocation;
133                 return;
134         }
135 #endif
136
137         relocation >>= RELOC_VALUE_RIGHTSHIFT(r);
138
139         /* Unshifted mask for relocation */
140         mask = 1 << RELOC_TARGET_BITSIZE(r) - 1;
141         mask |= mask - 1;
142         relocation &= mask;
143
144         /* Shift everything up to where it's going to be used */
145         relocation <<= RELOC_TARGET_BITPOS(r);
146         mask <<= RELOC_TARGET_BITPOS(r);
147
148         switch (RELOC_TARGET_SIZE(r)) {
149         case 0:
150                 if (RELOC_MEMORY_ADD_P(r))
151                         relocation += (mask & *(u_char *) (addr));
152                 *(u_char *) (addr) &= ~mask;
153                 *(u_char *) (addr) |= relocation;
154                 break;
155
156         case 1:
157                 if (RELOC_MEMORY_ADD_P(r))
158                         relocation += (mask & *(u_short *) (addr));
159                 *(u_short *) (addr) &= ~mask;
160                 *(u_short *) (addr) |= relocation;
161                 break;
162
163         case 2:
164                 if (RELOC_MEMORY_ADD_P(r))
165                         relocation += (mask & *(u_long *) (addr));
166                 *(u_long *) (addr) &= ~mask;
167                 *(u_long *) (addr) |= relocation;
168                 break;
169         default:
170                 errx(1, "Unimplemented relocation field length: %d",
171                         RELOC_TARGET_SIZE(r));
172         }
173 }
174
175 #ifndef RTLD
176 /*
177  * Machine dependent part of claim_rrs_reloc().
178  * On the Sparc the relocation offsets are stored in the r_addend member.
179  */
180 int
181 md_make_reloc(rp, r, type)
182 struct relocation_info  *rp, *r;
183 int                     type;
184 {
185         r->r_type = rp->r_type;
186         r->r_addend = rp->r_addend;
187
188 #if 1
189         /*
190          * This wouldn't be strictly necessary - we could record the
191          * relocation value "in situ" in stead of in the r_addend field -
192          * but we are being Sun compatible here. Besides, Sun's ld.so
193          * has a bug that prevents it from handling this alternate method.
194          *
195          * IT WOULD BE REALLY NICE TO HAVE CONSISTENCY THROUGHOUT THE ENTIRE
196          * RELOCATION PROCESS, ie. using `r_addend' for storing all partially
197          * completed relocations, in stead of mixing them in both relocation
198          * records and in the segment data.
199          */
200         if (RELOC_PCREL_P(rp))
201                 r->r_addend -= pc_relocation;
202 #endif
203
204         return 1;
205 }
206 #endif
207
208 /*
209  * Set up a transfer from jmpslot at OFFSET (relative to the PLT table)
210  * to the binder slot (which is at offset 0 of the PLT).
211  */
212 void
213 md_make_jmpslot(sp, offset, index)
214 jmpslot_t               *sp;
215 long                    offset;
216 long                    index;
217 {
218         u_long  fudge = (u_long) -(sizeof(sp->opcode1) + offset);
219         sp->opcode1 = SAVE;
220         /* The following is a RELOC_WDISP30 relocation */
221         sp->opcode2 = CALL | ((fudge >> 2) & 0x3fffffff);
222         sp->reloc_index = NOP | index;
223 }
224
225 /*
226  * Set up a "direct" transfer (ie. not through the run-time binder) from
227  * jmpslot at OFFSET to ADDR. Used by `ld' when the SYMBOLIC flag is on,
228  * and by `ld.so' after resolving the symbol.
229  * On the i386, we use the JMP instruction which is PC relative, so no
230  * further RRS relocations will be necessary for such a jmpslot.
231  *
232  * OFFSET unused on Sparc.
233  */
234 void
235 md_fix_jmpslot(sp, offset, addr)
236 jmpslot_t       *sp;
237 long            offset;
238 u_long          addr;
239 {
240         /*
241          * Here comes a RELOC_{LO10,HI22} relocation pair
242          * The resulting code is:
243          *      sethi   %hi(addr), %g1
244          *      jmp     %g1+%lo(addr)
245          *      nop     ! delay slot
246          */
247         sp->opcode1 = SETHI | ((addr >> 10) & 0x003fffff);
248         sp->opcode2 = JMP | (addr & 0x000003ff);
249         sp->reloc_index = NOP;
250 }
251
252 /*
253  * Update the relocation record for a jmpslot.
254  */
255 void
256 md_make_jmpreloc(rp, r, type)
257 struct relocation_info  *rp, *r;
258 int                     type;
259 {
260         if (type & RELTYPE_RELATIVE)
261                 r->r_type = RELOC_RELATIVE;
262         else
263                 r->r_type = RELOC_JMP_SLOT;
264
265         r->r_addend = rp->r_addend;
266 }
267
268 /*
269  * Set relocation type for a GOT RRS relocation.
270  */
271 void
272 md_make_gotreloc(rp, r, type)
273 struct relocation_info  *rp, *r;
274 int                     type;
275 {
276         /*
277          * GOT value resolved (symbolic or entry point): R_32
278          * GOT not resolved: GLOB_DAT
279          *
280          * NOTE: I don't think it makes a difference.
281          */
282         if (type & RELTYPE_RELATIVE)
283                 r->r_type = RELOC_32;
284         else
285                 r->r_type = RELOC_GLOB_DAT;
286
287         r->r_addend = 0;
288 }
289
290 /*
291  * Set relocation type for a RRS copy operation.
292  */
293 void
294 md_make_cpyreloc(rp, r)
295 struct relocation_info  *rp, *r;
296 {
297         r->r_type = RELOC_COPY_DAT;
298         r->r_addend = 0;
299 }
300
301 void
302 md_set_breakpoint(where, savep)
303 long    where;
304 long    *savep;
305 {
306         *savep = *(long *)where;
307         *(long *)where = TRAP;
308 }
309
310 #ifndef RTLD
311 /*
312  * Initialize (output) exec header such that useful values are
313  * obtained from subsequent N_*() macro evaluations.
314  */
315 void
316 md_init_header(hp, magic, flags)
317 struct exec     *hp;
318 int             magic, flags;
319 {
320 #ifdef NetBSD
321         N_SETMAGIC((*hp), magic, MID_MACHINE, flags);
322
323         /* TEXT_START depends on the value of outheader.a_entry.  */
324         if (!(link_mode & SHAREABLE)) /*WAS: if (entry_symbol) */
325                 hp->a_entry = PAGSIZ;
326 #else
327         hp->a_magic = magic;
328         hp->a_machtype = M_SPARC;
329         hp->a_toolversion = 1;
330         hp->a_dynamic = ((flags) & EX_DYNAMIC);
331
332         /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry.  */
333         if (!(link_mode & SHAREABLE)) /*WAS: if (entry_symbol) */
334                 hp->a_entry = N_PAGSIZ(*hp);
335 #endif
336 }
337
338 /*
339  * Check for acceptable foreign machine Ids
340  */
341 int
342 md_midcompat(hp)
343 struct exec *hp;
344 {
345 #ifdef NetBSD
346 #define SUN_M_SPARC     3
347         return (((md_swap_long(hp->a_midmag)&0x00ff0000) >> 16) == SUN_M_SPARC);
348 #else
349         return hp->a_machtype == M_SPARC;
350 #endif
351 }
352 #endif /* RTLD */