write.c revision 176528
1/*- 2 * Copyright (c) 2007 Kai Wang 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 14 * 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. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/usr.bin/ar/write.c 176528 2008-02-24 18:30:17Z kaiw $"); 29 30#include <sys/endian.h> 31#include <sys/mman.h> 32#include <sys/queue.h> 33#include <sys/stat.h> 34#include <archive.h> 35#include <archive_entry.h> 36#include <errno.h> 37#include <fcntl.h> 38#include <gelf.h> 39#include <libgen.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <string.h> 43#include <sysexits.h> 44 45#include "ar.h" 46 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 */ 54 55static void add_to_ar_str_table(struct bsdar *bsdar, const char *name); 56static void add_to_ar_sym_table(struct bsdar *bsdar, const char *name); 57static struct ar_obj *create_obj_from_file(struct bsdar *bsdar, 58 const char *name, time_t mtime); 59static void create_symtab_entry(struct bsdar *bsdar, void *maddr, 60 size_t size); 61static void insert_obj(struct bsdar *bsdar, struct ar_obj *obj, 62 struct ar_obj *pos); 63static void write_archive(struct bsdar *bsdar, char mode); 64static void write_cleanup(struct bsdar *bsdar); 65static void write_data(struct bsdar *bsdar, struct archive *a, 66 const void *buf, size_t s); 67static void write_objs(struct bsdar *bsdar); 68 69void 70ar_mode_d(struct bsdar *bsdar) 71{ 72 73 write_archive(bsdar, 'd'); 74} 75 76void 77ar_mode_m(struct bsdar *bsdar) 78{ 79 80 write_archive(bsdar, 'm'); 81} 82 83void 84ar_mode_r(struct bsdar *bsdar) 85{ 86 87 write_archive(bsdar, 'r'); 88} 89 90void 91ar_mode_s(struct bsdar *bsdar) 92{ 93 94 write_archive(bsdar, 's'); 95} 96 97/* 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. 101 */ 102static struct ar_obj * 103create_obj_from_file(struct bsdar *bsdar, const char *name, time_t mtime) 104{ 105 struct ar_obj *obj; 106 struct stat sb; 107 const char *bname; 108 109 if (name == NULL) 110 return (NULL); 111 112 obj = malloc(sizeof(struct ar_obj)); 113 if (obj == NULL) 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); 117 free(obj); 118 return (NULL); 119 } 120 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'; 128 } else 129 if ((obj->name = strdup(bname)) == NULL) 130 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed"); 131 132 if (fstat(obj->fd, &sb) < 0) { 133 bsdar_warnc(bsdar, errno, "can't fstat file: %s", obj->name); 134 goto giveup; 135 } 136 if (!S_ISREG(sb.st_mode)) { 137 bsdar_warnc(bsdar, 0, "%s is not an ordinary file", obj->name); 138 goto giveup; 139 } 140 141 /* 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'. 146 */ 147 if (mtime != 0 && bsdar->options & AR_U && sb.st_mtime <= mtime) 148 goto giveup; 149 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 158 if (obj->size == 0) { 159 obj->maddr = NULL; 160 return (obj); 161 } 162 163 if ((obj->maddr = mmap(NULL, obj->size, PROT_READ, 164 MAP_PRIVATE, obj->fd, (off_t)0)) == MAP_FAILED) { 165 bsdar_warnc(bsdar, errno, "can't mmap file: %s", obj->name); 166 goto giveup; 167 } 168 if (close(obj->fd) < 0) 169 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s", 170 obj->name); 171 172 return (obj); 173 174giveup: 175 if (close(obj->fd) < 0) 176 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s", 177 obj->name); 178 free(obj->name); 179 free(obj); 180 return (NULL); 181} 182 183/* 184 * Insert obj to the tail, or before/after the pos obj. 185 */ 186static void 187insert_obj(struct bsdar *bsdar, struct ar_obj *obj, struct ar_obj *pos) 188{ 189 if (obj == NULL) 190 bsdar_errc(bsdar, EX_SOFTWARE, 0, "try to insert a null obj"); 191 192 if (pos == NULL || obj == pos) 193 /* 194 * If the object to move happens to be the posistion obj, 195 * or if there is not a pos obj, move it to tail. 196 */ 197 goto tail; 198 199 if (bsdar->options & AR_B) { 200 TAILQ_INSERT_BEFORE(pos, obj, objs); 201 return; 202 } 203 if (bsdar->options & AR_A) { 204 TAILQ_INSERT_AFTER(&bsdar->v_obj, pos, obj, objs); 205 return; 206 } 207 208tail: 209 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs); 210 211} 212 213/* 214 * Determine the constitution of resulting archive. 215 */ 216static void 217write_archive(struct bsdar *bsdar, char mode) 218{ 219 struct archive *a; 220 struct archive_entry *entry; 221 struct ar_obj *nobj, *obj, *obj_temp, *pos; 222 struct stat sb; 223 const char *name; 224 const char *bname; 225 char *buff; 226 char **av; 227 size_t size; 228 int i, r; 229 230 TAILQ_INIT(&bsdar->v_obj); 231 nobj = NULL; 232 pos = NULL; 233 memset(&sb, 0, sizeof(sb)); 234 235 /* By default, no compression is assumed. */ 236 bsdar->compression = ARCHIVE_COMPRESSION_NONE; 237 238 /* 239 * Test if the specified archive exists, to figure out 240 * whether we are creating one here. 241 */ 242 if (stat(bsdar->filename, &sb) != 0) { 243 if (errno != ENOENT) { 244 bsdar_warnc(bsdar, 0, "stat %s failed", 245 bsdar->filename); 246 return; 247 } 248 249 /* We do not create archive in mode 'd', 'm' and 's'. */ 250 if (mode != 'r') { 251 bsdar_warnc(bsdar, 0, "%s: no such file", 252 bsdar->filename); 253 return; 254 } 255 256 /* Issue a warning if -c is not specified when creating. */ 257 if (!(bsdar->options & AR_C)) 258 bsdar_warnc(bsdar, 0, "creating %s", bsdar->filename); 259 goto new_archive; 260 } 261 262 /* 263 * First read members from existing archive. 264 */ 265 if ((a = archive_read_new()) == NULL) 266 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed"); 267 archive_read_support_compression_all(a); 268 archive_read_support_format_ar(a); 269 AC(archive_read_open_filename(a, bsdar->filename, DEF_BLKSZ)); 270 for (;;) { 271 r = archive_read_next_header(a, &entry); 272 if (r == ARCHIVE_FATAL) 273 bsdar_errc(bsdar, EX_DATAERR, 0, "%s", 274 archive_error_string(a)); 275 if (r == ARCHIVE_EOF) 276 break; 277 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY) 278 bsdar_warnc(bsdar, 0, "%s", archive_error_string(a)); 279 if (r == ARCHIVE_RETRY) { 280 bsdar_warnc(bsdar, 0, "Retrying..."); 281 continue; 282 } 283 284 /* 285 * Remember the compression mode of existing archive. 286 * If neither -j nor -z is specified, this mode will 287 * be used for resulting archive. 288 */ 289 bsdar->compression = archive_compression(a); 290 291 name = archive_entry_pathname(entry); 292 293 /* 294 * skip pseudo members. 295 */ 296 if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0) 297 continue; 298 299 size = archive_entry_size(entry); 300 301 if ((buff = malloc(size)) == NULL) 302 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); 303 if (archive_read_data(a, buff, size) != (ssize_t)size) { 304 bsdar_warnc(bsdar, 0, "%s", archive_error_string(a)); 305 free(buff); 306 continue; 307 } 308 obj = malloc(sizeof(struct ar_obj)); 309 if (obj == NULL) 310 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); 311 obj->maddr = buff; 312 if ((obj->name = strdup(name)) == NULL) 313 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed"); 314 obj->size = size; 315 obj->uid = archive_entry_uid(entry); 316 obj->gid = archive_entry_gid(entry); 317 obj->md = archive_entry_mode(entry); 318 obj->mtime = archive_entry_mtime(entry); 319 obj->dev = 0; 320 obj->ino = 0; 321 322 /* 323 * Objects from archive have obj->fd set to -1, 324 * for the ease of cleaning up. 325 */ 326 obj->fd = -1; 327 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs); 328 } 329 AC(archive_read_close(a)); 330 AC(archive_read_finish(a)); 331 332 /* 333 * For mode 's', no member will be moved, deleted or replaced. 334 */ 335 if (mode == 's') 336 goto write_objs; 337 338 /* 339 * Try to find the position member specified by user. 340 */ 341 if (bsdar->options & AR_A || bsdar->options & AR_B) { 342 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) { 343 if (strcmp(obj->name, bsdar->posarg) == 0) { 344 pos = obj; 345 break; 346 } 347 } 348 349 /* 350 * If can't find `pos' specified by user, 351 * sliently insert objects at tail. 352 */ 353 if (pos == NULL) 354 bsdar->options &= ~(AR_A | AR_B); 355 } 356 357 for (i = 0; i < bsdar->argc; i++) { 358 av = &bsdar->argv[i]; 359 360 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) { 361 if ((bname = basename(*av)) == NULL) 362 bsdar_errc(bsdar, EX_SOFTWARE, errno, 363 "basename failed"); 364 if (bsdar->options & AR_TR) { 365 if (strncmp(bname, obj->name, _TRUNCATE_LEN)) 366 continue; 367 } else 368 if (strcmp(bname, obj->name) != 0) 369 continue; 370 371 if (mode == 'r') { 372 /* 373 * if the new member is not qualified 374 * to replace the old one, skip it. 375 */ 376 nobj = create_obj_from_file(bsdar, *av, 377 obj->mtime); 378 if (nobj == NULL) 379 goto skip_obj; 380 } 381 382 if (bsdar->options & AR_V) 383 (void)fprintf(stdout, "%c - %s\n", mode, 384 *av); 385 386 TAILQ_REMOVE(&bsdar->v_obj, obj, objs); 387 if (mode == 'd' || mode == 'r') { 388 free(obj->maddr); 389 free(obj->name); 390 free(obj); 391 } 392 393 if (mode == 'm') 394 insert_obj(bsdar, obj, pos); 395 if (mode == 'r') 396 insert_obj(bsdar, nobj, pos); 397 398 skip_obj: 399 *av = NULL; 400 break; 401 } 402 403 } 404 405new_archive: 406 /* 407 * When operating in mode 'r', directly add those user specified 408 * objects which do not exist in current archive. 409 */ 410 for (i = 0; i < bsdar->argc; i++) { 411 av = &bsdar->argv[i]; 412 if (*av != NULL && mode == 'r') { 413 nobj = create_obj_from_file(bsdar, *av, 0); 414 if (nobj != NULL) 415 insert_obj(bsdar, nobj, pos); 416 if (bsdar->options & AR_V && nobj != NULL) 417 (void)fprintf(stdout, "a - %s\n", *av); 418 *av = NULL; 419 } 420 } 421 422write_objs: 423 write_objs(bsdar); 424 write_cleanup(bsdar); 425} 426 427/* 428 * Memory cleaning up. 429 */ 430static void 431write_cleanup(struct bsdar *bsdar) 432{ 433 struct ar_obj *obj, *obj_temp; 434 435 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) { 436 if (obj->fd == -1) 437 free(obj->maddr); 438 else 439 if (obj->maddr != NULL && munmap(obj->maddr, obj->size)) 440 bsdar_warnc(bsdar, errno, 441 "can't munmap file: %s", obj->name); 442 TAILQ_REMOVE(&bsdar->v_obj, obj, objs); 443 free(obj->name); 444 free(obj); 445 } 446 447 free(bsdar->as); 448 free(bsdar->s_so); 449 free(bsdar->s_sn); 450 bsdar->as = NULL; 451 bsdar->s_so = NULL; 452 bsdar->s_sn = NULL; 453} 454 455/* 456 * Wrapper for archive_write_data(). 457 */ 458static void 459write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s) 460{ 461 if (archive_write_data(a, buf, s) != (ssize_t)s) 462 bsdar_errc(bsdar, EX_SOFTWARE, 0, "%s", 463 archive_error_string(a)); 464} 465 466/* 467 * Write the resulting archive members. 468 */ 469static void 470write_objs(struct bsdar *bsdar) 471{ 472 struct ar_obj *obj; 473 struct archive *a; 474 struct archive_entry *entry; 475 size_t s_sz; /* size of archive symbol table. */ 476 size_t pm_sz; /* size of pseudo members */ 477 int i, nr; 478 479 if (elf_version(EV_CURRENT) == EV_NONE) 480 bsdar_errc(bsdar, EX_SOFTWARE, 0, 481 "ELF library initialization failed: %s", elf_errmsg(-1)); 482 483 bsdar->rela_off = 0; 484 485 /* Create archive symbol table and archive string table, if need. */ 486 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) { 487 if (!(bsdar->options & AR_SS) && obj->maddr != NULL) 488 create_symtab_entry(bsdar, obj->maddr, obj->size); 489 if (strlen(obj->name) > _MAXNAMELEN_SVR4) 490 add_to_ar_str_table(bsdar, obj->name); 491 bsdar->rela_off += _ARHDR_LEN + obj->size + obj->size % 2; 492 } 493 494 /* 495 * Pad the symbol name string table. It is treated specially because 496 * symbol name table should be padded by a '\0', not the common '\n' 497 * for other members. The size of sn table includes the pad bit. 498 */ 499 if (bsdar->s_cnt != 0 && bsdar->s_sn_sz % 2 != 0) 500 bsdar->s_sn[bsdar->s_sn_sz++] = '\0'; 501 502 /* 503 * Archive string table is padded by a "\n" as the normal members. 504 * The differece is that the size of archive string table counts 505 * in the pad bit, while normal members' size fileds do not. 506 */ 507 if (bsdar->as != NULL && bsdar->as_sz % 2 != 0) 508 bsdar->as[bsdar->as_sz++] = '\n'; 509 510 /* 511 * If there is a symbol table, calculate the size of pseudo members, 512 * covert previously stored relative offsets to absolute ones, and 513 * then make them Big Endian. 514 * 515 * absolute_offset = htobe32(relative_offset + size_of_pseudo_members) 516 */ 517 518 if (bsdar->s_cnt != 0) { 519 s_sz = (bsdar->s_cnt + 1) * sizeof(uint32_t) + bsdar->s_sn_sz; 520 pm_sz = _ARMAG_LEN + (_ARHDR_LEN + s_sz); 521 if (bsdar->as != NULL) 522 pm_sz += _ARHDR_LEN + bsdar->as_sz; 523 for (i = 0; (size_t)i < bsdar->s_cnt; i++) 524 *(bsdar->s_so + i) = htobe32(*(bsdar->s_so + i) + 525 pm_sz); 526 } 527 528 if ((a = archive_write_new()) == NULL) 529 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_write_new failed"); 530 531 archive_write_set_format_ar_svr4(a); 532 533 /* The compression mode of the existing archive is used 534 * for the result archive or if creating a new archive, we 535 * do not compress archive by default. This default behavior can 536 * be overrided by compression mode specified explicitly 537 * through command line option `-j' or `-z'. 538 */ 539 if (bsdar->options & AR_J) 540 bsdar->compression = ARCHIVE_COMPRESSION_BZIP2; 541 if (bsdar->options & AR_Z) 542 bsdar->compression = ARCHIVE_COMPRESSION_GZIP; 543 if (bsdar->compression == ARCHIVE_COMPRESSION_BZIP2) 544 archive_write_set_compression_bzip2(a); 545 else if (bsdar->compression == ARCHIVE_COMPRESSION_GZIP) 546 archive_write_set_compression_gzip(a); 547 else 548 archive_write_set_compression_none(a); 549 550 AC(archive_write_open_filename(a, bsdar->filename)); 551 552 /* 553 * write the archive symbol table, if there is one. 554 * If options -s is explicitly specified or we are invoked 555 * as ranlib, write the symbol table even if it is empty. 556 */ 557 if ((bsdar->s_cnt != 0 && !(bsdar->options & AR_SS)) || 558 bsdar->options & AR_S) { 559 entry = archive_entry_new(); 560 archive_entry_copy_pathname(entry, "/"); 561 archive_entry_set_mtime(entry, time(NULL), 0); 562 archive_entry_set_size(entry, (bsdar->s_cnt + 1) * 563 sizeof(uint32_t) + bsdar->s_sn_sz); 564 AC(archive_write_header(a, entry)); 565 nr = htobe32(bsdar->s_cnt); 566 write_data(bsdar, a, &nr, sizeof(uint32_t)); 567 write_data(bsdar, a, bsdar->s_so, sizeof(uint32_t) * 568 bsdar->s_cnt); 569 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz); 570 archive_entry_free(entry); 571 } 572 573 /* write the archive string table, if any. */ 574 if (bsdar->as != NULL) { 575 entry = archive_entry_new(); 576 archive_entry_copy_pathname(entry, "//"); 577 archive_entry_set_size(entry, bsdar->as_sz); 578 AC(archive_write_header(a, entry)); 579 write_data(bsdar, a, bsdar->as, bsdar->as_sz); 580 archive_entry_free(entry); 581 } 582 583 /* write normal members. */ 584 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) { 585 entry = archive_entry_new(); 586 archive_entry_copy_pathname(entry, obj->name); 587 archive_entry_set_uid(entry, obj->uid); 588 archive_entry_set_gid(entry, obj->gid); 589 archive_entry_set_mode(entry, obj->md); 590 archive_entry_set_size(entry, obj->size); 591 archive_entry_set_mtime(entry, obj->mtime, 0); 592 archive_entry_set_dev(entry, obj->dev); 593 archive_entry_set_ino(entry, obj->ino); 594 archive_entry_set_filetype(entry, AE_IFREG); 595 AC(archive_write_header(a, entry)); 596 write_data(bsdar, a, obj->maddr, obj->size); 597 archive_entry_free(entry); 598 } 599 600 AC(archive_write_close(a)); 601 AC(archive_write_finish(a)); 602} 603 604/* 605 * Extract global symbols from ELF binary members. 606 */ 607static void 608create_symtab_entry(struct bsdar *bsdar, void *maddr, size_t size) 609{ 610 Elf *e; 611 Elf_Scn *scn; 612 GElf_Shdr shdr; 613 GElf_Sym sym; 614 Elf_Data *data; 615 char *name; 616 size_t n, shstrndx; 617 int elferr, tabndx, len, i; 618 619 if ((e = elf_memory(maddr, size)) == NULL) { 620 bsdar_warnc(bsdar, 0, "elf_memory() failed: %s", 621 elf_errmsg(-1)); 622 return; 623 } 624 if (elf_kind(e) != ELF_K_ELF) { 625 /* Sliently ignore non-elf member. */ 626 elf_end(e); 627 return; 628 } 629 if (elf_getshstrndx(e, &shstrndx) == 0) { 630 bsdar_warnc(bsdar, EX_SOFTWARE, 0, "elf_getshstrndx failed: %s", 631 elf_errmsg(-1)); 632 elf_end(e); 633 return; 634 } 635 636 tabndx = -1; 637 scn = NULL; 638 while ((scn = elf_nextscn(e, scn)) != NULL) { 639 if (gelf_getshdr(scn, &shdr) != &shdr) { 640 bsdar_warnc(bsdar, 0, 641 "elf_getshdr failed: %s", elf_errmsg(-1)); 642 continue; 643 } 644 if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL) { 645 bsdar_warnc(bsdar, 0, 646 "elf_strptr failed: %s", elf_errmsg(-1)); 647 continue; 648 } 649 if (strcmp(name, ".strtab") == 0) { 650 tabndx = elf_ndxscn(scn); 651 break; 652 } 653 } 654 elferr = elf_errno(); 655 if (elferr != 0) 656 bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s", 657 elf_errmsg(elferr)); 658 if (tabndx == -1) { 659 bsdar_warnc(bsdar, 0, "can't find .strtab section"); 660 elf_end(e); 661 return; 662 } 663 664 scn = NULL; 665 while ((scn = elf_nextscn(e, scn)) != NULL) { 666 if (gelf_getshdr(scn, &shdr) != &shdr) { 667 bsdar_warnc(bsdar, EX_SOFTWARE, 0, 668 "elf_getshdr failed: %s", elf_errmsg(-1)); 669 continue; 670 } 671 if (shdr.sh_type != SHT_SYMTAB) 672 continue; 673 674 data = NULL; 675 n = 0; 676 while (n < shdr.sh_size && 677 (data = elf_getdata(scn, data)) != NULL) { 678 len = data->d_size / shdr.sh_entsize; 679 for (i = 0; i < len; i++) { 680 if (gelf_getsym(data, i, &sym) != &sym) { 681 bsdar_warnc(bsdar, EX_SOFTWARE, 0, 682 "gelf_getsym failed: %s", 683 elf_errmsg(-1)); 684 continue; 685 } 686 687 /* keep only global or weak symbols */ 688 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL && 689 GELF_ST_BIND(sym.st_info) != STB_WEAK) 690 continue; 691 692 /* keep only defined symbols */ 693 if (sym.st_shndx == SHN_UNDEF) 694 continue; 695 696 if ((name = elf_strptr(e, tabndx, 697 sym.st_name)) == NULL) { 698 bsdar_warnc(bsdar, EX_SOFTWARE, 0, 699 "elf_strptr failed: %s", 700 elf_errmsg(-1)); 701 continue; 702 } 703 704 add_to_ar_sym_table(bsdar, name); 705 } 706 } 707 } 708 elferr = elf_errno(); 709 if (elferr != 0) 710 bsdar_warnc(bsdar, EX_SOFTWARE, 0, "elf_nextscn failed: %s", 711 elf_errmsg(elferr)); 712 713 elf_end(e); 714} 715 716/* 717 * Append to the archive string table buffer. 718 */ 719static void 720add_to_ar_str_table(struct bsdar *bsdar, const char *name) 721{ 722 723 if (bsdar->as == NULL) { 724 bsdar->as_cap = _INIT_AS_CAP; 725 bsdar->as_sz = 0; 726 if ((bsdar->as = malloc(bsdar->as_cap)) == NULL) 727 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); 728 } 729 730 /* 731 * The space required for holding one member name in as table includes: 732 * strlen(name) + (1 for '/') + (1 for '\n') + (possibly 1 for padding). 733 */ 734 if (bsdar->as_sz + strlen(name) + 3 > bsdar->as_cap) { 735 bsdar->as_cap *= 2; 736 bsdar->as = realloc(bsdar->as, bsdar->as_cap); 737 if (bsdar->as == NULL) 738 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed"); 739 } 740 strncpy(&bsdar->as[bsdar->as_sz], name, strlen(name)); 741 bsdar->as_sz += strlen(name); 742 bsdar->as[bsdar->as_sz++] = '/'; 743 bsdar->as[bsdar->as_sz++] = '\n'; 744} 745 746/* 747 * Append to the archive symbol table buffer. 748 */ 749static void 750add_to_ar_sym_table(struct bsdar *bsdar, const char *name) 751{ 752 753 if (bsdar->s_so == NULL) { 754 if ((bsdar->s_so = malloc(_INIT_SYMOFF_CAP)) == 755 NULL) 756 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); 757 bsdar->s_so_cap = _INIT_SYMOFF_CAP; 758 bsdar->s_cnt = 0; 759 } 760 761 if (bsdar->s_sn == NULL) { 762 if ((bsdar->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL) 763 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); 764 bsdar->s_sn_cap = _INIT_SYMNAME_CAP; 765 bsdar->s_sn_sz = 0; 766 } 767 768 if (bsdar->s_cnt * sizeof(uint32_t) >= bsdar->s_so_cap) { 769 bsdar->s_so_cap *= 2; 770 bsdar->s_so = realloc(bsdar->s_so, bsdar->s_so_cap); 771 if (bsdar->s_so == NULL) 772 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed"); 773 } 774 bsdar->s_so[bsdar->s_cnt] = bsdar->rela_off; 775 bsdar->s_cnt++; 776 777 /* 778 * The space required for holding one symbol name in sn table includes: 779 * strlen(name) + (1 for '\n') + (possibly 1 for padding). 780 */ 781 if (bsdar->s_sn_sz + strlen(name) + 2 > bsdar->s_sn_cap) { 782 bsdar->s_sn_cap *= 2; 783 bsdar->s_sn = realloc(bsdar->s_sn, bsdar->s_sn_cap); 784 if (bsdar->s_sn == NULL) 785 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed"); 786 } 787 strncpy(&bsdar->s_sn[bsdar->s_sn_sz], name, strlen(name)); 788 bsdar->s_sn_sz += strlen(name); 789 bsdar->s_sn[bsdar->s_sn_sz++] = '\0'; 790} 791