home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / archive.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  59KB  |  2,095 lines

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