2 * Copyright (c) 2006 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #define LIBELF_NALLOC_SIZE 16
39 * `ar' archive handling.
41 * `ar' archives start with signature `ARMAG'. Each archive member is
42 * preceded by a header containing meta-data for the member. This
43 * header is described in <ar.h> (struct ar_hdr). The header always
44 * starts on an even address. File data is padded with "\n"
45 * characters to keep this invariant.
47 * Special considerations for `ar' archives:
49 * The `ar' header only has space for a 16 character file name. File
50 * names are terminated with a '/', so this effectively leaves 15
51 * characters for the actual file name. In order to accomodate longer
52 * file names, names may be stored in a separate 'string table' and
53 * referenced indirectly by a member header. The string table itself
54 * appears as an archive member with name "// ". An indirect file name
55 * in an `ar' header matches the pattern "/[0-9]*". The digits form a
56 * decimal number that corresponds to a byte offset into the string
57 * table where the actual file name of the object starts. Strings in
58 * the string table are padded to start on even addresses.
60 * Archives may also have a symbol table (see ranlib(1)), mapping
61 * program symbols to object files inside the archive. A symbol table
62 * uses a file name of "/ " in its archive header. The symbol table
64 * - a 4-byte count of entries stored as a binary value, MSB first
65 * - 'n' 4-byte offsets, stored as binary values, MSB first
66 * - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
68 * If the symbol table and string table are is present in an archive
69 * they must be the very first objects and in that order.
73 * Convert a string bounded by `start' and `start+sz' (exclusive) to a
74 * number in the specified base.
77 _libelf_ar_get_number(char *s
, size_t sz
, int base
, size_t *ret
)
87 /* skip leading blanks */
88 for (;s
< e
&& (c
= *s
) == ' '; s
++)
95 if (c
< '0' || c
> '9')
98 if (v
>= base
) /* Illegal digit. */
110 * Retrieve a string from a name field. If `rawname' is set, leave
111 * ar(1) control characters in.
114 _libelf_ar_get_string(const char *buf
, size_t bufsize
, int rawname
)
123 /* Skip back over trailing blanks. */
124 for (q
= buf
+ bufsize
- 1; q
>= buf
&& *q
== ' '; --q
)
129 * If the input buffer only had blanks in it,
130 * return a zero-length string.
136 * Remove the trailing '/' character, but only
137 * if the name isn't one of the special names
141 (q
== (buf
+ 1) && *buf
!= '/'))
144 sz
= q
- buf
+ 2; /* Space for a trailing NUL. */
148 if ((r
= malloc(sz
)) == NULL
) {
149 LIBELF_SET_ERROR(RESOURCE
, 0);
153 (void) strncpy(r
, buf
, sz
);
160 * Retrieve the full name of the archive member.
163 _libelf_ar_get_name(char *buf
, size_t bufsize
, Elf
*e
)
169 assert(e
->e_kind
== ELF_K_AR
);
171 if (buf
[0] == '/' && (c
= buf
[1]) >= '0' && c
<= '9') {
173 * The value in field ar_name is a decimal offset into
174 * the archive string table where the actual name
177 if (_libelf_ar_get_number(buf
+ 1, bufsize
- 1, 10,
179 LIBELF_SET_ERROR(ARCHIVE
, 0);
183 if (offset
> e
->e_u
.e_ar
.e_rawstrtabsz
) {
184 LIBELF_SET_ERROR(ARCHIVE
, 0);
188 s
= q
= e
->e_u
.e_ar
.e_rawstrtab
+ offset
;
189 r
= e
->e_u
.e_ar
.e_rawstrtab
+ e
->e_u
.e_ar
.e_rawstrtabsz
;
191 for (s
= q
; s
< r
&& *s
!= '/'; s
++)
193 len
= s
- q
+ 1; /* space for the trailing NUL */
195 if ((s
= malloc(len
)) == NULL
) {
196 LIBELF_SET_ERROR(RESOURCE
, 0);
200 (void) strncpy(s
, q
, len
);
209 return (_libelf_ar_get_string(buf
, bufsize
, 0));
214 _libelf_ar_gethdr(Elf
*e
)
221 if ((parent
= e
->e_parent
) == NULL
) {
222 LIBELF_SET_ERROR(ARGUMENT
, 0);
226 arh
= (struct ar_hdr
*) ((uintptr_t) e
->e_rawfile
- sizeof(struct ar_hdr
));
228 assert((uintptr_t) arh
>= (uintptr_t) parent
->e_rawfile
+ SARMAG
);
229 assert((uintptr_t) arh
<= (uintptr_t) parent
->e_rawfile
+ parent
->e_rawsize
-
230 sizeof(struct ar_hdr
));
232 if ((eh
= malloc(sizeof(Elf_Arhdr
))) == NULL
) {
233 LIBELF_SET_ERROR(RESOURCE
, 0);
238 eh
->ar_name
= eh
->ar_rawname
= NULL
;
240 if ((eh
->ar_name
= _libelf_ar_get_name(arh
->ar_name
, sizeof(arh
->ar_name
),
244 if (_libelf_ar_get_number(arh
->ar_uid
, sizeof(arh
->ar_uid
), 10, &n
) == 0)
246 eh
->ar_uid
= (uid_t
) n
;
248 if (_libelf_ar_get_number(arh
->ar_gid
, sizeof(arh
->ar_gid
), 10, &n
) == 0)
250 eh
->ar_gid
= (gid_t
) n
;
252 if (_libelf_ar_get_number(arh
->ar_mode
, sizeof(arh
->ar_mode
), 8, &n
) == 0)
254 eh
->ar_mode
= (mode_t
) n
;
256 if (_libelf_ar_get_number(arh
->ar_size
, sizeof(arh
->ar_size
), 10, &n
) == 0)
260 if ((eh
->ar_rawname
= _libelf_ar_get_string(arh
->ar_name
,
261 sizeof(arh
->ar_name
), 1)) == NULL
)
271 free(eh
->ar_rawname
);
280 _libelf_ar_open_member(int fd
, Elf_Cmd c
, Elf
*elf
)
287 assert(elf
->e_kind
== ELF_K_AR
);
289 next
= elf
->e_u
.e_ar
.e_next
;
292 * `next' is only set to zero by elf_next() when the last
293 * member of an archive is processed.
295 if (next
== (off_t
) 0)
298 assert((next
& 1) == 0);
300 arh
= (struct ar_hdr
*) (elf
->e_rawfile
+ next
);
302 if (_libelf_ar_get_number(arh
->ar_size
, sizeof(arh
->ar_size
), 10, &sz
) == 0) {
303 LIBELF_SET_ERROR(ARCHIVE
, 0);
309 arh
++; /* skip over archive member header */
311 if ((e
= elf_memory((char *) arh
, sz
)) == NULL
)
317 elf
->e_u
.e_ar
.e_nchildren
++;
324 _libelf_ar_open(Elf
*e
)
331 e
->e_kind
= ELF_K_AR
;
332 e
->e_u
.e_ar
.e_nchildren
= 0;
333 e
->e_u
.e_ar
.e_next
= (off_t
) -1;
336 * Look for special members.
339 s
= e
->e_rawfile
+ SARMAG
;
340 end
= e
->e_rawfile
+ e
->e_rawsize
;
342 assert(e
->e_rawsize
> 0);
345 * Look for magic names "/ " and "// " in the first two entries
348 for (i
= 0; i
< 2; i
++) {
350 if (s
+ sizeof(arh
) > end
) {
351 LIBELF_SET_ERROR(ARCHIVE
, 0);
355 (void) memcpy(&arh
, s
, sizeof(arh
));
357 if (arh
.ar_fmag
[0] != '`' || arh
.ar_fmag
[1] != '\n') {
358 LIBELF_SET_ERROR(ARCHIVE
, 0);
362 if (arh
.ar_name
[0] != '/') /* not a special symbol */
365 if (_libelf_ar_get_number(arh
.ar_size
, sizeof(arh
.ar_size
), 10, &sz
) == 0) {
366 LIBELF_SET_ERROR(ARCHIVE
, 0);
374 if (arh
.ar_name
[1] == ' ') { /* "/ " => symbol table */
376 e
->e_u
.e_ar
.e_rawsymtab
= s
;
377 e
->e_u
.e_ar
.e_rawsymtabsz
= sz
;
379 } else if (arh
.ar_name
[1] == '/' && arh
.ar_name
[2] == ' ') {
381 /* "// " => string table for long file names */
382 e
->e_u
.e_ar
.e_rawstrtab
= s
;
383 e
->e_u
.e_ar
.e_rawstrtabsz
= sz
;
386 sz
= LIBELF_ADJUST_AR_SIZE(sz
);
391 e
->e_u
.e_ar
.e_next
= (off_t
) (s
- e
->e_rawfile
);
397 * An ar(1) symbol table has the following layout:
399 * The first 4 bytes are a binary count of the number of entries in the
400 * symbol table, stored MSB-first.
402 * Then there are 'n' 4-byte binary offsets, also stored MSB first.
404 * Following this, there are 'n' null-terminated strings.
407 #define GET_WORD(P, V) do { \
409 (V) = (P)[0]; (V) <<= 8; \
410 (V) += (P)[1]; (V) <<= 8; \
411 (V) += (P)[2]; (V) <<= 8; \
418 _libelf_ar_process_symtab(Elf
*e
, size_t *count
)
420 size_t n
, nentries
, off
;
421 Elf_Arsym
*symtab
, *sym
;
425 assert(count
!= NULL
);
427 if (e
->e_u
.e_ar
.e_rawsymtabsz
< INTSZ
) {
428 LIBELF_SET_ERROR(ARCHIVE
, 0);
432 p
= e
->e_u
.e_ar
.e_rawsymtab
;
433 end
= p
+ e
->e_u
.e_ar
.e_rawsymtabsz
;
435 GET_WORD(p
, nentries
);
438 if (nentries
== 0 || p
+ nentries
* INTSZ
>= end
) {
439 LIBELF_SET_ERROR(ARCHIVE
, 0);
443 /* Allocate space for a nentries + a sentinel. */
444 if ((symtab
= malloc(sizeof(Elf_Arsym
) * (nentries
+1))) == NULL
) {
445 LIBELF_SET_ERROR(RESOURCE
, 0);
449 s
= p
+ (nentries
* INTSZ
); /* start of the string table. */
451 for (n
= nentries
, sym
= symtab
; n
> 0; n
--) {
457 sym
->as_hash
= elf_hash(s
);
463 for (; s
< end
&& *s
++ != '\0';) /* skip to next string */
466 LIBELF_SET_ERROR(ARCHIVE
, 0);
472 /* Fill up the sentinel entry. */
475 sym
->as_off
= (off_t
) 0;
477 *count
= e
->e_u
.e_ar
.e_symtabsz
= nentries
+ 1;
478 e
->e_u
.e_ar
.e_symtab
= symtab
;