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 3629 2018-09-30 19:26:28Z jkoshy $");
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));
356 * Returns EXIT_SUCCESS if the write succeeded or EXIT_FAILURE otherwise.
359 ar_write_archive(struct bsdar *bsdar, int mode)
361 struct ar_obj *nobj, *obj, *obj_temp, *pos;
367 TAILQ_INIT(&bsdar->v_obj);
368 exitcode = EXIT_SUCCESS;
371 memset(&sb, 0, sizeof(sb));
373 assert(mode == 'A' || mode == 'd' || mode == 'm' || mode == 'q' ||
374 mode == 'r' || mode == 's');
377 * Test if the specified archive exists, to determine
378 * whether we are creating a new archive.
380 if (stat(bsdar->filename, &sb) != 0) {
381 if (errno != ENOENT) {
382 bsdar_warnc(bsdar, errno, "stat %s failed",
384 return (EXIT_FAILURE);
387 /* We do not create archive in mode 'd', 'm' and 's'. */
388 if (mode != 'r' && mode != 'q') {
389 bsdar_warnc(bsdar, 0, "%s: no such file",
391 return (EXIT_FAILURE);
394 /* Issue a message if the '-c' option was not specified. */
395 if (!(bsdar->options & AR_C))
396 bsdar_warnc(bsdar, 0, "creating %s", bsdar->filename);
400 bsdar->ar_dev = sb.st_dev;
401 bsdar->ar_ino = sb.st_ino;
404 * First read members from the existing archive.
406 read_objs(bsdar, bsdar->filename, 0);
409 * For mode 's', no member will be moved, deleted or replaced.
415 * For mode 'q', we don't need to adjust existing members either.
416 * Also, -a, -b and -i are ignored in this mode. New members are
417 * always inserted at tail.
423 * Mode 'A' adds the contents of another archive to the tail
424 * of current archive. Note that mode 'A' is a special mode
425 * for the 'ADDLIB' command in ar's script mode. Currently
426 * there is no option that invokes this function from ar's
431 * Read objects from the target archive of the
432 * 'ADDLIB' command. If there are members specified in
433 * 'argv', read those members only, otherwise the
434 * entire archive will be read.
436 read_objs(bsdar, bsdar->addlib, 1);
441 * Try to find the position member specified by user.
443 if (bsdar->options & AR_A || bsdar->options & AR_B) {
444 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
445 if (strcmp(obj->name, bsdar->posarg) == 0) {
452 * If we cannot find the position specified by the
453 * user, silently insert objects at the tail of the
457 bsdar->options &= ~(AR_A | AR_B);
460 for (i = 0; i < bsdar->argc; i++) {
461 av = &bsdar->argv[i];
463 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
464 if ((bname = basename(*av)) == NULL)
465 bsdar_errc(bsdar, errno, "basename failed");
466 if (bsdar->options & AR_TR) {
467 if (strncmp(bname, obj->name, _TRUNCATE_LEN))
470 if (strcmp(bname, obj->name) != 0)
475 * If the new member should not
476 * replace the old one, skip it.
478 nobj = create_obj_from_file(bsdar, *av,
481 exitcode = EXIT_FAILURE;
486 if (bsdar->options & AR_V)
487 (void)fprintf(bsdar->output, "%c - %s\n",
490 TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
491 if (mode == 'd' || mode == 'r')
495 insert_obj(bsdar, obj, pos);
497 insert_obj(bsdar, nobj, pos);
508 * When operating in mode 'r', directly add the specified
509 * objects which do not exist in current archive. When
510 * operating in mode 'q', all objects specified by the command
511 * line args are appended to the archive, without checking
512 * existing members in the archive.
514 for (i = 0; i < bsdar->argc; i++) {
515 av = &bsdar->argv[i];
516 if (*av != NULL && (mode == 'r' || mode == 'q')) {
517 nobj = create_obj_from_file(bsdar, *av, 0);
519 exitcode = EXIT_FAILURE;
523 insert_obj(bsdar, nobj, pos);
524 if (bsdar->options & AR_V)
525 (void)fprintf(bsdar->output, "a - %s\n", *av);
532 write_cleanup(bsdar);
541 write_cleanup(struct bsdar *bsdar)
543 struct ar_obj *obj, *obj_temp;
545 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
546 TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
559 * Wrapper for archive_write_data().
562 write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s)
564 if (archive_write_data(a, buf, s) != (ssize_t)s)
565 bsdar_errc(bsdar, 0, "%s", archive_error_string(a));
569 * Compute the size of the symbol table for an archive.
572 bsdar_symtab_size(struct bsdar *bsdar)
576 if (bsdar->options & AR_BSD) {
578 * A BSD style symbol table has two parts.
579 * Each part is preceded by its size in bytes,
580 * encoded as a C 'long'. In the first part,
581 * there are 's_cnt' entries, each entry being
582 * 2 'long's in size. The second part
583 * contains a string table.
585 sz = 2 * sizeof(long) + (bsdar->s_cnt * 2 * sizeof(long)) +
589 * An SVR4 style symbol table comprises of a 32 bit
590 * number holding the number of entries, followed by
591 * that many 32-bit offsets, followed by a string
594 sz = sizeof(uint32_t) + bsdar->s_cnt * sizeof(uint32_t) +
602 write_svr4_symtab_entry(struct bsdar *bsdar, struct archive *a)
607 /* Translate offsets to big-endian form. */
608 for (i = 0; i < bsdar->s_cnt; i++)
609 bsdar->s_so[i] = htobe32(bsdar->s_so[i]);
611 nr = htobe32(bsdar->s_cnt);
612 write_data(bsdar, a, &nr, sizeof(uint32_t));
613 write_data(bsdar, a, bsdar->s_so, sizeof(uint32_t) *
615 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz);
619 write_bsd_symtab_entry(struct bsdar *bsdar, struct archive *a)
621 long br_sz, br_off, br_strx;
626 * Write out the size in the byte of the array of 'ranlib'
627 * descriptors to follow.
630 br_sz = (long) (bsdar->s_cnt * 2 * sizeof(long));
631 write_data(bsdar, a, &br_sz, sizeof(long));
634 * Write out the array of 'ranlib' descriptors. Each
635 * descriptor comprises of (a) an offset into the following
636 * string table and (b) a file offset to the relevant member.
638 for (i = 0, s = bsdar->s_sn; i < bsdar->s_cnt; i++) {
639 br_strx = (long) (s - bsdar->s_sn);
640 br_off = (long) bsdar->s_so[i];
641 write_data(bsdar, a, &br_strx, sizeof(long));
642 write_data(bsdar, a, &br_off, sizeof(long));
644 /* Find the start of the next symbol in the string table. */
650 * Write out the size of the string table as a 'long',
651 * followed by the string table itself.
653 br_sz = (long) bsdar->s_sn_sz;
654 write_data(bsdar, a, &br_sz, sizeof(long));
655 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz);
660 * Write the resulting archive members.
663 write_objs(struct bsdar *bsdar)
667 struct archive_entry *entry;
668 size_t s_sz; /* size of archive symbol table. */
669 size_t pm_sz; /* size of pseudo members */
670 size_t namelen; /* size of member name. */
671 size_t obj_sz; /* size of object + extended header. */
674 const char *entry_name;
679 * Create the archive symbol table and the archive string
682 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
683 if (!(bsdar->options & AR_SS) && obj->elf != NULL)
684 create_symtab_entry(bsdar, obj->elf);
687 namelen = strlen(obj->name);
688 if (bsdar->options & AR_BSD) {
689 /* Account for the space used by the file name. */
690 if (namelen > _MAXNAMELEN_BSD ||
691 strchr(obj->name, ' '))
693 } else if (namelen > _MAXNAMELEN_SVR4)
694 add_to_ar_str_table(bsdar, obj->name);
696 obj_sz += obj->size; /* add the actual object size */
698 /* Roundup the final size and add the header length. */
699 bsdar->rela_off += _ARHDR_LEN + obj_sz + (obj_sz & 1);
703 * Pad the symbol name string table. It is treated specially
704 * because symbol name table should be padded by a '\0', and
705 * not '\n' as for normal members. The size of the 'sn' table
706 * includes the pad byte.
708 if (bsdar->s_cnt != 0 && bsdar->s_sn_sz % 2 != 0)
709 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';
712 * The archive string table is padded by a "\n" like a normal
713 * member. The difference is that the size of archive string
714 * table includes the pad byte, while normal members' size
717 if (bsdar->as != NULL && bsdar->as_sz % 2 != 0)
718 bsdar->as[bsdar->as_sz++] = '\n';
721 * If there is a symbol table, calculate the size of pseudo
722 * members, and convert previously stored relative offsets to
725 * absolute_offset = relative_offset + size_of_pseudo_members)
728 s_sz = bsdar_symtab_size(bsdar);
729 if (bsdar->s_cnt != 0) {
730 pm_sz = _ARMAG_LEN + (_ARHDR_LEN + s_sz);
731 if (bsdar->as != NULL) /* SVR4 archives only */
732 pm_sz += _ARHDR_LEN + bsdar->as_sz;
733 for (i = 0; (size_t) i < bsdar->s_cnt; i++)
734 bsdar->s_so[i] = bsdar->s_so[i] + pm_sz;
737 if ((a = archive_write_new()) == NULL)
738 bsdar_errc(bsdar, 0, "archive_write_new failed");
740 if (bsdar->options & AR_BSD)
741 archive_write_set_format_ar_bsd(a);
743 archive_write_set_format_ar_svr4(a);
745 AC(archive_write_open_filename(a, bsdar->filename));
748 * Write the archive symbol table, if there is one. If
749 * options '-s' was explicitly specified or if we were invoked
750 * as 'ranlib', write the symbol table even if it is empty.
752 if ((bsdar->s_cnt != 0 && !(bsdar->options & AR_SS)) ||
753 bsdar->options & AR_S) {
754 if (bsdar->options & AR_BSD)
755 entry_name = AR_SYMTAB_NAME_BSD;
757 entry_name = AR_SYMTAB_NAME_SVR4;
759 entry = archive_entry_new();
760 archive_entry_copy_pathname(entry, entry_name);
761 if ((bsdar->options & AR_D) == 0)
762 archive_entry_set_mtime(entry, time(NULL), 0);
763 archive_entry_set_size(entry, s_sz);
764 AC(archive_write_header(a, entry));
765 if (bsdar->options & AR_BSD)
766 write_bsd_symtab_entry(bsdar, a);
768 write_svr4_symtab_entry(bsdar, a);
769 archive_entry_free(entry);
772 /* Write the archive string table, if any. */
773 if (bsdar->as != NULL) {
774 entry = archive_entry_new();
775 archive_entry_copy_pathname(entry, AR_STRINGTAB_NAME_SVR4);
776 archive_entry_set_size(entry, bsdar->as_sz);
777 AC(archive_write_header(a, entry));
778 write_data(bsdar, a, bsdar->as, bsdar->as_sz);
779 archive_entry_free(entry);
782 /* Write normal members. */
783 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
784 if ((buf = elf_rawfile(obj->elf, NULL)) == NULL) {
785 bsdar_warnc(bsdar, 0, "elf_rawfile() failed: %s",
790 entry = archive_entry_new();
791 archive_entry_copy_pathname(entry, obj->name);
792 archive_entry_set_uid(entry, obj->uid);
793 archive_entry_set_gid(entry, obj->gid);
794 archive_entry_set_mode(entry, obj->md);
795 archive_entry_set_size(entry, obj->size);
796 archive_entry_set_mtime(entry, obj->mtime, 0);
797 archive_entry_set_dev(entry, obj->dev);
798 archive_entry_set_ino(entry, obj->ino);
799 archive_entry_set_filetype(entry, AE_IFREG);
800 AC(archive_write_header(a, entry));
801 write_data(bsdar, a, buf, obj->size);
802 archive_entry_free(entry);
805 AC(archive_write_close(a));
806 ACV(archive_write_free(a));
810 * Extract global symbols from ELF binary members.
813 create_symtab_entry(struct bsdar *bsdar, Elf *e)
821 int elferr, tabndx, len, i;
823 if (elf_kind(e) != ELF_K_ELF) {
824 /* Silently a ignore non-ELF member. */
827 if (elf_getshstrndx(e, &shstrndx) == 0) {
828 bsdar_warnc(bsdar, 0, "elf_getshstrndx failed: %s",
835 while ((scn = elf_nextscn(e, scn)) != NULL) {
836 if (gelf_getshdr(scn, &shdr) != &shdr) {
837 bsdar_warnc(bsdar, 0,
838 "elf_getshdr failed: %s", elf_errmsg(-1));
841 if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL) {
842 bsdar_warnc(bsdar, 0,
843 "elf_strptr failed: %s", elf_errmsg(-1));
846 if (strcmp(name, ".strtab") == 0) {
847 tabndx = elf_ndxscn(scn);
851 elferr = elf_errno();
853 bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s",
856 bsdar_warnc(bsdar, 0, "can't find .strtab section");
861 while ((scn = elf_nextscn(e, scn)) != NULL) {
862 if (gelf_getshdr(scn, &shdr) != &shdr) {
863 bsdar_warnc(bsdar, 0, "elf_getshdr failed: %s",
867 if (shdr.sh_type != SHT_SYMTAB)
872 while (n < shdr.sh_size &&
873 (data = elf_getdata(scn, data)) != NULL) {
874 len = data->d_size / shdr.sh_entsize;
875 for (i = 0; i < len; i++) {
876 if (gelf_getsym(data, i, &sym) != &sym) {
877 bsdar_warnc(bsdar, 0,
878 "gelf_getsym failed: %s",
883 /* Keep only global and weak symbols. */
884 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL &&
885 GELF_ST_BIND(sym.st_info) != STB_WEAK)
888 /* Keep only defined symbols. */
889 if (sym.st_shndx == SHN_UNDEF)
892 if ((name = elf_strptr(e, tabndx,
893 sym.st_name)) == NULL) {
894 bsdar_warnc(bsdar, 0,
895 "elf_strptr failed: %s",
900 add_to_ar_sym_table(bsdar, name);
904 elferr = elf_errno();
906 bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s",
911 * Append to the archive string table buffer.
914 add_to_ar_str_table(struct bsdar *bsdar, const char *name)
917 if (bsdar->as == NULL) {
918 bsdar->as_cap = _INIT_AS_CAP;
920 if ((bsdar->as = malloc(bsdar->as_cap)) == NULL)
921 bsdar_errc(bsdar, errno, "malloc failed");
925 * The space required for holding one member name in the 'as'
926 * table includes: strlen(name) + (1 for '/') + (1 for '\n') +
927 * (possibly 1 for padding).
929 while (bsdar->as_sz + strlen(name) + 3 > bsdar->as_cap) {
931 bsdar->as = realloc(bsdar->as, bsdar->as_cap);
932 if (bsdar->as == NULL)
933 bsdar_errc(bsdar, errno, "realloc failed");
935 strncpy(&bsdar->as[bsdar->as_sz], name, strlen(name));
936 bsdar->as_sz += strlen(name);
937 bsdar->as[bsdar->as_sz++] = '/';
938 bsdar->as[bsdar->as_sz++] = '\n';
942 * Append to the archive symbol table buffer.
945 add_to_ar_sym_table(struct bsdar *bsdar, const char *name)
948 if (bsdar->s_so == NULL) {
949 if ((bsdar->s_so = malloc(_INIT_SYMOFF_CAP)) ==
951 bsdar_errc(bsdar, errno, "malloc failed");
952 bsdar->s_so_cap = _INIT_SYMOFF_CAP;
956 if (bsdar->s_sn == NULL) {
957 if ((bsdar->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL)
958 bsdar_errc(bsdar, errno, "malloc failed");
959 bsdar->s_sn_cap = _INIT_SYMNAME_CAP;
963 if (bsdar->s_cnt * sizeof(uint32_t) >= bsdar->s_so_cap) {
964 bsdar->s_so_cap *= 2;
965 bsdar->s_so = realloc(bsdar->s_so, bsdar->s_so_cap);
966 if (bsdar->s_so == NULL)
967 bsdar_errc(bsdar, errno, "realloc failed");
969 bsdar->s_so[bsdar->s_cnt] = bsdar->rela_off;
973 * The space required for holding one symbol name in the 'sn'
974 * table includes: strlen(name) + (1 for '\n') + (possibly 1
977 while (bsdar->s_sn_sz + strlen(name) + 2 > bsdar->s_sn_cap) {
978 bsdar->s_sn_cap *= 2;
979 bsdar->s_sn = realloc(bsdar->s_sn, bsdar->s_sn_cap);
980 if (bsdar->s_sn == NULL)
981 bsdar_errc(bsdar, errno, "realloc failed");
983 strncpy(&bsdar->s_sn[bsdar->s_sn_sz], name, strlen(name));
984 bsdar->s_sn_sz += strlen(name);
985 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';