1 /* BFD back-end for archive files (libraries). 2 Copyright (C) 1990-2015 Free Software Foundation, Inc. 3 Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault. 4 5 This file is part of BFD, the Binary File Descriptor library. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 20 21 /* 22 @setfilename archive-info 23 SECTION 24 Archives 25 26 DESCRIPTION 27 An archive (or library) is just another BFD. It has a symbol 28 table, although there's not much a user program will do with it. 29 30 The big difference between an archive BFD and an ordinary BFD 31 is that the archive doesn't have sections. Instead it has a 32 chain of BFDs that are considered its contents. These BFDs can 33 be manipulated like any other. The BFDs contained in an 34 archive opened for reading will all be opened for reading. You 35 may put either input or output BFDs into an archive opened for 36 output; they will be handled correctly when the archive is closed. 37 38 Use <<bfd_openr_next_archived_file>> to step through 39 the contents of an archive opened for input. You don't 40 have to read the entire archive if you don't want 41 to! Read it until you find what you want. 42 43 A BFD returned by <<bfd_openr_next_archived_file>> can be 44 closed manually with <<bfd_close>>. If you do not close it, 45 then a second iteration through the members of an archive may 46 return the same BFD. If you close the archive BFD, then all 47 the member BFDs will automatically be closed as well. 48 49 Archive contents of output BFDs are chained through the 50 <<archive_next>> pointer in a BFD. The first one is findable 51 through the <<archive_head>> slot of the archive. Set it with 52 <<bfd_set_archive_head>> (q.v.). A given BFD may be in only 53 one open output archive at a time. 54 55 As expected, the BFD archive code is more general than the 56 archive code of any given environment. BFD archives may 57 contain files of different formats (e.g., a.out and coff) and 58 even different architectures. You may even place archives 59 recursively into archives! 60 61 This can cause unexpected confusion, since some archive 62 formats are more expressive than others. For instance, Intel 63 COFF archives can preserve long filenames; SunOS a.out archives 64 cannot. If you move a file from the first to the second 65 format and back again, the filename may be truncated. 66 Likewise, different a.out environments have different 67 conventions as to how they truncate filenames, whether they 68 preserve directory names in filenames, etc. When 69 interoperating with native tools, be sure your files are 70 homogeneous. 71 72 Beware: most of these formats do not react well to the 73 presence of spaces in filenames. We do the best we can, but 74 can't always handle this case due to restrictions in the format of 75 archives. Many Unix utilities are braindead in regards to 76 spaces and such in filenames anyway, so this shouldn't be much 77 of a restriction. 78 79 Archives are supported in BFD in <<archive.c>>. 80 81 SUBSECTION 82 Archive functions 83 */ 84 85 /* Assumes: 86 o - all archive elements start on an even boundary, newline padded; 87 o - all arch headers are char *; 88 o - all arch headers are the same size (across architectures). 89 */ 90 91 /* Some formats provide a way to cram a long filename into the short 92 (16 chars) space provided by a BSD archive. The trick is: make a 93 special "file" in the front of the archive, sort of like the SYMDEF 94 entry. If the filename is too long to fit, put it in the extended 95 name table, and use its index as the filename. To prevent 96 confusion prepend the index with a space. This means you can't 97 have filenames that start with a space, but then again, many Unix 98 utilities can't handle that anyway. 99 100 This scheme unfortunately requires that you stand on your head in 101 order to write an archive since you need to put a magic file at the 102 front, and need to touch every entry to do so. C'est la vie. 103 104 We support two variants of this idea: 105 The SVR4 format (extended name table is named "//"), 106 and an extended pseudo-BSD variant (extended name table is named 107 "ARFILENAMES/"). The origin of the latter format is uncertain. 108 109 BSD 4.4 uses a third scheme: It writes a long filename 110 directly after the header. This allows 'ar q' to work. 111 */ 112 113 /* Summary of archive member names: 114 115 Symbol table (must be first): 116 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib. 117 "/ " - Symbol table, system 5 style. 118 119 Long name table (must be before regular file members): 120 "// " - Long name table, System 5 R4 style. 121 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4). 122 123 Regular file members with short names: 124 "filename.o/ " - Regular file, System 5 style (embedded spaces ok). 125 "filename.o " - Regular file, Berkeley style (no embedded spaces). 126 127 Regular files with long names (or embedded spaces, for BSD variants): 128 "/18 " - SVR4 style, name at offset 18 in name table. 129 "#1/23 " - Long name (or embedded spaces) 23 characters long, 130 BSD 4.4 style, full name follows header. 131 " 18 " - Long name 18 characters long, extended pseudo-BSD. 132 */ 133 134 #include "sysdep.h" 135 #include "bfd.h" 136 #include "libiberty.h" 137 #include "libbfd.h" 138 #include "aout/ar.h" 139 #include "aout/ranlib.h" 140 #include "safe-ctype.h" 141 #include "hashtab.h" 142 #include "filenames.h" 143 #include "bfdlink.h" 144 145 #ifndef errno 146 extern int errno; 147 #endif 148 149 /* We keep a cache of archive filepointers to archive elements to 150 speed up searching the archive by filepos. We only add an entry to 151 the cache when we actually read one. We also don't sort the cache; 152 it's generally short enough to search linearly. 153 Note that the pointers here point to the front of the ar_hdr, not 154 to the front of the contents! */ 155 struct ar_cache 156 { 157 file_ptr ptr; 158 bfd *arbfd; 159 }; 160 161 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char) 162 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen) 163 164 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data)) 165 166 static const char * normalize (bfd *, const char *); 167 168 #define arch_hdr(bfd) ((struct ar_hdr *) arch_eltdata (bfd)->arch_header) 169 170 /* True iff NAME designated a BSD 4.4 extended name. */ 171 172 #define is_bsd44_extended_name(NAME) \ 173 (NAME[0] == '#' && NAME[1] == '1' && NAME[2] == '/' && ISDIGIT (NAME[3])) 174 175 void 176 _bfd_ar_spacepad (char *p, size_t n, const char *fmt, long val) 177 { 178 static char buf[20]; 179 size_t len; 180 181 snprintf (buf, sizeof (buf), fmt, val); 182 len = strlen (buf); 183 if (len < n) 184 { 185 memcpy (p, buf, len); 186 memset (p + len, ' ', n - len); 187 } 188 else 189 memcpy (p, buf, n); 190 } 191 192 bfd_boolean 193 _bfd_ar_sizepad (char *p, size_t n, bfd_size_type size) 194 { 195 static char buf[21]; 196 size_t len; 197 198 snprintf (buf, sizeof (buf), "%-10" BFD_VMA_FMT "u", size); 199 len = strlen (buf); 200 if (len > n) 201 { 202 bfd_set_error (bfd_error_file_too_big); 203 return FALSE; 204 } 205 if (len < n) 206 { 207 memcpy (p, buf, len); 208 memset (p + len, ' ', n - len); 209 } 210 else 211 memcpy (p, buf, n); 212 return TRUE; 213 } 214 215 bfd_boolean 216 _bfd_generic_mkarchive (bfd *abfd) 217 { 218 bfd_size_type amt = sizeof (struct artdata); 219 220 abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt); 221 if (bfd_ardata (abfd) == NULL) 222 return FALSE; 223 224 /* Already cleared by bfd_zalloc above. 225 bfd_ardata (abfd)->cache = NULL; 226 bfd_ardata (abfd)->archive_head = NULL; 227 bfd_ardata (abfd)->symdefs = NULL; 228 bfd_ardata (abfd)->extended_names = NULL; 229 bfd_ardata (abfd)->extended_names_size = 0; 230 bfd_ardata (abfd)->tdata = NULL; */ 231 232 return TRUE; 233 } 234 235 /* 236 FUNCTION 237 bfd_get_next_mapent 238 239 SYNOPSIS 240 symindex bfd_get_next_mapent 241 (bfd *abfd, symindex previous, carsym **sym); 242 243 DESCRIPTION 244 Step through archive @var{abfd}'s symbol table (if it 245 has one). Successively update @var{sym} with the next symbol's 246 information, returning that symbol's (internal) index into the 247 symbol table. 248 249 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get 250 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already 251 got the last one. 252 253 A <<carsym>> is a canonical archive symbol. The only 254 user-visible element is its name, a null-terminated string. 255 */ 256 257 symindex 258 bfd_get_next_mapent (bfd *abfd, symindex prev, carsym **entry) 259 { 260 if (!bfd_has_map (abfd)) 261 { 262 bfd_set_error (bfd_error_invalid_operation); 263 return BFD_NO_MORE_SYMBOLS; 264 } 265 266 if (prev == BFD_NO_MORE_SYMBOLS) 267 prev = 0; 268 else 269 ++prev; 270 if (prev >= bfd_ardata (abfd)->symdef_count) 271 return BFD_NO_MORE_SYMBOLS; 272 273 *entry = (bfd_ardata (abfd)->symdefs + prev); 274 return prev; 275 } 276 277 /* To be called by backends only. */ 278 279 bfd * 280 _bfd_create_empty_archive_element_shell (bfd *obfd) 281 { 282 return _bfd_new_bfd_contained_in (obfd); 283 } 284 285 /* 286 FUNCTION 287 bfd_set_archive_head 288 289 SYNOPSIS 290 bfd_boolean bfd_set_archive_head (bfd *output, bfd *new_head); 291 292 DESCRIPTION 293 Set the head of the chain of 294 BFDs contained in the archive @var{output} to @var{new_head}. 295 */ 296 297 bfd_boolean 298 bfd_set_archive_head (bfd *output_archive, bfd *new_head) 299 { 300 output_archive->archive_head = new_head; 301 return TRUE; 302 } 303 304 bfd * 305 _bfd_look_for_bfd_in_cache (bfd *arch_bfd, file_ptr filepos) 306 { 307 htab_t hash_table = bfd_ardata (arch_bfd)->cache; 308 struct ar_cache m; 309 310 m.ptr = filepos; 311 312 if (hash_table) 313 { 314 struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m); 315 if (!entry) 316 return NULL; 317 318 /* Unfortunately this flag is set after checking that we have 319 an archive, and checking for an archive means one element has 320 sneaked into the cache. */ 321 entry->arbfd->no_export = arch_bfd->no_export; 322 return entry->arbfd; 323 } 324 else 325 return NULL; 326 } 327 328 static hashval_t 329 hash_file_ptr (const void * p) 330 { 331 return (hashval_t) (((struct ar_cache *) p)->ptr); 332 } 333 334 /* Returns non-zero if P1 and P2 are equal. */ 335 336 static int 337 eq_file_ptr (const void * p1, const void * p2) 338 { 339 struct ar_cache *arc1 = (struct ar_cache *) p1; 340 struct ar_cache *arc2 = (struct ar_cache *) p2; 341 return arc1->ptr == arc2->ptr; 342 } 343 344 /* The calloc function doesn't always take size_t (e.g. on VMS) 345 so wrap it to avoid a compile time warning. */ 346 347 static void * 348 _bfd_calloc_wrapper (size_t a, size_t b) 349 { 350 return calloc (a, b); 351 } 352 353 /* Kind of stupid to call cons for each one, but we don't do too many. */ 354 355 bfd_boolean 356 _bfd_add_bfd_to_archive_cache (bfd *arch_bfd, file_ptr filepos, bfd *new_elt) 357 { 358 struct ar_cache *cache; 359 htab_t hash_table = bfd_ardata (arch_bfd)->cache; 360 361 /* If the hash table hasn't been created, create it. */ 362 if (hash_table == NULL) 363 { 364 hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr, 365 NULL, _bfd_calloc_wrapper, free); 366 if (hash_table == NULL) 367 return FALSE; 368 bfd_ardata (arch_bfd)->cache = hash_table; 369 } 370 371 /* Insert new_elt into the hash table by filepos. */ 372 cache = (struct ar_cache *) bfd_zalloc (arch_bfd, sizeof (struct ar_cache)); 373 cache->ptr = filepos; 374 cache->arbfd = new_elt; 375 *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache; 376 377 /* Provide a means of accessing this from child. */ 378 arch_eltdata (new_elt)->parent_cache = hash_table; 379 arch_eltdata (new_elt)->key = filepos; 380 381 return TRUE; 382 } 383 384 static bfd * 385 open_nested_file (const char *filename, bfd *archive) 386 { 387 const char *target; 388 bfd *n_bfd; 389 390 target = NULL; 391 if (!archive->target_defaulted) 392 target = archive->xvec->name; 393 n_bfd = bfd_openr (filename, target); 394 if (n_bfd != NULL) 395 { 396 n_bfd->lto_output = archive->lto_output; 397 n_bfd->no_export = archive->no_export; 398 } 399 return n_bfd; 400 } 401 402 static bfd * 403 find_nested_archive (const char *filename, bfd *arch_bfd) 404 { 405 bfd *abfd; 406 407 /* PR 15140: Don't allow a nested archive pointing to itself. */ 408 if (filename_cmp (filename, arch_bfd->filename) == 0) 409 { 410 bfd_set_error (bfd_error_malformed_archive); 411 return NULL; 412 } 413 414 for (abfd = arch_bfd->nested_archives; 415 abfd != NULL; 416 abfd = abfd->archive_next) 417 { 418 if (filename_cmp (filename, abfd->filename) == 0) 419 return abfd; 420 } 421 abfd = open_nested_file (filename, arch_bfd); 422 if (abfd) 423 { 424 abfd->archive_next = arch_bfd->nested_archives; 425 arch_bfd->nested_archives = abfd; 426 } 427 return abfd; 428 } 429 430 /* The name begins with space. Hence the rest of the name is an index into 431 the string table. */ 432 433 static char * 434 get_extended_arelt_filename (bfd *arch, const char *name, file_ptr *originp) 435 { 436 unsigned long table_index = 0; 437 const char *endp; 438 439 /* Should extract string so that I can guarantee not to overflow into 440 the next region, but I'm too lazy. */ 441 errno = 0; 442 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */ 443 table_index = strtol (name + 1, (char **) &endp, 10); 444 if (errno != 0 || table_index >= bfd_ardata (arch)->extended_names_size) 445 { 446 bfd_set_error (bfd_error_malformed_archive); 447 return NULL; 448 } 449 /* In a thin archive, a member of an archive-within-an-archive 450 will have the offset in the inner archive encoded here. */ 451 if (bfd_is_thin_archive (arch) && endp != NULL && *endp == ':') 452 { 453 file_ptr origin = strtol (endp + 1, NULL, 10); 454 455 if (errno != 0) 456 { 457 bfd_set_error (bfd_error_malformed_archive); 458 return NULL; 459 } 460 *originp = origin; 461 } 462 else 463 *originp = 0; 464 465 return bfd_ardata (arch)->extended_names + table_index; 466 } 467 468 /* This functions reads an arch header and returns an areltdata pointer, or 469 NULL on error. 470 471 Presumes the file pointer is already in the right place (ie pointing 472 to the ar_hdr in the file). Moves the file pointer; on success it 473 should be pointing to the front of the file contents; on failure it 474 could have been moved arbitrarily. */ 475 476 void * 477 _bfd_generic_read_ar_hdr (bfd *abfd) 478 { 479 return _bfd_generic_read_ar_hdr_mag (abfd, NULL); 480 } 481 482 /* Alpha ECOFF uses an optional different ARFMAG value, so we have a 483 variant of _bfd_generic_read_ar_hdr which accepts a magic string. */ 484 485 void * 486 _bfd_generic_read_ar_hdr_mag (bfd *abfd, const char *mag) 487 { 488 struct ar_hdr hdr; 489 char *hdrp = (char *) &hdr; 490 bfd_size_type parsed_size; 491 struct areltdata *ared; 492 char *filename = NULL; 493 bfd_size_type namelen = 0; 494 bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr); 495 char *allocptr = 0; 496 file_ptr origin = 0; 497 unsigned int extra_size = 0; 498 char fmag_save; 499 int scan; 500 501 if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr)) 502 { 503 if (bfd_get_error () != bfd_error_system_call) 504 bfd_set_error (bfd_error_no_more_archived_files); 505 return NULL; 506 } 507 if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0 508 && (mag == NULL 509 || strncmp (hdr.ar_fmag, mag, 2) != 0)) 510 { 511 bfd_set_error (bfd_error_malformed_archive); 512 return NULL; 513 } 514 515 errno = 0; 516 fmag_save = hdr.ar_fmag[0]; 517 hdr.ar_fmag[0] = 0; 518 scan = sscanf (hdr.ar_size, "%" BFD_VMA_FMT "u", &parsed_size); 519 hdr.ar_fmag[0] = fmag_save; 520 if (scan != 1) 521 { 522 bfd_set_error (bfd_error_malformed_archive); 523 return NULL; 524 } 525 526 /* Extract the filename from the archive - there are two ways to 527 specify an extended name table, either the first char of the 528 name is a space, or it's a slash. */ 529 if ((hdr.ar_name[0] == '/' 530 || (hdr.ar_name[0] == ' ' 531 && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL)) 532 && bfd_ardata (abfd)->extended_names != NULL) 533 { 534 filename = get_extended_arelt_filename (abfd, hdr.ar_name, &origin); 535 if (filename == NULL) 536 return NULL; 537 } 538 /* BSD4.4-style long filename. */ 539 else if (is_bsd44_extended_name (hdr.ar_name)) 540 { 541 /* BSD-4.4 extended name */ 542 namelen = atoi (&hdr.ar_name[3]); 543 allocsize += namelen + 1; 544 parsed_size -= namelen; 545 extra_size = namelen; 546 547 allocptr = (char *) bfd_zmalloc (allocsize); 548 if (allocptr == NULL) 549 return NULL; 550 filename = (allocptr 551 + sizeof (struct areltdata) 552 + sizeof (struct ar_hdr)); 553 if (bfd_bread (filename, namelen, abfd) != namelen) 554 { 555 free (allocptr); 556 if (bfd_get_error () != bfd_error_system_call) 557 bfd_set_error (bfd_error_no_more_archived_files); 558 return NULL; 559 } 560 filename[namelen] = '\0'; 561 } 562 else 563 { 564 /* We judge the end of the name by looking for '/' or ' '. 565 Note: The SYSV format (terminated by '/') allows embedded 566 spaces, so only look for ' ' if we don't find '/'. */ 567 568 char *e; 569 e = (char *) memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd)); 570 if (e == NULL) 571 { 572 e = (char *) memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)); 573 if (e == NULL) 574 e = (char *) memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd)); 575 } 576 577 if (e != NULL) 578 namelen = e - hdr.ar_name; 579 else 580 { 581 /* If we didn't find a termination character, then the name 582 must be the entire field. */ 583 namelen = ar_maxnamelen (abfd); 584 } 585 586 allocsize += namelen + 1; 587 } 588 589 if (!allocptr) 590 { 591 allocptr = (char *) bfd_zmalloc (allocsize); 592 if (allocptr == NULL) 593 return NULL; 594 } 595 596 ared = (struct areltdata *) allocptr; 597 598 ared->arch_header = allocptr + sizeof (struct areltdata); 599 memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr)); 600 ared->parsed_size = parsed_size; 601 ared->extra_size = extra_size; 602 ared->origin = origin; 603 604 if (filename != NULL) 605 ared->filename = filename; 606 else 607 { 608 ared->filename = allocptr + (sizeof (struct areltdata) + 609 sizeof (struct ar_hdr)); 610 if (namelen) 611 memcpy (ared->filename, hdr.ar_name, namelen); 612 ared->filename[namelen] = '\0'; 613 } 614 615 return ared; 616 } 617 618 /* Append the relative pathname for a member of the thin archive 619 to the pathname of the directory containing the archive. */ 620 621 char * 622 _bfd_append_relative_path (bfd *arch, char *elt_name) 623 { 624 const char *arch_name = arch->filename; 625 const char *base_name = lbasename (arch_name); 626 size_t prefix_len; 627 char *filename; 628 629 if (base_name == arch_name) 630 return elt_name; 631 632 prefix_len = base_name - arch_name; 633 filename = (char *) bfd_alloc (arch, prefix_len + strlen (elt_name) + 1); 634 if (filename == NULL) 635 return NULL; 636 637 strncpy (filename, arch_name, prefix_len); 638 strcpy (filename + prefix_len, elt_name); 639 return filename; 640 } 641 642 /* This is an internal function; it's mainly used when indexing 643 through the archive symbol table, but also used to get the next 644 element, since it handles the bookkeeping so nicely for us. */ 645 646 bfd * 647 _bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos) 648 { 649 struct areltdata *new_areldata; 650 bfd *n_bfd; 651 char *filename; 652 653 n_bfd = _bfd_look_for_bfd_in_cache (archive, filepos); 654 if (n_bfd) 655 return n_bfd; 656 657 if (0 > bfd_seek (archive, filepos, SEEK_SET)) 658 return NULL; 659 660 if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL) 661 return NULL; 662 663 filename = new_areldata->filename; 664 665 if (bfd_is_thin_archive (archive)) 666 { 667 /* This is a proxy entry for an external file. */ 668 if (! IS_ABSOLUTE_PATH (filename)) 669 { 670 filename = _bfd_append_relative_path (archive, filename); 671 if (filename == NULL) 672 { 673 free (new_areldata); 674 return NULL; 675 } 676 } 677 678 if (new_areldata->origin > 0) 679 { 680 /* This proxy entry refers to an element of a nested archive. 681 Locate the member of that archive and return a bfd for it. */ 682 bfd *ext_arch = find_nested_archive (filename, archive); 683 684 if (ext_arch == NULL 685 || ! bfd_check_format (ext_arch, bfd_archive)) 686 { 687 free (new_areldata); 688 return NULL; 689 } 690 n_bfd = _bfd_get_elt_at_filepos (ext_arch, new_areldata->origin); 691 if (n_bfd == NULL) 692 { 693 free (new_areldata); 694 return NULL; 695 } 696 n_bfd->proxy_origin = bfd_tell (archive); 697 return n_bfd; 698 } 699 700 /* It's not an element of a nested archive; 701 open the external file as a bfd. */ 702 n_bfd = open_nested_file (filename, archive); 703 if (n_bfd == NULL) 704 bfd_set_error (bfd_error_malformed_archive); 705 } 706 else 707 { 708 n_bfd = _bfd_create_empty_archive_element_shell (archive); 709 } 710 711 if (n_bfd == NULL) 712 { 713 free (new_areldata); 714 return NULL; 715 } 716 717 n_bfd->proxy_origin = bfd_tell (archive); 718 719 if (bfd_is_thin_archive (archive)) 720 { 721 n_bfd->origin = 0; 722 } 723 else 724 { 725 n_bfd->origin = n_bfd->proxy_origin; 726 n_bfd->filename = xstrdup (filename); 727 } 728 729 n_bfd->arelt_data = new_areldata; 730 731 /* Copy BFD_COMPRESS, BFD_DECOMPRESS and BFD_COMPRESS_GABI flags. */ 732 n_bfd->flags |= archive->flags & (BFD_COMPRESS 733 | BFD_DECOMPRESS 734 | BFD_COMPRESS_GABI); 735 736 /* Copy is_linker_input. */ 737 n_bfd->is_linker_input = archive->is_linker_input; 738 739 if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_bfd)) 740 return n_bfd; 741 742 free (new_areldata); 743 n_bfd->arelt_data = NULL; 744 return NULL; 745 } 746 747 /* Return the BFD which is referenced by the symbol in ABFD indexed by 748 SYM_INDEX. SYM_INDEX should have been returned by bfd_get_next_mapent. */ 749 750 bfd * 751 _bfd_generic_get_elt_at_index (bfd *abfd, symindex sym_index) 752 { 753 carsym *entry; 754 755 entry = bfd_ardata (abfd)->symdefs + sym_index; 756 return _bfd_get_elt_at_filepos (abfd, entry->file_offset); 757 } 758 759 /* 760 FUNCTION 761 bfd_openr_next_archived_file 762 763 SYNOPSIS 764 bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous); 765 766 DESCRIPTION 767 Provided a BFD, @var{archive}, containing an archive and NULL, open 768 an input BFD on the first contained element and returns that. 769 Subsequent calls should pass 770 the archive and the previous return value to return a created 771 BFD to the next contained element. NULL is returned when there 772 are no more. 773 */ 774 775 bfd * 776 bfd_openr_next_archived_file (bfd *archive, bfd *last_file) 777 { 778 if ((bfd_get_format (archive) != bfd_archive) 779 || (archive->direction == write_direction)) 780 { 781 bfd_set_error (bfd_error_invalid_operation); 782 return NULL; 783 } 784 785 return BFD_SEND (archive, 786 openr_next_archived_file, (archive, last_file)); 787 } 788 789 bfd * 790 bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file) 791 { 792 ufile_ptr filestart; 793 794 if (!last_file) 795 filestart = bfd_ardata (archive)->first_file_filepos; 796 else 797 { 798 filestart = last_file->proxy_origin; 799 if (! bfd_is_thin_archive (archive)) 800 #if 0 801 /* OLD CODE */ 802 filestart += size; 803 /* Pad to an even boundary... 804 Note that last_file->origin can be odd in the case of 805 BSD-4.4-style element with a long odd size. */ 806 if (!strncmp(arch_hdr (last_file)->ar_name, "#1/", 3)) 807 size += strlen(normalize(last_file, last_file->filename)); 808 filestart += size % 2; 809 #endif 810 { 811 bfd_size_type size = arelt_size (last_file); 812 813 filestart += size; 814 /* Pad to an even boundary... 815 Note that last_file->origin can be odd in the case of 816 BSD-4.4-style element with a long odd size. */ 817 filestart += filestart % 2; 818 if (filestart < last_file->proxy_origin) 819 { 820 /* Prevent looping. See PR19256. */ 821 bfd_set_error (bfd_error_malformed_archive); 822 return NULL; 823 } 824 } 825 } 826 827 return _bfd_get_elt_at_filepos (archive, filestart); 828 } 829 830 const bfd_target * 831 bfd_generic_archive_p (bfd *abfd) 832 { 833 struct artdata *tdata_hold; 834 char armag[SARMAG + 1]; 835 bfd_size_type amt; 836 837 if (bfd_bread (armag, SARMAG, abfd) != SARMAG) 838 { 839 if (bfd_get_error () != bfd_error_system_call) 840 bfd_set_error (bfd_error_wrong_format); 841 return NULL; 842 } 843 844 bfd_is_thin_archive (abfd) = (strncmp (armag, ARMAGT, SARMAG) == 0); 845 846 if (strncmp (armag, ARMAG, SARMAG) != 0 847 && strncmp (armag, ARMAGB, SARMAG) != 0 848 && ! bfd_is_thin_archive (abfd)) 849 return NULL; 850 851 tdata_hold = bfd_ardata (abfd); 852 853 amt = sizeof (struct artdata); 854 bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt); 855 if (bfd_ardata (abfd) == NULL) 856 { 857 bfd_ardata (abfd) = tdata_hold; 858 return NULL; 859 } 860 861 bfd_ardata (abfd)->first_file_filepos = SARMAG; 862 /* Cleared by bfd_zalloc above. 863 bfd_ardata (abfd)->cache = NULL; 864 bfd_ardata (abfd)->archive_head = NULL; 865 bfd_ardata (abfd)->symdefs = NULL; 866 bfd_ardata (abfd)->extended_names = NULL; 867 bfd_ardata (abfd)->extended_names_size = 0; 868 bfd_ardata (abfd)->tdata = NULL; */ 869 870 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd)) 871 || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) 872 { 873 if (bfd_get_error () != bfd_error_system_call) 874 bfd_set_error (bfd_error_wrong_format); 875 bfd_release (abfd, bfd_ardata (abfd)); 876 bfd_ardata (abfd) = tdata_hold; 877 return NULL; 878 } 879 880 if (abfd->target_defaulted && bfd_has_map (abfd)) 881 { 882 bfd *first; 883 884 /* This archive has a map, so we may presume that the contents 885 are object files. Make sure that if the first file in the 886 archive can be recognized as an object file, it is for this 887 target. If not, assume that this is the wrong format. If 888 the first file is not an object file, somebody is doing 889 something weird, and we permit it so that ar -t will work. 890 891 This is done because any normal format will recognize any 892 normal archive, regardless of the format of the object files. 893 We do accept an empty archive. */ 894 895 first = bfd_openr_next_archived_file (abfd, NULL); 896 if (first != NULL) 897 { 898 first->target_defaulted = FALSE; 899 if (bfd_check_format (first, bfd_object) 900 && first->xvec != abfd->xvec) 901 bfd_set_error (bfd_error_wrong_object_format); 902 /* And we ought to close `first' here too. */ 903 } 904 } 905 906 return abfd->xvec; 907 } 908 909 /* Some constants for a 32 bit BSD archive structure. We do not 910 support 64 bit archives presently; so far as I know, none actually 911 exist. Supporting them would require changing these constants, and 912 changing some H_GET_32 to H_GET_64. */ 913 914 /* The size of an external symdef structure. */ 915 #define BSD_SYMDEF_SIZE 8 916 917 /* The offset from the start of a symdef structure to the file offset. */ 918 #define BSD_SYMDEF_OFFSET_SIZE 4 919 920 /* The size of the symdef count. */ 921 #define BSD_SYMDEF_COUNT_SIZE 4 922 923 /* The size of the string count. */ 924 #define BSD_STRING_COUNT_SIZE 4 925 926 /* Read a BSD-style archive symbol table. Returns FALSE on error, 927 TRUE otherwise. */ 928 929 static bfd_boolean 930 do_slurp_bsd_armap (bfd *abfd) 931 { 932 struct areltdata *mapdata; 933 unsigned int counter; 934 bfd_byte *raw_armap, *rbase; 935 struct artdata *ardata = bfd_ardata (abfd); 936 char *stringbase; 937 bfd_size_type parsed_size, amt; 938 carsym *set; 939 940 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 941 if (mapdata == NULL) 942 return FALSE; 943 parsed_size = mapdata->parsed_size; 944 free (mapdata); 945 /* PR 17512: file: 883ff754. */ 946 /* PR 17512: file: 0458885f. */ 947 if (parsed_size < 4) 948 return FALSE; 949 950 raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size); 951 if (raw_armap == NULL) 952 return FALSE; 953 954 if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size) 955 { 956 if (bfd_get_error () != bfd_error_system_call) 957 bfd_set_error (bfd_error_malformed_archive); 958 byebye: 959 bfd_release (abfd, raw_armap); 960 return FALSE; 961 } 962 963 ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE; 964 if (ardata->symdef_count * BSD_SYMDEF_SIZE > 965 parsed_size - BSD_SYMDEF_COUNT_SIZE) 966 { 967 /* Probably we're using the wrong byte ordering. */ 968 bfd_set_error (bfd_error_wrong_format); 969 goto byebye; 970 } 971 972 ardata->cache = 0; 973 rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE; 974 stringbase = ((char *) rbase 975 + ardata->symdef_count * BSD_SYMDEF_SIZE 976 + BSD_STRING_COUNT_SIZE); 977 amt = ardata->symdef_count * sizeof (carsym); 978 ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt); 979 if (!ardata->symdefs) 980 return FALSE; 981 982 for (counter = 0, set = ardata->symdefs; 983 counter < ardata->symdef_count; 984 counter++, set++, rbase += BSD_SYMDEF_SIZE) 985 { 986 set->name = H_GET_32 (abfd, rbase) + stringbase; 987 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); 988 } 989 990 ardata->first_file_filepos = bfd_tell (abfd); 991 /* Pad to an even boundary if you have to. */ 992 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 993 /* FIXME, we should provide some way to free raw_ardata when 994 we are done using the strings from it. For now, it seems 995 to be allocated on an objalloc anyway... */ 996 bfd_has_map (abfd) = TRUE; 997 return TRUE; 998 } 999 1000 /* Read a COFF archive symbol table. Returns FALSE on error, TRUE 1001 otherwise. */ 1002 1003 static bfd_boolean 1004 do_slurp_coff_armap (bfd *abfd) 1005 { 1006 struct areltdata *mapdata; 1007 int *raw_armap, *rawptr; 1008 struct artdata *ardata = bfd_ardata (abfd); 1009 char *stringbase; 1010 bfd_size_type stringsize; 1011 bfd_size_type parsed_size; 1012 carsym *carsyms; 1013 bfd_size_type nsymz; /* Number of symbols in armap. */ 1014 bfd_vma (*swap) (const void *); 1015 char int_buf[sizeof (long)]; 1016 bfd_size_type carsym_size, ptrsize; 1017 unsigned int i; 1018 1019 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 1020 if (mapdata == NULL) 1021 return FALSE; 1022 parsed_size = mapdata->parsed_size; 1023 free (mapdata); 1024 1025 if (bfd_bread (int_buf, 4, abfd) != 4) 1026 { 1027 if (bfd_get_error () != bfd_error_system_call) 1028 bfd_set_error (bfd_error_malformed_archive); 1029 return FALSE; 1030 } 1031 /* It seems that all numeric information in a coff archive is always 1032 in big endian format, nomatter the host or target. */ 1033 swap = bfd_getb32; 1034 nsymz = bfd_getb32 (int_buf); 1035 stringsize = parsed_size - (4 * nsymz) - 4; 1036 1037 /* ... except that some archive formats are broken, and it may be our 1038 fault - the i960 little endian coff sometimes has big and sometimes 1039 little, because our tools changed. Here's a horrible hack to clean 1040 up the crap. */ 1041 1042 if (stringsize > 0xfffff 1043 && bfd_get_arch (abfd) == bfd_arch_i960 1044 && bfd_get_flavour (abfd) == bfd_target_coff_flavour) 1045 { 1046 /* This looks dangerous, let's do it the other way around. */ 1047 nsymz = bfd_getl32 (int_buf); 1048 stringsize = parsed_size - (4 * nsymz) - 4; 1049 swap = bfd_getl32; 1050 } 1051 1052 /* The coff armap must be read sequentially. So we construct a 1053 bsd-style one in core all at once, for simplicity. */ 1054 1055 if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym)) 1056 return FALSE; 1057 1058 carsym_size = (nsymz * sizeof (carsym)); 1059 ptrsize = (4 * nsymz); 1060 1061 if (carsym_size + stringsize + 1 <= carsym_size) 1062 return FALSE; 1063 1064 ardata->symdefs = (struct carsym *) bfd_zalloc (abfd, 1065 carsym_size + stringsize + 1); 1066 if (ardata->symdefs == NULL) 1067 return FALSE; 1068 carsyms = ardata->symdefs; 1069 stringbase = ((char *) ardata->symdefs) + carsym_size; 1070 1071 /* Allocate and read in the raw offsets. */ 1072 raw_armap = (int *) bfd_alloc (abfd, ptrsize); 1073 if (raw_armap == NULL) 1074 goto release_symdefs; 1075 if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize 1076 || (bfd_bread (stringbase, stringsize, abfd) != stringsize)) 1077 { 1078 if (bfd_get_error () != bfd_error_system_call) 1079 bfd_set_error (bfd_error_malformed_archive); 1080 goto release_raw_armap; 1081 } 1082 1083 /* OK, build the carsyms. */ 1084 for (i = 0; i < nsymz && stringsize > 0; i++) 1085 { 1086 bfd_size_type len; 1087 1088 rawptr = raw_armap + i; 1089 carsyms->file_offset = swap ((bfd_byte *) rawptr); 1090 carsyms->name = stringbase; 1091 /* PR 17512: file: 4a1d50c1. */ 1092 len = strnlen (stringbase, stringsize); 1093 if (len < stringsize) 1094 len ++; 1095 stringbase += len; 1096 stringsize -= len; 1097 carsyms++; 1098 } 1099 *stringbase = 0; 1100 1101 ardata->symdef_count = nsymz; 1102 ardata->first_file_filepos = bfd_tell (abfd); 1103 /* Pad to an even boundary if you have to. */ 1104 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 1105 1106 bfd_has_map (abfd) = TRUE; 1107 bfd_release (abfd, raw_armap); 1108 1109 /* Check for a second archive header (as used by PE). */ 1110 { 1111 struct areltdata *tmp; 1112 1113 bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET); 1114 tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd); 1115 if (tmp != NULL) 1116 { 1117 if (tmp->arch_header[0] == '/' 1118 && tmp->arch_header[1] == ' ') 1119 { 1120 ardata->first_file_filepos += 1121 (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1; 1122 } 1123 free (tmp); 1124 } 1125 } 1126 1127 return TRUE; 1128 1129 release_raw_armap: 1130 bfd_release (abfd, raw_armap); 1131 release_symdefs: 1132 bfd_release (abfd, (ardata)->symdefs); 1133 return FALSE; 1134 } 1135 1136 /* This routine can handle either coff-style or bsd-style armaps 1137 (archive symbol table). Returns FALSE on error, TRUE otherwise */ 1138 1139 bfd_boolean 1140 bfd_slurp_armap (bfd *abfd) 1141 { 1142 char nextname[17]; 1143 int i = bfd_bread (nextname, 16, abfd); 1144 1145 if (i == 0) 1146 return TRUE; 1147 if (i != 16) 1148 return FALSE; 1149 1150 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 1151 return FALSE; 1152 1153 if (CONST_STRNEQ (nextname, "__.SYMDEF ") 1154 || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */ 1155 return do_slurp_bsd_armap (abfd); 1156 else if (CONST_STRNEQ (nextname, "/ ")) 1157 return do_slurp_coff_armap (abfd); 1158 else if (CONST_STRNEQ (nextname, "/SYM64/ ")) 1159 { 1160 /* 64bit ELF (Irix 6) archive. */ 1161 #ifdef BFD64 1162 extern bfd_boolean bfd_elf64_archive_slurp_armap (bfd *); 1163 return bfd_elf64_archive_slurp_armap (abfd); 1164 #else 1165 bfd_set_error (bfd_error_wrong_format); 1166 return FALSE; 1167 #endif 1168 } 1169 else if (CONST_STRNEQ (nextname, "#1/20 ")) 1170 { 1171 /* Mach-O has a special name for armap when the map is sorted by name. 1172 However because this name has a space it is slightly more difficult 1173 to check it. */ 1174 struct ar_hdr hdr; 1175 char extname[21]; 1176 1177 if (bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr)) 1178 return FALSE; 1179 /* Read the extended name. We know its length. */ 1180 if (bfd_bread (extname, 20, abfd) != 20) 1181 return FALSE; 1182 if (bfd_seek (abfd, -(file_ptr) (sizeof (hdr) + 20), SEEK_CUR) != 0) 1183 return FALSE; 1184 extname[20] = 0; 1185 if (CONST_STRNEQ (extname, "__.SYMDEF SORTED") 1186 || CONST_STRNEQ (extname, "__.SYMDEF")) 1187 return do_slurp_bsd_armap (abfd); 1188 } 1189 1190 bfd_has_map (abfd) = FALSE; 1191 return TRUE; 1192 } 1193 1194 /* Returns FALSE on error, TRUE otherwise. */ 1195 /* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the 1196 header is in a slightly different order and the map name is '/'. 1197 This flavour is used by hp300hpux. */ 1198 1199 #define HPUX_SYMDEF_COUNT_SIZE 2 1200 1201 bfd_boolean 1202 bfd_slurp_bsd_armap_f2 (bfd *abfd) 1203 { 1204 struct areltdata *mapdata; 1205 char nextname[17]; 1206 unsigned int counter; 1207 bfd_byte *raw_armap, *rbase; 1208 struct artdata *ardata = bfd_ardata (abfd); 1209 char *stringbase; 1210 unsigned int stringsize; 1211 unsigned int left; 1212 bfd_size_type amt; 1213 carsym *set; 1214 int i = bfd_bread (nextname, 16, abfd); 1215 1216 if (i == 0) 1217 return TRUE; 1218 if (i != 16) 1219 return FALSE; 1220 1221 /* The archive has at least 16 bytes in it. */ 1222 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 1223 return FALSE; 1224 1225 if (CONST_STRNEQ (nextname, "__.SYMDEF ") 1226 || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */ 1227 return do_slurp_bsd_armap (abfd); 1228 1229 if (! CONST_STRNEQ (nextname, "/ ")) 1230 { 1231 bfd_has_map (abfd) = FALSE; 1232 return TRUE; 1233 } 1234 1235 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 1236 if (mapdata == NULL) 1237 return FALSE; 1238 1239 if (mapdata->parsed_size < HPUX_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE) 1240 { 1241 free (mapdata); 1242 wrong_format: 1243 bfd_set_error (bfd_error_wrong_format); 1244 byebye: 1245 return FALSE; 1246 } 1247 left = mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE - BSD_STRING_COUNT_SIZE; 1248 1249 amt = mapdata->parsed_size; 1250 free (mapdata); 1251 1252 raw_armap = (bfd_byte *) bfd_zalloc (abfd, amt); 1253 if (raw_armap == NULL) 1254 goto byebye; 1255 1256 if (bfd_bread (raw_armap, amt, abfd) != amt) 1257 { 1258 if (bfd_get_error () != bfd_error_system_call) 1259 bfd_set_error (bfd_error_malformed_archive); 1260 goto byebye; 1261 } 1262 1263 ardata->symdef_count = H_GET_16 (abfd, raw_armap); 1264 1265 ardata->cache = 0; 1266 1267 stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE); 1268 if (stringsize > left) 1269 goto wrong_format; 1270 left -= stringsize; 1271 1272 /* Skip sym count and string sz. */ 1273 stringbase = ((char *) raw_armap 1274 + HPUX_SYMDEF_COUNT_SIZE 1275 + BSD_STRING_COUNT_SIZE); 1276 rbase = (bfd_byte *) stringbase + stringsize; 1277 amt = ardata->symdef_count * BSD_SYMDEF_SIZE; 1278 if (amt > left) 1279 goto wrong_format; 1280 1281 ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt); 1282 if (!ardata->symdefs) 1283 return FALSE; 1284 1285 for (counter = 0, set = ardata->symdefs; 1286 counter < ardata->symdef_count; 1287 counter++, set++, rbase += BSD_SYMDEF_SIZE) 1288 { 1289 set->name = H_GET_32 (abfd, rbase) + stringbase; 1290 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); 1291 } 1292 1293 ardata->first_file_filepos = bfd_tell (abfd); 1294 /* Pad to an even boundary if you have to. */ 1295 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 1296 /* FIXME, we should provide some way to free raw_ardata when 1297 we are done using the strings from it. For now, it seems 1298 to be allocated on an objalloc anyway... */ 1299 bfd_has_map (abfd) = TRUE; 1300 return TRUE; 1301 } 1302 1303 /** Extended name table. 1304 1305 Normally archives support only 14-character filenames. 1306 1307 Intel has extended the format: longer names are stored in a special 1308 element (the first in the archive, or second if there is an armap); 1309 the name in the ar_hdr is replaced by <space><index into filename 1310 element>. Index is the P.R. of an int (decimal). Data General have 1311 extended the format by using the prefix // for the special element. */ 1312 1313 /* Returns FALSE on error, TRUE otherwise. */ 1314 1315 bfd_boolean 1316 _bfd_slurp_extended_name_table (bfd *abfd) 1317 { 1318 char nextname[17]; 1319 struct areltdata *namedata; 1320 bfd_size_type amt; 1321 1322 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ, 1323 we probably don't want to return TRUE. */ 1324 if (bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET) != 0) 1325 return FALSE; 1326 1327 if (bfd_bread (nextname, 16, abfd) == 16) 1328 { 1329 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 1330 return FALSE; 1331 1332 if (! CONST_STRNEQ (nextname, "ARFILENAMES/ ") 1333 && ! CONST_STRNEQ (nextname, "// ")) 1334 { 1335 bfd_ardata (abfd)->extended_names = NULL; 1336 bfd_ardata (abfd)->extended_names_size = 0; 1337 return TRUE; 1338 } 1339 1340 namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 1341 if (namedata == NULL) 1342 return FALSE; 1343 1344 amt = namedata->parsed_size; 1345 if (amt + 1 == 0) 1346 goto byebye; 1347 1348 bfd_ardata (abfd)->extended_names_size = amt; 1349 bfd_ardata (abfd)->extended_names = (char *) bfd_zalloc (abfd, amt + 1); 1350 if (bfd_ardata (abfd)->extended_names == NULL) 1351 { 1352 byebye: 1353 free (namedata); 1354 bfd_ardata (abfd)->extended_names = NULL; 1355 bfd_ardata (abfd)->extended_names_size = 0; 1356 return FALSE; 1357 } 1358 1359 if (bfd_bread (bfd_ardata (abfd)->extended_names, amt, abfd) != amt) 1360 { 1361 if (bfd_get_error () != bfd_error_system_call) 1362 bfd_set_error (bfd_error_malformed_archive); 1363 bfd_release (abfd, (bfd_ardata (abfd)->extended_names)); 1364 bfd_ardata (abfd)->extended_names = NULL; 1365 goto byebye; 1366 } 1367 1368 /* Since the archive is supposed to be printable if it contains 1369 text, the entries in the list are newline-padded, not null 1370 padded. In SVR4-style archives, the names also have a 1371 trailing '/'. DOS/NT created archive often have \ in them 1372 We'll fix all problems here. */ 1373 { 1374 char *ext_names = bfd_ardata (abfd)->extended_names; 1375 char *temp = ext_names; 1376 char *limit = temp + namedata->parsed_size; 1377 1378 for (; temp < limit; ++temp) 1379 { 1380 if (*temp == ARFMAG[1]) 1381 temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0'; 1382 if (*temp == '\\') 1383 *temp = '/'; 1384 } 1385 *limit = '\0'; 1386 } 1387 1388 /* Pad to an even boundary if you have to. */ 1389 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd); 1390 bfd_ardata (abfd)->first_file_filepos += 1391 (bfd_ardata (abfd)->first_file_filepos) % 2; 1392 1393 free (namedata); 1394 } 1395 return TRUE; 1396 } 1397 1398 #ifdef VMS 1399 1400 /* Return a copy of the stuff in the filename between any :]> and a 1401 semicolon. */ 1402 1403 static const char * 1404 normalize (bfd *abfd, const char *file) 1405 { 1406 const char *first; 1407 const char *last; 1408 char *copy; 1409 1410 first = file + strlen (file) - 1; 1411 last = first + 1; 1412 1413 while (first != file) 1414 { 1415 if (*first == ';') 1416 last = first; 1417 if (*first == ':' || *first == ']' || *first == '>') 1418 { 1419 first++; 1420 break; 1421 } 1422 first--; 1423 } 1424 1425 copy = bfd_alloc (abfd, last - first + 1); 1426 if (copy == NULL) 1427 return NULL; 1428 1429 memcpy (copy, first, last - first); 1430 copy[last - first] = 0; 1431 1432 return copy; 1433 } 1434 1435 #else 1436 static const char * 1437 normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file) 1438 { 1439 return lbasename (file); 1440 } 1441 #endif 1442 1443 /* Adjust a relative path name based on the reference path. 1444 For example: 1445 1446 Relative path Reference path Result 1447 ------------- -------------- ------ 1448 bar.o lib.a bar.o 1449 foo/bar.o lib.a foo/bar.o 1450 bar.o foo/lib.a ../bar.o 1451 foo/bar.o baz/lib.a ../foo/bar.o 1452 bar.o ../lib.a <parent of current dir>/bar.o 1453 ; ../bar.o ../lib.a bar.o 1454 ; ../bar.o lib.a ../bar.o 1455 foo/bar.o ../lib.a <parent of current dir>/foo/bar.o 1456 bar.o ../../lib.a <grandparent>/<parent>/bar.o 1457 bar.o foo/baz/lib.a ../../bar.o 1458 1459 Note - the semicolons above are there to prevent the BFD chew 1460 utility from interpreting those lines as prototypes to put into 1461 the autogenerated bfd.h header... 1462 1463 Note - the string is returned in a static buffer. */ 1464 1465 static const char * 1466 adjust_relative_path (const char * path, const char * ref_path) 1467 { 1468 static char *pathbuf = NULL; 1469 static unsigned int pathbuf_len = 0; 1470 const char *pathp; 1471 const char *refp; 1472 char * lpath; 1473 char * rpath; 1474 unsigned int len; 1475 unsigned int dir_up = 0; 1476 unsigned int dir_down = 0; 1477 char *newp; 1478 char * pwd = getpwd (); 1479 const char * down; 1480 1481 /* Remove symlinks, '.' and '..' from the paths, if possible. */ 1482 lpath = lrealpath (path); 1483 pathp = lpath == NULL ? path : lpath; 1484 1485 rpath = lrealpath (ref_path); 1486 refp = rpath == NULL ? ref_path : rpath; 1487 1488 /* Remove common leading path elements. */ 1489 for (;;) 1490 { 1491 const char *e1 = pathp; 1492 const char *e2 = refp; 1493 1494 while (*e1 && ! IS_DIR_SEPARATOR (*e1)) 1495 ++e1; 1496 while (*e2 && ! IS_DIR_SEPARATOR (*e2)) 1497 ++e2; 1498 if (*e1 == '\0' || *e2 == '\0' || e1 - pathp != e2 - refp 1499 || filename_ncmp (pathp, refp, e1 - pathp) != 0) 1500 break; 1501 pathp = e1 + 1; 1502 refp = e2 + 1; 1503 } 1504 1505 len = strlen (pathp) + 1; 1506 /* For each leading path element in the reference path, 1507 insert "../" into the path. */ 1508 for (; *refp; ++refp) 1509 if (IS_DIR_SEPARATOR (*refp)) 1510 { 1511 /* PR 12710: If the path element is "../" then instead of 1512 inserting "../" we need to insert the name of the directory 1513 at the current level. */ 1514 if (refp > ref_path + 1 1515 && refp[-1] == '.' 1516 && refp[-2] == '.') 1517 dir_down ++; 1518 else 1519 dir_up ++; 1520 } 1521 1522 /* If the lrealpath calls above succeeded then we should never 1523 see dir_up and dir_down both being non-zero. */ 1524 1525 len += 3 * dir_up; 1526 1527 if (dir_down) 1528 { 1529 down = pwd + strlen (pwd) - 1; 1530 1531 while (dir_down && down > pwd) 1532 { 1533 if (IS_DIR_SEPARATOR (*down)) 1534 --dir_down; 1535 } 1536 BFD_ASSERT (dir_down == 0); 1537 len += strlen (down) + 1; 1538 } 1539 else 1540 down = NULL; 1541 1542 if (len > pathbuf_len) 1543 { 1544 if (pathbuf != NULL) 1545 free (pathbuf); 1546 pathbuf_len = 0; 1547 pathbuf = (char *) bfd_malloc (len); 1548 if (pathbuf == NULL) 1549 goto out; 1550 pathbuf_len = len; 1551 } 1552 1553 newp = pathbuf; 1554 while (dir_up-- > 0) 1555 { 1556 /* FIXME: Support Windows style path separators as well. */ 1557 strcpy (newp, "../"); 1558 newp += 3; 1559 } 1560 1561 if (down) 1562 sprintf (newp, "%s/%s", down, pathp); 1563 else 1564 strcpy (newp, pathp); 1565 1566 out: 1567 free (lpath); 1568 free (rpath); 1569 return pathbuf; 1570 } 1571 1572 /* Build a BFD style extended name table. */ 1573 1574 bfd_boolean 1575 _bfd_archive_bsd_construct_extended_name_table (bfd *abfd, 1576 char **tabloc, 1577 bfd_size_type *tablen, 1578 const char **name) 1579 { 1580 *name = "ARFILENAMES/"; 1581 return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen); 1582 } 1583 1584 /* Build an SVR4 style extended name table. */ 1585 1586 bfd_boolean 1587 _bfd_archive_coff_construct_extended_name_table (bfd *abfd, 1588 char **tabloc, 1589 bfd_size_type *tablen, 1590 const char **name) 1591 { 1592 *name = "//"; 1593 return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen); 1594 } 1595 1596 /* Follows archive_head and produces an extended name table if 1597 necessary. Returns (in tabloc) a pointer to an extended name 1598 table, and in tablen the length of the table. If it makes an entry 1599 it clobbers the filename so that the element may be written without 1600 further massage. Returns TRUE if it ran successfully, FALSE if 1601 something went wrong. A successful return may still involve a 1602 zero-length tablen! */ 1603 1604 bfd_boolean 1605 _bfd_construct_extended_name_table (bfd *abfd, 1606 bfd_boolean trailing_slash, 1607 char **tabloc, 1608 bfd_size_type *tablen) 1609 { 1610 unsigned int maxname = ar_maxnamelen (abfd); 1611 bfd_size_type total_namelen = 0; 1612 bfd *current; 1613 char *strptr; 1614 const char *last_filename; 1615 long last_stroff; 1616 1617 *tablen = 0; 1618 last_filename = NULL; 1619 1620 /* Figure out how long the table should be. */ 1621 for (current = abfd->archive_head; 1622 current != NULL; 1623 current = current->archive_next) 1624 { 1625 const char *normal; 1626 unsigned int thislen; 1627 1628 if (bfd_is_thin_archive (abfd)) 1629 { 1630 const char *filename = current->filename; 1631 1632 /* If the element being added is a member of another archive 1633 (i.e., we are flattening), use the containing archive's name. */ 1634 if (current->my_archive 1635 && ! bfd_is_thin_archive (current->my_archive)) 1636 filename = current->my_archive->filename; 1637 1638 /* If the path is the same as the previous path seen, 1639 reuse it. This can happen when flattening a thin 1640 archive that contains other archives. */ 1641 if (last_filename && filename_cmp (last_filename, filename) == 0) 1642 continue; 1643 1644 last_filename = filename; 1645 1646 /* If the path is relative, adjust it relative to 1647 the containing archive. */ 1648 if (! IS_ABSOLUTE_PATH (filename) 1649 && ! IS_ABSOLUTE_PATH (abfd->filename)) 1650 normal = adjust_relative_path (filename, abfd->filename); 1651 else 1652 normal = filename; 1653 1654 /* In a thin archive, always store the full pathname 1655 in the extended name table. */ 1656 total_namelen += strlen (normal) + 1; 1657 if (trailing_slash) 1658 /* Leave room for trailing slash. */ 1659 ++total_namelen; 1660 1661 continue; 1662 } 1663 1664 normal = normalize (current, current->filename); 1665 if (normal == NULL) 1666 return FALSE; 1667 1668 thislen = strlen (normal); 1669 1670 if (thislen > maxname 1671 && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0) 1672 thislen = maxname; 1673 1674 if (thislen > maxname) 1675 { 1676 /* Add one to leave room for \n. */ 1677 total_namelen += thislen + 1; 1678 if (trailing_slash) 1679 { 1680 /* Leave room for trailing slash. */ 1681 ++total_namelen; 1682 } 1683 } 1684 else 1685 { 1686 struct ar_hdr *hdr = arch_hdr (current); 1687 if (filename_ncmp (normal, hdr->ar_name, thislen) != 0 1688 || (thislen < sizeof hdr->ar_name 1689 && hdr->ar_name[thislen] != ar_padchar (current))) 1690 { 1691 /* Must have been using extended format even though it 1692 didn't need to. Fix it to use normal format. */ 1693 memcpy (hdr->ar_name, normal, thislen); 1694 if (thislen < maxname 1695 || (thislen == maxname && thislen < sizeof hdr->ar_name)) 1696 hdr->ar_name[thislen] = ar_padchar (current); 1697 } 1698 } 1699 } 1700 1701 if (total_namelen == 0) 1702 return TRUE; 1703 1704 *tabloc = (char *) bfd_zalloc (abfd, total_namelen); 1705 if (*tabloc == NULL) 1706 return FALSE; 1707 1708 *tablen = total_namelen; 1709 strptr = *tabloc; 1710 1711 last_filename = NULL; 1712 last_stroff = 0; 1713 1714 for (current = abfd->archive_head; 1715 current != NULL; 1716 current = current->archive_next) 1717 { 1718 const char *normal; 1719 unsigned int thislen; 1720 long stroff; 1721 const char *filename = current->filename; 1722 1723 if (bfd_is_thin_archive (abfd)) 1724 { 1725 /* If the element being added is a member of another archive 1726 (i.e., we are flattening), use the containing archive's name. */ 1727 if (current->my_archive 1728 && ! bfd_is_thin_archive (current->my_archive)) 1729 filename = current->my_archive->filename; 1730 /* If the path is the same as the previous path seen, 1731 reuse it. This can happen when flattening a thin 1732 archive that contains other archives. 1733 If the path is relative, adjust it relative to 1734 the containing archive. */ 1735 if (last_filename && filename_cmp (last_filename, filename) == 0) 1736 normal = last_filename; 1737 else if (! IS_ABSOLUTE_PATH (filename) 1738 && ! IS_ABSOLUTE_PATH (abfd->filename)) 1739 normal = adjust_relative_path (filename, abfd->filename); 1740 else 1741 normal = filename; 1742 } 1743 else 1744 { 1745 normal = normalize (current, filename); 1746 if (normal == NULL) 1747 return FALSE; 1748 } 1749 1750 thislen = strlen (normal); 1751 if (thislen > maxname || bfd_is_thin_archive (abfd)) 1752 { 1753 /* Works for now; may need to be re-engineered if we 1754 encounter an oddball archive format and want to 1755 generalise this hack. */ 1756 struct ar_hdr *hdr = arch_hdr (current); 1757 if (normal == last_filename) 1758 stroff = last_stroff; 1759 else 1760 { 1761 strcpy (strptr, normal); 1762 if (! trailing_slash) 1763 strptr[thislen] = ARFMAG[1]; 1764 else 1765 { 1766 strptr[thislen] = '/'; 1767 strptr[thislen + 1] = ARFMAG[1]; 1768 } 1769 stroff = strptr - *tabloc; 1770 last_stroff = stroff; 1771 } 1772 hdr->ar_name[0] = ar_padchar (current); 1773 if (bfd_is_thin_archive (abfd) && current->origin > 0) 1774 { 1775 int len = snprintf (hdr->ar_name + 1, maxname - 1, "%-ld:", 1776 stroff); 1777 _bfd_ar_spacepad (hdr->ar_name + 1 + len, maxname - 1 - len, 1778 "%-ld", 1779 current->origin - sizeof (struct ar_hdr)); 1780 } 1781 else 1782 _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", stroff); 1783 if (normal != last_filename) 1784 { 1785 strptr += thislen + 1; 1786 if (trailing_slash) 1787 ++strptr; 1788 last_filename = filename; 1789 } 1790 } 1791 } 1792 1793 return TRUE; 1794 } 1795 1796 /* Do not construct an extended name table but transforms name field into 1797 its extended form. */ 1798 1799 bfd_boolean 1800 _bfd_archive_bsd44_construct_extended_name_table (bfd *abfd, 1801 char **tabloc, 1802 bfd_size_type *tablen, 1803 const char **name) 1804 { 1805 unsigned int maxname = ar_maxnamelen (abfd); 1806 bfd *current; 1807 1808 *tablen = 0; 1809 *tabloc = NULL; 1810 *name = NULL; 1811 1812 for (current = abfd->archive_head; 1813 current != NULL; 1814 current = current->archive_next) 1815 { 1816 const char *normal = normalize (current, current->filename); 1817 int has_space = 0; 1818 unsigned int len; 1819 1820 if (normal == NULL) 1821 return FALSE; 1822 1823 for (len = 0; normal[len]; len++) 1824 if (normal[len] == ' ') 1825 has_space = 1; 1826 1827 if (len > maxname || has_space) 1828 { 1829 struct ar_hdr *hdr = arch_hdr (current); 1830 1831 len = (len + 3) & ~3; 1832 arch_eltdata (current)->extra_size = len; 1833 _bfd_ar_spacepad (hdr->ar_name, maxname, "#1/%lu", len); 1834 } 1835 } 1836 1837 return TRUE; 1838 } 1839 1840 /* Write an archive header. */ 1841 1842 bfd_boolean 1843 _bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd) 1844 { 1845 struct ar_hdr *hdr = arch_hdr (abfd); 1846 1847 if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr)) 1848 return FALSE; 1849 return TRUE; 1850 } 1851 1852 /* Write an archive header using BSD4.4 convention. */ 1853 1854 bfd_boolean 1855 _bfd_bsd44_write_ar_hdr (bfd *archive, bfd *abfd) 1856 { 1857 struct ar_hdr *hdr = arch_hdr (abfd); 1858 1859 if (is_bsd44_extended_name (hdr->ar_name)) 1860 { 1861 /* This is a BSD 4.4 extended name. */ 1862 const char *fullname = normalize (abfd, abfd->filename); 1863 unsigned int len = strlen (fullname); 1864 unsigned int padded_len = (len + 3) & ~3; 1865 1866 BFD_ASSERT (padded_len == arch_eltdata (abfd)->extra_size); 1867 1868 if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size), 1869 arch_eltdata (abfd)->parsed_size + padded_len)) 1870 return FALSE; 1871 1872 if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr)) 1873 return FALSE; 1874 1875 if (bfd_bwrite (fullname, len, archive) != len) 1876 return FALSE; 1877 1878 if (len & 3) 1879 { 1880 static const char pad[3] = { 0, 0, 0 }; 1881 1882 len = 4 - (len & 3); 1883 if (bfd_bwrite (pad, len, archive) != len) 1884 return FALSE; 1885 } 1886 } 1887 else 1888 { 1889 if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr)) 1890 return FALSE; 1891 } 1892 return TRUE; 1893 } 1894 1895 /* A couple of functions for creating ar_hdrs. */ 1896 1897 #ifdef HPUX_LARGE_AR_IDS 1898 /* Function to encode large UID/GID values according to HP. */ 1899 1900 static void 1901 hpux_uid_gid_encode (char str[6], long int id) 1902 { 1903 int cnt; 1904 1905 str[5] = '@' + (id & 3); 1906 id >>= 2; 1907 1908 for (cnt = 4; cnt >= 0; --cnt, id >>= 6) 1909 str[cnt] = ' ' + (id & 0x3f); 1910 } 1911 #endif /* HPUX_LARGE_AR_IDS */ 1912 1913 #ifndef HAVE_GETUID 1914 #define getuid() 0 1915 #endif 1916 1917 #ifndef HAVE_GETGID 1918 #define getgid() 0 1919 #endif 1920 1921 /* Takes a filename, returns an arelt_data for it, or NULL if it can't 1922 make one. The filename must refer to a filename in the filesystem. 1923 The filename field of the ar_hdr will NOT be initialized. If member 1924 is set, and it's an in-memory bfd, we fake it. */ 1925 1926 static struct areltdata * 1927 bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member) 1928 { 1929 struct stat status; 1930 struct areltdata *ared; 1931 struct ar_hdr *hdr; 1932 bfd_size_type amt; 1933 1934 if (member && (member->flags & BFD_IN_MEMORY) != 0) 1935 { 1936 /* Assume we just "made" the member, and fake it. */ 1937 struct bfd_in_memory *bim = (struct bfd_in_memory *) member->iostream; 1938 time (&status.st_mtime); 1939 status.st_uid = getuid (); 1940 status.st_gid = getgid (); 1941 status.st_mode = 0644; 1942 status.st_size = bim->size; 1943 } 1944 else if (stat (filename, &status) != 0) 1945 { 1946 bfd_set_error (bfd_error_system_call); 1947 return NULL; 1948 } 1949 1950 /* If the caller requested that the BFD generate deterministic output, 1951 fake values for modification time, UID, GID, and file mode. */ 1952 if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0) 1953 { 1954 status.st_mtime = 0; 1955 status.st_uid = 0; 1956 status.st_gid = 0; 1957 status.st_mode = 0644; 1958 } 1959 1960 amt = sizeof (struct ar_hdr) + sizeof (struct areltdata); 1961 ared = (struct areltdata *) bfd_zmalloc (amt); 1962 if (ared == NULL) 1963 return NULL; 1964 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata)); 1965 1966 /* ar headers are space padded, not null padded! */ 1967 memset (hdr, ' ', sizeof (struct ar_hdr)); 1968 1969 _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld", 1970 status.st_mtime); 1971 #ifdef HPUX_LARGE_AR_IDS 1972 /* HP has a very "special" way to handle UID/GID's with numeric values 1973 > 99999. */ 1974 if (status.st_uid > 99999) 1975 hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_uid); 1976 else 1977 #endif 1978 _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld", 1979 status.st_uid); 1980 #ifdef HPUX_LARGE_AR_IDS 1981 /* HP has a very "special" way to handle UID/GID's with numeric values 1982 > 99999. */ 1983 if (status.st_gid > 99999) 1984 hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_gid); 1985 else 1986 #endif 1987 _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld", 1988 status.st_gid); 1989 _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo", 1990 status.st_mode); 1991 if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size), status.st_size)) 1992 { 1993 free (ared); 1994 return NULL; 1995 } 1996 memcpy (hdr->ar_fmag, ARFMAG, 2); 1997 ared->parsed_size = status.st_size; 1998 ared->arch_header = (char *) hdr; 1999 2000 return ared; 2001 } 2002 2003 /* Analogous to stat call. */ 2004 2005 int 2006 bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf) 2007 { 2008 struct ar_hdr *hdr; 2009 char *aloser; 2010 2011 if (abfd->arelt_data == NULL) 2012 { 2013 bfd_set_error (bfd_error_invalid_operation); 2014 return -1; 2015 } 2016 2017 hdr = arch_hdr (abfd); 2018 /* PR 17512: file: 3d9e9fe9. */ 2019 if (hdr == NULL) 2020 return -1; 2021 #define foo(arelt, stelt, size) \ 2022 buf->stelt = strtol (hdr->arelt, &aloser, size); \ 2023 if (aloser == hdr->arelt) \ 2024 return -1; 2025 2026 /* Some platforms support special notations for large IDs. */ 2027 #ifdef HPUX_LARGE_AR_IDS 2028 # define foo2(arelt, stelt, size) \ 2029 if (hdr->arelt[5] == ' ') \ 2030 { \ 2031 foo (arelt, stelt, size); \ 2032 } \ 2033 else \ 2034 { \ 2035 int cnt; \ 2036 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \ 2037 { \ 2038 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \ 2039 return -1; \ 2040 buf->stelt <<= 6; \ 2041 buf->stelt += hdr->arelt[cnt] - ' '; \ 2042 } \ 2043 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \ 2044 return -1; \ 2045 buf->stelt <<= 2; \ 2046 buf->stelt += hdr->arelt[5] - '@'; \ 2047 } 2048 #else 2049 # define foo2(arelt, stelt, size) foo (arelt, stelt, size) 2050 #endif 2051 2052 foo (ar_date, st_mtime, 10); 2053 foo2 (ar_uid, st_uid, 10); 2054 foo2 (ar_gid, st_gid, 10); 2055 foo (ar_mode, st_mode, 8); 2056 2057 buf->st_size = arch_eltdata (abfd)->parsed_size; 2058 2059 return 0; 2060 } 2061 2062 void 2063 bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr) 2064 { 2065 /* FIXME: This interacts unpleasantly with ar's quick-append option. 2066 Fortunately ic960 users will never use that option. Fixing this 2067 is very hard; fortunately I know how to do it and will do so once 2068 intel's release is out the door. */ 2069 2070 struct ar_hdr *hdr = (struct ar_hdr *) arhdr; 2071 size_t length; 2072 const char *filename; 2073 size_t maxlen = ar_maxnamelen (abfd); 2074 2075 if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0) 2076 { 2077 bfd_bsd_truncate_arname (abfd, pathname, arhdr); 2078 return; 2079 } 2080 2081 filename = normalize (abfd, pathname); 2082 if (filename == NULL) 2083 { 2084 /* FIXME */ 2085 abort (); 2086 } 2087 2088 length = strlen (filename); 2089 2090 if (length <= maxlen) 2091 memcpy (hdr->ar_name, filename, length); 2092 2093 /* Add the padding character if there is room for it. */ 2094 if (length < maxlen 2095 || (length == maxlen && length < sizeof hdr->ar_name)) 2096 (hdr->ar_name)[length] = ar_padchar (abfd); 2097 } 2098 2099 void 2100 bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr) 2101 { 2102 struct ar_hdr *hdr = (struct ar_hdr *) arhdr; 2103 size_t length; 2104 const char *filename = lbasename (pathname); 2105 size_t maxlen = ar_maxnamelen (abfd); 2106 2107 length = strlen (filename); 2108 2109 if (length <= maxlen) 2110 memcpy (hdr->ar_name, filename, length); 2111 else 2112 { 2113 /* pathname: meet procrustes */ 2114 memcpy (hdr->ar_name, filename, maxlen); 2115 length = maxlen; 2116 } 2117 2118 if (length < maxlen) 2119 (hdr->ar_name)[length] = ar_padchar (abfd); 2120 } 2121 2122 /* Store name into ar header. Truncates the name to fit. 2123 1> strip pathname to be just the basename. 2124 2> if it's short enuf to fit, stuff it in. 2125 3> If it doesn't end with .o, truncate it to fit 2126 4> truncate it before the .o, append .o, stuff THAT in. */ 2127 2128 /* This is what gnu ar does. It's better but incompatible with the 2129 bsd ar. */ 2130 2131 void 2132 bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr) 2133 { 2134 struct ar_hdr *hdr = (struct ar_hdr *) arhdr; 2135 size_t length; 2136 const char *filename = lbasename (pathname); 2137 size_t maxlen = ar_maxnamelen (abfd); 2138 2139 length = strlen (filename); 2140 2141 if (length <= maxlen) 2142 memcpy (hdr->ar_name, filename, length); 2143 else 2144 { 2145 /* pathname: meet procrustes. */ 2146 memcpy (hdr->ar_name, filename, maxlen); 2147 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) 2148 { 2149 hdr->ar_name[maxlen - 2] = '.'; 2150 hdr->ar_name[maxlen - 1] = 'o'; 2151 } 2152 length = maxlen; 2153 } 2154 2155 if (length < 16) 2156 (hdr->ar_name)[length] = ar_padchar (abfd); 2157 } 2158 2159 /* The BFD is open for write and has its format set to bfd_archive. */ 2160 2161 bfd_boolean 2162 _bfd_write_archive_contents (bfd *arch) 2163 { 2164 bfd *current; 2165 char *etable = NULL; 2166 bfd_size_type elength = 0; 2167 const char *ename = NULL; 2168 bfd_boolean makemap = bfd_has_map (arch); 2169 /* If no .o's, don't bother to make a map. */ 2170 bfd_boolean hasobjects = FALSE; 2171 bfd_size_type wrote; 2172 int tries; 2173 char *armag; 2174 2175 /* Verify the viability of all entries; if any of them live in the 2176 filesystem (as opposed to living in an archive open for input) 2177 then construct a fresh ar_hdr for them. */ 2178 for (current = arch->archive_head; 2179 current != NULL; 2180 current = current->archive_next) 2181 { 2182 /* This check is checking the bfds for the objects we're reading 2183 from (which are usually either an object file or archive on 2184 disk), not the archive entries we're writing to. We don't 2185 actually create bfds for the archive members, we just copy 2186 them byte-wise when we write out the archive. */ 2187 if (bfd_write_p (current)) 2188 { 2189 bfd_set_error (bfd_error_invalid_operation); 2190 goto input_err; 2191 } 2192 if (!current->arelt_data) 2193 { 2194 current->arelt_data = 2195 bfd_ar_hdr_from_filesystem (arch, current->filename, current); 2196 if (!current->arelt_data) 2197 goto input_err; 2198 2199 /* Put in the file name. */ 2200 BFD_SEND (arch, _bfd_truncate_arname, 2201 (arch, current->filename, (char *) arch_hdr (current))); 2202 } 2203 2204 if (makemap && ! hasobjects) 2205 { /* Don't bother if we won't make a map! */ 2206 if ((bfd_check_format (current, bfd_object))) 2207 hasobjects = TRUE; 2208 } 2209 } 2210 2211 if (!BFD_SEND (arch, _bfd_construct_extended_name_table, 2212 (arch, &etable, &elength, &ename))) 2213 return FALSE; 2214 2215 if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0) 2216 return FALSE; 2217 armag = ARMAG; 2218 if (bfd_is_thin_archive (arch)) 2219 armag = ARMAGT; 2220 wrote = bfd_bwrite (armag, SARMAG, arch); 2221 if (wrote != SARMAG) 2222 return FALSE; 2223 2224 if (makemap && hasobjects) 2225 { 2226 if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength)) 2227 return FALSE; 2228 } 2229 2230 if (elength != 0) 2231 { 2232 struct ar_hdr hdr; 2233 2234 memset (&hdr, ' ', sizeof (struct ar_hdr)); 2235 memcpy (hdr.ar_name, ename, strlen (ename)); 2236 /* Round size up to even number in archive header. */ 2237 if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), 2238 (elength + 1) & ~(bfd_size_type) 1)) 2239 return FALSE; 2240 memcpy (hdr.ar_fmag, ARFMAG, 2); 2241 if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 2242 != sizeof (struct ar_hdr)) 2243 || bfd_bwrite (etable, elength, arch) != elength) 2244 return FALSE; 2245 if ((elength % 2) == 1) 2246 { 2247 if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1) 2248 return FALSE; 2249 } 2250 } 2251 2252 for (current = arch->archive_head; 2253 current != NULL; 2254 current = current->archive_next) 2255 { 2256 char buffer[DEFAULT_BUFFERSIZE]; 2257 bfd_size_type saved_size = arelt_size (current); 2258 bfd_size_type remaining = saved_size; 2259 struct ar_hdr *hdr = arch_hdr (current); 2260 2261 /* Write ar header. */ 2262 if (!_bfd_write_ar_hdr (arch, current)) 2263 return FALSE; 2264 /* Write filename if it is a 4.4BSD extended file, and add to size. */ 2265 if (!strncmp (hdr->ar_name, "#1/", 3)) 2266 { 2267 const char *normal = normalize (current, current->filename); 2268 unsigned int thislen = strlen (normal); 2269 if (bfd_write (normal, 1, thislen, arch) != thislen) 2270 return FALSE; 2271 saved_size += thislen; 2272 } 2273 if (bfd_is_thin_archive (arch)) 2274 continue; 2275 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0) 2276 goto input_err; 2277 2278 while (remaining) 2279 { 2280 unsigned int amt = DEFAULT_BUFFERSIZE; 2281 2282 if (amt > remaining) 2283 amt = remaining; 2284 errno = 0; 2285 if (bfd_bread (buffer, amt, current) != amt) 2286 { 2287 if (bfd_get_error () != bfd_error_system_call) 2288 bfd_set_error (bfd_error_file_truncated); 2289 goto input_err; 2290 } 2291 if (bfd_bwrite (buffer, amt, arch) != amt) 2292 return FALSE; 2293 remaining -= amt; 2294 } 2295 2296 if ((arelt_size (current) % 2) == 1) 2297 { 2298 if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1) 2299 return FALSE; 2300 } 2301 } 2302 2303 if (makemap && hasobjects) 2304 { 2305 /* Verify the timestamp in the archive file. If it would not be 2306 accepted by the linker, rewrite it until it would be. If 2307 anything odd happens, break out and just return. (The 2308 Berkeley linker checks the timestamp and refuses to read the 2309 table-of-contents if it is >60 seconds less than the file's 2310 modified-time. That painful hack requires this painful hack. */ 2311 tries = 1; 2312 do 2313 { 2314 if (bfd_update_armap_timestamp (arch)) 2315 break; 2316 (*_bfd_error_handler) 2317 (_("Warning: writing archive was slow: rewriting timestamp\n")); 2318 } 2319 while (++tries < 6); 2320 } 2321 2322 return TRUE; 2323 2324 input_err: 2325 bfd_set_error (bfd_error_on_input, current, bfd_get_error ()); 2326 return FALSE; 2327 } 2328 2329 /* Note that the namidx for the first symbol is 0. */ 2330 2331 bfd_boolean 2332 _bfd_compute_and_write_armap (bfd *arch, unsigned int elength) 2333 { 2334 char *first_name = NULL; 2335 bfd *current; 2336 file_ptr elt_no = 0; 2337 struct orl *map = NULL; 2338 unsigned int orl_max = 1024; /* Fine initial default. */ 2339 unsigned int orl_count = 0; 2340 int stridx = 0; 2341 asymbol **syms = NULL; 2342 long syms_max = 0; 2343 bfd_boolean ret; 2344 bfd_size_type amt; 2345 2346 /* Dunno if this is the best place for this info... */ 2347 if (elength != 0) 2348 elength += sizeof (struct ar_hdr); 2349 elength += elength % 2; 2350 2351 amt = orl_max * sizeof (struct orl); 2352 map = (struct orl *) bfd_malloc (amt); 2353 if (map == NULL) 2354 goto error_return; 2355 2356 /* We put the symbol names on the arch objalloc, and then discard 2357 them when done. */ 2358 first_name = (char *) bfd_alloc (arch, 1); 2359 if (first_name == NULL) 2360 goto error_return; 2361 2362 /* Drop all the files called __.SYMDEF, we're going to make our own. */ 2363 while (arch->archive_head 2364 && strcmp (arch->archive_head->filename, "__.SYMDEF") == 0) 2365 arch->archive_head = arch->archive_head->archive_next; 2366 2367 /* Map over each element. */ 2368 for (current = arch->archive_head; 2369 current != NULL; 2370 current = current->archive_next, elt_no++) 2371 { 2372 if (bfd_check_format (current, bfd_object) 2373 && (bfd_get_file_flags (current) & HAS_SYMS) != 0) 2374 { 2375 long storage; 2376 long symcount; 2377 long src_count; 2378 2379 storage = bfd_get_symtab_upper_bound (current); 2380 if (storage < 0) 2381 goto error_return; 2382 2383 if (storage != 0) 2384 { 2385 if (storage > syms_max) 2386 { 2387 if (syms_max > 0) 2388 free (syms); 2389 syms_max = storage; 2390 syms = (asymbol **) bfd_malloc (syms_max); 2391 if (syms == NULL) 2392 goto error_return; 2393 } 2394 symcount = bfd_canonicalize_symtab (current, syms); 2395 if (symcount < 0) 2396 goto error_return; 2397 2398 /* Now map over all the symbols, picking out the ones we 2399 want. */ 2400 for (src_count = 0; src_count < symcount; src_count++) 2401 { 2402 flagword flags = (syms[src_count])->flags; 2403 asection *sec = syms[src_count]->section; 2404 2405 if (((flags & (BSF_GLOBAL 2406 | BSF_WEAK 2407 | BSF_INDIRECT 2408 | BSF_GNU_UNIQUE)) != 0 2409 || bfd_is_com_section (sec)) 2410 && ! bfd_is_und_section (sec)) 2411 { 2412 bfd_size_type namelen; 2413 struct orl *new_map; 2414 2415 /* This symbol will go into the archive header. */ 2416 if (orl_count == orl_max) 2417 { 2418 orl_max *= 2; 2419 amt = orl_max * sizeof (struct orl); 2420 new_map = (struct orl *) bfd_realloc (map, amt); 2421 if (new_map == NULL) 2422 goto error_return; 2423 2424 map = new_map; 2425 } 2426 2427 if (strcmp (syms[src_count]->name, "__gnu_lto_slim") == 0) 2428 (*_bfd_error_handler) 2429 (_("%s: plugin needed to handle lto object"), 2430 bfd_get_filename (current)); 2431 namelen = strlen (syms[src_count]->name); 2432 amt = sizeof (char *); 2433 map[orl_count].name = (char **) bfd_alloc (arch, amt); 2434 if (map[orl_count].name == NULL) 2435 goto error_return; 2436 *(map[orl_count].name) = (char *) bfd_alloc (arch, 2437 namelen + 1); 2438 if (*(map[orl_count].name) == NULL) 2439 goto error_return; 2440 strcpy (*(map[orl_count].name), syms[src_count]->name); 2441 map[orl_count].u.abfd = current; 2442 map[orl_count].namidx = stridx; 2443 2444 stridx += namelen + 1; 2445 ++orl_count; 2446 } 2447 } 2448 } 2449 2450 /* Now ask the BFD to free up any cached information, so we 2451 don't fill all of memory with symbol tables. */ 2452 if (! bfd_free_cached_info (current)) 2453 goto error_return; 2454 } 2455 } 2456 2457 /* OK, now we have collected all the data, let's write them out. */ 2458 ret = BFD_SEND (arch, write_armap, 2459 (arch, elength, map, orl_count, stridx)); 2460 2461 if (syms_max > 0) 2462 free (syms); 2463 if (map != NULL) 2464 free (map); 2465 if (first_name != NULL) 2466 bfd_release (arch, first_name); 2467 2468 return ret; 2469 2470 error_return: 2471 if (syms_max > 0) 2472 free (syms); 2473 if (map != NULL) 2474 free (map); 2475 if (first_name != NULL) 2476 bfd_release (arch, first_name); 2477 2478 return FALSE; 2479 } 2480 2481 bfd_boolean 2482 bsd_write_armap (bfd *arch, 2483 unsigned int elength, 2484 struct orl *map, 2485 unsigned int orl_count, 2486 int stridx) 2487 { 2488 int padit = stridx & 1; 2489 unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE; 2490 unsigned int stringsize = stridx + padit; 2491 /* Include 8 bytes to store ranlibsize and stringsize in output. */ 2492 unsigned int mapsize = ranlibsize + stringsize + 8; 2493 file_ptr firstreal; 2494 bfd *current = arch->archive_head; 2495 bfd *last_elt = current; /* Last element arch seen. */ 2496 bfd_byte temp[4]; 2497 unsigned int count; 2498 struct ar_hdr hdr; 2499 long uid, gid; 2500 2501 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG; 2502 2503 /* If deterministic, we use 0 as the timestamp in the map. 2504 Some linkers may require that the archive filesystem modification 2505 time is less than (or near to) the archive map timestamp. Those 2506 linkers should not be used with deterministic mode. (GNU ld and 2507 Gold do not have this restriction.) */ 2508 bfd_ardata (arch)->armap_timestamp = 0; 2509 uid = 0; 2510 gid = 0; 2511 if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0) 2512 { 2513 struct stat statbuf; 2514 2515 if (stat (arch->filename, &statbuf) == 0) 2516 bfd_ardata (arch)->armap_timestamp = (statbuf.st_mtime 2517 + ARMAP_TIME_OFFSET); 2518 uid = getuid(); 2519 gid = getgid(); 2520 } 2521 2522 memset (&hdr, ' ', sizeof (struct ar_hdr)); 2523 memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG)); 2524 bfd_ardata (arch)->armap_datepos = (SARMAG 2525 + offsetof (struct ar_hdr, ar_date[0])); 2526 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld", 2527 bfd_ardata (arch)->armap_timestamp); 2528 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", uid); 2529 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", gid); 2530 if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize)) 2531 return FALSE; 2532 memcpy (hdr.ar_fmag, ARFMAG, 2); 2533 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 2534 != sizeof (struct ar_hdr)) 2535 return FALSE; 2536 H_PUT_32 (arch, ranlibsize, temp); 2537 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp)) 2538 return FALSE; 2539 2540 for (count = 0; count < orl_count; count++) 2541 { 2542 unsigned int offset; 2543 bfd_byte buf[BSD_SYMDEF_SIZE]; 2544 2545 if (map[count].u.abfd != last_elt) 2546 { 2547 do 2548 { 2549 #if 1 2550 bfd_size_type size = arelt_size (current); 2551 if (!strncmp(arch_hdr (current)->ar_name, "#1/", 3)) 2552 size += strlen(normalize(current, current->filename)); 2553 firstreal += size + sizeof (struct ar_hdr); 2554 firstreal += size % 2; 2555 #else 2556 struct areltdata *ared = arch_eltdata (current); 2557 2558 firstreal += (ared->parsed_size + ared->extra_size 2559 + sizeof (struct ar_hdr)); 2560 firstreal += firstreal % 2; 2561 #endif 2562 current = current->archive_next; 2563 } 2564 while (current != map[count].u.abfd); 2565 } 2566 2567 /* The archive file format only has 4 bytes to store the offset 2568 of the member. Check to make sure that firstreal has not grown 2569 too big. */ 2570 offset = (unsigned int) firstreal; 2571 if (firstreal != (file_ptr) offset) 2572 { 2573 bfd_set_error (bfd_error_file_truncated); 2574 return FALSE; 2575 } 2576 2577 last_elt = current; 2578 H_PUT_32 (arch, map[count].namidx, buf); 2579 H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE); 2580 if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch) 2581 != BSD_SYMDEF_SIZE) 2582 return FALSE; 2583 } 2584 2585 /* Now write the strings themselves. */ 2586 H_PUT_32 (arch, stringsize, temp); 2587 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp)) 2588 return FALSE; 2589 for (count = 0; count < orl_count; count++) 2590 { 2591 size_t len = strlen (*map[count].name) + 1; 2592 2593 if (bfd_bwrite (*map[count].name, len, arch) != len) 2594 return FALSE; 2595 } 2596 2597 /* The spec sez this should be a newline. But in order to be 2598 bug-compatible for sun's ar we use a null. */ 2599 if (padit) 2600 { 2601 if (bfd_bwrite ("", 1, arch) != 1) 2602 return FALSE; 2603 } 2604 2605 return TRUE; 2606 } 2607 2608 /* At the end of archive file handling, update the timestamp in the 2609 file, so the linker will accept it. 2610 2611 Return TRUE if the timestamp was OK, or an unusual problem happened. 2612 Return FALSE if we updated the timestamp. */ 2613 2614 bfd_boolean 2615 _bfd_archive_bsd_update_armap_timestamp (bfd *arch) 2616 { 2617 struct stat archstat; 2618 struct ar_hdr hdr; 2619 2620 /* If creating deterministic archives, just leave the timestamp as-is. */ 2621 if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) != 0) 2622 return TRUE; 2623 2624 /* Flush writes, get last-write timestamp from file, and compare it 2625 to the timestamp IN the file. */ 2626 bfd_flush (arch); 2627 if (bfd_stat (arch, &archstat) == -1) 2628 { 2629 bfd_perror (_("Reading archive file mod timestamp")); 2630 2631 /* Can't read mod time for some reason. */ 2632 return TRUE; 2633 } 2634 if (((long) archstat.st_mtime) <= bfd_ardata (arch)->armap_timestamp) 2635 /* OK by the linker's rules. */ 2636 return TRUE; 2637 2638 /* Update the timestamp. */ 2639 bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET; 2640 2641 /* Prepare an ASCII version suitable for writing. */ 2642 memset (hdr.ar_date, ' ', sizeof (hdr.ar_date)); 2643 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld", 2644 bfd_ardata (arch)->armap_timestamp); 2645 2646 /* Write it into the file. */ 2647 bfd_ardata (arch)->armap_datepos = (SARMAG 2648 + offsetof (struct ar_hdr, ar_date[0])); 2649 if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0 2650 || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch) 2651 != sizeof (hdr.ar_date))) 2652 { 2653 bfd_perror (_("Writing updated armap timestamp")); 2654 2655 /* Some error while writing. */ 2656 return TRUE; 2657 } 2658 2659 /* We updated the timestamp successfully. */ 2660 return FALSE; 2661 } 2662 2663 /* A coff armap looks like : 2664 lARMAG 2665 struct ar_hdr with name = '/' 2666 number of symbols 2667 offset of file for symbol 0 2668 offset of file for symbol 1 2669 2670 offset of file for symbol n-1 2671 symbol name 0 2672 symbol name 1 2673 2674 symbol name n-1 */ 2675 2676 bfd_boolean 2677 coff_write_armap (bfd *arch, 2678 unsigned int elength, 2679 struct orl *map, 2680 unsigned int symbol_count, 2681 int stridx) 2682 { 2683 /* The size of the ranlib is the number of exported symbols in the 2684 archive * the number of bytes in an int, + an int for the count. */ 2685 unsigned int ranlibsize = (symbol_count * 4) + 4; 2686 unsigned int stringsize = stridx; 2687 unsigned int mapsize = stringsize + ranlibsize; 2688 file_ptr archive_member_file_ptr; 2689 bfd *current = arch->archive_head; 2690 unsigned int count; 2691 struct ar_hdr hdr; 2692 int padit = mapsize & 1; 2693 2694 if (padit) 2695 mapsize++; 2696 2697 /* Work out where the first object file will go in the archive. */ 2698 archive_member_file_ptr = (mapsize 2699 + elength 2700 + sizeof (struct ar_hdr) 2701 + SARMAG); 2702 2703 memset (&hdr, ' ', sizeof (struct ar_hdr)); 2704 hdr.ar_name[0] = '/'; 2705 if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize)) 2706 return FALSE; 2707 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld", 2708 ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0 2709 ? time (NULL) : 0)); 2710 /* This, at least, is what Intel coff sets the values to. */ 2711 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0); 2712 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0); 2713 _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0); 2714 memcpy (hdr.ar_fmag, ARFMAG, 2); 2715 2716 /* Write the ar header for this item and the number of symbols. */ 2717 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 2718 != sizeof (struct ar_hdr)) 2719 return FALSE; 2720 2721 if (!bfd_write_bigendian_4byte_int (arch, symbol_count)) 2722 return FALSE; 2723 2724 /* Two passes, first write the file offsets for each symbol - 2725 remembering that each offset is on a two byte boundary. */ 2726 2727 /* Write out the file offset for the file associated with each 2728 symbol, and remember to keep the offsets padded out. */ 2729 2730 current = arch->archive_head; 2731 count = 0; 2732 while (current != NULL && count < symbol_count) 2733 { 2734 /* For each symbol which is used defined in this object, write 2735 out the object file's address in the archive. */ 2736 2737 while (count < symbol_count && map[count].u.abfd == current) 2738 { 2739 unsigned int offset = (unsigned int) archive_member_file_ptr; 2740 2741 /* Catch an attempt to grow an archive past its 4Gb limit. */ 2742 if (archive_member_file_ptr != (file_ptr) offset) 2743 { 2744 bfd_set_error (bfd_error_file_truncated); 2745 return FALSE; 2746 } 2747 if (!bfd_write_bigendian_4byte_int (arch, offset)) 2748 return FALSE; 2749 count++; 2750 } 2751 archive_member_file_ptr += sizeof (struct ar_hdr); 2752 if (! bfd_is_thin_archive (arch)) 2753 { 2754 /* Add size of this archive entry. */ 2755 archive_member_file_ptr += arelt_size (current); 2756 /* Remember about the even alignment. */ 2757 archive_member_file_ptr += archive_member_file_ptr % 2; 2758 } 2759 current = current->archive_next; 2760 } 2761 2762 /* Now write the strings themselves. */ 2763 for (count = 0; count < symbol_count; count++) 2764 { 2765 size_t len = strlen (*map[count].name) + 1; 2766 2767 if (bfd_bwrite (*map[count].name, len, arch) != len) 2768 return FALSE; 2769 } 2770 2771 /* The spec sez this should be a newline. But in order to be 2772 bug-compatible for arc960 we use a null. */ 2773 if (padit) 2774 { 2775 if (bfd_bwrite ("", 1, arch) != 1) 2776 return FALSE; 2777 } 2778 2779 return TRUE; 2780 } 2781 2782 static int 2783 archive_close_worker (void **slot, void *inf ATTRIBUTE_UNUSED) 2784 { 2785 struct ar_cache *ent = (struct ar_cache *) *slot; 2786 2787 bfd_close_all_done (ent->arbfd); 2788 return 1; 2789 } 2790 2791 bfd_boolean 2792 _bfd_archive_close_and_cleanup (bfd *abfd) 2793 { 2794 if (bfd_read_p (abfd) && abfd->format == bfd_archive) 2795 { 2796 bfd *nbfd; 2797 bfd *next; 2798 htab_t htab; 2799 2800 /* Close nested archives (if this bfd is a thin archive). */ 2801 for (nbfd = abfd->nested_archives; nbfd; nbfd = next) 2802 { 2803 next = nbfd->archive_next; 2804 bfd_close (nbfd); 2805 } 2806 2807 htab = bfd_ardata (abfd)->cache; 2808 if (htab) 2809 { 2810 htab_traverse_noresize (htab, archive_close_worker, NULL); 2811 htab_delete (htab); 2812 bfd_ardata (abfd)->cache = NULL; 2813 } 2814 } 2815 if (arch_eltdata (abfd) != NULL) 2816 { 2817 struct areltdata *ared = arch_eltdata (abfd); 2818 htab_t htab = (htab_t) ared->parent_cache; 2819 2820 if (htab) 2821 { 2822 struct ar_cache ent; 2823 void **slot; 2824 2825 ent.ptr = ared->key; 2826 slot = htab_find_slot (htab, &ent, NO_INSERT); 2827 if (slot != NULL) 2828 { 2829 BFD_ASSERT (((struct ar_cache *) *slot)->arbfd == abfd); 2830 htab_clear_slot (htab, slot); 2831 } 2832 } 2833 } 2834 if (abfd->is_linker_output) 2835 (*abfd->link.hash->hash_table_free) (abfd); 2836 2837 return TRUE; 2838 } 2839