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