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/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/endian.h>
32 #include <sys/queue.h>
35 #include <archive_entry.h>
47 #define _ARMAG_LEN 8 /* length of ar magic string */
48 #define _ARHDR_LEN 60 /* length of ar header */
49 #define _INIT_AS_CAP 128 /* initial archive string table size */
50 #define _INIT_SYMOFF_CAP (256*(sizeof(uint32_t))) /* initial so table size */
51 #define _INIT_SYMNAME_CAP 1024 /* initial sn table size */
52 #define _MAXNAMELEN_SVR4 15 /* max member name length in svr4 variant */
53 #define _TRUNCATE_LEN 15 /* number of bytes to keep for member name */
55 static void add_to_ar_str_table(struct bsdar *bsdar, const char *name);
56 static void add_to_ar_sym_table(struct bsdar *bsdar, const char *name);
57 static struct ar_obj *create_obj_from_file(struct bsdar *bsdar,
58 const char *name, time_t mtime);
59 static void create_symtab_entry(struct bsdar *bsdar, void *maddr,
61 static void free_obj(struct bsdar *bsdar, struct ar_obj *obj);
62 static void insert_obj(struct bsdar *bsdar, struct ar_obj *obj,
64 static void read_objs(struct bsdar *bsdar, const char *archive,
66 static void write_archive(struct bsdar *bsdar, char mode);
67 static void write_cleanup(struct bsdar *bsdar);
68 static void write_data(struct bsdar *bsdar, struct archive *a,
69 const void *buf, size_t s);
70 static void write_objs(struct bsdar *bsdar);
73 ar_mode_d(struct bsdar *bsdar)
76 write_archive(bsdar, 'd');
80 ar_mode_m(struct bsdar *bsdar)
83 write_archive(bsdar, 'm');
87 ar_mode_q(struct bsdar *bsdar)
90 write_archive(bsdar, 'q');
94 ar_mode_r(struct bsdar *bsdar)
97 write_archive(bsdar, 'r');
101 ar_mode_s(struct bsdar *bsdar)
104 write_archive(bsdar, 's');
108 ar_mode_A(struct bsdar *bsdar)
111 write_archive(bsdar, 'A');
115 * Create object from file, return created obj upon success, or NULL
116 * when an error occurs or the member is not newer than existing
117 * one while -u is specified.
119 static struct ar_obj *
120 create_obj_from_file(struct bsdar *bsdar, const char *name, time_t mtime)
129 obj = malloc(sizeof(struct ar_obj));
131 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
132 if ((obj->fd = open(name, O_RDONLY, 0)) < 0) {
133 bsdar_warnc(bsdar, errno, "can't open file: %s", name);
138 if ((bname = basename(name)) == NULL)
139 bsdar_errc(bsdar, EX_SOFTWARE, errno, "basename failed");
140 if (bsdar->options & AR_TR && strlen(bname) > _TRUNCATE_LEN) {
141 if ((obj->name = malloc(_TRUNCATE_LEN + 1)) == NULL)
142 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
143 (void)strncpy(obj->name, bname, _TRUNCATE_LEN);
144 obj->name[_TRUNCATE_LEN] = '\0';
146 if ((obj->name = strdup(bname)) == NULL)
147 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
149 if (fstat(obj->fd, &sb) < 0) {
150 bsdar_warnc(bsdar, errno, "can't fstat file: %s", obj->name);
153 if (!S_ISREG(sb.st_mode)) {
154 bsdar_warnc(bsdar, 0, "%s is not an ordinary file", obj->name);
159 * When option '-u' is specified and member is not newer than the
160 * existing one, the replace will not happen. While if mtime == 0,
161 * which indicates that this is to "replace a none exist member",
162 * the replace will proceed regardless of '-u'.
164 if (mtime != 0 && bsdar->options & AR_U && sb.st_mtime <= mtime)
168 * When option '-D' is specified, mtime and UID / GID from the file
169 * will be replaced with 0, and file mode with 644. This ensures that
170 * checksums will match for two archives containing the exact same
173 if (bsdar->options & AR_D) {
177 obj->md = S_IFREG | 0644;
179 obj->uid = sb.st_uid;
180 obj->gid = sb.st_gid;
181 obj->mtime = sb.st_mtime;
182 obj->md = sb.st_mode;
184 obj->size = sb.st_size;
185 obj->dev = sb.st_dev;
186 obj->ino = sb.st_ino;
188 if (obj->size == 0) {
193 if ((obj->maddr = mmap(NULL, obj->size, PROT_READ,
194 MAP_PRIVATE, obj->fd, (off_t)0)) == MAP_FAILED) {
195 bsdar_warnc(bsdar, errno, "can't mmap file: %s", obj->name);
198 if (close(obj->fd) < 0)
199 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s",
205 if (close(obj->fd) < 0)
206 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s",
214 * Free object itself and its associated allocations.
217 free_obj(struct bsdar *bsdar, struct ar_obj *obj)
222 if (obj->maddr != NULL && munmap(obj->maddr, obj->size))
223 bsdar_warnc(bsdar, errno,
224 "can't munmap file: %s", obj->name);
230 * Insert obj to the tail, or before/after the pos obj.
233 insert_obj(struct bsdar *bsdar, struct ar_obj *obj, struct ar_obj *pos)
236 bsdar_errc(bsdar, EX_SOFTWARE, 0, "try to insert a null obj");
238 if (pos == NULL || obj == pos)
240 * If the object to move happens to be the position obj,
241 * or if there is not a pos obj, move it to tail.
245 if (bsdar->options & AR_B) {
246 TAILQ_INSERT_BEFORE(pos, obj, objs);
249 if (bsdar->options & AR_A) {
250 TAILQ_INSERT_AFTER(&bsdar->v_obj, pos, obj, objs);
255 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs);
260 * Read objects from archive into v_obj list. Note that checkargv is
261 * set when read_objs is used to read objects from the target of
262 * ADDLIB command (ar script mode), in this case argv array possibly
263 * specifies the members ADDLIB want.
266 read_objs(struct bsdar *bsdar, const char *archive, int checkargv)
269 struct archive_entry *entry;
278 if ((a = archive_read_new()) == NULL)
279 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed");
280 archive_read_support_format_ar(a);
281 AC(archive_read_open_filename(a, archive, DEF_BLKSZ));
283 r = archive_read_next_header(a, &entry);
284 if (r == ARCHIVE_FATAL)
285 bsdar_errc(bsdar, EX_DATAERR, 0, "%s",
286 archive_error_string(a));
287 if (r == ARCHIVE_EOF)
289 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
290 bsdar_warnc(bsdar, 0, "%s", archive_error_string(a));
291 if (r == ARCHIVE_RETRY) {
292 bsdar_warnc(bsdar, 0, "Retrying...");
296 name = archive_entry_pathname(entry);
299 * skip pseudo members.
301 if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0)
305 * If checkargv is set, only read those members specified
308 if (checkargv && bsdar->argc > 0) {
310 for(i = 0; i < bsdar->argc; i++) {
311 av = &bsdar->argv[i];
314 if ((bname = basename(*av)) == NULL)
315 bsdar_errc(bsdar, EX_SOFTWARE, errno,
317 if (strcmp(bname, name) != 0)
328 size = archive_entry_size(entry);
331 if ((buff = malloc(size)) == NULL)
332 bsdar_errc(bsdar, EX_SOFTWARE, errno,
334 if (archive_read_data(a, buff, size) != (ssize_t)size) {
335 bsdar_warnc(bsdar, 0, "%s",
336 archive_error_string(a));
343 obj = malloc(sizeof(struct ar_obj));
345 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
347 if ((obj->name = strdup(name)) == NULL)
348 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
350 obj->uid = archive_entry_uid(entry);
351 obj->gid = archive_entry_gid(entry);
352 obj->md = archive_entry_mode(entry);
353 obj->mtime = archive_entry_mtime(entry);
358 * Objects from archive have obj->fd set to -1,
359 * for the ease of cleaning up.
362 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs);
364 AC(archive_read_close(a));
365 AC(archive_read_free(a));
369 * Determine the constitution of resulting archive.
372 write_archive(struct bsdar *bsdar, char mode)
374 struct ar_obj *nobj, *obj, *obj_temp, *pos;
380 TAILQ_INIT(&bsdar->v_obj);
383 memset(&sb, 0, sizeof(sb));
386 * Test if the specified archive exists, to figure out
387 * whether we are creating one here.
389 if (stat(bsdar->filename, &sb) != 0) {
390 if (errno != ENOENT) {
391 bsdar_warnc(bsdar, 0, "stat %s failed",
396 /* We do not create archive in mode 'd', 'm' and 's'. */
397 if (mode != 'r' && mode != 'q') {
398 bsdar_warnc(bsdar, 0, "%s: no such file",
403 /* Issue a warning if -c is not specified when creating. */
404 if (!(bsdar->options & AR_C))
405 bsdar_warnc(bsdar, 0, "creating %s", bsdar->filename);
410 * First read members from existing archive.
412 read_objs(bsdar, bsdar->filename, 0);
415 * For mode 's', no member will be moved, deleted or replaced.
421 * For mode 'q', we don't need to adjust existing members either.
422 * Also, -a, -b and -i are ignored in this mode. New members are
423 * always inserted at tail.
429 * Mode 'A' adds the contents of another archive to the tail of
430 * current archive. Note that mode 'A' is a special mode for the
431 * ADDLIB command of the ar script mode. Currently there is no
432 * access to this function from the ar command line mode.
436 * Read objects from the target archive of ADDLIB command.
437 * If there are members specified in argv, read those members
438 * only, otherwise the entire archive will be read.
440 read_objs(bsdar, bsdar->addlib, 1);
445 * Try to find the position member specified by user.
447 if (bsdar->options & AR_A || bsdar->options & AR_B) {
448 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
449 if (strcmp(obj->name, bsdar->posarg) == 0) {
456 * If can't find `pos' specified by user,
457 * silently insert objects at tail.
460 bsdar->options &= ~(AR_A | AR_B);
463 for (i = 0; i < bsdar->argc; i++) {
464 av = &bsdar->argv[i];
466 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
467 if ((bname = basename(*av)) == NULL)
468 bsdar_errc(bsdar, EX_SOFTWARE, errno,
470 if (bsdar->options & AR_TR) {
471 if (strncmp(bname, obj->name, _TRUNCATE_LEN))
474 if (strcmp(bname, obj->name) != 0)
479 * if the new member is not qualified
480 * to replace the old one, skip it.
482 nobj = create_obj_from_file(bsdar, *av,
488 if (bsdar->options & AR_V)
489 (void)fprintf(stdout, "%c - %s\n", mode,
492 TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
493 if (mode == 'd' || mode == 'r')
494 free_obj(bsdar, obj);
497 insert_obj(bsdar, obj, pos);
499 insert_obj(bsdar, nobj, pos);
510 * When operating in mode 'r', directly add those user specified
511 * objects which do not exist in current archive. When operating
512 * in mode 'q', all objects specified in command line args are
513 * appended to the archive, without comparing with existing ones.
515 for (i = 0; i < bsdar->argc; i++) {
516 av = &bsdar->argv[i];
517 if (*av != NULL && (mode == 'r' || mode == 'q')) {
518 nobj = create_obj_from_file(bsdar, *av, 0);
520 insert_obj(bsdar, nobj, pos);
521 if (bsdar->options & AR_V && nobj != NULL)
522 (void)fprintf(stdout, "a - %s\n", *av);
529 write_cleanup(bsdar);
533 * Memory cleaning up.
536 write_cleanup(struct bsdar *bsdar)
538 struct ar_obj *obj, *obj_temp;
540 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
541 TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
542 free_obj(bsdar, obj);
554 * Wrapper for archive_write_data().
557 write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s)
559 if (archive_write_data(a, buf, s) != (ssize_t)s)
560 bsdar_errc(bsdar, EX_SOFTWARE, 0, "%s",
561 archive_error_string(a));
565 * Write the resulting archive members.
568 write_objs(struct bsdar *bsdar)
572 struct archive_entry *entry;
573 size_t s_sz; /* size of archive symbol table. */
574 size_t pm_sz; /* size of pseudo members */
577 if (elf_version(EV_CURRENT) == EV_NONE)
578 bsdar_errc(bsdar, EX_SOFTWARE, 0,
579 "ELF library initialization failed: %s", elf_errmsg(-1));
583 /* Create archive symbol table and archive string table, if need. */
584 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
585 if (!(bsdar->options & AR_SS) && obj->maddr != NULL)
586 create_symtab_entry(bsdar, obj->maddr, obj->size);
587 if (strlen(obj->name) > _MAXNAMELEN_SVR4)
588 add_to_ar_str_table(bsdar, obj->name);
589 bsdar->rela_off += _ARHDR_LEN + obj->size + obj->size % 2;
593 * Pad the symbol name string table. It is treated specially because
594 * symbol name table should be padded by a '\0', not the common '\n'
595 * for other members. The size of sn table includes the pad bit.
597 if (bsdar->s_cnt != 0 && bsdar->s_sn_sz % 2 != 0)
598 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';
601 * Archive string table is padded by a "\n" as the normal members.
602 * The difference is that the size of archive string table counts
603 * in the pad bit, while normal members' size fileds do not.
605 if (bsdar->as != NULL && bsdar->as_sz % 2 != 0)
606 bsdar->as[bsdar->as_sz++] = '\n';
609 * If there is a symbol table, calculate the size of pseudo members,
610 * convert previously stored relative offsets to absolute ones, and
611 * then make them Big Endian.
613 * absolute_offset = htobe32(relative_offset + size_of_pseudo_members)
616 if (bsdar->s_cnt != 0) {
617 s_sz = (bsdar->s_cnt + 1) * sizeof(uint32_t) + bsdar->s_sn_sz;
618 pm_sz = _ARMAG_LEN + (_ARHDR_LEN + s_sz);
619 if (bsdar->as != NULL)
620 pm_sz += _ARHDR_LEN + bsdar->as_sz;
621 for (i = 0; (size_t)i < bsdar->s_cnt; i++)
622 *(bsdar->s_so + i) = htobe32(*(bsdar->s_so + i) +
626 if ((a = archive_write_new()) == NULL)
627 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_write_new failed");
629 archive_write_set_format_ar_svr4(a);
631 AC(archive_write_open_filename(a, bsdar->filename));
634 * write the archive symbol table, if there is one.
635 * If options -s is explicitly specified or we are invoked
636 * as ranlib, write the symbol table even if it is empty.
638 if ((bsdar->s_cnt != 0 && !(bsdar->options & AR_SS)) ||
639 bsdar->options & AR_S) {
640 entry = archive_entry_new();
641 archive_entry_copy_pathname(entry, "/");
642 if ((bsdar->options & AR_D) == 0)
643 archive_entry_set_mtime(entry, time(NULL), 0);
644 archive_entry_set_size(entry, (bsdar->s_cnt + 1) *
645 sizeof(uint32_t) + bsdar->s_sn_sz);
646 AC(archive_write_header(a, entry));
647 nr = htobe32(bsdar->s_cnt);
648 write_data(bsdar, a, &nr, sizeof(uint32_t));
649 write_data(bsdar, a, bsdar->s_so, sizeof(uint32_t) *
651 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz);
652 archive_entry_free(entry);
655 /* write the archive string table, if any. */
656 if (bsdar->as != NULL) {
657 entry = archive_entry_new();
658 archive_entry_copy_pathname(entry, "//");
659 archive_entry_set_size(entry, bsdar->as_sz);
660 AC(archive_write_header(a, entry));
661 write_data(bsdar, a, bsdar->as, bsdar->as_sz);
662 archive_entry_free(entry);
665 /* write normal members. */
666 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
667 entry = archive_entry_new();
668 archive_entry_copy_pathname(entry, obj->name);
669 archive_entry_set_uid(entry, obj->uid);
670 archive_entry_set_gid(entry, obj->gid);
671 archive_entry_set_mode(entry, obj->md);
672 archive_entry_set_size(entry, obj->size);
673 archive_entry_set_mtime(entry, obj->mtime, 0);
674 archive_entry_set_dev(entry, obj->dev);
675 archive_entry_set_ino(entry, obj->ino);
676 archive_entry_set_filetype(entry, AE_IFREG);
677 AC(archive_write_header(a, entry));
678 write_data(bsdar, a, obj->maddr, obj->size);
679 archive_entry_free(entry);
682 AC(archive_write_close(a));
683 AC(archive_write_free(a));
687 * Extract global symbols from ELF binary members.
690 create_symtab_entry(struct bsdar *bsdar, void *maddr, size_t size)
699 int elferr, tabndx, len, i;
701 if ((e = elf_memory(maddr, size)) == NULL) {
702 bsdar_warnc(bsdar, 0, "elf_memory() failed: %s",
706 if (elf_kind(e) != ELF_K_ELF) {
707 /* Silently ignore non-elf member. */
711 if (elf_getshstrndx(e, &shstrndx) == 0) {
712 bsdar_warnc(bsdar, EX_SOFTWARE, 0, "elf_getshstrndx failed: %s",
720 while ((scn = elf_nextscn(e, scn)) != NULL) {
721 if (gelf_getshdr(scn, &shdr) != &shdr) {
722 bsdar_warnc(bsdar, 0,
723 "elf_getshdr failed: %s", elf_errmsg(-1));
726 if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL) {
727 bsdar_warnc(bsdar, 0,
728 "elf_strptr failed: %s", elf_errmsg(-1));
731 if (strcmp(name, ".strtab") == 0) {
732 tabndx = elf_ndxscn(scn);
736 elferr = elf_errno();
738 bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s",
741 bsdar_warnc(bsdar, 0, "can't find .strtab section");
747 while ((scn = elf_nextscn(e, scn)) != NULL) {
748 if (gelf_getshdr(scn, &shdr) != &shdr) {
749 bsdar_warnc(bsdar, EX_SOFTWARE, 0,
750 "elf_getshdr failed: %s", elf_errmsg(-1));
753 if (shdr.sh_type != SHT_SYMTAB)
758 while (n < shdr.sh_size &&
759 (data = elf_getdata(scn, data)) != NULL) {
760 len = data->d_size / shdr.sh_entsize;
761 for (i = 0; i < len; i++) {
762 if (gelf_getsym(data, i, &sym) != &sym) {
763 bsdar_warnc(bsdar, EX_SOFTWARE, 0,
764 "gelf_getsym failed: %s",
769 /* keep only global or weak symbols */
770 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL &&
771 GELF_ST_BIND(sym.st_info) != STB_WEAK)
774 /* keep only defined symbols */
775 if (sym.st_shndx == SHN_UNDEF)
778 if ((name = elf_strptr(e, tabndx,
779 sym.st_name)) == NULL) {
780 bsdar_warnc(bsdar, EX_SOFTWARE, 0,
781 "elf_strptr failed: %s",
786 add_to_ar_sym_table(bsdar, name);
790 elferr = elf_errno();
792 bsdar_warnc(bsdar, EX_SOFTWARE, 0, "elf_nextscn failed: %s",
799 * Append to the archive string table buffer.
802 add_to_ar_str_table(struct bsdar *bsdar, const char *name)
805 if (bsdar->as == NULL) {
806 bsdar->as_cap = _INIT_AS_CAP;
808 if ((bsdar->as = malloc(bsdar->as_cap)) == NULL)
809 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
813 * The space required for holding one member name in as table includes:
814 * strlen(name) + (1 for '/') + (1 for '\n') + (possibly 1 for padding).
816 while (bsdar->as_sz + strlen(name) + 3 > bsdar->as_cap) {
818 bsdar->as = realloc(bsdar->as, bsdar->as_cap);
819 if (bsdar->as == NULL)
820 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
822 strncpy(&bsdar->as[bsdar->as_sz], name, strlen(name));
823 bsdar->as_sz += strlen(name);
824 bsdar->as[bsdar->as_sz++] = '/';
825 bsdar->as[bsdar->as_sz++] = '\n';
829 * Append to the archive symbol table buffer.
832 add_to_ar_sym_table(struct bsdar *bsdar, const char *name)
835 if (bsdar->s_so == NULL) {
836 if ((bsdar->s_so = malloc(_INIT_SYMOFF_CAP)) ==
838 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
839 bsdar->s_so_cap = _INIT_SYMOFF_CAP;
843 if (bsdar->s_sn == NULL) {
844 if ((bsdar->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL)
845 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
846 bsdar->s_sn_cap = _INIT_SYMNAME_CAP;
850 if (bsdar->s_cnt * sizeof(uint32_t) >= bsdar->s_so_cap) {
851 bsdar->s_so_cap *= 2;
852 bsdar->s_so = realloc(bsdar->s_so, bsdar->s_so_cap);
853 if (bsdar->s_so == NULL)
854 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
856 bsdar->s_so[bsdar->s_cnt] = bsdar->rela_off;
860 * The space required for holding one symbol name in sn table includes:
861 * strlen(name) + (1 for '\n') + (possibly 1 for padding).
863 while (bsdar->s_sn_sz + strlen(name) + 2 > bsdar->s_sn_cap) {
864 bsdar->s_sn_cap *= 2;
865 bsdar->s_sn = realloc(bsdar->s_sn, bsdar->s_sn_cap);
866 if (bsdar->s_sn == NULL)
867 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
869 strncpy(&bsdar->s_sn[bsdar->s_sn_sz], name, strlen(name));
870 bsdar->s_sn_sz += strlen(name);
871 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';