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.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*7257d1b4Sraf /*	Copyright (c) 1988 AT&T	*/
28*7257d1b4Sraf /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <memory.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include "decl.h"
347c478bd9Sstevel@tonic-gate #include "msg.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * This module is compiled twice, the second time having
387c478bd9Sstevel@tonic-gate  * -D_ELF64 defined.  The following set of macros, along
397c478bd9Sstevel@tonic-gate  * with machelf.h, represent the differences between the
407c478bd9Sstevel@tonic-gate  * two compilations.  Be careful *not* to add any class-
417c478bd9Sstevel@tonic-gate  * dependent code (anything that has elf32 or elf64 in the
427c478bd9Sstevel@tonic-gate  * name) to this code without hiding it behind a switch-
437c478bd9Sstevel@tonic-gate  * able macro like these.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	ELFCLASS	ELFCLASS64
487c478bd9Sstevel@tonic-gate #define	elf_newphdr	elf64_newphdr
497c478bd9Sstevel@tonic-gate #define	elf_getehdr	elf64_getehdr
507c478bd9Sstevel@tonic-gate #define	_elf_msize	_elf64_msize
517c478bd9Sstevel@tonic-gate #define	elf_fsize	elf64_fsize
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #else	/* else ELF32 */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	ELFCLASS	ELFCLASS32
567c478bd9Sstevel@tonic-gate #define	elf_newphdr	elf32_newphdr
577c478bd9Sstevel@tonic-gate #define	elf_getehdr	elf32_getehdr
587c478bd9Sstevel@tonic-gate #define	_elf_msize	_elf32_msize
597c478bd9Sstevel@tonic-gate #define	elf_fsize	elf32_fsize
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #endif /* ELF64 */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate Phdr *
elf_newphdr(Elf * elf,size_t count)657c478bd9Sstevel@tonic-gate elf_newphdr(Elf * elf, size_t count)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	Elf_Void *	ph;
687c478bd9Sstevel@tonic-gate 	size_t		sz;
697c478bd9Sstevel@tonic-gate 	Phdr *		rc;
707c478bd9Sstevel@tonic-gate 	unsigned	work;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (elf == 0)
737c478bd9Sstevel@tonic-gate 		return (0);
747c478bd9Sstevel@tonic-gate 	ELFRLOCK(elf)
757c478bd9Sstevel@tonic-gate 	if (elf->ed_class != ELFCLASS) {
767c478bd9Sstevel@tonic-gate 		_elf_seterr(EREQ_CLASS, 0);
777c478bd9Sstevel@tonic-gate 		ELFUNLOCK(elf)
787c478bd9Sstevel@tonic-gate 		return (0);
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 	ELFUNLOCK(elf)
817c478bd9Sstevel@tonic-gate 	if (elf_getehdr(elf) == 0) {		/* this cooks if necessary */
827c478bd9Sstevel@tonic-gate 		_elf_seterr(ESEQ_EHDR, 0);
837c478bd9Sstevel@tonic-gate 		return (0);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * Free the existing header if appropriate.  This could reuse
887c478bd9Sstevel@tonic-gate 	 * existing space if big enough, but that's unlikely, benefit
897c478bd9Sstevel@tonic-gate 	 * would be negligible, and code would be more complicated.
907c478bd9Sstevel@tonic-gate 	 */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	ELFWLOCK(elf)
937c478bd9Sstevel@tonic-gate 	if (elf->ed_myflags & EDF_PHALLOC) {
947c478bd9Sstevel@tonic-gate 		elf->ed_myflags &= ~EDF_PHALLOC;
957c478bd9Sstevel@tonic-gate 		rc = elf->ed_phdr;
967c478bd9Sstevel@tonic-gate 		free(rc);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * Delete the header if count is zero.
1017c478bd9Sstevel@tonic-gate 	 */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	ELFACCESSDATA(work, _elf_work)
1047c478bd9Sstevel@tonic-gate 	if ((sz = count * _elf_msize(ELF_T_PHDR, work)) == 0) {
1057c478bd9Sstevel@tonic-gate 		elf->ed_phflags &= ~ELF_F_DIRTY;
1067c478bd9Sstevel@tonic-gate 		elf->ed_phdr = 0;
1077c478bd9Sstevel@tonic-gate 		((Ehdr*)elf->ed_ehdr)->e_phnum = 0;
1087c478bd9Sstevel@tonic-gate 		((Ehdr*)elf->ed_ehdr)->e_phentsize = 0;
1097c478bd9Sstevel@tonic-gate 		elf->ed_phdrsz = 0;
1107c478bd9Sstevel@tonic-gate 		ELFUNLOCK(elf)
1117c478bd9Sstevel@tonic-gate 		return (0);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if ((ph = malloc(sz)) == 0) {
1157c478bd9Sstevel@tonic-gate 		_elf_seterr(EMEM_PHDR, errno);
1167c478bd9Sstevel@tonic-gate 		elf->ed_phflags &= ~ELF_F_DIRTY;
1177c478bd9Sstevel@tonic-gate 		elf->ed_phdr = 0;
1187c478bd9Sstevel@tonic-gate 		((Ehdr*)elf->ed_ehdr)->e_phnum = 0;
1197c478bd9Sstevel@tonic-gate 		((Ehdr*)elf->ed_ehdr)->e_phentsize = 0;
1207c478bd9Sstevel@tonic-gate 		elf->ed_phdrsz = 0;
1217c478bd9Sstevel@tonic-gate 		ELFUNLOCK(elf)
1227c478bd9Sstevel@tonic-gate 		return (0);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	elf->ed_myflags |= EDF_PHALLOC;
1267c478bd9Sstevel@tonic-gate 	(void) memset(ph, 0, sz);
1277c478bd9Sstevel@tonic-gate 	elf->ed_phflags |= ELF_F_DIRTY;
1287c478bd9Sstevel@tonic-gate 	/* LINTED */
1297c478bd9Sstevel@tonic-gate 	((Ehdr*)elf->ed_ehdr)->e_phnum = (Half)count;
1307c478bd9Sstevel@tonic-gate 	((Ehdr*)elf->ed_ehdr)->e_phentsize
1317c478bd9Sstevel@tonic-gate 	    /* LINTED */
1327c478bd9Sstevel@tonic-gate 	    = (Half)elf_fsize(ELF_T_PHDR, 1, work);
1337c478bd9Sstevel@tonic-gate 	elf->ed_phdrsz = sz;
1347c478bd9Sstevel@tonic-gate 	elf->ed_phdr = rc = ph;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	ELFUNLOCK(elf)
1377c478bd9Sstevel@tonic-gate 	return (rc);
1387c478bd9Sstevel@tonic-gate }
139