home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / bfd / format.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  7KB  |  253 lines

  1. /* Generic BFD support for file formats.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  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. SECTION
  23.     File formats
  24.  
  25.     A format is a BFD concept of high level file contents type. The
  26.     formats supported by BFD are: 
  27.  
  28.     o <<bfd_object>>
  29.  
  30.     The BFD may contain data, symbols, relocations and debug info.
  31.  
  32.     o <<bfd_archive>>
  33.  
  34.     The BFD contains other BFDs and an optional index.
  35.  
  36.     o <<bfd_core>>
  37.  
  38.     The BFD contains the result of an executable core dump.
  39.  
  40.  
  41. */
  42.  
  43. #include "bfd.h"
  44. #include "sysdep.h"
  45. #include "libbfd.h"
  46.  
  47. /*
  48. FUNCTION
  49.     bfd_check_format
  50.  
  51. SYNOPSIS
  52.     boolean bfd_check_format(bfd *abfd, bfd_format format);
  53.  
  54. DESCRIPTION
  55.     Verify if the file attached to the BFD @var{abfd} is compatible
  56.     with the format @var{format} (i.e., one of <<bfd_object>>,
  57.     <<bfd_archive>> or <<bfd_core>>).
  58.  
  59.     If the BFD has been set to a specific target before the
  60.     call, only the named target and format combination is
  61.     checked. If the target has not been set, or has been set to
  62.     <<default>>, then all the known target backends is
  63.     interrogated to determine a match.  If the default target
  64.     matches, it is used.  If not, exactly one target must recognize
  65.     the file, or an error results.
  66.  
  67.     The function returns <<true>> on success, otherwise <<false>>
  68.     with one of the following error codes:  
  69.  
  70.     o <<invalid_operation>> -
  71.     if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
  72.     <<bfd_core>>.
  73.  
  74.     o <<system_call_error>> -
  75.     if an error occured during a read - even some file mismatches
  76.     can cause system_call_errors.
  77.  
  78.     o <<file_not_recognised>> -
  79.     none of the backends recognised the file format.
  80.  
  81.     o <<file_ambiguously_recognized>> -
  82.     more than one backend recognised the file format.
  83.  
  84. */
  85.  
  86. boolean
  87. DEFUN(bfd_check_format,(abfd, format),
  88.       bfd *abfd AND
  89.       bfd_format format)
  90. {
  91.   bfd_target **target, *save_targ, *right_targ;
  92.   int match_count;
  93.  
  94.   if (!bfd_read_p (abfd) ||
  95.       ((int)(abfd->format) < (int)bfd_unknown) ||
  96.       ((int)(abfd->format) >= (int)bfd_type_end)) {
  97.     bfd_error = invalid_operation;
  98.     return false;
  99.   }
  100.  
  101.   if (abfd->format != bfd_unknown)
  102.     return (abfd->format == format)? true: false;
  103.  
  104.  
  105.   /* Since the target type was defaulted, check them 
  106.      all in the hope that one will be uniquely recognized.  */
  107.  
  108.   save_targ = abfd->xvec;
  109.   match_count = 0;
  110.   right_targ = 0;
  111.  
  112.  
  113.   /* presume the answer is yes */
  114.   abfd->format = format;
  115.  
  116.   /* If the target type was explicitly specified, just check that target.  */
  117.  
  118.   if (!abfd->target_defaulted) {
  119.     bfd_seek (abfd, (file_ptr)0, SEEK_SET);    /* rewind! */
  120.  
  121.     right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
  122.     if (right_targ) {
  123.       abfd->xvec = right_targ;        /* Set the target as returned */
  124.       return true;            /* File position has moved, BTW */
  125.     }
  126.   }
  127.  
  128.   for (target = target_vector; *target != NULL; target++) {
  129.     bfd_target *temp;
  130.  
  131.     abfd->xvec = *target;    /* Change BFD's target temporarily */
  132.     bfd_seek (abfd, (file_ptr)0, SEEK_SET);
  133.     /* If _bfd_check_format neglects to set bfd_error, assume wrong_format.
  134.        We didn't used to even pay any attention to bfd_error, so I suspect
  135.        that some _bfd_check_format might have this problem.  */
  136.     bfd_error = wrong_format;
  137.     temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
  138.     if (temp) {                /* This format checks out as ok! */
  139.       right_targ = temp;
  140.       match_count++;
  141.       /* If this is the default target, accept it, even if other targets
  142.      might match.  People who want those other targets have to set 
  143.      the GNUTARGET variable.  */
  144.       if (temp == default_vector[0])
  145.     {
  146.       match_count = 1;
  147.       break;
  148.     }
  149. #ifdef GNU960
  150.       /* Big- and little-endian b.out archives look the same, but it doesn't
  151.        * matter: there is no difference in their headers, and member file byte
  152.        * orders will (I hope) be handled appropriately by bfd.  Ditto for big
  153.        * and little coff archives.  And the 4 coff/b.out object formats are
  154.        * unambiguous.  So accept the first match we find.
  155.        */
  156.       break;
  157. #endif
  158.     } else if (bfd_error != wrong_format) {
  159.       abfd->xvec = save_targ;
  160.       abfd->format = bfd_unknown;
  161.       return false;
  162.     }
  163.   }
  164.  
  165.   if (match_count == 1) {
  166.     abfd->xvec = right_targ;        /* Change BFD's target permanently */
  167.     return true;            /* File position has moved, BTW */
  168.   }
  169.  
  170.   abfd->xvec = save_targ;        /* Restore original target type */
  171.   abfd->format = bfd_unknown;        /* Restore original format */
  172.   bfd_error = ((match_count == 0) ? file_not_recognized :
  173.            file_ambiguously_recognized);
  174.   return false;
  175. }
  176.  
  177.  
  178. /*
  179. FUNCTION
  180.     bfd_set_format
  181.  
  182. SYNOPSIS
  183.     boolean bfd_set_format(bfd *abfd, bfd_format format);
  184.  
  185. DESCRIPTION
  186.     This function sets the file format of the BFD @var{abfd} to the
  187.     format @var{format}. If the target set in the BFD does not
  188.     support the format requested, the format is invalid, or the BFD
  189.     is not open for writing, then an error occurs.
  190.  
  191. */
  192.  
  193. boolean
  194. DEFUN(bfd_set_format,(abfd, format),
  195.       bfd *abfd AND
  196.       bfd_format format)
  197. {
  198.  
  199.   if (bfd_read_p (abfd) ||
  200.       ((int)abfd->format < (int)bfd_unknown) ||
  201.       ((int)abfd->format >= (int)bfd_type_end)) {
  202.     bfd_error = invalid_operation;
  203.     return false;
  204.   }
  205.  
  206.   if (abfd->format != bfd_unknown)
  207.     return (abfd->format == format) ? true:false;
  208.  
  209.   /* presume the answer is yes */
  210.   abfd->format = format;
  211.  
  212.   if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) {
  213.     abfd->format = bfd_unknown;
  214.     return false;
  215.   }
  216.  
  217.   return true;
  218. }
  219.  
  220.  
  221. /*
  222. FUNCTION
  223.     bfd_format_string
  224.  
  225. SYNOPSIS
  226.     CONST char *bfd_format_string(bfd_format format);
  227.  
  228. DESCRIPTION
  229.     Return a pointer to a const string
  230.     <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
  231.     depending upon the value of @var{format}.
  232. */
  233.  
  234. CONST char *
  235. DEFUN(bfd_format_string,(format),
  236.      bfd_format format)
  237. {
  238.   if (((int)format <(int) bfd_unknown) 
  239.       || ((int)format >=(int) bfd_type_end)) 
  240.     return "invalid";
  241.   
  242.   switch (format) {
  243.   case bfd_object:
  244.     return "object";        /* linker/assember/compiler output */
  245.   case bfd_archive: 
  246.     return "archive";        /* object archive file */
  247.   case bfd_core: 
  248.     return "core";        /* core dump */
  249.   default: 
  250.     return "unknown";
  251.   }
  252. }
  253.