2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007 Kai Wang
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/endian.h>
34 #include <sys/queue.h>
37 #include <archive_entry.h>
50 #define _ARMAG_LEN 8 /* length of ar magic string */
51 #define _ARHDR_LEN 60 /* length of ar header */
52 #define _INIT_AS_CAP 128 /* initial archive string table size */
53 #define _INIT_SYMOFF_CAP (256*(sizeof(uint64_t))) /* initial so table size */
54 #define _INIT_SYMNAME_CAP 1024 /* initial sn table size */
55 #define _MAXNAMELEN_SVR4 15 /* max member name length in svr4 variant */
56 #define _TRUNCATE_LEN 15 /* number of bytes to keep for member name */
58 static void add_to_ar_str_table(struct bsdar *bsdar, const char *name);
59 static void add_to_ar_sym_table(struct bsdar *bsdar, const char *name);
60 static struct ar_obj *create_obj_from_file(struct bsdar *bsdar,
61 const char *name, time_t mtime);
62 static void create_symtab_entry(struct bsdar *bsdar, void *maddr,
64 static void free_obj(struct bsdar *bsdar, struct ar_obj *obj);
65 static void insert_obj(struct bsdar *bsdar, struct ar_obj *obj,
67 static void prefault_buffer(const char *buf, size_t s);
68 static void read_objs(struct bsdar *bsdar, const char *archive,
70 static void write_archive(struct bsdar *bsdar, char mode);
71 static void write_cleanup(struct bsdar *bsdar);
72 static void write_data(struct bsdar *bsdar, struct archive *a,
73 const void *buf, size_t s);
74 static void write_objs(struct bsdar *bsdar);
77 ar_mode_d(struct bsdar *bsdar)
80 write_archive(bsdar, 'd');
84 ar_mode_m(struct bsdar *bsdar)
87 write_archive(bsdar, 'm');
91 ar_mode_q(struct bsdar *bsdar)
94 write_archive(bsdar, 'q');
98 ar_mode_r(struct bsdar *bsdar)
101 write_archive(bsdar, 'r');
105 ar_mode_s(struct bsdar *bsdar)
108 write_archive(bsdar, 's');
112 ar_mode_A(struct bsdar *bsdar)
115 write_archive(bsdar, 'A');
119 * Create object from file, return created obj upon success, or NULL
120 * when an error occurs or the member is not newer than existing
121 * one while -u is specified.
123 static struct ar_obj *
124 create_obj_from_file(struct bsdar *bsdar, const char *name, time_t mtime)
134 obj = malloc(sizeof(struct ar_obj));
136 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
137 if ((obj->fd = open(name, O_RDONLY, 0)) < 0) {
138 bsdar_warnc(bsdar, errno, "can't open file: %s", name);
143 tmpname = strdup(name);
145 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
146 if ((bname = basename(tmpname)) == NULL)
147 bsdar_errc(bsdar, EX_SOFTWARE, errno, "basename failed");
148 if (bsdar->options & AR_TR && strlen(bname) > _TRUNCATE_LEN) {
149 if ((obj->name = malloc(_TRUNCATE_LEN + 1)) == NULL)
150 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
151 (void)strncpy(obj->name, bname, _TRUNCATE_LEN);
152 obj->name[_TRUNCATE_LEN] = '\0';
154 if ((obj->name = strdup(bname)) == NULL)
155 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
158 if (fstat(obj->fd, &sb) < 0) {
159 bsdar_warnc(bsdar, errno, "can't fstat file: %s", obj->name);
162 if (!S_ISREG(sb.st_mode)) {
163 bsdar_warnc(bsdar, 0, "%s is not an ordinary file", obj->name);
168 * When option '-u' is specified and member is not newer than the
169 * existing one, the replace will not happen. While if mtime == 0,
170 * which indicates that this is to "replace a none exist member",
171 * the replace will proceed regardless of '-u'.
173 if (mtime != 0 && bsdar->options & AR_U && sb.st_mtime <= mtime)
177 * When option '-D' is specified, mtime and UID / GID from the file
178 * will be replaced with 0, and file mode with 644. This ensures that
179 * checksums will match for two archives containing the exact same
182 if (bsdar->options & AR_D) {
186 obj->md = S_IFREG | 0644;
188 obj->uid = sb.st_uid;
189 obj->gid = sb.st_gid;
190 obj->mtime = sb.st_mtime;
191 obj->md = sb.st_mode;
193 obj->size = sb.st_size;
194 obj->dev = sb.st_dev;
195 obj->ino = sb.st_ino;
197 if (obj->size == 0) {
202 if ((obj->maddr = mmap(NULL, obj->size, PROT_READ,
203 MAP_PRIVATE, obj->fd, (off_t)0)) == MAP_FAILED) {
204 bsdar_warnc(bsdar, errno, "can't mmap file: %s", obj->name);
207 if (close(obj->fd) < 0)
208 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s",
214 if (close(obj->fd) < 0)
215 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s",
223 * Free object itself and its associated allocations.
226 free_obj(struct bsdar *bsdar, struct ar_obj *obj)
231 if (obj->maddr != NULL && munmap(obj->maddr, obj->size))
232 bsdar_warnc(bsdar, errno,
233 "can't munmap file: %s", obj->name);
239 * Insert obj to the tail, or before/after the pos obj.
242 insert_obj(struct bsdar *bsdar, struct ar_obj *obj, struct ar_obj *pos)
245 bsdar_errc(bsdar, EX_SOFTWARE, 0, "try to insert a null obj");
247 if (pos == NULL || obj == pos)
249 * If the object to move happens to be the position obj,
250 * or if there is not a pos obj, move it to tail.
254 if (bsdar->options & AR_B) {
255 TAILQ_INSERT_BEFORE(pos, obj, objs);
258 if (bsdar->options & AR_A) {
259 TAILQ_INSERT_AFTER(&bsdar->v_obj, pos, obj, objs);
264 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs);
269 * Read objects from archive into v_obj list. Note that checkargv is
270 * set when read_objs is used to read objects from the target of
271 * ADDLIB command (ar script mode), in this case argv array possibly
272 * specifies the members ADDLIB want.
275 read_objs(struct bsdar *bsdar, const char *archive, int checkargv)
278 struct archive_entry *entry;
287 if ((a = archive_read_new()) == NULL)
288 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed");
289 archive_read_support_format_ar(a);
290 AC(archive_read_open_filename(a, archive, DEF_BLKSZ));
292 r = archive_read_next_header(a, &entry);
293 if (r == ARCHIVE_FATAL)
294 bsdar_errc(bsdar, EX_DATAERR, archive_errno(a), "%s",
295 archive_error_string(a));
296 if (r == ARCHIVE_EOF)
298 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
299 bsdar_warnc(bsdar, archive_errno(a), "%s",
300 archive_error_string(a));
301 if (r == ARCHIVE_RETRY) {
302 bsdar_warnc(bsdar, 0, "Retrying...");
306 name = archive_entry_pathname(entry);
309 * skip pseudo members.
311 if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0)
315 * If checkargv is set, only read those members specified
318 if (checkargv && bsdar->argc > 0) {
320 for(i = 0; i < bsdar->argc; i++) {
321 av = &bsdar->argv[i];
324 if ((bname = basename(*av)) == NULL)
325 bsdar_errc(bsdar, EX_SOFTWARE, errno,
327 if (strcmp(bname, name) != 0)
338 size = archive_entry_size(entry);
341 if ((buff = malloc(size)) == NULL)
342 bsdar_errc(bsdar, EX_SOFTWARE, errno,
344 if (archive_read_data(a, buff, size) != (ssize_t)size) {
345 bsdar_warnc(bsdar, archive_errno(a), "%s",
346 archive_error_string(a));
353 obj = malloc(sizeof(struct ar_obj));
355 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
357 if ((obj->name = strdup(name)) == NULL)
358 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
360 obj->uid = archive_entry_uid(entry);
361 obj->gid = archive_entry_gid(entry);
362 obj->md = archive_entry_mode(entry);
363 obj->mtime = archive_entry_mtime(entry);
368 * Objects from archive have obj->fd set to -1,
369 * for the ease of cleaning up.
372 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs);
374 AC(archive_read_close(a));
375 AC(archive_read_free(a));
379 * Determine the constitution of resulting archive.
382 write_archive(struct bsdar *bsdar, char mode)
384 struct ar_obj *nobj, *obj, *obj_temp, *pos;
390 TAILQ_INIT(&bsdar->v_obj);
393 memset(&sb, 0, sizeof(sb));
396 * Test if the specified archive exists, to figure out
397 * whether we are creating one here.
399 if (stat(bsdar->filename, &sb) != 0) {
400 if (errno != ENOENT) {
401 bsdar_warnc(bsdar, 0, "stat %s failed",
406 /* We do not create archive in mode 'd', 'm' and 's'. */
407 if (mode != 'r' && mode != 'q') {
408 bsdar_warnc(bsdar, 0, "%s: no such file",
413 /* Issue a warning if -c is not specified when creating. */
414 if (!(bsdar->options & AR_C))
415 bsdar_warnc(bsdar, 0, "creating %s", bsdar->filename);
420 * First read members from existing archive.
422 read_objs(bsdar, bsdar->filename, 0);
425 * For mode 's', no member will be moved, deleted or replaced.
431 * For mode 'q', we don't need to adjust existing members either.
432 * Also, -a, -b and -i are ignored in this mode. New members are
433 * always inserted at tail.
439 * Mode 'A' adds the contents of another archive to the tail of
440 * current archive. Note that mode 'A' is a special mode for the
441 * ADDLIB command of the ar script mode. Currently there is no
442 * access to this function from the ar command line mode.
446 * Read objects from the target archive of ADDLIB command.
447 * If there are members specified in argv, read those members
448 * only, otherwise the entire archive will be read.
450 read_objs(bsdar, bsdar->addlib, 1);
455 * Try to find the position member specified by user.
457 if (bsdar->options & AR_A || bsdar->options & AR_B) {
458 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
459 if (strcmp(obj->name, bsdar->posarg) == 0) {
466 * If can't find `pos' specified by user,
467 * silently insert objects at tail.
470 bsdar->options &= ~(AR_A | AR_B);
473 for (i = 0; i < bsdar->argc; i++) {
474 av = &bsdar->argv[i];
476 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
477 if ((bname = basename(*av)) == NULL)
478 bsdar_errc(bsdar, EX_SOFTWARE, errno,
480 if (bsdar->options & AR_TR) {
481 if (strncmp(bname, obj->name, _TRUNCATE_LEN))
484 if (strcmp(bname, obj->name) != 0)
489 * if the new member is not qualified
490 * to replace the old one, skip it.
492 nobj = create_obj_from_file(bsdar, *av,
498 if (bsdar->options & AR_V)
499 (void)fprintf(stdout, "%c - %s\n", mode,
502 TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
503 if (mode == 'd' || mode == 'r')
504 free_obj(bsdar, obj);
507 insert_obj(bsdar, obj, pos);
509 insert_obj(bsdar, nobj, pos);
520 * When operating in mode 'r', directly add those user specified
521 * objects which do not exist in current archive. When operating
522 * in mode 'q', all objects specified in command line args are
523 * appended to the archive, without comparing with existing ones.
525 for (i = 0; i < bsdar->argc; i++) {
526 av = &bsdar->argv[i];
527 if (*av != NULL && (mode == 'r' || mode == 'q')) {
528 nobj = create_obj_from_file(bsdar, *av, 0);
530 insert_obj(bsdar, nobj, pos);
531 if (bsdar->options & AR_V && nobj != NULL)
532 (void)fprintf(stdout, "a - %s\n", *av);
539 write_cleanup(bsdar);
543 * Memory cleaning up.
546 write_cleanup(struct bsdar *bsdar)
548 struct ar_obj *obj, *obj_temp;
550 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
551 TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
552 free_obj(bsdar, obj);
565 * Fault in the buffer prior to writing as a workaround for poor performance
566 * due to interaction with kernel fs deadlock avoidance code. See the comment
567 * above vn_io_fault_doio() in sys/kern/vfs_vnops.c for details of the issue.
570 prefault_buffer(const char *buf, size_t s)
572 volatile const char *p;
577 page_size = sysconf(_SC_PAGESIZE);
578 for (p = buf; p < buf + s; p += page_size)
581 * Ensure we touch the last page as well, in case the buffer is not
584 *(volatile const char *)(buf + s - 1);
588 * Wrapper for archive_write_data().
591 write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s)
595 prefault_buffer(buf, s);
597 written = archive_write_data(a, buf, s);
599 bsdar_errc(bsdar, EX_SOFTWARE, archive_errno(a), "%s",
600 archive_error_string(a));
601 buf = (const char *)buf + written;
607 * Write the resulting archive members.
610 write_objs(struct bsdar *bsdar)
614 struct archive_entry *entry;
615 size_t s_sz; /* size of archive symbol table. */
616 size_t pm_sz; /* size of pseudo members */
617 size_t w_sz; /* size of words in symbol table */
622 if (elf_version(EV_CURRENT) == EV_NONE)
623 bsdar_errc(bsdar, EX_SOFTWARE, 0,
624 "ELF library initialization failed: %s", elf_errmsg(-1));
628 /* Create archive symbol table and archive string table, if need. */
629 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
630 if (!(bsdar->options & AR_SS) && obj->maddr != NULL)
631 create_symtab_entry(bsdar, obj->maddr, obj->size);
632 if (strlen(obj->name) > _MAXNAMELEN_SVR4)
633 add_to_ar_str_table(bsdar, obj->name);
634 bsdar->rela_off += _ARHDR_LEN + obj->size + obj->size % 2;
638 * Pad the symbol name string table. It is treated specially because
639 * symbol name table should be padded by a '\0', not the common '\n'
640 * for other members. The size of sn table includes the pad bit.
642 if (bsdar->s_cnt != 0 && bsdar->s_sn_sz % 2 != 0)
643 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';
646 * Archive string table is padded by a "\n" as the normal members.
647 * The difference is that the size of archive string table counts
648 * in the pad bit, while normal members' size fileds do not.
650 if (bsdar->as != NULL && bsdar->as_sz % 2 != 0)
651 bsdar->as[bsdar->as_sz++] = '\n';
654 * If there is a symbol table, calculate the size of pseudo members,
655 * convert previously stored relative offsets to absolute ones, and
656 * then make them Big Endian.
658 * absolute_offset = htobe32(relative_offset + size_of_pseudo_members)
660 w_sz = sizeof(uint32_t);
661 if (bsdar->s_cnt != 0) {
662 s_sz = (bsdar->s_cnt + 1) * sizeof(uint32_t) + bsdar->s_sn_sz;
663 pm_sz = _ARMAG_LEN + (_ARHDR_LEN + s_sz);
664 if (bsdar->as != NULL)
665 pm_sz += _ARHDR_LEN + bsdar->as_sz;
666 /* Use the 64-bit word size format if necessary. */
667 if (bsdar->s_so_max > UINT32_MAX - pm_sz) {
668 w_sz = sizeof(uint64_t);
670 s_sz = (bsdar->s_cnt + 1) * sizeof(uint64_t) +
673 /* Convert to big-endian. */
674 for (i = 0; i < bsdar->s_cnt; i++)
676 htobe64(bsdar->s_so[i] + pm_sz);
679 * Convert to big-endian and shuffle in-place to
680 * the front of the allocation. XXX UB
682 for (i = 0; i < bsdar->s_cnt; i++)
683 ((uint32_t *)(bsdar->s_so))[i] =
684 htobe32(bsdar->s_so[i] + pm_sz);
688 if ((a = archive_write_new()) == NULL)
689 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_write_new failed");
691 archive_write_set_format_ar_svr4(a);
693 AC(archive_write_open_filename(a, bsdar->filename));
696 * write the archive symbol table, if there is one.
697 * If options -s is explicitly specified or we are invoked
698 * as ranlib, write the symbol table even if it is empty.
700 if ((bsdar->s_cnt != 0 && !(bsdar->options & AR_SS)) ||
701 bsdar->options & AR_S) {
702 entry = archive_entry_new();
704 bsdar_errc(bsdar, EX_SOFTWARE, 0,
705 "archive_entry_new failed");
706 if (w_sz == sizeof(uint64_t))
707 archive_entry_copy_pathname(entry, "/SYM64/");
709 archive_entry_copy_pathname(entry, "/");
710 if ((bsdar->options & AR_D) == 0)
711 archive_entry_set_mtime(entry, time(NULL), 0);
712 archive_entry_set_size(entry, (bsdar->s_cnt + 1) * w_sz +
714 AC(archive_write_header(a, entry));
715 if (w_sz == sizeof(uint64_t)) {
716 nr = htobe64(bsdar->s_cnt);
717 write_data(bsdar, a, &nr, sizeof(nr));
719 nr32 = htobe32((uint32_t)bsdar->s_cnt);
720 write_data(bsdar, a, &nr32, sizeof(nr32));
722 write_data(bsdar, a, bsdar->s_so, w_sz * bsdar->s_cnt);
723 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz);
724 archive_entry_free(entry);
727 /* write the archive string table, if any. */
728 if (bsdar->as != NULL) {
729 entry = archive_entry_new();
731 bsdar_errc(bsdar, EX_SOFTWARE, 0,
732 "archive_entry_new failed");
733 archive_entry_copy_pathname(entry, "//");
734 archive_entry_set_size(entry, bsdar->as_sz);
735 AC(archive_write_header(a, entry));
736 write_data(bsdar, a, bsdar->as, bsdar->as_sz);
737 archive_entry_free(entry);
740 /* write normal members. */
741 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
742 entry = archive_entry_new();
744 bsdar_errc(bsdar, EX_SOFTWARE, 0,
745 "archive_entry_new failed");
746 archive_entry_copy_pathname(entry, obj->name);
747 archive_entry_set_uid(entry, obj->uid);
748 archive_entry_set_gid(entry, obj->gid);
749 archive_entry_set_mode(entry, obj->md);
750 archive_entry_set_size(entry, obj->size);
751 archive_entry_set_mtime(entry, obj->mtime, 0);
752 archive_entry_set_dev(entry, obj->dev);
753 archive_entry_set_ino(entry, obj->ino);
754 archive_entry_set_filetype(entry, AE_IFREG);
755 AC(archive_write_header(a, entry));
756 write_data(bsdar, a, obj->maddr, obj->size);
757 archive_entry_free(entry);
760 AC(archive_write_close(a));
761 AC(archive_write_free(a));
765 * Extract global symbols from ELF binary members.
768 create_symtab_entry(struct bsdar *bsdar, void *maddr, size_t size)
777 int elferr, tabndx, len, i;
779 if ((e = elf_memory(maddr, size)) == NULL) {
780 bsdar_warnc(bsdar, 0, "elf_memory() failed: %s",
784 if (elf_kind(e) != ELF_K_ELF) {
785 /* Silently ignore non-elf member. */
789 if (elf_getshstrndx(e, &shstrndx) == 0) {
790 bsdar_warnc(bsdar, EX_SOFTWARE, 0, "elf_getshstrndx failed: %s",
798 while ((scn = elf_nextscn(e, scn)) != NULL) {
799 if (gelf_getshdr(scn, &shdr) != &shdr) {
800 bsdar_warnc(bsdar, 0,
801 "elf_getshdr failed: %s", elf_errmsg(-1));
804 if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL) {
805 bsdar_warnc(bsdar, 0,
806 "elf_strptr failed: %s", elf_errmsg(-1));
809 if (strcmp(name, ".strtab") == 0) {
810 tabndx = elf_ndxscn(scn);
814 elferr = elf_errno();
816 bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s",
819 bsdar_warnc(bsdar, 0, "can't find .strtab section");
825 while ((scn = elf_nextscn(e, scn)) != NULL) {
826 if (gelf_getshdr(scn, &shdr) != &shdr) {
827 bsdar_warnc(bsdar, EX_SOFTWARE, 0,
828 "elf_getshdr failed: %s", elf_errmsg(-1));
831 if (shdr.sh_type != SHT_SYMTAB)
836 while (n < shdr.sh_size &&
837 (data = elf_getdata(scn, data)) != NULL) {
838 len = data->d_size / shdr.sh_entsize;
839 for (i = 0; i < len; i++) {
840 if (gelf_getsym(data, i, &sym) != &sym) {
841 bsdar_warnc(bsdar, EX_SOFTWARE, 0,
842 "gelf_getsym failed: %s",
847 /* keep only global or weak symbols */
848 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL &&
849 GELF_ST_BIND(sym.st_info) != STB_WEAK)
852 /* keep only defined symbols */
853 if (sym.st_shndx == SHN_UNDEF)
856 if ((name = elf_strptr(e, tabndx,
857 sym.st_name)) == NULL) {
858 bsdar_warnc(bsdar, EX_SOFTWARE, 0,
859 "elf_strptr failed: %s",
864 add_to_ar_sym_table(bsdar, name);
868 elferr = elf_errno();
870 bsdar_warnc(bsdar, EX_SOFTWARE, 0, "elf_nextscn failed: %s",
877 * Append to the archive string table buffer.
880 add_to_ar_str_table(struct bsdar *bsdar, const char *name)
883 if (bsdar->as == NULL) {
884 bsdar->as_cap = _INIT_AS_CAP;
886 if ((bsdar->as = malloc(bsdar->as_cap)) == NULL)
887 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
891 * The space required for holding one member name in as table includes:
892 * strlen(name) + (1 for '/') + (1 for '\n') + (possibly 1 for padding).
894 while (bsdar->as_sz + strlen(name) + 3 > bsdar->as_cap) {
896 bsdar->as = realloc(bsdar->as, bsdar->as_cap);
897 if (bsdar->as == NULL)
898 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
900 strncpy(&bsdar->as[bsdar->as_sz], name, strlen(name));
901 bsdar->as_sz += strlen(name);
902 bsdar->as[bsdar->as_sz++] = '/';
903 bsdar->as[bsdar->as_sz++] = '\n';
907 * Append to the archive symbol table buffer.
910 add_to_ar_sym_table(struct bsdar *bsdar, const char *name)
913 if (bsdar->s_so == NULL) {
914 if ((bsdar->s_so = malloc(_INIT_SYMOFF_CAP)) ==
916 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
917 bsdar->s_so_cap = _INIT_SYMOFF_CAP;
921 if (bsdar->s_sn == NULL) {
922 if ((bsdar->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL)
923 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
924 bsdar->s_sn_cap = _INIT_SYMNAME_CAP;
928 if (bsdar->s_cnt * sizeof(uint64_t) >= bsdar->s_so_cap) {
929 bsdar->s_so_cap *= 2;
930 bsdar->s_so = realloc(bsdar->s_so, bsdar->s_so_cap);
931 if (bsdar->s_so == NULL)
932 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
934 bsdar->s_so[bsdar->s_cnt] = bsdar->rela_off;
935 if ((uint64_t)bsdar->rela_off > bsdar->s_so_max)
936 bsdar->s_so_max = (uint64_t)bsdar->rela_off;
940 * The space required for holding one symbol name in sn table includes:
941 * strlen(name) + (1 for '\n') + (possibly 1 for padding).
943 while (bsdar->s_sn_sz + strlen(name) + 2 > bsdar->s_sn_cap) {
944 bsdar->s_sn_cap *= 2;
945 bsdar->s_sn = realloc(bsdar->s_sn, bsdar->s_sn_cap);
946 if (bsdar->s_sn == NULL)
947 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
949 strncpy(&bsdar->s_sn[bsdar->s_sn_sz], name, strlen(name));
950 bsdar->s_sn_sz += strlen(name);
951 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';