xref: /onnv-gate/usr/src/cmd/sgs/rtld/sparc/_setup.c (revision 4642:d7554fc0577a)
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 /*
230Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
240Sstevel@tonic-gate  *	  All Rights Reserved
250Sstevel@tonic-gate  *
264321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * SPARC specific setup routine  -  relocate ld.so's symbols, setup its
330Sstevel@tonic-gate  * environment, map in loadable sections of the executable.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * Takes base address ld.so was loaded at, address of ld.so's dynamic
360Sstevel@tonic-gate  * structure, address of process environment pointers, address of auxiliary
370Sstevel@tonic-gate  * vector and * argv[0] (process name).
380Sstevel@tonic-gate  * If errors occur, send process signal - otherwise
390Sstevel@tonic-gate  * return executable's entry point to the bootstrap routine.
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate #include	"_synonyms.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include	<signal.h>
440Sstevel@tonic-gate #include	<stdlib.h>
450Sstevel@tonic-gate #include	<fcntl.h>
460Sstevel@tonic-gate #include	<stdio.h>
470Sstevel@tonic-gate #include	<sys/auxv.h>
480Sstevel@tonic-gate #include	<sys/types.h>
490Sstevel@tonic-gate #include	<sys/stat.h>
500Sstevel@tonic-gate #include	<link.h>
510Sstevel@tonic-gate #include	<dlfcn.h>
520Sstevel@tonic-gate #include	"_rtld.h"
530Sstevel@tonic-gate #include	"_audit.h"
540Sstevel@tonic-gate #include	"msg.h"
550Sstevel@tonic-gate #ifdef A_OUT
560Sstevel@tonic-gate #include	"_a.out.h"
570Sstevel@tonic-gate #endif /* A_OUT */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate extern int	_end;
600Sstevel@tonic-gate extern int	_etext;
610Sstevel@tonic-gate extern void	_init(void);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /* VARARGS */
650Sstevel@tonic-gate unsigned long
660Sstevel@tonic-gate _setup(Boot * ebp, Dyn * ld_dyn)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	unsigned long	reladdr, relacount, ld_base = 0;
690Sstevel@tonic-gate 	unsigned long	relaent = 0;
700Sstevel@tonic-gate 	unsigned long	strtab, soname, interp_base = 0;
710Sstevel@tonic-gate 	char		*_rt_name, **_envp, **_argv;
720Sstevel@tonic-gate 	int		_syspagsz = 0, fd = -1, dz_fd = FD_UNAVAIL;
730Sstevel@tonic-gate 	uint_t		_flags = 0, hwcap_1 = 0;
740Sstevel@tonic-gate 	Dyn *		dyn_ptr;
750Sstevel@tonic-gate 	Phdr *		phdr = 0;
760Sstevel@tonic-gate 	Rt_map *	lmp;
770Sstevel@tonic-gate 	auxv_t		*auxv, *_auxv;
784321Scasper 	uid_t		uid = (uid_t)-1, euid = (uid_t)-1;
794321Scasper 	gid_t		gid = (gid_t)-1, egid = (gid_t)-1;
80*4642Ssl108498 	char		*_platform = 0, *_execname = 0, *_emulator = 0;
810Sstevel@tonic-gate 	int		auxflags = -1;
820Sstevel@tonic-gate #ifdef	A_OUT
830Sstevel@tonic-gate 	void *		aoutdyn = 0;
840Sstevel@tonic-gate #endif	/* A_OUT */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * Scan the bootstrap structure to pick up the basics.
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	for (; ebp->eb_tag != EB_NULL; ebp++)
900Sstevel@tonic-gate 		switch (ebp->eb_tag) {
910Sstevel@tonic-gate 		case EB_DYNAMIC:
920Sstevel@tonic-gate #ifdef A_OUT
930Sstevel@tonic-gate 			aoutdyn = (struct link_dynamic *)ebp->eb_un.eb_val;
940Sstevel@tonic-gate #endif /* A_OUT */
950Sstevel@tonic-gate 			break;
960Sstevel@tonic-gate 		case EB_LDSO_BASE:
970Sstevel@tonic-gate 			ld_base = (unsigned long)ebp->eb_un.eb_val;
980Sstevel@tonic-gate 			break;
990Sstevel@tonic-gate 		case EB_ARGV:
1000Sstevel@tonic-gate 			_argv = (char **)ebp->eb_un.eb_ptr;
1010Sstevel@tonic-gate 			break;
1020Sstevel@tonic-gate 		case EB_ENVP:
1030Sstevel@tonic-gate 			_envp = (char **)ebp->eb_un.eb_ptr;
1040Sstevel@tonic-gate 			break;
1050Sstevel@tonic-gate 		case EB_AUXV:
1060Sstevel@tonic-gate 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
1070Sstevel@tonic-gate 			break;
1080Sstevel@tonic-gate 		case EB_DEVZERO:
1090Sstevel@tonic-gate 			dz_fd = (int)ebp->eb_un.eb_val;
1100Sstevel@tonic-gate 			break;
1110Sstevel@tonic-gate 		case EB_PAGESIZE:
1120Sstevel@tonic-gate 			_syspagsz = (int)ebp->eb_un.eb_val;
1130Sstevel@tonic-gate 			break;
1140Sstevel@tonic-gate 		}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/*
1170Sstevel@tonic-gate 	 * Search the aux. vector for the information passed by exec.
1180Sstevel@tonic-gate 	 */
1190Sstevel@tonic-gate 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
1200Sstevel@tonic-gate 		switch (auxv->a_type) {
1210Sstevel@tonic-gate 		case AT_EXECFD:
1220Sstevel@tonic-gate 			/* this is the old exec that passes a file descriptor */
1230Sstevel@tonic-gate 			fd = (int)auxv->a_un.a_val;
1240Sstevel@tonic-gate 			break;
1250Sstevel@tonic-gate 		case AT_FLAGS:
1260Sstevel@tonic-gate 			/* processor flags (MAU available, etc) */
1270Sstevel@tonic-gate 			_flags = auxv->a_un.a_val;
1280Sstevel@tonic-gate 			break;
1290Sstevel@tonic-gate 		case AT_PAGESZ:
1300Sstevel@tonic-gate 			/* system page size */
1310Sstevel@tonic-gate 			_syspagsz = (int)auxv->a_un.a_val;
1320Sstevel@tonic-gate 			break;
1330Sstevel@tonic-gate 		case AT_PHDR:
1340Sstevel@tonic-gate 			/* address of the segment table */
1350Sstevel@tonic-gate 			phdr = (Phdr *)auxv->a_un.a_ptr;
1360Sstevel@tonic-gate 			break;
1370Sstevel@tonic-gate 		case AT_BASE:
1380Sstevel@tonic-gate 			/* interpreter base address */
1390Sstevel@tonic-gate 			if (ld_base == 0)
1400Sstevel@tonic-gate 				ld_base = auxv->a_un.a_val;
1410Sstevel@tonic-gate 			interp_base = auxv->a_un.a_val;
1420Sstevel@tonic-gate 			break;
1430Sstevel@tonic-gate 		case AT_SUN_UID:
1440Sstevel@tonic-gate 			/* effective user id for the executable */
1450Sstevel@tonic-gate 			euid = (uid_t)auxv->a_un.a_val;
1460Sstevel@tonic-gate 			break;
1470Sstevel@tonic-gate 		case AT_SUN_RUID:
1480Sstevel@tonic-gate 			/* real user id for the executable */
1490Sstevel@tonic-gate 			uid = (uid_t)auxv->a_un.a_val;
1500Sstevel@tonic-gate 			break;
1510Sstevel@tonic-gate 		case AT_SUN_GID:
1520Sstevel@tonic-gate 			/* effective group id for the executable */
1530Sstevel@tonic-gate 			egid = (gid_t)auxv->a_un.a_val;
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 		case AT_SUN_RGID:
1560Sstevel@tonic-gate 			/* real group id for the executable */
1570Sstevel@tonic-gate 			gid = (gid_t)auxv->a_un.a_val;
1580Sstevel@tonic-gate 			break;
1590Sstevel@tonic-gate #ifdef	AT_SUN_PLATFORM			/* Defined on SunOS 5.5 & greater. */
1600Sstevel@tonic-gate 		case AT_SUN_PLATFORM:
1610Sstevel@tonic-gate 			/* platform name */
1620Sstevel@tonic-gate 			_platform = auxv->a_un.a_ptr;
1630Sstevel@tonic-gate 			break;
1640Sstevel@tonic-gate #endif
1650Sstevel@tonic-gate #ifdef	AT_SUN_EXECNAME			/* Defined on SunOS 5.6 & greater. */
1660Sstevel@tonic-gate 		case AT_SUN_EXECNAME:
1670Sstevel@tonic-gate 			/* full pathname of execed object */
1680Sstevel@tonic-gate 			_execname = auxv->a_un.a_ptr;
1690Sstevel@tonic-gate 			break;
1700Sstevel@tonic-gate #endif
1710Sstevel@tonic-gate #ifdef	AT_SUN_AUXFLAGS			/* At the behest of PSARC 2002/188 */
1720Sstevel@tonic-gate 		case AT_SUN_AUXFLAGS:
1730Sstevel@tonic-gate 			auxflags = (int)auxv->a_un.a_val;
1740Sstevel@tonic-gate 			break;
1750Sstevel@tonic-gate #endif
1760Sstevel@tonic-gate #ifdef	AT_SUN_HWCAP			/* Hardware capabilities */
1770Sstevel@tonic-gate 		case AT_SUN_HWCAP:
1780Sstevel@tonic-gate 			hwcap_1 = (uint_t)auxv->a_un.a_val;
1790Sstevel@tonic-gate 			break;
1800Sstevel@tonic-gate #endif
181*4642Ssl108498 #ifdef  AT_SUN_EMULATOR			/* Emulation library name */
182*4642Ssl108498 		case AT_SUN_EMULATOR:
183*4642Ssl108498 			/* name of emulation library, if any */
184*4642Ssl108498 			_emulator = auxv->a_un.a_ptr;
185*4642Ssl108498 			break;
186*4642Ssl108498 #endif
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/*
1910Sstevel@tonic-gate 	 * Get needed info from ld.so's dynamic structure.
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 	/* LINTED */
1940Sstevel@tonic-gate 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
1950Sstevel@tonic-gate 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
1960Sstevel@tonic-gate 		switch (ld_dyn->d_tag) {
1970Sstevel@tonic-gate 		case DT_RELA:
1980Sstevel@tonic-gate 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
1990Sstevel@tonic-gate 			break;
2000Sstevel@tonic-gate 		case DT_RELACOUNT:
2010Sstevel@tonic-gate 			relacount = ld_dyn->d_un.d_val;
2020Sstevel@tonic-gate 			break;
2030Sstevel@tonic-gate 		case DT_RELAENT:
2040Sstevel@tonic-gate 			relaent = ld_dyn->d_un.d_val;
2050Sstevel@tonic-gate 			break;
2060Sstevel@tonic-gate 		case DT_STRTAB:
2070Sstevel@tonic-gate 			strtab = ld_dyn->d_un.d_ptr + ld_base;
2080Sstevel@tonic-gate 			break;
2090Sstevel@tonic-gate 		case DT_SONAME:
2100Sstevel@tonic-gate 			soname = ld_dyn->d_un.d_val;
2110Sstevel@tonic-gate 			break;
2120Sstevel@tonic-gate 		}
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	_rt_name = (char *)strtab + soname;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/*
2170Sstevel@tonic-gate 	 * If we don't have a RELAENT, just assume
2180Sstevel@tonic-gate 	 * the size.
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	if (relaent == 0)
2210Sstevel@tonic-gate 		relaent = sizeof (Rela);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	/*
2240Sstevel@tonic-gate 	 * Because ld.so.1 is built with -Bsymbolic there should only be
2250Sstevel@tonic-gate 	 * RELATIVE and JMPSLOT relocations.  Process all relatives first.
2260Sstevel@tonic-gate 	 */
2270Sstevel@tonic-gate 	for (; relacount; relacount--) {
2280Sstevel@tonic-gate 		ulong_t	roffset;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
2310Sstevel@tonic-gate 		*((ulong_t *)roffset) = ld_base +
2320Sstevel@tonic-gate 		    (long)(((Rela *)reladdr)->r_addend);
2330Sstevel@tonic-gate 		reladdr += relaent;
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	/*
237*4642Ssl108498 	 * If an emulation library is being used, use that as the linker's
238*4642Ssl108498 	 * effective executable name. The real executable is not linked by this
239*4642Ssl108498 	 * linker.
240*4642Ssl108498 	 */
241*4642Ssl108498 	if (_emulator != NULL) {
242*4642Ssl108498 		_execname = _emulator;
243*4642Ssl108498 		rtld_flags2 |= RT_FL2_BRANDED;
244*4642Ssl108498 	}
245*4642Ssl108498 
246*4642Ssl108498 	/*
2470Sstevel@tonic-gate 	 * Continue with generic startup processing.
2480Sstevel@tonic-gate 	 */
2496Srie 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
2506Srie 	    _syspagsz, _rt_name, dyn_ptr, ld_base, interp_base, fd, phdr,
2516Srie 	    _execname, _argv, dz_fd, uid, euid, gid, egid,
2520Sstevel@tonic-gate #ifdef	A_OUT
2530Sstevel@tonic-gate 	    aoutdyn, auxflags, hwcap_1)) == (Rt_map *)0) {
2540Sstevel@tonic-gate #else
2550Sstevel@tonic-gate 	    NULL, auxflags, hwcap_1)) == NULL) {
2560Sstevel@tonic-gate #endif	/* A_OUT */
2570Sstevel@tonic-gate 		rtldexit(&lml_main, 1);
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	return (LM_ENTRY_PT(lmp)());
2610Sstevel@tonic-gate }
262