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 */
26ebb8ac07SRobert Mustacchi /*
27d0158222SRobert Mustacchi * Copyright (c) 2018, Joyent, Inc.
28*56726c7eSRobert Mustacchi * Copyright 2022 Oxide Computer Company
29ebb8ac07SRobert Mustacchi */
307257d1b4Sraf
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * amd64 specific setup routine - relocate ld.so's symbols, setup its
337c478bd9Sstevel@tonic-gate * environment, map in loadable sections of the executable.
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * Takes base address ld.so was loaded at, address of ld.so's dynamic
367c478bd9Sstevel@tonic-gate * structure, address of process environment pointers, address of auxiliary
377c478bd9Sstevel@tonic-gate * vector and * argv[0] (process name).
387c478bd9Sstevel@tonic-gate * If errors occur, send process signal - otherwise
397c478bd9Sstevel@tonic-gate * return executable's entry point to the bootstrap routine.
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <link.h>
487c478bd9Sstevel@tonic-gate #include <dlfcn.h>
497c478bd9Sstevel@tonic-gate #include "_rtld.h"
507c478bd9Sstevel@tonic-gate #include "_audit.h"
517c478bd9Sstevel@tonic-gate #include "msg.h"
527c478bd9Sstevel@tonic-gate
53d0158222SRobert Mustacchi /*
54d0158222SRobert Mustacchi * Number of bytes to save for register usage.
55d0158222SRobert Mustacchi */
56d0158222SRobert Mustacchi uint_t _plt_save_size;
57d0158222SRobert Mustacchi void (*_plt_fp_save)(void *);
58d0158222SRobert Mustacchi void (*_plt_fp_restore)(void *);
59d0158222SRobert Mustacchi
60d0158222SRobert Mustacchi extern void _elf_rtbndr_fp_save_orig(void *);
61d0158222SRobert Mustacchi extern void _elf_rtbndr_fp_restore_orig(void *);
62d0158222SRobert Mustacchi extern void _elf_rtbndr_fp_fxsave(void *);
63d0158222SRobert Mustacchi extern void _elf_rtbndr_fp_fxrestore(void *);
64d0158222SRobert Mustacchi extern void _elf_rtbndr_fp_xsave(void *);
65d0158222SRobert Mustacchi extern void _elf_rtbndr_fp_xrestore(void *);
66d0158222SRobert Mustacchi
67d0158222SRobert Mustacchi /*
68d0158222SRobert Mustacchi * Based on what the kernel has told us, go through and set up the various
69d0158222SRobert Mustacchi * pointers that we'll need for elf_rtbndr for the FPU.
70d0158222SRobert Mustacchi */
71d0158222SRobert Mustacchi static void
_setup_plt_fpu(int kind,size_t len)72d0158222SRobert Mustacchi _setup_plt_fpu(int kind, size_t len)
73d0158222SRobert Mustacchi {
74d0158222SRobert Mustacchi /*
75d0158222SRobert Mustacchi * If we didn't get a length for some reason, fall back to the old
76d0158222SRobert Mustacchi * implementation.
77d0158222SRobert Mustacchi */
78d0158222SRobert Mustacchi if (len == 0)
79d0158222SRobert Mustacchi kind = -1;
80d0158222SRobert Mustacchi
81d0158222SRobert Mustacchi switch (kind) {
82d0158222SRobert Mustacchi case AT_386_FPINFO_FXSAVE:
83d0158222SRobert Mustacchi _plt_fp_save = _elf_rtbndr_fp_fxsave;
84d0158222SRobert Mustacchi _plt_fp_restore = _elf_rtbndr_fp_fxrestore;
85d0158222SRobert Mustacchi _plt_save_size = len;
86d0158222SRobert Mustacchi break;
87d0158222SRobert Mustacchi /*
88d0158222SRobert Mustacchi * We can treat processors that don't correctly handle the exception
89d0158222SRobert Mustacchi * information in xsave the same way we do others. The information
90d0158222SRobert Mustacchi * that may or may not be properly saved and restored should not be
91d0158222SRobert Mustacchi * relevant to us because of the ABI.
92d0158222SRobert Mustacchi */
93d0158222SRobert Mustacchi case AT_386_FPINFO_XSAVE:
94d0158222SRobert Mustacchi case AT_386_FPINFO_XSAVE_AMD:
95d0158222SRobert Mustacchi _plt_fp_save = _elf_rtbndr_fp_xsave;
96d0158222SRobert Mustacchi _plt_fp_restore = _elf_rtbndr_fp_xrestore;
97d0158222SRobert Mustacchi _plt_save_size = len;
98d0158222SRobert Mustacchi break;
99d0158222SRobert Mustacchi default:
100d0158222SRobert Mustacchi _plt_fp_save = _elf_rtbndr_fp_save_orig;
101d0158222SRobert Mustacchi _plt_fp_restore = _elf_rtbndr_fp_restore_orig;
102d0158222SRobert Mustacchi /*
103d0158222SRobert Mustacchi * The ABI says that 8 floating point registers are used for
104d0158222SRobert Mustacchi * passing arguments (%xmm0 through %xmm7). Because these
105d0158222SRobert Mustacchi * registers on some platforms may shadow the %ymm and %zmm
106d0158222SRobert Mustacchi * registers, we end up needing to size this for the maximally
107d0158222SRobert Mustacchi * sized register we care about, a 512-bit (64-byte) zmm
108d0158222SRobert Mustacchi * register.
109d0158222SRobert Mustacchi */
110d0158222SRobert Mustacchi _plt_save_size = 64 * 8;
111d0158222SRobert Mustacchi break;
112d0158222SRobert Mustacchi }
113d0158222SRobert Mustacchi }
114d0158222SRobert Mustacchi
1157c478bd9Sstevel@tonic-gate /* VARARGS */
1167c478bd9Sstevel@tonic-gate unsigned long
_setup(Boot * ebp,Dyn * ld_dyn)11756deab07SRod Evans _setup(Boot *ebp, Dyn *ld_dyn)
1187c478bd9Sstevel@tonic-gate {
11956deab07SRod Evans ulong_t reladdr, relacount, ld_base = 0;
12056deab07SRod Evans ulong_t relaent = 0, pltrelsz = 0;
12156deab07SRod Evans ulong_t strtab, soname, interp_base = 0;
1227c478bd9Sstevel@tonic-gate char *_rt_name, **_envp, **_argv;
12356deab07SRod Evans int _syspagsz = 0, fd = -1;
124ebb8ac07SRobert Mustacchi uint_t _flags = 0;
125*56726c7eSRobert Mustacchi uint_t hwcap[3] = { 0, 0, 0 };
12656deab07SRod Evans Dyn *dyn_ptr;
12756deab07SRod Evans Phdr *phdr = NULL;
12856deab07SRod Evans Rt_map *lmp;
1297c478bd9Sstevel@tonic-gate auxv_t *auxv, *_auxv;
130f48205beScasper uid_t uid = (uid_t)-1, euid = (uid_t)-1;
131f48205beScasper gid_t gid = (gid_t)-1, egid = (gid_t)-1;
13256deab07SRod Evans char *_platform = NULL, *_execname = NULL, *_emulator = NULL;
133d0158222SRobert Mustacchi int auxflags = -1, fpkind = -1;
134d0158222SRobert Mustacchi size_t fpsize = 0;
13556deab07SRod Evans
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate * Scan the bootstrap structure to pick up the basics.
1387c478bd9Sstevel@tonic-gate */
1397c478bd9Sstevel@tonic-gate for (; ebp->eb_tag != EB_NULL; ebp++)
1407c478bd9Sstevel@tonic-gate switch (ebp->eb_tag) {
1417c478bd9Sstevel@tonic-gate case EB_LDSO_BASE:
1427c478bd9Sstevel@tonic-gate ld_base = (unsigned long)ebp->eb_un.eb_val;
1437c478bd9Sstevel@tonic-gate break;
1447c478bd9Sstevel@tonic-gate case EB_ARGV:
1457c478bd9Sstevel@tonic-gate _argv = (char **)ebp->eb_un.eb_ptr;
1467c478bd9Sstevel@tonic-gate break;
1477c478bd9Sstevel@tonic-gate case EB_ENVP:
1487c478bd9Sstevel@tonic-gate _envp = (char **)ebp->eb_un.eb_ptr;
1497c478bd9Sstevel@tonic-gate break;
1507c478bd9Sstevel@tonic-gate case EB_AUXV:
1517c478bd9Sstevel@tonic-gate _auxv = (auxv_t *)ebp->eb_un.eb_ptr;
1527c478bd9Sstevel@tonic-gate break;
1537c478bd9Sstevel@tonic-gate case EB_PAGESIZE:
1547c478bd9Sstevel@tonic-gate _syspagsz = (int)ebp->eb_un.eb_val;
1557c478bd9Sstevel@tonic-gate break;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * Search the aux. vector for the information passed by exec.
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
1627c478bd9Sstevel@tonic-gate switch (auxv->a_type) {
1637c478bd9Sstevel@tonic-gate case AT_EXECFD:
1647c478bd9Sstevel@tonic-gate /* this is the old exec that passes a file descriptor */
1657c478bd9Sstevel@tonic-gate fd = (int)auxv->a_un.a_val;
1667c478bd9Sstevel@tonic-gate break;
1677c478bd9Sstevel@tonic-gate case AT_FLAGS:
1687c478bd9Sstevel@tonic-gate /* processor flags (MAU available, etc) */
1697c478bd9Sstevel@tonic-gate _flags = auxv->a_un.a_val;
1707c478bd9Sstevel@tonic-gate break;
1717c478bd9Sstevel@tonic-gate case AT_PAGESZ:
1727c478bd9Sstevel@tonic-gate /* system page size */
1737c478bd9Sstevel@tonic-gate _syspagsz = (int)auxv->a_un.a_val;
1747c478bd9Sstevel@tonic-gate break;
1757c478bd9Sstevel@tonic-gate case AT_PHDR:
1767c478bd9Sstevel@tonic-gate /* address of the segment table */
1777c478bd9Sstevel@tonic-gate phdr = (Phdr *)auxv->a_un.a_ptr;
1787c478bd9Sstevel@tonic-gate break;
1797c478bd9Sstevel@tonic-gate case AT_BASE:
1807c478bd9Sstevel@tonic-gate /* interpreter base address */
1817c478bd9Sstevel@tonic-gate if (ld_base == 0)
1827c478bd9Sstevel@tonic-gate ld_base = auxv->a_un.a_val;
1837c478bd9Sstevel@tonic-gate interp_base = auxv->a_un.a_val;
1847c478bd9Sstevel@tonic-gate break;
1857c478bd9Sstevel@tonic-gate case AT_SUN_UID:
1867c478bd9Sstevel@tonic-gate /* effective user id for the executable */
1877c478bd9Sstevel@tonic-gate euid = (uid_t)auxv->a_un.a_val;
1887c478bd9Sstevel@tonic-gate break;
1897c478bd9Sstevel@tonic-gate case AT_SUN_RUID:
1907c478bd9Sstevel@tonic-gate /* real user id for the executable */
1917c478bd9Sstevel@tonic-gate uid = (uid_t)auxv->a_un.a_val;
1927c478bd9Sstevel@tonic-gate break;
1937c478bd9Sstevel@tonic-gate case AT_SUN_GID:
1947c478bd9Sstevel@tonic-gate /* effective group id for the executable */
1957c478bd9Sstevel@tonic-gate egid = (gid_t)auxv->a_un.a_val;
1967c478bd9Sstevel@tonic-gate break;
1977c478bd9Sstevel@tonic-gate case AT_SUN_RGID:
1987c478bd9Sstevel@tonic-gate /* real group id for the executable */
1997c478bd9Sstevel@tonic-gate gid = (gid_t)auxv->a_un.a_val;
2007c478bd9Sstevel@tonic-gate break;
2017c478bd9Sstevel@tonic-gate case AT_SUN_PLATFORM:
2027c478bd9Sstevel@tonic-gate /* platform name */
2037c478bd9Sstevel@tonic-gate _platform = auxv->a_un.a_ptr;
2047c478bd9Sstevel@tonic-gate break;
2057c478bd9Sstevel@tonic-gate case AT_SUN_EXECNAME:
2067c478bd9Sstevel@tonic-gate /* full pathname of execed object */
2077c478bd9Sstevel@tonic-gate _execname = auxv->a_un.a_ptr;
2087c478bd9Sstevel@tonic-gate break;
2097c478bd9Sstevel@tonic-gate case AT_SUN_AUXFLAGS:
21056deab07SRod Evans /* auxiliary flags */
2117c478bd9Sstevel@tonic-gate auxflags = (int)auxv->a_un.a_val;
2127c478bd9Sstevel@tonic-gate break;
2137c478bd9Sstevel@tonic-gate case AT_SUN_HWCAP:
21456deab07SRod Evans /* hardware capabilities */
215ebb8ac07SRobert Mustacchi hwcap[0] = (uint_t)auxv->a_un.a_val;
216ebb8ac07SRobert Mustacchi break;
217ebb8ac07SRobert Mustacchi case AT_SUN_HWCAP2:
218ebb8ac07SRobert Mustacchi /* hardware capabilities */
219ebb8ac07SRobert Mustacchi hwcap[1] = (uint_t)auxv->a_un.a_val;
2207c478bd9Sstevel@tonic-gate break;
221*56726c7eSRobert Mustacchi case AT_SUN_HWCAP3:
222*56726c7eSRobert Mustacchi /* hardware capabilities */
223*56726c7eSRobert Mustacchi hwcap[2] = (uint_t)auxv->a_un.a_val;
224*56726c7eSRobert Mustacchi break;
2259acbbeafSnn case AT_SUN_EMULATOR:
2269acbbeafSnn /* name of emulation library, if any */
2279acbbeafSnn _emulator = auxv->a_un.a_ptr;
2289acbbeafSnn break;
229d0158222SRobert Mustacchi case AT_SUN_FPTYPE:
230d0158222SRobert Mustacchi fpkind = (int)auxv->a_un.a_val;
231d0158222SRobert Mustacchi break;
232d0158222SRobert Mustacchi case AT_SUN_FPSIZE:
233d0158222SRobert Mustacchi fpsize = (size_t)auxv->a_un.a_val;
234d0158222SRobert Mustacchi break;
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate * Get needed info from ld.so's dynamic structure.
2407c478bd9Sstevel@tonic-gate */
2417c478bd9Sstevel@tonic-gate /* LINTED */
2427c478bd9Sstevel@tonic-gate dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
2437c478bd9Sstevel@tonic-gate for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
2447c478bd9Sstevel@tonic-gate switch (ld_dyn->d_tag) {
2457c478bd9Sstevel@tonic-gate case DT_RELA:
2467c478bd9Sstevel@tonic-gate reladdr = ld_dyn->d_un.d_ptr + ld_base;
2477c478bd9Sstevel@tonic-gate break;
2487c478bd9Sstevel@tonic-gate case DT_RELACOUNT:
2497c478bd9Sstevel@tonic-gate relacount = ld_dyn->d_un.d_val;
2507c478bd9Sstevel@tonic-gate break;
2517c478bd9Sstevel@tonic-gate case DT_RELAENT:
2527c478bd9Sstevel@tonic-gate relaent = ld_dyn->d_un.d_val;
2537c478bd9Sstevel@tonic-gate break;
2547c478bd9Sstevel@tonic-gate case DT_PLTRELSZ:
2557c478bd9Sstevel@tonic-gate pltrelsz = ld_dyn->d_un.d_val;
2567c478bd9Sstevel@tonic-gate break;
2577c478bd9Sstevel@tonic-gate case DT_STRTAB:
2587c478bd9Sstevel@tonic-gate strtab = ld_dyn->d_un.d_ptr + ld_base;
2597c478bd9Sstevel@tonic-gate break;
2607c478bd9Sstevel@tonic-gate case DT_SONAME:
2617c478bd9Sstevel@tonic-gate soname = ld_dyn->d_un.d_val;
2627c478bd9Sstevel@tonic-gate break;
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate _rt_name = (char *)strtab + soname;
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate /*
26856deab07SRod Evans * If we don't have a RELAENT, just assume the size.
2697c478bd9Sstevel@tonic-gate */
2707c478bd9Sstevel@tonic-gate if (relaent == 0)
2717c478bd9Sstevel@tonic-gate relaent = sizeof (Rela);
27256deab07SRod Evans
2737c478bd9Sstevel@tonic-gate /*
27456deab07SRod Evans * As all global symbol references within ld.so.1 are protected
27556deab07SRod Evans * (symbolic), only RELATIVE and JMPSLOT relocations should be left
27656deab07SRod Evans * to process at runtime. Process all relocations now.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate relacount += (pltrelsz / relaent);
2797c478bd9Sstevel@tonic-gate for (; relacount; relacount--) {
2807c478bd9Sstevel@tonic-gate ulong_t roffset;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate roffset = ((Rela *)reladdr)->r_offset + ld_base;
2837c478bd9Sstevel@tonic-gate *((ulong_t *)roffset) += ld_base +
2847c478bd9Sstevel@tonic-gate ((Rela *)reladdr)->r_addend;
2857c478bd9Sstevel@tonic-gate reladdr += relaent;
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate
28807678296Ssl /*
28907678296Ssl * If an emulation library is being used, use that as the linker's
29007678296Ssl * effective executable name. The real executable is not linked by this
29107678296Ssl * linker.
29207678296Ssl */
2939acbbeafSnn if (_emulator != NULL) {
29407678296Ssl _execname = _emulator;
2959acbbeafSnn rtld_flags2 |= RT_FL2_BRANDED;
2969acbbeafSnn }
2979acbbeafSnn
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate * Initialize the dyn_plt_ent_size field. It currently contains the
3007c478bd9Sstevel@tonic-gate * size of the dyn_plt_template. It still needs to be aligned and have
3017c478bd9Sstevel@tonic-gate * space for the 'dyn_data' area added.
3027c478bd9Sstevel@tonic-gate */
3037c478bd9Sstevel@tonic-gate dyn_plt_ent_size = ROUND(dyn_plt_ent_size, M_WORD_ALIGN) +
3047c478bd9Sstevel@tonic-gate sizeof (uintptr_t) + sizeof (uintptr_t) + sizeof (ulong_t) +
3057c478bd9Sstevel@tonic-gate sizeof (ulong_t) + sizeof (Sym);
3067c478bd9Sstevel@tonic-gate
307d0158222SRobert Mustacchi /*
308d0158222SRobert Mustacchi * Initialize the amd64 specific PLT relocation constants based on the
309d0158222SRobert Mustacchi * FP information that we have.
310d0158222SRobert Mustacchi */
311d0158222SRobert Mustacchi _setup_plt_fpu(fpkind, fpsize);
312d0158222SRobert Mustacchi
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate * Continue with generic startup processing.
3157c478bd9Sstevel@tonic-gate */
31641072f3cSrie if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
31756deab07SRod Evans _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
318fec04708SRichard Lowe _execname, _argv, uid, euid, gid, egid, auxflags,
319ebb8ac07SRobert Mustacchi hwcap)) == NULL) {
3207c478bd9Sstevel@tonic-gate rtldexit(&lml_main, 1);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate return (LM_ENTRY_PT(lmp)());
3247c478bd9Sstevel@tonic-gate }
325