1 /* BFD back-end for archive files (libraries). 2 Copyright (C) 1990-2016 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 n_bfd->my_archive = archive; 399 } 400 return n_bfd; 401 } 402 403 static bfd * 404 find_nested_archive (const char *filename, bfd *arch_bfd) 405 { 406 bfd *abfd; 407 408 /* PR 15140: Don't allow a nested archive pointing to itself. */ 409 if (filename_cmp (filename, arch_bfd->filename) == 0) 410 { 411 bfd_set_error (bfd_error_malformed_archive); 412 return NULL; 413 } 414 415 for (abfd = arch_bfd->nested_archives; 416 abfd != NULL; 417 abfd = abfd->archive_next) 418 { 419 if (filename_cmp (filename, abfd->filename) == 0) 420 return abfd; 421 } 422 abfd = open_nested_file (filename, arch_bfd); 423 if (abfd) 424 { 425 abfd->archive_next = arch_bfd->nested_archives; 426 arch_bfd->nested_archives = abfd; 427 } 428 return abfd; 429 } 430 431 /* The name begins with space. Hence the rest of the name is an index into 432 the string table. */ 433 434 static char * 435 get_extended_arelt_filename (bfd *arch, const char *name, file_ptr *originp) 436 { 437 unsigned long table_index = 0; 438 const char *endp; 439 440 /* Should extract string so that I can guarantee not to overflow into 441 the next region, but I'm too lazy. */ 442 errno = 0; 443 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */ 444 table_index = strtol (name + 1, (char **) &endp, 10); 445 if (errno != 0 || table_index >= bfd_ardata (arch)->extended_names_size) 446 { 447 bfd_set_error (bfd_error_malformed_archive); 448 return NULL; 449 } 450 /* In a thin archive, a member of an archive-within-an-archive 451 will have the offset in the inner archive encoded here. */ 452 if (bfd_is_thin_archive (arch) && endp != NULL && *endp == ':') 453 { 454 file_ptr origin = strtol (endp + 1, NULL, 10); 455 456 if (errno != 0) 457 { 458 bfd_set_error (bfd_error_malformed_archive); 459 return NULL; 460 } 461 *originp = origin; 462 } 463 else 464 *originp = 0; 465 466 return bfd_ardata (arch)->extended_names + table_index; 467 } 468 469 /* This functions reads an arch header and returns an areltdata pointer, or 470 NULL on error. 471 472 Presumes the file pointer is already in the right place (ie pointing 473 to the ar_hdr in the file). Moves the file pointer; on success it 474 should be pointing to the front of the file contents; on failure it 475 could have been moved arbitrarily. */ 476 477 void * 478 _bfd_generic_read_ar_hdr (bfd *abfd) 479 { 480 return _bfd_generic_read_ar_hdr_mag (abfd, NULL); 481 } 482 483 /* Alpha ECOFF uses an optional different ARFMAG value, so we have a 484 variant of _bfd_generic_read_ar_hdr which accepts a magic string. */ 485 486 void * 487 _bfd_generic_read_ar_hdr_mag (bfd *abfd, const char *mag) 488 { 489 struct ar_hdr hdr; 490 char *hdrp = (char *) &hdr; 491 bfd_size_type parsed_size; 492 struct areltdata *ared; 493 char *filename = NULL; 494 bfd_size_type namelen = 0; 495 bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr); 496 char *allocptr = 0; 497 file_ptr origin = 0; 498 unsigned int extra_size = 0; 499 char fmag_save; 500 int scan; 501 502 if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr)) 503 { 504 if (bfd_get_error () != bfd_error_system_call) 505 bfd_set_error (bfd_error_no_more_archived_files); 506 return NULL; 507 } 508 if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0 509 && (mag == NULL 510 || strncmp (hdr.ar_fmag, mag, 2) != 0)) 511 { 512 bfd_set_error (bfd_error_malformed_archive); 513 return NULL; 514 } 515 516 errno = 0; 517 fmag_save = hdr.ar_fmag[0]; 518 hdr.ar_fmag[0] = 0; 519 scan = sscanf (hdr.ar_size, "%" BFD_VMA_FMT "u", &parsed_size); 520 hdr.ar_fmag[0] = fmag_save; 521 if (scan != 1) 522 { 523 bfd_set_error (bfd_error_malformed_archive); 524 return NULL; 525 } 526 527 /* Extract the filename from the archive - there are two ways to 528 specify an extended name table, either the first char of the 529 name is a space, or it's a slash. */ 530 if ((hdr.ar_name[0] == '/' 531 || (hdr.ar_name[0] == ' ' 532 && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL)) 533 && bfd_ardata (abfd)->extended_names != NULL) 534 { 535 filename = get_extended_arelt_filename (abfd, hdr.ar_name, &origin); 536 if (filename == NULL) 537 return NULL; 538 } 539 /* BSD4.4-style long filename. */ 540 else if (is_bsd44_extended_name (hdr.ar_name)) 541 { 542 /* BSD-4.4 extended name */ 543 namelen = atoi (&hdr.ar_name[3]); 544 allocsize += namelen + 1; 545 parsed_size -= namelen; 546 extra_size = namelen; 547 548 allocptr = (char *) bfd_zmalloc (allocsize); 549 if (allocptr == NULL) 550 return NULL; 551 filename = (allocptr 552 + sizeof (struct areltdata) 553 + sizeof (struct ar_hdr)); 554 if (bfd_bread (filename, namelen, abfd) != namelen) 555 { 556 free (allocptr); 557 if (bfd_get_error () != bfd_error_system_call) 558 bfd_set_error (bfd_error_no_more_archived_files); 559 return NULL; 560 } 561 filename[namelen] = '\0'; 562 } 563 else 564 { 565 /* We judge the end of the name by looking for '/' or ' '. 566 Note: The SYSV format (terminated by '/') allows embedded 567 spaces, so only look for ' ' if we don't find '/'. */ 568 569 char *e; 570 e = (char *) memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd)); 571 if (e == NULL) 572 { 573 e = (char *) memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)); 574 if (e == NULL) 575 e = (char *) memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd)); 576 } 577 578 if (e != NULL) 579 namelen = e - hdr.ar_name; 580 else 581 { 582 /* If we didn't find a termination character, then the name 583 must be the entire field. */ 584 namelen = ar_maxnamelen (abfd); 585 } 586 587 allocsize += namelen + 1; 588 } 589 590 if (!allocptr) 591 { 592 allocptr = (char *) bfd_zmalloc (allocsize); 593 if (allocptr == NULL) 594 return NULL; 595 } 596 597 ared = (struct areltdata *) allocptr; 598 599 ared->arch_header = allocptr + sizeof (struct areltdata); 600 memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr)); 601 ared->parsed_size = parsed_size; 602 ared->extra_size = extra_size; 603 ared->origin = origin; 604 605 if (filename != NULL) 606 ared->filename = filename; 607 else 608 { 609 ared->filename = allocptr + (sizeof (struct areltdata) + 610 sizeof (struct ar_hdr)); 611 if (namelen) 612 memcpy (ared->filename, hdr.ar_name, namelen); 613 ared->filename[namelen] = '\0'; 614 } 615 616 return ared; 617 } 618 619 /* Append the relative pathname for a member of the thin archive 620 to the pathname of the directory containing the archive. */ 621 622 char * 623 _bfd_append_relative_path (bfd *arch, char *elt_name) 624 { 625 const char *arch_name = arch->filename; 626 const char *base_name = lbasename (arch_name); 627 size_t prefix_len; 628 char *filename; 629 630 if (base_name == arch_name) 631 return elt_name; 632 633 prefix_len = base_name - arch_name; 634 filename = (char *) bfd_alloc (arch, prefix_len + strlen (elt_name) + 1); 635 if (filename == NULL) 636 return NULL; 637 638 strncpy (filename, arch_name, prefix_len); 639 strcpy (filename + prefix_len, elt_name); 640 return filename; 641 } 642 643 /* This is an internal function; it's mainly used when indexing 644 through the archive symbol table, but also used to get the next 645 element, since it handles the bookkeeping so nicely for us. */ 646 647 bfd * 648 _bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos) 649 { 650 struct areltdata *new_areldata; 651 bfd *n_bfd; 652 char *filename; 653 654 n_bfd = _bfd_look_for_bfd_in_cache (archive, filepos); 655 if (n_bfd) 656 return n_bfd; 657 658 if (0 > bfd_seek (archive, filepos, SEEK_SET)) 659 return NULL; 660 661 if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL) 662 return NULL; 663 664 filename = new_areldata->filename; 665 666 if (bfd_is_thin_archive (archive)) 667 { 668 /* This is a proxy entry for an external file. */ 669 if (! IS_ABSOLUTE_PATH (filename)) 670 { 671 filename = _bfd_append_relative_path (archive, filename); 672 if (filename == NULL) 673 { 674 free (new_areldata); 675 return NULL; 676 } 677 } 678 679 if (new_areldata->origin > 0) 680 { 681 /* This proxy entry refers to an element of a nested archive. 682 Locate the member of that archive and return a bfd for it. */ 683 bfd *ext_arch = find_nested_archive (filename, archive); 684 685 if (ext_arch == NULL 686 || ! bfd_check_format (ext_arch, bfd_archive)) 687 { 688 free (new_areldata); 689 return NULL; 690 } 691 n_bfd = _bfd_get_elt_at_filepos (ext_arch, new_areldata->origin); 692 if (n_bfd == NULL) 693 { 694 free (new_areldata); 695 return NULL; 696 } 697 n_bfd->proxy_origin = bfd_tell (archive); 698 return n_bfd; 699 } 700 701 /* It's not an element of a nested archive; 702 open the external file as a bfd. */ 703 n_bfd = open_nested_file (filename, archive); 704 if (n_bfd == NULL) 705 bfd_set_error (bfd_error_malformed_archive); 706 } 707 else 708 { 709 n_bfd = _bfd_create_empty_archive_element_shell (archive); 710 } 711 712 if (n_bfd == NULL) 713 { 714 free (new_areldata); 715 return NULL; 716 } 717 718 n_bfd->proxy_origin = bfd_tell (archive); 719 720 if (bfd_is_thin_archive (archive)) 721 { 722 n_bfd->origin = 0; 723 } 724 else 725 { 726 n_bfd->origin = n_bfd->proxy_origin; 727 n_bfd->filename = xstrdup (filename); 728 } 729 730 n_bfd->arelt_data = new_areldata; 731 732 /* Copy BFD_COMPRESS, BFD_DECOMPRESS and BFD_COMPRESS_GABI flags. */ 733 n_bfd->flags |= archive->flags & (BFD_COMPRESS 734 | BFD_DECOMPRESS 735 | BFD_COMPRESS_GABI); 736 737 /* Copy is_linker_input. */ 738 n_bfd->is_linker_input = archive->is_linker_input; 739 740 if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_bfd)) 741 return n_bfd; 742 743 free (new_areldata); 744 n_bfd->arelt_data = NULL; 745 return NULL; 746 } 747 748 /* Return the BFD which is referenced by the symbol in ABFD indexed by 749 SYM_INDEX. SYM_INDEX should have been returned by bfd_get_next_mapent. */ 750 751 bfd * 752 _bfd_generic_get_elt_at_index (bfd *abfd, symindex sym_index) 753 { 754 carsym *entry; 755 756 entry = bfd_ardata (abfd)->symdefs + sym_index; 757 return _bfd_get_elt_at_filepos (abfd, entry->file_offset); 758 } 759 760 /* 761 FUNCTION 762 bfd_openr_next_archived_file 763 764 SYNOPSIS 765 bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous); 766 767 DESCRIPTION 768 Provided a BFD, @var{archive}, containing an archive and NULL, open 769 an input BFD on the first contained element and returns that. 770 Subsequent calls should pass 771 the archive and the previous return value to return a created 772 BFD to the next contained element. NULL is returned when there 773 are no more. 774 */ 775 776 bfd * 777 bfd_openr_next_archived_file (bfd *archive, bfd *last_file) 778 { 779 if ((bfd_get_format (archive) != bfd_archive) 780 || (archive->direction == write_direction)) 781 { 782 bfd_set_error (bfd_error_invalid_operation); 783 return NULL; 784 } 785 786 return BFD_SEND (archive, 787 openr_next_archived_file, (archive, last_file)); 788 } 789 790 bfd * 791 bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file) 792 { 793 ufile_ptr filestart; 794 795 if (!last_file) 796 filestart = bfd_ardata (archive)->first_file_filepos; 797 else 798 { 799 filestart = last_file->proxy_origin; 800 if (! bfd_is_thin_archive (archive)) 801 #if 0 802 /* OLD CODE */ 803 filestart += size; 804 /* Pad to an even boundary... 805 Note that last_file->origin can be odd in the case of 806 BSD-4.4-style element with a long odd size. */ 807 if (!strncmp(arch_hdr (last_file)->ar_name, "#1/", 3)) 808 size += strlen(normalize(last_file, last_file->filename)); 809 filestart += size % 2; 810 #endif 811 { 812 bfd_size_type size = arelt_size (last_file); 813 814 filestart += size; 815 /* Pad to an even boundary... 816 Note that last_file->origin can be odd in the case of 817 BSD-4.4-style element with a long odd size. */ 818 filestart += filestart % 2; 819 if (filestart < last_file->proxy_origin) 820 { 821 /* Prevent looping. See PR19256. */ 822 bfd_set_error (bfd_error_malformed_archive); 823 return NULL; 824 } 825 } 826 } 827 828 return _bfd_get_elt_at_filepos (archive, filestart); 829 } 830 831 const bfd_target * 832 bfd_generic_archive_p (bfd *abfd) 833 { 834 struct artdata *tdata_hold; 835 char armag[SARMAG + 1]; 836 bfd_size_type amt; 837 838 if (bfd_bread (armag, SARMAG, abfd) != SARMAG) 839 { 840 if (bfd_get_error () != bfd_error_system_call) 841 bfd_set_error (bfd_error_wrong_format); 842 return NULL; 843 } 844 845 bfd_is_thin_archive (abfd) = (strncmp (armag, ARMAGT, SARMAG) == 0); 846 847 if (strncmp (armag, ARMAG, SARMAG) != 0 848 && strncmp (armag, ARMAGB, SARMAG) != 0 849 && ! bfd_is_thin_archive (abfd)) 850 return NULL; 851 852 tdata_hold = bfd_ardata (abfd); 853 854 amt = sizeof (struct artdata); 855 bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt); 856 if (bfd_ardata (abfd) == NULL) 857 { 858 bfd_ardata (abfd) = tdata_hold; 859 return NULL; 860 } 861 862 bfd_ardata (abfd)->first_file_filepos = SARMAG; 863 /* Cleared by bfd_zalloc above. 864 bfd_ardata (abfd)->cache = NULL; 865 bfd_ardata (abfd)->archive_head = NULL; 866 bfd_ardata (abfd)->symdefs = NULL; 867 bfd_ardata (abfd)->extended_names = NULL; 868 bfd_ardata (abfd)->extended_names_size = 0; 869 bfd_ardata (abfd)->tdata = NULL; */ 870 871 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd)) 872 || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) 873 { 874 if (bfd_get_error () != bfd_error_system_call) 875 bfd_set_error (bfd_error_wrong_format); 876 bfd_release (abfd, bfd_ardata (abfd)); 877 bfd_ardata (abfd) = tdata_hold; 878 return NULL; 879 } 880 881 if (abfd->target_defaulted && bfd_has_map (abfd)) 882 { 883 bfd *first; 884 885 /* This archive has a map, so we may presume that the contents 886 are object files. Make sure that if the first file in the 887 archive can be recognized as an object file, it is for this 888 target. If not, assume that this is the wrong format. If 889 the first file is not an object file, somebody is doing 890 something weird, and we permit it so that ar -t will work. 891 892 This is done because any normal format will recognize any 893 normal archive, regardless of the format of the object files. 894 We do accept an empty archive. */ 895 896 first = bfd_openr_next_archived_file (abfd, NULL); 897 if (first != NULL) 898 { 899 first->target_defaulted = FALSE; 900 if (bfd_check_format (first, bfd_object) 901 && first->xvec != abfd->xvec) 902 bfd_set_error (bfd_error_wrong_object_format); 903 /* And we ought to close `first' here too. */ 904 } 905 } 906 907 return abfd->xvec; 908 } 909 910 /* Some constants for a 32 bit BSD archive structure. We do not 911 support 64 bit archives presently; so far as I know, none actually 912 exist. Supporting them would require changing these constants, and 913 changing some H_GET_32 to H_GET_64. */ 914 915 /* The size of an external symdef structure. */ 916 #define BSD_SYMDEF_SIZE 8 917 918 /* The offset from the start of a symdef structure to the file offset. */ 919 #define BSD_SYMDEF_OFFSET_SIZE 4 920 921 /* The size of the symdef count. */ 922 #define BSD_SYMDEF_COUNT_SIZE 4 923 924 /* The size of the string count. */ 925 #define BSD_STRING_COUNT_SIZE 4 926 927 /* Read a BSD-style archive symbol table. Returns FALSE on error, 928 TRUE otherwise. */ 929 930 static bfd_boolean 931 do_slurp_bsd_armap (bfd *abfd) 932 { 933 struct areltdata *mapdata; 934 unsigned int counter; 935 bfd_byte *raw_armap, *rbase; 936 struct artdata *ardata = bfd_ardata (abfd); 937 char *stringbase; 938 bfd_size_type parsed_size, amt; 939 carsym *set; 940 941 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 942 if (mapdata == NULL) 943 return FALSE; 944 parsed_size = mapdata->parsed_size; 945 free (mapdata); 946 /* PR 17512: file: 883ff754. */ 947 /* PR 17512: file: 0458885f. */ 948 if (parsed_size < 4) 949 return FALSE; 950 951 raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size); 952 if (raw_armap == NULL) 953 return FALSE; 954 955 if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size) 956 { 957 if (bfd_get_error () != bfd_error_system_call) 958 bfd_set_error (bfd_error_malformed_archive); 959 byebye: 960 bfd_release (abfd, raw_armap); 961 return FALSE; 962 } 963 964 ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE; 965 if (ardata->symdef_count * BSD_SYMDEF_SIZE > 966 parsed_size - BSD_SYMDEF_COUNT_SIZE) 967 { 968 /* Probably we're using the wrong byte ordering. */ 969 bfd_set_error (bfd_error_wrong_format); 970 goto byebye; 971 } 972 973 ardata->cache = 0; 974 rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE; 975 stringbase = ((char *) rbase 976 + ardata->symdef_count * BSD_SYMDEF_SIZE 977 + BSD_STRING_COUNT_SIZE); 978 amt = ardata->symdef_count * sizeof (carsym); 979 ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt); 980 if (!ardata->symdefs) 981 return FALSE; 982 983 for (counter = 0, set = ardata->symdefs; 984 counter < ardata->symdef_count; 985 counter++, set++, rbase += BSD_SYMDEF_SIZE) 986 { 987 set->name = H_GET_32 (abfd, rbase) + stringbase; 988 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); 989 } 990 991 ardata->first_file_filepos = bfd_tell (abfd); 992 /* Pad to an even boundary if you have to. */ 993 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 994 /* FIXME, we should provide some way to free raw_ardata when 995 we are done using the strings from it. For now, it seems 996 to be allocated on an objalloc anyway... */ 997 bfd_has_map (abfd) = TRUE; 998 return TRUE; 999 } 1000 1001 /* Read a COFF archive symbol table. Returns FALSE on error, TRUE 1002 otherwise. */ 1003 1004 static bfd_boolean 1005 do_slurp_coff_armap (bfd *abfd) 1006 { 1007 struct areltdata *mapdata; 1008 int *raw_armap, *rawptr; 1009 struct artdata *ardata = bfd_ardata (abfd); 1010 char *stringbase; 1011 bfd_size_type stringsize; 1012 bfd_size_type parsed_size; 1013 carsym *carsyms; 1014 bfd_size_type nsymz; /* Number of symbols in armap. */ 1015 bfd_vma (*swap) (const void *); 1016 char int_buf[sizeof (long)]; 1017 bfd_size_type carsym_size, ptrsize; 1018 unsigned int i; 1019 1020 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 1021 if (mapdata == NULL) 1022 return FALSE; 1023 parsed_size = mapdata->parsed_size; 1024 free (mapdata); 1025 1026 if (bfd_bread (int_buf, 4, abfd) != 4) 1027 { 1028 if (bfd_get_error () != bfd_error_system_call) 1029 bfd_set_error (bfd_error_malformed_archive); 1030 return FALSE; 1031 } 1032 /* It seems that all numeric information in a coff archive is always 1033 in big endian format, nomatter the host or target. */ 1034 swap = bfd_getb32; 1035 nsymz = bfd_getb32 (int_buf); 1036 stringsize = parsed_size - (4 * nsymz) - 4; 1037 1038 /* ... except that some archive formats are broken, and it may be our 1039 fault - the i960 little endian coff sometimes has big and sometimes 1040 little, because our tools changed. Here's a horrible hack to clean 1041 up the crap. */ 1042 1043 if (stringsize > 0xfffff 1044 && bfd_get_arch (abfd) == bfd_arch_i960 1045 && bfd_get_flavour (abfd) == bfd_target_coff_flavour) 1046 { 1047 /* This looks dangerous, let's do it the other way around. */ 1048 nsymz = bfd_getl32 (int_buf); 1049 stringsize = parsed_size - (4 * nsymz) - 4; 1050 swap = bfd_getl32; 1051 } 1052 1053 /* The coff armap must be read sequentially. So we construct a 1054 bsd-style one in core all at once, for simplicity. */ 1055 1056 if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym)) 1057 return FALSE; 1058 1059 carsym_size = (nsymz * sizeof (carsym)); 1060 ptrsize = (4 * nsymz); 1061 1062 if (carsym_size + stringsize + 1 <= carsym_size) 1063 return FALSE; 1064 1065 ardata->symdefs = (struct carsym *) bfd_zalloc (abfd, 1066 carsym_size + stringsize + 1); 1067 if (ardata->symdefs == NULL) 1068 return FALSE; 1069 carsyms = ardata->symdefs; 1070 stringbase = ((char *) ardata->symdefs) + carsym_size; 1071 1072 /* Allocate and read in the raw offsets. */ 1073 raw_armap = (int *) bfd_alloc (abfd, ptrsize); 1074 if (raw_armap == NULL) 1075 goto release_symdefs; 1076 if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize 1077 || (bfd_bread (stringbase, stringsize, abfd) != stringsize)) 1078 { 1079 if (bfd_get_error () != bfd_error_system_call) 1080 bfd_set_error (bfd_error_malformed_archive); 1081 goto release_raw_armap; 1082 } 1083 1084 /* OK, build the carsyms. */ 1085 for (i = 0; i < nsymz && stringsize > 0; i++) 1086 { 1087 bfd_size_type len; 1088 1089 rawptr = raw_armap + i; 1090 carsyms->file_offset = swap ((bfd_byte *) rawptr); 1091 carsyms->name = stringbase; 1092 /* PR 17512: file: 4a1d50c1. */ 1093 len = strnlen (stringbase, stringsize); 1094 if (len < stringsize) 1095 len ++; 1096 stringbase += len; 1097 stringsize -= len; 1098 carsyms++; 1099 } 1100 *stringbase = 0; 1101 1102 ardata->symdef_count = nsymz; 1103 ardata->first_file_filepos = bfd_tell (abfd); 1104 /* Pad to an even boundary if you have to. */ 1105 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 1106 1107 bfd_has_map (abfd) = TRUE; 1108 bfd_release (abfd, raw_armap); 1109 1110 /* Check for a second archive header (as used by PE). */ 1111 { 1112 struct areltdata *tmp; 1113 1114 bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET); 1115 tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd); 1116 if (tmp != NULL) 1117 { 1118 if (tmp->arch_header[0] == '/' 1119 && tmp->arch_header[1] == ' ') 1120 { 1121 ardata->first_file_filepos += 1122 (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1; 1123 } 1124 free (tmp); 1125 } 1126 } 1127 1128 return TRUE; 1129 1130 release_raw_armap: 1131 bfd_release (abfd, raw_armap); 1132 release_symdefs: 1133 bfd_release (abfd, (ardata)->symdefs); 1134 return FALSE; 1135 } 1136 1137 /* This routine can handle either coff-style or bsd-style armaps 1138 (archive symbol table). Returns FALSE on error, TRUE otherwise */ 1139 1140 bfd_boolean 1141 bfd_slurp_armap (bfd *abfd) 1142 { 1143 char nextname[17]; 1144 int i = bfd_bread (nextname, 16, abfd); 1145 1146 if (i == 0) 1147 return TRUE; 1148 if (i != 16) 1149 return FALSE; 1150 1151 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 1152 return FALSE; 1153 1154 if (CONST_STRNEQ (nextname, "__.SYMDEF ") 1155 || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */ 1156 return do_slurp_bsd_armap (abfd); 1157 else if (CONST_STRNEQ (nextname, "/ ")) 1158 return do_slurp_coff_armap (abfd); 1159 else if (CONST_STRNEQ (nextname, "/SYM64/ ")) 1160 { 1161 /* 64bit (Irix 6) archive. */ 1162 #ifdef BFD64 1163 return _bfd_archive_64_bit_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, first; 2494 bfd *current; 2495 bfd *last_elt; 2496 bfd_byte temp[4]; 2497 unsigned int count; 2498 struct ar_hdr hdr; 2499 long uid, gid; 2500 2501 first = mapsize + elength + sizeof (struct ar_hdr) + SARMAG; 2502 2503 #ifdef BFD64 2504 firstreal = first; 2505 current = arch->archive_head; 2506 last_elt = current; /* Last element arch seen. */ 2507 for (count = 0; count < orl_count; count++) 2508 { 2509 unsigned int offset; 2510 2511 if (map[count].u.abfd != last_elt) 2512 { 2513 do 2514 { 2515 struct areltdata *ared = arch_eltdata (current); 2516 2517 firstreal += (ared->parsed_size + ared->extra_size 2518 + sizeof (struct ar_hdr)); 2519 firstreal += firstreal % 2; 2520 current = current->archive_next; 2521 } 2522 while (current != map[count].u.abfd); 2523 } 2524 2525 /* The archive file format only has 4 bytes to store the offset 2526 of the member. Generate 64-bit archive if an archive is past 2527 its 4Gb limit. */ 2528 offset = (unsigned int) firstreal; 2529 if (firstreal != (file_ptr) offset) 2530 return _bfd_archive_64_bit_write_armap (arch, elength, map, 2531 orl_count, stridx); 2532 2533 last_elt = current; 2534 } 2535 #endif 2536 2537 /* If deterministic, we use 0 as the timestamp in the map. 2538 Some linkers may require that the archive filesystem modification 2539 time is less than (or near to) the archive map timestamp. Those 2540 linkers should not be used with deterministic mode. (GNU ld and 2541 Gold do not have this restriction.) */ 2542 bfd_ardata (arch)->armap_timestamp = 0; 2543 uid = 0; 2544 gid = 0; 2545 if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0) 2546 { 2547 struct stat statbuf; 2548 2549 if (stat (arch->filename, &statbuf) == 0) 2550 bfd_ardata (arch)->armap_timestamp = (statbuf.st_mtime 2551 + ARMAP_TIME_OFFSET); 2552 uid = getuid(); 2553 gid = getgid(); 2554 } 2555 2556 memset (&hdr, ' ', sizeof (struct ar_hdr)); 2557 memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG)); 2558 bfd_ardata (arch)->armap_datepos = (SARMAG 2559 + offsetof (struct ar_hdr, ar_date[0])); 2560 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld", 2561 bfd_ardata (arch)->armap_timestamp); 2562 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", uid); 2563 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", gid); 2564 if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize)) 2565 return FALSE; 2566 memcpy (hdr.ar_fmag, ARFMAG, 2); 2567 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 2568 != sizeof (struct ar_hdr)) 2569 return FALSE; 2570 H_PUT_32 (arch, ranlibsize, temp); 2571 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp)) 2572 return FALSE; 2573 2574 firstreal = first; 2575 current = arch->archive_head; 2576 last_elt = current; /* Last element arch seen. */ 2577 for (count = 0; count < orl_count; count++) 2578 { 2579 unsigned int offset; 2580 bfd_byte buf[BSD_SYMDEF_SIZE]; 2581 2582 if (map[count].u.abfd != last_elt) 2583 { 2584 do 2585 { 2586 #if 1 2587 bfd_size_type size = arelt_size (current); 2588 if (!strncmp(arch_hdr (current)->ar_name, "#1/", 3)) 2589 size += strlen(normalize(current, current->filename)); 2590 firstreal += size + sizeof (struct ar_hdr); 2591 firstreal += size % 2; 2592 #else 2593 struct areltdata *ared = arch_eltdata (current); 2594 2595 firstreal += (ared->parsed_size + ared->extra_size 2596 + sizeof (struct ar_hdr)); 2597 firstreal += firstreal % 2; 2598 #endif 2599 current = current->archive_next; 2600 } 2601 while (current != map[count].u.abfd); 2602 } 2603 2604 /* The archive file format only has 4 bytes to store the offset 2605 of the member. Check to make sure that firstreal has not grown 2606 too big. */ 2607 offset = (unsigned int) firstreal; 2608 if (firstreal != (file_ptr) offset) 2609 { 2610 bfd_set_error (bfd_error_file_truncated); 2611 return FALSE; 2612 } 2613 2614 last_elt = current; 2615 H_PUT_32 (arch, map[count].namidx, buf); 2616 H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE); 2617 if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch) 2618 != BSD_SYMDEF_SIZE) 2619 return FALSE; 2620 } 2621 2622 /* Now write the strings themselves. */ 2623 H_PUT_32 (arch, stringsize, temp); 2624 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp)) 2625 return FALSE; 2626 for (count = 0; count < orl_count; count++) 2627 { 2628 size_t len = strlen (*map[count].name) + 1; 2629 2630 if (bfd_bwrite (*map[count].name, len, arch) != len) 2631 return FALSE; 2632 } 2633 2634 /* The spec sez this should be a newline. But in order to be 2635 bug-compatible for sun's ar we use a null. */ 2636 if (padit) 2637 { 2638 if (bfd_bwrite ("", 1, arch) != 1) 2639 return FALSE; 2640 } 2641 2642 return TRUE; 2643 } 2644 2645 /* At the end of archive file handling, update the timestamp in the 2646 file, so the linker will accept it. 2647 2648 Return TRUE if the timestamp was OK, or an unusual problem happened. 2649 Return FALSE if we updated the timestamp. */ 2650 2651 bfd_boolean 2652 _bfd_archive_bsd_update_armap_timestamp (bfd *arch) 2653 { 2654 struct stat archstat; 2655 struct ar_hdr hdr; 2656 2657 /* If creating deterministic archives, just leave the timestamp as-is. */ 2658 if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) != 0) 2659 return TRUE; 2660 2661 /* Flush writes, get last-write timestamp from file, and compare it 2662 to the timestamp IN the file. */ 2663 bfd_flush (arch); 2664 if (bfd_stat (arch, &archstat) == -1) 2665 { 2666 bfd_perror (_("Reading archive file mod timestamp")); 2667 2668 /* Can't read mod time for some reason. */ 2669 return TRUE; 2670 } 2671 if (((long) archstat.st_mtime) <= bfd_ardata (arch)->armap_timestamp) 2672 /* OK by the linker's rules. */ 2673 return TRUE; 2674 2675 /* Update the timestamp. */ 2676 bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET; 2677 2678 /* Prepare an ASCII version suitable for writing. */ 2679 memset (hdr.ar_date, ' ', sizeof (hdr.ar_date)); 2680 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld", 2681 bfd_ardata (arch)->armap_timestamp); 2682 2683 /* Write it into the file. */ 2684 bfd_ardata (arch)->armap_datepos = (SARMAG 2685 + offsetof (struct ar_hdr, ar_date[0])); 2686 if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0 2687 || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch) 2688 != sizeof (hdr.ar_date))) 2689 { 2690 bfd_perror (_("Writing updated armap timestamp")); 2691 2692 /* Some error while writing. */ 2693 return TRUE; 2694 } 2695 2696 /* We updated the timestamp successfully. */ 2697 return FALSE; 2698 } 2699 2700 /* A coff armap looks like : 2701 lARMAG 2702 struct ar_hdr with name = '/' 2703 number of symbols 2704 offset of file for symbol 0 2705 offset of file for symbol 1 2706 2707 offset of file for symbol n-1 2708 symbol name 0 2709 symbol name 1 2710 2711 symbol name n-1 */ 2712 2713 bfd_boolean 2714 coff_write_armap (bfd *arch, 2715 unsigned int elength, 2716 struct orl *map, 2717 unsigned int symbol_count, 2718 int stridx) 2719 { 2720 /* The size of the ranlib is the number of exported symbols in the 2721 archive * the number of bytes in an int, + an int for the count. */ 2722 unsigned int ranlibsize = (symbol_count * 4) + 4; 2723 unsigned int stringsize = stridx; 2724 unsigned int mapsize = stringsize + ranlibsize; 2725 file_ptr archive_member_file_ptr; 2726 file_ptr first_archive_member_file_ptr; 2727 bfd *current = arch->archive_head; 2728 unsigned int count; 2729 struct ar_hdr hdr; 2730 int padit = mapsize & 1; 2731 2732 if (padit) 2733 mapsize++; 2734 2735 /* Work out where the first object file will go in the archive. */ 2736 first_archive_member_file_ptr = (mapsize 2737 + elength 2738 + sizeof (struct ar_hdr) 2739 + SARMAG); 2740 2741 #ifdef BFD64 2742 current = arch->archive_head; 2743 count = 0; 2744 archive_member_file_ptr = first_archive_member_file_ptr; 2745 while (current != NULL && count < symbol_count) 2746 { 2747 /* For each symbol which is used defined in this object, write 2748 out the object file's address in the archive. */ 2749 2750 while (count < symbol_count && map[count].u.abfd == current) 2751 { 2752 unsigned int offset = (unsigned int) archive_member_file_ptr; 2753 2754 /* Generate 64-bit archive if an archive is past its 4Gb 2755 limit. */ 2756 if (archive_member_file_ptr != (file_ptr) offset) 2757 return _bfd_archive_64_bit_write_armap (arch, elength, map, 2758 symbol_count, stridx); 2759 count++; 2760 } 2761 archive_member_file_ptr += sizeof (struct ar_hdr); 2762 if (! bfd_is_thin_archive (arch)) 2763 { 2764 /* Add size of this archive entry. */ 2765 archive_member_file_ptr += arelt_size (current); 2766 /* Remember about the even alignment. */ 2767 archive_member_file_ptr += archive_member_file_ptr % 2; 2768 } 2769 current = current->archive_next; 2770 } 2771 #endif 2772 2773 memset (&hdr, ' ', sizeof (struct ar_hdr)); 2774 hdr.ar_name[0] = '/'; 2775 if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize)) 2776 return FALSE; 2777 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld", 2778 ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0 2779 ? time (NULL) : 0)); 2780 /* This, at least, is what Intel coff sets the values to. */ 2781 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0); 2782 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0); 2783 _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0); 2784 memcpy (hdr.ar_fmag, ARFMAG, 2); 2785 2786 /* Write the ar header for this item and the number of symbols. */ 2787 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 2788 != sizeof (struct ar_hdr)) 2789 return FALSE; 2790 2791 if (!bfd_write_bigendian_4byte_int (arch, symbol_count)) 2792 return FALSE; 2793 2794 /* Two passes, first write the file offsets for each symbol - 2795 remembering that each offset is on a two byte boundary. */ 2796 2797 /* Write out the file offset for the file associated with each 2798 symbol, and remember to keep the offsets padded out. */ 2799 2800 current = arch->archive_head; 2801 count = 0; 2802 archive_member_file_ptr = first_archive_member_file_ptr; 2803 while (current != NULL && count < symbol_count) 2804 { 2805 /* For each symbol which is used defined in this object, write 2806 out the object file's address in the archive. */ 2807 2808 while (count < symbol_count && map[count].u.abfd == current) 2809 { 2810 unsigned int offset = (unsigned int) archive_member_file_ptr; 2811 2812 /* Catch an attempt to grow an archive past its 4Gb limit. */ 2813 if (archive_member_file_ptr != (file_ptr) offset) 2814 { 2815 bfd_set_error (bfd_error_file_truncated); 2816 return FALSE; 2817 } 2818 if (!bfd_write_bigendian_4byte_int (arch, offset)) 2819 return FALSE; 2820 count++; 2821 } 2822 archive_member_file_ptr += sizeof (struct ar_hdr); 2823 if (! bfd_is_thin_archive (arch)) 2824 { 2825 /* Add size of this archive entry. */ 2826 archive_member_file_ptr += arelt_size (current); 2827 /* Remember about the even alignment. */ 2828 archive_member_file_ptr += archive_member_file_ptr % 2; 2829 } 2830 current = current->archive_next; 2831 } 2832 2833 /* Now write the strings themselves. */ 2834 for (count = 0; count < symbol_count; count++) 2835 { 2836 size_t len = strlen (*map[count].name) + 1; 2837 2838 if (bfd_bwrite (*map[count].name, len, arch) != len) 2839 return FALSE; 2840 } 2841 2842 /* The spec sez this should be a newline. But in order to be 2843 bug-compatible for arc960 we use a null. */ 2844 if (padit) 2845 { 2846 if (bfd_bwrite ("", 1, arch) != 1) 2847 return FALSE; 2848 } 2849 2850 return TRUE; 2851 } 2852 2853 static int 2854 archive_close_worker (void **slot, void *inf ATTRIBUTE_UNUSED) 2855 { 2856 struct ar_cache *ent = (struct ar_cache *) *slot; 2857 2858 bfd_close_all_done (ent->arbfd); 2859 return 1; 2860 } 2861 2862 bfd_boolean 2863 _bfd_archive_close_and_cleanup (bfd *abfd) 2864 { 2865 if (bfd_read_p (abfd) && abfd->format == bfd_archive) 2866 { 2867 bfd *nbfd; 2868 bfd *next; 2869 htab_t htab; 2870 2871 /* Close nested archives (if this bfd is a thin archive). */ 2872 for (nbfd = abfd->nested_archives; nbfd; nbfd = next) 2873 { 2874 next = nbfd->archive_next; 2875 bfd_close (nbfd); 2876 } 2877 2878 htab = bfd_ardata (abfd)->cache; 2879 if (htab) 2880 { 2881 htab_traverse_noresize (htab, archive_close_worker, NULL); 2882 htab_delete (htab); 2883 bfd_ardata (abfd)->cache = NULL; 2884 } 2885 } 2886 if (arch_eltdata (abfd) != NULL) 2887 { 2888 struct areltdata *ared = arch_eltdata (abfd); 2889 htab_t htab = (htab_t) ared->parent_cache; 2890 2891 if (htab) 2892 { 2893 struct ar_cache ent; 2894 void **slot; 2895 2896 ent.ptr = ared->key; 2897 slot = htab_find_slot (htab, &ent, NO_INSERT); 2898 if (slot != NULL) 2899 { 2900 BFD_ASSERT (((struct ar_cache *) *slot)->arbfd == abfd); 2901 htab_clear_slot (htab, slot); 2902 } 2903 } 2904 } 2905 if (abfd->is_linker_output) 2906 (*abfd->link.hash->hash_table_free) (abfd); 2907 2908 return TRUE; 2909 } 2910