xref: /titanic_51/usr/src/cmd/sgs/rtld/common/move.c (revision b885580b43755ee4ea1e280b85428893d2ba9291)
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 /*
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	"_inline.h"
41 #include	"msg.h"
42 
43 /*
44  * For backward compatibility copy relocation processing, it can be necessary to
45  * determine if a copy destination is also the recipient of a move record.  For
46  * these instances, the move record addresses are retained for is_move_data().
47  */
48 static	APlist	*alp = NULL;
49 
50 /*
51  * Warning message for bad move target.
52  */
53 void
54 elf_move_bad(Lm_list *lml, Rt_map *lmp, Sym *sym, ulong_t num, Addr addr)
55 {
56 	const char	*name;
57 	int		trace;
58 
59 	trace = (lml->lm_flags & LML_FLG_TRC_ENABLE) &&
60 	    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
61 	    (lml->lm_flags & (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_WARN)));
62 
63 	if ((trace == 0) && (DBG_ENABLED == 0))
64 		return;
65 
66 	if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
67 		name = (const char *)(STRTAB(lmp) + sym->st_name);
68 	else
69 		name = MSG_INTL(MSG_STR_UNKNOWN);
70 
71 	if (trace)
72 		(void) printf(MSG_INTL(MSG_LDD_MOVE_ERR), EC_XWORD(num), name,
73 		    EC_ADDR(addr));
74 	else
75 		DBG_CALL(Dbg_move_bad(lml, num, name, addr));
76 }
77 
78 /*
79  * Move data.  Apply sparse initialization to data in zeroed bss.
80  */
81 int
82 move_data(Rt_map *lmp, APlist **textrel)
83 {
84 	Lm_list		*lml = LIST(lmp);
85 	Move		*mv = MOVETAB(lmp);
86 	ulong_t		num, mvnum = MOVESZ(lmp) / MOVEENT(lmp);
87 	int		moves;
88 
89 	/*
90 	 * If these records are against the executable, and the executable was
91 	 * built prior to Solaris 8, keep track of the move record symbol.  See
92 	 * comment in analyze.c:lookup_sym_interpose() in regards Solaris 8
93 	 * objects and DT_FLAGS.
94 	 */
95 	moves = (lmp == lml->lm_head) && ((FLAGS1(lmp) & FL1_RT_DTFLAGS) == 0);
96 
97 	DBG_CALL(Dbg_move_data(lmp));
98 	for (num = 0; num < mvnum; num++, mv++) {
99 		mmapobj_result_t	*mpp;
100 		Addr			addr, taddr;
101 		Half 			rep, repno, stride;
102 		Sym			*sym;
103 
104 		if ((sym = (Sym *)SYMTAB(lmp) + ELF_M_SYM(mv->m_info)) == 0)
105 			continue;
106 
107 		stride = mv->m_stride + 1;
108 		addr = sym->st_value;
109 
110 		/*
111 		 * Determine the move data target, and verify the address is
112 		 * writable.
113 		 */
114 		if ((FLAGS(lmp) & FLG_RT_FIXED) == 0)
115 			addr += ADDR(lmp);
116 		taddr = addr + mv->m_poffset;
117 
118 		if ((mpp = find_segment((caddr_t)taddr, lmp)) == NULL) {
119 			elf_move_bad(lml, lmp, sym, num, taddr);
120 			continue;
121 		}
122 		if (((mpp->mr_prot & PROT_WRITE) == 0) &&
123 		    ((set_prot(lmp, mpp, 1) == 0) ||
124 		    (aplist_append(textrel, mpp, AL_CNT_TEXTREL) == NULL)))
125 			return (0);
126 
127 		DBG_CALL(Dbg_move_entry2(lml, mv, sym->st_name,
128 		    (const char *)(sym->st_name + STRTAB(lmp))));
129 
130 		for (rep = 0, repno = 0; rep < mv->m_repeat; rep++) {
131 			DBG_CALL(Dbg_move_expand(lml, mv, taddr));
132 
133 			switch (ELF_M_SIZE(mv->m_info)) {
134 			case 1:
135 				*((char *)taddr) = (char)mv->m_value;
136 				taddr += stride;
137 				repno++;
138 				break;
139 			case 2:
140 				/* LINTED */
141 				*((Half *)taddr) = (Half)mv->m_value;
142 				taddr += 2 * stride;
143 				repno++;
144 				break;
145 			case 4:
146 				/* LINTED */
147 				*((Word *)taddr) = (Word)mv->m_value;
148 				taddr += 4 * stride;
149 				repno++;
150 				break;
151 			case 8:
152 				/* LINTED */
153 				*((unsigned long long *)taddr) = mv->m_value;
154 				taddr += 8 * stride;
155 				repno++;
156 				break;
157 			default:
158 				eprintf(lml, ERR_NONE, MSG_INTL(MSG_MOVE_ERR1));
159 				break;
160 			}
161 		}
162 
163 		/*
164 		 * If any move records have been applied to this symbol, retain
165 		 * the symbol address if required for backward compatibility
166 		 * copy relocation processing.
167 		 */
168 		if (moves && repno &&
169 		    (aplist_append(&alp, (void *)addr, AL_CNT_MOVES) == NULL))
170 			return (0);
171 	}
172 
173 	/*
174 	 * Binaries built in the early 1990's prior to Solaris 8, using the ild
175 	 * incremental linker are known to have zero filled move sections
176 	 * (presumably place holders for new, incoming move sections).  If no
177 	 * move records have been processed, remove the move identifier to
178 	 * optimize the amount of backward compatibility copy relocation
179 	 * processing that is needed.
180 	 */
181 	if (moves && (alp == NULL))
182 		FLAGS(lmp) &= ~FLG_RT_MOVE;
183 
184 	return (1);
185 }
186 
187 /*
188  * Determine whether an address is the recipient of a move record.
189  * Returns 1 if the address matches a move symbol, 0 otherwise.
190  */
191 int
192 is_move_data(caddr_t addr)
193 {
194 	caddr_t	maddr;
195 	Aliste	idx;
196 
197 	for (APLIST_TRAVERSE(alp, idx, maddr)) {
198 		if (addr == maddr)
199 			return (1);
200 	}
201 	return (0);
202 }
203