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
22*6812Sraf /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24*6812Sraf * Use is subject to license terms.
25*6812Sraf */
26*6812Sraf
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
30*6812Sraf #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <ar.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <sys/mman.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <libelf.h>
380Sstevel@tonic-gate #include "decl.h"
390Sstevel@tonic-gate #include "member.h"
400Sstevel@tonic-gate #include "msg.h"
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <sys/mman.h>
430Sstevel@tonic-gate
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate * Cook the input file.
460Sstevel@tonic-gate * These functions take the input file buffer and extract
470Sstevel@tonic-gate * the Ehdr, Phdr table, and the Shdr table. They keep track
480Sstevel@tonic-gate * of the buffer status as "fresh," "cooked," or "frozen."
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * fresh The file buffer is in its original state and
510Sstevel@tonic-gate * nothing has yet referenced it.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * cooked The application asked for translated data first
540Sstevel@tonic-gate * and caused the library to return a pointer into
550Sstevel@tonic-gate * the file buffer. After this happens, all "raw"
560Sstevel@tonic-gate * operations must go back to the disk.
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * frozen The application first did a "raw" operation that
590Sstevel@tonic-gate * prohibits reusing the file buffer. This effectively
600Sstevel@tonic-gate * freezes the buffer, and all "normal" operations must
610Sstevel@tonic-gate * duplicate their data.
620Sstevel@tonic-gate *
630Sstevel@tonic-gate * For archive handling, these functions conspire to align the
640Sstevel@tonic-gate * file buffer to the host memory format. Archive members
650Sstevel@tonic-gate * are guaranteed only even byte alignment, but the file uses
660Sstevel@tonic-gate * objects at least 4 bytes long. If an archive member is about
670Sstevel@tonic-gate * to be cooked and is not aligned in memory, these functions
680Sstevel@tonic-gate * "slide" the buffer up into the archive member header.
690Sstevel@tonic-gate * This sliding never occurs for frozen files.
700Sstevel@tonic-gate *
710Sstevel@tonic-gate * Some processors might not need sliding at all, if they have
720Sstevel@tonic-gate * no alignment constraints on memory references. This code
730Sstevel@tonic-gate * ignores that possibility for two reasons. First, even machines
740Sstevel@tonic-gate * that have no constraints usually handle aligned objects faster
750Sstevel@tonic-gate * than unaligned. Forcing alignment here probably leads to better
760Sstevel@tonic-gate * performance. Second, there's no way to test at run time whether
770Sstevel@tonic-gate * alignment is required or not. The safe thing is to align in
780Sstevel@tonic-gate * all cases.
790Sstevel@tonic-gate *
800Sstevel@tonic-gate * This sliding relies on the archive header being disposable.
810Sstevel@tonic-gate * Only archive members that are object files ever slide.
820Sstevel@tonic-gate * They're also the only ones that ever need to. Archives never
830Sstevel@tonic-gate * freeze to make headers disposable. Any program peculiar enough
840Sstevel@tonic-gate * to want a frozen archive pays the penalty.
850Sstevel@tonic-gate *
860Sstevel@tonic-gate * The library itself inspects the Ehdr and the Shdr table
870Sstevel@tonic-gate * from the file. Consequently, it converts the file's data
880Sstevel@tonic-gate * to EV_CURRENT version, not the working version. This is
890Sstevel@tonic-gate * transparent to the user. The library never looks at the
900Sstevel@tonic-gate * Phdr table; so that's kept in the working version.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate
930Sstevel@tonic-gate Dnode *
_elf_dnode()940Sstevel@tonic-gate _elf_dnode()
950Sstevel@tonic-gate {
960Sstevel@tonic-gate register Dnode *d;
970Sstevel@tonic-gate
980Sstevel@tonic-gate if ((d = (Dnode *)malloc(sizeof (Dnode))) == 0) {
990Sstevel@tonic-gate _elf_seterr(EMEM_DNODE, errno);
1000Sstevel@tonic-gate return (0);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*d))
1030Sstevel@tonic-gate *d = _elf_dnode_init;
1040Sstevel@tonic-gate d->db_myflags = DBF_ALLOC;
1050Sstevel@tonic-gate NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*d))
1060Sstevel@tonic-gate return (d);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate int
_elf_slide(Elf * elf)1120Sstevel@tonic-gate _elf_slide(Elf * elf)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate NOTE(ASSUMING_PROTECTED(*elf))
1150Sstevel@tonic-gate Elf *par = elf->ed_parent;
1160Sstevel@tonic-gate size_t sz, szof;
1170Sstevel@tonic-gate register char *dst;
1180Sstevel@tonic-gate register char *src = elf->ed_ident;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate if (par == 0 || par->ed_kind != ELF_K_AR)
1210Sstevel@tonic-gate return (0);
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate * This code relies on other code to ensure
1250Sstevel@tonic-gate * the ar_hdr is big enough to move into.
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate if (elf->ed_ident[EI_CLASS] == ELFCLASS64)
1280Sstevel@tonic-gate szof = sizeof (Elf64);
1290Sstevel@tonic-gate else
1300Sstevel@tonic-gate szof = sizeof (Elf32);
1310Sstevel@tonic-gate if ((sz = (size_t)(src - (char *)elf->ed_image) % szof) == 0)
1320Sstevel@tonic-gate return (0);
1330Sstevel@tonic-gate dst = src - sz;
1340Sstevel@tonic-gate elf->ed_ident -= sz;
1350Sstevel@tonic-gate elf->ed_memoff -= sz;
1360Sstevel@tonic-gate elf->ed_armem->m_slide = sz;
1370Sstevel@tonic-gate if (_elf_vm(par, elf->ed_memoff, sz + elf->ed_fsz) != OK_YES)
1380Sstevel@tonic-gate return (-1);
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate * If the archive has been mmaped in, and we're going to slide it,
1420Sstevel@tonic-gate * and it wasn't open for write in the first place, and we've never
1430Sstevel@tonic-gate * done the mprotect() operation before, then do it now.
1440Sstevel@tonic-gate */
1450Sstevel@tonic-gate if ((elf->ed_vm == 0) && ((elf->ed_myflags & EDF_WRITE) == 0) &&
1460Sstevel@tonic-gate ((elf->ed_myflags & EDF_MPROTECT) == 0)) {
1470Sstevel@tonic-gate if (mprotect((char *)elf->ed_image, elf->ed_imagesz,
1480Sstevel@tonic-gate PROT_READ|PROT_WRITE) == -1) {
1490Sstevel@tonic-gate _elf_seterr(EIO_VM, errno);
1500Sstevel@tonic-gate return (-1);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate elf->ed_myflags |= EDF_MPROTECT;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate if (memmove((void *)dst, (const void *)src, elf->ed_fsz) != (void *)dst)
1560Sstevel@tonic-gate return (-1);
1570Sstevel@tonic-gate else
1580Sstevel@tonic-gate return (0);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate Okay
_elf_cook(Elf * elf)1630Sstevel@tonic-gate _elf_cook(Elf * elf)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate NOTE(ASSUMING_PROTECTED(*elf))
1660Sstevel@tonic-gate register int inplace = 1;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (elf->ed_kind != ELF_K_ELF)
1690Sstevel@tonic-gate return (OK_YES);
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if ((elf->ed_status == ES_COOKED) ||
1720Sstevel@tonic-gate ((elf->ed_myflags & EDF_READ) == 0))
1730Sstevel@tonic-gate return (OK_YES);
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate * Here's where the unaligned archive member gets fixed.
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate if (elf->ed_status == ES_FRESH && _elf_slide(elf) != 0)
1790Sstevel@tonic-gate return (OK_NO);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate if (elf->ed_status == ES_FROZEN)
1820Sstevel@tonic-gate inplace = 0;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate * This is the first time we've actually looked at the file
1860Sstevel@tonic-gate * contents. We need to know whether or not this is an
1870Sstevel@tonic-gate * Elf32 or Elf64 file before we can decode the header.
1880Sstevel@tonic-gate * But it's the header that tells us which is which.
1890Sstevel@tonic-gate *
1900Sstevel@tonic-gate * Resolve the chicken-and-egg problem by peeking at the
1910Sstevel@tonic-gate * 'class' byte in the ident string.
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate if (elf->ed_ident[EI_CLASS] == ELFCLASS32) {
1940Sstevel@tonic-gate if (_elf32_ehdr(elf, inplace) != 0)
1950Sstevel@tonic-gate return (OK_NO);
1960Sstevel@tonic-gate if (_elf32_phdr(elf, inplace) != 0)
1970Sstevel@tonic-gate goto xehdr;
1980Sstevel@tonic-gate if (_elf32_shdr(elf, inplace) != 0)
1990Sstevel@tonic-gate goto xphdr;
2000Sstevel@tonic-gate elf->ed_class = ELFCLASS32;
2010Sstevel@tonic-gate } else if (elf->ed_ident[EI_CLASS] == ELFCLASS64) {
2020Sstevel@tonic-gate if (_elf64_ehdr(elf, inplace) != 0)
2030Sstevel@tonic-gate return (OK_NO);
2040Sstevel@tonic-gate if (_elf64_phdr(elf, inplace) != 0)
2050Sstevel@tonic-gate goto xehdr;
2060Sstevel@tonic-gate if (_elf64_shdr(elf, inplace) != 0)
2070Sstevel@tonic-gate goto xphdr;
2080Sstevel@tonic-gate elf->ed_class = ELFCLASS64;
2090Sstevel@tonic-gate } else
2100Sstevel@tonic-gate return (OK_NO);
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate return (OK_YES);
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate xphdr:
2150Sstevel@tonic-gate if (elf->ed_myflags & EDF_PHALLOC) {
2160Sstevel@tonic-gate elf->ed_myflags &= ~EDF_PHALLOC;
2170Sstevel@tonic-gate free(elf->ed_phdr);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate elf->ed_phdr = 0;
2200Sstevel@tonic-gate xehdr:
2210Sstevel@tonic-gate if (elf->ed_myflags & EDF_EHALLOC) {
2220Sstevel@tonic-gate elf->ed_myflags &= ~EDF_EHALLOC;
2230Sstevel@tonic-gate free(elf->ed_ehdr);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate elf->ed_ehdr = 0;
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate return (OK_NO);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate Okay
_elf_cookscn(Elf_Scn * s)2320Sstevel@tonic-gate _elf_cookscn(Elf_Scn * s)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate Elf * elf = s->s_elf;
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate if (elf->ed_class == ELFCLASS32) {
2370Sstevel@tonic-gate return (_elf32_cookscn(s));
2380Sstevel@tonic-gate } else if (elf->ed_class == ELFCLASS64) {
2390Sstevel@tonic-gate return (_elf64_cookscn(s));
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate _elf_seterr(EREQ_CLASS, 0);
2430Sstevel@tonic-gate return (OK_NO);
2440Sstevel@tonic-gate }
245