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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
43 * Cook the input file.
44 * These functions take the input file buffer and extract
45 * the Ehdr, Phdr table, and the Shdr table. They keep track
46 * of the buffer status as "fresh," "cooked," or "frozen."
48 * fresh The file buffer is in its original state and
49 * nothing has yet referenced it.
51 * cooked The application asked for translated data first
52 * and caused the library to return a pointer into
53 * the file buffer. After this happens, all "raw"
54 * operations must go back to the disk.
56 * frozen The application first did a "raw" operation that
57 * prohibits reusing the file buffer. This effectively
58 * freezes the buffer, and all "normal" operations must
59 * duplicate their data.
61 * For archive handling, these functions conspire to align the
62 * file buffer to the host memory format. Archive members
63 * are guaranteed only even byte alignment, but the file uses
64 * objects at least 4 bytes long. If an archive member is about
65 * to be cooked and is not aligned in memory, these functions
66 * "slide" the buffer up into the archive member header.
67 * This sliding never occurs for frozen files.
69 * Some processors might not need sliding at all, if they have
70 * no alignment constraints on memory references. This code
71 * ignores that possibility for two reasons. First, even machines
72 * that have no constraints usually handle aligned objects faster
73 * than unaligned. Forcing alignment here probably leads to better
74 * performance. Second, there's no way to test at run time whether
75 * alignment is required or not. The safe thing is to align in
78 * This sliding relies on the archive header being disposable.
79 * Only archive members that are object files ever slide.
80 * They're also the only ones that ever need to. Archives never
81 * freeze to make headers disposable. Any program peculiar enough
82 * to want a frozen archive pays the penalty.
84 * The library itself inspects the Ehdr and the Shdr table
85 * from the file. Consequently, it converts the file's data
86 * to EV_CURRENT version, not the working version. This is
87 * transparent to the user. The library never looks at the
88 * Phdr table; so that's kept in the working version.
96 if ((d
= (Dnode
*)malloc(sizeof (Dnode
))) == 0) {
97 _elf_seterr(EMEM_DNODE
, errno
);
100 *d
= _elf_dnode_init
;
101 d
->db_myflags
= DBF_ALLOC
;
108 _elf_slide(Elf
* elf
)
110 Elf
*par
= elf
->ed_parent
;
113 register char *src
= elf
->ed_ident
;
115 if (par
== 0 || par
->ed_kind
!= ELF_K_AR
)
119 * This code relies on other code to ensure
120 * the ar_hdr is big enough to move into.
122 if (elf
->ed_ident
[EI_CLASS
] == ELFCLASS64
)
123 szof
= sizeof (Elf64
);
125 szof
= sizeof (Elf32
);
126 if ((sz
= (size_t)(src
- (char *)elf
->ed_image
) % szof
) == 0)
130 elf
->ed_memoff
-= sz
;
131 elf
->ed_armem
->m_slide
= sz
;
132 if (_elf_vm(par
, elf
->ed_memoff
, sz
+ elf
->ed_fsz
) != OK_YES
)
136 * If the archive has been mmaped in, and we're going to slide it,
137 * and it wasn't open for write in the first place, and we've never
138 * done the mprotect() operation before, then do it now.
140 if ((elf
->ed_vm
== 0) && ((elf
->ed_myflags
& EDF_WRITE
) == 0) &&
141 ((elf
->ed_myflags
& EDF_MPROTECT
) == 0)) {
142 if (mprotect((char *)elf
->ed_image
, elf
->ed_imagesz
,
143 PROT_READ
|PROT_WRITE
) == -1) {
144 _elf_seterr(EIO_VM
, errno
);
147 elf
->ed_myflags
|= EDF_MPROTECT
;
150 if (memmove((void *)dst
, (const void *)src
, elf
->ed_fsz
) != (void *)dst
)
160 register int inplace
= 1;
162 if (elf
->ed_kind
!= ELF_K_ELF
)
165 if ((elf
->ed_status
== ES_COOKED
) ||
166 ((elf
->ed_myflags
& EDF_READ
) == 0))
170 * Here's where the unaligned archive member gets fixed.
172 if (elf
->ed_status
== ES_FRESH
&& _elf_slide(elf
) != 0)
175 if (elf
->ed_status
== ES_FROZEN
)
179 * This is the first time we've actually looked at the file
180 * contents. We need to know whether or not this is an
181 * Elf32 or Elf64 file before we can decode the header.
182 * But it's the header that tells us which is which.
184 * Resolve the chicken-and-egg problem by peeking at the
185 * 'class' byte in the ident string.
187 if (elf
->ed_ident
[EI_CLASS
] == ELFCLASS32
) {
188 if (_elf32_ehdr(elf
, inplace
) != 0)
190 if (_elf32_phdr(elf
, inplace
) != 0)
192 if (_elf32_shdr(elf
, inplace
) != 0)
194 elf
->ed_class
= ELFCLASS32
;
195 } else if (elf
->ed_ident
[EI_CLASS
] == ELFCLASS64
) {
196 if (_elf64_ehdr(elf
, inplace
) != 0)
198 if (_elf64_phdr(elf
, inplace
) != 0)
200 if (_elf64_shdr(elf
, inplace
) != 0)
202 elf
->ed_class
= ELFCLASS64
;
209 if (elf
->ed_myflags
& EDF_PHALLOC
) {
210 elf
->ed_myflags
&= ~EDF_PHALLOC
;
215 if (elf
->ed_myflags
& EDF_EHALLOC
) {
216 elf
->ed_myflags
&= ~EDF_EHALLOC
;
226 _elf_cookscn(Elf_Scn
* s
)
228 Elf
* elf
= s
->s_elf
;
230 if (elf
->ed_class
== ELFCLASS32
) {
231 return (_elf32_cookscn(s
));
232 } else if (elf
->ed_class
== ELFCLASS64
) {
233 return (_elf64_cookscn(s
));
236 _elf_seterr(EREQ_CLASS
, 0);