xref: /titanic_44/usr/src/cmd/sgs/rtld/sparc/_setup.c (revision ebb8ac078e9265f87093fbb363e8c2cbc6ee13e6)
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 /*
2356deab07SRod Evans  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267257d1b4Sraf 
277257d1b4Sraf /*
287257d1b4Sraf  *	Copyright (c) 1988 AT&T
297257d1b4Sraf  *	  All Rights Reserved
307257d1b4Sraf  */
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 #ifdef A_OUT
577c478bd9Sstevel@tonic-gate #include	"_a.out.h"
587c478bd9Sstevel@tonic-gate #endif /* A_OUT */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* VARARGS */
617c478bd9Sstevel@tonic-gate unsigned long
_setup(Boot * ebp,Dyn * ld_dyn)627c478bd9Sstevel@tonic-gate _setup(Boot *ebp, Dyn *ld_dyn)
637c478bd9Sstevel@tonic-gate {
6456deab07SRod Evans 	ulong_t		reladdr, relacount, ld_base = 0;
6556deab07SRod Evans 	ulong_t		relaent = 0;
6656deab07SRod Evans 	ulong_t		strtab, soname, interp_base = 0;
677c478bd9Sstevel@tonic-gate 	char		*_rt_name, **_envp, **_argv;
6856deab07SRod Evans 	int		_syspagsz = 0, fd = -1;
69*ebb8ac07SRobert Mustacchi 	uint_t		_flags = 0;
70*ebb8ac07SRobert Mustacchi 	uint_t		hwcap[2] = { 0, 0 };
717c478bd9Sstevel@tonic-gate 	Dyn		*dyn_ptr;
7256deab07SRod Evans 	Phdr		*phdr = NULL;
737c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
747c478bd9Sstevel@tonic-gate 	auxv_t		*auxv, *_auxv;
75f48205beScasper 	uid_t		uid = (uid_t)-1, euid = (uid_t)-1;
76f48205beScasper 	gid_t		gid = (gid_t)-1, egid = (gid_t)-1;
7756deab07SRod Evans 	char		*_platform = NULL, *_execname = NULL, *_emulator = NULL;
787c478bd9Sstevel@tonic-gate 	int		auxflags = -1;
797c478bd9Sstevel@tonic-gate #ifdef	A_OUT
8056deab07SRod Evans 	void		*aoutdyn = NULL;
817c478bd9Sstevel@tonic-gate #endif	/* A_OUT */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	/*
847c478bd9Sstevel@tonic-gate 	 * Scan the bootstrap structure to pick up the basics.
857c478bd9Sstevel@tonic-gate 	 */
867c478bd9Sstevel@tonic-gate 	for (; ebp->eb_tag != EB_NULL; ebp++)
877c478bd9Sstevel@tonic-gate 		switch (ebp->eb_tag) {
887c478bd9Sstevel@tonic-gate 		case EB_DYNAMIC:
897c478bd9Sstevel@tonic-gate #ifdef A_OUT
9056deab07SRod Evans 			aoutdyn = (Link_dynamic *)ebp->eb_un.eb_val;
917c478bd9Sstevel@tonic-gate #endif /* A_OUT */
927c478bd9Sstevel@tonic-gate 			break;
937c478bd9Sstevel@tonic-gate 		case EB_LDSO_BASE:
947c478bd9Sstevel@tonic-gate 			ld_base = (unsigned long)ebp->eb_un.eb_val;
957c478bd9Sstevel@tonic-gate 			break;
967c478bd9Sstevel@tonic-gate 		case EB_ARGV:
977c478bd9Sstevel@tonic-gate 			_argv = (char **)ebp->eb_un.eb_ptr;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		case EB_ENVP:
1007c478bd9Sstevel@tonic-gate 			_envp = (char **)ebp->eb_un.eb_ptr;
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 		case EB_AUXV:
1037c478bd9Sstevel@tonic-gate 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 		case EB_PAGESIZE:
1067c478bd9Sstevel@tonic-gate 			_syspagsz = (int)ebp->eb_un.eb_val;
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * Search the aux. vector for the information passed by exec.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
1147c478bd9Sstevel@tonic-gate 		switch (auxv->a_type) {
1157c478bd9Sstevel@tonic-gate 		case AT_EXECFD:
1167c478bd9Sstevel@tonic-gate 			/* this is the old exec that passes a file descriptor */
1177c478bd9Sstevel@tonic-gate 			fd = (int)auxv->a_un.a_val;
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 		case AT_FLAGS:
1207c478bd9Sstevel@tonic-gate 			/* processor flags (MAU available, etc) */
1217c478bd9Sstevel@tonic-gate 			_flags = auxv->a_un.a_val;
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 		case AT_PAGESZ:
1247c478bd9Sstevel@tonic-gate 			/* system page size */
1257c478bd9Sstevel@tonic-gate 			_syspagsz = (int)auxv->a_un.a_val;
1267c478bd9Sstevel@tonic-gate 			break;
1277c478bd9Sstevel@tonic-gate 		case AT_PHDR:
1287c478bd9Sstevel@tonic-gate 			/* address of the segment table */
1297c478bd9Sstevel@tonic-gate 			phdr = (Phdr *)auxv->a_un.a_ptr;
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 		case AT_BASE:
1327c478bd9Sstevel@tonic-gate 			/* interpreter base address */
1337c478bd9Sstevel@tonic-gate 			if (ld_base == 0)
1347c478bd9Sstevel@tonic-gate 				ld_base = auxv->a_un.a_val;
1357c478bd9Sstevel@tonic-gate 			interp_base = auxv->a_un.a_val;
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 		case AT_SUN_UID:
1387c478bd9Sstevel@tonic-gate 			/* effective user id for the executable */
1397c478bd9Sstevel@tonic-gate 			euid = (uid_t)auxv->a_un.a_val;
1407c478bd9Sstevel@tonic-gate 			break;
1417c478bd9Sstevel@tonic-gate 		case AT_SUN_RUID:
1427c478bd9Sstevel@tonic-gate 			/* real user id for the executable */
1437c478bd9Sstevel@tonic-gate 			uid = (uid_t)auxv->a_un.a_val;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case AT_SUN_GID:
1467c478bd9Sstevel@tonic-gate 			/* effective group id for the executable */
1477c478bd9Sstevel@tonic-gate 			egid = (gid_t)auxv->a_un.a_val;
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 		case AT_SUN_RGID:
1507c478bd9Sstevel@tonic-gate 			/* real group id for the executable */
1517c478bd9Sstevel@tonic-gate 			gid = (gid_t)auxv->a_un.a_val;
1527c478bd9Sstevel@tonic-gate 			break;
1537c478bd9Sstevel@tonic-gate 		case AT_SUN_PLATFORM:
1547c478bd9Sstevel@tonic-gate 			/* platform name */
1557c478bd9Sstevel@tonic-gate 			_platform = auxv->a_un.a_ptr;
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 		case AT_SUN_EXECNAME:
1587c478bd9Sstevel@tonic-gate 			/* full pathname of execed object */
1597c478bd9Sstevel@tonic-gate 			_execname = auxv->a_un.a_ptr;
1607c478bd9Sstevel@tonic-gate 			break;
1617c478bd9Sstevel@tonic-gate 		case AT_SUN_AUXFLAGS:
16256deab07SRod Evans 			/* auxiliary flags */
1637c478bd9Sstevel@tonic-gate 			auxflags = (int)auxv->a_un.a_val;
1647c478bd9Sstevel@tonic-gate 			break;
1657c478bd9Sstevel@tonic-gate 		case AT_SUN_HWCAP:
16656deab07SRod Evans 			/* hardware capabilities */
167*ebb8ac07SRobert Mustacchi 			hwcap[0] = (uint_t)auxv->a_un.a_val;
168*ebb8ac07SRobert Mustacchi 			break;
169*ebb8ac07SRobert Mustacchi 		case AT_SUN_HWCAP2:
170*ebb8ac07SRobert Mustacchi 			/* hardware capabilities */
171*ebb8ac07SRobert Mustacchi 			hwcap[1] = (uint_t)auxv->a_un.a_val;
1727c478bd9Sstevel@tonic-gate 			break;
17307678296Ssl108498 		case AT_SUN_EMULATOR:
17407678296Ssl108498 			/* name of emulation library, if any */
17507678296Ssl108498 			_emulator = auxv->a_un.a_ptr;
17607678296Ssl108498 			break;
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * Get needed info from ld.so's dynamic structure.
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 	/* LINTED */
1847c478bd9Sstevel@tonic-gate 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
1857c478bd9Sstevel@tonic-gate 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
1867c478bd9Sstevel@tonic-gate 		switch (ld_dyn->d_tag) {
1877c478bd9Sstevel@tonic-gate 		case DT_RELA:
1887c478bd9Sstevel@tonic-gate 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 		case DT_RELACOUNT:
1917c478bd9Sstevel@tonic-gate 			relacount = ld_dyn->d_un.d_val;
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 		case DT_RELAENT:
1947c478bd9Sstevel@tonic-gate 			relaent = ld_dyn->d_un.d_val;
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 		case DT_STRTAB:
1977c478bd9Sstevel@tonic-gate 			strtab = ld_dyn->d_un.d_ptr + ld_base;
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 		case DT_SONAME:
2007c478bd9Sstevel@tonic-gate 			soname = ld_dyn->d_un.d_val;
2017c478bd9Sstevel@tonic-gate 			break;
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 	_rt_name = (char *)strtab + soname;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
20756deab07SRod Evans 	 * If we don't have a RELAENT, just assume the size.
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	if (relaent == 0)
2107c478bd9Sstevel@tonic-gate 		relaent = sizeof (Rela);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
21356deab07SRod Evans 	 * As all global symbol references within ld.so.1 are protected
21456deab07SRod Evans 	 * (symbolic), only RELATIVE and JMPSLOT relocations should be left
21556deab07SRod Evans 	 * to process at runtime.  Process all relative relocations now.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	for (; relacount; relacount--) {
2187c478bd9Sstevel@tonic-gate 		ulong_t	roffset;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
2217c478bd9Sstevel@tonic-gate 		*((ulong_t *)roffset) = ld_base +
22256deab07SRod Evans 		    ((Rela *)reladdr)->r_addend;
2237c478bd9Sstevel@tonic-gate 		reladdr += relaent;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
22707678296Ssl108498 	 * If an emulation library is being used, use that as the linker's
22807678296Ssl108498 	 * effective executable name. The real executable is not linked by this
22907678296Ssl108498 	 * linker.
23007678296Ssl108498 	 */
23107678296Ssl108498 	if (_emulator != NULL) {
23207678296Ssl108498 		_execname = _emulator;
23307678296Ssl108498 		rtld_flags2 |= RT_FL2_BRANDED;
23407678296Ssl108498 	}
23507678296Ssl108498 
23607678296Ssl108498 	/*
2377c478bd9Sstevel@tonic-gate 	 * Continue with generic startup processing.
2387c478bd9Sstevel@tonic-gate 	 */
23956deab07SRod Evans 	/* BEGIN CSTYLED */
24041072f3cSrie 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
24156deab07SRod Evans 	    _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
24256deab07SRod Evans 	    _execname, _argv, uid, euid, gid, egid,
2437c478bd9Sstevel@tonic-gate #ifdef	A_OUT
244*ebb8ac07SRobert Mustacchi 	    aoutdyn, auxflags, hwcap)) == NULL) {
2457c478bd9Sstevel@tonic-gate #else
2467257d1b4Sraf 	    /* CSTYLED */
247*ebb8ac07SRobert Mustacchi 	    NULL, auxflags, hwcap)) == NULL) {
2487c478bd9Sstevel@tonic-gate #endif	/* A_OUT */
2497c478bd9Sstevel@tonic-gate 		rtldexit(&lml_main, 1);
2507c478bd9Sstevel@tonic-gate 	}
25156deab07SRod Evans 	/* END CSTYLED */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	return (LM_ENTRY_PT(lmp)());
2547c478bd9Sstevel@tonic-gate }
255