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
227257d1b4Sraf /*
2356deab07SRod Evans * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247257d1b4Sraf * Use is subject to license terms.
257257d1b4Sraf */
267257d1b4Sraf
277c478bd9Sstevel@tonic-gate /*
285aefb655Srie * Copyright (c) 1988 AT&T
295aefb655Srie * All Rights Reserved
307c478bd9Sstevel@tonic-gate */
31ebb8ac07SRobert Mustacchi /*
32ebb8ac07SRobert Mustacchi * Copyright (c) 2012, Joyent, Inc. All rights reserved.
33*56726c7eSRobert Mustacchi * Copyright 2022 Oxide Computer Company
34ebb8ac07SRobert Mustacchi */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate * i386 specific setup routine - relocate ld.so's symbols, setup its
387c478bd9Sstevel@tonic-gate * environment, map in loadable sections of the executable.
397c478bd9Sstevel@tonic-gate *
407c478bd9Sstevel@tonic-gate * Takes base address ld.so was loaded at, address of ld.so's dynamic
417c478bd9Sstevel@tonic-gate * structure, address of process environment pointers, address of auxiliary
427c478bd9Sstevel@tonic-gate * vector and * argv[0] (process name).
437c478bd9Sstevel@tonic-gate * If errors occur, send process signal - otherwise
447c478bd9Sstevel@tonic-gate * return executable's entry point to the bootstrap routine.
457c478bd9Sstevel@tonic-gate */
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #include <signal.h>
487c478bd9Sstevel@tonic-gate #include <stdlib.h>
497c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
507c478bd9Sstevel@tonic-gate #include <sys/types.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <link.h>
537c478bd9Sstevel@tonic-gate #include <dlfcn.h>
547c478bd9Sstevel@tonic-gate #include "_rtld.h"
557c478bd9Sstevel@tonic-gate #include "_audit.h"
567c478bd9Sstevel@tonic-gate #include "msg.h"
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /* VARARGS */
597c478bd9Sstevel@tonic-gate unsigned long
_setup(Boot * ebp,Dyn * ld_dyn)6056deab07SRod Evans _setup(Boot *ebp, Dyn *ld_dyn)
617c478bd9Sstevel@tonic-gate {
6256deab07SRod Evans ulong_t reladdr, relcount, ld_base = 0;
6356deab07SRod Evans ulong_t relent = 0;
6456deab07SRod Evans ulong_t strtab, soname, interp_base = 0;
657c478bd9Sstevel@tonic-gate char *_rt_name, **_envp, **_argv;
6656deab07SRod Evans int _syspagsz = 0, fd = -1;
67ebb8ac07SRobert Mustacchi uint_t _flags = 0;
68*56726c7eSRobert Mustacchi uint_t hwcap[3] = { 0, 0, 0 };
6956deab07SRod Evans Dyn *dyn_ptr;
7056deab07SRod Evans Phdr *phdr = NULL;
7156deab07SRod Evans Rt_map *lmp;
727c478bd9Sstevel@tonic-gate auxv_t *auxv, *_auxv;
73f48205beScasper uid_t uid = (uid_t)-1, euid = (uid_t)-1;
74f48205beScasper gid_t gid = (gid_t)-1, egid = (gid_t)-1;
7556deab07SRod Evans char *_platform = NULL, *_execname = NULL, *_emulator = NULL;
767c478bd9Sstevel@tonic-gate int auxflags = -1;
7756deab07SRod Evans
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate * Scan the bootstrap structure to pick up the basics.
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate for (; ebp->eb_tag != EB_NULL; ebp++)
827c478bd9Sstevel@tonic-gate switch (ebp->eb_tag) {
837c478bd9Sstevel@tonic-gate case EB_LDSO_BASE:
847c478bd9Sstevel@tonic-gate ld_base = (unsigned long)ebp->eb_un.eb_val;
857c478bd9Sstevel@tonic-gate break;
867c478bd9Sstevel@tonic-gate case EB_ARGV:
877c478bd9Sstevel@tonic-gate _argv = (char **)ebp->eb_un.eb_ptr;
887c478bd9Sstevel@tonic-gate break;
897c478bd9Sstevel@tonic-gate case EB_ENVP:
907c478bd9Sstevel@tonic-gate _envp = (char **)ebp->eb_un.eb_ptr;
917c478bd9Sstevel@tonic-gate break;
927c478bd9Sstevel@tonic-gate case EB_AUXV:
937c478bd9Sstevel@tonic-gate _auxv = (auxv_t *)ebp->eb_un.eb_ptr;
947c478bd9Sstevel@tonic-gate break;
957c478bd9Sstevel@tonic-gate case EB_PAGESIZE:
967c478bd9Sstevel@tonic-gate _syspagsz = (int)ebp->eb_un.eb_val;
977c478bd9Sstevel@tonic-gate break;
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate * Search the aux. vector for the information passed by exec.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
1047c478bd9Sstevel@tonic-gate switch (auxv->a_type) {
1057c478bd9Sstevel@tonic-gate case AT_EXECFD:
1067c478bd9Sstevel@tonic-gate /* this is the old exec that passes a file descriptor */
1077c478bd9Sstevel@tonic-gate fd = (int)auxv->a_un.a_val;
1087c478bd9Sstevel@tonic-gate break;
1097c478bd9Sstevel@tonic-gate case AT_FLAGS:
1107c478bd9Sstevel@tonic-gate /* processor flags (MAU available, etc) */
1117c478bd9Sstevel@tonic-gate _flags = auxv->a_un.a_val;
1127c478bd9Sstevel@tonic-gate break;
1137c478bd9Sstevel@tonic-gate case AT_PAGESZ:
1147c478bd9Sstevel@tonic-gate /* system page size */
1157c478bd9Sstevel@tonic-gate _syspagsz = (int)auxv->a_un.a_val;
1167c478bd9Sstevel@tonic-gate break;
1177c478bd9Sstevel@tonic-gate case AT_PHDR:
1187c478bd9Sstevel@tonic-gate /* address of the segment table */
1197c478bd9Sstevel@tonic-gate phdr = (Phdr *)auxv->a_un.a_ptr;
1207c478bd9Sstevel@tonic-gate break;
1217c478bd9Sstevel@tonic-gate case AT_BASE:
1227c478bd9Sstevel@tonic-gate /* interpreter base address */
1237c478bd9Sstevel@tonic-gate if (ld_base == 0)
1247c478bd9Sstevel@tonic-gate ld_base = auxv->a_un.a_val;
1257c478bd9Sstevel@tonic-gate interp_base = auxv->a_un.a_val;
1267c478bd9Sstevel@tonic-gate break;
1277c478bd9Sstevel@tonic-gate case AT_SUN_UID:
1287c478bd9Sstevel@tonic-gate /* effective user id for the executable */
1297c478bd9Sstevel@tonic-gate euid = (uid_t)auxv->a_un.a_val;
1307c478bd9Sstevel@tonic-gate break;
1317c478bd9Sstevel@tonic-gate case AT_SUN_RUID:
1327c478bd9Sstevel@tonic-gate /* real user id for the executable */
1337c478bd9Sstevel@tonic-gate uid = (uid_t)auxv->a_un.a_val;
1347c478bd9Sstevel@tonic-gate break;
1357c478bd9Sstevel@tonic-gate case AT_SUN_GID:
1367c478bd9Sstevel@tonic-gate /* effective group id for the executable */
1377c478bd9Sstevel@tonic-gate egid = (gid_t)auxv->a_un.a_val;
1387c478bd9Sstevel@tonic-gate break;
1397c478bd9Sstevel@tonic-gate case AT_SUN_RGID:
1407c478bd9Sstevel@tonic-gate /* real group id for the executable */
1417c478bd9Sstevel@tonic-gate gid = (gid_t)auxv->a_un.a_val;
1427c478bd9Sstevel@tonic-gate break;
1437c478bd9Sstevel@tonic-gate case AT_SUN_PLATFORM:
1447c478bd9Sstevel@tonic-gate /* platform name */
1457c478bd9Sstevel@tonic-gate _platform = auxv->a_un.a_ptr;
1467c478bd9Sstevel@tonic-gate break;
1477c478bd9Sstevel@tonic-gate case AT_SUN_EXECNAME:
1487c478bd9Sstevel@tonic-gate /* full pathname of execed object */
1497c478bd9Sstevel@tonic-gate _execname = auxv->a_un.a_ptr;
1507c478bd9Sstevel@tonic-gate break;
1517c478bd9Sstevel@tonic-gate case AT_SUN_AUXFLAGS:
15256deab07SRod Evans /* auxiliary flags */
1537c478bd9Sstevel@tonic-gate auxflags = (int)auxv->a_un.a_val;
1547c478bd9Sstevel@tonic-gate break;
1557c478bd9Sstevel@tonic-gate case AT_SUN_HWCAP:
15656deab07SRod Evans /* hardware capabilities */
157ebb8ac07SRobert Mustacchi hwcap[0] = (uint_t)auxv->a_un.a_val;
158ebb8ac07SRobert Mustacchi break;
159ebb8ac07SRobert Mustacchi case AT_SUN_HWCAP2:
160ebb8ac07SRobert Mustacchi /* hardware capabilities */
161ebb8ac07SRobert Mustacchi hwcap[1] = (uint_t)auxv->a_un.a_val;
1627c478bd9Sstevel@tonic-gate break;
163*56726c7eSRobert Mustacchi case AT_SUN_HWCAP3:
164*56726c7eSRobert Mustacchi /* hardware capabilities */
165*56726c7eSRobert Mustacchi hwcap[2] = (uint_t)auxv->a_un.a_val;
166*56726c7eSRobert Mustacchi break;
1679acbbeafSnn case AT_SUN_EMULATOR:
1689acbbeafSnn /* name of emulation library, if any */
1699acbbeafSnn _emulator = auxv->a_un.a_ptr;
1709acbbeafSnn break;
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate * Get needed info from ld.so's dynamic structure.
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate /* LINTED */
1787c478bd9Sstevel@tonic-gate dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
1797c478bd9Sstevel@tonic-gate for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
1807c478bd9Sstevel@tonic-gate switch (ld_dyn->d_tag) {
1817c478bd9Sstevel@tonic-gate case DT_REL:
1827c478bd9Sstevel@tonic-gate reladdr = ld_dyn->d_un.d_ptr + ld_base;
1837c478bd9Sstevel@tonic-gate break;
1847c478bd9Sstevel@tonic-gate case DT_RELCOUNT:
1857c478bd9Sstevel@tonic-gate relcount = ld_dyn->d_un.d_val;
1867c478bd9Sstevel@tonic-gate break;
1877c478bd9Sstevel@tonic-gate case DT_RELENT:
1887c478bd9Sstevel@tonic-gate relent = ld_dyn->d_un.d_val;
1897c478bd9Sstevel@tonic-gate break;
1907c478bd9Sstevel@tonic-gate case DT_STRTAB:
1917c478bd9Sstevel@tonic-gate strtab = ld_dyn->d_un.d_ptr + ld_base;
1927c478bd9Sstevel@tonic-gate break;
1937c478bd9Sstevel@tonic-gate case DT_SONAME:
1947c478bd9Sstevel@tonic-gate soname = ld_dyn->d_un.d_val;
1957c478bd9Sstevel@tonic-gate break;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate _rt_name = (char *)strtab + soname;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate /*
20156deab07SRod Evans * If we don't have a RELENT, just assume the size.
2027c478bd9Sstevel@tonic-gate */
2037c478bd9Sstevel@tonic-gate if (relent == 0)
2047c478bd9Sstevel@tonic-gate relent = sizeof (Rel);
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /*
20756deab07SRod Evans * As all global symbol references within ld.so.1 are protected
20856deab07SRod Evans * (symbolic), only RELATIVE and JMPSLOT relocations should be left
20956deab07SRod Evans * to process at runtime. Process all relative relocations now.
2107c478bd9Sstevel@tonic-gate */
2117c478bd9Sstevel@tonic-gate for (; relcount; relcount--) {
2127c478bd9Sstevel@tonic-gate ulong_t roffset;
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate roffset = ((Rel *)reladdr)->r_offset + ld_base;
2157c478bd9Sstevel@tonic-gate *((ulong_t *)roffset) += ld_base;
2167c478bd9Sstevel@tonic-gate reladdr += relent;
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate
2199acbbeafSnn /*
2209acbbeafSnn * If an emulation library is being used, use that as the linker's
2219acbbeafSnn * effective executable name. The real executable is not linked by this
2229acbbeafSnn * linker.
2239acbbeafSnn */
2249acbbeafSnn if (_emulator != NULL) {
2259acbbeafSnn _execname = _emulator;
2269acbbeafSnn rtld_flags2 |= RT_FL2_BRANDED;
2279acbbeafSnn }
2289acbbeafSnn
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate * Initialize the dyn_plt_ent_size field. It currently contains the
2317c478bd9Sstevel@tonic-gate * size of the dyn_plt_template. It still needs to be aligned and have
2327c478bd9Sstevel@tonic-gate * space for the 'dyn_data' area added.
2337c478bd9Sstevel@tonic-gate */
2347c478bd9Sstevel@tonic-gate dyn_plt_ent_size = ROUND(dyn_plt_ent_size, M_WORD_ALIGN) +
2357c478bd9Sstevel@tonic-gate sizeof (uintptr_t) + sizeof (uintptr_t) + sizeof (ulong_t) +
2367c478bd9Sstevel@tonic-gate sizeof (ulong_t) + sizeof (Sym);
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate * Continue with generic startup processing.
2407c478bd9Sstevel@tonic-gate */
24141072f3cSrie if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
24256deab07SRod Evans _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
243fec04708SRichard Lowe _execname, _argv, uid, euid, gid, egid, auxflags,
244ebb8ac07SRobert Mustacchi hwcap)) == NULL) {
2457c478bd9Sstevel@tonic-gate rtldexit(&lml_main, 1);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate return (LM_ENTRY_PT(lmp)());
2497c478bd9Sstevel@tonic-gate }
250