xref: /titanic_52/usr/src/cmd/sgs/librtld/amd64/_relocate.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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 /*
37  * Undo relocations that have been applied to a memory image.  Basically this
38  * involves copying the original files relocation offset into the new image
39  * being created.
40  */
41 void
42 undo_reloc(void *vrel, unsigned char *oaddr, unsigned char *iaddr,
43     Reloc *reloc)
44 {
45 #if 0		/* XX64 -- fix me */
46 	Rel		*rel = vrel;
47 	/* LINTED */
48 	unsigned long	*_oaddr = (unsigned long *)oaddr;
49 	/* LINTED */
50 	unsigned long	*_iaddr = (unsigned long *)iaddr;
51 
52 	switch (ELF_R_TYPE(rel->r_info)) {
53 	case R_386_NONE:
54 		break;
55 
56 	case R_386_COPY:
57 		(void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
58 		break;
59 
60 	case R_386_JMP_SLOT:
61 		if (_iaddr)
62 			*_oaddr = *_iaddr + reloc->r_value;
63 		else
64 			*_oaddr = reloc->r_value;
65 		break;
66 
67 	default:
68 		if (_iaddr)
69 			*_oaddr = *_iaddr;
70 		else
71 			*_oaddr = 0;
72 		break;
73 	}
74 #endif	/* XX64 -- fix me */
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 void
84 inc_reloc(void *vnrel, void *vorel, Reloc *reloc, unsigned char *oaddr,
85     unsigned char *iaddr)
86 {
87 #if 0		/* XX64 -- fix me */
88 	Rel		*nrel = vnrel;
89 	Rel		*orel = vorel;
90 	/* LINTED */
91 	unsigned long	*_oaddr = (unsigned long *)oaddr;
92 	/* LINTED */
93 	unsigned long	*_iaddr = (unsigned long *)iaddr;
94 
95 	if (ELF_R_TYPE(nrel->r_info) == R_386_JMP_SLOT) {
96 		if (_iaddr)
97 			*_oaddr = *_iaddr + reloc->r_value;
98 		else
99 			*_oaddr = reloc->r_value;
100 	}
101 
102 	*nrel = *orel;
103 	nrel->r_offset += reloc->r_value;
104 #endif	/* XX64 -- fix me */
105 }
106 
107 
108 /*
109  * Clear a relocation record.  The relocation has been applied to the image and
110  * thus the relocation must not occur again.
111  */
112 void
113 clear_reloc(void * vrel)
114 {
115 #if 0		/* XX64 -- fix me */
116 	Rel *	rel = vrel;
117 
118 	rel->r_offset = 0;
119 	rel->r_info = ELF_R_INFO(0, R_386_NONE);
120 #endif	/* XX64 -- fix me */
121 }
122 
123 
124 /*
125  * Apply a relocation to an image being built from an input file.  Use the
126  * runtime linkers routines to do the necessary magic.
127  */
128 void
129 apply_reloc(void *vrel, Reloc *reloc, const char *name,
130     unsigned char *oaddr, Rt_map *lmp)
131 {
132 #if 0		/* XX64 -- fix me */
133 	Rel	*rel = vrel;
134 	Byte	type = ELF_R_TYPE(rel->r_info);
135 	Word	value = reloc->r_value;
136 
137 	if (type == R_386_JMP_SLOT) {
138 		uintptr_t	addr, vaddr;
139 
140 		if (FLAGS(lmp) & FLG_RT_FIXED)
141 			vaddr = 0;
142 		else
143 			vaddr = ADDR(lmp);
144 		addr = (uintptr_t)oaddr - rel->r_offset;
145 		/* LINTED */
146 		elf_plt_write((uintptr_t)addr, vaddr, rel,
147 		    (uintptr_t)value, reloc->r_pltndx);
148 	} else if (type == R_386_COPY) {
149 		(void) memcpy((void *)oaddr, (void *)value,
150 		    (size_t)reloc->r_size);
151 
152 	} else {
153 		(void) do_reloc(type, oaddr, &value, reloc->r_name, name);
154 	}
155 #endif	/* XX64 -- fix me */
156 }
157