xref: /titanic_51/usr/src/cmd/sgs/librtld/amd64/_relocate.c (revision fe54a78e1aacf39261ad56e9903bce02e3fb6d21)
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 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include	<string.h>
30 #include	"machdep.h"
31 #include	"reloc.h"
32 #include	"_librtld.h"
33 #include	"_elf.h"
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, uchar_t *oaddr, uchar_t *iaddr, Reloc *reloc)
43 {
44 	Rela		*rel = vrel;
45 	Xword		rtype = ELF_R_TYPE(rel->r_info, M_MACH);
46 
47 	switch (rtype) {
48 	case R_AMD64_NONE:
49 		break;
50 	case R_AMD64_COPY:
51 		(void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
52 		break;
53 	case R_AMD64_JUMP_SLOT:
54 		{
55 			/* LINTED */
56 			ulong_t *_oaddr = (ulong_t *)oaddr;
57 			/* LINTED */
58 			ulong_t *_iaddr = (ulong_t *)iaddr;
59 
60 			if (_iaddr)
61 				*_oaddr = *_iaddr + reloc->r_value;
62 			else
63 				*_oaddr = reloc->r_value;
64 		}
65 		break;
66 	default:
67 		{
68 			size_t re_fsize = reloc_table[rtype].re_fsize;
69 
70 			if (iaddr)
71 				(void) memcpy(oaddr, iaddr, re_fsize);
72 			else
73 				(void) memset(oaddr, 0, re_fsize);
74 		}
75 	}
76 }
77 
78 /*
79  * Copy a relocation record and increment its value.  The record must reflect
80  * the new address to which this image is fixed. Note that .got entries
81  * associated with .plt's must be fixed to the new base address.
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 	if (ELF_R_TYPE(nrel->r_info, M_MACH) == R_AMD64_JUMP_SLOT) {
92 		/* LINTED */
93 		ulong_t	*_oaddr = (ulong_t *)oaddr;
94 		/* LINTED */
95 		ulong_t	*_iaddr = (ulong_t *)iaddr;
96 
97 		if (_iaddr)
98 			*_oaddr = *_iaddr + reloc->r_value;
99 		else
100 			*_oaddr = reloc->r_value;
101 	}
102 
103 	*nrel = *orel;
104 	nrel->r_offset += reloc->r_value;
105 }
106 
107 /*
108  * Clear a relocation record.  The relocation has been applied to the image and
109  * thus the relocation must not occur again.
110  */
111 void
112 clear_reloc(void *vrel)
113 {
114 	Rela	*rel = vrel;
115 
116 	rel->r_offset = 0;
117 	rel->r_info = ELF_R_INFO(0, R_AMD64_NONE);
118 	rel->r_addend = 0;
119 }
120 
121 /*
122  * Apply a relocation to an image being built from an input file.  Use the
123  * runtime linkers routines to do the necessary magic.
124  */
125 void
126 apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr,
127     Rt_map *lmp)
128 {
129 	Rela	*rel = vrel;
130 	Xword	type = ELF_R_TYPE(rel->r_info, M_MACH);
131 	Xword	value = reloc->r_value + rel->r_addend;
132 
133 	if (type == R_AMD64_JUMP_SLOT) {
134 		uintptr_t	addr, vaddr;
135 
136 		if (FLAGS(lmp) & FLG_RT_FIXED)
137 			vaddr = 0;
138 		else
139 			vaddr = ADDR(lmp);
140 
141 		addr = (uintptr_t)oaddr - rel->r_offset;
142 		/* LINTED */
143 		elf_plt_write((uintptr_t)addr, vaddr, rel,
144 		    (uintptr_t)value, reloc->r_pltndx);
145 
146 	} else if (type == R_AMD64_COPY) {
147 		(void) memcpy((void *)oaddr, (void *)value,
148 		    (size_t)reloc->r_size);
149 	} else {
150 		(void) do_reloc_rtld(type, oaddr, &value, reloc->r_name, name,
151 		    LIST(lmp));
152 	}
153 }
154