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
56812Sraf * Common Development and Distribution License (the "License").
66812Sraf * 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 */
216812Sraf
220Sstevel@tonic-gate /*
23*11798SRoger.Faulkner@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * Redirection ld.so. Based on the 4.x binary compatibility ld.so, used
290Sstevel@tonic-gate * to redirect aliases for ld.so to the real one.
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * Import data structures
340Sstevel@tonic-gate */
356812Sraf #include "lint.h"
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/mman.h>
380Sstevel@tonic-gate #include <sys/fcntl.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate #include <sys/sysconfig.h>
410Sstevel@tonic-gate #include <sys/auxv.h>
420Sstevel@tonic-gate #include <elf.h>
430Sstevel@tonic-gate #include <link.h>
440Sstevel@tonic-gate #include <string.h>
450Sstevel@tonic-gate #include "alias_boot.h"
460Sstevel@tonic-gate
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate * Local manifest constants and macros.
490Sstevel@tonic-gate */
500Sstevel@tonic-gate #define ALIGN(x, a) ((uintptr_t)(x) & ~((a) - 1))
510Sstevel@tonic-gate #define ROUND(x, a) (((uintptr_t)(x) + ((a) - 1)) & ~((a) - 1))
520Sstevel@tonic-gate
530Sstevel@tonic-gate #define EMPTY strings[EMPTY_S]
540Sstevel@tonic-gate #define LDSO strings[LDSO_S]
550Sstevel@tonic-gate #define ZERO strings[ZERO_S]
560Sstevel@tonic-gate #define CLOSE (*(funcs[CLOSE_F]))
57*11798SRoger.Faulkner@Sun.COM #define FSTATAT (*(funcs[FSTATAT_F]))
580Sstevel@tonic-gate #define MMAP (*(funcs[MMAP_F]))
590Sstevel@tonic-gate #define MUNMAP (*(funcs[MUNMAP_F]))
60*11798SRoger.Faulkner@Sun.COM #define OPENAT (*(funcs[OPENAT_F]))
610Sstevel@tonic-gate #define PANIC (*(funcs[PANIC_F]))
620Sstevel@tonic-gate #define SYSCONFIG (*(funcs[SYSCONFIG_F]))
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * Alias ld.so entry point -- receives a bootstrap structure and a vector
660Sstevel@tonic-gate * of strings. The vector is "well-known" to us, and consists of pointers
670Sstevel@tonic-gate * to string constants. This aliasing bootstrap requires no relocation in
680Sstevel@tonic-gate * order to run, save for the pointers of constant strings. This second
690Sstevel@tonic-gate * parameter provides this. Note that this program is carefully coded in
700Sstevel@tonic-gate * order to maintain the "no bootstrapping" requirement -- it calls only
710Sstevel@tonic-gate * local functions, uses no intrinsics, etc.
720Sstevel@tonic-gate */
730Sstevel@tonic-gate static void *
__rtld(Elf32_Boot * ebp,const char * strings[],int (* funcs[])())740Sstevel@tonic-gate __rtld(Elf32_Boot *ebp, const char *strings[], int (*funcs[])())
750Sstevel@tonic-gate {
760Sstevel@tonic-gate int i, p; /* working */
770Sstevel@tonic-gate long j; /* working */
780Sstevel@tonic-gate long page_size = 0; /* size of a page */
790Sstevel@tonic-gate const char *program_name = EMPTY; /* our name */
800Sstevel@tonic-gate int ldfd; /* fd assigned to ld.so */
810Sstevel@tonic-gate int dzfd = 0; /* fd assigned to /dev/zero */
820Sstevel@tonic-gate Elf32_Ehdr *ehdr; /* ELF header of ld.so */
830Sstevel@tonic-gate Elf32_Phdr *phdr; /* first Phdr in file */
840Sstevel@tonic-gate Elf32_Phdr *pptr; /* working Phdr */
850Sstevel@tonic-gate Elf32_Phdr *lph = NULL; /* last loadable Phdr */
860Sstevel@tonic-gate Elf32_Phdr *fph = NULL; /* first loadable Phdr */
870Sstevel@tonic-gate caddr_t maddr; /* pointer to mapping claim */
880Sstevel@tonic-gate Elf32_Off mlen; /* total mapping claim */
890Sstevel@tonic-gate caddr_t faddr; /* first program mapping of ld.so */
900Sstevel@tonic-gate Elf32_Off foff; /* file offset for segment mapping */
910Sstevel@tonic-gate Elf32_Off flen; /* file length for segment mapping */
920Sstevel@tonic-gate caddr_t addr; /* working mapping address */
930Sstevel@tonic-gate caddr_t zaddr; /* /dev/zero working mapping addr */
940Sstevel@tonic-gate struct stat sb; /* stat buffer for sizing */
950Sstevel@tonic-gate auxv_t *ap; /* working aux pointer */
960Sstevel@tonic-gate
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate * Discover things about our environment: auxiliary vector (if
990Sstevel@tonic-gate * any), arguments, program name, and the like.
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate while (ebp->eb_tag != NULL) {
1020Sstevel@tonic-gate switch (ebp->eb_tag) {
1030Sstevel@tonic-gate case EB_ARGV:
1040Sstevel@tonic-gate program_name = *((char **)ebp->eb_un.eb_ptr);
1050Sstevel@tonic-gate break;
1060Sstevel@tonic-gate case EB_AUXV:
1070Sstevel@tonic-gate for (ap = (auxv_t *)ebp->eb_un.eb_ptr;
1080Sstevel@tonic-gate ap->a_type != AT_NULL; ap++)
1090Sstevel@tonic-gate if (ap->a_type == AT_PAGESZ) {
1100Sstevel@tonic-gate page_size = ap->a_un.a_val;
1110Sstevel@tonic-gate break;
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate break;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate ebp++;
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate * If we didn't get a page size from looking in the auxiliary
1200Sstevel@tonic-gate * vector, we need to get one now.
1210Sstevel@tonic-gate */
1220Sstevel@tonic-gate if (page_size == 0) {
1230Sstevel@tonic-gate page_size = SYSCONFIG(_CONFIG_PAGESIZE);
1240Sstevel@tonic-gate ebp->eb_tag = EB_PAGESIZE, (ebp++)->eb_un.eb_val =
1250Sstevel@tonic-gate (Elf32_Word)page_size;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate * Map in the real ld.so. Note that we're mapping it as
1300Sstevel@tonic-gate * an ELF database, not as a program -- we just want to walk it's
1310Sstevel@tonic-gate * data structures. Further mappings will actually establish the
1320Sstevel@tonic-gate * program in the address space.
1330Sstevel@tonic-gate */
134*11798SRoger.Faulkner@Sun.COM if ((ldfd = OPENAT(AT_FDCWD, LDSO, O_RDONLY)) == -1)
1350Sstevel@tonic-gate PANIC(program_name);
136*11798SRoger.Faulkner@Sun.COM if (FSTATAT(ldfd, NULL, &sb, 0) == -1)
1370Sstevel@tonic-gate PANIC(program_name);
1380Sstevel@tonic-gate ehdr = (Elf32_Ehdr *)MMAP(0, sb.st_size, PROT_READ | PROT_EXEC,
1390Sstevel@tonic-gate MAP_SHARED, ldfd, 0);
1400Sstevel@tonic-gate if (ehdr == (Elf32_Ehdr *)-1)
1410Sstevel@tonic-gate PANIC(program_name);
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate * Validate the file we're looking at, ensure it has the correct
1450Sstevel@tonic-gate * ELF structures, such as: ELF magic numbers, coded for SPARC,
1460Sstevel@tonic-gate * is a ".so", etc.
1470Sstevel@tonic-gate */
1480Sstevel@tonic-gate if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1490Sstevel@tonic-gate ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1500Sstevel@tonic-gate ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1510Sstevel@tonic-gate ehdr->e_ident[EI_MAG3] != ELFMAG3)
1520Sstevel@tonic-gate PANIC(program_name);
1530Sstevel@tonic-gate if (ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
1540Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2MSB)
1550Sstevel@tonic-gate PANIC(program_name);
1560Sstevel@tonic-gate if (ehdr->e_type != ET_DYN)
1570Sstevel@tonic-gate PANIC(program_name);
1580Sstevel@tonic-gate if ((ehdr->e_machine != EM_SPARC) &&
1590Sstevel@tonic-gate (ehdr->e_machine != EM_SPARC32PLUS))
1600Sstevel@tonic-gate PANIC(program_name);
1610Sstevel@tonic-gate if (ehdr->e_version > EV_CURRENT)
1620Sstevel@tonic-gate PANIC(program_name);
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate * Point at program headers and start figuring out what to load.
1660Sstevel@tonic-gate */
1670Sstevel@tonic-gate phdr = (Elf32_Phdr *)((caddr_t)ehdr + ehdr->e_phoff);
1680Sstevel@tonic-gate for (p = 0, pptr = phdr; p < (int)ehdr->e_phnum; p++,
1690Sstevel@tonic-gate pptr = (Elf32_Phdr *)((caddr_t)pptr + ehdr->e_phentsize))
1700Sstevel@tonic-gate if (pptr->p_type == PT_LOAD) {
1710Sstevel@tonic-gate if (fph == 0) {
1720Sstevel@tonic-gate fph = pptr;
1730Sstevel@tonic-gate } else if (pptr->p_vaddr <= lph->p_vaddr)
1740Sstevel@tonic-gate PANIC(program_name);
1750Sstevel@tonic-gate lph = pptr;
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate * We'd better have at least one loadable segment.
1800Sstevel@tonic-gate */
1810Sstevel@tonic-gate if (fph == 0)
1820Sstevel@tonic-gate PANIC(program_name);
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate * Map enough address space to hold the program (as opposed to the
1860Sstevel@tonic-gate * file) represented by ld.so. The amount to be assigned is the
1870Sstevel@tonic-gate * range between the end of the last loadable segment and the
1880Sstevel@tonic-gate * beginning of the first PLUS the alignment of the first segment.
1890Sstevel@tonic-gate * mmap() can assign us any page-aligned address, but the relocations
1900Sstevel@tonic-gate * assume the alignments included in the program header. As an
1910Sstevel@tonic-gate * optimization, however, let's assume that mmap() will actually
1920Sstevel@tonic-gate * give us an aligned address -- since if it does, we can save
1930Sstevel@tonic-gate * an munmap() later on. If it doesn't -- then go try it again.
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate mlen = ROUND((lph->p_vaddr + lph->p_memsz) -
1960Sstevel@tonic-gate ALIGN(fph->p_vaddr, page_size), page_size);
1970Sstevel@tonic-gate maddr = (caddr_t)MMAP(0, mlen, PROT_READ | PROT_EXEC,
1980Sstevel@tonic-gate MAP_SHARED, ldfd, 0);
1990Sstevel@tonic-gate if (maddr == (caddr_t)-1)
2000Sstevel@tonic-gate PANIC(program_name);
2010Sstevel@tonic-gate faddr = (caddr_t)ROUND(maddr, fph->p_align);
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * Check to see whether alignment skew was really needed.
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate if (faddr != maddr) {
2070Sstevel@tonic-gate (void) MUNMAP(maddr, mlen);
2080Sstevel@tonic-gate mlen = ROUND((lph->p_vaddr + lph->p_memsz) -
2090Sstevel@tonic-gate ALIGN(fph->p_vaddr, fph->p_align) + fph->p_align,
2100Sstevel@tonic-gate page_size);
2110Sstevel@tonic-gate maddr = (caddr_t)MMAP(0, mlen, PROT_READ | PROT_EXEC,
2120Sstevel@tonic-gate MAP_SHARED, ldfd, 0);
2130Sstevel@tonic-gate if (maddr == (caddr_t)-1)
2140Sstevel@tonic-gate PANIC(program_name);
2150Sstevel@tonic-gate faddr = (caddr_t)ROUND(maddr, fph->p_align);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate * We have the address space reserved, so map each loadable segment.
2200Sstevel@tonic-gate */
2210Sstevel@tonic-gate for (p = 0, pptr = phdr; p < (int)ehdr->e_phnum; p++,
2220Sstevel@tonic-gate pptr = (Elf32_Phdr *)((caddr_t)pptr + ehdr->e_phentsize)) {
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate * Skip non-loadable segments or segments that don't occupy
2260Sstevel@tonic-gate * any memory.
2270Sstevel@tonic-gate */
2280Sstevel@tonic-gate if ((pptr->p_type != PT_LOAD) || (pptr->p_memsz == 0))
2290Sstevel@tonic-gate continue;
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate * Determine the file offset to which the mapping will
2330Sstevel@tonic-gate * directed (must be aligned) and how much to map (might
2340Sstevel@tonic-gate * be more than the file in the case of .bss.)
2350Sstevel@tonic-gate */
2360Sstevel@tonic-gate foff = ALIGN(pptr->p_offset, page_size);
2370Sstevel@tonic-gate flen = pptr->p_memsz + (pptr->p_offset - foff);
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate * Set address of this segment relative to our base.
2410Sstevel@tonic-gate */
2420Sstevel@tonic-gate addr = (caddr_t)ALIGN(faddr + pptr->p_vaddr, page_size);
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate * If this is the first program header, record our base
2460Sstevel@tonic-gate * address for later use.
2470Sstevel@tonic-gate */
2480Sstevel@tonic-gate if (pptr == phdr) {
2490Sstevel@tonic-gate ebp->eb_tag = EB_LDSO_BASE;
2500Sstevel@tonic-gate (ebp++)->eb_un.eb_ptr = (Elf32_Addr)addr;
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate /*
2540Sstevel@tonic-gate * Unmap anything from the last mapping address to this
2550Sstevel@tonic-gate * one.
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate if (addr - maddr) {
2580Sstevel@tonic-gate (void) MUNMAP(maddr, addr - maddr);
2590Sstevel@tonic-gate mlen -= addr - maddr;
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate * Determine the mapping protection from the section
2640Sstevel@tonic-gate * attributes.
2650Sstevel@tonic-gate */
2660Sstevel@tonic-gate i = 0;
2670Sstevel@tonic-gate if (pptr->p_flags & PF_R)
2680Sstevel@tonic-gate i |= PROT_READ;
2690Sstevel@tonic-gate if (pptr->p_flags & PF_W)
2700Sstevel@tonic-gate i |= PROT_WRITE;
2710Sstevel@tonic-gate if (pptr->p_flags & PF_X)
2720Sstevel@tonic-gate i |= PROT_EXEC;
2730Sstevel@tonic-gate if ((caddr_t)MMAP((caddr_t)addr, flen, i,
2740Sstevel@tonic-gate MAP_FIXED | MAP_PRIVATE, ldfd, foff) == (caddr_t)-1)
2750Sstevel@tonic-gate PANIC(program_name);
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate * If the memory occupancy of the segment overflows the
2790Sstevel@tonic-gate * definition in the file, we need to "zero out" the
2800Sstevel@tonic-gate * end of the mapping we've established, and if necessary,
2810Sstevel@tonic-gate * map some more space from /dev/zero.
2820Sstevel@tonic-gate */
2830Sstevel@tonic-gate if (pptr->p_memsz > pptr->p_filesz) {
2840Sstevel@tonic-gate foff = (uintptr_t)faddr + pptr->p_vaddr +
2856812Sraf pptr->p_filesz;
2860Sstevel@tonic-gate zaddr = (caddr_t)ROUND(foff, page_size);
2870Sstevel@tonic-gate for (j = 0; j < (int)(zaddr - foff); j++)
2880Sstevel@tonic-gate *((char *)foff + j) = 0;
2890Sstevel@tonic-gate j = (faddr + pptr->p_vaddr + pptr->p_memsz) - zaddr;
2900Sstevel@tonic-gate if (j > 0) {
2910Sstevel@tonic-gate if (dzfd == 0) {
292*11798SRoger.Faulkner@Sun.COM dzfd = OPENAT(AT_FDCWD, ZERO, O_RDWR);
2930Sstevel@tonic-gate if (dzfd == -1)
2940Sstevel@tonic-gate PANIC(program_name);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate if ((caddr_t)MMAP((caddr_t)zaddr, j, i,
2970Sstevel@tonic-gate MAP_FIXED | MAP_PRIVATE, dzfd,
2980Sstevel@tonic-gate 0) == (caddr_t)-1)
2990Sstevel@tonic-gate PANIC(program_name);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate * Update the mapping claim pointer.
3050Sstevel@tonic-gate */
3060Sstevel@tonic-gate maddr = addr + ROUND(flen, page_size);
3070Sstevel@tonic-gate mlen -= maddr - addr;
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate * Unmap any final reservation.
3120Sstevel@tonic-gate */
3130Sstevel@tonic-gate if (mlen != 0)
3140Sstevel@tonic-gate (void) MUNMAP(maddr, mlen);
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate * Clean up file descriptor space we've consumed. Pass along
3180Sstevel@tonic-gate * the /dev/zero file descriptor we got -- every cycle counts.
3190Sstevel@tonic-gate */
3200Sstevel@tonic-gate (void) CLOSE(ldfd);
3210Sstevel@tonic-gate if (dzfd != 0)
3220Sstevel@tonic-gate ebp->eb_tag = EB_DEVZERO, (ebp++)->eb_un.eb_val = dzfd;
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate * The call itself. Note that we start 1 instruction word in.
3260Sstevel@tonic-gate * The ELF ld.so contains an "entry vector" of branch instructions,
3270Sstevel@tonic-gate * which, for our interest are:
3280Sstevel@tonic-gate * +0: ba, a <normal startup>
3290Sstevel@tonic-gate * +4: ba, a <compatibility startup>
3300Sstevel@tonic-gate * +8: ba, a <alias startup>
3310Sstevel@tonic-gate * By starting at the alias startup, the ELF ld.so knows
3320Sstevel@tonic-gate * that a pointer to "eb" is available to it and further knows
3330Sstevel@tonic-gate * how to calculate the offset to the program's arguments and
3340Sstevel@tonic-gate * other structures. We do the "call" by returning to our
3350Sstevel@tonic-gate * bootstrap and then jumping to the address that we return.
3360Sstevel@tonic-gate */
3370Sstevel@tonic-gate ebp->eb_tag = EB_NULL, ebp->eb_un.eb_val = 0;
3380Sstevel@tonic-gate return ((void *)(ehdr->e_entry + faddr + 8));
3390Sstevel@tonic-gate }
340