xref: /titanic_41/usr/src/cmd/sgs/librtld/common/dynamic.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Update any dynamic entry offsets.  One issue with dynamic entries is that
27  * you only know whether they refer to a value or an offset if you know each
28  * type.  Thus we check for all types we know about, it a type is found that
29  * we don't know about then return and error as we have no idea what to do.
30  */
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include	<libelf.h>
34 #include	<link.h>
35 #include	"libld.h"
36 #include	"msg.h"
37 #include	"rtld.h"
38 #include	"_librtld.h"
39 
40 int
41 update_dynamic(Cache * cache, Cache * _cache, Rt_map * lmp, int flags,
42     Addr addr, Off off, const char *file, Xword null, Xword data, Xword func,
43     Xword entsize, Xword checksum)
44 {
45 	Dyn *		dyn = (Dyn *)_cache->c_data->d_buf, * posdyn = 0;
46 	const char	*strs;
47 	Cache *		__cache;
48 
49 	/*
50 	 * If we're dealing with an object that might have bound to an external
51 	 * dependency establish our string table for possible NEEDED processing.
52 	 */
53 	if (flags & RTLD_REL_DEPENDS) {
54 		__cache = &cache[_cache->c_shdr->sh_link];
55 		strs = (const char *)__cache->c_data->d_buf;
56 	}
57 
58 	/*
59 	 * Loop through the dynamic table updating all offsets.
60 	 */
61 	while (dyn->d_tag != DT_NULL) {
62 		switch ((Xword)dyn->d_tag) {
63 		case DT_NEEDED:
64 			if (posdyn) {
65 				Rt_map *	dlmp;
66 
67 				/*
68 				 * Determine whether this dependency has been
69 				 * loaded (this is the most generic way to check
70 				 * any alias names), and if it has been bound
71 				 * to, undo any lazy-loading position flag.
72 				 */
73 				if (dlmp = is_so_loaded(LIST(lmp),
74 				    (strs + dyn->d_un.d_val), 1)) {
75 					Bnd_desc **	bdpp;
76 					Aliste		off;
77 
78 					for (ALIST_TRAVERSE(DEPENDS(lmp), off,
79 					    bdpp)) {
80 						if (dlmp == (*bdpp)->b_depend) {
81 						    posdyn->d_un.d_val &=
82 							~DF_P1_LAZYLOAD;
83 						    break;
84 						}
85 					}
86 				}
87 			}
88 			break;
89 
90 		case DT_RELAENT:
91 		case DT_STRSZ:
92 		case DT_SYMENT:
93 		case DT_SONAME:
94 		case DT_RPATH:
95 		case DT_SYMBOLIC:
96 		case DT_RELENT:
97 		case DT_PLTREL:
98 		case DT_TEXTREL:
99 		case DT_VERDEFNUM:
100 		case DT_VERNEEDNUM:
101 		case DT_AUXILIARY:
102 		case DT_USED:
103 		case DT_FILTER:
104 		case DT_DEPRECATED_SPARC_REGISTER:
105 		case M_DT_REGISTER:
106 		case DT_BIND_NOW:
107 		case DT_INIT_ARRAYSZ:
108 		case DT_FINI_ARRAYSZ:
109 		case DT_RUNPATH:
110 		case DT_FLAGS:
111 		case DT_CONFIG:
112 		case DT_DEPAUDIT:
113 		case DT_AUDIT:
114 			break;
115 		case DT_PLTGOT:
116 		case DT_HASH:
117 		case DT_STRTAB:
118 		case DT_SYMTAB:
119 		case DT_INIT:
120 		case DT_FINI:
121 		case DT_VERSYM:
122 		case DT_VERDEF:
123 		case DT_VERNEED:
124 		case DT_INIT_ARRAY:
125 		case DT_FINI_ARRAY:
126 			dyn->d_un.d_ptr += addr;
127 			break;
128 
129 		/*
130 		 * If the memory image is being used, this element would have
131 		 * been initialized to the runtime linkers internal link-map
132 		 * list.  Clear it.
133 		 */
134 		case DT_DEBUG:
135 			dyn->d_un.d_val = 0;
136 			break;
137 
138 		/*
139 		 * The number of relocations may have been reduced if
140 		 * relocations have been saved in the new image.  Thus we
141 		 * compute the new relocation size and start.
142 		 */
143 		case DT_RELASZ:
144 		case DT_RELSZ:
145 			dyn->d_un.d_val = ((data + func) * entsize);
146 			break;
147 
148 		case DT_RELA:
149 		case DT_REL:
150 			dyn->d_un.d_ptr = (addr + off + (null * entsize));
151 			break;
152 
153 		/*
154 		 * If relative relocations have been processed clear the count.
155 		 */
156 		case DT_RELACOUNT:
157 		case DT_RELCOUNT:
158 			if (flags & RTLD_REL_RELATIVE)
159 				dyn->d_un.d_val = 0;
160 			break;
161 
162 		case DT_PLTRELSZ:
163 			dyn->d_un.d_val = (func * entsize);
164 			break;
165 
166 		case DT_JMPREL:
167 			dyn->d_un.d_ptr = (addr + off +
168 				((null + data) * entsize));
169 			break;
170 
171 		/*
172 		 * Recompute the images elf checksum.
173 		 */
174 		case DT_CHECKSUM:
175 			dyn->d_un.d_val = checksum;
176 			break;
177 
178 		/*
179 		 * If a flag entry is available, indicate if this image has
180 		 * been generated via the configuration process (crle(1)).
181 		 * Because we only started depositing DT_FLAGS_1 entries in all
182 		 * objects starting with Solaris 8, set a feature flag if it
183 		 * is present (these got added in Solaris 7).
184 		 * The runtime linker may use this flag to search for a local
185 		 * configuration file - this is only meaningful in executables
186 		 * but the flag has value for identifying images regardless.
187 		 *
188 		 * If this file is acting as a filter, and dependency
189 		 * relocations have been processed (a filter is thought of as a
190 		 * dependency in terms of symbol binding), we may have bound to
191 		 * the filtee, and hence carried out the relocation.  Indicate
192 		 * that the filtee must be preloaded, as the .plt won't get
193 		 * exercised to cause its normal loading.
194 		 */
195 		case DT_FLAGS_1:
196 			if (flags & RTLD_CONFSET)
197 				dyn->d_un.d_val |= DF_1_CONFALT;
198 			if ((flags & RTLD_REL_DEPENDS) &&
199 			    (FLAGS1(lmp)) & MSK_RT_FILTER)
200 				dyn->d_un.d_val |= DF_1_LOADFLTR;
201 			break;
202 
203 		case DT_FEATURE_1:
204 			if (flags & RTLD_CONFSET)
205 				dyn->d_un.d_val |= DTF_1_CONFEXP;
206 			break;
207 
208 		/*
209 		 * If a position flag is available save it for possible update
210 		 * when processing the next NEEDED tag.
211 		 */
212 		case DT_POSFLAG_1:
213 			if (flags & RTLD_REL_DEPENDS) {
214 				posdyn = dyn++;
215 				continue;
216 			}
217 			break;
218 
219 		/*
220 		 * Collect the defaults.
221 		 */
222 		default:
223 			/*
224 			 * If d_val is used, don't touch.
225 			 */
226 			if ((dyn->d_tag >= DT_VALRNGLO) &&
227 			    (dyn->d_tag <= DT_VALRNGHI))
228 				break;
229 
230 			/*
231 			 * If d_ptr is used, adjust.  Note, some entries that
232 			 * fell into this range are offsets into the dynamic
233 			 * string table.  Although these would need modifying
234 			 * if the section itself were resized, there is no
235 			 * resizing with dldump().  Entries that correspond to
236 			 * offsets are picked off in the initial DT_ loop
237 			 * above.
238 			 */
239 			if ((dyn->d_tag >= DT_ADDRRNGLO) &&
240 			    (dyn->d_tag <= DT_ADDRRNGHI)) {
241 				dyn->d_un.d_ptr += addr;
242 				break;
243 			}
244 
245 			/*
246 			 * Check to see if this DT_ entry conforms
247 			 * to the DT_ENCODING rules.
248 			 */
249 			if ((dyn->d_tag >= DT_ENCODING) &&
250 			    (dyn->d_tag <= DT_HIOS)) {
251 				/*
252 				 * Even tag values are ADDRESS encodings
253 				 */
254 				if ((dyn->d_tag % 2) == 0) {
255 					dyn->d_un.d_ptr += addr;
256 				}
257 				break;
258 			}
259 			eprintf(ERR_WARNING, MSG_INTL(MSG_DT_UNKNOWN), file,
260 			    EC_XWORD(dyn->d_tag));
261 			return (1);
262 		}
263 		posdyn = 0;
264 		dyn++;
265 	}
266 	return (0);
267 }
268