xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/move.c (revision 95efa359b507db290a2484b8f615822b1e06096f)
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 /*
28  * Object file dependent support for ELF objects.
29  */
30 
31 #include	<stdio.h>
32 #include	<sys/procfs.h>
33 #include	<sys/mman.h>
34 #include	<dlfcn.h>
35 #include	<debug.h>
36 #include	<conv.h>
37 #include	"_rtld.h"
38 #include	"_audit.h"
39 #include	"_elf.h"
40 #include	"msg.h"
41 
42 /*
43  * For backward compatibility copy relocation processing, it can be necessary to
44  * determine if a copy destination is also the recipient of a move record.  For
45  * these instances, the move record addresses are retained for is_move_data().
46  */
47 static	APlist	*alp = NULL;
48 
49 /*
50  * Move data
51  */
52 int
53 move_data(Rt_map *lmp)
54 {
55 	Lm_list	*lml = LIST(lmp);
56 	Move	*mv = MOVETAB(lmp);
57 	ulong_t	num, mvnum = MOVESZ(lmp) / MOVEENT(lmp);
58 	int	moves;
59 
60 	/*
61 	 * If these records are against the executable, and the executable was
62 	 * built prior to Solaris 8, keep track of the move record symbol.  See
63 	 * comment in analyze.c:lookup_sym_interpose() in regards Solaris 8
64 	 * objects and DT_FLAGS.
65 	 */
66 	moves = (lmp == lml->lm_head) && ((FLAGS2(lmp) & FL2_RT_DTFLAGS) == 0);
67 
68 	DBG_CALL(Dbg_move_data(lmp));
69 	for (num = 0; num < mvnum; num++, mv++) {
70 		Addr	addr, taddr;
71 		Half 	rep, repno, stride;
72 		Sym	*sym;
73 
74 		if ((sym = (Sym *)SYMTAB(lmp) + ELF_M_SYM(mv->m_info)) == 0)
75 			continue;
76 
77 		stride = mv->m_stride + 1;
78 		addr = sym->st_value;
79 		if ((FLAGS(lmp) & FLG_RT_FIXED) == 0)
80 			addr += ADDR(lmp);
81 		taddr = addr + mv->m_poffset;
82 
83 		DBG_CALL(Dbg_move_entry2(lml, mv, sym->st_name,
84 		    (const char *)(sym->st_name + STRTAB(lmp))));
85 
86 		for (rep = 0, repno = 0; rep < mv->m_repeat; rep++) {
87 			DBG_CALL(Dbg_move_expand(lml, mv, taddr));
88 
89 			switch (ELF_M_SIZE(mv->m_info)) {
90 			case 1:
91 				*((char *)taddr) = (char)mv->m_value;
92 				taddr += stride;
93 				repno++;
94 				break;
95 			case 2:
96 				/* LINTED */
97 				*((Half *)taddr) = (Half)mv->m_value;
98 				taddr += 2 * stride;
99 				repno++;
100 				break;
101 			case 4:
102 				/* LINTED */
103 				*((Word *)taddr) = (Word)mv->m_value;
104 				taddr += 4 * stride;
105 				repno++;
106 				break;
107 			case 8:
108 				/* LINTED */
109 				*((unsigned long long *)taddr) = mv->m_value;
110 				taddr += 8 * stride;
111 				repno++;
112 				break;
113 			default:
114 				eprintf(lml, ERR_NONE, MSG_INTL(MSG_MOVE_ERR1));
115 				break;
116 			}
117 		}
118 
119 		/*
120 		 * If any move records have been applied to this symbol, retain
121 		 * the symbol address if required for backward compatibility
122 		 * copy relocation processing.
123 		 */
124 		if (moves && repno &&
125 		    (aplist_append(&alp, (void *)addr, AL_CNT_MOVES) == 0))
126 			return (0);
127 	}
128 
129 	/*
130 	 * Binaries built in the early 1990's prior to Solaris 8, using the ild
131 	 * incremental linker are known to have zero filled move sections
132 	 * (presumably place holders for new, incoming move sections).  If no
133 	 * move records have been processed, remove the move identifier to
134 	 * optimize the amount of backward compatibility copy relocation
135 	 * processing that is needed.
136 	 */
137 	if (moves && (alp == NULL))
138 		FLAGS(lmp) &= ~FLG_RT_MOVE;
139 
140 	return (1);
141 }
142 
143 /*
144  * Determine whether an address is the recipient of a move record.
145  * Returns 1 if the address matches a move symbol, 0 otherwise.
146  */
147 int
148 is_move_data(caddr_t addr)
149 {
150 	caddr_t	maddr;
151 	Aliste	idx;
152 
153 	for (APLIST_TRAVERSE(alp, idx, maddr)) {
154 		if (addr == maddr)
155 			return (1);
156 	}
157 	return (0);
158 }
159