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 */ 21*7257d1b4Sraf 22*7257d1b4Sraf /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24*7257d1b4Sraf * Use is subject to license terms. 25*7257d1b4Sraf */ 26*7257d1b4Sraf 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * Copyright (c) 1988 AT&T 297c478bd9Sstevel@tonic-gate * All Rights Reserved 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 32*7257d1b4Sraf #pragma ident "%Z%%M% %I% %E% SMI" 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include <ar.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <memory.h> 377c478bd9Sstevel@tonic-gate #include <errno.h> 387c478bd9Sstevel@tonic-gate #include <libelf.h> 397c478bd9Sstevel@tonic-gate #include <sys/mman.h> 407c478bd9Sstevel@tonic-gate #include "decl.h" 417c478bd9Sstevel@tonic-gate #include "member.h" 427c478bd9Sstevel@tonic-gate #include "msg.h" 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate static const char armag[] = ARMAG; 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /* 487c478bd9Sstevel@tonic-gate * Initialize archive member 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate Elf * 517c478bd9Sstevel@tonic-gate _elf_member(int fd, Elf * ref, unsigned flags) 527c478bd9Sstevel@tonic-gate { 537c478bd9Sstevel@tonic-gate register Elf *elf; 547c478bd9Sstevel@tonic-gate Member *mh; 557c478bd9Sstevel@tonic-gate size_t base; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate if (ref->ed_nextoff >= ref->ed_fsz) 587c478bd9Sstevel@tonic-gate return (0); 597c478bd9Sstevel@tonic-gate if (ref->ed_fd == -1) /* disabled */ 607c478bd9Sstevel@tonic-gate fd = -1; 617c478bd9Sstevel@tonic-gate if (flags & EDF_WRITE) { 627c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_ARRDWR, 0); 637c478bd9Sstevel@tonic-gate return (0); 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate if (ref->ed_fd != fd) { 667c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_ARMEMFD, 0); 677c478bd9Sstevel@tonic-gate return (0); 687c478bd9Sstevel@tonic-gate } 697c478bd9Sstevel@tonic-gate if ((_elf_vm(ref, ref->ed_nextoff, sizeof (struct ar_hdr)) != 707c478bd9Sstevel@tonic-gate OK_YES) || ((mh = _elf_armem(ref, 717c478bd9Sstevel@tonic-gate ref->ed_ident + ref->ed_nextoff, ref->ed_fsz)) == 0)) 727c478bd9Sstevel@tonic-gate return (0); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate base = ref->ed_nextoff + sizeof (struct ar_hdr); 757c478bd9Sstevel@tonic-gate if (ref->ed_fsz - base < mh->m_hdr.ar_size) { 767c478bd9Sstevel@tonic-gate _elf_seterr(EFMT_ARMEMSZ, 0); 777c478bd9Sstevel@tonic-gate return (0); 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) { 807c478bd9Sstevel@tonic-gate _elf_seterr(EMEM_ELF, errno); 817c478bd9Sstevel@tonic-gate return (0); 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate ++ref->ed_activ; 847c478bd9Sstevel@tonic-gate elf->ed_parent = ref; 857c478bd9Sstevel@tonic-gate elf->ed_fd = fd; 867c478bd9Sstevel@tonic-gate elf->ed_myflags |= flags; 877c478bd9Sstevel@tonic-gate elf->ed_armem = mh; 887c478bd9Sstevel@tonic-gate elf->ed_fsz = mh->m_hdr.ar_size; 897c478bd9Sstevel@tonic-gate elf->ed_baseoff = ref->ed_baseoff + base; 907c478bd9Sstevel@tonic-gate elf->ed_memoff = base - mh->m_slide; 917c478bd9Sstevel@tonic-gate elf->ed_siboff = base + elf->ed_fsz + (elf->ed_fsz & 1); 927c478bd9Sstevel@tonic-gate ref->ed_nextoff = elf->ed_siboff; 937c478bd9Sstevel@tonic-gate elf->ed_image = ref->ed_image; 947c478bd9Sstevel@tonic-gate elf->ed_imagesz = ref->ed_imagesz; 957c478bd9Sstevel@tonic-gate elf->ed_vm = ref->ed_vm; 967c478bd9Sstevel@tonic-gate elf->ed_vmsz = ref->ed_vmsz; 977c478bd9Sstevel@tonic-gate elf->ed_ident = ref->ed_ident + base - mh->m_slide; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * If this member is the archive string table, 1017c478bd9Sstevel@tonic-gate * we've already altered the bytes. 1027c478bd9Sstevel@tonic-gate */ 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate if (ref->ed_arstroff == ref->ed_nextoff) 1057c478bd9Sstevel@tonic-gate elf->ed_status = ES_COOKED; 1067c478bd9Sstevel@tonic-gate return (elf); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate Elf * 1117c478bd9Sstevel@tonic-gate _elf_regular(int fd, unsigned flags) /* initialize regular file */ 1127c478bd9Sstevel@tonic-gate { 1137c478bd9Sstevel@tonic-gate Elf *elf; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) { 1167c478bd9Sstevel@tonic-gate _elf_seterr(EMEM_ELF, errno); 1177c478bd9Sstevel@tonic-gate return (0); 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf)) 1217c478bd9Sstevel@tonic-gate elf->ed_fd = fd; 1227c478bd9Sstevel@tonic-gate elf->ed_myflags |= flags; 1237c478bd9Sstevel@tonic-gate if (_elf_inmap(elf) != OK_YES) { 1247c478bd9Sstevel@tonic-gate free(elf); 1257c478bd9Sstevel@tonic-gate return (0); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf)) 1287c478bd9Sstevel@tonic-gate return (elf); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate Elf * 1337c478bd9Sstevel@tonic-gate _elf_config(Elf * elf) 1347c478bd9Sstevel@tonic-gate { 1357c478bd9Sstevel@tonic-gate char *base; 1367c478bd9Sstevel@tonic-gate unsigned encode; 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate ELFRWLOCKINIT(&elf->ed_rwlock); 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* 1417c478bd9Sstevel@tonic-gate * Determine if this is a ELF file. 1427c478bd9Sstevel@tonic-gate */ 1437c478bd9Sstevel@tonic-gate base = elf->ed_ident; 1447c478bd9Sstevel@tonic-gate if ((elf->ed_fsz >= EI_NIDENT) && 1457c478bd9Sstevel@tonic-gate (_elf_vm(elf, (size_t)0, (size_t)EI_NIDENT) == OK_YES) && 1467c478bd9Sstevel@tonic-gate (base[EI_MAG0] == ELFMAG0) && 1477c478bd9Sstevel@tonic-gate (base[EI_MAG1] == ELFMAG1) && 1487c478bd9Sstevel@tonic-gate (base[EI_MAG2] == ELFMAG2) && 1497c478bd9Sstevel@tonic-gate (base[EI_MAG3] == ELFMAG3)) { 1507c478bd9Sstevel@tonic-gate elf->ed_kind = ELF_K_ELF; 1517c478bd9Sstevel@tonic-gate elf->ed_class = base[EI_CLASS]; 1527c478bd9Sstevel@tonic-gate elf->ed_encode = base[EI_DATA]; 1537c478bd9Sstevel@tonic-gate if ((elf->ed_version = base[EI_VERSION]) == 0) 1547c478bd9Sstevel@tonic-gate elf->ed_version = 1; 1557c478bd9Sstevel@tonic-gate elf->ed_identsz = EI_NIDENT; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /* 1587c478bd9Sstevel@tonic-gate * Allow writing only if originally specified read only. 1597c478bd9Sstevel@tonic-gate * This is only necessary if the file must be translating 1607c478bd9Sstevel@tonic-gate * from one encoding to another. 1617c478bd9Sstevel@tonic-gate */ 1627c478bd9Sstevel@tonic-gate ELFACCESSDATA(encode, _elf_encode) 1637c478bd9Sstevel@tonic-gate if ((elf->ed_vm == 0) && ((elf->ed_myflags & EDF_WRITE) == 0) && 1647c478bd9Sstevel@tonic-gate (elf->ed_encode != encode)) { 1657c478bd9Sstevel@tonic-gate if (mprotect((char *)elf->ed_image, elf->ed_imagesz, 1667c478bd9Sstevel@tonic-gate PROT_READ|PROT_WRITE) == -1) { 1677c478bd9Sstevel@tonic-gate _elf_seterr(EIO_VM, errno); 1687c478bd9Sstevel@tonic-gate return (0); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate return (elf); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate /* 1757c478bd9Sstevel@tonic-gate * Determine if this is an Archive 1767c478bd9Sstevel@tonic-gate */ 1777c478bd9Sstevel@tonic-gate if ((elf->ed_fsz >= SARMAG) && 1787c478bd9Sstevel@tonic-gate (_elf_vm(elf, (size_t)0, (size_t)SARMAG) == OK_YES) && 1797c478bd9Sstevel@tonic-gate (memcmp(base, armag, SARMAG) == 0)) { 1807c478bd9Sstevel@tonic-gate _elf_arinit(elf); 1817c478bd9Sstevel@tonic-gate elf->ed_kind = ELF_K_AR; 1827c478bd9Sstevel@tonic-gate elf->ed_identsz = SARMAG; 1837c478bd9Sstevel@tonic-gate return (elf); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate /* 1877c478bd9Sstevel@tonic-gate * Return a few ident bytes, but not so many that 1887c478bd9Sstevel@tonic-gate * getident() must read a large file. 512 is arbitrary. 1897c478bd9Sstevel@tonic-gate */ 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate elf->ed_kind = ELF_K_NONE; 1927c478bd9Sstevel@tonic-gate if ((elf->ed_identsz = elf->ed_fsz) > 512) 1937c478bd9Sstevel@tonic-gate elf->ed_identsz = 512; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate return (elf); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate Elf * 1997c478bd9Sstevel@tonic-gate elf_memory(char *image, size_t sz) 2007c478bd9Sstevel@tonic-gate { 2017c478bd9Sstevel@tonic-gate Elf *elf; 2027c478bd9Sstevel@tonic-gate unsigned work; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate /* 2057c478bd9Sstevel@tonic-gate * version() no called yet? 2067c478bd9Sstevel@tonic-gate */ 2077c478bd9Sstevel@tonic-gate ELFACCESSDATA(work, _elf_work) 2087c478bd9Sstevel@tonic-gate if (work == EV_NONE) { 2097c478bd9Sstevel@tonic-gate _elf_seterr(ESEQ_VER, 0); 2107c478bd9Sstevel@tonic-gate return (0); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) { 2147c478bd9Sstevel@tonic-gate _elf_seterr(EMEM_ELF, errno); 2157c478bd9Sstevel@tonic-gate return (0); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf)) 2187c478bd9Sstevel@tonic-gate elf->ed_fd = -1; 2197c478bd9Sstevel@tonic-gate elf->ed_myflags |= EDF_READ | EDF_MEMORY; 2207c478bd9Sstevel@tonic-gate elf->ed_image = elf->ed_ident = image; 2217c478bd9Sstevel@tonic-gate elf->ed_imagesz = elf->ed_fsz = elf->ed_identsz = sz; 2227c478bd9Sstevel@tonic-gate elf->ed_kind = ELF_K_ELF; 2237c478bd9Sstevel@tonic-gate elf->ed_class = image[EI_CLASS]; 2247c478bd9Sstevel@tonic-gate elf->ed_encode = image[EI_DATA]; 2257c478bd9Sstevel@tonic-gate if ((elf->ed_version = image[EI_VERSION]) == 0) 2267c478bd9Sstevel@tonic-gate elf->ed_version = 1; 2277c478bd9Sstevel@tonic-gate elf->ed_identsz = EI_NIDENT; 2287c478bd9Sstevel@tonic-gate elf->ed_activ = 1; 2297c478bd9Sstevel@tonic-gate elf = _elf_config(elf); 2307c478bd9Sstevel@tonic-gate NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf)) 2317c478bd9Sstevel@tonic-gate return (elf); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /* 2357c478bd9Sstevel@tonic-gate * The following is a private interface between the linkers (ld & ld.so.1) 2367c478bd9Sstevel@tonic-gate * and libelf. 2377c478bd9Sstevel@tonic-gate * 2387c478bd9Sstevel@tonic-gate * elf_begin(0, ELF_C_IMAGE, ref) 2397c478bd9Sstevel@tonic-gate * Return a new elf_descriptor which uses the memory image from 2407c478bd9Sstevel@tonic-gate * ref as the base image of the elf file. Before this elf_begin() 2417c478bd9Sstevel@tonic-gate * is called an elf_update(ref, ELF_C_WRIMAGE) must have been 2427c478bd9Sstevel@tonic-gate * done to the ref elf descriptor. 2437c478bd9Sstevel@tonic-gate * The ELF_C_IMAGE is unique in that modificatino of the Elf structure 2447c478bd9Sstevel@tonic-gate * is illegal (no elf_new*()) but you can modify the actual 2457c478bd9Sstevel@tonic-gate * data image of the file in question. 2467c478bd9Sstevel@tonic-gate * 2477c478bd9Sstevel@tonic-gate * When you are done processing this file you can then perform a 2487c478bd9Sstevel@tonic-gate * elf_end() on it. 2497c478bd9Sstevel@tonic-gate * 2507c478bd9Sstevel@tonic-gate * NOTE: if an elf_update(ref, ELF_C_WRITE) is done on the ref Elf 2517c478bd9Sstevel@tonic-gate * descriptor then the memory image that the ELF_C_IMAGE 2527c478bd9Sstevel@tonic-gate * is using has been discarded. The proper calling convention 2537c478bd9Sstevel@tonic-gate * for this is as follows: 2547c478bd9Sstevel@tonic-gate * 2557c478bd9Sstevel@tonic-gate * elf1 = elf_begin(fd, ELF_C_WRITE, 0); 2567c478bd9Sstevel@tonic-gate * ... 2577c478bd9Sstevel@tonic-gate * elf_update(elf1, ELF_C_WRIMAGE); build memory image 2587c478bd9Sstevel@tonic-gate * elf2 = elf_begin(0, ELF_C_IMAGE, elf1); 2597c478bd9Sstevel@tonic-gate * ... 2607c478bd9Sstevel@tonic-gate * elf_end(elf2); 2617c478bd9Sstevel@tonic-gate * elf_updage(elf1, ELF_C_WRITE); flush memory image to disk 2627c478bd9Sstevel@tonic-gate * elf_end(elf1); 2637c478bd9Sstevel@tonic-gate * 2647c478bd9Sstevel@tonic-gate * 2657c478bd9Sstevel@tonic-gate * elf_begin(0, ELF_C_IMAGE, 0); 2667c478bd9Sstevel@tonic-gate * returns a pointer to an elf descriptor as if it were opened 2677c478bd9Sstevel@tonic-gate * with ELF_C_WRITE except that it has no file descriptor and it 2687c478bd9Sstevel@tonic-gate * will not create a file. It's to be used with the command: 2697c478bd9Sstevel@tonic-gate * 2707c478bd9Sstevel@tonic-gate * elf_update(elf, ELF_C_WRIMAGE) 2717c478bd9Sstevel@tonic-gate * 2727c478bd9Sstevel@tonic-gate * which will build a memory image instead of a file image. 2737c478bd9Sstevel@tonic-gate * The memory image is allocated via dynamic memory (malloc) and 2747c478bd9Sstevel@tonic-gate * can be free with a subsequent call to 2757c478bd9Sstevel@tonic-gate * 2767c478bd9Sstevel@tonic-gate * elf_update(elf, ELF_C_WRITE) 2777c478bd9Sstevel@tonic-gate * 2787c478bd9Sstevel@tonic-gate * NOTE: that if elf_end(elf) is called it will not free the 2797c478bd9Sstevel@tonic-gate * memory image if it is still allocated. It is then 2807c478bd9Sstevel@tonic-gate * the callers responsiblity to free it via a call 2817c478bd9Sstevel@tonic-gate * to free(). 2827c478bd9Sstevel@tonic-gate * 2837c478bd9Sstevel@tonic-gate * Here is a potential calling sequence for this interface: 2847c478bd9Sstevel@tonic-gate * 2857c478bd9Sstevel@tonic-gate * elf1 = elf_begin(0, ELF_C_IMAGE, 0); 2867c478bd9Sstevel@tonic-gate * ... 2877c478bd9Sstevel@tonic-gate * elf_update(elf1, ELF_C_WRIMAGE); build memory image 2887c478bd9Sstevel@tonic-gate * elf2 = elf_begin(0, ELF_C_IMAGE, elf1); 2897c478bd9Sstevel@tonic-gate * ... 2907c478bd9Sstevel@tonic-gate * image_ptr = elf32_getehdr(elf2); get pointer to image 2917c478bd9Sstevel@tonic-gate * elf_end(elf2); 2927c478bd9Sstevel@tonic-gate * elf_end(elf1); 2937c478bd9Sstevel@tonic-gate * ... 2947c478bd9Sstevel@tonic-gate * use image 2957c478bd9Sstevel@tonic-gate * ... 2967c478bd9Sstevel@tonic-gate * free(image_ptr); 2977c478bd9Sstevel@tonic-gate */ 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate Elf * 3007c478bd9Sstevel@tonic-gate elf_begin(int fd, Elf_Cmd cmd, Elf *ref) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate register Elf *elf; 3037c478bd9Sstevel@tonic-gate unsigned work; 3047c478bd9Sstevel@tonic-gate unsigned flags = 0; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate ELFACCESSDATA(work, _elf_work) 3077c478bd9Sstevel@tonic-gate if (work == EV_NONE) /* version() not called yet */ 3087c478bd9Sstevel@tonic-gate { 3097c478bd9Sstevel@tonic-gate _elf_seterr(ESEQ_VER, 0); 3107c478bd9Sstevel@tonic-gate return (0); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate switch (cmd) { 3137c478bd9Sstevel@tonic-gate default: 3147c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_BEGIN, 0); 3157c478bd9Sstevel@tonic-gate return (0); 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate case ELF_C_NULL: 3187c478bd9Sstevel@tonic-gate return (0); 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate case ELF_C_IMAGE: 3217c478bd9Sstevel@tonic-gate if (ref) { 3227c478bd9Sstevel@tonic-gate char *image; 3237c478bd9Sstevel@tonic-gate size_t imagesz; 3247c478bd9Sstevel@tonic-gate ELFRLOCK(ref); 3257c478bd9Sstevel@tonic-gate if ((image = ref->ed_wrimage) == 0) { 3267c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_NOWRIMAGE, 0); 3277c478bd9Sstevel@tonic-gate ELFUNLOCK(ref); 3287c478bd9Sstevel@tonic-gate return (0); 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate imagesz = ref->ed_wrimagesz; 3317c478bd9Sstevel@tonic-gate ELFUNLOCK(ref); 3327c478bd9Sstevel@tonic-gate return (elf_memory(image, imagesz)); 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 3357c478bd9Sstevel@tonic-gate case ELF_C_WRITE: 3367c478bd9Sstevel@tonic-gate if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) { 3377c478bd9Sstevel@tonic-gate _elf_seterr(EMEM_ELF, errno); 3387c478bd9Sstevel@tonic-gate return (0); 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf)) 3417c478bd9Sstevel@tonic-gate ELFRWLOCKINIT(&elf->ed_rwlock); 3427c478bd9Sstevel@tonic-gate elf->ed_fd = fd; 3437c478bd9Sstevel@tonic-gate elf->ed_activ = 1; 3447c478bd9Sstevel@tonic-gate elf->ed_myflags |= EDF_WRITE; 3457c478bd9Sstevel@tonic-gate if (cmd == ELF_C_IMAGE) 3467c478bd9Sstevel@tonic-gate elf->ed_myflags |= EDF_WRALLOC; 3477c478bd9Sstevel@tonic-gate NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf)) 3487c478bd9Sstevel@tonic-gate return (elf); 3497c478bd9Sstevel@tonic-gate case ELF_C_RDWR: 3507c478bd9Sstevel@tonic-gate flags = EDF_WRITE | EDF_READ; 3517c478bd9Sstevel@tonic-gate break; 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate case ELF_C_READ: 3547c478bd9Sstevel@tonic-gate flags = EDF_READ; 3557c478bd9Sstevel@tonic-gate break; 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate /* 3597c478bd9Sstevel@tonic-gate * A null ref asks for a new file 3607c478bd9Sstevel@tonic-gate * Non-null ref bumps the activation count 3617c478bd9Sstevel@tonic-gate * or gets next archive member 3627c478bd9Sstevel@tonic-gate */ 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate if (ref == 0) { 3657c478bd9Sstevel@tonic-gate if ((elf = _elf_regular(fd, flags)) == 0) 3667c478bd9Sstevel@tonic-gate return (0); 3677c478bd9Sstevel@tonic-gate } else { 3687c478bd9Sstevel@tonic-gate ELFWLOCK(ref); 3697c478bd9Sstevel@tonic-gate if ((ref->ed_myflags & flags) != flags) { 3707c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_RDWR, 0); 3717c478bd9Sstevel@tonic-gate ELFUNLOCK(ref); 3727c478bd9Sstevel@tonic-gate return (0); 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate /* 3757c478bd9Sstevel@tonic-gate * new activation ? 3767c478bd9Sstevel@tonic-gate */ 3777c478bd9Sstevel@tonic-gate if (ref->ed_kind != ELF_K_AR) { 3787c478bd9Sstevel@tonic-gate ++ref->ed_activ; 3797c478bd9Sstevel@tonic-gate ELFUNLOCK(ref); 3807c478bd9Sstevel@tonic-gate return (ref); 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate if ((elf = _elf_member(fd, ref, flags)) == 0) { 3837c478bd9Sstevel@tonic-gate ELFUNLOCK(ref); 3847c478bd9Sstevel@tonic-gate return (0); 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate ELFUNLOCK(ref); 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf)) 3907c478bd9Sstevel@tonic-gate elf->ed_activ = 1; 3917c478bd9Sstevel@tonic-gate elf = _elf_config(elf); 3927c478bd9Sstevel@tonic-gate NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf)) 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate return (elf); 3957c478bd9Sstevel@tonic-gate } 396