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