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