xref: /titanic_51/usr/src/cmd/sgs/librtld/sparc/_relocate.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	Copyright (c) 2000-2001 by Sun Microsystems, Inc.
24  *	All rights reserved.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include	<string.h>
29 #include	"machdep.h"
30 #include	"reloc.h"
31 #include	"_librtld.h"
32 #include	"_elf.h"
33 
34 
35 /*
36  * Undo relocations that have been applied to a memory image.  Basically this
37  * involves copying the original files relocation offset into the new image
38  * being created.
39  */
40 /* ARGSUSED3 */
41 void
42 undo_reloc(void *vrel, unsigned char *oaddr, unsigned char *iaddr,
43     Reloc *reloc)
44 {
45 	Rela		*rel = vrel;
46 	const Rel_entry	*rep;
47 	int		rtype = ELF_R_TYPE(rel->r_info);
48 	unsigned long	*_oaddr;
49 	unsigned long	*_iaddr;
50 
51 	switch (rtype) {
52 	case R_SPARC_NONE:
53 		break;
54 	case R_SPARC_COPY:
55 		(void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
56 		break;
57 	case R_SPARC_JMP_SLOT:
58 		/* LINTED */
59 		_oaddr = (unsigned long *)oaddr;
60 		/* LINTED */
61 		_iaddr = (unsigned long *)iaddr;
62 
63 		if (_iaddr) {
64 			*_oaddr++ = *_iaddr++;
65 			*_oaddr++ = *_iaddr++;
66 			*_oaddr = *_iaddr;
67 		} else {
68 			*_oaddr++ = 0;
69 			*_oaddr++ = 0;
70 			*_oaddr = 0;
71 		}
72 		break;
73 	default:
74 		rep = &reloc_table[rtype];
75 		if (_iaddr)
76 			(void) memcpy(oaddr, iaddr, rep->re_fsize);
77 		else
78 			(void) memset(oaddr, 0, rep->re_fsize);
79 		break;
80 	}
81 }
82 
83 
84 /*
85  * Copy a relocation record and increment its value.  The record must reflect
86  * the new address to which this image is fixed.
87  */
88 /* ARGSUSED3 */
89 void
90 inc_reloc(void *vnrel, void *vorel, Reloc *reloc, unsigned char *oaddr,
91     unsigned char *iaddr)
92 {
93 	Rela	*nrel = vnrel;
94 	Rela	*orel = vorel;
95 
96 	*nrel = *orel;
97 	nrel->r_offset += reloc->r_value;
98 }
99 
100 
101 /*
102  * Clear a relocation record.  The relocation has been applied to the image and
103  * thus the relocation must not occur again.
104  */
105 void
106 clear_reloc(void *vrel)
107 {
108 	Rela	*rel = vrel;
109 
110 	rel->r_offset = 0;
111 	rel->r_info = ELF_R_INFO(0, R_SPARC_NONE);
112 	rel->r_addend = 0;
113 }
114 
115 
116 /*
117  * Apply a relocation to an image being built from an input file.  Use the
118  * runtime linkers routines to do the necessary magic.
119  */
120 void
121 apply_reloc(void *vrel, Reloc *reloc, const char *name,
122     unsigned char *oaddr, Rt_map *lmp)
123 {
124 	Rela		*rel = vrel;
125 	Byte		type = (Byte)ELF_R_TYPE(rel->r_info);
126 	Xword		value = reloc->r_value + rel->r_addend;
127 
128 	if (type == R_SPARC_JMP_SLOT) {
129 		uintptr_t	addr, vaddr;
130 
131 		if (FLAGS(lmp) & FLG_RT_FIXED)
132 			vaddr = 0;
133 		else
134 			vaddr = ADDR(lmp);
135 
136 		addr = (uintptr_t)oaddr - rel->r_offset;
137 		/* LINTED */
138 		elf_plt_write((uintptr_t)addr, vaddr, rel,
139 		    (uintptr_t)value, reloc->r_pltndx);
140 	} else if (type == R_SPARC_COPY) {
141 		(void) memcpy((void *)oaddr, (void *)value,
142 		    (size_t)reloc->r_size);
143 
144 	} else {
145 		(void) do_reloc(type, oaddr, &value, reloc->r_name, name);
146 	}
147 }
148