1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <stdlib.h>
31 #include <errno.h>
32 #include "decl.h"
33 #include "msg.h"
34 
35 /*
36  * This module is compiled twice, the second time having
37  * -D_ELF64 defined.  The following set of macros, along
38  * with machelf.h, represent the differences between the
39  * two compilations.  Be careful *not* to add any class-
40  * dependent code (anything that has elf32 or elf64 in the
41  * name) to this code without hiding it behind a switch-
42  * able macro like these.
43  */
44 #if	defined(_ELF64)
45 
46 #define	ELFCLASS		ELFCLASS64
47 #define	_elf_ehdr_init		_elf64_ehdr_init
48 #define	elf_newehdr		elf64_newehdr
49 #define	getehdr			elf64_getehdr
50 
51 #else	/* else ELF32 */
52 
53 #define	ELFCLASS		ELFCLASS32
54 #define	_elf_ehdr_init		_elf32_ehdr_init
55 #define	elf_newehdr		elf32_newehdr
56 #define	getehdr			elf32_getehdr
57 
58 #endif	/* ELF64 */
59 
60 
61 Ehdr *
elf_newehdr(Elf * elf)62 elf_newehdr(Elf * elf)
63 {
64 	Ehdr	*eh;
65 
66 	if (elf == 0)
67 		return (0);
68 
69 	/*
70 	 * If reading file, return its hdr
71 	 */
72 
73 	ELFWLOCK(elf)
74 	if (elf->ed_myflags & EDF_READ) {
75 		ELFUNLOCK(elf)
76 		if ((eh = (Ehdr *)getehdr(elf)) != 0) {
77 			ELFWLOCK(elf)
78 			elf->ed_ehflags |= ELF_F_DIRTY;
79 			ELFUNLOCK(elf)
80 		}
81 		return (eh);
82 	}
83 
84 	/*
85 	 * Writing file
86 	 */
87 
88 	if (elf->ed_class == ELFCLASSNONE)
89 		elf->ed_class = ELFCLASS;
90 	else if (elf->ed_class != ELFCLASS) {
91 		_elf_seterr(EREQ_CLASS, 0);
92 		ELFUNLOCK(elf)
93 		return (0);
94 	}
95 	ELFUNLOCK(elf);
96 	if ((eh = (Ehdr *)getehdr(elf)) != 0) {	/* this cooks if necessary */
97 		ELFWLOCK(elf)
98 		elf->ed_ehflags |= ELF_F_DIRTY;
99 		ELFUNLOCK(elf)
100 		return (eh);
101 	}
102 	ELFWLOCK(elf)
103 
104 	if ((eh = (Ehdr *)malloc(sizeof (Ehdr))) == 0) {
105 		_elf_seterr(EMEM_EHDR, errno);
106 		ELFUNLOCK(elf)
107 		return (0);
108 	}
109 	*eh = _elf_ehdr_init;
110 	elf->ed_myflags |= EDF_EHALLOC;
111 	elf->ed_ehflags |= ELF_F_DIRTY;
112 	elf->ed_ehdr = eh;
113 	ELFUNLOCK(elf)
114 	return (eh);
115 }
116