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