xref: /onnv-gate/usr/src/cmd/sgs/rtld/amd64/_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.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
266812Sraf 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * amd64 specific setup routine  -  relocate ld.so's symbols, setup its
290Sstevel@tonic-gate  * environment, map in loadable sections of the executable.
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * Takes base address ld.so was loaded at, address of ld.so's dynamic
320Sstevel@tonic-gate  * structure, address of process environment pointers, address of auxiliary
330Sstevel@tonic-gate  * vector and * argv[0] (process name).
340Sstevel@tonic-gate  * If errors occur, send process signal - otherwise
350Sstevel@tonic-gate  * return executable's entry point to the bootstrap routine.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include	<signal.h>
390Sstevel@tonic-gate #include	<stdlib.h>
400Sstevel@tonic-gate #include	<sys/auxv.h>
410Sstevel@tonic-gate #include	<sys/types.h>
420Sstevel@tonic-gate #include	<sys/stat.h>
430Sstevel@tonic-gate #include	<link.h>
440Sstevel@tonic-gate #include	<dlfcn.h>
450Sstevel@tonic-gate #include	"_rtld.h"
460Sstevel@tonic-gate #include	"_audit.h"
470Sstevel@tonic-gate #include	"msg.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /* VARARGS */
500Sstevel@tonic-gate unsigned long
_setup(Boot * ebp,Dyn * ld_dyn)51*8598SRod.Evans@Sun.COM _setup(Boot *ebp, Dyn *ld_dyn)
520Sstevel@tonic-gate {
53*8598SRod.Evans@Sun.COM 	ulong_t		reladdr, relacount, ld_base = 0;
54*8598SRod.Evans@Sun.COM 	ulong_t		relaent = 0, pltrelsz = 0;
55*8598SRod.Evans@Sun.COM 	ulong_t		strtab, soname, interp_base = 0;
560Sstevel@tonic-gate 	char		*_rt_name, **_envp, **_argv;
57*8598SRod.Evans@Sun.COM 	int		_syspagsz = 0, fd = -1;
580Sstevel@tonic-gate 	uint_t		_flags = 0, hwcap_1 = 0;
59*8598SRod.Evans@Sun.COM 	Dyn		*dyn_ptr;
60*8598SRod.Evans@Sun.COM 	Phdr		*phdr = NULL;
61*8598SRod.Evans@Sun.COM 	Rt_map		*lmp;
620Sstevel@tonic-gate 	auxv_t		*auxv, *_auxv;
634321Scasper 	uid_t		uid = (uid_t)-1, euid = (uid_t)-1;
644321Scasper 	gid_t		gid = (gid_t)-1, egid = (gid_t)-1;
65*8598SRod.Evans@Sun.COM 	char		*_platform = NULL, *_execname = NULL, *_emulator = NULL;
660Sstevel@tonic-gate 	int		auxflags = -1;
67*8598SRod.Evans@Sun.COM 
680Sstevel@tonic-gate 	/*
690Sstevel@tonic-gate 	 * Scan the bootstrap structure to pick up the basics.
700Sstevel@tonic-gate 	 */
710Sstevel@tonic-gate 	for (; ebp->eb_tag != EB_NULL; ebp++)
720Sstevel@tonic-gate 		switch (ebp->eb_tag) {
730Sstevel@tonic-gate 		case EB_LDSO_BASE:
740Sstevel@tonic-gate 			ld_base = (unsigned long)ebp->eb_un.eb_val;
750Sstevel@tonic-gate 			break;
760Sstevel@tonic-gate 		case EB_ARGV:
770Sstevel@tonic-gate 			_argv = (char **)ebp->eb_un.eb_ptr;
780Sstevel@tonic-gate 			break;
790Sstevel@tonic-gate 		case EB_ENVP:
800Sstevel@tonic-gate 			_envp = (char **)ebp->eb_un.eb_ptr;
810Sstevel@tonic-gate 			break;
820Sstevel@tonic-gate 		case EB_AUXV:
830Sstevel@tonic-gate 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
840Sstevel@tonic-gate 			break;
850Sstevel@tonic-gate 		case EB_PAGESIZE:
860Sstevel@tonic-gate 			_syspagsz = (int)ebp->eb_un.eb_val;
870Sstevel@tonic-gate 			break;
880Sstevel@tonic-gate 		}
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	/*
910Sstevel@tonic-gate 	 * Search the aux. vector for the information passed by exec.
920Sstevel@tonic-gate 	 */
930Sstevel@tonic-gate 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
940Sstevel@tonic-gate 		switch (auxv->a_type) {
950Sstevel@tonic-gate 		case AT_EXECFD:
960Sstevel@tonic-gate 			/* this is the old exec that passes a file descriptor */
970Sstevel@tonic-gate 			fd = (int)auxv->a_un.a_val;
980Sstevel@tonic-gate 			break;
990Sstevel@tonic-gate 		case AT_FLAGS:
1000Sstevel@tonic-gate 			/* processor flags (MAU available, etc) */
1010Sstevel@tonic-gate 			_flags = auxv->a_un.a_val;
1020Sstevel@tonic-gate 			break;
1030Sstevel@tonic-gate 		case AT_PAGESZ:
1040Sstevel@tonic-gate 			/* system page size */
1050Sstevel@tonic-gate 			_syspagsz = (int)auxv->a_un.a_val;
1060Sstevel@tonic-gate 			break;
1070Sstevel@tonic-gate 		case AT_PHDR:
1080Sstevel@tonic-gate 			/* address of the segment table */
1090Sstevel@tonic-gate 			phdr = (Phdr *)auxv->a_un.a_ptr;
1100Sstevel@tonic-gate 			break;
1110Sstevel@tonic-gate 		case AT_BASE:
1120Sstevel@tonic-gate 			/* interpreter base address */
1130Sstevel@tonic-gate 			if (ld_base == 0)
1140Sstevel@tonic-gate 				ld_base = auxv->a_un.a_val;
1150Sstevel@tonic-gate 			interp_base = auxv->a_un.a_val;
1160Sstevel@tonic-gate 			break;
1170Sstevel@tonic-gate 		case AT_SUN_UID:
1180Sstevel@tonic-gate 			/* effective user id for the executable */
1190Sstevel@tonic-gate 			euid = (uid_t)auxv->a_un.a_val;
1200Sstevel@tonic-gate 			break;
1210Sstevel@tonic-gate 		case AT_SUN_RUID:
1220Sstevel@tonic-gate 			/* real user id for the executable */
1230Sstevel@tonic-gate 			uid = (uid_t)auxv->a_un.a_val;
1240Sstevel@tonic-gate 			break;
1250Sstevel@tonic-gate 		case AT_SUN_GID:
1260Sstevel@tonic-gate 			/* effective group id for the executable */
1270Sstevel@tonic-gate 			egid = (gid_t)auxv->a_un.a_val;
1280Sstevel@tonic-gate 			break;
1290Sstevel@tonic-gate 		case AT_SUN_RGID:
1300Sstevel@tonic-gate 			/* real group id for the executable */
1310Sstevel@tonic-gate 			gid = (gid_t)auxv->a_un.a_val;
1320Sstevel@tonic-gate 			break;
1330Sstevel@tonic-gate 		case AT_SUN_PLATFORM:
1340Sstevel@tonic-gate 			/* platform name */
1350Sstevel@tonic-gate 			_platform = auxv->a_un.a_ptr;
1360Sstevel@tonic-gate 			break;
1370Sstevel@tonic-gate 		case AT_SUN_EXECNAME:
1380Sstevel@tonic-gate 			/* full pathname of execed object */
1390Sstevel@tonic-gate 			_execname = auxv->a_un.a_ptr;
1400Sstevel@tonic-gate 			break;
1410Sstevel@tonic-gate 		case AT_SUN_AUXFLAGS:
142*8598SRod.Evans@Sun.COM 			/* auxiliary flags */
1430Sstevel@tonic-gate 			auxflags = (int)auxv->a_un.a_val;
1440Sstevel@tonic-gate 			break;
1450Sstevel@tonic-gate 		case AT_SUN_HWCAP:
146*8598SRod.Evans@Sun.COM 			/* hardware capabilities */
1470Sstevel@tonic-gate 			hwcap_1 = (uint_t)auxv->a_un.a_val;
1480Sstevel@tonic-gate 			break;
1492712Snn35248 		case AT_SUN_EMULATOR:
1502712Snn35248 			/* name of emulation library, if any */
1512712Snn35248 			_emulator = auxv->a_un.a_ptr;
1522712Snn35248 			break;
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/*
1570Sstevel@tonic-gate 	 * Get needed info from ld.so's dynamic structure.
1580Sstevel@tonic-gate 	 */
1590Sstevel@tonic-gate 	/* LINTED */
1600Sstevel@tonic-gate 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
1610Sstevel@tonic-gate 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
1620Sstevel@tonic-gate 		switch (ld_dyn->d_tag) {
1630Sstevel@tonic-gate 		case DT_RELA:
1640Sstevel@tonic-gate 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
1650Sstevel@tonic-gate 			break;
1660Sstevel@tonic-gate 		case DT_RELACOUNT:
1670Sstevel@tonic-gate 			relacount = ld_dyn->d_un.d_val;
1680Sstevel@tonic-gate 			break;
1690Sstevel@tonic-gate 		case DT_RELAENT:
1700Sstevel@tonic-gate 			relaent = ld_dyn->d_un.d_val;
1710Sstevel@tonic-gate 			break;
1720Sstevel@tonic-gate 		case DT_PLTRELSZ:
1730Sstevel@tonic-gate 			pltrelsz = ld_dyn->d_un.d_val;
1740Sstevel@tonic-gate 			break;
1750Sstevel@tonic-gate 		case DT_STRTAB:
1760Sstevel@tonic-gate 			strtab = ld_dyn->d_un.d_ptr + ld_base;
1770Sstevel@tonic-gate 			break;
1780Sstevel@tonic-gate 		case DT_SONAME:
1790Sstevel@tonic-gate 			soname = ld_dyn->d_un.d_val;
1800Sstevel@tonic-gate 			break;
1810Sstevel@tonic-gate 		}
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 	_rt_name = (char *)strtab + soname;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
186*8598SRod.Evans@Sun.COM 	 * If we don't have a RELAENT, just assume the size.
1870Sstevel@tonic-gate 	 */
1880Sstevel@tonic-gate 	if (relaent == 0)
1890Sstevel@tonic-gate 		relaent = sizeof (Rela);
190*8598SRod.Evans@Sun.COM 
1910Sstevel@tonic-gate 	/*
192*8598SRod.Evans@Sun.COM 	 * As all global symbol references within ld.so.1 are protected
193*8598SRod.Evans@Sun.COM 	 * (symbolic), only RELATIVE and JMPSLOT relocations should be left
194*8598SRod.Evans@Sun.COM 	 * to process at runtime.  Process all relocations now.
1950Sstevel@tonic-gate 	 */
1960Sstevel@tonic-gate 	relacount += (pltrelsz / relaent);
1970Sstevel@tonic-gate 	for (; relacount; relacount--) {
1980Sstevel@tonic-gate 		ulong_t	roffset;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
2010Sstevel@tonic-gate 		*((ulong_t *)roffset) += ld_base +
2020Sstevel@tonic-gate 		    ((Rela *)reladdr)->r_addend;
2030Sstevel@tonic-gate 		reladdr += relaent;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2064642Ssl108498 	/*
2074642Ssl108498 	 * If an emulation library is being used, use that as the linker's
2084642Ssl108498 	 * effective executable name. The real executable is not linked by this
2094642Ssl108498 	 * linker.
2104642Ssl108498 	 */
2112712Snn35248 	if (_emulator != NULL) {
2124642Ssl108498 		_execname = _emulator;
2132712Snn35248 		rtld_flags2 |= RT_FL2_BRANDED;
2142712Snn35248 	}
2152712Snn35248 
2160Sstevel@tonic-gate 	/*
2170Sstevel@tonic-gate 	 * Initialize the dyn_plt_ent_size field.  It currently contains the
2180Sstevel@tonic-gate 	 * size of the dyn_plt_template.  It still needs to be aligned and have
2190Sstevel@tonic-gate 	 * space for the 'dyn_data' area added.
2200Sstevel@tonic-gate 	 */
2210Sstevel@tonic-gate 	dyn_plt_ent_size = ROUND(dyn_plt_ent_size, M_WORD_ALIGN) +
2220Sstevel@tonic-gate 	    sizeof (uintptr_t) + sizeof (uintptr_t) + sizeof (ulong_t) +
2230Sstevel@tonic-gate 	    sizeof (ulong_t) + sizeof (Sym);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/*
2260Sstevel@tonic-gate 	 * Continue with generic startup processing.
2270Sstevel@tonic-gate 	 */
2286Srie 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
229*8598SRod.Evans@Sun.COM 	    _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
230*8598SRod.Evans@Sun.COM 	    _execname, _argv, uid, euid, gid, egid, NULL, auxflags,
2316Srie 	    hwcap_1)) == NULL) {
2320Sstevel@tonic-gate 		rtldexit(&lml_main, 1);
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	return (LM_ENTRY_PT(lmp)());
2360Sstevel@tonic-gate }
237