summaryrefslogtreecommitdiff
path: root/ext/libelf/libelf_ar.c
blob: 01e16245582fd18b401bcd2e3b3ee3cd7390020b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*-
 * Copyright (c) 2006 Joseph Koshy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <ar.h>
#include <assert.h>
#include <ctype.h>
#include "libelf.h"
#include <stdlib.h>
#include <string.h>

#include "_libelf.h"

#define	LIBELF_NALLOC_SIZE	16

/*
 * `ar' archive handling.
 *
 * `ar' archives start with signature `ARMAG'.  Each archive member is
 * preceded by a header containing meta-data for the member.  This
 * header is described in <ar.h> (struct ar_hdr).  The header always
 * starts on an even address.  File data is padded with "\n"
 * characters to keep this invariant.
 *
 * Special considerations for `ar' archives:
 *
 * The `ar' header only has space for a 16 character file name.  File
 * names are terminated with a '/', so this effectively leaves 15
 * characters for the actual file name.  In order to accomodate longer
 * file names, names may be stored in a separate 'string table' and
 * referenced indirectly by a member header.  The string table itself
 * appears as an archive member with name "// ".  An indirect file name
 * in an `ar' header matches the pattern "/[0-9]*". The digits form a
 * decimal number that corresponds to a byte offset into the string
 * table where the actual file name of the object starts.  Strings in
 * the string table are padded to start on even addresses.
 *
 * Archives may also have a symbol table (see ranlib(1)), mapping
 * program symbols to object files inside the archive.  A symbol table
 * uses a file name of "/ " in its archive header.  The symbol table
 * is structured as:
 *  - a 4-byte count of entries stored as a binary value, MSB first
 *  - 'n' 4-byte offsets, stored as binary values, MSB first
 *  - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
 *
 * If the symbol table and string table are is present in an archive
 * they must be the very first objects and in that order.
 */

/*
 * Convert a string bounded by `start' and `start+sz' (exclusive) to a
 * number in the specified base.
 */
static int
_libelf_ar_get_number(char *s, size_t sz, int base, size_t *ret)
{
        int c, v;
        size_t r;
        char *e;

        assert(base <= 10);

        e = s + sz;

        /* skip leading blanks */
        for (;s < e && (c = *s) == ' '; s++)
                ;

        r = 0L;
        for (;s < e; s++) {
                if ((c = *s) == ' ')
                        break;
                if (c < '0' || c > '9')
                        return (0);
                v = c - '0';
                if (v >= base)		/* Illegal digit. */
                        break;
                r *= base;
                r += v;
        }

        *ret = r;

        return (1);
}

/*
 * Retrieve a string from a name field.  If `rawname' is set, leave
 * ar(1) control characters in.
 */
static char *
_libelf_ar_get_string(const char *buf, size_t bufsize, int rawname)
{
        const char *q;
        char *r;
        size_t sz;

        if (rawname)
                sz = bufsize + 1;
        else {
                /* Skip back over trailing blanks. */
                for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
                        ;

                if (q < buf) {
                        /*
                         * If the input buffer only had blanks in it,
                         * return a zero-length string.
                         */
                        buf = "";
                        sz = 1;
                } else {
                        /*
                         * Remove the trailing '/' character, but only
                         * if the name isn't one of the special names
                         * "/" and "//".
                         */
                        if (q > buf + 1 ||
                            (q == (buf + 1) && *buf != '/'))
                                q--;

                        sz = q - buf + 2; /* Space for a trailing NUL. */
                }
        }

        if ((r = malloc(sz)) == NULL) {
                LIBELF_SET_ERROR(RESOURCE, 0);
                return (NULL);
        }

        (void) strncpy(r, buf, sz);
        r[sz - 1] = '\0';

        return (r);
}

/*
 * Retrieve the full name of the archive member.
 */
static char *
_libelf_ar_get_name(char *buf, size_t bufsize, Elf *e)
{
        char c, *q, *r, *s;
        size_t len;
        size_t offset;

        assert(e->e_kind == ELF_K_AR);

        if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
                /*
                 * The value in field ar_name is a decimal offset into
                 * the archive string table where the actual name
                 * resides.
                 */
                if (_libelf_ar_get_number(buf + 1, bufsize - 1, 10,
                    &offset) == 0) {
                        LIBELF_SET_ERROR(ARCHIVE, 0);
                        return (NULL);
                }

                if (offset > e->e_u.e_ar.e_rawstrtabsz) {
                        LIBELF_SET_ERROR(ARCHIVE, 0);
                        return (NULL);
                }

                s = q = e->e_u.e_ar.e_rawstrtab + offset;
                r = e->e_u.e_ar.e_rawstrtab + e->e_u.e_ar.e_rawstrtabsz;

                for (s = q; s < r && *s != '/'; s++)
                        ;
                len = s - q + 1; /* space for the trailing NUL */

                if ((s = malloc(len)) == NULL) {
                        LIBELF_SET_ERROR(RESOURCE, 0);
                        return (NULL);
                }

                (void) strncpy(s, q, len);
                s[len - 1] = '\0';

                return (s);
        }

        /*
         * Normal 'name'
         */
        return (_libelf_ar_get_string(buf, bufsize, 0));
}


Elf_Arhdr *
_libelf_ar_gethdr(Elf *e)
{
        Elf *parent;
        struct ar_hdr *arh;
        Elf_Arhdr *eh;
        size_t n;

        if ((parent = e->e_parent) == NULL) {
                LIBELF_SET_ERROR(ARGUMENT, 0);
                return (NULL);
        }

        arh = (struct ar_hdr *) ((uintptr_t) e->e_rawfile - sizeof(struct ar_hdr));

        assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
        assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile + parent->e_rawsize -
            sizeof(struct ar_hdr));

        if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
                LIBELF_SET_ERROR(RESOURCE, 0);
                return (NULL);
        }

        e->e_arhdr = eh;
        eh->ar_name = eh->ar_rawname = NULL;

        if ((eh->ar_name = _libelf_ar_get_name(arh->ar_name, sizeof(arh->ar_name),
                 parent)) == NULL)
                goto error;

        if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10, &n) == 0)
                goto error;
        eh->ar_uid = (uid_t) n;

        if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10, &n) == 0)
                goto error;
        eh->ar_gid = (gid_t) n;

        if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8, &n) == 0)
                goto error;
        eh->ar_mode = (mode_t) n;

        if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &n) == 0)
                goto error;
        eh->ar_size = n;

        if ((eh->ar_rawname = _libelf_ar_get_string(arh->ar_name,
                 sizeof(arh->ar_name), 1)) == NULL)
                goto error;

        return (eh);

 error:
        if (eh) {
                if (eh->ar_name)
                        free(eh->ar_name);
                if (eh->ar_rawname)
                        free(eh->ar_rawname);
                free(eh);
        }
        e->e_arhdr = NULL;

        return (NULL);
}

Elf *
_libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
{
        Elf *e;
        off_t next;
        struct ar_hdr *arh;
        size_t sz;

        assert(elf->e_kind == ELF_K_AR);

        next = elf->e_u.e_ar.e_next;

        /*
         * `next' is only set to zero by elf_next() when the last
         * member of an archive is processed.
         */
        if (next == (off_t) 0)
                return (NULL);

        assert((next & 1) == 0);

        arh = (struct ar_hdr *) (elf->e_rawfile + next);

        if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &sz) == 0) {
                LIBELF_SET_ERROR(ARCHIVE, 0);
                return (NULL);
        }

        assert(sz > 0);

        arh++;	/* skip over archive member header */

        if ((e = elf_memory((char *) arh, sz)) == NULL)
                return (NULL);

        e->e_fd = fd;
        e->e_cmd = c;

        elf->e_u.e_ar.e_nchildren++;
        e->e_parent = elf;

        return (e);
}

Elf *
_libelf_ar_open(Elf *e)
{
        int i;
        char *s, *end;
        size_t sz;
        struct ar_hdr arh;

        e->e_kind = ELF_K_AR;
        e->e_u.e_ar.e_nchildren = 0;
        e->e_u.e_ar.e_next = (off_t) -1;

        /*
         * Look for special members.
         */

        s = e->e_rawfile + SARMAG;
        end = e->e_rawfile + e->e_rawsize;

        assert(e->e_rawsize > 0);

        /*
         * Look for magic names "/ " and "// " in the first two entries
         * of the archive.
         */
        for (i = 0; i < 2; i++) {

                if (s + sizeof(arh) > end) {
                        LIBELF_SET_ERROR(ARCHIVE, 0);
                        return (NULL);
                }

                (void) memcpy(&arh, s, sizeof(arh));

                if (arh.ar_fmag[0] != '`' || arh.ar_fmag[1] != '\n') {
                        LIBELF_SET_ERROR(ARCHIVE, 0);
                        return (NULL);
                }

                if (arh.ar_name[0] != '/')	/* not a special symbol */
                        break;

                if (_libelf_ar_get_number(arh.ar_size, sizeof(arh.ar_size), 10, &sz) == 0) {
                        LIBELF_SET_ERROR(ARCHIVE, 0);
                        return (NULL);
                }

                assert(sz > 0);

                s += sizeof(arh);

                if (arh.ar_name[1] == ' ') {	/* "/ " => symbol table */

                        e->e_u.e_ar.e_rawsymtab = s;
                        e->e_u.e_ar.e_rawsymtabsz = sz;

                } else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {

                        /* "// " => string table for long file names */
                        e->e_u.e_ar.e_rawstrtab = s;
                        e->e_u.e_ar.e_rawstrtabsz = sz;
                }

                sz = LIBELF_ADJUST_AR_SIZE(sz);

                s += sz;
        }

        e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);

        return (e);
}

/*
 * An ar(1) symbol table has the following layout:
 *
 * The first 4 bytes are a binary count of the number of entries in the
 * symbol table, stored MSB-first.
 *
 * Then there are 'n' 4-byte binary offsets, also stored MSB first.
 *
 * Following this, there are 'n' null-terminated strings.
 */

#define	GET_WORD(P, V) do {			\
                (V) = 0;			\
                (V) = (P)[0]; (V) <<= 8;	\
                (V) += (P)[1]; (V) <<= 8;	\
                (V) += (P)[2]; (V) <<= 8;	\
                (V) += (P)[3];			\
        } while (0)

#define	INTSZ	4

Elf_Arsym *
_libelf_ar_process_symtab(Elf *e, size_t *count)
{
        size_t n, nentries, off;
        Elf_Arsym *symtab, *sym;
        char *p, *s, *end;

        assert(e != NULL);
        assert(count != NULL);

        if (e->e_u.e_ar.e_rawsymtabsz < INTSZ) {
                LIBELF_SET_ERROR(ARCHIVE, 0);
                return (NULL);
        }

        p = e->e_u.e_ar.e_rawsymtab;
        end = p + e->e_u.e_ar.e_rawsymtabsz;

        GET_WORD(p, nentries);
        p += INTSZ;

        if (nentries == 0 || p + nentries * INTSZ >= end) {
                LIBELF_SET_ERROR(ARCHIVE, 0);
                return (NULL);
        }

        /* Allocate space for a nentries + a sentinel. */
        if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
                LIBELF_SET_ERROR(RESOURCE, 0);
                return (NULL);
        }

        s = p + (nentries * INTSZ); /* start of the string table. */

        for (n = nentries, sym = symtab; n > 0; n--) {
                off = 0;

                GET_WORD(p, off);

                sym->as_off = off;
                sym->as_hash = elf_hash(s);
                sym->as_name = s;

                p += INTSZ;
                sym++;

                for (; s < end && *s++ != '\0';) /* skip to next string */
                        ;
                if (s > end) {
                        LIBELF_SET_ERROR(ARCHIVE, 0);
                        free(symtab);
                        return (NULL);
                }
        }

        /* Fill up the sentinel entry. */
        sym->as_name = NULL;
        sym->as_hash = ~0UL;
        sym->as_off = (off_t) 0;

        *count = e->e_u.e_ar.e_symtabsz = nentries + 1;
        e->e_u.e_ar.e_symtab = symtab;

        return (symtab);
}