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