xref: /illumos-gate/usr/src/cmd/sgs/rtld/sparc/_setup.c (revision fec04708)
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 
227257d1b4Sraf /*
2356deab07SRod Evans  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247257d1b4Sraf  * Use is subject to license terms.
257257d1b4Sraf  */
267257d1b4Sraf 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
297c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
307c478bd9Sstevel@tonic-gate  */
31*ebb8ac07SRobert Mustacchi /*
32*ebb8ac07SRobert Mustacchi  * Copyright (c) 2012, Joyent, Inc.  All rights reserved.
33*ebb8ac07SRobert Mustacchi  */
347257d1b4Sraf 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * SPARC specific setup routine  -  relocate ld.so's symbols, setup its
377c478bd9Sstevel@tonic-gate  * environment, map in loadable sections of the executable.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * Takes base address ld.so was loaded at, address of ld.so's dynamic
407c478bd9Sstevel@tonic-gate  * structure, address of process environment pointers, address of auxiliary
417c478bd9Sstevel@tonic-gate  * vector and * argv[0] (process name).
427c478bd9Sstevel@tonic-gate  * If errors occur, send process signal - otherwise
437c478bd9Sstevel@tonic-gate  * return executable's entry point to the bootstrap routine.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include	<signal.h>
477c478bd9Sstevel@tonic-gate #include	<stdlib.h>
487c478bd9Sstevel@tonic-gate #include	<sys/auxv.h>
497c478bd9Sstevel@tonic-gate #include	<sys/types.h>
507c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
517c478bd9Sstevel@tonic-gate #include	<link.h>
527c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
537c478bd9Sstevel@tonic-gate #include	"_rtld.h"
547c478bd9Sstevel@tonic-gate #include	"_audit.h"
557c478bd9Sstevel@tonic-gate #include	"msg.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* VARARGS */
587c478bd9Sstevel@tonic-gate unsigned long
_setup(Boot * ebp,Dyn * ld_dyn)5956deab07SRod Evans _setup(Boot *ebp, Dyn *ld_dyn)
607c478bd9Sstevel@tonic-gate {
6156deab07SRod Evans 	ulong_t		reladdr, relacount, ld_base = 0;
6256deab07SRod Evans 	ulong_t		relaent = 0;
6356deab07SRod Evans 	ulong_t		strtab, soname, interp_base = 0;
647c478bd9Sstevel@tonic-gate 	char		*_rt_name, **_envp, **_argv;
6556deab07SRod Evans 	int		_syspagsz = 0, fd = -1;
66*ebb8ac07SRobert Mustacchi 	uint_t		_flags = 0;
67*ebb8ac07SRobert Mustacchi 	uint_t		hwcap[2] = { 0, 0 };
6856deab07SRod Evans 	Dyn		*dyn_ptr;
6956deab07SRod Evans 	Phdr		*phdr = NULL;
7056deab07SRod Evans 	Rt_map		*lmp;
717c478bd9Sstevel@tonic-gate 	auxv_t		*auxv, *_auxv;
72f48205beScasper 	uid_t		uid = (uid_t)-1, euid = (uid_t)-1;
73f48205beScasper 	gid_t		gid = (gid_t)-1, egid = (gid_t)-1;
7456deab07SRod Evans 	char		*_platform = NULL, *_execname = NULL, *_emulator = NULL;
757c478bd9Sstevel@tonic-gate 	int		auxflags = -1;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	/*
787c478bd9Sstevel@tonic-gate 	 * Scan the bootstrap structure to pick up the basics.
797c478bd9Sstevel@tonic-gate 	 */
807c478bd9Sstevel@tonic-gate 	for (; ebp->eb_tag != EB_NULL; ebp++)
817c478bd9Sstevel@tonic-gate 		switch (ebp->eb_tag) {
827c478bd9Sstevel@tonic-gate 		case EB_DYNAMIC:
837c478bd9Sstevel@tonic-gate 			break;
847c478bd9Sstevel@tonic-gate 		case EB_LDSO_BASE:
857c478bd9Sstevel@tonic-gate 			ld_base = (unsigned long)ebp->eb_un.eb_val;
867c478bd9Sstevel@tonic-gate 			break;
877c478bd9Sstevel@tonic-gate 		case EB_ARGV:
887c478bd9Sstevel@tonic-gate 			_argv = (char **)ebp->eb_un.eb_ptr;
897c478bd9Sstevel@tonic-gate 			break;
907c478bd9Sstevel@tonic-gate 		case EB_ENVP:
917c478bd9Sstevel@tonic-gate 			_envp = (char **)ebp->eb_un.eb_ptr;
927c478bd9Sstevel@tonic-gate 			break;
937c478bd9Sstevel@tonic-gate 		case EB_AUXV:
947c478bd9Sstevel@tonic-gate 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
957c478bd9Sstevel@tonic-gate 			break;
967c478bd9Sstevel@tonic-gate 		case EB_PAGESIZE:
977c478bd9Sstevel@tonic-gate 			_syspagsz = (int)ebp->eb_un.eb_val;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * Search the aux. vector for the information passed by exec.
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
1057c478bd9Sstevel@tonic-gate 		switch (auxv->a_type) {
1067c478bd9Sstevel@tonic-gate 		case AT_EXECFD:
1077c478bd9Sstevel@tonic-gate 			/* this is the old exec that passes a file descriptor */
1087c478bd9Sstevel@tonic-gate 			fd = (int)auxv->a_un.a_val;
1097c478bd9Sstevel@tonic-gate 			break;
1107c478bd9Sstevel@tonic-gate 		case AT_FLAGS:
1117c478bd9Sstevel@tonic-gate 			/* processor flags (MAU available, etc) */
1127c478bd9Sstevel@tonic-gate 			_flags = auxv->a_un.a_val;
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		case AT_PAGESZ:
1157c478bd9Sstevel@tonic-gate 			/* system page size */
1167c478bd9Sstevel@tonic-gate 			_syspagsz = (int)auxv->a_un.a_val;
1177c478bd9Sstevel@tonic-gate 			break;
1187c478bd9Sstevel@tonic-gate 		case AT_PHDR:
1197c478bd9Sstevel@tonic-gate 			/* address of the segment table */
1207c478bd9Sstevel@tonic-gate 			phdr = (Phdr *)auxv->a_un.a_ptr;
1217c478bd9Sstevel@tonic-gate 			break;
1227c478bd9Sstevel@tonic-gate 		case AT_BASE:
1237c478bd9Sstevel@tonic-gate 			/* interpreter base address */
1247c478bd9Sstevel@tonic-gate 			if (ld_base == 0)
1257c478bd9Sstevel@tonic-gate 				ld_base = auxv->a_un.a_val;
1267c478bd9Sstevel@tonic-gate 			interp_base = auxv->a_un.a_val;
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 		case AT_SUN_UID:
1297c478bd9Sstevel@tonic-gate 			/* effective user id for the executable */
1307c478bd9Sstevel@tonic-gate 			euid = (uid_t)auxv->a_un.a_val;
1317c478bd9Sstevel@tonic-gate 			break;
1327c478bd9Sstevel@tonic-gate 		case AT_SUN_RUID:
1337c478bd9Sstevel@tonic-gate 			/* real user id for the executable */
1347c478bd9Sstevel@tonic-gate 			uid = (uid_t)auxv->a_un.a_val;
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 		case AT_SUN_GID:
1377c478bd9Sstevel@tonic-gate 			/* effective group id for the executable */
1387c478bd9Sstevel@tonic-gate 			egid = (gid_t)auxv->a_un.a_val;
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 		case AT_SUN_RGID:
1417c478bd9Sstevel@tonic-gate 			/* real group id for the executable */
1427c478bd9Sstevel@tonic-gate 			gid = (gid_t)auxv->a_un.a_val;
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 		case AT_SUN_PLATFORM:
1457c478bd9Sstevel@tonic-gate 			/* platform name */
1467c478bd9Sstevel@tonic-gate 			_platform = auxv->a_un.a_ptr;
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		case AT_SUN_EXECNAME:
1497c478bd9Sstevel@tonic-gate 			/* full pathname of execed object */
1507c478bd9Sstevel@tonic-gate 			_execname = auxv->a_un.a_ptr;
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		case AT_SUN_AUXFLAGS:
15356deab07SRod Evans 			/* auxiliary flags */
1547c478bd9Sstevel@tonic-gate 			auxflags = (int)auxv->a_un.a_val;
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 		case AT_SUN_HWCAP:
15756deab07SRod Evans 			/* hardware capabilities */
158*ebb8ac07SRobert Mustacchi 			hwcap[0] = (uint_t)auxv->a_un.a_val;
159*ebb8ac07SRobert Mustacchi 			break;
160*ebb8ac07SRobert Mustacchi 		case AT_SUN_HWCAP2:
161*ebb8ac07SRobert Mustacchi 			/* hardware capabilities */
162*ebb8ac07SRobert Mustacchi 			hwcap[1] = (uint_t)auxv->a_un.a_val;
1637c478bd9Sstevel@tonic-gate 			break;
16407678296Ssl 		case AT_SUN_EMULATOR:
16507678296Ssl 			/* name of emulation library, if any */
16607678296Ssl 			_emulator = auxv->a_un.a_ptr;
16707678296Ssl 			break;
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * Get needed info from ld.so's dynamic structure.
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	/* LINTED */
1757c478bd9Sstevel@tonic-gate 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
1767c478bd9Sstevel@tonic-gate 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
1777c478bd9Sstevel@tonic-gate 		switch (ld_dyn->d_tag) {
1787c478bd9Sstevel@tonic-gate 		case DT_RELA:
1797c478bd9Sstevel@tonic-gate 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
1807c478bd9Sstevel@tonic-gate 			break;
1817c478bd9Sstevel@tonic-gate 		case DT_RELACOUNT:
1827c478bd9Sstevel@tonic-gate 			relacount = ld_dyn->d_un.d_val;
1837c478bd9Sstevel@tonic-gate 			break;
1847c478bd9Sstevel@tonic-gate 		case DT_RELAENT:
1857c478bd9Sstevel@tonic-gate 			relaent = ld_dyn->d_un.d_val;
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 		case DT_STRTAB:
1887c478bd9Sstevel@tonic-gate 			strtab = ld_dyn->d_un.d_ptr + ld_base;
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 		case DT_SONAME:
1917c478bd9Sstevel@tonic-gate 			soname = ld_dyn->d_un.d_val;
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	_rt_name = (char *)strtab + soname;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/*
19856deab07SRod Evans 	 * If we don't have a RELAENT, just assume the size.
1997c478bd9Sstevel@tonic-gate 	 */
2007c478bd9Sstevel@tonic-gate 	if (relaent == 0)
2017c478bd9Sstevel@tonic-gate 		relaent = sizeof (Rela);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/*
20456deab07SRod Evans 	 * As all global symbol references within ld.so.1 are protected
20556deab07SRod Evans 	 * (symbolic), only RELATIVE and JMPSLOT relocations should be left
20656deab07SRod Evans 	 * to process at runtime.  Process all relative relocations now.
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	for (; relacount; relacount--) {
2097c478bd9Sstevel@tonic-gate 		ulong_t	roffset;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
2127c478bd9Sstevel@tonic-gate 		*((ulong_t *)roffset) = ld_base +
21356deab07SRod Evans 		    ((Rela *)reladdr)->r_addend;
2147c478bd9Sstevel@tonic-gate 		reladdr += relaent;
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
21707678296Ssl 	/*
21807678296Ssl 	 * If an emulation library is being used, use that as the linker's
21907678296Ssl 	 * effective executable name. The real executable is not linked by this
22007678296Ssl 	 * linker.
22107678296Ssl 	 */
22207678296Ssl 	if (_emulator != NULL) {
22307678296Ssl 		_execname = _emulator;
22407678296Ssl 		rtld_flags2 |= RT_FL2_BRANDED;
22507678296Ssl 	}
22607678296Ssl 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * Continue with generic startup processing.
2297c478bd9Sstevel@tonic-gate 	 */
23056deab07SRod Evans 	/* BEGIN CSTYLED */
23141072f3cSrie 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
23256deab07SRod Evans 	    _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
23356deab07SRod Evans 	    _execname, _argv, uid, euid, gid, egid,
2347257d1b4Sraf 	    /* CSTYLED */
235*ebb8ac07SRobert Mustacchi 	    NULL, auxflags, hwcap)) == NULL) {
2367c478bd9Sstevel@tonic-gate 		rtldexit(&lml_main, 1);
2377c478bd9Sstevel@tonic-gate 	}
23856deab07SRod Evans 	/* END CSTYLED */
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	return (LM_ENTRY_PT(lmp)());
2417c478bd9Sstevel@tonic-gate }
242