1 /* BFD back-end for archive files (libraries). 2 Copyright (C) 1990-2018 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 the archive and the previous return 771 value to return a created BFD to the next contained element. NULL 772 is returned when there are no more. 773 Note - if you want to process the bfd returned by this call be 774 sure to call bfd_check_format() on it first. 775 */ 776 777 bfd * 778 bfd_openr_next_archived_file (bfd *archive, bfd *last_file) 779 { 780 if ((bfd_get_format (archive) != bfd_archive) 781 || (archive->direction == write_direction)) 782 { 783 bfd_set_error (bfd_error_invalid_operation); 784 return NULL; 785 } 786 787 return BFD_SEND (archive, 788 openr_next_archived_file, (archive, last_file)); 789 } 790 791 bfd * 792 bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file) 793 { 794 ufile_ptr filestart; 795 796 if (!last_file) 797 filestart = bfd_ardata (archive)->first_file_filepos; 798 else 799 { 800 filestart = last_file->proxy_origin; 801 if (! bfd_is_thin_archive (archive)) 802 #if 0 803 /* OLD CODE */ 804 filestart += size; 805 /* Pad to an even boundary... 806 Note that last_file->origin can be odd in the case of 807 BSD-4.4-style element with a long odd size. */ 808 if (!strncmp(arch_hdr (last_file)->ar_name, "#1/", 3)) 809 size += strlen(normalize(last_file, last_file->filename)); 810 filestart += size % 2; 811 #endif 812 { 813 bfd_size_type size = arelt_size (last_file); 814 815 filestart += size; 816 /* Pad to an even boundary... 817 Note that last_file->origin can be odd in the case of 818 BSD-4.4-style element with a long odd size. */ 819 filestart += filestart % 2; 820 if (filestart < last_file->proxy_origin) 821 { 822 /* Prevent looping. See PR19256. */ 823 bfd_set_error (bfd_error_malformed_archive); 824 return NULL; 825 } 826 } 827 } 828 829 return _bfd_get_elt_at_filepos (archive, filestart); 830 } 831 832 const bfd_target * 833 bfd_generic_archive_p (bfd *abfd) 834 { 835 struct artdata *tdata_hold; 836 char armag[SARMAG + 1]; 837 bfd_size_type amt; 838 839 if (bfd_bread (armag, SARMAG, abfd) != SARMAG) 840 { 841 if (bfd_get_error () != bfd_error_system_call) 842 bfd_set_error (bfd_error_wrong_format); 843 return NULL; 844 } 845 846 bfd_is_thin_archive (abfd) = (strncmp (armag, ARMAGT, SARMAG) == 0); 847 848 if (strncmp (armag, ARMAG, SARMAG) != 0 849 && strncmp (armag, ARMAGB, SARMAG) != 0 850 && ! bfd_is_thin_archive (abfd)) 851 { 852 bfd_set_error (bfd_error_wrong_format); 853 if (abfd->format == bfd_archive) 854 abfd->format = bfd_unknown; 855 return NULL; 856 } 857 858 tdata_hold = bfd_ardata (abfd); 859 860 amt = sizeof (struct artdata); 861 bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt); 862 if (bfd_ardata (abfd) == NULL) 863 { 864 bfd_ardata (abfd) = tdata_hold; 865 return NULL; 866 } 867 868 bfd_ardata (abfd)->first_file_filepos = SARMAG; 869 /* Cleared by bfd_zalloc above. 870 bfd_ardata (abfd)->cache = NULL; 871 bfd_ardata (abfd)->archive_head = NULL; 872 bfd_ardata (abfd)->symdefs = NULL; 873 bfd_ardata (abfd)->extended_names = NULL; 874 bfd_ardata (abfd)->extended_names_size = 0; 875 bfd_ardata (abfd)->tdata = NULL; */ 876 877 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd)) 878 || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) 879 { 880 if (bfd_get_error () != bfd_error_system_call) 881 bfd_set_error (bfd_error_wrong_format); 882 bfd_release (abfd, bfd_ardata (abfd)); 883 bfd_ardata (abfd) = tdata_hold; 884 return NULL; 885 } 886 887 if (abfd->target_defaulted && bfd_has_map (abfd)) 888 { 889 bfd *first; 890 891 /* This archive has a map, so we may presume that the contents 892 are object files. Make sure that if the first file in the 893 archive can be recognized as an object file, it is for this 894 target. If not, assume that this is the wrong format. If 895 the first file is not an object file, somebody is doing 896 something weird, and we permit it so that ar -t will work. 897 898 This is done because any normal format will recognize any 899 normal archive, regardless of the format of the object files. 900 We do accept an empty archive. */ 901 902 first = bfd_openr_next_archived_file (abfd, NULL); 903 if (first != NULL) 904 { 905 first->target_defaulted = FALSE; 906 if (bfd_check_format (first, bfd_object) 907 && first->xvec != abfd->xvec) 908 bfd_set_error (bfd_error_wrong_object_format); 909 /* And we ought to close `first' here too. */ 910 } 911 } 912 913 return abfd->xvec; 914 } 915 916 /* Some constants for a 32 bit BSD archive structure. We do not 917 support 64 bit archives presently; so far as I know, none actually 918 exist. Supporting them would require changing these constants, and 919 changing some H_GET_32 to H_GET_64. */ 920 921 /* The size of an external symdef structure. */ 922 #define BSD_SYMDEF_SIZE 8 923 924 /* The offset from the start of a symdef structure to the file offset. */ 925 #define BSD_SYMDEF_OFFSET_SIZE 4 926 927 /* The size of the symdef count. */ 928 #define BSD_SYMDEF_COUNT_SIZE 4 929 930 /* The size of the string count. */ 931 #define BSD_STRING_COUNT_SIZE 4 932 933 /* Read a BSD-style archive symbol table. Returns FALSE on error, 934 TRUE otherwise. */ 935 936 static bfd_boolean 937 do_slurp_bsd_armap (bfd *abfd) 938 { 939 struct areltdata *mapdata; 940 unsigned int counter; 941 bfd_byte *raw_armap, *rbase; 942 struct artdata *ardata = bfd_ardata (abfd); 943 char *stringbase; 944 bfd_size_type parsed_size, amt; 945 carsym *set; 946 947 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 948 if (mapdata == NULL) 949 return FALSE; 950 parsed_size = mapdata->parsed_size; 951 free (mapdata); 952 /* PR 17512: file: 883ff754. */ 953 /* PR 17512: file: 0458885f. */ 954 if (parsed_size < 4) 955 return FALSE; 956 957 raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size); 958 if (raw_armap == NULL) 959 return FALSE; 960 961 if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size) 962 { 963 if (bfd_get_error () != bfd_error_system_call) 964 bfd_set_error (bfd_error_malformed_archive); 965 byebye: 966 bfd_release (abfd, raw_armap); 967 return FALSE; 968 } 969 970 ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE; 971 if (ardata->symdef_count * BSD_SYMDEF_SIZE > 972 parsed_size - BSD_SYMDEF_COUNT_SIZE) 973 { 974 /* Probably we're using the wrong byte ordering. */ 975 bfd_set_error (bfd_error_wrong_format); 976 goto byebye; 977 } 978 979 ardata->cache = 0; 980 rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE; 981 stringbase = ((char *) rbase 982 + ardata->symdef_count * BSD_SYMDEF_SIZE 983 + BSD_STRING_COUNT_SIZE); 984 amt = ardata->symdef_count * sizeof (carsym); 985 ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt); 986 if (!ardata->symdefs) 987 return FALSE; 988 989 for (counter = 0, set = ardata->symdefs; 990 counter < ardata->symdef_count; 991 counter++, set++, rbase += BSD_SYMDEF_SIZE) 992 { 993 set->name = H_GET_32 (abfd, rbase) + stringbase; 994 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); 995 } 996 997 ardata->first_file_filepos = bfd_tell (abfd); 998 /* Pad to an even boundary if you have to. */ 999 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 1000 /* FIXME, we should provide some way to free raw_ardata when 1001 we are done using the strings from it. For now, it seems 1002 to be allocated on an objalloc anyway... */ 1003 bfd_has_map (abfd) = TRUE; 1004 return TRUE; 1005 } 1006 1007 /* Read a COFF archive symbol table. Returns FALSE on error, TRUE 1008 otherwise. */ 1009 1010 static bfd_boolean 1011 do_slurp_coff_armap (bfd *abfd) 1012 { 1013 struct areltdata *mapdata; 1014 int *raw_armap, *rawptr; 1015 struct artdata *ardata = bfd_ardata (abfd); 1016 char *stringbase; 1017 bfd_size_type stringsize; 1018 bfd_size_type parsed_size; 1019 carsym *carsyms; 1020 bfd_size_type nsymz; /* Number of symbols in armap. */ 1021 bfd_vma (*swap) (const void *); 1022 char int_buf[sizeof (long)]; 1023 bfd_size_type carsym_size, ptrsize; 1024 unsigned int i; 1025 1026 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 1027 if (mapdata == NULL) 1028 return FALSE; 1029 parsed_size = mapdata->parsed_size; 1030 free (mapdata); 1031 1032 if (bfd_bread (int_buf, 4, abfd) != 4) 1033 { 1034 if (bfd_get_error () != bfd_error_system_call) 1035 bfd_set_error (bfd_error_malformed_archive); 1036 return FALSE; 1037 } 1038 /* It seems that all numeric information in a coff archive is always 1039 in big endian format, nomatter the host or target. */ 1040 swap = bfd_getb32; 1041 nsymz = bfd_getb32 (int_buf); 1042 stringsize = parsed_size - (4 * nsymz) - 4; 1043 1044 /* ... except that some archive formats are broken, and it may be our 1045 fault - the i960 little endian coff sometimes has big and sometimes 1046 little, because our tools changed. Here's a horrible hack to clean 1047 up the crap. */ 1048 1049 if (stringsize > 0xfffff 1050 && bfd_get_arch (abfd) == bfd_arch_i960 1051 && bfd_get_flavour (abfd) == bfd_target_coff_flavour) 1052 { 1053 /* This looks dangerous, let's do it the other way around. */ 1054 nsymz = bfd_getl32 (int_buf); 1055 stringsize = parsed_size - (4 * nsymz) - 4; 1056 swap = bfd_getl32; 1057 } 1058 1059 /* The coff armap must be read sequentially. So we construct a 1060 bsd-style one in core all at once, for simplicity. */ 1061 1062 if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym)) 1063 return FALSE; 1064 1065 carsym_size = (nsymz * sizeof (carsym)); 1066 ptrsize = (4 * nsymz); 1067 1068 if (carsym_size + stringsize + 1 <= carsym_size) 1069 return FALSE; 1070 1071 ardata->symdefs = (struct carsym *) bfd_zalloc (abfd, 1072 carsym_size + stringsize + 1); 1073 if (ardata->symdefs == NULL) 1074 return FALSE; 1075 carsyms = ardata->symdefs; 1076 stringbase = ((char *) ardata->symdefs) + carsym_size; 1077 1078 /* Allocate and read in the raw offsets. */ 1079 raw_armap = (int *) bfd_alloc (abfd, ptrsize); 1080 if (raw_armap == NULL) 1081 goto release_symdefs; 1082 if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize 1083 || (bfd_bread (stringbase, stringsize, abfd) != stringsize)) 1084 { 1085 if (bfd_get_error () != bfd_error_system_call) 1086 bfd_set_error (bfd_error_malformed_archive); 1087 goto release_raw_armap; 1088 } 1089 1090 /* OK, build the carsyms. */ 1091 for (i = 0; i < nsymz && stringsize > 0; i++) 1092 { 1093 bfd_size_type len; 1094 1095 rawptr = raw_armap + i; 1096 carsyms->file_offset = swap ((bfd_byte *) rawptr); 1097 carsyms->name = stringbase; 1098 /* PR 17512: file: 4a1d50c1. */ 1099 len = strnlen (stringbase, stringsize); 1100 if (len < stringsize) 1101 len ++; 1102 stringbase += len; 1103 stringsize -= len; 1104 carsyms++; 1105 } 1106 *stringbase = 0; 1107 1108 ardata->symdef_count = nsymz; 1109 ardata->first_file_filepos = bfd_tell (abfd); 1110 /* Pad to an even boundary if you have to. */ 1111 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 1112 1113 bfd_has_map (abfd) = TRUE; 1114 bfd_release (abfd, raw_armap); 1115 1116 /* Check for a second archive header (as used by PE). */ 1117 { 1118 struct areltdata *tmp; 1119 1120 bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET); 1121 tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd); 1122 if (tmp != NULL) 1123 { 1124 if (tmp->arch_header[0] == '/' 1125 && tmp->arch_header[1] == ' ') 1126 { 1127 ardata->first_file_filepos += 1128 (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1; 1129 } 1130 free (tmp); 1131 } 1132 } 1133 1134 return TRUE; 1135 1136 release_raw_armap: 1137 bfd_release (abfd, raw_armap); 1138 release_symdefs: 1139 bfd_release (abfd, (ardata)->symdefs); 1140 return FALSE; 1141 } 1142 1143 /* This routine can handle either coff-style or bsd-style armaps 1144 (archive symbol table). Returns FALSE on error, TRUE otherwise */ 1145 1146 bfd_boolean 1147 bfd_slurp_armap (bfd *abfd) 1148 { 1149 char nextname[17]; 1150 int i = bfd_bread (nextname, 16, abfd); 1151 1152 if (i == 0) 1153 return TRUE; 1154 if (i != 16) 1155 return FALSE; 1156 1157 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 1158 return FALSE; 1159 1160 if (CONST_STRNEQ (nextname, "__.SYMDEF ") 1161 || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */ 1162 return do_slurp_bsd_armap (abfd); 1163 else if (CONST_STRNEQ (nextname, "/ ")) 1164 return do_slurp_coff_armap (abfd); 1165 else if (CONST_STRNEQ (nextname, "/SYM64/ ")) 1166 { 1167 /* 64bit (Irix 6) archive. */ 1168 #ifdef BFD64 1169 return _bfd_archive_64_bit_slurp_armap (abfd); 1170 #else 1171 bfd_set_error (bfd_error_wrong_format); 1172 return FALSE; 1173 #endif 1174 } 1175 else if (CONST_STRNEQ (nextname, "#1/20 ")) 1176 { 1177 /* Mach-O has a special name for armap when the map is sorted by name. 1178 However because this name has a space it is slightly more difficult 1179 to check it. */ 1180 struct ar_hdr hdr; 1181 char extname[21]; 1182 1183 if (bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr)) 1184 return FALSE; 1185 /* Read the extended name. We know its length. */ 1186 if (bfd_bread (extname, 20, abfd) != 20) 1187 return FALSE; 1188 if (bfd_seek (abfd, -(file_ptr) (sizeof (hdr) + 20), SEEK_CUR) != 0) 1189 return FALSE; 1190 extname[20] = 0; 1191 if (CONST_STRNEQ (extname, "__.SYMDEF SORTED") 1192 || CONST_STRNEQ (extname, "__.SYMDEF")) 1193 return do_slurp_bsd_armap (abfd); 1194 } 1195 1196 bfd_has_map (abfd) = FALSE; 1197 return TRUE; 1198 } 1199 1200 /* Returns FALSE on error, TRUE otherwise. */ 1201 /* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the 1202 header is in a slightly different order and the map name is '/'. 1203 This flavour is used by hp300hpux. */ 1204 1205 #define HPUX_SYMDEF_COUNT_SIZE 2 1206 1207 bfd_boolean 1208 bfd_slurp_bsd_armap_f2 (bfd *abfd) 1209 { 1210 struct areltdata *mapdata; 1211 char nextname[17]; 1212 unsigned int counter; 1213 bfd_byte *raw_armap, *rbase; 1214 struct artdata *ardata = bfd_ardata (abfd); 1215 char *stringbase; 1216 unsigned int stringsize; 1217 unsigned int left; 1218 bfd_size_type amt; 1219 carsym *set; 1220 int i = bfd_bread (nextname, 16, abfd); 1221 1222 if (i == 0) 1223 return TRUE; 1224 if (i != 16) 1225 return FALSE; 1226 1227 /* The archive has at least 16 bytes in it. */ 1228 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 1229 return FALSE; 1230 1231 if (CONST_STRNEQ (nextname, "__.SYMDEF ") 1232 || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */ 1233 return do_slurp_bsd_armap (abfd); 1234 1235 if (! CONST_STRNEQ (nextname, "/ ")) 1236 { 1237 bfd_has_map (abfd) = FALSE; 1238 return TRUE; 1239 } 1240 1241 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 1242 if (mapdata == NULL) 1243 return FALSE; 1244 1245 if (mapdata->parsed_size < HPUX_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE) 1246 { 1247 free (mapdata); 1248 wrong_format: 1249 bfd_set_error (bfd_error_wrong_format); 1250 byebye: 1251 return FALSE; 1252 } 1253 left = mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE - BSD_STRING_COUNT_SIZE; 1254 1255 amt = mapdata->parsed_size; 1256 free (mapdata); 1257 1258 raw_armap = (bfd_byte *) bfd_zalloc (abfd, amt); 1259 if (raw_armap == NULL) 1260 goto byebye; 1261 1262 if (bfd_bread (raw_armap, amt, abfd) != amt) 1263 { 1264 if (bfd_get_error () != bfd_error_system_call) 1265 bfd_set_error (bfd_error_malformed_archive); 1266 goto byebye; 1267 } 1268 1269 ardata->symdef_count = H_GET_16 (abfd, raw_armap); 1270 1271 ardata->cache = 0; 1272 1273 stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE); 1274 if (stringsize > left) 1275 goto wrong_format; 1276 left -= stringsize; 1277 1278 /* Skip sym count and string sz. */ 1279 stringbase = ((char *) raw_armap 1280 + HPUX_SYMDEF_COUNT_SIZE 1281 + BSD_STRING_COUNT_SIZE); 1282 rbase = (bfd_byte *) stringbase + stringsize; 1283 amt = ardata->symdef_count * BSD_SYMDEF_SIZE; 1284 if (amt > left) 1285 goto wrong_format; 1286 1287 ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt); 1288 if (!ardata->symdefs) 1289 return FALSE; 1290 1291 for (counter = 0, set = ardata->symdefs; 1292 counter < ardata->symdef_count; 1293 counter++, set++, rbase += BSD_SYMDEF_SIZE) 1294 { 1295 set->name = H_GET_32 (abfd, rbase) + stringbase; 1296 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); 1297 } 1298 1299 ardata->first_file_filepos = bfd_tell (abfd); 1300 /* Pad to an even boundary if you have to. */ 1301 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 1302 /* FIXME, we should provide some way to free raw_ardata when 1303 we are done using the strings from it. For now, it seems 1304 to be allocated on an objalloc anyway... */ 1305 bfd_has_map (abfd) = TRUE; 1306 return TRUE; 1307 } 1308 1309 /** Extended name table. 1310 1311 Normally archives support only 14-character filenames. 1312 1313 Intel has extended the format: longer names are stored in a special 1314 element (the first in the archive, or second if there is an armap); 1315 the name in the ar_hdr is replaced by <space><index into filename 1316 element>. Index is the P.R. of an int (decimal). Data General have 1317 extended the format by using the prefix // for the special element. */ 1318 1319 /* Returns FALSE on error, TRUE otherwise. */ 1320 1321 bfd_boolean 1322 _bfd_slurp_extended_name_table (bfd *abfd) 1323 { 1324 char nextname[17]; 1325 struct areltdata *namedata; 1326 bfd_size_type amt; 1327 1328 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ, 1329 we probably don't want to return TRUE. */ 1330 if (bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET) != 0) 1331 return FALSE; 1332 1333 if (bfd_bread (nextname, 16, abfd) == 16) 1334 { 1335 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 1336 return FALSE; 1337 1338 if (! CONST_STRNEQ (nextname, "ARFILENAMES/ ") 1339 && ! CONST_STRNEQ (nextname, "// ")) 1340 { 1341 bfd_ardata (abfd)->extended_names = NULL; 1342 bfd_ardata (abfd)->extended_names_size = 0; 1343 return TRUE; 1344 } 1345 1346 namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 1347 if (namedata == NULL) 1348 return FALSE; 1349 1350 amt = namedata->parsed_size; 1351 if (amt + 1 == 0) 1352 goto byebye; 1353 1354 bfd_ardata (abfd)->extended_names_size = amt; 1355 bfd_ardata (abfd)->extended_names = (char *) bfd_zalloc (abfd, amt + 1); 1356 if (bfd_ardata (abfd)->extended_names == NULL) 1357 { 1358 byebye: 1359 free (namedata); 1360 bfd_ardata (abfd)->extended_names = NULL; 1361 bfd_ardata (abfd)->extended_names_size = 0; 1362 return FALSE; 1363 } 1364 1365 if (bfd_bread (bfd_ardata (abfd)->extended_names, amt, abfd) != amt) 1366 { 1367 if (bfd_get_error () != bfd_error_system_call) 1368 bfd_set_error (bfd_error_malformed_archive); 1369 bfd_release (abfd, (bfd_ardata (abfd)->extended_names)); 1370 bfd_ardata (abfd)->extended_names = NULL; 1371 goto byebye; 1372 } 1373 1374 /* Since the archive is supposed to be printable if it contains 1375 text, the entries in the list are newline-padded, not null 1376 padded. In SVR4-style archives, the names also have a 1377 trailing '/'. DOS/NT created archive often have \ in them 1378 We'll fix all problems here. */ 1379 { 1380 char *ext_names = bfd_ardata (abfd)->extended_names; 1381 char *temp = ext_names; 1382 char *limit = temp + namedata->parsed_size; 1383 1384 for (; temp < limit; ++temp) 1385 { 1386 if (*temp == ARFMAG[1]) 1387 temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0'; 1388 if (*temp == '\\') 1389 *temp = '/'; 1390 } 1391 *limit = '\0'; 1392 } 1393 1394 /* Pad to an even boundary if you have to. */ 1395 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd); 1396 bfd_ardata (abfd)->first_file_filepos += 1397 (bfd_ardata (abfd)->first_file_filepos) % 2; 1398 1399 free (namedata); 1400 } 1401 return TRUE; 1402 } 1403 1404 #ifdef VMS 1405 1406 /* Return a copy of the stuff in the filename between any :]> and a 1407 semicolon. */ 1408 1409 static const char * 1410 normalize (bfd *abfd, const char *file) 1411 { 1412 const char *first; 1413 const char *last; 1414 char *copy; 1415 1416 first = file + strlen (file) - 1; 1417 last = first + 1; 1418 1419 while (first != file) 1420 { 1421 if (*first == ';') 1422 last = first; 1423 if (*first == ':' || *first == ']' || *first == '>') 1424 { 1425 first++; 1426 break; 1427 } 1428 first--; 1429 } 1430 1431 copy = bfd_alloc (abfd, last - first + 1); 1432 if (copy == NULL) 1433 return NULL; 1434 1435 memcpy (copy, first, last - first); 1436 copy[last - first] = 0; 1437 1438 return copy; 1439 } 1440 1441 #else 1442 static const char * 1443 normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file) 1444 { 1445 return lbasename (file); 1446 } 1447 #endif 1448 1449 /* Adjust a relative path name based on the reference path. 1450 For example: 1451 1452 Relative path Reference path Result 1453 ------------- -------------- ------ 1454 bar.o lib.a bar.o 1455 foo/bar.o lib.a foo/bar.o 1456 bar.o foo/lib.a ../bar.o 1457 foo/bar.o baz/lib.a ../foo/bar.o 1458 bar.o ../lib.a <parent of current dir>/bar.o 1459 ; ../bar.o ../lib.a bar.o 1460 ; ../bar.o lib.a ../bar.o 1461 foo/bar.o ../lib.a <parent of current dir>/foo/bar.o 1462 bar.o ../../lib.a <grandparent>/<parent>/bar.o 1463 bar.o foo/baz/lib.a ../../bar.o 1464 1465 Note - the semicolons above are there to prevent the BFD chew 1466 utility from interpreting those lines as prototypes to put into 1467 the autogenerated bfd.h header... 1468 1469 Note - the string is returned in a static buffer. */ 1470 1471 static const char * 1472 adjust_relative_path (const char * path, const char * ref_path) 1473 { 1474 static char *pathbuf = NULL; 1475 static unsigned int pathbuf_len = 0; 1476 const char *pathp; 1477 const char *refp; 1478 char * lpath; 1479 char * rpath; 1480 unsigned int len; 1481 unsigned int dir_up = 0; 1482 unsigned int dir_down = 0; 1483 char *newp; 1484 char * pwd = getpwd (); 1485 const char * down; 1486 1487 /* Remove symlinks, '.' and '..' from the paths, if possible. */ 1488 lpath = lrealpath (path); 1489 pathp = lpath == NULL ? path : lpath; 1490 1491 rpath = lrealpath (ref_path); 1492 refp = rpath == NULL ? ref_path : rpath; 1493 1494 /* Remove common leading path elements. */ 1495 for (;;) 1496 { 1497 const char *e1 = pathp; 1498 const char *e2 = refp; 1499 1500 while (*e1 && ! IS_DIR_SEPARATOR (*e1)) 1501 ++e1; 1502 while (*e2 && ! IS_DIR_SEPARATOR (*e2)) 1503 ++e2; 1504 if (*e1 == '\0' || *e2 == '\0' || e1 - pathp != e2 - refp 1505 || filename_ncmp (pathp, refp, e1 - pathp) != 0) 1506 break; 1507 pathp = e1 + 1; 1508 refp = e2 + 1; 1509 } 1510 1511 len = strlen (pathp) + 1; 1512 /* For each leading path element in the reference path, 1513 insert "../" into the path. */ 1514 for (; *refp; ++refp) 1515 if (IS_DIR_SEPARATOR (*refp)) 1516 { 1517 /* PR 12710: If the path element is "../" then instead of 1518 inserting "../" we need to insert the name of the directory 1519 at the current level. */ 1520 if (refp > ref_path + 1 1521 && refp[-1] == '.' 1522 && refp[-2] == '.') 1523 dir_down ++; 1524 else 1525 dir_up ++; 1526 } 1527 1528 /* If the lrealpath calls above succeeded then we should never 1529 see dir_up and dir_down both being non-zero. */ 1530 1531 len += 3 * dir_up; 1532 1533 if (dir_down) 1534 { 1535 down = pwd + strlen (pwd) - 1; 1536 1537 while (dir_down && down > pwd) 1538 { 1539 if (IS_DIR_SEPARATOR (*down)) 1540 --dir_down; 1541 } 1542 BFD_ASSERT (dir_down == 0); 1543 len += strlen (down) + 1; 1544 } 1545 else 1546 down = NULL; 1547 1548 if (len > pathbuf_len) 1549 { 1550 if (pathbuf != NULL) 1551 free (pathbuf); 1552 pathbuf_len = 0; 1553 pathbuf = (char *) bfd_malloc (len); 1554 if (pathbuf == NULL) 1555 goto out; 1556 pathbuf_len = len; 1557 } 1558 1559 newp = pathbuf; 1560 while (dir_up-- > 0) 1561 { 1562 /* FIXME: Support Windows style path separators as well. */ 1563 strcpy (newp, "../"); 1564 newp += 3; 1565 } 1566 1567 if (down) 1568 sprintf (newp, "%s/%s", down, pathp); 1569 else 1570 strcpy (newp, pathp); 1571 1572 out: 1573 free (lpath); 1574 free (rpath); 1575 return pathbuf; 1576 } 1577 1578 /* Build a BFD style extended name table. */ 1579 1580 bfd_boolean 1581 _bfd_archive_bsd_construct_extended_name_table (bfd *abfd, 1582 char **tabloc, 1583 bfd_size_type *tablen, 1584 const char **name) 1585 { 1586 *name = "ARFILENAMES/"; 1587 return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen); 1588 } 1589 1590 /* Build an SVR4 style extended name table. */ 1591 1592 bfd_boolean 1593 _bfd_archive_coff_construct_extended_name_table (bfd *abfd, 1594 char **tabloc, 1595 bfd_size_type *tablen, 1596 const char **name) 1597 { 1598 *name = "//"; 1599 return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen); 1600 } 1601 1602 /* Follows archive_head and produces an extended name table if 1603 necessary. Returns (in tabloc) a pointer to an extended name 1604 table, and in tablen the length of the table. If it makes an entry 1605 it clobbers the filename so that the element may be written without 1606 further massage. Returns TRUE if it ran successfully, FALSE if 1607 something went wrong. A successful return may still involve a 1608 zero-length tablen! */ 1609 1610 bfd_boolean 1611 _bfd_construct_extended_name_table (bfd *abfd, 1612 bfd_boolean trailing_slash, 1613 char **tabloc, 1614 bfd_size_type *tablen) 1615 { 1616 unsigned int maxname = ar_maxnamelen (abfd); 1617 bfd_size_type total_namelen = 0; 1618 bfd *current; 1619 char *strptr; 1620 const char *last_filename; 1621 long last_stroff; 1622 1623 *tablen = 0; 1624 last_filename = NULL; 1625 1626 /* Figure out how long the table should be. */ 1627 for (current = abfd->archive_head; 1628 current != NULL; 1629 current = current->archive_next) 1630 { 1631 const char *normal; 1632 unsigned int thislen; 1633 1634 if (bfd_is_thin_archive (abfd)) 1635 { 1636 const char *filename = current->filename; 1637 1638 /* If the element being added is a member of another archive 1639 (i.e., we are flattening), use the containing archive's name. */ 1640 if (current->my_archive 1641 && ! bfd_is_thin_archive (current->my_archive)) 1642 filename = current->my_archive->filename; 1643 1644 /* If the path is the same as the previous path seen, 1645 reuse it. This can happen when flattening a thin 1646 archive that contains other archives. */ 1647 if (last_filename && filename_cmp (last_filename, filename) == 0) 1648 continue; 1649 1650 last_filename = filename; 1651 1652 /* If the path is relative, adjust it relative to 1653 the containing archive. */ 1654 if (! IS_ABSOLUTE_PATH (filename) 1655 && ! IS_ABSOLUTE_PATH (abfd->filename)) 1656 normal = adjust_relative_path (filename, abfd->filename); 1657 else 1658 normal = filename; 1659 1660 /* In a thin archive, always store the full pathname 1661 in the extended name table. */ 1662 total_namelen += strlen (normal) + 1; 1663 if (trailing_slash) 1664 /* Leave room for trailing slash. */ 1665 ++total_namelen; 1666 1667 continue; 1668 } 1669 1670 normal = normalize (current, current->filename); 1671 if (normal == NULL) 1672 return FALSE; 1673 1674 thislen = strlen (normal); 1675 1676 if (thislen > maxname 1677 && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0) 1678 thislen = maxname; 1679 1680 if (thislen > maxname) 1681 { 1682 /* Add one to leave room for \n. */ 1683 total_namelen += thislen + 1; 1684 if (trailing_slash) 1685 { 1686 /* Leave room for trailing slash. */ 1687 ++total_namelen; 1688 } 1689 } 1690 else 1691 { 1692 struct ar_hdr *hdr = arch_hdr (current); 1693 if (filename_ncmp (normal, hdr->ar_name, thislen) != 0 1694 || (thislen < sizeof hdr->ar_name 1695 && hdr->ar_name[thislen] != ar_padchar (current))) 1696 { 1697 /* Must have been using extended format even though it 1698 didn't need to. Fix it to use normal format. */ 1699 memcpy (hdr->ar_name, normal, thislen); 1700 if (thislen < maxname 1701 || (thislen == maxname && thislen < sizeof hdr->ar_name)) 1702 hdr->ar_name[thislen] = ar_padchar (current); 1703 } 1704 } 1705 } 1706 1707 if (total_namelen == 0) 1708 return TRUE; 1709 1710 *tabloc = (char *) bfd_zalloc (abfd, total_namelen); 1711 if (*tabloc == NULL) 1712 return FALSE; 1713 1714 *tablen = total_namelen; 1715 strptr = *tabloc; 1716 1717 last_filename = NULL; 1718 last_stroff = 0; 1719 1720 for (current = abfd->archive_head; 1721 current != NULL; 1722 current = current->archive_next) 1723 { 1724 const char *normal; 1725 unsigned int thislen; 1726 long stroff; 1727 const char *filename = current->filename; 1728 1729 if (bfd_is_thin_archive (abfd)) 1730 { 1731 /* If the element being added is a member of another archive 1732 (i.e., we are flattening), use the containing archive's name. */ 1733 if (current->my_archive 1734 && ! bfd_is_thin_archive (current->my_archive)) 1735 filename = current->my_archive->filename; 1736 /* If the path is the same as the previous path seen, 1737 reuse it. This can happen when flattening a thin 1738 archive that contains other archives. 1739 If the path is relative, adjust it relative to 1740 the containing archive. */ 1741 if (last_filename && filename_cmp (last_filename, filename) == 0) 1742 normal = last_filename; 1743 else if (! IS_ABSOLUTE_PATH (filename) 1744 && ! IS_ABSOLUTE_PATH (abfd->filename)) 1745 normal = adjust_relative_path (filename, abfd->filename); 1746 else 1747 normal = filename; 1748 } 1749 else 1750 { 1751 normal = normalize (current, filename); 1752 if (normal == NULL) 1753 return FALSE; 1754 } 1755 1756 thislen = strlen (normal); 1757 if (thislen > maxname || bfd_is_thin_archive (abfd)) 1758 { 1759 /* Works for now; may need to be re-engineered if we 1760 encounter an oddball archive format and want to 1761 generalise this hack. */ 1762 struct ar_hdr *hdr = arch_hdr (current); 1763 if (normal == last_filename) 1764 stroff = last_stroff; 1765 else 1766 { 1767 strcpy (strptr, normal); 1768 if (! trailing_slash) 1769 strptr[thislen] = ARFMAG[1]; 1770 else 1771 { 1772 strptr[thislen] = '/'; 1773 strptr[thislen + 1] = ARFMAG[1]; 1774 } 1775 stroff = strptr - *tabloc; 1776 last_stroff = stroff; 1777 } 1778 hdr->ar_name[0] = ar_padchar (current); 1779 if (bfd_is_thin_archive (abfd) && current->origin > 0) 1780 { 1781 int len = snprintf (hdr->ar_name + 1, maxname - 1, "%-ld:", 1782 stroff); 1783 _bfd_ar_spacepad (hdr->ar_name + 1 + len, maxname - 1 - len, 1784 "%-ld", 1785 current->origin - sizeof (struct ar_hdr)); 1786 } 1787 else 1788 _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", stroff); 1789 if (normal != last_filename) 1790 { 1791 strptr += thislen + 1; 1792 if (trailing_slash) 1793 ++strptr; 1794 last_filename = filename; 1795 } 1796 } 1797 } 1798 1799 return TRUE; 1800 } 1801 1802 /* Do not construct an extended name table but transforms name field into 1803 its extended form. */ 1804 1805 bfd_boolean 1806 _bfd_archive_bsd44_construct_extended_name_table (bfd *abfd, 1807 char **tabloc, 1808 bfd_size_type *tablen, 1809 const char **name) 1810 { 1811 unsigned int maxname = ar_maxnamelen (abfd); 1812 bfd *current; 1813 1814 *tablen = 0; 1815 *tabloc = NULL; 1816 *name = NULL; 1817 1818 for (current = abfd->archive_head; 1819 current != NULL; 1820 current = current->archive_next) 1821 { 1822 const char *normal = normalize (current, current->filename); 1823 int has_space = 0; 1824 unsigned int len; 1825 1826 if (normal == NULL) 1827 return FALSE; 1828 1829 for (len = 0; normal[len]; len++) 1830 if (normal[len] == ' ') 1831 has_space = 1; 1832 1833 if (len > maxname || has_space) 1834 { 1835 struct ar_hdr *hdr = arch_hdr (current); 1836 1837 len = (len + 3) & ~3; 1838 arch_eltdata (current)->extra_size = len; 1839 _bfd_ar_spacepad (hdr->ar_name, maxname, "#1/%lu", len); 1840 } 1841 } 1842 1843 return TRUE; 1844 } 1845 1846 /* Write an archive header. */ 1847 1848 bfd_boolean 1849 _bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd) 1850 { 1851 struct ar_hdr *hdr = arch_hdr (abfd); 1852 1853 if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr)) 1854 return FALSE; 1855 return TRUE; 1856 } 1857 1858 /* Write an archive header using BSD4.4 convention. */ 1859 1860 bfd_boolean 1861 _bfd_bsd44_write_ar_hdr (bfd *archive, bfd *abfd) 1862 { 1863 struct ar_hdr *hdr = arch_hdr (abfd); 1864 1865 if (is_bsd44_extended_name (hdr->ar_name)) 1866 { 1867 /* This is a BSD 4.4 extended name. */ 1868 const char *fullname = normalize (abfd, abfd->filename); 1869 unsigned int len = strlen (fullname); 1870 unsigned int padded_len = (len + 3) & ~3; 1871 1872 BFD_ASSERT (padded_len == arch_eltdata (abfd)->extra_size); 1873 1874 if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size), 1875 arch_eltdata (abfd)->parsed_size + padded_len)) 1876 return FALSE; 1877 1878 if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr)) 1879 return FALSE; 1880 1881 if (bfd_bwrite (fullname, len, archive) != len) 1882 return FALSE; 1883 1884 if (len & 3) 1885 { 1886 static const char pad[3] = { 0, 0, 0 }; 1887 1888 len = 4 - (len & 3); 1889 if (bfd_bwrite (pad, len, archive) != len) 1890 return FALSE; 1891 } 1892 } 1893 else 1894 { 1895 if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr)) 1896 return FALSE; 1897 } 1898 return TRUE; 1899 } 1900 1901 /* A couple of functions for creating ar_hdrs. */ 1902 1903 #ifdef HPUX_LARGE_AR_IDS 1904 /* Function to encode large UID/GID values according to HP. */ 1905 1906 static void 1907 hpux_uid_gid_encode (char str[6], long int id) 1908 { 1909 int cnt; 1910 1911 str[5] = '@' + (id & 3); 1912 id >>= 2; 1913 1914 for (cnt = 4; cnt >= 0; --cnt, id >>= 6) 1915 str[cnt] = ' ' + (id & 0x3f); 1916 } 1917 #endif /* HPUX_LARGE_AR_IDS */ 1918 1919 #ifndef HAVE_GETUID 1920 #define getuid() 0 1921 #endif 1922 1923 #ifndef HAVE_GETGID 1924 #define getgid() 0 1925 #endif 1926 1927 /* Takes a filename, returns an arelt_data for it, or NULL if it can't 1928 make one. The filename must refer to a filename in the filesystem. 1929 The filename field of the ar_hdr will NOT be initialized. If member 1930 is set, and it's an in-memory bfd, we fake it. */ 1931 1932 static struct areltdata * 1933 bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member) 1934 { 1935 struct stat status; 1936 struct areltdata *ared; 1937 struct ar_hdr *hdr; 1938 bfd_size_type amt; 1939 1940 if (member && (member->flags & BFD_IN_MEMORY) != 0) 1941 { 1942 /* Assume we just "made" the member, and fake it. */ 1943 struct bfd_in_memory *bim = (struct bfd_in_memory *) member->iostream; 1944 time (&status.st_mtime); 1945 status.st_uid = getuid (); 1946 status.st_gid = getgid (); 1947 status.st_mode = 0644; 1948 status.st_size = bim->size; 1949 } 1950 else if (stat (filename, &status) != 0) 1951 { 1952 bfd_set_error (bfd_error_system_call); 1953 return NULL; 1954 } 1955 1956 /* If the caller requested that the BFD generate deterministic output, 1957 fake values for modification time, UID, GID, and file mode. */ 1958 if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0) 1959 { 1960 status.st_mtime = 0; 1961 status.st_uid = 0; 1962 status.st_gid = 0; 1963 status.st_mode = 0644; 1964 } 1965 1966 amt = sizeof (struct ar_hdr) + sizeof (struct areltdata); 1967 ared = (struct areltdata *) bfd_zmalloc (amt); 1968 if (ared == NULL) 1969 return NULL; 1970 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata)); 1971 1972 /* ar headers are space padded, not null padded! */ 1973 memset (hdr, ' ', sizeof (struct ar_hdr)); 1974 1975 _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld", 1976 status.st_mtime); 1977 #ifdef HPUX_LARGE_AR_IDS 1978 /* HP has a very "special" way to handle UID/GID's with numeric values 1979 > 99999. */ 1980 if (status.st_uid > 99999) 1981 hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_uid); 1982 else 1983 #endif 1984 _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld", 1985 status.st_uid); 1986 #ifdef HPUX_LARGE_AR_IDS 1987 /* HP has a very "special" way to handle UID/GID's with numeric values 1988 > 99999. */ 1989 if (status.st_gid > 99999) 1990 hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_gid); 1991 else 1992 #endif 1993 _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld", 1994 status.st_gid); 1995 _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo", 1996 status.st_mode); 1997 if (status.st_size - (bfd_size_type) status.st_size != 0) 1998 { 1999 bfd_set_error (bfd_error_file_too_big); 2000 free (ared); 2001 return NULL; 2002 } 2003 if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size), status.st_size)) 2004 { 2005 free (ared); 2006 return NULL; 2007 } 2008 memcpy (hdr->ar_fmag, ARFMAG, 2); 2009 ared->parsed_size = status.st_size; 2010 ared->arch_header = (char *) hdr; 2011 2012 return ared; 2013 } 2014 2015 /* Analogous to stat call. */ 2016 2017 int 2018 bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf) 2019 { 2020 struct ar_hdr *hdr; 2021 char *aloser; 2022 2023 if (abfd->arelt_data == NULL) 2024 { 2025 bfd_set_error (bfd_error_invalid_operation); 2026 return -1; 2027 } 2028 2029 hdr = arch_hdr (abfd); 2030 /* PR 17512: file: 3d9e9fe9. */ 2031 if (hdr == NULL) 2032 return -1; 2033 #define foo(arelt, stelt, size) \ 2034 buf->stelt = strtol (hdr->arelt, &aloser, size); \ 2035 if (aloser == hdr->arelt) \ 2036 return -1; 2037 2038 /* Some platforms support special notations for large IDs. */ 2039 #ifdef HPUX_LARGE_AR_IDS 2040 # define foo2(arelt, stelt, size) \ 2041 if (hdr->arelt[5] == ' ') \ 2042 { \ 2043 foo (arelt, stelt, size); \ 2044 } \ 2045 else \ 2046 { \ 2047 int cnt; \ 2048 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \ 2049 { \ 2050 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \ 2051 return -1; \ 2052 buf->stelt <<= 6; \ 2053 buf->stelt += hdr->arelt[cnt] - ' '; \ 2054 } \ 2055 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \ 2056 return -1; \ 2057 buf->stelt <<= 2; \ 2058 buf->stelt += hdr->arelt[5] - '@'; \ 2059 } 2060 #else 2061 # define foo2(arelt, stelt, size) foo (arelt, stelt, size) 2062 #endif 2063 2064 foo (ar_date, st_mtime, 10); 2065 foo2 (ar_uid, st_uid, 10); 2066 foo2 (ar_gid, st_gid, 10); 2067 foo (ar_mode, st_mode, 8); 2068 2069 buf->st_size = arch_eltdata (abfd)->parsed_size; 2070 2071 return 0; 2072 } 2073 2074 void 2075 bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr) 2076 { 2077 /* FIXME: This interacts unpleasantly with ar's quick-append option. 2078 Fortunately ic960 users will never use that option. Fixing this 2079 is very hard; fortunately I know how to do it and will do so once 2080 intel's release is out the door. */ 2081 2082 struct ar_hdr *hdr = (struct ar_hdr *) arhdr; 2083 size_t length; 2084 const char *filename; 2085 size_t maxlen = ar_maxnamelen (abfd); 2086 2087 if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0) 2088 { 2089 bfd_bsd_truncate_arname (abfd, pathname, arhdr); 2090 return; 2091 } 2092 2093 filename = normalize (abfd, pathname); 2094 if (filename == NULL) 2095 { 2096 /* FIXME */ 2097 abort (); 2098 } 2099 2100 length = strlen (filename); 2101 2102 if (length <= maxlen) 2103 memcpy (hdr->ar_name, filename, length); 2104 2105 /* Add the padding character if there is room for it. */ 2106 if (length < maxlen 2107 || (length == maxlen && length < sizeof hdr->ar_name)) 2108 (hdr->ar_name)[length] = ar_padchar (abfd); 2109 } 2110 2111 void 2112 bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr) 2113 { 2114 struct ar_hdr *hdr = (struct ar_hdr *) arhdr; 2115 size_t length; 2116 const char *filename = lbasename (pathname); 2117 size_t maxlen = ar_maxnamelen (abfd); 2118 2119 length = strlen (filename); 2120 2121 if (length <= maxlen) 2122 memcpy (hdr->ar_name, filename, length); 2123 else 2124 { 2125 /* pathname: meet procrustes */ 2126 memcpy (hdr->ar_name, filename, maxlen); 2127 length = maxlen; 2128 } 2129 2130 if (length < maxlen) 2131 (hdr->ar_name)[length] = ar_padchar (abfd); 2132 } 2133 2134 /* Store name into ar header. Truncates the name to fit. 2135 1> strip pathname to be just the basename. 2136 2> if it's short enuf to fit, stuff it in. 2137 3> If it doesn't end with .o, truncate it to fit 2138 4> truncate it before the .o, append .o, stuff THAT in. */ 2139 2140 /* This is what gnu ar does. It's better but incompatible with the 2141 bsd ar. */ 2142 2143 void 2144 bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr) 2145 { 2146 struct ar_hdr *hdr = (struct ar_hdr *) arhdr; 2147 size_t length; 2148 const char *filename = lbasename (pathname); 2149 size_t maxlen = ar_maxnamelen (abfd); 2150 2151 length = strlen (filename); 2152 2153 if (length <= maxlen) 2154 memcpy (hdr->ar_name, filename, length); 2155 else 2156 { 2157 /* pathname: meet procrustes. */ 2158 memcpy (hdr->ar_name, filename, maxlen); 2159 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) 2160 { 2161 hdr->ar_name[maxlen - 2] = '.'; 2162 hdr->ar_name[maxlen - 1] = 'o'; 2163 } 2164 length = maxlen; 2165 } 2166 2167 if (length < 16) 2168 (hdr->ar_name)[length] = ar_padchar (abfd); 2169 } 2170 2171 /* The BFD is open for write and has its format set to bfd_archive. */ 2172 2173 bfd_boolean 2174 _bfd_write_archive_contents (bfd *arch) 2175 { 2176 bfd *current; 2177 char *etable = NULL; 2178 bfd_size_type elength = 0; 2179 const char *ename = NULL; 2180 bfd_boolean makemap = bfd_has_map (arch); 2181 /* If no .o's, don't bother to make a map. */ 2182 bfd_boolean hasobjects = FALSE; 2183 bfd_size_type wrote; 2184 int tries; 2185 char *armag; 2186 2187 /* Verify the viability of all entries; if any of them live in the 2188 filesystem (as opposed to living in an archive open for input) 2189 then construct a fresh ar_hdr for them. */ 2190 for (current = arch->archive_head; 2191 current != NULL; 2192 current = current->archive_next) 2193 { 2194 /* This check is checking the bfds for the objects we're reading 2195 from (which are usually either an object file or archive on 2196 disk), not the archive entries we're writing to. We don't 2197 actually create bfds for the archive members, we just copy 2198 them byte-wise when we write out the archive. */ 2199 if (bfd_write_p (current)) 2200 { 2201 bfd_set_error (bfd_error_invalid_operation); 2202 goto input_err; 2203 } 2204 if (!current->arelt_data) 2205 { 2206 current->arelt_data = 2207 bfd_ar_hdr_from_filesystem (arch, current->filename, current); 2208 if (!current->arelt_data) 2209 goto input_err; 2210 2211 /* Put in the file name. */ 2212 BFD_SEND (arch, _bfd_truncate_arname, 2213 (arch, current->filename, (char *) arch_hdr (current))); 2214 } 2215 2216 if (makemap && ! hasobjects) 2217 { /* Don't bother if we won't make a map! */ 2218 if ((bfd_check_format (current, bfd_object))) 2219 hasobjects = TRUE; 2220 } 2221 } 2222 2223 if (!BFD_SEND (arch, _bfd_construct_extended_name_table, 2224 (arch, &etable, &elength, &ename))) 2225 return FALSE; 2226 2227 if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0) 2228 return FALSE; 2229 armag = ARMAG; 2230 if (bfd_is_thin_archive (arch)) 2231 armag = ARMAGT; 2232 wrote = bfd_bwrite (armag, SARMAG, arch); 2233 if (wrote != SARMAG) 2234 return FALSE; 2235 2236 if (makemap && hasobjects) 2237 { 2238 if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength)) 2239 return FALSE; 2240 } 2241 2242 if (elength != 0) 2243 { 2244 struct ar_hdr hdr; 2245 2246 memset (&hdr, ' ', sizeof (struct ar_hdr)); 2247 memcpy (hdr.ar_name, ename, strlen (ename)); 2248 /* Round size up to even number in archive header. */ 2249 if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), 2250 (elength + 1) & ~(bfd_size_type) 1)) 2251 return FALSE; 2252 memcpy (hdr.ar_fmag, ARFMAG, 2); 2253 if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 2254 != sizeof (struct ar_hdr)) 2255 || bfd_bwrite (etable, elength, arch) != elength) 2256 return FALSE; 2257 if ((elength % 2) == 1) 2258 { 2259 if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1) 2260 return FALSE; 2261 } 2262 } 2263 2264 for (current = arch->archive_head; 2265 current != NULL; 2266 current = current->archive_next) 2267 { 2268 char buffer[DEFAULT_BUFFERSIZE]; 2269 bfd_size_type saved_size = arelt_size (current); 2270 bfd_size_type remaining = saved_size; 2271 struct ar_hdr *hdr = arch_hdr (current); 2272 2273 /* Write ar header. */ 2274 if (!_bfd_write_ar_hdr (arch, current)) 2275 return FALSE; 2276 /* Write filename if it is a 4.4BSD extended file, and add to size. */ 2277 if (!strncmp (hdr->ar_name, "#1/", 3)) 2278 { 2279 const char *normal = normalize (current, current->filename); 2280 unsigned int thislen = strlen (normal); 2281 if (bfd_write (normal, 1, thislen, arch) != thislen) 2282 return FALSE; 2283 saved_size += thislen; 2284 } 2285 if (bfd_is_thin_archive (arch)) 2286 continue; 2287 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0) 2288 goto input_err; 2289 2290 while (remaining) 2291 { 2292 unsigned int amt = DEFAULT_BUFFERSIZE; 2293 2294 if (amt > remaining) 2295 amt = remaining; 2296 errno = 0; 2297 if (bfd_bread (buffer, amt, current) != amt) 2298 { 2299 if (bfd_get_error () != bfd_error_system_call) 2300 bfd_set_error (bfd_error_file_truncated); 2301 goto input_err; 2302 } 2303 if (bfd_bwrite (buffer, amt, arch) != amt) 2304 return FALSE; 2305 remaining -= amt; 2306 } 2307 2308 if ((arelt_size (current) % 2) == 1) 2309 { 2310 if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1) 2311 return FALSE; 2312 } 2313 } 2314 2315 if (makemap && hasobjects) 2316 { 2317 /* Verify the timestamp in the archive file. If it would not be 2318 accepted by the linker, rewrite it until it would be. If 2319 anything odd happens, break out and just return. (The 2320 Berkeley linker checks the timestamp and refuses to read the 2321 table-of-contents if it is >60 seconds less than the file's 2322 modified-time. That painful hack requires this painful hack. */ 2323 tries = 1; 2324 do 2325 { 2326 if (bfd_update_armap_timestamp (arch)) 2327 break; 2328 _bfd_error_handler 2329 (_("Warning: writing archive was slow: rewriting timestamp\n")); 2330 } 2331 while (++tries < 6); 2332 } 2333 2334 return TRUE; 2335 2336 input_err: 2337 bfd_set_input_error (current, bfd_get_error ()); 2338 return FALSE; 2339 } 2340 2341 /* Note that the namidx for the first symbol is 0. */ 2342 2343 bfd_boolean 2344 _bfd_compute_and_write_armap (bfd *arch, unsigned int elength) 2345 { 2346 char *first_name = NULL; 2347 bfd *current; 2348 file_ptr elt_no = 0; 2349 struct orl *map = NULL; 2350 unsigned int orl_max = 1024; /* Fine initial default. */ 2351 unsigned int orl_count = 0; 2352 int stridx = 0; 2353 asymbol **syms = NULL; 2354 long syms_max = 0; 2355 bfd_boolean ret; 2356 bfd_size_type amt; 2357 2358 /* Dunno if this is the best place for this info... */ 2359 if (elength != 0) 2360 elength += sizeof (struct ar_hdr); 2361 elength += elength % 2; 2362 2363 amt = orl_max * sizeof (struct orl); 2364 map = (struct orl *) bfd_malloc (amt); 2365 if (map == NULL) 2366 goto error_return; 2367 2368 /* We put the symbol names on the arch objalloc, and then discard 2369 them when done. */ 2370 first_name = (char *) bfd_alloc (arch, 1); 2371 if (first_name == NULL) 2372 goto error_return; 2373 2374 /* Drop all the files called __.SYMDEF, we're going to make our own. */ 2375 while (arch->archive_head 2376 && strcmp (arch->archive_head->filename, "__.SYMDEF") == 0) 2377 arch->archive_head = arch->archive_head->archive_next; 2378 2379 /* Map over each element. */ 2380 for (current = arch->archive_head; 2381 current != NULL; 2382 current = current->archive_next, elt_no++) 2383 { 2384 if (bfd_check_format (current, bfd_object) 2385 && (bfd_get_file_flags (current) & HAS_SYMS) != 0) 2386 { 2387 long storage; 2388 long symcount; 2389 long src_count; 2390 2391 storage = bfd_get_symtab_upper_bound (current); 2392 if (storage < 0) 2393 goto error_return; 2394 2395 if (storage != 0) 2396 { 2397 if (storage > syms_max) 2398 { 2399 if (syms_max > 0) 2400 free (syms); 2401 syms_max = storage; 2402 syms = (asymbol **) bfd_malloc (syms_max); 2403 if (syms == NULL) 2404 goto error_return; 2405 } 2406 symcount = bfd_canonicalize_symtab (current, syms); 2407 if (symcount < 0) 2408 goto error_return; 2409 2410 /* Now map over all the symbols, picking out the ones we 2411 want. */ 2412 for (src_count = 0; src_count < symcount; src_count++) 2413 { 2414 flagword flags = (syms[src_count])->flags; 2415 asection *sec = syms[src_count]->section; 2416 2417 if (((flags & (BSF_GLOBAL 2418 | BSF_WEAK 2419 | BSF_INDIRECT 2420 | BSF_GNU_UNIQUE)) != 0 2421 || bfd_is_com_section (sec)) 2422 && ! bfd_is_und_section (sec)) 2423 { 2424 bfd_size_type namelen; 2425 struct orl *new_map; 2426 2427 /* This symbol will go into the archive header. */ 2428 if (orl_count == orl_max) 2429 { 2430 orl_max *= 2; 2431 amt = orl_max * sizeof (struct orl); 2432 new_map = (struct orl *) bfd_realloc (map, amt); 2433 if (new_map == NULL) 2434 goto error_return; 2435 2436 map = new_map; 2437 } 2438 2439 if (syms[src_count]->name[0] == '_' 2440 && syms[src_count]->name[1] == '_' 2441 && strcmp (syms[src_count]->name 2442 + (syms[src_count]->name[2] == '_'), 2443 "__gnu_lto_slim") == 0) 2444 _bfd_error_handler 2445 (_("%B: plugin needed to handle lto object"), 2446 current); 2447 namelen = strlen (syms[src_count]->name); 2448 amt = sizeof (char *); 2449 map[orl_count].name = (char **) bfd_alloc (arch, amt); 2450 if (map[orl_count].name == NULL) 2451 goto error_return; 2452 *(map[orl_count].name) = (char *) bfd_alloc (arch, 2453 namelen + 1); 2454 if (*(map[orl_count].name) == NULL) 2455 goto error_return; 2456 strcpy (*(map[orl_count].name), syms[src_count]->name); 2457 map[orl_count].u.abfd = current; 2458 map[orl_count].namidx = stridx; 2459 2460 stridx += namelen + 1; 2461 ++orl_count; 2462 } 2463 } 2464 } 2465 2466 /* Now ask the BFD to free up any cached information, so we 2467 don't fill all of memory with symbol tables. */ 2468 if (! bfd_free_cached_info (current)) 2469 goto error_return; 2470 } 2471 } 2472 2473 /* OK, now we have collected all the data, let's write them out. */ 2474 ret = BFD_SEND (arch, write_armap, 2475 (arch, elength, map, orl_count, stridx)); 2476 2477 if (syms_max > 0) 2478 free (syms); 2479 if (map != NULL) 2480 free (map); 2481 if (first_name != NULL) 2482 bfd_release (arch, first_name); 2483 2484 return ret; 2485 2486 error_return: 2487 if (syms_max > 0) 2488 free (syms); 2489 if (map != NULL) 2490 free (map); 2491 if (first_name != NULL) 2492 bfd_release (arch, first_name); 2493 2494 return FALSE; 2495 } 2496 2497 bfd_boolean 2498 _bfd_bsd_write_armap (bfd *arch, 2499 unsigned int elength, 2500 struct orl *map, 2501 unsigned int orl_count, 2502 int stridx) 2503 { 2504 int padit = stridx & 1; 2505 unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE; 2506 unsigned int stringsize = stridx + padit; 2507 /* Include 8 bytes to store ranlibsize and stringsize in output. */ 2508 unsigned int mapsize = ranlibsize + stringsize + 8; 2509 file_ptr firstreal, first; 2510 bfd *current; 2511 bfd *last_elt; 2512 bfd_byte temp[4]; 2513 unsigned int count; 2514 struct ar_hdr hdr; 2515 long uid, gid; 2516 2517 first = mapsize + elength + sizeof (struct ar_hdr) + SARMAG; 2518 2519 #ifdef BFD64 2520 firstreal = first; 2521 current = arch->archive_head; 2522 last_elt = current; /* Last element arch seen. */ 2523 for (count = 0; count < orl_count; count++) 2524 { 2525 unsigned int offset; 2526 2527 if (map[count].u.abfd != last_elt) 2528 { 2529 do 2530 { 2531 struct areltdata *ared = arch_eltdata (current); 2532 2533 firstreal += (ared->parsed_size + ared->extra_size 2534 + sizeof (struct ar_hdr)); 2535 firstreal += firstreal % 2; 2536 current = current->archive_next; 2537 } 2538 while (current != map[count].u.abfd); 2539 } 2540 2541 /* The archive file format only has 4 bytes to store the offset 2542 of the member. Generate 64-bit archive if an archive is past 2543 its 4Gb limit. */ 2544 offset = (unsigned int) firstreal; 2545 if (firstreal != (file_ptr) offset) 2546 return _bfd_archive_64_bit_write_armap (arch, elength, map, 2547 orl_count, stridx); 2548 2549 last_elt = current; 2550 } 2551 #endif 2552 2553 /* If deterministic, we use 0 as the timestamp in the map. 2554 Some linkers may require that the archive filesystem modification 2555 time is less than (or near to) the archive map timestamp. Those 2556 linkers should not be used with deterministic mode. (GNU ld and 2557 Gold do not have this restriction.) */ 2558 bfd_ardata (arch)->armap_timestamp = 0; 2559 uid = 0; 2560 gid = 0; 2561 if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0) 2562 { 2563 struct stat statbuf; 2564 2565 if (stat (arch->filename, &statbuf) == 0) 2566 bfd_ardata (arch)->armap_timestamp = (statbuf.st_mtime 2567 + ARMAP_TIME_OFFSET); 2568 uid = getuid(); 2569 gid = getgid(); 2570 } 2571 2572 memset (&hdr, ' ', sizeof (struct ar_hdr)); 2573 memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG)); 2574 bfd_ardata (arch)->armap_datepos = (SARMAG 2575 + offsetof (struct ar_hdr, ar_date[0])); 2576 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld", 2577 bfd_ardata (arch)->armap_timestamp); 2578 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", uid); 2579 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", gid); 2580 if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize)) 2581 return FALSE; 2582 memcpy (hdr.ar_fmag, ARFMAG, 2); 2583 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 2584 != sizeof (struct ar_hdr)) 2585 return FALSE; 2586 H_PUT_32 (arch, ranlibsize, temp); 2587 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp)) 2588 return FALSE; 2589 2590 firstreal = first; 2591 current = arch->archive_head; 2592 last_elt = current; /* Last element arch seen. */ 2593 for (count = 0; count < orl_count; count++) 2594 { 2595 unsigned int offset; 2596 bfd_byte buf[BSD_SYMDEF_SIZE]; 2597 2598 if (map[count].u.abfd != last_elt) 2599 { 2600 do 2601 { 2602 #if 1 2603 bfd_size_type size = arelt_size (current); 2604 if (!strncmp(arch_hdr (current)->ar_name, "#1/", 3)) 2605 size += strlen(normalize(current, current->filename)); 2606 firstreal += size + sizeof (struct ar_hdr); 2607 firstreal += size % 2; 2608 #else 2609 struct areltdata *ared = arch_eltdata (current); 2610 2611 firstreal += (ared->parsed_size + ared->extra_size 2612 + sizeof (struct ar_hdr)); 2613 firstreal += firstreal % 2; 2614 #endif 2615 current = current->archive_next; 2616 } 2617 while (current != map[count].u.abfd); 2618 } 2619 2620 /* The archive file format only has 4 bytes to store the offset 2621 of the member. Check to make sure that firstreal has not grown 2622 too big. */ 2623 offset = (unsigned int) firstreal; 2624 if (firstreal != (file_ptr) offset) 2625 { 2626 bfd_set_error (bfd_error_file_truncated); 2627 return FALSE; 2628 } 2629 2630 last_elt = current; 2631 H_PUT_32 (arch, map[count].namidx, buf); 2632 H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE); 2633 if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch) 2634 != BSD_SYMDEF_SIZE) 2635 return FALSE; 2636 } 2637 2638 /* Now write the strings themselves. */ 2639 H_PUT_32 (arch, stringsize, temp); 2640 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp)) 2641 return FALSE; 2642 for (count = 0; count < orl_count; count++) 2643 { 2644 size_t len = strlen (*map[count].name) + 1; 2645 2646 if (bfd_bwrite (*map[count].name, len, arch) != len) 2647 return FALSE; 2648 } 2649 2650 /* The spec sez this should be a newline. But in order to be 2651 bug-compatible for sun's ar we use a null. */ 2652 if (padit) 2653 { 2654 if (bfd_bwrite ("", 1, arch) != 1) 2655 return FALSE; 2656 } 2657 2658 return TRUE; 2659 } 2660 2661 /* At the end of archive file handling, update the timestamp in the 2662 file, so the linker will accept it. 2663 2664 Return TRUE if the timestamp was OK, or an unusual problem happened. 2665 Return FALSE if we updated the timestamp. */ 2666 2667 bfd_boolean 2668 _bfd_archive_bsd_update_armap_timestamp (bfd *arch) 2669 { 2670 struct stat archstat; 2671 struct ar_hdr hdr; 2672 2673 /* If creating deterministic archives, just leave the timestamp as-is. */ 2674 if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) != 0) 2675 return TRUE; 2676 2677 /* Flush writes, get last-write timestamp from file, and compare it 2678 to the timestamp IN the file. */ 2679 bfd_flush (arch); 2680 if (bfd_stat (arch, &archstat) == -1) 2681 { 2682 bfd_perror (_("Reading archive file mod timestamp")); 2683 2684 /* Can't read mod time for some reason. */ 2685 return TRUE; 2686 } 2687 if (((long) archstat.st_mtime) <= bfd_ardata (arch)->armap_timestamp) 2688 /* OK by the linker's rules. */ 2689 return TRUE; 2690 2691 /* Update the timestamp. */ 2692 bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET; 2693 2694 /* Prepare an ASCII version suitable for writing. */ 2695 memset (hdr.ar_date, ' ', sizeof (hdr.ar_date)); 2696 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld", 2697 bfd_ardata (arch)->armap_timestamp); 2698 2699 /* Write it into the file. */ 2700 bfd_ardata (arch)->armap_datepos = (SARMAG 2701 + offsetof (struct ar_hdr, ar_date[0])); 2702 if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0 2703 || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch) 2704 != sizeof (hdr.ar_date))) 2705 { 2706 bfd_perror (_("Writing updated armap timestamp")); 2707 2708 /* Some error while writing. */ 2709 return TRUE; 2710 } 2711 2712 /* We updated the timestamp successfully. */ 2713 return FALSE; 2714 } 2715 2716 /* A coff armap looks like : 2717 lARMAG 2718 struct ar_hdr with name = '/' 2719 number of symbols 2720 offset of file for symbol 0 2721 offset of file for symbol 1 2722 2723 offset of file for symbol n-1 2724 symbol name 0 2725 symbol name 1 2726 2727 symbol name n-1 */ 2728 2729 bfd_boolean 2730 _bfd_coff_write_armap (bfd *arch, 2731 unsigned int elength, 2732 struct orl *map, 2733 unsigned int symbol_count, 2734 int stridx) 2735 { 2736 /* The size of the ranlib is the number of exported symbols in the 2737 archive * the number of bytes in an int, + an int for the count. */ 2738 unsigned int ranlibsize = (symbol_count * 4) + 4; 2739 unsigned int stringsize = stridx; 2740 unsigned int mapsize = stringsize + ranlibsize; 2741 file_ptr archive_member_file_ptr; 2742 file_ptr first_archive_member_file_ptr; 2743 bfd *current = arch->archive_head; 2744 unsigned int count; 2745 struct ar_hdr hdr; 2746 int padit = mapsize & 1; 2747 2748 if (padit) 2749 mapsize++; 2750 2751 /* Work out where the first object file will go in the archive. */ 2752 first_archive_member_file_ptr = (mapsize 2753 + elength 2754 + sizeof (struct ar_hdr) 2755 + SARMAG); 2756 2757 #ifdef BFD64 2758 current = arch->archive_head; 2759 count = 0; 2760 archive_member_file_ptr = first_archive_member_file_ptr; 2761 while (current != NULL && count < symbol_count) 2762 { 2763 /* For each symbol which is used defined in this object, write 2764 out the object file's address in the archive. */ 2765 2766 while (count < symbol_count && map[count].u.abfd == current) 2767 { 2768 unsigned int offset = (unsigned int) archive_member_file_ptr; 2769 2770 /* Generate 64-bit archive if an archive is past its 4Gb 2771 limit. */ 2772 if (archive_member_file_ptr != (file_ptr) offset) 2773 return _bfd_archive_64_bit_write_armap (arch, elength, map, 2774 symbol_count, stridx); 2775 count++; 2776 } 2777 archive_member_file_ptr += sizeof (struct ar_hdr); 2778 if (! bfd_is_thin_archive (arch)) 2779 { 2780 /* Add size of this archive entry. */ 2781 archive_member_file_ptr += arelt_size (current); 2782 /* Remember about the even alignment. */ 2783 archive_member_file_ptr += archive_member_file_ptr % 2; 2784 } 2785 current = current->archive_next; 2786 } 2787 #endif 2788 2789 memset (&hdr, ' ', sizeof (struct ar_hdr)); 2790 hdr.ar_name[0] = '/'; 2791 if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize)) 2792 return FALSE; 2793 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld", 2794 ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0 2795 ? time (NULL) : 0)); 2796 /* This, at least, is what Intel coff sets the values to. */ 2797 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0); 2798 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0); 2799 _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0); 2800 memcpy (hdr.ar_fmag, ARFMAG, 2); 2801 2802 /* Write the ar header for this item and the number of symbols. */ 2803 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 2804 != sizeof (struct ar_hdr)) 2805 return FALSE; 2806 2807 if (!bfd_write_bigendian_4byte_int (arch, symbol_count)) 2808 return FALSE; 2809 2810 /* Two passes, first write the file offsets for each symbol - 2811 remembering that each offset is on a two byte boundary. */ 2812 2813 /* Write out the file offset for the file associated with each 2814 symbol, and remember to keep the offsets padded out. */ 2815 2816 current = arch->archive_head; 2817 count = 0; 2818 archive_member_file_ptr = first_archive_member_file_ptr; 2819 while (current != NULL && count < symbol_count) 2820 { 2821 /* For each symbol which is used defined in this object, write 2822 out the object file's address in the archive. */ 2823 2824 while (count < symbol_count && map[count].u.abfd == current) 2825 { 2826 unsigned int offset = (unsigned int) archive_member_file_ptr; 2827 2828 /* Catch an attempt to grow an archive past its 4Gb limit. */ 2829 if (archive_member_file_ptr != (file_ptr) offset) 2830 { 2831 bfd_set_error (bfd_error_file_truncated); 2832 return FALSE; 2833 } 2834 if (!bfd_write_bigendian_4byte_int (arch, offset)) 2835 return FALSE; 2836 count++; 2837 } 2838 archive_member_file_ptr += sizeof (struct ar_hdr); 2839 if (! bfd_is_thin_archive (arch)) 2840 { 2841 /* Add size of this archive entry. */ 2842 archive_member_file_ptr += arelt_size (current); 2843 /* Remember about the even alignment. */ 2844 archive_member_file_ptr += archive_member_file_ptr % 2; 2845 } 2846 current = current->archive_next; 2847 } 2848 2849 /* Now write the strings themselves. */ 2850 for (count = 0; count < symbol_count; count++) 2851 { 2852 size_t len = strlen (*map[count].name) + 1; 2853 2854 if (bfd_bwrite (*map[count].name, len, arch) != len) 2855 return FALSE; 2856 } 2857 2858 /* The spec sez this should be a newline. But in order to be 2859 bug-compatible for arc960 we use a null. */ 2860 if (padit) 2861 { 2862 if (bfd_bwrite ("", 1, arch) != 1) 2863 return FALSE; 2864 } 2865 2866 return TRUE; 2867 } 2868 2869 static int 2870 archive_close_worker (void **slot, void *inf ATTRIBUTE_UNUSED) 2871 { 2872 struct ar_cache *ent = (struct ar_cache *) *slot; 2873 2874 bfd_close_all_done (ent->arbfd); 2875 return 1; 2876 } 2877 2878 bfd_boolean 2879 _bfd_archive_close_and_cleanup (bfd *abfd) 2880 { 2881 if (bfd_read_p (abfd) && abfd->format == bfd_archive) 2882 { 2883 bfd *nbfd; 2884 bfd *next; 2885 htab_t htab; 2886 2887 /* Close nested archives (if this bfd is a thin archive). */ 2888 for (nbfd = abfd->nested_archives; nbfd; nbfd = next) 2889 { 2890 next = nbfd->archive_next; 2891 bfd_close (nbfd); 2892 } 2893 2894 htab = bfd_ardata (abfd)->cache; 2895 if (htab) 2896 { 2897 htab_traverse_noresize (htab, archive_close_worker, NULL); 2898 htab_delete (htab); 2899 bfd_ardata (abfd)->cache = NULL; 2900 } 2901 } 2902 if (arch_eltdata (abfd) != NULL) 2903 { 2904 struct areltdata *ared = arch_eltdata (abfd); 2905 htab_t htab = (htab_t) ared->parent_cache; 2906 2907 if (htab) 2908 { 2909 struct ar_cache ent; 2910 void **slot; 2911 2912 ent.ptr = ared->key; 2913 slot = htab_find_slot (htab, &ent, NO_INSERT); 2914 if (slot != NULL) 2915 { 2916 BFD_ASSERT (((struct ar_cache *) *slot)->arbfd == abfd); 2917 htab_clear_slot (htab, slot); 2918 } 2919 } 2920 } 2921 if (abfd->is_linker_output) 2922 (*abfd->link.hash->hash_table_free) (abfd); 2923 2924 return TRUE; 2925 } 2926