2 * Copyright (c) 1995 by Sun Microsystems, Inc.
5 * This source code is a product of Sun Microsystems, Inc. and is provided
6 * for unrestricted use provided that this legend is included on all tape
7 * media and as a part of the software program in whole or part. Users
8 * may copy or modify this source code without charge, but are not authorized
9 * to license or distribute it to anyone else except as part of a product or
10 * program developed by the user.
12 * THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC.
13 * SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY
14 * OF SUCH SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT
15 * EXPRESS OR IMPLIED WARRANTY OF ANY KIND. SUN MICROSYSTEMS, INC. DISCLAIMS
16 * ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN
18 * NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT,
19 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
20 * FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY.
22 * This source code is provided with no support and without any obligation on
23 * the part of Sun Microsystems, Inc. to assist in its use, correction,
24 * modification or enhancement.
26 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
27 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS
28 * SOURCE CODE OR ANY PART THEREOF.
30 * Sun Microsystems, Inc.
32 * Mountain View, California 94043
36 * Update the value of the `_edata' and `_end' symbols.
38 #pragma ident "@(#) $Id: _relocate.c,v 1.4 1995/06/26 20:13:26 georgn Exp $ - SMI"
43 #include "_dynodump.h"
46 #define GETbyte(P) ((long)((unsigned long)(P)[0]))
47 #define GEThalf(P) ((long)(((unsigned long)(P)[0] << 8) | \
48 ((unsigned long)(P)[1])))
49 #define GETword(P) ((long)(((unsigned long)(P)[0] << 24) | \
50 ((unsigned long)(P)[1] << 16) | \
51 ((unsigned long)(P)[2] << 8) | \
52 (unsigned long)(P)[3]))
53 #define GETdisp30(P) (GETword(P) & 0x3fffffff)
54 #define GETdisp22(P) (GETword(P) & 0x3fffff)
55 #define GETdisp16(P) (((GETword(P) & 0x300000) >> 6) | \
56 (GETword(P) & 0x3fff))
57 #define GETdisp19(P) (GETword(P) & 0x7ffff)
58 #define GETimm22(P) (GETword(P) & 0x3fffff)
59 #define GETimm5(P) (GEThalf((P)+2) & 0x1f)
60 #define GETimm6(P) (GEThalf((P)+2) & 0x2f)
61 #define GETimm7(P) (GEThalf((P)+2) & 0x3f)
62 #define GETsimm13(P) (GEThalf((P)+2) & 0x1fff)
63 #define GETsimm10(P) (GEThalf((P)+2) & 0x3ff)
64 #define GETsimm11(P) (GEThalf((P)+2) & 0x7ff)
65 #define GETplt22(P) (GETword((P)+8) & 0x3fffff)
67 #define PUTbyte(V, P) (P)[0] = (V)
68 #define PUThalf(V, P) (P)[0] = ((V) >> 8); \
70 #define PUTword(V, P) (P)[0] = (unsigned char)((V) >> 24); \
71 (P)[1] = (unsigned char)((V) >> 16); \
72 (P)[2] = (unsigned char)((V) >> 8); \
73 (P)[3] = (unsigned char)(V)
74 #define PUTdisp30(V, P) { \
75 unsigned long int temp; \
76 temp = GETword(P) & ~0x3fffffff; \
77 temp |= ((V) & 0x3fffffff); \
80 #define PUTdisp22(V, P) { \
81 unsigned long int temp; \
82 temp = GETword(P) & ~0x3fffff; \
83 temp |= ((V) & 0x3fffff); \
86 #define PUTimm22(V, P) { \
87 unsigned long int temp; \
88 temp = GETword(P) & ~0x3fffff; \
89 temp |= ((V) & 0x3fffff); \
92 #define PUTimm5(V, P) { \
93 unsigned long int temp; \
94 temp = GEThalf(P+2) & ~0x1f; \
95 temp |= ((V) & 0x1f); \
96 PUThalf(temp, (P+2)); \
98 #define PUTimm6(V, P) { \
99 unsigned long int temp; \
100 temp = GEThalf(P+2) & ~0x2f; \
101 temp |= ((V) & 0x2f); \
102 PUThalf(temp, (P+2)); \
104 #define PUTimm7(V, P) { \
105 unsigned long int temp; \
106 temp = GEThalf(P+2) & ~0x3f; \
107 temp |= ((V) & 0x3f); \
108 PUThalf(temp, (P+2)); \
110 #define PUTsimm13(V, P) { \
111 unsigned long int temp; \
112 temp = GEThalf(P+2) & ~0x1fff; \
113 temp |= ((V) & 0x1fff); \
114 PUThalf(temp, (P+2)); \
116 #define PUTplt22(V, P) { \
117 unsigned long int temp; \
118 temp = GETword((P)+8) & ~0x3fffff; \
119 temp |= ((V) & 0x3fffff); \
120 PUTword(temp, ((P)+8)); \
122 #define PUTsimm10(V, P) { \
123 unsigned long int temp; \
124 temp = GEThalf(P+2) & ~0x3ff; \
125 temp |= ((V) & 0x3ff); \
126 PUThalf(temp, (P+2)); \
128 #define PUTsimm11(V, P) { \
129 unsigned long int temp; \
130 temp = GEThalf(P+2) & ~0x7ff; \
131 temp |= ((V) & 0x7ff); \
132 PUThalf(temp, (P+2)); \
134 #define PUTdisp16(V, P) { \
135 unsigned long int temp; \
136 temp = GETword(P) & ~0x303fff; \
137 temp |= ((V) & 0xc000) << 6; \
138 temp |= ((V) & 0x3fff); \
141 #define PUTdisp19(V, P) { \
142 unsigned long int temp; \
143 temp = GETword(P) & ~0x7ffff; \
144 temp |= ((V) & 0x7ffff); \
149 move_reloc(unsigned char * iaddr, unsigned char * oaddr, unsigned char type)
154 PUTbyte(GETbyte(iaddr), oaddr);
159 PUThalf(GEThalf(iaddr), oaddr);
164 case R_SPARC_GLOB_DAT:
165 case R_SPARC_RELATIVE:
167 PUTword(GETword(iaddr), oaddr);
170 case R_SPARC_WDISP30:
172 PUTdisp30(GETdisp30(iaddr), oaddr);
175 case R_SPARC_WDISP22:
177 PUTdisp22(GETdisp22(iaddr), oaddr);
183 PUTimm22(GETimm22(iaddr), oaddr);
188 PUTsimm13(GETsimm13(iaddr), oaddr);
197 PUTsimm10(GETsimm10(iaddr), oaddr);
202 PUTsimm11(GETsimm11(iaddr), oaddr);
206 #ifdef R_SPARC_WDISP16
207 case R_SPARC_WDISP16:
208 PUTdisp16(GETdisp16(iaddr), oaddr);
212 #ifdef R_SPARC_WDISP19
213 case R_SPARC_WDISP19:
214 PUTdisp19(GETdisp19(iaddr), oaddr);
220 PUTimm5(GETimm5(iaddr), oaddr);
226 PUTimm6(GETimm6(iaddr), oaddr);
232 PUTimm7(GETimm7(iaddr), oaddr);
242 update_reloc(Cache *ocache, Cache *_ocache,
243 Cache *icache, Cache *_icache,
249 Cache *orcache, *ircache;
252 * Set up to read the output relocation table.
254 shdr = _ocache->c_shdr;
255 rels = (Rel *)_ocache->c_data->d_buf;
256 reln = shdr->sh_size / shdr->sh_entsize;
259 * Determine the section that is being relocated.
261 orcache = &ocache[shdr->sh_info];
262 shdr = _icache->c_shdr;
263 ircache = &icache[shdr->sh_info];
266 * Loop through the relocation table.
268 for (cnt = 0; cnt < reln; cnt++, rels++) {
269 unsigned char *iaddr, *oaddr;
271 unsigned char type = ELF_R_TYPE(rels->r_info);
274 * Ignore some relocations as these can safely be carried out
275 * twice (they simply override any existing data). In fact,
276 * some relocations like __iob's copy relocation must be carried
277 * out each time the process restarts otherwise stdio blows up.
279 if ((type == R_SPARC_COPY) || (type == R_SPARC_JMP_SLOT) ||
280 (type == R_SPARC_NONE))
284 * If we are required to restore the relocation location
285 * to its value prior to relocation, then read the
286 * locations original contents from the input image and
287 * copy it to the output image.
289 off = rels->r_offset - ircache->c_shdr->sh_addr;
290 iaddr = (unsigned char *)ircache->c_data->d_buf + off;
291 oaddr = (unsigned char *)orcache->c_data->d_buf + off;
292 move_reloc(iaddr, oaddr, type);