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