2 * Copyright (c) 2007 Kai Wang
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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/queue.h>
31 #include <archive_entry.h>
43 ELFTC_VCSID("$Id: write.c 3183 2015-04-10 16:18:42Z emaste $");
45 #define _ARMAG_LEN 8 /* length of the magic string */
46 #define _ARHDR_LEN 60 /* length of the archive header */
47 #define _INIT_AS_CAP 128 /* initial archive string table size */
48 #define _INIT_SYMOFF_CAP (256*(sizeof(uint32_t))) /* initial so table size */
49 #define _INIT_SYMNAME_CAP 1024 /* initial sn table size */
50 #define _MAXNAMELEN_SVR4 15 /* max member name length in svr4 variant */
51 #define _MAXNAMELEN_BSD 16 /* max member name length in bsd variant */
52 #define _TRUNCATE_LEN 15 /* number of bytes to keep for member name */
54 static void add_to_ar_str_table(struct bsdar *bsdar, const char *name);
55 static void add_to_ar_sym_table(struct bsdar *bsdar, const char *name);
56 static struct ar_obj *create_obj_from_file(struct bsdar *bsdar,
57 const char *name, time_t mtime);
58 static void create_symtab_entry(struct bsdar *bsdar, Elf *e);
59 static void free_obj(struct ar_obj *obj);
60 static void insert_obj(struct bsdar *bsdar, struct ar_obj *obj,
62 static void read_objs(struct bsdar *bsdar, const char *archive,
64 static void write_cleanup(struct bsdar *bsdar);
65 static void write_data(struct bsdar *bsdar, struct archive *a,
66 const void *buf, size_t s);
67 static void write_objs(struct bsdar *bsdar);
70 * Create an object from a file, and return the created object
71 * descriptor. Return NULL if either an error occurs, or if the '-u'
72 * option was specified and the member is not newer than the existing
75 static struct ar_obj *
76 create_obj_from_file(struct bsdar *bsdar, const char *name, time_t mtime)
87 obj = malloc(sizeof(struct ar_obj));
89 bsdar_errc(bsdar, errno, "malloc failed");
93 if ((fd = open(name, O_RDONLY, 0)) < 0) {
94 bsdar_warnc(bsdar, errno, "can't open file: %s", name);
99 tmpname = strdup(name);
100 if ((bname = basename(tmpname)) == NULL)
101 bsdar_errc(bsdar, errno, "basename failed");
102 if (bsdar->options & AR_TR && strlen(bname) > _TRUNCATE_LEN) {
103 if ((obj->name = malloc(_TRUNCATE_LEN + 1)) == NULL)
104 bsdar_errc(bsdar, errno, "malloc failed");
105 (void)strncpy(obj->name, bname, _TRUNCATE_LEN);
106 obj->name[_TRUNCATE_LEN] = '\0';
108 if ((obj->name = strdup(bname)) == NULL)
109 bsdar_errc(bsdar, errno, "strdup failed");
112 if (fstat(fd, &sb) < 0) {
113 bsdar_warnc(bsdar, errno, "can't fstat file: %s", obj->name);
116 if (!S_ISREG(sb.st_mode)) {
117 bsdar_warnc(bsdar, 0, "%s is not an ordinary file", obj->name);
121 if (sb.st_dev == bsdar->ar_dev && sb.st_ino == bsdar->ar_ino) {
122 bsdar_warnc(bsdar, 0, "cannot add archive \"%s\" to itself",
128 * If the '-u' option is specified and member is not newer
129 * than the existing one, we should not replace the member.
130 * However, if mtime == 0, i.e., if nonexistent members are to
131 * be forcibly replaced, then the '-u' option is to be ignored.
133 if (mtime != 0 && bsdar->options & AR_U && sb.st_mtime <= mtime)
137 * When the '-D' option is specified, the mtime and UID/GID of
138 * the member will be set to 0, and the file mode will be set
139 * to 644. This ensures that checksums will match for two
140 * archives containing identical content.
142 if (bsdar->options & AR_D) {
146 obj->md = S_IFREG | 0644;
148 obj->uid = sb.st_uid;
149 obj->gid = sb.st_gid;
150 obj->mtime = sb.st_mtime;
151 obj->md = sb.st_mode;
153 obj->size = sb.st_size;
154 obj->dev = sb.st_dev;
155 obj->ino = sb.st_ino;
157 if (obj->size == 0) {
161 if ((obj->elf = elf_open(fd)) == NULL) {
162 bsdar_warnc(bsdar, 0, "file initialization failed for %s: %s",
163 obj->name, elf_errmsg(-1));
168 * Read the object fully into memory and close its file
171 if (elf_cntl(obj->elf, ELF_C_FDREAD) < 0) {
172 bsdar_warnc(bsdar, 0, "%s could not be read in: %s",
173 obj->name, elf_errmsg(-1));
178 bsdar_errc(bsdar, errno, "close failed: %s",
188 bsdar_errc(bsdar, errno, "close failed: %s",
196 * Free an object and its associated allocations.
199 free_obj(struct ar_obj *obj)
209 * Insert an object into a list, either before/after the 'pos' obj or
210 * at the end of the list.
213 insert_obj(struct bsdar *bsdar, struct ar_obj *obj, struct ar_obj *pos)
216 bsdar_errc(bsdar, 0, "try to insert a null obj");
218 if (pos == NULL || obj == pos)
220 * If the object to move happens to be the position
221 * obj, or if there is no position obj, move the
226 if (bsdar->options & AR_B) {
227 TAILQ_INSERT_BEFORE(pos, obj, objs);
230 if (bsdar->options & AR_A) {
231 TAILQ_INSERT_AFTER(&bsdar->v_obj, pos, obj, objs);
236 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs);
241 * Read objects from archive into the 'v_obj' list. Note that
242 * 'checkargv' is set when read_objs() is used to read objects from
243 * the target of 'ADDLIB' command in ar script mode; in this case the
244 * 'argv' array specifies the members that 'ADDLIB' is to operate on.
247 read_objs(struct bsdar *bsdar, const char *archive, int checkargv)
250 struct archive_entry *entry;
259 if ((a = archive_read_new()) == NULL)
260 bsdar_errc(bsdar, 0, "archive_read_new failed");
261 archive_read_support_format_ar(a);
262 AC(archive_read_open_filename(a, archive, DEF_BLKSZ));
264 r = archive_read_next_header(a, &entry);
265 if (r == ARCHIVE_FATAL)
266 bsdar_errc(bsdar, 0, "%s", archive_error_string(a));
267 if (r == ARCHIVE_EOF)
269 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
270 bsdar_warnc(bsdar, 0, "%s", archive_error_string(a));
271 if (r == ARCHIVE_RETRY) {
272 bsdar_warnc(bsdar, 0, "Retrying...");
276 name = archive_entry_pathname(entry);
279 * Skip pseudo members.
281 if (bsdar_is_pseudomember(bsdar, name))
285 * If 'checkargv' is set, only read those members
288 if (checkargv && bsdar->argc > 0) {
290 for(i = 0; i < bsdar->argc; i++) {
291 av = &bsdar->argv[i];
294 if ((bname = basename(*av)) == NULL)
295 bsdar_errc(bsdar, errno,
297 if (strcmp(bname, name) != 0)
308 size = archive_entry_size(entry);
311 if ((buff = malloc(size)) == NULL)
312 bsdar_errc(bsdar, errno, "malloc failed");
313 if (archive_read_data(a, buff, size) != (ssize_t)size) {
314 bsdar_warnc(bsdar, 0, "%s",
315 archive_error_string(a));
322 obj = malloc(sizeof(struct ar_obj));
324 bsdar_errc(bsdar, errno, "malloc failed");
327 obj->elf = elf_openmemory(buff, size);
328 if (obj->elf == NULL) {
329 bsdar_warnc(bsdar, 0, "elf_openmemory() "
330 "failed for %s: %s", name,
337 if ((obj->name = strdup(name)) == NULL)
338 bsdar_errc(bsdar, errno, "strdup failed");
340 obj->uid = archive_entry_uid(entry);
341 obj->gid = archive_entry_gid(entry);
342 obj->md = archive_entry_mode(entry);
343 obj->mtime = archive_entry_mtime(entry);
347 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs);
349 AC(archive_read_close(a));
350 ACV(archive_read_free(a));
357 ar_write_archive(struct bsdar *bsdar, int mode)
359 struct ar_obj *nobj, *obj, *obj_temp, *pos;
365 TAILQ_INIT(&bsdar->v_obj);
368 memset(&sb, 0, sizeof(sb));
370 assert(mode == 'A' || mode == 'd' || mode == 'm' || mode == 'q' ||
371 mode == 'r' || mode == 's');
374 * Test if the specified archive exists, to determine
375 * whether we are creating a new archive.
377 if (stat(bsdar->filename, &sb) != 0) {
378 if (errno != ENOENT) {
379 bsdar_warnc(bsdar, 0, "stat %s failed",
384 /* We do not create archive in mode 'd', 'm' and 's'. */
385 if (mode != 'r' && mode != 'q') {
386 bsdar_warnc(bsdar, 0, "%s: no such file",
391 /* Issue a message if the '-c' option was not specified. */
392 if (!(bsdar->options & AR_C))
393 bsdar_warnc(bsdar, 0, "creating %s", bsdar->filename);
397 bsdar->ar_dev = sb.st_dev;
398 bsdar->ar_ino = sb.st_ino;
401 * First read members from the existing archive.
403 read_objs(bsdar, bsdar->filename, 0);
406 * For mode 's', no member will be moved, deleted or replaced.
412 * For mode 'q', we don't need to adjust existing members either.
413 * Also, -a, -b and -i are ignored in this mode. New members are
414 * always inserted at tail.
420 * Mode 'A' adds the contents of another archive to the tail
421 * of current archive. Note that mode 'A' is a special mode
422 * for the 'ADDLIB' command in ar's script mode. Currently
423 * there is no option that invokes this function from ar's
428 * Read objects from the target archive of the
429 * 'ADDLIB' command. If there are members specified in
430 * 'argv', read those members only, otherwise the
431 * entire archive will be read.
433 read_objs(bsdar, bsdar->addlib, 1);
438 * Try to find the position member specified by user.
440 if (bsdar->options & AR_A || bsdar->options & AR_B) {
441 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
442 if (strcmp(obj->name, bsdar->posarg) == 0) {
449 * If we cannot find the position specified by the
450 * user, silently insert objects at the tail of the
454 bsdar->options &= ~(AR_A | AR_B);
457 for (i = 0; i < bsdar->argc; i++) {
458 av = &bsdar->argv[i];
460 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
461 if ((bname = basename(*av)) == NULL)
462 bsdar_errc(bsdar, errno, "basename failed");
463 if (bsdar->options & AR_TR) {
464 if (strncmp(bname, obj->name, _TRUNCATE_LEN))
467 if (strcmp(bname, obj->name) != 0)
472 * If the new member should not
473 * replace the old one, skip it.
475 nobj = create_obj_from_file(bsdar, *av,
481 if (bsdar->options & AR_V)
482 (void)fprintf(bsdar->output, "%c - %s\n",
485 TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
486 if (mode == 'd' || mode == 'r')
490 insert_obj(bsdar, obj, pos);
492 insert_obj(bsdar, nobj, pos);
503 * When operating in mode 'r', directly add the specified
504 * objects which do not exist in current archive. When
505 * operating in mode 'q', all objects specified by the command
506 * line args are appended to the archive, without checking
507 * existing members in the archive.
509 for (i = 0; i < bsdar->argc; i++) {
510 av = &bsdar->argv[i];
511 if (*av != NULL && (mode == 'r' || mode == 'q')) {
512 nobj = create_obj_from_file(bsdar, *av, 0);
514 insert_obj(bsdar, nobj, pos);
515 if (bsdar->options & AR_V && nobj != NULL)
516 (void)fprintf(bsdar->output, "a - %s\n", *av);
523 write_cleanup(bsdar);
530 write_cleanup(struct bsdar *bsdar)
532 struct ar_obj *obj, *obj_temp;
534 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
535 TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
548 * Wrapper for archive_write_data().
551 write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s)
553 if (archive_write_data(a, buf, s) != (ssize_t)s)
554 bsdar_errc(bsdar, 0, "%s", archive_error_string(a));
558 * Compute the size of the symbol table for an archive.
561 bsdar_symtab_size(struct bsdar *bsdar)
565 if (bsdar->options & AR_BSD) {
567 * A BSD style symbol table has two parts.
568 * Each part is preceded by its size in bytes,
569 * encoded as a C 'long'. In the first part,
570 * there are 's_cnt' entries, each entry being
571 * 2 'long's in size. The second part
572 * contains a string table.
574 sz = 2 * sizeof(long) + (bsdar->s_cnt * 2 * sizeof(long)) +
578 * An SVR4 style symbol table comprises of a 32 bit
579 * number holding the number of entries, followed by
580 * that many 32-bit offsets, followed by a string
583 sz = sizeof(uint32_t) + bsdar->s_cnt * sizeof(uint32_t) +
591 write_svr4_symtab_entry(struct bsdar *bsdar, struct archive *a)
596 /* Translate offsets to big-endian form. */
597 for (i = 0; i < bsdar->s_cnt; i++)
598 bsdar->s_so[i] = htobe32(bsdar->s_so[i]);
600 nr = htobe32(bsdar->s_cnt);
601 write_data(bsdar, a, &nr, sizeof(uint32_t));
602 write_data(bsdar, a, bsdar->s_so, sizeof(uint32_t) *
604 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz);
608 write_bsd_symtab_entry(struct bsdar *bsdar, struct archive *a)
610 long br_sz, br_off, br_strx;
615 * Write out the size in the byte of the array of 'ranlib'
616 * descriptors to follow.
619 br_sz = (long) (bsdar->s_cnt * 2 * sizeof(long));
620 write_data(bsdar, a, &br_sz, sizeof(long));
623 * Write out the array of 'ranlib' descriptors. Each
624 * descriptor comprises of (a) an offset into the following
625 * string table and (b) a file offset to the relevant member.
627 for (i = 0, s = bsdar->s_sn; i < bsdar->s_cnt; i++) {
628 br_strx = (long) (s - bsdar->s_sn);
629 br_off = (long) bsdar->s_so[i];
630 write_data(bsdar, a, &br_strx, sizeof(long));
631 write_data(bsdar, a, &br_off, sizeof(long));
633 /* Find the start of the next symbol in the string table. */
639 * Write out the size of the string table as a 'long',
640 * followed by the string table itself.
642 br_sz = (long) bsdar->s_sn_sz;
643 write_data(bsdar, a, &br_sz, sizeof(long));
644 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz);
649 * Write the resulting archive members.
652 write_objs(struct bsdar *bsdar)
656 struct archive_entry *entry;
657 size_t s_sz; /* size of archive symbol table. */
658 size_t pm_sz; /* size of pseudo members */
659 size_t namelen; /* size of member name. */
660 size_t obj_sz; /* size of object + extended header. */
663 const char *entry_name;
668 * Create the archive symbol table and the archive string
671 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
672 if (!(bsdar->options & AR_SS) && obj->elf != NULL)
673 create_symtab_entry(bsdar, obj->elf);
676 namelen = strlen(obj->name);
677 if (bsdar->options & AR_BSD) {
678 /* Account for the space used by the file name. */
679 if (namelen > _MAXNAMELEN_BSD ||
680 strchr(obj->name, ' '))
682 } else if (namelen > _MAXNAMELEN_SVR4)
683 add_to_ar_str_table(bsdar, obj->name);
685 obj_sz += obj->size; /* add the actual object size */
687 /* Roundup the final size and add the header length. */
688 bsdar->rela_off += _ARHDR_LEN + obj_sz + (obj_sz & 1);
692 * Pad the symbol name string table. It is treated specially
693 * because symbol name table should be padded by a '\0', and
694 * not '\n' as for normal members. The size of the 'sn' table
695 * includes the pad byte.
697 if (bsdar->s_cnt != 0 && bsdar->s_sn_sz % 2 != 0)
698 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';
701 * The archive string table is padded by a "\n" like a normal
702 * member. The difference is that the size of archive string
703 * table includes the pad byte, while normal members' size
706 if (bsdar->as != NULL && bsdar->as_sz % 2 != 0)
707 bsdar->as[bsdar->as_sz++] = '\n';
710 * If there is a symbol table, calculate the size of pseudo
711 * members, and convert previously stored relative offsets to
714 * absolute_offset = relative_offset + size_of_pseudo_members)
717 s_sz = bsdar_symtab_size(bsdar);
718 if (bsdar->s_cnt != 0) {
719 pm_sz = _ARMAG_LEN + (_ARHDR_LEN + s_sz);
720 if (bsdar->as != NULL) /* SVR4 archives only */
721 pm_sz += _ARHDR_LEN + bsdar->as_sz;
722 for (i = 0; (size_t) i < bsdar->s_cnt; i++)
723 bsdar->s_so[i] = bsdar->s_so[i] + pm_sz;
726 if ((a = archive_write_new()) == NULL)
727 bsdar_errc(bsdar, 0, "archive_write_new failed");
729 if (bsdar->options & AR_BSD)
730 archive_write_set_format_ar_bsd(a);
732 archive_write_set_format_ar_svr4(a);
734 AC(archive_write_open_filename(a, bsdar->filename));
737 * Write the archive symbol table, if there is one. If
738 * options '-s' was explicitly specified or if we were invoked
739 * as 'ranlib', write the symbol table even if it is empty.
741 if ((bsdar->s_cnt != 0 && !(bsdar->options & AR_SS)) ||
742 bsdar->options & AR_S) {
743 if (bsdar->options & AR_BSD)
744 entry_name = AR_SYMTAB_NAME_BSD;
746 entry_name = AR_SYMTAB_NAME_SVR4;
748 entry = archive_entry_new();
749 archive_entry_copy_pathname(entry, entry_name);
750 if ((bsdar->options & AR_D) == 0)
751 archive_entry_set_mtime(entry, time(NULL), 0);
752 archive_entry_set_size(entry, s_sz);
753 AC(archive_write_header(a, entry));
754 if (bsdar->options & AR_BSD)
755 write_bsd_symtab_entry(bsdar, a);
757 write_svr4_symtab_entry(bsdar, a);
758 archive_entry_free(entry);
761 /* Write the archive string table, if any. */
762 if (bsdar->as != NULL) {
763 entry = archive_entry_new();
764 archive_entry_copy_pathname(entry, AR_STRINGTAB_NAME_SVR4);
765 archive_entry_set_size(entry, bsdar->as_sz);
766 AC(archive_write_header(a, entry));
767 write_data(bsdar, a, bsdar->as, bsdar->as_sz);
768 archive_entry_free(entry);
771 /* Write normal members. */
772 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
773 if ((buf = elf_rawfile(obj->elf, NULL)) == NULL) {
774 bsdar_warnc(bsdar, 0, "elf_rawfile() failed: %s",
779 entry = archive_entry_new();
780 archive_entry_copy_pathname(entry, obj->name);
781 archive_entry_set_uid(entry, obj->uid);
782 archive_entry_set_gid(entry, obj->gid);
783 archive_entry_set_mode(entry, obj->md);
784 archive_entry_set_size(entry, obj->size);
785 archive_entry_set_mtime(entry, obj->mtime, 0);
786 archive_entry_set_dev(entry, obj->dev);
787 archive_entry_set_ino(entry, obj->ino);
788 archive_entry_set_filetype(entry, AE_IFREG);
789 AC(archive_write_header(a, entry));
790 write_data(bsdar, a, buf, obj->size);
791 archive_entry_free(entry);
794 AC(archive_write_close(a));
795 ACV(archive_write_free(a));
799 * Extract global symbols from ELF binary members.
802 create_symtab_entry(struct bsdar *bsdar, Elf *e)
810 int elferr, tabndx, len, i;
812 if (elf_kind(e) != ELF_K_ELF) {
813 /* Silently a ignore non-ELF member. */
816 if (elf_getshstrndx(e, &shstrndx) == 0) {
817 bsdar_warnc(bsdar, 0, "elf_getshstrndx failed: %s",
824 while ((scn = elf_nextscn(e, scn)) != NULL) {
825 if (gelf_getshdr(scn, &shdr) != &shdr) {
826 bsdar_warnc(bsdar, 0,
827 "elf_getshdr failed: %s", elf_errmsg(-1));
830 if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL) {
831 bsdar_warnc(bsdar, 0,
832 "elf_strptr failed: %s", elf_errmsg(-1));
835 if (strcmp(name, ".strtab") == 0) {
836 tabndx = elf_ndxscn(scn);
840 elferr = elf_errno();
842 bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s",
845 bsdar_warnc(bsdar, 0, "can't find .strtab section");
850 while ((scn = elf_nextscn(e, scn)) != NULL) {
851 if (gelf_getshdr(scn, &shdr) != &shdr) {
852 bsdar_warnc(bsdar, 0, "elf_getshdr failed: %s",
856 if (shdr.sh_type != SHT_SYMTAB)
861 while (n < shdr.sh_size &&
862 (data = elf_getdata(scn, data)) != NULL) {
863 len = data->d_size / shdr.sh_entsize;
864 for (i = 0; i < len; i++) {
865 if (gelf_getsym(data, i, &sym) != &sym) {
866 bsdar_warnc(bsdar, 0,
867 "gelf_getsym failed: %s",
872 /* Keep only global and weak symbols. */
873 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL &&
874 GELF_ST_BIND(sym.st_info) != STB_WEAK)
877 /* Keep only defined symbols. */
878 if (sym.st_shndx == SHN_UNDEF)
881 if ((name = elf_strptr(e, tabndx,
882 sym.st_name)) == NULL) {
883 bsdar_warnc(bsdar, 0,
884 "elf_strptr failed: %s",
889 add_to_ar_sym_table(bsdar, name);
893 elferr = elf_errno();
895 bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s",
900 * Append to the archive string table buffer.
903 add_to_ar_str_table(struct bsdar *bsdar, const char *name)
906 if (bsdar->as == NULL) {
907 bsdar->as_cap = _INIT_AS_CAP;
909 if ((bsdar->as = malloc(bsdar->as_cap)) == NULL)
910 bsdar_errc(bsdar, errno, "malloc failed");
914 * The space required for holding one member name in the 'as'
915 * table includes: strlen(name) + (1 for '/') + (1 for '\n') +
916 * (possibly 1 for padding).
918 while (bsdar->as_sz + strlen(name) + 3 > bsdar->as_cap) {
920 bsdar->as = realloc(bsdar->as, bsdar->as_cap);
921 if (bsdar->as == NULL)
922 bsdar_errc(bsdar, errno, "realloc failed");
924 strncpy(&bsdar->as[bsdar->as_sz], name, strlen(name));
925 bsdar->as_sz += strlen(name);
926 bsdar->as[bsdar->as_sz++] = '/';
927 bsdar->as[bsdar->as_sz++] = '\n';
931 * Append to the archive symbol table buffer.
934 add_to_ar_sym_table(struct bsdar *bsdar, const char *name)
937 if (bsdar->s_so == NULL) {
938 if ((bsdar->s_so = malloc(_INIT_SYMOFF_CAP)) ==
940 bsdar_errc(bsdar, errno, "malloc failed");
941 bsdar->s_so_cap = _INIT_SYMOFF_CAP;
945 if (bsdar->s_sn == NULL) {
946 if ((bsdar->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL)
947 bsdar_errc(bsdar, errno, "malloc failed");
948 bsdar->s_sn_cap = _INIT_SYMNAME_CAP;
952 if (bsdar->s_cnt * sizeof(uint32_t) >= bsdar->s_so_cap) {
953 bsdar->s_so_cap *= 2;
954 bsdar->s_so = realloc(bsdar->s_so, bsdar->s_so_cap);
955 if (bsdar->s_so == NULL)
956 bsdar_errc(bsdar, errno, "realloc failed");
958 bsdar->s_so[bsdar->s_cnt] = bsdar->rela_off;
962 * The space required for holding one symbol name in the 'sn'
963 * table includes: strlen(name) + (1 for '\n') + (possibly 1
966 while (bsdar->s_sn_sz + strlen(name) + 2 > bsdar->s_sn_cap) {
967 bsdar->s_sn_cap *= 2;
968 bsdar->s_sn = realloc(bsdar->s_sn, bsdar->s_sn_cap);
969 if (bsdar->s_sn == NULL)
970 bsdar_errc(bsdar, errno, "realloc failed");
972 strncpy(&bsdar->s_sn[bsdar->s_sn_sz], name, strlen(name));
973 bsdar->s_sn_sz += strlen(name);
974 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';