summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sgs/librtld/sparcv9/_relocate.c
blob: 5a6cd945f3bedddf4a5cd3b25a607b4f81edb295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include	<string.h>
#include	"machdep.h"
#include	"reloc.h"
#include	"_librtld.h"
#include	"_elf.h"

/*
 * Undo relocations that have been applied to a memory image.  Basically this
 * involves copying the original files relocation offset into the new image
 * being created.
 */
/* ARGSUSED3 */
void
undo_reloc(void *vrel, uchar_t *oaddr, uchar_t *iaddr, Reloc *reloc)
{
	Rela		*rel = vrel;
	const Rel_entry	*rep;
	Xword		rtype = ELF_R_TYPE(rel->r_info, M_MACH);
	ulong_t		*_oaddr;
	ulong_t		*_iaddr;

	switch (rtype) {
	case R_SPARC_NONE:
		break;
	case R_SPARC_COPY:
		(void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
		break;
	case R_SPARC_JMP_SLOT:
		/* LINTED */
		_oaddr = (unsigned long *)oaddr;
		/* LINTED */
		_iaddr = (unsigned long *)iaddr;

		if (_iaddr) {
			*_oaddr++ = *_iaddr++;
			*_oaddr++ = *_iaddr++;
			*_oaddr = *_iaddr;
		} else {
			*_oaddr++ = 0;
			*_oaddr++ = 0;
			*_oaddr = 0;
		}
		break;
	default:
		rep = &reloc_table[rtype];
		if (iaddr)
			(void) memcpy(oaddr, iaddr, rep->re_fsize);
		else
			(void) memset(oaddr, 0, rep->re_fsize);
	}
}

/*
 * Copy a relocation record and increment its value.  The record must reflect
 * the new address to which this image is fixed.
 */
/* ARGSUSED3 */
void
inc_reloc(void *vnrel, void *vorel, Reloc *reloc, uchar_t *oaddr,
    uchar_t *iaddr)
{
	Rela	*nrel = vnrel;
	Rela	*orel = vorel;

	*nrel = *orel;
	nrel->r_offset += reloc->r_value;
}

/*
 * Clear a relocation record.  The relocation has been applied to the image and
 * thus the relocation must not occur again.
 */
void
clear_reloc(void *vrel)
{
	Rela	*rel = vrel;

	rel->r_offset = 0;
	rel->r_info = ELF_R_INFO(0, R_SPARC_NONE);
	rel->r_addend = 0;
}

/*
 * Apply a relocation to an image being built from an input file.  Use the
 * runtime linkers routines to do the necessary magic.
 */
void
apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr,
    Rt_map *lmp)
{
	Rela	*rel = vrel;
	Xword	type = ELF_R_TYPE(rel->r_info, M_MACH);
	Xword	value = reloc->r_value + rel->r_addend;

	if (type == R_SPARC_JMP_SLOT) {
		uintptr_t	addr, vaddr;

		if (FLAGS(lmp) & FLG_RT_FIXED)
			vaddr = 0;
		else
			vaddr = ADDR(lmp);

		addr = (uintptr_t)oaddr - rel->r_offset;
		/* LINTED */
		(void) elf_plt_write((uintptr_t)addr, vaddr, rel,
		    (uintptr_t)value, reloc->r_pltndx);

	} else if (type == R_SPARC_COPY) {
		(void) memcpy((void *)oaddr, (void *)value,
		    (size_t)reloc->r_size);
	} else {
		if (IS_EXTOFFSET(type))
			value += ELF_R_TYPE_DATA(rel->r_info);
		(void) do_reloc_rtld(type, oaddr, &value, reloc->r_name, name,
		    LIST(lmp));
	}
}