xref: /titanic_41/usr/src/cmd/sgs/libelf/common/update.c (revision c48875b94bd0268d4643aeefa7a37119085caf0e)
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
50bc07c75Srie  * Common Development and Distribution License (the "License").
60bc07c75Srie  * 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 /*
233c573fccSAli Bahrami  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277257d1b4Sraf /*
287257d1b4Sraf  *	Copyright (c) 1988 AT&T
297257d1b4Sraf  *	  All Rights Reserved
307257d1b4Sraf  */
317257d1b4Sraf 
327c478bd9Sstevel@tonic-gate #include <memory.h>
337c478bd9Sstevel@tonic-gate #include <malloc.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sgs.h>
377c478bd9Sstevel@tonic-gate #include "decl.h"
387c478bd9Sstevel@tonic-gate #include "msg.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * This module is compiled twice, the second time having
427c478bd9Sstevel@tonic-gate  * -D_ELF64 defined.  The following set of macros, along
437c478bd9Sstevel@tonic-gate  * with machelf.h, represent the differences between the
447c478bd9Sstevel@tonic-gate  * two compilations.  Be careful *not* to add any class-
457c478bd9Sstevel@tonic-gate  * dependent code (anything that has elf32 or elf64 in the
467c478bd9Sstevel@tonic-gate  * name) to this code without hiding it behind a switch-
477c478bd9Sstevel@tonic-gate  * able macro like these.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	FSZ_LONG	ELF64_FSZ_XWORD
527c478bd9Sstevel@tonic-gate #define	ELFCLASS	ELFCLASS64
537c478bd9Sstevel@tonic-gate #define	_elf_snode_init	_elf64_snode_init
547c478bd9Sstevel@tonic-gate #define	_elfxx_cookscn	_elf64_cookscn
557c478bd9Sstevel@tonic-gate #define	_elf_upd_lib	_elf64_upd_lib
567c478bd9Sstevel@tonic-gate #define	elf_fsize	elf64_fsize
577c478bd9Sstevel@tonic-gate #define	_elf_entsz	_elf64_entsz
587c478bd9Sstevel@tonic-gate #define	_elf_msize	_elf64_msize
597c478bd9Sstevel@tonic-gate #define	_elf_upd_usr	_elf64_upd_usr
607c478bd9Sstevel@tonic-gate #define	wrt		wrt64
617c478bd9Sstevel@tonic-gate #define	elf_xlatetof	elf64_xlatetof
627c478bd9Sstevel@tonic-gate #define	_elfxx_update	_elf64_update
63ba2be530Sab196087 #define	_elfxx_swap_wrimage	_elf64_swap_wrimage
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #else	/* ELF32 */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	FSZ_LONG	ELF32_FSZ_WORD
687c478bd9Sstevel@tonic-gate #define	ELFCLASS	ELFCLASS32
697c478bd9Sstevel@tonic-gate #define	_elf_snode_init	_elf32_snode_init
707c478bd9Sstevel@tonic-gate #define	_elfxx_cookscn	_elf32_cookscn
717c478bd9Sstevel@tonic-gate #define	_elf_upd_lib	_elf32_upd_lib
727c478bd9Sstevel@tonic-gate #define	elf_fsize	elf32_fsize
737c478bd9Sstevel@tonic-gate #define	_elf_entsz	_elf32_entsz
747c478bd9Sstevel@tonic-gate #define	_elf_msize	_elf32_msize
757c478bd9Sstevel@tonic-gate #define	_elf_upd_usr	_elf32_upd_usr
767c478bd9Sstevel@tonic-gate #define	wrt		wrt32
777c478bd9Sstevel@tonic-gate #define	elf_xlatetof	elf32_xlatetof
787c478bd9Sstevel@tonic-gate #define	_elfxx_update	_elf32_update
79ba2be530Sab196087 #define	_elfxx_swap_wrimage	_elf32_swap_wrimage
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #endif /* ELF64 */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 
84df14233eSab196087 #if	!(defined(_LP64) && defined(_ELF64))
85df14233eSab196087 #define	TEST_SIZE
86df14233eSab196087 
87df14233eSab196087 /*
88df14233eSab196087  * Handle the decision of whether the current linker can handle the
89df14233eSab196087  * desired object size, and if not, which error to issue.
90df14233eSab196087  *
91df14233eSab196087  * Input is the desired size. On failure, an error has been issued
92df14233eSab196087  * and 0 is returned. On success, 1 is returned.
93df14233eSab196087  */
94df14233eSab196087 static int
test_size(Lword hi)95df14233eSab196087 test_size(Lword hi)
96df14233eSab196087 {
97df14233eSab196087 #ifndef _LP64			/* 32-bit linker */
98df14233eSab196087 	/*
99df14233eSab196087 	 * A 32-bit libelf is limited to a 2GB output file. This limit
100df14233eSab196087 	 * is due to the fact that off_t is a signed value, and that
101df14233eSab196087 	 * libelf cannot support large file support:
102df14233eSab196087 	 *	- ABI reasons
103df14233eSab196087 	 *	- Memory use generally is 2x output file size anyway,
104df14233eSab196087 	 *		so lifting the file size limit will just send
105df14233eSab196087 	 *		you crashing into the 32-bit VM limit.
106df14233eSab196087 	 * If the output is an ELFCLASS64 object, or an ELFCLASS32 object
107df14233eSab196087 	 * under 4GB, switching to the 64-bit version of libelf will help.
108df14233eSab196087 	 * However, an ELFCLASS32 object must not exceed 4GB.
109df14233eSab196087 	 */
110df14233eSab196087 	if (hi > INT_MAX) {	/* Bigger than 2GB */
111df14233eSab196087 #ifndef _ELF64
112df14233eSab196087 		/* ELFCLASS32 object is fundamentally too big? */
113df14233eSab196087 		if (hi > UINT_MAX) {
114df14233eSab196087 			_elf_seterr(EFMT_FBIG_CLASS32, 0);
115df14233eSab196087 			return (0);
116df14233eSab196087 		}
117df14233eSab196087 #endif				/* _ELF64 */
118df14233eSab196087 
119df14233eSab196087 		/* Should switch to the 64-bit libelf? */
120df14233eSab196087 		_elf_seterr(EFMT_FBIG_LARGEFILE, 0);
121df14233eSab196087 		return (0);
122df14233eSab196087 	}
123df14233eSab196087 #endif				/* !_LP64 */
124df14233eSab196087 
125df14233eSab196087 
126df14233eSab196087 #if	defined(_LP64) && !defined(_ELF64)   /* 64-bit linker, ELFCLASS32 */
127df14233eSab196087 	/*
128df14233eSab196087 	 * A 64-bit linker can produce any size output
129df14233eSab196087 	 * file, but if the resulting file is ELFCLASS32,
130df14233eSab196087 	 * it must not exceed 4GB.
131df14233eSab196087 	 */
132df14233eSab196087 	if (hi > UINT_MAX) {
133df14233eSab196087 		_elf_seterr(EFMT_FBIG_CLASS32, 0);
134df14233eSab196087 		return (0);
135df14233eSab196087 	}
136df14233eSab196087 #endif
137df14233eSab196087 
138df14233eSab196087 	return (1);
139df14233eSab196087 }
140df14233eSab196087 #endif				/* TEST_SIZE */
141df14233eSab196087 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Output file update
1447c478bd9Sstevel@tonic-gate  *	These functions walk an Elf structure, update its information,
1457c478bd9Sstevel@tonic-gate  *	and optionally write the output file.  Because the application
1467c478bd9Sstevel@tonic-gate  *	may control of the output file layout, two upd_... routines
1477c478bd9Sstevel@tonic-gate  *	exist.  They're similar but too different to merge cleanly.
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  *	The library defines a "dirty" bit to force parts of the file
1507c478bd9Sstevel@tonic-gate  *	to be written on update.  These routines ignore the dirty bit
1517c478bd9Sstevel@tonic-gate  *	and do everything.  A minimal update routine might be useful
1527c478bd9Sstevel@tonic-gate  *	someday.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate static size_t
_elf_upd_lib(Elf * elf)1567c478bd9Sstevel@tonic-gate _elf_upd_lib(Elf * elf)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	NOTE(ASSUMING_PROTECTED(*elf))
1597c478bd9Sstevel@tonic-gate 	Lword		hi;
1607c478bd9Sstevel@tonic-gate 	Lword		hibit;
1617c478bd9Sstevel@tonic-gate 	Elf_Scn *	s;
162df14233eSab196087 	register Lword	sz;
1637c478bd9Sstevel@tonic-gate 	Ehdr *		eh = elf->ed_ehdr;
1647c478bd9Sstevel@tonic-gate 	unsigned	ver = eh->e_version;
1657c478bd9Sstevel@tonic-gate 	register char	*p = (char *)eh->e_ident;
1667c478bd9Sstevel@tonic-gate 	size_t		scncnt;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/*
1697c478bd9Sstevel@tonic-gate 	 * Ehdr and Phdr table go first
1707c478bd9Sstevel@tonic-gate 	 */
1717c478bd9Sstevel@tonic-gate 	p[EI_MAG0] = ELFMAG0;
1727c478bd9Sstevel@tonic-gate 	p[EI_MAG1] = ELFMAG1;
1737c478bd9Sstevel@tonic-gate 	p[EI_MAG2] = ELFMAG2;
1747c478bd9Sstevel@tonic-gate 	p[EI_MAG3] = ELFMAG3;
1757c478bd9Sstevel@tonic-gate 	p[EI_CLASS] = ELFCLASS;
1767c478bd9Sstevel@tonic-gate 	/* LINTED */
1777c478bd9Sstevel@tonic-gate 	p[EI_VERSION] = (Byte)ver;
1787c478bd9Sstevel@tonic-gate 	hi = elf_fsize(ELF_T_EHDR, 1, ver);
1797c478bd9Sstevel@tonic-gate 	/* LINTED */
1807c478bd9Sstevel@tonic-gate 	eh->e_ehsize = (Half)hi;
1817c478bd9Sstevel@tonic-gate 	if (eh->e_phnum != 0) {
1827c478bd9Sstevel@tonic-gate 		/* LINTED */
1837c478bd9Sstevel@tonic-gate 		eh->e_phentsize = (Half)elf_fsize(ELF_T_PHDR, 1, ver);
1847c478bd9Sstevel@tonic-gate 		/* LINTED */
1857c478bd9Sstevel@tonic-gate 		eh->e_phoff = (Off)hi;
1867c478bd9Sstevel@tonic-gate 		hi += eh->e_phentsize * eh->e_phnum;
1877c478bd9Sstevel@tonic-gate 	} else {
1887c478bd9Sstevel@tonic-gate 		eh->e_phoff = 0;
1897c478bd9Sstevel@tonic-gate 		eh->e_phentsize = 0;
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
1930bc07c75Srie 	 * Obtain the first section header.  Typically, this section has NULL
1940bc07c75Srie 	 * contents, however in the case of Extended ELF Sections this section
1950bc07c75Srie 	 * is used to hold an alternative e_shnum, e_shstrndx and e_phnum.
1960bc07c75Srie 	 * On initial allocation (see _elf_snode) the elements of this section
1970bc07c75Srie 	 * would have been zeroed.  The e_shnum is initialized later, after the
1980bc07c75Srie 	 * section header count has been determined.  The e_shstrndx and
1990bc07c75Srie 	 * e_phnum may have already been initialized by the caller (for example,
2000bc07c75Srie 	 * gelf_update_shdr() in mcs(1)).
2017c478bd9Sstevel@tonic-gate 	 */
2027c478bd9Sstevel@tonic-gate 	if ((s = elf->ed_hdscn) == 0) {
2037c478bd9Sstevel@tonic-gate 		eh->e_shnum = 0;
2047c478bd9Sstevel@tonic-gate 		scncnt = 0;
2057c478bd9Sstevel@tonic-gate 	} else {
2067c478bd9Sstevel@tonic-gate 		s = s->s_next;
2070bc07c75Srie 		scncnt = 1;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2100bc07c75Srie 	/*
2110bc07c75Srie 	 * Loop through sections.  Compute section size before changing hi.
2120bc07c75Srie 	 * Allow null buffers for NOBITS.
2130bc07c75Srie 	 */
2147c478bd9Sstevel@tonic-gate 	hibit = 0;
2157c478bd9Sstevel@tonic-gate 	for (; s != 0; s = s->s_next) {
2167c478bd9Sstevel@tonic-gate 		register Dnode	*d;
2177c478bd9Sstevel@tonic-gate 		register Lword	fsz, j;
2187c478bd9Sstevel@tonic-gate 		Shdr *sh = s->s_shdr;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		scncnt++;
2217c478bd9Sstevel@tonic-gate 		if (sh->sh_type == SHT_NULL) {
2227c478bd9Sstevel@tonic-gate 			*sh = _elf_snode_init.sb_shdr;
2237c478bd9Sstevel@tonic-gate 			continue;
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		if ((s->s_myflags & SF_READY) == 0)
2277c478bd9Sstevel@tonic-gate 			(void) _elfxx_cookscn(s);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		sh->sh_addralign = 1;
230df14233eSab196087 		if ((sz = (Lword)_elf_entsz(elf, sh->sh_type, ver)) != 0)
2317c478bd9Sstevel@tonic-gate 			/* LINTED */
2327c478bd9Sstevel@tonic-gate 			sh->sh_entsize = (Half)sz;
2337c478bd9Sstevel@tonic-gate 		sz = 0;
2347c478bd9Sstevel@tonic-gate 		for (d = s->s_hdnode; d != 0; d = d->db_next) {
2357c478bd9Sstevel@tonic-gate 			if ((fsz = elf_fsize(d->db_data.d_type,
2367c478bd9Sstevel@tonic-gate 			    1, ver)) == 0)
2377c478bd9Sstevel@tonic-gate 				return (0);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 			j = _elf_msize(d->db_data.d_type, ver);
2407c478bd9Sstevel@tonic-gate 			fsz *= (d->db_data.d_size / j);
2417c478bd9Sstevel@tonic-gate 			d->db_osz = (size_t)fsz;
2427c478bd9Sstevel@tonic-gate 			if ((j = d->db_data.d_align) > 1) {
2437c478bd9Sstevel@tonic-gate 				if (j > sh->sh_addralign)
2447c478bd9Sstevel@tonic-gate 					sh->sh_addralign = (Xword)j;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 				if (sz % j != 0)
2477c478bd9Sstevel@tonic-gate 					sz += j - sz % j;
2487c478bd9Sstevel@tonic-gate 			}
2497c478bd9Sstevel@tonic-gate 			d->db_data.d_off = (off_t)sz;
2507c478bd9Sstevel@tonic-gate 			d->db_xoff = sz;
251df14233eSab196087 			sz += fsz;
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 
254df14233eSab196087 		sh->sh_size = (Xword) sz;
2557c478bd9Sstevel@tonic-gate 		/*
2567c478bd9Sstevel@tonic-gate 		 * We want to take into account the offsets for NOBITS
2577c478bd9Sstevel@tonic-gate 		 * sections and let the "sh_offsets" point to where
2587c478bd9Sstevel@tonic-gate 		 * the section would 'conceptually' fit within
2597c478bd9Sstevel@tonic-gate 		 * the file (as required by the ABI).
2607c478bd9Sstevel@tonic-gate 		 *
2617c478bd9Sstevel@tonic-gate 		 * But - we must also make sure that the NOBITS does
2627c478bd9Sstevel@tonic-gate 		 * not take up any actual space in the file.  We preserve
2637c478bd9Sstevel@tonic-gate 		 * the actual offset into the file in the 'hibit' variable.
2647c478bd9Sstevel@tonic-gate 		 * When we come to the first non-NOBITS section after a
2657c478bd9Sstevel@tonic-gate 		 * encountering a NOBITS section the hi counter is restored
2667c478bd9Sstevel@tonic-gate 		 * to its proper place in the file.
2677c478bd9Sstevel@tonic-gate 		 */
2687c478bd9Sstevel@tonic-gate 		if (sh->sh_type == SHT_NOBITS) {
2697c478bd9Sstevel@tonic-gate 			if (hibit == 0)
2707c478bd9Sstevel@tonic-gate 				hibit = hi;
2717c478bd9Sstevel@tonic-gate 		} else {
2727c478bd9Sstevel@tonic-gate 			if (hibit) {
2737c478bd9Sstevel@tonic-gate 				hi = hibit;
2747c478bd9Sstevel@tonic-gate 				hibit = 0;
2757c478bd9Sstevel@tonic-gate 			}
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 		j = sh->sh_addralign;
2787c478bd9Sstevel@tonic-gate 		if ((fsz = hi % j) != 0)
2797c478bd9Sstevel@tonic-gate 			hi += j - fsz;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		/* LINTED */
2827c478bd9Sstevel@tonic-gate 		sh->sh_offset = (Off)hi;
2837c478bd9Sstevel@tonic-gate 		hi += sz;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/*
2877c478bd9Sstevel@tonic-gate 	 * if last section was a 'NOBITS' section then we need to
2887c478bd9Sstevel@tonic-gate 	 * restore the 'hi' counter to point to the end of the last
2897c478bd9Sstevel@tonic-gate 	 * non 'NOBITS' section.
2907c478bd9Sstevel@tonic-gate 	 */
2917c478bd9Sstevel@tonic-gate 	if (hibit) {
2927c478bd9Sstevel@tonic-gate 		hi = hibit;
2937c478bd9Sstevel@tonic-gate 		hibit = 0;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * Shdr table last
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	if (scncnt != 0) {
3007c478bd9Sstevel@tonic-gate 		if (hi % FSZ_LONG != 0)
3017c478bd9Sstevel@tonic-gate 			hi += FSZ_LONG - hi % FSZ_LONG;
3027c478bd9Sstevel@tonic-gate 		/* LINTED */
3037c478bd9Sstevel@tonic-gate 		eh->e_shoff = (Off)hi;
3047c478bd9Sstevel@tonic-gate 		/*
3057c478bd9Sstevel@tonic-gate 		 * If we are using 'extended sections' then the
3067c478bd9Sstevel@tonic-gate 		 * e_shnum is stored in the sh_size field of the
3077c478bd9Sstevel@tonic-gate 		 * first section header.
3087c478bd9Sstevel@tonic-gate 		 *
3097c478bd9Sstevel@tonic-gate 		 * NOTE: we set e_shnum to '0' because it's specified
3107c478bd9Sstevel@tonic-gate 		 * this way in the gABI, and in the hopes that
3117c478bd9Sstevel@tonic-gate 		 * this will cause less problems to unaware
3127c478bd9Sstevel@tonic-gate 		 * tools then if we'd set it to SHN_XINDEX (0xffff).
3137c478bd9Sstevel@tonic-gate 		 */
3147c478bd9Sstevel@tonic-gate 		if (scncnt < SHN_LORESERVE)
3157c478bd9Sstevel@tonic-gate 			eh->e_shnum = scncnt;
3167c478bd9Sstevel@tonic-gate 		else {
3177c478bd9Sstevel@tonic-gate 			Shdr	*sh;
3187c478bd9Sstevel@tonic-gate 			sh = (Shdr *)elf->ed_hdscn->s_shdr;
3197c478bd9Sstevel@tonic-gate 			sh->sh_size = scncnt;
3207c478bd9Sstevel@tonic-gate 			eh->e_shnum = 0;
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 		/* LINTED */
3237c478bd9Sstevel@tonic-gate 		eh->e_shentsize = (Half)elf_fsize(ELF_T_SHDR, 1, ver);
3247c478bd9Sstevel@tonic-gate 		hi += eh->e_shentsize * scncnt;
3257c478bd9Sstevel@tonic-gate 	} else {
3267c478bd9Sstevel@tonic-gate 		eh->e_shoff = 0;
3277c478bd9Sstevel@tonic-gate 		eh->e_shentsize = 0;
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
330df14233eSab196087 #ifdef TEST_SIZE
331df14233eSab196087 	if (test_size(hi) == 0)
3327c478bd9Sstevel@tonic-gate 		return (0);
3337c478bd9Sstevel@tonic-gate #endif
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	return ((size_t)hi);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate static size_t
_elf_upd_usr(Elf * elf)3417c478bd9Sstevel@tonic-gate _elf_upd_usr(Elf * elf)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	NOTE(ASSUMING_PROTECTED(*elf))
3447c478bd9Sstevel@tonic-gate 	Lword		hi;
3457c478bd9Sstevel@tonic-gate 	Elf_Scn *	s;
346df14233eSab196087 	register Lword	sz;
3477c478bd9Sstevel@tonic-gate 	Ehdr *		eh = elf->ed_ehdr;
3487c478bd9Sstevel@tonic-gate 	unsigned	ver = eh->e_version;
3497c478bd9Sstevel@tonic-gate 	register char	*p = (char *)eh->e_ident;
350*c48875b9SRichard Lowe 	size_t		scncnt;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/*
3537c478bd9Sstevel@tonic-gate 	 * Ehdr and Phdr table go first
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 	p[EI_MAG0] = ELFMAG0;
3567c478bd9Sstevel@tonic-gate 	p[EI_MAG1] = ELFMAG1;
3577c478bd9Sstevel@tonic-gate 	p[EI_MAG2] = ELFMAG2;
3587c478bd9Sstevel@tonic-gate 	p[EI_MAG3] = ELFMAG3;
3597c478bd9Sstevel@tonic-gate 	p[EI_CLASS] = ELFCLASS;
3607c478bd9Sstevel@tonic-gate 	/* LINTED */
3617c478bd9Sstevel@tonic-gate 	p[EI_VERSION] = (Byte)ver;
3627c478bd9Sstevel@tonic-gate 	hi = elf_fsize(ELF_T_EHDR, 1, ver);
3637c478bd9Sstevel@tonic-gate 	/* LINTED */
3647c478bd9Sstevel@tonic-gate 	eh->e_ehsize = (Half)hi;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * If phnum is zero, phoff "should" be zero too,
3687c478bd9Sstevel@tonic-gate 	 * but the application is responsible for it.
3697c478bd9Sstevel@tonic-gate 	 * Allow a non-zero value here and update the
3707c478bd9Sstevel@tonic-gate 	 * hi water mark accordingly.
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (eh->e_phnum != 0)
3747c478bd9Sstevel@tonic-gate 		/* LINTED */
3757c478bd9Sstevel@tonic-gate 		eh->e_phentsize = (Half)elf_fsize(ELF_T_PHDR, 1, ver);
3767c478bd9Sstevel@tonic-gate 	else
3777c478bd9Sstevel@tonic-gate 		eh->e_phentsize = 0;
3787c478bd9Sstevel@tonic-gate 	if ((sz = eh->e_phoff + eh->e_phentsize * eh->e_phnum) > hi)
3797c478bd9Sstevel@tonic-gate 		hi = sz;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * Loop through sections, skipping index zero.
3837c478bd9Sstevel@tonic-gate 	 * Compute section size before changing hi.
3847c478bd9Sstevel@tonic-gate 	 * Allow null buffers for NOBITS.
3857c478bd9Sstevel@tonic-gate 	 */
3867c478bd9Sstevel@tonic-gate 
387*c48875b9SRichard Lowe 	if ((s = elf->ed_hdscn) == 0) {
3887c478bd9Sstevel@tonic-gate 		eh->e_shnum = 0;
389*c48875b9SRichard Lowe 		scncnt = 0;
390*c48875b9SRichard Lowe 	} else {
391*c48875b9SRichard Lowe 		scncnt = 1;
3927c478bd9Sstevel@tonic-gate 		s = s->s_next;
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 	for (; s != 0; s = s->s_next) {
3957c478bd9Sstevel@tonic-gate 		register Dnode	*d;
396df14233eSab196087 		register Lword	fsz, j;
3977c478bd9Sstevel@tonic-gate 		Shdr *sh = s->s_shdr;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		if ((s->s_myflags & SF_READY) == 0)
4007c478bd9Sstevel@tonic-gate 			(void) _elfxx_cookscn(s);
4017c478bd9Sstevel@tonic-gate 
402*c48875b9SRichard Lowe 		++scncnt;
4037c478bd9Sstevel@tonic-gate 		sz = 0;
4047c478bd9Sstevel@tonic-gate 		for (d = s->s_hdnode; d != 0; d = d->db_next) {
405df14233eSab196087 			if ((fsz = elf_fsize(d->db_data.d_type, 1,
4067c478bd9Sstevel@tonic-gate 			    ver)) == 0)
4077c478bd9Sstevel@tonic-gate 				return (0);
408df14233eSab196087 			j = _elf_msize(d->db_data.d_type, ver);
409df14233eSab196087 			fsz *= (d->db_data.d_size / j);
4107c478bd9Sstevel@tonic-gate 			d->db_osz = (size_t)fsz;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 			if ((sh->sh_type != SHT_NOBITS) &&
413df14233eSab196087 			    ((j = (d->db_data.d_off + d->db_osz)) > sz))
4147c478bd9Sstevel@tonic-gate 				sz = j;
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 		if (sh->sh_size < sz) {
4177c478bd9Sstevel@tonic-gate 			_elf_seterr(EFMT_SCNSZ, 0);
4187c478bd9Sstevel@tonic-gate 			return (0);
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 		if ((sh->sh_type != SHT_NOBITS) &&
4217c478bd9Sstevel@tonic-gate 		    (hi < sh->sh_offset + sh->sh_size))
4227c478bd9Sstevel@tonic-gate 			hi = sh->sh_offset + sh->sh_size;
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * Shdr table last.  Comment above for phnum/phoff applies here.
4277c478bd9Sstevel@tonic-gate 	 */
428*c48875b9SRichard Lowe 	if (scncnt != 0) {
4297c478bd9Sstevel@tonic-gate 		/* LINTED */
4307c478bd9Sstevel@tonic-gate 		eh->e_shentsize = (Half)elf_fsize(ELF_T_SHDR, 1, ver);
431*c48875b9SRichard Lowe 		if (scncnt < SHN_LORESERVE) {
432*c48875b9SRichard Lowe 			eh->e_shnum = scncnt;
433*c48875b9SRichard Lowe 		} else {
434*c48875b9SRichard Lowe 			Shdr *sh;
435*c48875b9SRichard Lowe 			sh = (Shdr *)elf->ed_hdscn->s_shdr;
436*c48875b9SRichard Lowe 			sh->sh_size = scncnt;
437*c48875b9SRichard Lowe 			eh->e_shnum = 0;
438*c48875b9SRichard Lowe 		}
439*c48875b9SRichard Lowe 	} else {
4407c478bd9Sstevel@tonic-gate 		eh->e_shentsize = 0;
441*c48875b9SRichard Lowe 	}
4427c478bd9Sstevel@tonic-gate 
443*c48875b9SRichard Lowe 	if ((sz = eh->e_shoff + eh->e_shentsize * scncnt) > hi)
4447c478bd9Sstevel@tonic-gate 		hi = sz;
4457c478bd9Sstevel@tonic-gate 
446df14233eSab196087 #ifdef TEST_SIZE
447df14233eSab196087 	if (test_size(hi) == 0)
4487c478bd9Sstevel@tonic-gate 		return (0);
4497c478bd9Sstevel@tonic-gate #endif
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	return ((size_t)hi);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate static size_t
wrt(Elf * elf,Xword outsz,unsigned fill,int update_cmd)4567c478bd9Sstevel@tonic-gate wrt(Elf * elf, Xword outsz, unsigned fill, int update_cmd)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	NOTE(ASSUMING_PROTECTED(*elf))
4597c478bd9Sstevel@tonic-gate 	Elf_Data		dst, src;
4607c478bd9Sstevel@tonic-gate 	unsigned		flag;
4617c478bd9Sstevel@tonic-gate 	Xword			hi, sz;
4627c478bd9Sstevel@tonic-gate 	char			*image;
4637c478bd9Sstevel@tonic-gate 	Elf_Scn			*s;
4647c478bd9Sstevel@tonic-gate 	Ehdr			*eh = elf->ed_ehdr;
4657c478bd9Sstevel@tonic-gate 	unsigned		ver = eh->e_version;
466ba2be530Sab196087 	unsigned		encode;
4677c478bd9Sstevel@tonic-gate 	int			byte;
4683c573fccSAli Bahrami 	_elf_execfill_func_t	*execfill_func;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
471ba2be530Sab196087 	 * If this is an ELF_C_WRIMAGE write, then we encode into the
472ba2be530Sab196087 	 * byte order of the system we are running on rather than that of
473ba2be530Sab196087 	 * of the object. For ld.so.1, this is the same order, but
474ba2be530Sab196087 	 * for 'ld', it might not be in the case where we are cross
475ba2be530Sab196087 	 * linking an object for a different target. In this later case,
476ba2be530Sab196087 	 * the linker-host byte order is necessary so that the linker can
477ba2be530Sab196087 	 * manipulate the resulting  image. It is expected that the linker
478ba2be530Sab196087 	 * will call elf_swap_wrimage() if necessary to convert the image
479ba2be530Sab196087 	 * to the target byte order.
480ba2be530Sab196087 	 */
481ba2be530Sab196087 	encode = (update_cmd == ELF_C_WRIMAGE) ? _elf_sys_encoding() :
482ba2be530Sab196087 	    eh->e_ident[EI_DATA];
483ba2be530Sab196087 
484ba2be530Sab196087 	/*
4857c478bd9Sstevel@tonic-gate 	 * Two issues can cause trouble for the output file.
4867c478bd9Sstevel@tonic-gate 	 * First, begin() with ELF_C_RDWR opens a file for both
4877c478bd9Sstevel@tonic-gate 	 * read and write.  On the write update(), the library
4887c478bd9Sstevel@tonic-gate 	 * has to read everything it needs before truncating
4897c478bd9Sstevel@tonic-gate 	 * the file.  Second, using mmap for both read and write
4907c478bd9Sstevel@tonic-gate 	 * is too tricky.  Consequently, the library disables mmap
4917c478bd9Sstevel@tonic-gate 	 * on the read side.  Using mmap for the output saves swap
4927c478bd9Sstevel@tonic-gate 	 * space, because that mapping is SHARED, not PRIVATE.
4937c478bd9Sstevel@tonic-gate 	 *
4947c478bd9Sstevel@tonic-gate 	 * If the file is write-only, there can be nothing of
4957c478bd9Sstevel@tonic-gate 	 * interest to bother with.
4967c478bd9Sstevel@tonic-gate 	 *
4977c478bd9Sstevel@tonic-gate 	 * The following reads the entire file, which might be
4987c478bd9Sstevel@tonic-gate 	 * more than necessary.  Better safe than sorry.
4997c478bd9Sstevel@tonic-gate 	 */
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if ((elf->ed_myflags & EDF_READ) &&
5027c478bd9Sstevel@tonic-gate 	    (_elf_vm(elf, (size_t)0, elf->ed_fsz) != OK_YES))
5037c478bd9Sstevel@tonic-gate 		return (0);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	flag = elf->ed_myflags & EDF_WRALLOC;
5067c478bd9Sstevel@tonic-gate 	if ((image = _elf_outmap(elf->ed_fd, outsz, &flag)) == 0)
5077c478bd9Sstevel@tonic-gate 		return (0);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if (flag == 0)
5107c478bd9Sstevel@tonic-gate 		elf->ed_myflags |= EDF_IMALLOC;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * If an error occurs below, a "dirty" bit may be cleared
5147c478bd9Sstevel@tonic-gate 	 * improperly.  To save a second pass through the file,
5157c478bd9Sstevel@tonic-gate 	 * this code sets the dirty bit on the elf descriptor
5167c478bd9Sstevel@tonic-gate 	 * when an error happens, assuming that will "cover" any
5177c478bd9Sstevel@tonic-gate 	 * accidents.
5187c478bd9Sstevel@tonic-gate 	 */
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * Hi is needed only when 'fill' is non-zero.
5227c478bd9Sstevel@tonic-gate 	 * Fill is non-zero only when the library
5237c478bd9Sstevel@tonic-gate 	 * calculates file/section/data buffer offsets.
5247c478bd9Sstevel@tonic-gate 	 * The lib guarantees they increase monotonically.
5257c478bd9Sstevel@tonic-gate 	 * That guarantees proper filling below.
5267c478bd9Sstevel@tonic-gate 	 */
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/*
5307c478bd9Sstevel@tonic-gate 	 * Ehdr first
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	src.d_buf = (Elf_Void *)eh;
5347c478bd9Sstevel@tonic-gate 	src.d_type = ELF_T_EHDR;
5357c478bd9Sstevel@tonic-gate 	src.d_size = sizeof (Ehdr);
5367c478bd9Sstevel@tonic-gate 	src.d_version = EV_CURRENT;
5377c478bd9Sstevel@tonic-gate 	dst.d_buf = (Elf_Void *)image;
5387c478bd9Sstevel@tonic-gate 	dst.d_size = eh->e_ehsize;
5397c478bd9Sstevel@tonic-gate 	dst.d_version = ver;
5407c478bd9Sstevel@tonic-gate 	if (elf_xlatetof(&dst, &src, encode) == 0)
5417c478bd9Sstevel@tonic-gate 		return (0);
5427c478bd9Sstevel@tonic-gate 	elf->ed_ehflags &= ~ELF_F_DIRTY;
5437c478bd9Sstevel@tonic-gate 	hi = eh->e_ehsize;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	 * Phdr table if one exists
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (eh->e_phnum != 0) {
5507c478bd9Sstevel@tonic-gate 		unsigned	work;
5517c478bd9Sstevel@tonic-gate 		/*
5527c478bd9Sstevel@tonic-gate 		 * Unlike other library data, phdr table is
5537c478bd9Sstevel@tonic-gate 		 * in the user version.  Change src buffer
5547c478bd9Sstevel@tonic-gate 		 * version here, fix it after translation.
5557c478bd9Sstevel@tonic-gate 		 */
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 		src.d_buf = (Elf_Void *)elf->ed_phdr;
5587c478bd9Sstevel@tonic-gate 		src.d_type = ELF_T_PHDR;
5597c478bd9Sstevel@tonic-gate 		src.d_size = elf->ed_phdrsz;
5607c478bd9Sstevel@tonic-gate 		ELFACCESSDATA(work, _elf_work)
5617c478bd9Sstevel@tonic-gate 		src.d_version = work;
5627c478bd9Sstevel@tonic-gate 		dst.d_buf = (Elf_Void *)(image + eh->e_phoff);
5637c478bd9Sstevel@tonic-gate 		dst.d_size = eh->e_phnum * eh->e_phentsize;
5647c478bd9Sstevel@tonic-gate 		hi = (Xword)(eh->e_phoff + dst.d_size);
5657c478bd9Sstevel@tonic-gate 		if (elf_xlatetof(&dst, &src, encode) == 0) {
5667c478bd9Sstevel@tonic-gate 			elf->ed_uflags |= ELF_F_DIRTY;
5677c478bd9Sstevel@tonic-gate 			return (0);
5687c478bd9Sstevel@tonic-gate 		}
5697c478bd9Sstevel@tonic-gate 		elf->ed_phflags &= ~ELF_F_DIRTY;
5707c478bd9Sstevel@tonic-gate 		src.d_version = EV_CURRENT;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	/*
5747c478bd9Sstevel@tonic-gate 	 * Loop through sections
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	ELFACCESSDATA(byte, _elf_byte);
5783c573fccSAli Bahrami 	ELFACCESSDATA(execfill_func, _elf_execfill_func);
5797c478bd9Sstevel@tonic-gate 	for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
5807c478bd9Sstevel@tonic-gate 		register Dnode	*d, *prevd;
5817c478bd9Sstevel@tonic-gate 		Xword		off = 0;
5827c478bd9Sstevel@tonic-gate 		Shdr		*sh = s->s_shdr;
5837c478bd9Sstevel@tonic-gate 		char		*start = image + sh->sh_offset;
5847c478bd9Sstevel@tonic-gate 		char		*here;
5853c573fccSAli Bahrami 		_elf_execfill_func_t	*execfill;
5863c573fccSAli Bahrami 
5873c573fccSAli Bahrami 		/* Only use the execfill function on SHF_EXECINSTR sections */
5883c573fccSAli Bahrami 		execfill = (sh->sh_flags & SHF_EXECINSTR) ?
5893c573fccSAli Bahrami 		    execfill_func : NULL;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		/*
5927c478bd9Sstevel@tonic-gate 		 * Just "clean" DIRTY flag for "empty" sections.  Even if
5937c478bd9Sstevel@tonic-gate 		 * NOBITS needs padding, the next thing in the
5947c478bd9Sstevel@tonic-gate 		 * file will provide it.  (And if this NOBITS is
5957c478bd9Sstevel@tonic-gate 		 * the last thing in the file, no padding needed.)
5967c478bd9Sstevel@tonic-gate 		 */
5977c478bd9Sstevel@tonic-gate 		if ((sh->sh_type == SHT_NOBITS) ||
5987c478bd9Sstevel@tonic-gate 		    (sh->sh_type == SHT_NULL)) {
5997c478bd9Sstevel@tonic-gate 			d = s->s_hdnode, prevd = 0;
6007c478bd9Sstevel@tonic-gate 			for (; d != 0; prevd = d, d = d->db_next)
6017c478bd9Sstevel@tonic-gate 				d->db_uflags &= ~ELF_F_DIRTY;
6027c478bd9Sstevel@tonic-gate 			continue;
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 		/*
6057c478bd9Sstevel@tonic-gate 		 * Clear out the memory between the end of the last
6067c478bd9Sstevel@tonic-gate 		 * section and the begining of this section.
6077c478bd9Sstevel@tonic-gate 		 */
6087c478bd9Sstevel@tonic-gate 		if (fill && (sh->sh_offset > hi)) {
6097c478bd9Sstevel@tonic-gate 			sz = sh->sh_offset - hi;
6107c478bd9Sstevel@tonic-gate 			(void) memset(start - sz, byte, sz);
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		for (d = s->s_hdnode, prevd = 0;
6157c478bd9Sstevel@tonic-gate 		    d != 0; prevd = d, d = d->db_next) {
6167c478bd9Sstevel@tonic-gate 			d->db_uflags &= ~ELF_F_DIRTY;
6177c478bd9Sstevel@tonic-gate 			here = start + d->db_data.d_off;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 			/*
6207c478bd9Sstevel@tonic-gate 			 * Clear out the memory between the end of the
6217c478bd9Sstevel@tonic-gate 			 * last update and the start of this data buffer.
6223c573fccSAli Bahrami 			 *
6233c573fccSAli Bahrami 			 * These buffers represent input sections that have
6243c573fccSAli Bahrami 			 * been concatenated into an output section, so if
6253c573fccSAli Bahrami 			 * the output section is executable (SHF_EXECINSTR)
6263c573fccSAli Bahrami 			 * and a fill function has been registered, use the
6273c573fccSAli Bahrami 			 * function. Otherwise, use the fill byte.
6287c478bd9Sstevel@tonic-gate 			 */
6297c478bd9Sstevel@tonic-gate 			if (fill && (d->db_data.d_off > off)) {
6307c478bd9Sstevel@tonic-gate 				sz = (Xword)(d->db_data.d_off - off);
6313c573fccSAli Bahrami 				if (execfill != NULL)
6323c573fccSAli Bahrami 					(* execfill)(start,
6333c573fccSAli Bahrami 					    here - start - sz, sz);
6343c573fccSAli Bahrami 				else
6357c478bd9Sstevel@tonic-gate 					(void) memset(here - sz, byte, sz);
6367c478bd9Sstevel@tonic-gate 			}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 			if ((d->db_myflags & DBF_READY) == 0) {
6397c478bd9Sstevel@tonic-gate 				SCNLOCK(s);
6407c478bd9Sstevel@tonic-gate 				if (_elf_locked_getdata(s, &prevd->db_data) !=
6417c478bd9Sstevel@tonic-gate 				    &d->db_data) {
6427c478bd9Sstevel@tonic-gate 					elf->ed_uflags |= ELF_F_DIRTY;
6437c478bd9Sstevel@tonic-gate 					SCNUNLOCK(s);
6447c478bd9Sstevel@tonic-gate 					return (0);
6457c478bd9Sstevel@tonic-gate 				}
6467c478bd9Sstevel@tonic-gate 				SCNUNLOCK(s);
6477c478bd9Sstevel@tonic-gate 			}
6487c478bd9Sstevel@tonic-gate 			dst.d_buf = (Elf_Void *)here;
6497c478bd9Sstevel@tonic-gate 			dst.d_size = d->db_osz;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 			/*
6527c478bd9Sstevel@tonic-gate 			 * Copy the translated bits out to the destination
6537c478bd9Sstevel@tonic-gate 			 * image.
6547c478bd9Sstevel@tonic-gate 			 */
6557c478bd9Sstevel@tonic-gate 			if (elf_xlatetof(&dst, &d->db_data, encode) == 0) {
6567c478bd9Sstevel@tonic-gate 				elf->ed_uflags |= ELF_F_DIRTY;
6577c478bd9Sstevel@tonic-gate 				return (0);
6587c478bd9Sstevel@tonic-gate 			}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 			off = (Xword)(d->db_data.d_off + dst.d_size);
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 		hi = sh->sh_offset + sh->sh_size;
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	/*
6667c478bd9Sstevel@tonic-gate 	 * Shdr table last
6677c478bd9Sstevel@tonic-gate 	 */
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if (fill && (eh->e_shoff > hi)) {
6707c478bd9Sstevel@tonic-gate 		sz = eh->e_shoff - hi;
6717c478bd9Sstevel@tonic-gate 		(void) memset(image + hi, byte, sz);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	src.d_type = ELF_T_SHDR;
6757c478bd9Sstevel@tonic-gate 	src.d_size = sizeof (Shdr);
6767c478bd9Sstevel@tonic-gate 	dst.d_buf = (Elf_Void *)(image + eh->e_shoff);
6777c478bd9Sstevel@tonic-gate 	dst.d_size = eh->e_shentsize;
6787c478bd9Sstevel@tonic-gate 	for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
6797c478bd9Sstevel@tonic-gate 		assert((uintptr_t)dst.d_buf < ((uintptr_t)image + outsz));
6807c478bd9Sstevel@tonic-gate 		s->s_shflags &= ~ELF_F_DIRTY;
6817c478bd9Sstevel@tonic-gate 		s->s_uflags &= ~ELF_F_DIRTY;
6827c478bd9Sstevel@tonic-gate 		src.d_buf = s->s_shdr;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		if (elf_xlatetof(&dst, &src, encode) == 0) {
6857c478bd9Sstevel@tonic-gate 			elf->ed_uflags |= ELF_F_DIRTY;
6867c478bd9Sstevel@tonic-gate 			return (0);
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		dst.d_buf = (char *)dst.d_buf + eh->e_shentsize;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * ELF_C_WRIMAGE signifyes that we build the memory image, but
6937c478bd9Sstevel@tonic-gate 	 * that we do not actually write it to disk.  This is used
6947c478bd9Sstevel@tonic-gate 	 * by ld(1) to build up a full image of an elf file and then
6957c478bd9Sstevel@tonic-gate 	 * to process the file before it's actually written out to
6967c478bd9Sstevel@tonic-gate 	 * disk.  This saves ld(1) the overhead of having to write
6977c478bd9Sstevel@tonic-gate 	 * the image out to disk twice.
6987c478bd9Sstevel@tonic-gate 	 */
6997c478bd9Sstevel@tonic-gate 	if (update_cmd == ELF_C_WRIMAGE) {
7007c478bd9Sstevel@tonic-gate 		elf->ed_uflags &= ~ELF_F_DIRTY;
7017c478bd9Sstevel@tonic-gate 		elf->ed_wrimage = image;
7027c478bd9Sstevel@tonic-gate 		elf->ed_wrimagesz = outsz;
7037c478bd9Sstevel@tonic-gate 		return (outsz);
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	if (_elf_outsync(elf->ed_fd, image, outsz,
7077c478bd9Sstevel@tonic-gate 	    ((elf->ed_myflags & EDF_IMALLOC) ? 0 : 1)) != 0) {
7087c478bd9Sstevel@tonic-gate 		elf->ed_uflags &= ~ELF_F_DIRTY;
7097c478bd9Sstevel@tonic-gate 		elf->ed_myflags &= ~EDF_IMALLOC;
7107c478bd9Sstevel@tonic-gate 		return (outsz);
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	elf->ed_uflags |= ELF_F_DIRTY;
7147c478bd9Sstevel@tonic-gate 	return (0);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate /*
7217c478bd9Sstevel@tonic-gate  * The following is a private interface between the linkers (ld & ld.so.1)
7227c478bd9Sstevel@tonic-gate  * and libelf:
7237c478bd9Sstevel@tonic-gate  *
7247c478bd9Sstevel@tonic-gate  * elf_update(elf, ELF_C_WRIMAGE)
7257c478bd9Sstevel@tonic-gate  *	This will cause full image representing the elf file
7267c478bd9Sstevel@tonic-gate  *	described by the elf pointer to be built in memory.  If the
7277c478bd9Sstevel@tonic-gate  *	elf pointer has a valid file descriptor associated with it
7287c478bd9Sstevel@tonic-gate  *	we will attempt to build the memory image from mmap()'ed
7297c478bd9Sstevel@tonic-gate  *	storage.  If the elf descriptor does not have a valid
7307c478bd9Sstevel@tonic-gate  *	file descriptor (opened with elf_begin(0, ELF_C_IMAGE, 0))
7317c478bd9Sstevel@tonic-gate  *	then the image will be allocated from dynamic memory (malloc()).
7327c478bd9Sstevel@tonic-gate  *
7337c478bd9Sstevel@tonic-gate  *	elf_update() will return the size of the memory image built
7347c478bd9Sstevel@tonic-gate  *	when sucessful.
7357c478bd9Sstevel@tonic-gate  *
7367c478bd9Sstevel@tonic-gate  *	When a subsequent call to elf_update() with ELF_C_WRITE as
7377c478bd9Sstevel@tonic-gate  *	the command is performed it will sync the image created
7387c478bd9Sstevel@tonic-gate  *	by ELF_C_WRIMAGE to disk (if fd available) and
7397c478bd9Sstevel@tonic-gate  *	free the memory allocated.
7407c478bd9Sstevel@tonic-gate  */
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate off_t
_elfxx_update(Elf * elf,Elf_Cmd cmd)7437c478bd9Sstevel@tonic-gate _elfxx_update(Elf * elf, Elf_Cmd cmd)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	size_t		sz;
7467c478bd9Sstevel@tonic-gate 	unsigned	u;
7477c478bd9Sstevel@tonic-gate 	Ehdr		*eh = elf->ed_ehdr;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if (elf == 0)
7507c478bd9Sstevel@tonic-gate 		return (-1);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	ELFWLOCK(elf)
7537c478bd9Sstevel@tonic-gate 	switch (cmd) {
7547c478bd9Sstevel@tonic-gate 	default:
7557c478bd9Sstevel@tonic-gate 		_elf_seterr(EREQ_UPDATE, 0);
7567c478bd9Sstevel@tonic-gate 		ELFUNLOCK(elf)
7577c478bd9Sstevel@tonic-gate 		return (-1);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	case ELF_C_WRIMAGE:
7607c478bd9Sstevel@tonic-gate 		if ((elf->ed_myflags & EDF_WRITE) == 0) {
7617c478bd9Sstevel@tonic-gate 			_elf_seterr(EREQ_UPDWRT, 0);
7627c478bd9Sstevel@tonic-gate 			ELFUNLOCK(elf)
7637c478bd9Sstevel@tonic-gate 			return (-1);
7647c478bd9Sstevel@tonic-gate 		}
7657c478bd9Sstevel@tonic-gate 		break;
7667c478bd9Sstevel@tonic-gate 	case ELF_C_WRITE:
7677c478bd9Sstevel@tonic-gate 		if ((elf->ed_myflags & EDF_WRITE) == 0) {
7687c478bd9Sstevel@tonic-gate 			_elf_seterr(EREQ_UPDWRT, 0);
7697c478bd9Sstevel@tonic-gate 			ELFUNLOCK(elf)
7707c478bd9Sstevel@tonic-gate 			return (-1);
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 		if (elf->ed_wrimage) {
7737c478bd9Sstevel@tonic-gate 			if (elf->ed_myflags & EDF_WRALLOC) {
7747c478bd9Sstevel@tonic-gate 				free(elf->ed_wrimage);
7757c478bd9Sstevel@tonic-gate 				/*
7767c478bd9Sstevel@tonic-gate 				 * The size is still returned even
7777c478bd9Sstevel@tonic-gate 				 * though nothing is actually written
7787c478bd9Sstevel@tonic-gate 				 * out.  This is just to be consistant
7797c478bd9Sstevel@tonic-gate 				 * with the rest of the interface.
7807c478bd9Sstevel@tonic-gate 				 */
7817c478bd9Sstevel@tonic-gate 				sz = elf->ed_wrimagesz;
7827c478bd9Sstevel@tonic-gate 				elf->ed_wrimage = 0;
7837c478bd9Sstevel@tonic-gate 				elf->ed_wrimagesz = 0;
7847c478bd9Sstevel@tonic-gate 				ELFUNLOCK(elf);
7857c478bd9Sstevel@tonic-gate 				return ((off_t)sz);
7867c478bd9Sstevel@tonic-gate 			}
7877c478bd9Sstevel@tonic-gate 			sz = _elf_outsync(elf->ed_fd, elf->ed_wrimage,
7887c478bd9Sstevel@tonic-gate 			    elf->ed_wrimagesz,
7897c478bd9Sstevel@tonic-gate 			    (elf->ed_myflags & EDF_IMALLOC ? 0 : 1));
7907c478bd9Sstevel@tonic-gate 			elf->ed_myflags &= ~EDF_IMALLOC;
7917c478bd9Sstevel@tonic-gate 			elf->ed_wrimage = 0;
7927c478bd9Sstevel@tonic-gate 			elf->ed_wrimagesz = 0;
7937c478bd9Sstevel@tonic-gate 			ELFUNLOCK(elf);
7947c478bd9Sstevel@tonic-gate 			return ((off_t)sz);
7957c478bd9Sstevel@tonic-gate 		}
7967c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
7977c478bd9Sstevel@tonic-gate 	case ELF_C_NULL:
7987c478bd9Sstevel@tonic-gate 		break;
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (eh == 0) {
8027c478bd9Sstevel@tonic-gate 		_elf_seterr(ESEQ_EHDR, 0);
8037c478bd9Sstevel@tonic-gate 		ELFUNLOCK(elf)
8047c478bd9Sstevel@tonic-gate 		return (-1);
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if ((u = eh->e_version) > EV_CURRENT) {
8087c478bd9Sstevel@tonic-gate 		_elf_seterr(EREQ_VER, 0);
8097c478bd9Sstevel@tonic-gate 		ELFUNLOCK(elf)
8107c478bd9Sstevel@tonic-gate 		return (-1);
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (u == EV_NONE)
8147c478bd9Sstevel@tonic-gate 		eh->e_version = EV_CURRENT;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	if ((u = eh->e_ident[EI_DATA]) == ELFDATANONE) {
8177c478bd9Sstevel@tonic-gate 		unsigned	encode;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		ELFACCESSDATA(encode, _elf_encode)
8207c478bd9Sstevel@tonic-gate 		if (encode == ELFDATANONE) {
8217c478bd9Sstevel@tonic-gate 			_elf_seterr(EREQ_ENCODE, 0);
8227c478bd9Sstevel@tonic-gate 			ELFUNLOCK(elf)
8237c478bd9Sstevel@tonic-gate 			return (-1);
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 		/* LINTED */
8267c478bd9Sstevel@tonic-gate 		eh->e_ident[EI_DATA] = (Byte)encode;
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	u = 1;
8307c478bd9Sstevel@tonic-gate 	if (elf->ed_uflags & ELF_F_LAYOUT) {
8317c478bd9Sstevel@tonic-gate 		sz = _elf_upd_usr(elf);
8327c478bd9Sstevel@tonic-gate 		u = 0;
8337c478bd9Sstevel@tonic-gate 	} else
8347c478bd9Sstevel@tonic-gate 		sz = _elf_upd_lib(elf);
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	if ((sz != 0) && ((cmd == ELF_C_WRITE) || (cmd == ELF_C_WRIMAGE)))
8377c478bd9Sstevel@tonic-gate 		sz = wrt(elf, (Xword)sz, u, cmd);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	if (sz == 0) {
8407c478bd9Sstevel@tonic-gate 		ELFUNLOCK(elf)
8417c478bd9Sstevel@tonic-gate 		return (-1);
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	ELFUNLOCK(elf)
8457c478bd9Sstevel@tonic-gate 	return ((off_t)sz);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 
849ba2be530Sab196087 /*
850ba2be530Sab196087  * When wrt() processes an ELF_C_WRIMAGE request, the resulting image
851ba2be530Sab196087  * gets the byte order (encoding) of the platform running the linker
852ba2be530Sab196087  * rather than that of the target host. This allows the linker to modify
853ba2be530Sab196087  * the image, prior to flushing it to the output file. This routine
854ba2be530Sab196087  * is used to re-translate such an image into the byte order of the
855ba2be530Sab196087  * target host.
856ba2be530Sab196087  */
857ba2be530Sab196087 int
_elfxx_swap_wrimage(Elf * elf)858ba2be530Sab196087 _elfxx_swap_wrimage(Elf *elf)
859ba2be530Sab196087 {
860ba2be530Sab196087 	Elf_Data	dst, src;
861ba2be530Sab196087 	Elf_Scn		*s;
8625b764efaSab196087 	Ehdr		*eh;
8635b764efaSab196087 	Half		e_phnum;
8645b764efaSab196087 	unsigned	ver;
8655b764efaSab196087 	unsigned	encode;
866ba2be530Sab196087 
867ba2be530Sab196087 	/*
868ba2be530Sab196087 	 * Ehdr first
869ba2be530Sab196087 	 */
870ba2be530Sab196087 
8715b764efaSab196087 	ELFWLOCK(elf);
8725b764efaSab196087 	eh = elf->ed_ehdr;
8735b764efaSab196087 	e_phnum = eh->e_phnum;
8745b764efaSab196087 	ver = eh->e_version;
8755b764efaSab196087 	encode = eh->e_ident[EI_DATA];
8765b764efaSab196087 
877ba2be530Sab196087 	src.d_buf = dst.d_buf = (Elf_Void *)eh;
878ba2be530Sab196087 	src.d_type = dst.d_type = ELF_T_EHDR;
879ba2be530Sab196087 	src.d_size = dst.d_size = sizeof (Ehdr);
880ba2be530Sab196087 	src.d_version = dst.d_version = ver;
8815b764efaSab196087 	if (elf_xlatetof(&dst, &src, encode) == 0) {
8825b764efaSab196087 		ELFUNLOCK(elf);
883ba2be530Sab196087 		return (1);
8845b764efaSab196087 	}
885ba2be530Sab196087 
886ba2be530Sab196087 	/*
887ba2be530Sab196087 	 * Phdr table if one exists
888ba2be530Sab196087 	 */
889ba2be530Sab196087 
890ba2be530Sab196087 	if (e_phnum != 0) {
891ba2be530Sab196087 		unsigned	work;
892ba2be530Sab196087 		/*
893ba2be530Sab196087 		 * Unlike other library data, phdr table is
894ba2be530Sab196087 		 * in the user version.
895ba2be530Sab196087 		 */
896ba2be530Sab196087 
897ba2be530Sab196087 		src.d_buf = dst.d_buf = (Elf_Void *)elf->ed_phdr;
898ba2be530Sab196087 		src.d_type = dst.d_type = ELF_T_PHDR;
899ba2be530Sab196087 		src.d_size = dst.d_size = elf->ed_phdrsz;
900ba2be530Sab196087 		ELFACCESSDATA(work, _elf_work)
901ba2be530Sab196087 		src.d_version = dst.d_version = work;
902ba2be530Sab196087 		if (elf_xlatetof(&dst, &src, encode) == 0) {
9035b764efaSab196087 			ELFUNLOCK(elf);
904ba2be530Sab196087 			return (1);
905ba2be530Sab196087 		}
906ba2be530Sab196087 	}
907ba2be530Sab196087 
908ba2be530Sab196087 	/*
909ba2be530Sab196087 	 * Loop through sections
910ba2be530Sab196087 	 */
911ba2be530Sab196087 
912ba2be530Sab196087 	for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
913ba2be530Sab196087 		register Dnode	*d, *prevd;
914ba2be530Sab196087 		Shdr		*sh = s->s_shdr;
915ba2be530Sab196087 
916ba2be530Sab196087 		if ((sh->sh_type == SHT_NOBITS) || (sh->sh_type == SHT_NULL))
917ba2be530Sab196087 			continue;
918ba2be530Sab196087 
919ba2be530Sab196087 		for (d = s->s_hdnode, prevd = 0;
920ba2be530Sab196087 		    d != 0; prevd = d, d = d->db_next) {
921ba2be530Sab196087 
922ba2be530Sab196087 			if ((d->db_myflags & DBF_READY) == 0) {
923ba2be530Sab196087 				SCNLOCK(s);
924ba2be530Sab196087 				if (_elf_locked_getdata(s, &prevd->db_data) !=
925ba2be530Sab196087 				    &d->db_data) {
926ba2be530Sab196087 					SCNUNLOCK(s);
9275b764efaSab196087 					ELFUNLOCK(elf);
928ba2be530Sab196087 					return (1);
929ba2be530Sab196087 				}
930ba2be530Sab196087 				SCNUNLOCK(s);
931ba2be530Sab196087 			}
932ba2be530Sab196087 
933ba2be530Sab196087 			dst = d->db_data;
9345b764efaSab196087 			if (elf_xlatetof(&dst, &d->db_data, encode) == 0) {
9355b764efaSab196087 				ELFUNLOCK(elf);
936ba2be530Sab196087 				return (1);
937ba2be530Sab196087 			}
938ba2be530Sab196087 		}
9395b764efaSab196087 	}
940ba2be530Sab196087 
941ba2be530Sab196087 	/*
942ba2be530Sab196087 	 * Shdr table
943ba2be530Sab196087 	 */
944ba2be530Sab196087 
945ba2be530Sab196087 	src.d_type = dst.d_type = ELF_T_SHDR;
946ba2be530Sab196087 	src.d_version = dst.d_version = ver;
947ba2be530Sab196087 	for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
948ba2be530Sab196087 		src.d_buf = dst.d_buf = s->s_shdr;
949ba2be530Sab196087 		src.d_size = dst.d_size = sizeof (Shdr);
9505b764efaSab196087 		if (elf_xlatetof(&dst, &src, encode) == 0) {
9515b764efaSab196087 			ELFUNLOCK(elf);
952ba2be530Sab196087 			return (1);
953ba2be530Sab196087 		}
9545b764efaSab196087 	}
955ba2be530Sab196087 
9565b764efaSab196087 	ELFUNLOCK(elf);
957ba2be530Sab196087 	return (0);
958ba2be530Sab196087 }
959ba2be530Sab196087 
960ba2be530Sab196087 
961ba2be530Sab196087 
9627c478bd9Sstevel@tonic-gate #ifndef _ELF64
9637c478bd9Sstevel@tonic-gate /* class-independent, only needs to be compiled once */
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate off_t
elf_update(Elf * elf,Elf_Cmd cmd)9667c478bd9Sstevel@tonic-gate elf_update(Elf *elf, Elf_Cmd cmd)
9677c478bd9Sstevel@tonic-gate {
9687c478bd9Sstevel@tonic-gate 	if (elf == 0)
9697c478bd9Sstevel@tonic-gate 		return (-1);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	if (elf->ed_class == ELFCLASS32)
9727c478bd9Sstevel@tonic-gate 		return (_elf32_update(elf, cmd));
9737c478bd9Sstevel@tonic-gate 	else if (elf->ed_class == ELFCLASS64) {
9747c478bd9Sstevel@tonic-gate 		return (_elf64_update(elf, cmd));
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	_elf_seterr(EREQ_CLASS, 0);
9787c478bd9Sstevel@tonic-gate 	return (-1);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
981ba2be530Sab196087 int
_elf_swap_wrimage(Elf * elf)982ba2be530Sab196087 _elf_swap_wrimage(Elf *elf)
983ba2be530Sab196087 {
984ba2be530Sab196087 	if (elf == 0)
985ba2be530Sab196087 		return (0);
986ba2be530Sab196087 
987ba2be530Sab196087 	if (elf->ed_class == ELFCLASS32)
988ba2be530Sab196087 		return (_elf32_swap_wrimage(elf));
989ba2be530Sab196087 
990ba2be530Sab196087 	if (elf->ed_class == ELFCLASS64)
991ba2be530Sab196087 		return (_elf64_swap_wrimage(elf));
992ba2be530Sab196087 
993ba2be530Sab196087 	_elf_seterr(EREQ_CLASS, 0);
994ba2be530Sab196087 	return (0);
995ba2be530Sab196087 }
996ba2be530Sab196087 
9977c478bd9Sstevel@tonic-gate /*
9987c478bd9Sstevel@tonic-gate  * 4106312, 4106398, This is an ad-hoc means for the 32-bit
9997c478bd9Sstevel@tonic-gate  * Elf64 version of libld.so.3 to get around the limitation
10007c478bd9Sstevel@tonic-gate  * of a 32-bit d_off field.  This is only intended to be
10017c478bd9Sstevel@tonic-gate  * used by libld to relocate symbols in large NOBITS sections.
10027c478bd9Sstevel@tonic-gate  */
10037c478bd9Sstevel@tonic-gate Elf64_Off
_elf_getxoff(Elf_Data * d)10047c478bd9Sstevel@tonic-gate _elf_getxoff(Elf_Data * d)
10057c478bd9Sstevel@tonic-gate {
10067c478bd9Sstevel@tonic-gate 	return (((Dnode *)d)->db_xoff);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate #endif /* !_ELF64 */
1009