xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/newehdr.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
22*6812Sraf /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6812Sraf  * Use is subject to license terms.
25*6812Sraf  */
26*6812Sraf 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include "decl.h"
350Sstevel@tonic-gate #include "msg.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * This module is compiled twice, the second time having
390Sstevel@tonic-gate  * -D_ELF64 defined.  The following set of macros, along
400Sstevel@tonic-gate  * with machelf.h, represent the differences between the
410Sstevel@tonic-gate  * two compilations.  Be careful *not* to add any class-
420Sstevel@tonic-gate  * dependent code (anything that has elf32 or elf64 in the
430Sstevel@tonic-gate  * name) to this code without hiding it behind a switch-
440Sstevel@tonic-gate  * able macro like these.
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate #if	defined(_ELF64)
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	ELFCLASS		ELFCLASS64
490Sstevel@tonic-gate #define	_elf_ehdr_init		_elf64_ehdr_init
500Sstevel@tonic-gate #define	elf_newehdr		elf64_newehdr
510Sstevel@tonic-gate #define	getehdr			elf64_getehdr
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #else	/* else ELF32 */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define	ELFCLASS		ELFCLASS32
560Sstevel@tonic-gate #define	_elf_ehdr_init		_elf32_ehdr_init
57*6812Sraf #define	elf_newehdr		elf32_newehdr
580Sstevel@tonic-gate #define	getehdr			elf32_getehdr
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #endif	/* ELF64 */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 
630Sstevel@tonic-gate Ehdr *
elf_newehdr(Elf * elf)640Sstevel@tonic-gate elf_newehdr(Elf * elf)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	Ehdr	*eh;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if (elf == 0)
690Sstevel@tonic-gate 		return (0);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	/*
720Sstevel@tonic-gate 	 * If reading file, return its hdr
730Sstevel@tonic-gate 	 */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	ELFWLOCK(elf)
760Sstevel@tonic-gate 	if (elf->ed_myflags & EDF_READ) {
770Sstevel@tonic-gate 		ELFUNLOCK(elf)
780Sstevel@tonic-gate 		if ((eh = (Ehdr *)getehdr(elf)) != 0) {
790Sstevel@tonic-gate 			ELFWLOCK(elf)
800Sstevel@tonic-gate 			elf->ed_ehflags |= ELF_F_DIRTY;
810Sstevel@tonic-gate 			ELFUNLOCK(elf)
820Sstevel@tonic-gate 		}
830Sstevel@tonic-gate 		return (eh);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * Writing file
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	if (elf->ed_class == ELFCLASSNONE)
910Sstevel@tonic-gate 		elf->ed_class = ELFCLASS;
920Sstevel@tonic-gate 	else if (elf->ed_class != ELFCLASS) {
930Sstevel@tonic-gate 		_elf_seterr(EREQ_CLASS, 0);
940Sstevel@tonic-gate 		ELFUNLOCK(elf)
950Sstevel@tonic-gate 		return (0);
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 	ELFUNLOCK(elf);
980Sstevel@tonic-gate 	if ((eh = (Ehdr *)getehdr(elf)) != 0) {	/* this cooks if necessary */
990Sstevel@tonic-gate 		ELFWLOCK(elf)
1000Sstevel@tonic-gate 		elf->ed_ehflags |= ELF_F_DIRTY;
1010Sstevel@tonic-gate 		ELFUNLOCK(elf)
1020Sstevel@tonic-gate 		return (eh);
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	ELFWLOCK(elf)
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if ((eh = (Ehdr *)malloc(sizeof (Ehdr))) == 0) {
1070Sstevel@tonic-gate 		_elf_seterr(EMEM_EHDR, errno);
1080Sstevel@tonic-gate 		ELFUNLOCK(elf)
1090Sstevel@tonic-gate 		return (0);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	*eh = _elf_ehdr_init;
1120Sstevel@tonic-gate 	elf->ed_myflags |= EDF_EHALLOC;
1130Sstevel@tonic-gate 	elf->ed_ehflags |= ELF_F_DIRTY;
1140Sstevel@tonic-gate 	elf->ed_ehdr = eh;
1150Sstevel@tonic-gate 	ELFUNLOCK(elf)
1160Sstevel@tonic-gate 	return (eh);
1170Sstevel@tonic-gate }
118