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 (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
26 /* Copyright (c) 1988 AT&T */
27 /* All Rights Reserved */
37 * Convert archive symbol table to memory format
39 * This takes a pointer to file's archive symbol table, alignment
40 * unconstrained. Returns null terminated vector of Elf_Arsym
41 * structures. Elf_Arsym uses size_t to represent offsets, which
42 * will be 32-bit in 32-bit versions, and 64-bits otherwise.
44 * There are two forms of archive symbol table, the original 32-bit
45 * form, and a 64-bit form originally found in IRIX64. The two formats
46 * differ only in the width of the integer word:
48 * # offsets 4/8-byte word
49 * offset[0...] 4/8-byte word each
50 * strings null-terminated, for offset[x]
52 * By default, the 64-bit form is only used when the archive exceeds
53 * the limits of 32-bits (4GB) in size. However, this is not required,
54 * and the ar -S option can be used to create a 64-bit symbol table in
55 * an archive that is under 4GB.
57 * Both 32 and 64-bit versions of libelf can read the 32-bit format
58 * without loss of information. Similarly, a 64-bit version of libelf
59 * will have no problem reading a 64-bit symbol table. This leaves the
60 * case where a 32-bit libelf reads a 64-bit symbol table, which requires
61 * some explanation. The offsets in a 64-bit symbol table will have zeros
62 * in the upper half of the words until the size of the archive exceeds 4GB.
63 * However, 32-bit libelf is unable to read any files larger than 2GB
64 * (see comments in update.c). As such, any archive that the 32-bit version
65 * of this code will encounter will be under 4GB in size. The upper 4
66 * bytes of each word will be zero, and can be safely ignored.
71 * Offsets in archive headers are written in MSB (large endian) order
72 * on all platforms, regardless of native byte order. These macros read
73 * 4 and 8 byte values from unaligned memory.
76 * - The get8() macro for 32-bit code can ignore the first 4 bytes of
77 * of the word, because they are known to be 0.
79 * - The inner most value in these macros is cast to an unsigned integer
80 * of the final width in order to prevent the C comilier from doing
81 * unwanted sign extension when the topmost bit of a byte is set.
83 #define get4(p) (((((((uint32_t)p[0]<<8)+p[1])<<8)+p[2])<<8)+p[3])
86 #define get8(p) (((((((((((((((uint64_t)p[0]<<8)+p[1])<<8)+p[2])<<8)+ \
87 p[3])<<8)+p[4])<<8)+p[5])<<8)+p[6])<<8)+p[7])
89 #define get8(p) (((((((uint64_t)p[4]<<8)+p[5])<<8)+p[6])<<8)+p[7])
94 arsym(Byte
*off
, size_t sz
, size_t *e
, int is64
)
96 char *endstr
= (char *)off
+ sz
;
100 size_t eltsize
= is64
? 8 : 4;
106 if (sz
< 8 || (sz
- 8) / 8 < (n
= get8(off
))) {
107 _elf_seterr(EFMT_ARSYMSZ
, 0);
111 if (sz
< 4 || (sz
- 4) / 4 < (n
= get4(off
))) {
112 _elf_seterr(EFMT_ARSYMSZ
, 0);
117 endoff
= off
+ n
* eltsize
;
120 * If there are symbols in the symbol table, a
121 * string table must be present and NULL terminated.
123 * The format dictates that the string table must always be
124 * present, however in the case of an archive containing no
125 * symbols GNU ar will not create one. We are permissive for
126 * the sake of compatibility.
128 if ((n
> 0) && (((str
= (char *)endoff
) >= endstr
) ||
129 (*(endstr
- 1) != '\0'))) {
130 _elf_seterr(EFMT_ARSYM
, 0);
135 * There is always at least one entry returned if a symtab
136 * exists since the table's last entry is an artificial one
137 * with a NULL as_name, but is included in the count.
139 * overflow can occur here, but not likely
142 if ((oas
= calloc(n
+ 1, sizeof (Elf_Arsym
))) == NULL
) {
143 _elf_seterr(EMEM_ARSYM
, errno
);
148 register Elf_Arsym
*as
= (Elf_Arsym
*)oas
;
150 while (off
< endoff
) {
152 _elf_seterr(EFMT_ARSYMSTR
, 0);
157 as
->as_off
= get8(off
);
159 as
->as_off
= get4(off
);
161 as
->as_hash
= elf_hash(str
);
164 while (*str
++ != '\0')
170 as
->as_hash
= ~(unsigned long)0L;
177 elf_getarsym(Elf
*elf
, size_t *ptr
)
189 if (elf
->ed_kind
!= ELF_K_AR
) {
191 _elf_seterr(EREQ_AR
, 0);
194 if ((as
= (Byte
*)elf
->ed_arsym
) == 0) {
198 if (elf
->ed_myflags
& EDF_ASALLOC
) {
200 *ptr
= elf
->ed_arsymsz
;
203 return ((Elf_Arsym
*)as
);
205 is64
= (elf
->ed_myflags
& EDF_ARSYM64
) != 0;
208 * We're gonna need a write lock.
212 sz
= elf
->ed_arsymsz
;
213 if (_elf_vm(elf
, (size_t)(as
- (Byte
*)elf
->ed_ident
), sz
) !=
218 if ((elf
->ed_arsym
= arsym(as
, sz
, &elf
->ed_arsymsz
, is64
)) == 0) {
222 elf
->ed_myflags
|= EDF_ASALLOC
;
224 *ptr
= elf
->ed_arsymsz
;
225 rc
= (Elf_Arsym
*)elf
->ed_arsym
;
231 * Private function to obtain the value sizeof() would return
232 * for a word from the symbol table from the given archive. Normally,
233 * this is an unimportant implementation detail hidden within
234 * elf_getarsym(). However, it is useful to elfdump for formatting the
235 * output correctly, and for the file command.
238 * Returns 4 (32-bit) or 8 (64-bit) if a symbol table is present.
239 * Returns 0 in all other cases.
242 _elf_getarsymwordsize(Elf
*elf
)
250 if ((elf
->ed_kind
== ELF_K_AR
) && (elf
->ed_arsym
!= 0))
251 size
= (elf
->ed_myflags
& EDF_ARSYM64
) ? 8 : 4;