2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/*
2N/A * Redirection ld.so. Based on the 4.x binary compatibility ld.so, used
2N/A * to redirect aliases for ld.so to the real one.
2N/A */
2N/A
2N/A/*
2N/A * Import data structures
2N/A */
2N/A#include "lint.h"
2N/A#include <sys/types.h>
2N/A#include <sys/mman.h>
2N/A#include <sys/fcntl.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/sysconfig.h>
2N/A#include <sys/auxv.h>
2N/A#include <elf.h>
2N/A#include <link.h>
2N/A#include <string.h>
2N/A#include "alias_boot.h"
2N/A
2N/A/*
2N/A * Local manifest constants and macros.
2N/A */
2N/A#define ALIGN(x, a) ((uintptr_t)(x) & ~((a) - 1))
2N/A#define ROUND(x, a) (((uintptr_t)(x) + ((a) - 1)) & ~((a) - 1))
2N/A
2N/A#define EMPTY strings[EMPTY_S]
2N/A#define LDSO strings[LDSO_S]
2N/A#define ZERO strings[ZERO_S]
2N/A#define CLOSE (*(funcs[CLOSE_F]))
2N/A#define FSTATAT (*(funcs[FSTATAT_F]))
2N/A#define MMAP (*(funcs[MMAP_F]))
2N/A#define MUNMAP (*(funcs[MUNMAP_F]))
2N/A#define OPENAT (*(funcs[OPENAT_F]))
2N/A#define PANIC (*(funcs[PANIC_F]))
2N/A#define SYSCONFIG (*(funcs[SYSCONFIG_F]))
2N/A
2N/A/*
2N/A * Alias ld.so entry point -- receives a bootstrap structure and a vector
2N/A * of strings. The vector is "well-known" to us, and consists of pointers
2N/A * to string constants. This aliasing bootstrap requires no relocation in
2N/A * order to run, save for the pointers of constant strings. This second
2N/A * parameter provides this. Note that this program is carefully coded in
2N/A * order to maintain the "no bootstrapping" requirement -- it calls only
2N/A * local functions, uses no intrinsics, etc.
2N/A */
2N/Astatic void *
2N/A__rtld(Elf32_Boot *ebp, const char *strings[], int (*funcs[])())
2N/A{
2N/A int i, p; /* working */
2N/A long j; /* working */
2N/A long page_size = 0; /* size of a page */
2N/A const char *program_name = EMPTY; /* our name */
2N/A int ldfd; /* fd assigned to ld.so */
2N/A int dzfd = 0; /* fd assigned to /dev/zero */
2N/A Elf32_Ehdr *ehdr; /* ELF header of ld.so */
2N/A Elf32_Phdr *phdr; /* first Phdr in file */
2N/A Elf32_Phdr *pptr; /* working Phdr */
2N/A Elf32_Phdr *lph = NULL; /* last loadable Phdr */
2N/A Elf32_Phdr *fph = NULL; /* first loadable Phdr */
2N/A caddr_t maddr; /* pointer to mapping claim */
2N/A Elf32_Off mlen; /* total mapping claim */
2N/A caddr_t faddr; /* first program mapping of ld.so */
2N/A Elf32_Off foff; /* file offset for segment mapping */
2N/A Elf32_Off flen; /* file length for segment mapping */
2N/A caddr_t addr; /* working mapping address */
2N/A caddr_t zaddr; /* /dev/zero working mapping addr */
2N/A struct stat sb; /* stat buffer for sizing */
2N/A auxv_t *ap; /* working aux pointer */
2N/A
2N/A /*
2N/A * Discover things about our environment: auxiliary vector (if
2N/A * any), arguments, program name, and the like.
2N/A */
2N/A while (ebp->eb_tag != NULL) {
2N/A switch (ebp->eb_tag) {
2N/A case EB_ARGV:
2N/A program_name = *((char **)ebp->eb_un.eb_ptr);
2N/A break;
2N/A case EB_AUXV:
2N/A for (ap = (auxv_t *)ebp->eb_un.eb_ptr;
2N/A ap->a_type != AT_NULL; ap++)
2N/A if (ap->a_type == AT_PAGESZ) {
2N/A page_size = ap->a_un.a_val;
2N/A break;
2N/A }
2N/A break;
2N/A }
2N/A ebp++;
2N/A }
2N/A
2N/A /*
2N/A * If we didn't get a page size from looking in the auxiliary
2N/A * vector, we need to get one now.
2N/A */
2N/A if (page_size == 0) {
2N/A page_size = SYSCONFIG(_CONFIG_PAGESIZE);
2N/A ebp->eb_tag = EB_PAGESIZE, (ebp++)->eb_un.eb_val =
2N/A (Elf32_Word)page_size;
2N/A }
2N/A
2N/A /*
2N/A * Map in the real ld.so. Note that we're mapping it as
2N/A * an ELF database, not as a program -- we just want to walk it's
2N/A * data structures. Further mappings will actually establish the
2N/A * program in the address space.
2N/A */
2N/A if ((ldfd = OPENAT(AT_FDCWD, LDSO, O_RDONLY)) == -1)
2N/A PANIC(program_name);
2N/A if (FSTATAT(ldfd, NULL, &sb, 0) == -1)
2N/A PANIC(program_name);
2N/A ehdr = (Elf32_Ehdr *)MMAP(0, sb.st_size, PROT_READ | PROT_EXEC,
2N/A MAP_SHARED, ldfd, 0);
2N/A if (ehdr == (Elf32_Ehdr *)-1)
2N/A PANIC(program_name);
2N/A
2N/A /*
2N/A * Validate the file we're looking at, ensure it has the correct
2N/A * ELF structures, such as: ELF magic numbers, coded for SPARC,
2N/A * is a ".so", etc.
2N/A */
2N/A if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2N/A ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2N/A ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2N/A ehdr->e_ident[EI_MAG3] != ELFMAG3)
2N/A PANIC(program_name);
2N/A if (ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
2N/A ehdr->e_ident[EI_DATA] != ELFDATA2MSB)
2N/A PANIC(program_name);
2N/A if (ehdr->e_type != ET_DYN)
2N/A PANIC(program_name);
2N/A if ((ehdr->e_machine != EM_SPARC) &&
2N/A (ehdr->e_machine != EM_SPARC32PLUS))
2N/A PANIC(program_name);
2N/A if (ehdr->e_version > EV_CURRENT)
2N/A PANIC(program_name);
2N/A
2N/A /*
2N/A * Point at program headers and start figuring out what to load.
2N/A */
2N/A phdr = (Elf32_Phdr *)((caddr_t)ehdr + ehdr->e_phoff);
2N/A for (p = 0, pptr = phdr; p < (int)ehdr->e_phnum; p++,
2N/A pptr = (Elf32_Phdr *)((caddr_t)pptr + ehdr->e_phentsize))
2N/A if (pptr->p_type == PT_LOAD) {
2N/A if (fph == 0) {
2N/A fph = pptr;
2N/A } else if (pptr->p_vaddr <= lph->p_vaddr)
2N/A PANIC(program_name);
2N/A lph = pptr;
2N/A }
2N/A
2N/A /*
2N/A * We'd better have at least one loadable segment.
2N/A */
2N/A if (fph == 0)
2N/A PANIC(program_name);
2N/A
2N/A /*
2N/A * Map enough address space to hold the program (as opposed to the
2N/A * file) represented by ld.so. The amount to be assigned is the
2N/A * range between the end of the last loadable segment and the
2N/A * beginning of the first PLUS the alignment of the first segment.
2N/A * mmap() can assign us any page-aligned address, but the relocations
2N/A * assume the alignments included in the program header. As an
2N/A * optimization, however, let's assume that mmap() will actually
2N/A * give us an aligned address -- since if it does, we can save
2N/A * an munmap() later on. If it doesn't -- then go try it again.
2N/A */
2N/A mlen = ROUND((lph->p_vaddr + lph->p_memsz) -
2N/A ALIGN(fph->p_vaddr, page_size), page_size);
2N/A maddr = (caddr_t)MMAP(0, mlen, PROT_READ | PROT_EXEC,
2N/A MAP_SHARED, ldfd, 0);
2N/A if (maddr == (caddr_t)-1)
2N/A PANIC(program_name);
2N/A faddr = (caddr_t)ROUND(maddr, fph->p_align);
2N/A
2N/A /*
2N/A * Check to see whether alignment skew was really needed.
2N/A */
2N/A if (faddr != maddr) {
2N/A (void) MUNMAP(maddr, mlen);
2N/A mlen = ROUND((lph->p_vaddr + lph->p_memsz) -
2N/A ALIGN(fph->p_vaddr, fph->p_align) + fph->p_align,
2N/A page_size);
2N/A maddr = (caddr_t)MMAP(0, mlen, PROT_READ | PROT_EXEC,
2N/A MAP_SHARED, ldfd, 0);
2N/A if (maddr == (caddr_t)-1)
2N/A PANIC(program_name);
2N/A faddr = (caddr_t)ROUND(maddr, fph->p_align);
2N/A }
2N/A
2N/A /*
2N/A * We have the address space reserved, so map each loadable segment.
2N/A */
2N/A for (p = 0, pptr = phdr; p < (int)ehdr->e_phnum; p++,
2N/A pptr = (Elf32_Phdr *)((caddr_t)pptr + ehdr->e_phentsize)) {
2N/A
2N/A /*
2N/A * Skip non-loadable segments or segments that don't occupy
2N/A * any memory.
2N/A */
2N/A if ((pptr->p_type != PT_LOAD) || (pptr->p_memsz == 0))
2N/A continue;
2N/A
2N/A /*
2N/A * Determine the file offset to which the mapping will
2N/A * directed (must be aligned) and how much to map (might
2N/A * be more than the file in the case of .bss.)
2N/A */
2N/A foff = ALIGN(pptr->p_offset, page_size);
2N/A flen = pptr->p_memsz + (pptr->p_offset - foff);
2N/A
2N/A /*
2N/A * Set address of this segment relative to our base.
2N/A */
2N/A addr = (caddr_t)ALIGN(faddr + pptr->p_vaddr, page_size);
2N/A
2N/A /*
2N/A * If this is the first program header, record our base
2N/A * address for later use.
2N/A */
2N/A if (pptr == phdr) {
2N/A ebp->eb_tag = EB_LDSO_BASE;
2N/A (ebp++)->eb_un.eb_ptr = (Elf32_Addr)addr;
2N/A }
2N/A
2N/A /*
2N/A * Unmap anything from the last mapping address to this
2N/A * one.
2N/A */
2N/A if (addr - maddr) {
2N/A (void) MUNMAP(maddr, addr - maddr);
2N/A mlen -= addr - maddr;
2N/A }
2N/A
2N/A /*
2N/A * Determine the mapping protection from the section
2N/A * attributes.
2N/A */
2N/A i = 0;
2N/A if (pptr->p_flags & PF_R)
2N/A i |= PROT_READ;
2N/A if (pptr->p_flags & PF_W)
2N/A i |= PROT_WRITE;
2N/A if (pptr->p_flags & PF_X)
2N/A i |= PROT_EXEC;
2N/A if ((caddr_t)MMAP((caddr_t)addr, flen, i,
2N/A MAP_FIXED | MAP_PRIVATE, ldfd, foff) == (caddr_t)-1)
2N/A PANIC(program_name);
2N/A
2N/A /*
2N/A * If the memory occupancy of the segment overflows the
2N/A * definition in the file, we need to "zero out" the
2N/A * end of the mapping we've established, and if necessary,
2N/A * map some more space from /dev/zero.
2N/A */
2N/A if (pptr->p_memsz > pptr->p_filesz) {
2N/A foff = (uintptr_t)faddr + pptr->p_vaddr +
2N/A pptr->p_filesz;
2N/A zaddr = (caddr_t)ROUND(foff, page_size);
2N/A for (j = 0; j < (int)(zaddr - foff); j++)
2N/A *((char *)foff + j) = 0;
2N/A j = (faddr + pptr->p_vaddr + pptr->p_memsz) - zaddr;
2N/A if (j > 0) {
2N/A if (dzfd == 0) {
2N/A dzfd = OPENAT(AT_FDCWD, ZERO, O_RDWR);
2N/A if (dzfd == -1)
2N/A PANIC(program_name);
2N/A }
2N/A if ((caddr_t)MMAP((caddr_t)zaddr, j, i,
2N/A MAP_FIXED | MAP_PRIVATE, dzfd,
2N/A 0) == (caddr_t)-1)
2N/A PANIC(program_name);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Update the mapping claim pointer.
2N/A */
2N/A maddr = addr + ROUND(flen, page_size);
2N/A mlen -= maddr - addr;
2N/A }
2N/A
2N/A /*
2N/A * Unmap any final reservation.
2N/A */
2N/A if (mlen != 0)
2N/A (void) MUNMAP(maddr, mlen);
2N/A
2N/A /*
2N/A * Clean up file descriptor space we've consumed. Pass along
2N/A * the /dev/zero file descriptor we got -- every cycle counts.
2N/A */
2N/A (void) CLOSE(ldfd);
2N/A if (dzfd != 0)
2N/A ebp->eb_tag = EB_DEVZERO, (ebp++)->eb_un.eb_val = dzfd;
2N/A
2N/A /*
2N/A * The call itself. Note that we start 1 instruction word in.
2N/A * The ELF ld.so contains an "entry vector" of branch instructions,
2N/A * which, for our interest are:
2N/A * +0: ba, a <normal startup>
2N/A * +4: ba, a <compatibility startup>
2N/A * +8: ba, a <alias startup>
2N/A * By starting at the alias startup, the ELF ld.so knows
2N/A * that a pointer to "eb" is available to it and further knows
2N/A * how to calculate the offset to the program's arguments and
2N/A * other structures. We do the "call" by returning to our
2N/A * bootstrap and then jumping to the address that we return.
2N/A */
2N/A ebp->eb_tag = EB_NULL, ebp->eb_un.eb_val = 0;
2N/A return ((void *)(ehdr->e_entry + faddr + 8));
2N/A}