xref: /titanic_44/usr/src/cmd/sgs/librtld/sparcv9/_relocate.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
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	<stdio.h>
30 #include	"machdep.h"
31 #include	"reloc.h"
32 #include	"_librtld.h"
33 #include	"_elf.h"
34 
35 
36 
37 /*
38  * Undo relocations that have been applied to a memory image.  Basically this
39  * involves copying the original files relocation offset into the new image
40  * being created.
41  */
42 /* ARGSUSED3 */
43 void
44 undo_reloc(void *vrel, unsigned char *oaddr, unsigned char *iaddr,
45     Reloc *reloc)
46 {
47 	Rela		*rel = vrel;
48 	const Rel_entry	*rep;
49 	/* LINTED */
50 	int		rtype = (int)ELF_R_TYPE(rel->r_info);
51 	unsigned long	*_oaddr;
52 	unsigned long	*_iaddr;
53 
54 	switch (rtype) {
55 	case R_SPARC_NONE:
56 		break;
57 	case R_SPARC_COPY:
58 		(void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
59 		break;
60 	case R_SPARC_JMP_SLOT:
61 		/* LINTED */
62 		_oaddr = (unsigned long *)oaddr;
63 		/* LINTED */
64 		_iaddr = (unsigned long *)iaddr;
65 		if (_iaddr) {
66 			*_oaddr++ = *_iaddr++;
67 			*_oaddr++ = *_iaddr++;
68 			*_oaddr = *_iaddr;
69 		} else {
70 			*_oaddr++ = 0;
71 			*_oaddr++ = 0;
72 			*_oaddr = 0;
73 		}
74 		break;
75 	default:
76 		rep = &reloc_table[rtype];
77 		if (iaddr)
78 			(void) memcpy(oaddr, iaddr, rep->re_fsize);
79 		else
80 			(void) memset(oaddr, 0, rep->re_fsize);
81 	}
82 }
83 
84 
85 /*
86  * Copy a relocation record and increment its value.  The record must reflect
87  * the new address to which this image is fixed.
88  */
89 /* ARGSUSED3 */
90 void
91 inc_reloc(void *vnrel, void *vorel, Reloc *reloc, unsigned char *oaddr,
92     unsigned char *iaddr)
93 {
94 	Rela	*nrel = vnrel;
95 	Rela	*orel = vorel;
96 
97 	*nrel = *orel;
98 	nrel->r_offset += reloc->r_value;
99 }
100 
101 
102 /*
103  * Clear a relocation record.  The relocation has been applied to the image and
104  * thus the relocation must not occur again.
105  */
106 void
107 clear_reloc(void *vrel)
108 {
109 	Rela	*rel = vrel;
110 
111 	rel->r_offset = 0;
112 	rel->r_info = ELF_R_INFO(0, R_SPARC_NONE);
113 	rel->r_addend = 0;
114 }
115 
116 
117 /*
118  * Apply a relocation to an image being built from an input file.  Use the
119  * runtime linkers routines to do the necessary magic.
120  */
121 void
122 apply_reloc(void *vrel, Reloc *reloc, const char *name,
123     unsigned char *oaddr, Rt_map *lmp)
124 {
125 	Rela	*rel = vrel;
126 	/* LINTED */
127 	Byte	type = (Byte)ELF_R_TYPE(rel->r_info);
128 	Xword	value = reloc->r_value + rel->r_addend;
129 
130 	if (type == R_SPARC_JMP_SLOT) {
131 		uintptr_t	addr, vaddr;
132 
133 		if (FLAGS(lmp) & FLG_RT_FIXED)
134 			vaddr = 0;
135 		else
136 			vaddr = ADDR(lmp);
137 
138 		addr = (uintptr_t)oaddr - rel->r_offset;
139 		/* LINTED */
140 		elf_plt_write((uintptr_t)addr, vaddr, rel,
141 		    (uintptr_t)value, reloc->r_pltndx);
142 
143 	} else if (type == R_SPARC_COPY) {
144 		(void) memcpy((void *)oaddr, (void *)value,
145 		    (size_t)reloc->r_size);
146 
147 	} else {
148 		if (IS_EXTOFFSET(type))
149 			value += ELF_R_TYPE_DATA(rel->r_info);
150 		(void) do_reloc(type, oaddr, &value, reloc->r_name, name);
151 	}
152 }
153