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