home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / binutils.2 / bfd / archive.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  46.8 KB  |  1,673 lines

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