write.c revision 176434
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 176434 2008-02-21 10:52:31Z 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 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); 160 goto giveup; 161 } 162 if (close(obj->fd) < 0) 163 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s", 164 obj->name); 165 166 return(obj); 167 168giveup: 169 if (close(obj->fd) < 0) 170 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s", 171 obj->name); 172 free(obj->name); 173 free(obj); 174 return(NULL); 175} 176 177/* 178 * Insert obj to the tail, or before/after the pos obj. 179 */ 180static void 181insert_obj(struct bsdar *bsdar, struct ar_obj *obj, struct ar_obj *pos) 182{ 183 if (obj == NULL) 184 bsdar_errc(bsdar, EX_SOFTWARE, 0, "try to insert a null obj"); 185 186 if (pos == NULL || obj == pos) 187 /* 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. 190 */ 191 goto tail; 192 193 if (bsdar->options & AR_B) { 194 TAILQ_INSERT_BEFORE(pos, obj, objs); 195 return; 196 } 197 if (bsdar->options & AR_A) { 198 TAILQ_INSERT_AFTER(&bsdar->v_obj, pos, obj, objs); 199 return; 200 } 201 202tail: 203 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs); 204 205} 206 207/* 208 * Determine the constitution of resulting archive. 209 */ 210static void 211write_archive(struct bsdar *bsdar, char mode) 212{ 213 struct archive *a; 214 struct archive_entry *entry; 215 struct ar_obj *nobj, *obj, *obj_temp, *pos; 216 struct stat sb; 217 const char *name; 218 const char *bname; 219 char *buff; 220 char **av; 221 size_t size; 222 int i, r; 223 224 TAILQ_INIT(&bsdar->v_obj); 225 nobj = NULL; 226 pos = NULL; 227 memset(&sb, 0, sizeof(sb)); 228 229 /* By default, no compression is assumed. */ 230 bsdar->compression = ARCHIVE_COMPRESSION_NONE; 231 232 /* 233 * Test if the specified archive exists, to figure out 234 * whether we are creating one here. 235 */ 236 if (stat(bsdar->filename, &sb) != 0) { 237 if (errno != ENOENT) { 238 bsdar_warnc(bsdar, 0, "stat %s failed", 239 bsdar->filename); 240 return; 241 } 242 243 /* We do not create archive in mode 'd', 'm' and 's'. */ 244 if (mode != 'r') { 245 bsdar_warnc(bsdar, 0, "%s: no such file", 246 bsdar->filename); 247 return; 248 } 249 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); 253 goto new_archive; 254 } 255 256 /* 257 * First read members from existing archive. 258 */ 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)); 264 for (;;) { 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) 270 break; 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..."); 275 continue; 276 } 277 278 /* 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. 282 */ 283 bsdar->compression = archive_compression(a); 284 285 name = archive_entry_pathname(entry); 286 287 /* 288 * skip pseudo members. 289 */ 290 if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0) 291 continue; 292 293 size = archive_entry_size(entry); 294 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)); 299 free(buff); 300 continue; 301 } 302 obj = malloc(sizeof(struct ar_obj)); 303 if (obj == NULL) 304 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); 305 obj->maddr = buff; 306 if ((obj->name = strdup(name)) == NULL) 307 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed"); 308 obj->size = size; 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); 313 obj->dev = 0; 314 obj->ino = 0; 315 316 /* 317 * Objects from archive have obj->fd set to -1, 318 * for the ease of cleaning up. 319 */ 320 obj->fd = -1; 321 TAILQ_INSERT_TAIL(&bsdar->v_obj, obj, objs); 322 } 323 AC(archive_read_close(a)); 324 AC(archive_read_finish(a)); 325 326 /* 327 * For mode 's', no member will be moved, deleted or replaced. 328 */ 329 if (mode == 's') 330 goto write_objs; 331 332 /* 333 * Try to find the position member specified by user. 334 */ 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) { 338 pos = obj; 339 break; 340 } 341 } 342 343 /* 344 * If can't find `pos' specified by user, 345 * sliently insert objects at tail. 346 */ 347 if (pos == NULL) 348 bsdar->options &= ~(AR_A | AR_B); 349 } 350 351 for (i = 0; i < bsdar->argc; i++) { 352 av = &bsdar->argv[i]; 353 354 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) { 355 if ((bname = basename(*av)) == NULL) 356 bsdar_errc(bsdar, EX_SOFTWARE, errno, 357 "basename failed"); 358 if (bsdar->options & AR_TR) { 359 if (strncmp(bname, obj->name, _TRUNCATE_LEN)) 360 continue; 361 } else 362 if (strcmp(bname, obj->name) != 0) 363 continue; 364 365 if (mode == 'r') { 366 /* 367 * if the new member is not qualified 368 * to replace the old one, skip it. 369 */ 370 nobj = create_obj_from_file(bsdar, *av, 371 obj->mtime); 372 if (nobj == NULL) 373 goto skip_obj; 374 } 375 376 if (bsdar->options & AR_V) 377 (void)fprintf(stdout, "%c - %s\n", mode, 378 *av); 379 380 TAILQ_REMOVE(&bsdar->v_obj, obj, objs); 381 if (mode == 'd' || mode == 'r') { 382 free(obj->maddr); 383 free(obj->name); 384 free(obj); 385 } 386 387 if (mode == 'm') 388 insert_obj(bsdar, obj, pos); 389 if (mode == 'r') 390 insert_obj(bsdar, nobj, pos); 391 392 skip_obj: 393 *av = NULL; 394 break; 395 } 396 397 } 398 399new_archive: 400 /* 401 * When operating in mode 'r', directly add those user specified 402 * objects which do not exist in current archive. 403 */ 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); 408 if (nobj != NULL) 409 insert_obj(bsdar, nobj, pos); 410 if (bsdar->options & AR_V && nobj != NULL) 411 (void)fprintf(stdout, "a - %s\n", *av); 412 *av = NULL; 413 } 414 } 415 416write_objs: 417 write_objs(bsdar); 418 write_cleanup(bsdar); 419} 420 421/* 422 * Memory cleaning up. 423 */ 424static void 425write_cleanup(struct bsdar *bsdar) 426{ 427 struct ar_obj *obj, *obj_temp; 428 429 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) { 430 free(obj->name); 431 if (obj->fd == -1) 432 free(obj->maddr); 433 else 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); 438 free(obj); 439 } 440 441 free(bsdar->as); 442 free(bsdar->s_so); 443 free(bsdar->s_sn); 444 bsdar->as = NULL; 445 bsdar->s_so = NULL; 446 bsdar->s_sn = NULL; 447} 448 449/* 450 * Wrapper for archive_write_data(). 451 */ 452static void 453write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s) 454{ 455 if (archive_write_data(a, buf, s) != (ssize_t)s) 456 bsdar_errc(bsdar, EX_SOFTWARE, 0, "%s", 457 archive_error_string(a)); 458} 459 460/* 461 * Write the resulting archive members. 462 */ 463static void 464write_objs(struct bsdar *bsdar) 465{ 466 struct ar_obj *obj; 467 struct archive *a; 468 struct archive_entry *entry; 469 size_t s_sz; /* size of archive symbol table. */ 470 size_t pm_sz; /* size of pseudo members */ 471 int i, nr; 472 473 if (elf_version(EV_CURRENT) == EV_NONE) 474 bsdar_errc(bsdar, EX_SOFTWARE, 0, 475 "ELF library initialization failed: %s", elf_errmsg(-1)); 476 477 bsdar->rela_off = 0; 478 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; 486 } 487 488 /* 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. 492 */ 493 if (bsdar->s_cnt != 0 && bsdar->s_sn_sz % 2 != 0) 494 bsdar->s_sn[bsdar->s_sn_sz++] = '\0'; 495 496 /* 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. 500 */ 501 if (bsdar->as != NULL && bsdar->as_sz % 2 != 0) 502 bsdar->as[bsdar->as_sz++] = '\n'; 503 504 /* 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. 508 * 509 * absolute_offset = htobe32(relative_offset + size_of_pseudo_members) 510 */ 511 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) + 519 pm_sz); 520 } 521 522 if ((a = archive_write_new()) == NULL) 523 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_write_new failed"); 524 525 archive_write_set_format_ar_svr4(a); 526 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'. 532 */ 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); 541 else 542 archive_write_set_compression_none(a); 543 544 AC(archive_write_open_filename(a, bsdar->filename)); 545 546 /* 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. 550 */ 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) * 562 bsdar->s_cnt); 563 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz); 564 archive_entry_free(entry); 565 } 566 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); 575 } 576 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); 592 } 593 594 AC(archive_write_close(a)); 595 AC(archive_write_finish(a)); 596} 597 598/* 599 * Extract global symbols from ELF binary members. 600 */ 601static void 602create_symtab_entry(struct bsdar *bsdar, void *maddr, size_t size) 603{ 604 Elf *e; 605 Elf_Scn *scn; 606 GElf_Shdr shdr; 607 GElf_Sym sym; 608 Elf_Data *data; 609 char *name; 610 size_t n, shstrndx; 611 int elferr, tabndx, len, i; 612 613 if ((e = elf_memory(maddr, size)) == NULL) { 614 bsdar_warnc(bsdar, 0, "elf_memory() failed: %s", 615 elf_errmsg(-1)); 616 return; 617 } 618 if (elf_kind(e) != ELF_K_ELF) { 619 /* Sliently ignore non-elf member. */ 620 elf_end(e); 621 return; 622 } 623 if (elf_getshstrndx(e, &shstrndx) == 0) { 624 bsdar_warnc(bsdar, EX_SOFTWARE, 0, "elf_getshstrndx failed: %s", 625 elf_errmsg(-1)); 626 elf_end(e); 627 return; 628 } 629 630 tabndx = -1; 631 scn = NULL; 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)); 636 continue; 637 } 638 if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL) { 639 bsdar_warnc(bsdar, 0, 640 "elf_strptr failed: %s", elf_errmsg(-1)); 641 continue; 642 } 643 if (strcmp(name, ".strtab") == 0) { 644 tabndx = elf_ndxscn(scn); 645 break; 646 } 647 } 648 elferr = elf_errno(); 649 if (elferr != 0) 650 bsdar_warnc(bsdar, 0, "elf_nextscn failed: %s", 651 elf_errmsg(elferr)); 652 if (tabndx == -1) { 653 bsdar_warnc(bsdar, 0, "can't find .strtab section"); 654 elf_end(e); 655 return; 656 } 657 658 scn = NULL; 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)); 663 continue; 664 } 665 if (shdr.sh_type != SHT_SYMTAB) 666 continue; 667 668 data = NULL; 669 n = 0; 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", 677 elf_errmsg(-1)); 678 continue; 679 } 680 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) 684 continue; 685 686 /* keep only defined symbols */ 687 if (sym.st_shndx == SHN_UNDEF) 688 continue; 689 690 if ((name = elf_strptr(e, tabndx, 691 sym.st_name)) == NULL) { 692 bsdar_warnc(bsdar, EX_SOFTWARE, 0, 693 "elf_strptr failed: %s", 694 elf_errmsg(-1)); 695 continue; 696 } 697 698 add_to_ar_sym_table(bsdar, name); 699 } 700 } 701 } 702 elferr = elf_errno(); 703 if (elferr != 0) 704 bsdar_warnc(bsdar, EX_SOFTWARE, 0, "elf_nextscn failed: %s", 705 elf_errmsg(elferr)); 706 707 elf_end(e); 708} 709 710/* 711 * Append to the archive string table buffer. 712 */ 713static void 714add_to_ar_str_table(struct bsdar *bsdar, const char *name) 715{ 716 717 if (bsdar->as == NULL) { 718 bsdar->as_cap = _INIT_AS_CAP; 719 bsdar->as_sz = 0; 720 if ((bsdar->as = malloc(bsdar->as_cap)) == NULL) 721 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); 722 } 723 724 /* 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). 727 */ 728 if (bsdar->as_sz + strlen(name) + 3 > bsdar->as_cap) { 729 bsdar->as_cap *= 2; 730 bsdar->as = realloc(bsdar->as, bsdar->as_cap); 731 if (bsdar->as == NULL) 732 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed"); 733 } 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'; 738} 739 740/* 741 * Append to the archive symbol table buffer. 742 */ 743static void 744add_to_ar_sym_table(struct bsdar *bsdar, const char *name) 745{ 746 747 if (bsdar->s_so == NULL) { 748 if ((bsdar->s_so = malloc(_INIT_SYMOFF_CAP)) == 749 NULL) 750 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); 751 bsdar->s_so_cap = _INIT_SYMOFF_CAP; 752 bsdar->s_cnt = 0; 753 } 754 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; 759 bsdar->s_sn_sz = 0; 760 } 761 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"); 767 } 768 bsdar->s_so[bsdar->s_cnt] = bsdar->rela_off; 769 bsdar->s_cnt++; 770 771 /* 772 * The space required for holding one symbol name in sn table includes: 773 * strlen(name) + (1 for '\n') + (possibly 1 for padding). 774 */ 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"); 780 } 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'; 784} 785