xref: /titanic_50/usr/src/cmd/sgs/rtld/sparcv9/_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  * SPARCV9-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)597c478bd9Sstevel@tonic-gate _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 };
687c478bd9Sstevel@tonic-gate 	Dyn		*dyn_ptr;
6956deab07SRod Evans 	Phdr		*phdr = NULL;
707c478bd9Sstevel@tonic-gate 	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_LDSO_BASE:
837c478bd9Sstevel@tonic-gate 			ld_base = (unsigned long)ebp->eb_un.eb_val;
847c478bd9Sstevel@tonic-gate 			break;
857c478bd9Sstevel@tonic-gate 		case EB_ARGV:
867c478bd9Sstevel@tonic-gate 			_argv = (char **)ebp->eb_un.eb_ptr;
877c478bd9Sstevel@tonic-gate 			break;
887c478bd9Sstevel@tonic-gate 		case EB_ENVP:
897c478bd9Sstevel@tonic-gate 			_envp = (char **)ebp->eb_un.eb_ptr;
907c478bd9Sstevel@tonic-gate 			break;
917c478bd9Sstevel@tonic-gate 		case EB_AUXV:
927c478bd9Sstevel@tonic-gate 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
937c478bd9Sstevel@tonic-gate 			break;
947c478bd9Sstevel@tonic-gate 		case EB_PAGESIZE:
957c478bd9Sstevel@tonic-gate 			_syspagsz = (int)ebp->eb_un.eb_val;
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * Search the aux. vector for the information passed by exec.
1017c478bd9Sstevel@tonic-gate 	 */
1027c478bd9Sstevel@tonic-gate 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
1037c478bd9Sstevel@tonic-gate 		switch (auxv->a_type) {
1047c478bd9Sstevel@tonic-gate 		case AT_EXECFD:
1057c478bd9Sstevel@tonic-gate 			/* this is the old exec that passes a file descriptor */
1067c478bd9Sstevel@tonic-gate 			fd = (int)auxv->a_un.a_val;
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 		case AT_FLAGS:
1097c478bd9Sstevel@tonic-gate 			/* processor flags (MAU available, etc) */
11056deab07SRod Evans 			_flags = auxv->a_un.a_val;
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 		case AT_PAGESZ:
1137c478bd9Sstevel@tonic-gate 			/* system page size */
1147c478bd9Sstevel@tonic-gate 			_syspagsz = (int)auxv->a_un.a_val;
1157c478bd9Sstevel@tonic-gate 			break;
1167c478bd9Sstevel@tonic-gate 		case AT_PHDR:
1177c478bd9Sstevel@tonic-gate 			/* address of the segment table */
1187c478bd9Sstevel@tonic-gate 			phdr = (Phdr *)auxv->a_un.a_ptr;
1197c478bd9Sstevel@tonic-gate 			break;
1207c478bd9Sstevel@tonic-gate 		case AT_BASE:
1217c478bd9Sstevel@tonic-gate 			/* interpreter base address */
1227c478bd9Sstevel@tonic-gate 			if (ld_base == 0)
1237c478bd9Sstevel@tonic-gate 				ld_base = auxv->a_un.a_val;
1247c478bd9Sstevel@tonic-gate 			interp_base = auxv->a_un.a_val;
1257c478bd9Sstevel@tonic-gate 			break;
1267c478bd9Sstevel@tonic-gate 		case AT_SUN_UID:
1277c478bd9Sstevel@tonic-gate 			/* effective user id for the executable */
1287c478bd9Sstevel@tonic-gate 			euid = (uid_t)auxv->a_un.a_val;
1297c478bd9Sstevel@tonic-gate 			break;
1307c478bd9Sstevel@tonic-gate 		case AT_SUN_RUID:
1317c478bd9Sstevel@tonic-gate 			/* real user id for the executable */
1327c478bd9Sstevel@tonic-gate 			uid = (uid_t)auxv->a_un.a_val;
1337c478bd9Sstevel@tonic-gate 			break;
1347c478bd9Sstevel@tonic-gate 		case AT_SUN_GID:
1357c478bd9Sstevel@tonic-gate 			/* effective group id for the executable */
1367c478bd9Sstevel@tonic-gate 			egid = (gid_t)auxv->a_un.a_val;
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 		case AT_SUN_RGID:
1397c478bd9Sstevel@tonic-gate 			/* real group id for the executable */
1407c478bd9Sstevel@tonic-gate 			gid = (gid_t)auxv->a_un.a_val;
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 		case AT_SUN_PLATFORM:
1437c478bd9Sstevel@tonic-gate 			/* platform name */
1447c478bd9Sstevel@tonic-gate 			_platform = auxv->a_un.a_ptr;
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		case AT_SUN_EXECNAME:
1477c478bd9Sstevel@tonic-gate 			/* full pathname of execed object */
1487c478bd9Sstevel@tonic-gate 			_execname = auxv->a_un.a_ptr;
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 		case AT_SUN_AUXFLAGS:
15156deab07SRod Evans 			/* auxiliary flags */
1527c478bd9Sstevel@tonic-gate 			auxflags = (int)auxv->a_un.a_val;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case AT_SUN_HWCAP:
15556deab07SRod Evans 			/* hardware capabilities */
156*ebb8ac07SRobert Mustacchi 			hwcap[0] = (uint_t)auxv->a_un.a_val;
157*ebb8ac07SRobert Mustacchi 			break;
158*ebb8ac07SRobert Mustacchi 		case AT_SUN_HWCAP2:
159*ebb8ac07SRobert Mustacchi 			/* hardware capabilities */
160*ebb8ac07SRobert Mustacchi 			hwcap[1] = (uint_t)auxv->a_un.a_val;
1617c478bd9Sstevel@tonic-gate 			break;
16207678296Ssl108498 		case AT_SUN_EMULATOR:
16307678296Ssl108498 			/* name of emulation library, if any */
16407678296Ssl108498 			_emulator = auxv->a_un.a_ptr;
16507678296Ssl108498 			break;
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Get needed info from ld.so's dynamic structure.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	/* LINTED */
1737c478bd9Sstevel@tonic-gate 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
1747c478bd9Sstevel@tonic-gate 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
1757c478bd9Sstevel@tonic-gate 		switch (ld_dyn->d_tag) {
1767c478bd9Sstevel@tonic-gate 		case DT_RELA:
1777c478bd9Sstevel@tonic-gate 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 		case DT_RELACOUNT:
1807c478bd9Sstevel@tonic-gate 			relacount = ld_dyn->d_un.d_val;
1817c478bd9Sstevel@tonic-gate 			break;
1827c478bd9Sstevel@tonic-gate 		case DT_RELAENT:
1837c478bd9Sstevel@tonic-gate 			relaent = ld_dyn->d_un.d_val;
1847c478bd9Sstevel@tonic-gate 			break;
1857c478bd9Sstevel@tonic-gate 		case DT_STRTAB:
1867c478bd9Sstevel@tonic-gate 			strtab = ld_dyn->d_un.d_ptr + ld_base;
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		case DT_SONAME:
1897c478bd9Sstevel@tonic-gate 			soname = ld_dyn->d_un.d_val;
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	_rt_name = (char *)strtab + soname;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/*
19656deab07SRod Evans 	 * If we don't have a RELAENT, just assume the size.
1977c478bd9Sstevel@tonic-gate 	 */
1987c478bd9Sstevel@tonic-gate 	if (relaent == 0)
1997c478bd9Sstevel@tonic-gate 		relaent = sizeof (Rela);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/*
20256deab07SRod Evans 	 * As all global symbol references within ld.so.1 are protected
20356deab07SRod Evans 	 * (symbolic), only RELATIVE and JMPSLOT relocations should be left
20456deab07SRod Evans 	 * to process at runtime.  Process all relative relocations now.
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	for (; relacount; relacount--) {
2077c478bd9Sstevel@tonic-gate 		ulong_t	roffset;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
2107c478bd9Sstevel@tonic-gate 		*((ulong_t *)roffset) = ld_base +
2117c478bd9Sstevel@tonic-gate 		    ((Rela *)reladdr)->r_addend;
2127c478bd9Sstevel@tonic-gate 		reladdr += relaent;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
21607678296Ssl108498 	 * If an emulation library is being used, use that as the linker's
21707678296Ssl108498 	 * effective executable name. The real executable is not linked by this
21807678296Ssl108498 	 * linker.
21907678296Ssl108498 	 */
22007678296Ssl108498 	if (_emulator != NULL) {
22107678296Ssl108498 		_execname = _emulator;
22207678296Ssl108498 		rtld_flags2 |= RT_FL2_BRANDED;
22307678296Ssl108498 	}
22407678296Ssl108498 
22507678296Ssl108498 	/*
2267c478bd9Sstevel@tonic-gate 	 * Continue with generic startup processing.
2277c478bd9Sstevel@tonic-gate 	 */
22841072f3cSrie 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
22956deab07SRod Evans 	    _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
23056deab07SRod Evans 	    _execname, _argv, uid, euid, gid, egid, NULL, auxflags,
231*ebb8ac07SRobert Mustacchi 	    hwcap)) == NULL) {
2327c478bd9Sstevel@tonic-gate 		rtldexit(&lml_main, 1);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	return (LM_ENTRY_PT(lmp)());
2367c478bd9Sstevel@tonic-gate }
237