1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gate /* 25*0Sstevel@tonic-gate * Redirection ld.so. Based on the 4.x binary compatibility ld.so, used 26*0Sstevel@tonic-gate * to redirect aliases for ld.so to the real one. 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * Copyright (c) 1990, 1991, 2001 by Sun Microsystems, Inc. 31*0Sstevel@tonic-gate * All rights reserved. 32*0Sstevel@tonic-gate */ 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate /* 35*0Sstevel@tonic-gate * Import data structures 36*0Sstevel@tonic-gate */ 37*0Sstevel@tonic-gate #include <sys/types.h> 38*0Sstevel@tonic-gate #include <sys/mman.h> 39*0Sstevel@tonic-gate #include <sys/fcntl.h> 40*0Sstevel@tonic-gate #include <sys/stat.h> 41*0Sstevel@tonic-gate #include <sys/sysconfig.h> 42*0Sstevel@tonic-gate #include <sys/auxv.h> 43*0Sstevel@tonic-gate #include <elf.h> 44*0Sstevel@tonic-gate #include <link.h> 45*0Sstevel@tonic-gate #include <string.h> 46*0Sstevel@tonic-gate #include "alias_boot.h" 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate /* 49*0Sstevel@tonic-gate * Local manifest constants and macros. 50*0Sstevel@tonic-gate */ 51*0Sstevel@tonic-gate #define ALIGN(x, a) ((int)(x) & ~((int)(a) - 1)) 52*0Sstevel@tonic-gate #define ROUND(x, a) (((int)(x) + ((int)(a) - 1)) & \ 53*0Sstevel@tonic-gate ~((int)(a) - 1)) 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate #define EMPTY strings[EMPTY_S] 56*0Sstevel@tonic-gate #define LDSO strings[LDSO_S] 57*0Sstevel@tonic-gate #define ZERO strings[ZERO_S] 58*0Sstevel@tonic-gate #define CLOSE (*(funcs[CLOSE_F])) 59*0Sstevel@tonic-gate #define FSTAT (*(funcs[FSTAT_F])) 60*0Sstevel@tonic-gate #define MMAP (*(funcs[MMAP_F])) 61*0Sstevel@tonic-gate #define MUNMAP (*(funcs[MUNMAP_F])) 62*0Sstevel@tonic-gate #define OPEN (*(funcs[OPEN_F])) 63*0Sstevel@tonic-gate #define PANIC (*(funcs[PANIC_F])) 64*0Sstevel@tonic-gate #define SYSCONFIG (*(funcs[SYSCONFIG_F])) 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate #include <link.h> 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate /* 69*0Sstevel@tonic-gate * Alias ld.so entry point -- receives a bootstrap structure and a vector 70*0Sstevel@tonic-gate * of strings. The vector is "well-known" to us, and consists of pointers 71*0Sstevel@tonic-gate * to string constants. This aliasing bootstrap requires no relocation in 72*0Sstevel@tonic-gate * order to run, save for the pointers of constant strings. This second 73*0Sstevel@tonic-gate * parameter provides this. Note that this program is carefully coded in 74*0Sstevel@tonic-gate * order to maintain the "no bootstrapping" requirement -- it calls only 75*0Sstevel@tonic-gate * local functions, uses no intrinsics, etc. 76*0Sstevel@tonic-gate */ 77*0Sstevel@tonic-gate void * 78*0Sstevel@tonic-gate __rtld(Elf32_Boot *ebp, const char *strings[], int (*funcs[])()) 79*0Sstevel@tonic-gate { 80*0Sstevel@tonic-gate int i, j, p; /* working */ 81*0Sstevel@tonic-gate int page_size = 0; /* size of a page */ 82*0Sstevel@tonic-gate const char *program_name = EMPTY; /* our name */ 83*0Sstevel@tonic-gate int ldfd; /* fd assigned to ld.so */ 84*0Sstevel@tonic-gate int dzfd = 0; /* fd assigned to /dev/zero */ 85*0Sstevel@tonic-gate Elf32_Ehdr *ehdr; /* ELF header of ld.so */ 86*0Sstevel@tonic-gate Elf32_Phdr *phdr; /* first Phdr in file */ 87*0Sstevel@tonic-gate Elf32_Phdr *pptr; /* working Phdr */ 88*0Sstevel@tonic-gate Elf32_Phdr *lph; /* last loadable Phdr */ 89*0Sstevel@tonic-gate Elf32_Phdr *fph = 0; /* first loadable Phdr */ 90*0Sstevel@tonic-gate caddr_t maddr; /* pointer to mapping claim */ 91*0Sstevel@tonic-gate Elf32_Off mlen; /* total mapping claim */ 92*0Sstevel@tonic-gate caddr_t faddr; /* first program mapping of ld.so */ 93*0Sstevel@tonic-gate Elf32_Off foff; /* file offset for segment mapping */ 94*0Sstevel@tonic-gate Elf32_Off flen; /* file length for segment mapping */ 95*0Sstevel@tonic-gate caddr_t addr; /* working mapping address */ 96*0Sstevel@tonic-gate caddr_t zaddr; /* /dev/zero working mapping addr */ 97*0Sstevel@tonic-gate struct stat sb; /* stat buffer for sizing */ 98*0Sstevel@tonic-gate auxv_t *ap; /* working aux pointer */ 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate /* 101*0Sstevel@tonic-gate * Discover things about our environment: auxiliary vector (if 102*0Sstevel@tonic-gate * any), arguments, program name, and the like. 103*0Sstevel@tonic-gate */ 104*0Sstevel@tonic-gate while (ebp->eb_tag != NULL) { 105*0Sstevel@tonic-gate switch (ebp->eb_tag) { 106*0Sstevel@tonic-gate case EB_ARGV: 107*0Sstevel@tonic-gate program_name = *((char **)ebp->eb_un.eb_ptr); 108*0Sstevel@tonic-gate break; 109*0Sstevel@tonic-gate case EB_AUXV: 110*0Sstevel@tonic-gate for (ap = (auxv_t *)ebp->eb_un.eb_ptr; 111*0Sstevel@tonic-gate ap->a_type != AT_NULL; ap++) 112*0Sstevel@tonic-gate if (ap->a_type == AT_PAGESZ) { 113*0Sstevel@tonic-gate page_size = ap->a_un.a_val; 114*0Sstevel@tonic-gate break; 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate break; 117*0Sstevel@tonic-gate } 118*0Sstevel@tonic-gate ebp++; 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate /* 122*0Sstevel@tonic-gate * If we didn't get a page size from looking in the auxiliary 123*0Sstevel@tonic-gate * vector, we need to get one now. 124*0Sstevel@tonic-gate */ 125*0Sstevel@tonic-gate if (page_size == 0) { 126*0Sstevel@tonic-gate page_size = SYSCONFIG(_CONFIG_PAGESIZE); 127*0Sstevel@tonic-gate ebp->eb_tag = EB_PAGESIZE, (ebp++)->eb_un.eb_val = 128*0Sstevel@tonic-gate (Elf32_Word)page_size; 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate /* 132*0Sstevel@tonic-gate * Map in the real ld.so. Note that we're mapping it as 133*0Sstevel@tonic-gate * an ELF database, not as a program -- we just want to walk it's 134*0Sstevel@tonic-gate * data structures. Further mappings will actually establish the 135*0Sstevel@tonic-gate * program in the address space. 136*0Sstevel@tonic-gate */ 137*0Sstevel@tonic-gate if ((ldfd = OPEN(LDSO, O_RDONLY)) == -1) 138*0Sstevel@tonic-gate PANIC(program_name); 139*0Sstevel@tonic-gate /* NEEDSWORK (temp kludge to use xstat so we can run on G6) */ 140*0Sstevel@tonic-gate if (FSTAT(2, ldfd, &sb) == -1) 141*0Sstevel@tonic-gate PANIC(program_name); 142*0Sstevel@tonic-gate ehdr = (Elf32_Ehdr *)MMAP(0, sb.st_size, PROT_READ | PROT_EXEC, 143*0Sstevel@tonic-gate MAP_SHARED, ldfd, 0); 144*0Sstevel@tonic-gate if (ehdr == (Elf32_Ehdr *)-1) 145*0Sstevel@tonic-gate PANIC(program_name); 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate /* 148*0Sstevel@tonic-gate * Validate the file we're looking at, ensure it has the correct 149*0Sstevel@tonic-gate * ELF structures, such as: ELF magic numbers, coded for 386, 150*0Sstevel@tonic-gate * is a ".so", etc. 151*0Sstevel@tonic-gate */ 152*0Sstevel@tonic-gate if (ehdr->e_ident[EI_MAG0] != ELFMAG0 || 153*0Sstevel@tonic-gate ehdr->e_ident[EI_MAG1] != ELFMAG1 || 154*0Sstevel@tonic-gate ehdr->e_ident[EI_MAG2] != ELFMAG2 || 155*0Sstevel@tonic-gate ehdr->e_ident[EI_MAG3] != ELFMAG3) 156*0Sstevel@tonic-gate PANIC(program_name); 157*0Sstevel@tonic-gate if (ehdr->e_ident[EI_CLASS] != ELFCLASS32 || 158*0Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2LSB) 159*0Sstevel@tonic-gate PANIC(program_name); 160*0Sstevel@tonic-gate if (ehdr->e_type != ET_DYN) 161*0Sstevel@tonic-gate PANIC(program_name); 162*0Sstevel@tonic-gate if (ehdr->e_machine != EM_386) 163*0Sstevel@tonic-gate PANIC(program_name); 164*0Sstevel@tonic-gate if (ehdr->e_version > EV_CURRENT) 165*0Sstevel@tonic-gate PANIC(program_name); 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate /* 168*0Sstevel@tonic-gate * Point at program headers and start figuring out what to load. 169*0Sstevel@tonic-gate */ 170*0Sstevel@tonic-gate phdr = (Elf32_Phdr *)((caddr_t)ehdr + ehdr->e_phoff); 171*0Sstevel@tonic-gate for (p = 0, pptr = phdr; p < (int)ehdr->e_phnum; p++, 172*0Sstevel@tonic-gate pptr = (Elf32_Phdr *)((caddr_t)pptr + ehdr->e_phentsize)) 173*0Sstevel@tonic-gate if (pptr->p_type == PT_LOAD) { 174*0Sstevel@tonic-gate if (fph == 0) { 175*0Sstevel@tonic-gate fph = pptr; 176*0Sstevel@tonic-gate } else if (pptr->p_vaddr <= lph->p_vaddr) 177*0Sstevel@tonic-gate PANIC(program_name); 178*0Sstevel@tonic-gate lph = pptr; 179*0Sstevel@tonic-gate } 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate /* 182*0Sstevel@tonic-gate * We'd better have at least one loadable segment. 183*0Sstevel@tonic-gate */ 184*0Sstevel@tonic-gate if (fph == 0) 185*0Sstevel@tonic-gate PANIC(program_name); 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gate /* 188*0Sstevel@tonic-gate * Map enough address space to hold the program (as opposed to the 189*0Sstevel@tonic-gate * file) represented by ld.so. The amount to be assigned is the 190*0Sstevel@tonic-gate * range between the end of the last loadable segment and the 191*0Sstevel@tonic-gate * beginning of the first PLUS the alignment of the first segment. 192*0Sstevel@tonic-gate * mmap() can assign us any page-aligned address, but the relocations 193*0Sstevel@tonic-gate * assume the alignments included in the program header. As an 194*0Sstevel@tonic-gate * optimization, however, let's assume that mmap() will actually 195*0Sstevel@tonic-gate * give us an aligned address -- since if it does, we can save 196*0Sstevel@tonic-gate * an munmap() later on. If it doesn't -- then go try it again. 197*0Sstevel@tonic-gate */ 198*0Sstevel@tonic-gate mlen = ROUND((lph->p_vaddr + lph->p_memsz) - 199*0Sstevel@tonic-gate ALIGN(fph->p_vaddr, page_size), page_size); 200*0Sstevel@tonic-gate maddr = (caddr_t)MMAP(0, mlen, PROT_READ | PROT_EXEC, 201*0Sstevel@tonic-gate MAP_SHARED, ldfd, 0); 202*0Sstevel@tonic-gate if (maddr == (caddr_t)-1) 203*0Sstevel@tonic-gate PANIC(program_name); 204*0Sstevel@tonic-gate faddr = (caddr_t)ROUND(maddr, fph->p_align); 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate /* 207*0Sstevel@tonic-gate * Check to see whether alignment skew was really needed. 208*0Sstevel@tonic-gate */ 209*0Sstevel@tonic-gate if (faddr != maddr) { 210*0Sstevel@tonic-gate (void) MUNMAP(maddr, mlen); 211*0Sstevel@tonic-gate mlen = ROUND((lph->p_vaddr + lph->p_memsz) - 212*0Sstevel@tonic-gate ALIGN(fph->p_vaddr, fph->p_align) + fph->p_align, 213*0Sstevel@tonic-gate page_size); 214*0Sstevel@tonic-gate maddr = (caddr_t)MMAP(0, mlen, PROT_READ | PROT_EXEC, 215*0Sstevel@tonic-gate MAP_SHARED, ldfd, 0); 216*0Sstevel@tonic-gate if (maddr == (caddr_t)-1) 217*0Sstevel@tonic-gate PANIC(program_name); 218*0Sstevel@tonic-gate faddr = (caddr_t)ROUND(maddr, fph->p_align); 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate /* 222*0Sstevel@tonic-gate * We have the address space reserved, so map each loadable segment. 223*0Sstevel@tonic-gate */ 224*0Sstevel@tonic-gate for (p = 0, pptr = phdr; p < (int)ehdr->e_phnum; p++, 225*0Sstevel@tonic-gate pptr = (Elf32_Phdr *)((caddr_t)pptr + ehdr->e_phentsize)) { 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate /* 228*0Sstevel@tonic-gate * Skip non-loadable segments or segments that don't occupy 229*0Sstevel@tonic-gate * any memory. 230*0Sstevel@tonic-gate */ 231*0Sstevel@tonic-gate if ((pptr->p_type != PT_LOAD) || (pptr->p_memsz == 0)) 232*0Sstevel@tonic-gate continue; 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate /* 235*0Sstevel@tonic-gate * Determine the file offset to which the mapping will 236*0Sstevel@tonic-gate * directed (must be aligned) and how much to map (might 237*0Sstevel@tonic-gate * be more than the file in the case of .bss.) 238*0Sstevel@tonic-gate */ 239*0Sstevel@tonic-gate foff = ALIGN(pptr->p_offset, page_size); 240*0Sstevel@tonic-gate flen = pptr->p_memsz + (pptr->p_offset - foff); 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate /* 243*0Sstevel@tonic-gate * Set address of this segment relative to our base. 244*0Sstevel@tonic-gate */ 245*0Sstevel@tonic-gate addr = (caddr_t)ALIGN(faddr + pptr->p_vaddr, page_size); 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate /* 248*0Sstevel@tonic-gate * If this is the first program header, record our base 249*0Sstevel@tonic-gate * address for later use. 250*0Sstevel@tonic-gate */ 251*0Sstevel@tonic-gate if (pptr == phdr) { 252*0Sstevel@tonic-gate ebp->eb_tag = EB_LDSO_BASE; 253*0Sstevel@tonic-gate (ebp++)->eb_un.eb_ptr = (Elf32_Addr)addr; 254*0Sstevel@tonic-gate } 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate /* 257*0Sstevel@tonic-gate * Unmap anything from the last mapping address to this 258*0Sstevel@tonic-gate * one. 259*0Sstevel@tonic-gate */ 260*0Sstevel@tonic-gate if (addr - maddr) { 261*0Sstevel@tonic-gate (void) MUNMAP(maddr, addr - maddr); 262*0Sstevel@tonic-gate mlen -= addr - maddr; 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate /* 266*0Sstevel@tonic-gate * Determine the mapping protection from the section 267*0Sstevel@tonic-gate * attributes. 268*0Sstevel@tonic-gate */ 269*0Sstevel@tonic-gate i = 0; 270*0Sstevel@tonic-gate if (pptr->p_flags & PF_R) 271*0Sstevel@tonic-gate i |= PROT_READ; 272*0Sstevel@tonic-gate if (pptr->p_flags & PF_W) 273*0Sstevel@tonic-gate i |= PROT_WRITE; 274*0Sstevel@tonic-gate if (pptr->p_flags & PF_X) 275*0Sstevel@tonic-gate i |= PROT_EXEC; 276*0Sstevel@tonic-gate if ((caddr_t)MMAP((caddr_t)addr, flen, i, 277*0Sstevel@tonic-gate MAP_FIXED | MAP_PRIVATE, ldfd, foff) == (caddr_t)-1) 278*0Sstevel@tonic-gate PANIC(program_name); 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate /* 281*0Sstevel@tonic-gate * If the memory occupancy of the segment overflows the 282*0Sstevel@tonic-gate * definition in the file, we need to "zero out" the 283*0Sstevel@tonic-gate * end of the mapping we've established, and if necessary, 284*0Sstevel@tonic-gate * map some more space from /dev/zero. 285*0Sstevel@tonic-gate */ 286*0Sstevel@tonic-gate if (pptr->p_memsz > pptr->p_filesz) { 287*0Sstevel@tonic-gate foff = (int)faddr + pptr->p_vaddr + pptr->p_filesz; 288*0Sstevel@tonic-gate zaddr = (caddr_t)ROUND(foff, page_size); 289*0Sstevel@tonic-gate for (j = 0; j < (int)(zaddr - foff); j++) 290*0Sstevel@tonic-gate *((char *)foff + j) = 0; 291*0Sstevel@tonic-gate j = (faddr + pptr->p_vaddr + pptr->p_memsz) - zaddr; 292*0Sstevel@tonic-gate if (j > 0) { 293*0Sstevel@tonic-gate if (dzfd == 0) { 294*0Sstevel@tonic-gate dzfd = OPEN(ZERO, O_RDWR); 295*0Sstevel@tonic-gate if (dzfd == -1) 296*0Sstevel@tonic-gate PANIC(program_name); 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate if ((caddr_t)MMAP((caddr_t)zaddr, j, i, 299*0Sstevel@tonic-gate MAP_FIXED | MAP_PRIVATE, dzfd, 300*0Sstevel@tonic-gate 0) == (caddr_t)-1) 301*0Sstevel@tonic-gate PANIC(program_name); 302*0Sstevel@tonic-gate } 303*0Sstevel@tonic-gate } 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate /* 306*0Sstevel@tonic-gate * Update the mapping claim pointer. 307*0Sstevel@tonic-gate */ 308*0Sstevel@tonic-gate maddr = addr + ROUND(flen, page_size); 309*0Sstevel@tonic-gate mlen -= maddr - addr; 310*0Sstevel@tonic-gate } 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate /* 313*0Sstevel@tonic-gate * Unmap any final reservation. 314*0Sstevel@tonic-gate */ 315*0Sstevel@tonic-gate if (mlen > 0) 316*0Sstevel@tonic-gate (void) MUNMAP(maddr, mlen); 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate /* 319*0Sstevel@tonic-gate * Clean up file descriptor space we've consumed. Pass along 320*0Sstevel@tonic-gate * the /dev/zero file descriptor we got -- every cycle counts. 321*0Sstevel@tonic-gate */ 322*0Sstevel@tonic-gate (void) CLOSE(ldfd); 323*0Sstevel@tonic-gate if (dzfd != 0) 324*0Sstevel@tonic-gate ebp->eb_tag = EB_DEVZERO, (ebp++)->eb_un.eb_val = dzfd; 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gate ebp->eb_tag = EB_NULL, ebp->eb_un.eb_val = 0; 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate /* The two bytes before _rt_boot is for the alias entry point */ 329*0Sstevel@tonic-gate return (void *) (ehdr->e_entry + faddr - 2); 330*0Sstevel@tonic-gate } 331