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 insert_obj(struct bsdar *bsdar, struct ar_obj *obj,
63 static void write_archive(struct bsdar *bsdar, char mode);
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 ar_mode_d(struct bsdar *bsdar)
73 write_archive(bsdar, 'd');
77 ar_mode_m(struct bsdar *bsdar)
80 write_archive(bsdar, 'm');
84 ar_mode_r(struct bsdar *bsdar)
87 write_archive(bsdar, 'r');
91 ar_mode_s(struct bsdar *bsdar)
94 write_archive(bsdar, 's');
98 * Create object from file, return created obj upon success, or NULL
99 * when an error occurs or the member is not newer than existing
100 * one while -u is specifed.
102 static struct ar_obj *
103 create_obj_from_file(struct bsdar *bsdar, const char *name, time_t mtime)
112 obj = malloc(sizeof(struct ar_obj));
114 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
115 if ((obj->fd = open(name, O_RDONLY, 0)) < 0) {
116 bsdar_warnc(bsdar, errno, "can't open file: %s", name);
121 if ((bname = basename(name)) == NULL)
122 bsdar_errc(bsdar, EX_SOFTWARE, errno, "basename failed");
123 if (bsdar->options & AR_TR && strlen(bname) > _TRUNCATE_LEN) {
124 if ((obj->name = malloc(_TRUNCATE_LEN + 1)) == NULL)
125 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
126 (void)strncpy(obj->name, bname, _TRUNCATE_LEN);
127 obj->name[_TRUNCATE_LEN] = '\0';
129 if ((obj->name = strdup(bname)) == NULL)
130 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
132 if (fstat(obj->fd, &sb) < 0) {
133 bsdar_warnc(bsdar, errno, "can't fstat file: %s", obj->name);
136 if (!S_ISREG(sb.st_mode)) {
137 bsdar_warnc(bsdar, 0, "%s is not an ordinary file", obj->name);
142 * When option '-u' is specified and member is not newer than the
143 * existing one, the replace will not happen. While if mtime == 0,
144 * which indicates that this is to "replace a none exist member",
145 * the replace will proceed regardless of '-u'.
147 if (mtime != 0 && bsdar->options & AR_U && sb.st_mtime <= mtime)
150 obj->uid = sb.st_uid;
151 obj->gid = sb.st_gid;
152 obj->md = sb.st_mode;
153 obj->size = sb.st_size;
154 obj->mtime = sb.st_mtime;
155 obj->dev = sb.st_dev;
156 obj->ino = sb.st_ino;
157 if ((obj->maddr = mmap(NULL, obj->size, PROT_READ,
158 MAP_PRIVATE, obj->fd, (off_t)0)) == MAP_FAILED) {
159 bsdar_warnc(bsdar, errno, "can't mmap file: %s", obj->name);
162 if (close(obj->fd) < 0)
163 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s",
169 if (close(obj->fd) < 0)
170 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s",
178 * Insert obj to the tail, or before/after the pos obj.
181 insert_obj(struct bsdar *bsdar, struct ar_obj *obj, struct ar_obj *pos)
184 bsdar_errc(bsdar, EX_SOFTWARE, 0, "try to insert a null obj");
186 if (pos == NULL || obj == pos)
188 * If the object to move happens to be the posistion obj,
189 * or if there is not a pos obj, move it to tail.
193 if (bsdar->options & AR_B) {
194 TAILQ_INSERT_BEFORE(pos, obj, objs);
197 if (bsdar->options & AR_A) {
198 TAILQ_INSERT_AFTER(&bsdar->v_obj, pos, obj, objs);
203 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs);
208 * Determine the constitution of resulting archive.
211 write_archive(struct bsdar *bsdar, char mode)
214 struct archive_entry *entry;
215 struct ar_obj *nobj, *obj, *obj_temp, *pos;
224 TAILQ_INIT(&bsdar->v_obj);
227 memset(&sb, 0, sizeof(sb));
229 /* By default, no compression is assumed. */
230 bsdar->compression = ARCHIVE_COMPRESSION_NONE;
233 * Test if the specified archive exists, to figure out
234 * whether we are creating one here.
236 if (stat(bsdar->filename, &sb) != 0) {
237 if (errno != ENOENT) {
238 bsdar_warnc(bsdar, 0, "stat %s failed",
243 /* We do not create archive in mode 'd', 'm' and 's'. */
245 bsdar_warnc(bsdar, 0, "%s: no such file",
250 /* Issue a warning if -c is not specified when creating. */
251 if (!(bsdar->options & AR_C))
252 bsdar_warnc(bsdar, 0, "creating %s", bsdar->filename);
257 * First read members from existing archive.
259 if ((a = archive_read_new()) == NULL)
260 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed");
261 archive_read_support_compression_all(a);
262 archive_read_support_format_ar(a);
263 AC(archive_read_open_filename(a, bsdar->filename, DEF_BLKSZ));
265 r = archive_read_next_header(a, &entry);
266 if (r == ARCHIVE_FATAL)
267 bsdar_errc(bsdar, EX_DATAERR, 0, "%s",
268 archive_error_string(a));
269 if (r == ARCHIVE_EOF)
271 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
272 bsdar_warnc(bsdar, 0, "%s", archive_error_string(a));
273 if (r == ARCHIVE_RETRY) {
274 bsdar_warnc(bsdar, 0, "Retrying...");
279 * Remember the compression mode of existing archive.
280 * If neither -j nor -z is specified, this mode will
281 * be used for resulting archive.
283 bsdar->compression = archive_compression(a);
285 name = archive_entry_pathname(entry);
288 * skip pseudo members.
290 if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0)
293 size = archive_entry_size(entry);
295 if ((buff = malloc(size)) == NULL)
296 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
297 if (archive_read_data(a, buff, size) != (ssize_t)size) {
298 bsdar_warnc(bsdar, 0, "%s", archive_error_string(a));
302 obj = malloc(sizeof(struct ar_obj));
304 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
306 if ((obj->name = strdup(name)) == NULL)
307 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
309 obj->uid = archive_entry_uid(entry);
310 obj->gid = archive_entry_gid(entry);
311 obj->md = archive_entry_mode(entry);
312 obj->mtime = archive_entry_mtime(entry);
317 * Objects from archive have obj->fd set to -1,
318 * for the ease of cleaning up.
321 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs);
323 AC(archive_read_close(a));
324 AC(archive_read_finish(a));
327 * For mode 's', no member will be moved, deleted or replaced.
333 * Try to find the position member specified by user.
335 if (bsdar->options & AR_A || bsdar->options & AR_B) {
336 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
337 if (strcmp(obj->name, bsdar->posarg) == 0) {
344 * If can't find `pos' specified by user,
345 * sliently insert objects at tail.
348 bsdar->options &= ~(AR_A | AR_B);
351 for (i = 0; i < bsdar->argc; i++) {
352 av = &bsdar->argv[i];
354 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
355 if ((bname = basename(*av)) == NULL)
356 bsdar_errc(bsdar, EX_SOFTWARE, errno,
358 if (bsdar->options & AR_TR) {
359 if (strncmp(bname, obj->name, _TRUNCATE_LEN))
362 if (strcmp(bname, obj->name) != 0)
367 * if the new member is not qualified
368 * to replace the old one, skip it.
370 nobj = create_obj_from_file(bsdar, *av,
376 if (bsdar->options & AR_V)
377 (void)fprintf(stdout, "%c - %s\n", mode,
380 TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
381 if (mode == 'd' || mode == 'r') {
388 insert_obj(bsdar, obj, pos);
390 insert_obj(bsdar, nobj, pos);
401 * When operating in mode 'r', directly add those user specified
402 * objects which do not exist in current archive.
404 for (i = 0; i < bsdar->argc; i++) {
405 av = &bsdar->argv[i];
406 if (*av != NULL && mode == 'r') {
407 nobj = create_obj_from_file(bsdar, *av, 0);
409 insert_obj(bsdar, nobj, pos);
410 if (bsdar->options & AR_V && nobj != NULL)
411 (void)fprintf(stdout, "a - %s\n", *av);
418 write_cleanup(bsdar);
422 * Memory cleaning up.
425 write_cleanup(struct bsdar *bsdar)
427 struct ar_obj *obj, *obj_temp;
429 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
434 if (munmap(obj->maddr, obj->size))
435 bsdar_warnc(bsdar, errno,
436 "can't munmap file: %s", obj->name);
437 TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
450 * Wrapper for archive_write_data().
453 write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s)
455 if (archive_write_data(a, buf, s) != (ssize_t)s)
456 bsdar_errc(bsdar, EX_SOFTWARE, 0, "%s",
457 archive_error_string(a));
461 * Write the resulting archive members.
464 write_objs(struct bsdar *bsdar)
468 struct archive_entry *entry;
469 size_t s_sz; /* size of archive symbol table. */
470 size_t pm_sz; /* size of pseudo members */
473 if (elf_version(EV_CURRENT) == EV_NONE)
474 bsdar_errc(bsdar, EX_SOFTWARE, 0,
475 "ELF library initialization failed: %s", elf_errmsg(-1));
479 /* Create archive symbol table and archive string table, if need. */
480 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
481 if (!(bsdar->options & AR_SS))
482 create_symtab_entry(bsdar, obj->maddr, obj->size);
483 if (strlen(obj->name) > _MAXNAMELEN_SVR4)
484 add_to_ar_str_table(bsdar, obj->name);
485 bsdar->rela_off += _ARHDR_LEN + obj->size + obj->size % 2;
489 * Pad the symbol name string table. It is treated specially because
490 * symbol name table should be padded by a '\0', not the common '\n'
491 * for other members. The size of sn table includes the pad bit.
493 if (bsdar->s_cnt != 0 && bsdar->s_sn_sz % 2 != 0)
494 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';
497 * Archive string table is padded by a "\n" as the normal members.
498 * The differece is that the size of archive string table counts
499 * in the pad bit, while normal members' size fileds do not.
501 if (bsdar->as != NULL && bsdar->as_sz % 2 != 0)
502 bsdar->as[bsdar->as_sz++] = '\n';
505 * If there is a symbol table, calculate the size of pseudo members,
506 * covert previously stored relative offsets to absolute ones, and
507 * then make them Big Endian.
509 * absolute_offset = htobe32(relative_offset + size_of_pseudo_members)
512 if (bsdar->s_cnt != 0) {
513 s_sz = (bsdar->s_cnt + 1) * sizeof(uint32_t) + bsdar->s_sn_sz;
514 pm_sz = _ARMAG_LEN + (_ARHDR_LEN + s_sz);
515 if (bsdar->as != NULL)
516 pm_sz += _ARHDR_LEN + bsdar->as_sz;
517 for (i = 0; (size_t)i < bsdar->s_cnt; i++)
518 *(bsdar->s_so + i) = htobe32(*(bsdar->s_so + i) +
522 if ((a = archive_write_new()) == NULL)
523 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_write_new failed");
525 archive_write_set_format_ar_svr4(a);
527 /* The compression mode of the existing archive is used
528 * for the result archive or if creating a new archive, we
529 * do not compress archive by default. This default behavior can
530 * be overrided by compression mode specified explicitly
531 * through command line option `-j' or `-z'.
533 if (bsdar->options & AR_J)
534 bsdar->compression = ARCHIVE_COMPRESSION_BZIP2;
535 if (bsdar->options & AR_Z)
536 bsdar->compression = ARCHIVE_COMPRESSION_GZIP;
537 if (bsdar->compression == ARCHIVE_COMPRESSION_BZIP2)
538 archive_write_set_compression_bzip2(a);
539 else if (bsdar->compression == ARCHIVE_COMPRESSION_GZIP)
540 archive_write_set_compression_gzip(a);
542 archive_write_set_compression_none(a);
544 AC(archive_write_open_filename(a, bsdar->filename));
547 * write the archive symbol table, if there is one.
548 * If options -s is explicitly specified or we are invoked
549 * as ranlib, write the symbol table even if it is empty.
551 if ((bsdar->s_cnt != 0 && !(bsdar->options & AR_SS)) ||
552 bsdar->options & AR_S) {
553 entry = archive_entry_new();
554 archive_entry_copy_pathname(entry, "/");
555 archive_entry_set_mtime(entry, time(NULL), 0);
556 archive_entry_set_size(entry, (bsdar->s_cnt + 1) *
557 sizeof(uint32_t) + bsdar->s_sn_sz);
558 AC(archive_write_header(a, entry));
559 nr = htobe32(bsdar->s_cnt);
560 write_data(bsdar, a, &nr, sizeof(uint32_t));
561 write_data(bsdar, a, bsdar->s_so, sizeof(uint32_t) *
563 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz);
564 archive_entry_free(entry);
567 /* write the archive string table, if any. */
568 if (bsdar->as != NULL) {
569 entry = archive_entry_new();
570 archive_entry_copy_pathname(entry, "//");
571 archive_entry_set_size(entry, bsdar->as_sz);
572 AC(archive_write_header(a, entry));
573 write_data(bsdar, a, bsdar->as, bsdar->as_sz);
574 archive_entry_free(entry);
577 /* write normal members. */
578 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
579 entry = archive_entry_new();
580 archive_entry_copy_pathname(entry, obj->name);
581 archive_entry_set_uid(entry, obj->uid);
582 archive_entry_set_gid(entry, obj->gid);
583 archive_entry_set_mode(entry, obj->md);
584 archive_entry_set_size(entry, obj->size);
585 archive_entry_set_mtime(entry, obj->mtime, 0);
586 archive_entry_set_dev(entry, obj->dev);
587 archive_entry_set_ino(entry, obj->ino);
588 archive_entry_set_filetype(entry, AE_IFREG);
589 AC(archive_write_header(a, entry));
590 write_data(bsdar, a, obj->maddr, obj->size);
591 archive_entry_free(entry);
594 AC(archive_write_close(a));
595 AC(archive_write_finish(a));
599 * Extract global symbols from ELF binary members.
602 create_symtab_entry(struct bsdar *bsdar, void *maddr, size_t size)
611 int elferr, tabndx, len, i;
613 if ((e = elf_memory(maddr, size)) == NULL) {
614 bsdar_warnc(bsdar, 0, "elf_memory() failed: %s",
618 if (elf_kind(e) != ELF_K_ELF) {
619 /* Sliently ignore non-elf member. */
623 if (elf_getshstrndx(e, &shstrndx) == 0) {
624 bsdar_warnc(bsdar, EX_SOFTWARE, 0, "elf_getshstrndx failed: %s",
632 while ((scn = elf_nextscn(e, scn)) != NULL) {
633 if (gelf_getshdr(scn, &shdr) != &shdr) {
634 bsdar_warnc(bsdar, 0,
635 "elf_getshdr failed: %s", elf_errmsg(-1));
638 if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL) {
639 bsdar_warnc(bsdar, 0,
640 "elf_strptr failed: %s", elf_errmsg(-1));
643 if (strcmp(name, ".strtab") == 0) {
644 tabndx = elf_ndxscn(scn);
648 elferr = elf_errno();
650 bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s",
653 bsdar_warnc(bsdar, 0, "can't find .strtab section");
659 while ((scn = elf_nextscn(e, scn)) != NULL) {
660 if (gelf_getshdr(scn, &shdr) != &shdr) {
661 bsdar_warnc(bsdar, EX_SOFTWARE, 0,
662 "elf_getshdr failed: %s", elf_errmsg(-1));
665 if (shdr.sh_type != SHT_SYMTAB)
670 while (n < shdr.sh_size &&
671 (data = elf_getdata(scn, data)) != NULL) {
672 len = data->d_size / shdr.sh_entsize;
673 for (i = 0; i < len; i++) {
674 if (gelf_getsym(data, i, &sym) != &sym) {
675 bsdar_warnc(bsdar, EX_SOFTWARE, 0,
676 "gelf_getsym failed: %s",
681 /* keep only global or weak symbols */
682 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL &&
683 GELF_ST_BIND(sym.st_info) != STB_WEAK)
686 /* keep only defined symbols */
687 if (sym.st_shndx == SHN_UNDEF)
690 if ((name = elf_strptr(e, tabndx,
691 sym.st_name)) == NULL) {
692 bsdar_warnc(bsdar, EX_SOFTWARE, 0,
693 "elf_strptr failed: %s",
698 add_to_ar_sym_table(bsdar, name);
702 elferr = elf_errno();
704 bsdar_warnc(bsdar, EX_SOFTWARE, 0, "elf_nextscn failed: %s",
711 * Append to the archive string table buffer.
714 add_to_ar_str_table(struct bsdar *bsdar, const char *name)
717 if (bsdar->as == NULL) {
718 bsdar->as_cap = _INIT_AS_CAP;
720 if ((bsdar->as = malloc(bsdar->as_cap)) == NULL)
721 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
725 * The space required for holding one member name in as table includes:
726 * strlen(name) + (1 for '/') + (1 for '\n') + (possibly 1 for padding).
728 if (bsdar->as_sz + strlen(name) + 3 > bsdar->as_cap) {
730 bsdar->as = realloc(bsdar->as, bsdar->as_cap);
731 if (bsdar->as == NULL)
732 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
734 strncpy(&bsdar->as[bsdar->as_sz], name, strlen(name));
735 bsdar->as_sz += strlen(name);
736 bsdar->as[bsdar->as_sz++] = '/';
737 bsdar->as[bsdar->as_sz++] = '\n';
741 * Append to the archive symbol table buffer.
744 add_to_ar_sym_table(struct bsdar *bsdar, const char *name)
747 if (bsdar->s_so == NULL) {
748 if ((bsdar->s_so = malloc(_INIT_SYMOFF_CAP)) ==
750 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
751 bsdar->s_so_cap = _INIT_SYMOFF_CAP;
755 if (bsdar->s_sn == NULL) {
756 if ((bsdar->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL)
757 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
758 bsdar->s_sn_cap = _INIT_SYMNAME_CAP;
762 if (bsdar->s_cnt * sizeof(uint32_t) >= bsdar->s_so_cap) {
763 bsdar->s_so_cap *= 2;
764 bsdar->s_so = realloc(bsdar->s_so, bsdar->s_so_cap);
765 if (bsdar->s_so == NULL)
766 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
768 bsdar->s_so[bsdar->s_cnt] = bsdar->rela_off;
772 * The space required for holding one symbol name in sn table includes:
773 * strlen(name) + (1 for '\n') + (possibly 1 for padding).
775 if (bsdar->s_sn_sz + strlen(name) + 2 > bsdar->s_sn_cap) {
776 bsdar->s_sn_cap *= 2;
777 bsdar->s_sn = realloc(bsdar->s_sn, bsdar->s_sn_cap);
778 if (bsdar->s_sn == NULL)
779 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
781 strncpy(&bsdar->s_sn[bsdar->s_sn_sz], name, strlen(name));
782 bsdar->s_sn_sz += strlen(name);
783 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';