17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
23*f441771bSRod Evans  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Update any dynamic entry offsets.  One issue with dynamic entries is that
267c478bd9Sstevel@tonic-gate  * you only know whether they refer to a value or an offset if you know each
277c478bd9Sstevel@tonic-gate  * type.  Thus we check for all types we know about, it a type is found that
287c478bd9Sstevel@tonic-gate  * we don't know about then return and error as we have no idea what to do.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include	<libelf.h>
327c478bd9Sstevel@tonic-gate #include	<link.h>
337c478bd9Sstevel@tonic-gate #include	"libld.h"
347c478bd9Sstevel@tonic-gate #include	"msg.h"
357c478bd9Sstevel@tonic-gate #include	"rtld.h"
367c478bd9Sstevel@tonic-gate #include	"_librtld.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate int
update_dynamic(Cache * cache,Cache * _cache,Rt_map * lmp,int flags,Addr addr,Off off,const char * file,Xword null,Xword data,Xword func,Xword entsize,Xword checksum)395aefb655Srie update_dynamic(Cache *cache, Cache *_cache, Rt_map *lmp, int flags,
407c478bd9Sstevel@tonic-gate     Addr addr, Off off, const char *file, Xword null, Xword data, Xword func,
417c478bd9Sstevel@tonic-gate     Xword entsize, Xword checksum)
427c478bd9Sstevel@tonic-gate {
435aefb655Srie 	Dyn		*dyn = (Dyn *)_cache->c_data->d_buf, *posdyn = 0;
447c478bd9Sstevel@tonic-gate 	const char	*strs;
455aefb655Srie 	Cache		*__cache;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	/*
487c478bd9Sstevel@tonic-gate 	 * If we're dealing with an object that might have bound to an external
497c478bd9Sstevel@tonic-gate 	 * dependency establish our string table for possible NEEDED processing.
507c478bd9Sstevel@tonic-gate 	 */
517c478bd9Sstevel@tonic-gate 	if (flags & RTLD_REL_DEPENDS) {
527c478bd9Sstevel@tonic-gate 		__cache = &cache[_cache->c_shdr->sh_link];
537c478bd9Sstevel@tonic-gate 		strs = (const char *)__cache->c_data->d_buf;
547c478bd9Sstevel@tonic-gate 	}
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	/*
577c478bd9Sstevel@tonic-gate 	 * Loop through the dynamic table updating all offsets.
587c478bd9Sstevel@tonic-gate 	 */
597c478bd9Sstevel@tonic-gate 	while (dyn->d_tag != DT_NULL) {
60*f441771bSRod Evans 		Rt_map	*dlmp;
61*f441771bSRod Evans 
627c478bd9Sstevel@tonic-gate 		switch ((Xword)dyn->d_tag) {
637c478bd9Sstevel@tonic-gate 		case DT_NEEDED:
64*f441771bSRod Evans 			if (posdyn == 0)
65*f441771bSRod Evans 				break;
66*f441771bSRod Evans 
67*f441771bSRod Evans 			/*
68*f441771bSRod Evans 			 * Determine whether this dependency has been loaded
69*f441771bSRod Evans 			 * (this is the most generic way to check any alias
70*f441771bSRod Evans 			 * names), and if it has been bound to, undo any
71*f441771bSRod Evans 			 * lazy-loading or deferred position flag.
72*f441771bSRod Evans 			 */
73*f441771bSRod Evans 			if (dlmp = is_so_loaded(LIST(lmp),
74*f441771bSRod Evans 			    (strs + dyn->d_un.d_val), NULL)) {
75*f441771bSRod Evans 				Bnd_desc	*bdp;
76*f441771bSRod Evans 				Aliste		idx;
777c478bd9Sstevel@tonic-gate 
78*f441771bSRod Evans 				for (APLIST_TRAVERSE(DEPENDS(lmp), idx, bdp)) {
79*f441771bSRod Evans 					if (dlmp != bdp->b_depend)
80*f441771bSRod Evans 						continue;
81cce0e03bSab 
82*f441771bSRod Evans 					posdyn->d_un.d_val &=
83*f441771bSRod Evans 					    ~(DF_P1_LAZYLOAD | DF_P1_DEFERRED);
84*f441771bSRod Evans 					break;
857c478bd9Sstevel@tonic-gate 				}
867c478bd9Sstevel@tonic-gate 			}
877c478bd9Sstevel@tonic-gate 			break;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 		case DT_RELAENT:
907c478bd9Sstevel@tonic-gate 		case DT_STRSZ:
917c478bd9Sstevel@tonic-gate 		case DT_SYMENT:
927c478bd9Sstevel@tonic-gate 		case DT_SONAME:
937c478bd9Sstevel@tonic-gate 		case DT_RPATH:
947c478bd9Sstevel@tonic-gate 		case DT_SYMBOLIC:
957c478bd9Sstevel@tonic-gate 		case DT_RELENT:
967c478bd9Sstevel@tonic-gate 		case DT_PLTREL:
977c478bd9Sstevel@tonic-gate 		case DT_TEXTREL:
987c478bd9Sstevel@tonic-gate 		case DT_VERDEFNUM:
997c478bd9Sstevel@tonic-gate 		case DT_VERNEEDNUM:
1007c478bd9Sstevel@tonic-gate 		case DT_AUXILIARY:
1017c478bd9Sstevel@tonic-gate 		case DT_USED:
1027c478bd9Sstevel@tonic-gate 		case DT_FILTER:
1037c478bd9Sstevel@tonic-gate 		case DT_DEPRECATED_SPARC_REGISTER:
1047c478bd9Sstevel@tonic-gate 		case M_DT_REGISTER:
1057c478bd9Sstevel@tonic-gate 		case DT_BIND_NOW:
1067c478bd9Sstevel@tonic-gate 		case DT_INIT_ARRAYSZ:
1077c478bd9Sstevel@tonic-gate 		case DT_FINI_ARRAYSZ:
1087c478bd9Sstevel@tonic-gate 		case DT_RUNPATH:
1097c478bd9Sstevel@tonic-gate 		case DT_FLAGS:
1107c478bd9Sstevel@tonic-gate 		case DT_CONFIG:
1117c478bd9Sstevel@tonic-gate 		case DT_DEPAUDIT:
1127c478bd9Sstevel@tonic-gate 		case DT_AUDIT:
1139039eeafSab 		case DT_SUNW_SYMSZ:
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 		case DT_PLTGOT:
1167c478bd9Sstevel@tonic-gate 		case DT_HASH:
1177c478bd9Sstevel@tonic-gate 		case DT_STRTAB:
1187c478bd9Sstevel@tonic-gate 		case DT_SYMTAB:
1199039eeafSab 		case DT_SUNW_SYMTAB:
1207c478bd9Sstevel@tonic-gate 		case DT_INIT:
1217c478bd9Sstevel@tonic-gate 		case DT_FINI:
1227c478bd9Sstevel@tonic-gate 		case DT_VERSYM:
1237c478bd9Sstevel@tonic-gate 		case DT_VERDEF:
1247c478bd9Sstevel@tonic-gate 		case DT_VERNEED:
1257c478bd9Sstevel@tonic-gate 		case DT_INIT_ARRAY:
1267c478bd9Sstevel@tonic-gate 		case DT_FINI_ARRAY:
1277c478bd9Sstevel@tonic-gate 			dyn->d_un.d_ptr += addr;
1287c478bd9Sstevel@tonic-gate 			break;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		/*
1317c478bd9Sstevel@tonic-gate 		 * If the memory image is being used, this element would have
1327c478bd9Sstevel@tonic-gate 		 * been initialized to the runtime linkers internal link-map
1337c478bd9Sstevel@tonic-gate 		 * list.  Clear it.
1347c478bd9Sstevel@tonic-gate 		 */
1357c478bd9Sstevel@tonic-gate 		case DT_DEBUG:
1367c478bd9Sstevel@tonic-gate 			dyn->d_un.d_val = 0;
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		/*
1407c478bd9Sstevel@tonic-gate 		 * The number of relocations may have been reduced if
1417c478bd9Sstevel@tonic-gate 		 * relocations have been saved in the new image.  Thus we
1427c478bd9Sstevel@tonic-gate 		 * compute the new relocation size and start.
1437c478bd9Sstevel@tonic-gate 		 */
1447c478bd9Sstevel@tonic-gate 		case DT_RELASZ:
1457c478bd9Sstevel@tonic-gate 		case DT_RELSZ:
1467c478bd9Sstevel@tonic-gate 			dyn->d_un.d_val = ((data + func) * entsize);
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		case DT_RELA:
1507c478bd9Sstevel@tonic-gate 		case DT_REL:
1517c478bd9Sstevel@tonic-gate 			dyn->d_un.d_ptr = (addr + off + (null * entsize));
1527c478bd9Sstevel@tonic-gate 			break;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		/*
1557c478bd9Sstevel@tonic-gate 		 * If relative relocations have been processed clear the count.
1567c478bd9Sstevel@tonic-gate 		 */
1577c478bd9Sstevel@tonic-gate 		case DT_RELACOUNT:
1587c478bd9Sstevel@tonic-gate 		case DT_RELCOUNT:
1597c478bd9Sstevel@tonic-gate 			if (flags & RTLD_REL_RELATIVE)
1607c478bd9Sstevel@tonic-gate 				dyn->d_un.d_val = 0;
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		case DT_PLTRELSZ:
1647c478bd9Sstevel@tonic-gate 			dyn->d_un.d_val = (func * entsize);
1657c478bd9Sstevel@tonic-gate 			break;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		case DT_JMPREL:
1687c478bd9Sstevel@tonic-gate 			dyn->d_un.d_ptr = (addr + off +
169cce0e03bSab 			    ((null + data) * entsize));
1707c478bd9Sstevel@tonic-gate 			break;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		/*
1737c478bd9Sstevel@tonic-gate 		 * Recompute the images elf checksum.
1747c478bd9Sstevel@tonic-gate 		 */
1757c478bd9Sstevel@tonic-gate 		case DT_CHECKSUM:
1767c478bd9Sstevel@tonic-gate 			dyn->d_un.d_val = checksum;
1777c478bd9Sstevel@tonic-gate 			break;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		/*
1807c478bd9Sstevel@tonic-gate 		 * If a flag entry is available, indicate if this image has
1817c478bd9Sstevel@tonic-gate 		 * been generated via the configuration process (crle(1)).
1827c478bd9Sstevel@tonic-gate 		 * Because we only started depositing DT_FLAGS_1 entries in all
1837c478bd9Sstevel@tonic-gate 		 * objects starting with Solaris 8, set a feature flag if it
1847c478bd9Sstevel@tonic-gate 		 * is present (these got added in Solaris 7).
1857c478bd9Sstevel@tonic-gate 		 * The runtime linker may use this flag to search for a local
1867c478bd9Sstevel@tonic-gate 		 * configuration file - this is only meaningful in executables
1877c478bd9Sstevel@tonic-gate 		 * but the flag has value for identifying images regardless.
1887c478bd9Sstevel@tonic-gate 		 *
1897c478bd9Sstevel@tonic-gate 		 * If this file is acting as a filter, and dependency
1907c478bd9Sstevel@tonic-gate 		 * relocations have been processed (a filter is thought of as a
1917c478bd9Sstevel@tonic-gate 		 * dependency in terms of symbol binding), we may have bound to
1927c478bd9Sstevel@tonic-gate 		 * the filtee, and hence carried out the relocation.  Indicate
1937c478bd9Sstevel@tonic-gate 		 * that the filtee must be preloaded, as the .plt won't get
1947c478bd9Sstevel@tonic-gate 		 * exercised to cause its normal loading.
1957c478bd9Sstevel@tonic-gate 		 */
1967c478bd9Sstevel@tonic-gate 		case DT_FLAGS_1:
1977c478bd9Sstevel@tonic-gate 			if (flags & RTLD_CONFSET)
1987c478bd9Sstevel@tonic-gate 				dyn->d_un.d_val |= DF_1_CONFALT;
1997c478bd9Sstevel@tonic-gate 			if ((flags & RTLD_REL_DEPENDS) &&
2007c478bd9Sstevel@tonic-gate 			    (FLAGS1(lmp)) & MSK_RT_FILTER)
2017c478bd9Sstevel@tonic-gate 				dyn->d_un.d_val |= DF_1_LOADFLTR;
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		case DT_FEATURE_1:
2057c478bd9Sstevel@tonic-gate 			if (flags & RTLD_CONFSET)
2067c478bd9Sstevel@tonic-gate 				dyn->d_un.d_val |= DTF_1_CONFEXP;
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		/*
2107c478bd9Sstevel@tonic-gate 		 * If a position flag is available save it for possible update
2117c478bd9Sstevel@tonic-gate 		 * when processing the next NEEDED tag.
2127c478bd9Sstevel@tonic-gate 		 */
2137c478bd9Sstevel@tonic-gate 		case DT_POSFLAG_1:
2147c478bd9Sstevel@tonic-gate 			if (flags & RTLD_REL_DEPENDS) {
2157c478bd9Sstevel@tonic-gate 				posdyn = dyn++;
2167c478bd9Sstevel@tonic-gate 				continue;
2177c478bd9Sstevel@tonic-gate 			}
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		/*
2217c478bd9Sstevel@tonic-gate 		 * Collect the defaults.
2227c478bd9Sstevel@tonic-gate 		 */
2237c478bd9Sstevel@tonic-gate 		default:
2247c478bd9Sstevel@tonic-gate 			/*
2257c478bd9Sstevel@tonic-gate 			 * If d_val is used, don't touch.
2267c478bd9Sstevel@tonic-gate 			 */
2277c478bd9Sstevel@tonic-gate 			if ((dyn->d_tag >= DT_VALRNGLO) &&
2287c478bd9Sstevel@tonic-gate 			    (dyn->d_tag <= DT_VALRNGHI))
2297c478bd9Sstevel@tonic-gate 				break;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 			/*
2327c478bd9Sstevel@tonic-gate 			 * If d_ptr is used, adjust.  Note, some entries that
2337c478bd9Sstevel@tonic-gate 			 * fell into this range are offsets into the dynamic
2347c478bd9Sstevel@tonic-gate 			 * string table.  Although these would need modifying
2357c478bd9Sstevel@tonic-gate 			 * if the section itself were resized, there is no
2367c478bd9Sstevel@tonic-gate 			 * resizing with dldump().  Entries that correspond to
2377c478bd9Sstevel@tonic-gate 			 * offsets are picked off in the initial DT_ loop
2387c478bd9Sstevel@tonic-gate 			 * above.
2397c478bd9Sstevel@tonic-gate 			 */
2407c478bd9Sstevel@tonic-gate 			if ((dyn->d_tag >= DT_ADDRRNGLO) &&
2417c478bd9Sstevel@tonic-gate 			    (dyn->d_tag <= DT_ADDRRNGHI)) {
2427c478bd9Sstevel@tonic-gate 				dyn->d_un.d_ptr += addr;
2437c478bd9Sstevel@tonic-gate 				break;
2447c478bd9Sstevel@tonic-gate 			}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 			/*
2477c478bd9Sstevel@tonic-gate 			 * Check to see if this DT_ entry conforms
2487c478bd9Sstevel@tonic-gate 			 * to the DT_ENCODING rules.
2497c478bd9Sstevel@tonic-gate 			 */
2507c478bd9Sstevel@tonic-gate 			if ((dyn->d_tag >= DT_ENCODING) &&
2517c478bd9Sstevel@tonic-gate 			    (dyn->d_tag <= DT_HIOS)) {
2527c478bd9Sstevel@tonic-gate 				/*
2537c478bd9Sstevel@tonic-gate 				 * Even tag values are ADDRESS encodings
2547c478bd9Sstevel@tonic-gate 				 */
2557c478bd9Sstevel@tonic-gate 				if ((dyn->d_tag % 2) == 0) {
2567c478bd9Sstevel@tonic-gate 					dyn->d_un.d_ptr += addr;
2577c478bd9Sstevel@tonic-gate 				}
2587c478bd9Sstevel@tonic-gate 				break;
2597c478bd9Sstevel@tonic-gate 			}
2605aefb655Srie 			eprintf(LIST(lmp), ERR_WARNING,
2615aefb655Srie 			    MSG_INTL(MSG_DT_UNKNOWN), file,
2627c478bd9Sstevel@tonic-gate 			    EC_XWORD(dyn->d_tag));
2637c478bd9Sstevel@tonic-gate 			return (1);
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 		posdyn = 0;
2667c478bd9Sstevel@tonic-gate 		dyn++;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 	return (0);
2697c478bd9Sstevel@tonic-gate }
270