1 /* BFD back-end for archive files (libraries). 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 3 2000, 2001, 2002, 2003, 2004, 2005 4 Free Software Foundation, Inc. 5 Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault. 6 7 This file is part of BFD, the Binary File Descriptor library. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23 /* 24 @setfilename archive-info 25 SECTION 26 Archives 27 28 DESCRIPTION 29 An archive (or library) is just another BFD. It has a symbol 30 table, although there's not much a user program will do with it. 31 32 The big difference between an archive BFD and an ordinary BFD 33 is that the archive doesn't have sections. Instead it has a 34 chain of BFDs that are considered its contents. These BFDs can 35 be manipulated like any other. The BFDs contained in an 36 archive opened for reading will all be opened for reading. You 37 may put either input or output BFDs into an archive opened for 38 output; they will be handled correctly when the archive is closed. 39 40 Use <<bfd_openr_next_archived_file>> to step through 41 the contents of an archive opened for input. You don't 42 have to read the entire archive if you don't want 43 to! Read it until you find what you want. 44 45 Archive contents of output BFDs are chained through the 46 <<next>> pointer in a BFD. The first one is findable through 47 the <<archive_head>> slot of the archive. Set it with 48 <<bfd_set_archive_head>> (q.v.). A given BFD may be in only one 49 open output archive at a time. 50 51 As expected, the BFD archive code is more general than the 52 archive code of any given environment. BFD archives may 53 contain files of different formats (e.g., a.out and coff) and 54 even different architectures. You may even place archives 55 recursively into archives! 56 57 This can cause unexpected confusion, since some archive 58 formats are more expressive than others. For instance, Intel 59 COFF archives can preserve long filenames; SunOS a.out archives 60 cannot. If you move a file from the first to the second 61 format and back again, the filename may be truncated. 62 Likewise, different a.out environments have different 63 conventions as to how they truncate filenames, whether they 64 preserve directory names in filenames, etc. When 65 interoperating with native tools, be sure your files are 66 homogeneous. 67 68 Beware: most of these formats do not react well to the 69 presence of spaces in filenames. We do the best we can, but 70 can't always handle this case due to restrictions in the format of 71 archives. Many Unix utilities are braindead in regards to 72 spaces and such in filenames anyway, so this shouldn't be much 73 of a restriction. 74 75 Archives are supported in BFD in <<archive.c>>. 76 77 SUBSECTION 78 Archive functions 79 */ 80 81 /* Assumes: 82 o - all archive elements start on an even boundary, newline padded; 83 o - all arch headers are char *; 84 o - all arch headers are the same size (across architectures). 85 */ 86 87 /* Some formats provide a way to cram a long filename into the short 88 (16 chars) space provided by a BSD archive. The trick is: make a 89 special "file" in the front of the archive, sort of like the SYMDEF 90 entry. If the filename is too long to fit, put it in the extended 91 name table, and use its index as the filename. To prevent 92 confusion prepend the index with a space. This means you can't 93 have filenames that start with a space, but then again, many Unix 94 utilities can't handle that anyway. 95 96 This scheme unfortunately requires that you stand on your head in 97 order to write an archive since you need to put a magic file at the 98 front, and need to touch every entry to do so. C'est la vie. 99 100 We support two variants of this idea: 101 The SVR4 format (extended name table is named "//"), 102 and an extended pseudo-BSD variant (extended name table is named 103 "ARFILENAMES/"). The origin of the latter format is uncertain. 104 105 BSD 4.4 uses a third scheme: It writes a long filename 106 directly after the header. This allows 'ar q' to work. 107 We currently can read BSD 4.4 archives, but not write them. 108 */ 109 110 /* Summary of archive member names: 111 112 Symbol table (must be first): 113 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib. 114 "/ " - Symbol table, system 5 style. 115 116 Long name table (must be before regular file members): 117 "// " - Long name table, System 5 R4 style. 118 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4). 119 120 Regular file members with short names: 121 "filename.o/ " - Regular file, System 5 style (embedded spaces ok). 122 "filename.o " - Regular file, Berkeley style (no embedded spaces). 123 124 Regular files with long names (or embedded spaces, for BSD variants): 125 "/18 " - SVR4 style, name at offset 18 in name table. 126 "#1/23 " - Long name (or embedded spaces) 23 characters long, 127 BSD 4.4 style, full name follows header. 128 Implemented for reading, not writing. 129 " 18 " - Long name 18 characters long, extended pseudo-BSD. 130 */ 131 132 #include "bfd.h" 133 #include "sysdep.h" 134 #include "libiberty.h" 135 #include "libbfd.h" 136 #include "aout/ar.h" 137 #include "aout/ranlib.h" 138 #include "safe-ctype.h" 139 #include "hashtab.h" 140 141 #ifndef errno 142 extern int errno; 143 #endif 144 145 /* We keep a cache of archive filepointers to archive elements to 146 speed up searching the archive by filepos. We only add an entry to 147 the cache when we actually read one. We also don't sort the cache; 148 it's generally short enough to search linearly. 149 Note that the pointers here point to the front of the ar_hdr, not 150 to the front of the contents! */ 151 struct ar_cache { 152 file_ptr ptr; 153 bfd *arbfd; 154 }; 155 156 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char) 157 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen) 158 159 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data)) 160 #define arch_hdr(bfd) ((struct ar_hdr *) arch_eltdata(bfd)->arch_header) 161 162 void 163 _bfd_ar_spacepad (char *p, size_t n, const char *fmt, long val) 164 { 165 static char buf[20]; 166 size_t len; 167 snprintf (buf, sizeof (buf), fmt, val); 168 len = strlen (buf); 169 if (len < n) 170 { 171 memcpy (p, buf, len); 172 memset (p + len, ' ', n - len); 173 } 174 else 175 memcpy (p, buf, n); 176 } 177 178 void 179 _bfd_ar_spacepadll (char *p, size_t n, const char *fmt, long long val) 180 { 181 static char buf[20]; 182 size_t len; 183 snprintf (buf, sizeof (buf), fmt, val); 184 len = strlen (buf); 185 if (len < n) 186 { 187 memcpy (p, buf, len); 188 memset (p + len, ' ', n - len); 189 } 190 else 191 memcpy (p, buf, n); 192 } 193 194 bfd_boolean 195 _bfd_generic_mkarchive (bfd *abfd) 196 { 197 bfd_size_type amt = sizeof (struct artdata); 198 199 abfd->tdata.aout_ar_data = bfd_zalloc (abfd, amt); 200 if (bfd_ardata (abfd) == NULL) 201 return FALSE; 202 203 /* Already cleared by bfd_zalloc above. 204 bfd_ardata (abfd)->cache = NULL; 205 bfd_ardata (abfd)->archive_head = NULL; 206 bfd_ardata (abfd)->symdefs = NULL; 207 bfd_ardata (abfd)->extended_names = NULL; 208 bfd_ardata (abfd)->extended_names_size = 0; 209 bfd_ardata (abfd)->tdata = NULL; */ 210 211 return TRUE; 212 } 213 214 /* 215 FUNCTION 216 bfd_get_next_mapent 217 218 SYNOPSIS 219 symindex bfd_get_next_mapent 220 (bfd *abfd, symindex previous, carsym **sym); 221 222 DESCRIPTION 223 Step through archive @var{abfd}'s symbol table (if it 224 has one). Successively update @var{sym} with the next symbol's 225 information, returning that symbol's (internal) index into the 226 symbol table. 227 228 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get 229 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already 230 got the last one. 231 232 A <<carsym>> is a canonical archive symbol. The only 233 user-visible element is its name, a null-terminated string. 234 */ 235 236 symindex 237 bfd_get_next_mapent (bfd *abfd, symindex prev, carsym **entry) 238 { 239 if (!bfd_has_map (abfd)) 240 { 241 bfd_set_error (bfd_error_invalid_operation); 242 return BFD_NO_MORE_SYMBOLS; 243 } 244 245 if (prev == BFD_NO_MORE_SYMBOLS) 246 prev = 0; 247 else 248 ++prev; 249 if (prev >= bfd_ardata (abfd)->symdef_count) 250 return BFD_NO_MORE_SYMBOLS; 251 252 *entry = (bfd_ardata (abfd)->symdefs + prev); 253 return prev; 254 } 255 256 /* To be called by backends only. */ 257 258 bfd * 259 _bfd_create_empty_archive_element_shell (bfd *obfd) 260 { 261 return _bfd_new_bfd_contained_in (obfd); 262 } 263 264 /* 265 FUNCTION 266 bfd_set_archive_head 267 268 SYNOPSIS 269 bfd_boolean bfd_set_archive_head (bfd *output, bfd *new_head); 270 271 DESCRIPTION 272 Set the head of the chain of 273 BFDs contained in the archive @var{output} to @var{new_head}. 274 */ 275 276 bfd_boolean 277 bfd_set_archive_head (bfd *output_archive, bfd *new_head) 278 { 279 output_archive->archive_head = new_head; 280 return TRUE; 281 } 282 283 bfd * 284 _bfd_look_for_bfd_in_cache (bfd *arch_bfd, file_ptr filepos) 285 { 286 htab_t hash_table = bfd_ardata (arch_bfd)->cache; 287 struct ar_cache m; 288 m.ptr = filepos; 289 290 if (hash_table) 291 { 292 struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m); 293 if (!entry) 294 return NULL; 295 else 296 return entry->arbfd; 297 } 298 else 299 return NULL; 300 } 301 302 static hashval_t 303 hash_file_ptr (const PTR p) 304 { 305 return (hashval_t) (((struct ar_cache *) p)->ptr); 306 } 307 308 /* Returns non-zero if P1 and P2 are equal. */ 309 310 static int 311 eq_file_ptr (const PTR p1, const PTR p2) 312 { 313 struct ar_cache *arc1 = (struct ar_cache *) p1; 314 struct ar_cache *arc2 = (struct ar_cache *) p2; 315 return arc1->ptr == arc2->ptr; 316 } 317 318 /* Kind of stupid to call cons for each one, but we don't do too many. */ 319 320 bfd_boolean 321 _bfd_add_bfd_to_archive_cache (bfd *arch_bfd, file_ptr filepos, bfd *new_elt) 322 { 323 struct ar_cache *cache; 324 htab_t hash_table = bfd_ardata (arch_bfd)->cache; 325 326 /* If the hash table hasn't been created, create it. */ 327 if (hash_table == NULL) 328 { 329 hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr, 330 NULL, calloc, free); 331 if (hash_table == NULL) 332 return FALSE; 333 bfd_ardata (arch_bfd)->cache = hash_table; 334 } 335 336 /* Insert new_elt into the hash table by filepos. */ 337 cache = bfd_zalloc (arch_bfd, sizeof (struct ar_cache)); 338 cache->ptr = filepos; 339 cache->arbfd = new_elt; 340 *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache; 341 342 return TRUE; 343 } 344 345 /* The name begins with space. Hence the rest of the name is an index into 346 the string table. */ 347 348 static char * 349 get_extended_arelt_filename (bfd *arch, const char *name) 350 { 351 unsigned long index = 0; 352 353 /* Should extract string so that I can guarantee not to overflow into 354 the next region, but I'm too lazy. */ 355 errno = 0; 356 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */ 357 index = strtol (name + 1, NULL, 10); 358 if (errno != 0 || index >= bfd_ardata (arch)->extended_names_size) 359 { 360 bfd_set_error (bfd_error_malformed_archive); 361 return NULL; 362 } 363 364 return bfd_ardata (arch)->extended_names + index; 365 } 366 367 /* This functions reads an arch header and returns an areltdata pointer, or 368 NULL on error. 369 370 Presumes the file pointer is already in the right place (ie pointing 371 to the ar_hdr in the file). Moves the file pointer; on success it 372 should be pointing to the front of the file contents; on failure it 373 could have been moved arbitrarily. */ 374 375 void * 376 _bfd_generic_read_ar_hdr (bfd *abfd) 377 { 378 return _bfd_generic_read_ar_hdr_mag (abfd, NULL); 379 } 380 381 /* Alpha ECOFF uses an optional different ARFMAG value, so we have a 382 variant of _bfd_generic_read_ar_hdr which accepts a magic string. */ 383 384 void * 385 _bfd_generic_read_ar_hdr_mag (bfd *abfd, const char *mag) 386 { 387 struct ar_hdr hdr; 388 char *hdrp = (char *) &hdr; 389 size_t parsed_size; 390 struct areltdata *ared; 391 char *filename = NULL; 392 bfd_size_type namelen = 0; 393 bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr); 394 char *allocptr = 0; 395 396 if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr)) 397 { 398 if (bfd_get_error () != bfd_error_system_call) 399 bfd_set_error (bfd_error_no_more_archived_files); 400 return NULL; 401 } 402 if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0 403 && (mag == NULL 404 || strncmp (hdr.ar_fmag, mag, 2) != 0)) 405 { 406 bfd_set_error (bfd_error_malformed_archive); 407 return NULL; 408 } 409 410 errno = 0; 411 parsed_size = strtol (hdr.ar_size, NULL, 10); 412 if (errno != 0) 413 { 414 bfd_set_error (bfd_error_malformed_archive); 415 return NULL; 416 } 417 418 /* Extract the filename from the archive - there are two ways to 419 specify an extended name table, either the first char of the 420 name is a space, or it's a slash. */ 421 if ((hdr.ar_name[0] == '/' 422 || (hdr.ar_name[0] == ' ' 423 && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL)) 424 && bfd_ardata (abfd)->extended_names != NULL) 425 { 426 filename = get_extended_arelt_filename (abfd, hdr.ar_name); 427 if (filename == NULL) 428 return NULL; 429 } 430 /* BSD4.4-style long filename. 431 Only implemented for reading, so far! */ 432 else if (hdr.ar_name[0] == '#' 433 && hdr.ar_name[1] == '1' 434 && hdr.ar_name[2] == '/' 435 && ISDIGIT (hdr.ar_name[3])) 436 { 437 /* BSD-4.4 extended name */ 438 namelen = atoi (&hdr.ar_name[3]); 439 allocsize += namelen + 1; 440 parsed_size -= namelen; 441 442 allocptr = bfd_zalloc (abfd, allocsize); 443 if (allocptr == NULL) 444 return NULL; 445 filename = (allocptr 446 + sizeof (struct areltdata) 447 + sizeof (struct ar_hdr)); 448 if (bfd_bread (filename, namelen, abfd) != namelen) 449 { 450 if (bfd_get_error () != bfd_error_system_call) 451 bfd_set_error (bfd_error_no_more_archived_files); 452 return NULL; 453 } 454 filename[namelen] = '\0'; 455 } 456 else 457 { 458 /* We judge the end of the name by looking for '/' or ' '. 459 Note: The SYSV format (terminated by '/') allows embedded 460 spaces, so only look for ' ' if we don't find '/'. */ 461 462 char *e; 463 e = memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd)); 464 if (e == NULL) 465 { 466 e = memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)); 467 if (e == NULL) 468 e = memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd)); 469 } 470 471 if (e != NULL) 472 namelen = e - hdr.ar_name; 473 else 474 { 475 /* If we didn't find a termination character, then the name 476 must be the entire field. */ 477 namelen = ar_maxnamelen (abfd); 478 } 479 480 allocsize += namelen + 1; 481 } 482 483 if (!allocptr) 484 { 485 allocptr = bfd_zalloc (abfd, allocsize); 486 if (allocptr == NULL) 487 return NULL; 488 } 489 490 ared = (struct areltdata *) allocptr; 491 492 ared->arch_header = allocptr + sizeof (struct areltdata); 493 memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr)); 494 ared->parsed_size = parsed_size; 495 496 if (filename != NULL) 497 ared->filename = filename; 498 else 499 { 500 ared->filename = allocptr + (sizeof (struct areltdata) + 501 sizeof (struct ar_hdr)); 502 if (namelen) 503 memcpy (ared->filename, hdr.ar_name, namelen); 504 ared->filename[namelen] = '\0'; 505 } 506 507 return ared; 508 } 509 510 /* This is an internal function; it's mainly used when indexing 511 through the archive symbol table, but also used to get the next 512 element, since it handles the bookkeeping so nicely for us. */ 513 514 bfd * 515 _bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos) 516 { 517 struct areltdata *new_areldata; 518 bfd *n_nfd; 519 520 if (archive->my_archive) 521 { 522 filepos += archive->origin; 523 archive = archive->my_archive; 524 } 525 526 n_nfd = _bfd_look_for_bfd_in_cache (archive, filepos); 527 if (n_nfd) 528 return n_nfd; 529 530 if (0 > bfd_seek (archive, filepos, SEEK_SET)) 531 return NULL; 532 533 if ((new_areldata = _bfd_read_ar_hdr (archive)) == NULL) 534 return NULL; 535 536 n_nfd = _bfd_create_empty_archive_element_shell (archive); 537 if (n_nfd == NULL) 538 { 539 bfd_release (archive, new_areldata); 540 return NULL; 541 } 542 543 n_nfd->origin = bfd_tell (archive); 544 n_nfd->arelt_data = new_areldata; 545 n_nfd->filename = new_areldata->filename; 546 547 if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_nfd)) 548 return n_nfd; 549 550 /* Huh? */ 551 bfd_release (archive, n_nfd); 552 bfd_release (archive, new_areldata); 553 return NULL; 554 } 555 556 /* Return the BFD which is referenced by the symbol in ABFD indexed by 557 INDEX. INDEX should have been returned by bfd_get_next_mapent. */ 558 559 bfd * 560 _bfd_generic_get_elt_at_index (bfd *abfd, symindex index) 561 { 562 carsym *entry; 563 564 entry = bfd_ardata (abfd)->symdefs + index; 565 return _bfd_get_elt_at_filepos (abfd, entry->file_offset); 566 } 567 568 /* 569 FUNCTION 570 bfd_openr_next_archived_file 571 572 SYNOPSIS 573 bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous); 574 575 DESCRIPTION 576 Provided a BFD, @var{archive}, containing an archive and NULL, open 577 an input BFD on the first contained element and returns that. 578 Subsequent calls should pass 579 the archive and the previous return value to return a created 580 BFD to the next contained element. NULL is returned when there 581 are no more. 582 */ 583 584 bfd * 585 bfd_openr_next_archived_file (bfd *archive, bfd *last_file) 586 { 587 if ((bfd_get_format (archive) != bfd_archive) || 588 (archive->direction == write_direction)) 589 { 590 bfd_set_error (bfd_error_invalid_operation); 591 return NULL; 592 } 593 594 return BFD_SEND (archive, 595 openr_next_archived_file, (archive, last_file)); 596 } 597 598 bfd * 599 bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file) 600 { 601 file_ptr filestart; 602 603 if (!last_file) 604 filestart = bfd_ardata (archive)->first_file_filepos; 605 else 606 { 607 unsigned int size = arelt_size (last_file); 608 filestart = last_file->origin + size; 609 if (archive->my_archive) 610 filestart -= archive->origin; 611 /* Pad to an even boundary... 612 Note that last_file->origin can be odd in the case of 613 BSD-4.4-style element with a long odd size. */ 614 filestart += filestart % 2; 615 } 616 617 return _bfd_get_elt_at_filepos (archive, filestart); 618 } 619 620 const bfd_target * 621 bfd_generic_archive_p (bfd *abfd) 622 { 623 struct artdata *tdata_hold; 624 char armag[SARMAG + 1]; 625 bfd_size_type amt; 626 627 if (bfd_bread (armag, SARMAG, abfd) != SARMAG) 628 { 629 if (bfd_get_error () != bfd_error_system_call) 630 bfd_set_error (bfd_error_wrong_format); 631 return NULL; 632 } 633 634 if (strncmp (armag, ARMAG, SARMAG) != 0 && 635 strncmp (armag, ARMAGB, SARMAG) != 0) 636 return 0; 637 638 tdata_hold = bfd_ardata (abfd); 639 640 amt = sizeof (struct artdata); 641 bfd_ardata (abfd) = bfd_zalloc (abfd, amt); 642 if (bfd_ardata (abfd) == NULL) 643 { 644 bfd_ardata (abfd) = tdata_hold; 645 return NULL; 646 } 647 648 bfd_ardata (abfd)->first_file_filepos = SARMAG; 649 /* Cleared by bfd_zalloc above. 650 bfd_ardata (abfd)->cache = NULL; 651 bfd_ardata (abfd)->archive_head = NULL; 652 bfd_ardata (abfd)->symdefs = NULL; 653 bfd_ardata (abfd)->extended_names = NULL; 654 bfd_ardata (abfd)->extended_names_size = 0; 655 bfd_ardata (abfd)->tdata = NULL; */ 656 657 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd)) 658 || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) 659 { 660 if (bfd_get_error () != bfd_error_system_call) 661 bfd_set_error (bfd_error_wrong_format); 662 bfd_release (abfd, bfd_ardata (abfd)); 663 bfd_ardata (abfd) = tdata_hold; 664 return NULL; 665 } 666 667 if (bfd_has_map (abfd)) 668 { 669 bfd *first; 670 671 /* This archive has a map, so we may presume that the contents 672 are object files. Make sure that if the first file in the 673 archive can be recognized as an object file, it is for this 674 target. If not, assume that this is the wrong format. If 675 the first file is not an object file, somebody is doing 676 something weird, and we permit it so that ar -t will work. 677 678 This is done because any normal format will recognize any 679 normal archive, regardless of the format of the object files. 680 We do accept an empty archive. */ 681 682 first = bfd_openr_next_archived_file (abfd, NULL); 683 if (first != NULL) 684 { 685 bfd_boolean fail; 686 687 first->target_defaulted = FALSE; 688 fail = FALSE; 689 if (bfd_check_format (first, bfd_object) 690 && first->xvec != abfd->xvec) 691 { 692 bfd_set_error (bfd_error_wrong_object_format); 693 bfd_ardata (abfd) = tdata_hold; 694 return NULL; 695 } 696 /* And we ought to close `first' here too. */ 697 } 698 } 699 700 return abfd->xvec; 701 } 702 703 /* Some constants for a 32 bit BSD archive structure. We do not 704 support 64 bit archives presently; so far as I know, none actually 705 exist. Supporting them would require changing these constants, and 706 changing some H_GET_32 to H_GET_64. */ 707 708 /* The size of an external symdef structure. */ 709 #define BSD_SYMDEF_SIZE 8 710 711 /* The offset from the start of a symdef structure to the file offset. */ 712 #define BSD_SYMDEF_OFFSET_SIZE 4 713 714 /* The size of the symdef count. */ 715 #define BSD_SYMDEF_COUNT_SIZE 4 716 717 /* The size of the string count. */ 718 #define BSD_STRING_COUNT_SIZE 4 719 720 /* Returns FALSE on error, TRUE otherwise. */ 721 722 static bfd_boolean 723 do_slurp_bsd_armap (bfd *abfd) 724 { 725 struct areltdata *mapdata; 726 unsigned int counter; 727 bfd_byte *raw_armap, *rbase; 728 struct artdata *ardata = bfd_ardata (abfd); 729 char *stringbase; 730 bfd_size_type parsed_size, amt; 731 carsym *set; 732 733 mapdata = _bfd_read_ar_hdr (abfd); 734 if (mapdata == NULL) 735 return FALSE; 736 parsed_size = mapdata->parsed_size; 737 bfd_release (abfd, mapdata); /* Don't need it any more. */ 738 739 raw_armap = bfd_zalloc (abfd, parsed_size); 740 if (raw_armap == NULL) 741 return FALSE; 742 743 if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size) 744 { 745 if (bfd_get_error () != bfd_error_system_call) 746 bfd_set_error (bfd_error_malformed_archive); 747 byebye: 748 bfd_release (abfd, raw_armap); 749 return FALSE; 750 } 751 752 ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE; 753 754 if (ardata->symdef_count * BSD_SYMDEF_SIZE > 755 parsed_size - BSD_SYMDEF_COUNT_SIZE) 756 { 757 /* Probably we're using the wrong byte ordering. */ 758 bfd_set_error (bfd_error_wrong_format); 759 goto byebye; 760 } 761 762 ardata->cache = 0; 763 rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE; 764 stringbase = ((char *) rbase 765 + ardata->symdef_count * BSD_SYMDEF_SIZE 766 + BSD_STRING_COUNT_SIZE); 767 amt = ardata->symdef_count * sizeof (carsym); 768 ardata->symdefs = bfd_alloc (abfd, amt); 769 if (!ardata->symdefs) 770 return FALSE; 771 772 for (counter = 0, set = ardata->symdefs; 773 counter < ardata->symdef_count; 774 counter++, set++, rbase += BSD_SYMDEF_SIZE) 775 { 776 set->name = H_GET_32 (abfd, rbase) + stringbase; 777 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); 778 } 779 780 ardata->first_file_filepos = bfd_tell (abfd); 781 /* Pad to an even boundary if you have to. */ 782 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 783 /* FIXME, we should provide some way to free raw_ardata when 784 we are done using the strings from it. For now, it seems 785 to be allocated on an objalloc anyway... */ 786 bfd_has_map (abfd) = TRUE; 787 return TRUE; 788 } 789 790 /* Returns FALSE on error, TRUE otherwise. */ 791 792 static bfd_boolean 793 do_slurp_coff_armap (bfd *abfd) 794 { 795 struct areltdata *mapdata; 796 int *raw_armap, *rawptr; 797 struct artdata *ardata = bfd_ardata (abfd); 798 char *stringbase; 799 bfd_size_type stringsize; 800 unsigned int parsed_size; 801 carsym *carsyms; 802 bfd_size_type nsymz; /* Number of symbols in armap. */ 803 bfd_vma (*swap) (const void *); 804 char int_buf[sizeof (long)]; 805 bfd_size_type carsym_size, ptrsize; 806 unsigned int i; 807 808 mapdata = _bfd_read_ar_hdr (abfd); 809 if (mapdata == NULL) 810 return FALSE; 811 parsed_size = mapdata->parsed_size; 812 bfd_release (abfd, mapdata); /* Don't need it any more. */ 813 814 if (bfd_bread (int_buf, 4, abfd) != 4) 815 { 816 if (bfd_get_error () != bfd_error_system_call) 817 bfd_set_error (bfd_error_malformed_archive); 818 return FALSE; 819 } 820 /* It seems that all numeric information in a coff archive is always 821 in big endian format, nomatter the host or target. */ 822 swap = bfd_getb32; 823 nsymz = bfd_getb32 (int_buf); 824 stringsize = parsed_size - (4 * nsymz) - 4; 825 826 /* ... except that some archive formats are broken, and it may be our 827 fault - the i960 little endian coff sometimes has big and sometimes 828 little, because our tools changed. Here's a horrible hack to clean 829 up the crap. */ 830 831 if (stringsize > 0xfffff 832 && bfd_get_arch (abfd) == bfd_arch_i960 833 && bfd_get_flavour (abfd) == bfd_target_coff_flavour) 834 { 835 /* This looks dangerous, let's do it the other way around. */ 836 nsymz = bfd_getl32 (int_buf); 837 stringsize = parsed_size - (4 * nsymz) - 4; 838 swap = bfd_getl32; 839 } 840 841 /* The coff armap must be read sequentially. So we construct a 842 bsd-style one in core all at once, for simplicity. */ 843 844 if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym)) 845 return FALSE; 846 847 carsym_size = (nsymz * sizeof (carsym)); 848 ptrsize = (4 * nsymz); 849 850 if (carsym_size + stringsize + 1 <= carsym_size) 851 return FALSE; 852 853 ardata->symdefs = bfd_zalloc (abfd, carsym_size + stringsize + 1); 854 if (ardata->symdefs == NULL) 855 return FALSE; 856 carsyms = ardata->symdefs; 857 stringbase = ((char *) ardata->symdefs) + carsym_size; 858 859 /* Allocate and read in the raw offsets. */ 860 raw_armap = bfd_alloc (abfd, ptrsize); 861 if (raw_armap == NULL) 862 goto release_symdefs; 863 if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize 864 || (bfd_bread (stringbase, stringsize, abfd) != stringsize)) 865 { 866 if (bfd_get_error () != bfd_error_system_call) 867 bfd_set_error (bfd_error_malformed_archive); 868 goto release_raw_armap; 869 } 870 871 /* OK, build the carsyms. */ 872 for (i = 0; i < nsymz; i++) 873 { 874 rawptr = raw_armap + i; 875 carsyms->file_offset = swap ((bfd_byte *) rawptr); 876 carsyms->name = stringbase; 877 stringbase += strlen (stringbase) + 1; 878 carsyms++; 879 } 880 *stringbase = 0; 881 882 ardata->symdef_count = nsymz; 883 ardata->first_file_filepos = bfd_tell (abfd); 884 /* Pad to an even boundary if you have to. */ 885 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 886 887 bfd_has_map (abfd) = TRUE; 888 bfd_release (abfd, raw_armap); 889 890 /* Check for a second archive header (as used by PE). */ 891 { 892 struct areltdata *tmp; 893 894 bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET); 895 tmp = _bfd_read_ar_hdr (abfd); 896 if (tmp != NULL) 897 { 898 if (tmp->arch_header[0] == '/' 899 && tmp->arch_header[1] == ' ') 900 { 901 ardata->first_file_filepos += 902 (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1; 903 } 904 bfd_release (abfd, tmp); 905 } 906 } 907 908 return TRUE; 909 910 release_raw_armap: 911 bfd_release (abfd, raw_armap); 912 release_symdefs: 913 bfd_release (abfd, (ardata)->symdefs); 914 return FALSE; 915 } 916 917 /* This routine can handle either coff-style or bsd-style armaps. 918 Returns FALSE on error, TRUE otherwise */ 919 920 bfd_boolean 921 bfd_slurp_armap (bfd *abfd) 922 { 923 char nextname[17]; 924 int i = bfd_bread (nextname, 16, abfd); 925 926 if (i == 0) 927 return TRUE; 928 if (i != 16) 929 return FALSE; 930 931 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 932 return FALSE; 933 934 if (!strncmp (nextname, "__.SYMDEF ", 16) 935 || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* old Linux archives */ 936 return do_slurp_bsd_armap (abfd); 937 else if (!strncmp (nextname, "/ ", 16)) 938 return do_slurp_coff_armap (abfd); 939 else if (!strncmp (nextname, "/SYM64/ ", 16)) 940 { 941 /* 64bit ELF (Irix 6) archive. */ 942 #ifdef BFD64 943 extern bfd_boolean bfd_elf64_archive_slurp_armap (bfd *); 944 return bfd_elf64_archive_slurp_armap (abfd); 945 #else 946 bfd_set_error (bfd_error_wrong_format); 947 return FALSE; 948 #endif 949 } 950 951 bfd_has_map (abfd) = FALSE; 952 return TRUE; 953 } 954 955 /* Returns FALSE on error, TRUE otherwise. */ 956 /* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the 957 header is in a slightly different order and the map name is '/'. 958 This flavour is used by hp300hpux. */ 959 960 #define HPUX_SYMDEF_COUNT_SIZE 2 961 962 bfd_boolean 963 bfd_slurp_bsd_armap_f2 (bfd *abfd) 964 { 965 struct areltdata *mapdata; 966 char nextname[17]; 967 unsigned int counter; 968 bfd_byte *raw_armap, *rbase; 969 struct artdata *ardata = bfd_ardata (abfd); 970 char *stringbase; 971 unsigned int stringsize; 972 bfd_size_type amt; 973 carsym *set; 974 int i = bfd_bread (nextname, 16, abfd); 975 976 if (i == 0) 977 return TRUE; 978 if (i != 16) 979 return FALSE; 980 981 /* The archive has at least 16 bytes in it. */ 982 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 983 return FALSE; 984 985 if (!strncmp (nextname, "__.SYMDEF ", 16) 986 || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* Old Linux archives. */ 987 return do_slurp_bsd_armap (abfd); 988 989 if (strncmp (nextname, "/ ", 16)) 990 { 991 bfd_has_map (abfd) = FALSE; 992 return TRUE; 993 } 994 995 mapdata = _bfd_read_ar_hdr (abfd); 996 if (mapdata == NULL) 997 return FALSE; 998 999 amt = mapdata->parsed_size; 1000 raw_armap = bfd_zalloc (abfd, amt); 1001 if (raw_armap == NULL) 1002 { 1003 byebye: 1004 bfd_release (abfd, mapdata); 1005 return FALSE; 1006 } 1007 1008 if (bfd_bread (raw_armap, amt, abfd) != amt) 1009 { 1010 if (bfd_get_error () != bfd_error_system_call) 1011 bfd_set_error (bfd_error_malformed_archive); 1012 byebyebye: 1013 bfd_release (abfd, raw_armap); 1014 goto byebye; 1015 } 1016 1017 ardata->symdef_count = H_GET_16 (abfd, raw_armap); 1018 1019 if (ardata->symdef_count * BSD_SYMDEF_SIZE 1020 > mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE) 1021 { 1022 /* Probably we're using the wrong byte ordering. */ 1023 bfd_set_error (bfd_error_wrong_format); 1024 goto byebyebye; 1025 } 1026 1027 ardata->cache = 0; 1028 1029 stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE); 1030 /* Skip sym count and string sz. */ 1031 stringbase = ((char *) raw_armap 1032 + HPUX_SYMDEF_COUNT_SIZE 1033 + BSD_STRING_COUNT_SIZE); 1034 rbase = (bfd_byte *) stringbase + stringsize; 1035 amt = ardata->symdef_count * BSD_SYMDEF_SIZE; 1036 ardata->symdefs = bfd_alloc (abfd, amt); 1037 if (!ardata->symdefs) 1038 return FALSE; 1039 1040 for (counter = 0, set = ardata->symdefs; 1041 counter < ardata->symdef_count; 1042 counter++, set++, rbase += BSD_SYMDEF_SIZE) 1043 { 1044 set->name = H_GET_32 (abfd, rbase) + stringbase; 1045 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); 1046 } 1047 1048 ardata->first_file_filepos = bfd_tell (abfd); 1049 /* Pad to an even boundary if you have to. */ 1050 ardata->first_file_filepos += (ardata->first_file_filepos) % 2; 1051 /* FIXME, we should provide some way to free raw_ardata when 1052 we are done using the strings from it. For now, it seems 1053 to be allocated on an objalloc anyway... */ 1054 bfd_has_map (abfd) = TRUE; 1055 return TRUE; 1056 } 1057 1058 /** Extended name table. 1059 1060 Normally archives support only 14-character filenames. 1061 1062 Intel has extended the format: longer names are stored in a special 1063 element (the first in the archive, or second if there is an armap); 1064 the name in the ar_hdr is replaced by <space><index into filename 1065 element>. Index is the P.R. of an int (decimal). Data General have 1066 extended the format by using the prefix // for the special element. */ 1067 1068 /* Returns FALSE on error, TRUE otherwise. */ 1069 1070 bfd_boolean 1071 _bfd_slurp_extended_name_table (bfd *abfd) 1072 { 1073 char nextname[17]; 1074 struct areltdata *namedata; 1075 bfd_size_type amt; 1076 1077 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ, 1078 we probably don't want to return TRUE. */ 1079 bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET); 1080 if (bfd_bread (nextname, 16, abfd) == 16) 1081 { 1082 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 1083 return FALSE; 1084 1085 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 && 1086 strncmp (nextname, "// ", 16) != 0) 1087 { 1088 bfd_ardata (abfd)->extended_names = NULL; 1089 bfd_ardata (abfd)->extended_names_size = 0; 1090 return TRUE; 1091 } 1092 1093 namedata = _bfd_read_ar_hdr (abfd); 1094 if (namedata == NULL) 1095 return FALSE; 1096 1097 amt = namedata->parsed_size; 1098 if (amt + 1 == 0) 1099 goto byebye; 1100 1101 bfd_ardata (abfd)->extended_names_size = amt; 1102 bfd_ardata (abfd)->extended_names = bfd_zalloc (abfd, amt + 1); 1103 if (bfd_ardata (abfd)->extended_names == NULL) 1104 { 1105 byebye: 1106 bfd_release (abfd, namedata); 1107 return FALSE; 1108 } 1109 1110 if (bfd_bread (bfd_ardata (abfd)->extended_names, amt, abfd) != amt) 1111 { 1112 if (bfd_get_error () != bfd_error_system_call) 1113 bfd_set_error (bfd_error_malformed_archive); 1114 bfd_release (abfd, (bfd_ardata (abfd)->extended_names)); 1115 bfd_ardata (abfd)->extended_names = NULL; 1116 goto byebye; 1117 } 1118 1119 /* Since the archive is supposed to be printable if it contains 1120 text, the entries in the list are newline-padded, not null 1121 padded. In SVR4-style archives, the names also have a 1122 trailing '/'. DOS/NT created archive often have \ in them 1123 We'll fix all problems here.. */ 1124 { 1125 char *ext_names = bfd_ardata (abfd)->extended_names; 1126 char *temp = ext_names; 1127 char *limit = temp + namedata->parsed_size; 1128 for (; temp < limit; ++temp) 1129 { 1130 if (*temp == '\012') 1131 temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0'; 1132 if (*temp == '\\') 1133 *temp = '/'; 1134 } 1135 *limit = '\0'; 1136 } 1137 1138 /* Pad to an even boundary if you have to. */ 1139 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd); 1140 bfd_ardata (abfd)->first_file_filepos += 1141 (bfd_ardata (abfd)->first_file_filepos) % 2; 1142 1143 /* FIXME, we can't release namedata here because it was allocated 1144 below extended_names on the objalloc... */ 1145 } 1146 return TRUE; 1147 } 1148 1149 #ifdef VMS 1150 1151 /* Return a copy of the stuff in the filename between any :]> and a 1152 semicolon. */ 1153 1154 static const char * 1155 normalize (bfd *abfd, const char *file) 1156 { 1157 const char *first; 1158 const char *last; 1159 char *copy; 1160 1161 first = file + strlen (file) - 1; 1162 last = first + 1; 1163 1164 while (first != file) 1165 { 1166 if (*first == ';') 1167 last = first; 1168 if (*first == ':' || *first == ']' || *first == '>') 1169 { 1170 first++; 1171 break; 1172 } 1173 first--; 1174 } 1175 1176 copy = bfd_alloc (abfd, last - first + 1); 1177 if (copy == NULL) 1178 return NULL; 1179 1180 memcpy (copy, first, last - first); 1181 copy[last - first] = 0; 1182 1183 return copy; 1184 } 1185 1186 #else 1187 static const char * 1188 normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file) 1189 { 1190 const char *filename = strrchr (file, '/'); 1191 1192 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 1193 { 1194 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ 1195 char *bslash = strrchr (file, '\\'); 1196 if (filename == NULL || (bslash != NULL && bslash > filename)) 1197 filename = bslash; 1198 if (filename == NULL && file[0] != '\0' && file[1] == ':') 1199 filename = file + 1; 1200 } 1201 #endif 1202 if (filename != NULL) 1203 filename++; 1204 else 1205 filename = file; 1206 return filename; 1207 } 1208 #endif 1209 1210 /* Build a BFD style extended name table. */ 1211 1212 bfd_boolean 1213 _bfd_archive_bsd_construct_extended_name_table (bfd *abfd, 1214 char **tabloc, 1215 bfd_size_type *tablen, 1216 const char **name) 1217 { 1218 *name = "ARFILENAMES/"; 1219 return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen); 1220 } 1221 1222 /* Build an SVR4 style extended name table. */ 1223 1224 bfd_boolean 1225 _bfd_archive_coff_construct_extended_name_table (bfd *abfd, 1226 char **tabloc, 1227 bfd_size_type *tablen, 1228 const char **name) 1229 { 1230 *name = "//"; 1231 return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen); 1232 } 1233 1234 /* Follows archive_head and produces an extended name table if 1235 necessary. Returns (in tabloc) a pointer to an extended name 1236 table, and in tablen the length of the table. If it makes an entry 1237 it clobbers the filename so that the element may be written without 1238 further massage. Returns TRUE if it ran successfully, FALSE if 1239 something went wrong. A successful return may still involve a 1240 zero-length tablen! */ 1241 1242 bfd_boolean 1243 _bfd_construct_extended_name_table (bfd *abfd, 1244 bfd_boolean trailing_slash, 1245 char **tabloc, 1246 bfd_size_type *tablen) 1247 { 1248 unsigned int maxname = abfd->xvec->ar_max_namelen; 1249 bfd_size_type total_namelen = 0; 1250 bfd *current; 1251 char *strptr; 1252 1253 *tablen = 0; 1254 1255 /* Figure out how long the table should be. */ 1256 for (current = abfd->archive_head; current != NULL; current = current->next) 1257 { 1258 const char *normal; 1259 unsigned int thislen; 1260 1261 normal = normalize (current, current->filename); 1262 if (normal == NULL) 1263 return FALSE; 1264 1265 thislen = strlen (normal); 1266 1267 if (thislen > maxname 1268 && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0) 1269 thislen = maxname; 1270 1271 if (thislen > maxname) 1272 { 1273 /* Add one to leave room for \n. */ 1274 total_namelen += thislen + 1; 1275 if (trailing_slash) 1276 { 1277 /* Leave room for trailing slash. */ 1278 ++total_namelen; 1279 } 1280 } 1281 else 1282 { 1283 struct ar_hdr *hdr = arch_hdr (current); 1284 if (strncmp (normal, hdr->ar_name, thislen) != 0 1285 || (thislen < sizeof hdr->ar_name 1286 && hdr->ar_name[thislen] != ar_padchar (current))) 1287 { 1288 /* Must have been using extended format even though it 1289 didn't need to. Fix it to use normal format. */ 1290 memcpy (hdr->ar_name, normal, thislen); 1291 if (thislen < maxname 1292 || (thislen == maxname && thislen < sizeof hdr->ar_name)) 1293 hdr->ar_name[thislen] = ar_padchar (current); 1294 } 1295 } 1296 } 1297 1298 if (total_namelen == 0) 1299 return TRUE; 1300 1301 *tabloc = bfd_zalloc (abfd, total_namelen); 1302 if (*tabloc == NULL) 1303 return FALSE; 1304 1305 *tablen = total_namelen; 1306 strptr = *tabloc; 1307 1308 for (current = abfd->archive_head; current != NULL; current = 1309 current->next) 1310 { 1311 const char *normal; 1312 unsigned int thislen; 1313 1314 normal = normalize (current, current->filename); 1315 if (normal == NULL) 1316 return FALSE; 1317 1318 thislen = strlen (normal); 1319 if (thislen > maxname) 1320 { 1321 /* Works for now; may need to be re-engineered if we 1322 encounter an oddball archive format and want to 1323 generalise this hack. */ 1324 struct ar_hdr *hdr = arch_hdr (current); 1325 strcpy (strptr, normal); 1326 if (! trailing_slash) 1327 strptr[thislen] = '\012'; 1328 else 1329 { 1330 strptr[thislen] = '/'; 1331 strptr[thislen + 1] = '\012'; 1332 } 1333 hdr->ar_name[0] = ar_padchar (current); 1334 _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", 1335 strptr - *tabloc); 1336 strptr += thislen + 1; 1337 if (trailing_slash) 1338 ++strptr; 1339 } 1340 } 1341 1342 return TRUE; 1343 } 1344 1345 /* A couple of functions for creating ar_hdrs. */ 1346 1347 #ifdef HPUX_LARGE_AR_IDS 1348 /* Function to encode large UID/GID values according to HP. */ 1349 1350 static void 1351 hpux_uid_gid_encode (char str[6], long int id) 1352 { 1353 int cnt; 1354 1355 str[5] = '@' + (id & 3); 1356 id >>= 2; 1357 1358 for (cnt = 4; cnt >= 0; ++cnt, id >>= 6) 1359 str[cnt] = ' ' + (id & 0x3f); 1360 } 1361 #endif /* HPUX_LARGE_AR_IDS */ 1362 1363 #ifndef HAVE_GETUID 1364 #define getuid() 0 1365 #endif 1366 1367 #ifndef HAVE_GETGID 1368 #define getgid() 0 1369 #endif 1370 1371 /* Takes a filename, returns an arelt_data for it, or NULL if it can't 1372 make one. The filename must refer to a filename in the filesystem. 1373 The filename field of the ar_hdr will NOT be initialized. If member 1374 is set, and it's an in-memory bfd, we fake it. */ 1375 1376 static struct areltdata * 1377 bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member) 1378 { 1379 struct stat status; 1380 struct areltdata *ared; 1381 struct ar_hdr *hdr; 1382 bfd_size_type amt; 1383 1384 if (member && (member->flags & BFD_IN_MEMORY) != 0) 1385 { 1386 /* Assume we just "made" the member, and fake it. */ 1387 struct bfd_in_memory *bim = member->iostream; 1388 time (&status.st_mtime); 1389 status.st_uid = getuid (); 1390 status.st_gid = getgid (); 1391 status.st_mode = 0644; 1392 status.st_size = bim->size; 1393 } 1394 else if (stat (filename, &status) != 0) 1395 { 1396 bfd_set_error (bfd_error_system_call); 1397 return NULL; 1398 } 1399 1400 amt = sizeof (struct ar_hdr) + sizeof (struct areltdata); 1401 ared = bfd_zalloc (abfd, amt); 1402 if (ared == NULL) 1403 return NULL; 1404 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata)); 1405 1406 /* ar headers are space padded, not null padded! */ 1407 memset (hdr, ' ', sizeof (struct ar_hdr)); 1408 1409 _bfd_ar_spacepadll (hdr->ar_date, sizeof (hdr->ar_date), "%-12lld", 1410 status.st_mtime); 1411 #ifdef HPUX_LARGE_AR_IDS 1412 /* HP has a very "special" way to handle UID/GID's with numeric values 1413 > 99999. */ 1414 if (status.st_uid > 99999) 1415 hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_uid); 1416 else 1417 #endif 1418 _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld", 1419 status.st_uid); 1420 #ifdef HPUX_LARGE_AR_IDS 1421 /* HP has a very "special" way to handle UID/GID's with numeric values 1422 > 99999. */ 1423 if (status.st_gid > 99999) 1424 hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_gid); 1425 else 1426 #endif 1427 _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld", 1428 status.st_gid); 1429 _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo", 1430 status.st_mode); 1431 _bfd_ar_spacepadll (hdr->ar_size, sizeof (hdr->ar_size), "%-10lld", 1432 status.st_size); 1433 memcpy (hdr->ar_fmag, ARFMAG, 2); 1434 ared->parsed_size = status.st_size; 1435 ared->arch_header = (char *) hdr; 1436 1437 return ared; 1438 } 1439 1440 /* This is magic required by the "ar" program. Since it's 1441 undocumented, it's undocumented. You may think that it would take 1442 a strong stomach to write this, and it does, but it takes even a 1443 stronger stomach to try to code around such a thing! */ 1444 1445 struct ar_hdr *bfd_special_undocumented_glue (bfd *, const char *); 1446 1447 struct ar_hdr * 1448 bfd_special_undocumented_glue (bfd *abfd, const char *filename) 1449 { 1450 struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename, 0); 1451 if (ar_elt == NULL) 1452 return NULL; 1453 return (struct ar_hdr *) ar_elt->arch_header; 1454 } 1455 1456 /* Analogous to stat call. */ 1457 1458 int 1459 bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf) 1460 { 1461 struct ar_hdr *hdr; 1462 char *aloser; 1463 1464 if (abfd->arelt_data == NULL) 1465 { 1466 bfd_set_error (bfd_error_invalid_operation); 1467 return -1; 1468 } 1469 1470 hdr = arch_hdr (abfd); 1471 1472 #define foo(arelt, stelt, size) \ 1473 buf->stelt = strtol (hdr->arelt, &aloser, size); \ 1474 if (aloser == hdr->arelt) \ 1475 return -1; 1476 1477 #define fooll(arelt, stelt, size) \ 1478 buf->stelt = strtoll (hdr->arelt, &aloser, size); \ 1479 if (aloser == hdr->arelt) \ 1480 return -1; 1481 1482 /* Some platforms support special notations for large IDs. */ 1483 #ifdef HPUX_LARGE_AR_IDS 1484 # define foo2(arelt, stelt, size) \ 1485 if (hdr->arelt[5] == ' ') \ 1486 { \ 1487 foo (arelt, stelt, size); \ 1488 } \ 1489 else \ 1490 { \ 1491 int cnt; \ 1492 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \ 1493 { \ 1494 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \ 1495 return -1; \ 1496 buf->stelt <<= 6; \ 1497 buf->stelt += hdr->arelt[cnt] - ' '; \ 1498 } \ 1499 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \ 1500 return -1; \ 1501 buf->stelt <<= 2; \ 1502 buf->stelt += hdr->arelt[5] - '@'; \ 1503 } 1504 #else 1505 # define foo2(arelt, stelt, size) foo (arelt, stelt, size) 1506 #endif 1507 1508 fooll (ar_date, st_mtime, 10); 1509 foo2 (ar_uid, st_uid, 10); 1510 foo2 (ar_gid, st_gid, 10); 1511 foo (ar_mode, st_mode, 8); 1512 1513 buf->st_size = arch_eltdata (abfd)->parsed_size; 1514 1515 return 0; 1516 } 1517 1518 void 1519 bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr) 1520 { 1521 /* FIXME: This interacts unpleasantly with ar's quick-append option. 1522 Fortunately ic960 users will never use that option. Fixing this 1523 is very hard; fortunately I know how to do it and will do so once 1524 intel's release is out the door. */ 1525 1526 struct ar_hdr *hdr = (struct ar_hdr *) arhdr; 1527 size_t length; 1528 const char *filename; 1529 size_t maxlen = ar_maxnamelen (abfd); 1530 1531 if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0) 1532 { 1533 bfd_bsd_truncate_arname (abfd, pathname, arhdr); 1534 return; 1535 } 1536 1537 filename = normalize (abfd, pathname); 1538 if (filename == NULL) 1539 { 1540 /* FIXME */ 1541 abort (); 1542 } 1543 1544 length = strlen (filename); 1545 1546 if (length <= maxlen) 1547 memcpy (hdr->ar_name, filename, length); 1548 1549 /* Add the padding character if there is room for it. */ 1550 if (length < maxlen 1551 || (length == maxlen && length < sizeof hdr->ar_name)) 1552 (hdr->ar_name)[length] = ar_padchar (abfd); 1553 } 1554 1555 void 1556 bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr) 1557 { 1558 struct ar_hdr *hdr = (struct ar_hdr *) arhdr; 1559 size_t length; 1560 const char *filename = strrchr (pathname, '/'); 1561 size_t maxlen = ar_maxnamelen (abfd); 1562 1563 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 1564 { 1565 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ 1566 char *bslash = strrchr (pathname, '\\'); 1567 if (filename == NULL || (bslash != NULL && bslash > filename)) 1568 filename = bslash; 1569 if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':') 1570 filename = pathname + 1; 1571 } 1572 #endif 1573 1574 if (filename == NULL) 1575 filename = pathname; 1576 else 1577 ++filename; 1578 1579 length = strlen (filename); 1580 1581 if (length <= maxlen) 1582 memcpy (hdr->ar_name, filename, length); 1583 else 1584 { 1585 /* pathname: meet procrustes */ 1586 memcpy (hdr->ar_name, filename, maxlen); 1587 length = maxlen; 1588 } 1589 1590 if (length < maxlen) 1591 (hdr->ar_name)[length] = ar_padchar (abfd); 1592 } 1593 1594 /* Store name into ar header. Truncates the name to fit. 1595 1> strip pathname to be just the basename. 1596 2> if it's short enuf to fit, stuff it in. 1597 3> If it doesn't end with .o, truncate it to fit 1598 4> truncate it before the .o, append .o, stuff THAT in. */ 1599 1600 /* This is what gnu ar does. It's better but incompatible with the 1601 bsd ar. */ 1602 1603 void 1604 bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr) 1605 { 1606 struct ar_hdr *hdr = (struct ar_hdr *) arhdr; 1607 size_t length; 1608 const char *filename = strrchr (pathname, '/'); 1609 size_t maxlen = ar_maxnamelen (abfd); 1610 1611 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 1612 { 1613 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ 1614 char *bslash = strrchr (pathname, '\\'); 1615 if (filename == NULL || (bslash != NULL && bslash > filename)) 1616 filename = bslash; 1617 if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':') 1618 filename = pathname + 1; 1619 } 1620 #endif 1621 1622 if (filename == NULL) 1623 filename = pathname; 1624 else 1625 ++filename; 1626 1627 length = strlen (filename); 1628 1629 if (length <= maxlen) 1630 memcpy (hdr->ar_name, filename, length); 1631 else 1632 { /* pathname: meet procrustes */ 1633 memcpy (hdr->ar_name, filename, maxlen); 1634 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) 1635 { 1636 hdr->ar_name[maxlen - 2] = '.'; 1637 hdr->ar_name[maxlen - 1] = 'o'; 1638 } 1639 length = maxlen; 1640 } 1641 1642 if (length < 16) 1643 (hdr->ar_name)[length] = ar_padchar (abfd); 1644 } 1645 1646 /* The BFD is open for write and has its format set to bfd_archive. */ 1647 1648 bfd_boolean 1649 _bfd_write_archive_contents (bfd *arch) 1650 { 1651 bfd *current; 1652 char *etable = NULL; 1653 bfd_size_type elength = 0; 1654 const char *ename = NULL; 1655 bfd_boolean makemap = bfd_has_map (arch); 1656 /* If no .o's, don't bother to make a map. */ 1657 bfd_boolean hasobjects = FALSE; 1658 bfd_size_type wrote; 1659 int tries; 1660 1661 /* Verify the viability of all entries; if any of them live in the 1662 filesystem (as opposed to living in an archive open for input) 1663 then construct a fresh ar_hdr for them. */ 1664 for (current = arch->archive_head; current; current = current->next) 1665 { 1666 /* This check is checking the bfds for the objects we're reading 1667 from (which are usually either an object file or archive on 1668 disk), not the archive entries we're writing to. We don't 1669 actually create bfds for the archive members, we just copy 1670 them byte-wise when we write out the archive. */ 1671 if (bfd_write_p (current)) 1672 { 1673 bfd_set_error (bfd_error_invalid_operation); 1674 return FALSE; 1675 } 1676 if (!current->arelt_data) 1677 { 1678 current->arelt_data = 1679 bfd_ar_hdr_from_filesystem (arch, current->filename, current); 1680 if (!current->arelt_data) 1681 return FALSE; 1682 1683 /* Put in the file name. */ 1684 BFD_SEND (arch, _bfd_truncate_arname, 1685 (arch, current->filename, (char *) arch_hdr (current))); 1686 } 1687 1688 if (makemap && ! hasobjects) 1689 { /* Don't bother if we won't make a map! */ 1690 if ((bfd_check_format (current, bfd_object))) 1691 hasobjects = TRUE; 1692 } 1693 } 1694 1695 if (!BFD_SEND (arch, _bfd_construct_extended_name_table, 1696 (arch, &etable, &elength, &ename))) 1697 return FALSE; 1698 1699 if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0) 1700 return FALSE; 1701 wrote = bfd_bwrite (ARMAG, SARMAG, arch); 1702 if (wrote != SARMAG) 1703 return FALSE; 1704 1705 if (makemap && hasobjects) 1706 { 1707 if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength)) 1708 return FALSE; 1709 } 1710 1711 if (elength != 0) 1712 { 1713 struct ar_hdr hdr; 1714 1715 memset (&hdr, ' ', sizeof (struct ar_hdr)); 1716 memcpy (hdr.ar_name, ename, strlen (ename)); 1717 /* Round size up to even number in archive header. */ 1718 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", 1719 (elength + 1) & ~(bfd_size_type) 1); 1720 memcpy (hdr.ar_fmag, ARFMAG, 2); 1721 if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 1722 != sizeof (struct ar_hdr)) 1723 || bfd_bwrite (etable, elength, arch) != elength) 1724 return FALSE; 1725 if ((elength % 2) == 1) 1726 { 1727 if (bfd_bwrite ("\012", 1, arch) != 1) 1728 return FALSE; 1729 } 1730 } 1731 1732 for (current = arch->archive_head; current; current = current->next) 1733 { 1734 char buffer[DEFAULT_BUFFERSIZE]; 1735 unsigned int remaining = arelt_size (current); 1736 struct ar_hdr *hdr = arch_hdr (current); 1737 1738 /* Write ar header. */ 1739 if (bfd_bwrite (hdr, sizeof (*hdr), arch) 1740 != sizeof (*hdr)) 1741 return FALSE; 1742 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0) 1743 return FALSE; 1744 while (remaining) 1745 { 1746 unsigned int amt = DEFAULT_BUFFERSIZE; 1747 if (amt > remaining) 1748 amt = remaining; 1749 errno = 0; 1750 if (bfd_bread (buffer, amt, current) != amt) 1751 { 1752 if (bfd_get_error () != bfd_error_system_call) 1753 bfd_set_error (bfd_error_malformed_archive); 1754 return FALSE; 1755 } 1756 if (bfd_bwrite (buffer, amt, arch) != amt) 1757 return FALSE; 1758 remaining -= amt; 1759 } 1760 if ((arelt_size (current) % 2) == 1) 1761 { 1762 if (bfd_bwrite ("\012", 1, arch) != 1) 1763 return FALSE; 1764 } 1765 } 1766 1767 if (makemap && hasobjects) 1768 { 1769 /* Verify the timestamp in the archive file. If it would not be 1770 accepted by the linker, rewrite it until it would be. If 1771 anything odd happens, break out and just return. (The 1772 Berkeley linker checks the timestamp and refuses to read the 1773 table-of-contents if it is >60 seconds less than the file's 1774 modified-time. That painful hack requires this painful hack. */ 1775 tries = 1; 1776 do 1777 { 1778 if (bfd_update_armap_timestamp (arch)) 1779 break; 1780 (*_bfd_error_handler) 1781 (_("Warning: writing archive was slow: rewriting timestamp\n")); 1782 } 1783 while (++tries < 6); 1784 } 1785 1786 return TRUE; 1787 } 1788 1789 /* Note that the namidx for the first symbol is 0. */ 1790 1791 bfd_boolean 1792 _bfd_compute_and_write_armap (bfd *arch, unsigned int elength) 1793 { 1794 char *first_name = NULL; 1795 bfd *current; 1796 file_ptr elt_no = 0; 1797 struct orl *map = NULL; 1798 unsigned int orl_max = 1024; /* Fine initial default. */ 1799 unsigned int orl_count = 0; 1800 int stridx = 0; 1801 asymbol **syms = NULL; 1802 long syms_max = 0; 1803 bfd_boolean ret; 1804 bfd_size_type amt; 1805 1806 /* Dunno if this is the best place for this info... */ 1807 if (elength != 0) 1808 elength += sizeof (struct ar_hdr); 1809 elength += elength % 2; 1810 1811 amt = orl_max * sizeof (struct orl); 1812 map = bfd_malloc (amt); 1813 if (map == NULL) 1814 goto error_return; 1815 1816 /* We put the symbol names on the arch objalloc, and then discard 1817 them when done. */ 1818 first_name = bfd_alloc (arch, 1); 1819 if (first_name == NULL) 1820 goto error_return; 1821 1822 /* Drop all the files called __.SYMDEF, we're going to make our own. */ 1823 while (arch->archive_head && 1824 strcmp (arch->archive_head->filename, "__.SYMDEF") == 0) 1825 arch->archive_head = arch->archive_head->next; 1826 1827 /* Map over each element. */ 1828 for (current = arch->archive_head; 1829 current != NULL; 1830 current = current->next, elt_no++) 1831 { 1832 if (bfd_check_format (current, bfd_object) 1833 && (bfd_get_file_flags (current) & HAS_SYMS) != 0) 1834 { 1835 long storage; 1836 long symcount; 1837 long src_count; 1838 1839 storage = bfd_get_symtab_upper_bound (current); 1840 if (storage < 0) 1841 goto error_return; 1842 1843 if (storage != 0) 1844 { 1845 if (storage > syms_max) 1846 { 1847 if (syms_max > 0) 1848 free (syms); 1849 syms_max = storage; 1850 syms = bfd_malloc (syms_max); 1851 if (syms == NULL) 1852 goto error_return; 1853 } 1854 symcount = bfd_canonicalize_symtab (current, syms); 1855 if (symcount < 0) 1856 goto error_return; 1857 1858 /* Now map over all the symbols, picking out the ones we 1859 want. */ 1860 for (src_count = 0; src_count < symcount; src_count++) 1861 { 1862 flagword flags = (syms[src_count])->flags; 1863 asection *sec = syms[src_count]->section; 1864 1865 if ((flags & BSF_GLOBAL || 1866 flags & BSF_WEAK || 1867 flags & BSF_INDIRECT || 1868 bfd_is_com_section (sec)) 1869 && ! bfd_is_und_section (sec)) 1870 { 1871 bfd_size_type namelen; 1872 struct orl *new_map; 1873 1874 /* This symbol will go into the archive header. */ 1875 if (orl_count == orl_max) 1876 { 1877 orl_max *= 2; 1878 amt = orl_max * sizeof (struct orl); 1879 new_map = bfd_realloc (map, amt); 1880 if (new_map == NULL) 1881 goto error_return; 1882 1883 map = new_map; 1884 } 1885 1886 namelen = strlen (syms[src_count]->name); 1887 amt = sizeof (char *); 1888 map[orl_count].name = bfd_alloc (arch, amt); 1889 if (map[orl_count].name == NULL) 1890 goto error_return; 1891 *(map[orl_count].name) = bfd_alloc (arch, namelen + 1); 1892 if (*(map[orl_count].name) == NULL) 1893 goto error_return; 1894 strcpy (*(map[orl_count].name), syms[src_count]->name); 1895 map[orl_count].u.abfd = current; 1896 map[orl_count].namidx = stridx; 1897 1898 stridx += namelen + 1; 1899 ++orl_count; 1900 } 1901 } 1902 } 1903 1904 /* Now ask the BFD to free up any cached information, so we 1905 don't fill all of memory with symbol tables. */ 1906 if (! bfd_free_cached_info (current)) 1907 goto error_return; 1908 } 1909 } 1910 1911 /* OK, now we have collected all the data, let's write them out. */ 1912 ret = BFD_SEND (arch, write_armap, 1913 (arch, elength, map, orl_count, stridx)); 1914 1915 if (syms_max > 0) 1916 free (syms); 1917 if (map != NULL) 1918 free (map); 1919 if (first_name != NULL) 1920 bfd_release (arch, first_name); 1921 1922 return ret; 1923 1924 error_return: 1925 if (syms_max > 0) 1926 free (syms); 1927 if (map != NULL) 1928 free (map); 1929 if (first_name != NULL) 1930 bfd_release (arch, first_name); 1931 1932 return FALSE; 1933 } 1934 1935 bfd_boolean 1936 bsd_write_armap (bfd *arch, 1937 unsigned int elength, 1938 struct orl *map, 1939 unsigned int orl_count, 1940 int stridx) 1941 { 1942 int padit = stridx & 1; 1943 unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE; 1944 unsigned int stringsize = stridx + padit; 1945 /* Include 8 bytes to store ranlibsize and stringsize in output. */ 1946 unsigned int mapsize = ranlibsize + stringsize + 8; 1947 file_ptr firstreal; 1948 bfd *current = arch->archive_head; 1949 bfd *last_elt = current; /* Last element arch seen. */ 1950 bfd_byte temp[4]; 1951 unsigned int count; 1952 struct ar_hdr hdr; 1953 struct stat statbuf; 1954 1955 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG; 1956 1957 stat (arch->filename, &statbuf); 1958 memset (&hdr, ' ', sizeof (struct ar_hdr)); 1959 memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG)); 1960 /* Remember the timestamp, to keep it holy. But fudge it a little. */ 1961 bfd_ardata (arch)->armap_timestamp = statbuf.st_mtime + ARMAP_TIME_OFFSET; 1962 bfd_ardata (arch)->armap_datepos = (SARMAG 1963 + offsetof (struct ar_hdr, ar_date[0])); 1964 _bfd_ar_spacepadll (hdr.ar_date, sizeof (hdr.ar_date), "%lld", 1965 bfd_ardata (arch)->armap_timestamp); 1966 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", getuid ()); 1967 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", getgid ()); 1968 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize); 1969 memcpy (hdr.ar_fmag, ARFMAG, 2); 1970 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 1971 != sizeof (struct ar_hdr)) 1972 return FALSE; 1973 H_PUT_32 (arch, ranlibsize, temp); 1974 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp)) 1975 return FALSE; 1976 1977 for (count = 0; count < orl_count; count++) 1978 { 1979 bfd_byte buf[BSD_SYMDEF_SIZE]; 1980 1981 if (map[count].u.abfd != last_elt) 1982 { 1983 do 1984 { 1985 firstreal += arelt_size (current) + sizeof (struct ar_hdr); 1986 firstreal += firstreal % 2; 1987 current = current->next; 1988 } 1989 while (current != map[count].u.abfd); 1990 } 1991 1992 last_elt = current; 1993 H_PUT_32 (arch, map[count].namidx, buf); 1994 H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE); 1995 if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch) 1996 != BSD_SYMDEF_SIZE) 1997 return FALSE; 1998 } 1999 2000 /* Now write the strings themselves. */ 2001 H_PUT_32 (arch, stringsize, temp); 2002 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp)) 2003 return FALSE; 2004 for (count = 0; count < orl_count; count++) 2005 { 2006 size_t len = strlen (*map[count].name) + 1; 2007 2008 if (bfd_bwrite (*map[count].name, len, arch) != len) 2009 return FALSE; 2010 } 2011 2012 /* The spec sez this should be a newline. But in order to be 2013 bug-compatible for sun's ar we use a null. */ 2014 if (padit) 2015 { 2016 if (bfd_bwrite ("", 1, arch) != 1) 2017 return FALSE; 2018 } 2019 2020 return TRUE; 2021 } 2022 2023 /* At the end of archive file handling, update the timestamp in the 2024 file, so the linker will accept it. 2025 2026 Return TRUE if the timestamp was OK, or an unusual problem happened. 2027 Return FALSE if we updated the timestamp. */ 2028 2029 bfd_boolean 2030 _bfd_archive_bsd_update_armap_timestamp (bfd *arch) 2031 { 2032 struct stat archstat; 2033 struct ar_hdr hdr; 2034 2035 /* Flush writes, get last-write timestamp from file, and compare it 2036 to the timestamp IN the file. */ 2037 bfd_flush (arch); 2038 if (bfd_stat (arch, &archstat) == -1) 2039 { 2040 bfd_perror (_("Reading archive file mod timestamp")); 2041 2042 /* Can't read mod time for some reason. */ 2043 return TRUE; 2044 } 2045 if (archstat.st_mtime <= bfd_ardata (arch)->armap_timestamp) 2046 /* OK by the linker's rules. */ 2047 return TRUE; 2048 2049 /* Update the timestamp. */ 2050 bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET; 2051 2052 /* Prepare an ASCII version suitable for writing. */ 2053 memset (hdr.ar_date, ' ', sizeof (hdr.ar_date)); 2054 _bfd_ar_spacepadll (hdr.ar_date, sizeof (hdr.ar_date), "%lld", 2055 bfd_ardata (arch)->armap_timestamp); 2056 2057 /* Write it into the file. */ 2058 bfd_ardata (arch)->armap_datepos = (SARMAG 2059 + offsetof (struct ar_hdr, ar_date[0])); 2060 if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0 2061 || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch) 2062 != sizeof (hdr.ar_date))) 2063 { 2064 bfd_perror (_("Writing updated armap timestamp")); 2065 2066 /* Some error while writing. */ 2067 return TRUE; 2068 } 2069 2070 /* We updated the timestamp successfully. */ 2071 return FALSE; 2072 } 2073 2074 /* A coff armap looks like : 2075 lARMAG 2076 struct ar_hdr with name = '/' 2077 number of symbols 2078 offset of file for symbol 0 2079 offset of file for symbol 1 2080 2081 offset of file for symbol n-1 2082 symbol name 0 2083 symbol name 1 2084 2085 symbol name n-1 */ 2086 2087 bfd_boolean 2088 coff_write_armap (bfd *arch, 2089 unsigned int elength, 2090 struct orl *map, 2091 unsigned int symbol_count, 2092 int stridx) 2093 { 2094 /* The size of the ranlib is the number of exported symbols in the 2095 archive * the number of bytes in an int, + an int for the count. */ 2096 unsigned int ranlibsize = (symbol_count * 4) + 4; 2097 unsigned int stringsize = stridx; 2098 unsigned int mapsize = stringsize + ranlibsize; 2099 unsigned int archive_member_file_ptr; 2100 bfd *current = arch->archive_head; 2101 unsigned int count; 2102 struct ar_hdr hdr; 2103 int padit = mapsize & 1; 2104 2105 if (padit) 2106 mapsize++; 2107 2108 /* Work out where the first object file will go in the archive. */ 2109 archive_member_file_ptr = (mapsize 2110 + elength 2111 + sizeof (struct ar_hdr) 2112 + SARMAG); 2113 2114 memset (&hdr, ' ', sizeof (struct ar_hdr)); 2115 hdr.ar_name[0] = '/'; 2116 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", 2117 mapsize); 2118 _bfd_ar_spacepadll (hdr.ar_date, sizeof (hdr.ar_date), "%lld", 2119 time (NULL)); 2120 /* This, at least, is what Intel coff sets the values to. */ 2121 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0); 2122 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0); 2123 _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0); 2124 memcpy (hdr.ar_fmag, ARFMAG, 2); 2125 2126 /* Write the ar header for this item and the number of symbols. */ 2127 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch) 2128 != sizeof (struct ar_hdr)) 2129 return FALSE; 2130 2131 if (!bfd_write_bigendian_4byte_int (arch, symbol_count)) 2132 return FALSE; 2133 2134 /* Two passes, first write the file offsets for each symbol - 2135 remembering that each offset is on a two byte boundary. */ 2136 2137 /* Write out the file offset for the file associated with each 2138 symbol, and remember to keep the offsets padded out. */ 2139 2140 current = arch->archive_head; 2141 count = 0; 2142 while (current != NULL && count < symbol_count) 2143 { 2144 /* For each symbol which is used defined in this object, write 2145 out the object file's address in the archive. */ 2146 2147 while (count < symbol_count && map[count].u.abfd == current) 2148 { 2149 if (!bfd_write_bigendian_4byte_int (arch, archive_member_file_ptr)) 2150 return FALSE; 2151 count++; 2152 } 2153 /* Add size of this archive entry. */ 2154 archive_member_file_ptr += arelt_size (current) + sizeof (struct ar_hdr); 2155 /* Remember aboout the even alignment. */ 2156 archive_member_file_ptr += archive_member_file_ptr % 2; 2157 current = current->next; 2158 } 2159 2160 /* Now write the strings themselves. */ 2161 for (count = 0; count < symbol_count; count++) 2162 { 2163 size_t len = strlen (*map[count].name) + 1; 2164 2165 if (bfd_bwrite (*map[count].name, len, arch) != len) 2166 return FALSE; 2167 } 2168 2169 /* The spec sez this should be a newline. But in order to be 2170 bug-compatible for arc960 we use a null. */ 2171 if (padit) 2172 { 2173 if (bfd_bwrite ("", 1, arch) != 1) 2174 return FALSE; 2175 } 2176 2177 return TRUE; 2178 } 2179