Subversion Repositories Kolibri OS

Rev

Rev 5197 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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