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