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