1d192f3d3SKai Wang /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
4d192f3d3SKai Wang * Copyright (c) 2007 Kai Wang
5d192f3d3SKai Wang * All rights reserved.
6d192f3d3SKai Wang *
7d192f3d3SKai Wang * Redistribution and use in source and binary forms, with or without
8d192f3d3SKai Wang * modification, are permitted provided that the following conditions
9d192f3d3SKai Wang * are met:
10d192f3d3SKai Wang * 1. Redistributions of source code must retain the above copyright
1125f82d56SEd Maste * notice, this list of conditions and the following disclaimer.
12d192f3d3SKai Wang * 2. Redistributions in binary form must reproduce the above copyright
13d192f3d3SKai Wang * notice, this list of conditions and the following disclaimer in the
14d192f3d3SKai Wang * documentation and/or other materials provided with the distribution.
15d192f3d3SKai Wang *
1625f82d56SEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1725f82d56SEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1825f82d56SEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1925f82d56SEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2025f82d56SEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2125f82d56SEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2225f82d56SEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2325f82d56SEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2425f82d56SEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2525f82d56SEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2625f82d56SEd Maste * SUCH DAMAGE.
27d192f3d3SKai Wang */
28d192f3d3SKai Wang
29d192f3d3SKai Wang #include <sys/cdefs.h>
30d192f3d3SKai Wang #include <sys/endian.h>
31d192f3d3SKai Wang #include <sys/mman.h>
32d192f3d3SKai Wang #include <sys/queue.h>
33d192f3d3SKai Wang #include <sys/stat.h>
34d192f3d3SKai Wang #include <archive.h>
35d192f3d3SKai Wang #include <archive_entry.h>
3657aaefdcSEd Maste #include <assert.h>
37d192f3d3SKai Wang #include <errno.h>
38d192f3d3SKai Wang #include <fcntl.h>
39d192f3d3SKai Wang #include <gelf.h>
40d192f3d3SKai Wang #include <libgen.h>
41d192f3d3SKai Wang #include <stdio.h>
42d192f3d3SKai Wang #include <stdlib.h>
43d192f3d3SKai Wang #include <string.h>
4401715d8cSEd Maste #include <unistd.h>
45d192f3d3SKai Wang
46d192f3d3SKai Wang #include "ar.h"
47d192f3d3SKai Wang
48d192f3d3SKai Wang #define _ARMAG_LEN 8 /* length of ar magic string */
49d192f3d3SKai Wang #define _ARHDR_LEN 60 /* length of ar header */
50d192f3d3SKai Wang #define _INIT_AS_CAP 128 /* initial archive string table size */
51fabed6b2SEd Maste #define _INIT_SYMOFF_CAP (256*(sizeof(uint64_t))) /* initial so table size */
52d192f3d3SKai Wang #define _INIT_SYMNAME_CAP 1024 /* initial sn table size */
53d192f3d3SKai Wang #define _MAXNAMELEN_SVR4 15 /* max member name length in svr4 variant */
54d192f3d3SKai Wang #define _TRUNCATE_LEN 15 /* number of bytes to keep for member name */
55d192f3d3SKai Wang
56d192f3d3SKai Wang static void add_to_ar_str_table(struct bsdar *bsdar, const char *name);
57d192f3d3SKai Wang static void add_to_ar_sym_table(struct bsdar *bsdar, const char *name);
58d192f3d3SKai Wang static struct ar_obj *create_obj_from_file(struct bsdar *bsdar,
59d192f3d3SKai Wang const char *name, time_t mtime);
60d192f3d3SKai Wang static void create_symtab_entry(struct bsdar *bsdar, void *maddr,
61d192f3d3SKai Wang size_t size);
62cf933f5bSEd Maste static void free_obj(struct bsdar *bsdar, struct ar_obj *obj);
63d192f3d3SKai Wang static void insert_obj(struct bsdar *bsdar, struct ar_obj *obj,
64d192f3d3SKai Wang struct ar_obj *pos);
6501715d8cSEd Maste static void prefault_buffer(const char *buf, size_t s);
660c099281SKai Wang static void read_objs(struct bsdar *bsdar, const char *archive,
670c099281SKai Wang int checkargv);
68d192f3d3SKai Wang static void write_cleanup(struct bsdar *bsdar);
69d192f3d3SKai Wang static void write_data(struct bsdar *bsdar, struct archive *a,
70d192f3d3SKai Wang const void *buf, size_t s);
71d192f3d3SKai Wang static void write_objs(struct bsdar *bsdar);
72d192f3d3SKai Wang
73d192f3d3SKai Wang /*
74d192f3d3SKai Wang * Create object from file, return created obj upon success, or NULL
75d192f3d3SKai Wang * when an error occurs or the member is not newer than existing
766bef5d28SBenedict Reuschling * one while -u is specified.
77d192f3d3SKai Wang */
78d192f3d3SKai Wang static struct ar_obj *
create_obj_from_file(struct bsdar * bsdar,const char * name,time_t mtime)79d192f3d3SKai Wang create_obj_from_file(struct bsdar *bsdar, const char *name, time_t mtime)
80d192f3d3SKai Wang {
81d192f3d3SKai Wang struct ar_obj *obj;
82d192f3d3SKai Wang struct stat sb;
83d192f3d3SKai Wang const char *bname;
8409a11349SEd Schouten char *tmpname;
85d192f3d3SKai Wang
86d192f3d3SKai Wang if (name == NULL)
87d192f3d3SKai Wang return (NULL);
88d192f3d3SKai Wang
89d192f3d3SKai Wang obj = malloc(sizeof(struct ar_obj));
90d192f3d3SKai Wang if (obj == NULL)
91086f0908SEd Maste bsdar_errc(bsdar, errno, "malloc failed");
92d192f3d3SKai Wang if ((obj->fd = open(name, O_RDONLY, 0)) < 0) {
93d192f3d3SKai Wang bsdar_warnc(bsdar, errno, "can't open file: %s", name);
94d192f3d3SKai Wang free(obj);
95d192f3d3SKai Wang return (NULL);
96d192f3d3SKai Wang }
97d192f3d3SKai Wang
9809a11349SEd Schouten tmpname = strdup(name);
9909a11349SEd Schouten if (tmpname == NULL)
100086f0908SEd Maste bsdar_errc(bsdar, errno, "strdup failed");
10109a11349SEd Schouten if ((bname = basename(tmpname)) == NULL)
102086f0908SEd Maste bsdar_errc(bsdar, errno, "basename failed");
103d192f3d3SKai Wang if (bsdar->options & AR_TR && strlen(bname) > _TRUNCATE_LEN) {
104d192f3d3SKai Wang if ((obj->name = malloc(_TRUNCATE_LEN + 1)) == NULL)
105086f0908SEd Maste bsdar_errc(bsdar, errno, "malloc failed");
106d192f3d3SKai Wang (void)strncpy(obj->name, bname, _TRUNCATE_LEN);
107d192f3d3SKai Wang obj->name[_TRUNCATE_LEN] = '\0';
108d192f3d3SKai Wang } else
109d192f3d3SKai Wang if ((obj->name = strdup(bname)) == NULL)
110086f0908SEd Maste bsdar_errc(bsdar, errno, "strdup failed");
11109a11349SEd Schouten free(tmpname);
112d192f3d3SKai Wang
113d192f3d3SKai Wang if (fstat(obj->fd, &sb) < 0) {
114d192f3d3SKai Wang bsdar_warnc(bsdar, errno, "can't fstat file: %s", obj->name);
115d192f3d3SKai Wang goto giveup;
116d192f3d3SKai Wang }
117d192f3d3SKai Wang if (!S_ISREG(sb.st_mode)) {
118d192f3d3SKai Wang bsdar_warnc(bsdar, 0, "%s is not an ordinary file", obj->name);
119d192f3d3SKai Wang goto giveup;
120d192f3d3SKai Wang }
121d192f3d3SKai Wang
122d192f3d3SKai Wang /*
123d192f3d3SKai Wang * When option '-u' is specified and member is not newer than the
124*b1d0cf28SLO WEN-CHIEN * existing one, the replacement will not happen. While if mtime == 0,
125d192f3d3SKai Wang * which indicates that this is to "replace a none exist member",
126d192f3d3SKai Wang * the replace will proceed regardless of '-u'.
127d192f3d3SKai Wang */
128d192f3d3SKai Wang if (mtime != 0 && bsdar->options & AR_U && sb.st_mtime <= mtime)
129d192f3d3SKai Wang goto giveup;
130d192f3d3SKai Wang
1310e479ac8STim Kientzle /*
1320e479ac8STim Kientzle * When option '-D' is specified, mtime and UID / GID from the file
1330e479ac8STim Kientzle * will be replaced with 0, and file mode with 644. This ensures that
1340e479ac8STim Kientzle * checksums will match for two archives containing the exact same
1350e479ac8STim Kientzle * files.
1360e479ac8STim Kientzle */
1370e479ac8STim Kientzle if (bsdar->options & AR_D) {
1380e479ac8STim Kientzle obj->uid = 0;
1390e479ac8STim Kientzle obj->gid = 0;
1400e479ac8STim Kientzle obj->mtime = 0;
1410e479ac8STim Kientzle obj->md = S_IFREG | 0644;
1420e479ac8STim Kientzle } else {
143d192f3d3SKai Wang obj->uid = sb.st_uid;
144d192f3d3SKai Wang obj->gid = sb.st_gid;
145d192f3d3SKai Wang obj->mtime = sb.st_mtime;
1460e479ac8STim Kientzle obj->md = sb.st_mode;
1470e479ac8STim Kientzle }
1480e479ac8STim Kientzle obj->size = sb.st_size;
149d192f3d3SKai Wang obj->dev = sb.st_dev;
150d192f3d3SKai Wang obj->ino = sb.st_ino;
15147203048SKai Wang
15247203048SKai Wang if (obj->size == 0) {
15347203048SKai Wang obj->maddr = NULL;
15447203048SKai Wang return (obj);
15547203048SKai Wang }
15647203048SKai Wang
157d192f3d3SKai Wang if ((obj->maddr = mmap(NULL, obj->size, PROT_READ,
158d192f3d3SKai Wang MAP_PRIVATE, obj->fd, (off_t)0)) == MAP_FAILED) {
159d192f3d3SKai Wang bsdar_warnc(bsdar, errno, "can't mmap file: %s", obj->name);
160d192f3d3SKai Wang goto giveup;
161d192f3d3SKai Wang }
162d192f3d3SKai Wang if (close(obj->fd) < 0)
163086f0908SEd Maste bsdar_errc(bsdar, errno, "close failed: %s",
164d192f3d3SKai Wang obj->name);
165d192f3d3SKai Wang
166d192f3d3SKai Wang return (obj);
167d192f3d3SKai Wang
168d192f3d3SKai Wang giveup:
169d192f3d3SKai Wang if (close(obj->fd) < 0)
170086f0908SEd Maste bsdar_errc(bsdar, errno, "close failed: %s",
171d192f3d3SKai Wang obj->name);
172d192f3d3SKai Wang free(obj->name);
173d192f3d3SKai Wang free(obj);
174d192f3d3SKai Wang return (NULL);
175d192f3d3SKai Wang }
176d192f3d3SKai Wang
177d192f3d3SKai Wang /*
178cf933f5bSEd Maste * Free object itself and its associated allocations.
179cf933f5bSEd Maste */
180cf933f5bSEd Maste static void
free_obj(struct bsdar * bsdar,struct ar_obj * obj)181cf933f5bSEd Maste free_obj(struct bsdar *bsdar, struct ar_obj *obj)
182cf933f5bSEd Maste {
183cf933f5bSEd Maste if (obj->fd == -1)
184cf933f5bSEd Maste free(obj->maddr);
185cf933f5bSEd Maste else
186cf933f5bSEd Maste if (obj->maddr != NULL && munmap(obj->maddr, obj->size))
187cf933f5bSEd Maste bsdar_warnc(bsdar, errno,
188cf933f5bSEd Maste "can't munmap file: %s", obj->name);
189cf933f5bSEd Maste free(obj->name);
190cf933f5bSEd Maste free(obj);
191cf933f5bSEd Maste }
192cf933f5bSEd Maste
193cf933f5bSEd Maste /*
194d192f3d3SKai Wang * Insert obj to the tail, or before/after the pos obj.
195d192f3d3SKai Wang */
196d192f3d3SKai Wang static void
insert_obj(struct bsdar * bsdar,struct ar_obj * obj,struct ar_obj * pos)197d192f3d3SKai Wang insert_obj(struct bsdar *bsdar, struct ar_obj *obj, struct ar_obj *pos)
198d192f3d3SKai Wang {
199d192f3d3SKai Wang if (obj == NULL)
200086f0908SEd Maste bsdar_errc(bsdar, 0, "try to insert a null obj");
201d192f3d3SKai Wang
202d192f3d3SKai Wang if (pos == NULL || obj == pos)
203d192f3d3SKai Wang /*
2046bef5d28SBenedict Reuschling * If the object to move happens to be the position obj,
205d192f3d3SKai Wang * or if there is not a pos obj, move it to tail.
206d192f3d3SKai Wang */
207d192f3d3SKai Wang goto tail;
208d192f3d3SKai Wang
209d192f3d3SKai Wang if (bsdar->options & AR_B) {
210d192f3d3SKai Wang TAILQ_INSERT_BEFORE(pos, obj, objs);
211d192f3d3SKai Wang return;
212d192f3d3SKai Wang }
213d192f3d3SKai Wang if (bsdar->options & AR_A) {
214d192f3d3SKai Wang TAILQ_INSERT_AFTER(&bsdar->v_obj, pos, obj, objs);
215d192f3d3SKai Wang return;
216d192f3d3SKai Wang }
217d192f3d3SKai Wang
218d192f3d3SKai Wang tail:
219d192f3d3SKai Wang TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs);
220d192f3d3SKai Wang
221d192f3d3SKai Wang }
222d192f3d3SKai Wang
223d192f3d3SKai Wang /*
2240c099281SKai Wang * Read objects from archive into v_obj list. Note that checkargv is
2250c099281SKai Wang * set when read_objs is used to read objects from the target of
2260c099281SKai Wang * ADDLIB command (ar script mode), in this case argv array possibly
2270c099281SKai Wang * specifies the members ADDLIB want.
228d192f3d3SKai Wang */
229d192f3d3SKai Wang static void
read_objs(struct bsdar * bsdar,const char * archive,int checkargv)2300c099281SKai Wang read_objs(struct bsdar *bsdar, const char *archive, int checkargv)
231d192f3d3SKai Wang {
232d192f3d3SKai Wang struct archive *a;
233d192f3d3SKai Wang struct archive_entry *entry;
2340c099281SKai Wang struct ar_obj *obj;
235d192f3d3SKai Wang const char *name;
236d192f3d3SKai Wang const char *bname;
237d192f3d3SKai Wang char *buff;
238d192f3d3SKai Wang char **av;
239d192f3d3SKai Wang size_t size;
2400c099281SKai Wang int i, r, find;
241d192f3d3SKai Wang
242d192f3d3SKai Wang if ((a = archive_read_new()) == NULL)
243086f0908SEd Maste bsdar_errc(bsdar, 0, "archive_read_new failed");
244d192f3d3SKai Wang archive_read_support_format_ar(a);
2450c099281SKai Wang AC(archive_read_open_filename(a, archive, DEF_BLKSZ));
246d192f3d3SKai Wang for (;;) {
247d192f3d3SKai Wang r = archive_read_next_header(a, &entry);
248d192f3d3SKai Wang if (r == ARCHIVE_FATAL)
249086f0908SEd Maste bsdar_errc(bsdar, archive_errno(a), "%s",
250d192f3d3SKai Wang archive_error_string(a));
251d192f3d3SKai Wang if (r == ARCHIVE_EOF)
252d192f3d3SKai Wang break;
253d192f3d3SKai Wang if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
254ccd8660bSEd Maste bsdar_warnc(bsdar, archive_errno(a), "%s",
255ccd8660bSEd Maste archive_error_string(a));
256d192f3d3SKai Wang if (r == ARCHIVE_RETRY) {
257d192f3d3SKai Wang bsdar_warnc(bsdar, 0, "Retrying...");
258d192f3d3SKai Wang continue;
259d192f3d3SKai Wang }
260d192f3d3SKai Wang
261d192f3d3SKai Wang name = archive_entry_pathname(entry);
262d192f3d3SKai Wang
263d192f3d3SKai Wang /*
264d192f3d3SKai Wang * skip pseudo members.
265d192f3d3SKai Wang */
266d192f3d3SKai Wang if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0)
267d192f3d3SKai Wang continue;
268d192f3d3SKai Wang
2690c099281SKai Wang /*
2700c099281SKai Wang * If checkargv is set, only read those members specified
2710c099281SKai Wang * in argv.
2720c099281SKai Wang */
2730c099281SKai Wang if (checkargv && bsdar->argc > 0) {
2740c099281SKai Wang find = 0;
2750c099281SKai Wang for(i = 0; i < bsdar->argc; i++) {
2760c099281SKai Wang av = &bsdar->argv[i];
2770c099281SKai Wang if (*av == NULL)
2780c099281SKai Wang continue;
2790c099281SKai Wang if ((bname = basename(*av)) == NULL)
280086f0908SEd Maste bsdar_errc(bsdar, errno,
2810c099281SKai Wang "basename failed");
2820c099281SKai Wang if (strcmp(bname, name) != 0)
2830c099281SKai Wang continue;
2840c099281SKai Wang
2850c099281SKai Wang *av = NULL;
2860c099281SKai Wang find = 1;
2870c099281SKai Wang break;
2880c099281SKai Wang }
2890c099281SKai Wang if (!find)
2900c099281SKai Wang continue;
2910c099281SKai Wang }
2920c099281SKai Wang
293d192f3d3SKai Wang size = archive_entry_size(entry);
294d192f3d3SKai Wang
295517dbe2eSKai Wang if (size > 0) {
296d192f3d3SKai Wang if ((buff = malloc(size)) == NULL)
297086f0908SEd Maste bsdar_errc(bsdar, errno, "malloc failed");
298d192f3d3SKai Wang if (archive_read_data(a, buff, size) != (ssize_t)size) {
299ccd8660bSEd Maste bsdar_warnc(bsdar, archive_errno(a), "%s",
300517dbe2eSKai Wang archive_error_string(a));
301d192f3d3SKai Wang free(buff);
302d192f3d3SKai Wang continue;
303d192f3d3SKai Wang }
304517dbe2eSKai Wang } else
305517dbe2eSKai Wang buff = NULL;
306517dbe2eSKai Wang
307d192f3d3SKai Wang obj = malloc(sizeof(struct ar_obj));
308d192f3d3SKai Wang if (obj == NULL)
309086f0908SEd Maste bsdar_errc(bsdar, errno, "malloc failed");
310d192f3d3SKai Wang obj->maddr = buff;
311d192f3d3SKai Wang if ((obj->name = strdup(name)) == NULL)
312086f0908SEd Maste bsdar_errc(bsdar, errno, "strdup failed");
313d192f3d3SKai Wang obj->size = size;
314d192f3d3SKai Wang obj->uid = archive_entry_uid(entry);
315d192f3d3SKai Wang obj->gid = archive_entry_gid(entry);
316d192f3d3SKai Wang obj->md = archive_entry_mode(entry);
317d192f3d3SKai Wang obj->mtime = archive_entry_mtime(entry);
318d192f3d3SKai Wang obj->dev = 0;
319d192f3d3SKai Wang obj->ino = 0;
320d192f3d3SKai Wang
321d192f3d3SKai Wang /*
322d192f3d3SKai Wang * Objects from archive have obj->fd set to -1,
323d192f3d3SKai Wang * for the ease of cleaning up.
324d192f3d3SKai Wang */
325d192f3d3SKai Wang obj->fd = -1;
326d192f3d3SKai Wang TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs);
327d192f3d3SKai Wang }
328d192f3d3SKai Wang AC(archive_read_close(a));
329ebb8fc42SMartin Matuska AC(archive_read_free(a));
3300c099281SKai Wang }
3310c099281SKai Wang
3320c099281SKai Wang /*
3330c099281SKai Wang * Determine the constitution of resulting archive.
3340c099281SKai Wang */
33557aaefdcSEd Maste int
ar_write_archive(struct bsdar * bsdar,int mode)33657aaefdcSEd Maste ar_write_archive(struct bsdar *bsdar, int mode)
3370c099281SKai Wang {
3380c099281SKai Wang struct ar_obj *nobj, *obj, *obj_temp, *pos;
3390c099281SKai Wang struct stat sb;
3400c099281SKai Wang const char *bname;
3410c099281SKai Wang char **av;
34238911b3cSEd Maste int exitcode, i;
3430c099281SKai Wang
3440c099281SKai Wang TAILQ_INIT(&bsdar->v_obj);
34538911b3cSEd Maste exitcode = EXIT_SUCCESS;
3460c099281SKai Wang nobj = NULL;
3470c099281SKai Wang pos = NULL;
3480c099281SKai Wang memset(&sb, 0, sizeof(sb));
3490c099281SKai Wang
35057aaefdcSEd Maste assert(mode == 'A' || mode == 'd' || mode == 'm' || mode == 'q' ||
35157aaefdcSEd Maste mode == 'r' || mode == 's');
35257aaefdcSEd Maste
3530c099281SKai Wang /*
3540c099281SKai Wang * Test if the specified archive exists, to figure out
3550c099281SKai Wang * whether we are creating one here.
3560c099281SKai Wang */
3570c099281SKai Wang if (stat(bsdar->filename, &sb) != 0) {
3580c099281SKai Wang if (errno != ENOENT) {
3590c099281SKai Wang bsdar_warnc(bsdar, 0, "stat %s failed",
3600c099281SKai Wang bsdar->filename);
36138911b3cSEd Maste return (EXIT_FAILURE);
3620c099281SKai Wang }
3630c099281SKai Wang
3640c099281SKai Wang /* We do not create archive in mode 'd', 'm' and 's'. */
3650c099281SKai Wang if (mode != 'r' && mode != 'q') {
3660c099281SKai Wang bsdar_warnc(bsdar, 0, "%s: no such file",
3670c099281SKai Wang bsdar->filename);
36838911b3cSEd Maste return (EXIT_FAILURE);
3690c099281SKai Wang }
3700c099281SKai Wang
3710c099281SKai Wang /* Issue a warning if -c is not specified when creating. */
3720c099281SKai Wang if (!(bsdar->options & AR_C))
3730c099281SKai Wang bsdar_warnc(bsdar, 0, "creating %s", bsdar->filename);
3740c099281SKai Wang goto new_archive;
3750c099281SKai Wang }
3760c099281SKai Wang
3770c099281SKai Wang /*
3780c099281SKai Wang * First read members from existing archive.
3790c099281SKai Wang */
3800c099281SKai Wang read_objs(bsdar, bsdar->filename, 0);
381d192f3d3SKai Wang
382d192f3d3SKai Wang /*
383d192f3d3SKai Wang * For mode 's', no member will be moved, deleted or replaced.
384d192f3d3SKai Wang */
385d192f3d3SKai Wang if (mode == 's')
386d192f3d3SKai Wang goto write_objs;
387d192f3d3SKai Wang
388d192f3d3SKai Wang /*
389cb0dad38SKai Wang * For mode 'q', we don't need to adjust existing members either.
390cb0dad38SKai Wang * Also, -a, -b and -i are ignored in this mode. New members are
391cb0dad38SKai Wang * always inserted at tail.
392cb0dad38SKai Wang */
393cb0dad38SKai Wang if (mode == 'q')
394cb0dad38SKai Wang goto new_archive;
395cb0dad38SKai Wang
396cb0dad38SKai Wang /*
3970c099281SKai Wang * Mode 'A' adds the contents of another archive to the tail of
3980c099281SKai Wang * current archive. Note that mode 'A' is a special mode for the
3990c099281SKai Wang * ADDLIB command of the ar script mode. Currently there is no
4000c099281SKai Wang * access to this function from the ar command line mode.
4010c099281SKai Wang */
4020c099281SKai Wang if (mode == 'A') {
4030c099281SKai Wang /*
4040c099281SKai Wang * Read objects from the target archive of ADDLIB command.
4056bef5d28SBenedict Reuschling * If there are members specified in argv, read those members
4060c099281SKai Wang * only, otherwise the entire archive will be read.
4070c099281SKai Wang */
4080c099281SKai Wang read_objs(bsdar, bsdar->addlib, 1);
4090c099281SKai Wang goto write_objs;
4100c099281SKai Wang }
4110c099281SKai Wang
4120c099281SKai Wang /*
413d192f3d3SKai Wang * Try to find the position member specified by user.
414d192f3d3SKai Wang */
415d192f3d3SKai Wang if (bsdar->options & AR_A || bsdar->options & AR_B) {
416d192f3d3SKai Wang TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
417d192f3d3SKai Wang if (strcmp(obj->name, bsdar->posarg) == 0) {
418d192f3d3SKai Wang pos = obj;
419d192f3d3SKai Wang break;
420d192f3d3SKai Wang }
421d192f3d3SKai Wang }
422d192f3d3SKai Wang
423d192f3d3SKai Wang /*
424d192f3d3SKai Wang * If can't find `pos' specified by user,
4256bef5d28SBenedict Reuschling * silently insert objects at tail.
426d192f3d3SKai Wang */
427d192f3d3SKai Wang if (pos == NULL)
428d192f3d3SKai Wang bsdar->options &= ~(AR_A | AR_B);
429d192f3d3SKai Wang }
430d192f3d3SKai Wang
431d192f3d3SKai Wang for (i = 0; i < bsdar->argc; i++) {
432d192f3d3SKai Wang av = &bsdar->argv[i];
433d192f3d3SKai Wang
434d192f3d3SKai Wang TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
435d192f3d3SKai Wang if ((bname = basename(*av)) == NULL)
436086f0908SEd Maste bsdar_errc(bsdar, errno, "basename failed");
437d192f3d3SKai Wang if (bsdar->options & AR_TR) {
438d192f3d3SKai Wang if (strncmp(bname, obj->name, _TRUNCATE_LEN))
439d192f3d3SKai Wang continue;
440d192f3d3SKai Wang } else
441d192f3d3SKai Wang if (strcmp(bname, obj->name) != 0)
442d192f3d3SKai Wang continue;
443d192f3d3SKai Wang
444d192f3d3SKai Wang if (mode == 'r') {
445d192f3d3SKai Wang /*
446d192f3d3SKai Wang * if the new member is not qualified
447d192f3d3SKai Wang * to replace the old one, skip it.
448d192f3d3SKai Wang */
449d192f3d3SKai Wang nobj = create_obj_from_file(bsdar, *av,
450d192f3d3SKai Wang obj->mtime);
45138911b3cSEd Maste if (nobj == NULL) {
45238911b3cSEd Maste exitcode = EXIT_FAILURE;
453d192f3d3SKai Wang goto skip_obj;
454d192f3d3SKai Wang }
45538911b3cSEd Maste }
456d192f3d3SKai Wang
457d192f3d3SKai Wang if (bsdar->options & AR_V)
458d192f3d3SKai Wang (void)fprintf(stdout, "%c - %s\n", mode,
459d192f3d3SKai Wang *av);
460d192f3d3SKai Wang
461d192f3d3SKai Wang TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
462cf933f5bSEd Maste if (mode == 'd' || mode == 'r')
463cf933f5bSEd Maste free_obj(bsdar, obj);
464d192f3d3SKai Wang
465d192f3d3SKai Wang if (mode == 'm')
466d192f3d3SKai Wang insert_obj(bsdar, obj, pos);
467d192f3d3SKai Wang if (mode == 'r')
468d192f3d3SKai Wang insert_obj(bsdar, nobj, pos);
469d192f3d3SKai Wang
470d192f3d3SKai Wang skip_obj:
471d192f3d3SKai Wang *av = NULL;
472d192f3d3SKai Wang break;
473d192f3d3SKai Wang }
474d192f3d3SKai Wang
475d192f3d3SKai Wang }
476d192f3d3SKai Wang
477d192f3d3SKai Wang new_archive:
478d192f3d3SKai Wang /*
479d192f3d3SKai Wang * When operating in mode 'r', directly add those user specified
480cb0dad38SKai Wang * objects which do not exist in current archive. When operating
481cb0dad38SKai Wang * in mode 'q', all objects specified in command line args are
482cb0dad38SKai Wang * appended to the archive, without comparing with existing ones.
483d192f3d3SKai Wang */
484d192f3d3SKai Wang for (i = 0; i < bsdar->argc; i++) {
485d192f3d3SKai Wang av = &bsdar->argv[i];
486cb0dad38SKai Wang if (*av != NULL && (mode == 'r' || mode == 'q')) {
487d192f3d3SKai Wang nobj = create_obj_from_file(bsdar, *av, 0);
48838911b3cSEd Maste if (nobj == NULL) {
48938911b3cSEd Maste exitcode = EXIT_FAILURE;
49038911b3cSEd Maste *av = NULL;
49138911b3cSEd Maste continue;
49238911b3cSEd Maste }
493d192f3d3SKai Wang insert_obj(bsdar, nobj, pos);
494d192f3d3SKai Wang if (bsdar->options & AR_V && nobj != NULL)
495d192f3d3SKai Wang (void)fprintf(stdout, "a - %s\n", *av);
496d192f3d3SKai Wang *av = NULL;
497d192f3d3SKai Wang }
498d192f3d3SKai Wang }
499d192f3d3SKai Wang
500d192f3d3SKai Wang write_objs:
501d192f3d3SKai Wang write_objs(bsdar);
502d192f3d3SKai Wang write_cleanup(bsdar);
50338911b3cSEd Maste
50438911b3cSEd Maste return (exitcode);
505d192f3d3SKai Wang }
506d192f3d3SKai Wang
507d192f3d3SKai Wang /*
508d192f3d3SKai Wang * Memory cleaning up.
509d192f3d3SKai Wang */
510d192f3d3SKai Wang static void
write_cleanup(struct bsdar * bsdar)511d192f3d3SKai Wang write_cleanup(struct bsdar *bsdar)
512d192f3d3SKai Wang {
513d192f3d3SKai Wang struct ar_obj *obj, *obj_temp;
514d192f3d3SKai Wang
515d192f3d3SKai Wang TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
516d192f3d3SKai Wang TAILQ_REMOVE(&bsdar->v_obj, obj, objs);
517cf933f5bSEd Maste free_obj(bsdar, obj);
518d192f3d3SKai Wang }
519d192f3d3SKai Wang
520d192f3d3SKai Wang free(bsdar->as);
521d192f3d3SKai Wang free(bsdar->s_so);
522d192f3d3SKai Wang free(bsdar->s_sn);
523d192f3d3SKai Wang bsdar->as = NULL;
524d192f3d3SKai Wang bsdar->s_so = NULL;
525fabed6b2SEd Maste bsdar->s_so_max = 0;
526d192f3d3SKai Wang bsdar->s_sn = NULL;
527d192f3d3SKai Wang }
528d192f3d3SKai Wang
529d192f3d3SKai Wang /*
53001715d8cSEd Maste * Fault in the buffer prior to writing as a workaround for poor performance
53101715d8cSEd Maste * due to interaction with kernel fs deadlock avoidance code. See the comment
53201715d8cSEd Maste * above vn_io_fault_doio() in sys/kern/vfs_vnops.c for details of the issue.
53301715d8cSEd Maste */
53401715d8cSEd Maste static void
prefault_buffer(const char * buf,size_t s)53501715d8cSEd Maste prefault_buffer(const char *buf, size_t s)
53601715d8cSEd Maste {
53701715d8cSEd Maste volatile const char *p;
53801715d8cSEd Maste size_t page_size;
53901715d8cSEd Maste
54001715d8cSEd Maste if (s == 0)
54101715d8cSEd Maste return;
54201715d8cSEd Maste page_size = sysconf(_SC_PAGESIZE);
54301715d8cSEd Maste for (p = buf; p < buf + s; p += page_size)
54401715d8cSEd Maste *p;
54501715d8cSEd Maste /*
54601715d8cSEd Maste * Ensure we touch the last page as well, in case the buffer is not
54701715d8cSEd Maste * page-aligned.
54801715d8cSEd Maste */
54901715d8cSEd Maste *(volatile const char *)(buf + s - 1);
55001715d8cSEd Maste }
55101715d8cSEd Maste
55201715d8cSEd Maste /*
553d192f3d3SKai Wang * Wrapper for archive_write_data().
554d192f3d3SKai Wang */
555d192f3d3SKai Wang static void
write_data(struct bsdar * bsdar,struct archive * a,const void * buf,size_t s)556d192f3d3SKai Wang write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s)
557d192f3d3SKai Wang {
5583f71a0c9SEd Maste ssize_t written;
5593f71a0c9SEd Maste
56001715d8cSEd Maste prefault_buffer(buf, s);
5613f71a0c9SEd Maste while (s > 0) {
5623f71a0c9SEd Maste written = archive_write_data(a, buf, s);
5633f71a0c9SEd Maste if (written < 0)
564086f0908SEd Maste bsdar_errc(bsdar, archive_errno(a), "%s",
565d192f3d3SKai Wang archive_error_string(a));
5663f71a0c9SEd Maste buf = (const char *)buf + written;
5673f71a0c9SEd Maste s -= written;
5683f71a0c9SEd Maste }
569d192f3d3SKai Wang }
570d192f3d3SKai Wang
571d192f3d3SKai Wang /*
572d192f3d3SKai Wang * Write the resulting archive members.
573d192f3d3SKai Wang */
574d192f3d3SKai Wang static void
write_objs(struct bsdar * bsdar)575d192f3d3SKai Wang write_objs(struct bsdar *bsdar)
576d192f3d3SKai Wang {
577d192f3d3SKai Wang struct ar_obj *obj;
578d192f3d3SKai Wang struct archive *a;
579d192f3d3SKai Wang struct archive_entry *entry;
580d192f3d3SKai Wang size_t s_sz; /* size of archive symbol table. */
581d192f3d3SKai Wang size_t pm_sz; /* size of pseudo members */
582fabed6b2SEd Maste size_t w_sz; /* size of words in symbol table */
583597d9b40SEd Maste size_t i;
584fabed6b2SEd Maste uint64_t nr;
5851dffcf9fSEd Maste uint32_t nr32;
586d192f3d3SKai Wang
587d192f3d3SKai Wang if (elf_version(EV_CURRENT) == EV_NONE)
588086f0908SEd Maste bsdar_errc(bsdar, 0, "ELF library initialization failed: %s",
589086f0908SEd Maste elf_errmsg(-1));
590d192f3d3SKai Wang
591d192f3d3SKai Wang bsdar->rela_off = 0;
592d192f3d3SKai Wang
593d192f3d3SKai Wang /* Create archive symbol table and archive string table, if need. */
594d192f3d3SKai Wang TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
59547203048SKai Wang if (!(bsdar->options & AR_SS) && obj->maddr != NULL)
596d192f3d3SKai Wang create_symtab_entry(bsdar, obj->maddr, obj->size);
597d192f3d3SKai Wang if (strlen(obj->name) > _MAXNAMELEN_SVR4)
598d192f3d3SKai Wang add_to_ar_str_table(bsdar, obj->name);
599d192f3d3SKai Wang bsdar->rela_off += _ARHDR_LEN + obj->size + obj->size % 2;
600d192f3d3SKai Wang }
601d192f3d3SKai Wang
602d192f3d3SKai Wang /*
603d192f3d3SKai Wang * Pad the symbol name string table. It is treated specially because
604d192f3d3SKai Wang * symbol name table should be padded by a '\0', not the common '\n'
605d192f3d3SKai Wang * for other members. The size of sn table includes the pad bit.
606d192f3d3SKai Wang */
607d192f3d3SKai Wang if (bsdar->s_cnt != 0 && bsdar->s_sn_sz % 2 != 0)
608d192f3d3SKai Wang bsdar->s_sn[bsdar->s_sn_sz++] = '\0';
609d192f3d3SKai Wang
610d192f3d3SKai Wang /*
611d192f3d3SKai Wang * Archive string table is padded by a "\n" as the normal members.
612517dbe2eSKai Wang * The difference is that the size of archive string table counts
6135666643aSGordon Bergling * in the pad bit, while normal members' size fields do not.
614d192f3d3SKai Wang */
615d192f3d3SKai Wang if (bsdar->as != NULL && bsdar->as_sz % 2 != 0)
616d192f3d3SKai Wang bsdar->as[bsdar->as_sz++] = '\n';
617d192f3d3SKai Wang
618d192f3d3SKai Wang /*
619d192f3d3SKai Wang * If there is a symbol table, calculate the size of pseudo members,
620517dbe2eSKai Wang * convert previously stored relative offsets to absolute ones, and
621d192f3d3SKai Wang * then make them Big Endian.
622d192f3d3SKai Wang *
623d192f3d3SKai Wang * absolute_offset = htobe32(relative_offset + size_of_pseudo_members)
624d192f3d3SKai Wang */
625fabed6b2SEd Maste w_sz = sizeof(uint32_t);
626d192f3d3SKai Wang if (bsdar->s_cnt != 0) {
627d192f3d3SKai Wang s_sz = (bsdar->s_cnt + 1) * sizeof(uint32_t) + bsdar->s_sn_sz;
628d192f3d3SKai Wang pm_sz = _ARMAG_LEN + (_ARHDR_LEN + s_sz);
629d192f3d3SKai Wang if (bsdar->as != NULL)
630d192f3d3SKai Wang pm_sz += _ARHDR_LEN + bsdar->as_sz;
631fabed6b2SEd Maste /* Use the 64-bit word size format if necessary. */
632fabed6b2SEd Maste if (bsdar->s_so_max > UINT32_MAX - pm_sz) {
633fabed6b2SEd Maste w_sz = sizeof(uint64_t);
634fabed6b2SEd Maste pm_sz -= s_sz;
635fabed6b2SEd Maste s_sz = (bsdar->s_cnt + 1) * sizeof(uint64_t) +
636fabed6b2SEd Maste bsdar->s_sn_sz;
637fabed6b2SEd Maste pm_sz += s_sz;
6381dffcf9fSEd Maste /* Convert to big-endian. */
639597d9b40SEd Maste for (i = 0; i < bsdar->s_cnt; i++)
640687c0904SEd Maste bsdar->s_so[i] =
641687c0904SEd Maste htobe64(bsdar->s_so[i] + pm_sz);
6421dffcf9fSEd Maste } else {
6431dffcf9fSEd Maste /*
6441dffcf9fSEd Maste * Convert to big-endian and shuffle in-place to
6451dffcf9fSEd Maste * the front of the allocation. XXX UB
6461dffcf9fSEd Maste */
647597d9b40SEd Maste for (i = 0; i < bsdar->s_cnt; i++)
6481dffcf9fSEd Maste ((uint32_t *)(bsdar->s_so))[i] =
6491dffcf9fSEd Maste htobe32(bsdar->s_so[i] + pm_sz);
650d192f3d3SKai Wang }
651699f1801SEd Maste }
652d192f3d3SKai Wang
653d192f3d3SKai Wang if ((a = archive_write_new()) == NULL)
654086f0908SEd Maste bsdar_errc(bsdar, 0, "archive_write_new failed");
655d192f3d3SKai Wang
656d192f3d3SKai Wang archive_write_set_format_ar_svr4(a);
657d192f3d3SKai Wang
658d192f3d3SKai Wang AC(archive_write_open_filename(a, bsdar->filename));
659d192f3d3SKai Wang
660d192f3d3SKai Wang /*
661d192f3d3SKai Wang * write the archive symbol table, if there is one.
662d192f3d3SKai Wang * If options -s is explicitly specified or we are invoked
663d192f3d3SKai Wang * as ranlib, write the symbol table even if it is empty.
664d192f3d3SKai Wang */
665d192f3d3SKai Wang if ((bsdar->s_cnt != 0 && !(bsdar->options & AR_SS)) ||
666d192f3d3SKai Wang bsdar->options & AR_S) {
667d192f3d3SKai Wang entry = archive_entry_new();
668bd9302d8SBryan Drewery if (entry == NULL)
669086f0908SEd Maste bsdar_errc(bsdar, 0, "archive_entry_new failed");
670fabed6b2SEd Maste if (w_sz == sizeof(uint64_t))
671fabed6b2SEd Maste archive_entry_copy_pathname(entry, "/SYM64/");
672fabed6b2SEd Maste else
673d192f3d3SKai Wang archive_entry_copy_pathname(entry, "/");
6740e479ac8STim Kientzle if ((bsdar->options & AR_D) == 0)
675d192f3d3SKai Wang archive_entry_set_mtime(entry, time(NULL), 0);
676fabed6b2SEd Maste archive_entry_set_size(entry, (bsdar->s_cnt + 1) * w_sz +
677fabed6b2SEd Maste bsdar->s_sn_sz);
678d192f3d3SKai Wang AC(archive_write_header(a, entry));
6791dffcf9fSEd Maste if (w_sz == sizeof(uint64_t)) {
680fabed6b2SEd Maste nr = htobe64(bsdar->s_cnt);
6811dffcf9fSEd Maste write_data(bsdar, a, &nr, sizeof(nr));
6821dffcf9fSEd Maste } else {
6831dffcf9fSEd Maste nr32 = htobe32((uint32_t)bsdar->s_cnt);
6841dffcf9fSEd Maste write_data(bsdar, a, &nr32, sizeof(nr32));
6851dffcf9fSEd Maste }
6861dffcf9fSEd Maste write_data(bsdar, a, bsdar->s_so, w_sz * bsdar->s_cnt);
687d192f3d3SKai Wang write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz);
688d192f3d3SKai Wang archive_entry_free(entry);
689d192f3d3SKai Wang }
690d192f3d3SKai Wang
691d192f3d3SKai Wang /* write the archive string table, if any. */
692d192f3d3SKai Wang if (bsdar->as != NULL) {
693d192f3d3SKai Wang entry = archive_entry_new();
694bd9302d8SBryan Drewery if (entry == NULL)
695086f0908SEd Maste bsdar_errc(bsdar, 0, "archive_entry_new failed");
696d192f3d3SKai Wang archive_entry_copy_pathname(entry, "//");
697d192f3d3SKai Wang archive_entry_set_size(entry, bsdar->as_sz);
698d192f3d3SKai Wang AC(archive_write_header(a, entry));
699d192f3d3SKai Wang write_data(bsdar, a, bsdar->as, bsdar->as_sz);
700d192f3d3SKai Wang archive_entry_free(entry);
701d192f3d3SKai Wang }
702d192f3d3SKai Wang
703d192f3d3SKai Wang /* write normal members. */
704d192f3d3SKai Wang TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
705d192f3d3SKai Wang entry = archive_entry_new();
706bd9302d8SBryan Drewery if (entry == NULL)
707086f0908SEd Maste bsdar_errc(bsdar, 0, "archive_entry_new failed");
708d192f3d3SKai Wang archive_entry_copy_pathname(entry, obj->name);
709d192f3d3SKai Wang archive_entry_set_uid(entry, obj->uid);
710d192f3d3SKai Wang archive_entry_set_gid(entry, obj->gid);
711d192f3d3SKai Wang archive_entry_set_mode(entry, obj->md);
712d192f3d3SKai Wang archive_entry_set_size(entry, obj->size);
713d192f3d3SKai Wang archive_entry_set_mtime(entry, obj->mtime, 0);
714d192f3d3SKai Wang archive_entry_set_dev(entry, obj->dev);
715d192f3d3SKai Wang archive_entry_set_ino(entry, obj->ino);
716d192f3d3SKai Wang archive_entry_set_filetype(entry, AE_IFREG);
717d192f3d3SKai Wang AC(archive_write_header(a, entry));
718d192f3d3SKai Wang write_data(bsdar, a, obj->maddr, obj->size);
719d192f3d3SKai Wang archive_entry_free(entry);
720d192f3d3SKai Wang }
721d192f3d3SKai Wang
722d192f3d3SKai Wang AC(archive_write_close(a));
723ebb8fc42SMartin Matuska AC(archive_write_free(a));
724d192f3d3SKai Wang }
725d192f3d3SKai Wang
726d192f3d3SKai Wang /*
727d192f3d3SKai Wang * Extract global symbols from ELF binary members.
728d192f3d3SKai Wang */
729d192f3d3SKai Wang static void
create_symtab_entry(struct bsdar * bsdar,void * maddr,size_t size)730d192f3d3SKai Wang create_symtab_entry(struct bsdar *bsdar, void *maddr, size_t size)
731d192f3d3SKai Wang {
732d192f3d3SKai Wang Elf *e;
733d192f3d3SKai Wang Elf_Scn *scn;
734d192f3d3SKai Wang GElf_Shdr shdr;
735d192f3d3SKai Wang GElf_Sym sym;
736d192f3d3SKai Wang Elf_Data *data;
737d192f3d3SKai Wang char *name;
738d192f3d3SKai Wang size_t n, shstrndx;
739d192f3d3SKai Wang int elferr, tabndx, len, i;
740d192f3d3SKai Wang
741d192f3d3SKai Wang if ((e = elf_memory(maddr, size)) == NULL) {
742d192f3d3SKai Wang bsdar_warnc(bsdar, 0, "elf_memory() failed: %s",
743d192f3d3SKai Wang elf_errmsg(-1));
744d192f3d3SKai Wang return;
745d192f3d3SKai Wang }
746d192f3d3SKai Wang if (elf_kind(e) != ELF_K_ELF) {
7476bef5d28SBenedict Reuschling /* Silently ignore non-elf member. */
748d192f3d3SKai Wang elf_end(e);
749d192f3d3SKai Wang return;
750d192f3d3SKai Wang }
751d192f3d3SKai Wang if (elf_getshstrndx(e, &shstrndx) == 0) {
75209319f7dSEd Maste bsdar_warnc(bsdar, 0, "elf_getshstrndx failed: %s",
753d192f3d3SKai Wang elf_errmsg(-1));
754d192f3d3SKai Wang elf_end(e);
755d192f3d3SKai Wang return;
756d192f3d3SKai Wang }
757d192f3d3SKai Wang
758d192f3d3SKai Wang tabndx = -1;
759d192f3d3SKai Wang scn = NULL;
760d192f3d3SKai Wang while ((scn = elf_nextscn(e, scn)) != NULL) {
761d192f3d3SKai Wang if (gelf_getshdr(scn, &shdr) != &shdr) {
762d192f3d3SKai Wang bsdar_warnc(bsdar, 0,
763d192f3d3SKai Wang "elf_getshdr failed: %s", elf_errmsg(-1));
764d192f3d3SKai Wang continue;
765d192f3d3SKai Wang }
766d192f3d3SKai Wang if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL) {
767d192f3d3SKai Wang bsdar_warnc(bsdar, 0,
768d192f3d3SKai Wang "elf_strptr failed: %s", elf_errmsg(-1));
769d192f3d3SKai Wang continue;
770d192f3d3SKai Wang }
771d192f3d3SKai Wang if (strcmp(name, ".strtab") == 0) {
772d192f3d3SKai Wang tabndx = elf_ndxscn(scn);
773d192f3d3SKai Wang break;
774d192f3d3SKai Wang }
775d192f3d3SKai Wang }
776d192f3d3SKai Wang elferr = elf_errno();
777d192f3d3SKai Wang if (elferr != 0)
778d192f3d3SKai Wang bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s",
779d192f3d3SKai Wang elf_errmsg(elferr));
780d192f3d3SKai Wang if (tabndx == -1) {
781d192f3d3SKai Wang bsdar_warnc(bsdar, 0, "can't find .strtab section");
782d192f3d3SKai Wang elf_end(e);
783d192f3d3SKai Wang return;
784d192f3d3SKai Wang }
785d192f3d3SKai Wang
786d192f3d3SKai Wang scn = NULL;
787d192f3d3SKai Wang while ((scn = elf_nextscn(e, scn)) != NULL) {
788d192f3d3SKai Wang if (gelf_getshdr(scn, &shdr) != &shdr) {
78909319f7dSEd Maste bsdar_warnc(bsdar, 0, "elf_getshdr failed: %s",
79009319f7dSEd Maste elf_errmsg(-1));
791d192f3d3SKai Wang continue;
792d192f3d3SKai Wang }
793d192f3d3SKai Wang if (shdr.sh_type != SHT_SYMTAB)
794d192f3d3SKai Wang continue;
795d192f3d3SKai Wang
796d192f3d3SKai Wang data = NULL;
797d192f3d3SKai Wang n = 0;
798d192f3d3SKai Wang while (n < shdr.sh_size &&
799d192f3d3SKai Wang (data = elf_getdata(scn, data)) != NULL) {
800d192f3d3SKai Wang len = data->d_size / shdr.sh_entsize;
801d192f3d3SKai Wang for (i = 0; i < len; i++) {
802d192f3d3SKai Wang if (gelf_getsym(data, i, &sym) != &sym) {
80309319f7dSEd Maste bsdar_warnc(bsdar, 0,
804d192f3d3SKai Wang "gelf_getsym failed: %s",
805d192f3d3SKai Wang elf_errmsg(-1));
806d192f3d3SKai Wang continue;
807d192f3d3SKai Wang }
808d192f3d3SKai Wang
809d192f3d3SKai Wang /* keep only global or weak symbols */
810d192f3d3SKai Wang if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL &&
811d192f3d3SKai Wang GELF_ST_BIND(sym.st_info) != STB_WEAK)
812d192f3d3SKai Wang continue;
813d192f3d3SKai Wang
814d192f3d3SKai Wang /* keep only defined symbols */
815d192f3d3SKai Wang if (sym.st_shndx == SHN_UNDEF)
816d192f3d3SKai Wang continue;
817d192f3d3SKai Wang
818d192f3d3SKai Wang if ((name = elf_strptr(e, tabndx,
819d192f3d3SKai Wang sym.st_name)) == NULL) {
82009319f7dSEd Maste bsdar_warnc(bsdar, 0,
821d192f3d3SKai Wang "elf_strptr failed: %s",
822d192f3d3SKai Wang elf_errmsg(-1));
823d192f3d3SKai Wang continue;
824d192f3d3SKai Wang }
825d192f3d3SKai Wang
826d192f3d3SKai Wang add_to_ar_sym_table(bsdar, name);
827d192f3d3SKai Wang }
828d192f3d3SKai Wang }
829d192f3d3SKai Wang }
830d192f3d3SKai Wang elferr = elf_errno();
831d192f3d3SKai Wang if (elferr != 0)
83209319f7dSEd Maste bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s",
833d192f3d3SKai Wang elf_errmsg(elferr));
834d192f3d3SKai Wang
835d192f3d3SKai Wang elf_end(e);
836d192f3d3SKai Wang }
837d192f3d3SKai Wang
838d192f3d3SKai Wang /*
839d192f3d3SKai Wang * Append to the archive string table buffer.
840d192f3d3SKai Wang */
841d192f3d3SKai Wang static void
add_to_ar_str_table(struct bsdar * bsdar,const char * name)842d192f3d3SKai Wang add_to_ar_str_table(struct bsdar *bsdar, const char *name)
843d192f3d3SKai Wang {
844d192f3d3SKai Wang
845d192f3d3SKai Wang if (bsdar->as == NULL) {
846d192f3d3SKai Wang bsdar->as_cap = _INIT_AS_CAP;
847d192f3d3SKai Wang bsdar->as_sz = 0;
848d192f3d3SKai Wang if ((bsdar->as = malloc(bsdar->as_cap)) == NULL)
849086f0908SEd Maste bsdar_errc(bsdar, errno, "malloc failed");
850d192f3d3SKai Wang }
851d192f3d3SKai Wang
852d192f3d3SKai Wang /*
853d192f3d3SKai Wang * The space required for holding one member name in as table includes:
854d192f3d3SKai Wang * strlen(name) + (1 for '/') + (1 for '\n') + (possibly 1 for padding).
855d192f3d3SKai Wang */
856517dbe2eSKai Wang while (bsdar->as_sz + strlen(name) + 3 > bsdar->as_cap) {
857d192f3d3SKai Wang bsdar->as_cap *= 2;
858d192f3d3SKai Wang bsdar->as = realloc(bsdar->as, bsdar->as_cap);
859d192f3d3SKai Wang if (bsdar->as == NULL)
860086f0908SEd Maste bsdar_errc(bsdar, errno, "realloc failed");
861d192f3d3SKai Wang }
862d192f3d3SKai Wang strncpy(&bsdar->as[bsdar->as_sz], name, strlen(name));
863d192f3d3SKai Wang bsdar->as_sz += strlen(name);
864d192f3d3SKai Wang bsdar->as[bsdar->as_sz++] = '/';
865d192f3d3SKai Wang bsdar->as[bsdar->as_sz++] = '\n';
866d192f3d3SKai Wang }
867d192f3d3SKai Wang
868d192f3d3SKai Wang /*
869d192f3d3SKai Wang * Append to the archive symbol table buffer.
870d192f3d3SKai Wang */
871d192f3d3SKai Wang static void
add_to_ar_sym_table(struct bsdar * bsdar,const char * name)872d192f3d3SKai Wang add_to_ar_sym_table(struct bsdar *bsdar, const char *name)
873d192f3d3SKai Wang {
874d192f3d3SKai Wang
875d192f3d3SKai Wang if (bsdar->s_so == NULL) {
876d192f3d3SKai Wang if ((bsdar->s_so = malloc(_INIT_SYMOFF_CAP)) ==
877d192f3d3SKai Wang NULL)
878086f0908SEd Maste bsdar_errc(bsdar, errno, "malloc failed");
879d192f3d3SKai Wang bsdar->s_so_cap = _INIT_SYMOFF_CAP;
880d192f3d3SKai Wang bsdar->s_cnt = 0;
881d192f3d3SKai Wang }
882d192f3d3SKai Wang
883d192f3d3SKai Wang if (bsdar->s_sn == NULL) {
884d192f3d3SKai Wang if ((bsdar->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL)
885086f0908SEd Maste bsdar_errc(bsdar, errno, "malloc failed");
886d192f3d3SKai Wang bsdar->s_sn_cap = _INIT_SYMNAME_CAP;
887d192f3d3SKai Wang bsdar->s_sn_sz = 0;
888d192f3d3SKai Wang }
889d192f3d3SKai Wang
890fabed6b2SEd Maste if (bsdar->s_cnt * sizeof(uint64_t) >= bsdar->s_so_cap) {
891d192f3d3SKai Wang bsdar->s_so_cap *= 2;
892d192f3d3SKai Wang bsdar->s_so = realloc(bsdar->s_so, bsdar->s_so_cap);
893d192f3d3SKai Wang if (bsdar->s_so == NULL)
894086f0908SEd Maste bsdar_errc(bsdar, errno, "realloc failed");
895d192f3d3SKai Wang }
896d192f3d3SKai Wang bsdar->s_so[bsdar->s_cnt] = bsdar->rela_off;
897fabed6b2SEd Maste if ((uint64_t)bsdar->rela_off > bsdar->s_so_max)
898fabed6b2SEd Maste bsdar->s_so_max = (uint64_t)bsdar->rela_off;
899d192f3d3SKai Wang bsdar->s_cnt++;
900d192f3d3SKai Wang
901d192f3d3SKai Wang /*
902d192f3d3SKai Wang * The space required for holding one symbol name in sn table includes:
903d192f3d3SKai Wang * strlen(name) + (1 for '\n') + (possibly 1 for padding).
904d192f3d3SKai Wang */
905517dbe2eSKai Wang while (bsdar->s_sn_sz + strlen(name) + 2 > bsdar->s_sn_cap) {
906d192f3d3SKai Wang bsdar->s_sn_cap *= 2;
907d192f3d3SKai Wang bsdar->s_sn = realloc(bsdar->s_sn, bsdar->s_sn_cap);
908d192f3d3SKai Wang if (bsdar->s_sn == NULL)
909086f0908SEd Maste bsdar_errc(bsdar, errno, "realloc failed");
910d192f3d3SKai Wang }
911d192f3d3SKai Wang strncpy(&bsdar->s_sn[bsdar->s_sn_sz], name, strlen(name));
912d192f3d3SKai Wang bsdar->s_sn_sz += strlen(name);
913d192f3d3SKai Wang bsdar->s_sn[bsdar->s_sn_sz++] = '\0';
914d192f3d3SKai Wang }
915