xref: /titanic_41/usr/src/cmd/sgs/librtld/i386/_relocate.c (revision 1dd08564e4a3aafe66b00aee6f222b0885346fe8)
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 void
40 undo_reloc(void *vrel, uchar_t *oaddr, uchar_t *iaddr, Reloc *reloc)
41 {
42 	Rel	*rel = vrel;
43 	/* LINTED */
44 	ulong_t	*_oaddr = (ulong_t *)oaddr;
45 	/* LINTED */
46 	ulong_t	*_iaddr = (ulong_t *)iaddr;
47 
48 	switch (ELF_R_TYPE(rel->r_info, M_MACH)) {
49 	case R_386_NONE:
50 		break;
51 
52 	case R_386_COPY:
53 		(void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
54 		break;
55 
56 	case R_386_JMP_SLOT:
57 		if (_iaddr)
58 			*_oaddr = *_iaddr + reloc->r_value;
59 		else
60 			*_oaddr = reloc->r_value;
61 		break;
62 
63 	default:
64 		if (_iaddr)
65 			*_oaddr = *_iaddr;
66 		else
67 			*_oaddr = 0;
68 		break;
69 	}
70 }
71 
72 /*
73  * Copy a relocation record and increment its value.  The record must reflect
74  * the new address to which this image is fixed.  Note that .got entries
75  * associated with .plt's must be fixed to the new base address.
76  */
77 void
78 inc_reloc(void *vnrel, void *vorel, Reloc *reloc, uchar_t *oaddr,
79     uchar_t *iaddr)
80 {
81 	Rel	*nrel = vnrel;
82 	Rel	*orel = vorel;
83 	/* LINTED */
84 	ulong_t	*_oaddr = (ulong_t *)oaddr;
85 	/* LINTED */
86 	ulong_t	*_iaddr = (ulong_t *)iaddr;
87 
88 	if (ELF_R_TYPE(nrel->r_info, M_MACH) == R_386_JMP_SLOT) {
89 		if (_iaddr)
90 			*_oaddr = *_iaddr + reloc->r_value;
91 		else
92 			*_oaddr = reloc->r_value;
93 	}
94 
95 	*nrel = *orel;
96 	nrel->r_offset += reloc->r_value;
97 }
98 
99 /*
100  * Clear a relocation record.  The relocation has been applied to the image and
101  * thus the relocation must not occur again.
102  */
103 void
104 clear_reloc(void *vrel)
105 {
106 	Rel	*rel = vrel;
107 
108 	rel->r_offset = 0;
109 	rel->r_info = ELF_R_INFO(0, R_386_NONE);
110 }
111 
112 /*
113  * Apply a relocation to an image being built from an input file.  Use the
114  * runtime linkers routines to do the necessary magic.
115  */
116 void
117 apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr,
118     Rt_map *lmp)
119 {
120 	Rel	*rel = vrel;
121 	Xword	type = ELF_R_TYPE(rel->r_info, M_MACH);
122 	Word	value = reloc->r_value;
123 
124 	if (type == R_386_JMP_SLOT) {
125 		uintptr_t	addr, vaddr;
126 
127 		if (FLAGS(lmp) & FLG_RT_FIXED)
128 			vaddr = 0;
129 		else
130 			vaddr = ADDR(lmp);
131 		addr = (uintptr_t)oaddr - rel->r_offset;
132 		/* LINTED */
133 		elf_plt_write((uintptr_t)addr, vaddr, rel,
134 		    (uintptr_t)value, reloc->r_pltndx);
135 
136 	} else if (type == R_386_COPY) {
137 		(void) memcpy((void *)oaddr, (void *)value,
138 		    (size_t)reloc->r_size);
139 	} else {
140 		(void) do_reloc_rtld(type, oaddr, &value, reloc->r_name, name,
141 		    LIST(lmp));
142 	}
143 }
144