]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libelf/libelf_ar.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libelf / libelf_ar.c
1 /*-
2  * Copyright (c) 2006 Joseph Koshy
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <ar.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <libelf.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "_libelf.h"
38
39 #define LIBELF_NALLOC_SIZE      16
40
41 /*
42  * `ar' archive handling.
43  *
44  * `ar' archives start with signature `ARMAG'.  Each archive member is
45  * preceded by a header containing meta-data for the member.  This
46  * header is described in <ar.h> (struct ar_hdr).  The header always
47  * starts on an even address.  File data is padded with "\n"
48  * characters to keep this invariant.
49  *
50  * Special considerations for `ar' archives:
51  *
52  * The `ar' header only has space for a 16 character file name.  File
53  * names are terminated with a '/', so this effectively leaves 15
54  * characters for the actual file name.  In order to accomodate longer
55  * file names, names may be stored in a separate 'string table' and
56  * referenced indirectly by a member header.  The string table itself
57  * appears as an archive member with name "// ".  An indirect file name
58  * in an `ar' header matches the pattern "/[0-9]*". The digits form a
59  * decimal number that corresponds to a byte offset into the string
60  * table where the actual file name of the object starts.  Strings in
61  * the string table are padded to start on even addresses.
62  *
63  * Archives may also have a symbol table (see ranlib(1)), mapping
64  * program symbols to object files inside the archive.  A symbol table
65  * uses a file name of "/ " in its archive header.  The symbol table
66  * is structured as:
67  *  - a 4-byte count of entries stored as a binary value, MSB first
68  *  - 'n' 4-byte offsets, stored as binary values, MSB first
69  *  - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
70  *
71  * If the symbol table and string table are is present in an archive
72  * they must be the very first objects and in that order.
73  */
74
75 /*
76  * Convert a string bounded by `start' and `start+sz' (exclusive) to a
77  * number in the specified base.
78  */
79 static int
80 _libelf_ar_get_number(char *s, size_t sz, int base, size_t *ret)
81 {
82         int c, v;
83         size_t r;
84         char *e;
85
86         assert(base <= 10);
87
88         e = s + sz;
89
90         /* skip leading blanks */
91         for (;s < e && (c = *s) == ' '; s++)
92                 ;
93
94         r = 0L;
95         for (;s < e; s++) {
96                 if ((c = *s) == ' ')
97                         break;
98                 if (c < '0' || c > '9')
99                         return (0);
100                 v = c - '0';
101                 if (v >= base)          /* Illegal digit. */
102                         break;
103                 r *= base;
104                 r += v;
105         }
106
107         *ret = r;
108
109         return (1);
110 }
111
112 /*
113  * Retrieve a string from a name field.  If `rawname' is set, leave
114  * ar(1) control characters in.
115  */
116 static char *
117 _libelf_ar_get_string(const char *buf, size_t bufsize, int rawname)
118 {
119         const char *q;
120         char *r;
121         size_t sz;
122
123         if (rawname)
124                 sz = bufsize + 1;
125         else {
126                 /* Skip back over trailing blanks. */
127                 for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
128                         ;
129
130                 if (q < buf) {
131                         /*
132                          * If the input buffer only had blanks in it,
133                          * return a zero-length string.
134                          */
135                         buf = "";
136                         sz = 1;
137                 } else {
138                         /*
139                          * Remove the trailing '/' character, but only
140                          * if the name isn't one of the special names
141                          * "/" and "//".
142                          */
143                         if (q > buf + 1 ||
144                             (q == (buf + 1) && *buf != '/'))
145                                 q--;
146
147                         sz = q - buf + 2; /* Space for a trailing NUL. */
148                 }
149         }
150
151         if ((r = malloc(sz)) == NULL) {
152                 LIBELF_SET_ERROR(RESOURCE, 0);
153                 return (NULL);
154         }
155
156         (void) strncpy(r, buf, sz);
157         r[sz - 1] = '\0';
158
159         return (r);
160 }
161
162 /*
163  * Retrieve the full name of the archive member.
164  */
165 static char *
166 _libelf_ar_get_name(char *buf, size_t bufsize, Elf *e)
167 {
168         char c, *q, *r, *s;
169         size_t len;
170         size_t offset;
171
172         assert(e->e_kind == ELF_K_AR);
173
174         if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
175                 /*
176                  * The value in field ar_name is a decimal offset into
177                  * the archive string table where the actual name
178                  * resides.
179                  */
180                 if (_libelf_ar_get_number(buf + 1, bufsize - 1, 10,
181                     &offset) == 0) {
182                         LIBELF_SET_ERROR(ARCHIVE, 0);
183                         return (NULL);
184                 }
185
186                 if (offset > e->e_u.e_ar.e_rawstrtabsz) {
187                         LIBELF_SET_ERROR(ARCHIVE, 0);
188                         return (NULL);
189                 }
190
191                 s = q = e->e_u.e_ar.e_rawstrtab + offset;
192                 r = e->e_u.e_ar.e_rawstrtab + e->e_u.e_ar.e_rawstrtabsz;
193
194                 for (s = q; s < r && *s != '/'; s++)
195                         ;
196                 len = s - q + 1; /* space for the trailing NUL */
197
198                 if ((s = malloc(len)) == NULL) {
199                         LIBELF_SET_ERROR(RESOURCE, 0);
200                         return (NULL);
201                 }
202
203                 (void) strncpy(s, q, len);
204                 s[len - 1] = '\0';
205
206                 return (s);
207         }
208
209         /*
210          * Normal 'name'
211          */
212         return (_libelf_ar_get_string(buf, bufsize, 0));
213 }
214
215
216 Elf_Arhdr *
217 _libelf_ar_gethdr(Elf *e)
218 {
219         Elf *parent;
220         struct ar_hdr *arh;
221         Elf_Arhdr *eh;
222         size_t n;
223
224         if ((parent = e->e_parent) == NULL) {
225                 LIBELF_SET_ERROR(ARGUMENT, 0);
226                 return (NULL);
227         }
228
229         arh = (struct ar_hdr *) ((uintptr_t) e->e_rawfile - sizeof(struct ar_hdr));
230
231         assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
232         assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile + parent->e_rawsize -
233             sizeof(struct ar_hdr));
234
235         if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
236                 LIBELF_SET_ERROR(RESOURCE, 0);
237                 return (NULL);
238         }
239
240         e->e_arhdr = eh;
241         eh->ar_name = eh->ar_rawname = NULL;
242
243         if ((eh->ar_name = _libelf_ar_get_name(arh->ar_name, sizeof(arh->ar_name),
244                  parent)) == NULL)
245                 goto error;
246
247         if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10, &n) == 0)
248                 goto error;
249         eh->ar_uid = (uid_t) n;
250
251         if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10, &n) == 0)
252                 goto error;
253         eh->ar_gid = (gid_t) n;
254
255         if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8, &n) == 0)
256                 goto error;
257         eh->ar_mode = (mode_t) n;
258
259         if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &n) == 0)
260                 goto error;
261         eh->ar_size = n;
262
263         if ((eh->ar_rawname = _libelf_ar_get_string(arh->ar_name,
264                  sizeof(arh->ar_name), 1)) == NULL)
265                 goto error;
266
267         return (eh);
268
269  error:
270         if (eh) {
271                 if (eh->ar_name)
272                         free(eh->ar_name);
273                 if (eh->ar_rawname)
274                         free(eh->ar_rawname);
275                 free(eh);
276         }
277         e->e_arhdr = NULL;
278
279         return (NULL);
280 }
281
282 Elf *
283 _libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
284 {
285         Elf *e;
286         off_t next;
287         struct ar_hdr *arh;
288         size_t sz;
289
290         assert(elf->e_kind == ELF_K_AR);
291
292         next = elf->e_u.e_ar.e_next;
293
294         /*
295          * `next' is only set to zero by elf_next() when the last
296          * member of an archive is processed.
297          */
298         if (next == (off_t) 0)
299                 return (NULL);
300
301         assert((next & 1) == 0);
302
303         arh = (struct ar_hdr *) (elf->e_rawfile + next);
304
305         if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &sz) == 0) {
306                 LIBELF_SET_ERROR(ARCHIVE, 0);
307                 return (NULL);
308         }
309
310         assert(sz > 0);
311
312         arh++;  /* skip over archive member header */
313
314         if ((e = elf_memory((char *) arh, sz)) == NULL)
315                 return (NULL);
316
317         e->e_fd = fd;
318         e->e_cmd = c;
319
320         elf->e_u.e_ar.e_nchildren++;
321         e->e_parent = elf;
322
323         return (e);
324 }
325
326 Elf *
327 _libelf_ar_open(Elf *e)
328 {
329         int i;
330         char *s, *end;
331         size_t sz;
332         struct ar_hdr arh;
333
334         e->e_kind = ELF_K_AR;
335         e->e_u.e_ar.e_nchildren = 0;
336         e->e_u.e_ar.e_next = (off_t) -1;
337
338         /*
339          * Look for special members.
340          */
341
342         s = e->e_rawfile + SARMAG;
343         end = e->e_rawfile + e->e_rawsize;
344
345         assert(e->e_rawsize > 0);
346
347         /*
348          * Look for magic names "/ " and "// " in the first two entries
349          * of the archive.
350          */
351         for (i = 0; i < 2; i++) {
352
353                 if (s + sizeof(arh) > end) {
354                         LIBELF_SET_ERROR(ARCHIVE, 0);
355                         return (NULL);
356                 }
357
358                 (void) memcpy(&arh, s, sizeof(arh));
359
360                 if (arh.ar_fmag[0] != '`' || arh.ar_fmag[1] != '\n') {
361                         LIBELF_SET_ERROR(ARCHIVE, 0);
362                         return (NULL);
363                 }
364
365                 if (arh.ar_name[0] != '/')      /* not a special symbol */
366                         break;
367
368                 if (_libelf_ar_get_number(arh.ar_size, sizeof(arh.ar_size), 10, &sz) == 0) {
369                         LIBELF_SET_ERROR(ARCHIVE, 0);
370                         return (NULL);
371                 }
372
373                 assert(sz > 0);
374
375                 s += sizeof(arh);
376
377                 if (arh.ar_name[1] == ' ') {    /* "/ " => symbol table */
378
379                         e->e_u.e_ar.e_rawsymtab = s;
380                         e->e_u.e_ar.e_rawsymtabsz = sz;
381
382                 } else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
383
384                         /* "// " => string table for long file names */
385                         e->e_u.e_ar.e_rawstrtab = s;
386                         e->e_u.e_ar.e_rawstrtabsz = sz;
387                 }
388
389                 sz = LIBELF_ADJUST_AR_SIZE(sz);
390
391                 s += sz;
392         }
393
394         e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
395
396         return (e);
397 }
398
399 /*
400  * An ar(1) symbol table has the following layout:
401  *
402  * The first 4 bytes are a binary count of the number of entries in the
403  * symbol table, stored MSB-first.
404  *
405  * Then there are 'n' 4-byte binary offsets, also stored MSB first.
406  *
407  * Following this, there are 'n' null-terminated strings.
408  */
409
410 #define GET_WORD(P, V) do {                     \
411                 (V) = 0;                        \
412                 (V) = (P)[0]; (V) <<= 8;        \
413                 (V) += (P)[1]; (V) <<= 8;       \
414                 (V) += (P)[2]; (V) <<= 8;       \
415                 (V) += (P)[3];                  \
416         } while (0)
417
418 #define INTSZ   4
419
420 Elf_Arsym *
421 _libelf_ar_process_symtab(Elf *e, size_t *count)
422 {
423         size_t n, nentries, off;
424         Elf_Arsym *symtab, *sym;
425         unsigned char  *p, *s, *end;
426
427         assert(e != NULL);
428         assert(count != NULL);
429
430         if (e->e_u.e_ar.e_rawsymtabsz < INTSZ) {
431                 LIBELF_SET_ERROR(ARCHIVE, 0);
432                 return (NULL);
433         }
434
435         p = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
436         end = p + e->e_u.e_ar.e_rawsymtabsz;
437
438         GET_WORD(p, nentries);
439         p += INTSZ;
440
441         if (nentries == 0 || p + nentries * INTSZ >= end) {
442                 LIBELF_SET_ERROR(ARCHIVE, 0);
443                 return (NULL);
444         }
445
446         /* Allocate space for a nentries + a sentinel. */
447         if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
448                 LIBELF_SET_ERROR(RESOURCE, 0);
449                 return (NULL);
450         }
451
452         s = p + (nentries * INTSZ); /* start of the string table. */
453
454         for (n = nentries, sym = symtab; n > 0; n--) {
455                 off = 0;
456
457                 GET_WORD(p, off);
458
459                 sym->as_off = off;
460                 sym->as_hash = elf_hash(s);
461                 sym->as_name = s;
462
463                 p += INTSZ;
464                 sym++;
465
466                 for (; s < end && *s++ != '\0';) /* skip to next string */
467                         ;
468                 if (s > end) {
469                         LIBELF_SET_ERROR(ARCHIVE, 0);
470                         free(symtab);
471                         return (NULL);
472                 }
473         }
474
475         /* Fill up the sentinel entry. */
476         sym->as_name = NULL;
477         sym->as_hash = ~0UL;
478         sym->as_off = (off_t) 0;
479
480         *count = e->e_u.e_ar.e_symtabsz = nentries + 1;
481         e->e_u.e_ar.e_symtab = symtab;
482
483         return (symtab);
484 }