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