xref: /titanic_52/usr/src/cmd/sgs/libelf/common/newphdr.c (revision 7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fe)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #if !defined(_ELF64)
34 #pragma weak elf32_newphdr = _elf32_newphdr
35 #endif
36 
37 #include "syn.h"
38 #include <stdlib.h>
39 #include <memory.h>
40 #include <errno.h>
41 #include "decl.h"
42 #include "msg.h"
43 
44 /*
45  * This module is compiled twice, the second time having
46  * -D_ELF64 defined.  The following set of macros, along
47  * with machelf.h, represent the differences between the
48  * two compilations.  Be careful *not* to add any class-
49  * dependent code (anything that has elf32 or elf64 in the
50  * name) to this code without hiding it behind a switch-
51  * able macro like these.
52  */
53 #if	defined(_ELF64)
54 
55 #define	ELFCLASS	ELFCLASS64
56 #define	elf_newphdr	elf64_newphdr
57 #define	elf_getehdr	elf64_getehdr
58 #define	_elf_msize	_elf64_msize
59 #define	elf_fsize	elf64_fsize
60 
61 #else	/* else ELF32 */
62 
63 #define	ELFCLASS	ELFCLASS32
64 #define	elf_newphdr	elf32_newphdr
65 #define	elf_getehdr	elf32_getehdr
66 #define	_elf_msize	_elf32_msize
67 #define	elf_fsize	elf32_fsize
68 
69 #endif /* ELF64 */
70 
71 
72 Phdr *
73 elf_newphdr(Elf * elf, size_t count)
74 {
75 	Elf_Void *	ph;
76 	size_t		sz;
77 	Phdr *		rc;
78 	unsigned	work;
79 
80 	if (elf == 0)
81 		return (0);
82 	ELFRLOCK(elf)
83 	if (elf->ed_class != ELFCLASS) {
84 		_elf_seterr(EREQ_CLASS, 0);
85 		ELFUNLOCK(elf)
86 		return (0);
87 	}
88 	ELFUNLOCK(elf)
89 	if (elf_getehdr(elf) == 0) {		/* this cooks if necessary */
90 		_elf_seterr(ESEQ_EHDR, 0);
91 		return (0);
92 	}
93 
94 	/*
95 	 * Free the existing header if appropriate.  This could reuse
96 	 * existing space if big enough, but that's unlikely, benefit
97 	 * would be negligible, and code would be more complicated.
98 	 */
99 
100 	ELFWLOCK(elf)
101 	if (elf->ed_myflags & EDF_PHALLOC) {
102 		elf->ed_myflags &= ~EDF_PHALLOC;
103 		rc = elf->ed_phdr;
104 		free(rc);
105 	}
106 
107 	/*
108 	 * Delete the header if count is zero.
109 	 */
110 
111 	ELFACCESSDATA(work, _elf_work)
112 	if ((sz = count * _elf_msize(ELF_T_PHDR, work)) == 0) {
113 		elf->ed_phflags &= ~ELF_F_DIRTY;
114 		elf->ed_phdr = 0;
115 		((Ehdr*)elf->ed_ehdr)->e_phnum = 0;
116 		((Ehdr*)elf->ed_ehdr)->e_phentsize = 0;
117 		elf->ed_phdrsz = 0;
118 		ELFUNLOCK(elf)
119 		return (0);
120 	}
121 
122 	if ((ph = malloc(sz)) == 0) {
123 		_elf_seterr(EMEM_PHDR, errno);
124 		elf->ed_phflags &= ~ELF_F_DIRTY;
125 		elf->ed_phdr = 0;
126 		((Ehdr*)elf->ed_ehdr)->e_phnum = 0;
127 		((Ehdr*)elf->ed_ehdr)->e_phentsize = 0;
128 		elf->ed_phdrsz = 0;
129 		ELFUNLOCK(elf)
130 		return (0);
131 	}
132 
133 	elf->ed_myflags |= EDF_PHALLOC;
134 	(void) memset(ph, 0, sz);
135 	elf->ed_phflags |= ELF_F_DIRTY;
136 	/* LINTED */
137 	((Ehdr*)elf->ed_ehdr)->e_phnum = (Half)count;
138 	((Ehdr*)elf->ed_ehdr)->e_phentsize
139 	    /* LINTED */
140 	    = (Half)elf_fsize(ELF_T_PHDR, 1, work);
141 	elf->ed_phdrsz = sz;
142 	elf->ed_phdr = rc = ph;
143 
144 	ELFUNLOCK(elf)
145 	return (rc);
146 }
147