xref: /titanic_50/usr/src/cmd/sgs/rtld/amd64/_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  */
26*ebb8ac07SRobert Mustacchi /*
27*ebb8ac07SRobert Mustacchi  * Copyright (c) 2012, Joyent, Inc.  All rights reserved.
28*ebb8ac07SRobert Mustacchi  */
297257d1b4Sraf 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * amd64 specific setup routine  -  relocate ld.so's symbols, setup its
327c478bd9Sstevel@tonic-gate  * environment, map in loadable sections of the executable.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Takes base address ld.so was loaded at, address of ld.so's dynamic
357c478bd9Sstevel@tonic-gate  * structure, address of process environment pointers, address of auxiliary
367c478bd9Sstevel@tonic-gate  * vector and * argv[0] (process name).
377c478bd9Sstevel@tonic-gate  * If errors occur, send process signal - otherwise
387c478bd9Sstevel@tonic-gate  * return executable's entry point to the bootstrap routine.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include	<signal.h>
427c478bd9Sstevel@tonic-gate #include	<stdlib.h>
437c478bd9Sstevel@tonic-gate #include	<sys/auxv.h>
447c478bd9Sstevel@tonic-gate #include	<sys/types.h>
457c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
467c478bd9Sstevel@tonic-gate #include	<link.h>
477c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
487c478bd9Sstevel@tonic-gate #include	"_rtld.h"
497c478bd9Sstevel@tonic-gate #include	"_audit.h"
507c478bd9Sstevel@tonic-gate #include	"msg.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* VARARGS */
537c478bd9Sstevel@tonic-gate unsigned long
_setup(Boot * ebp,Dyn * ld_dyn)547c478bd9Sstevel@tonic-gate _setup(Boot *ebp, Dyn *ld_dyn)
557c478bd9Sstevel@tonic-gate {
5656deab07SRod Evans 	ulong_t		reladdr, relacount, ld_base = 0;
5756deab07SRod Evans 	ulong_t		relaent = 0, pltrelsz = 0;
5856deab07SRod Evans 	ulong_t		strtab, soname, interp_base = 0;
597c478bd9Sstevel@tonic-gate 	char		*_rt_name, **_envp, **_argv;
6056deab07SRod Evans 	int		_syspagsz = 0, fd = -1;
61*ebb8ac07SRobert Mustacchi 	uint_t		_flags = 0;
62*ebb8ac07SRobert Mustacchi 	uint_t		hwcap[2] = { 0, 0 };
637c478bd9Sstevel@tonic-gate 	Dyn		*dyn_ptr;
6456deab07SRod Evans 	Phdr		*phdr = NULL;
657c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
667c478bd9Sstevel@tonic-gate 	auxv_t		*auxv, *_auxv;
67f48205beScasper 	uid_t		uid = (uid_t)-1, euid = (uid_t)-1;
68f48205beScasper 	gid_t		gid = (gid_t)-1, egid = (gid_t)-1;
6956deab07SRod Evans 	char		*_platform = NULL, *_execname = NULL, *_emulator = NULL;
707c478bd9Sstevel@tonic-gate 	int		auxflags = -1;
7156deab07SRod Evans 
727c478bd9Sstevel@tonic-gate 	/*
737c478bd9Sstevel@tonic-gate 	 * Scan the bootstrap structure to pick up the basics.
747c478bd9Sstevel@tonic-gate 	 */
757c478bd9Sstevel@tonic-gate 	for (; ebp->eb_tag != EB_NULL; ebp++)
767c478bd9Sstevel@tonic-gate 		switch (ebp->eb_tag) {
777c478bd9Sstevel@tonic-gate 		case EB_LDSO_BASE:
787c478bd9Sstevel@tonic-gate 			ld_base = (unsigned long)ebp->eb_un.eb_val;
797c478bd9Sstevel@tonic-gate 			break;
807c478bd9Sstevel@tonic-gate 		case EB_ARGV:
817c478bd9Sstevel@tonic-gate 			_argv = (char **)ebp->eb_un.eb_ptr;
827c478bd9Sstevel@tonic-gate 			break;
837c478bd9Sstevel@tonic-gate 		case EB_ENVP:
847c478bd9Sstevel@tonic-gate 			_envp = (char **)ebp->eb_un.eb_ptr;
857c478bd9Sstevel@tonic-gate 			break;
867c478bd9Sstevel@tonic-gate 		case EB_AUXV:
877c478bd9Sstevel@tonic-gate 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
887c478bd9Sstevel@tonic-gate 			break;
897c478bd9Sstevel@tonic-gate 		case EB_PAGESIZE:
907c478bd9Sstevel@tonic-gate 			_syspagsz = (int)ebp->eb_un.eb_val;
917c478bd9Sstevel@tonic-gate 			break;
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/*
957c478bd9Sstevel@tonic-gate 	 * Search the aux. vector for the information passed by exec.
967c478bd9Sstevel@tonic-gate 	 */
977c478bd9Sstevel@tonic-gate 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
987c478bd9Sstevel@tonic-gate 		switch (auxv->a_type) {
997c478bd9Sstevel@tonic-gate 		case AT_EXECFD:
1007c478bd9Sstevel@tonic-gate 			/* this is the old exec that passes a file descriptor */
1017c478bd9Sstevel@tonic-gate 			fd = (int)auxv->a_un.a_val;
1027c478bd9Sstevel@tonic-gate 			break;
1037c478bd9Sstevel@tonic-gate 		case AT_FLAGS:
1047c478bd9Sstevel@tonic-gate 			/* processor flags (MAU available, etc) */
1057c478bd9Sstevel@tonic-gate 			_flags = auxv->a_un.a_val;
1067c478bd9Sstevel@tonic-gate 			break;
1077c478bd9Sstevel@tonic-gate 		case AT_PAGESZ:
1087c478bd9Sstevel@tonic-gate 			/* system page size */
1097c478bd9Sstevel@tonic-gate 			_syspagsz = (int)auxv->a_un.a_val;
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		case AT_PHDR:
1127c478bd9Sstevel@tonic-gate 			/* address of the segment table */
1137c478bd9Sstevel@tonic-gate 			phdr = (Phdr *)auxv->a_un.a_ptr;
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 		case AT_BASE:
1167c478bd9Sstevel@tonic-gate 			/* interpreter base address */
1177c478bd9Sstevel@tonic-gate 			if (ld_base == 0)
1187c478bd9Sstevel@tonic-gate 				ld_base = auxv->a_un.a_val;
1197c478bd9Sstevel@tonic-gate 			interp_base = auxv->a_un.a_val;
1207c478bd9Sstevel@tonic-gate 			break;
1217c478bd9Sstevel@tonic-gate 		case AT_SUN_UID:
1227c478bd9Sstevel@tonic-gate 			/* effective user id for the executable */
1237c478bd9Sstevel@tonic-gate 			euid = (uid_t)auxv->a_un.a_val;
1247c478bd9Sstevel@tonic-gate 			break;
1257c478bd9Sstevel@tonic-gate 		case AT_SUN_RUID:
1267c478bd9Sstevel@tonic-gate 			/* real user id for the executable */
1277c478bd9Sstevel@tonic-gate 			uid = (uid_t)auxv->a_un.a_val;
1287c478bd9Sstevel@tonic-gate 			break;
1297c478bd9Sstevel@tonic-gate 		case AT_SUN_GID:
1307c478bd9Sstevel@tonic-gate 			/* effective group id for the executable */
1317c478bd9Sstevel@tonic-gate 			egid = (gid_t)auxv->a_un.a_val;
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 		case AT_SUN_RGID:
1347c478bd9Sstevel@tonic-gate 			/* real group id for the executable */
1357c478bd9Sstevel@tonic-gate 			gid = (gid_t)auxv->a_un.a_val;
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 		case AT_SUN_PLATFORM:
1387c478bd9Sstevel@tonic-gate 			/* platform name */
1397c478bd9Sstevel@tonic-gate 			_platform = auxv->a_un.a_ptr;
1407c478bd9Sstevel@tonic-gate 			break;
1417c478bd9Sstevel@tonic-gate 		case AT_SUN_EXECNAME:
1427c478bd9Sstevel@tonic-gate 			/* full pathname of execed object */
1437c478bd9Sstevel@tonic-gate 			_execname = auxv->a_un.a_ptr;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case AT_SUN_AUXFLAGS:
14656deab07SRod Evans 			/* auxiliary flags */
1477c478bd9Sstevel@tonic-gate 			auxflags = (int)auxv->a_un.a_val;
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 		case AT_SUN_HWCAP:
15056deab07SRod Evans 			/* hardware capabilities */
151*ebb8ac07SRobert Mustacchi 			hwcap[0] = (uint_t)auxv->a_un.a_val;
152*ebb8ac07SRobert Mustacchi 			break;
153*ebb8ac07SRobert Mustacchi 		case AT_SUN_HWCAP2:
154*ebb8ac07SRobert Mustacchi 			/* hardware capabilities */
155*ebb8ac07SRobert Mustacchi 			hwcap[1] = (uint_t)auxv->a_un.a_val;
1567c478bd9Sstevel@tonic-gate 			break;
1579acbbeafSnn35248 		case AT_SUN_EMULATOR:
1589acbbeafSnn35248 			/* name of emulation library, if any */
1599acbbeafSnn35248 			_emulator = auxv->a_un.a_ptr;
1609acbbeafSnn35248 			break;
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * Get needed info from ld.so's dynamic structure.
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	/* LINTED */
1687c478bd9Sstevel@tonic-gate 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
1697c478bd9Sstevel@tonic-gate 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
1707c478bd9Sstevel@tonic-gate 		switch (ld_dyn->d_tag) {
1717c478bd9Sstevel@tonic-gate 		case DT_RELA:
1727c478bd9Sstevel@tonic-gate 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 		case DT_RELACOUNT:
1757c478bd9Sstevel@tonic-gate 			relacount = ld_dyn->d_un.d_val;
1767c478bd9Sstevel@tonic-gate 			break;
1777c478bd9Sstevel@tonic-gate 		case DT_RELAENT:
1787c478bd9Sstevel@tonic-gate 			relaent = ld_dyn->d_un.d_val;
1797c478bd9Sstevel@tonic-gate 			break;
1807c478bd9Sstevel@tonic-gate 		case DT_PLTRELSZ:
1817c478bd9Sstevel@tonic-gate 			pltrelsz = ld_dyn->d_un.d_val;
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 		case DT_STRTAB:
1847c478bd9Sstevel@tonic-gate 			strtab = ld_dyn->d_un.d_ptr + ld_base;
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 		case DT_SONAME:
1877c478bd9Sstevel@tonic-gate 			soname = ld_dyn->d_un.d_val;
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	_rt_name = (char *)strtab + soname;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/*
19456deab07SRod Evans 	 * If we don't have a RELAENT, just assume the size.
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 	if (relaent == 0)
1977c478bd9Sstevel@tonic-gate 		relaent = sizeof (Rela);
19856deab07SRod Evans 
1997c478bd9Sstevel@tonic-gate 	/*
20056deab07SRod Evans 	 * As all global symbol references within ld.so.1 are protected
20156deab07SRod Evans 	 * (symbolic), only RELATIVE and JMPSLOT relocations should be left
20256deab07SRod Evans 	 * to process at runtime.  Process all relocations now.
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	relacount += (pltrelsz / relaent);
2057c478bd9Sstevel@tonic-gate 	for (; relacount; relacount--) {
2067c478bd9Sstevel@tonic-gate 		ulong_t	roffset;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
2097c478bd9Sstevel@tonic-gate 		*((ulong_t *)roffset) += ld_base +
2107c478bd9Sstevel@tonic-gate 		    ((Rela *)reladdr)->r_addend;
2117c478bd9Sstevel@tonic-gate 		reladdr += relaent;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
21407678296Ssl108498 	/*
21507678296Ssl108498 	 * If an emulation library is being used, use that as the linker's
21607678296Ssl108498 	 * effective executable name. The real executable is not linked by this
21707678296Ssl108498 	 * linker.
21807678296Ssl108498 	 */
2199acbbeafSnn35248 	if (_emulator != NULL) {
22007678296Ssl108498 		_execname = _emulator;
2219acbbeafSnn35248 		rtld_flags2 |= RT_FL2_BRANDED;
2229acbbeafSnn35248 	}
2239acbbeafSnn35248 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Initialize the dyn_plt_ent_size field.  It currently contains the
2267c478bd9Sstevel@tonic-gate 	 * size of the dyn_plt_template.  It still needs to be aligned and have
2277c478bd9Sstevel@tonic-gate 	 * space for the 'dyn_data' area added.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	dyn_plt_ent_size = ROUND(dyn_plt_ent_size, M_WORD_ALIGN) +
2307c478bd9Sstevel@tonic-gate 	    sizeof (uintptr_t) + sizeof (uintptr_t) + sizeof (ulong_t) +
2317c478bd9Sstevel@tonic-gate 	    sizeof (ulong_t) + sizeof (Sym);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * Continue with generic startup processing.
2357c478bd9Sstevel@tonic-gate 	 */
23641072f3cSrie 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
23756deab07SRod Evans 	    _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
23856deab07SRod Evans 	    _execname, _argv, uid, euid, gid, egid, NULL, auxflags,
239*ebb8ac07SRobert Mustacchi 	    hwcap)) == NULL) {
2407c478bd9Sstevel@tonic-gate 		rtldexit(&lml_main, 1);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	return (LM_ENTRY_PT(lmp)());
2447c478bd9Sstevel@tonic-gate }
245