xref: /onnv-gate/usr/src/cmd/sgs/rtld/sparc/_setup.c (revision 8598:0867fc633d66)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211618Srie 
220Sstevel@tonic-gate /*
23*8598SRod.Evans@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
246812Sraf  * Use is subject to license terms.
256812Sraf  */
266812Sraf 
276812Sraf /*
280Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
290Sstevel@tonic-gate  *	  All Rights Reserved
300Sstevel@tonic-gate  */
316812Sraf 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * SPARC specific setup routine  -  relocate ld.so's symbols, setup its
340Sstevel@tonic-gate  * environment, map in loadable sections of the executable.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * Takes base address ld.so was loaded at, address of ld.so's dynamic
370Sstevel@tonic-gate  * structure, address of process environment pointers, address of auxiliary
380Sstevel@tonic-gate  * vector and * argv[0] (process name).
390Sstevel@tonic-gate  * If errors occur, send process signal - otherwise
400Sstevel@tonic-gate  * return executable's entry point to the bootstrap routine.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include	<signal.h>
440Sstevel@tonic-gate #include	<stdlib.h>
450Sstevel@tonic-gate #include	<sys/auxv.h>
460Sstevel@tonic-gate #include	<sys/types.h>
470Sstevel@tonic-gate #include	<sys/stat.h>
480Sstevel@tonic-gate #include	<link.h>
490Sstevel@tonic-gate #include	<dlfcn.h>
500Sstevel@tonic-gate #include	"_rtld.h"
510Sstevel@tonic-gate #include	"_audit.h"
520Sstevel@tonic-gate #include	"msg.h"
530Sstevel@tonic-gate #ifdef A_OUT
540Sstevel@tonic-gate #include	"_a.out.h"
550Sstevel@tonic-gate #endif /* A_OUT */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /* VARARGS */
580Sstevel@tonic-gate unsigned long
_setup(Boot * ebp,Dyn * ld_dyn)59*8598SRod.Evans@Sun.COM _setup(Boot *ebp, Dyn *ld_dyn)
600Sstevel@tonic-gate {
61*8598SRod.Evans@Sun.COM 	ulong_t		reladdr, relacount, ld_base = 0;
62*8598SRod.Evans@Sun.COM 	ulong_t		relaent = 0;
63*8598SRod.Evans@Sun.COM 	ulong_t		strtab, soname, interp_base = 0;
640Sstevel@tonic-gate 	char		*_rt_name, **_envp, **_argv;
65*8598SRod.Evans@Sun.COM 	int		_syspagsz = 0, fd = -1;
660Sstevel@tonic-gate 	uint_t		_flags = 0, hwcap_1 = 0;
67*8598SRod.Evans@Sun.COM 	Dyn		*dyn_ptr;
68*8598SRod.Evans@Sun.COM 	Phdr		*phdr = NULL;
69*8598SRod.Evans@Sun.COM 	Rt_map		*lmp;
700Sstevel@tonic-gate 	auxv_t		*auxv, *_auxv;
714321Scasper 	uid_t		uid = (uid_t)-1, euid = (uid_t)-1;
724321Scasper 	gid_t		gid = (gid_t)-1, egid = (gid_t)-1;
73*8598SRod.Evans@Sun.COM 	char		*_platform = NULL, *_execname = NULL, *_emulator = NULL;
740Sstevel@tonic-gate 	int		auxflags = -1;
750Sstevel@tonic-gate #ifdef	A_OUT
76*8598SRod.Evans@Sun.COM 	void		*aoutdyn = NULL;
770Sstevel@tonic-gate #endif	/* A_OUT */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	/*
800Sstevel@tonic-gate 	 * Scan the bootstrap structure to pick up the basics.
810Sstevel@tonic-gate 	 */
820Sstevel@tonic-gate 	for (; ebp->eb_tag != EB_NULL; ebp++)
830Sstevel@tonic-gate 		switch (ebp->eb_tag) {
840Sstevel@tonic-gate 		case EB_DYNAMIC:
850Sstevel@tonic-gate #ifdef A_OUT
86*8598SRod.Evans@Sun.COM 			aoutdyn = (Link_dynamic *)ebp->eb_un.eb_val;
870Sstevel@tonic-gate #endif /* A_OUT */
880Sstevel@tonic-gate 			break;
890Sstevel@tonic-gate 		case EB_LDSO_BASE:
900Sstevel@tonic-gate 			ld_base = (unsigned long)ebp->eb_un.eb_val;
910Sstevel@tonic-gate 			break;
920Sstevel@tonic-gate 		case EB_ARGV:
930Sstevel@tonic-gate 			_argv = (char **)ebp->eb_un.eb_ptr;
940Sstevel@tonic-gate 			break;
950Sstevel@tonic-gate 		case EB_ENVP:
960Sstevel@tonic-gate 			_envp = (char **)ebp->eb_un.eb_ptr;
970Sstevel@tonic-gate 			break;
980Sstevel@tonic-gate 		case EB_AUXV:
990Sstevel@tonic-gate 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
1000Sstevel@tonic-gate 			break;
1010Sstevel@tonic-gate 		case EB_PAGESIZE:
1020Sstevel@tonic-gate 			_syspagsz = (int)ebp->eb_un.eb_val;
1030Sstevel@tonic-gate 			break;
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/*
1070Sstevel@tonic-gate 	 * Search the aux. vector for the information passed by exec.
1080Sstevel@tonic-gate 	 */
1090Sstevel@tonic-gate 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
1100Sstevel@tonic-gate 		switch (auxv->a_type) {
1110Sstevel@tonic-gate 		case AT_EXECFD:
1120Sstevel@tonic-gate 			/* this is the old exec that passes a file descriptor */
1130Sstevel@tonic-gate 			fd = (int)auxv->a_un.a_val;
1140Sstevel@tonic-gate 			break;
1150Sstevel@tonic-gate 		case AT_FLAGS:
1160Sstevel@tonic-gate 			/* processor flags (MAU available, etc) */
1170Sstevel@tonic-gate 			_flags = auxv->a_un.a_val;
1180Sstevel@tonic-gate 			break;
1190Sstevel@tonic-gate 		case AT_PAGESZ:
1200Sstevel@tonic-gate 			/* system page size */
1210Sstevel@tonic-gate 			_syspagsz = (int)auxv->a_un.a_val;
1220Sstevel@tonic-gate 			break;
1230Sstevel@tonic-gate 		case AT_PHDR:
1240Sstevel@tonic-gate 			/* address of the segment table */
1250Sstevel@tonic-gate 			phdr = (Phdr *)auxv->a_un.a_ptr;
1260Sstevel@tonic-gate 			break;
1270Sstevel@tonic-gate 		case AT_BASE:
1280Sstevel@tonic-gate 			/* interpreter base address */
1290Sstevel@tonic-gate 			if (ld_base == 0)
1300Sstevel@tonic-gate 				ld_base = auxv->a_un.a_val;
1310Sstevel@tonic-gate 			interp_base = auxv->a_un.a_val;
1320Sstevel@tonic-gate 			break;
1330Sstevel@tonic-gate 		case AT_SUN_UID:
1340Sstevel@tonic-gate 			/* effective user id for the executable */
1350Sstevel@tonic-gate 			euid = (uid_t)auxv->a_un.a_val;
1360Sstevel@tonic-gate 			break;
1370Sstevel@tonic-gate 		case AT_SUN_RUID:
1380Sstevel@tonic-gate 			/* real user id for the executable */
1390Sstevel@tonic-gate 			uid = (uid_t)auxv->a_un.a_val;
1400Sstevel@tonic-gate 			break;
1410Sstevel@tonic-gate 		case AT_SUN_GID:
1420Sstevel@tonic-gate 			/* effective group id for the executable */
1430Sstevel@tonic-gate 			egid = (gid_t)auxv->a_un.a_val;
1440Sstevel@tonic-gate 			break;
1450Sstevel@tonic-gate 		case AT_SUN_RGID:
1460Sstevel@tonic-gate 			/* real group id for the executable */
1470Sstevel@tonic-gate 			gid = (gid_t)auxv->a_un.a_val;
1480Sstevel@tonic-gate 			break;
1490Sstevel@tonic-gate 		case AT_SUN_PLATFORM:
1500Sstevel@tonic-gate 			/* platform name */
1510Sstevel@tonic-gate 			_platform = auxv->a_un.a_ptr;
1520Sstevel@tonic-gate 			break;
1530Sstevel@tonic-gate 		case AT_SUN_EXECNAME:
1540Sstevel@tonic-gate 			/* full pathname of execed object */
1550Sstevel@tonic-gate 			_execname = auxv->a_un.a_ptr;
1560Sstevel@tonic-gate 			break;
1570Sstevel@tonic-gate 		case AT_SUN_AUXFLAGS:
158*8598SRod.Evans@Sun.COM 			/* auxiliary flags */
1590Sstevel@tonic-gate 			auxflags = (int)auxv->a_un.a_val;
1600Sstevel@tonic-gate 			break;
1610Sstevel@tonic-gate 		case AT_SUN_HWCAP:
162*8598SRod.Evans@Sun.COM 			/* hardware capabilities */
1630Sstevel@tonic-gate 			hwcap_1 = (uint_t)auxv->a_un.a_val;
1640Sstevel@tonic-gate 			break;
1654642Ssl108498 		case AT_SUN_EMULATOR:
1664642Ssl108498 			/* name of emulation library, if any */
1674642Ssl108498 			_emulator = auxv->a_un.a_ptr;
1684642Ssl108498 			break;
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/*
1730Sstevel@tonic-gate 	 * Get needed info from ld.so's dynamic structure.
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate 	/* LINTED */
1760Sstevel@tonic-gate 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
1770Sstevel@tonic-gate 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
1780Sstevel@tonic-gate 		switch (ld_dyn->d_tag) {
1790Sstevel@tonic-gate 		case DT_RELA:
1800Sstevel@tonic-gate 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
1810Sstevel@tonic-gate 			break;
1820Sstevel@tonic-gate 		case DT_RELACOUNT:
1830Sstevel@tonic-gate 			relacount = ld_dyn->d_un.d_val;
1840Sstevel@tonic-gate 			break;
1850Sstevel@tonic-gate 		case DT_RELAENT:
1860Sstevel@tonic-gate 			relaent = ld_dyn->d_un.d_val;
1870Sstevel@tonic-gate 			break;
1880Sstevel@tonic-gate 		case DT_STRTAB:
1890Sstevel@tonic-gate 			strtab = ld_dyn->d_un.d_ptr + ld_base;
1900Sstevel@tonic-gate 			break;
1910Sstevel@tonic-gate 		case DT_SONAME:
1920Sstevel@tonic-gate 			soname = ld_dyn->d_un.d_val;
1930Sstevel@tonic-gate 			break;
1940Sstevel@tonic-gate 		}
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 	_rt_name = (char *)strtab + soname;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	/*
199*8598SRod.Evans@Sun.COM 	 * If we don't have a RELAENT, just assume the size.
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	if (relaent == 0)
2020Sstevel@tonic-gate 		relaent = sizeof (Rela);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
205*8598SRod.Evans@Sun.COM 	 * As all global symbol references within ld.so.1 are protected
206*8598SRod.Evans@Sun.COM 	 * (symbolic), only RELATIVE and JMPSLOT relocations should be left
207*8598SRod.Evans@Sun.COM 	 * to process at runtime.  Process all relative relocations now.
2080Sstevel@tonic-gate 	 */
2090Sstevel@tonic-gate 	for (; relacount; relacount--) {
2100Sstevel@tonic-gate 		ulong_t	roffset;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
2130Sstevel@tonic-gate 		*((ulong_t *)roffset) = ld_base +
214*8598SRod.Evans@Sun.COM 		    ((Rela *)reladdr)->r_addend;
2150Sstevel@tonic-gate 		reladdr += relaent;
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/*
2194642Ssl108498 	 * If an emulation library is being used, use that as the linker's
2204642Ssl108498 	 * effective executable name. The real executable is not linked by this
2214642Ssl108498 	 * linker.
2224642Ssl108498 	 */
2234642Ssl108498 	if (_emulator != NULL) {
2244642Ssl108498 		_execname = _emulator;
2254642Ssl108498 		rtld_flags2 |= RT_FL2_BRANDED;
2264642Ssl108498 	}
2274642Ssl108498 
2284642Ssl108498 	/*
2290Sstevel@tonic-gate 	 * Continue with generic startup processing.
2300Sstevel@tonic-gate 	 */
231*8598SRod.Evans@Sun.COM 	/* BEGIN CSTYLED */
2326Srie 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
233*8598SRod.Evans@Sun.COM 	    _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
234*8598SRod.Evans@Sun.COM 	    _execname, _argv, uid, euid, gid, egid,
2350Sstevel@tonic-gate #ifdef	A_OUT
236*8598SRod.Evans@Sun.COM 	    aoutdyn, auxflags, hwcap_1)) == NULL) {
2370Sstevel@tonic-gate #else
2386812Sraf 	    /* CSTYLED */
2390Sstevel@tonic-gate 	    NULL, auxflags, hwcap_1)) == NULL) {
2400Sstevel@tonic-gate #endif	/* A_OUT */
2410Sstevel@tonic-gate 		rtldexit(&lml_main, 1);
2420Sstevel@tonic-gate 	}
243*8598SRod.Evans@Sun.COM 	/* END CSTYLED */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	return (LM_ENTRY_PT(lmp)());
2460Sstevel@tonic-gate }
247