17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24*7257d1b4Sraf * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*7257d1b4Sraf /* Copyright (c) 1988 AT&T */
28*7257d1b4Sraf /* All Rights Reserved */
29*7257d1b4Sraf
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <ar.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <sys/mman.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <libelf.h>
367c478bd9Sstevel@tonic-gate #include "decl.h"
377c478bd9Sstevel@tonic-gate #include "member.h"
387c478bd9Sstevel@tonic-gate #include "msg.h"
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <sys/mman.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate * Cook the input file.
447c478bd9Sstevel@tonic-gate * These functions take the input file buffer and extract
457c478bd9Sstevel@tonic-gate * the Ehdr, Phdr table, and the Shdr table. They keep track
467c478bd9Sstevel@tonic-gate * of the buffer status as "fresh," "cooked," or "frozen."
477c478bd9Sstevel@tonic-gate *
487c478bd9Sstevel@tonic-gate * fresh The file buffer is in its original state and
497c478bd9Sstevel@tonic-gate * nothing has yet referenced it.
507c478bd9Sstevel@tonic-gate *
517c478bd9Sstevel@tonic-gate * cooked The application asked for translated data first
527c478bd9Sstevel@tonic-gate * and caused the library to return a pointer into
537c478bd9Sstevel@tonic-gate * the file buffer. After this happens, all "raw"
547c478bd9Sstevel@tonic-gate * operations must go back to the disk.
557c478bd9Sstevel@tonic-gate *
567c478bd9Sstevel@tonic-gate * frozen The application first did a "raw" operation that
577c478bd9Sstevel@tonic-gate * prohibits reusing the file buffer. This effectively
587c478bd9Sstevel@tonic-gate * freezes the buffer, and all "normal" operations must
597c478bd9Sstevel@tonic-gate * duplicate their data.
607c478bd9Sstevel@tonic-gate *
617c478bd9Sstevel@tonic-gate * For archive handling, these functions conspire to align the
627c478bd9Sstevel@tonic-gate * file buffer to the host memory format. Archive members
637c478bd9Sstevel@tonic-gate * are guaranteed only even byte alignment, but the file uses
647c478bd9Sstevel@tonic-gate * objects at least 4 bytes long. If an archive member is about
657c478bd9Sstevel@tonic-gate * to be cooked and is not aligned in memory, these functions
667c478bd9Sstevel@tonic-gate * "slide" the buffer up into the archive member header.
677c478bd9Sstevel@tonic-gate * This sliding never occurs for frozen files.
687c478bd9Sstevel@tonic-gate *
697c478bd9Sstevel@tonic-gate * Some processors might not need sliding at all, if they have
707c478bd9Sstevel@tonic-gate * no alignment constraints on memory references. This code
717c478bd9Sstevel@tonic-gate * ignores that possibility for two reasons. First, even machines
727c478bd9Sstevel@tonic-gate * that have no constraints usually handle aligned objects faster
737c478bd9Sstevel@tonic-gate * than unaligned. Forcing alignment here probably leads to better
747c478bd9Sstevel@tonic-gate * performance. Second, there's no way to test at run time whether
757c478bd9Sstevel@tonic-gate * alignment is required or not. The safe thing is to align in
767c478bd9Sstevel@tonic-gate * all cases.
777c478bd9Sstevel@tonic-gate *
787c478bd9Sstevel@tonic-gate * This sliding relies on the archive header being disposable.
797c478bd9Sstevel@tonic-gate * Only archive members that are object files ever slide.
807c478bd9Sstevel@tonic-gate * They're also the only ones that ever need to. Archives never
817c478bd9Sstevel@tonic-gate * freeze to make headers disposable. Any program peculiar enough
827c478bd9Sstevel@tonic-gate * to want a frozen archive pays the penalty.
837c478bd9Sstevel@tonic-gate *
847c478bd9Sstevel@tonic-gate * The library itself inspects the Ehdr and the Shdr table
857c478bd9Sstevel@tonic-gate * from the file. Consequently, it converts the file's data
867c478bd9Sstevel@tonic-gate * to EV_CURRENT version, not the working version. This is
877c478bd9Sstevel@tonic-gate * transparent to the user. The library never looks at the
887c478bd9Sstevel@tonic-gate * Phdr table; so that's kept in the working version.
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate Dnode *
_elf_dnode()927c478bd9Sstevel@tonic-gate _elf_dnode()
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate register Dnode *d;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate if ((d = (Dnode *)malloc(sizeof (Dnode))) == 0) {
977c478bd9Sstevel@tonic-gate _elf_seterr(EMEM_DNODE, errno);
987c478bd9Sstevel@tonic-gate return (0);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate *d = _elf_dnode_init;
1017c478bd9Sstevel@tonic-gate d->db_myflags = DBF_ALLOC;
1027c478bd9Sstevel@tonic-gate return (d);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate int
_elf_slide(Elf * elf)1087c478bd9Sstevel@tonic-gate _elf_slide(Elf * elf)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate Elf *par = elf->ed_parent;
1117c478bd9Sstevel@tonic-gate size_t sz, szof;
1127c478bd9Sstevel@tonic-gate register char *dst;
1137c478bd9Sstevel@tonic-gate register char *src = elf->ed_ident;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate if (par == 0 || par->ed_kind != ELF_K_AR)
1167c478bd9Sstevel@tonic-gate return (0);
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate * This code relies on other code to ensure
1207c478bd9Sstevel@tonic-gate * the ar_hdr is big enough to move into.
1217c478bd9Sstevel@tonic-gate */
1227c478bd9Sstevel@tonic-gate if (elf->ed_ident[EI_CLASS] == ELFCLASS64)
1237c478bd9Sstevel@tonic-gate szof = sizeof (Elf64);
1247c478bd9Sstevel@tonic-gate else
1257c478bd9Sstevel@tonic-gate szof = sizeof (Elf32);
1267c478bd9Sstevel@tonic-gate if ((sz = (size_t)(src - (char *)elf->ed_image) % szof) == 0)
1277c478bd9Sstevel@tonic-gate return (0);
1287c478bd9Sstevel@tonic-gate dst = src - sz;
1297c478bd9Sstevel@tonic-gate elf->ed_ident -= sz;
1307c478bd9Sstevel@tonic-gate elf->ed_memoff -= sz;
1317c478bd9Sstevel@tonic-gate elf->ed_armem->m_slide = sz;
1327c478bd9Sstevel@tonic-gate if (_elf_vm(par, elf->ed_memoff, sz + elf->ed_fsz) != OK_YES)
1337c478bd9Sstevel@tonic-gate return (-1);
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate * If the archive has been mmaped in, and we're going to slide it,
1377c478bd9Sstevel@tonic-gate * and it wasn't open for write in the first place, and we've never
1387c478bd9Sstevel@tonic-gate * done the mprotect() operation before, then do it now.
1397c478bd9Sstevel@tonic-gate */
1407c478bd9Sstevel@tonic-gate if ((elf->ed_vm == 0) && ((elf->ed_myflags & EDF_WRITE) == 0) &&
1417c478bd9Sstevel@tonic-gate ((elf->ed_myflags & EDF_MPROTECT) == 0)) {
1427c478bd9Sstevel@tonic-gate if (mprotect((char *)elf->ed_image, elf->ed_imagesz,
1437c478bd9Sstevel@tonic-gate PROT_READ|PROT_WRITE) == -1) {
1447c478bd9Sstevel@tonic-gate _elf_seterr(EIO_VM, errno);
1457c478bd9Sstevel@tonic-gate return (-1);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate elf->ed_myflags |= EDF_MPROTECT;
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate if (memmove((void *)dst, (const void *)src, elf->ed_fsz) != (void *)dst)
1517c478bd9Sstevel@tonic-gate return (-1);
1527c478bd9Sstevel@tonic-gate else
1537c478bd9Sstevel@tonic-gate return (0);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate Okay
_elf_cook(Elf * elf)1587c478bd9Sstevel@tonic-gate _elf_cook(Elf * elf)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate register int inplace = 1;
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate if (elf->ed_kind != ELF_K_ELF)
1637c478bd9Sstevel@tonic-gate return (OK_YES);
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate if ((elf->ed_status == ES_COOKED) ||
1667c478bd9Sstevel@tonic-gate ((elf->ed_myflags & EDF_READ) == 0))
1677c478bd9Sstevel@tonic-gate return (OK_YES);
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate * Here's where the unaligned archive member gets fixed.
1717c478bd9Sstevel@tonic-gate */
1727c478bd9Sstevel@tonic-gate if (elf->ed_status == ES_FRESH && _elf_slide(elf) != 0)
1737c478bd9Sstevel@tonic-gate return (OK_NO);
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate if (elf->ed_status == ES_FROZEN)
1767c478bd9Sstevel@tonic-gate inplace = 0;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate * This is the first time we've actually looked at the file
1807c478bd9Sstevel@tonic-gate * contents. We need to know whether or not this is an
1817c478bd9Sstevel@tonic-gate * Elf32 or Elf64 file before we can decode the header.
1827c478bd9Sstevel@tonic-gate * But it's the header that tells us which is which.
1837c478bd9Sstevel@tonic-gate *
1847c478bd9Sstevel@tonic-gate * Resolve the chicken-and-egg problem by peeking at the
1857c478bd9Sstevel@tonic-gate * 'class' byte in the ident string.
1867c478bd9Sstevel@tonic-gate */
1877c478bd9Sstevel@tonic-gate if (elf->ed_ident[EI_CLASS] == ELFCLASS32) {
1887c478bd9Sstevel@tonic-gate if (_elf32_ehdr(elf, inplace) != 0)
1897c478bd9Sstevel@tonic-gate return (OK_NO);
1907c478bd9Sstevel@tonic-gate if (_elf32_phdr(elf, inplace) != 0)
1917c478bd9Sstevel@tonic-gate goto xehdr;
1927c478bd9Sstevel@tonic-gate if (_elf32_shdr(elf, inplace) != 0)
1937c478bd9Sstevel@tonic-gate goto xphdr;
1947c478bd9Sstevel@tonic-gate elf->ed_class = ELFCLASS32;
1957c478bd9Sstevel@tonic-gate } else if (elf->ed_ident[EI_CLASS] == ELFCLASS64) {
1967c478bd9Sstevel@tonic-gate if (_elf64_ehdr(elf, inplace) != 0)
1977c478bd9Sstevel@tonic-gate return (OK_NO);
1987c478bd9Sstevel@tonic-gate if (_elf64_phdr(elf, inplace) != 0)
1997c478bd9Sstevel@tonic-gate goto xehdr;
2007c478bd9Sstevel@tonic-gate if (_elf64_shdr(elf, inplace) != 0)
2017c478bd9Sstevel@tonic-gate goto xphdr;
2027c478bd9Sstevel@tonic-gate elf->ed_class = ELFCLASS64;
2037c478bd9Sstevel@tonic-gate } else
2047c478bd9Sstevel@tonic-gate return (OK_NO);
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate return (OK_YES);
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate xphdr:
2097c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_PHALLOC) {
2107c478bd9Sstevel@tonic-gate elf->ed_myflags &= ~EDF_PHALLOC;
2117c478bd9Sstevel@tonic-gate free(elf->ed_phdr);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate elf->ed_phdr = 0;
2147c478bd9Sstevel@tonic-gate xehdr:
2157c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_EHALLOC) {
2167c478bd9Sstevel@tonic-gate elf->ed_myflags &= ~EDF_EHALLOC;
2177c478bd9Sstevel@tonic-gate free(elf->ed_ehdr);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate elf->ed_ehdr = 0;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate return (OK_NO);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate Okay
_elf_cookscn(Elf_Scn * s)2267c478bd9Sstevel@tonic-gate _elf_cookscn(Elf_Scn * s)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate Elf * elf = s->s_elf;
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate if (elf->ed_class == ELFCLASS32) {
2317c478bd9Sstevel@tonic-gate return (_elf32_cookscn(s));
2327c478bd9Sstevel@tonic-gate } else if (elf->ed_class == ELFCLASS64) {
2337c478bd9Sstevel@tonic-gate return (_elf64_cookscn(s));
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_CLASS, 0);
2377c478bd9Sstevel@tonic-gate return (OK_NO);
2387c478bd9Sstevel@tonic-gate }
239