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